Skip to content

Commit

Permalink
Merge pull request #678 from amulyagaur/patch-5
Browse files Browse the repository at this point in the history
added Topological Sorting code in c++
  • Loading branch information
TANIYA GUPTA authored Oct 29, 2018
2 parents 3ec0700 + 94e9f57 commit 5246cb1
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions OPEN CHALLENGE/TopoSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//Prints the Lexicographically smallest TS order.
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
vector< vector<int> > v(100);
vector<bool> visit(100,false);
stack<int> st;
void toposort(int s)
{
visit[s]=true;
for(int i=0;i<v[s].size();i++)
{
if(visit[v[s][i]]==false)
{
toposort(v[s][i]);
}
}
st.push(s);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
cin>>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<<st.top()<<" ";
st.pop();
}
return 0;
}

0 comments on commit 5246cb1

Please sign in to comment.