-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs.cpp
162 lines (134 loc) · 3.1 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <queue>
using namespace std;
#define Max 110
class Node
{ public:
int data;
Node* next;
};
/*
* Adjacency List
*/
class AdjList
{ public:
Node *head;
};
/*
* Class Graph
*/
class Graph
{
public:
//
bool vis[Max];
int time, prev[Max], dist[Max],f[Max];
int v;
AdjList* a;
bool directed;
//**************** Initialization*****************
Graph(int v)
{
this->v = v;
a = new AdjList [v];
for (int i = 0; i < v; ++i){
a[i].head = NULL;
}
}
// add edge
void addEdge(int src, int dest)
{
Node * n = new Node;
n->data = dest;
n->next = NULL;
if (a[src].head == NULL)
{ a[src].head = n;
}
else {
Node* tmp = a[src].head; // code logic not working because of insert at last in the linked list
while(tmp->next != NULL){
tmp=tmp->next;
}
tmp->next=n;
}
}
//DFS implementation
void dfs(int source){
for(int i=0;i< v; ++i){
vis[i]= 0;
prev[i]= NULL;
dist[i]= Max; //distance infinity
f[i]=Max;
}
time = 0;
dfs_visit(source,vis);
// For unvisited vertices which are not adjacent of any visited nodes
for(int i=0;i< v; ++i)
{ if(vis[i]==0){
dfs_visit(i,vis);
}
}
}
void dfs_visit(int source, bool vis[]){
vis[source]= 1; // grey
cout<<source<<" ";
time=time+1;
dist[source] = time;
Node * tmp = a[source].head;
while(tmp != NULL){
int next = tmp->data;
if (vis[next] == 0){
prev[next]= source;
dfs_visit(next,vis);
} tmp= tmp->next;
}
//vis[next] = 1; // visit
time=time+ 1;
f[source]=time;
}
void printGraph()
{
int i;
for (i = 0; i < v; ++i)
{
Node* tmp = a[i].head;
cout<<"\n Adjacency list of vertex "<<i<<"\n head ";
while (tmp)
{
cout<<"-> "<<tmp->data;
tmp = tmp->next;
}
cout<<endl;
}
}
};
int main()
{
int s,v;
Graph g(v=8);
g.addEdge(0, 1);
g.addEdge(0, 3);
g.addEdge(0, 4);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.addEdge(4, 5);
g.addEdge(5, 3);
g.addEdge(3, 2);
g.addEdge(2, 0);
g.addEdge(4, 3);
g.addEdge(6, 4);
g.addEdge(6, 5);
g.addEdge(6, 7);
g.addEdge(7, 3);
//g.printGraph();
cout<<"\nEnter source: ";
cin >> s;
cout << "\nDepth First Traversal :\n"<<endl;
g.dfs(s);
cout << "\nFrom node " << s << "\n" << endl;
for (int i = 0; i < v; i++)
{
cout << "Distance of " << i << " is : " << g.dist[i] << " , "<<"Finish time : "<< g.f[i] << endl;
}
return 0;
}