cse 326 data structures
play

CSE 326: Data Structures distinguished vertex s , find the shortest - PowerPoint PPT Presentation

Single-Source Shortest Path Given a graph G = (V, E) and a single CSE 326: Data Structures distinguished vertex s , find the shortest weighted path from s to every other vertex g p y Dynamic Programming Dynamic Programming in G .


  1. Single-Source Shortest Path • Given a graph G = (V, E) and a single CSE 326: Data Structures distinguished vertex s , find the shortest weighted path from s to every other vertex g p y Dynamic Programming – Dynamic Programming in G . Floyd/Warhsall Algorithm All-Pairs Shortest Path: • Find the shortest paths between all pairs of Hal Perkins vertices in the graph vertices in the graph. Winter 2008 • How? Lecture 26 2 Analysis Dynamic Programming • Total running time for Dijkstra’s: • Algorithmic technique that systematically O(|V| 2 + |E|) (linear scan) records the answers to sub-problems in a table records the answers to sub-problems in a table O(|V| log |V| + |E| log |V|) (heaps) and re-uses those recorded results (rather than re-computing them). What if we want to find the shortest path from each point to ALL other points? • Simple Example : Calculating the Nth g p p Fibonacci number. Fib(N) = Fib(N-1) + Fib(N-2) 3 4 1

  2. Dynamic Programming & Floyd-Warshall Shortest Paths – Floyd-Warshall • Given a directed graph G = (V, E) with no • Data structure: M[x][y] contains the shortest negative-weight cycles (negative-weight edges negative-weight cycles (negative-weight edges known path from x to y Initially this is just the known path from x to y. Initially this is just the may be present), calculate the shortest paths adjacency matrix for the graph between all pairs of vertices • This version only shows the computation of the • Idea: For each pair of verticies vi, vj, find final path lengths – need additional shortest path from vi to vj that only passes bookkeeping to actually remember the paths through {v1, v2, …, vk } – Initially k=1. At each step, increase k by 1. Re- examine each pair vi, vj and see if using vk gives a shorter path than any discovered so far 5 6 b 2 a Floyd-Warshall -2 Initial state of the 1 c -4 3 matrix: • Algorithm 1 d d for (int k = 1; k =< V; k++) for (int k = 1; k =< V; k++) e a b c d e 4 for (int i = 1; i =< V; i++) a 0 2 - -4 - for (int j = 1; j =< V; j++) if ( ( M[i][k]+ M[k][j] ) < M[i][j] ) b - 0 -2 1 3 M[i][j] = M[i][k]+ M[k][j] c - - 0 - 1 d - - - 0 4 Invariant: After the kth iteration, the matrix includes the shortest paths for all e - - - - 0 pairs of vertices (i,j) containing only vertices 1..k as intermediate vertices M[i][j] = min(M[i][j], M[i][k]+ M[k][j]) 7 8 2

  3. 2 b a Floyd-Warshall -2 Floyd-Warshall - 1 for All-pairs c -4 3 • Algorithm shortest path 1 for (int k = 1; k =< V; k++) d d e for (int i = 1; i =< V; i++) 4 for (int j = 1; j =< V; j++) a b c d e if ( ( M[i][k]+ M[k][j] ) < M[i][j] ) a 0 2 0 -4 0 M[i][j] = M[i][k]+ M[k][j] b - 0 -2 1 -1 Final Matrix • Total cost: Contents C t t c - - 0 - 1 • Compared to running Dijkstra’s |V| times? d - - - 0 4 e - - - - 0 9 10 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend