Topological search and Breadth first search

前端 未结 4 499
执笔经年
执笔经年 2020-12-10 14:58

Is it possible to use Breadth first search logic to do a topological sort of a DAG? The solution in Cormen makes use of Depth first search but wouldn\'t be easier to use BFS

4条回答
  •  感动是毒
    2020-12-10 15:12

    It is possible, even wikipedia describes an algorithm based on BFS.

    Basically, you use a queue in which you insert all nodes with no incoming edges. Then, when you extract a node, you remove all of its outgoing edges and insert the nodes reachable from it that have no other incoming edges.

提交回复
热议问题