How to find the shortest cycle containing two specific nodes?

1.1k Views Asked by At

I would like to find the shortest cycle for each $v$ vertex in an undirected graph with positive weights that contains $v$ and a fixed $u$ node. What is the cheepest way to do so?

Edit: The notion of cycle is very confusing. In the sense of the question, a cycle is a closed trail where edges are not repeated.

1

There are 1 best solutions below

0
On

I'm editing my answer, because it was totally wrong.

I came across Suurballe's algorithm, which solves the problem for two fixed nodes $u$ and $v$. In your question, you want to find the shortest simple cycle between a fixed $u$ and any other node $v$. Then, you can run Suubralle's algorithm for the fixed $u$ and every $v$.

The first part of the algorithm (running Dijkstra for the first time) will always give you the same result. So, you can save some time by running this part only once and then running the rest of the algorithm for each $v$ separately.