Shortest Paths Eric Price UT Austin CS 331, Spring 2020 - - PowerPoint PPT Presentation

shortest paths
SMART_READER_LITE
LIVE PREVIEW

Shortest Paths Eric Price UT Austin CS 331, Spring 2020 - - PowerPoint PPT Presentation

Shortest Paths Eric Price UT Austin CS 331, Spring 2020 Coronavirus Edition CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24 Talk Outline Logistics 1 Shortest Paths: Bellman-Ford 2 Dijkstras Algorithm 3 CS 331, Spring


slide-1
SLIDE 1

Shortest Paths

Eric Price

UT Austin

CS 331, Spring 2020 Coronavirus Edition

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-2
SLIDE 2

Talk Outline

1

Logistics

2

Shortest Paths: Bellman-Ford

3

Dijkstra’s Algorithm

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-3
SLIDE 3

Talk Outline

1

Logistics

2

Shortest Paths: Bellman-Ford

3

Dijkstra’s Algorithm

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-4
SLIDE 4

Welcome to 331 online!

I’ll be experimenting with different teaching methods.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-5
SLIDE 5

Welcome to 331 online!

I’ll be experimenting with different teaching methods.

◮ Bear with me. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-6
SLIDE 6

Welcome to 331 online!

I’ll be experimenting with different teaching methods.

◮ Bear with me. ◮ Give feedback! Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-7
SLIDE 7

Welcome to 331 online!

I’ll be experimenting with different teaching methods.

◮ Bear with me. ◮ Give feedback!

Stay healthy

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-8
SLIDE 8

Welcome to 331 online!

I’ll be experimenting with different teaching methods.

◮ Bear with me. ◮ Give feedback!

Stay healthy

◮ Let me know if you’re sick & need extra time. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-9
SLIDE 9

Welcome to 331 online!

I’ll be experimenting with different teaching methods.

◮ Bear with me. ◮ Give feedback!

Stay healthy

◮ Let me know if you’re sick & need extra time. ◮ Please wear a mask if you interact with others. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-10
SLIDE 10

Welcome to 331 online!

I’ll be experimenting with different teaching methods.

◮ Bear with me. ◮ Give feedback!

Stay healthy

◮ Let me know if you’re sick & need extra time. ◮ Please wear a mask if you interact with others.

Second exam will be take-home

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-11
SLIDE 11

Welcome to 331 online!

I’ll be experimenting with different teaching methods.

◮ Bear with me. ◮ Give feedback!

Stay healthy

◮ Let me know if you’re sick & need extra time. ◮ Please wear a mask if you interact with others.

Second exam will be take-home

◮ Given out after class on April 8. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-12
SLIDE 12

Welcome to 331 online!

I’ll be experimenting with different teaching methods.

◮ Bear with me. ◮ Give feedback!

Stay healthy

◮ Let me know if you’re sick & need extra time. ◮ Please wear a mask if you interact with others.

Second exam will be take-home

◮ Given out after class on April 8. ◮ Return before 10am on Friday, April 10. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-13
SLIDE 13

Zoom details

Mute your audio unless you have a question.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-14
SLIDE 14

Zoom details

Mute your audio unless you have a question. I encourage sharing your video

◮ Makes this seem more like a community Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-15
SLIDE 15

Zoom details

Mute your audio unless you have a question. I encourage sharing your video

◮ Makes this seem more like a community

You can “raise your hand” so I know to call on you.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-16
SLIDE 16

Zoom details

Mute your audio unless you have a question. I encourage sharing your video

◮ Makes this seem more like a community

You can “raise your hand” so I know to call on you.

◮ Press “View Participants”, then the little blue hand. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-17
SLIDE 17

Zoom details

Mute your audio unless you have a question. I encourage sharing your video

◮ Makes this seem more like a community

You can “raise your hand” so I know to call on you.

◮ Press “View Participants”, then the little blue hand.

Videos will be recorded & available after class.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-18
SLIDE 18

Talk Outline

1

Logistics

2

Shortest Paths: Bellman-Ford

3

Dijkstra’s Algorithm

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-19
SLIDE 19

Single-Source Shortest Paths

Problem setup:

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-20
SLIDE 20

Single-Source Shortest Paths

Problem setup:

◮ Given a directed graph G = (V , E) Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-21
SLIDE 21

Single-Source Shortest Paths

Problem setup:

