Let's assume that I have a graph G and all of the edges in G have negative weights without negative cycle.
If I run this by Dijkstra Algorithm, will it find a shortest path?
Thanks
Let's assume that I have a graph G and all of the edges in G have negative weights without negative cycle.
If I run this by Dijkstra Algorithm, will it find a shortest path?
Thanks
I think it won't. There is a similar question with answer here: Why doesn't Dijkstra's algorithm work for negative weight edges?.
The only difference between your question and that one is that your graph doesn't have positive edges at all. Still, i.e. in this graph:
A
/ \
/ \
/ \
-2 -5
< >
B--(-10)-->C
If the algorithm starts from A
it will find A->C
as shortest path to C
(where the shortest path is actually A->B->C
).
EDIT:
Directed edges are: (A,C,-5)
, (A,B,-2)
, (B,C,-10)
If the graph is undirected, with only negative edge weights and without cycles, then the graph is a tree and there it should work. At least as long as you look for simple paths, without going back and forth over and over again.
E.g. A---B---C
all weights are -1. Then the shortest simple path from A to C would be A,B,C, but if you drop the simple restriction, then there is no shortest path anymore. Instead it would look like this A,B,A,B,A,B, ... ,C.
If your graph is directed, the example from @h3yduck can be made directed and is a counterexample that shows that Dijkstras Algorithm does not work in this case.