Chapter 5 Chapter 5

Decrease Decrease- -and and- -Conquer Conquer
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
5-1 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
uecreaseŴandŴConquer
1Ŧ 8educe problem lnsLance Lo smaller lnsLance of Lhe
same problem
2Ŧ Solve smaller lnsLance
3Ŧ LxLend soluLlon of smaller lnsLance Lo obLaln soluLlon
Lo orlglnal lnsLance
W Can be lmplemenLed elLher LopŴdown or boLLomŴup
W Also referred Lo as loJoctlve or loctemeotol approach
5-2 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
3 1ypes of uecrease and Conquer
W ecteose by o coostoot %usually by 1)ť
lnserLlon sorL
graph Lraversal algorlLhms %ulS and 8lS)
Lopologlcal sorLlng
algorlLhms for generaLlng permuLaLlonsţ subseLs
W ecteose by o coostoot foctot %usually by half)
blnary search and blsecLlon meLhod
exponenLlaLlon by squarlng
mulLlpllcaLlon à la russe
W IotlobleŴslze Jecteose
Luclld's algorlLhm
selecLlon by parLlLlon
nlmŴllke games
1hls usually resulLs ln a recurslve algorlLhmŦ
5-3 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
JhaL's Lhe dlfference?
Conslder Lhe problem of exponenLlaLlonť CompuLe

o
W 8ruLe lorceť
W ulvlde and conquerť
W uecrease by oneť
W uecrease by consLanL facLorť
n-1 multiplications
T(n) ÷ 2*T(n/2) ¹ 1
÷ n-1
T(n) ÷ T(n-1) ¹ 1 ÷ n-1
T(n) ÷ T(n/a) ¹ a-1
÷ (a-1) n
÷ when a ÷ 2
a
log
n
2
log
5-1 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
nserLlon SorL
1o sorL array Aż0ŦŦoŴ1Žţ sorL Aż0ŦŦoŴ2Ž recurslvely and
Lhen lnserL AżoŴ1Ž ln lLs proper place among Lhe
sorLed Aż0ŦŦoŴ2Ž
W Dsually lmplemenLed boLLom up %nonrecurslvely)
Lxampleť SorL 6ţ 4ţ 1ţ 8ţ 3
6 | 4 1 8 3
4 6 | 1 8 3
1 4 6 | 8 3
1 4 6 8 | 3
1 4 3 6 8
5-5 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
9seudocode of nserLlon SorL
5-ê Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
Analysls of nserLlon SorL
W 1lme efflclency

otst
%o) ƹ o%oŴ1)/2 Z %o
2
)
C
avg
%o) o
2
/4 Z %o
2
)

