-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShortestPath_Un.cpp
More file actions
63 lines (60 loc) · 1.22 KB
/
ShortestPath_Un.cpp
File metadata and controls
63 lines (60 loc) · 1.22 KB
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
#include<bits/stdc++.h>
using namespace std;
class graph
{
int V;
list<int>*adj;
public:
graph(int V)
{
this->V = V;
this->adj = new list<int>[this->V];
}
void addedge(int u,int v,bool directed=false)
{
this->adj[u].push_back(v);
if(directed==false)
{
this->adj[v].push_back(u);
}
}
void BFS_SS(int s)
{
vector<bool>visited(this->V,false);
vector<int>dist(this->V,INT_MAX);
dist[0]=0;
list<int>q;
visited[s]=true;
q.push_back(s);
while (!q.empty())
{
s = q.front();
q.pop_front();
for(auto itr=this->adj[s].begin();itr!=this->adj[s].end();itr++)
{
if(visited[*itr]==false)
{
dist[*itr]=dist[s]+1;
visited[*itr]=true;
q.push_back(*itr);
}
}
}
for(auto v:dist)
{
cout<<v<<" ";
}
cout<<endl;
}
};
int main()
{
graph g(4);
g.addedge(0,1);
g.addedge(1,2);
g.addedge(2,3);
g.addedge(0,2);
g.addedge(1,3);
g.BFS_SS(0);
return 0;
}