Download presentation
Presentation is loading. Please wait.
Published byΠερσεφόνη Γεωργίου Modified over 6 years ago
1
Topic 11: Shortest Path Basics Dijkstra Algorithm
CSE 2331/5331 Topic 11: Shortest Path Basics Dijkstra Algorithm CSE 2331 / 5331
2
Shortest Path Given a graph G = (V, E) with weight function
w : E --> R Shortest path weight: CSE 780 Algorithms
3
Various Problems Single-source shortest-paths problem
Given source node s to all nodes from V Single-destination shortest-paths problem From all nodes in V to a destination u Single-pair shortest-path problem Shortest path between u and v All-pairs shortest-paths problem Shortest paths between all pairs of nodes CSE 780 Algorithms
4
Example The green one is NOT a shortest path. CSE 2331 / 5331
5
Negative-weight Edges
Some edges may have negative weights If there is a negative cycle reachable from s: Shortest path is no longer well-defined Example Otherwise, it is fine CSE 780 Algorithms
6
Cycles Shortest path cannot have cycles inside
Negative cycles : already eliminated Positive cycles: can be removed 0-weight cycles: can be removed A path with no cycles inside is also called a simple path. No-Cycle Theorem: Given any two vertices of graph, there exists a shortest path between them with no cycle. CSE 780 Algorithms
7
Optimal Substructure Property
Optimal Substructure Property (Theorem): If 𝑢 1 , 𝑢 2 ,…, 𝑢 𝑚 is a shortest path from 𝑢 1 to 𝑢 𝑚 , then any sub-path 𝑢 𝑖 , …, 𝑢 𝑗 is also a shortest path. Proof Proof by contradiction. If there is a shorter path 𝜋 from 𝑢 𝑖 to 𝑢 𝑗 , then the path 𝑢 1 ,…, 𝑢 𝑖 ∘𝜋∘ 𝑢 𝑗 , …, 𝑢 𝑚 is a shorter path from 𝑢 1 to 𝑢 𝑚 . Contradiciton. CSE 780 Algorithms
8
Shortest-paths Tree For every node v V, π[v] is the predecessor of v in a shortest path from source s to v Nil if does not exist A shortest-path tree Root is source s Edges are (π[v] , v) The shortest path between s and v is the unique tree path from root s to v. CSE 780 Algorithms
9
Example: directed graph
CSE 780 Algorithms
10
Example: undirected graph
What if we change weight of v_6,v_7 to 8? Then the shortest path tree is not unique any more. Key property: Given shortest path tree rooted at s ( 𝑣 1 in this example), one can obtain the shortest path from s to every other vertex connected to it. CSE 2331 / 5331
11
Shortest Path Tree Question: Key property:
Given a shortest path tree of G rooted at s, how fast can we report the shortest path distance from s to all other vertices in G? O(V) Key property: Given shortest path tree rooted at s ( 𝑣 1 in this example), one can obtain the shortest path from s to every other vertex connected to it. O(V) homework. The output of shortest-path algorithms usually contains both shortest-path tree and distances. CSE 2331 / 5331
12
Shortest Path Tree Related to minimum spanning tree? CSE 2331 / 5331
13
Goal: Input: a weighted graph G = (V, E), with positive weights! and a source node vs V Output: For every vertex v V, v.distance = (vs , v) v.parent = 𝜋[𝑣] Shortest-paths tree induced by v.parent CSE 780 Algorithms
14
Breadth-First Search Recall BFS:
Shortest path for unweighted graph (i.e, each edge has weight 1). Would the same idea work for weighted graph? CSE 2331 / 5331
15
Example We can no longer guarantee that at the time the algorithm first discovers a node, the distance is shortest. BFS is only for shortest number of edges to reach a node! How to guarantee that when we first reach a node, the distance is shortest? CSE 2331 / 5331
16
Dijkstra Algorithm CSE 2331 / 5331
17
Intuition: If 𝑣 𝑖 .𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒=𝛿 𝑣 𝑠 , 𝑣 𝑖
Then 𝑣 𝑖 .𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒+𝑤𝑒𝑖𝑔ℎ𝑡 𝑣 𝑖 , 𝑣 𝑗 is the shortest distance to reach 𝑣 𝑗 through 𝑣 𝑖 . Note, this does not have to be the same as shortest distance to 𝑣 𝑗 . CSE 2331 / 5331
18
Example CSE 2331 / 5331
19
Correctness Invariance: Prove that this invariance is maintained:
At the beginning of the While-loop, all vertices already discovered have correct shortest distance value. Prove that this invariance is maintained: Base case: in the first iteration, only 𝑣 𝑠 is discovered, and this invariance holds. Inductive step: If the invariance holds at the beginning of the 𝑘-th iteration, then it holds at the end of 𝑘-th iteration (i.e, it holds at the beginning of (𝑘+1)-th iteration). CSE 2331 / 5331
20
Proof of Induction Step
Let 𝑣 𝑖 , 𝑣 𝑗 as identified in Line 5 of the algorithm. Let P be the shortest path from 𝑣 𝑠 to 𝑣 𝑖 . 𝑣 𝑖 .𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒+𝑤𝑒𝑖𝑔ℎ𝑡 𝑣 𝑠 , 𝑣 𝑖 is the weight of the path 𝑃∪ 𝑣 𝑖 , 𝑣 𝑗 . Proof that any other path from 𝑣 𝑠 to 𝑣 𝑗 has larger weight. Consider any other path 𝑃 ′ from 𝑣 𝑠 to 𝑣 𝑗 . Let 𝑥,𝑦 be the first edge in 𝑃 ′ that connects a vertex from 𝑉−𝑈 to a vertex in 𝑈. Argue that the weight of subpath from 𝑣 𝑠 to 𝑦 is at least 𝑣 𝑖 .𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒+𝑤𝑒𝑖𝑔ℎ𝑡 𝑣 𝑠 , 𝑣 𝑖 . Hence the weight of 𝑃 ′ is larger than that of 𝑃∪ 𝑣 𝑖 , 𝑣 𝑗 . Done. CSE 2331 / 5331
21
CSE 2331 / 5331
22
Why do we require that the weights are all positive? Example.
CSE 2331 / 5331
23
Running Time Analysis CSE 2331 / 5331
24
Running Time Analysis Naïve implementation:
Spend 𝑂 𝐸 time to identify 𝑣 𝑖 , 𝑣 𝑗 in Line 5. Total time: 𝑂(𝑉𝐸) How to identify 𝑣 𝑖 , 𝑣 𝑗 more efficiently? First improvement: Storing cost at vertices. 𝑣.distance: current estimate of shortest path weight from 𝑣 𝑠 to 𝑣. CSE 2331 / 5331
25
Storing Cost at Vertices
CSE 2331 / 5331
26
Running Time Analysis First improvement:
Spend 𝑂 𝑉 time to identify 𝑣 𝑖 , 𝑣 𝑗 in Line 5. Total time: 𝑂( 𝑉 2 ) Note: the weight stored at 𝑣 𝑗 is NOT the smallest weight of edge connecting 𝑣 𝑗 to any visited vertex That is how Prim’s MST algorithm works. How to identify 𝑣 𝑖 , 𝑣 𝑗 more efficiently? Second improvement: Use Priority Queue to store / maintain vertex costs! 𝑣.distance: current estimate of shortest path weight from 𝑣 𝑠 to 𝑣. CSE 2331 / 5331
27
Second Improvement: Priority Queue
CSE 2331 / 5331
28
Running Time Analysis Priority Queue: Q; # Q.insert: 𝑂(𝑉)
Total size: 𝑂 𝑉 # Q.insert: 𝑂(𝑉) Total time: 𝑂(𝑉 lg𝑉) # Q.DeleteMin: 𝑂(𝑉) # Q.IsNotEmpty() and # Q.MinKey(): 𝑂(𝑉), Total time: 𝑂(𝑉) # Q.DecreaseKey: 𝑂(𝐸) Total time: 𝑂(𝐸 lg𝑉) Total time: 𝑂( 𝑉+𝐸 lg 𝑉) CSE 2331 / 5331
29
Remarks Similar idea as breadth first search:
Greedy type of algorithm Guarantees that when we first discover a node, the distance is the correct shortest path weight Similar to Prim’s Alg for MST: But the cost at each vertex is defined differently. Also works for directed graphs But require weights to be positive 𝑂(𝑉𝑙𝑔 𝑉+𝐸 lg 𝑉) =𝑂( 𝑉+𝐸 lg 𝑉) Can be improved to 𝑂(𝐸+𝑉 lg 𝑉) by using a better implementation of priority queue (Fibonacci heap) CSE 2331 / 5331
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.