-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.cpp
More file actions
65 lines (61 loc) · 1.03 KB
/
Copy pathdfs.cpp
File metadata and controls
65 lines (61 loc) · 1.03 KB
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
#include<iostream>
#include<cstdlib>
#include<queue>
#include<cstring>
using namespace std;
int Time;
struct vertices
{
string color;
int parent;
int d;
int f;
vector<int> adjList;
};
struct vertices vert[8];
void dfs_visit(int u)
{
Time=Time+1;
vert[u].d=Time;
vert[u].color="Gray";
cout<<u<<" ";
for(int val : vert[u].adjList)
{
if(vert[val].color=="White")
{
vert[val].parent=u;
dfs_visit(val);
}
}
vert[u].color="Black";
Time=Time+1;
vert[u].f=Time;
}
void dfs()
{
for(int i=7;i>=0;i--)
{
vert[i].color="White";
vert[i].parent=-1;
}
Time=0;
for(int i=7;i>=0;i--)
{
if(vert[i].color=="White")
{
dfs_visit(i);
}
}
}
int main()
{
vert[0].adjList={1,3};
vert[1].adjList={0,2};
vert[2].adjList={1};
vert[3].adjList={0,4,7};
vert[4].adjList={3,7,6,5};
vert[5].adjList={4,6};
vert[6].adjList={7,4,5};
vert[7].adjList={3,4,6};
dfs();
}