Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dijkstra with Parallel edges and self-loop

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.

like image 202
Manuel Avatar asked May 28 '16 22:05

Manuel


People also ask

Does Dijkstra work with self-loops?

If there is an edge between two vertices, we call them neighbors. The degree of a vertex is the number of neighbors it has. Unless otherwise specified, we will not allow self-loops or multi-edges (multiple edges between the same pair of nodes).

Does Dijkstra work with parallel edges?

Like the answers mentioned it really depends on the particular implementation of dijkstra algorithm. This 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.

Is self loop A parallel edge?

An edge {vi , vj } having the same vertex as both its end vertices is called a self-loop. Two edges with the same end vertices are referred to as parallel edges. A graph that has neither self-loops nor parallel edges is called a simple graph.

Which edge is not accepted in Dijkstra algorithm?

Since Dijkstra follows a Greedy Approach, once a node is marked as visited it cannot be reconsidered even if there is another path with less cost or distance. This issue arises only if there exists a negative weight or edge in the graph.


2 Answers

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.

like image 112
lalatnayak Avatar answered Sep 18 '22 11:09

lalatnayak


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.

like image 36
biziclop Avatar answered Sep 21 '22 11:09

biziclop