National University
Of Computer & Emerging Sciences Faisalabad-Chiniot Campus
National University of Computer and Emerging
Sciences
Department of Computer Sciences
Zhalay Munir 21F-9618
Section B
Q6: Write a program to test whether two simple graphs are isomorphic by using the
Brute-force algorithm.
OUTPUT:
CODE:
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX_VERTICES 100
class Graph {
int V;
int adj[MAX_VERTICES][MAX_VERTICES];
public:
Graph(int V) {
this->V = V;
for (int i = 0; i < V; ++i) {
for (int j = 0; j < V; ++j) {
adj[i][j] = 0;
}
}
}
void addEdge(int u, int v) {
adj[u][v] = 1;
adj[v][u] = 1;
}
bool isIsomorphic(Graph other) {
//if graphs have different number of vertices then they can't be isomorphic
if (V != other.V) return false;
int deg[MAX_VERTICES];
int other_deg[MAX_VERTICES];
//initialize degree arrays
for (int i = 0; i < V; ++i) {
deg[i] = 0;
other_deg[i] = 0;
}
//calculate degrees for this graph
for (int i = 0; i < V; ++i) {
for (int j = 0; j < V; ++j) {
deg[i] += adj[i][j];
}
}
//calculate degrees for the other graph
for (int i = 0; i < other.V; ++i) {
for (int j = 0; j < other.V; ++j) {
other_deg[i] += other.adj[i][j];
}
}
sort(deg, deg + V);
sort(other_deg, other_deg + other.V);
//check if the sorted degree arrays are equal
for (int i = 0; i < V; ++i) {
if (deg[i] != other_deg[i]) return false;
}
return canBeMapped(0, 0, other);
}
private:
bool canBeMapped(int u, int v, Graph other) {
if (u == V && v == other.V) return true;
for (int i = 0; i < V; ++i) {
if (adj[u][i] && other.adj[v][i] && canBeMapped(u + 1, v + 1, other)) {
return true;
}
}
return false;
}
};
int main() {
Graph g1(8);
Graph g2(8);
g1.addEdge(0, 1);
g1.addEdge(0, 3);
g1.addEdge(0, 4);
g1.addEdge(1, 3);
g1.addEdge(2, 3);
g1.addEdge(2, 6);
g1.addEdge(4, 5);
g1.addEdge(4, 7);
g1.addEdge(5, 6);
g1.addEdge(6, 7);
g1.addEdge(0, 1);
g1.addEdge(0, 3);
g1.addEdge(0, 4);
g1.addEdge(1, 3);
g1.addEdge(1, 5);
g1.addEdge(2, 3);
g1.addEdge(4, 5);
g1.addEdge(4, 7);
g1.addEdge(5, 6);
g1.addEdge(6, 7);
// g1.addEdge(0, 1);
// g1.addEdge(1, 2);
// g1.addEdge(2, 3);
// g1.addEdge(3, 0);
// g2.addEdge(0, 2);
// g2.addEdge(2, 1);
// g2.addEdge(1, 3);
// g2.addEdge(3, 0);
if (g1.isIsomorphic(g2)) {
cout << "Graphs are isomorphic." << endl;
} else {
cout << "Graphs are not isomorphic." << endl;
}
return 0;
}
Questions 7
Output:
Code:
#include<iostream>
#define MAX_SIZE 100
using namespace std;
class Graph {
private:
int V;
int A[MAX_SIZE][MAX_SIZE];
public:
Graph(int num_vertices) {
this->V = num_vertices;
for (int i = 0; i < V; ++i) {
for (int j = 0; j < V; ++j) {
A[i][j] = 0;
}
}
}
void add_edge(int u, int v) {
A[u][v] = 1;
A[v][u] = 1;
}
int theorm() {
int total_degree = 0;
for (int i = 0; i < V; ++i) {
int vertex_degree = 0;
for (int j = 0; j < V; ++j) {
vertex_degree += A[i][j];
}
total_degree += vertex_degree;
}
return total_degree / 2;
}
};
int main() {
int num_vertices = 5;
Graph graph(num_vertices);
graph.add_edge(0, 1);
graph.add_edge(0, 2);
graph.add_edge(1, 2);
graph.add_edge(1, 3);
graph.add_edge(2, 3);
graph.add_edge(3, 4);
int num_edges = graph.theorm();
cout << "Number of edges in the graph: " << num_edges << endl;
return 0;
}
Algorithm:
1. Adjacency matrix A that represents graph G.
2. Initialize the variables:
3. num_vertices to store the number of vertices in G ,total_degree to store the total
degree of all vertices in graph, edge_count to store the number of edges in the graph
4. Calculate the number of vertices in the graph
5. Set Num vertices to the number of columns in graph.
6. Initialize a loop to iterate over each vertex vi in the graph.
7. Initialize vertex degree to 0 to store the degree of vi .
8. Initialize an inner loop to iterate over each adjacent vertex vj of vi.
9. If the entry A[i][j] in the adjacency matrix A is 1, increment vertex_degree by 1.
10. After the inner loop, add vertex_degree to total_degree to accumulate the degrees
of all vertices in the graph.
11. According to the Handshaking Theorem, the total degree of all vertices in an
undirected graph is equal to twice the number of edges. Therefore, to find the
number of edges EE in GG:
12. Set edge_count to total_degree / 2.
13. Output the value of edge_count as the number of edges in
Q8: Given a graph G and its adjacency matrix A, describe an algorithm to find if
G is acyclic (contains no
cycles).
Algorithm :
1. Input an Adjacency matrix A that represents the graph “G”.
2. Initialize an array visited of size equal to the number of vertices in G to keep track of
visited vertices and initially, set all elements of visited to false to indicate that we have
not visited them yet.
3. Initialize a loop to iterate over each vertex vi in the graph for each vertex vi. If the
visited element is false, then call a DFS function to explore the connected component
starting from vertex vi.
4. In the DFS function DFS
Mark vertex u as visited by setting visited[u] to true.
Initialize an inner loop to iterate over each adjacent vertex v.
If the entry A[u][v] in the adjacency matrix A is 1:
If visited[v] is false, recursively call DFS with v as the current vertex and u as its
parent vertex.
If visited[v] is true and v is not equal to the parent of u (v is already visited but not the
parent of u), then there exists a cycle in the graph. Return false.
5. If the DFS traversal completes without finding any cycles, return true, indicating that
the graph is acyclic.
QUESTION 9
OUTPUT:
CODE:
#include <iostream>
#define MAX_SIZE 100
using namespace std;
class Graph {
private:
int V;
int adjacency_matrix[MAX_SIZE][MAX_SIZE];
public:
Graph(int V) {
this->V = V;
for (int i = 0; i < V; ++i) {
for (int j = 0; j < V; ++j) {
adjacency_matrix[i][j] = 0;
}
}
}
void add_edge(int u, int v) {
adjacency_matrix[u][v] = 1;
}
int count_walks(int start, int end, int length) {
int walks[MAX_SIZE][MAX_SIZE];
for (int i = 0; i < V; ++i) {
for (int j = 0; j < V; ++j) {
walks[i][j] = adjacency_matrix[i][j];
}
}
for (int step = 1; step < length; ++step) {
int temp[MAX_SIZE][MAX_SIZE];
for (int i = 0; i < V; ++i) {
for (int j = 0; j < V; ++j) {
temp[i][j] = 0;
for (int k = 0; k < V; ++k) {
temp[i][j] += walks[i][k] * adjacency_matrix[k][j];
}
}
}
for (int i = 0; i < V; ++i) {
for (int j = 0; j < V; ++j) {
walks[i][j] = temp[i][j];
}
}
}
return walks[start][end];
}
};
int main() {
int V = 8;
Graph g(V);
g.add_edge(0, 1);
g.add_edge(0, 3);
g.add_edge(0, 4);
g.add_edge(0, 7);
g.add_edge(1, 2);
g.add_edge(1, 5);
g.add_edge(1, 6);
g.add_edge(2, 5);
g.add_edge(3, 4);
g.add_edge(4, 5);
g.add_edge(4, 7);
g.add_edge(5, 6);
int num_walks = 1; // number of walks
int walk_length = 2; // length of walks
int start_vertex = 0; // starting vertex
int end_vertex = 4; // goal vertex
int walks_count = g.count_walks(start_vertex, end_vertex, walk_length);
cout << "Number of walks of length " << walk_length << " from vertex " << start_vertex << " to
vertex " << end_vertex << ": " << walks_count << endl;
return 0;
Question 10