Single-Source Shortest Paths Chapter 24 1 CPTR 430 Algorithms - - PowerPoint PPT Presentation

single source shortest paths
SMART_READER_LITE
LIVE PREVIEW

Single-Source Shortest Paths Chapter 24 1 CPTR 430 Algorithms - - PowerPoint PPT Presentation

Single-Source Shortest Paths Chapter 24 1 CPTR 430 Algorithms Single-Source Shortest Paths Motivation We plan a trip from Collegedale to Silicon Valley Our map has distances between interstate highway interchanges marked We want to


slide-1
SLIDE 1

Single-Source Shortest Paths

Chapter 24

CPTR 430 Algorithms Single-Source Shortest Paths

1

slide-2
SLIDE 2

Motivation

■ We plan a trip from Collegedale to Silicon Valley ■ Our map has distances between interstate highway interchanges

marked

■ We want to layout the shortest route ■ Proposal: Try all possible routes and select the shortest one

CPTR 430 Algorithms Single-Source Shortest Paths

2

slide-3
SLIDE 3

Try-All-Routes Algorithm

The try-all-routes approach is combinatorially explosive!

CPTR 430 Algorithms Single-Source Shortest Paths

3

slide-4
SLIDE 4

Shortest-Path Problem

■ G

V

E

is a weighted, directed graph

■ Weight function w : E

☎ ✆

■ Weight of a path p

v0

v1

✂✞ ✞ ✞ ✂

vk

is the sum of the weights of the edges that make up the path:

w

p

  • k

i

1

w

vi

1

vi

CPTR 430 Algorithms Single-Source Shortest Paths

4

slide-5
SLIDE 5

Shortest-Path Weight

The shortest-path weight from u to v is

δ

u

v

  • min
  • w

p

✄ ✁

u p

v

if there is a path from u to v

  • therwise

A shortest path from vertex u to vertex v is any path with weight

w

p

  • δ

u

v

CPTR 430 Algorithms Single-Source Shortest Paths

5

slide-6
SLIDE 6

Model

■ In our trip planner problem ❚ Each interchange is a vertex in the graph ❚ Each road connecting interchanges is an edge in the graph ❚ The distance between two interchanges represents an edge weight ■ Edge weights may be interpreted as metrics other than distance: ❚ time ❚ cost ❚ penalties

Any quantity that accumulates linearly along a path that we wish to minimize

CPTR 430 Algorithms Single-Source Shortest Paths

6

slide-7
SLIDE 7

Breadth-first Search

BFS (Chapter 22) is one type of shortest path problem

■ BFS is (usually) applied to unweighted, undirected graphs ■ Viewed another way, unweighted graphs are just weighted graphs with

unit-weight edges

■ The concepts of BFS can be applied to shortest paths in weighted,

directed graphs

CPTR 430 Algorithms Single-Source Shortest Paths

7

slide-8
SLIDE 8

Shortest Path Variants

■ Single-source SP ❚ Find the shortest path from a source vertex s

  • V to every vertex

v

  • V

■ Single-destination SP ❚ Find the shortest path to a destination vertex t

  • V from every vertex

v

  • V

❚ For undirected and symmetric directed graphs, just use the single-

source SP algorithm and reverse all the edges

CPTR 430 Algorithms Single-Source Shortest Paths

8

slide-9
SLIDE 9

Shortest Path Variants

■ Single-pair SP ❚ Find the shortest path from a source vertex u

  • V to a destination

vertex v

  • V

❚ Apply the single-source SP algorithm with u as the source vertex; the

shortest path to v is found in the solution

❚ No known algorithms for this problem run faster asymptotically than

the single-source algorithm applied to this problem

■ All-pairs SP ❚ Find the SP between u and v,

  • u

v

  • V

❚ Can run single-source SP algorithm on every vertex ❚ A more efficient algorithm exists

CPTR 430 Algorithms Single-Source Shortest Paths

