topological sorting

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());
}

0
0
Treerose61 90 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;
        }
}

0
0
4.33
6

                                    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.33 (6 Votes)
0
3
2
Denise 100 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 (2 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 algorithm complexity The topological sort algorithm complexity Implement Topological Sort all possible topological sorting topological sort definition when topological sorting is preferred topological sort for graph topological sort connected Time Complexity of Topological Sorting is: topological sort works on topological sort full code graph topological ordering topological order sorting algorithm Which of the following techniques can implement Topological sort is topological sort important topological graph Topological sort uses topological sorting of graph topological sort in tree topologically sorted sort a tree in topological order topological sorting order of evolution topological sort graph theory whta is topological sorting topological sort graphs topological sort l Topological sort can be applied to which of the following graph? * topological sort proble topological sort gfg topological sorting example what is topological sort in graphs topological sort graph\ topological sort in graph Topological sorting. topological sort practice Write and explain Topological sorting with an example What is the purpose of a topological sort algorithm topological sort complexity where to start topological sort topological sort use cases topological sorted order Which of the following is not a topological sorting of the given graph? topological order in graph meaning when topological sort invented Applications of topological sort are ? graph for topological sort topological sort means Time Complexity of topological sorting graphs topological sort what is the use of topological sorting where is topological sort used topological sort explanation Topological Sort (DFS) graph topological order how many topological sorting is possible in a graph topologically sorted order topological sort using indegree use of topological sort topological ordering of a graph topological ordering in graph find topological sort of graph Topological sort can be applied to which of the following graphs? correct topological sort Topological sort is used in topological sort can be implemented by? topological sort different results implementation of topological sort topological sort time complexity topological sort cp algorithms topological sorting using dfs topological order of a graph Explain Topological sort Explain Topological sort. topological sort geeks application of topological sorting Which of the following techniques can implement Topological sort * topological sorting dfs topological sort graph requirement topological sort gfg Which of the following is a topological sort of the graph above. topologically sorted graph 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 order graph topological sorting definition topological sort in array topological sorting algorithms what is a topological sorting topological sorting SPOJ topological sort code 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 of graph topological sort tree what is topological sorting topological sorting graph topological sorting algorithm topological sort example topological sort in data structure topological sort cp algorithm topological sort examples topological ordering algorithm topological order algorithm topological sort topological sort algorithm what does topological sort do topological sorting topological sort which graph to use topological sort graph topological sort
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