-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreadth-First-Search.cpp
147 lines (127 loc) · 3.07 KB
/
Breadth-First-Search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream.h>
#include <conio.h>
#include <timer.h>
#include <stdlib.h>
/*--------------------------------
The nodes are numbered from 1 to last...don't enter from 0..always start from 1
--------------------------------*/
typedef enum bool {false = 0,true} boolean;
#define MAX 20
class Graph
{
int edges[MAX][MAX],*visited,nodes,sides,*connect,x;
void bfs(int);
public:
Graph()
{
sides = nodes = 0;
x = -1;
}
void input();
void traverse();
boolean isConnected();
void print();
void randomize();
};
void Graph :: input()
{
while(1)
{
cout << "\nGive the number of nodes : ";cin >> nodes;
if(nodes >= MAX) cout << "\nGive the nodes lesser than 20\n";
else break;
}
cout << "\nGive the number of edges : ";cin >> sides;
visited = new int [nodes];connect = new int [nodes];
for(int i = 0;i < nodes;i++) visited[i] = false;
for(int e = 0;e < nodes;e++)
for(int j = 0;j < nodes;j++) edges[e][j] = false;
cout << "\nNow give the edges one-by-one\n\n";
while(sides--)
{
int start,end;
cout << "From : ";cin >> start;
cout << "To : ";cin >> end;cout << "\n";
edges[start - 1][end - 1] = 1;edges[end - 1][start - 1] = 1;
}
}
boolean Graph :: isConnected()
{
//do it only after traverse
for(int f = 0;f < nodes;f++) visited[f] = false;
visited[0] = true;
for(int i = 1;i < nodes;i++)
if(edges[0][i] && !visited[i])
{
visited[i] = true;
bfs(i);
}
for(int w = 0;w < nodes;w++)
if(!visited[w]) return false;
return true;
}
void Graph :: traverse()
{
for(int i = 0;i < nodes;i++)
if(!visited[i])
{
visited[i] = true;
connect[++x] = i + 1;
bfs(i);
}
}
void Graph :: bfs(int source)
{
for(int i = 0;i < nodes;i++)
if(edges[source][i] && !visited[i])
{
visited[i] = true;
connect[++x] = i + 1;
}
}
void Graph :: print()
{
cout << "\n[ " << connect[0];
for(int i = 1;i < nodes;i++) cout << " , " << connect[i];
cout << " ]\n";
x = -1;
}
void Graph :: randomize()
{
Timer t;
cout << "\nNow comes the data to be tabulated\n";
for(int i = 1;i < MAX;i++)
{
nodes = i;
visited = new int [nodes];connect = new int [nodes];
for(int c = 0;c < nodes;c++) visited[c] = false;
for(int j = 0;j < nodes;j++)
for(int k = 0;k < nodes;k++)
edges[j][k] = -1;
for(int row = 0;row < nodes;row++)
for(int col = 0;col < nodes;col++)
{
if(edges[row][col] != -1) continue;
int tmp = rand() % 2;
edges[row][col] = edges[col][row] = tmp;
}
t.start();traverse();t.stop();
cout << "For nodes : " << nodes << " time taken is : " << t.time();
cout << " seconds\n";t.reset();
}
}
int main()
{
Graph G;
Timer t;
clrscr();
G.input();t.reset();
t.start();G.traverse();t.stop();
if(G.isConnected() == false) cout << "\nThe graph is DIS-connected\n";
else cout << "\nThe graph is connected\n";
G.print();
cout << "\nTime taken for this operation is : "<< t.time() << " seconds\n";
G.randomize();
getch();
return 0;
}