-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS.cpp
39 lines (31 loc) · 892 Bytes
/
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
#include <iostream>
#include <queue>
using namespace std;
void DFS(int start, vector<vector<int>> A, int n){
static vector<int> V(n,0);
if(V[start] == 0) {
cout << start << " " ;
V[start] = 1;
for(int j = 1; j <=n; j++)
{
if(V[j] == 0 && A[start][j] == 1) {
DFS(j, A, n);
}
}
}
}
int main (){
vector<vector<int>> A = {{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 0, 0, 0},
{0, 1, 0, 1, 0, 0, 0, 0},
{0, 1, 1, 0, 1, 1, 0, 0},
{0, 1, 0, 1, 0, 1, 0, 0},
{0, 0, 0, 1, 1, 0, 1, 1},
{0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0}};
cout << "Vertex: 1 -> " << flush;
DFS(4, A, 8);
cout << "Vertex: 4 -> " << flush;
//DFS(4, A, 8);
return 0;
}