dijkstra's vs Bellman-Ford algorithm [duplicate] - algorithm

This question already has answers here:
Why doesn't Dijkstra's algorithm work for negative weight edges?
(12 answers)
Closed 3 years ago.
my current understanding is that dijkstra's algorithm is more efficient then bellman-ford, only it cannot handle negative edges. However say we have an edge weighted graph where there are negative-weight edges, there are no negative-weight cycles in the graph, can we still use dijkstra's algorithm?

This question has already been answered well here.
https://stackoverflow.com/a/13159425/12449779
To summarize, Dijkstra's algorithm in each iteration marks the vertices at minimum distances from "marked" vertices. Initially, the source vertex is the only marked vertex with distance 0. Suppose two marked vertices exists A and B who are at a certain finite distance from source and their paths do not overlap. Currently, the paths to A and B would be considered as the shortest possible paths from A to B. Since the paths do not overlap, any path from Source to A to B would be of length source to A + length of Path from A to B which is greater than path from source to B as paths from source to A and B do not overlap. But if negative weighted edges are allowed, this does NOT hold. As the length of path from A to B may be negative. Thus Dijkstra's Algorithm does not work. There is no simple way to convert a general graph with negative edges to one with non-negative edges either. Bellman Ford works for any graph which does not contain negative weighted cycles.

Related

Modified Dijkstra's Algorithm

We are given a directed graph with edge weights W lying between 0 and 1. Cost of a path from source to target node is the product of the weights of edges lying on the path from source to target node. I wanted to know of an algorithm which can find the minimum cost path in polynomial time or using any other heuristic.
I thought along the lines of taking the log values of the edges weights (taking mod values) and then applying dijkstra for this graph but think there will be precision problems which can't be calculated.
Is there any other better way or can I improve upon the log approach.
In Dijkstra's algorithm, when you visit a node you know that there is no shorter road to this node. This is not true if you multiply the edges with weights between 0..1 as if you visit more vertices you will get a smaller number.
Basically this is equivalent of finding the longest path in a graph. This can be seen also by using your idea of taking logarithms, as the logarithm of a number between 0 and 1 is negative. If you take absolute values of the logarithms of the weights, the longest path corresponds to the shortest path in the multiplicative graph.
If your graph is acyclic there is a straightforward algorithm (modified from Longest path problem).
Find a Topological ordering of the DAG.
For each vertex you need to store the cost of path. Initialize this to one at the beginning.
Travel through the DAG in topological order starting from your start vertex. In each vertex check all the children and if the cost is smaller than previously, update it. Store also the vertex where you arrive at this vertex with the lowest cost.
After you reach your final vertex, you can find the "shortest" path by travelling back from the end vertex using the stored vertices.
Of course, if you graph is not acyclic you can always reach a zero end cost by repeating a loop infinitely.

Shortest path spanning tree with 1 weighted edges vs MST

I currently studying about graphs and their algorithms, and i noticed a question which i don't know to to exactly prove:
If we have a connected, undirected graph G=(V,E), and every edge is with weight=1,is it true to say that every spanning tree that built from the shortest paths from the root, is a minimum spanning tree?
I ran some examples in http://visualgo.net/sssp.html and is seems for me that the answer for this question is true, but someone can show me how can i prove this?
and another question that crossed my mind, does the other direction is also true?
Every tree has exactly n - 1 edges. Since all weights are equal to 1, every spanning tree of G has a total weight of n - 1. It is also true for the minimal spanning tree. So the answer is yes.
TLDR: Not Necessarily
Consider the triangle graph with unit weights - it has three vertices x,y,z, and all three edges {x,y},{x,z},{y,z} have unit weight. The shortest path between any two vertices is the direct path. Agree?However, to satisfy the condition of a MST in the graph, you have to put together a set of 2 edges connecting the 3 vertices. Say {x,y}, {y,z} for example. This does not represent the shortest path between any pair of vertices.
Hence, your proposition is false :)

Dijkstra's Algorithm for Negative Weights

