Repository navigation
Expand file tree
/
Copy pathgraphs.cpp
More file actions
577 lines (556 loc) · 15.4 KB
/
Copy pathgraphs.cpp
File metadata and controls
577 lines (556 loc) · 15.4 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
#include <functional>
#include <limits>
#include <cassert>
using namespace std;
void printVector(vector<int> &v)
{
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
}
// This class is the base class for other graph-related classes such as DirectedGraph, WeightedGraph, and WeightedDirectedGraph
class Graph
{
private:
// making this private so only friend class i.e. DirectedGraph can access it
vector<vector<int>> adjList;
void numConnectedComponentsHelper(int node, vector<int> &visited);
bool isCyclicHelper(int node, vector<int> &visited, int parent);
protected:
// making this protected as numNodes will be common for all graph types
int numNodes;
public:
Graph(int nodes); // constructor
virtual ~Graph() {}
virtual void addEdge(int v, int w);
virtual void removeEdge(int v, int w);
// this display function will be used both in Graph and DirectedGraph classes
virtual void display();
virtual vector<int> bfs(int node);
virtual vector<int> dfs(int node);
// FOR YOU----> // used call by reference, for just using the address, instead of unnecessary copying
virtual void dfsHelper(int node, vector<int> &visited, vector<int> &ans);
bool isCyclic();
int numConnectedComponents();
virtual int ShortestPath(int start, int destination);
// this way only DerictedGraph class can access adjList
friend class DirectedGraph;
friend ostream & operator << (ostream &out, const Graph &graph);
};
ostream & operator << (ostream &out, const Graph &graph)
{
for (int node = 0; node < graph.numNodes; node++){
out << node << " -> ";
for(auto neighbour : graph.adjList[node]){
out << neighbour << " ";
}
out << endl;
}
return out;
}
// Graph::Graph(int nodes): This is the constructor for the Graph class, which takes an integer argument vertices to specify the number of nodes in the graph.
// : numNodes(nodes): This is a member initialization list, which initializes the numNodes attribute with the value of the vertices argument.
Graph::Graph(int nodes) : numNodes(nodes)
{
// Resizes the adjacency list to accommodate the specified number of vertices
adjList.resize(nodes);
}
void Graph::addEdge(int v, int w)
{
adjList[v].push_back(w);
adjList[w].push_back(v);
}
void Graph::removeEdge(int v, int w)
{
adjList[v].erase(find(adjList[v].begin(), adjList[v].end(), w));
adjList[w].erase(find(adjList[w].begin(), adjList[w].end(), v));
}
void Graph::display()
{
for (int i = 0; i < numNodes; i++)
{
cout << i << " -> ";
for (int j = 0; j < adjList[i].size(); j++)
{
cout << adjList[i][j] << " ";
}
cout << endl;
}
}
vector<int> Graph::bfs(int vertex)
{
vector<int> ans;
queue<int> q;
q.push(vertex);
vector<int> visited(numNodes, 0);
visited[vertex] = 1;
while (!q.empty())
{
int node = q.front();
q.pop();
ans.push_back(node);
for (auto neighbour : adjList[node])
{
if (!visited[neighbour])
{
visited[neighbour] = 1;
q.push(neighbour);
}
}
}
return ans;
}
void Graph::dfsHelper(int node, vector<int> &visited, vector<int> &ans)
{
visited[node] = 1;
ans.push_back(node);
for (auto neighbour : adjList[node])
{
if (!visited[neighbour])
{
dfsHelper(neighbour, visited, ans);
}
}
}
vector<int> Graph::dfs(int node)
{
vector<int> ans;
vector<int> visited(numNodes, 0);
visited[node] = 1;
ans.push_back(node);
for (auto neighbour : adjList[node])
{
dfsHelper(neighbour, visited, ans);
}
return ans;
}
bool Graph::isCyclicHelper(int node, vector<int>& visited, int parent)
{
visited[node] = 1;
for(auto neighbour : adjList[node]){
if(!visited[neighbour]){
if(isCyclicHelper(neighbour, visited, node)){
return true;
}
}else if(neighbour != parent){
return true;
}
}
return false;
}
bool Graph::isCyclic()
{
vector<int> visited(numNodes, 0);
for(int node = 0; node < numNodes; node++){
if(!visited[node])
{
if(isCyclicHelper(node, visited, -1)==true)
{
return true;
}
}
}
return false;
}
void Graph::numConnectedComponentsHelper(int node, vector<int> &visited)
{
visited[node] = 1;
for (auto neighbour : adjList[node])
{
if (!visited[neighbour])
{
numConnectedComponentsHelper(neighbour, visited);
}
}
}
int Graph::numConnectedComponents()
{
vector<int> visited(numNodes, 0);
int result = 0;
for (int node = 0; node < numNodes; node++)
{
if (!visited[node])
{
numConnectedComponentsHelper(node, visited);
result++;
}
}
return result;
}
int Graph::ShortestPath(int start, int destination)
{
// FOR YOU---> // this array is used to store the minimum distance from start to any node encountered during the loop
vector<int> dp(numNodes, 1e9);
dp[start] = 0;
priority_queue <pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> q;
q.push(make_pair(0, start));
while(!q.empty())
{
// lambda capture
auto [distance , node] = q.top();
q.pop();
if (node == destination) break;
for (auto neighbour : adjList[node])
{
if (dp[neighbour] > distance + 1)
{
dp[neighbour] = distance + 1;
q.push(make_pair(dp[neighbour], neighbour));
}
}
}
// -1 for not reachable from start to destination
if (dp[destination] == 1e9) return -1;
return dp[destination];
}
class DirectedGraph : public Graph
{
private:
bool isCyclicHelper(int node, vector<int>& visited);
public:
DirectedGraph(int nodes);
void addEdge(int v, int w);
void removeEdge(int v, int w);
// display function of Graph class will be used
// topological sort using Kahn algorithm
virtual vector<int> topoSort();
bool isCyclic();
};
DirectedGraph::DirectedGraph(int nodes) : Graph(nodes) {}
void DirectedGraph::addEdge(int v, int w)
{
adjList[v].push_back(w); // v:source w:destination
}
void DirectedGraph::removeEdge(int v, int w)
{
adjList[v].erase(find(adjList[v].begin(), adjList[v].end(), w));
}
vector<int> DirectedGraph::topoSort()
{
vector<int> ans;
vector<int> indegree(numNodes, 0);
queue<int> q;
for (int node = 0; node < numNodes; node++)
{
for (auto neighbour : adjList[node])
{
indegree[neighbour]++;
}
}
for (int node = 0; node < numNodes; node++)
{
if (indegree[node] == 0)
{
q.push(node);
}
}
while (!q.empty())
{
int node = q.front();
q.pop();
ans.push_back(node);
for (auto neighbour : adjList[node])
{
indegree[neighbour]--;
if (indegree[neighbour] == 0)
{
q.push(neighbour);
}
}
}
return ans;
}
bool DirectedGraph::isCyclicHelper(int node, vector<int>& visited)
{
visited[node] = 1;
for (auto neighbour : adjList[node]){
if(!visited[neighbour]){
if(isCyclicHelper(neighbour, visited)){
return true;
}
}else if (visited[neighbour] == 1){
return true;
}
}
visited[node] = 2;
return false;
}
bool DirectedGraph::isCyclic()
{
vector<int> visited(numNodes, 0);
for(int node = 0; node < numNodes; node++){
if (!visited[node])
{
if(isCyclicHelper(node, visited)==true)
{
return true;
}
}
}
return false;
}
class WeightedGraph : public Graph
{
// protected so that WeightedDirectedGraph class can access it
protected:
vector<vector<pair<int, int>>> adjList;
void numConnectedComponenetsHelper(int node, vector<int> &visited);
bool isCyclicHelper(int node, vector<int> & visited, int parent);
public:
WeightedGraph(int nodes);
// since number of arguments are different, it is the case of method overloading(consider it different function all together)
// so we need to again define it to be virtual here(for appropriate binding behaviour in this class and WeightedDirectedGraph class)
virtual void addEdge(int v, int w, int weight);
virtual void removeEdge(int v, int w);
vector<int> bfs(int node);
vector<int> dfs(int node);
void dfsHelper(int node, vector<int> &visited, vector<int> &ans);
int numConnectedComponents();
virtual int ShortestPath(int start, int target);
vector<pair<int,int>> MinimumSpanningTree();
// display has same number of arguments so no need to define it to be virtual here(as already done in Graph class)
void display();
friend ostream & operator << (ostream &out, const WeightedGraph &graph);
};
vector<int> WeightedGraph::bfs(int vertex)
{
vector<int> ans;
queue<int> q;
q.push(vertex);
vector<int> visited(numNodes, 0);
visited[vertex] = 1;
while (!q.empty())
{
int node = q.front();
q.pop();
ans.push_back(node);
for (auto neighbour : adjList[node])
{
if (!visited[neighbour.first])
{
visited[neighbour.first] = 1;
q.push(neighbour.first);
}
}
}
return ans;
}
void WeightedGraph::dfsHelper(int node, vector<int> &visited, vector<int> &ans)
{
visited[node] = 1;
ans.push_back(node);
for (auto neighbour : adjList[node])
{
if (!visited[neighbour.first])
{
dfsHelper(neighbour.first, visited, ans);
}
}
}
vector<int> WeightedGraph::dfs(int node)
{
vector<int> ans;
vector<int> visited(numNodes, 0);
visited[node] = 1;
ans.push_back(node);
for (auto neighbour : adjList[node])
{
dfsHelper(neighbour.first, visited, ans);
}
return ans;
}
ostream & operator << (ostream &out, const WeightedGraph &graph){
for (int node = 0; node < graph.numNodes; node++){
out << node << " -> ";
for(auto neighbour : graph.adjList[node]){
out << neighbour.first << " " << neighbour.second << " ";
}
out << endl;
}
return out;
}
WeightedGraph::WeightedGraph(int nodes) : Graph(nodes)
{
// since adjList of this class is different from Graph class, we need to resize it here
adjList.resize(numNodes);
}
void WeightedGraph::addEdge(int v, int w, int weight)
{
adjList[v].push_back(make_pair(w, weight));
adjList[w].push_back(make_pair(v, weight));
}
void WeightedGraph::removeEdge(int v, int w)
{
for (auto itr = adjList[v].begin(); itr != adjList[v].end(); itr++)
{
if (itr->first == w)
{
adjList[v].erase(itr);
break;
}
}
for (auto itr = adjList[w].begin(); itr != adjList[w].end(); itr++)
{
if (itr->first == v)
{
adjList[w].erase(itr);
break;
}
}
}
void WeightedGraph::numConnectedComponenetsHelper(int node, vector<int> & visited)
{
visited[node] = 1;
for (auto neighbour : adjList[node])
{
if (!visited[neighbour.first])
{
WeightedGraph::numConnectedComponenetsHelper(neighbour.first, visited);
}
}
}
int WeightedGraph::numConnectedComponents()
{
vector<int> visited(numNodes, 0);
int result = 0;
for (int node = 0; node < numNodes; node++)
{
if (!visited[node])
{
WeightedGraph::numConnectedComponenetsHelper(node, visited);
result++;
}
}
return result;
}
bool WeightedGraph::isCyclicHelper(int node, vector<int> & visited, int parent)
{
visited[node] = 1;
for(auto neighbour : adjList[node]){
if(!visited[neighbour.first]){
if(WeightedGraph::isCyclicHelper(neighbour.first, visited, node)){
return true;
}
}else if(neighbour.first != parent){
return true;
}
}
return false;
}
int WeightedGraph::ShortestPath(int start, int destination)
{
vector<int> dp(numNodes, 1e9);
dp[start] = 0;
priority_queue <pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> q;
q.push(make_pair(0, start));
while(!q.empty())
{
auto [distance , node] = q.top();
q.pop();
if (node == destination) break;
for (auto neighbour : adjList[node])
{
if (dp[neighbour.first] > distance + neighbour.second)
{
dp[neighbour.first] = distance + neighbour.second;
q.push(make_pair(dp[neighbour.first], neighbour.first));
}
}
}
// return -1 for not reachable from start to destination
if (dp[destination] == 1e9) return -1;
return dp[destination];
}
vector<pair<int,int>> WeightedGraph::MinimumSpanningTree()
{
vector<int> included(numNodes, 0);
// stores the minimum edge weight through which the any node was encountered
vector<int> dp(numNodes, 1e9);
dp[0] = 0;
vector<int> parent(numNodes);
parent[0] = 0;
priority_queue <pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> q;
q.push(make_pair(0, 0));
while(!q.empty())
{
auto [distance , node] = q.top();
q.pop();
if (included[node] == 1) continue;
included[node] = 1;
for (auto neighbour : adjList[node])
{
if (!included[neighbour.first] && dp[neighbour.first] > neighbour.second) {
dp[neighbour.first] = neighbour.second;
parent[neighbour.first] = node;
q.push(make_pair(dp[neighbour.first], neighbour.first));
}
}
}
vector<pair<int,int>> ans;
for (int node = 1; node < numNodes; node++)
{
ans.push_back(make_pair(node, parent[node]));
}
return ans;
}
void WeightedGraph::display()
{
for (int i = 0; i < numNodes; i++)
{
cout << i << " -> ";
for (int j = 0; j < adjList[i].size(); j++)
{
cout << adjList[i][j].first << " " << adjList[i][j].second << " ";
}
cout << endl;
}
}
class WeightedDirectedGraph : public WeightedGraph
{
public:
WeightedDirectedGraph(int vertices);
void addEdge(int v, int w, int weight);
void removeEdge(int v, int w);
};
WeightedDirectedGraph::WeightedDirectedGraph(int vertices) : WeightedGraph(vertices) {}
void WeightedDirectedGraph::addEdge(int v, int w, int weight)
{
adjList[v].push_back(make_pair(w, weight));
}
void WeightedDirectedGraph::removeEdge(int v, int w)
{
for (auto itr = adjList[v].begin(); itr != adjList[v].end(); itr++)
{
if (itr->first == w)
{
adjList[v].erase(itr);
break;
}
}
}
int main()
{
WeightedGraph g(8);
g.addEdge(0, 1, 1);
g.addEdge(0, 2, 1);
g.addEdge(0, 3, 1);
g.addEdge(2, 3, 1);
g.addEdge(4, 5, 1);
g.addEdge(5, 7, 1);
g.addEdge(4, 6, 1);
g.addEdge(6, 7, 1);
g.addEdge(3, 4, 1);
cout << "Graph :\n";
cout << g;
cout << '\n';
cout << "Number of Connected Components: ";
cout << g.numConnectedComponents();
return 0;
}