-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphAlgos.cpp
More file actions
95 lines (89 loc) · 2.14 KB
/
Copy pathgraphAlgos.cpp
File metadata and controls
95 lines (89 loc) · 2.14 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
#include<bits/stdc++.h>
using namespace std;
vector<int> calc_Indegree(vector<vector<int> > &graph, int V) {
vector<int> indegree(V, 0);
for(int i = 0; i < V; i++) {
for(int v : graph[i]) {
indegree[v]++;
}
}
return indegree;
}
void topologicalSort_bfs(vector<vector<int> > &graph, int V) { // Kahn's Algo, time: O(V + E), space: O(V)
vector<int> indegree = calc_Indegree(graph, V);
queue<int> q;
for(int i = 0; i < V; i++) {
if(indegree[i] == 0) {
q.push(i);
}
}
while(!q.empty()) {
int u = q.front();
cout << u << " ";
q.pop();
for(int v : graph[u]) {
indegree[v]--;
if(indegree[v] == 0) {
q.push(v);
}
}
}
}
void dfs(int s, stack<int> &st, vector<vector<int> > &graph, vector<bool> &vis) {
vis[s] = true;
for(int u : graph[s]) {
if(vis[u] == false) {
dfs(u, st, graph, vis);
}
}
st.push(s);
}
void topologicalSort_dfs(vector<vector<int> > &graph, int V) {
vector<bool> vis(V, false);
stack<int> st;
for(int i = 0; i < V; i++) {
if(vis[i] == false) {
dfs(i, st, graph, vis);
}
}
while(!st.empty()){
int x = st.top();
st.pop();
cout << x << " ";
}
}
bool cycleDetection(int V, vector<vector<int> > graph) { // directed graph
vector<int> indegree = calc_Indegree(graph, V);
queue<int> q;
for(int i = 0; i < V; i++) {
if(indegree[i] == 0){
q.push(i);
}
}
int count = 0;
while(!q.empty()) {
int u = q.front();
q.pop();
for(int v : graph[u]) {
indegree[v]--;
if(indegree[v] == 0){
q.push(v);
count++;
}
}
}
return count != V;
}
int main(){
int V = 5, E = 5;
vector<vector<int> > graph(V);
while(E--) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
}
// topologicalSort_bfs(graph, V);
// topologicalSort_dfs(graph, V);
cout << cycleDetection(V, graph);
return 0;
}