-
Notifications
You must be signed in to change notification settings - Fork 12
/
BFS&DFS.cpp
73 lines (66 loc) · 1.41 KB
/
BFS&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
70
71
72
73
#include<bits/stdc++.h>
using namespace std;
int e, x, f(0), r(-1), t(-1);
const int v = 5;
void init(int visited[]){
for(int i = 0; i < v; i++){xxxxxxx
visited[i] = 0;
}
}
void BFS(int graph[v][v], int visited[], int x){
init(visited);
cout<<"\nBFS : \n";
int q[10];
q[r++] = x;
cout<<x<<" ";
int j = 1;
while(j < v){
for(int i = 0; i < v; i++){
if(graph[x][i] == 1 && visited[i] == 0){
visited[i] = 1;
q[r++] = i;
}
}
x = q[f++];
cout<<x<<" ";
j++;
}
}
void DFS(int graph[v][v], int visited[], int x){
init(visited);
cout<<"\nDFS : \n";
int s[10];
s[t++] = x;
cout<<x<<" ";
visited[x] = 1;
int j = 1;
while(j < v){
for(int i = v; i >= 0; i--){
if(graph[x][i] == 1 && visited[i] == 0){
visited[i] = 1;
s[t++] = i;
}
}
x = s[--t];
cout<<x<<" ";
j++;
}
}
int main(){
cout<<"Enter number of edges : ";
cin>>e;
int graph[v][v];
int visited[v];
cout<<"\nNow, enter the edges :\n";
for(int i = 0; i < e; i++){
int s, d;
cout<<"S:"; cin>>s;
cout<<"D:"; cin>>d;
graph[s][d] = 1;
}
cout<<"\nEnter edge to traverse from : ";
cin>>x;
BFS(graph, visited, x);
DFS(graph, visited, x);
return 0;
}