topological sort cp algorithms

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

showing results for - "topological sort cp algorithms"
Matt
20 Jan 2018
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
Loren
28 May 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 sorting algorithms comparisongraphs topological sorttarjan algo for topological sort cp algorithmstopological sort cp algorithmsc 2b 2b implementing topological sortingtopological sorting program in c 2b 2btopological sorting algorithmstopological sorting problemstopological sorting gfghow to conduct a topological sortwhen topological sorting is neededtopological sort with ascending ordertopological sort using dfs in c 2b 2btopological sort space complexitygraph for topological sorttopological sorting onlinewhat is topological sorttopological sort codetopological sort applicationsintroduction to topological sorting 3ftopological sorting algorithm written bytime complexity of topological sorttopological sorting of graphtopological sort 3ftopological sort algorithmshow to find all topological sortswhat is topological sortingcp algorithm lista topological sort of the above graph is 3atopological ordering from graphgraph suitable for topological sortwhat can topological sort be used for competitve programmigtopological shorttopological stream ordertopological sort time complexitytopological sort ptyohtopological sort complexitytopological order sorting algorithmtopological sort orderings c 2b 2btopological sort practicetopological sort gfgtopological sorting exampletopological sorting time complexitytopological sort implementation c 2b 2bgraph topological sorta topological sort algorithmtopological sorting cp algorithmtopological sort cp algorithmtopological sort geeksis topological sort for directed graphall topological sorttopological sort definitiontopological sort code in c 2b 2btopological sort competitive programming 5dwhat is topological order in graphkahn algorithm cp algorithmwhat is meant by topological sortingsorting cp algorithmstopological sort c 2b 2b complexitywhat can be the applications of topological sortingtopological sort ltopological sort cpptopological sort cp algorithmstopological sorting codecp algorithms topological sorttopological sort example graphtopological sortingtopological sort bfstopologically sortingtopological sort completopological sort program in cppfind topological sort of graphtopological sorting c 2b 2b using queintroduction to topological sorting algorithmwhere to use topological sortinngtopological sort explainedleetcode topological sorttopological sort 28dfs 29topological sort directed graph c 2b 2b 22topological sorting algorithm 22topological ordering of a grapha topological sorting of ittopological ordering graphtopological sorting algorithmtopological sorttopological sort algorithmtoposort cpwhich algorithm will be used to implement topological sortingwhat is a topological sortalgorithm for topological sorttopological order preorderdfs cp algorithmstopological sort in c 2b 2btopological sort graph c 2b 2btopological sorting using dfstopological sort dag c 2b 2btotal topological orderings of a graphbest algorithm topological sorttopological sort c 2b 2btopological sort gfgtopological sorting in c 2b 2btopological sorting c 2b 2btopological sort examplestopological sort cp algorithms