# Shortest path in weighted(positive or negative) undirected graph

I have to find an algorithm that finds the SSSP (single-source shortest path - shortest paths from one source vertex to all other vertices) on a weighted undirected graph. If there are 2 different shortest paths, the algorithm should prefer the one with less edges on it. The time complexity of the algorithm is $O((|V|+|E|) \log |V|)$.

So, I tried:

• My first thought was DFS/BFS (the $O(|V|+|E|)$ looks tempting), but (I believe) no modification of these two algorithms works on weighed graphs.
• Then I thought about Dijkstra's algorithm - but that one doesn't work on graphs, which can have negative edges.
• My last hope was Bellman-Ford algorithm, which could work I guess, but I have no idea, how to rewrite it for undirected graphs.

Any hint is appreciated.

• Undirected graph can be converted to directed graphs ny replacing a undirected edge with 2 directed edges in different direction. – Tushar May 15 '14 at 20:01
• Just a note, Dijkstra's algorithm works for graphs that can have negative edges. It only fails if the graph has negative weight cycles. EDIT: Just realized that a negative edge means a negative cycle for undirected graphs. – Tushar May 15 '14 at 20:02
• @Tushar Nope. Dijkstra will fail for negative edges. Starting in a, with edges (a,3,b), (a,5,c), and (c,-3,b) Dijkstra will claim the distance to b equals 3 in the first step, only to discover later it is 2 via node c. No negative edges. – Hendrik Jan May 16 '14 at 23:38

A generic scheme to make an undirected graph directed is to replace each of its edges by two directed edges with same weight as the original weight of the edge. For an undirected edge $(u,v)$. Replace it by two directed edges $(u,v)$(direction u to v) and $(v,u)$(direction v to u). The weight of both of these edges are same as weight of undirected edge $(u,v)$.
And, a good way to get a shortest path with smallest number of edges is to add a small constant weight ($\epsilon$) to all edges. Note that if we have two shortest path having $x$ and $y$ edges ($x > y$) before, then adding small value to all edges changes the weight of the shortest path with $x$ and $y$ edges by amount $x\epsilon$ and $y\epsilon$. As $x\epsilon > y\epsilon$, now the shortest path with $y$ edges is having lesser cost. Thus after changing the edge weight by a very small amount you can apply your favorite SSSP algo to get the path with minimum number of edges. Note that as $\epsilon$ is very small a path which was not shortest before can't be shortest after changing the weights.
• The defect of replacing an edge $\{\,u, v\,\}$ of negative weight by two arcs $(u, v)$ and $(v, u)$ is that you produce a cycle $v, (v, u), u, (u, v), v$ of negative weight. – Smylic Jun 3 '15 at 22:55
After making the graph undirected, you can use Johnson's Algorithm, which works by using the Bellman–Ford algorithm to modify the input graph removing all negative weights, allowing Dijkstra's algorithm to be used on the resultant graph. It has a time complexity of $O(V^2 \log V + VE)$, which is better than Floyd-Warshall alone!