◮ Given a directed graph G = (V , E) ◮ Each edge u → v has distance: w(u → v) ∈ R Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-22
SLIDE 22

Single-Source Shortest Paths

Problem setup:

◮ Given a directed graph G = (V , E) ◮ Each edge u → v has distance: w(u → v) ∈ R ◮ Distance of path is sum of distance of edges. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-23
SLIDE 23

Single-Source Shortest Paths

Problem setup:

◮ Given a directed graph G = (V , E) ◮ Each edge u → v has distance: w(u → v) ∈ R ◮ Distance of path is sum of distance of edges. ◮ Given a source s Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-24
SLIDE 24

Single-Source Shortest Paths

Problem setup:

◮ Given a directed graph G = (V , E) ◮ Each edge u → v has distance: w(u → v) ∈ R ◮ Distance of path is sum of distance of edges. ◮ Given a source s

Goal: for every v, compute c∗(v), the distance of shortest s v path in G.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-25
SLIDE 25

Single-Source Shortest Paths

Problem setup:

◮ Given a directed graph G = (V , E) ◮ Each edge u → v has distance: w(u → v) ∈ R ◮ Distance of path is sum of distance of edges. ◮ Given a source s

Goal: for every v, compute c∗(v), the distance of shortest s v path in G. Output two arrays: dist() and pred().

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-26
SLIDE 26

Single-Source Shortest Paths

Problem setup:

◮ Given a directed graph G = (V , E) ◮ Each edge u → v has distance: w(u → v) ∈ R ◮ Distance of path is sum of distance of edges. ◮ Given a source s

Goal: for every v, compute c∗(v), the distance of shortest s v path in G. Output two arrays: dist() and pred().

◮ dist(v) = c∗(v) for all v Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-27
SLIDE 27

Single-Source Shortest Paths

Problem setup:

◮ Given a directed graph G = (V , E) ◮ Each edge u → v has distance: w(u → v) ∈ R ◮ Distance of path is sum of distance of edges. ◮ Given a source s

Goal: for every v, compute c∗(v), the distance of shortest s v path in G. Output two arrays: dist() and pred().

◮ dist(v) = c∗(v) for all v ◮ pred(s) = None Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-28
SLIDE 28

Single-Source Shortest Paths

Problem setup:

◮ Given a directed graph G = (V , E) ◮ Each edge u → v has distance: w(u → v) ∈ R ◮ Distance of path is sum of distance of edges. ◮ Given a source s

Goal: for every v, compute c∗(v), the distance of shortest s v path in G. Output two arrays: dist() and pred().

◮ dist(v) = c∗(v) for all v ◮ pred(s) = None ◮ v ← pred(v) ← pred(pred(v)) ← · · · ← s is shortest s v path. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-29
SLIDE 29

Single-Source Shortest Paths

Problem setup:

◮ Given a directed graph G = (V , E) ◮ Each edge u → v has distance: w(u → v) ∈ R ◮ Distance of path is sum of distance of edges. ◮ Given a source s

Goal: for every v, compute c∗(v), the distance of shortest s v path in G. Output two arrays: dist() and pred().

◮ dist(v) = c∗(v) for all v ◮ pred(s) = None ◮ v ← pred(v) ← pred(pred(v)) ← · · · ← s is shortest s v path.

Question: what if w(u → v) = 1 for all u → v ∈ E?

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-30
SLIDE 30

Generic SSSP algorithm

We maintain a vector dist that satisfies the invariant: dist(v) ≥ c∗(v) for all v at all times.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-31
SLIDE 31

Generic SSSP algorithm

We maintain a vector dist that satisfies the invariant: dist(v) ≥ c∗(v) for all v at all times. InitializeSSSP(s):

◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-32
SLIDE 32

Generic SSSP algorithm

We maintain a vector dist that satisfies the invariant: dist(v) ≥ c∗(v) for all v at all times. InitializeSSSP(s):

◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0.

FordSSSP(s):

◮ InitializeSSSP(s) ◮ Repeat: ⋆ Pick an edge ⋆ If it is “tense”, relax it. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-33
SLIDE 33

Relaxing an edge

Relax(u → v):

◮ If dist(v) > dist(u) + w(u → v): ⋆ dist(v) ← dist(u) + w(u → v) ⋆ pred(v) ← u.

Triangle Inequality

For any edge u → v, c∗(v) ≤ c∗(u) + w(u → v).

Lemma

