-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBFS_Traversal.cpp
More file actions
65 lines (60 loc) · 1.16 KB
/
BFS_Traversal.cpp
File metadata and controls
65 lines (60 loc) · 1.16 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
64
65
#include <bits/stdc++.h>
using namespace std;
void print(int **matrix,int V,int sv,bool *visited)
{
queue<int>q;
q.push(sv);
visited[sv]=true;
while(!q.empty())
{
int current=q.front();
cout<<current<<" ";
q.pop();
for(int i=0;i<V;i++)
{
if(matrix[current][i]==1 &&!visited[i])
{
q.push(i);
visited[i]=true;
}
}
}
}
int main() {
int V, E;
cin >> V >> E;
/*
Write Your Code Here
Complete the Rest of the Program
You have to take input and print the output yourself
*/
int **matrix=new int*[V];
for(int i=0;i<V;i++)
{
matrix[i]=new int[V];
for(int j=0;j<V;j++)
{
matrix[i][j]=0;
}
}
for(int i=0;i<E;i++)
{
int s,e;
cin>>s>>e;
matrix[s][e]=1;
matrix[e][s]=1;
}
bool *visited=new bool[V];
for(int i=0;i<V;i++)
{
visited[i]=false;
}
print(matrix,V,0,visited);
delete[]visited;
for(int i=0;i<V;i++)
{
delete[]matrix[i];
}
delete[]matrix;
return 0;
}