If I need to find shortest paths from one source to all other vertices in graph, which is both directed and weighed, can I use Dijkstras algorithm or do I need to use BellFord algorithm?
Since a proper implementation of Dijkstra is faster than Bellman-Ford, use Dijkstra unless there are negative weight edges in the graph. In this case Dijkstra can be incorrect, but Bellman-Ford will still return the correct answer.
Remember that if a graph has a negative weight cycle, then the shortest path is not well defined. Bellman-Ford can be modified to be able to check if a given graph has a negative weight cycle.
Related
Dijkstra isn't assured to work on graphs with negative weights: Why doesn't Dijkstra's algorithm work for negative weight edges?, but can I assume it works for one of the following cases (even with negative weights)
Directed Graph with no Directed Cycles
Directed Graph which its infrastructure graph is a tree (Connected and have no Cycles)
Note: By infrastructure graph I mean the same graph while removing the directions of all edges.
For 1. there are many counterexamples which you can find by searching, and some are given in the Q&A you linked to.
For 2., if the undirected version of the graph is a tree, then there cannot be more than one path between any two nodes. So whatever path Dijkstra's algorithm finds will be the shortest by default. I'll leave it to you to show that Dijkstra's algorithm does find a path if one exists.
Will Dijkstra's shortest path algorithm return correct results on a directed tree with negative weight edges?
On a general graph with negative weights, the algorithm will fail, but since it’s a directed tree it feels like the algorithm will succeed.
From other answers, you know that there is no good reason to run Dijkstra's algorithm if you know that the graph is a tree.
If you do run it, though, it will work even if the tree has negative edge weights.
The reason that Dijkstra's algorithm doesn't work for graphs with negative weights, is that negative weights allow a 2nd, shorter, path to be found to a vertex after its distance has already been decided. In a tree there are no 2nd paths.
In a tree there is only one path between any two given nodes, so searching for the "shortest" path in a tree makes little sense: when you find a path it is the shortest, and this search does not need to take weights into account, so there is no need to use Dijkstra's algorithm. A simple depth-first search will do.
If the graph is not a tree, but a directed acyclic graph (DAG) with negative edges, then Dijkstra's algorithm cannot be used to find a shortest path. Take this counter example:
If we have to look for the shortest path from A to C, Dijkstra's algorithm will proceed to visit B and C, and as it hits the target, it will stop looking further, never considering the edge from B to C.
Other attempts to apply Dijkstra
Another (now deleted) answer proposed to make all edge weights positive by adding an absolute value to all weights, but this does not yield correct results: what is the shortest path in the original graph, is not guaranteed to be still the shortest path in the derived graph.
Counter example:
Where in the original graph the shortest path from A to C runs via B, in the adjusted graph, the shortest path is A-C.
Given a directed graph with edges having -ve or +ve weights, what is the algorithms to find the smallest weight edges of all paths from vertex s to vertes d?
From Wikipedia
You are describing the Single Source shortest path problem. Which can be solved using Dijkstra's if the edges are only positive or Bellman-Ford if the edges are allowed to be negative as well.
The most important algorithms for solving this problem are:
Dijkstra's algorithm solves the single-source shortest path problem.
Bellman–Ford algorithm solves the single-source problem if edge weights may be negative.
A* search algorithm solves for single pair shortest path using heuristics to try to speed up the search.
Floyd–Warshall algorithm solves all pairs shortest paths.
Johnson's algorithm solves all pairs shortest paths, and may be faster than Floyd–Warshall on sparse graphs.
Viterbi algorithm solves the shortest stochastic path problem with an additional probabilistic weight on each node.
I would find all reachable from s, e.g. by depth first search. Then find all nodes that can reach d (equivalently, all nodes reachable from d in the graph with directions reversed). Now you want the smallest weight edges that start in the first set and end in the second set.
What is the most efficient shortest path algorithm performed on a graph that is not directed and only has positive edges out of these five algorithms?
BFS
DAG
Dijkstra
Floyd-Warshall
Bellman-Ford
So I know Dijkstra's can't be used on negative edges and has a running time of O(E * logV) where E is the number of edges and V is the number of vertices, so this would be my best guess. Is this correct?
If you need to find the shortest path in an unweighted graph, BFS would be the best option, however if there are weights on the edges, and you only need to find the optimal route between a single source and one or many other nodes, Dijkstra would be the best option. If you need to find the shortest path between any two pairs of nodes(i.e. have multiple sources), the best option is Floyd-Warshall.
I know Bellman Ford algorithm works well with negative weighted Graph, But I've developed a code of Dijkstra Algorithm that works very well. But it fails when I insert negative weighted edges. Any Solution?
I think we can't do that, as Dijkstra algorithm will not find a final way to reach at destination vertex because it may get stuck in loops, it is not made for negative weighted graphs, you should go for bellman ford algorithm
Actually, it is possible in a special case. Dijkstra algorithm will fail, if there is an edge of a negative length. However, if all edges are of negative length, then you may inverse the lengths of all edges and use the algorithm to find the longest path between two vertices of the graph (the path will represent the shortest path in the original graph).
But in a general case, as you have stated, it is not possible to use Dijkstra. If there is no cycle of negative length, than you shall use Bellman-Ford algorithm. If you cannot guarantee that there no cycle of negative length, than the problem is NP-complete and there is no known polynomial algorithm.
As you stated, Bellman Ford is the algorithm of choice for finding the shortest path in a graph with negative weights. The issue with using Dijkstra's Algorithm in this scenario is that Dijkstra's assumes that all possible subpaths from s to t in a graph must be smaller than the goal subpath, which is not necessarily true when negative edge weights are added.
If all edge weights are positive, this guarantees that adding more edges to a path makes it longer. Knowing this, Dijkstra's algorithm will discard any paths that are longer than the shortest one it has found to some vertex since there is no chance of the long path becoming shorter than the short path.
However, this assumption is not true if there are negative edge weights since we could have an extremely long path P that we discarded, but somewhere down the road, P can pass through a very negative edge and become shorter than the shortest path you currently have. Therefore, we can't guarantee that a path we've found is the shortest at any stage in Dijkstra's algorithm, which the algorithm relies on.