-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
93 lines (79 loc) · 1.5 KB
/
bfs.cpp
File metadata and controls
93 lines (79 loc) · 1.5 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
89
90
91
92
93
#include<iostream>
#include<list>
#include<vector>
#include<queue>
using namespace std;
class Graph{
int V;
list<int> *adj;
void bfs_utils(int start_node, bool visited[]);
public:
Graph(int v);
void add_edge(int u, int v);
void bfs(int start_node);
void print(void);
};
Graph::Graph(int V){
this->V=V;
adj= new list<int>[V];
}
void Graph::add_edge(int u, int v){
adj[u].push_back(v);
}
void Graph::bfs_utils(int start_node, bool visited[]){
queue<int> temp;
temp.push(start_node);
while(!temp.empty()){
int node=temp.front();
temp.pop();
if(!visited[node]){
visited[node]=true;
cout<<node<<" ";
list<int>::iterator it;
for(it=adj[node].begin();it!=adj[node].end();++it){
temp.push(*it);
}
}
}cout<<endl;
}
void Graph::bfs(int start_node){
bool *visited=new bool[V];
for(int i=0;i<V;++i){
visited[i]=false;
}
bfs_utils(start_node, visited);
}
void Graph::print(void){
list<int>::iterator it;
for(int i=0;i<V;++i){
cout<<i<<":";
for(it=adj[i].begin();it!=adj[i].end();++it){
cout<<"->"<<*it;
}cout<<endl;
}cout<<endl;
}
int main(){
Graph g(8);
g.add_edge(0, 1);
g.add_edge(0, 2);
g.add_edge(0, 3);
g.add_edge(1, 4);
g.add_edge(4, 7);
g.add_edge(2, 5);
g.add_edge(3, 6);
g.add_edge(6, 7);
g.add_edge(4, 5);
g.print();
g.bfs(0);
/* Graph g(4);
g.add_edge(0, 1);
g.add_edge(0, 2);
g.add_edge(1, 2);
g.add_edge(2, 0);
g.add_edge(2, 3);
g.add_edge(3, 3);
g.print();
g.bfs(0);
*/
return 0;
}