-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.cpp
95 lines (82 loc) · 1.7 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
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
#include<cstdlib>
#include<queue>
#include<cstring>
using namespace std;
struct vertices
{
string color;
int dist;
int parent;
vector<int> adjList;
};
int n;
vector<int> prev;
struct vertices vert[8];
//Finding shortest path between source and destination
void bfs(int src, int dest)
{
for(int i=0;i<=7;i++)
{
if(i!=src)
{
vert[i].color="White";
vert[i].dist=9999;
vert[i].parent=-1;
cout<<"Parent of "<<i<<" is : "<<vert[i].parent<<endl;
}
}
vert[src].color="Gray";
vert[src].dist=0;
vert[src].parent=-1;
queue<int> q;
q.push(src);
// prev.push_back(src);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int vertex : vert[u].adjList)
{
if(vert[vertex].color=="White")
{
vert[vertex].color="Gray";
vert[vertex].dist= vert[u].dist+1;
vert[vertex].parent = u;
// prev.push_back(u);//for the path
q.push(vertex);
// if(vertex==dest)
// break;
}
}
vert[u].color="Black";
}
cout<<"Parent of 7 is : "<<vert[7].parent<<endl;
}
int print(int src, int dest)
{
if(dest==src)
return src;
cout<<vert[dest].parent<<" ";
print(src,vert[dest].parent);
}
int main()
{
// int n;
int source = 1;
vert[0].adjList={1,3};
vert[1].adjList={0,2};
vert[2].adjList={1};
vert[3].adjList={0,4,7};
vert[4].adjList={3,7,6,5};
vert[5].adjList={4,6};
vert[6].adjList={7,4,5};
vert[7].adjList={3,4,6};
bfs(0,7);
print(0,7);
}
/*
1 ---- 0 7 ---- 6
| | | |
2 3 ---- 4 ---- 5
*/