9

slide-10
SLIDE 10

Optimal Substructure

■ The shortest path between two vertices contains other shortest paths ■ SP problems thus exhibit an optimal substructure; recall ❚ Dynamic programming

(Chapter 15)

❚ Greedy algorithms

(Chapters 16, 23) Dijkstra’s SP algorithm is a greedy algorithm

CPTR 430 Algorithms Single-Source Shortest Paths

10

slide-11
SLIDE 11

Lemma 24.1

Subpaths of shortest paths are shortest paths

■ Given a weighted digraph G

V

E

with weight function w : E

☎ ✆

■ Let p

v0

v1

✂✞ ✞ ✞ ✂

vk

be a shortest path from vertex v0 to vertex vk

■ For any i

j such that 0

  • i
  • j
  • k, let pi j

vi

vi

1

✂✞ ✞ ✞ ✂

v j

be a subpath of p from vertex vi to vertex v j

pi j is a shortest path from vi to v j

CPTR 430 Algorithms Single-Source Shortest Paths

11

slide-12
SLIDE 12

Proof of Lemma 24.1

We’ll use a proof by contradiction

■ Let p be a shortest path from v0 to vk ■ Decompose path p into three segments: v0

p0i

vi vi pi j

v j v j p jk

vk

  • w

p

  • w

p0i

✄✂✁

w

pi j

✄✂✁

w

p jk

■ Assume there exists a path p

i j from vi to v j such that w

p

i j

✄✆☎

w

pi j

  • v0

p0i

vi vi p

i j

v j v j p jk

vk is a path

from v0 to vk with weight w

p

  • w

p0i

✄✂✁

w

p

i j

✄✂✁

w

p jk

✄ ☎

w

p

■ This contradicts the assumption that p is a shortest path

from v0 to vk

CPTR 430 Algorithms Single-Source Shortest Paths

12

slide-13
SLIDE 13

Negative-Weight Edges

■ Some SP algorithms can handle graphs with negative edges ❚ E.g., Bellman-Ford algorithm

while others cannot

❚ E.g., Dijkstra’s algorithm ■ We will focus on graphs with non-negative edges ■ Most practical problems involve non-negative edges exclusivley

CPTR 430 Algorithms Single-Source Shortest Paths

13

slide-14
SLIDE 14

Cycles

■ Any path that contains a positive-weight cycle can be made shorter if

the cycle is removed from the path

■ Negative-weight cycles are pathological for SP problems, so they are

not allowed (Why?)

■ 0-weight cycles do not affect the path lengths, so we generally remove

0-weight cycles from paths, if necessary

CPTR 430 Algorithms Single-Source Shortest Paths

14

slide-15
SLIDE 15

Representing Shortest Paths

■ Use a representation similar to BFS (Chapter 22) ■

  • v
  • V, π
  • v

is the predecessor of v on a shortest path from s (start vertex) to v

❚ π

  • v

is either another vertex or null

❚ The predecessors can be traced back from a given vertex v to s to

determine a shortest path from s to v

❚ Given a vertex v with π

  • v
✁ ✂
  • null, the printPath() algorithm

(Chapter 22) will print a shortest path from s to v

■ The predecessor subgraph Gπ

is induced by the π values

❚ Vπ

  • v
  • V

π

  • v
✁ ✂
  • null
✄ ✄
  • s

❚ Eπ

π

  • v
✁ ✂

v

  • E

v

  • s
✄ ✄

CPTR 430 Algorithms Single-Source Shortest Paths

15

slide-16
SLIDE 16

Shortest-paths Tree

■ When the shortest-path algorithm is finished, Gπ is a tree that contains

a shortest path from the source vertex s to every other vertex reachable from s

■ A shortest-path tree, G

V

✄ ✂

E

✄ ✄

, is a directed subgraph of G, where

❚ V

  • V and E
  • E

❚ V

