Computing the k Shortest Paths - Virginia...

18
Computing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March 31, 2014

Transcript of Computing the k Shortest Paths - Virginia...

Page 1: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

Computing the k Shortest Paths

T. M. MuraliSlides courtesy of Chris Poirel

March 31, 2014

Page 2: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

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

t

1

1

1

2

2

3

2

2

2

2

4

T. M. Murali Slides courtesy of Chris Poirel March 31, 2014 k Shortest Paths

Page 3: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

Two Formulations

I “Finding the k Shortest Loopless Paths in a Network”Yen, Management Science, 17(11), 712–716, 1971.

I “Finding the k Shortest Paths in a Network”Eppstein, SIAM J. Computing, 28(2), 652–673, 1998.

s

t

1

1

1

2

2

3

2

2

2

2

4

s

t

1

1

1

2

3

2

2

2

s

t

2

2

2 1

1

1

T. M. Murali Slides courtesy of Chris Poirel March 31, 2014 k Shortest Paths

Page 4: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

Two Formulations

I “Finding the k Shortest Loopless Paths in a Network”Yen, Management Science, 17(11), 712–716, 1971.

I “Finding the k Shortest Paths in a Network”Eppstein, SIAM J. Computing, 28(2), 652–673, 1998.

s

t

1

1

1

2

2

3

2

2

2

2

4

s

t

1

1

1

2

3

2

2

2

s

t

2

2

2 1

1

1

T. M. Murali Slides courtesy of Chris Poirel March 31, 2014 k Shortest Paths

Page 5: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

Shortest Loopless Paths – Basic Idea

s t I Naıve Approaches (time-consuming):I Enumerate all paths from s to t and sort.I Obtain k − 1 shortest paths, hide an edge from

each path and find a shortest path in the modifiednetwork. Test all combinations.

I Basic idea of Yen’s algorithm:I Compute the shortest path from s to tI The kth shortest path will be a deviation from the

previously-discovered shortest path.

T. M. Murali Slides courtesy of Chris Poirel March 31, 2014 k Shortest Paths

Page 6: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

Shortest Loopless Paths – Basic Idea

s t

s t

s t

s t

s t

I Naıve Approaches (time-consuming):I Enumerate all paths from s to t and sort.I Obtain k − 1 shortest paths, hide an edge from

each path and find a shortest path in the modifiednetwork. Test all combinations.

I Basic idea of Yen’s algorithm:I Compute the shortest path from s to tI The kth shortest path will be a deviation from the

previously-discovered shortest path.

T. M. Murali Slides courtesy of Chris Poirel March 31, 2014 k Shortest Paths

Page 7: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

Shortest Loopless Paths

I {s, v2, v3, . . . , t} denotes a simple path from s to t

I Pk = {s,Pk2 ,P

k3 , . . . ,P

k|Pk |−1, t} is the kth shortest path from s to t

I Dki is the “deviation from Pk−1 at node Pk−1

i ”More specifically, the shortest s t path that:

1. coincides with Pk−1 from s to Pk−1i

2. deviates to a node u where u is not used as this deviation in any of the k − 1shortest paths

3. reaches t by a shortest path from u without using any node in the first part ofthe path

I Rki = {s,Pk

2 ,Pk3 , . . . ,P

ki } is the root of Dk

i

I Ski = {Pk

i , . . . , t} is the spur of Dki

T. M. Murali Slides courtesy of Chris Poirel March 31, 2014 k Shortest Paths

Page 8: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

Shortest Loopless Paths

I {s, v2, v3, . . . , t} denotes a simple path from s to t

I Pk = {s,Pk2 ,P

k3 , . . . ,P

k|Pk |−1, t} is the kth shortest path from s to t

I Dki is the “deviation from Pk−1 at node Pk−1

i ”More specifically, the shortest s t path that:

1. coincides with Pk−1 from s to Pk−1i

2. deviates to a node u where u is not used as this deviation in any of the k − 1shortest paths

3. reaches t by a shortest path from u without using any node in the first part ofthe path

s t

u

I Rki = {s,Pk

2 ,Pk3 , . . . ,P

ki } is the root of Dk

i

I Ski = {Pk

i , . . . , t} is the spur of Dki

T. M. Murali Slides courtesy of Chris Poirel March 31, 2014 k Shortest Paths

Page 9: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

Shortest Loopless Paths

I {s, v2, v3, . . . , t} denotes a simple path from s to t

I Pk = {s,Pk2 ,P

k3 , . . . ,P

k|Pk |−1, t} is the kth shortest path from s to t

I Dki is the “deviation from Pk−1 at node Pk−1

i ”More specifically, the shortest s t path that:

1. coincides with Pk−1 from s to Pk−1i

2. deviates to a node u where u is not used as this deviation in any of the k − 1shortest paths

3. reaches t by a shortest path from u without using any node in the first part ofthe path

s t

u

I Rki = {s,Pk

2 ,Pk3 , . . . ,P

ki } is the root of Dk

i

I Ski = {Pk

i , . . . , t} is the spur of Dki

T. M. Murali Slides courtesy of Chris Poirel March 31, 2014 k Shortest Paths

Page 10: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

Shortest Loopless Paths

I Find the shortest path P1

