Why use Dijkstra's Algorithm if Breadth First Search (BFS) can do the same thing faster?

后端 未结 4 1175
南笙
南笙 2020-11-30 18:47

Both can be used to find the shortest path from single source. BFS runs in O(E+V), while Dijkstra\'s runs in O((V+E)*log(V)).

Also, I\'ve s

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 19:12

    Dijkstra allows assigning distances other than 1 for each step. For example, in routing the distances (or weights) could be assigned by speed, cost, preference, etc. The algorithm then gives you the shortest path from your source to every node in the traversed graph.

    Meanwhile BFS basically just expands the search by one “step” (link, edge, whatever you want to call it in your application) on every iteration, which happens to have the effect of finding the smallest number of steps it takes to get to any given node from your source (“root”).

提交回复
热议问题