MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / bfs

Function bfs

CPP/graph_tree/bfs.cpp:11–39  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9queue<int> q;
10
11void bfs(int sour)
12{
13 for(int i=1;i<=n;i++)
14 {
15 if(n!=1)
16 {
17 parent[i]=0;
18 dist[i]=-1;
19 }
20 }
21 parent[sour]=0;
22 dist[sour]=0;
23 q.push(sour);
24 while(!q.empty())
25 {
26 int x=q.front();
27 q.pop();
28 for(int j=0;j<adjList[x].size();j++)
29 {
30 int v=adjList[x][j];
31 if(parent[v]==0)
32 {
33 dist[v]=dist[x]+1;
34 parent[v]=x;
35 q.push(v);
36 }
37 }
38 }
39}
40
41int main()
42{

Callers 2

mainFunction · 0.70
isBipartiteMethod · 0.70

Calls 3

pushMethod · 0.45
popMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected