diff --git a/Topological Sort.cpp b/Topological Sort.cpp new file mode 100644 index 0000000..ad475ba --- /dev/null +++ b/Topological Sort.cpp @@ -0,0 +1,35 @@ + //Function to return list containing vertices in Topological order. + + // Time complexity :- O(V+E) , V-vertices and E-edges + void topo(int v,vector&vis,stack&s,vectoradj[]) + { + vis[v]=1; + for(auto it:adj[v]) + { + if(!vis[it]){ + topo(it,vis,s,adj); + } + } + s.push(v); + } + vector topoSort(int V, vector adj[]) + { + // code here + stacks; + vectorvis(V,0); + for(int i=0;ians; + while(!s.empty()) + { + int t=s.top(); + s.pop(); + ans.push_back(t); + } + return ans; + } \ No newline at end of file