topological sort c 2b 2b

Solutions on MaxInterview for topological sort c 2b 2b by the best coders in the world

showing results for - "topological sort c 2b 2b"
Louisa
10 Aug 2019
1int n; // number of vertices
2vector<vector<int>> adj; // adjacency list of graph
3vector<bool> visited;
4vector<int> ans;
5
6void dfs(int v) {
7    visited[v] = true;
8    for (int u : adj[v]) {
9        if (!visited[u])
10            dfs(u);
11    }
12    ans.push_back(v);
13}
14
15void topological_sort() {
16    visited.assign(n, false);
17    ans.clear();
18    for (int i = 0; i < n; ++i) {
19        if (!visited[i])
20            dfs(i);
21    }
22    reverse(ans.begin(), ans.end());
23}
24
Axel
17 Sep 2018
1//Topological sort DFS
2//Complete code 
3//Possible Only on DAG(Directed Acyclic Graph)
4#include<bits/stdc++.h>
5using namespace std;
6void addedge(vector<int>adj[],int u,int v)
7{
8    adj[u].push_back(v);
9
10}
11void topo(int val,stack<int>&st,vector<int>adj[],vector<int>&visited)
12{
13    visited[val]=1;
14    for(auto i:adj[val])
15    {
16        if(!visited[i])
17        {
18            topo(i,st,adj,visited);
19        }
20    }
21    st.push(val);
22}
23void toposort(vector<int>adj[],int n)
24{
25    stack<int>st;
26    vector<int>visited(n,0);
27    for(int i=0;i<n;i++)
28    {
29        if(visited[i]==0)
30        {
31            topo(i,st,adj,visited);
32        }
33    }
34    vector<int>topo;
35    while(!st.empty())
36    {
37        topo.push_back(st.top());
38        st.pop();
39    }
40    int s=topo.size();
41    for(int j=0;j<s;j++)
42    {
43        cout<<topo[j]<<" ";
44    }
45}
46int main()
47{
48    int vertex,edges;
49    cout<<"Enter the vertex and edges"<<endl;
50    cin>>vertex>>edges;
51    vector<int>adj[vertex];
52    int a,b;
53    cout<<"Enter the links"<<endl;
54    for(int i=0;i<vertex;i++)
55    {
56        cin>>a>>b;
57        addedge(adj,a,b);
58    }
59    toposort(adj,vertex);
60    return 0;
61}
62
queries leading to this page
topological sort geeksgraphs topological sorttopological sorting examplewhat is topological sortall topological sorttopological sorting using dfstopological sorting cp algorithmtopological sorting problemstopological sort implementation c 2b 2btopological sort completopological sorting codewhere to use topological sortinngtopological sort code in c 2b 2ba topological sort of the above graph is 3atopological sorting algorithm written bya topological sorting of itwhat can topological sort be used for competitve programmigtopological sort directed graph c 2b 2balgorithm for topological sorttopological stream ordertopological sort can be applied to which of the following graphs 3ftopological sort examplestopological sort c 2b 2btopological sorting of graphcp algorithm listtopological sort in c 2b 2btopological sort definitionc 2b 2b implementing topological sortingtopological sort space complexitycp algorithms topological sorttopological sort gfgtopological sorting c 2b 2btopological sort bfshow to conduct a topological sortwhat is topological sortingtopological ordering from graphgraph suitable for topological sort 22topological sorting algorithm 22topological ordering graphwhat is a topological sorttopological shorttopological sort dag c 2b 2btopological order preorderis topological sort for directed graphtopological sort 3ftopologically sortingtopological sort algorithmsintroduction to topological sorting 3ftopological sort c 2b 2b complexitytopological sorting program in c 2b 2btime complexity of topological sorttopological sort with ascending ordertopological sort 28dfs 29topological sort competitive programming 5dkahn algorithm cp algorithmtopological sort graph c 2b 2bgraph topological sorttopological sort practicetopological sort cp algorithmstopological sorting in c 2b 2bleetcode topological sorttopological sort algorithmtopological sorting c 2b 2b using quetopological sort program in cpptopological sort ltopological sorting gfgtoposort cptopological sort complexitywhen topological sorting is neededtopological sort using dfs in c 2b 2bsorting cp algorithmshow to find all topological sortstopological order sorting algorithmfind topological sort of graphtopological sorting time complexitytopological sorting algorithmstopological sort gfgtopological sort cp algorithmtotal topological orderings of a graphtopological sort time complexitytopological sort cp algorithmstopological sorting algorithms comparisontopological sorttopological sorting algorithmtopological sort explainedtopological sort cpptopological sort example graphbest algorithm topological sortgraph for topological sortdfs cp algorithmstopological sort applicationswhich algorithm will be used to implement topological sortingtopological sort programiztopological sort orderings c 2b 2btopological sort ptyohtopological sorting graphtopological sort codewhat is meant by topological sortingtopological ordering of a graphtarjan algo for topological sort cp algorithmsintroduction to topological sorting algorithmwhat is topological order in graphwhat can be the applications of topological sortingtopological sortingtopological sorting onlinea topological sort algorithmtopological sort c 2b 2b