Computing the k Shortest Paths

Computing the k Shortest Paths
T. M. Murali
Slides courtesy of Chris Poirel
March 31, 2014
Motivation
Find the k shortest paths between a pair of nodes s and t in a directed graph,
where each edge has a real-valued positive weight.
s
2
1
2
2
2
2
1
4
T. M. Murali Slides courtesy of Chris Poirel
2
1
t
3
March 31, 2014
k Shortest Paths
Two Formulations
I
I
“Finding the k Shortest Loopless Paths in a Network”
Yen, Management Science, 17(11), 712–716, 1971.
“Finding the k Shortest Paths in a Network”
Eppstein, SIAM J. Computing, 28(2), 652–673, 1998.
s
2
1
2
2
1
2
2
2
1
4
t
3
T. M. Murali Slides courtesy of Chris Poirel
March 31, 2014
k Shortest Paths
Two Formulations
I
I
“Finding the k Shortest Loopless Paths in a Network”
Yen, Management Science, 17(11), 712–716, 1971.
“Finding the k Shortest Paths in a Network”
Eppstein, SIAM J. Computing, 28(2), 652–673, 1998.
s
2
1
2
s
2
1
2
2
s
2
2
1
2
1
t
3
T. M. Murali Slides courtesy of Chris Poirel
1
1
2
2
2
4
1
2
2
1
t
3
March 31, 2014
1
t
k Shortest Paths
Shortest Loopless Paths – Basic Idea
s
t
I
Naı̈ve Approaches (time-consuming):
I
I
T. M. Murali Slides courtesy of Chris Poirel
Enumerate all paths from s to t and sort.
Obtain k − 1 shortest paths, hide an edge from
each path and find a shortest path in the modified
network. Test all combinations.
March 31, 2014
k Shortest Paths
Shortest Loopless Paths – Basic Idea
s
t
I
Naı̈ve Approaches (time-consuming):
I
I
s
t
s
t
I
Basic idea of Yen’s algorithm:
I
I
s
t
s
t
T. M. Murali Slides courtesy of Chris Poirel
Enumerate all paths from s to t and sort.
Obtain k − 1 shortest paths, hide an edge from
each path and find a shortest path in the modified
network. Test all combinations.
Compute the shortest path from s to t
The k th shortest path will be a deviation from the
previously-discovered shortest path.
March 31, 2014
k Shortest Paths
Shortest Loopless Paths
I
I
{s, v2 , v3 , . . . , t} denotes a simple path from s to t
th
k
shortest path from s to t
P k = {s, P2k , P3k , . . . , P|P
k |−1 , t} is the k
T. M. Murali Slides courtesy of Chris Poirel
March 31, 2014
k Shortest Paths
Shortest Loopless Paths
I
I
I
{s, v2 , v3 , . . . , t} denotes a simple path from s to t
th
k
shortest path from s to t
P k = {s, P2k , P3k , . . . , P|P
k |−1 , t} is the k
Dik is the “deviation from P k−1 at node Pik−1 ”
More specifically, the shortest s
t path that:
1. coincides with P k−1 from s to Pik−1
2. deviates to a node u where u is not used as this deviation in any of the k − 1
shortest paths
3. reaches t by a shortest path from u without using any node in the first part of
the path
s
T. M. Murali Slides courtesy of Chris Poirel
u
t
March 31, 2014
k Shortest Paths
Shortest Loopless Paths
I
I
I
{s, v2 , v3 , . . . , t} denotes a simple path from s to t
th
k
shortest path from s to t
P k = {s, P2k , P3k , . . . , P|P
k |−1 , t} is the k
Dik is the “deviation from P k−1 at node Pik−1 ”
More specifically, the shortest s
t path that:
1. coincides with P k−1 from s to Pik−1
2. deviates to a node u where u is not used as this deviation in any of the k − 1
shortest paths
3. reaches t by a shortest path from u without using any node in the first part of
the path
s
I
I
u
t
Rik = {s, P2k , P3k , . . . , Pik } is the root of Dik
Sik = {Pik , . . . , t} is the spur of Dik
T. M. Murali Slides courtesy of Chris Poirel
March 31, 2014
k Shortest Paths
Shortest Loopless Paths
I
I
Find the shortest path P 1
For k = 2, 3, . . ., find P k as follows:
1: Let B k = B k−1 , the set of candidate paths from iteration k − 1
2: for 1 ≤ i < |P k−1 | do
3:
Let x = Pik−1
4:
Hide incoming edges to x for the remainder of iteration k
5:
for each j such that the first i nodes in in P j match P k−1 do
j
6:
Hide edge (x, Pi+1
) for the remainder of iteration k
7:
Rik is the first i nodes of P k−1
8:
Sik is the shortest path from x to t
9:
Join Rik and Sik to form Dik
10:
Add candidate path Dik to B k
11: Remove the shortest path from B k and return it
s
T. M. Murali Slides courtesy of Chris Poirel
u
t
March 31, 2014
k Shortest Paths
Example – Find P 3
s
2
2
1
P 1 = {s, c, d, t}
c
a
1
2
2
b
2
t
P3 =
?
2
f
d
1
4
e
P 2 = {s, a, t}
3
T. M. Murali Slides courtesy of Chris Poirel
March 31, 2014
k Shortest Paths
Example – Hide Edges for Root {s}
s
2
P 1 = {s, c, d, t}
c
a
1
2
2
b
2
t
P3 =
?
2
f
d
1
4
e
P 2 = {s, a, t}
3
T. M. Murali Slides courtesy of Chris Poirel
March 31, 2014
k Shortest Paths
Example – Hide Edges for Root {s, a}
s
2
P 1 = {s, c, d, t}
c
a
1
e
b
1
4
t
P3 =
?
2
2
f
d
2
P 2 = {s, a, t}
3
T. M. Murali Slides courtesy of Chris Poirel
March 31, 2014
k Shortest Paths
Example – Find Shortest Spur for Each Root
s
2
P 1 = {s, c, d, t}
c
a
1
e
b
1
4
t
P3 =
?
2
2
f
d
2
P 2 = {s, a, t}
3
T. M. Murali Slides courtesy of Chris Poirel
S13 = {s, e, f , t}
S23 = {a, b, t}
March 31, 2014
k Shortest Paths
Example – Identify Shortest Deviation
s
2
2
1
P 1 = {s, c, d, t}
c
a
1
2
2
b
2
t
P3 =
?
2
f
d
1
4
e
P 2 = {s, a, t}
3
S13 = {s, e, f , t}
S23 = {a, b, t}
D13 = {s, e, f , t}
D23 = {s, a, b, t}
T. M. Murali Slides courtesy of Chris Poirel
March 31, 2014
k Shortest Paths
How do we find Sik efficiently?
I
For k = 2, 3, . . ., find P k as follows:
1: Let B k = B k−1 , the set of candidate paths from iteration k − 1
2: for 1 ≤ i < |P k−1 | do
3:
Let x = Pik−1
4:
Hide incoming edges to x for the remainder of iteration k
5:
for each j such that the first i nodes in in P j match P k−1 do
j
6:
Hide edge (x, Pi+1
) for the remainder of iteration k
k
7:
Ri is the first i nodes of P k−1
8:
Sik is the shortest path from x to t
9:
Join Rik and Sik to form Dik
10:
Add candidate path Dik to B k
11: Remove the shortest path from B k and return it
T. M. Murali Slides courtesy of Chris Poirel
March 31, 2014
k Shortest Paths
How do we find Sik efficiently?
I
For k = 2, 3, . . ., find P k as follows:
1: Let B k = B k−1 , the set of candidate paths from iteration k − 1
2: for 1 ≤ i < |P k−1 | do
3:
Let x = Pik−1
4:
Hide incoming edges to x for the remainder of iteration k
5:
for each j such that the first i nodes in in P j match P k−1 do
j
6:
Hide edge (x, Pi+1
) for the remainder of iteration k
k
7:
Ri is the first i nodes of P k−1
8:
Sik is the shortest path from x to t
9:
Join Rik and Sik to form Dik
10:
Add candidate path Dik to B k
11: Remove the shortest path from B k and return it
I
Run Dijkstra’s algorithm.
I
What is the running time of each iteration if the graph has n nodes and m edges?
T. M. Murali Slides courtesy of Chris Poirel
March 31, 2014
k Shortest Paths
How do we find Sik efficiently?
I
For k = 2, 3, . . ., find P k as follows:
1: Let B k = B k−1 , the set of candidate paths from iteration k − 1
2: for 1 ≤ i < |P k−1 | do
3:
Let x = Pik−1
4:
Hide incoming edges to x for the remainder of iteration k
5:
for each j such that the first i nodes in in P j match P k−1 do
j
6:
Hide edge (x, Pi+1
) for the remainder of iteration k
k
7:
Ri is the first i nodes of P k−1
8:
Sik is the shortest path from x to t
9:
Join Rik and Sik to form Dik
10:
Add candidate path Dik to B k
11: Remove the shortest path from B k and return it
I
Run Dijkstra’s algorithm.
I
What is the running time of each iteration if the graph has n nodes and m edges?
At most n invocations of Dijkstra’s algorithm, i.e., O(n(m + n log n)).
I
Therefore total running time is O(kn(m + n log n)).
T. M. Murali Slides courtesy of Chris Poirel
March 31, 2014
k Shortest Paths