Topological sorting [theory]

Source: Internet
Author: User

What is a topological sequence?
Generally, such a linear sequence is called a sequence that satisfies the Topological Order. In short, a full order is obtained from a partial order on a set. This operation is called topological sorting. Definitions of partial and full order in discrete mathematics:
If the relationship on set X is R, and R is self-inverse, inverse, and transmitted, R is called the partial order relationship on set X.
If R is set to Partial Order on X, if for each x, y belongs to X, there must be xRy or yRx, then R is the full Order relation on X.


A relatively simple understanding: Partial Order means that only some Members in the set can be compared, and full order means that all members in the set can be compared.
Note:
① If the vertex in the graph is arranged in a line in the topological order, all the directed edges in the graph are directed from left to right.
② If a directed ring exists in the graph, the vertex cannot meet the topological order.
③ The topological sequence of a DAG usually indicates that a scheme is feasible.
General applications
Topological sorting is often used to determine the order of occurrence of a dependency set. For example, in daily work, the project may be split into four sub-parts: A, B, C, and D, but A depends on B and D, and C depends on D. In order to calculate the order of the project, you can sort the link set in a topological order to obtain a linear sequence. The task that comes before is the task that needs to be completed first.
Basic Implementation Method
The Topology Sorting method is as follows:
(1) Select a vertex from the directed graph without a forward (that is, the inbound degree is 0) and output it.
(2) Delete the vertex from the net and delete all directed edges from the vertex.
(3) Repeat the above two steps until the remaining networks no longer have vertices without a forward trend.

Topology sequence C ++ core code

Bool TopologicalSort (int a [] [101]) // returns True {int n = a [0] [0], I, j if Topology Sorting can be completed; int into [101], ans [101]; memset (into, 0, sizeof (into); memset (ans, 0, sizeof (ans )); for (I = 1; I <= n; I ++) {for (j = 1; j <= n; j ++) {if (a [I] [j]> 0) // into [j] ++ of the adjacent matrix; // statistical input} into [0] = 1; for (I = 1; I <= n; I ++) {j = 0; while (into [j]! = 0) // traverse until a point with zero inbound value is found {j ++; if (j> n) return false;} ans [I] = j; // This point has been sorted. into [j] =-1; // Delete this point for (int k = 1; k <= n; k ++) {if (a [j] [k]> 0) into [k] --; // Delete the inbound degree of the vertex that the edge is connected to} for (I = 1; I <= n; I ++) {cout <ans [I] <"" ;}cout <endl; return true ;}

 

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.