-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenric_graph.cpp
266 lines (237 loc) · 6.03 KB
/
genric_graph.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
///Graphs 04 - Adjacency List Implementation for Generic Data
#include<iostream>
#include<bits/stdc++.h>
#include<map>
#include<queue>
using namespace std;
template<typename T>
class graph
{
map<T,list<T> > adjlist;
public:
///Dynamic graph without defining number of nodes.
graph()
{
}
void addedge(T u,T v,bool bidir = true)
{
adjlist[u].push_back(v);
if(bidir)
{
adjlist[v].push_back(u);
}
}
void print()
{
for( i:adjlist)
{
cout<<i.first<<"->";
for(T entry:i.second)
{
cout<<entry<<", ";
}
cout<<endl;
}
}
///Breadth first Search.
void bfs(T src)
{
queue<T> q;
map<T, bool> Visited;
q.push(src);
Visited[src]=true;
while(!q.empty())
{
T node = q.front();
cout<<node<<" ";
q.pop();
/// For the neighbours of the current node, find nodes which are not visited.
for(neighbour: adjlist[node])
{
if(!Visited[neighbour])
{
q.push(neighbour);
Visited[neighbour]=true;
}
}
}
}
/// Using BFS - Single Source Shortest Path
void ssp(T src)
{
queue<T> q;
map<T, int> dist;
///Another map to store the parents.
map<T,T> parent;
for(node:adjlist)
dist[node.first] = INT_MAX;
q.push(src);
dist[src]=0;
parent[src] = src;
while(!q.empty())
{
T node = q.front();
//cout<<node<<" ";
q.pop();
/// For the neighbours of the current node, find nodes which are not visited.
for(neighbour: adjlist[node])
{
if(dist[neighbour]==INT_MAX)
{
q.push(neighbour);
dist[neighbour]=dist[node]+1;
parent[neighbour] = node;
}
}
}
///Print distances to all the nodes.
for(auto i:adjlist)
{
T node = i.first;
cout<<"Distance of "<<node<<" from "<<src<<" is "<<dist[node]<<endl;
}
}
void dfshelper(T node, map<T, bool> &visited)
{
/// whenever come to a node mark it visited.
visited[node] = true;
cout<<node<<" ";
/// Find neighbour which is not visited.
for(T neighbour: adjlist[node])
{
if(!visited[neighbour])
dfshelper(neighbour,visited);
}
}
///Recursive call for DFS
void dfs(T src)
{
map<T,bool> visited;
dfshelper(src,visited);
}
///Conncted Components.
void connected_components(T src)
{
map<T,bool> visited;
int c = 1;
dfshelper(src,visited);
cout<<endl;
for(auto i:adjlist)
{
T node = i.first;
if(!visited[node])
{
dfshelper(node,visited);
c++;
}
}
cout<<endl<<"The current graph has "<<c<<" components.";
}
/// Cycle Detection using BFS.
bool iscyclicbfs(T src)
{
map<T,bool> visited;
map<T,T> parent;
queue<T> q;
q.push(src);
visited[src] = true;
parent[src] = src;
while(!q.empty())
{
T node = q.front();
q.pop();
for(T nbr:adjlist[node])
{
if(visited[nbr]!=true && parent[node]!=nbr)
{
return true;
}
/// if and else if working without else part.
else if(!visited[nbr])
{
visited[nbr]=true;
parent[nbr] = node;
q.push(nbr);
}
}
}
return false;
}
/// Cycle Detection using DFS.
cychelper(T node, map<T,bool> &visited, map<T,bool> &instack)
{
instack[node]=true;
visited[node]=true;
for(T nbr: adjlist[node])
{
if(!visited[nbr] && cychelper(nbr,visited,instack)|| instack[nbr])
{
return true;
}
}
return false;
}
bool iscyclicdfs(T src)
{
map<T,bool>visited;
map<T,bool> instack;
visited[src]=true;
instack[src]=true;
bool c;
for(auto i: adjlist)
{
T node = i.first;
if(!visited[node])
{
c = cychelper(src,visited,instack);
}
}
if(c)
cout<<"Cyclic"<<endl;
else
cout<<"Not Cyclic"<<endl;
}
};
int main()
{
graph <int> g;
g.addedge(1,2,false);
g.addedge(2,3,false);
g.addedge(3,4,false);
g.addedge(4,2,false);
g.addedge(4,1,false);
g.addedge(5,3,false);
g.addedge(5,6,false);
g.addedge(6,6,false);
//g.addedge(6,7);
g.print();
cout<<endl<<endl;
//g.bfs(0);
cout<<endl<<endl;
//g.ssp(0);
cout<<endl<<endl;
g.dfs(1);
cout<<endl<<endl;
g.connected_components(1);
cout<<endl<<endl;
if(g.iscyclicbfs(1))
{
cout<<"Cyclic"<<endl;
}
else
cout<<"Not, Cyclic "<<endl;
g.iscyclicdfs(1);
cout<<endl<<endl;
graph <string> g1;
g1.addedge("PUTIN","TRUMP",false);
g1.addedge("PUTIN","MODI",false);
g1.addedge("PUTIN","POPE",false);
g1.addedge("MODI","TRUMP",true);
g1.addedge("MODI","YOGI",true);
g1.addedge("YOGI","PRABHU",false);
g1.addedge("PRABHU","MODI",false);
//g1.print();
cout<<endl<<endl;
//g1.bfs("MODI");
return 0;
}