Shortest Paths
Eric Price
UT Austin
CS 331, Spring 2020 Coronavirus Edition
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
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
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Bear with me. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Bear with me. ◮ Give feedback! Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Bear with me. ◮ Give feedback!
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Bear with me. ◮ Give feedback!
◮ Let me know if you’re sick & need extra time. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Bear with me. ◮ Give feedback!
◮ 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
◮ Bear with me. ◮ Give feedback!
◮ 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
◮ Bear with me. ◮ Give feedback!
◮ Let me know if you’re sick & need extra time. ◮ Please wear a mask if you interact with others.
◮ Given out after class on April 8. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Bear with me. ◮ Give feedback!
◮ Let me know if you’re sick & need extra time. ◮ Please wear a mask if you interact with others.
◮ Given out after class on April 8. ◮ Return before 10am on Friday, April 10. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Makes this seem more like a community Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Makes this seem more like a community
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Makes this seem more like a community
◮ Press “View Participants”, then the little blue hand. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Makes this seem more like a community
◮ Press “View Participants”, then the little blue hand.
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Given a directed graph G = (V , E) Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ 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
◮ 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
◮ 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
◮ 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
◮ 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
◮ 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
◮ dist(v) = c∗(v) for all v Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ 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
◮ dist(v) = c∗(v) for all v ◮ pred(s) = None Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ 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
◮ 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
◮ 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
◮ 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
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0.
◮ InitializeSSSP(s) ◮ Repeat: ⋆ Pick an edge ⋆ If it is “tense”, relax it. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ 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
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0.
◮ 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
◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0.
◮ InitializeSSSP(s) ◮ Repeat some number of times: ⋆ Pick an edge u → v (somehow) ⋆ 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
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0.
◮ InitializeSSSP(s) ◮ Repeat some number of times: ⋆ Pick an edge u → v (somehow) ⋆ 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
◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0.
◮ InitializeSSSP(s) ◮ Repeat some number of times: ⋆ Pick an edge u → v (somehow) ⋆ 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
◮ dist(v) = ∞ ∀v ◮ pred(v) = None ∀v ◮ dist(s) = 0.
◮ InitializeSSSP(s) ◮ Repeat some number of times: ⋆ Pick an edge u → v (somehow) ⋆ 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
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ InitializeSSSP(s) ◮ Repeat V − 1 times: ⋆ For every edge u → v in E:
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ InitializeSSSP(s) ◮ Repeat V − 1 times: ⋆ For every edge u → v in E:
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Repeat one more time. If no negative cycles, no edge should change in
◮ Follow the predecessor chain to find a negative cycle.
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ InitializeSSSP(s) ◮ Repeat V times: ⋆ Find the unvisited vertex u of minimal dist(u). ⋆ For every edge u → v out from u:
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Time from E pushes/pops, for O(E log V ) with binary heap. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Time from E pushes/pops, for O(E log V ) with binary heap. ◮ Modifying the algorithm slightly and using a Fibonacci heap can bring
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Time from E pushes/pops, for O(E log V ) with binary heap. ◮ Modifying the algorithm slightly and using a Fibonacci heap can bring
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Time from E pushes/pops, for O(E log V ) with binary heap. ◮ Modifying the algorithm slightly and using a Fibonacci heap can bring
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Time from E pushes/pops, for O(E log V ) with binary heap. ◮ Modifying the algorithm slightly and using a Fibonacci heap can bring
◮ Bellman-Ford relaxes each edge V times. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Time from E pushes/pops, for O(E log V ) with binary heap. ◮ Modifying the algorithm slightly and using a Fibonacci heap can bring
◮ Bellman-Ford relaxes each edge V times. ◮ Dijkstra only relaxes each edge once, so it better happen at the right
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ At each step, values pushed aren’t smaller than the one just popped. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ At each step, values pushed aren’t smaller than the one just popped.
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ At each step, values pushed aren’t smaller than the one just popped.
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Outputs the correct answer always. Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ 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
◮ 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
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Works with negative edge weights Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Works with negative edge weights ◮ Can detect cycles Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Works with negative edge weights ◮ Can detect cycles
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Works with negative edge weights ◮ Can detect cycles
◮ but only with nonnegative edge weights Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Works with negative edge weights ◮ Can detect cycles
◮ but only with nonnegative edge weights ◮ either wrong or exponential time with negative edges Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Works with negative edge weights ◮ Can detect cycles
◮ but only with nonnegative edge weights ◮ either wrong or exponential time with negative edges
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Works with negative edge weights ◮ Can detect cycles
◮ but only with nonnegative edge weights ◮ either wrong or exponential time with negative edges
◮ A∗ search: Dijkstra with a twist Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
◮ Works with negative edge weights ◮ Can detect cycles
◮ but only with nonnegative edge weights ◮ either wrong or exponential time with negative edges
◮ A∗ search: Dijkstra with a twist ◮ Exercises Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24
Eric Price (UT Austin) Shortest Paths CS 331, Spring / 24