The principle of topological sorting algorithm and the implementation of the complete C code

Source: Internet
Author: User
Tags in degrees

topology Sort definition

The topological ordering of a directed acyclic graph (Directed acyclic graph called dag) G is to arrange all the vertices in G into a linear sequence so that any pair of vertices in the graph, U and V, if the Edge (u,v) ∈e (g), the U appears before v in a linear sequence. Typically, such a linear sequence is called a sequence that satisfies the topological order (topological order), referred to as a topological sequence. Simply put, a partial order on a set gets a full order on the set, and this operation is called topological ordering.

Basic Knowledge

A larger project is often divided into many sub-projects, and we call these sub-projects activities. In the whole project, some sub-projects (activities) must be completed before other related sub-projects are finished, that is, a sub-project begins with the completion of all its pre-order sub-projects, but some sub-projects have no prerequisites, can be scheduled at any time to start. In order to reflect the relationship between the various sub-projects (activities) in the whole project, it is possible to show that the vertices in the graph represent the activities (sub-works), the forward side of the graph represents the succession of activities, that is, the activity of the starting point of the edge is the pre-order activity of the end-point activity, Its end-of-activity. Usually, we put this vertex activity, the side represents the relationship between the activities of the graph called Vertex activity Network (activity on Vertex Network), referred to as AoV net.

A AOV net should be a direction-free graph, that is, should not have a loop, because if there is a loop, all the activities on the circuit can not be carried out. 3-6 is a loop with three vertices, by <A,B> B activity must after a activity, from the <B,C> side can get C activity must after the B activity, so the launch C activity will necessarily after a activity, but by the <C,A> The C activity must precede the activity of a, thus causing contradictions so that each activity cannot be carried out. If this happens in the program, it is called a deadlock or a dead loop and should be avoided.


In the AOV network, if there is no loop, then all activities can be arranged into a linear sequence, so that all precursor activity of each activity is in front of the activity, we call this sequence of topological sequence (topological order), the process of constructing topological sequence by AoV Network is called topological sort ( Topological sort). The topological sequence of a AOV network is not unique, and any linear sequence that satisfies the above definition is called its topological sequence.


topology sort basic steps

The topological sequencing algorithm for constructing topological sequences by AOV network is mainly to loop the following two steps until there are no vertices with a degree of 0 in it.
(1) Select a vertex with an entry level of 0 and output it;
(2) Delete this vertex and all out edges from the net.
After the end of the loop, if the number of vertices of the output is less than the number of vertices in the net, then the output "have loop" information, otherwise the output of the vertex sequence is a topological sequence.


topology sequencing complete C code implementation

/* AoV network topology sort C code */#include <stdio.h> #include <stdlib.h> #include <string.h> #define MaxSize 20typedef Char vertextype;typedef struct Outnode {//defines the degree of node Vertextype data;struct outnode *next;} outnode;typedef struct Vertex {//define vertex structure int in; Vertextype Data;outnode *first;} vertex;//creating AoV net void Createaov (Vertex **g) with Adjacency table method {Vertextype Ver;int i = 0;int vertexnum;outnode *p, *q; (*g) = (Vertex *) malloc (sizeof (Vertex) *maxsize);p rintf ("Please enter the vertex of the AOV network: \ n"), while (' \ n '! = (Ver=getchar ())) {//Storage network vertex information (* g) [i].data = ver; (*g) [I].first = null;i++;} (*G) [i].data = ' + '; Vertexnum = i;//Record the number of vertices printf ("Enter the entry of the AoV mesh vertex in turn: \ n"), for (i=0; i<vertexnum; i++)//Store the vertex's entry information scanf ("%d", & (*g) [i]. IN), Fflush (stdin);p rintf ("Please enter AOV mesh vertex corresponding to adjacency point: \ n"), for (i=0; i<vertexnum; i++) {//store adjacency point information for each vertex printf ("%c:", (*G) [i]. Data), if (' \ n '! = (Ver=getchar ())) {p = (Outnode *) malloc (sizeof (Outnode));p->data = ver; (*g) [I].first = P;p->next = Null;q = P;while (' \ n '! = (Ver=getchar ())) {p = (Outnode *) malloc (sizeof (OUtnode));p->data = Ver;p->next = Null;q->next = P;q = P;}}} Gets the number of vertices int calvernum (Vertex *g) {int len = 0;while (' g++ '!! =->data) Len++;return len;} Print AoV net void Printaov (Vertex *g) {int vertexnum = Calvernum (g); Outnode *p;printf ("AoV vertices, degrees, and adjacency points: \ n"); for (int i=0; i& Lt Vertexnum; i++) {//print vertex printf ("%c%d", G[i].data, g[i].in);p = G[i].first;while (NULL! = p) {printf ("%c", p->data);p = P-&gt ; next;} printf ("\ n");}} Topological sorting functions void Topsort (Vertex *g) {int I, J, k;outnode *p;int vertexnum = Calvernum (g);p rintf ("The sequence after the topology is: \ n"); for (i=0; i <VertexNum; i++) {//outer loop is used to control the number of vertices of the output for (j=0; j<vertexnum; j + +)//inner loop is used to find the next 0 vertex if (0 = = g[j].in) {printf ("%c", g[j].data); G[j]. in = -1;p = G[j].first;while (NULL! = p) {//deletes all of the forward edges emitted from the vertex, by locating the vertex adjacency point one at a time and then -1for (k=0; k<vertexnum; k++) if (P- >data = = g[k].data) {g[k].in--;break;} p = p->next;} Break;}} printf ("\ n");} int main () {Vertex *g; Createaov (&AMP;G); Printaov (g); Topsort (g); return 0;}

Code test data and test results

The tested direction-free graphs are:

Where the identifier of the input vertex, there is no space between the letters, after the input, enter.

When you enter the adjacency point of a vertex, as the vertex is an adjacency point, enter directly.

Enter the degree of the vertex, the space between each data is separated, all the input is completed, enter.


Convert a non-circular graph to an input data table

Number In degrees Vertex Adjacency Point
0 0 A Gc
1 0 B Ce
2 2 C D
3 2 D IF
4 1 E Df
5 2 F
6 1 G H
7 1 H I
8 2 I

The test results are:


Test passed.


The principle of topological sorting algorithm and the implementation of the complete C code

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.