topological sort

int n; // number of vertices
vector<vector<int>> adj; // adjacency list of graph
vector<bool> visited;
vector<int> ans;

void dfs(int v) {
    visited[v] = true;
    for (int u : adj[v]) {
        if (!visited[u])
            dfs(u);
    }
    ans.push_back(v);
}

void topological_sort() {
    visited.assign(n, false);
    ans.clear();
    for (int i = 0; i < n; ++i) {
        if (!visited[i])
            dfs(i);
    }
    reverse(ans.begin(), ans.end());
}

4.4
5
Ignatz 105 points

                                    L &larr; Empty list that will contain the sorted nodes
while exists nodes without a permanent mark do
    select an unmarked node n
    visit(n)

function visit(node n)
    if n has a permanent mark then
        return
    if n has a temporary mark then
        stop   (not a DAG)

    mark n with a temporary mark

    for each node m with an edge from n to m do
        visit(m)

    remove temporary mark from n
    mark n with a permanent mark
    add n to head of L

4.4 (5 Votes)
0
3.8
10
Noemi 130 points

                                    def topological_sort():
    for each node:
        if visited[node] is False:
            dfs(node)
def dfs(node):
    visited[node] = True
    for nei in neighbours[node]:
        dfs(node)
    ret.insert_at_the _front(node)

3.8 (10 Votes)
0
4.25
8
Ntg 75 points

                                    void dfs_helper(T src, map&lt;int,bool&gt;&amp; visited, list&lt;T&gt; &amp;ordering) { 
    //Recursive function that will traverse the graph 
         
    visited[src] = true; 
    //go to all nbr of that node that is not visited 
    for(T nbr:l[src]) { 
        if(!visited[nbr]) { 
            dfs_helper(src,visited,ordering);
        } 
    } 
    ordering.push_front(src);
} 

int dfs(T src) { 
        map&lt;int,bool&gt; visited; 
        list&lt;T&gt; ordering;
        //Mark all nodes as not visited. 
        //l = adjacency list implemented using std::unordered_map
        for(auto p:l) { 
            T node = p.first; 
            visited[node] = false; 
        } 

        for(auto p:l) {
            T node = p.first;
            if(!visited[node]) {
                dfs_helper(node,visited,ordering);
            }
        }

        //Finally print the list
        for(auto node:ordering) {
            cout &lt;&lt; node &lt;&lt; endl;
        }
}

4.25 (8 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
topological sort introducton when to use topological sort topological sort algorithm complexity topological sort code topological sorting c++ Implement Topological Sort all possible topological sorting topological sort definition A topological sort algorithm topological sort for graph topological sort cpp topological sort c++ complexity topological sort connected topological sort works on topological sort full code graph topological ordering topological order sorting algorithm topological sorting code Topological sort uses topological sorting of graph topological sort in tree topologically sorted topological sorting order of evolution topological sort graph theory whta is topological sorting topological sort graphs topological sort l topological sort proble topological sort gfg what is topological sort in graphs topological sort graph\ topological sort in graph Topological sorting. topological sort practice What is the purpose of a topological sort algorithm topological sort complexity where to start topological sort topological sorted order topological sorting program in c++ when topological sort invented topological sort graph c++ Applications of topological sort are ? graph for topological sort topological sort means graphs topological sort what is the use of topological sorting where is topological sort used topological sort explanation graph topological order topologically sorted order topological sort using indegree use of topological sort topological ordering in graph correct topological sort Topological sort is used in topological sort can be implemented by? implementation of topological sort Topological sort using DFS in C++ topological sort implementation c++ Explain Topological sort Explain Topological sort. c++ Implementing Topological Sorting topological sort geeks tarjan algo for topological sort cp algorithms topological sorting gfg topologically sorted graph topological sort competitive programming] topological sort simulation topological ordering graph topological sort scheme graph topological sorting how to do topological sort handle topological sort topological sort meaning what is a topological sort topological sort dag topological sort runtime topological sort stack graph topological sort topological sort does not sort the entire graph?? why topological sorting used Topological algorithm best algorithm topological sort time complexity of topological sort when is topological sort used topological sort algorithm explanation topological sort gfg topological order graph topological sorting definition topological sort in array topological sorting algorithms what is a topological sorting topological sorting SPOJ topological sort online algorithm for topological sort topological sort wiki topological sort of a graph topological sorting cp algorithm what is topological sort topological sort of tree what is topological sort used for topological sort orderings c++ topological sort of graph topological sort tree what is topological sorting topological sort program in cpp topological sorting graph topological sort example topological sort in data structure topological sort cp algorithm topological sort examples topological sort dag c++ topological ordering algorithm topological order algorithm topological sort what does topological sort do topological sort which graph to use topological sort graph leetcode topological sort topological sort algorithms cp algorithm list a topological sorting of it topological sort bfs topological sorting topological sort topological sorting algorithm dfs cp algorithms topological sorting example topological sort algorithm cp algorithms topological sort topological sorting online Topological sort C++ topological sort in c++ what can topological sort be used for competitve programmig topological sort cp algorithms topological sorting problems toposort cp sorting cp algorithms kahn algorithm cp algorithm
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source