DAG图的拓扑排序 python

在DAG中DFS中顶点的出栈顺序即逆拓扑序。

DAG图的拓扑排序 python


def topological_sort( graph ):

    is_visit = dict( ( node, False ) for node in graph )
    li = []

    def dfs( graph, start_node ):
        
        for end_node in graph[start_node]:
            if not is_visit[end_node]:
                is_visit[end_node] = True
                dfs( graph, end_node )
        li.append( start_node )
    
    for start_node in graph:
        if not is_visit[start_node]:
            is_visit[start_node] = True
            dfs( graph, start_node )

    li.reverse()
    return li
    
            
if __name__ == '__main__':
    graph = {
        'v1': ['v5'],
        'v2': ['v1'],
        'v3': ['v1', 'v5'],
        'v4': ['v2', 'v5'],
        'v5': [],
    }
    li = topological_sort( graph )
    print li


DAG图的拓扑排序 python,布布扣,bubuko.com

DAG图的拓扑排序 python

上一篇:进程/线程


下一篇:50个国内外最棒的C/C++源码站点分享