From 5dbf857fb41bfbbd70b012a4c6014bcfb7e451ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Hl=C3=A1sek?= Date: Tue, 4 Aug 2020 00:00:52 -0700 Subject: [PATCH] Fix global variable name in topological_sort. --- graph/topological_sort.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/graph/topological_sort.cpp b/graph/topological_sort.cpp index f77780cdc..3827b9bcf 100644 --- a/graph/topological_sort.cpp +++ b/graph/topological_sort.cpp @@ -2,44 +2,44 @@ #include #include -int n, m; // For number of Vertices (V) and number of edges (E) -std::vector> G; +int number_of_vertices, number_of_edges; // For number of Vertices (V) and number of edges (E) +std::vector> graph; std::vector visited; -std::vector ans; +std::vector topological_order; void dfs(int v) { visited[v] = true; - for (int u : G[v]) { + for (int u : graph[v]) { if (!visited[u]) { dfs(u); } } - ans.push_back(v); + topological_order.push_back(v); } void topological_sort() { - visited.assign(n, false); - ans.clear(); - for (int i = 0; i < n; ++i) { + visited.assign(number_of_vertices, false); + topological_order.clear(); + for (int i = 0; i < number_of_vertices; ++i) { if (!visited[i]) { dfs(i); } } - reverse(ans.begin(), ans.end()); + reverse(topological_order.begin(), topological_order.end()); } int main() { std::cout << "Enter the number of vertices and the number of directed edges\n"; - std::cin >> n >> m; + std::cin >> number_of_vertices >> number_of_edges; int x = 0, y = 0; - G.resize(n, std::vector()); - for (int i = 0; i < n; ++i) { + graph.resize(number_of_vertices, std::vector()); + for (int i = 0; i < number_of_edges; ++i) { std::cin >> x >> y; x--, y--; // to convert 1-indexed to 0-indexed - G[x].push_back(y); + graph[x].push_back(y); } topological_sort(); std::cout << "Topological Order : \n"; - for (int v : ans) { + for (int v : topological_order) { std::cout << v + 1 << ' '; // converting zero based indexing back to one based. }