best
%o) ƹ o Ŵ 1 Z %o) %also fasL on almosL sorLed
arrays)
W Space efflclencyť lnŴplace
W SLablllLyť yes
W 8esL elemenLary sorLlng algorlLhm overall
W 8lnary lnserLlon sorL
5-Z Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
raph 1raversal
Many problems requlre processlng all graph
verLlces %and edges) ln sysLemaLlc fashlon
raph Lraversal algorlLhmsť
uepLhŴflrsL search %ulS)
8readLhŴflrsL search %8lS)
5-8 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
uepLhŴllrsL Search %ulS)
W vlslLs graph's verLlces by always movlng away from
lasL
vlslLed verLex Lo an unvlslLed oneţ backLracks lf no
ad[acenL
unvlslLed verLex ls avallableŦ
W 8ecurlsve or lL uses a sLack
a verLex ls pushed onLo Lhe sLack when lL's reached for Lhe flrsL Llme
a verLex ls popped off Lhe sLack when lL becomes a dead endţ lŦeŦţ
when Lhere ls no ad[acenL unvlslLed verLex
W º8edraws" graph ln LreeŴllke fashlon %wlLh Lree
edges and
back edges for undlrecLed graph)
5-9 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
9seudocode of ulS
5-10 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
Lxampleť ulS Lraversal of undlrecLed graph
a b
e I
c d
g h
DFS traversal stack: DFS traversal stack: DFS tree: DFS tree:
a b
e I
c d
g h
a a
b
a
b
I
a
b
I
e
a
b
I
a
b
a
b
g
a
b
g
c
a
b
g
c
d
a
b
g
c
d
h
a
b
g
c
d
.
Red edges are tree edges and
white edges are back edges.
1 2
5 4
6
3
7
8
5-11 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
noLes on ulS
W ulS can be lmplemenLed wlLh graphs represenLed asť
ad[acency maLrlcesť %|I$
2
)Ŧ Jhy?
ad[acency llsLsť %|I$+|L|)Ŧ Jhy?
W lelds Lwo dlsLlncL orderlng of verLlcesť
order ln whlch verLlces are flrsL encounLered %pushed onLo sLack)
order ln whlch verLlces become deadŴends %popped off sLack)
W AppllcaLlonsť
checklng connecLlvlLyţ flndlng connecLed componenLs
checklng acycllclLy %lf no back edges)
flndlng arLlculaLlon polnLs and blconnecLed componenLs
searchlng Lhe sLaLeŴspace of problems for soluLlons %ln A)
5-12 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
8readLhŴflrsL search %8lS)
W vlslLs graph verLlces by movlng across Lo all Lhe
nelghbors of Lhe lasL vlslLed verLex
W nsLead of a sLackţ 8lS uses a queue
W Slmllar Lo levelŴbyŴlevel Lree Lraversal
W º8edraws" graph ln LreeŴllke fashlon %wlLh Lree
edges and cross edges for undlrecLed graph)
5-13 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
9seudocode of 8lS
5-11 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
Lxample of 8lS Lraversal of undlrecLed graph
8lS Lraversal queueť
a b
e I
c d
g h
FS tree: FS tree:
a b
e I
c d
g h
a b
e
I
e
I
g
I
g
g c
h
h
d
d
Red edges are tree edges and
white edges are cross edges.
1
3
2 6
4 7 5
8
5-15 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
noLes on 8lS
W 8lS has same efflclency as ulS and can be
lmplemenLed wlLh graphs represenLed asť
ad[acency maLrlcesť %|I$
2
)Ŧ Jhy?
ad[acency llsLsť %|I$+|L|)Ŧ Jhy?
W lelds slngle orderlng of verLlces %order
added/deleLed from queue ls Lhe same)
W AppllcaLlonsť same as ulSţ buL can also flnd paLhs
from a verLex Lo all oLher verLlces wlLh Lhe
smallesL number of edges
5-1ê Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
uAs and 1opologlcal SorLlng
A Joqť a dlrecLed acycllc graphţ lŦeŦ a dlrecLed graph wlLh no %dlrecLed) cycles
Arlse ln modellng many problems LhaL lnvolve prerequlslLe
consLralnLs %consLrucLlon pro[ecLsţ documenL verslon conLrol)
verLlces of a dag can be llnearly ordered so LhaL for every edge lLs sLarLlng
verLex ls llsLed before lLs endlng verLex %topoloqlcol sottloq)Ŧ 8elng a dag ls also
a necessary condlLlon for Lopologlcal sorLlng Lo be posslbleŦ
a b
c d
a b
c d
a dag a dag not a dag not a dag
5-1Z Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
1opologlcal SorLlng Lxample
Crder Lhe followlng lLems ln a food chaln
Iish
human
shrimp
sheep
wheat plankton
tiger
5-18 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
ulSŴbased AlgorlLhm
ulSŴbased algorlLhm for Lopologlcal sorLlng
9erform ulS Lraversalţ noLlng Lhe order verLlces are
popped off Lhe Lraversal sLack
8everse order solves Lopologlcal sorLlng problem
8ack edges encounLered?lj nC1 a dag!
Lxampleť
Lfflclencyť 1he same as LhaL of ulSŦ
b a
e I
c d
g h
5-19 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
Source 8emoval AlgorlLhm
Source removal algorlLhm
8epeaLedly ldenLlfy and remove a sootce %a verLex
wlLh no lncomlng edges) and all Lhe edges lncldenL Lo
lL unLll elLher no verLex ls lefL or Lhere ls no source
among Lhe remalnlng verLlces %noL a dag)
Lxampleť
Lfflclencyť same as efflclency of Lhe ulSŴbased algorlLhmţ buL how would you
ldenLlfy a source? Pow do you remove a source from Lhe dag?
a b
e I
c d
g h
'Invert¨ the adjacency lists Ior each vertex to count the number oI incoming edges by
going thru each adjacency list and counting the number oI times that each vertex appears
in these lists. To remove a source, decrement the count oI each oI its neighbors by one.
5-20 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
uecreaseŴbyŴConsLanLŴlacLor AlgorlLhms
n Lhls varlaLlon of decreaseŴandŴconquerţ lnsLance
slze ls reduced by Lhe same facLor %Lyplcallyţ 2)
Lxamplesť
- 8lnary search and Lhe meLhod of blsecLlon
- LxponenLlaLlon by squarlng
- MulLlpllcaLlon à la russe %8usslan peasanL meLhod)
- lakeŴcoln puzzle
- !osephus problem
5-21 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
LxponenLlaLlon by Squarlng
1he problemť CompuLe o
o
where o ls a nonnegaLlve
lnLeger
1he problem can be solved by applylng recurslvely
Lhe formulasť
For even values of For even values of n n
For odd values of For odd values of n n
a a
n n
a a
n n
) )

if if n n > 0 and > 0 and a a
0 0
÷ 1 ÷ 1
a a
n n
a a
n n- -1) 1)
) )

