This blog post for the migration, written on February 24, 2015, does not represent my current views and views. Original address: http://blog.sina.com.cn/s/blog_6022c4720102vspe.html

**1. Preface**before we get to the topological sort, let's look at a topic to better understand: 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. Course Code Course name First CourseC1 Advanced Mathematics withoutC2 Programming Basics NoC3 Discrete Mathematics c1,c2C4 data Structure C3,C5C5 algorithm Language C2C6 Compiling technology c4,c5C7 operating System c4,c9C8 General Physics C1C9 Computer principle C8 its AOV network:

**2. What is topology sequencing? **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. A loop with three vertices, by the side B activity must after a activity, by the

**side can get C activity must after the B activity, so the introduction of C activities must be after a activity, but by the side can get C activity before a activity, thus the contradiction, so that each activity can not 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 net, if there is no loop, all activities can be arranged in a linear sequence so that all precursor activities of each activity are in front of the activity, and we call this sequence a topological sequence (topological order), The process of constructing a topological sequence from a AOV network is called topological ordering (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. For example, the following three sequences are topological sequences, and of course you can write a lot. (1) c1,c8,c9,c2,c3,c5,c4,c7,c6(2) C2,c1,c3,c5,c4,c6,c8,c9,c7(3) C1,c2,c3,c8,c9,c5,c4,c6,c7

**3. What is the role? **In addition to one of the topics we showed above, topological sequencing applications are quite extensive. Let's look at one more example:

**soldiers lined up Soldier.cpp**
**<1> Problem Description**There are N soldiers (1<=n<=26), numbered sequentially a,b,c, ... In the queue training, the commander to a number of soldiers from high to short line, but now the commander can not directly obtain the height of each soldier information, can only get "P1 than P2 High" comparison results (p1,p2∈{a,b, ... Z}, denoted as P1>P2), such as "a>b" for A higher than B. Compiling a program, according to the results obtained by the comparison to find the matching conditions of the queueing scheme. Note: The comparison results do not involve soldiers who are not in line. For example, there are 3 soldiers, a, B, and C, giving a relationship (a, a, a, a), (B,C). Where (A, a) indicates that soldier A is higher than B, and when the above relationship is given, they can be lined up as a team: ABC.

**<2> Input**with file input, each comparison results in a single row in the file.

**<3> Output**If the input data is not solved or is not uniquely determined, the output "no answer! , otherwise the number of each soldier is output from high to low, with no separators in the middle, and the result is written to a text file.

**<4> Input Sample**a>bb>ff>d

**<5> Output Example**ABFD The code is as follows: #include <cstdio>#include <cstdlib>#define MAXN struct Edge{int next,v;};Edge list[maxn*2];int HEAD[MAXN],TOT,N,HAVE[MAXN],LISTNUM[MAXN],DEL[MAXN],GET,ANS[MAXN]; void Addlist (char stru,char STRV){int u=stru-64,v=strv-64;have[u]=have[v]=1;tot++;List[tot].v=v;List[tot].next=head[u];Head[u]=tot;listnum[v]++;} void init (){Char s[4];freopen ("3.in", "R", stdin);freopen ("3.out", "w", stdout);while (scanf ("%s", s)!=eof)addlist (s[0],s[2]);for (int i=1;i<=26;i++)if (have[i]==1) n++;} int check (){for (int i=1;i<=26;i++)if (del[i]!=1 && have[i]==1) return 0;return 1;} void DFS (int now,int dep){del[now]=1;for (int x=head[now];x!=0;x=list[x].next)listnum[list[x].v]--;Ans[dep]=now;if (check () ==1){if (get==1) {printf ("NO answer!"); exit (0);}else {get=1; return;}}for (int i=1;i<=26;i++)if (listnum[i]==0 && del[i]!=1 && have[i]==1) DFS (i,dep+1);for (int x=head[now];x!=0;x=list[x].next)listnum[list[x].v]++;del[now]=0;} int main (){init ();for (int i=1;i<=26;i++)if (listnum[i]==0 && have[i]==1) DFS (i,1);if (get==0) printf ("NO answer!");else for (int i=1;i<=n;i++) printf ("%c", ans[i]+64);return 0;}

[Knowledge points] topological ordering