Download presentation
Presentation is loading. Please wait.
Published byJaylin Stockham Modified over 10 years ago
1
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 13 Instructor: Paul Beame
2
2 All-pairs shortest paths If no negative-weight edges and sparse graphs run Dijsktra from each vertex O(nm log n) What about other cases?
3
3 Floyd’s Algorithm Idea Interior vertices in a path Dijsktra’s Algorithm at each step always computed shortest paths that only had interior vertices from a set S at each step Floyd’s Algorithm slowly add interior vertices on fixed schedule rather than depending on the graph v w interior vertices
4
4 Floyd’s Algorithm Vertices V={v 1,...,v n } Let D k [i,j] = length of shortest path from v i to v j that only allows {v 1,...,v k } as interior vertices Note D 0 [i,j] = weight of edge (v i,v j ) if (v i,v j ) E if (v i,v j ) E D n [i,j] = length of shortest path from v i to v j
5
5 Floyd’s Algorithm vivi vertices from v 1,...,v k vivi vivi vertices from v 1,...,v k-1 vivi Computing D k [i,j] Case 1: v k not used D k [i,j] = D k-1 [i,j]
6
6 Floyd’s Algorithm vjvj vkvk vertices from v 1,...,v k-1 Case 2: v k used vertices from v 1,...,v k-1 vivi
7
7 Floyd’s Algorithm vjvj vkvk vertices from v 1,...,v k-1 Case 2: v k used vertices from v 1,...,v k-1 vivi not on shortest path since no negative cycles
8
8 Floyd’s Algorithm vjvj vkvk vertices from v 1,...,v k-1 Case 2: v k used vivi vertices from v 1,...,v k-1 D k [i,j] = D k-1 [i,k] + D k-1 [k,j]
9
9 Floyd’s Algorithm Floyd(G) D 0 weight matrix of G for k=1 to n do for i=1 to n do for i=1 to n do D k [i,j] min{ D k-1 [i,j], D k-1 [i,k]+D k-1 [k,j]} endfor endfor endfor O(n 3 ) time O(n 2 ) storage by maintaining array for last two values of k
10
10 Multiplying Faster On the first HW you analyzed our usual algorithm for multiplying numbers (n 2 ) time We can do better! We’ll describe the basic ideas by multiplying polynomials rather than integers Advantage is we don’t get confused by worrying about carries at first
11
11 Note on Polynomials These are just formal sequences of coefficients so when we show something multiplied by x k it just means shifted k places to the left
12
12 Polynomial Multiplication Given: Degree n-1 polynomials P and Q P = a n-1 x n-1 + a n-2 x n-2 + … + a 2 x 2 + a 1 x + a 0 Q = b n-1 x n-1 + b n-2 x n-2 + … + b 2 x 2 + b 1 x + b 0 Compute: Degree 2n-2 Polynomial P Q P Q = a n-1 b n-1 x 2n-2 + (a n-1 b n-2 + a n-2 b n-1 ) x 2n-3 +... + (a n-1 b i+1 +a n-2 b i+2 +...+a i+1 b n-1 ) x n+i +... + a 0 b 0 Obvious Algorithm: Compute all a i b j and collect terms (n 2 ) time
13
13 Naive Divide and Conquer Assume n is a power of 2 P = (a n-1 x n/2-1 + a n-2 x n/2-2 + … + a n/2 ) x n/2 + (a n/2-1 x n/2-1 +... + a 2 x 2 + a 1 x + a 0 ) = P 1 x n/2 + P 0 Q = Q 1 x n/2 + Q 0 P Q = (P 1 x n/2 +P 0 )(Q 1 x n/2 +Q 0 ) = P 1 Q 1 x n +(P 1 Q 0 +P 0 Q 1 )x n/2 +P 0 Q 0 4 sub-problems of size n/2 + plus linear combining T(n)=4T(n/2)+cn Solution T(n) = O(n 2 )
14
14 Karatsuba’s Algorithm A better way to compute the terms Compute P 0 Q 0 P 1 Q 1 (P 1 +P 0 )(Q 1 +Q 0 ) which is P 1 Q 1 +P 1 Q 0 +P 0 Q 1 +P 0 Q 0 Then P 0 Q 1 +P 1 Q 0 = (P 1 +P 0 )(Q 1 +Q 0 ) - P 0 Q 0 - P 1 Q 1 3 sub-problems of size n/2 plus O(n) work T(n) = 3 T(n/2) + cn T(n) = O(n ) where = log 2 3 = 1.59...
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.