If dist(v) ≥ c∗(v) for all v, then for any edge u → v, c∗(v) ≤ dist(u) + w(u → v). Hence Relax preserves the invariant that dist(v) ≥ c∗(v)∀v.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-34
SLIDE 34

Generic SSSP algorithm

Invariant: dist(v) ≥ c∗(v) for all v at all times.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-35
SLIDE 35

Generic SSSP algorithm

Invariant: dist(v) ≥ c∗(v) for all v at all times. InitializeSSSP(s):

◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-36
SLIDE 36

Generic SSSP algorithm

Invariant: dist(v) ≥ c∗(v) for all v at all times. InitializeSSSP(s):

◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0.

FordSSSP(s):

◮ InitializeSSSP(s) ◮ Repeat some number of times: ⋆ Pick an edge u → v (somehow) ⋆ Relax(u → v) Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-37
SLIDE 37

Generic SSSP algorithm

Invariant: dist(v) ≥ c∗(v) for all v at all times. InitializeSSSP(s):

◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0.

FordSSSP(s):

◮ InitializeSSSP(s) ◮ Repeat some number of times: ⋆ Pick an edge u → v (somehow) ⋆ Relax(u → v)

Relax(u → v):

◮ If dist(v) > dist(u) + w(u → v): ⋆ dist(v) ← dist(u) + w(u → v) ⋆ pred(v) ← u. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-38
SLIDE 38

Analysis

So far: dist(v) ≥ c∗(v). What we need: eventually dist(v) = c∗(v).

Lemma

Let s = u0 → u1 → · · · → uk−1 → uk be a shortest s uk path. After Relax has been called on every edge of this path in

  • rder—u0 → u1, then u1 → u2, until uk−1 → uk, with arbitrarily many
  • ther calls interleaved—then dist(uk) = c∗(uk).

Moreover, uk ← pred(uk) ← pred(pred(uk)) ← · · · ← s is a shortest s uk path.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-39
SLIDE 39

Lemma

Let s = u0 → u1 → · · · → uk−1 → uk be a shortest s uk path. After Relax has been called on every edge of this path in

  • rder—u0 → u1, then u1 → u2, until uk−1 → uk, with arbitrarily many
  • ther calls interleaved—then dist(uk) = c∗(uk).

Proof.

Induct on k. Base case (k = 0) is easy.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-40
SLIDE 40

Lemma

Let s = u0 → u1 → · · · → uk−1 → uk be a shortest s uk path. After Relax has been called on every edge of this path in

  • rder—u0 → u1, then u1 → u2, until uk−1 → uk, with arbitrarily many
  • ther calls interleaved—then dist(uk) = c∗(uk).

Proof.

Induct on k. Base case (k = 0) is easy... or is it?

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-41
SLIDE 41

Lemma

Let s = u0 → u1 → · · · → uk−1 → uk be a shortest s uk path. After Relax has been called on every edge of this path in

  • rder—u0 → u1, then u1 → u2, until uk−1 → uk, with arbitrarily many
  • ther calls interleaved—then dist(uk) = c∗(uk).

Proof.

Induct on k. Base case (k = 0) is easy... or is it? Be careful about negative edges!

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-42
SLIDE 42

Lemma

Let s = u0 → u1 → · · · → uk−1 → uk be a shortest s uk path. After Relax has been called on every edge of this path in

  • rder—u0 → u1, then u1 → u2, until uk−1 → uk, with arbitrarily many
  • ther calls interleaved—then dist(uk) = c∗(uk).

Proof.

Induct on k. Base case (k = 0) is easy... or is it? Be careful about negative edges! For the inductive step, assume it holds for all paths of length k − 1.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-43
SLIDE 43

Lemma

Let s = u0 → u1 → · · · → uk−1 → uk be a shortest s uk path. After Relax has been called on every edge of this path in

  • rder—u0 → u1, then u1 → u2, until uk−1 → uk, with arbitrarily many
  • ther calls interleaved—then dist(uk) = c∗(uk).

Proof.

Induct on k. Base case (k = 0) is easy... or is it? Be careful about negative edges! For the inductive step, assume it holds for all paths of length k − 1. So the last time Relax(uk−1 → uk) is called, dist(uk−1) = c∗(uk−1).

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-44
SLIDE 44

Lemma

