Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use Dijkstra's algorithm instead of Best (Cheapest) First Search?

From what I have read so far. The Best First Search seems faster in terms of finding the shortest path to the goal because Dijkstra's algorithm has to relax all the nodes as it traverses the graph. What makes Dijkstra's algorithm better than Best First Search?

like image 891
Alexander Suraphel Avatar asked Apr 29 '12 17:04

Alexander Suraphel


People also ask

Why use Dijkstra's algorithm instead of BFS?

If you consider travel websites, these use Dijkstra's algorithm because of weights (distances) on nodes. If you will consider the same distance between all nodes, then BFS is the better choice. For example, consider A -> (B, C) -> (F) with edge weights given by A->B = 10, A->C = 20, B->F = C->F = 5.

What is the advantage of Dijkstra's algorithm?

The main advantage of Dijkstra's algorithm is its considerably low complexity, which is almost linear. However, when working with negative weights, Dijkstra's algorithm can't be used. , if we need to calculate the shortest path between any pair of nodes, using Dijkstra's algorithm is not a good option.

Is Dijkstra better than DFS?

Most people prefer Dijkstra to DFS in pathfinding because Dijkstra is so accurate. Well, Dijkstra finds the shortest path from the starting point. DFS does not guarantee shortest path, it would just generate a path that visits very nodes in the graph. Dijkstra finds the shortest path for weighted graphs.

Which is better BFS or Dijkstra?

Dijkstra's algorithm is more general than BFS,in deed it is a generalization of BFS where edges' weights no longer have to be equal - this is “THE” only significant difference. For efficiency reason,a FIFO queue in BFS generalizes to a priority queue in Dijkstra.

What is Dijkstra's algorithm?

Dijkstra's algorithm (or Dijkstra's Shortest Path First algorithm, SPF algorithm) is an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks.

What is the difference between Dijkstra and Bellman Ford algorithm?

Unlike Dijkstra's algorithm, the Bellman–Ford algorithm can be used on graphs with negative edge weights, as long as the graph contains no negative cycle reachable from the source vertex s. The presence of such cycles means there is no shortest path, since the total weight becomes lower each time the cycle is traversed.

What is single source shortest path algorithm?

Overview In graph theory, SSSP (Single Source Shortest Path) algorithms solve the problem of finding the shortest path from a starting node (source), to all other nodes inside the graph. The main algorithms that fall under this definition are Breadth-First Search (BFS) and Dijkstra ‘s algorithms.

What is the difference between prim's and Dijkstra?

Prim's purpose is to find a minimum spanning tree that connects all nodes in the graph; Dijkstra is concerned with only two nodes. Prim's does not evaluate the total weight of the path from the starting node, only the individual edges.


2 Answers

EDIT: Your edit clarifies you are interested in Best-First Search, and not BFS.

Best-First Search is actually an informed algorithm, which expands the most promising node first. Very similar to the well known A* algorithm (actually A* is a specific best-first search algorithm).

Dijkstra is uninformed algorithm - it should be used when you have no knowledge on the graph, and cannot estimate the distance from each node to the target.

Note that A* (which is a s best-first search) decays into Dijkstra's algorithm when you use heuristic function h(v) = 0 for each v.

In addition, Best First Search is not optimal [not guaranteed to find the shortest path], and also A*, if you do not use an admissible heuristic function, while Dijkstra's algorithm is always optimal, since it does not relay on any heuristic.

Conclusion: Best-First Search should be prefered over dijkstra when you have some knowledge on the graph, and can estimate a distance from target. If you don't - an uninformed Best-First Search that uses h(v) = 0, and relays only on already explored vertices, decays into Dijkstra's algorithm.
Also, if optimality is important - Dijkstra's Algorithm always fit, while a best-first search algorithm (A* specifically) can be used only if an appropriate heuristic function is available.


Original answer - answering why to chose Dijkstra over BFS:

BFS fails when it comes to weighted graphs.

Example:

     A
   /   \
  1     5
 /       \
B----1----C

In here: w(A,B) = w(B,C) = 1, w(A,C) = 5.

BFS from A will return A->C as the shortest path, but for the weighted graph, it is a path of weight 5!!! while the shortest path is of weight 2: A->B->C.
Dijkstra's algorithm will not make this mistake, and return the shortest weighted path.

If your graph is unweighted - BFS is both optimal and complete - and should usually be prefered over dijkstra - both because it is simpler and faster (at least asymptotically speaking).

like image 90
amit Avatar answered Sep 21 '22 01:09

amit


Normally Best First Search algorithms in path finding, searching for path between two given nodes: Source and Sink, but Dijkstra's algorithm finds path between source and all other nodes. So you can't compare them. Also Dijkstra itself is kind of Best First Search (variation of A*) means you can't say it's not Best First Search. Also normal Best First Search Algorithms are using heuristics and they're not guarantee about correctness, Finally in weighted case normally their running time depends on weights, but Dijkstra's algorithm just depend to graph size.

like image 36
Saeed Amiri Avatar answered Sep 21 '22 01:09

Saeed Amiri