Topological order using bfs

前端 未结 3 1101
后悔当初
后悔当初 2020-12-21 05:05

The following question was found in Sedgewick and Wayne book about algorithms in java:

4.2.19 Topological sort and BFS. Explain why the followin

3条回答
  •  北海茫月
    2020-12-21 05:42

    You cannot use BFS, because a node with a higher rank may have an incident edge with a lower rank. Here's an example:

    Let's say you start BFS at the source (A). DAG

    With the algorithm you proposed, node D would come before node C, which is clearly not a topological order. You really have to use DFS.

提交回复
热议问题