在什么情况下,我应该使用BFS和使用DFS使用拓扑排序?
使用 bfs 和 dfs 的拓扑排序具有相同的时间复杂度
即O(V+E),其中V=>顶点数和 E =>边数
但问题是在什么情况下使用哪种算法???
both topological sorting using bfs and dfs has same time complexity
ie O(V+E) where V => number of vertices and E => number of edges
but the question is which algo to use in what condition????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用广度优先排序,您将在更靠近开头的位置获得距离根最近的节点。但对于深度优先排序,排序不会与根的接近程度相对应,而是与整个或部分路径一个接一个地相对应。因此:
例如,您需要BFS(广度优先排序)来执行淹没算法,其中您需要每个深度级别的节点在下一个级别的节点之前全部处理完毕。案例一:构建网络地图。
另一方面,DFS(深度优先排序)用于处理依赖关系,您必须在处理任何其他路径之前处理节点的完整路径。编译系统或作业调度程序可以从这种排序中受益。
With a breadth first sort you would have the closest nodes to the root in positions closer to the beginning. But with a depth first sort, the ordering would not correspond with the closeness to the root, but with whole or partial paths, one after another. So:
You would require BFS, (Breadth First Sort) for example, to perform an inundation algorithm where you need the nodes of each depth level to be all processed before the ones in the next level. One case: to build network maps.
On the other hand, DFS (Depth First Sort) is used to process dependencies, where you must process a full path of nodes before processing any other path. Compile systems or job schedulers benefit from this kind of sort.