Topology sequencing explained

Source: Internet
Author: User
Tags in degrees

Here's what we're talking about. Topological ordering is a prerequisite

The topological sort we are talking about here is based on the direction-free graph !!!

(⊙o⊙) ... I'm talking about a no-ring diagram that knows what it is.

If you do not know, let us first come to say what is the direction of the non-circular diagram.

The so-called non-circular map, as the name implies that there is no ring of the direction of the graph (as for the direction of the graph is what do not know in front of us there is a diagram of the explanation).

Point entry: The number of edges with this point as the end point.

The degree of the point: the number of edges that take this point as the starting point.

Topological order is an arrangement for a node, so that (U,v) belongs to E, then u must appear in front of v. Topological ordering, however, is something that is used to find the topological order.

For this diagram on the left, a valid topological order is (1,2,4,3,5).

There is another problem, after we know these things, how do we ask for a topological sequence of the graph without the ring?

We can observe that the topological ordering is defined as: if (u,v) ∈e, then the position where u appears in the arrangement must be in front of v.

That is, considering a node u, when we delete u in the sequence before all the points in front of him, you should be in the degree of 0.

So we get a rough idea of the algorithm for topological sequencing.

What is it exactly??

Loop n times

Select a point with an entry level of 0 and still exist (not appearing in the sequence) v

Delete Point V and all edges starting from point v to update the remaining point's entry

Repeat the process so that we can get a legitimate topological order.

In this case, let's summarize the essence of topological sequencing.

Essence (Concrete method):
⑴ Select a point with an entry level of 0 to join the topology sequence.
⑵ deletes the node and all its out edges (that is, its adjacent point in degrees minus 1).
Repeat these two steps until all nodes have entered the topological sequence.

Can you digest it?

If not, let's talk about the topological sort in detail below.

Reference Blog: http://blog.csdn.net/dm_vincent/article/details/7714519

One. Defined

The vertices in the graph are sorted in a linear fashion. That is, for any edge Uvs connected from the vertex u to the vertex V, the vertex U is always in front of the vertex v in the final sort result.

If the concept is still slightly abstract, consider a very, very classical example--elective course. I think anyone who has read the data structure-related books knows it. Let's say I really want to learn a machine-learning course, but before we do this, we have to learn some basic courses, such as computer science,C programming, data structures, algorithms and so on. So the process of making an elective course is actually a topological sequencing process, where each course corresponds to a vertex in the graph, and the forward edge between the connecting vertices is the sequence of course learning. It's just that the process is not so complicated that it's naturally done in our brains. The result of this process is described in the form of an algorithm, which is topological ordering.

Two. Code implementation

#include <bits/stdc++.h>using namespacestd;Const intmaxn=100000+ the;structedge{intX,y,next; Edge (intx=0,inty=0,intnext=0): X (x), Y (y), next (next) {}}EDGE[MAXN];intN,M,HEAD[MAXN],SUMEDGE,INN[MAXN];intInsintXinty) {edge[++sumedge]=Edge (x,y,head[x]); returnhead[x]=Sumedge;}intHEAD,TAIL,QUE[MAXN];intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=m;i++)    {        intx, y; scanf ("%d%d",&x,&y);        Ins (x, y); Inn[y]++; } Head=1; tail=0;  for(intI=1; i<=n;i++)     if(inn[i]==0) Que[++tail]=i;//If the point is in the degree of 0, delete the point connected to this point, after the deletion if the degree of 0, the point to the queue.     for(; head<=tail; head++)//The loop enumerates the points connected to the point, and the point is enqueued. {//Loop through the process until all points are queued (there are no loops, each point must be connected to a point so that we can repeat the process to take all the points into account, so that all the points are in the queue.)         intx=Que[head];  for(intu=head[x];u;u=Edge[u].next) {INN[EDGE[U].Y]--;//Delete the point connected to this point            if(inn[edge[u].y]==0)//If the entry level is 0 after the deletion,QUE[++TAIL]=EDGE[U].Y;//put that point in the queue        }    }    return 0;}

Let's just say this, and we'll say a few more examples below. (So take a look at the next page.)

Topology sequencing explained

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.