is the set of vertices reachable from s

❚ G

is a tree rooted at s

  • v
  • V

, the unique simple path from s to v is a shortest path from s to v in G

■ Shortest-path trees for given graph G and start vertex s are not unique ■ Shortest-path trees are like breadth-first trees (Chapter 22)

But here shortest paths are defi ned based on edge weights, not simply the number of edges in the path

CPTR 430 Algorithms Single-Source Shortest Paths

16

slide-17
SLIDE 17

Shortest-path Estimate

■ Each vertex v

  • V maintains a shortest-path estimate, d
  • v

, an upper bound on the weight of a shortest path from s to v

■ Initially, ❚ d

  • v
  • v
  • V
  • s

❚ d

  • s
  • ❚ π
  • v
  • null
  • v
  • V

■ The SP algorithm traverses the graph and updates the d

  • v

and π

  • v

values as necessary

CPTR 430 Algorithms Single-Source Shortest Paths

17

slide-18
SLIDE 18

Relaxation

■ An edge

u

v

is relaxed when the SP algorithm determines if the shortest path found so far to v can be improved if

u

v

is used in the shortest path

u

v

results a shorter path

  • d
  • v

and π

  • v

are updated

■ d

  • v
  • d
  • u
✁ ✁

w

u

v

  • d
  • v
✁ ✁

d

  • u
✁ ✁

w

u

v

, and

π

  • v
✁ ✁

u

■ Relaxation is the only way shortest-path estimates and predecessors

are updated

CPTR 430 Algorithms Single-Source Shortest Paths

18

slide-19
SLIDE 19

Relaxation Example

u v u v π[ ] = v x π[ ] = v x d u = [ ] 5 d u = [ ] 5 6 d v = [ ] 6 d v = [ ] ( , ) u v Relax π[ ] = v x d u = [ ] 5 u v u v 9 d v = [ ] d u = [ ] 5 7 d v = [ ] π[ ] = v u ( , ) u v Relax 2 2 2 2

CPTR 430 Algorithms Single-Source Shortest Paths

19

slide-20
SLIDE 20

Properties of Shortest Paths and Relaxation

■ Triangle inequality ❚

u

v

  • E, δ

s

v

  • δ

s

u

✄ ✁

w

u

v

❚ This is a standard metric property ■ Upper-bound property ❚ It is always true that

  • v
  • V

d

  • v
✁✁

δ

s

v

❚ Once d

  • v

is assigned δ

s

v

, it never changes

■ No-path property ❚ If no path exists from s to v, then d

  • v
  • δ

s

v

  • ∞ at all times

CPTR 430 Algorithms Single-Source Shortest Paths

20

slide-21
SLIDE 21

Properties of Shortest Paths and Relaxation

■ Convergence property ❚ If s

u

v is a shortest path in G for some u

v

  • V, and if

d

  • u
  • δ

s

u

at any time before relaxing edge

u

v

, then d

  • v
  • δ

s

v

at all times afterward

■ Path-relaxation property ❚ If p

v0

v1

✂✞ ✞ ✞ ✂

vk

is a shortest path from s

  • v0 to vk, and the

edges of p are relaxed in the order

v0

v1

,

v1

v2

, . . . ,

vk

1

vk

, then d

  • vk
  • δ

s

vk

❚ The path-relaxation property holds regardless of other intermixed

relaxations

■ Predecessor-subgraph property ❚ d

  • v
  • δ

s

v

for all v

  • V, the predecessor subgraph is a shortest-

paths tree rooted at s

CPTR 430 Algorithms Single-Source Shortest Paths

21

slide-22
SLIDE 22

Dijkstra’a Algorithm

■ Solves the single-source SP problem on graphs ❚ that are weighted ❚ directed ❚ contain only non-negative edge weights ■ S is a set of vertices whose final SP weights from source s have already

been determined

CPTR 430 Algorithms Single-Source Shortest Paths

22