Yen's algorithm

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]

The ith{\displaystyle i^{th}} node of the graph, where i{\displaystyle i} ranges from 1{\displaystyle 1} to N{\displaystyle N}. This means that (1){\displaystyle (1)} is the source node of the graph and (N){\displaystyle (N)} is the sink node of the graph.

dij{\displaystyle d_{ij}}

The cost of the edge between (i){\displaystyle (i)} and (j){\displaystyle (j)}, assuming that (i)≠(j){\displaystyle (i)\neq (j)} and dij≥0{\displaystyle d_{ij}\geq 0}.

Ak{\displaystyle A^{k}}

The kth{\displaystyle k^{th}} shortest path from (1){\displaystyle (1)} to (N){\displaystyle (N)}, where k{\displaystyle k} ranges from 1{\displaystyle 1} to K{\displaystyle K}. Then Ak=(1)−(2k)−(3k)−⋯−(Qkk)−(N){\displaystyle A^{k}=(1)-(2^{k})-(3^{k})-\cdots -({Q_{k}}^{k})-(N)}, where (2k){\displaystyle (2^{k})} is the 2nd node of the kth{\displaystyle k^{th}} shortest path and (3k){\displaystyle (3^{k})} is the 3rd node of the kth{\displaystyle k^{th}} shortest path, and so on.

Aki{\displaystyle {A^{k}}_{i}}

A deviation path from Ak−1{\displaystyle A^{k-1}} at node (ik){\displaystyle (i^{k})}, where i{\displaystyle i} ranges from 1{\displaystyle 1} to Qk{\displaystyle Q_{k}}. Note that the maximum value of i{\displaystyle i} is Qk{\displaystyle Q_{k}}, which is the node just before the sink in the k{\displaystyle k} shortest path. This means that the deviation path cannot deviate from the k−1{\displaystyle k-1} shortest path at the sink. The paths Ak{\displaystyle A^{k}} and Ak−1{\displaystyle A^{k-1}} follow the same path until the ith{\displaystyle i_{th}} node, then (i)k−(i+1)k{\displaystyle (i)^{k}-(i+1)^{k}} edge is different from any path in Aj{\displaystyle A^{j}}, where j{\displaystyle j} ranges from 1{\displaystyle 1} to k−1{\displaystyle k-1}.

Rki{\displaystyle {R^{k}}_{i}}

The root path of Aki{\displaystyle {A^{k}}_{i}} that follows that Ak−1{\displaystyle A^{k-1}} until the ith{\displaystyle i_{th}} node of Ak−1{\displaystyle A^{k-1}}.

Ski{\displaystyle {S^{k}}_{i}}

The spur path of Aki{\displaystyle {A^{k}}_{i}} that starts at the ith{\displaystyle i_{th}} node of Aki{\displaystyle {A^{k}}_{i}} and ends at the sink.

The algorithm can be broken down into two parts, determining the first k-shortest path, Ak{\displaystyle A^{k}}, and then determining all other k-shortest paths. It is assumed that the container A{\displaystyle A} will hold the k-shortest path, whereas the container B{\displaystyle B}, will hold the potential k-shortest paths. To determine A1{\displaystyle A^{1}}, the shortest path from the source to the sink, any efficient shortest path algorithm can be used.

To find the Ak{\displaystyle A^{k}}, where k{\displaystyle k} ranges from 2{\displaystyle 2} to K{\displaystyle K}, the algorithm assumes that all paths from A1{\displaystyle A^{1}} to Ak−1{\displaystyle A^{k-1}} have previously been found. The k{\displaystyle k} iteration can be divided into two processes, finding all the deviations Aki{\displaystyle {A^{k}}_{i}} and choosing a minimum length path to become Ak{\displaystyle A^{k}}. Note that in this iteration, i{\displaystyle i} ranges from 1{\displaystyle 1} to Qkk{\displaystyle {Q^{k}}_{k}}.

The first process can be further subdivided into three operations, choosing the Rki{\displaystyle {R^{k}}_{i}}, finding Ski{\displaystyle {S^{k}}_{i}}, and then adding Aki{\displaystyle {A^{k}}_{i}} to the container B{\displaystyle B}. The root path, Rki{\displaystyle {R^{k}}_{i}}, is chosen by finding the subpath in Ak−1{\displaystyle A^{k-1}} that follows the first i{\displaystyle i} nodes of Aj{\displaystyle A^{j}}, where j{\displaystyle j} ranges from 1{\displaystyle 1} to k−1{\displaystyle k-1}. Then, if a path is found, the cost of edge di(i+1){\displaystyle d_{i(i+1)}} of Aj{\displaystyle A^{j}} is set to infinity. Next, the spur path, Ski{\displaystyle {S^{k}}_{i}}, is found by computing the shortest path from the spur node, node i{\displaystyle i}, to the sink. The removal of previous used edges from (i){\displaystyle (i)} to (i+1){\displaystyle (i+1)} ensures that the spur path is different. Aki=Rki+Ski{\displaystyle {A^{k}}_{i}={R^{k}}_{i}+{S^{k}}_{i}}, the addition of the root path and the spur path, is added to B{\displaystyle 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{\displaystyle A^{k}} by finding the path in container B{\displaystyle B} with the lowest cost. This path is removed from container B{\displaystyle B} and inserted into container A{\displaystyle A} and the algorithm continues to the next iteration.

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.
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.
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];
B.pop();
return A;

