├── BFS ├── Cycle_Detection_Directed_Graph ├── Cycle_Detection_Undirected_graph ├── DFS ├── Kosaraju_Strongly_Connected_Components ├── Pre_Post_Visit_Times_DFS ├── README.md └── Topological_Sort /BFS: -------------------------------------------------------------------------------- 1 | Breadth First Search(BFS) traversal 2 | Detailed Explanation available here: https://youtu.be/6J50_2SD0C8 3 | 4 | // C++ 5 | 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | class Graph{ 11 | int m_V; 12 | vector> m_adj; 13 | public: 14 | Graph(int V): m_V(V), m_adj(V){} 15 | void addEdge(int u, int v){ 16 | m_adj[u].push_back(v); 17 | } 18 | void BFS(int s){ 19 | queue Q; 20 | vector visited(m_V, false); 21 | Q.push(s); 22 | visited[s] = true; 23 | while(!Q.empty()){ 24 | int v = Q.front(); 25 | Q.pop(); 26 | cout << v << " "; 27 | for(int u : m_adj[v]) 28 | if(!visited[u]){ 29 | Q.push(u); 30 | visited[u] = true; 31 | } 32 | } 33 | } 34 | }; 35 | 36 | int main(){ 37 | Graph G(5); 38 | G.addEdge(0,1); 39 | G.addEdge(0,3); 40 | G.addEdge(1,2); 41 | G.addEdge(3,2); 42 | G.addEdge(3,4); 43 | G.BFS(0); 44 | 45 | return 0; 46 | } 47 | 48 | // Java 49 | import java.util.*; 50 | import java.lang.*; 51 | import java.io.*; 52 | 53 | class Graph { 54 | private int m_V; 55 | private List[] m_adj; 56 | Graph(int v){ 57 | m_V = v; 58 | m_adj = new LinkedList[v]; 59 | for(int i = 0; i < v; ++i) 60 | m_adj[i] = new LinkedList(); 61 | } 62 | 63 | public void addEdge(int u, int v){ 64 | m_adj[u].add(v); 65 | } 66 | 67 | public void BFS(int s){ 68 | LinkedList Q = new LinkedList(); 69 | boolean[] visited = new boolean[m_V]; 70 | Q.add(s); 71 | visited[s] = true; 72 | while(!Q.isEmpty()){ 73 | int v = Q.poll(); 74 | System.out.println(v+" "); 75 | for(int u : m_adj[v]){ 76 | if(!visited[u]){ 77 | Q.add(u); 78 | visited[u] = true; 79 | } 80 | } 81 | } 82 | } 83 | 84 | public static void main (String[] args) throws java.lang.Exception{ 85 | Graph G = new Graph(5); 86 | G.addEdge(0,1); 87 | G.addEdge(0,3); 88 | G.addEdge(1,2); 89 | G.addEdge(3,2); 90 | G.addEdge(3,4); 91 | G.BFS(0); 92 | } 93 | } 94 | 95 | 96 | # Python3 97 | 98 | class Graph: 99 | def __init__(self, v): 100 | self.m_V = v 101 | self.m_adj = [[] for i in range(v)] 102 | 103 | def addEdge(self, u, v): 104 | self.m_adj[u].append(v) 105 | 106 | def BFS(self, s): 107 | Q = [] 108 | visited = [False]*self.m_V 109 | Q.append(s) 110 | visited[s] = True 111 | while len(Q) > 0: 112 | v = Q.pop(0) 113 | print(f'{v} ') 114 | for u in self.m_adj[v]: 115 | if not visited[u]: 116 | Q.append(u) 117 | visited[u] = True 118 | 119 | G = Graph(5) 120 | G.addEdge(0,1) 121 | G.addEdge(0,3) 122 | G.addEdge(1,2) 123 | G.addEdge(3,2) 124 | G.addEdge(3,4) 125 | G.BFS(0) 126 | -------------------------------------------------------------------------------- /Cycle_Detection_Directed_Graph: -------------------------------------------------------------------------------- 1 | // Detect Cycle in Directed Graph 2 | // Author: Knowledge Center 3 | // https://www.youtube.com/c/KnowledgeCenter/ 4 | // Video Explanation: https://youtu.be/1CdgY5KTQQE 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | enum node_states_en{ 13 | UNVISITED, 14 | INSTACK, 15 | VISITED 16 | }; 17 | 18 | class Graph{ 19 | int m_v; 20 | vector> m_adj; 21 | int time; 22 | bool DFS_rec(int s, vector& visited){ 23 | visited[s] = INSTACK; 24 | //cout << s << endl; 25 | for(int u: m_adj[s]){ 26 | if(visited[u] == INSTACK) return true; 27 | if(visited[u] == UNVISITED && DFS_rec(u, visited)) return true; 28 | } 29 | visited[s] = VISITED; 30 | return false; 31 | } 32 | public: 33 | Graph(int v):m_v(v), m_adj(v), time(0){} 34 | 35 | void addEdge(int u, int v){ 36 | m_adj[u].push_back(v); 37 | } 38 | 39 | bool hasCycle(){ 40 | vector visited(m_v, UNVISITED); 41 | for(int i = 0; i < m_v; ++i) 42 | if(visited[i] == UNVISITED && DFS_rec(i, visited)) return true; 43 | return false; 44 | } 45 | }; 46 | 47 | int main(){ 48 | Graph G(5); 49 | G.addEdge(0,1); 50 | G.addEdge(0,3); 51 | G.addEdge(1,2); 52 | G.addEdge(3,4); 53 | G.addEdge(4,0); 54 | G.addEdge(4,2); 55 | bool hasCycle = G.hasCycle(); 56 | cout << std::boolalpha << hasCycle << endl; 57 | 58 | return 0; 59 | } 60 | 61 | -------------------------------------------------------------------------------- /Cycle_Detection_Undirected_graph: -------------------------------------------------------------------------------- 1 | // Detect Cycle in Undirected Graph 2 | // Author: Knowledge Center 3 | // https://www.youtube.com/c/KnowledgeCenter/ 4 | // Video Explanation: https://youtu.be/Gx-uJSqIa1Q 5 | 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | class Graph{ 11 | int m_v; 12 | vector> m_adj; 13 | 14 | bool hasCycle_rec(int u, vector& visited, int parent){ 15 | visited[u] = true; 16 | //cout << u << endl; 17 | for(int v : m_adj[u]){ 18 | if(visited[v] && v != parent) return true; 19 | if(!visited[v] && hasCycle_rec(v, visited, u)) return true; 20 | } 21 | return false; 22 | } 23 | public: 24 | Graph(int v):m_v(v), m_adj(v){} 25 | 26 | void add_Edge(int u, int v){ 27 | m_adj[u].push_back(v); 28 | m_adj[v].push_back(u); 29 | } 30 | 31 | bool hasCycle(){ 32 | vector visited(m_v, false); 33 | for(int i = 0; i < m_v; ++i){ 34 | if(!visited[i] && hasCycle_rec(i, visited, -1)) return true; 35 | } 36 | return false; 37 | } 38 | }; 39 | 40 | int main(){ 41 | Graph G(5); 42 | G.add_Edge(0,1); 43 | G.add_Edge(0,3); 44 | //G.add_Edge(0,4); 45 | G.add_Edge(1,2); 46 | G.add_Edge(3,4); 47 | //G.add_Edge(2,4); 48 | bool cycle = G.hasCycle(); 49 | cout << boolalpha << cycle << endl; 50 | 51 | return 0; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /DFS: -------------------------------------------------------------------------------- 1 | // Defpth First Search(DFS) Traversal of Graph 2 | // Detailed Explanation available here: https://youtu.be/tXSk6POIBJA 3 | 4 | // C++ 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class Graph{ 12 | int m_v; 13 | vector> m_adj; 14 | void DFS_rec(int s, vector& visited){ 15 | visited[s] = true; 16 | cout << s << endl; 17 | for(int u: m_adj[s]){ 18 | if(!visited[u]) 19 | DFS_rec(u, visited); 20 | } 21 | } 22 | 23 | void DFS_it(int s, vector& visited){ 24 | stack S; 25 | S.push(s); 26 | visited[s] = true; 27 | 28 | while(!S.empty()){ 29 | int u = S.top(); 30 | S.pop(); 31 | cout << u << endl; 32 | for(int v: m_adj[u]){ 33 | if(!visited[v]){ 34 | S.push(v); 35 | visited[v] = true; 36 | } 37 | } 38 | } 39 | } 40 | public: 41 | Graph(int v):m_v(v), m_adj(v){} 42 | 43 | void addEdge(int u, int v){ 44 | m_adj[u].push_back(v); 45 | } 46 | 47 | void DFS(){ 48 | vector visited(m_v, false); 49 | for(int i = 0; i < m_v; ++i) 50 | if(!visited[i]) DFS_rec(i, visited); 51 | } 52 | }; 53 | 54 | int main(){ 55 | Graph G(5); 56 | G.addEdge(0,1); 57 | G.addEdge(0,3); 58 | G.addEdge(1,2); 59 | G.addEdge(3,2); 60 | G.addEdge(3,4); 61 | G.DFS(); 62 | 63 | return 0; 64 | } 65 | 66 | 67 | // Java 68 | 69 | import java.util.*; 70 | import java.lang.*; 71 | import java.io.*; 72 | 73 | public class Graph{ 74 | private int m_v; 75 | private List[] m_adj; 76 | Graph(int v){ 77 | m_v = v; 78 | m_adj = new LinkedList[v]; 79 | for(int i = 0; i < v; ++i) 80 | m_adj[i] = new LinkedList(); 81 | } 82 | 83 | public void addEdge(int u, int v){ 84 | m_adj[u].add(v); 85 | } 86 | 87 | private void DFS_rec(int s, boolean[] visited){ 88 | visited[s] = true; 89 | System.out.println(s); 90 | for(int u: m_adj[s]){ 91 | if(!visited[u]) 92 | DFS_rec(u, visited); 93 | } 94 | } 95 | private void DFS_it(int s, boolean[] visited){ 96 | Stack S = new Stack(); 97 | S.push(s); 98 | visited[s] = true; 99 | 100 | while(!S.empty()){ 101 | int u = S.peek(); 102 | S.pop(); 103 | System.out.println(u); 104 | for(int v: m_adj[u]){ 105 | if(!visited[v]){ 106 | S.push(v); 107 | visited[v] = true; 108 | } 109 | } 110 | } 111 | 112 | public void DFS(){ 113 | boolean[] visited = new boolean[m_v]; 114 | for(int i = 0; i < m_v; ++i) 115 | if(!visited[i]) DFS_rec(i, visited); 116 | } 117 | 118 | public static void main(String []args){ 119 | Graph G = new Graph(5); 120 | G.addEdge(0,1); 121 | G.addEdge(0,3); 122 | G.addEdge(1,2); 123 | G.addEdge(3,2); 124 | G.addEdge(3,4); 125 | G.DFS(); 126 | } 127 | } 128 | 129 | // Python3 130 | 131 | class Graph: 132 | def __init__(self, v): 133 | self.m_v = v 134 | self.m_adj = [[] for i in range(v)] 135 | 136 | def addEdge(self, u, v): 137 | self.m_adj[u].append(v) 138 | 139 | def DFS_rec(self, s, visited): 140 | visited[s] = True 141 | print(s) 142 | for u in self.m_adj[s]: 143 | if not visited[u]: 144 | self.DFS_rec(u, visited) 145 | 146 | def DFS_it(self, s, visited): 147 | S = [] 148 | S.append(s) 149 | visited[s] = True 150 | while len(S) > 0: 151 | u = S[-1] 152 | S.pop() 153 | print(u) 154 | for v in self.m_adj[u]: 155 | if not visited[v]: 156 | S.append(v) 157 | visited[v] = True 158 | 159 | def DFS(self): 160 | visited = [False]*self.m_v 161 | for i in range(self.m_v): 162 | if not visited[i]: 163 | self.DFS_rec(i, visited) 164 | 165 | G = Graph(5) 166 | G.addEdge(0,1) 167 | G.addEdge(0,3) 168 | G.addEdge(1,2) 169 | G.addEdge(3,2) 170 | G.addEdge(3,4) 171 | G.DFS() 172 | -------------------------------------------------------------------------------- /Kosaraju_Strongly_Connected_Components: -------------------------------------------------------------------------------- 1 | Kosaraju's Algorithm for Strongly connected components in directed graph. 2 | Detailed video explanation: https://youtu.be/ZKb6jSpmbgk 3 | ========================================================================== 4 | 5 | 6 | C++: 7 | ------------------------------ 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | class Graph{ 15 | int m_v; 16 | vector> m_adj; 17 | 18 | void orderVertices(int s, vector& visited, stack& S){ 19 | visited[s] = true; 20 | 21 | for(int u: m_adj[s]) 22 | if(!visited[u]) orderVertices(u, visited, S); 23 | 24 | S.push(s); 25 | } 26 | 27 | Graph reverseGraph(){ 28 | Graph G(m_v); 29 | for(int i = 0; i < m_v; ++i) 30 | for(int u: m_adj[i]) G.m_adj[u].push_back(i); 31 | return G; 32 | } 33 | 34 | void dfs(vector& visited, int s){ 35 | visited[s] = true; 36 | cout << s << " "; 37 | for(int u: m_adj[s]) 38 | if(!visited[u]) dfs(visited, u); 39 | } 40 | public: 41 | Graph(int v):m_v(v), m_adj(v){} 42 | 43 | void addEdge(int u, int v){ 44 | m_adj[u].push_back(v); 45 | } 46 | 47 | void SCC(){ 48 | vector visited(m_v, false); 49 | stack S; 50 | for(int i = 0; i < m_v; ++i) 51 | if(!visited[i]) orderVertices(i, visited, S); 52 | 53 | Graph G = reverseGraph(); 54 | for(int i = 0; i < m_v; ++i) visited[i] = false; 55 | 56 | while(!S.empty()){ 57 | int v = S.top(); 58 | S.pop(); 59 | if(!visited[v]){ 60 | G.dfs(visited, v); 61 | cout << endl; 62 | } 63 | } 64 | } 65 | }; 66 | 67 | int main(){ 68 | Graph G(9); 69 | G.addEdge(0,1); 70 | G.addEdge(1,2); 71 | G.addEdge(2,3); 72 | G.addEdge(3,0); 73 | G.addEdge(2,4); 74 | G.addEdge(4,5); 75 | G.addEdge(5,6); 76 | G.addEdge(6,4); 77 | G.addEdge(7,6); 78 | G.addEdge(7,8); 79 | 80 | G.SCC(); 81 | 82 | return 0; 83 | } 84 | 85 | 86 | Java: 87 | ------------------------------ 88 | import java.util.*; 89 | import java.lang.*; 90 | import java.io.*; 91 | 92 | public class Graph{ 93 | private int m_v; 94 | private List[] m_adj; 95 | Graph(int v){ 96 | m_v = v; 97 | m_adj = new LinkedList[v]; 98 | for(int i = 0; i < v; ++i) 99 | m_adj[i] = new LinkedList(); 100 | } 101 | 102 | public void addEdge(int u, int v){ 103 | m_adj[u].add(v); 104 | } 105 | 106 | private void orderVertices(int s, boolean[] visited, Stack S){ 107 | visited[s] = true; 108 | for(int u: m_adj[s]){ 109 | if(!visited[u]) 110 | orderVertices(u, visited, S); 111 | } 112 | S.push(s); 113 | } 114 | 115 | private Graph reverseGraph(){ 116 | Graph G = new Graph(m_v); 117 | for(int i = 0; i < m_v; ++i) 118 | for(int u: m_adj[i]) G.m_adj[u].add(i); 119 | return G; 120 | } 121 | 122 | private void dfs(boolean[] visited, int s){ 123 | visited[s] = true; 124 | System.out.print(s + " "); 125 | for(int u: m_adj[s]) 126 | if(!visited[u]) dfs(visited, u); 127 | } 128 | 129 | public void SCC(){ 130 | boolean[] visited = new boolean[m_v]; 131 | Stack S = new Stack<>(); 132 | for(int i = 0; i < m_v; ++i) 133 | if(!visited[i]) orderVertices(i, visited, S); 134 | 135 | for(int i = 0; i < m_v; ++i) visited[i] = false; 136 | Graph G = reverseGraph(); 137 | 138 | while(!S.empty()){ 139 | int v = S.pop(); 140 | if(!visited[v]){ 141 | G.dfs(visited, v); 142 | System.out.println(); 143 | } 144 | } 145 | } 146 | 147 | public static void main(String []args){ 148 | Graph G = new Graph(9); 149 | G.addEdge(0,1); 150 | G.addEdge(1,2); 151 | G.addEdge(2,3); 152 | G.addEdge(3,0); 153 | G.addEdge(2,4); 154 | G.addEdge(4,5); 155 | G.addEdge(5,6); 156 | G.addEdge(6,4); 157 | G.addEdge(7,6); 158 | G.addEdge(7,8); 159 | 160 | G.SCC(); 161 | } 162 | } 163 | 164 | 165 | Python3: 166 | ------------------------------ 167 | class Graph: 168 | def __init__(self, v): 169 | self.m_v = v 170 | self.m_adj = [[] for i in range(v)] 171 | 172 | def addEdge(self, u, v): 173 | self.m_adj[u].append(v) 174 | 175 | def orderVertices(self, s, visited): 176 | visited[s] = True 177 | for u in self.m_adj[s]: 178 | if not visited[u]: self.orderVertices(u, visited) 179 | self.S.append(s) 180 | 181 | def reverseGraph(self): 182 | G = Graph(self.m_v) 183 | for i in range(self.m_v): 184 | for u in self.m_adj[i]: G.m_adj[u].append(i) 185 | return G 186 | 187 | def dfs(self, visited, s): 188 | visited[s] = True 189 | print(f'{s} ', end = "") 190 | for u in self.m_adj[s]: 191 | if not visited[u]: self.dfs(visited, u) 192 | 193 | def SCC(self): 194 | visited = [False]*self.m_v 195 | self.S = [] 196 | for i in range(self.m_v): 197 | if not visited[i]: 198 | self.orderVertices(i, visited) 199 | for i in range(self.m_v): visited[i] = False 200 | G = self.reverseGraph() 201 | 202 | while len(self.S) > 0: 203 | v = self.S.pop() 204 | if not visited[v]: 205 | G.dfs(visited, v) 206 | print() 207 | 208 | G = Graph(9) 209 | G.addEdge(0,1) 210 | G.addEdge(1,2) 211 | G.addEdge(2,3) 212 | G.addEdge(3,0) 213 | G.addEdge(2,4) 214 | G.addEdge(4,5) 215 | G.addEdge(5,6) 216 | G.addEdge(6,4) 217 | G.addEdge(7,6) 218 | G.addEdge(7,8) 219 | 220 | G.SCC() 221 | 222 | 223 | -------------------------------------------------------------------------------- /Pre_Post_Visit_Times_DFS: -------------------------------------------------------------------------------- 1 | Pre and Post Numbers in DFS of Graph 2 | Detailed Video explanation is available here: https://youtu.be/2UvzjtJb-WQ 3 | 4 | C++: 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | class Graph{ 13 | int m_v; 14 | vector> m_adj; 15 | int time; 16 | void DFS_rec(int s, vector& visited, vector& pre, vector& post){ 17 | visited[s] = true; 18 | pre[s] = time++; 19 | cout << s << endl; 20 | for(int u: m_adj[s]){ 21 | if(!visited[u]) 22 | DFS_rec(u, visited, pre, post); 23 | } 24 | post[s] = time++; 25 | } 26 | public: 27 | Graph(int v):m_v(v), m_adj(v), time(0){} 28 | 29 | void addEdge(int u, int v){ 30 | m_adj[u].push_back(v); 31 | } 32 | 33 | void DFS(){ 34 | vector visited(m_v, false); 35 | vector pre(m_v), post(m_v); 36 | for(int i = 0; i < m_v; ++i) 37 | if(!visited[i]) DFS_rec(i, visited, pre, post); 38 | 39 | for(int i = 0; i < m_v; ++i){ 40 | cout << i << " : " << pre[i] << ", " << post[i] << endl; 41 | } 42 | } 43 | }; 44 | 45 | int main(){ 46 | Graph G(5); 47 | G.addEdge(0,1); 48 | G.addEdge(0,3); 49 | G.addEdge(1,2); 50 | G.addEdge(3,2); 51 | G.addEdge(3,4); 52 | G.DFS(); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Graphs 2 | Graph data structure and Algorithms 3 | 4 | ## Complete Playlist on [Graph data structure and Algorithms](https://www.youtube.com/playlist?list=PL1w8k37X_6L9IfRTVvL-tKnrZ_F-8HJQt) on Youtube: 5 | 6 | 7 | -------------------------------------------------------------------------------- /Topological_Sort: -------------------------------------------------------------------------------- 1 | Topological Sort of Directed Acyclic Graph 2 | Detailed video explanation: https://youtu.be/3HHlOG05qEo 3 | ============================================ 4 | 5 | C++: 6 | ---- 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | class Graph{ 14 | int m_v; 15 | vector> m_adj; 16 | void TS_rec(int s, vector& visited, stack& S){ 17 | visited[s] = true; 18 | 19 | for(int u: m_adj[s]){ 20 | if(!visited[u]) 21 | TS_rec(u, visited, S); 22 | } 23 | 24 | S.push(s); 25 | } 26 | public: 27 | Graph(int v):m_v(v), m_adj(v){} 28 | 29 | void addEdge(int u, int v){ 30 | m_adj[u].push_back(v); 31 | } 32 | 33 | void Top_Sort(){ 34 | vector visited(m_v, false); 35 | stack S; 36 | for(int i = 0; i < m_v; ++i) 37 | if(!visited[i]) TS_rec(i, visited, S); 38 | 39 | while(!S.empty()){ 40 | cout << S.top() << " "; 41 | S.pop(); 42 | } 43 | } 44 | }; 45 | 46 | int main(){ 47 | Graph G(5); 48 | G.addEdge(0,1); 49 | G.addEdge(0,3); 50 | G.addEdge(0,4); 51 | G.addEdge(1,2); 52 | G.addEdge(4,2); 53 | G.addEdge(3,4); 54 | G.Top_Sort(); 55 | 56 | return 0; 57 | } 58 | 59 | 60 | Java: 61 | ----- 62 | import java.util.*; 63 | import java.lang.*; 64 | import java.io.*; 65 | 66 | public class Graph{ 67 | private int m_v; 68 | private List[] m_adj; 69 | Graph(int v){ 70 | m_v = v; 71 | m_adj = new LinkedList[v]; 72 | for(int i = 0; i < v; ++i) 73 | m_adj[i] = new LinkedList(); 74 | } 75 | 76 | public void addEdge(int u, int v){ 77 | m_adj[u].add(v); 78 | } 79 | 80 | private void TS_rec(int s, boolean[] visited,Stack S){ 81 | visited[s] = true; 82 | for(int u: m_adj[s]){ 83 | if(!visited[u]) 84 | TS_rec(u, visited, S); 85 | } 86 | S.push(s); 87 | } 88 | 89 | public void Top_Sort(){ 90 | boolean[] visited = new boolean[m_v]; 91 | Stack S = new Stack<>(); 92 | for(int i = 0; i < m_v; ++i) 93 | if(!visited[i]) TS_rec(i, visited, S); 94 | 95 | while(!S.empty()) 96 | System.out.println(S.pop()); 97 | } 98 | 99 | public static void main(String []args){ 100 | Graph G = new Graph(5); 101 | G.addEdge(0,1); 102 | G.addEdge(0,3); 103 | G.addEdge(0,4); 104 | G.addEdge(1,2); 105 | G.addEdge(4,2); 106 | G.addEdge(3,4); 107 | G.Top_Sort(); 108 | } 109 | } 110 | 111 | 112 | Python3: 113 | ------- 114 | class Graph: 115 | def __init__(self, v): 116 | self.m_v = v 117 | self.m_adj = [[] for i in range(v)] 118 | 119 | def addEdge(self, u, v): 120 | self.m_adj[u].append(v) 121 | 122 | def TS_rec(self, s, visited): 123 | visited[s] = True 124 | for u in self.m_adj[s]: 125 | if not visited[u]: self.TS_rec(u, visited) 126 | self.S.append(s) 127 | 128 | def Top_Sort(self): 129 | visited = [False]*self.m_v 130 | self.S = [] 131 | for i in range(self.m_v): 132 | if not visited[i]: 133 | self.TS_rec(i, visited) 134 | print(self.S) 135 | 136 | G = Graph(5) 137 | G.addEdge(0,1); 138 | G.addEdge(0,3); 139 | G.addEdge(0,4); 140 | G.addEdge(1,2); 141 | G.addEdge(4,2); 142 | G.addEdge(3,4); 143 | G.Top_Sort() 144 | 145 | 146 | 147 | 148 | --------------------------------------------------------------------------------