Sort topological sorting python

In the field of computer science, there is a sort of first topology to sort its vertices, edges to UV, are the result of sorting prior to each have from vertex u to vertex v u v.

If the graph is a directed acyclic graph, the topological sorting is possible (why not say it necessarily?)

Having at least one DAG any topological sort, and these known algorithms for linear time constructs a DAG any topological sort

Graph theory: the combination is a branch of mathematics, it and other branches such as: group theory, topology, matrix theory has a close relationship. Figure is the main object of study of graph theory. FIG given by a number of vertices, and two connection pattern change point configuration, which are commonly used to describe a specific pattern relationship between something. Is used to represent the vertices of things, the edges between the vertices of the object having such a specific relationship between the representative.

In graph theory, the vertices of a sequence consisting of acyclic graph, and only when the following conditions are referred to as a topological sort of FIG change:

  Each vertex appears only once

  When B before A appears in the sequence, then the point A in route B does not exist in FIG.

 

ALGORITHM

Kahn algorithm

  Find the degree of the 0-point, point into the results, while deleting the node connected in this FIG then search again FIG once again find the 0 degree point, because this point has occurred parent in the result set, so the point can also be placed in the result set. FIG not repeated until the degree of point 0

  In this case the result set if the number of points equal to the number of the original point in the drawing, it shows the sort completed successfully, otherwise it is not described to be sorted FIG directed acyclic graph, not sort

 

Algorithm steps

  For the sorting process has been directed acyclic graph:

  Find the point of 0 degree, into the result set, and then deleting the edge connected to the node

  The above-described operation is repeatedly performed until the result of the midpoint of the number of nodes and the same as FIG.

 

Algorithm:

  

topological_sort DEF (G): 
    '' 'to locate the ingress node a is 0, the sequence is then placed, by deleting this point adjacent a list of' '' 
    topological_list = [] 
    node_list = List (Range (len (G ))) 
    node_list_temp = copy.deepcopy (node_list) # backup the reason for the node_list selected as follows: 
    # If you find first-degree point 0 is not deleted, then each while the end of the selection will be the point 
    # but we can not be deleted when traversing node_list, that would cause an infinite loop (similar to that for i in range (a) :, they were meant to operate in a cycle of 
    the while True: 
        
        IF len (topological_list) == len (G ): 
            return topological_list 
        node_list = node_list_temp 
        for node in node_list: 
            flage = True 
            for I in Range (len (G)): # traversal of a list of all nodes adjacent to the point, if none exists, not dependent on any point instructions, i.e., the degree of 0 
                IF in Node G [I]: 
                    flage = False
                    BREAK
            flage IF: 
                node_list_temp.remove (Node) 
                G [Node] = [] # blanking selected, otherwise it will cause an overflow index 
                topological_list.append (Node) 
                BREAK 
    # return topological_list

 

Efficiency Analysis

  Linear time

 

Generation and detection directed acyclic graph

  

 

 

Guess you like

Origin www.cnblogs.com/Gaoqiking/p/11412248.html