Topological sequencing and continuation

Source: Internet
Author: User

"Topological sorting problems"

Workaround:

1. Calculate the entry value for each point deg[i], this step needs to scan all points and edges, complexity O (n+m).

2. Adding a point of 0 to the queue Q, of course there may be more than 0 points, and there is no connection between them, so it is possible to add Q in any order.

3. Remove a point p from the Q. For each point not deleted and connected to P q,deg[q] = deg[q]-1; if deg[q]==0, add Q to Q.

Code: Complexity: O (v+e)

<span style= "FONT-SIZE:14PX;" > #include <bits/stdc++.h>using namespace std;const int N=5*1e5+10;const int Mod=142857;int t,n,k,m,x;int    Father[n],v[n],indegree[n];vector <int >vec[n];bool Topsort () {Queue<int >q;    while (!q.empty ()) Q.pop ();    for (int i=1; i<=n; i++) if (indegree[i]==0) Q.push (i);    int ans=0,sum=0;        while (!q.empty ()) {int U=q.front ();        Q.pop ();        sum++;            for (int i=0; i<vec[u].size (); i++) {int temp=vec[u][i];        if (--indegree[temp]==0) Q.push (temp); }} if (Sum<n) return false;//to determine the topological sort return true;    int main () {int u,v,a;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        memset (indegree,0,sizeof (Indegree));        for (int i=1; i<=n; i++) if (Vec[i].size ()) vec[i].clear ();            while (m--) {scanf ("%d%d", &v,&u);            Vec[v].push_back (U);        indegree[u]++; } if (topSort ()) puts ("YES");    Else puts ("NO"); } return 0;} </span>

If the time complexity of topological sorting is O (n*e) with adjacency table, the adjacency matrix is O (n^2), N is the number of vertices, E is the number of edges, and the Floyd time Complexity is O (n^3).

Properties
1, topological sequencing in the direction of the non-circular graph to discharge the effective sequence, otherwise it can be judged that there is a ring to the graph.
2, if the input of the point in the graph, there is no point in the degree of 0, then the direction of the graph exists loop
3, if the presence of a point of 0 is greater than one, then the graph certainly does not exist a topological sequence can be determined, but does not hinder the topological ordering

Code:

<span style= "FONT-SIZE:14PX;" >/* the adjacency Matrix O (n*n) node has been accessed, 0 means no, 1 indicates that it has been accessed, 1 means that it is being accessed, that is, in the recursive call frame */int g[n][n];int visit[n];int N;    /*n indicates the number of nodes */bool dfs (int pos) {Visit[pos] =-1;            for (int i=1; i <= n; i++) {if (G[pos][i]) {if (Visit[i] < 0) return false;        else if (!visit[i] &&!dfs (i)) return false;    }} Visit[pos] = 1;    S.push (POS); return true;} /* The adjacency table O (n*e) node has been accessed, 0 means no, 1 indicates that it has been accessed, 1 means that it is being accessed, that is, in the recursive call frame */#include <bits/stdc++.h>using namespace Std;const    int n=1e5+100;vector<int> g[n];int vis[n];bool dfs (int u) {vis[u]=-1;        for (int i=0; i<g[u].size (); i++) {int v=g[u][i];        if (vis[v]<0) return false;    else if (!vis[v]&&!dfs (v)) return false;    } vis[u]=1; return true;}    BOOL Topu (int n) {for (int i=1; i<=n; i++) if (!vis[i]) if (!dfs (i)) return false; return true;}    int main () {int t;    scanf ("%d", &t);    while (t--){int n,m,v,u;        memset (vis,0,sizeof (VIS));        scanf ("%d%d", &n,&m);        for (int i=0; i<=n; i++) g[i].clear ();            for (int i=0; i<m; i++) {scanf ("%d%d", &v,&u);        G[v].push_back (U);        } if (Topu (n)) puts ("YES");    Else puts ("NO"); } return 0;} </span>



Topological sequencing and continuation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.