-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTopological_Sort
More file actions
147 lines (119 loc) · 2.82 KB
/
Topological_Sort
File metadata and controls
147 lines (119 loc) · 2.82 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
Topological Sort of Directed Acyclic Graph
Detailed video explanation: https://youtu.be/3HHlOG05qEo
============================================
C++:
----
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
class Graph{
int m_v;
vector<vector<int>> m_adj;
void TS_rec(int s, vector<bool>& visited, stack<int>& S){
visited[s] = true;
for(int u: m_adj[s]){
if(!visited[u])
TS_rec(u, visited, S);
}
S.push(s);
}
public:
Graph(int v):m_v(v), m_adj(v){}
void addEdge(int u, int v){
m_adj[u].push_back(v);
}
void Top_Sort(){
vector<bool> visited(m_v, false);
stack<int> S;
for(int i = 0; i < m_v; ++i)
if(!visited[i]) TS_rec(i, visited, S);
while(!S.empty()){
cout << S.top() << " ";
S.pop();
}
}
};
int main(){
Graph G(5);
G.addEdge(0,1);
G.addEdge(0,3);
G.addEdge(0,4);
G.addEdge(1,2);
G.addEdge(4,2);
G.addEdge(3,4);
G.Top_Sort();
return 0;
}
Java:
-----
import java.util.*;
import java.lang.*;
import java.io.*;
public class Graph{
private int m_v;
private List<Integer>[] m_adj;
Graph(int v){
m_v = v;
m_adj = new LinkedList[v];
for(int i = 0; i < v; ++i)
m_adj[i] = new LinkedList<Integer>();
}
public void addEdge(int u, int v){
m_adj[u].add(v);
}
private void TS_rec(int s, boolean[] visited,Stack<Integer> S){
visited[s] = true;
for(int u: m_adj[s]){
if(!visited[u])
TS_rec(u, visited, S);
}
S.push(s);
}
public void Top_Sort(){
boolean[] visited = new boolean[m_v];
Stack<Integer> S = new Stack<>();
for(int i = 0; i < m_v; ++i)
if(!visited[i]) TS_rec(i, visited, S);
while(!S.empty())
System.out.println(S.pop());
}
public static void main(String []args){
Graph G = new Graph(5);
G.addEdge(0,1);
G.addEdge(0,3);
G.addEdge(0,4);
G.addEdge(1,2);
G.addEdge(4,2);
G.addEdge(3,4);
G.Top_Sort();
}
}
Python3:
-------
class Graph:
def __init__(self, v):
self.m_v = v
self.m_adj = [[] for i in range(v)]
def addEdge(self, u, v):
self.m_adj[u].append(v)
def TS_rec(self, s, visited):
visited[s] = True
for u in self.m_adj[s]:
if not visited[u]: self.TS_rec(u, visited)
self.S.append(s)
def Top_Sort(self):
visited = [False]*self.m_v
self.S = []
for i in range(self.m_v):
if not visited[i]:
self.TS_rec(i, visited)
print(self.S)
G = Graph(5)
G.addEdge(0,1);
G.addEdge(0,3);
G.addEdge(0,4);
G.addEdge(1,2);
G.addEdge(4,2);
G.addEdge(3,4);
G.Top_Sort()