aa
Recurrence: M Recurrence: Mn n) ÷ M ) ÷ M n n ) + f ) + fn n) ),, where f where fn n) ÷ 1 or ) ÷ 1 or
M0) ÷ 0 M0) ÷ 0
Master Theorem: Master Theorem: M Mn n) ) ZZ log log n n) ÷ ) ÷ --) where ) where - ÷ - ÷ log log

n+ n+1) 1)
5-22 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
8usslan 9easanL MulLlpllcaLlon
1he problemť CompuLe Lhe producL of Lwo
poslLlve lnLegers
Can be solved by a decreaseŴbyŴhalf algorlLhm
based on Lhe followlng formulasŦ
For even values of For even values of n n: :
For odd values of For odd values of n n: :
n n ` ` mm ÷ ` ÷ ` mm
n n ` ` mm ÷ ` ÷ ` m + m m + m if if n n > 1 and > 1 and m m if if n n ÷ 1 ÷ 1
n n

n n 1 1

5-23 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
Lxample of 8usslan 9easanL MulLlpllcaLlon
CompuLe 20 * 26
o m
20 26
10 32
3 104 104
2 208 +
1 416 416
0 0
Note: Note: Method reduces to adding Method reduces to adding mm`s `s values corresponding to values corresponding to
odd odd n n`s. `s.
5-21 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
lakeŴColn 9uzzle %slmpler verslon)
1here are o ldenLlcally looklng colns one of whlch ls fakeŦ
1here ls a balance scale buL Lhere are no welghLsŤ Lhe
scale can Lell wheLher Lwo seLs of colns welgh Lhe same
andţ lf noLţ whlch of Lhe Lwo seLs ls heavler %buL noL by
how muchţ lŦeŦ 3Ŵway comparlson)Ŧ ueslgn an efflclenL
algorlLhm for deLecLlng Lhe fake colnŦ Assume LhaL Lhe
fake coln ls known Lo be llghLer Lhan Lhe genulne onesŦ
uecrease by facLor 2 algorlLhm
uecrease by facLor 3 algorlLhm %;3 on page 187 of
LevlLln)
T(n) log n
T(n)
n
3
log
5-25 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
varlableŴSlzeŴuecrease AlgorlLhms
n Lhe varlableŴslzeŴdecrease varlaLlon of decreaseŴandŴconquerţ
lnsLance slze reducLlon varles from one lLeraLlon Lo anoLher
Lxamplesť
- Luclld's algorlLhm for greaLesL common dlvlsor
- 9arLlLlonŴbased algorlLhm for selecLlon problem
- nLerpolaLlon search
- Some algorlLhms on blnary search Lrees
- nlm and nlmŴllke games
5-2ê Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
Luclld's AlgorlLhm
Luclld's algorlLhm ls based on repeaLed
appllcaLlon of equallLy
gcd%mţ o) ƹ gcd%oţ m mod o)
LxŦť gcd%80ţ44) ƹ gcd%44ţ36) ƹ gcd%36ţ 8) ƹ gcd%8ţ4) ƹ gcd%4ţ0) ƹ 4
Cne can prove LhaL Lhe slzeţ measured by Lhe flrsL
numberţ
decreases aL leasL by half afLer Lwo consecuLlve
lLeraLlonsŦ
Penceţ 1%o) Z C%log o)
!rooI. Assume 2 ~ n, and consider m and m mod n.
ase 1 n · 2/2. 2 mod n · n · 2/2.
ase 2 n ~ 2/2. 2 mod n ÷ 2-n · 2/2.
5-2Z Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
SelecLlon 9roblem
llnd Lhe ŴLh smallesL elemenL ln a llsL of o
numbers
W ƹ 1 or ƹ o
W meJlooť ƹ n/2
Lxampleť 4ţ 1ţ 10ţ 9ţ 7ţ 12ţ 8ţ 2ţ 13
medlanƹ ?
1he medlan ls used ln sLaLlsLlcs as a measure of an
average
5-28 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
ulgresslonť 9osL Cfflce LocaLlon 9roblem
lven o vlllage locaLlons along a sLralghL hlghwayţ
where should a new posL offlce be locaLed Lo
mlnlmlze Lhe average dlsLance from Lhe vlllages Lo
Lhe posL offlce?
The average!
In two dimensions with Manhattan distance?
Linear programming!
5-29 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
AlgorlLhms for Lhe SelecLlon
9roblem
1he sorLlngŴbased algorlLhmť SorL and reLurn Lhe ŴLh elemenL
Lfflclency %lf sorLed by mergesorL)ť %olog o)
A fasLer algorlLhm ls based on uslng Lhe qulcksorLŴllke parLlLlon of Lhe llsLŦ LeL s
be a spllL poslLlon obLalned by a parLlLlon %uslng some plvoL)ť
Assumlng LhaL Lhe llsL ls lndexed from 1 Lo oť
f s ƹ ţ Lhe problem ls solvedŤ
lf s ƽ ţ look for Lhe ŴLh smallesL elemenL ln Lhe lefL parLŤ
lf s Ƽ ţ look for Lhe %Ŵs)ŴLh smallesL elemenL ln Lhe rlghL parLŦ
noLeť 1he algorlLhm can slmply conLlnue unLll s ƹ Ŧ
s
all are ^ A|s( all are _ A|s(
5-30 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
1raclng Lhe Medlan / SelecLlon AlgorlLhm
Lxampleť 4 1 10 9 7 12 8 2 13 Pereť o ƹ 9ţ
ƹ 9/2 ƹ 3
array lndex 1 2 3 4 3 6 7 8 9
4 1 9 7 12 8 13
4 1 2 9 7 12 8 10 13
2 1 4 9 7 12 8 10 13 ŴŴŴ sƹ3 Ƽ
ƹ3
9 7 10 13
9 7 8 12 10 13
8 7 9 12 10 13 ŴŴŴ sƹ6 ƽ
ƹ3
5-31 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
Lfflclency of Lhe 9arLlLlonŴbased
AlgorlLhm
Average case %average spllL ln Lhe mlddle)ť
C%o) ƹ C%o/2)+%o+1) C%o) Z %o)
JorsL case %degeneraLe spllL)ť C%o) Z %o
2
)
A more sophlsLlcaLed cholce of Lhe plvoL leads Lo a
compllcaLed algorlLhm wlLh %o) worsLŴcase
efflclencyŦ ueLalls can be found ln CL8Sţ Ch 9Ŧ3Ŧ
5-32 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
nLerpolaLlon Search
Searches a sorLed array slmllar Lo blnary search
buL esLlmaLes locaLlon of Lhe search key ln żlŦŦtŽ
by uslng lLs value vŦ Speclflcallyţ Lhe values of Lhe
array's elemenLs are assumed Lo grow llnearly
from żlŽ Lo żtŽ and Lhe locaLlon of v ls esLlmaLed
as Lhe ŴcoordlnaLe of Lhe polnL on Lhe sLralghL
llne Lhrough %lţ AżlŽ) and %tţ AżtŽ) whose yŴ
coordlnaLe ls vť
index
value
|7(
;
|(
7
.
.
÷ ÷ + + ; ; - - A¡ A¡]) ])7 7 - - )A¡ )A¡7 7] ] A¡ A¡] ) ] )
5-33 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
Analysls of nLerpolaLlon Search
W Lfflclency
average caseť C%o) Ƽ log
2
log
2
o + 1 %from
ºroundlng errors")
worsL caseť C%o) ƹ o
W 9referable Lo blnary search only for vL8 large
arrays and/or expenslve comparlsons
W Pas a counLerparLţ Lhe metboJ of folse posltloo
%teqolo folsl)ţ for solvlng equaLlons ln one
5-31 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
8lnary Search 1ree AlgorlLhms
Several algorlLhms on 8S1 requlres recurslve
processlng of [usL one of lLs subLreesţ eŦgŦţ
W Searchlng
W nserLlon of a new key
W llndlng Lhe smallesL %or Lhe largesL) key
k
·k ~k
5-35 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
Searchlng ln 8lnary Search 1ree
AlgorlLhm @%ţ v)
//Searches for node wlLh key equal Lo v ln 8S1
rooLed aL node
lf ƹ nL reLurn Ŵ1
else lf v ƹ k%) reLurn
else lf v Ƽ k%) reLurn @%left%)ţ v)
else reLurn @%tlqbt%)ţ v)
Lfflclency
worsL caseť C%o) ƹ o
5-3ê Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
CneŴ9lle nlm
1here ls a plle of o chlpsŦ 1wo players Lake Lurn by
removlng from Lhe plle aL leasL 1 and aL mosL m
chlpsŦ %1he number of chlps Laken can vary
from move Lo moveŦ) 1he wlnner ls Lhe player
LhaL Lakes Lhe lasL chlpŦ Jho wlns Lhe game Ŷ
Lhe player movlng flrsL or secondţ lf boLh player
make Lhe besL moves posslble?
L's a good ldea Lo analyze Lhls and slmllar games
ºbackwards"ţ lŦeŦţ sLarLlng wlLh o ƹ 0ţ 1ţ 2ţ ŧ
5-3Z Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Lev|l|r "lrlroducl|or lo lre 0es|gr & Ara|ys|s ol A|gor|lrrs,¨ 2
rd
ed., Cr. 5
9arLlal raph of CneŴ9lle nlm wlLh m ƹ 4
0
5
1
2
3
4
10
6
7
8
9
'ertex numbers indicate 'ertex numbers indicate n n the number of chips in the pile. The the number of chips in the pile. The
losing positions for the player to move are circled. Only winning losing positions for the player to move are circled. Only winning
moves from a winning position are shown in bold). moves from a winning position are shown in bold).
Generalization Generalization: The player moving first wins iff : The player moving first wins iff n n is not a is not a
multiple of more generally multiple of more generally mm+1); the +1); the
winning move is to take winning move is to take n n mod mod n n mod mod mm+1)) +1))
chips on every move. chips on every move.

 n f¾ f°

