-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06bfs.cpp
More file actions
52 lines (48 loc) · 1.04 KB
/
06bfs.cpp
File metadata and controls
52 lines (48 loc) · 1.04 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
#include<iostream>
#include<ctime>
#define MAX 100
using namespace std;
bool visited[MAX];
int adj[MAX][MAX];
void bfs(int start, int n)
{
int queue[MAX];
int front = 0, rear=-1;
int v;
for (int i=0;i<n;i++)
visited[i] = false;
visited[start] = true;
queue[++rear] = start;
while(front<=rear)
{
v = queue[front++];
cout<<v<<" ";
for(int i=0;i<n;i++)
{
if(adj[v][i]&& !visited[i])
{
visited[i]=true;
queue[++rear]=i;
}
}
}
cout<<endl;
}
int main()
{
int start, n;
cout<<"enter num of vertices: ";
cin>>n;
cout<<"enter vertices:"<<endl;
for(int i = 0;i<n;i++)
for(int j=0;j<n;j++)
cin>>adj[i][j];
cout<<"enter starting vertice: ";
cin>>start;
clock_t start_t = clock();
bfs(start, n);
clock_t end = clock();
double time = double(end-start_t)/CLOCKS_PER_SEC;
cout<<"time taken: "<<time*1000000<<" Microsseconds\n";
return 0;
}