The example uses Yen's K-Shortest Path Algorithm to compute three paths from (C){\displaystyle (C)} to (H){\displaystyle (H)}. Dijkstra's algorithm is used to calculate the best path from (C){\displaystyle (C)} to (H){\displaystyle (H)}, which is (C)−(E)−(F)−(H){\displaystyle (C)-(E)-(F)-(H)} with cost 5. This path is appended to container A{\displaystyle A} and becomes the first k-shortest path, A1{\displaystyle A^{1}}.

Node (C){\displaystyle (C)} of A1{\displaystyle A^{1}} becomes the spur node with a root path of itself, R21=(C){\displaystyle {R^{2}}_{1}=(C)}. The edge, (C)−(E){\displaystyle (C)-(E)}, is removed because it coincides with the root path and a path in container A{\displaystyle A}. Dijkstra's algorithm is used to compute the spur path S21{\displaystyle {S^{2}}_{1}}, which is (C)−(D)−(F)−(H){\displaystyle (C)-(D)-(F)-(H)}, with a cost of 8. A21=R21+S21=(C)−(D)−(F)−(H){\displaystyle {A^{2}}_{1}={R^{2}}_{1}+{S^{2}}_{1}=(C)-(D)-(F)-(H)} is added to container B{\displaystyle B} as a potential k-shortest path.

Node (E){\displaystyle (E)} of A1{\displaystyle A^{1}} becomes the spur node with R22=(C)−(E){\displaystyle {R^{2}}_{2}=(C)-(E)}. The edge, (E)−(F){\displaystyle (E)-(F)}, is removed because it coincides with the root path and a path in container A{\displaystyle A}. Dijkstra's algorithm is used to compute the spur path S22{\displaystyle {S^{2}}_{2}}, which is (E)−(G)−(H){\displaystyle (E)-(G)-(H)}, with a cost of 7. A22=R22+S22=(C)−(E)−(G)−(H){\displaystyle {A^{2}}_{2}={R^{2}}_{2}+{S^{2}}_{2}=(C)-(E)-(G)-(H)} is added to container B{\displaystyle B} as a potential k-shortest path.

Node (F){\displaystyle (F)} of A1{\displaystyle A^{1}} becomes the spur node with a root path, R23=(C)−(E)−(F){\displaystyle {R^{2}}_{3}=(C)-(E)-(F)}. The edge, (F)−(H){\displaystyle (F)-(H)}, is removed because it coincides with the root path and a path in container A{\displaystyle A}. Dijkstra's algorithm is used to compute the spur path S23{\displaystyle {S^{2}}_{3}}, which is (F)−(G)−(H){\displaystyle (F)-(G)-(H)}, with a cost of 8. A23=R23+S23=(C)−(E)−(F)−(G)−(H){\displaystyle {A^{2}}_{3}={R^{2}}_{3}+{S^{2}}_{3}=(C)-(E)-(F)-(G)-(H)} is added to container B{\displaystyle B} as a potential k-shortest path.

Of the three paths in container B, A22{\displaystyle {A^{2}}_{2}} is chosen to become A2{\displaystyle A^{2}} 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{\displaystyle A^{3}} is (C)−(D)−(F)−(H){\displaystyle (C)-(D)-(F)-(H)}.

To store the edges of the graph, the shortest path list A{\displaystyle A}, and the potential shortest path list B{\displaystyle B}, N2+KN{\displaystyle N^{2}+KN} memory addresses are required.[2] At worse case, the every node in the graph has an edge to every other node in the graph, thus N2{\displaystyle N^{2}} addresses are needed. Only KN{\displaystyle KN} addresses are need for both list A{\displaystyle A} and B{\displaystyle B} because at most only K{\displaystyle K} paths will be stored,[2] where it is possible for each path to have N{\displaystyle N} nodes.

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){\displaystyle O(N^{2})}, but using a Fibonacci heap it becomes O(M+Nlog⁡N){\displaystyle O(M+N\log N)},[3] where M{\displaystyle M} is the amount of edges in the graph. Since Yen's algorithm makes Kl{\displaystyle Kl} calls to the Dijkstra in computing the spur paths, where l{\displaystyle l} is the length of spur paths. In a condensed graph, the expected value of l{\displaystyle l} is O(log⁡N){\displaystyle O(\log N)}, while the worst case is N{\displaystyle N}.
, the time complexity becomes O(KN(M+Nlog⁡N)){\displaystyle O(KN(M+N\log N))}.
[4]

Yen's algorithm can be improved by using a heap to store B{\displaystyle 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{\displaystyle A^{k}}. Also, if container B{\displaystyle B} has K−k{\displaystyle K-k} paths of minimum length, in reference to those in container A{\displaystyle A}, then they can be extract and inserted into container A{\displaystyle A} since no shorter paths will be found.

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{\displaystyle A^{k}}. For instance, Ak{\displaystyle A^{k}} deviates from Ak−1{\displaystyle A^{k-1}} at some node (i){\displaystyle (i)}. Any spur path, Skj{\displaystyle {S^{k}}_{j}} where j=0,…,i{\displaystyle j=0,\ldots ,i}, that is calculated will be a duplicate because they have already been calculated during the k−1{\displaystyle k-1} iteration. Therefore, only spur paths for nodes that were on the spur path of Ak−1{\displaystyle A^{k-1}} must be calculated, i.e. only Skh{\displaystyle {S^{k}}_{h}} where h{\displaystyle h} ranges from (i+1)k−1{\displaystyle (i+1)^{k-1}} to (Qk)k−1{\displaystyle (Q_{k})^{k-1}}. To perform this operation for Ak{\displaystyle A^{k}}, a record is needed to identify the node where Ak−1{\displaystyle A^{k-1}} branched from Ak−2{\displaystyle A^{k-2}}.