°  
 n ½ 
¯°¾f°n ¾¯f °¾f°n €  ¾f¯ ½ 
¯   ¾¯f °¾f°n   ° ¾°€¾¯f °¾f°n  
f°¾° –°f°¾f°n

W

0/  0.f° ¯½ ¯ °   ½ ° ¯ ½ W ¾ €  f¾° n  °n ¯ °f f½½fn 4579 !0.93 3974/:.7843//843 080  79870807.9439490083 3.884147928 3/ 0/     .

@½ ¾€ n f¾ f° .

9439490083 3.93 3974/:.7843//843 080  79870807.°  W  n f¾  fn°¾f°%¾f % °¾ °¾ –f½f ¾ff–¯¾%f°  % ½–nf¾°– f–¯¾€– ° f°–½ ¯f°¾ ¾ ¾ ¾ W  n f¾  fn°¾f°€fn %¾f f€% °f¾ fnf°  ¾ n°¯  ½° °f° ¾f°– ¯½nf°gf¾¾ W Iff  ¾  n f¾ n #¾f–¯ ¾  n° ½f° -¯  –f¯ ¾ 4579 !0.884147928 @¾¾f ¾¾°f n¾ f–¯ 3/ 0/     .0/  0.

Jf#¾  €€  °n " .

°¾  ½  ¯€ ½° °f° .