I For k = 2, 3, . . ., find Pk as follows:1: Let Bk = Bk−1, the set of candidate paths from iteration k − 12: for 1 ≤ i < |Pk−1| do3: Let x = Pk−1

i

4: Hide incoming edges to x for the remainder of iteration k5: for each j such that the first i nodes in in P j match Pk−1 do6: Hide edge (x ,P j

i+1) for the remainder of iteration k

7: Rki is the first i nodes of Pk−1

8: Ski is the shortest path from x to t

9: Join Rki and Sk

i to form Dki

10: Add candidate path Dki to Bk

11: Remove the shortest path from Bk and return it

s t

u

T. M. Murali Slides courtesy of Chris Poirel March 31, 2014 k Shortest Paths

Page 11: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

Example – Find P3

s

t

1

1

1

2

2

3

2

2

2

2

4

a

b

c

d

e

f

P1 = {s, c , d , t}P2 = {s, a, t}P3 = ?

S31 = {s, e, f , t}

S32 = {a, b, t}

D31 = {s, e, f , t}

D32 = {s, a, b, t}

T. M. Murali Slides courtesy of Chris Poirel March 31, 2014 k Shortest Paths

Page 12: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

Example – Hide Edges for Root {s}

s

t

1

1

2

2

3

2

2

2

4

a

b

c

d

e

f

P1 = {s, c , d , t}P2 = {s, a, t}P3 = ?

S31 = {s, e, f , t}

S32 = {a, b, t}

D31 = {s, e, f , t}

D32 = {s, a, b, t}

T. M. Murali Slides courtesy of Chris Poirel March 31, 2014 k Shortest Paths

Page 13: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

Example – Hide Edges for Root {s, a}

s

t

1

1

2

3

2

2

2

4

a

b

c

d

e

f

P1 = {s, c , d , t}P2 = {s, a, t}P3 = ?

S31 = {s, e, f , t}

S32 = {a, b, t}

D31 = {s, e, f , t}

D32 = {s, a, b, t}

T. M. Murali Slides courtesy of Chris Poirel March 31, 2014 k Shortest Paths

Page 14: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

Example – Find Shortest Spur for Each Root

s

t

1

1

2

3

2

2

2

4

a

b

c

d

e

f

P1 = {s, c , d , t}P2 = {s, a, t}P3 = ?

S31 = {s, e, f , t}

S32 = {a, b, t}

D31 = {s, e, f , t}

D32 = {s, a, b, t}

T. M. Murali Slides courtesy of Chris Poirel March 31, 2014 k Shortest Paths

Page 15: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

Example – Identify Shortest Deviation

s

t

1

1

1

2

2

3

2

2

2

2

4

a

b

c

d

e

f

P1 = {s, c , d , t}P2 = {s, a, t}P3 = ?

S31 = {s, e, f , t}

S32 = {a, b, t}

D31 = {s, e, f , t}

D32 = {s, a, b, t}

T. M. Murali Slides courtesy of Chris Poirel March 31, 2014 k Shortest Paths

Page 16: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

How do we find Ski efficiently?

I For k = 2, 3, . . ., find Pk as follows:1: Let Bk = Bk−1, the set of candidate paths from iteration k − 12: for 1 ≤ i < |Pk−1| do3: Let x = Pk−1

i

4: Hide incoming edges to x for the remainder of iteration k5: for each j such that the first i nodes in in P j match Pk−1 do6: Hide edge (x ,P j

i+1) for the remainder of iteration k

7: Rki is the first i nodes of Pk−1

8: Ski is the shortest path from x to t

9: Join Rki and Sk

i to form Dki

10: Add candidate path Dki to Bk

11: Remove the shortest path from Bk 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

Page 17: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

How do we find Ski efficiently?

I For k = 2, 3, . . ., find Pk as follows:1: Let Bk = Bk−1, the set of candidate paths from iteration k − 12: for 1 ≤ i < |Pk−1| do3: Let x = Pk−1

i

4: Hide incoming edges to x for the remainder of iteration k5: for each j such that the first i nodes in in P j match Pk−1 do6: Hide edge (x ,P j

i+1) for the remainder of iteration k

7: Rki is the first i nodes of Pk−1

8: Ski is the shortest path from x to t

9: Join Rki and Sk

i to form Dki

10: Add candidate path Dki to Bk

11: Remove the shortest path from Bk 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

Page 18: Computing the k Shortest Paths - Virginia Techcourses.cs.vt.edu/.../lecture-09-k-shortest-paths.pdfComputing the k Shortest Paths T. M. Murali Slides courtesy of Chris Poirel March

How do we find Ski efficiently?

I For k = 2, 3, . . ., find Pk as follows:1: Let Bk = Bk−1, the set of candidate paths from iteration k − 12: for 1 ≤ i < |Pk−1| do3: Let x = Pk−1

i

4: Hide incoming edges to x for the remainder of iteration k5: for each j such that the first i nodes in in P j match Pk−1 do6: Hide edge (x ,P j

i+1) for the remainder of iteration k

7: Rki is the first i nodes of Pk−1

8: Ski is the shortest path from x to t

9: Join Rki and Sk

i to form Dki

10: Add candidate path Dki to Bk

11: Remove the shortest path from Bk 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