11

If I have a weighted undirected Graph with no negative weights, but can contain multiple edges between vertex and self-loops, Can I run Dijkstra algorithm without problem to find the minimum path between a source and a destination or exists a counterexample? graph

My guess is that there is not problem, but I want to be sure.

Manuel
  • 247
  • 2
  • 3
  • 14
  • 1
    Like the answers mentioned it really depends on the particular implementation of dijkstra algorithm. [This](https://www.quora.com/What-is-the-most-simple-efficient-C%2B%2B-code-for-Dijkstras-shortest-path-algorithm/answer/Michal-Fori%C5%A1ek?srid=ojqI) implementation will work fine because it adds the smallest possible edge among all possible parallel edges to the data structure you are using to keep track of the yet to be explored nodes. – thebenman Aug 09 '16 at 14:55

5 Answers5

6

If you're going to run Dijkstra's algorithm without making any changes to he graph, there's a chance that you'll not get the shortest path between source and destination.

For example, consider S and O. Now, finding the shortest path really depends on which edge is being being traversed when you want to push O to the queue. If your code picks edge with weight 1, you're fine. But if your code picks the edge with weight 8, then your algorithm is going to give you the wrong answer.

This means that the algorithm's correctness is now dependent on the order of edges entered in the adjacency list of the source node.

attaboy182
  • 2,039
  • 3
  • 22
  • 28
  • 3
    What is important though that whether it will work or not is not a property of the algorithm, it's the property of the implementation. – biziclop May 29 '16 at 16:48
3

You can trivially transform your graph to one without single-edge loops and parallel edges.

With single-edge loops you need to check whether their weight is negative or non-negative. If the weight is negative, there obviously is no shortest path, as you can keep spinning in place and reduce your path length beyond any limit. If however the weight is positive, you can throw that edge away, as no shortest path can go through that edge.

A zero-weight edge would create a similar problem than any zero-weight loop: there will be not one but an infinite number of shortest paths, going through the same loop over and over again. In these cases the sensible thing is again to remove the edge from the graph.

Out of the parallel edges you can throw away all but the one with the lowest weight. The reasoning for this is equally simple: if there was a shortest path going through an edge A that has a parallel edge B with lower weight, you could construct an even shorter path by simply replacing A with B. Therefore no shortest path can go through A.

biziclop
  • 48,926
  • 12
  • 77
  • 104
  • Ok, that sounds great. But my question is: If I run Dijkstra Algorithm on that graph without any modification, there could be a problem? – Manuel May 28 '16 at 22:56
  • 1
    @Manuel That depends fully on the implementation. For example in the step that you calculate the distance from node `N` to all its neighbours, an implementation might assume that every outgoing edge from `N` leads to a different node, so it might store the result in a map where the nodes are the keys. In that case parallel edges can easily lead you to the wrong result. – biziclop May 29 '16 at 16:46
3

It just needs a minor variation. If there are multiple edges directed from u to v and each edge has a different weight, you can either:

  1. Pick the weight with least edge for relaxation; or
  2. Run relaxation for each edge.

Both of the above will have the same complexity although the constant factors in #2 will have higher values.

In any case you'll need to make sure that you evaluate all edges between u and v before moving to the next adjacent node of u.

lalatnayak
  • 160
  • 1
  • 6
  • 21
1

I don't think it will create any kind of problem.As the dijkstra algorithm will use priority queue ,so offcourse minimum value will get update first.

0

In your example, from nodes S to O, for Dijkstra's algorithm, it is possible depending on your graphical representation. When creating the graph, for this algorithm and other shortest path algorithms, you only need to consider the lowest weighted edge so when creating your graph structure, only store the the edge with the lowest value. You can then run Dijkstra's algorithm as intended.

raiyan
  • 13
  • 5