1 weighted graphs. 2 outline (single-source) shortest path –dijkstra (section 4.4) –bellman-ford...

Download 1 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall

If you can't read please download the document

Upload: adam-caldwell

Post on 17-Jan-2018

241 views

Category:

Documents


0 download

DESCRIPTION

3 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted graph: vertices = cities edges = road segments between cities edge weights = road distances –Goal: find a shortest path between two vertices (cities)

TRANSCRIPT

1 Weighted Graphs 2 Outline (single-source) shortest path Dijkstra (Section 4.4) Bellman-Ford (Section 4.6) (all-pairs) shortest path Floyd-Warshall (Section 6.6) minimum spanning tree Kruskal (Section 5.1.3) Prim (Section 5.1.5) 3 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: Road map is a weighted graph: vertices = cities edges = road segments between cities edge weights = road distances Goal: find a shortest path between two vertices (cities) 4 Shortest Path Problem Input: Directed graph G = (V, E) Weight function w : E R Weight of path p = v 0, v 1,..., v k Shortest-path weight from u to v : (u, v) = min w(p) : u v if there exists a path from u to v otherwise Note: there might be multiple shortest paths from u to v p s tx yz 5 Variants of Shortest Path Single-source shortest paths G = (V, E) find a shortest path from a given source vertex s to each vertex v V Single-destination shortest paths Find a shortest path to a given destination vertex t from each vertex v Question: How to solve it ? 6 Variants of Shortest Path Single-source shortest paths G = (V, E) find a shortest path from a given source vertex s to each vertex v V Single-destination shortest paths Find a shortest path to a given destination vertex t from each vertex v Reversing the direction of each edge single-source 7 Variants of Shortest Paths (contd) Single-pair shortest path Find a shortest path from u to v for given vertices u and v All-pairs shortest-paths Find a shortest path from u to v for every pair of vertices u and v 8 Shortest-Path Problems Single-source (single-destination). Find a shortest path from a given source (vertex s) to all the other vertices positive weights greedy algorithm pos. & neg. weights dynamic programming All-pairs. Find shortest-paths for every pair of vertices pos. & neg. weights dynamic programming 9 Optimal Substructure Theorem Given: A weighted, directed graph G = (V, E) A weight function w: E R, A shortest path p = v 1, v 2,..., v k from v 1 to v k A subpath of p : p ij = v i, v i+1,..., v j , with 1 i j k Then: p ij is a shortest path from v i to v j Proof: p = v 1 v i v j v k w(p) = w(p 1i ) + w(p ij ) + w(p jk ) Assume p ij from v i to v j with w(p ij ) < w(p ij ) w(p) = w(p 1i ) + w(p ij ) + w(p jk ) < w(p) contradiction! p 1i p ij p jk v1v1 vivi vjvj vkvk p 1i p ij p ij p jk 10 Triangle Inequality for positive weights For all ( u, v ) E, we have: (s, v) (s, u) + (u, v) - If u is on the shortest path to v we have the equality sign uv s v s u 11 Algorithms Operations common in both algorithms: Initialization Relaxation Dijkstras algorithm Negative weights are not allowed Bellman-Ford algorithm Negative weights are allowed Negative cycles reachable from the source are not allowed. 12 Shortest-Paths Notation For each vertex v V: (s, v): shortest-path weight d[v]: shortest-path weight estimate Initially, d[v]= d[v] (s,v) as algorithm progresses [v] = predecessor of v on a shortest path from s If no predecessor, [v] = NIL induces a treeshortest-path tree s tx yz 13 Initialization Alg.: INITIALIZE-SINGLE-SOURCE(V, s) 1. for each v V 2. do d[v] 3. [v] NIL 4.d[s] 0 All the shortest-paths algorithms start with INITIALIZE-SINGLE-SOURCE 14 Relaxation Step Relaxing an edge (u, v) = testing whether we can improve the shortest path to v found so far by going through u If d[v] > d[u] + w(u, v) we can improve the shortest path to v d[v]=d[u]+w(u,v) [v] u 59 2 uv 57 2 uv RELAX(u, v, w) 56 2 uv 56 2 uv After relaxation: d[v] d[u] + w(u, v) ss no change 15 Dijkstras algorithm 16 Dijkstras Algorithm Single-source shortest path problem: positive-weight edges: w(u, v) > 0, (u, v) E Each edge is relaxed only once! Maintains two sets of vertices: d[v]= (s, v) d[v]> (s, v) 17 Dijkstras Algorithm (cont.) Vertices in V S reside in a min-priority queue Keys in Q are estimates of shortest-path weights d[u] Repeatedly select a vertex u V S, with the minimum shortest-path estimate d[u] Relax all edges leaving u 18 Dijkstra (G, w, s) 0 s tx yz s tx yz S= Q= S= Q= 19 Example (cont.) 0 10 5 s tx yz s tx yz S= Q= 20 Example (cont.) s tx yz s tx yz S= Q= S= Q= 21 Dijkstra (G, w, s) 1. INITIALIZE-SINGLE-SOURCE( V, s ) 2. S 3.Q V[G] 4. while Q 5. do u EXTRACT-MIN(Q) 6. S S { u } 7. for each vertex v Adj[u] 8. do RELAX( u, v, w ) 9. Update Q (DECREASE_KEY) Running time: O(VlgV + ElgV) = O(ElgV) (V) O(V) build min-heap Executed O(V) times O(lgV) O(E) times (total) O(lgV) O(VlgV) O(ElgV) 22 Binary Heap vs Fibonacci Heap Running time depends on the implementation of the heap 23 Correctness Dijkstras algorithm is a greedy algorithm make choices that currently seem the best locally optimal does not always mean globally optimal Correct because maintains following two properties: for every known vertex, recorded distance is shortest distance to that vertex from source vertex for every unknown vertex v, its recorded distance is shortest path distance to v from source vertex, considering only currently known vertices and v Lets prove the Correctness 24 Correctness of Dijskstras Algorithm For each vertex u V, we have d[u] = (s, u) at the time when u is added to S. Proof: Let u be the first vertex for which d[u] (s, u) when added to S Lets look at a true shortest path p from s to u: 25 Correctness of Dijskstras Algorithm Since u is in the shortest path of u: d[u](s,u) d[u]