-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
45 lines (40 loc) · 678 Bytes
/
bfs.cpp
File metadata and controls
45 lines (40 loc) · 678 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
36
37
38
39
40
41
42
43
44
45
#include<iostream>
using namespace std;
int main()
{
int n, m, cost[10][10], v, visited[10], visit[10], front=0, rear=0, i, j, k, qu[10];
cout<<"Enter no. of vertices: ";
cin>>n;
cout<<"Enter no. of edges: ";
cin>>m;
cout<<"Enter EDGES: ";
for(int k=0; k<m; k++)
{
cin>>i>>j;
cost[i][j] = 1;
cost[j][i] = 1;
}
cout<<"Enter initial vertex: ";
cin>>v;
cout<<"Visited vertices\n";
cout<<v;
visited[v] =1;
k=1;
while(k<n)
{
for(j=0; j<n; j++)
{
if(cost[v][j] != 0 && visited[v] != 1 && visit[v] != 1)
{
visit[v] = 1;
qu[rear++] = j;
}
}
v = qu[front++];
cout<<v<<" ";
k++;
visit[v]=0;
visited[v] =1;
}
return 0;
}