zjgggpgggg Solving Recurrences g.gg Jan 13, 2020 The Master - - PowerPoint PPT Presentation

zjgggpgggg
SMART_READER_LITE
LIVE PREVIEW

zjgggpgggg Solving Recurrences g.gg Jan 13, 2020 The Master - - PowerPoint PPT Presentation

CS3000: Algorithms & Data Jonathan Ullman Lecture 3: Divide and Conquer: Karatsuba ekeMMtBsneleagioenocBmarigologgthAEthfthggpatz zjgggpgggg Solving Recurrences g.gg Jan 13, 2020 The Master Theorem Recipe for


slide-1
SLIDE 1

CS3000: Algorithms & Data Jonathan Ullman

Lecture 3:

  • Divide and Conquer: Karatsuba
  • Solving Recurrences

Jan 13, 2020

zjgggpgggg

g.gg

ekeMMtBsneleagioenocBmarigologgthAEthfthggpatz

slide-2
SLIDE 2

The “Master Theorem”

  • Recipe for recurrences of the form:
  • ! " = $ ⋅ ! " &

⁄ + )"*

  • Three cases:
  • $

&* > 1 : ! " = Θ "./0& $

  • $

&* = 1 : ! " = Θ "* log "

  • $

&* < 1 : ! " = Θ "*

karatsuba

1 n

3T E

D

Megesort TH

2T E

in

Binarysearch 1 n

TCE

l

Hn

2T E

t

1

slide-3
SLIDE 3

Ask the Audience!

  • Use the Master Theorem to Solve:
  • ! " = 16 ⋅ !

6 7 + )"8

  • ! " = 21 ⋅ !

6 : + )"8

  • ! " = 2 ⋅ !

6 8 + )

  • ! " = 1 ⋅ !

6 8 + )

1

n

_a

T E

Cnd

a

16 b

4

Fa I

n'tog

n

D

2

a

21

b

5

Fd I

m2

d

2

a 2

age

I

n'Gbla

b 2

d

O

n

slide-4
SLIDE 4

Divide and Conquer: Selection (Median)

slide-5
SLIDE 5

Selection

11 3 42 28 17 8 2 15

  • Given an array of numbers ;[1: "], how quickly can

I find the:

  • Smallest number?
  • Second smallest?
  • ?-th smallest?

@ OCD time

n

time nk time

median

01h2

t.me

FET nd smallest

slide-6
SLIDE 6

Selection

11 3 42 28 17 8 2 15 2 3 8 11 15 17 28 42

  • Fact: can select the ?-th smallest in A("?) time
  • Fact: can select the ?-th smallest in A " log " time
  • Sort the list, then return ;[?]
  • Today: select the ?-th smallest in A " time

@

sort

slide-7
SLIDE 7

Warmup

  • You have 25 horses and want to find the 3 fastest
  • You have a racetrack where you can race 5 at a time
  • In: 1, 5, 6, 18, 22

Out: 6 ≻ 5 ≻ 18 ≻ 22 ≻ 1

  • Problem: find the 3 fastest with only seven races

fate

4

a pg

e Tf

e

ry

c YE

t winner

i

QD

tE

Di Di

DK

QD

B

DW

h

rid

v v slowest

Ky Fix

slide-8
SLIDE 8

Median Algorithm: Take I

17 3 42 11 28 8 2 15 13 @ 11 3 5 13 2 8 17 28 42

Select(A[1:n],k): If(n = 1): return A[1] Choose a pivot p = A[1] Partition around the pivot, let p = A[r] If(k = r): return A[r] ElseIf(k < r): return Select(A[1:r-1],k) ElseIf(k > r): return Select(A[r+1:n],k-r)

Ocn time

slide-9
SLIDE 9

Median Algorithm: Take I

1 2 3 4 5 6 7 8 9 @

1 9

12

3

4

5

7 8

91

k 8

617184

k

7

ht

n

l

t

n 2

t

1 2

1 ne

I9

k I

slide-10
SLIDE 10

Median Algorithm: Take II

  • Problem: we need to find a good pivot element

The median

is the bestpivot but also

it's

the problem

we're trying

to solve

It's

enough to

find

an

element

in the middle

half of the

list

slide-11
SLIDE 11

Median of Medians

MOM(A[1:n]): Let H ← J K ⁄ For i = 1,…,m: Meds[i] = median{A[5i-4],A[5i-3],…,A[5i]} Let p ← Select(Meds[1:m], H L ⁄ )

T.me

OG

TLE

047

I

30cm

Ei

e

Median of the medians

B

D

B

D

B

D

B

D

B

D

Do

Do

i OD

OD

D B

B B

D

D

D

D b

D

slide-12
SLIDE 12

Median of Medians

  • Claim: For every @ here are at least 3" 10

⁄ items that are smaller than OPO(@) and at least 3"/10 items that are larger.

smallestmedian biggest med

ran

smallest

E

I

Fo

z 411144141444444

44

Mr HI 1111 H 4ham heMakinIN KK11111444 41 14 1944 biggest

AH red dts

are smaller than the MOM

  • f red dts

is

3 70

Fo

slide-13
SLIDE 13

Median Algorithm: Take II

17 3 42 11 28 8 2 15 13 @ 11 3 5 13 2 8 17 28 42

MOMSelect(A[1:n],k): If(n ≤ 25): return median{A} Let p = MOM(A) Partition around the pivot, let p = A[r] If(k = r): return A[r] ElseIf(k < r): return MOMSelect(A[1:r-1],k) ElseIf(k > r): return MOMSelect(A[r+1:n],k-r)

1 F

t

n

n

11

slide-14
SLIDE 14

Running Time Analysis

slide-15
SLIDE 15

Recursion Tree

! " = !

R6 ST + ! 6 : + )"

! 1 = )

