-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFS and DFS.cpp
69 lines (63 loc) · 1.12 KB
/
BFS and DFS.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
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
//For the edges
void edge(vector<int> adj[], int u, int v) {
adj[u].push_back(v);
}
//For BFS
void BFS(int s, vector<int>adj[], bool visit[]) {
queue<int> Q;
Q.push(s);
visit[s] = true;
while (!Q.empty()) {
int R = Q.front();
cout << R << " ";
Q.pop();
for (int i = 0; i < adj[R].size(); i++) {
if (!visit[adj[R][i]]) {
Q.push(adj[R][i]);
visit[adj[R][i]] = true;
}
}
}
}
//DFS
void DFS(int s, vector<int>adj[], bool visit[]) {
stack<int>S1;
S1.push(s);
visit[s] = true;
while (!S1.empty()) {
int S = S1.top();
cout << S << " ";
S1.pop();
for (int i = 0; i < adj[S].size(); i++) {
if (!visit[adj[S][i]]) {
S1.push(adj[S][i]);
visit[adj[S][i]] = true;
}
}
}
}
int main() {
vector<int> adj[5];
bool visit[5];
for (int i = 0; i < 5; i++) {
visit[i] = false;
}
edge(adj, 0, 2);
edge(adj, 0, 1);
edge(adj, 1, 3);
edge(adj, 2, 0);
edge(adj, 2, 3);
edge(adj, 2, 4);
BFS(0, adj, visit);
cout << endl;
for (int i = 0; i < 5; i++) {
visit[i] = false;
}
DFS(0, adj, visit);
return 0;
}