eric price - university of texas at austinecprice/courses/331/slides/03-30.pdf · welcome to 331...

95
Shortest Paths Eric Price UT Austin CS 331, Spring 2020 Coronavirus Edition Eric Price (UT Austin) Shortest Paths CS 331, Sprin / 24

Upload: others

Post on 27-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Shortest Paths

Eric Price

UT Austin

CS 331, Spring 2020 Coronavirus Edition

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 1

/ 24

Page 2: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Talk Outline

1 Logistics

2 Shortest Paths: Bellman-Ford

3 Dijkstra’s Algorithm

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 2

/ 24

Page 3: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Talk Outline

1 Logistics

2 Shortest Paths: Bellman-Ford

3 Dijkstra’s Algorithm

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 3

/ 24

Page 4: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Welcome to 331 online!

I’ll be experimenting with different teaching methods.

I Bear with me.I Give feedback!

Stay healthy

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

Second exam will be take-home

I Given out after class on April 8.I Return before 10am on Friday, April 10.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 4

/ 24

Page 5: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Welcome to 331 online!

I’ll be experimenting with different teaching methods.I Bear with me.

I Give feedback!

Stay healthy

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

Second exam will be take-home

I Given out after class on April 8.I Return before 10am on Friday, April 10.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 4

/ 24

Page 6: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Welcome to 331 online!

I’ll be experimenting with different teaching methods.I Bear with me.I Give feedback!

Stay healthy

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

Second exam will be take-home

I Given out after class on April 8.I Return before 10am on Friday, April 10.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 4

/ 24

Page 7: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Welcome to 331 online!

I’ll be experimenting with different teaching methods.I Bear with me.I Give feedback!

Stay healthy

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

Second exam will be take-home

I Given out after class on April 8.I Return before 10am on Friday, April 10.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 4

/ 24

Page 8: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Welcome to 331 online!

I’ll be experimenting with different teaching methods.I Bear with me.I Give feedback!

Stay healthyI Let me know if you’re sick & need extra time.

I Please wear a mask if you interact with others.

Second exam will be take-home

I Given out after class on April 8.I Return before 10am on Friday, April 10.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 4

/ 24

Page 9: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Welcome to 331 online!

I’ll be experimenting with different teaching methods.I Bear with me.I Give feedback!

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

Second exam will be take-home

I Given out after class on April 8.I Return before 10am on Friday, April 10.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 4

/ 24

Page 10: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Welcome to 331 online!

I’ll be experimenting with different teaching methods.I Bear with me.I Give feedback!

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

Second exam will be take-home

I Given out after class on April 8.I Return before 10am on Friday, April 10.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 4

/ 24

Page 11: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Welcome to 331 online!

I’ll be experimenting with different teaching methods.I Bear with me.I Give feedback!

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

Second exam will be take-homeI Given out after class on April 8.

I Return before 10am on Friday, April 10.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 4

/ 24

Page 12: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Welcome to 331 online!

I’ll be experimenting with different teaching methods.I Bear with me.I Give feedback!

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

Second exam will be take-homeI Given out after class on April 8.I Return before 10am on Friday, April 10.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 4

/ 24

Page 13: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Zoom details

Mute your audio unless you have a question.

I encourage sharing your video

I Makes this seem more like a community

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

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

Videos will be recorded & available after class.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 5

/ 24

Page 14: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Zoom details

Mute your audio unless you have a question.

I encourage sharing your videoI Makes this seem more like a community

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

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

Videos will be recorded & available after class.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 5

/ 24

Page 15: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Zoom details

Mute your audio unless you have a question.

I encourage sharing your videoI Makes this seem more like a community

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

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

Videos will be recorded & available after class.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 5

/ 24

Page 16: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Zoom details

Mute your audio unless you have a question.

I encourage sharing your videoI Makes this seem more like a community

You can “raise your hand” so I know to call on you.I Press “View Participants”, then the little blue hand.

Videos will be recorded & available after class.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 5

/ 24

Page 17: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Zoom details

Mute your audio unless you have a question.

I encourage sharing your videoI Makes this seem more like a community

You can “raise your hand” so I know to call on you.I Press “View Participants”, then the little blue hand.

Videos will be recorded & available after class.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 5

/ 24

