-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_Strongly_Connected_Graph_Efficient.cpp
More file actions
72 lines (64 loc) · 1.93 KB
/
Copy pathcheck_Strongly_Connected_Graph_Efficient.cpp
File metadata and controls
72 lines (64 loc) · 1.93 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
#include <iostream>
#include <vector>
#include "set"
using namespace std;
struct Edge{
int source,destination;
};
class Graph{
vector<vector<int>> adjList;
int V;
public:
Graph(vector<Edge> edges,int V){
this->V = V;
adjList.resize(V);
for(auto i: edges){
adjList[i.source].push_back(i.destination);
//adjList[i.destination].push_back(i.source);
}
}
void checkStronglyConnectedGraphEfficient();
void DFS(int s,vector<bool> &discovered);
void printGraph();
};
void Graph ::printGraph() {
for (int i = 0; i < V; i++)
{
cout << i << " -- ";
for (int v : adjList[i])
cout <<"->"<< v << " ";
cout << endl;
}
}
void Graph ::DFS(int s, vector<bool> &discovered) {
discovered[s] = true;
for(auto i : adjList[s]){
if(!discovered[i]) DFS(i,discovered);
}
}
void Graph ::checkStronglyConnectedGraphEfficient() {
vector<bool> discovered(V,false);
int randVertex = 0;
DFS(randVertex,discovered);
for(auto i : discovered) if(i == false) {cout <<"No Graph is not a Strongly Connected Graph"; return ;}
//reversing all the edges
vector<Edge> edges;
for(int i = 0;i < V ;i++) for(auto j:adjList[i]) edges.push_back({j,i});
Graph graph2(edges,V);
fill(discovered.begin(),discovered.end(),false);
DFS(randVertex,discovered);
for(auto i : discovered) if(i == false) {cout <<"No Graph is not a Strongly Connected Graph"; return ;}
cout <<"Yes Graph is Strongly Connected Graph";
}
int main(){
vector<Edge> edges =
{ {0, 4}, {1, 0}, {1, 2}, {2, 1}, {2, 4},
{3, 1}, {3, 2}, {4, 3}
};
set <int > setsize;
for(auto i : edges){ setsize.insert(i.source);setsize.insert(i.destination);}
int V = setsize.size();
Graph graph(edges, V);
// graph.printGraph();
graph.checkStronglyConnectedGraphEfficient();
}