32-WarshallFloyd

32-WarshallFloyd - Discussion#32 Warshalls and Floyds...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Discussion #32 Chapter 7, Section 4 1/15 Discussion #32 Warshall’s and Floyd’s Algorithms
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Discussion #32 Chapter 7, Section 4 2/15 Topics Faster reachability algorithms Warshall’s algorithm Weighted graphs Floyd’s algorithm
Background image of page 2
Discussion #32 Chapter 7, Section 4 3/15 Warshall’s Algorithm A more efficient way to compute reachability. Algorithm: 2. for (k=1 to n) for (i=1 to n) for (j=1 to n) if (a ij = 0) a ij a ik a kj Clearly more efficient than our straightforward reachability algorithm O(n 3 ) < O(n 4 ); but does it work? 1. Initialize adjacency matrix A: (For an edge from i to j , a ij is set to 1; otherwise, 0.)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Discussion #32 Chapter 7, Section 4 4/15 Warshall’s Algorithm: Intuitive Idea We’ll see that after k outer loops, a ij = 1 iff there is a path from i to j directly or through {1, 2, …, k }. 1 2 3 4 5 6 1 0 0 1 0 0 0 2 0 0 0 0 0 1 3 0 1 1 0 0 0 4 1 0 0 0 0 0 5 0 0 0 0 0 0 6 0 1 0 0 1 0 k=6: adds (2,5), (3,5), (1,5), (4,5), (2,2) 1 1 1 1 1 1 1 1 1 1 1 k is called a pivot 1 k=1: a 41 a 13 adds (4,3) k=2: a 32 a 26 adds (3,6), a 62 a 26 adds (6,6) k=3: a 13 a 32 adds (1,2), a 43 a 32 adds (4,2), a 43 a 36 adds (4,6), a 13 a 36 adds (1,6) k=4 and k=5 add nothing (either no in or no out) 3 4 1 5 6 2 for (k=1 to n) for (i=1 to n) for (j=1 to n) if (a ij = 0) a ij a ik a kj
Background image of page 4
Discussion #32 Chapter 7, Section 4 5/15 Warshall’s Algorithm Works Are you convinced? If not, how can you become convinced? Prove it even a sketch helps What should you prove? Terminates: clear (can map each loop to a descending sequence on a well-founded poset) Inductive proof based on the loop invariant, which is the key observation about Warshall’s algorithm. • W( k ): After k outer loops, a ij = 1 iff there is a path from i to j directly or through {1, 2, …, k }.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/02/2012 for the course C S 236 taught by Professor Michaelgoodrich during the Winter '12 term at BYU.

Page1 / 15

32-WarshallFloyd - Discussion#32 Warshalls and Floyds...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online