Let s = u0 → u1 → · · · → uk−1 → uk be a shortest s uk path. After Relax has been called on every edge of this path in

  • rder—u0 → u1, then u1 → u2, until uk−1 → uk, with arbitrarily many
  • ther calls interleaved—then dist(uk) = c∗(uk).

Proof.

Induct on k. Base case (k = 0) is easy... or is it? Be careful about negative edges! For the inductive step, assume it holds for all paths of length k − 1. So the last time Relax(uk−1 → uk) is called, dist(uk−1) = c∗(uk−1). Therefore after this, dist(uk) ≤ c∗(uk−1) + w(uk−1 → uk).

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-45
SLIDE 45

Lemma

Let s = u0 → u1 → · · · → uk−1 → uk be a shortest s uk path. After Relax has been called on every edge of this path in

  • rder—u0 → u1, then u1 → u2, until uk−1 → uk, with arbitrarily many
  • ther calls interleaved—then dist(uk) = c∗(uk).

Proof.

Induct on k. Base case (k = 0) is easy... or is it? Be careful about negative edges! For the inductive step, assume it holds for all paths of length k − 1. So the last time Relax(uk−1 → uk) is called, dist(uk−1) = c∗(uk−1). Therefore after this, dist(uk) ≤ c∗(uk−1) + w(uk−1 → uk). Since u0 → u1 → · · · → uk−1 → uk is a shortest path, this RHS is c∗(uk).

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-46
SLIDE 46

Question for you all

Lemma

Let s = u0 → u1 → · · · → uk−1 → uk be a shortest s uk path. After Relax has been called on every edge of this path in

  • rder—u0 → u1, then u1 → u2, until uk−1 → uk, with arbitrarily many
  • ther calls interleaved—then dist(uk) = c∗(uk).

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-47
SLIDE 47

Question for you all

Lemma

Let s = u0 → u1 → · · · → uk−1 → uk be a shortest s uk path. After Relax has been called on every edge of this path in

  • rder—u0 → u1, then u1 → u2, until uk−1 → uk, with arbitrarily many
  • ther calls interleaved—then dist(uk) = c∗(uk).

What happens with negative edges?

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-48
SLIDE 48

Question for you all

Lemma

Let s = u0 → u1 → · · · → uk−1 → uk be a shortest s uk path. After Relax has been called on every edge of this path in

  • rder—u0 → u1, then u1 → u2, until uk−1 → uk, with arbitrarily many
  • ther calls interleaved—then dist(uk) = c∗(uk).

What happens with negative edges? What happens with negative cycles?

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-49
SLIDE 49

Back to the algorithm

InitializeSSSP(s):

◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0.

FordSSSP(s):

◮ InitializeSSSP(s) ◮ Repeat some number of times: ⋆ Pick an edge u → v (somehow) ⋆ Relax(u → v)

Relax(u → v):

◮ If dist(v) > dist(u) + w(u → v): ⋆ dist(v) ← dist(u) + w(u → v) ⋆ pred(v) ← u. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-50
SLIDE 50

Back to the algorithm

InitializeSSSP(s):

◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0.

FordSSSP(s):

◮ InitializeSSSP(s) ◮ Repeat some number of times: ⋆ Pick an edge u → v (somehow) ⋆ Relax(u → v)

Relax(u → v):

◮ If dist(v) > dist(u) + w(u → v): ⋆ dist(v) ← dist(u) + w(u → v) ⋆ pred(v) ← u. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-51
SLIDE 51

Back to the algorithm

InitializeSSSP(s):

◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0.

FordSSSP(s):

◮ InitializeSSSP(s) ◮ Repeat some number of times: ⋆ Pick an edge u → v (somehow) ⋆ Relax(u → v)

Relax(u → v):

◮ If dist(v) > dist(u) + w(u → v): ⋆ dist(v) ← dist(u) + w(u → v) ⋆ pred(v) ← u.

Lemma states: need to call Relax in order for every shortest path.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-52
SLIDE 52

Bellman-Ford Algorithm

Lemma states: need to call Relax in order for every shortest path.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-53
SLIDE 53

Bellman-Ford Algorithm

Lemma states: need to call Relax in order for every shortest path. Every shortest path has length at most V − 1.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-54
SLIDE 54

Bellman-Ford Algorithm

Lemma states: need to call Relax in order for every shortest path. Every shortest path has length at most V − 1. If we relax every edge, we’ll surely relax the first edge of the path.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-55
SLIDE 55

Bellman-Ford Algorithm