¯½  ° 3  2:95.7843//843 080  79870807.0/ % 3  % 3.9438 W  n W  f° n°  W  n f¾  ° W  n f¾  n°¾f°€fn 4579 !0..

   3  % 3 % 3   3  % 3 % 3.

884147928 3/ 0/   .  3 4 .    0.9439490083 3.  ..   .93 3974/:. 4  3 03.

9439490083 3.0/  0.884147928 3/ 0/     .7843//843 080  79870807.°¾ ° @¾ff  °  ¾  °   n¾ f°   °°¾  °  °¾½½ ½fn f¯°–  ¾   °  W D¾f¯½ ¯ °  ¯½%°° n¾ % f¯½      $  $  $  $  4579 !0.93 3974/:.

9¾  n €°¾ ° 4579 !0.884147928 3/ 0/     .9439490083 3.93 3974/:.7843//843 080  79870807.0/  0.

W @¯  €€n °n .

¾%°% °%° %$Z .

%°% .

f–%°% °$Z .

%°% .

¾%°% ° Z .

%°% %f¾€f¾°f¯¾¾  ff¾% W ½fn  €€n °n ° ½fn W f   ¾ W ¾  ¯ °f¾°–f–¯ f W °f°¾ °¾ 4579 !0.884147928 3/ 0/     .0/ °f¾¾€°¾ °  0.9439490083 3.7843//843 080  79870807.93 3974/:.

884147928 3/ 0/     .0/  0.7843//843 080  79870807.93 3974/:.f°½  ¯¾  ½n ¾¾°–f–f½  n ¾%f°  – ¾%°¾¾ ¯fn€f¾° f½f ¾ff–¯¾  ½ €¾¾ fn%%  f  €¾¾ fn% % 4579 !0.f½@f ¾f .9439490083 3.

93 3974/:.7843//843 080  79870807.884147928 3/ 0/     . ½ ¾ fn%% W I¾¾–f½#¾ n ¾ ff¾¯°–ff€¯ f¾ ¾   f°°¾ °  fnfn¾€° f ©fn ° °¾   ¾fff  W  n¾ ¾ ¾f¾fn f  ¾½¾ ° ¾fn °#¾ fn € €¾¯ f  ¾½½½ €€ ¾fn ° n¯ ¾f f  °    °  ¾°f ©fn °°¾    W # f¾#–f½°  €f¾°%  – ¾f° fn – ¾€°  n –f½% 4579 !0.0/  0.9439490083 3.

0/  0.93 3974/:.9¾  n € 4579 !0.884147928 3/ 0/     .9439490083 3.7843//843 080  79870807.

 .-.    / $97.884147928 3/ 0/   ..89...70-. ./ ./   #0/0/08.-1 .-10 . / 0 1   $9700  ..93 3974/:./ .7097000/08.-.-.0/  0.-1 . .9439490083 3..3/ 900/08.0/08   4579 !0.7843//843 080  79870807.078.- .-. 0   1   .f¯½ f ¾f€°  n –f½ . .

