cs3000:&algorithms&&&data jonathanullman · inductive hypothesis h n can make...

32
CS3000: Algorithms & Data Jonathan Ullman Lecture 10: Midterm I Review Oct 9, 2018 HWY due Friday solutions at shortly thereafter No HU the week of the midterm

Upload: others

Post on 13-Jan-2020

6 views

Category:

Documents


1 download

TRANSCRIPT

CS3000:&Algorithms&&&DataJonathan&Ullman

Lecture&10:&• Midterm&I&Review&

Oct&9,&2018

HWY due Friday solutions

at shortly thereafter

No HU the weekof the midterm

Midterm&1&Review

Midterm&I&Topics

• Fundamentals:• Induction• Asymptotics• Recurrences

• Stable&Matching• Divide&and&Conquer• Dynamic&Programming

Topics:&Induction

• Proof&by&Induction:• Mathematical&formulas,&e.g.&∑ "#

$%& = # #(&)

• Spot&the&bug• Solutions&to&recurrences• Correctness&of&dividePandPconquer&algorithms

• Good&way&to&study:• LehmanPLeightonPMeyer,&Mathematics*for*CS• Review&dividePandPconquer&in&KleinbergPTardos

1 n 2T E t Cn TCiteProve 1 In Cnlogz 2n

j

eg prove that mergesort is correct

Practice&Question:&Induction

• Suppose&you&have&an&unlimited&supply&of&3&and&7&cent&coins,&prove&by&induction&that&you&can&make&any&amount&* ≥ 12.

Key observation If I can make change for n 3I can make change for n add one com

Second observation If I can make change for n 3 n Z n

I can make change for any m n

L2 13 1,4 15 16 17 18 19 20

4 3 2 3 1 7 2 7

Inductive Hypothesis

H n can make change for any 12 Ken

We're trying to provethen Hln is true

Base Cases

H iz HC137 H M are true by inspection

for some n 14

Inductive Step Suppose H n is true Wewill show

that HInti is true

By Hln we can make change for 12 Ken

If we can make change fer n 2 then we

can make change for htt add a 3cest com

o we can make change for ntl b c n 2 12

e Hln117 is true

Topics:&Asymptotics

• Asymptotic&Notation• ., 0, 1, Ω, Θ• Relationships&between&common&function&types

• Good&way&to&study:• KleinbergPTardos Chapter&2

Mostly use f n O gcn

Notation …&means&… Think… E.g.

f(n)=O(n) ∃5 > 0,*0 > 0, ∀* ≥ *0:0 ≤ ; * ≤ 5<(*)

At&most&“≤”

100n2 =&O(n3)

f(n)=Ω(g(n)) ∃5 > 0,*0 > 0, ∀* ≥ *0:0 ≤ 5< * ≤ ;(*)

At&least&“≥”

2n =&Ω(n100)

f(n)=Θ(g(n)) ; * = 0 < * ?and&; * = @(< * )

Equals“=”

log(n!)& =&Θ(n&log&n)

f(n)=o(g(n)) ∀5 > 0,∃*0 > 0, ∀* ≥ *0:0 ≤ ; * < 5<(*)

Less&than“<”

n2 =&o(2n)

f(n)=ω(g(n)) ∀5 > 0,∃*0 > 0, ∀* ≥ *0:0 ≤ 5< * < ;(*)

Greater&than“>”

n2 =&ω(log&n)

Topics:&Asymptoticsyes

2 m 22 Ocu

Clem login OCnlogn

n nx n 1 x n 2 x n 3 x x 3 2 x I

n x n x n x n x x n x n xn

h

N

log In t login n logini login O nlogan

n nx n i x n 2 x x E x 2 1

7 Ex Ex x E

IHi.ge

login logs E Eloy E togalogot

log 7 blogFlog.tn ED nlog.in

• Constant'factors'can'be'ignored• ∀B > 0????B* = 0 *

• Smaller'exponents'are'Big6Oh'of'larger'exponents• ∀C > D????*E = 0 *F

• Any'logarithm'is'Big6Oh'of'any'polynomial'• ∀C, G > 0??? log)F ?* = 0 *K

• Any'polynomial'is'Big6Oh'of'any'exponential• ∀?C > 0, D > 1???*F = 0 D#

• Lower'order'terms'can'be'dropped• *) + *M/) + * = 0 *)

Topics:&Asymptotics

Practice&Question:&Asymptotics

• Put&these&functions&in&order&so&that&;$ = 0 ;$(&• *OPQR S• 8OPQR #• 2M OPQR OPQR #• 2 OPQR # R

• ∑ "#$%&

• *) log)*

