Relationship between BFS and topological sort

前端 未结 2 1175
无人共我
无人共我 2020-12-18 08:23

Topological sort can be done using both a DFS(having edges reversed) and also using a queue . A BFS can also be done using a queue . Is there any relationship between the wa

2条回答
  •  猫巷女王i
    2020-12-18 08:54

    The level by level traversal of BFS from a source node makes the nodes appear in the order of their distance from source which also means that parent nodes appear before their children nodes which are in the next level.

    This might appear like what we need in a topological sort, however, stay with me. The next level in the previous sentence is the key, because if a node and its child are in the same level from source, then BFS enforces no order in traversing them meaning it may present the node's child before itself and this will be a direct violation to the rule of topological sort and the ordering is indeed important when we want a topological sort.

    Although it seems like there is a relationship between BFS and topological sort, it is rather weak.

提交回复
热议问题