K-Shortest Paths Problem
K-Shortest Paths Problem
LIAM RODITTY
Abstract. We present the rst approximation algorithm for nding the k shortest simple paths
connecting a pair of vertices in a weighted directed graph that breaks the barrier of mn. It is
deterministic and has a running time of O(k(m
n+n
3/2
log n)), where m is the number of edges in
the graph and n is the number of vertices. Let s, t V ; the length of the ith simple path from s to t
computed by our algorithm is at most
3
2
times the length of the ith shortest simple path from s to
t. The best algorithms for computing the exact k shortest simple paths connecting a pair of vertices
in a weighted directed graph are due to Yen [Management Sci., 17 (1970/1971), pp. 712716] and
Lawler [Management Sci., 18 (1971/1972), pp. 401405]. The running time of their algorithms, using
modern data structures, is O(k(mn + n
2
log n)). Both algorithms are from the early 70s. Although
this problem and other variants of the k shortest path problem has drawn a lot of attention during
the last three and a half decades, the O(k(mn + n
2
log n)) bound is still unbeaten.
Key words. shortest path, k shortest simple path, graph algorithms, second shortest path
AMS subject classications. 68W40, 68W20, 68W05, 68Q25
DOI. 10.1137/080730950
1. Introduction. Computing the shortest path connecting a pair of vertices in
a graph is one of the most fundamental algorithmic problems in graph theory. A
natural generalization of this problem is to compute a set of k shortest paths that
connect a given pair of vertices for some k > 1. More formally, given a graph G(V, E)
with nonnegative edge weights, a positive integer k > 1, and two vertices s, t V ,
the algorithm has to compute the k shortest paths from s to t, where any two paths
have to dier by at least one edge. Yen [21] and Lawler [14] showed how to compute
the k shortest simple paths in weighted directed graphs. The running time of their
algorithms, when modern data structures are used, is O(k(mn + n
2
log n)). For the
restricted case of undirected graphs, Katoh, Ibaraki, and Mine [13] presented an
algorithm with a running time of O(k(m+nlog n)) when modern data structures are
used. Eppstein [8] presented an algorithm, for weighted directed graphs, that nds
the k shortest paths when the paths are not required to be simple, i.e., they may
contain loops. The running time of his algorithm is O(m +nlog n +k). Roditty and
Zwick [19] present a randomized algorithm with a running time of O(km
nlog
2
n)
for unweighted directed graphs. The algorithms of Yen and Lawler go back to the
early 70s. Throughout the last three and a half decades there were many improved
implementations of their algorithms [3, 9, 16, 17]; however, the worst case bound of
O(k(mn + n
2
log n)) remains unbeaten.
The most classical and well-known shortest path problem is computing the all-
pairs shortest paths (APSPs) of a graph. For directed graphs with real edge weights,
the fastest known algorithm is due to Pettie [18]. Its running time is O(mn +
n
2
log log n). Many eorts have been made throughout the years to beat the O(mn)
Received by the editors July 23, 2008; accepted for publication (in revised form) February 16,
2010; published electronically April 9, 2010. A preliminary version of this article appeared in Pro-
ceedings of the 18th Symposium on Discrete Algorithms (SODA 2007), pp. 920928.
http://www.siam.org/journals/sicomp/39-6/73095.html
n+n
3/2
log n))
time. The length of the ith simple path from s to t computed by our algorithm is at
most
3
2
times more than the length of the ith shortest simple path from s to t. We
say that
3
2
is the stretch of the path. Our algorithm is built upon an approximation
algorithm for nding the second simple shortest path. This algorithm is based, among
other things, on a novel technique for computing, in parallel, many truncated shortest
paths trees at the cost of only a single run of Dijkstras algorithm.
A closely related problem to the k shortest simple paths problem is the replace-
ment path problem. In the replacement paths problem we are required to nd, for
every edge e on the shortest path from s to t, a shortest path from s to t that avoids e;
see Demetrescu et al. [5] for more details. In our context, it is quite easy to see that
the second shortest path is one of the replacement paths. Many algorithms for com-
puting k shortest paths solve O(k) instances of the replacement path problem. In
undirected graphs, Malik, Mittal, and Gupta [15] and Hershberger and Suri [10, 11]
presented an algorithm with running time O(m + nlog n). The trivial algorithm to
solve the problem excludes each edge on the path in its turn and computes a short-
est path from s to t. In weighted directed graphs nothing better than the trivial
algorithm is known. Hershberger, Suri, and Bhosle [12] presented a lower bound of
(
nm) for the replacement path problem in weighted directed graphs. Their lower
bound also implies similar lower bound for k shortest simple paths problem. Their
lower bounds are for algorithms that are allowed only to make comparisons between
lengths of paths.
The backbone of our algorithm, the approximation algorithm for the second short-
est path, does not solve a replacement path problem. Our algorithm for approximating
the k shortest simple path has the following useful property. The approximation of
the ith shortest path is obtained by nding another simple path in the graph of stretch
3
2
with respect to the ith shortest path. As a result, if the length of the (i + 1)th
shortest path is more than
3
2
the length of the ith shortest path, then when computing
k shortest simple paths using our approximated algorithm, with k i, the exact ith
shortest path is included in the output.
There are numerous applications in which shortest paths computation is needed.
Many of these applications need to compute a set of shortest paths, not only the short-
est one. Eppstein [8] lists a number of applications for k shortest paths computation.
The rst application mentioned there, which also was the motivation for Lawler [14],
is the case when a short path is needed but there are other constraints in addition to
short length. These other constraints, however, are not well dened or are hard to
Copyright by SIAM. Unauthorized reproduction of this article is prohibited.
k-SHORTEST SIMPLE PATHS 2365
optimize. A natural solution in such a case is to compute a set of shortest paths and
to choose one of them by considering the other constraints. Our algorithm for nding
approximated k shortest simple paths can be used in such a situation, especially if
we consider its property that if the lengths of the paths are well spread out, then the
exact paths are found by it. For a comprehensive list of applications see [8].
The rest of this paper is organized as follows. We start by presenting our tech-
niques in section 2 with a 2-approximation algorithm of the second shortest path. In
section 3 we show how to improve the stretch factor to
3
2
. In section 4 we present
our approximation algorithm for computing the k shortest simple paths. We end in
section 5 with some concluding remarks and open problems.
2. A 2-approximation of the second shortest path. In this section we
present an algorithm that computes a 2-approximation of the second shortest path
from s to t. Although our main result uses our stretch
3
2
algorithm, we start with
this somewhat simpler algorithm to explain ideas that will be used later in a more
complex way.
Let G(V, E) be a directed graph and let s and t be two vertices in the graph.
Let P(s, t) = s = u
1
, u
2
, . . . , u
q
= t be a shortest path from s to t. Let P
E
(s, t) =
(u
1
, u
2
), . . . , (u
q1
, u
q
) be the set of edges of P(s, t). For u
i
, u
j
P(s, t), with i < j,
let P(u
i
, u
j
) denote the subpath of P(s, t) from u
i
to u
j
. The objective of a second
shortest path algorithm is to nd a simple path from s to t of minimal length among
all paths in the graph from s to t other than the shortest path from s to t. In what
follows, the ith simple shortest path from s to t is denoted by P
i
(s, t). P(s, t) always
denotes the shortest path from s to t. Its number of vertices is denoted by [P(s, t)[.
The length of P
i
(s, t) is denoted by
i
and its approximated distance is denoted by
i
.
Next, we dene a detour.
Definition 2.1 (a detour). Let P(s, t) be a simple path from s to t. A simple
path from u to v, D(u, v), is a detour of P(s, t) if D(u, v) P(s, t) = u, v and u
precedes v on P(s, t).
It is easy to see that a second shortest path from s to t is a path composed of
three parts: an initial part on P(s, t) from s to some vertex u
i
, a detour from u
i
to
some vertex u
j
, where j > i, and a nal part on P(s, t) from u
j
to t. We denote such
a path by P(s, u
i
) D(u
i
, u
j
) P(u
j
, t), where stands for concatenating two paths.
In [19] the replacement paths in directed unweighted graphs were found by di-
viding the possible detours into two types, long and short detours, according to the
number of edges in each detours. In unweighted graphs a long detour has many edges
and a short detour has only a few edges. In weighted graphs there is no relation
between the number of edges a detour has to its actual length, and other ideas should
be used.
We now give a high-level view of the main ideas used in our new algorithm. Our
goal is to get an algorithm with a running time of
O(m
n).
1
Thus, as long as the
shortest path [P(s, t)[ has fewer than
n edges on it, a trivial algorithmwhich
excludes every edge from the shortest path in its turn, computes a shortest path
without it, and then picks the best path among all the paths computedwill nd
the exact second shortest path in
O(m
n
E
i
(u
j
, u
j+1
) [ (i 1)
n + 1 j i
n
W min(W, Dijkstra(G(V, E ` E
i
), s, t))
return W
algorithm SetParameters(G(V, E))
for i 1 to [P(s, t)[
h(u
i
)
W
2
d[s, u
i
] d[u
i+2
n
, t]
if i 2
n then p(u
i
) 0
else p(u
i
) h(u
i2
n
) + p(u
i2
n
)
S
i mod 2
n
S
i mod 2
n
u
i
d[src(u), u] = d[u]
for every (u, v) E ` P
E
(s, t) do
if v H then Relax((u, v), src(u))
return
d
algorithm Relax((u, v), s)
x
d[s, u] + weight(u, v)
if (x < h(s)) (x + p(s) d[v] + p(src(v))) then
d[v] x
src(v) s
Decrease-Key(H, v, d[v] + p(s))
Fig. 1. The top two algorithms show the procedure for computing the upper bound and for setting
the parameters. The bottom two show the parallel implementation of Dijkstra and its corresponding
relax procedure.
single run. In the parallel run of Dijkstras algorithm we compute truncated shortest
paths trees for a set of vertices of the path P(s, t). A truncated shortest paths tree is
a shortest paths tree that is allowed to grow up to a certain height. We repeat this
parallel run until we have truncated shortest paths trees for every vertex of P(s, t).
The information gathered in these trees is used either to improve the upper bound on
2
by nding a better detour or to prove that the upper bound gives an approximation
of 2.
The upper bound is found using a method which is also used, in a similar context,
by Demetrescu et al. [5]. The algorithm for computing the upper bound is given in
Figure 1. To simplify the presentation and to avoid rounding problems, we assume
that the number of vertices [P(s, t)[ is a multiple of 2
n, is at least W.
Proof. Since j i 2
n + 1
and p
n.
After computing the upper bound we use the procedure SetParameters to set for
every u
i
P(s, t) two parameters. The rst parameter, h(u
i
), is the maximal height
of the shortest paths tree that ParallelDijkstra is allowed to grow from u
i
. The second
parameter, p(u
i
), which we refer to as the priority of u
i
, is a value that will be part of
the key of every vertex that is reached during the run of ParallelDijkstra from u
i
. (See
Figure 1.) Note that h(u
i
) is always positive since W > 2d[s, t]. We then proceed
by dividing the vertices of the path into sets. We will have a total number of 2
n
sets, each containing at most
|P(s,t)|
2
n
vertices. (Recall that for simplicity we assume
that [P(s, t)[ is a multiple of 2
n] let S
j
= u
j+2
ni
[ 0 i <
[P(s, t)[/(2
n) be the jth set. It is crucial for the algorithm that any two vertices of
the path which are in the same set are separated by at least 2
n
u
j u
k u
k
y
x
1
x
2
Fig. 2. The possible paths and their respective sources.
Any other edge is also relaxed once when its tail is extracted from H. This stems
from the fact that once a vertex is extracted from H, it is never reinserted. Thus, the
running time is O(m + nlog n).
We turn our attention to proving an important property of the ParallelDijkstra
algorithm. This property is used later to establish the correctness proof of the algo-
rithm.
Lemma 2.4. Let P(s, t) be the shortest path from s to t, and let W be the upper
bound. Let P(s, u
i
) D(u
i
, u
j
) P(u
j
, t) be a second simple shortest path of length
less than
W
2
. The truncated tree computed for u
i
by the ParallelDijkstra algorithm
contains the path D(u
i
, u
j
).
Proof. Corollary 2.3 implies that such a short path must satisfy that j i <
2
n, because otherwise W would not be the upper bound. Let S be the set that
contains u
i
and let h be the length of D(u
i
, u
j
). We know that h <
W
2
d[s, u
i
]
d[u
j
, t]. The height of a tree computed for u
i
by the ParallelDijkstra algorithm is at
most h(u
i
), where h(u
i
) =
W
2
d[s, u
i
] d[u
i+2
n
, t]. It is easy to see that h < h(u
i
)
since j < i +2
n and d[u
i+2
n
, t] d[u
j
, t]. Thus, every vertex of the path D(u
i
, u
j
)
can be part of the truncated tree of u
i
from the perspective of the height threshold.
We show that the path D(u
i
, u
j
) is indeed contained in the truncated tree of u
i
.
Assume it is not, and let y be the rst vertex of D(u
i
, u
j
) such that src(y) = u
k
for some u
k
S ` u
i
, when the execution of the ParallelDijkstra algorithm halts
(see Figure 2). Thus, y is in the truncated tree of u
k
. Let D(u
k
, y) be the path from
u
k
to y in the truncated tree of u
k
. We divide the proof into two cases. First we show
that k cannot be strictly smaller than i, and then we show that k cannot be strictly
larger than i.
Assume k < i. We examine the path created by the following concatenation of
paths: P(s, u
k
) D(u
k
, y) D(y, u
j
) P(u
j
, t). From the above discussion it follows
that the path D(u
k
, y) satises the height threshold of u
k
. The path D(y, u
j
) is a
portion of the path D(u
i
, u
j
) which satises the height threshold of u
i
. Adding this
together, we get that there is a simple path from s to t of length strictly less than
d[s, u
k
] +h(u
k
) +h(u
i
) +d[u
j
, t]. Replacing h(u
i
) and h(u
k
) with their values, we get
d[s, u
k
] +
W
2
d[s, u
k
] d[u
k+2
n
, t] +
W
2
d[s, u
i
] d[u
i+2
n
, t] + d[u
j
, t]
= W d[s, u
i
] d[u
i+2
n
, t] d[u
k+2
n
, t] + d[u
j
, t].
We bound the value of this expression with W. Recall that u
k
, u
i
S, and thus
k + 2
n i. Also note that j > i, because otherwise the path is not simple. Hence,
k + 2
n
, t] d[u
j
, t]. We get that
W d[s, u
i
] d[u
i+2
n
, t] d[u
k+2
n
, t] + d[u
j
, t] W.
We conclude that the length of the path P(s, u
k
)D(u
k
, y)D(y, u
j
)P(u
j
, t) is strictly
less than W. This leads to a contradiction from the following: Between u
k
and u
j
Copyright by SIAM. Unauthorized reproduction of this article is prohibited.
k-SHORTEST SIMPLE PATHS 2369
algorithm 2-SecondPath(G(V, E), P(s, t))
W UpperBound(G(V, E))
2
W
if
2
2
1
then return
2
SetParameters(G(V, E))
for j 1 to 2
d ParallelDijkstra(G(V, E), S
j
)
for every u
p
S
j
for i p + 1 to p + 2
n 1 do
if
2
> d[s, u
p
] +
d[u
p
, u
i
] + d[u
i
, t] then
2
d[s, u
p
] +
d[u
p
, u
i
] + d[u
i
, t]
return
2
Fig. 3. A 2-approximation of the second shortest path.
there are at least 2
n then
p(u
i
) 0
else
p(u
i
) h(u
i2
n
) + p(u
i2
n
)
S
i mod 2
n
S
i mod 2
n
u
i
algorithm
3
2
SecondPath(G(V, E), P(s, t))
W UpperBound(G(V, E))
2
W
if
2
3
2
1
then return
2
SetParameters(G(V, E), 1)
for j 1 to 2
d ParallelDijkstra(G(V, E), S
j
)
SetParameters(G(V, E), 0)
for j 1 to 2
d ParallelDijkstra(
G(V, E), S
j
)
return BestPath(G(V, E))
Fig. 4. A 1.5-approximation of the second shortest path.
3. A
3
2
-approximation of the second shortest path. In this section we
show how to improve the stretch factor to
3
2
. As before we compute an upper bound
using UpperBound. By Lemma 2.2 we know that the length of any path of the form
P(s, u
i
) D(u
i
, u
j
) P(u
j
, t), where j i 2
d[v, u
j
] <
2W
3
d[s, u
i
]
d[u
i
, v] d[u
j
, t].
Notice that v does not satisfy the height threshold of u
i
, since u is the farthest
vertex from u
i
that satises the height threshold of u
i
. Hence, it follows that
d[u
i
, v]
W
3
d[s, u
i
]. This implies that
d[v, u
j
] <
W
3
d[u
j
, t] as desired.
Assume now that d[s, u
i
]
W
3
; i.e., the truncated tree of u
i
contains only itself.
Let (u
i
, v) be the rst edge on the path D(u
i
, u
j
). The length of the path D(u
i
, u
j
)
P(u
j
, t) is less than
W
3
, and hence
d[v, u
j
] <
W
3
d[u
j
, t]. The case that d[u
j
, t]
W
3
is analogous.
In the following, we call an edge that satises the conditions of Lemma 3.1 a
border edge. In the next lemma we show not only that the endpoints of a border edge
satisfy the height thresholds, but that they are also part of the suitable truncated
trees.
Lemma 3.2. Let P(s, u
i
)D(u
i
, u
j
)P(u
j
, t) be a simple path with length less than
2W
3
. If (u, v) E is a border edge of D(u
i
, u
j
), then u T
out
(u
i
) and v T
in
(u
j
).
Proof. From Lemma 3.1 it follows that the vertex u satises the height threshold
of u
i
. Thus, every vertex of the path D(u
i
, u) can be part of the truncated tree
of u
i
from the perspective of the height threshold. Let u
i
S. We claim that
the path D(u
i
, u) is indeed contained in the truncated tree computed for u
i
by the
ParallelDijkstra algorithm when it is executed with S.
Assume that the claim does not hold, and let y be the rst vertex of D(u
i
, u) such
that src(y) = u
k
, for some u
k
S ` u
i
, when the execution of the ParallelDijkstra
algorithm halts. Let D(u
k
, y) be the path discovered by the algorithm. As before, we
divide the proof into two cases. First we show that k cannot be strictly smaller than
i, and then we show that k cannot be strictly larger than i.
Assume k < i. We examine the path created by the following concatenation of
paths: P(s, u
k
) D(u
k
, y) D(y, u
j
) P(u
j
, t). The path D(u
k
, y) satises the height
threshold of u
k
. The path D(y, u
j
) is a portion of the path D(u
i
, u
j
), and thus we can
bound the length of D(y, u
j
) with
2W
3
d[s, u
i
] d[u
j
, t]. Adding this all together,
Copyright by SIAM. Unauthorized reproduction of this article is prohibited.
2372 LIAM RODITTY
we get that there is a simple path from s to t of length strictly less than
d[s, u
k
] + h(u
k
) +
2W
3
d[s, u
i
] d[u
j
, t] + d[u
j
, t]
= d[s, u
k
] +
W
3
d[s, u
k
] +
2W
3
d[s, u
i
] = W d[s, u
i
].
This leads to a contradiction because of the following: j > i > k and both u
k
and u
i
are in the same set S, which implies that i k 2
n. Thus, between u
k
and
u
j
there are at least 2
n) time. Let
(u) be the smallest index such that u T
out
(u
(u)
).
The main diculty is that a vertex may participate in (
n) truncated trees.
However, as we show in the next lemma, in our search for a better path, it is sucient
to consider only 2
n trees.
Lemma 3.3. Let u T
out
(u
i
) for some i (u) + 2
n vertices, and by
Lemma 2.2 it is impossible that has length smaller than W.
The algorithm for searching for a better path is given in Figure 5. For every u V
two heaps, H
in
(u) and H
out
(u), are maintained. By Lemma 3.3 only vertices from
P(u
(u)
, u
(u)+2
n1
) should be considered by u (since any path with a detour that
starts at u
(u)+2
n
is of length at least
2W
3
). When the heaps are initialized, H
in
(u)
contains the vertex u
(u)
and H
out
(u) contains the vertices u
(u)+1
, . . . , u
(u)+2
n1
.
The key of u
i
in H
in
(u) is d[s, u
i
] +
d[u
i
, u] and in H
out
(u) is
d[u, u
i
] + d[u
i
, t]. The
initialization procedure is given also in Figure 5. We use Insert and Delete to denote
the standard heap insert and delete operations.
The algorithm processes every edge (u, v) E ` P
E
(s, t) in the following way. A
path is built by adding the minimum from H
in
(u) and from H
out
(v), to the weight
of (u, v). If this path improves the current upper bound, then the upper bound is
updated with the length of the new path. The algorithm proceeds by deleting u
(u)+1
from H
out
(v) and inserting it to H
in
(u). This procedure is performed until H
out
(v)
becomes empty. The endpoints u and v are then reinitialized and a new edge is picked.
The total running time of this algorithm is
O(m
n).
The next theorem summarizes the main result of this section.
Copyright by SIAM. Unauthorized reproduction of this article is prohibited.
k-SHORTEST SIMPLE PATHS 2373
algorithm BestPath(G(V, E))
for every u V do Init(u)
for every (u, v) E ` P
E
(s, t)
for i (u) + 1 to (u) + 2
n 1
if
2
> min(H
in
(u)) + w(u, v) + min(H
out
(v))
2
min(H
in
(u)) + w(u, v) + min(H
out
(v))
Delete(H
out
(v), u
i
)
Insert(H
in
(u), u
i
, d[s, u
i
] +
d[u
i
, u])
Init(u), Init(v)
return
2
algorithm Init(u)
H
in
(u) , H
out
(u)
Insert(H
in
(u), u
(u)
, d[s, u
(u)
] +
d[u
(u)
, u])
for i (u) + 1 to (u) + 2
n 1
Insert(H
out
(u), u
i
,
d[u, u
i
] + d[u
i
, t])
Border Edge
u
i
u
i+2
n
u
j s t
u v
Fig. 5. The procedure for nding a better path and an example of such a path.
Theorem 3.4. The algorithm given in Figure 4 nds a path with stretch at most
3
2
with respect to the length of the second simple shortest path from s to t. The running
time of the algorithm is
O(m
n).
Proof. Let P(s, u
i
) D(u
i
, u
j
) P(u
j
, t) be the exact simple second shortest path
and let
2
be its length. If
2
= W, then the algorithm computes the exact length.
If
2
<
2W
3
, then by Corollary 2.3 it must be that j i < 2
n. Let (u, v) be a
border edge of D(u
i
, u
j
). Lemma 3.2 implies that u T
out
(u
i
) and v T
in
(u
j
).
From Lemma 3.3 it follows that i must be smaller than (u) + 2
n). An actual path can also be returned by the algorithm. As every path is
dened by two vertices, u
i
and u
j
, and a border edge, (u, v), it is possible to retrieve
the path from u
i
to u from the truncated tree of u
i
and the path from v to u
j
from
the reversed truncated tree of u
j
.
4. Finding the k shortest simple paths. In this section we show how to
compute an approximation of the k shortest simple paths. Our algorithm guarantees
a stretch of
3
2
for the ith path computed by it, with respect to the ith shortest path.
In [19] we showed that computing the k shortest simple paths can be done by O(k)
computations of second shortest paths. However, it is not straightforward to apply
Copyright by SIAM. Unauthorized reproduction of this article is prohibited.
2374 LIAM RODITTY
s
t
t t
t
t
t
P
4
P
2
P
1
P
3
P
5
P
6
algorithm k-SimplePath(G(V, E), s, t, k)
P
1
(s, t) Dijkstra(G(V, E), s, t)
T P
1
(s, t)
E
d
(s)
Insert(H, 'SecondPath(G(V, E), P
1
(s, t)), 1`)
for i 2 to k
'P
i
(s, t), j` min(H)
Concatenate P
i
(v
i
, t) to u
i
in T
E
d
(v
j
) E
d
(v
j
) (u
i
, v
i
)
E
d
(v
i
)
P
2
i
(v
i
, t) SecondPath(G(V ` P
i
(s, u
i
), E ` E
d
(v
i
)), P
i
(v
i
, t))
P
2
j
(v
j
, t) SecondPath(G(V ` P
j
(s, u
j
), E ` E
d
(v
j
)), P
j
(v
j
, t))
Insert(H, 'P
i
(s, v
i
) P
2
i
(v
i
, t), i`)
Insert(H, 'P
j
(s, v
j
) P
2
j
(v
j
, t), j`)
return T
Fig. 6. A deviations tree and the k shortest simple paths algorithm.
the algorithm from [19] when only an approximation algorithm of the second shortest
path is available. We start this section with an overview of the k shortest simple
paths algorithm from [19]. We then explain how to modify the algorithm to work
when only an approximation of the second shortest path can be computed. We end
with a correctness proof for the modied algorithm.
Let P
i
(s, t) be the ith shortest path in G. The output of the k shortest simple
paths algorithm is given in the form of a tree of paths, which we dene as follows.
Definition 4.1 (deviation tree). For k = 1 the tree is simply P
1
(s, t). Let T
i1
be the tree that contains the (i 1)th shortest paths, and let P
i
(s, t) = s, u
1
, . . . , u
, t
be the ith shortest path. Let P
i
(s, u
j
) be the longest subpath of P
i
(s, t) already in
T
i1
. We make a copy from P
i
(u
j+1
, t) and concatenate it with the copy of P
i
(s, u
j
)
in T
i1
. The resulting tree is T
i
. The edge (u
j
, u
j+1
) is said to be the deviation edge
of P
i
(s, t).
The algorithm for computing the k shortest simple paths is given in Figure 6.
The algorithm maintains a heap H of paths. The key of each path in the heap is its
length. It performs k iterations, and in each iteration the item with the minimal key
is extracted from H. When the ith iteration begins, every item of H is a pair 'Q, j`,
where Q is a path and j < i. The key of the item is the length of Q. The path Q
and the index j satisfy the following relation. Let (u
j
, v
j
) be the deviation edge of
P
j
(s, t), and let E
d
(v
j
) be the set of deviation edges emanating from vertices of the
path P
j
(v
j
, t) so far. (The purpose of the set E
d
(v
j
) is to prevent a path from being
the second shortest path of P
j
(v
j
, t) more than once.) In the graph G(V `P
j
(s, u
j
), E`
E
d
(v
j
)) the path P
j
(v
j
, t) is the shortest path from v
j
to t, and Q(v
j
, t) is the second
shortest path from v
j
to t. The portion Q(s, v
j
) of Q is simply P
j
(s, v
j
).
In the ith iteration the algorithm extracts a path from H. In [19] we proved the
following lemma.
Lemma 4.2. The extracted path in the ith iteration of the algorithm is the ith
shortest path, P
i
(s, t).
Let (u
i
, v
i
) be the deviation edge of P
i
(s, t). After the extraction the algo-
rithm computes a second shortest path P
2
i
(v
i
, t) for P
i
(v
i
, t) in the graph G(V `
P
i
(s, u
i
), E ` E
d
(v
i
)) and a second shortest path P
2
j
(v
j
, t) for P
j
(v
j
, t) in the graph
Copyright by SIAM. Unauthorized reproduction of this article is prohibited.
k-SHORTEST SIMPLE PATHS 2375
G(V `P
j
(s, u
j
), E`E
d
(v
j
)). Notice that the edge (u
i
, v
i
) is added to E
d
(v
j
) to prevent
the path P
i
(v
j
, t) from being the second shortest path of P
j
(v
j
, t). The algorithm in-
serts into H the following two pairs: 'P
i
(s, v
i
) P
2
i
(v
i
, t), i` and 'P
j
(s, v
j
) P
2
j
(v
j
, t), j`.
The main issue that we need to address here, however, is the eect of using an
approximation of the second shortest path.
We modify the algorithm in the following way. Let
P
2
i
(v
i
, t) be the approximated
second shortest path of P
i
(v
i
, t) in the graph G(V `P
i
(s, u
i
), E`E
d
(v
i
)), and let (u, v)
be the edge of
P
2
i
(v
i
, t) which deviates from P
i
(v
i
, t). We compute the shortest path
from v to t in the graph G(V ` P
i
(s, u), E). Let Q(v, t) be the resulting shortest path
from v to t. The path inserted eventually to the heap is the following concatenation
of paths: P
i
(s, u) (u, v) Q(v, t). Note that the length of Q(v, t) is at most the length
of
P
2
i
(v, t). We are now ready to prove the main theorem of this section.
Theorem 4.3. Let k 1, and let T be the output of the modied k shortest simple
paths algorithm. For every i k the length of the ith path added to T is at most
3
2
the length of P
i
(s, t). The running time of the algorithm is O(k(m
n + n
3/2
log n)).
Proof. Let T
i1
be the tree after i 1 iterations of the algorithm. Let j < i, and
let
P
j
(s, t) be the path in T
i1
that has the longest common prex with P
i
(s, t). We
show that the length of the ith path that is extracted from H and is added to T
i1
to create T
i
is at most
3
2
times more than the length of P
i
(s, t).
Let (u
j
, v
j
) be the deviation edge of
P
j
(s, t). The modication of the algorithm
described above guarantees that
P
j
(v
j
, t) is a shortest path in the graph G
= G(V `
P
j
(s, u
j
), E). Let
P
2
j
(v
j
, t) be the approximated second shortest path computed for
the path
P
j
(v
j
, t) in the graph G
= G(V `
P
j
(s, u
j
), E ` E
d
(v
j
)). The path P
i
(v
j
, t)
is part of G
n + n
3/2
log n)).
5. Concluding remarks and open problems. We presented the rst approxi-
mation algorithm for nding k shortest simple paths in weighted directed graphs. The
running time of the algorithm is
O(km
n polylog(n, 1/))
and stretch of 1 + ?
Acknowledgments. Many thanks to Vera Asodi and Uri Stav for reading the
paper and giving many helpful remarks. I would also like to thank the anonymous
referees for their helpful remarks.
REFERENCES
[1] D. Aingworth, C. Chekuri, P. Indyk, and R. Motwani, Fast estimation of diameter and
shortest paths (without matrix multiplication), SIAM J. Comput., 28 (1999), pp. 1167
1181.
[2] G. Blelloch, V. Vassilevska, and R. Williams, A new combinatorial approach for sparse
graph problems, in Proceedings of the 35th International Colloquium on Automata, Lan-
guages and Programming (ICALP 2008), 2008, pp. 108120.
Copyright by SIAM. Unauthorized reproduction of this article is prohibited.
2376 LIAM RODITTY
[3] A. W. Brander and M. C. Sinclair, A comparative study of k-shortest path algorithms,
in Proceedings of the 11th UK Performance Engineering Workshop for Computer and
Telecommunications Systems, 1995, pp. 370379.
[4] E. Cohen and U. Zwick, All-pairs small-stretch paths, J. Algorithms, 38 (2001), pp. 335353.
[5] C. Demetrescu, M. Thorup, R. A. Chowdhury, and V. Ramachandran, Oracles for dis-
tances avoiding a failed node or link, SIAM J. Comput., 37 (2008), pp. 12991318.
[6] D. Dor, S. Halperin, and U. Zwick, All-pairs almost shortest paths, SIAM J. Comput., 29
(2000), pp. 17401759.
[7] M. Elkin, Computing almost shortest paths, in Proceedings of the 20th ACM Symposium on
Principles of Distributed Computing (PODC 2001), 2001, pp. 5362.
[8] D. Eppstein, Finding the k shortest paths, SIAM J. Comput., 28 (1998), pp. 652673.
[9] E. Hadjiconstantinou and N. Christofides, An ecient implementation of an algorithm
for nding K shortest simple paths, Networks, 34 (1999), pp. 88101.
[10] J. Hershberger and S. Suri, Vickrey prices and shortest paths: What is an edge worth?, in
Proceedings of the 42nd Annual IEEE Symposium on Foundations of Computer Science
(FOCS 2001), 2001, pp. 252259.
[11] J. Hershberger and S. Suri, Erratum to Vickrey pricing and shortest paths: What is an
edge worth?, in Proceedings of the 43rd Annual IEEE Symposium on Foundations of
Computer Science (FOCS 2002), 2002, p. 809.
[12] J. Hershberger, S. Suri, and A. Bhosle, On the diculty of some shortest path problems,
ACM Trans. Algorithms, 3 (2007), article 5.
[13] N. Katoh, T. Ibaraki, and H. Mine, An ecient algorithm for K shortest simple paths,
Networks, 12 (1982), pp. 411427.
[14] E. L. Lawler, A procedure for computing the K best solutions to discrete optimization problems
and its application to the shortest path problem, Management Sci., 18 (1971/1972), pp.
401405.
[15] K. Malik, A. K. Mittal, and S. K. Gupta, The k most vital arcs in the shortest path problem,
Oper. Res. Lett., 8 (1989), pp. 223227.
[16] E. Martins, M. Pascoal, and J. Esteves dos Santos, Deviation algorithms for ranking
shortest paths, Internat. J. Found. Comput. Sci., 10 (1999), pp. 247263.
[17] A. Perko, Implementation of algorithms for K shortest loopless paths, Networks, 16 (1986),
pp. 149160.
[18] S. Pettie, A new approach to all-pairs shortest paths on real-weighted graphs, Theoret. Com-
put. Sci., 312 (2004), pp. 4774.
[19] L. Roditty and U. Zwick, Replacement paths and k simple shortest paths in unweighted
directed graphs, in Proceedings of the 32nd International Colloquium on Automata, Lan-
guages and Programming (ICALP 2005), 2005, pp. 249260.
[20] M. Thorup and U. Zwick, Approximate distance oracles, J. ACM, 52 (2005), pp. 124.
[21] J. Y. Yen, Finding the k shortest loopless paths in a network, Management Sci., 17 (1970/1971),
pp. 712716.