Lemma states: need to call Relax in order for every shortest path. Every shortest path has length at most V − 1. If we relax every edge, we’ll surely relax the first edge of the path. If we relax every edge again, we’ll get the second edge.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-56
SLIDE 56

Bellman-Ford Algorithm

Lemma states: need to call Relax in order for every shortest path. Every shortest path has length at most V − 1. If we relax every edge, we’ll surely relax the first edge of the path. If we relax every edge again, we’ll get the second edge. Do this V − 1 times.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-57
SLIDE 57

Bellman-Ford Algorithm

Lemma states: need to call Relax in order for every shortest path. Every shortest path has length at most V − 1. If we relax every edge, we’ll surely relax the first edge of the path. If we relax every edge again, we’ll get the second edge. Do this V − 1 times. BellmanFord(s):

◮ InitializeSSSP(s) ◮ Repeat V − 1 times: ⋆ For every edge u → v in E:

Relax(u → v)

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-58
SLIDE 58

Bellman-Ford Algorithm

Lemma states: need to call Relax in order for every shortest path. Every shortest path has length at most V − 1. If we relax every edge, we’ll surely relax the first edge of the path. If we relax every edge again, we’ll get the second edge. Do this V − 1 times. BellmanFord(s):

◮ InitializeSSSP(s) ◮ Repeat V − 1 times: ⋆ For every edge u → v in E:

Relax(u → v)

O(EV ) time for SSSP.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-59
SLIDE 59

Bellman-Ford Algorithm

Bellman-Ford solves SSSP in O(EV ) time. It works with negative edges. It’s the fastest known algorithm in general! Can use to find negative cycles:

◮ Repeat one more time. If no negative cycles, no edge should change in

the V th iteration.

◮ Follow the predecessor chain to find a negative cycle.

Can go faster if edge lengths nonnegative: Dijkstra’s algorithm.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-60
SLIDE 60

Talk Outline

1

Logistics

2

Shortest Paths: Bellman-Ford

3

Dijkstra’s Algorithm

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-61
SLIDE 61

Dijkstra’s Algorithm

Dijkstra(s):

◮ InitializeSSSP(s) ◮ Repeat V times: ⋆ Find the unvisited vertex u of minimal dist(u). ⋆ For every edge u → v out from u:

Relax(u → v)

Alternative view: WhateverFirstSearch that visits the nearest vertex to s. Another alternative view: a small variant on Prim’s algorithm.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-62
SLIDE 62

Dijkstra’s Algorithm

1: function Dijkstra(s) 2:

pred, dist ← {}, {}

3:

q ← PriorityQueue([(0, s, None)]) ⊲ dist, vertex, pred

4:

while q do

5:

d, u, parent ← q.pop()

6:

if u ∈ pred then

7:

continue

8:

pred[u] ← parent

9:

dist[u] ← d

10:

for u → v ∈ E do

11:

q.push( (dist[u] + w(u → v), v, u) )

12:

return dist, pred

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-63
SLIDE 63

Dijkstra’s Prim’s Algorithm

1: function Prim(s) 2:

pred, dist ← {}, {}

3:

q ← PriorityQueue([(0, s, None)]) ⊲ dist, vertex, pred

4:

while q do

5:

d, u, parent ← q.pop()

6:

if u ∈ pred then

7:

continue

8:

pred[u] ← parent

9:

dist[u] ← d

10:

for u → v ∈ E do

11:

q.push( (dist[u] + w(u → v), v, u) )

12:

return dist, pred

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-64
SLIDE 64

Dijkstra’s Algorithm

Just like Prim: visits each vertex once and scans through outgoing edges, so looks at each edge once.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-65
SLIDE 65

Dijkstra’s Algorithm

Just like Prim: visits each vertex once and scans through outgoing edges, so looks at each edge once.

◮ Time from E pushes/pops, for O(E log V ) with binary heap. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-66
SLIDE 66

Dijkstra’s Algorithm

Just like Prim: visits each vertex once and scans through outgoing edges, so looks at each edge once.

◮ Time from E pushes/pops, for O(E log V ) with binary heap. ◮ Modifying the algorithm slightly and using a Fibonacci heap can bring

this down to O(E + V log V ).

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-67
SLIDE 67

Dijkstra’s Algorithm

Just like Prim: visits each vertex once and scans through outgoing edges, so looks at each edge once.

