It's stated in a book that "Dijkstra's algorithm only works with Directed Acyclic Graphs".
It appears the algorithm works for graphs with cycles too as long as there are no negative cycles. Is that correct?
Edit 1:
The book "Grokking Algorithms" -Aditya Bhargava.
Chapter 7. Page 122.
I'm the author of Grokking Algorithms. Sorry for this error—Dijkstra's algorithm does work on graphs with cycles, as long as it is a positive weight cycle. I have updated the errata page to reflect this error. Dijkstra's doesn't work on negative weight cycles, and here's an image that explains why:
Actually, it works as long as all edge weights are non-negative. This is a stronger condition as "no negative cycles". On the other hand it would not work on a DAG with negative weights. So, provided you cited correctly, the statement from the book is wrong for two reasons.
Btw. if you have negative cycles, there may no longer be a shortest path since you may cycle an infinite number of times and go down with your cost as much as you like.
In case someone is looking for an example DAG with negative weights where Dijkstra does not give the correct shortest path: http://stackoverflow.com/a/6799344/3924118
Related
Can someone help me with this question
I am still confused weather Dijkstra's algorithm works with negative edges or not
this question is from Grokking Algorithms book and in its errata it is said that this question has a possible answer
how does it has a possible answer with a negative edge?
First of all Dijkstra's algorithm won't work on all negative weighted graphs, but on this particular case you can see that the negative weight not make any different on the shortest way to get from the starting point to any other point on the graph. this is why you will get the same result with the negative edge or without.
The idea why Dijkstra's won't work on negative weighted graphs is explained pretty well here (thanks to Amit) -> Why doesn't Dijkstra's algorithm work for negative weight edges?
It's been literally 10 years since I last touched anything to do with Dijkstra, but I think this one doesn't break because every time you pass over the negative portion, the loop still increases the sum (minus 1 vs several plus 2). This way, the path with the negative is discarded. But change that minus 1 to minus 5 and then you have a problem.
I'm not on my PC right now to apply this graph to my 10 year old homework, but I'm pretty sure that's what's going on.
I know that Dijkstra's algorithm can be used only on positive lengths of edges, and Bellman-Ford can be used when the graph also has negative ones.
Suppose we have a graph with only positive edges, though. Will Bellman-Ford give the same results as Dijkstra?
Yes, it will give the same results. It will run slower, though, as it could also have been used for graphs with negative edges (subject to the absence of negative cycles). If you look at the proof of BF's correctness, there is no assumption there that some of the edges are negative.
I want to add something to Ami Tavory's answer. Bellman-ford's algorithm can be made a little bit faster if you can detect that on any pass, there is no node value update, then return from there. If there is no node update then it proves that every node traversal is complete.
Can we use Dijkstra's algorithm to find cycles???
Negative cycles
Positive cycles
If we can what, are the changes we have to do?
1) Dijkstra's doesn't work on graphs with negative edges because you can (possibly) find a minimum distance of negative infinity.
2) Um, you normally run it on graphs with cycles (otherwise, you might as well be traversing a tree), so it can handle them just fine.
If your real question is just about finding cycles, look at Finding all cycles in graph
No We cant use Dijkstra algorithm if negative cycles exist as the algorithm works on the shortest path and for such graphs it is undefined.Once you get to a negative cycle, you can bring the cost of your "shortest path" as low as you wish by following the negative cycle multiple times.
This type of restriction is applicable to all sort of algorithms to find shortest path in a graph and this is the same reason that prohibits all negative edges in Dijkstra.
You may modify Dijkstra to find the cycles in the graph but i do not think it is the best practice.
Rather you can possibility Use:
Tarjan's strongly connected components algorithm(Time complexity -O(|E| + |V|))
or Kosaraju's algorithm (uses DFS and is a linear time alogoritham)
or you may follow this link for better idea:
https://en.wikipedia.org/wiki/Strongly_connected_component
Hope I answered your question.
Hey i have been studying the bellman ford algorithm for "single source shortest path" problems.
Now i am stuck at one point where i need to find out the solution for a graph having negative weight cycle.
But Bellman ford algorithm does not work here.
Can some one suggest me what to do. How to solve a problem having negative weight cycle?
Thanks for your time.
If there is a negative cycle which is reachable from the origin, which Bellman-Ford can detect, then you have two choices: either allow repeating edges, or do not. If you allow repeating edges, your shortest path could be considered to be infinitely negative. Otherwise, if you do not, the problem is NP complete. From Wikipedia:
One NP-Complete variant of the shortest-path problem asks for the shortest path in G (containing a negative cycle) such that no edge is repeated.
In a paper here discussed here the authors (Wulff-Nilson, Nanongki, Berstein) mention that a graph with negative-weight cycles can be reduced to one without such cycles and then they give a method that finds the shortest path in nearly linear time.
Ok, I posted this question because of this exercise:
Can we modify Dijkstra’s algorithm to solve the single-source longest path problem by changing minimum to maximum? If so, then prove your algorithm correct. If not, then provide a counterexample.
For this exercise or all things related to Dijkstra's algorithm, I assume there are no negative weights in the graph. Otherwise, it makes not much sense, as even for shortest path problem, Dijkstra can't work properly if negative edge exists.
Ok, my intuition answered it for me:
Yes, I think it can be modified.
I just
initialise distance array to MININT
change distance[w] > distance[v]+weight to distance[w] < distance[v]+weight
Then I did some research to verify my answer. I found this post:
Longest path between from a source to certain nodes in a DAG
First I thought my answer was wrong because of the post above. But I found that maybe the answer in the post above is wrong. It mixed up The Single-Source Longest Path Problem with The Longest Path Problem.
Also in wiki of Bellman–Ford algorithm, it said correctly :
The Bellman–Ford algorithm computes single-source shortest paths in a weighted digraph. For graphs with only non-negative edge weights, the faster Dijkstra's algorithm also solves the problem. Thus, Bellman–Ford is used primarily for graphs with negative edge weights.
So I think my answer is correct, right?
Dijkstra can really be The Single-Source Longest Path Problem and my modifications are also correct, right?
No, we cannot1 - or at the very least, no polynomial reduction/modification is known - longest path problem is NP-Hard, while dijkstra runs in polynomial time!
If we can find a modfication to dijsktra to answer longest-path problem in polynomial time, we can derive P=NP
If not, then provide a counterexample.
This is very bad task. The counter example can provide a specific modification is wrong, while there could be a different modification that is OK.
The truth is we do not know if longest-path problem is solveable in polynomial time or not, but the general assumption is - it is not.
regarding just changing the relaxation step:
A
/ \
1 2
/ \
B<--1---C
edges are (A,B),(A,C),(C,B)
dijkstra from A will first pick B, and then B is never reachable - because it is out of the set of distances.
At the very least, one will have also to change min heap into max heap, but it will have a different counter-example why it fails.
(1) probably, maybe if P=NP it is possible, but it is very unlikely.
Yes, we can. And your answer is almost correct. Except one thing.
You assume no negative weights. In this case Dijkstra's algorithm cannot find longest path.
But if you assume only negative weights, you can easily find the longest path. To prove correctness, just take absolute values of all weights and you get exactly the usual Dijkstra's algorithm with positive weights.
It works in directed acyclic graphs but not in cyclic graphs. Since the path will back track and there is no way of avoiding that in dijkstra's algo