Okay, first of all I know Dijkstra does not work for negative weights and we can use Bellman-ford instead of it. But in a problem I was given it states that all the edges have weights from 0 to 1 (0 and 1 are not included). And the cost of the path is actually the product.
So what I was thinking is just take the log. Now all the edges are negative. Now I know Dijkstra won't work for negative weights but in this case all the edges are negative so can't we do something so that Dijkstra would work.
I though of multiplying all the weights by -1 but then the shortest path becomes the longest path.
So is there anyway I can avoid the Bellman-Ford algorithm in this case.
The exact question is: "Suppose for some application, the cost of a path is equal to the product all the weights of the edges in the path. How would you use Dijkstra's algorithm in this case? All the weights of the edges are from 0 to 1 (0 and 1 are not inclusive)."
If all the weights on the graph are in the range (0, 1), then there will always be a cycle whose weight is less that 1, and thus you will be stuck in this cycle for ever (every pass on the cycle reduces the total weight of the shortest path). Probably you have misunderstood the problem, and you either want to find the longest path, or you are not allowed to visit the same vertex twice. Anyway, in the first case dijkstra'a algorithm is definitely applicable, even without the log modification. And I am pretty sure the second case cannot be solved with polynomial complexity.
So you want to use a function, let's say F, that you will apply to the weights of the original graph and then with Dijkstra's algorithm you'll find the shortest product path. Let's also consider the following graph that we start from node A and where 0 < x < y < 1:
In the above graph F(x) must be smaller than F(y) for Dijkstra's algorithm to output correctly the shortest paths from A.
Now, let's take a slightly different graph that we start again from node A:
Then how Dijkstra's algorithm will work?
Since F(x) < F(y) then we will select node B at the next step. Then we'll visit the remaining node C. Dijkstra's algorithm will output that the shortest path from A to B is A -> B and the shortest path from A to C is A -> C.
But the shortest path from A to B is A -> C -> B with cost x * y < x.
This means we can't find a weight transformation function and expect Dijkstra's algorithm to work in every case.
You wrote:
I though of multiplying all the weights by -1 but then the shortest
path becomes the longest path.
To switch between the shortest and the longest path inverse the weights. So 1/3 will be 3, 5 will be 1/5 and so on.
If your graph has cycles, no shortest path algorithm will find an answer, because those cycles will always be "negative cycles", as Rontogiannis Aristofanis pointed out.
If your graph doesn't have cycles, you don't have to use Dijkstra at all.
If it is directed, it is a DAG and there are linear-time shortest path algorithms.
If it is undirected, it is a tree, and it's trivial to find shortest path in trees. And if your graph is directed, even without cycles, Dijkstra still won't work for the same reason it doesn't work for negative edge graph.
In all cases, Dijkstra is a terrible choice of algorithm for your problem.

Dijkstra vs Bellman- ford A Directed Graph which will give different result

I am trying to learn Graphs in which i found that to find shortest path from one node to other node we can use Dijkstra and Bellman-ford algorithm.
In which Dijkstra will not work for the Graph which contains negative weight edges.
While Brllman-ford can handle such Graph which contains negative weight edges.
My doubt is i tried many kind of Graphs which contains negative weight edge and applied Dijkstra and Bellman-ford both but in all the cases i found the same result i mean no difference, for negative weight edge also dijkstra is working fine.
May be my thought process or the way how i am solving is wrong so only i am getting correct answer for dikstra.
My question is can any one explain me a Graph which have negative edge and explain the different result for dijkstra and bellman-ford.
Djikstra algorithm to find the shortest path between two edges can be used only for graphs that have positive weights. To see the difference of answers that bellman-ford and djikstra gives when there is a negative edge weight, lets take a simple example
we have 3 nodes in the graph, A B C
A is connected to B edge weight 4
A is connected to C edge weight 2
B is connected to C edge weight -3
when djikstra is used to calculate shortest path between A and C, we get weight 2
but when bellman-ford is used to calculate the shortest path between A and C, the weight is 1
This is happening because of the fact that djikstra finalises the node which has the minimum edge weight, ignoring the fact that there could be path with less weight to that node (note that this could happen only when negative weights are present. with only positive weights this is not possible).
hope you understood the difference

Does a polynomial time shortest path algorithm exist given that both source and target verteces are reachable from a negative cycle?

I'm not asking for an algorithm to check the existence of a negative cycle in a graph(Bellman Ford or Floyd Warshall can do that), rather whether or not there exists a polynomial time algorithm to find the shortest path between two points when the graph contains at least one negative cycle that is reachable from the source vertex and the target vertex can be reached from the negative cycle.
If you are looking for a path (no repeating vertices) then this problem is NP hard.
You can reduce longest path problem to this one by simply multiplying weights by -1.
The main difference between classical (only positive weights on edges) shortest path (which is in P) and longest path problem (which is NP-complete) is following:
All polynomial shortest path algorithms are basically calculating shortest walk, but since you have positive weight on edges, the shortest walk is also shortest path (repeating edges does not decrease the length of walk).
In longest path problem you have to somehow check for uniqueness of vertices on the path.
When you have negative cycle, then the Bellman-Ford algorithm calculates the length of the shortest walk which has at most N edges.

Resources