-
Notifications
You must be signed in to change notification settings - Fork 7
/
bfs_graph.cpp
73 lines (65 loc) · 1.42 KB
/
bfs_graph.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
#include <bits/stdc++.h>
using namespace std;
//structure for node
struct node
{
int data;
};
//creating a new node
node *createNode(int val)
{
node *newNode=new node;
newNode->data=val;
return newNode;
}
//adding new edge in graph
void addEdge(vector<node*>g[],int s,int d)
{
//create a new node and add it to to the adjacency list
node *temp=createNode(d);
g[s].push_back(temp);
}
void BFS(vector<node*>g[],int s,int v)
{
//boolean array to maintain record of visited nodes
bool visited[v]={0};
//queue for BFS
queue<int> q;
q.push(s);
visited[s]=1;
cout<<s<<" ";
while(!q.empty())
{
//get the front element of queue
int ind=q.front();
//dequeue the front element
q.pop();
//traverse the adjacency list of dequeued node
for(int i=0;i<g[ind].size();i++)
{
int cur=g[ind][i]->data;
//if current node is not visited push it into queue and mark it visited
if(!visited[cur])
{
cout<<cur<<" ";
q.push(cur);
visited[cur]=1;
}
}
}
}
int main()
{
//No. of vertex,edges and starting source
int v,e,source;
cin>>v>>e>>source;
//adjacency list for graph
vector<node*>g[v];
for(int i=0;i<e;i++)
{
int s,d;
cin>>s>>d;
addEdge(g,s,d);
}
BFS(g,source,v);
}