Page 18: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Talk Outline

1 Logistics

2 Shortest Paths: Bellman-Ford

3 Dijkstra’s Algorithm

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 6

/ 24

Page 19: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Single-Source Shortest Paths

Problem setup:

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

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

Output two arrays: dist() and pred().

I dist(v) = c∗(v) for all vI pred(s) = NoneI 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 PathsCS 331, Spring 2020 Coronavirus Edition 7

/ 24

Page 20: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Single-Source Shortest Paths

Problem setup:I Given a directed graph G = (V ,E )

I Each edge u → v has distance: w(u → v) ∈ RI Distance of path is sum of distance of edges.I Given a source s

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

Output two arrays: dist() and pred().

I dist(v) = c∗(v) for all vI pred(s) = NoneI 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 PathsCS 331, Spring 2020 Coronavirus Edition 7

/ 24

Page 21: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Single-Source Shortest Paths

Problem setup:I Given a directed graph G = (V ,E )I Each edge u → v has distance: w(u → v) ∈ R

I Distance of path is sum of distance of edges.I Given a source s

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

Output two arrays: dist() and pred().

I dist(v) = c∗(v) for all vI pred(s) = NoneI 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 PathsCS 331, Spring 2020 Coronavirus Edition 7

/ 24

Page 22: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Single-Source Shortest Paths

Problem setup:I Given a directed graph G = (V ,E )I Each edge u → v has distance: w(u → v) ∈ RI Distance of path is sum of distance of edges.

I Given a source s

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

Output two arrays: dist() and pred().

I dist(v) = c∗(v) for all vI pred(s) = NoneI 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 PathsCS 331, Spring 2020 Coronavirus Edition 7

/ 24

Page 23: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Single-Source Shortest Paths

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

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

Output two arrays: dist() and pred().

I dist(v) = c∗(v) for all vI pred(s) = NoneI 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 PathsCS 331, Spring 2020 Coronavirus Edition 7

/ 24

Page 24: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Single-Source Shortest Paths

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

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

Output two arrays: dist() and pred().

I dist(v) = c∗(v) for all vI pred(s) = NoneI 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 PathsCS 331, Spring 2020 Coronavirus Edition 7

/ 24

Page 25: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Single-Source Shortest Paths

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

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

Output two arrays: dist() and pred().

I dist(v) = c∗(v) for all vI pred(s) = NoneI 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 PathsCS 331, Spring 2020 Coronavirus Edition 7

/ 24

Page 26: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Single-Source Shortest Paths

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

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

Output two arrays: dist() and pred().I dist(v) = c∗(v) for all v

I pred(s) = NoneI 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 PathsCS 331, Spring 2020 Coronavirus Edition 7

/ 24

Page 27: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Single-Source Shortest Paths

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

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

Output two arrays: dist() and pred().I dist(v) = c∗(v) for all vI pred(s) = None

I 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 PathsCS 331, Spring 2020 Coronavirus Edition 7

/ 24

Page 28: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Single-Source Shortest Paths

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

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

Output two arrays: dist() and pred().I dist(v) = c∗(v) for all vI pred(s) = NoneI 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 PathsCS 331, Spring 2020 Coronavirus Edition 7

/ 24

Page 29: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Single-Source Shortest Paths

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

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

Output two arrays: dist() and pred().I dist(v) = c∗(v) for all vI pred(s) = NoneI 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 PathsCS 331, Spring 2020 Coronavirus Edition 7

/ 24

Page 30: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Generic SSSP algorithm

We maintain a vector dist that satisfies the invariant:

dist(v) ≥ c∗(v)

for all v at all times.

InitializeSSSP(s):

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

FordSSSP(s):

I InitializeSSSP(s)I Repeat:

F Pick an edgeF If it is “tense”, relax it.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 8

/ 24

Page 31: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Generic SSSP algorithm

We maintain a vector dist that satisfies the invariant:

dist(v) ≥ c∗(v)

for all v at all times.

InitializeSSSP(s):I dist(v) =∞ ∀vI pred(v) = None ∀vI dist(s) = 0.

FordSSSP(s):

I InitializeSSSP(s)I Repeat:

F Pick an edgeF If it is “tense”, relax it.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 8

/ 24

Page 32: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Generic SSSP algorithm

