Topology Sorting (poj2367, poj1094, etc) [topology No. 1]

Source: Internet
Author: User

Topological sorting-Topological Sort;

To sort the topology of a directed acyclic graph (DAG) g, all vertices in G are arranged in a linear sequence so that any pair of vertices u and v in the graph, if <u, v> ε E (G), u appears before V in the linear sequence.

The above is the definition of the gray-Abstract topological sorting;

In fact, topological sorting is simply about some events, some of which must be before others, such as wearing so and shoes;

The general question may give some events and some order of occurrence to find a topological sorting;

Analyze a topology

A ----> B -----> C -----> D;

We can see that the first point must be the point with the inbound degree of 0;

If a ring exists, there is no point with an inbound degree of 0;

First Algorithm Implementation

An algorithm that uses an adjacent table as the graph storage structure:

A) scan the vertex table and add the vertex with zero degree of input to the stack. (PS: the stack here can enable the priority queue and so on ..)

B) when the stack is not empty:

Element V at the top of the output stack;

Check the outdegree of V and subtract 1 from the indegree of the terminal vertex of each outdegree. If the indegree of the vertex is 0, the vertex is added to the stack;

C) when the stack is empty, if the output vertex is smaller than the number of vertices, the AOV network has a loop. Otherwise, the Topology Sorting is completed.

Second Algorithm Implementation
You can perform a deep search from the root node according to the DFS mentioned in "computing Guide". Each time you find a point, record the start and end timestamps of the search;

Then, sort the values from large to small Based on the end time;

**************************************** **************************************** **

Here we will use an example to illustrate the first method, which is easy to implement and simple to code. Generally, topsort can handle drops...

I started with the lrj DFS code. Refer to the first algorithm. A picture is attached.

 

Poj1094

1.

Three cases.

With loops, you can directly determine to exit.

It cannot be determined that all groups of data must be input before judgment.

For (INT I = 1; I <= N; I ++) // where n loops are obtained, N points are added to the topological sequence.
{
Int J, temp;
T = 0; // if there is no ring, the last node must be added to DIS.
For (j = 1; j <= N; j ++)
{
If (in [J] = 0 & t = 0)
{
If (t = 0) temp = J;
T = 1;
}
Else if (in [J] = 0 & t = 1)
{
Flag = 1;
}
}
If (! T) return 2; // There are loops, contradictions, and the inbound degrees of all nodes are not 0, so there must be loops.
In [temp] =-1;
Dis [CNT ++] = temp;
For (INT Mm = 1; mm <= N; mm ++)
If (adj [temp] [mm])
In [mm] --;
}

This code still references someone on the Internet.

First error:

In the beginning, we should not consider those situations.

It cannot be determined that all data must be read. The trick is to read the data in a for loop and then judge it one by one. You also need to update the node and change the corresponding inbound level of the node.

# Include <iostream> # include <cstdio> # include <string. h> using namespace STD; int adj [30] [30]; int in [30]; // input degree int dis [30]; char phosphatase [30] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'h ', 'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P', 'Q', 'R ', 'S ', 't', 'U', 'V', 'w', 'x', 'y', 'z'}; int toposort (int n, int m) {int CNT = 1, t, flag = 0; memset (in, 0, sizeof (in); memset (DIS, 0, sizeof (DIS )); for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) if (adj [I] [J] = 1) in [J] + +; // For (INT I = 1; I <= N; I ++) printf ("% d \ n", in [I]); for (INT I = 1; I <= N; I ++) // This place gets N cycles, and N points are added to the topological sequence {Int J, temp; T = 0; // if there is no ring, the last node must be added to dis for (j = 1; j <= N; j ++) {If (in [J] = 0 & t = 0) {If (t = 0) temp = J; t = 1 ;} else if (in [J] = 0 & t = 1) {flag = 1 ;}} if (! T) return 2; // There are loops and contradictions. The inbound degrees of all nodes are not 0, so there must be loops in [temp] =-1; dis [CNT ++] = temp; For (INT Mm = 1; mm <= N; mm ++) if (adj [temp] [mm]) in [mm] --;} If (flag = 1) return 3; // 3 indicates that return 1 cannot be judged; // 1 indicates success, indicates that all N letters have been entered successfully.} int main () {int n, m; while (scanf ("% d", & N, & M )! = EOF) {If (! N &&! M) break; memset (adj, 0, sizeof (adj); int I, j, Judge = 0; char s [10]; int COUNT = 0; for (I = 1; I <= m; I ++) {scanf ("% s", S); count ++; If (Judge = 1) continue; int u = s [0]-'A' + 1; int v = s [2]-'A' + 1; adj [u] [v] = 1; int ans = toposort (n, n); If (ANS = 1) // if the Code has a problem, it seems that you cannot judge {judge = 1; printf ("sorted sequence determined after % d relations:", I); For (INT xx = 1; XX <= N; XX ++) printf ("% C ", alb [dis [XX]-1]); printf (". \ n ");} else if (ANS = 2) {judge = 1; printf (" inconsistency found after % d relations. \ n ", I);} else if (ANS = 3 & COUNT = m) // unable to judge, it must wait until all input is complete {printf ("sorted sequence cannot be determined. \ n ") ;}} return 0 ;}

 

Appendix:

Floyd ring determination algorithm // three loops.

Int Floyd ()

{
Int I, J, K;
For (k = 0; k <n; k ++)
For (I = 0; I <n; I ++)
For (j = 0; j <n; j ++)

{
If (Map [I] [k] & map [k] [J]) map [I] [J] = 1;
}

For (I = 0; I <n; I ++)
If (Map [I] [I]) return0;
Return1;
}

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.