Upload
tomai
View
33
Download
6
Embed Size (px)
DESCRIPTION
CSE 780: Design and Analysis of Algorithms. Lecture 12: Augmenting Data Structures. Balanced Binary Search Tree. Maximum Extract-Max Insert Increase-key Search Delete Successor Predecessor. Also support Select o peration ? . Augment Tree Structure. Select ( T, k ) Goal: - PowerPoint PPT Presentation
Citation preview
CSE 2331 / 5331
CSE 2331/5331
Topic 12:Shortest Path
Basics
Dijkstra AlgorithmRelaxationBellman-Ford Alg.
CSE 780 Algorithms
Shortest Path
Given a graph G = (V, E) with weight function w : E --> R
Shortest path weight:
CSE 780 Algorithms
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
Example
CSE 2331 / 5331
CSE 780 Algorithms
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
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
Optimal Substructure Property
Proof Proof by contradiction. If there is a shorter path from to , then the path is a
shorter path from to Contradiciton.
Optimal Substructure Property (Theorem): If is a shortest path from to ,
then any sub-path is also a shortest path.
CSE 780 Algorithms
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
Example: directed graph
Example: undirected graph
Key property: Given shortest path tree rooted at s ( in this example),
one can obtain the shortest path from s to every other vertex connected to it. CSE 2331 / 5331
Shortest Path Tree Question:
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?
CSE 2331 / 5331
Key property: Given shortest path tree rooted at s ( in this example),
one can obtain the shortest path from s to every other vertex connected to it.
O(V)
The output of shortest-path algorithms usually contains both shortest-path tree and distances.
Shortest Path Tree Related to minimum spanning tree?
CSE 2331 / 5331
CSE 780 Algorithms
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
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
Example
CSE 2331 / 5331
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?
Dijkstra Algorithm
CSE 2331 / 5331
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
Example
CSE 2331 / 5331
Correctness
Invariance: 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 -th iteration).
CSE 2331 / 5331
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
CSE 2331 / 5331
Why do we require that the weights are all positive?
Example.
CSE 2331 / 5331
Running Time Analysis
CSE 2331 / 5331
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
Storing Cost at Vertices
CSE 2331 / 5331
Running Time Analysis
First improvement: Spend time to identify in Line 5. Total time: 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
Second Improvement: Priority Queue
CSE 2331 / 5331
Running Time Analysis Priority Queue: Q;
Total size: # Q.insert:
Total time: # Q.DeleteMin:
Total time: # Q.IsNotEmpty() and # Q.MinKey(): ,
Total time: # Q.DecreaseKey:
Total time:
CSE 2331 / 5331
Total time:
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
Can be improved to by using a better implementation of priority queue (Fibonacci heap)
CSE 2331 / 5331
A more general algorithm
CSE 2331 / 5331
CSE 2331/5331
Shortest Path
Given a graph G = (V, E) with weight function w : E --> R
Shortest path weight:
CSE 2331/5331
Goal:
Input: directed weighted graph G = (V, E), source node s V
Output: For every vertex v V,
d[v] = (s, v) π[v]
Shortest-paths tree induced by π[v]
CSE 2331/5331
Previously
Dijkstra Algorithm: Compute shortest paths from a source node when the
input graph has no negative edge weights!
Now:
A single source shortest path algorithm for arbitrary edge weight!
CSE 2331/5331
Relaxation
Maintain shortest-path estimate d[v] for each node d[v]: initialize
Algorithms will repeatedly apply Relax.Differ in the order of Relax operation
Intuition:Do we have a shorter path
if use edge (u,v) ?
CSE 2331/5331
Shortest-paths Properties Triangle inequality
For (u, v) E, (s, v) ≤ (s, u) + w(u, v)
Equality holds when u is predecessor of v.
Upper-bound property If start with d[v] = and apply Relax operations Always have d[v] ≥ (s, v) Once d[v] = (s, v), never changes
Proof by contradiction
CSE 2331/5331
Properties cont.
Convergence property Suppose s u -> v is the shortest path from s to v Currently d[u] = (s, u) Relax (u,v) will set d[v] = (s, v)
Proof: follow from Relaxation defintion
Path-relaxation property Given a shortest path Apply Relax in order Then, afterwards,
Proof: Follow from above property
CSE 2331/5331
Bellman-Ford Algorithm Allow negative weights Follow the frame work that first, initialize:
Then apply a set of Relax compute d[v] and π[v] Return False if there exists negative cycles
CSE 2331/5331
Pseudo-code
Time complexity:O(VE)
CSE 2331/5331
Example
CSE 2331/5331
Correctness Why d[v] = (s, v) for every node v ?
Proof: Use path-relaxation property
Return True (or False) iff there is (or is no) negative cycles ? If there is no negative cycle: obvious
use triangle inequality If there is negative cycle:
Proof by contradiction