We maintain a vector dist that satisfies the invariant:

dist(v) ≥ c∗(v)

for all v at all times.

InitializeSSSP(s):I dist(v) =∞ ∀vI pred(v) = None ∀vI dist(s) = 0.

FordSSSP(s):I InitializeSSSP(s)I Repeat:

F Pick an edgeF If it is “tense”, relax it.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 8

/ 24

Page 33: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Relaxing an edge

Relax(u → v):I If dist(v) > dist(u) + w(u → v):

F dist(v)← dist(u) + w(u → v)F 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 PathsCS 331, Spring 2020 Coronavirus Edition 9

/ 24

Page 34: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Generic SSSP algorithm

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

InitializeSSSP(s):

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

FordSSSP(s):

I InitializeSSSP(s)I Repeat some number of times:

F Pick an edge u → v (somehow)F Relax(u → v)

Relax(u → v):

I If dist(v) > dist(u) + w(u → v):

F dist(v)← dist(u) + w(u → v)F pred(v)← u.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 10

/ 24

Page 35: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Generic SSSP algorithm

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

InitializeSSSP(s):I dist(v) =∞ ∀vI pred(v) = None ∀vI dist(s) = 0.

FordSSSP(s):

I InitializeSSSP(s)I Repeat some number of times:

F Pick an edge u → v (somehow)F Relax(u → v)

Relax(u → v):

I If dist(v) > dist(u) + w(u → v):

F dist(v)← dist(u) + w(u → v)F pred(v)← u.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 10

/ 24

Page 36: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Generic SSSP algorithm

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

InitializeSSSP(s):I dist(v) =∞ ∀vI pred(v) = None ∀vI dist(s) = 0.

FordSSSP(s):I InitializeSSSP(s)I Repeat some number of times:

F Pick an edge u → v (somehow)F Relax(u → v)

Relax(u → v):

I If dist(v) > dist(u) + w(u → v):

F dist(v)← dist(u) + w(u → v)F pred(v)← u.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 10

/ 24

Page 37: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Generic SSSP algorithm

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

InitializeSSSP(s):I dist(v) =∞ ∀vI pred(v) = None ∀vI dist(s) = 0.

FordSSSP(s):I InitializeSSSP(s)I Repeat some number of times:

F Pick an edge u → v (somehow)F Relax(u → v)

Relax(u → v):I If dist(v) > dist(u) + w(u → v):

F dist(v)← dist(u) + w(u → v)F pred(v)← u.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 10

/ 24

Page 38: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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 inorder—u0 → u1, then u1 → u2, until uk−1 → uk , with arbitrarily manyother calls interleaved—then dist(uk) = c∗(uk).Moreover, uk ← pred(uk)← pred(pred(uk))← · · · ← s is a shortests uk path.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 11

/ 24

Page 39: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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 inorder—u0 → u1, then u1 → u2, until uk−1 → uk , with arbitrarily manyother calls interleaved—then dist(uk) = c∗(uk).

Proof.

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

.. or is it?

Be careful aboutnegative edges!

For the inductive step, assume it holds for all paths of length k − 1. Sothe 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 isc∗(uk).

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 12

/ 24

Page 40: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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 inorder—u0 → u1, then u1 → u2, until uk−1 → uk , with arbitrarily manyother calls interleaved—then dist(uk) = c∗(uk).

Proof.

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

Be careful aboutnegative edges!For the inductive step, assume it holds for all paths of length k − 1. Sothe 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 isc∗(uk).

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 12

/ 24

Page 41: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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 inorder—u0 → u1, then u1 → u2, until uk−1 → uk , with arbitrarily manyother calls interleaved—then dist(uk) = c∗(uk).

Proof.

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

For the inductive step, assume it holds for all paths of length k − 1. Sothe 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 isc∗(uk).

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 12

/ 24

Page 42: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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 inorder—u0 → u1, then u1 → u2, until uk−1 → uk , with arbitrarily manyother calls interleaved—then dist(uk) = c∗(uk).

Proof.

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

Sothe 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 isc∗(uk).

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 12

/ 24

Page 43: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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 inorder—u0 → u1, then u1 → u2, until uk−1 → uk , with arbitrarily manyother calls interleaved—then dist(uk) = c∗(uk).

Proof.

