topological sort

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

showing results for - "topological sort"
Marwane
01 Jun 2017
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
Isabell
01 Sep 2020
1L ← Empty list that will contain the sorted nodes
2while exists nodes without a permanent mark do
3    select an unmarked node n
4    visit(n)
5
6function visit(node n)
7    if n has a permanent mark then
8        return
9    if n has a temporary mark then
10        stop   (not a DAG)
11
12    mark n with a temporary mark
13
14    for each node m with an edge from n to m do
15        visit(m)
16
17    remove temporary mark from n
18    mark n with a permanent mark
19    add n to head of L
20
Nicola
24 Mar 2016
1void dfs_helper(T src, map<int,bool>& visited, list<T> &ordering) { 
2    //Recursive function that will traverse the graph 
3         
4    visited[src] = true; 
5    //go to all nbr of that node that is not visited 
6    for(T nbr:l[src]) { 
7        if(!visited[nbr]) { 
8            dfs_helper(src,visited,ordering);
9        } 
10    } 
11    ordering.push_front(src);
12} 
13
14int dfs(T src) { 
15        map<int,bool> visited; 
16        list<T> ordering;
17        //Mark all nodes as not visited. 
18        //l = adjacency list implemented using std::unordered_map
19        for(auto p:l) { 
20            T node = p.first; 
21            visited[node] = false; 
22        } 
23
24        for(auto p:l) {
25            T node = p.first;
26            if(!visited[node]) {
27                dfs_helper(node,visited,ordering);
28            }
29        }
30
31        //Finally print the list
32        for(auto node:ordering) {
33            cout << node << endl;
34        }
35}
Arlette
03 Oct 2016
1def topological_sort():
2    for each node:
3        if visited[node] is False:
4            dfs(node)
5def dfs(node):
6    visited[node] = True
7    for nei in neighbours[node]:
8        dfs(node)
9    ret.insert_at_the _front(node)
queries leading to this page
topological sort example graphtopological sort onlinecp algorithms topological sorttopological sort treewhere to start topological sorttopologically sorted orderkahn algorithm cp algorithmtopological sort in data structuretopological sorting exampletopological sort examplestopological sort definitionwhich algorithm will be used to implement topological sortingtopological sorting algorithm written bytopological sort codetopological sort in c 2b 2btopological sortinguse of topological sorttopological sorting codetarjan algo for topological sort cp algorithmsimplement topological sorttopological sort graph theorytopological sorting using dfsexplain topological sort topological sorting algorithmtopological sort with ascending orderdfs cp algorithmstopological sort introductontopological sort full codetopological sort uses topological ordering of a graphtopological sort implementation c 2b 2bexplain topological sorthow to conduct a topological sorttopological sort complexitycorrect topological sorttopological sort graphgraph suitable for topological sorttopological sorting spojtopological sort graph c 2b 2btopological sort 28dfs 29why topological sorting usedtopological sort graphstime complexity of topological sorttopological sort algorithmtopological sorting c 2b 2b using quetopologically sortingwhen to use topological sorttopological sorting graphall possible topological sortingwhat is the use of topological sortingintroduction to topological sorting algorithmc 2b 2b implementing topological sortinggraph topological sorttopological sort dagtopological sorting program in c 2b 2bhow to do topological sortsorting cp algorithmstopological sorting algorithms comparisontopological order sorting algorithmtopological sort c 2b 2b topological algorithma topological sort of the above graph is 3agraph for topological sorttopological sort using 7eoperatortopological sort 3ftopological sort in arraytopological sort graph 5ctopological sort space complexitytopological sorting cp algorithmtopological sort of a graphwhat is the purpose of a topological sort algorithmwhat is topological sort used fortopological sort dag c 2b 2btopological sort cp algorithmstopological sorting order of evolutiona topological sort algorithmhandle topological sorttotal topological orderings of a graphwhere to use topological sortinngtopological sort wikihow to find all topological sortstopologically sorted graphtopological sorting in c 2b 2balgorithm for topological sorttopological sort stacktopological sort gfgtopological sort can be implemented by 3ftopologically sortedtopological sort algorithm explanationcp algorithm listgraph topological sortingtopological sort bfswhat is topological sortingtopological sorting definitiontopological sort program in cpptopological sort applicationswhat is topological sorttopological stream ordertopological sorted ordertopological sort cp algorithmtopological sorting onlineintroduction to topological sorting 3ftopological sort c 2b 2b complexitytopological sort gfgwhat can topological sort be used for competitve programmigtopological shorttopological sort algorithmswhat does topological sort dotopological sorting algorithmswhen is topological sort usedtopological order preordergraph topological orderingtopological sorting time complexitytopological sort ptyohwhat is a topological sortwhen topological sorting is neededtopological sort using indegreetopological sort for graphtopological sorting c 2b 2btopological order algorithmtopological sorting gfg 22topological sorting algorithm 22toposort cpgraphs topological sorttopological sort geekstopological sorttopological sort of treetopological sort ltopological sort simulationapplications of topological sort are 3fgraph topological orderall topological sorttopological sorting of graphtopological sort in graphtopological sort meansbest algorithm topological sort topological sortwhere is topological sort usedtopological sort schemewhat is topological order in grapha topological sorting of ittopological sort meaningtopological sorting topological sort practicetopological sort cp algorithmstopological sort explainedtopological sort directed graph c 2b 2btopological sort of graphtopological ordering in graphtopological sort competitive programming 5dtopological sort does not sort the entire graph 3f 3ftopological sort cpptopological order graphtopological sort time complexitytopological sort runtimewhat is topological sort in graphstopological sort which graph to useleetcode topological sorttopological sorting problemswhat can be the applications of topological sortingwhat is meant by topological sortingtopological sort orderings c 2b 2btopological ordering algorithmwhat is a topological sortingtopological sort explanationtopological sort probletopological ordering graphis topological sort for directed graphwhen topological sort inventedtopological ordering from graphtopological sort code in c 2b 2btopological sort intefind topological sort of graphtopological sort algorithm complexitytopological sort completopological sort exampletopological sort using dfs in c 2b 2bimplementation of topological sorttopological sort