haus nb

ZteN cC Ogilogan tog D nlogz8 n3

3

loginlogo logan

nln 0cm nlogan

231092109M E c n'logzncnlogz7 apbgzzzllog.nli I

Practice&Question:&Asymptotics

• Suppose&;& = 0 < and&;) = 0 < .&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&Prove&that&;& + 4;) = 0 < .

Topics:&Recurrences

• Recurrences• Representing&running&time&by&a&recurrence• Solving&common&recurrences• Master&Theorem

• Good&way&to&study:• Erickson&book• KleinbergPTardos dividePandPconquer&chapter&

0 Tfn T Fo TfE Cn

T n at E t endRecursiontrees

Practice&Question:&Recurrences

• Write&a&recurrence&for&the&running&time&of&this&algorithm.&&Write&the&asymptotic&running&time&given&by&the&recurrence.

F(n):For i = 1,…,n2: Print “Hi”For i = 1,…,3: F(n/3)

0cm3xT F

T n 3xT E t CntT t d 2

Ibd sa 3 5 3

Tfn Ofw

Topics:&Recurrences

• Consder&the&recurrence&V * = * ⋅ V * + *with&V 1 = 1.&&Solve&using&a&recursion&tree.

1 n nlogalogan

OculoglognL J

22

Depeth Size Word

Tf n

i rn FIFES rnxrn n

pieces

2 THEY rnxin.vn n

nxVn piecesi workat level ith

I BDR DR Max depth logdogan

At level L there aree times

n'the 2107

logannaffna

Te IWE

logan zl

l log logan

Topics:&Stable&Matching

• Stable&Matching• Definition&of&a&stable&matching• The&GalePShapley&algorithm• Consequences&of&the&GalePShapley&algorithm

• Good&way&to&study:• KleinbergPTardos

Practice&Question:&Stable&Matching

• Give&an&example&of&3&doctors&and&3&hospitals&such&that&there&exists&a&stable&matching&in&which&every&hospital&gets&it&last&choice&of&doctor

Topics:&DividePandPConquer

• DividePandPConquer• Writing&pseudocode• Proving&correctness&by&induction• Analyzing&running&time&via&recurrences

• Examples&we’ve&studied:&• Mergesort,&Binary&Search,&Karatsuba’s,&Selection

• Good&way&to&study:• Example&problems&from&KleinbergPTardos or&Erickson• Practice,&practice,&practice!

Practice&Question:&DividePandPConquer

Practice&Question:&DividePandPConquer

• Describe&your&algorithm&in&pseudocode

Practice&Question:&DividePandPConquer

• Prove&by&induction&that&the&algorithm&is&correct

Practice&Question:&DividePandPConquer

• Analyze&your&algorithm’s&running&time&by&writing&a&recurrence

Topics:&Dynamic&Programming

• Dynamic&Programming• Identify&subPproblems• Write&a&recurrence,&0XV * = max \# + 0XV * − 6 , 0XV(* − 1)• Fill&the&dynamic&programming&table• Find&the&optimal&solution• Analyze&running&time

• Good&way&to&study:• Example&problems&from&KleinbergPTardos or&Erickson• Practice,&practice,&practice!

Practice&Question:&DP

Practice&Question:&DividePandPConquer

• What&is&the&set&of&subproblems you&will&use?

• Give&a&recurrence&for&this&problem.

Practice&Question:&DividePandPConquer

• Give&a&pseudocode description&of&your&algorithm

Practice&Question:&DividePandPConquer

• Analyze&the&running&time&and&space&usage

Practice&Question

• Design&an&0(*)Ptime&algorithm&that&takes&an&array&_[1:*] and&returns&a&sorted&array&containing&the&smallest& * elements&of&_

Practice&Question

• Consider&the&following&sorting&algorithm

• Prove&that&it&is&correct• Analyze&its&running&time

A[1:n] is a global arraySillySort(1,n):if (n <= 3): put A in orderelse:SillySort(1,2n/3)SillySort(n/3,n)SillySort(1,2n/3)

Practice&Question A[1:n] is a global arraySillySort(1,n):

if (n <= 3): put A in orderelse:

SillySort(1,2n/3)SillySort(n/3,n)SillySort(1,2n/3)

Practice&Question

• Consider&the&following&sorting&algorithm

• Prove&that&it&is&correct• Analyze&its&running&time

A[1:n] is a global arraySillySort(1,n):if (n <= 3): put A in orderelse:SillySort(1,2n/3)SillySort(n/3,n)SillySort(1,2n/3)