Induct on k . Base case (k = 0) is easy... or is it? Be careful aboutnegative edges!For the inductive step, assume it holds for all paths of length k − 1. Sothe 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 isc∗(uk).

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 12

/ 24

Page 44: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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 inorder—u0 → u1, then u1 → u2, until uk−1 → uk , with arbitrarily manyother calls interleaved—then dist(uk) = c∗(uk).

Proof.

Induct on k . Base case (k = 0) is easy... or is it? Be careful aboutnegative edges!For the inductive step, assume it holds for all paths of length k − 1. Sothe 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 isc∗(uk).

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 12

/ 24

Page 45: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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 inorder—u0 → u1, then u1 → u2, until uk−1 → uk , with arbitrarily manyother calls interleaved—then dist(uk) = c∗(uk).

Proof.

Induct on k . Base case (k = 0) is easy... or is it? Be careful aboutnegative edges!For the inductive step, assume it holds for all paths of length k − 1. Sothe 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 isc∗(uk).

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 12

/ 24

Page 46: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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 inorder—u0 → u1, then u1 → u2, until uk−1 → uk , with arbitrarily manyother calls interleaved—then dist(uk) = c∗(uk).

What happens with negative edges?

What happens with negative cycles?

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 13

/ 24

Page 47: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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 inorder—u0 → u1, then u1 → u2, until uk−1 → uk , with arbitrarily manyother calls interleaved—then dist(uk) = c∗(uk).

What happens with negative edges?

What happens with negative cycles?

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 13

/ 24

Page 48: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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 inorder—u0 → u1, then u1 → u2, until uk−1 → uk , with arbitrarily manyother calls interleaved—then dist(uk) = c∗(uk).

What happens with negative edges?

What happens with negative cycles?

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 13

/ 24

Page 49: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Back to the algorithm

InitializeSSSP(s):I dist(v) =∞ ∀vI pred(v) = None ∀vI dist(s) = 0.

FordSSSP(s):I InitializeSSSP(s)I Repeat some number of times:

F Pick an edge u → v (somehow)F Relax(u → v)

Relax(u → v):I If dist(v) > dist(u) + w(u → v):

F dist(v)← dist(u) + w(u → v)F pred(v)← u.

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

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 14

/ 24

Page 50: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Back to the algorithm

InitializeSSSP(s):I dist(v) =∞ ∀vI pred(v) = None ∀vI dist(s) = 0.

FordSSSP(s):I InitializeSSSP(s)I Repeat some number of times:

F Pick an edge u → v (somehow)F Relax(u → v)

Relax(u → v):I If dist(v) > dist(u) + w(u → v):

F dist(v)← dist(u) + w(u → v)F pred(v)← u.

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

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 14

/ 24

Page 51: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Back to the algorithm

InitializeSSSP(s):I dist(v) =∞ ∀vI pred(v) = None ∀vI dist(s) = 0.

FordSSSP(s):I InitializeSSSP(s)I Repeat some number of times:

F Pick an edge u → v (somehow)F Relax(u → v)

Relax(u → v):I If dist(v) > dist(u) + w(u → v):

F dist(v)← dist(u) + w(u → v)F pred(v)← u.

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

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 14

/ 24

Page 52: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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):

I InitializeSSSP(s)I Repeat V − 1 times:

F For every edge u → v in E :Relax(u → v)

O(EV ) time for SSSP.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 15

/ 24

Page 53: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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):

I InitializeSSSP(s)I Repeat V − 1 times:

F For every edge u → v in E :Relax(u → v)

O(EV ) time for SSSP.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 15

/ 24

Page 54: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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):

I InitializeSSSP(s)I Repeat V − 1 times:

F For every edge u → v in E :Relax(u → v)

O(EV ) time for SSSP.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 15

/ 24

Page 55: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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):

I InitializeSSSP(s)I Repeat V − 1 times:

F For every edge u → v in E :Relax(u → v)

O(EV ) time for SSSP.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 15

/ 24

Page 56: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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):

I InitializeSSSP(s)I Repeat V − 1 times:

F For every edge u → v in E :Relax(u → v)

O(EV ) time for SSSP.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 15

/ 24

Page 57: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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):I InitializeSSSP(s)I Repeat V − 1 times:

F For every edge u → v in E :Relax(u → v)

O(EV ) time for SSSP.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 15

/ 24

Page 58: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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):I InitializeSSSP(s)I Repeat V − 1 times:

F For every edge u → v in E :Relax(u → v)

O(EV ) time for SSSP.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 15

/ 24

Page 59: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

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:I Repeat one more time. If no negative cycles, no edge should change in

the V th iteration.I Follow the predecessor chain to find a negative cycle.

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

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 16

/ 24

Page 60: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Talk Outline

1 Logistics

2 Shortest Paths: Bellman-Ford

3 Dijkstra’s Algorithm

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 17

/ 24

Page 61: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm

Dijkstra(s):I InitializeSSSP(s)I Repeat V times:

F Find the unvisited vertex u of minimal dist(u).F For every edge u → v out from u:

Relax(u → v)

Alternative view: WhateverFirstSearch that visits the nearestvertex to s.

Another alternative view: a small variant on Prim’s algorithm.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 18

/ 24

Page 62: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm

1: function Dijkstra(s)2: pred, dist ← {}, {}3: q ← PriorityQueue([(0, s, None)]) . dist, vertex, pred4: while q do5: d, u, parent ← q.pop()6: if u ∈ pred then7: continue8: pred[u] ← parent9: dist[u] ← d

10: for u → v ∈ E do11: q.push( (dist[u] + w(u → v), v , u) )

12: return dist, pred

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 19

/ 24

Page 63: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Prim’s Algorithm

1: function Prim(s)2: pred, dist ← {}, {}3: q ← PriorityQueue([(0, s, None)]) . dist, vertex, pred4: while q do5: d, u, parent ← q.pop()6: if u ∈ pred then7: continue8: pred[u] ← parent9: dist[u] ← d

10: for u → v ∈ E do11: q.push( (dist[u] + w(u → v), v , u) )

12: return dist, pred

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 19

/ 24

Page 64: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm

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

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

this down to O(E + V logV ).

Tricky part: correctness.

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

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

time.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 20

/ 24

Page 65: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm

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

I Time from E pushes/pops, for O(E logV ) with binary heap.

I Modifying the algorithm slightly and using a Fibonacci heap can bringthis down to O(E + V logV ).

Tricky part: correctness.

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

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

time.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 20

/ 24

Page 66: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm

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

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

this down to O(E + V logV ).

Tricky part: correctness.

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

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

time.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 20

/ 24

Page 67: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm

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

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

this down to O(E + V logV ).

Tricky part: correctness.

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

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

time.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 20

/ 24

Page 68: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm

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

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

this down to O(E + V logV ).

Tricky part: correctness.

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

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

time.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 20

/ 24

Page 69: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm

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

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

this down to O(E + V logV ).

Tricky part: correctness.

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

I Bellman-Ford relaxes each edge V times.

I Dijkstra only relaxes each edge once, so it better happen at the righttime.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 20

/ 24

Page 70: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm

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

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

this down to O(E + V logV ).

Tricky part: correctness.

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

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

time.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 20

/ 24

Page 71: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm: CorrectnessThe distances d popped from the queue are nondecreasing.

I 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 lengthLj , 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 willpush (d , vj , vj−1) for

d = dist[vj−1] + w(vj−1, vj) ≤ Lj−1 + w(vj−1, vj) = Lj

onto the queue. At some point this gets popped from the queue. Sincethe distances popped are nondecreasing, the first time we pop vj from thequeue it must also be with a distance at most Lj .

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 21

/ 24

Page 72: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm: CorrectnessThe distances d popped from the queue are nondecreasing.

I 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 lengthLj , 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 willpush (d , vj , vj−1) for

d = dist[vj−1] + w(vj−1, vj) ≤ Lj−1 + w(vj−1, vj) = Lj

onto the queue. At some point this gets popped from the queue. Sincethe distances popped are nondecreasing, the first time we pop vj from thequeue it must also be with a distance at most Lj .

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 21

/ 24

Page 73: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm: CorrectnessThe distances d popped from the queue are nondecreasing.

I 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 lengthLj , 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 willpush (d , vj , vj−1) for

d = dist[vj−1] + w(vj−1, vj) ≤ Lj−1 + w(vj−1, vj) = Lj

