all pairs shortest paths
play

All Pairs Shortest Paths Carola Wenk Slides courtesy of Charles - PowerPoint PPT Presentation

CMPS 2200 Fall 2012 All Pairs Shortest Paths Carola Wenk Slides courtesy of Charles Leiserson y with changes by Carola Wenk 11/26/12 CMPS 2200 Intro. to Algorithms 1 Shortest paths p Single-source shortest paths Nonnegative edge


  1. CMPS 2200 – Fall 2012 All Pairs Shortest Paths Carola Wenk Slides courtesy of Charles Leiserson y with changes by Carola Wenk 11/26/12 CMPS 2200 Intro. to Algorithms 1

  2. Shortest paths p Single-source shortest paths • Nonnegative edge weights No egat ve edge we g ts • Dijkstra’s algorithm: O (| E| log | V| ) • General: Bellman-Ford: O (| V||E| ) • DAG: One pass of Bellman Ford: O (| V| + | E| ) • DAG: One pass of Bellman-Ford: O (| V| + | E| ) CMPS 2200 Intro. to Algorithms 2 11/26/12

  3. Shortest paths p Single-source shortest paths • Nonnegative edge weights No egat ve edge we g ts • Dijkstra’s algorithm: O (| E| log | V| ) • General: Bellman-Ford: O (| V||E| ) • DAG: One pass of Bellman Ford: O (| V| + | E| ) • DAG: One pass of Bellman-Ford: O (| V| + | E| ) All-pairs shortest paths • Nonnegative edge weights No egat ve edge we g ts • Dijkstra’s algorithm | V | times: O (| V||E| log | V| ) • General • Bellman-Ford | V | times: O(| V| 2 | E| ) Bellman Ford | V | times: O(| V| | E| ) • Floyd-Warshall: O(| V| 3 ) CS 3343 Analysis of Algorithms 3 11/26/12

  4. All-pairs shortest paths p p Input: Digraph G = ( V , E ), where | V | = n , with edge-weight function w : E → R . Output: n × n matrix of shortest-path lengths δ ( i j ) f δ ( i , j ) for all i , j ∈ V . ll i j V Algorithm #1: • Run Bellman-Ford once from each vertex. • Time = O(| V| 2 | E| ). • But: Dense graph ⇒ O(| V| 4 ) time. O(| V| 4 ) ti B t D h CS 3343 Analysis of Algorithms 4 11/26/12

  5. Floyd-Warshall algorithm y g • Dynamic programming algorithm. • Assume V ={1, 2, …, n }, and assume G is given A V {1 2 } d G i i in an adjacency matrix A =( a ij ) 1 ≤ i,j ≤ n where a ij is the weight of the edge from i to j . the weight of the edge from i to j ( k ) = weight of a shortest path from i Define c ij to j with intermediate vertices to j with intermediate vertices belonging to the set {1, 2, …, k }. ≤ k ≤ k ≤ k ≤ k ≤ k ≤ k ≤ k ≤ k ≤ k ≤ k ≤ k ≤ k ≤ k ≤ k ≤ k ≤ k i i j j Thus δ ( i j ) = c ( n ) Also c (0) = a Thus, δ ( i , j ) = c ij ( n ) . Also, c ij (0) = a ij . CS 3343 Analysis of Algorithms 5 11/26/12

  6. Floyd-Warshall recurrence y ( k ) = min { c ij ( k– 1) + c kj ( k– 1) , c ik ( k– 1) } c ij Use vertex k Do not use vertex k k ( k– 1) ( k ) ( k ( k– 1) ) c ik c c kj c i i i i j j ( k– 1) c ij intermediate vertices in {1 2 intermediate vertices in {1, 2, …, k -1} k 1} CS 3343 Analysis of Algorithms 6 11/26/12

  7. Pseudocode for Floyd- Warshall Warshall for k ← 1 to n do for i ← 1 to n do for j ← 1 to n do if c ( k-1) > c ( k 1) > c ik (k-1) + c (k 1) + c kj (k-1) then (k 1) then if c ij relaxation ( k) ← c ik (k-1) + c kj (k-1) c ij else l (k) ← c ij (k-1) c ij • Runs in Θ ( n 3 ) time and space • Simple to code. p • Efficient in practice. CS 3343 Analysis of Algorithms 7 11/26/12

  8. Shortest paths p Single-source shortest paths • Nonnegative edge weights No egat ve edge we g ts • Dijkstra’s algorithm: O (| E| log | V| ) • General: Bellman-Ford: O (| V||E| ) adj. list • DAG: One pass of Bellman Ford: O (| V| + | E| ) • DAG: One pass of Bellman-Ford: O (| V| + | E| ) All-pairs shortest paths adj. list j • Nonnegative edge weights No egat ve edge we g ts • Dijkstra’s algorithm | V | times: O (| V||E| log | V| ) • General adj. list j • Bellman-Ford | V | times: O(| V| 2 | E| ) Bellman Ford | V | times: O(| V| | E| ) adj. matrix • Floyd-Warshall: O(| V| 3 ) CS 3343 Analysis of Algorithms 8 11/26/12

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