-
Notifications
You must be signed in to change notification settings - Fork 3
/
BFS.cpp
84 lines (72 loc) · 1.45 KB
/
BFS.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
// Implement Breadth First Search (BFS) Algorithm
// Enter pair of vertices for an Edge (v1,v2) or (v2,v1))
// Don't I/P disconnected graph, Otherwise the result will not be as expected.
// Choose vertices as integers 0,1,2,..., SIZE-1 in any order.
// All arrays should be initialized to 0 for correct result(Some compiler implicitly initialized all the arrays to 0, Some doesn't).
#include<iostream>
#define SIZE 7
#define SOURCE 1
void enqueue(int);
int dequeue();
void BFS();
int queue[SIZE], visited[SIZE], bfs_order[SIZE], k = 0, front = 0, rear = 0, m;
int graph[SIZE][SIZE];
using namespace std;
int main()
{
int t, v1, v2;
do
{
cout << "\n[1] Enter an Edge of the graph";
cout << "\n[0] Exit and compute BFS order\n";
cin >> t;
if(t == 1)
{
cout << "\nEnter 1st Vertex of an Edge:\n";
cin >> v1;
cout << "\nEnter 2n Vertex of an Edge:\n";
cin >> v2;
graph[v1][v2] = 1;
graph[v2][v1] = 1;
}
}while(t != 0);
enqueue(SOURCE);
visited[SOURCE] = 1;
while(front != rear)
{
BFS();
}
cout << "\nOne of the BFS order :\n";
for(int i = 0; i < SIZE ;i++)
{
cout << bfs_order[i];
}
cin >> m;
}
void enqueue(int i)
{
queue[rear] = i;
rear = rear + 1;
}
int dequeue()
{
int t = queue[front];
queue[front] = 0;
front = front + 1;
return t;
}
void BFS()
{
int t;
t = dequeue();
bfs_order[k] = t;
k++;
for(int i = 0; i < SIZE; i++)
{
if(graph[t][i] == 1 && visited[i] != 1)
{
enqueue(i);
visited[i] = 1;
}
}
}