◮ Time from E pushes/pops, for O(E log V ) with binary heap. ◮ Modifying the algorithm slightly and using a Fibonacci heap can bring

this down to O(E + V log V ).

Tricky part: correctness.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-68
SLIDE 68

Dijkstra’s Algorithm

Just like Prim: visits each vertex once and scans through outgoing edges, so looks at each edge once.

◮ Time from E pushes/pops, for O(E log V ) with binary heap. ◮ Modifying the algorithm slightly and using a Fibonacci heap can bring

this down to O(E + V log V ).

Tricky part: correctness. Need to argue: if edge weights nonnegative, for any shortest path, will visit the vertices in order.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-69
SLIDE 69

Dijkstra’s Algorithm

Just like Prim: visits each vertex once and scans through outgoing edges, so looks at each edge once.

◮ Time from E pushes/pops, for O(E log V ) with binary heap. ◮ Modifying the algorithm slightly and using a Fibonacci heap can bring

this down to O(E + V log V ).

Tricky part: correctness. Need to argue: if edge weights nonnegative, for any shortest path, will visit the vertices in order.

◮ Bellman-Ford relaxes each edge V times. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-70
SLIDE 70

Dijkstra’s Algorithm

Just like Prim: visits each vertex once and scans through outgoing edges, so looks at each edge once.

◮ Time from E pushes/pops, for O(E log V ) with binary heap. ◮ Modifying the algorithm slightly and using a Fibonacci heap can bring

this down to O(E + V log V ).

Tricky part: correctness. Need to argue: if edge weights nonnegative, for any shortest path, will visit the vertices in order.

◮ Bellman-Ford relaxes each edge V times. ◮ Dijkstra only relaxes each edge once, so it better happen at the right

time.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-71
SLIDE 71

Dijkstra’s Algorithm: Correctness

The distances d popped from the queue are nondecreasing.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-72
SLIDE 72

Dijkstra’s Algorithm: Correctness

The distances d popped from the queue are nondecreasing.

◮ At each step, values pushed aren’t smaller than the one just popped. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-73
SLIDE 73

Dijkstra’s Algorithm: Correctness

The distances d popped from the queue are nondecreasing.

◮ At each step, values pushed aren’t smaller than the one just popped.

Lemma

For any (not necessarily shortest) path s = v0 → v1 → . . . → vj of length Lj, then dist[vj] is at most Lj when it is set.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-74
SLIDE 74

Dijkstra’s Algorithm: Correctness

The distances d popped from the queue are nondecreasing.

◮ At each step, values pushed aren’t smaller than the one just popped.

Lemma

For any (not necessarily shortest) path s = v0 → v1 → . . . → vj of length Lj, then dist[vj] is at most Lj when it is set.

Proof.

Induct on j. For j = 0, trivially true. If true for j − 1, then dist[vj−1] ≤ Lj−1. So when vj−1 is visited, we will push (d, vj, vj−1) for d = dist[vj−1] + w(vj−1, vj) ≤ Lj−1 + w(vj−1, vj) = Lj

  • nto the queue. At some point this gets popped from the queue. Since

the distances popped are nondecreasing, the first time we pop vj from the queue it must also be with a distance at most Lj.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-75
SLIDE 75

Dijkstra’s Algorithm: Conclusion

Takes O(E + V log V ) time.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-76
SLIDE 76

Dijkstra’s Algorithm: Conclusion

Takes O(E + V log V ) time. Outputs the correct answer if all edge weights nonnegative.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-77
SLIDE 77

Dijkstra’s Algorithm: Conclusion

Takes O(E + V log V ) time. Outputs the correct answer if all edge weights nonnegative. Alternative version:

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-78
SLIDE 78

Dijkstra’s Algorithm: Conclusion

Takes O(E + V log V ) time. Outputs the correct answer if all edge weights nonnegative. Alternative version:

◮ Outputs the correct answer always. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-79
SLIDE 79

Dijkstra’s Algorithm: Conclusion

Takes O(E + V log V ) time. Outputs the correct answer if all edge weights nonnegative. Alternative version:

◮ Outputs the correct answer always. ◮ Takes O(E + V log V ) time if all edge weights nonnegative. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-80
SLIDE 80

Dijkstra’s Algorithm: Conclusion

Takes O(E + V log V ) time. Outputs the correct answer if all edge weights nonnegative. Alternative version:

