1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ /*
5
+ Input Format: number of vertices of the graph, number of edges of the graph, folloewed by the edges of the directed graph
6
+ Output Format: If the graph has cycle then Graph contains cycle else the topological sort of the vertices of the graph
7
+ ALgorithm: Kahn's Algorithm
8
+ Time Complexity: O(V + E), V = number of vertices of the graph, E = number of edges of the graph
9
+ Space Complexity: O(V + E)
10
+
11
+ Sample Input and Output
12
+
13
+ **Sample Input 1:**
14
+
15
+ Enter the number of vertices of the graph: 8
16
+
17
+ Enter the number of edges of the graph: 9
18
+
19
+ Enter edges of the graph in the format u v, u has a directed edge to v:
20
+ 1 4
21
+ 1 2
22
+ 4 2
23
+ 4 3
24
+ 3 2
25
+ 5 2
26
+ 3 5
27
+ 8 2
28
+ 8 6
29
+
30
+ Topological sort of the graph is: 1 4 3 5 7 8 2 6
31
+
32
+
33
+ **Sample Input 2:**
34
+
35
+ Enter the number of vertices of the graph: 2
36
+
37
+ Enter the number of edges of the graph: 2
38
+
39
+ Enter edges of the graph in the format u v, u has a directed edge to v:
40
+ 1 2
41
+ 2 1
42
+
43
+ Graph contains cycle!
44
+ */
45
+
46
+ int main ()
47
+ {
48
+
49
+ /*
50
+ n is the number of vertices of the graph
51
+ m is the number of edges of the graph
52
+ Note: here graph means a directed graph
53
+ */
54
+
55
+ int n, m;
56
+ cout << " \n Enter the number of vertices of the graph: " ;
57
+ cin >> n;
58
+ cout << " \n Enter the number of edges of the graph: " ;
59
+ cin >> m;
60
+ vector<int > adj[n + 1 ]; // stores the adjacency list format of the graph
61
+ vector<int > indeg (n + 1 , 0 ); // stores the indegree of all vertices of the graph
62
+
63
+ cout << " \n Enter edges of the graph in the format u v, u has a directed edge to v: \n " ;
64
+ for (int i = 0 ; i < m; i++)
65
+ {
66
+ int u, v;
67
+ cin >> u >> v;
68
+ adj[u].push_back (v);
69
+ indeg[v]++;
70
+ }
71
+
72
+ /*
73
+ open stores the vertices whose indegrees are zero
74
+ priority queue with min heap will give the lexicographically smallest topological sort of the graph
75
+ if the order of the topological sort doesn't matter, replace
76
+ priority_queue <int, vector <int>, greater<int>> open;
77
+ with queue<int> open;
78
+ and replace top() function with front()
79
+ */
80
+ priority_queue<int , vector<int >, greater<int >> open ;
81
+ for (int i = 1 ; i <= n; i++)
82
+ {
83
+ if (indeg[i] == 0 )
84
+ {
85
+ open .push (i);
86
+ }
87
+ }
88
+
89
+ vector<int > toposort; // this stores the topological sort of the graph
90
+ while (!open .empty ())
91
+ {
92
+ int node = open .top ();
93
+ toposort.push_back (node);
94
+ open .pop ();
95
+ for (auto u : adj[node])
96
+ {
97
+ indeg[u]--;
98
+ // if after decreasing the indegree the indegree of the vertex becomes zero include in the open
99
+ if (indeg[u] == 0 )
100
+ {
101
+ open .push (u);
102
+ }
103
+ }
104
+ }
105
+
106
+ // if toposort vector doesnt contain all the vertices of the graph then it contains a cycle
107
+ if ((int )toposort.size () != n)
108
+ {
109
+ cout << " \n Graph contains cycle!" << ' \n ' ;
110
+ }
111
+ else
112
+ {
113
+ cout << " \n Topological sort of the graph is: " ;
114
+ for (auto u : toposort)
115
+ {
116
+ cout << u << " " ;
117
+ }
118
+ cout << ' \n ' ;
119
+ }
120
+ }
0 commit comments