ACM courseware (lecture_06) and query set learning notes-topological sorting

Source: Internet
Author: User
ACM courseware (lecture_06) and query set learning notes <3> -- topological sorting

 

I. topological sorting

A full sequence of a set is obtained from an partial order of a set. In fact, all vertices in graph G are arranged in a linear order. Intuitively, partial order means that only some Members in the set can be compared, while full order means that all members in the set can be compared.

2. Perform topological sorting on a directed acyclic graph

Is to arrange all vertices in the graph into a linear sequence. If the vertices in the graph are arranged in a line in the topological order, all the directed edges are directed from left to right. Obviously, Topology Sorting is not allowed if a directed ring exists in the graph.

3. A directed graph can be used to represent a project.

In this directed graph, vertices are used to represent activity, and directed edge <Vi, Vj> is used to indicate that activity Vi must be prior to activity Vj, that is, arc is used to represent priority of activity. This directed graph is called vertex to represent the active AOV Network (Activity On Vertices ).

Iv. AOV Network

In the AOV network, if the active Vi must be performed before the active Vj, a directed edge <Vi, Vj> exists, and no loop exists in the AOV network, that is, a directed ring. If a directed ring appears in the AOV network, it means that an activity should take itself as a prerequisite. Therefore, for a given AOV network, you must first determine whether it has a directed ring.

5. One way to check whether a directed ring exists is to construct the topological sequence of the AOV network.

The various vertices (representing various activities) are arranged into a linear ordered sequence, so that all the precursor and successor relationships in the AOV network can be satisfied. This operation of constructing the topological ordered sequence of all vertices in the AOV network is called topological sorting.

1. If all vertices in the AOV network can be ranked in a topological ordered sequence, directed loops will not appear in the AOV network;

2. On the contrary, if the topological sequence that meets the requirements is not obtained, it indicates that there is a directed ring in the AOV network, and the project represented by this AOV network is not feasible.

Topology sequence can be obtained because there is no directed ring in. Therefore, it is feasible to arrange courses according to this method.

For example, to sort the topology of the selected engineering drawing, the obtained topological sequence is:

C1, C2, C3, C4, C5, C6, C8, C9, C7

Or C1, C8, C9, C2, C5, C3, C4, C7, C6

 

 

Vi. Topology Sorting Algorithm ideas

1. Select a vertex without a direct precursor in the AOV network and output it;

2. Delete the vertex and delete all directed edges it emits;

3. Repeat the preceding steps

◆ All vertices have been output, and the topological sequence is formed. The topological sorting is completed;

◆ There are still unoutput vertices in the graph, but the processing cycle has exceeded. This shows that there are still some vertices in the graph. They all have a direct precursor and no longer find any vertices without the precursor. At this time, the AOV network must have a directed ring.

 

VII. topological sorting algorithm

Status topologicalsort (ALGraph G ){

// Directed graph G adopts the storage structure of the adjacent table

// If G has no loop, a topological sequence of the vertex of G is output and OK is returned. Otherwise, error is returned.

Findindegree (G, indegree );

Initstack (s );

For (I = 0; I <G. vexnum; ++ I)

If (! Indegree [I]) push (s, I );

Count = 0;

While (! Stackempty (s )){

Pop (s, I); printf (G. vertices [I]. data); ++ count; // output vertex I and count

For (p = G. vertices [I]. firstarc; p = p-> next ){

K = p-> adjvex; // subtract 1 from each adjacent point of vertex I

If (! (-- Indegree [k]) push (s, k); // If the inbound degree is reduced to 0

}

}

If (count <G. vexnum) return error; // The directed graph has a loop.

Else return OK;

}

VIII. Topology Sorting Algorithm Summary

According to this topological sorting algorithm, if the AOV network has n vertices and e edges, search for vertices whose input degree is zero during the topological sorting process, the time required to create a vertex stack is O (n ). Under normal circumstances, a directed graph has n vertices. Each vertex is pushed to the stack once, And the stack is output n times in total. The vertex increment minus one operation executes e times. Therefore, the total time complexity is O (n + e ).

 

 

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.