1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA:...

20
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit

Transcript of 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA:...

Page 1: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

1

CSE 417: Algorithms and Computational Complexity

Winter 2001

Lecture 4

Instructor: Paul Beame

TA: Gidon Shavit

Page 2: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

2

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<bk then T(n) is (nk)

if a=bk then T(n) is (nk log n)

Works even if it is n/b instead of n/b.

)( log abn

Page 3: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

3

Multiplying Matrices

n3 multiplications, n3-n2 additions

44434241

34333231

24232221

14131211

44434241

34333231

24232221

14131211

bbbb

bbbb

bbbb

bbbb

aaaa

aaaa

aaaa

aaaa

444434432442144142443243224212414144314321421141

443434332432143142343233223212314134313321321131

442434232422142142243223222212214124312321221121

441434132412141142143213221212114114311321121111

babababababababababababa

babababababababababababa

babababababababababababa

babababababababababababa

Page 4: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

4

Multiplying Matrices

44434241

34333231

24232221

14131211

44434241

34333231

24232221

14131211

bbbb

bbbb

bbbb

bbbb

aaaa

aaaa

aaaa

aaaa

444434432442144142443243224212414144314321421141

443434332432143142343233223212314134313321321131

442434232422142142243223222212214124312321221121

441434132412141142143213221212114114311321121111

babababababababababababa

babababababababababababa

babababababababababababa

babababababababababababa

Page 5: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

5

Multiplying Matrices

44434241

34333231

24232221

14131211

44434241

34333231

24232221

14131211

bbbb

bbbb

bbbb

bbbb

aaaa

aaaa

aaaa

aaaa

444434432442144142443243224212414144314321421141

443434332432143142343233223212314134313321321131

442434232422142142243223222212214124312321221121

441434132412141142143213221212114114311321121111

babababababababababababa

babababababababababababa

babababababababababababa

babababababababababababa

Page 6: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

6

Multiplying Matrices

44434241

34333231

24232221

14131211

44434241

34333231

24232221

14131211

bbbb

bbbb

bbbb

bbbb

aaaa

aaaa

aaaa

aaaa

444434432442144142443243224212414144314321421141

443434332432143142343233223212314134313321321131

442434232422142142243223222212214124312321221121

441434132412141142143213221212114114311321121111

babababababababababababa

babababababababababababa

babababababababababababa

babababababababababababa

A11 A12

A21

A11B12+A12B22

A22

A11B11+A12B21

B11 B12

B21 B22

A21B12+A22B22A21B11+A22B21

Page 7: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

7

Multiplying Matrices

T(n)=8T(n/2)+4(n/2)2=8T(n/2)+n2

8>22 so T(n) is

A11 A12

A21

A11B12+A12B22

A22

A11B11+A12B21

B11 B12

B21 B22

A21B12+A22B22A21B11+A22B21

=

)()()( 3loglog nnn 8a 2b

Page 8: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

8

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)+cn2

7>22 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)

log27

Page 9: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

9

The algorithm

P1=A12(B11+B21) P2=A21(B12+B22)

P3=(A11 - A12)B11 P4=(A22 - A21)B22

P5=(A22 - A12)(B21 - B22)

P6=(A11 - A21)(B12 - B11)

P7= (A21 - A12)(B11+B22)

C11=P1+P3 C12=P2+P3+P6 - P7

C21= P1+P4+P5+P7 C22=P2+P4

Page 10: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

10

Proving Master recurrence

T(n)=aT(n/b)+cnk

an

Problem size

n/b

n/b2

b

1

# probs

a2

a

1

ad

T(1)=c

Page 11: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

11

Proving Master recurrence

T(n)=aT(n/b)+cnk

an

Problem size

n/b

n/b2

b

1

d=lo

g bn

# probs

a2

a

1

ad

T(1)=c

Page 12: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

12

Proving Master recurrence

T(n)=aT(n/b)+cnk

an

Problem size

n/b

n/b2

b

1

d=lo

g bn

# probs

a2

a

1

ad

costcnk

T(1)=c

cank/bk

ca2nk/b2k

=cnk(a/bk)2

cnk(a/bk)d

=cad

Page 13: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

13

Total Cost

Geometric series ratio a/bk

d+1=logbn +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 (ad)=(a ) =(n )(To see this take logb of both sides)

logbn logba

Page 14: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

14

Quicksort

Quicksort(X,left,right) if left < right

split=Partition(X,left,right) Quicksort(X,left,split-1) Quicksort(X,split+1,right)

Page 15: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

15

Partition - two finger algorithmPartition(X,left,right)

choose a random element to be a pivot and pull it out of the array, say at left end maintain two fingers starting at each end of the arrayslide them towards each other until you get a pair of elements where right finger has a smaller element and left finger has a bigger one (when compared to pivot) swap them and repeat until fingers meet put the pivot element where they meet

Page 16: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

16

Partition - two finger algorithmPartition(X,left,right)

swap X[left],X[random(left,right)] pivot X[left]; L left; R right while L<R do while (X[L] pivot & L right) do L L+1 while (X[R] > pivot & R left) do R R-1 if L>R then swap X[L],X[R]

swap X[left],X[R] return R

Page 17: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

17

In practiceoften 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

Page 18: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

18

Quicksort Analysis

Partition does n-1 comparisons on a list of length n pivot is compared to each other element

If pivot is ith largest then two subproblems are of size i-1 and n-i

Pivot is equally likely to be any one of 1st through nth largest

n

1i

i)T(n1)T(in

11nT(n)

Page 19: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

19

Quicksort analysis

2)1)(n(n

2n

1n

T(n)

2n

1)T(n

2n2)T(n)(n1)1)T(n(n

2nT(n) 2nT(n)-1)1)T(n(n

T(n) 2...T(2) 2T(1) 21)n(n1)1)T(n(n

1)-T(n 2...T(2) 2T(1) 21)-n(nnT(n)n

1)-T(n 2...T(2) 2T(1) 2 1-n

i)T(n1)T(in

11nT(n)

n

1i

Page 20: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 4 Instructor: Paul Beame TA: Gidon Shavit.

20

Quicksort analysis

nn 1.38T(n)

dx) 1/x n that (Recall

n381n 22Hn

1

3

1

2

12(1Q(n)

1n

2Q(n)1)Q(n

1n

T(n)Q(n) Let

2

n

2n

log

ln

ln

1

log.)...