- ¾° W nf° ¯½ ¯ ° –f½¾ ½ ¾ ° f¾ f ©fn °n¯fn ¾ .

%$I$% J" f ©fn °n¾¾ .

9439490083 3.93 3974/:.884147928 3/ 0/     .%$I$+$$% J" W   ¾ ¾°n °–€ n ¾  °n n ¾f €¾ °n°  %½¾ °¾fn%  °n n ¾ n¯  f ° ¾%½½½ €€¾fn% W ½½nf°¾ n n°–n°° n €° °–n°° n n¯½° °¾ n n°–fnnn%€° fn – ¾% €° °–fnf°½°¾f°  n°° n n¯½° °¾ ¾ fn°– ¾f ¾½fn €½  ¯¾€¾°¾%°% 4579 !0.0/  0.7843//843 080  79870807.

884147928 3/ 0/     .93 3974/:.  f  €¾¾ fn% % W I¾¾–f½ n ¾ ¯°–fn¾¾f  ° – ¾€ f¾¾    W °¾ f €f¾fn  ¾ ¾f  W ¯f       f ¾f W # f¾#–f½°  €f¾°%  – ¾f° n¾¾ – ¾€°  n –f½% 4579 !0.0/  0.9439490083 3.7843//843 080  79870807.

0/  0.9439490083 3.93 3974/:.7843//843 080  79870807.9¾  n €  4579 !0.884147928 3/ 0/     .

0/  0.3/ 900/08. 0 1 .f¯½ € f ¾f€°  n –f½ .7097000/08. -01 01 1  .  /  $9700     f ¾f  .9439490083 3.70.7843//843 080  79870807.884147928 3/ 0/     .  /      #0/0/08.74880/08 4579 !0. .93 3974/:. / / 0 1 .

- ¾°  W f¾¾f¯  €€n °nf¾f° nf°  ¯½ ¯ ° –f½¾ ½ ¾ ° f¾ f ©fn °n¯fn ¾ .

%$I$% J" f ©fn °n¾¾ .

9439490083 3.7843//843 080  79870807.93 3974/:.0/  0.884147928 3/ 0/     .%$I$+$$% J" W   ¾¾°–  °–€ n ¾%  f $   €¯  ¾ ¾f¯ % W ½½nf°¾ ¾f¯ f¾  nf°f¾€° ½f¾ €¯f  f  n ¾  ¾¯f ¾°¯ € – ¾ 4579 !0.

- .¾f° @½–nf°–  f– f  n fnnn–f½  f  n –f½°%  n %nn ¾ .7843//843 080  79870807. / ./. ./.93 3974/:.9439490083 3.0/  0. / ¾ °¯ °–¯f°½  ¯¾f° ½  ¾ n°¾f°¾%n°¾n°½© n¾  n¯ ° ¾°n°% I n ¾€f f–nf° ° f  ¾f€   – ¾¾f°–   ¾¾  € ¾ ° °–  %½–nf¾°–%  °–f f–¾f¾ f° n ¾¾fn° °€½–nf¾°– ½¾¾   4579 !0.884147928 3/ 0/     . 349. .

0/   .884147928 3/ 0/   4579 !0.93 3974/:.7843//843 080  79870807.3943 0.3 18 8005 8725 5.9  0.9439490083 3.@½–nf°–f¯½   €°– ¯¾°f€ nf° 907 :2.

9439490083 3. f¾ –¯  f¾ f–¯€½–nf¾°– 9 €¯f ¾f °°–   n ¾f  ½½½ €€ f ¾f¾fn   ¾  ¾ ¾½–nf¾°–½  ¯ fn – ¾ °n°  " -@f f–" f¯½ - .7843//843 080  79870807.884147928 3/ 0/   .93 3974/:.0/  0. / 0 1     €€n °n @ ¾f¯ f¾f€ 4579 !0. .

90.n  ¯f–¯ n  ¯ff–¯  ½ f  °€f°  ¯ f¾n %f   °°n¯°– – ¾%f° f  – ¾°n ° °  °  ¾ €  ¾°¾n  ./..4:39410.4:39903:2-07413..9439490083 3.84:7.079 90.4230/08-  0./.0..89.0790...03.0/ 3..419830-478-430 4579 !0.3/.03. .. / f¯°–  ¯f°°– n ¾%°f f–% f¯½ 0 1   €€n °n ¾f¯ f¾ €€n °n€  f¾ f–¯    4397:0.70203990.7843//843 080  79870807.93 3974/:.4:393903:2-074192089...0 /0.079094.8981470.550.78  °€f¾n "  ¯ f¾n €¯  f–" 39080898 %47024.884147928 3/ 0/     .

 n f¾  .

