This repository was archived by the owner on Oct 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathdfs.cpp
More file actions
92 lines (77 loc) · 1.85 KB
/
dfs.cpp
File metadata and controls
92 lines (77 loc) · 1.85 KB
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
#include <bits/stdc++.h>
using namespace std;
// Number of vertices in the graph
#define N 13
// data structure to store graph edges
struct Edge {
int src, dest;
};
// class to represent a graph object
class Graph
{
public:
// A array of vectors to represent adjacency list
vector<int> adjList[N];
// Constructor
Graph(vector<Edge> edges)
{
// add edges to the undirected graph
for (int i = 0; i < edges.size(); i++)
{
int src = edges[i].src;
int dest = edges[i].dest;
adjList[src].push_back(dest);
adjList[dest].push_back(src);
}
}
};
// Perform iterative DFS on graph g starting from vertex v
int iterativeDFS(Graph const &graph, int v)
{
// stores vertex is discovered or not
vector<bool> discovered(N);
// create a stack used to do iterative DFS
stack<int> stack;
// push the source node into stack
stack.push(v);
// run till stack is not empty
while (!stack.empty())
{
// Pop a vertex from stack
v = stack.top();
stack.pop();
// if the vertex is already discovered yet,
// ignore it
if (discovered[v])
continue;
// we will reach here if the popped vertex v
// is not discovered yet. We print it and process
// its undiscovered adjacent nodes into stack
discovered[v] = true;
cout << v << " ";
// do for every edge (v -> u)
// we use reverse iterator (Why?)
for (auto it = graph.adjList[v].rbegin();
it != graph.adjList[v].rend(); ++it)
{
int u = *it;
if (!discovered[u])
stack.push(u);
}
}
}
// main function
int main()
{
// vector of graph edges as per above diagram
vector<Edge> edges = {
{1, 2}, {1, 7}, {1, 8}, {2, 3}, {2, 6}, {3, 4},
{3, 5}, {8, 9}, {8, 12}, {9, 10}, {9, 11}
// , {6, 9} // introduce cycle
};
// create a graph from given edges
Graph graph(edges);
// Do iterative DFS traversal from vertex 1
iterativeDFS(graph, 1);
return 0;
}