◮ Outputs the correct answer always. ◮ Takes O(E + V log V ) time if all edge weights nonnegative. ◮ Exponential time in general. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-81
SLIDE 81

Alternative Dijkstra: correct but slow with negative weights

1: function Dijkstra(s) 2:

pred, dist ← {}, {}

3:

q ← PriorityQueue([(0, s, None)]) ⊲ dist, vertex, pred

4:

while q do

5:

d, u, parent ← q.pop()

6:

if u ∈ pred then

7:

continue

8:

pred[u] ← parent

9:

dist[u] ← d

10:

for u → v ∈ E do

11:

q.push( (dist[u] + w(u → v), v, u) )

12:

return dist, pred

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-82
SLIDE 82

Alternative Dijkstra: correct but slow with negative weights

1: function Dijkstra(s) 2:

pred, dist ← {}, {}

3:

q ← PriorityQueue([(0, s, None)]) ⊲ dist, vertex, pred

4:

while q do

5:

d, u, parent ← q.pop()

6:

if d ≥ dist[u] then

7:

continue

8:

pred[u] ← parent

9:

dist[u] ← d

10:

for u → v ∈ E do

11:

q.push( (dist[u] + w(u → v), v, u) )

12:

return dist, pred

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-83
SLIDE 83

Summary of shortest paths

DAGs: DP for O(E) time.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-84
SLIDE 84

Summary of shortest paths

DAGs: DP for O(E) time. Unweighted graphs: BFS for O(E) time.

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-85
SLIDE 85

Summary of shortest paths

DAGs: DP for O(E) time. Unweighted graphs: BFS for O(E) time. Bellman-Ford: O(EV ) time

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-86
SLIDE 86

Summary of shortest paths

DAGs: DP for O(E) time. Unweighted graphs: BFS for O(E) time. Bellman-Ford: O(EV ) time

◮ Works with negative edge weights Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-87
SLIDE 87

Summary of shortest paths

DAGs: DP for O(E) time. Unweighted graphs: BFS for O(E) time. Bellman-Ford: O(EV ) time

◮ Works with negative edge weights ◮ Can detect cycles Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-88
SLIDE 88

Summary of shortest paths

DAGs: DP for O(E) time. Unweighted graphs: BFS for O(E) time. Bellman-Ford: O(EV ) time

◮ Works with negative edge weights ◮ Can detect cycles

Dijkstra: O(E + V log V ) time

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-89
SLIDE 89

Summary of shortest paths

DAGs: DP for O(E) time. Unweighted graphs: BFS for O(E) time. Bellman-Ford: O(EV ) time

◮ Works with negative edge weights ◮ Can detect cycles

Dijkstra: O(E + V log V ) time

◮ but only with nonnegative edge weights Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-90
SLIDE 90

Summary of shortest paths

DAGs: DP for O(E) time. Unweighted graphs: BFS for O(E) time. Bellman-Ford: O(EV ) time

◮ Works with negative edge weights ◮ Can detect cycles

Dijkstra: O(E + V log V ) time

◮ but only with nonnegative edge weights ◮ either wrong or exponential time with negative edges Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-91
SLIDE 91

Summary of shortest paths

DAGs: DP for O(E) time. Unweighted graphs: BFS for O(E) time. Bellman-Ford: O(EV ) time

◮ Works with negative edge weights ◮ Can detect cycles

Dijkstra: O(E + V log V ) time

◮ but only with nonnegative edge weights ◮ either wrong or exponential time with negative edges

Next class:

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-92
SLIDE 92

Summary of shortest paths

DAGs: DP for O(E) time. Unweighted graphs: BFS for O(E) time. Bellman-Ford: O(EV ) time

◮ Works with negative edge weights ◮ Can detect cycles

Dijkstra: O(E + V log V ) time

◮ but only with nonnegative edge weights ◮ either wrong or exponential time with negative edges

Next class:

◮ A∗ search: Dijkstra with a twist Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-93
SLIDE 93

Summary of shortest paths

DAGs: DP for O(E) time. Unweighted graphs: BFS for O(E) time. Bellman-Ford: O(EV ) time

◮ Works with negative edge weights ◮ Can detect cycles

Dijkstra: O(E + V log V ) time

◮ but only with nonnegative edge weights ◮ either wrong or exponential time with negative edges

Next class:

◮ A∗ search: Dijkstra with a twist ◮ Exercises Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-94
SLIDE 94

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24

slide-95
SLIDE 95

Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24