During a course in University concerning graph theory, we were talking about finding shortest paths thus Dijkstra's algorithm came up, at that point I should mention that the edges of the graph were weighted, with weights>0. Then the professor asked how we could find the shortest path if the edges weren't weighted, I thought the same algorithm would do, since the edges had the "same" non-negative weight. But he suggested BFS. Is this true? wouldn't Dijkstra work correct? I'm not questing BFS finding the path but since it is exhaustive I thought maybe it would be better to avoid it.
Dijsktra worked fine for me even with non-wighted graphs. Every connection just has weight 1.
Related
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.
I'm currently studying shortest path problems in graphs with non-negative edge weight.
I know that Dijkstra algorithm can give me the solution to the single-source shortest path problem ie one can find the shortest path from one node to all other nodes but I haven't found algorithm that can give me the solution to a a priori simpler problem : find the shortest path between two nodes.
Intuitively, I think that one can find examples that show that the "simpler" problem isn't simpler than the single-source shortest path problem but I'm looking for references that show this contradiction (a priori) on simple (ie with a few number of nodes) graphs.
Some (but not all) single-source shortest paths algorithms can be easily modified to return the shortest path between two nodes by stopping the algorithm early. A simple example of this is breadth-first search in unweighted graphs. For example, to find the shortest path from a node u to a node v, start a BFS from u. As soon as v is found, the path from u to v discovered that way is the shortest path. Dijkstra’s algorithm can also do this: if you run a Dijkstra’s algorithm starting at node u, you can stop the algorithm as soon as you dequeue v from the priority queue to get the shortest path there.
These approaches are usually faster than running the whole algorithm to completion. But if you’re interested specifically in finding a path from one node to another, you might want to look at the A* search algorithm. This is a modification of Dijkstra’s algorithm that’s specifically optimized for the problem of getting from one node to another. It uses a heuristic to guide the search toward the target, deprioritizing searching for other nodes, and thus is much faster than Dijkstra’s algorithm in general.
As a note, not all shortest paths algorithms can be cut off early this way. For example, when there are negative edge weights but no negative cycles, Bellman-Ford can still compute shortest paths. However, it may continually revise node distances as it runs, up to the last round of the algorithm. Cutting the search off early can give back the wrong answer.
I need to solve a longest path problem for graphs that are both directed and non-directed (unweighted in both cases).
For directed graph, it is pretty easy to find dynamic programming algorithms that are able to solve the problem in pseudopolynomial time, starting at some node, and calculating the longest path for subproblems until every problem has been looked at.
Can I do a similar thing for at non-directed graph? I cant seem to find any litterature about it?
Every directed graph algorithm works on undirected graphs. Simply treat each edge as two directed edges with the same weight.
I'm wanting to find the shortest path from source to sink in a directed graph, that has all negative weights(edges). From the algorithms that I'm aware of I don't think of any algorithm that helps solve problem like this. Dijkstras algorithm fails for graphs with negative edge! and also I don't want to traverse through all nodes.
There are no negative cycles.
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.