Documente Academic
Documente Profesional
Documente Cultură
#include <conio.h>
int c = 0, t = 0;
struct node_info {
int no;
int st_time;
struct node {
node_info * pt;
node * next;
np = new node;
np - > pt = ptr;
if (front == NULL) {
} else {
rear = np;
node_info * remove() {
if (front == NULL) {
} else {
p = front;
1
x = p - > pt;
delete(p);
return (x);
if (c == 0) {
q = new node_info;
q - > no = i;
cout << "time of visitation for node " << q - > no << ":" << q - > st_time << "\n\n";
v[i] = 1;
push(q);
c++;
continue;
r = new node_info;
r - > no = j;
cout << "time of visitation for node " << r - > no << ":" << r - > st_time << "\n\n";
v[j] = 1;
push(r);
remove();
2
}
int main() {
v[i] = 0;
cout << "enter the values for adjacency matrix row:" << i + 1 << endl;
getch();
Output
enter the values for adjacency matrix row:1
3
0
4
1
5
2. Program to Traverse a Graph using DFS
#include <iostream>
#include <conio.h>
int c = 0;
struct node {
char data;
}* p = NULL, * r = NULL;
struct stack {
node * pt;
stack * next;
np = new stack;
np - > pt = ptr;
if (top == NULL) {
top = np;
} else {
top = np;
node * pop() {
if (top == NULL) {
} else {
q = top;
6
delete(q);
c++;
p = new node;
p - > st_time = c;
cout << "start time for " << p - > data << " is " << c << endl;
a[i] = 1;
push(p);
while (j < 7) {
j++;
create(a, b, j, 0);
r = pop();
c++;
cout << "leave time for " << r - > data << " is " << c << endl;
return;
int main() {
int a[7];
a[i] = 0;
int b[7][7];
7
cout << "enter values for adjacency matrix" << endl;
cout << "enter values for " << (i + 1) << " row" << endl;
create(a, b, 0, 0);
getch();
Output:
enter values for adjacency matrix
8
0
9
0
node popped
10
g
node popped
node popped
node popped
node popped
node popped
node popped
11
3. Program to Check the Connectivity of Undirected Graph using BFS
#include <iostream>
#include <list>
#include <queue>
/*
* Class Declaration
*/
class Graph {
private:
int V;
public:
Graph(int V) {
this - > V = V;
Graph getTranspose();
bool isConnected();
};
/*
*/
adj[v].push_back(w);
adj[w].push_back(v);
12
/*
*/
visited[s] = true;
q.push_back(s);
while (!q.empty()) {
s = q.front();
q.pop_front();
if (!visited[ * i]) {
visited[ * i] = true;
q.push_back( * i);
/*
*/
Graph Graph::getTranspose() {
Graph g(V);
g.adj[ * i].push_back(v);
return g;
13
}
/*
*/
bool Graph::isConnected() {
bool visited[V];
visited[i] = false;
BFS(0, visited);
if (visited[i] == false)
return false;
Graph gr = getTranspose();
visited[i] = false;
gr.BFS(0, visited);
if (visited[i] == false)
return false;
return true;
/*
*/
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 3);
g.addEdge(3, 3);
14
if (g.isConnected())
else
Graph g1(5);
g1.addEdge(0, 1);
g1.addEdge(1, 2);
g1.addEdge(2, 3);
g1.addEdge(3, 0);
g1.addEdge(2, 4);
g1.addEdge(4, 2);
if (g1.isConnected())
else
return 0;
Output
$ g++ undirected_connected_bfs.cpp
$ a.out
------------------
15
4. Program to Check the Connectivity of directed Graph using BFS
#include <iostream>
#include <list>
#include <queue>
/*
* Class Declaration
*/
class Graph {
private:
int V;
public:
Graph(int V) {
this - > V = V;
Graph getTranspose();
bool isConnected();
};
/*
*/
adj[v].push_back(w);
//adj[w].push_back(v);
/*
16
*/
visited[s] = true;
q.push_back(s);
while (!q.empty()) {
s = q.front();
q.pop_front();
if (!visited[ * i]) {
visited[ * i] = true;
q.push_back( * i);
/*
*/
Graph Graph::getTranspose() {
Graph g(V);
g.adj[ * i].push_back(v);
return g;
/*
17
* Check if Graph is Connected
*/
bool Graph::isConnected() {
bool visited[V];
visited[i] = false;
BFS(0, visited);
if (visited[i] == false)
return false;
Graph gr = getTranspose();
visited[i] = false;
gr.BFS(0, visited);
if (visited[i] == false)
return false;
return true;
/*
*/
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 3);
g.addEdge(3, 3);
if (g.isConnected())
18
else
Graph g1(5);
g1.addEdge(0, 1);
g1.addEdge(1, 2);
g1.addEdge(2, 3);
g1.addEdge(3, 0);
g1.addEdge(2, 4);
g1.addEdge(4, 2);
if (g1.isConnected())
else
return 0;
Output
$ g++ ConnectivityUsingBFSDiredctedGraph.cpp
$ a.out
------------------
19
5. Program to Check the connectivity of Undirected Graph using DFS
#include <iostream>
#include <list>
#include <stack>
/*
* Class Declaration
*/
class Graph {
private:
int V;
public:
Graph(int V) {
this - > V = V;
}~Graph() {
delete[] adj;
bool isConnected();
Graph getTranspose();
};
/*
*/
visited[v] = true;
20
list < int > ::iterator i;
if (!visited[ * i])
DFSUtil( * i, visited);
/*
*/
Graph Graph::getTranspose() {
Graph g(V);
g.adj[ * i].push_back(v);
return g;
/*
*/
adj[v].push_back(w);
adj[w].push_back(v);
/*
*/
bool Graph::isConnected() {
21
bool visited[V];
visited[i] = false;
DFSUtil(0, visited);
if (visited[i] == false)
return false;
Graph gr = getTranspose();
visited[i] = false;
gr.DFSUtil(0, visited);
if (visited[i] == false)
return false;
return true;
/*
*/
int main() {
Graph g1(5);
g1.addEdge(0, 1);
g1.addEdge(1, 2);
g1.addEdge(2, 3);
g1.addEdge(3, 0);
g1.addEdge(2, 4);
g1.addEdge(4, 2);
if (g1.isConnected())
else
22
Graph g2(4);
g2.addEdge(0, 1);
g2.addEdge(1, 2);
g2.addEdge(2, 3);
if (g2.isConnected())
else
return 0;
Output
$ g++ undirected_connected_dfs.cpp
$ a.out
------------------
23
6. Program to Check the connectivity of Directed Graph using DFS
#include <iostream>
#include <list>
#include <stack>
/*
* Class Declaration
*/
class Graph {
private:
int V;
public:
Graph(int V) {
this - > V = V;
}~Graph() {
delete[] adj;
bool isConnected();
Graph getTranspose();
};
/*
*/
visited[v] = true;
24
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[ * i])
DFSUtil( * i, visited);
/*
*/
Graph Graph::getTranspose() {
Graph g(V);
g.adj[ * i].push_back(v);
return g;
/*
*/
adj[v].push_back(w);
/*
*/
bool Graph::isConnected() {
bool visited[V];
visited[i] = false;
DFSUtil(0, visited);
25
for (int i = 0; i < V; i++)
if (visited[i] == false)
return false;
Graph gr = getTranspose();
visited[i] = false;
gr.DFSUtil(0, visited);
if (visited[i] == false)
return false;
return true;
/*
*/
int main() {
Graph g1(5);
g1.addEdge(0, 1);
g1.addEdge(1, 2);
g1.addEdge(2, 3);
g1.addEdge(3, 0);
g1.addEdge(2, 4);
g1.addEdge(4, 2);
if (g1.isConnected())
else
Graph g2(4);
g2.addEdge(0, 1);
g2.addEdge(1, 2);
26
g2.addEdge(2, 3);
if (g2.isConnected())
else
return 0;
Output
$ g++ directed_connected_dfs.cpp
$ a.out
------------------
27
7. Program to Find MST(Minimum Spanning Tree) using Kruskal's
Algorithm
#include<iostream>
#include<conio.h>
struct node_info {
int no;
}* q = NULL, * r = NULL;
struct node {
node_info * pt;
node * next;
np = new node;
np - > pt = ptr;
if (top == NULL) {
top = np;
} else {
top = np;
node_info * pop() {
if (top == NULL) {
} else {
p = top;
28
delete(p);
q = new node_info;
q - > no = i;
push(q);
v[i] = 1;
} else if (am[i][j] == 0)
continue;
continue;
else {
flag = -1;
r = pop();
return (flag);
void init() {
v[i] = 0;
pop();
29
cout << "/n/nEDGES CREATED AS FOLLOWS:-/n/n";
min = wm[i][j];
temp = i;
temp1 = j;
} else if (wm[i][j] == 0)
continue;
am[temp][temp1] = am[temp1][temp] = 1;
init();
am[temp][temp1] = am[temp1][temp] = 0;
flag = 0;
continue;
} else {
cout << "edge created between " << temp << " th node and " << temp1 << " th node" << endl;
ve++;
int main() {
v[i] = 0;
30
for (int j = 0; j < 7; j++) {
am[i][j] = 0;
cout << "enter the values for weight matrix row:" << i + 1 << endl;
kruskals(am, wm);
getch();
Output
enter the values for weight matrix row:1
31
enter the values for weight matrix row:3
32
1
33
8. Program to find MST(Minimum Spanning Tree) using Prim's Algorithm
#include <iostream>
#include <conio.h>
struct node {
p[6];
a[i] = 1;
while (c < 6) {
if (a[i] == 1) {
j++;
min = b[i][j];
temp = i;
temp1 = j;
a[temp1] = 1;
p[c].fr = temp;
p[c].to = temp1;
p[c].cost = min;
c++;
34
b[temp][temp1] = b[temp1][temp] = 1000;
int main() {
int a[7];
a[i] = 0;
int b[7][7];
cout << "enter values for " << (i + 1) << " row" << endl;
prims(a, b, 0, 0);
getch();
Output
enter values of adjacency matrix for a 7 node graph:
35
6
36
0
00
source node:0
destination node:1
weight of node3
source node:1
destination node:2
weight of node2
source node:2
37
destination node:3
weight of node1
source node:2
destination node:5
weight of node2
source node:5
destination node:6
weight of node1
source node:6
destination node:4
weight of node1
38
9. Program to Check Cycle in a Graph using Topological Sort
#include<iostream>
#include<conio.h>
struct node_info {
int no;
}* q = NULL;
struct node {
node_info * pt;
node * next;
struct node1 {
node1 * link;
node_info * pt1;
int c = 0;
np = new node;
np - > pt = ptr;
if (top == NULL) {
top = np;
} else {
top = np;
node_info * pop() {
if (top == NULL) {
39
cout << "underflow\n";
} else {
p = top;
delete(p);
if (c == 0) {
head = np1;
m = head;
c++;
} else {
m = head;
head = np1;
void remove(int x) {
m = head;
delete(m);
} else {
n = m;
40
m = m - > link;
delete(m);
flag = true;
q = new node_info;
q - > no = i;
q - > st_time = c;
push(q);
v[i] = 1;
continue;
c++;
c++;
q = pop();
q - > lv_time = c;
store(q);
return;
41
void topo1(int * v, int am[][5], int i) {
v[i] = 1;
remove(i);
continue;
return;
am[src][dest] = 1;
return;
int main() {
v[i] = 0;
am[i][j] = 0;
while (c < 5) {
addEdge(am, a, b);
c++;
42
}
v[i] = 0;
amt[j][i] = am[i][j];
if (head != NULL) {
if (flag == false) {
getch();
Output
Enter the source and destination
43
1
44
10. Program to Find the Shortest Path Between Two Vertices Using
Dijkstra’s Algorithm
#include <iostream>
#include <list>
class Graph {
list < int > * adj; // Pointer to an array containing adjacency lists
public:
};
Graph::Graph(int V) {
this - > V = V;
// Base case
if (s == d)
return true;
45
bool * visited = new bool[V];
visited[i] = false;
visited[s] = true;
queue.push_back(s);
while (!queue.empty()) {
s = queue.front();
queue.pop_front();
// and enqueue it
if ( * i == d)
return true;
if (!visited[ * i]) {
visited[ * i] = true;
queue.push_back( * i);
46
}
return false;
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
int u, v;
if (g.isReachable(u, v))
cout << "\nThere is a path from " << u << " to " << v;
else
cout << "\nThere is no path from " << u << " to " << v;
int temp;
temp = u;
u = v;
v = temp;
if (g.isReachable(u, v))
cout << "\nThere is a path from " << u << " to " << v;
else
cout << "\nThere is no path from " << u << " to " << v;
return 0;
47
Output
$ g++ PathBetweenNodes.cpp
$ a.out
13
23
------------------
48
11. Program to Find All Pairs Shortest Path using Floyd- warshall’s
Algorithm
#include <iostream>
#include <cstdlib>
int p[max][max];
/*
*/
int k, i, j;
p[i][j] = k;
/*
*/
int k = p[i][j];
if (k > 0) {
shortest(i, k);
49
cout << " " << k << " ";
shortest(k, j);
/*
*/
cout << "Path from " << i << " to " << j << ":";
shortest(i, j);
/*
*/
int main() {
int i, j;
};
allpairshort(a, 5);
findpath(a, 0, 4, 5);
return 0;
50
Output
$ g++ allpairshortestpath.cpp
$ a.out
Path from 0 to 4: 0 3 2 4
------------------
51
12. Program to find edge connectivity of a graph
#include<iostream>
#include <list>
#define NIL - 1
class Graph {
int parent[]);
public:
};
Graph::Graph(int V) {
this - > V = V;
adj[v].push_back(w);
int parent[]) {
52
static int time = 0;
visited[u] = true;
if (!visited[v]) {
parent[v] = u;
else if (v != parent[u])
53
}
// DFS based function to find all bridges. It uses recursive function bridgeUtil()
void Graph::bridge() {
parent[i] = NIL;
visited[i] = false;
if (visited[i] == false)
int main() {
Graph g1(5);
g1.addEdge(1, 0);
g1.addEdge(0, 2);
54
g1.addEdge(2, 1);
g1.addEdge(0, 3);
g1.addEdge(3, 4);
g1.bridge();
Graph g2(4);
g2.addEdge(0, 1);
g2.addEdge(1, 2);
g2.addEdge(2, 3);
g2.bridge();
Graph g3(7);
g3.addEdge(0, 1);
g3.addEdge(1, 2);
g3.addEdge(2, 0);
g3.addEdge(1, 3);
g3.addEdge(1, 4);
g3.addEdge(1, 6);
g3.addEdge(3, 5);
g3.addEdge(4, 5);
g3.bridge();
return 0;
55
Output
$ g++ EdgeConnectivity.cpp
$ a.out
34
03
23
12
01
16
------------------
56
13. Program to find vertex connectivity of a graph
#include<iostream>
#include <list>
#define NIL - 1
class Graph {
void APUtil(int v, bool visited[], int disc[], int low[], int parent[],
bool ap[]);
public:
};
Graph::Graph(int V) {
this - > V = V;
adj[v].push_back(w);
57
// parent[] --> Stores parent vertices in DFS tree
void Graph::APUtil(int u, bool visited[], int disc[], int low[], int parent[],
bool ap[]) {
int children = 0;
visited[u] = true;
if (!visited[v]) {
children++;
parent[v] = u;
58
low[u] = min(low[u], low[v]);
ap[u] = true;
// (2) If u is not root and low value of one of its child is more
ap[u] = true;
else if (v != parent[u])
void Graph::AP() {
59
parent[i] = NIL;
visited[i] = false;
ap[i] = false;
if (visited[i] == false)
if (ap[i] == true)
int main() {
Graph g1(5);
g1.addEdge(1, 0);
g1.addEdge(0, 2);
g1.addEdge(2, 1);
g1.addEdge(0, 3);
g1.addEdge(3, 4);
g1.AP();
Graph g2(4);
60
g2.addEdge(0, 1);
g2.addEdge(1, 2);
g2.addEdge(2, 3);
g2.AP();
Graph g3(7);
g3.addEdge(0, 1);
g3.addEdge(1, 2);
g3.addEdge(2, 0);
g3.addEdge(1, 3);
g3.addEdge(1, 4);
g3.addEdge(1, 6);
g3.addEdge(3, 5);
g3.addEdge(4, 5);
g3.AP();
return 0;
61
Output
$ g++ VertexConnectivity.cpp
$ a.out
03
12
------------------
62
14. Program to Find Number of Articulation points in a Graph
#include<iostream>
#include<conio.h>
int cnt = 0;
struct node_info {
int no;
}* q = NULL, * r = NULL;
struct node {
node_info * pt;
node * next;
np = new node;
np - > pt = ptr;
if (top == NULL) {
top = np;
} else {
top = np;
node_info * pop() {
if (top == NULL) {
} else {
p = top;
delete(p);
63
}
q = new node_info;
q - > no = i;
push(q);
v[i] = 1;
continue;
cnt++;
q = pop();
am[src][dest] = 1;
am[dest][src] = 1;
return;
int main() {
v[i] = 0;
am[i][j] = 0;
64
}
while (c < 9) {
addEdge(am, a, b);
c++;
amt[i][j] = am[i][j];
am[i][j] = 0;
am[j][i] = 0;
if (i < 6) {
} else {
if (cnt < 5) {
cout << endl << i << " is an articulation point" << endl;
cnt = 0;
am[i][j] = amt[i][j];
am[j][i] = amt[j][i];
v[j] = 0;
65
while (top != NULL) {
pop();
getch();
66
Output:
Enter the source and destination
0 is an articulation point
67
15. Program to Implement Ford–Fulkerson Algorithm
#include <iostream>
#include <string.h>
#include <queue>
bool visited[6];
memset(visited, 0, sizeof(visited));
q.push(s);
visited[s] = true;
parent[s] = -1;
while (!q.empty()) {
int u = q.front();
q.pop();
q.push(v);
parent[v] = u;
visited[v] = true;
int u, v;
int rGraph[6][6];
rGraph[u][v] = graph[u][v];
68
}
int parent[6];
int max_flow = 0;
for (v = t; v != s; v = parent[v]) {
u = parent[v];
for (v = t; v != s; v = parent[v]) {
u = parent[v];
rGraph[u][v] -= path_flow;
rGraph[v][u] += path_flow;
max_flow += path_flow;
return max_flow;
int main() {
{0, 0, 9, 0, 0, 20},
{0, 0, 0, 7, 0, 4},
{0, 0, 0, 0, 0, 0} };
cout << "The maximum possible flow is " << fordFulkerson(graph, 0, 5);
getch();
Output
The maximum possible flow is 23
69