forked from bhawna-menghani/DSA_programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
62 lines (58 loc) · 1.15 KB
/
Copy pathbfs.cpp
File metadata and controls
62 lines (58 loc) · 1.15 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
#include<bits/stdc++.h>
using namespace std;
void addEdge(vector<int>adj[], int v, int u)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void printGraph(vector<int>adj[],int v)
{
for(int i=0;i<v;i++)
{
cout<<i<<": ";
for(int x: adj[i])
{
cout<<x<<" ";
}
cout<<"\n";
}
}
void BFS(vector<int>adj[],int s,int v)
{
bool visited[v+1];
for(int i=0;i<v;i++)
visited[i]=false;
queue<int>q;
q.push(s);
visited[s]=true;
while(!q.empty())
{
int x = q.front();
cout<<x<<" ";
q.pop();
for(int s: adj[x])
{
if (visited[s]==false)
{
visited[s]=true;
q.push(s);
}
}
}
}
int main()
{
int v = 5;
vector<int>adj[v];
addEdge(adj,0,1);
addEdge(adj,0,2);
addEdge(adj,2,3);
addEdge(adj,2,4);
cout<<"\nAuthor: Abhishek Kumar\n\nAdjancy list representation:\n";
printGraph(adj,v);
cout<<"\nBFS of Graph is: ";
int s = 0;
BFS(adj,s,v);
cout<<"\n\n";
return 0;
}