-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path13.DFS.cpp
86 lines (67 loc) · 1.57 KB
/
13.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
/*
* This program applies DFS on a graph constructed from the inputs of the user
*
* Coded by: Abdurrezak EFE
*
* */
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
class Graph{
vector<vector<int>> edge;
vector<int> color; // 1 black(finished node), 0 white(neither visited nor finished node)
public:
Graph(int v)
{
edge.resize(v);
color.resize(v);
for(int i=0;i<edge.size();i++) //initializing nodes' colors
color[i] = 0;
}
void add_edge(int u, int v)
{
//adding from both directions makes it undirected
//edge[v].push_back(u);
edge[u].push_back(v);
}
void DFS(int s) //dfs operation
{
if(color[s])
return;
stack<int> st;
st.push(s);
while(!st.empty())
{
s = st.top();
st.pop();
if(!color[s])
{
cout << s << " ";
color[s] = 1;
}
for(int i=0;i<edge[s].size();i++)
if(color[edge[s][i]] == 0)
st.push(edge[s][i]);
}
}
};
int main()
{
int v;
cout << "Enter the number of vertices you want to add" << endl;
cin >> v;
int e;
cout << "Enter the number of edges you want to add" << endl;
cin >> e;
Graph g(v);
cout << "Enter the edges" << endl;
int u,w;
for(int i=0;i<e;i++)
cin >> u >> w, g.add_edge(u,w);
cout << endl;
for(int i=0;i<v;i++)
g.DFS(i);
}