Skip to content

Commit 7bb4ae8

Browse files
committedApr 21, 2020
Added undirected graph with BFS and DFS traversals.
1 parent 0ea8d09 commit 7bb4ae8

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
// Implements Graph Data Structure using adjacency list.
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
struct Graph
7+
{
8+
int V;
9+
unordered_set<int>* adjList;
10+
bool *marked;
11+
int *edgeTo;
12+
int searchSrc;
13+
14+
Graph(int V);
15+
16+
~Graph()
17+
{
18+
delete[] adjList;
19+
delete[] marked;
20+
delete[] edgeTo;
21+
}
22+
23+
void addEdge(int src, int dest);
24+
25+
void printGraph();
26+
27+
void searchEdge(int src, int dest);
28+
29+
void resetSearch();
30+
31+
void BFS(int s);
32+
33+
void DFS(int s);
34+
35+
void dfsUtil(int v);
36+
37+
bool hasPathTo(int v) { return marked[v]; }
38+
39+
stack<int> pathTo(int v);
40+
};
41+
42+
Graph::Graph(int V) :
43+
V(V),
44+
adjList(new unordered_set<int>[V])
45+
{
46+
marked = new bool[V];
47+
edgeTo = new int[V];
48+
resetSearch();
49+
}
50+
51+
void Graph::resetSearch()
52+
{
53+
for (int i = 0; i < V; i++)
54+
{
55+
marked[i] = false;
56+
edgeTo[i] = -1;
57+
}
58+
}
59+
60+
void Graph::addEdge(int src, int dest)
61+
{
62+
adjList[src].insert(dest);
63+
adjList[dest].insert(src);
64+
}
65+
66+
void Graph::printGraph()
67+
{
68+
for (int i = 0; i < V; i++)
69+
{
70+
unordered_set<int> adj = adjList[i];
71+
cout << "Adjacency List of vertex " << i;
72+
if (marked[i])
73+
cout << "*\n";
74+
else
75+
cout << "\n";
76+
for (auto& it : adj)
77+
{
78+
cout << it << " ";
79+
}
80+
cout << "\n";
81+
}
82+
}
83+
84+
void Graph::searchEdge(int src, int dest)
85+
{
86+
auto it = adjList[src].find(dest);
87+
if (it == adjList[src].end())
88+
cout << "Edge not found!";
89+
else
90+
cout << "Edge found!";
91+
cout << endl;
92+
}
93+
94+
void Graph::BFS(int s)
95+
{
96+
searchSrc = s;
97+
cout << "BFS Traversal: \n";
98+
resetSearch();
99+
queue<int> q;
100+
q.push(s);
101+
marked[s] = true;
102+
cout << s << " ";
103+
while (!q.empty())
104+
{
105+
int v = q.front();
106+
q.pop();
107+
for (auto& it : adjList[v])
108+
{
109+
if (!marked[it])
110+
{
111+
q.push(it);
112+
marked[it] = true;
113+
edgeTo[it] = v;
114+
cout << it << " ";
115+
}
116+
}
117+
}
118+
cout << endl;
119+
}
120+
121+
void Graph::DFS(int s)
122+
{
123+
searchSrc = s;
124+
resetSearch();
125+
cout << "DFS Traversal: \n";
126+
dfsUtil(s);
127+
cout << "\n";
128+
}
129+
130+
void Graph::dfsUtil(int v)
131+
{
132+
marked[v] = true;
133+
cout << v << " ";
134+
for (auto& it : adjList[v])
135+
{
136+
if (!marked[it]){
137+
dfsUtil(it);
138+
edgeTo[it] = v;
139+
}
140+
}
141+
}
142+
143+
stack<int> Graph::pathTo(int v)
144+
{
145+
stack<int> path;
146+
if (!hasPathTo(v))
147+
return path;
148+
for (int x = v; x != searchSrc; x = edgeTo[x])
149+
path.push(x);
150+
path.push(searchSrc);
151+
return path;
152+
}
153+
154+
int main()
155+
{
156+
int V = 7;
157+
Graph g(V);
158+
g.addEdge(0, 1);
159+
g.addEdge(0, 4);
160+
g.addEdge(1, 2);
161+
g.addEdge(1, 3);
162+
g.addEdge(1, 4);
163+
g.addEdge(2, 3);
164+
g.addEdge(3, 4);
165+
g.addEdge(5, 6);
166+
cout << "Before BFS: \n";
167+
g.printGraph();
168+
169+
g.DFS(0);
170+
171+
stack<int> path = g.pathTo(2);
172+
cout << "0 to 2(with DFS): \n";
173+
while (!path.empty())
174+
{
175+
cout << path.top() << " ";
176+
path.pop();
177+
}
178+
cout << endl;
179+
g.BFS(0);
180+
181+
path = g.pathTo(2);
182+
cout << "0 to 2(with BFS): \n";
183+
while (!path.empty())
184+
{
185+
cout << path.top() << " ";
186+
path.pop();
187+
}
188+
189+
return 0;
190+
}
191+

0 commit comments

Comments
 (0)
Please sign in to comment.