onto the queue. At some point this gets popped from the queue. Sincethe distances popped are nondecreasing, the first time we pop vj from thequeue it must also be with a distance at most Lj .

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 21

/ 24

Page 74: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm: CorrectnessThe distances d popped from the queue are nondecreasing.

I 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 lengthLj , 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 willpush (d , vj , vj−1) for

d = dist[vj−1] + w(vj−1, vj) ≤ Lj−1 + w(vj−1, vj) = Lj

onto the queue. At some point this gets popped from the queue. Sincethe distances popped are nondecreasing, the first time we pop vj from thequeue it must also be with a distance at most Lj .

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 21

/ 24

Page 75: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm: Conclusion

Takes O(E + V logV ) time.

Outputs the correct answer if all edge weights nonnegative.

Alternative version:

I Outputs the correct answer always.I Takes O(E + V logV ) time if all edge weights nonnegative.I Exponential time in general.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 22

/ 24

Page 76: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm: Conclusion

Takes O(E + V logV ) time.

Outputs the correct answer if all edge weights nonnegative.

Alternative version:

I Outputs the correct answer always.I Takes O(E + V logV ) time if all edge weights nonnegative.I Exponential time in general.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 22

/ 24

Page 77: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm: Conclusion

Takes O(E + V logV ) time.

Outputs the correct answer if all edge weights nonnegative.

Alternative version:

I Outputs the correct answer always.I Takes O(E + V logV ) time if all edge weights nonnegative.I Exponential time in general.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 22

/ 24

Page 78: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm: Conclusion

Takes O(E + V logV ) time.

Outputs the correct answer if all edge weights nonnegative.

Alternative version:I Outputs the correct answer always.

I Takes O(E + V logV ) time if all edge weights nonnegative.I Exponential time in general.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 22

/ 24

Page 79: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm: Conclusion

Takes O(E + V logV ) time.

Outputs the correct answer if all edge weights nonnegative.

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

I Exponential time in general.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 22

/ 24

Page 80: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Dijkstra’s Algorithm: Conclusion

Takes O(E + V logV ) time.

Outputs the correct answer if all edge weights nonnegative.

Alternative version:I Outputs the correct answer always.I Takes O(E + V logV ) time if all edge weights nonnegative.I Exponential time in general.

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 22

/ 24

Page 81: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Alternative Dijkstra: correct but slow with negative weights

1: function Dijkstra(s)2: pred, dist ← {}, {}3: q ← PriorityQueue([(0, s, None)]) . dist, vertex, pred4: while q do5: d, u, parent ← q.pop()6: if u ∈ pred then7: continue8: pred[u] ← parent9: dist[u] ← d

10: for u → v ∈ E do11: q.push( (dist[u] + w(u → v), v , u) )

12: return dist, pred

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 23

/ 24

Page 82: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Alternative Dijkstra: correct but slow with negative weights

1: function Dijkstra(s)2: pred, dist ← {}, {}3: q ← PriorityQueue([(0, s, None)]) . dist, vertex, pred4: while q do5: d, u, parent ← q.pop()6: if d ≥ dist[u] then7: continue8: pred[u] ← parent9: dist[u] ← d

10: for u → v ∈ E do11: q.push( (dist[u] + w(u → v), v , u) )

12: return dist, pred

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 23

/ 24

Page 83: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Summary of shortest paths

DAGs: DP for O(E ) time.

Unweighted graphs: BFS for O(E ) time.

Bellman-Ford: O(EV ) time

I Works with negative edge weightsI Can detect cycles

Dijkstra: O(E + V logV ) time

I but only with nonnegative edge weightsI either wrong or exponential time with negative edges

Next class:

I A∗ search: Dijkstra with a twistI Exercises

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 24

/ 24

Page 84: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Summary of shortest paths

DAGs: DP for O(E ) time.

Unweighted graphs: BFS for O(E ) time.

Bellman-Ford: O(EV ) time

I Works with negative edge weightsI Can detect cycles

Dijkstra: O(E + V logV ) time

I but only with nonnegative edge weightsI either wrong or exponential time with negative edges

Next class:

I A∗ search: Dijkstra with a twistI Exercises

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 24

/ 24

Page 85: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Summary of shortest paths

DAGs: DP for O(E ) time.

