-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS.cpp
99 lines (91 loc) · 1.65 KB
/
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<bits/stdc++.h>
using namespace std;
struct Node{
int v;
Node *next;
};
int V,E,t;
char c[10];
int p[10],dist[10],start[10],finish[10];
Node *head[10];
void make_graph(int s,int d){
Node *newNode = new Node;
newNode->v = d;
newNode->next = NULL;
if(head[s]==NULL){
head[s] = newNode;
return;
}
newNode->next = head[s];
head[s] = newNode;
}
void printList(Node *tmp){
while(tmp){
cout<<"("<<tmp->v<<")"<<"->";
tmp = tmp->next;
}
cout<<endl;
}
void DFS_VISIT(int s){
t = t + 1;
start[s] = t;
c[s] = 'g';
Node *vertices = head[s];
while(vertices){
int neighbor = vertices->v;
if(c[neighbor]=='w'){
p[neighbor] = s;
dist[neighbor] = dist[s] + 1;
DFS_VISIT(neighbor);
}
vertices = vertices->next;
}
t= t + 1;
finish[s] = t;
c[s] = 'b';
cout<<s<<"->";
}
void DFS(){
for(int i=0;i<V;i++){
c[i] = 'w';
p[i] = -1;
dist[i] = 0;
}
t = 0;
for(int i=0;i<V;i++){
if(c[i]=='w'){
DFS_VISIT(i);
}
}
}
int main(){
cin>>V>>E;
int a,b,c;
for(int i=1;i<=E;i++){
cin>>a>>b;
make_graph(a,b);
make_graph(b,a);
}
cout<<"-------Graph-----"<<endl;
for(int i=0;i<V;i++){
cout<<"Adjacent of "<<i<<": ";
Node *ptr = head[i];
printList(ptr);
}
cout<<"--------- Traversing ------"<<endl;
DFS();
return 0;
}
/*
10 10
0 1
0 2
1 2
1 5
2 5
2 7
2 4
3 6
4 6
8 9
*/