Docsity

Algorithms and Computational Complexity, Cheat Sheet of Computer science

Partial preview of the text

Download Algorithms and Computational Complexity and more Cheat Sheet Computer science in PDF only on Docsity!

CSE 417: Algorithms and

Computational Complexity

Winter 2001

Lecture 4

Instructor: Paul Beame

TA: Gidon Shavit

Master Divide and Conquer

Recurrence

If T(n)=aT(n/b)+cnk^ for n>b then

 if a>bk^ then T(n) is
 if a

Multiplying Matrices

                         41 42 43 44 31 32 33 34 21 22 23 24 11 12 13 14 41 42 43 44 31 32 33 34 21 22 23 24 11 12 13 14 b b b b b b b b b b b b b b b b a a a a a a a a a a a a a a a a                                                  41 11 42 21 43 31 44 41 41 12 42 22 43 32 44 42 41 14 42 24 43 34 44 44 31 11 32 21 33 31 34 41 31 12 32 22 33 32 34 42 31 14 32 24 33 34 34 44 21 11 22 21 23 31 24 41 21 12 22 22 23 32 24 42 21 14 22 24 23 34 24 44 11 11 12 21 13 31 14 41 11 12 12 22 13 32 14 42 11 14 12 24 13 34 14 44 a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b    

Multiplying Matrices

                         41 42 43 44 31 32 33 34 21 22 23 24 11 12 13 14 41 42 43 44 31 32 33 34 21 22 23 24 11 12 13 14 b b b b b b b b b b b b b b b b a a a a a a a a a a a a a a a a                                                  41 11 42 21 43 31 44 41 41 12 42 22 43 32 44 42 41 14 42 24 43 34 44 44 31 11 32 21 33 31 34 41 31 12 32 22 33 32 34 42 31 14 32 24 33 34 34 44 21 11 22 21 23 31 24 41 21 12 22 22 23 32 24 42 21 14 22 24 23 34 24 44 11 11 12 21 13 31 14 41 11 12 12 22 13 32 14 42 11 14 12 24 13 34 14 44 a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b a b    

Multiplying Matrices

T(n)=8T(n/2)+4(n/2)^2 =8T(n/2)+n^2

 8>2^2 so T(n) is

A 11 A 12 A 21 A 11 B 12 +A 12 B 22 A 22 A 11 B 11 +A 12 B 21 B 11 B 12 B 21 B 22 A 21 B 12 +A 22 B 22 A 21 B 11 +A 22 B 21 =

log (^) log 3

n n n

b a^28

Strassen’s algorithm

Strassen’s algorithm  (^) Multiply 2x2 matrices using 7 instead of 8 multiplications (and lots more than 4 additions)  (^) T(n)=7 T(n/2)+cn^2 7>2^2 so T(n) is (n ) which is O(n2.81)  (^) Fastest algorithms theoretically use O(n2.376) time not practical but Strassen’s is practical provided calculations are exact and we stop recursion when matrix has size about 100 (maybe 10) log 27

Proving Master recurrence

T(n)=aT(n/b)+cn k a n Problem size **n/b n/b 2 b 1

probs**

a 2 a 1 a d T(1)=c

Proving Master recurrence

T(n)=aT(n/b)+cn k a n Problem size n/b n/b 2 b 1

d=log

b

n

# probs a 2 a 1 a d T(1)=c

Total Cost

Geometric series  (^) ratio a/bk  (^) d+1=log b n +1 terms  (^) first term cnk, last term cad  (^) If a/bk=1, all terms are equal T(n) is (nk^ log n)  (^) If a/bk<1, first term is largest T(n) is (nk)  (^) If a/bk>1, last term is largest T(n) is (a d )=(a ) =(n ) (To see this take log b of both sides) log b n log b a

Quicksort

Quicksort(X,left,right)

if left < right

split=Partition(X,left,right)

Quicksort(X,left,split-1)

Quicksort(X,split+1,right)

Partition - two finger algorithm

 (^) Partition(X,left,right) swap X[left],X[random(left,right)] pivot  X[left]; L  left; R right while L pivot & R  left) do R  R- if L>R then swap X[L],X[R] swap X[left],X[R] return R

In practice

often choose pivot in fixed way as

 (^) middle element for small arrays  (^) median of 1st, middle, and last for larger arrays  (^) median of 3 medians of 3 (9 elements in all) for largest arrays

 four finger algorithm is better

 (^) also maintain two groups at each end of elements equal to the pivot

  • (^) swap them all into middle at the end of Partition  (^) equal elements are bad cases for two fingers

Quicksort analysis

  (n 1)(n 2) 2n n 1 T(n) n 2 T(n 1) (n 1)T(n 1) (n 2)T(n) 2n (n 1)T(n 1)-nT(n) 2 T(n) 2n (n 1)T(n 1) (n 1)n 2 T(1) 2 T(2) ... 2 T(n) nT(n) n(n-1) 2 T(1) 2 T(2) ... 2 T(n- 1) n 2 T(1) 2 T(2) ... 2 T(n- 1) n- 1 T(i 1) T(n i) n 1 T(n) n 1 n i 1                                             

Quicksort analysis

T(n) 1.38 n n (Recallthat n 1/x dx) 2H 2 n 1 38 n n 1 3 1 2 1 Q(n) 2( n 1 2 Q(n 1) Q(n) n 1 T(n) Let Q(n) 2 n n 2

log

ln

ln

1 ... ). log                    