-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.cpp
More file actions
88 lines (79 loc) · 1.39 KB
/
BFS.cpp
File metadata and controls
88 lines (79 loc) · 1.39 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
#include <fstream>
#include "queuelink (1).cpp"
using namespace std;
int v;
int graph[100][100];
int visited[100]={0};
void bfs(int n)
{
queue Q;
visited[n]=1;
Q.enqueue(n);
while(!Q.isempty())
{
int i=Q.dequeue();
char c=i+65;
cout<<c<<" ";
for(int w=0;w<v;++w)
{
if(graph[i][w]==1)
{
if(visited[w]!=1)
{
visited[w]=1;
Q.enqueue(w);
}
}
}
}
}
int main()
{
fstream infile;
infile.open("BFS_input.txt",ios::in);
if(!infile)
{
cout<<"Error!"<<endl;
return 0;
}
infile>>v;
for(int i=0;i<v;i++)
{
for(int j=0;j<v;j++)
infile>>graph[i][j];
}
cout<<"Input graph is:"<<endl;
for(int i=0;i<v;i++)
{
for(int j=0;j<v;j++)
cout<<graph[i][j]<<" ";
cout<<endl;
}
int m;
cout<<"Enter the starting vertex: ";
cin>>m;
cout<<"BFS is: "<<endl;
bfs(m);
return 0;
}
/*
// vertex: 7
0 1 0 1 0 0 0
1 0 1 1 0 1 1
0 1 0 1 1 1 0
1 1 1 1 0 0 0
0 0 1 1 0 0 1
0 1 1 0 0 0 0
0 1 0 0 1 0 0
A B D C F G E
*/
/*
Given graph:
0 1 1 0 0 0
1 0 0 1 1 0
1 0 0 0 1 0
0 1 0 0 1 1
0 1 1 1 0 1
0 0 0 1 1 0
*/