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

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?

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).

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.

BFS is good for finding shortest path from source to a vertex in case where all edges have same weight i.e. to find minimum no of edges from source to a vertex.
While Dikjstra holds good for weighted graphs

Related

Shortest path in a directed, weighted graph with set number of weights

Lets say there is a graph (V,E) that is directed and weighted.
The weights of every edge in the graph are 1 or 2 or 3.
What would be a quick and efficient algo to find the shortest path? Thanks in advance!!
Dijkstra's algorithm would likely still have near-optimal performance, and has the advantage of being fairly simple.
However, for a graph with small integer weights, you can use more complicated versions of the (fibonacci) heap used in Dijkstra that have better asymptotic performance. Specifically, consider this work by Mikkel Thorup which solves single-source shortest path in O(|E| + |V| log log |C|) where E is the set of edges, V is the set of vertices and C is the largest weight. In your case C is constant, which turns the asymptotic complexity into O(|E| + |V|).
Do note that this is mostly of theoretical interest, and is unlikely to give any significant speedup over a simpler algorithm.
There a list of algorithms for the Shortest path problem:
Dijkstra's algorithm
A* search algorithm
...etc
you can find a list in this Wikipedia page about Shortest path problem Algorithms
you can test and play with this online Dijkstra Shortest Path visualization.
a very interesting Pathfinding-Visualizer to see how some algorithms explore the research space to reach the goal is in github Pathfinding-Visualizer , this will give you a general idea on th differences between algorithms visually. Note: Pathfinding doesn't mean the shortest path; it's depend on the used algorithm.

Confusion regarding the algorithm to be applied for CCHESS

I am aware of the fact that I have to apply Dijkstra's algorithm to get an answer.The entire algorithm is explained in depth in one of the answers .
However why do we need to apply Dijkstra's algorithm to this problem.According to my knowledge Dijkstra will find the shortest distance path.
But the problem setter has clearly asked for minimum cost path.Considering this should'nt we apply Prim's algorithm to the question and find the MST for the entire chess board.
Here is the link to the problem.
Dijkstra's algorithm is indeed for finding the shortest distance path. However, note that "distance" does not have to mean distance measured in the normal way (i.e. with a ruler).
In fact Dijkstra's algorithm will also work for finding the shortest cost path in any network (providing that all the costs are greater than or equal to zero). All you need to do is to define the distance between any two nodes to be equal to the cost of the corresponding edge.
So, in this problem, when they search for the shortest path, they are defining distance in terms of the cost function defined in the problem.

Shortest path from single source to single destination in a graph

My graph contains no such edges which connect a vertex to itself. There is only one edge between two vertices. From Wikipedia i got to know about some algorithm which are used for calculating shortest path based on the given conditions. One of the most famous algorithm is Dijkstra's algorithm, which finds a shortest paths from source vertex to all other vertices in the graph.
But by using Dijkstra's algorithm, i am unnecessary exploring all the vertices, however my goal is just to find shortest path from single source to single destination. Which strategy should i use here? So that i need not to explore all other vertices.
One of my approach is to use bidirectional bfs. By bidirectional bfs i mean to apply two bfs one from source node, another one from destination node. As soon as for the first time when i find any same child in both tree,i can stop both bfs .Now the path from source to that child union path from child to destination would be my shortest path from source to destination.
But out of all the approaches described by Wikipedia and bidirectional bfs, which one suits best for my graph?
It depends if there's any apparent heuristic function that you could use or if you don't have any further usable information about your graph.
Your options are:
BFS: in general case or if you don't care about computation time that much.
Dijkstra (Dijkstra "is" BFS with priority queue): if your edges have weights/prices (non negative) and you care about CPU time.
A* (A* "is" Dijkstra with heuristic function - e.g. distance on a city map): if you want it to be really fast in average case, but you have to provide good heuristic function.
For some graph problems it may be possible to use Dynamic programming or other algorithmic tools. It depends on a situation. Further information can be found in tutorials from http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=alg_index ...
From Introduction to Algorithms (CLRS) second edition, page 581 :
Find a shortest path from u to v for a given vertices u and v. If we solve the single-source problem with source vertex u, we solve this problem also. Moreover, no algorithms for this problem are known that run asymptotically faster than the best single-source algorithms in the worst case.
So, stick to Dijkstra's algorithm :)
The Wikipedia article spells out the answer for you:
If we are only interested in a shortest path between vertices source and target, we can terminate the search at line 13 if u = target.
You could use Dijkstra's algorithm and optimize it in the way that you stop exploring paths that are already longer than the shortest you found so far. Because those are not going to be shorter (provided that you don't have negative weighs on your edges).

Does A* algorithm work with negative edge weights?

I was trying to figure out why the heuristic used in A* tree search needs to be admissible if A* has to be optimal. By tree search I mean that there is no explored set maintained by the algorithm.
While doing this, I ran into the question: Does A* work for negative edge weights?
The A* algorithm is basically Dijkstra’s algorithm with heuristics. And Dijkstra’s algorithm does not work with negative edge weights. So A* won’t work with negative edge weights neither.
If you’re looking for an algorithm that works with negative edge weights, take a look at the Bellman-Ford algorithm (but it doesn’t use heuristics).
This excellent article regarding Dijkstra's may prove helpful and provides a nice example about negative edges...
http://www.ics.uci.edu/~eppstein/161/960208.html

How is Dijkstra algorithm better tham A* algorithm for finding shortest path?

How is Dijkstra algorithm better tham A* algorithm for finding shortest path?
It is not better at finding the shortest path. As long as you have an admissible heuristic for A* it will find the shorted path quicker than Dijkstra's would.
And as Mehrad pointed out in the comments, A* degrades into Dijktras if you give it a heuristic function that returns 0.
The wikipedia article for A* has a ton of good information on all of this.
If you want to find the shortest paths from a given source node to all nodes in a graph Dijkstra is better than A* since Dijkstra covers the whole graph anyway if you don't stop at a specific target. In contrast to simple Dijkstra A* is a goal-oriented algorithms and therefore needs to know both source and target node. If you wanted to cover the whole graph with N nodes with an A* algorithm you basically have to run the algorithm N times from the source node.
Dijkstra might also be better for finding the shortest paths from a source node to many targets of the graph. Depending on the location of the targets (especially distance from the source), number of targets M, size of the graph, quality of the A* heuristic there will be some break-even point where running one Dijkstra is better or less performant than running M times the A* algorithm.
Edit: Inspired by Matthew's correct critical comment I rephrase a bit and add remarks:
Dijkstra is not better in finding the shortest paths to all nodes than A*. Correct would be: It's not worse than A*.
To find the shortest paths to all nodes with A* one would set the function which estimates the costs to the target node to zero (since there is no target node). Setting the function which estimates the costs to the target node to zero makes A* identical with Dijkstra; Dijkstra is a special case of A*. (It's questionable if one should call the algorithm still "A*" (and not simply "Dijkstra") if the function is set to zero, because having a non-zero function is the core of A*.)
When we try to find the shortest path to all nodes, we could also say: Dijkstra is sufficient. The refinement of A* is not necessary and doesn't help us here.
The last remark is also true for searching in a graph, for instance: Find the node marked with a specific flag which is closest to a given source node. Since you don't know the target in advance it's not possible to define a function which estimates the costs to the searched node, thus A* (in the narrower sense of a non-zero function) is not applicable.

Resources