Topological search and Breadth first search

前端 未结 4 500
执笔经年
执笔经年 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条回答
  •  旧时难觅i
    2020-12-10 15:09

    In a BFS all of the edges you actually walk will end up in the correct direction. But all the edges you don't walk (those between nodes at the same depth, or those from deeper nodes back up to earlier nodes) will end up going the wrong way if you lay out the graph in BFS order.

    Yes, you really need DFS to do it.

提交回复
热议问题