In
graph theory
In mathematics and computer science, graph theory is the study of ''graph (discrete mathematics), graphs'', which are mathematical structures used to model pairwise relations between objects. A graph in this context is made up of ''Vertex (graph ...
, Yen's algorithm computes single-source
''K''-shortest loopless paths for a
graph
Graph may refer to:
Mathematics
*Graph (discrete mathematics), a structure made of vertices and edges
**Graph theory, the study of such graphs and their properties
*Graph (topology), a topological space resembling a graph in the sense of discret ...
with non-negative
edge
Edge or EDGE may refer to:
Technology Computing
* Edge computing, a network load-balancing system
* Edge device, an entry point to a computer network
* Adobe Edge, a graphical development application
* Microsoft Edge, a web browser developed by ...
cost.
The algorithm was published by Jin Y. Yen in 1971 and employs any
shortest path algorithm
In graph theory, the shortest path problem is the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized.
The problem of finding the shortest path between two ...
to find the best path, then proceeds to find ''K'' − 1 deviations of the best path.
Algorithm
Terminology and notation
Description
The algorithm can be broken down into two parts: determining the first
k-shortest path,
, and then determining all other ''k''-shortest paths. It is assumed that the container
will hold the ''k''-shortest path, whereas the container
will hold the potential ''k''-shortest paths. To determine
, the shortest path from the source to the sink, any efficient
shortest path algorithm
In graph theory, the shortest path problem is the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized.
The problem of finding the shortest path between two ...
can be used.
To find the
, where
ranges from
to
, the algorithm assumes that all paths from
to
have previously been found. The
iteration can be divided into two processes: finding all the deviations
and choosing a minimum length path to become
. Note that in this iteration,
ranges from
to
.
The first process can be further subdivided into three operations: choosing the
, finding
, and then adding
to the container
. The root path,
, is chosen by finding the subpath in
that follows the first
nodes of
, where
ranges from
to
. Then, if a path is found, the cost of edge
of
is set to infinity. Next, the spur path,
, is found by computing the shortest path from the spur node, node
, to the sink. The removal of previous used edges from
to
ensures that the spur path is different.
, the addition of the root path and the spur path, is added to
. 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
by finding the path in container
with the lowest cost. This path is removed from container
and inserted into container
, and the algorithm continues to the next iteration.
Pseudocode
The algorithm assumes that the
Dijkstra algorithm
Dijkstra's algorithm ( ) is an algorithm for finding the shortest paths between nodes in a weighted graph, which may represent, for example, a road network. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three y ...
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
= 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
− 1 − 2:
''// Spur node is retrieved from the previous k-shortest path, k − 1.''
spurNode = A
-1node(i);
''// The sequence of nodes from the source to the spur node of the previous k-shortest path.''
rootPath = A
-1nodes(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
= B
''// 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
to
. Dijkstra's algorithm is used to calculate the best path from
to
, which is
with cost 5. This path is appended to container
and becomes the first ''k''-shortest path,
.
Node
of
becomes the spur node with a root path of itself,
. The edge,
, is removed because it coincides with the root path and a path in container
. Dijkstra's algorithm is used to compute the spur path
, which is
, with a cost of 8.
is added to container
as a potential ''k''-shortest path.
Node
of
becomes the spur node with
. The edge,
, is removed because it coincides with the root path and a path in container
. Dijkstra's algorithm is used to compute the spur path
, which is
, with a cost of 7.
is added to container
as a potential ''k''-shortest path.
Node
of
becomes the spur node with a root path,
. The edge,
, is removed because it coincides with the root path and a path in container
. Dijkstra's algorithm is used to compute the spur path
, which is
, with a cost of 8.
is added to container
as a potential ''k''-shortest path.
Of the three paths in container
,
is chosen to become
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
is
.
Features
Space complexity
To store the edges of the graph, the shortest path list
, and the potential shortest path list
,
memory addresses are required.
[ At worse case, the every node in the graph has an edge to every other node in the graph, thus addresses are needed. Only addresses are need for both list and because at most only paths will be stored,][ where it is possible for each path to have 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 , but using a Fibonacci heap it becomes , where is the number of edges in the graph. Since Yen's algorithm makes calls to the Dijkstra in computing the spur paths, where is the length of spur paths. In a condensed graph, the expected value of is , while the worst case is .
The time complexity becomes .
Improvements
Yen's algorithm can be improved by using a heap to store , 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. 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 . Also, if container has paths of minimum length, in reference to those in container , then they can be extract and inserted into container since no shorter paths will be found.
Lawler's modification
Eugene Lawler
Eugene Leighton (Gene) Lawler (1933 – September 2, 1994) was an American computer scientist and a professor of computer science at the University of California, Berkeley... Reprinted in .
Academic life
Lawler came to Harvard as a graduate stu ...
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. These duplicates paths result from calculating spur paths of nodes in the root of . For instance, deviates from at some node . Any spur path, where , that is calculated will be a duplicate because they have already been calculated during the iteration. Therefore, only spur paths for nodes that were on the spur path of must be calculated, i.e. only where ranges from to . To perform this operation for , a record is needed to identify the node where branched from .
See also
* Yen's improvement to the Bellman–Ford algorithm
The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex (graph theory), vertex to all of the other vertices in a weighted digraph.
It is slower than Dijkstra's algorithm for the same problem, but more ...
References
External links
Open Source C++ Implementation
Open Source C++ Implementation using Boost Graph Library
{{Graph traversal algorithms
Graph algorithms
Polynomial-time problems
Articles with example pseudocode