7843//843 080  79870807.°¾f° fn–¯¾ °¾ff°€ n f¾ f° n°  °¾f°n  ¾ ¾ n   ¾f¯ €fn%½nf % f¯½ ¾ % °f¾ fnf°  ¯  € ¾ n° % ½° °f° ¾f°– % .9439490083 3.93 3974/:.0/  0.884147928 3/ 0/     .½nf°g f¾¾ %¾¾f°½ f¾f°¯  % % f n°½ % ¾ ½¾½  ¯ 4579 !0.

½° °f° f°– @ ½  ¯ .

3 13 .  €¯f¾ .3/.3.¯½ f°   °¾f°°° –f  ° –  @ ½  ¯nf° ¾  f½½°– n¾  .

 3  ..:08413 . 13 ..3  .:08413 474//.3/.  474//.

   .8907%04702 3 Z .8907%04702 .

43 .

070- 4 3  43 . .

0/ 470. 070- 4579 !0.7843//843 080  79870807. #0..:08413 470.:08413 ..03.:7703.03.0 3   3.

  1 3 0701 3 47   0.93 3974/:.884147928 3/ 0/     .9439490083 3.

¾¾f°9 f¾f°.½nf° @ ½  ¯ .

:08413 .:08413 470.03..¯½  ½ n€ ½¾ ° – ¾ 470.03..

.9439490083 3.0/  0..884147928 3/ 0/     .3/213 3 2  2 213 .3/213 2  4579 !0.:08413 474//.93 3974/:.f° ¾  f n f¾  f€f–¯ 3 3 2  2 2 2 f¾ ° €°–€¯f¾  474//.7843//843 080  79870807.:08413 3  2 213 .

½nf° .f¯½ €¾¾f°9 f¾f°.

9439490083 3.//32 8 .//32 4//3 4//3 8 4579 !0.7843//843 080  79870807.47708543/394 094/70/:.0/  0.¯½ & °¯    +   490 094/70/:.0894.884147928 3/ 0/     ..:08.93 3974/:.0894.

f .

7843//843 080  79870807.0/  0.9439490083 3.°½f– €  °% % 3   4  3 4579 !0.884147928 3/ 0/     .°9 %¾¯½  ¾°% @  f °  °nf°–n°¾° €n¾€f  @  ¾f ff°n ¾nf    f ° –¾   ¾nf nf°   ¾ ¾€n°¾ – ¾f¯  f° €° n€ ¾ ¾¾ f % °  ¯n   fn¯½f¾°%  ¾–°f° €€n ° f–¯€  n°– €f n° ¾¾¯ f  €f n°¾°° – f° – °° ° ¾  n f¾  €fnf–¯ % 3 43  n f¾  €fnf–¯%.93 3974/:.

93 3974/:.0/  0.Iff    n f¾ –¯¾ ° ff  ¾ n f¾ ff°€ n f¾ f° n°  °¾f°n ¾  n°f ¾€¯°  f°f°  f¯½ ¾ % n #¾f–¯€– f ¾n¯¯° ¾ % % % % 9f° f¾ f–¯€¾  n°½  ¯ ° ½f°¾ fn ¯ f–¯¾° °f¾ fn ¾ -¯f° -¯  –f¯ ¾   4579 !0.9439490083 3.884147928 3/ 0/   .7843//843 080  79870807.

3/224/ 3 °¯  .803 2.0/ ° nf°½ f ¾ ¯ f¾   €¾ !7441 88:2023 .3/.n #¾–¯ n #¾f–¯¾ f¾ ° ½ f  f½½nf°€ f –n %¯ °% –n %° ¯¯ °%  –n % % –n % % –n % % –n % % –n % %  4579 !0.438/072 .7843//843 080  79870807.

 2 24/3 3 2.

803 2. n f¾ ¾f f¾ f€f€ n°¾ n   f°¾  .

 2 24/32 3 2.

884147928 3/ 0/   .9439490083 3.  °n @%°%Z %–°%    0.93 3974/:.

884147928 3/ 0/   4579 !0.  n°9  ¯ °   ¾¯f ¾  ¯ °°f¾€° °¯ ¾ W   ° W ¯ f°   °$ f¯½          ¯ f° " @ ¯ f°¾¾ °¾f¾n¾f¾f¯ f¾ €f°   f f–  0.7843//843 080  79870807.0/ .9439490083 3.93 3974/:.

7843//843 080  79870807.75747.93 3974/:.0 30.0 394/20384389.223 4579 !0.884147928 3/ 0/     .3/89.9439490083 3.0/  0.– ¾¾° 9¾€€n nf°9  ¯  °°f– nf°¾f°–f¾f––f    ¾ f° ½¾€€n  nf  ¯°¯  f f–  ¾f°n €¯ f– ¾  ½¾€€n " %0.99.07..3.3.

