forked from Srajan-Jaiswal/Minor-II-Project
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDFS.cpp
89 lines (88 loc) · 1.73 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
#include <bits/stdc++.h>
using namespace std;
vector<int> vec;
class Graph
{
int V;
list<int> *adj;
public:
Graph(int vertices)
{
V = vertices;
adj = new list<int>[V];
}
void addEdge(int u, int v)
{
adj[u].push_back(v);
}
void depth_first_search(int v)
{
bool *visited = new bool[V];
memset(visited, false, sizeof(visited));
dfs_recursive(v, visited);
for (int i = 0; i < V; i++)
{
if (visited[i] == false)
{
dfs_recursive(i, visited);
}
}
}
void dfs_recursive(int v, bool *visited)
{
vec.push_back(v);
visited[v] = true;
for (auto it = adj[v].begin(); it != adj[v].end(); it++)
{
if (!visited[*it])
{
dfs_recursive(*it, visited);
}
}
}
};
int main()
{
clock_t tStart = clock();
int n, e, a, b, c;
int vstart;
cout << "No. of vertices:";
cin >> n;
cout << "No. of edges:";
cin >> e;
Graph g(n);
cout << "Enter the list of edges in the format : v1 v2 ";
for (int i = 0; i < e; i++)
{
cin >> a >> b;
g.addEdge(a, b);
}
cout << "Enter the start vertex:";
cin >> vstart;
g.depth_first_search(vstart);
cout << "PATH: ";
for (int i = 0; i < vec.size() - 1; i++)
{
cout << vec[i] << "->";
}
auto it = vec.end() - 1;
cout << *it << endl;
cout << "Time taken: ";
double ans = (double)(clock() - tStart) / CLOCKS_PER_SEC;
cout << ans;
return 0;
}
/*
10
9
0 1
0 2
0 3
1 4
1 5
2 6
2 7
3 8
3 9
0
*/