From 94e9f5771aa58883e216154e9dd3f24e2de4c4a4 Mon Sep 17 00:00:00 2001 From: Amulya Gaur Date: Sun, 28 Oct 2018 18:52:48 +0530 Subject: [PATCH] added Topological Sorting code in c++ C++ code to find lexicographically smallest topological order of a DAG. --- OPEN CHALLENGE/TopoSort.cpp | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 OPEN CHALLENGE/TopoSort.cpp diff --git a/OPEN CHALLENGE/TopoSort.cpp b/OPEN CHALLENGE/TopoSort.cpp new file mode 100644 index 00000000..84939355 --- /dev/null +++ b/OPEN CHALLENGE/TopoSort.cpp @@ -0,0 +1,43 @@ +//Prints the Lexicographically smallest TS order. +#include +using namespace std; +#define endl '\n' +vector< vector > v(100); +vector visit(100,false); +stack st; +void toposort(int s) +{ + visit[s]=true; + for(int i=0;i>n>>m; + while(m--) + { + int x,y; + cin>>x>>y; + v[x].push_back(y); + } + for(int i=1;i<=n;i++) + sort(v[i].rbegin(),v[i].rend()); + for(int i=n;i>=1;i--) + if(visit[i]==false) + toposort(i); + while(!st.empty()) + { + cout<