Led

Problems

Werk

  • t

n

I

n

I

I

7h40

11TH

Fo15

9,3

2

1,49110

FEET

9

I

n

slide-16
SLIDE 16
  • Claim: !(") = A(")

Proof by Induction

! " = !

R6 ST + ! 6 : + )"

! 1 = )

BI

Nn

E Cn

for

some

C

I'll

choose later

Ba

e

1

I

I E

Cn

aslong as Csi

Inductiest

1

n

1

Fo

T E

t n

9

1 E

C

E

C 75 Co E

e

n

Etc

Fo t CIs

t

1

  • n

al

c

10

C Fo

1

  • n

E

C

n aslong as C

lo

slide-17
SLIDE 17

Ask the Audience

  • If we change MOM so that it uses n/3 blocks of size

3, would Select still run in O(n) time?

slide-18
SLIDE 18
  • Find the ?-th largest element in A " time
  • Selection is strictly easier than sorting!
  • Divide-and-conquer approach
  • Find a pivot element that splits the list roughly in half
  • Key Fact: median-of-medians-of-five is a good pivot
  • Can sort in A " log " time using same technique
  • Algorithm is called Quicksort
  • Analyze running time via recurrence
  • Master Theorem does not apply
  • Fun Fact: a random pivot is also a good pivot!

Selection Wrapup

slide-19
SLIDE 19

Divide and Conquer: Binary Search

slide-20
SLIDE 20

Binary Search

2 3 8 11 15 17 28 42 ; Is 28 in this list?

slide-21
SLIDE 21

Binary Search

StartSearch(A,t): // A[1:n] sorted in ascending order Return Search(A,1,n,t) Search(A,ℓ,r,t): If(ℓ > r): return FALSE m ← ℓ +

WXℓ L

If(A[m] = t): return m ElseIf(A[m] > t): return Search(A,ℓ,m-1,t) Else: return Search(A,m+1,r,t)

slide-22
SLIDE 22

Running Time Analysis

! " = ! " 2 ⁄ + ) ! 1 = )

slide-23
SLIDE 23
  • Search a sorted array in time A(log ")
  • Divide-and-conquer approach
  • Find the middle of the list, recursively search half the list
  • Key Fact: eliminate half the list each time
  • Prove correctness via induction
  • Analyze running time via recurrence
  • ! " = ! " 2

⁄ + )

Binary Search Wrapup