Unweighted graphs: BFS for O(E ) time.

Bellman-Ford: O(EV ) time

I Works with negative edge weightsI Can detect cycles

Dijkstra: O(E + V logV ) time

I but only with nonnegative edge weightsI either wrong or exponential time with negative edges

Next class:

I A∗ search: Dijkstra with a twistI Exercises

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 24

/ 24

Page 86: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Summary of shortest paths

DAGs: DP for O(E ) time.

Unweighted graphs: BFS for O(E ) time.

Bellman-Ford: O(EV ) timeI Works with negative edge weights

I Can detect cycles

Dijkstra: O(E + V logV ) time

I but only with nonnegative edge weightsI either wrong or exponential time with negative edges

Next class:

I A∗ search: Dijkstra with a twistI Exercises

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 24

/ 24

Page 87: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Summary of shortest paths

DAGs: DP for O(E ) time.

Unweighted graphs: BFS for O(E ) time.

Bellman-Ford: O(EV ) timeI Works with negative edge weightsI Can detect cycles

Dijkstra: O(E + V logV ) time

I but only with nonnegative edge weightsI either wrong or exponential time with negative edges

Next class:

I A∗ search: Dijkstra with a twistI Exercises

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 24

/ 24

Page 88: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Summary of shortest paths

DAGs: DP for O(E ) time.

Unweighted graphs: BFS for O(E ) time.

Bellman-Ford: O(EV ) timeI Works with negative edge weightsI Can detect cycles

Dijkstra: O(E + V logV ) time

I but only with nonnegative edge weightsI either wrong or exponential time with negative edges

Next class:

I A∗ search: Dijkstra with a twistI Exercises

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 24

/ 24

Page 89: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Summary of shortest paths

DAGs: DP for O(E ) time.

Unweighted graphs: BFS for O(E ) time.

Bellman-Ford: O(EV ) timeI Works with negative edge weightsI Can detect cycles

Dijkstra: O(E + V logV ) timeI but only with nonnegative edge weights

I either wrong or exponential time with negative edges

Next class:

I A∗ search: Dijkstra with a twistI Exercises

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 24

/ 24

Page 90: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Summary of shortest paths

DAGs: DP for O(E ) time.

Unweighted graphs: BFS for O(E ) time.

Bellman-Ford: O(EV ) timeI Works with negative edge weightsI Can detect cycles

Dijkstra: O(E + V logV ) timeI but only with nonnegative edge weightsI either wrong or exponential time with negative edges

Next class:

I A∗ search: Dijkstra with a twistI Exercises

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 24

/ 24

Page 91: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Summary of shortest paths

DAGs: DP for O(E ) time.

Unweighted graphs: BFS for O(E ) time.

Bellman-Ford: O(EV ) timeI Works with negative edge weightsI Can detect cycles

Dijkstra: O(E + V logV ) timeI but only with nonnegative edge weightsI either wrong or exponential time with negative edges

Next class:

I A∗ search: Dijkstra with a twistI Exercises

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 24

/ 24

Page 92: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Summary of shortest paths

DAGs: DP for O(E ) time.

Unweighted graphs: BFS for O(E ) time.

Bellman-Ford: O(EV ) timeI Works with negative edge weightsI Can detect cycles

Dijkstra: O(E + V logV ) timeI but only with nonnegative edge weightsI either wrong or exponential time with negative edges

Next class:I A∗ search: Dijkstra with a twist

I Exercises

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 24

/ 24

Page 93: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Summary of shortest paths

DAGs: DP for O(E ) time.

Unweighted graphs: BFS for O(E ) time.

Bellman-Ford: O(EV ) timeI Works with negative edge weightsI Can detect cycles

Dijkstra: O(E + V logV ) timeI but only with nonnegative edge weightsI either wrong or exponential time with negative edges

Next class:I A∗ search: Dijkstra with a twistI Exercises

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 24

/ 24

Page 94: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 25

/ 24

Page 95: Eric Price - University of Texas at Austinecprice/courses/331/slides/03-30.pdf · Welcome to 331 online! I’ll be experimenting with di erent teaching methods. I Bear with me. I

Eric Price (UT Austin) Shortest PathsCS 331, Spring 2020 Coronavirus Edition 26

/ 24