-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.cpp
More file actions
46 lines (40 loc) · 1020 Bytes
/
dfs.cpp
File metadata and controls
46 lines (40 loc) · 1020 Bytes
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
#include<bits/stdc++.h>
using namespace std;
void DFS(int u,int A[][8],int n)
{
int visited[8] = {0};
std::stack<int>stk;
stk.push(u);
while(!stk.empty())
{
int x = stk.top();
stk.pop();
if(visited[x] == 0)
{
cout<<x<<"->";
visited[x] = 1;
for(int v=n-1;v>=0;v--)
{
if(A[x][v] == 1 && visited[v] == 0)
{
stk.push(v);
}
}
}
}
}
int main (){
int A[8][8] = {{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}};
int u = 5;
cout << "DFS Vertex: " << u << " -> " << flush;
DFS(u, A, 8);
cout << endl;
return 0;
}