Yen's algorithm explained

In graph theory, Yen's algorithm computes single-source K-shortest loopless paths for a graph with non-negative edge cost.[1] The algorithm was published by Jin Y. Yen in 1971 and employs any shortest path algorithm to find the best path, then proceeds to find K - 1 deviations of the best path.[2]

Algorithm

Terminology and notation

Notation Description

N

The size of the graph, i.e., the number of nodes in the network.

(i)

The

i

-th node of the graph, where

i

ranges from

1

to

N

. This means that

(1)

is the source node of the graph, and

(N)

is the sink node of the graph.

dij

The cost of the edge between

(i)

and

(j)

, assuming that

(i)\ne(j)

and

dij\ge0

.

Ak

The

k

-th shortest path from

(1)

to

(N)

, where

k

ranges from

1

to

K

. Then

Ak=(1)-(2k)-(3k)--

k)
({Q
k}

-(N)

, where

(2k)

is the 2nd node of the

k

-th shortest path,

(3k)

is the 3rd node of the

k

-th shortest path, and so on.
k}
{A
i
A deviation path from

Ak-1

at node

(i)

, where

i

ranges from

1

to

Qk

. Note that the maximum value of

i

is

Qk

, which is the node just before the sink in the

k

shortest path. This means that the deviation path cannot deviate from the

k-1

shortest path at the sink. The paths

Ak

and

Ak-1

follow the same path until the

i

-th node, then

(i)k-(i+1)k

edge is different from any path in

Aj

, where

j

ranges from

1

to

k-1

.
k}
{R
i
The root path of
k}
{A
i
that follows that

Ak-1

until the

i

-th node of

Ak-1

.
k}
{S
i
The spur path of
k}
{A
i
that starts at the

i

-th node of
k}
{A
i
and ends at the sink.

Description

The algorithm can be broken down into two parts: determining the first k-shortest path,

A1

, and then determining all other k-shortest paths. It is assumed that the container

A

will hold the k-shortest path, whereas the container

B

will hold the potential k-shortest paths. To determine

A1

, the shortest path from the source to the sink, any efficient shortest path algorithm can be used.

To find the

Ak

, where

k

ranges from

2

to

K

, the algorithm assumes that all paths from

A1

to

Ak-1

have previously been found. The

k

iteration can be divided into two processes: finding all the deviations
k}
{A
i
and choosing a minimum length path to become

Ak

. Note that in this iteration,

i

ranges from

1

to
k}
{Q
k
.

The first process can be further subdivided into three operations: choosing the

k}
{R
i
, finding
k}
{S
i
, and then adding
k}
{A
i
to the container

B

. The root path,
k}
{R
i
, is chosen by finding the subpath in

Ak-1

that follows the first

i

nodes of

Aj

, where

j

ranges from

1

to

k-1

. Then, if a path is found, the cost of edge

di(i+1)

of

Aj

is set to infinity. Next, the spur path,
k}
{S
i
, is found by computing the shortest path from the spur node, node

i

, to the sink. The removal of previous used edges from

(i)

to

(i+1)

ensures that the spur path is different.
k}
{A
i

=

k}
{R
i

+

k}
{S
i
, the addition of the root path and the spur path, is added to

B

. Next, the edges that were removed, i.e. had their cost set to infinity, are restored to their initial values.

The second process determines a suitable path for

Ak

by finding the path in container

B

with the lowest cost. This path is removed from container

B

and inserted into container

A

, and the algorithm continues to the next iteration.

Pseudocode

The algorithm assumes that the Dijkstra algorithm is used to find the shortest path between two nodes, but any shortest path algorithm can be used in its place.

function YenKSP(Graph, source, sink, K): // Determine the shortest path from the source to the sink. A[0] = Dijkstra(Graph, source, sink); // Initialize the set to store the potential kth shortest path. B = []; for k from 1 to K: // The spur node ranges from the first node to the next to last node in the previous k-shortest path. for i from 0 to size(A[k − 1]) - 2: // Spur node is retrieved from the previous k-shortest path, k - 1. spurNode = A[k-1].node(i); // The sequence of nodes from the source to the spur node of the previous k-shortest path. rootPath = A[k-1].nodes(0, i); for each path p in A: if rootPath

p.nodes(0, i): // Remove the links that are part of the previous shortest paths which share the same root path. remove p.edge(i,i + 1) from Graph; for each node rootPathNode in rootPath except spurNode: remove rootPathNode from Graph; // Calculate the spur path from the spur node to the sink. // Consider also checking if any spurPath found spurPath = Dijkstra(Graph, spurNode, sink); // Entire path is made up of the root path and spur path. totalPath = rootPath + spurPath; // Add the potential k-shortest path to the heap. if (totalPath not in B): B.append(totalPath); // Add back the edges and nodes that were removed from the graph. restore edges to Graph; restore nodes in rootPath to Graph; if B is empty: // This handles the case of there being no spur paths, or no spur paths left. // This could happen if the spur paths have already been exhausted (added to A), // or there are no spur paths at all - such as when both the source and sink vertices // lie along a "dead end". break; // Sort the potential k-shortest paths by cost. B.sort; // Add the lowest cost path becomes the k-shortest path. A[k] = B[0]; // In fact we should rather use shift since we are removing the first element B.pop; return A;

Example

The example uses Yen's K-Shortest Path Algorithm to compute three paths from

(C)

to

(H)

. Dijkstra's algorithm is used to calculate the best path from

(C)

to

(H)

, which is

(C)-(E)-(F)-(H)

with cost 5. This path is appended to container

A

and becomes the first k-shortest path,

A1

.

Node

(C)

of

A1

becomes the spur node with a root path of itself,
2}
{R
1

=(C)

. The edge,

(C)-(E)

, is removed because it coincides with the root path and a path in container

A

. Dijkstra's algorithm is used to compute the spur path
2}
{S
1
, which is

(C)-(D)-(F)-(H)

, with a cost of 8.
2}
{A
1

=

2}
{R
1

+

2}
{S
1

=(C)-(D)-(F)-(H)

is added to container

B

as a potential k-shortest path.

Node

(E)

of

A1

becomes the spur node with
2}
{R
2

=(C)-(E)

. The edge,

(E)-(F)

, is removed because it coincides with the root path and a path in container

A

. Dijkstra's algorithm is used to compute the spur path
2}
{S
2
, which is

(E)-(G)-(H)

, with a cost of 7.
2}
{A
2

=

2}
{R
2

+

2}
{S
2

=(C)-(E)-(G)-(H)

is added to container

B

as a potential k-shortest path.

Node

(F)

of

A1

becomes the spur node with a root path,
2}
{R
3

=(C)-(E)-(F)

. The edge,

(F)-(H)

, is removed because it coincides with the root path and a path in container

A

. Dijkstra's algorithm is used to compute the spur path
2}
{S
3
, which is

(F)-(G)-(H)

, with a cost of 8.
2}
{A
3

=

2}
{R
3

+

2}
{S
3

=(C)-(E)-(F)-(G)-(H)

is added to container

B

as a potential k-shortest path.

Of the three paths in container

B

,
2}
{A
2
is chosen to become

A2

because it has the lowest cost of 7. This process is continued to the 3rd k-shortest path. However, within this 3rd iteration, note that some spur paths do not exist. And the path that is chosen to become

A3

is

(C)-(D)-(F)-(H)

.

Features

Space complexity

To store the edges of the graph, the shortest path list

A

, and the potential shortest path list

B

,

N2+KN

memory addresses are required. At worse case, the every node in the graph has an edge to every other node in the graph, thus

N2

addresses are needed. Only

KN

addresses are need for both list

A

and

B

because at most only

K

paths will be stored, where it is possible for each path to have

N

nodes.

Time complexity

The time complexity of Yen's algorithm is dependent on the shortest path algorithm used in the computation of the spur paths, so the Dijkstra algorithm is assumed. Dijkstra's algorithm has a worse case time complexity of

O(N2)

, but using a Fibonacci heap it becomes

O(M+NlogN)

,[3] where

M

is the number of edges in the graph. Since Yen's algorithm makes

Kl

calls to the Dijkstra in computing the spur paths, where

l

is the length of spur paths. In a condensed graph, the expected value of

l

is

O(logN)

, while the worst case is

N

.The time complexity becomes

O(KN(M+NlogN))

.[4]

Improvements

Yen's algorithm can be improved by using a heap to store

B

, the set of potential k-shortest paths. Using a heap instead of a list will improve the performance of the algorithm, but not the complexity.[5] One method to slightly decrease complexity is to skip the nodes where there are non-existent spur paths. This case is produced when all the spur paths from a spur node have been used in the previous

Ak

. Also, if container

B

has

K-k

paths of minimum length, in reference to those in container

A

, then they can be extract and inserted into container

A

since no shorter paths will be found.

Lawler's modification

Eugene Lawler proposed a modification to Yen's algorithm in which duplicates path are not calculated as opposed to the original algorithm where they are calculated and then discarded when they are found to be duplicates.[6] These duplicates paths result from calculating spur paths of nodes in the root of

Ak

. For instance,

Ak

deviates from

Ak-1

at some node

(i)

. Any spur path,
k}
{S
j
where

j=0,\ldots,i

, that is calculated will be a duplicate because they have already been calculated during the

k-1

iteration. Therefore, only spur paths for nodes that were on the spur path of

Ak-1

must be calculated, i.e. only
k}
{S
h
where

h

ranges from

(i+1)k-1

to
k-1
(Q
k)
. To perform this operation for

Ak

, a record is needed to identify the node where

Ak-1

branched from

Ak-2

.

See also

External links

Notes and References

  1. Yen. Jin Y.. 253822. An algorithm for finding shortest routes from all source nodes to a given destination in general networks. Quarterly of Applied Mathematics. 1970. 27. 4. 526–530. 10.1090/qam/253822. free.
  2. Yen. Jin Y.. Finding the k Shortest Loopless Paths in a Network. Management Science. Jul 1971. 17. 11. 712–716. 2629312. 10.1287/mnsc.17.11.712.
  3. Michael Lawrence. Fredman. Michael Fredman. Robert E.. Tarjan. Robert Tarjan. Fibonacci heaps and their uses in improved network optimization algorithms. 25th Annual Symposium on Foundations of Computer Science. 1984. IEEE. 338 - 346. 10.1109/SFCS.1984.715934.
  4. Book: Bouillet, Eric . Path routing in mesh optical networks. 2007. John Wiley & Sons. Chichester, England. 9780470032985.
  5. Book: Brander. Andrew William. Mark C. . Sinclair. A comparative study of k-shortest path algorithms.. Department of Electronic Systems Engineering, University of Essex, 1995.
  6. Lawler. EL. A procedure for computing the k best solutions to discrete optimization problems and its application to the shortest path problem.. Management Science. 1972. 18. 7. 401–405. 10.1287/mnsc.18.7.401.