–¯¾€   n° 9  ¯ @ ¾°– f¾ f–¯ f°  °    ¯ ° €€n °n%€¾  ¯ – ¾% .

9439490083 3.7843//843 080  79870807..0/  0.70_ 8( ¾¾¯°–f ¾¾°  €¯° €¾   ½  ¯¾¾ €¾  €  ¾¯f ¾  ¯ °°  €½f €¾  € % ¾% ¾¯f ¾  ¯ °° –½f - @ f–¯nf°¾¯½n°° ° ¾  4579 !0.%°–°% €f¾ f–¯¾ f¾ °¾°– n¾  ½f°€ ¾  ¾ f¾½½¾° f°  f½f°%¾°–¾¯ ½% .884147928 3/ 0/     .93 3974/:..70^ 8( 8 .

f°$  n°–¯ f¯½   $  ff°    °           ¾             4579 !0.@fn°– .0/    0.884147928 3/ 0/   ¾    .9439490083 3.7843//843 080  79870807.93 3974/:.

€€n °n€ 9f° f¾  –¯  f– nf¾ %f f– ¾½° ¯  %  .

%°% .

%°$%+%°+%.

%°%Z .

%°% J¾nf¾ % – ° f ¾½% .

%°%Z .

%°% ¯ ¾½¾nf nn € ½ f ¾f n¯½nf f–¯.

%°%¾ nf¾  €€n °n  f¾nf° €° °.

 .

93 3974/:.9439490083 3.884147928 3/ 0/     .7843//843 080  79870807.  4579 !0.0/  0.

 ( 7  .° ½f° fn  fn ¾f¾ ff¾¯f °f¾ fn  ¾¯f ¾nf°€ ¾ fn °    ¾°–¾f  ½ n€nf  f ¾€  ff#¾  ¯ °¾f f¾¾¯ –° f €¯    f°  nf°€¾ ¾¯f  f¾  n °f € ½°° ¾f– ° –%   %f° %   %¾  n °f ¾    .

. 7( (  .884147928 3/ 0/     .0/  0.   7 3/0 4579 !0.:0 7( .93 3974/:.7843//843 080  79870807.9439490083 3.  7  ( .

°f¾¾€° ½f° fn W €€n °n f f– nf¾ .

%°% – – ° +%€¯ #° °– ¾#% ¾nf¾ .

93 3974/:.%°% ° W 9 € f   °f¾ fn°€If–  ff¾f° $ ½ °¾ n¯½f¾°¾   W f¾fn° ½f  ¯  €€f¾ ½¾° 4579 !0.884147928 3/ 0/   .9439490083 3.7843//843 080  79870807.0/  0.

°f fn@ –¯¾   ff–¯¾° @  ¾ n¾  ½n ¾¾°–€©¾° €¾¾  ¾  –  W  fn°– W °¾ °€f°   W ° °– ¾¯f ¾% f– ¾%    4579 !0.9439490083 3.7843//843 080  79870807.93 3974/:.0/  0.884147928 3/ 0/     .

0/  0.93 3974/:. fn°–° °f fn@ –¯ @% % $$ fn ¾€°   f° @  f°  € - °  ¾ € %% ° ¾ € %% ° @% €%% % ¾  ° @%–%% % €€n °n 4579 !0.884147928 3/ 0/     ¾nf¾ .7843//843 080  79870807.9439490083 3.

%°% ° .

9439490083 3.0/  0.7843//843 080  79870807.° 9 -¯ @  ¾f½ €°n½¾ @½f ¾f °   ¯°–€¯ ½ f f¾f° f¯¾¯ n½¾ %@ °¯ €n½¾f °nf°f €¯¯ ¯ %@ °° ¾ ½f  ff ¾ f¾n½ J°¾ –f¯   ½f ¯°–€¾¾ n° € ½f  ¯f   ¾¯ ¾½¾¾  " #¾f–  ff°f ¾f° ¾¯f–f¯ ¾ # fnf ¾#  ¾f°–°      4579 !0.884147928 3/ 0/     .93 3974/:.

31789381138349.70.0. 2:95041 24700307.081742.0724.0724.9439490083 3.58430. 2  90 2 33324.93 3974/:.7.943%05..7843//843 080  79870807.903 '07903:2-0783/.08949.333548943.884147928 3/ 0/     .903 903:2-0741...0/  3333 24.0324/ 324/ 2  .9fff½€° 9 -¯¯            '07903:2-0783/.0724.0324/ 324/ 33324.079424.70843 3-4/ 0307.. 0307.31789381138349.5839050 %0 4835489438147905.0/  0.0 4579 !0.943%05.08949.