-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFSnDFS.cpp
117 lines (81 loc) · 2.04 KB
/
BFSnDFS.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// bfs in graph
#include<bits/stdc++.h>
using namespace std;
class Graph{
int vertices;
list<int> *adj;
void dfsUtil(int src,bool *);
public:
Graph(int v){
vertices=v;
adj = new list<int>[v];
}
void addEdge(int src,int dest);
void printGraph();
void bfs(int src);
void dfs(int src);
};
void Graph::addEdge(int src,int dest){ // directed graph
adj[src].push_back(dest);
}
void Graph::printGraph(){
for(int i=0;i<vertices;i++){
list<int>::iterator it = adj[i].begin();
while(it!=adj[i].end()){
cout<<*it<<" ";
it++;
}
cout<<endl;
}
}
void Graph::bfs(int src){
// similar to level order traversal in Tree
bool *visited = new bool[vertices];
for(int i=0;i<vertices;i++)
visited[i]=false;
queue<int>q;
q.push(src);
visited[src]=true;
while(!q.empty()){
int value = q.front();q.pop();
cout<<value<<" ";
list<int>::iterator itr;
for(itr=adj[value].begin();itr!=adj[value].end();itr++){
if(!visited[*itr]){
visited[*itr] = true;
q.push(*itr);
}
}
}
cout<<"================"<<endl;
}
void Graph::dfsUtil(int src,bool *visited){
visited[src]=true;
cout<<src<<" ";
list<int>::iterator itr;
for(itr=adj[src].begin();itr!=adj[src].end();itr++){
if(!visited[*itr])
dfsUtil(*itr,visited);
}
}
void Graph::dfs(int src){
bool *visited = new bool[vertices];
for(int i=0;i<vertices;i++)
visited[i]=false; // initially none of the vertex is visited
dfsUtil(src,visited);
}
int main(){
Graph graph(5); // graph with 5 vertices
graph.addEdge(0,1);
graph.addEdge(0,4);
graph.addEdge(1,2);
graph.addEdge(1,3);
graph.addEdge(1,4);
graph.addEdge(2,3);
graph.addEdge(3,4);
graph.printGraph();
cout<<"\n========BFS=======\n"<<endl;
graph.bfs(0);
cout<<"\n========DFS=======\n"<<endl;
graph.dfs(0);
}