forked from shrox/Random-Codes
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
Latest commit
35 lines (31 loc) · 565 Bytes
/
Copy pathbfs.cpp
File metadata and controls
35 lines (31 loc) · 565 Bytes
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
/* Breadth First Search
* Number of Vertices: n
* Source: s
* Distance Array: dist[]
* Adjacency List: adj[][]
* Infinite Constant: inf
*/
voidbfs()
{
for(int i=0;i<n;++i)
{
dist[i] = inf;
}
queue<int> q;
q.push(s);
dist[s] = 0;
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i=0;i<adj[u].size();++i)
{
int v = adj[u][i];
if(dist[v]==inf)
{
dist[v] = dist[u]+1;
q.push(v);
}
}
}
}