From 85086f246598fa7211650f6ee90fc40d8fbbca04 Mon Sep 17 00:00:00 2001 From: Ellika Mishra <55554508+ellikamishra@users.noreply.github.com> Date: Fri, 9 Jul 2021 18:55:18 +0530 Subject: [PATCH] chore(CPlusPlus): topological sort on graphs (#381) Co-authored-by: Ujjwal <75884061+UG-SEP@users.noreply.github.com> --- .../CPlusPlus/Graphs/topological-sort.cpp | 170 ++++++++++++++++++ algorithms/CPlusPlus/README.md | 1 + 2 files changed, 171 insertions(+) create mode 100644 algorithms/CPlusPlus/Graphs/topological-sort.cpp diff --git a/algorithms/CPlusPlus/Graphs/topological-sort.cpp b/algorithms/CPlusPlus/Graphs/topological-sort.cpp new file mode 100644 index 00000000..2ba5a5ed --- /dev/null +++ b/algorithms/CPlusPlus/Graphs/topological-sort.cpp @@ -0,0 +1,170 @@ +//Topological sort to check order using dfs + +#include + +using namespace std; + +/* +Using graph class with vector of nodes to represent DAG(Directed acyclic graph) +adding edge and display functions in class along with Cycle to check for cycle +and if no cycle exits then print a topological sort. +*/ +class Graph { + + int n = 0; + vector < int > * EdgeList; + + public: + Graph() { + n = 0; + } + + Graph(int s) { //initialize edgelist as vector of nodes + n = s; + EdgeList = new vector < int > [n]; + } + void addEdge(int src, int dest) { //add edges + + EdgeList[src].push_back(dest); + + } + + bool Cycle(int v, vector < int > & visited, vector < int > & rec) { //Check cycle + + if (!visited[v]) { + + visited[v] = 1; + rec[v] = 1; + + for (auto it: EdgeList[v]) { + + if (!visited[it] && Cycle(it, visited, rec)) return true; + + else if (rec[it]) return true; + + } + } + + rec[v] = 0; + return false; + } + + void TopSort(int v, vector < int > & visited, stack < int > & s) { //Topological sorted elements added in stack + + visited[v] = 1; + vector < int > ::iterator it; + for (it = EdgeList[v].begin(); it != EdgeList[v].end(); it++) { + + if (!visited[ * it]) TopSort( * it, visited, s); //visit graph in order(depth first) for topological + + } + + s.push(v); + + } + +}; + +int main() { + + int flag = 0, n, k, src, dest; + cout << "\nEnter no. of nodes\n"; //input number of nodes + cin >> n; + Graph g(n); + vector < int > visited(n, 0); //stack to keep track of topological sort,visited vector to keep track of nodes visited and rec stack to detect cycle + vector < int > rec(n, 0); + stack < int > s; + + for (int i = 0; i < n; i++) { + cout << "Enter 1 to add edge from node " << i << ",0 to exit" << "\n"; //take input + cin >> k; + while (k) { + + cout << "\nEnter dest\n"; + cin >> dest; + g.addEdge(i, dest); + + cout << "Enter 1 to add edge from node " << i << ",0 to exit" << "\n"; + cin >> k; + } + } + + for (int i = 0; i < n; i++) { + if (g.Cycle(i, visited, rec)) { //check for cycle + + flag = 1; + break; + } + } + + if (flag) cout << "Not DAG"; //if not cycle then find topological sort + + else { + + fill(visited.begin(), visited.end(), 0); + + for (int i = 0; i < n; i++) { + if (!visited[i]) g.TopSort(i, visited, s); + } + cout << "\nOne topological sorted order: "; + while (!s.empty()) { //print stack contents + + cout << s.top() << " "; + s.pop(); + } + } + + return 0; +} + +/* + +Time Complexity-O(V+E) +Space Complexity-O(V) + +Enter no. of nodes +6 +Enter 1 to add edge from node 0,0 to exit +0 +Enter 1 to add edge from node 1,0 to exit +0 +Enter 1 to add edge from node 2,0 to exit +1 + +Enter dest +3 +Enter 1 to add edge from node 2,0 to exit +0 +Enter 1 to add edge from node 3,0 to exit +1 + +Enter dest +1 +Enter 1 to add edge from node 3,0 to exit +0 +Enter 1 to add edge from node 4,0 to exit +1 + +Enter dest +0 +Enter 1 to add edge from node 4,0 to exit +1 + +Enter dest +1 +Enter 1 to add edge from node 4,0 to exit +0 +Enter 1 to add edge from node 5,0 to exit +1 + +Enter dest +0 +Enter 1 to add edge from node 5,0 to exit +1 + +Enter dest +2 +Enter 1 to add edge from node 5,0 to exit +0 +One Topological sorted order: 5 4 2 3 1 0 +*/ diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 083ed512..3ec34a3c 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -17,6 +17,7 @@ 1. [Bellman Ford Algorithm](Graphs/bellmam-ford.cpp) 2. [kruskal Algorithm](Graphs/kruskal-algorithm.cpp) 3. [Breadth First Search](Graphs/breadth-first-search.cpp) +4. [Topological sort](Graphs/topological-sort.cpp) ## Multiplication