Can I use Dijkstra algorithm for negative weighted graph? - algorithm

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.

Related

Question about single source longest path in a DAG

If I am right, the problem of single source longest path for any general graph is NP-hard.
Can we negate all the edge weights of a DAG and run Dijstra's or Bellman Ford algorithm to get single source longest path?
If yes, Dijstra's can't run on a graph with negative edge weights, then how come it gives us a result will all negative edge weights?
Yes, you are right. Your proposed solution can be used to find a longest path in a DAG.
However, notice that the longest path problem for a DAG is not NP-hard. It is NP-hard for the general case where the graph might not be a DAG. There is some interesting, relevant discussion on Wikipedia's article on the Longest path problem.
As for your second question, Dijkstra's algorithm is defined for non-negative edge weights. The implementation might give you a result anyway, but it is no longer guaranteed to be correct.

Dijkstra vs BellFord algorithm

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.

Bellman-Ford algorithm for positive circuits

I am working on a project with directed graphs where the weight of the edges all depend on a variable x.
I'm trying to find the minimum value of x such that my graph does not contain any circuit of positive weight.
My question is -and it is probably pretty stupid but I don't see how- :
How can I use a modified Bellman-Ford to check for the presence of positive circuits instead of negative circuits ?
Thanks.
How can I use a modified Bellman-Ford to check for the presence of
positive circuits instead of negative circuits ?
Change all the weights to their negative value:
w'(u,v) = -w(u,v)
And then simply run regular BF.
You can run a binary search on that value x to find the minimal value that is needed for a negative cycle, for example, in O(logx) invokations of BF.
A more efficient solution to find the minimal weight of edge (u,v) needed to make a negative cycle, is to remove that edge, find the shortest path from v to u.
Now, you can tell what should be the weight of the edge to get a cycle of weight 0, (-d(v,u)), anything more than that - you have a positive cycle, less - negative cycle.
If you want to find shortest paths and do not want to use negative edges, you can use Dijkstra Algorithm which is nearly similar to Bellman-Ford.
However, if you want to just access the edges with possibly low weights, you can look "Prim's Algorithm" or "Kruskall Algorithm", these are using for find "Minimum Spanning Tree(MST)" for graphs.
Dijkstra Algorithm
Prim's Algorithm
Kruskal's Algorithm

Can I use Dijkstra's shortest path algorithm in my graph?

I have a directed graph that has all non-negative edges except the edge(s) that leave the source (S). There are no edges from any other vertices to the source. To find the shortest distance from source (S) to a vertex (T) in the graph, can I use Dijkstra's shortest path algorithm even though the edges leaving the source is negative?
Assuming only source-adjecent edges can have negative weights and there is no path back to the source from any of the source-adjecent nodes (as mentioned in the comment), you can just add a constant C onto all edges leaving the source to make them all non-negative. Then subtract C from the final result.
On a more general note, Dijkstra can be used to solve shortest-path in any graph with negative edge weights (but no negative cycles) after applying Johnson's reweighting algorithm (which is essentially Bellman-Ford, but needs to be performed only once).
Yes, you can use Dijkstra on that type of directed graph.
If you use already finished alghoritm for Dijsktra and it cannot use negative values, it can be good practise to find the lowest negative edge and add that number to all starting edges, therefore there is no-negative number at all. You substract that number after finishing.
If you code it yourself (which is acutally pretty easy and I recommend it to you), you almost does not change anything, just start with lowest value (as usual for Dijkstra) and allow it, that lowest value can be negative. It will work in your case.
The reason you generally can't use Dijkstra's algorithm for (directed) graphs with negative links is that Dijkstra's algorithm is greedy. It assumes that once you pick a vertex with minimum distance, there is no way it can later be reached by a smaller paths.
In your particular graph, after the very first step, you traverse all possible negative edges and Dijkstra's assumption actually holds from now on. Regardless of the fact that those vertices directly connected to start now have negative values, once you identify which has the minimum distance, it can never be reached again with a smaller distance (since all edges you would traverse from this point on would have a positive distance).
If you think about the conditions that dijkstra's algorithm puts upon the edges for the algorithm to work it is only that they are never decreasing after initialisation.
Thus, it actually doesn't matter if the first step is negative as from those several points onwards the function is constantly increasing and thus the correct output will be found (provided there is no way to get back to the start square.).

How to get Single Source Shortest Path for graphs having a negative weight cycle

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.

Resources