topological sorting in almost all projects, even in daily life, between different tasks to be completed usually there are some dependencies, these dependencies for their execution order schedule constraints. For this dependency relationship, it is easy to express it as a Directed Acyclic Graph (DAG, acyclic is an important condition), and the process of finding the sequence of dependency is called topological sorting (topological sorting). The

topology ordering meets the following two conditions,

- , each vertex appearing and only once.
- if A is in the front of the B in the sequence, there is no path from B to A in the graph.

topological sorting algorithm without any loop vertex activity network (AOV network) N can make

- topological sequence:
from N to a selected degree of 0 vertices as the next vertex sequence.

- removes the selected vertices and all the edges from the N net.
- executes the above two steps repeatedly, knowing that all the vertices of the graph have been selected, or the vertices with no 0 degree can be found again.

if the rest is not 0 of the vertices, it shows that there is a loop in the N, and there is no topological sort. The existence of a loop in the

means that the beginning of some activity is a prerequisite for its own completion, which becomes a deadlock between activities. An example of a common vertex active network is the first course of a university course. Curriculum knowledge is practiced before and after. One course may be based on knowledge of other courses. When students want to take this course, they need to see if they have already completed all previous courses. If there is a loop, that means entering a cycle, then the student will not be able to work.

, therefore, can be said that the topology sorting algorithm is designed to make work arrangements to satisfy the constraints.

we operate a case like this is a directed acyclic graph:

dictionary said: G = {'a':'bce','b':'d','c':'d','d':'e':'cd'}

.

** **

def toposort Code:(graph): in_degrees = dict ((U, 0) for u in graph) # initialize all the vertex degree of 0 vertex_num = len (in_degrees) for u in graph: for V in graph[u]: Q = [u in_degrees[v] = 1 in each # calculation the for u in in_degrees if vertex in_degrees[u] = = 0] # screening degree of 0 vertices of Seq = while Q: u = Q.pop ([]) # default delete Seq.ap from the last Pend (U) for V in graph[u]: in_degrees[v] = 1 # to remove all of its if in_degrees[v] = = 0: Q.append (V) # again screening degree of 0 vertices of if len (Seq) = vertex_num: # cycle after the end of 0 if the existence of non penetration vertices that ring in the picture, there is no return Seq else: topological sorting print ("there's a circle.") G = {'a':'bce','b':'d','c':'d','d':'e':'cd'}', print (toposort (G))

['a', output:'e','c','b', ring'd']

:

G = {'a':'bce','b':'d','c':'d','d':'e','e':'cd'}

there's a output: circ Le. None

summarized above is all the contents of this article, I hope this paper has some reference value of learning to learn or work, if in doubt, you can exchange messages, thank you for the support of the script.

#### you might be interested in this article:

- python sort, sorted
- python to list in average and integer
- python
- Python bubble sort algorithm code
- python bubble sort to achieve a simple method
- python quick sort code An instance of the
- python
- python
- python

code selection sort sorting algorithm algorithm

This paper fixed link:http://www.script-home.com/a-basic-tutorial-for-python-to-implement-topological-sorting.html | Script Home |** +Copy Link **

Article reprint please specify:A basic tutorial for Python to implement topological sorting | Script Home