-
Notifications
You must be signed in to change notification settings - Fork 63
/
bfs.cpp
67 lines (67 loc) ยท 1.36 KB
/
bfs.cpp
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
#include <iostream>
#include <queue>
using namespace std;
class Graph{
bool **arr;
int size;
public:
Graph(int n){
size = n;
arr = new bool *[n];
for(int i=0;i<n;i++)
arr[i] = new bool[n]{false};
}
~Graph(){
for (int i = 0; i < size;i++)
delete[] arr[i];
delete[] arr;
}
void bfs(int start);
void createGraph();
};
void Graph::createGraph(){
int a, b;
while(true){
cout << "Enter edge vertex from start->end\n";
cin >> a >> b;
if(a==-1 && b==-1)
break;
a--;b--;
if(a<size && b<size){
arr[a][b] = true;
}
}
}
void Graph::bfs(int start){
cout << "BFS : ";
start--;
queue<int> q;
int temp;
bool visited[size] = {false};
q.push(start);
cout << start+1 << " ";
visited[start] = true;
while(!q.empty()){
temp = q.front();
q.pop();
for(int i=0;i<size;i++){
if(arr[temp][i] && !visited[i]){
cout << i+1 << " ";
q.push(i);
visited[i] = true;
}
}
}
}
int main(){
int n;
cout << "Enter number of vertices\n";
cin >> n;
Graph g = Graph(n);
g.createGraph();
int start;
cout << "Enter start of graph\n";
cin >> start;
g.bfs(start);
return 0;
}