-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfsWithoutOOPs.cpp
68 lines (61 loc) · 1.4 KB
/
dfsWithoutOOPs.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
#include <iostream>
#include<stack>
#include <vector>
using namespace std;
vector<int> adj[10];
bool visited[10];
void dfs(int s)
{
visited[s] = true;
for (int i = 0; i < adj[s].size(); ++i)
{
if (visited[adj[s][i]] == false)
dfs(adj[s][i]);
}
}
void dfsIt(int s){
stack<int> st;
st.push(s);
while (!st.empty())
{
int node =st.top();
// visited[s] = true;
st.pop();
if(!visited[node]){
cout << node << "->";
visited[node] = true;
}
for (auto i = adj[node].begin(); i != adj[node].end(); ++i)
if (!visited[*i])
st.push(*i);
}
}
void initialize()
{
for (int i = 0; i < 10; ++i)
visited[i] = false;
}
int main()
{
int nodes, edges, x, y, connectedComponents = 0;
cin >> nodes; //Number of nodes
cin >> edges; //Number of edges
for (int i = 0; i < edges; ++i)
{
cin >> x >> y;
//Undirected Graph
adj[x].push_back(y); //Edge from vertex x to vertex y
adj[y].push_back(x); //Edge from vertex y to vertex x
}
initialize(); //Initialize all nodes as not visited
for (int i = 1; i <= nodes; ++i)
{
if (visited[i] == false)
{
dfsIt(i);
connectedComponents++;
}
}
cout << "Number of connected components: " << connectedComponents << endl;
return 0;
}