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 Dijkstra’s Algorithm 3 CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
Talk Outline Logistics 1 Shortest Paths: Bellman-Ford 2 Dijkstra’s Algorithm 3 CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
Welcome to 331 online! I’ll be experimenting with different teaching methods. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
Welcome to 331 online! I’ll be experimenting with different teaching methods. ◮ Bear with me. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
Welcome to 331 online! I’ll be experimenting with different teaching methods. ◮ Bear with me. ◮ Give feedback! CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
Welcome to 331 online! I’ll be experimenting with different teaching methods. ◮ Bear with me. ◮ Give feedback! Stay healthy CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
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. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
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. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
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 CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
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. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
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. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
Zoom details Mute your audio unless you have a question. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
Zoom details Mute your audio unless you have a question. I encourage sharing your video ◮ Makes this seem more like a community CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
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. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
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. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
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. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
Talk Outline Logistics 1 Shortest Paths: Bellman-Ford 2 Dijkstra’s Algorithm 3 CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
Single-Source Shortest Paths Problem setup: CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
Single-Source Shortest Paths Problem setup: ◮ Given a directed graph G = ( V , E ) CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
Single-Source Shortest Paths Problem setup: ◮ Given a directed graph G = ( V , E ) ◮ Each edge u → v has distance : w ( u → v ) ∈ R CS 331, Spring Eric Price (UT Austin) Shortest Paths / 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. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 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 CS 331, Spring Eric Price (UT Austin) Shortest Paths / 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 . CS 331, Spring Eric Price (UT Austin) Shortest Paths / 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 . Output two arrays: dist() and pred(). CS 331, Spring Eric Price (UT Austin) Shortest Paths / 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 . Output two arrays: dist() and pred(). ◮ dist( v ) = c ∗ ( v ) for all v CS 331, Spring Eric Price (UT Austin) Shortest Paths / 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 . Output two arrays: dist() and pred(). ◮ dist( v ) = c ∗ ( v ) for all v ◮ pred( s ) = None CS 331, Spring Eric Price (UT Austin) Shortest Paths / 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 . 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. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 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 . 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 ? CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
Generic SSSP algorithm We maintain a vector dist that satisfies the invariant: dist( v ) ≥ c ∗ ( v ) for all v at all times. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
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. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
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. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
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. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
Generic SSSP algorithm Invariant: dist( v ) ≥ c ∗ ( v ) for all v at all times. CS 331, Spring Eric Price (UT Austin) Shortest Paths / 24
Recommend
More recommend