Algorithms for Interviews [Aziz & Prakash 2010].pdf - X-Files

221
If you find the book helpful, please purchase a copy to support the authors!

Transcript of Algorithms for Interviews [Aziz & Prakash 2010].pdf - X-Files

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

11

AdnanAziz is a professorat theDepartmentof ElectricalandComputerEngineeringat TheUniversityof Texasat Austin, whereheconductsre-searchand teachesclassesin appliedalgorithms. He receivedhis PhDfrom TheUniversityof Californiaat Berkeley;his undergraduatedegreeis from IIT Kanpur.Hehasworkedat Google, Qua1comm, IBM , andsev-eralsoftwareウ エ 。 イ エ オ ー ウ N t ィ ・ ョ ッ エ designingalgorithms, heplayswithhischildren, Laila, Imran, andOmar.

Amit Prakashis a Memberof the TechnicalStaff at Google, whereheworks primarily on machinelearningproblemsthatarisein the contextof online advertising. Prior to that he worked at Microsoft in the websearchエ ・ ョ N HereceivedhisPhDfrom TheUniversityof TexasatAustin;his undergraduatedegreeis from IIT k。ョーオイN tィ ・ョ heis not improvingthequalityof ads, heindulgesin his passionsfor puzzles, movies, travel,andadventureswith his wife.

All rights reserved. No part of this ー オ 「 ャ ゥ 」 。 エ ゥ ッ ュ 。 ケ be reproduced,storedin aretrievalsystem, or transmitted, in anyform, orby anymeans,electronic, mechanical, photocopying, recording, or otherwise, withouttheprior consentof theauthors.

This book was typeset by the authors using Lesley l ョ ー ッ イ エ G ウ

、 ッ 」 オ ュ ・ ョ エ ー イ ・ ー 。 イ 。 エ ゥ ッ ウ ケ ウ エ ・ ュ and PeterWilson's Memoir class.The coverdesignwasdoneusingInkscape.MacOSaiXwasusedto 」 イ ・

ate the front coverimage; it approximatesShelaNye'sportrait of AlanTuring usinga collectionof public domainimagesof famouscomputerscientistsand ュ 。 エ ィ ・ ュ 。 エ ゥ 」 ゥ 。 ョ ウ N ャ ・ graphicon the backcoverwas cre-atedby Nidhi Rohatgi.

The companionwebsitefor thebookincludesa list of known errorsforeachversionof the book. If you come acrossa technicalerror, pleasewrite to us andwe will cheerfullysendyou $0.42. Pleaserefer to thewebsitefor details.

v ・ イ ッ ョ 1.0.0 (SeptemberI , 2010)

l ・「ウゥエ・ZィエエーZOO。ャァッイゥエィュウヲッイゥョエ・イカゥ・キウN」ッュ

ISBN: 1453792996EAN-13: 9781453792995

To myfather! Ishrat Aziz!for givingmemy ャセ ャッョァ loveoflearning

AdnanAziz

To myparents!Manju ShreeandArunPrakash!themostlovingparentsI can imagine

Amit Prakash

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

11

AdnanAziz is a professorat theDepartmentof ElectricalandComputerEngineeringat TheUniversityof Texasat Austin, whereheconductsre-searchand teachesclassesin appliedalgorithms. He receivedhis PhDfrom TheUniversityof Californiaat Berkeley;his undergraduatedegreeis from IIT Kanpur.Hehasworkedat Google, Qua1comm, IBM , andsev-eralsoftwareウ エ 。 イ エ オ ー ウ N t ィ ・ ョ ッ エ designingalgorithms, heplayswithhischildren, Laila, Imran, andOmar.

Amit Prakashis a Memberof the TechnicalStaff at Google, whereheworks primarily on machinelearningproblemsthatarisein the contextof online advertising. Prior to that he worked at Microsoft in the websearchエ ・ ョ N HereceivedhisPhDfrom TheUniversityof TexasatAustin;his undergraduatedegreeis from IIT k。ョーオイN tィ ・ョ heis not improvingthequalityof ads, heindulgesin his passionsfor puzzles, movies, travel,andadventureswith his wife.

All rights reserved. No part of this ー オ 「 ャ ゥ 」 。 エ ゥ ッ ュ 。 ケ be reproduced,storedin aretrievalsystem, or transmitted, in anyform, orby anymeans,electronic, mechanical, photocopying, recording, or otherwise, withouttheprior consentof theauthors.

This book was typeset by the authors using Lesley l ョ ー ッ イ エ G ウ

、 ッ 」 オ ュ ・ ョ エ ー イ ・ ー 。 イ 。 エ ゥ ッ ウ ケ ウ エ ・ ュ and PeterWilson's Memoir class.The coverdesignwasdoneusingInkscape.MacOSaiXwasusedto 」 イ ・

ate the front coverimage; it approximatesShelaNye'sportrait of AlanTuring usinga collectionof public domainimagesof famouscomputerscientistsand ュ 。 エ ィ ・ ュ 。 エ ゥ 」 ゥ 。 ョ ウ N ャ ・ graphicon the backcoverwas cre-atedby Nidhi Rohatgi.

The companionwebsitefor thebookincludesa list of known errorsforeachversionof the book. If you come acrossa technicalerror, pleasewrite to us andwe will cheerfullysendyou $0.42. Pleaserefer to thewebsitefor details.

v ・ イ ッ ョ 1.0.0 (SeptemberI , 2010)

l ・「ウゥエ・ZィエエーZOO。ャァッイゥエィュウヲッイゥョエ・イカゥ・キウN」ッュ

ISBN: 1453792996EAN-13: 9781453792995

To myfather! Ishrat Aziz!for givingmemy ャセ ャッョァ loveoflearning

AdnanAziz

To myparents!Manju ShreeandArunPrakash!themostlovingparentsI can imagine

Amit Prakash

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Tableof Contents

Prologue·1

Problem s ッ ャ カ ゥ ョ ァ G ・ 」 ィ ョ ゥ ア オ ・ ウ . 5

I Problems 13

1 Searching. 14

2 Sorting. 23

3 m・エ。 。ャァッイゥエィュウN 29

4 Algorithms on Graphs· 41

5 Algorithms on Strings· 52

6 Intractability. 56

7 Parallel Computing· 62

8 Design Problems· 67

9 Discrete Mathematics· 73

10 Probability· 80

11 Programming· 88

II TheInterview 99

12 Strategies For A Great Interview· 100

13 Conducting An Interview· 105

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Tableof Contents

Prologue·1

Problem s ッ ャ カ ゥ ョ ァ G ・ 」 ィ ョ ゥ ア オ ・ ウ . 5

I Problems 13

1 Searching. 14

2 Sorting. 23

3 m・エ。 。ャァッイゥエィュウN 29

4 Algorithms on Graphs· 41

5 Algorithms on Strings· 52

6 Intractability. 56

7 Parallel Computing· 62

8 Design Problems· 67

9 Discrete Mathematics· 73

10 Probability· 80

11 Programming· 88

II TheInterview 99

12 Strategies For A Great Interview· 100

13 Conducting An Interview· 105

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Let'sbeginwith the pictureon the front cover. You mayhaveobservedthatthe ー ッ イ エ イ 。 ッ ヲ Alan Turing is constructedfrom anumberof pictures("tiles") of greatcomputerscientistsandmathematicians.

Supposeyou were askedin an interview to designa programthattakes。 ョ ョ 。 ァ ・ anda collectionof s x s-sizedtiles andproducea mosaicfrom the tiles thatresemblesthe image.A goodway to beginmaybe topartition the imageinto s x s-sizedsquares, computethe averagecolorof eachsuchimagesquare, and thenfind the tile that is closestto it inthe color space.Heredistancein color spacecanbe L2-norm overRed-Green-Blue(RGB) intensitiesfor thecolor. As you look morecarefullyatthe problem, you might concludethat it would bebetterto matcheachtile with an imagesquarethat hasa similar structure. Oneway couldbeto performa coarsepixelization(2 x 2 or 3 x 3) of ・ 。 」 ィ ャ 。 ァ ・ squareandfinding thetile thatis "closest"to theimagesquareundera distance

V1TABLEOFCONTENTS

III Solutions 109

l Searching·110

2 Sorting'123

3 Meta-algorithms·130

4 Algorithms on Graphs·144

5 Algorithms on Strings·156

6 Intractability·160

7 Parallel Computing·167

Prologue

8 Design Problems·174

9 DiscreteMathematics·186

10 Probability·194

11 Programming·206

Index of Problems·212

セn rNN |tセ cNpエ n .sO R.."

RZZtゥエQS[_SRl[エRS

77。 オ ARf / /

somew e //

100\ I inynte ェI

|I A NEWMo):>E.LI) ../ ッセ cッエ p QG a ion

/(!?Cn MY aN エ ッs

MMャゥエB iauc. H G.VER.'fU ../" PPlCKE'T oN

| セ THE int n・t

/

Figure1. Evolutionof a computerscientist

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Let'sbeginwith the pictureon the front cover. You mayhaveobservedthatthe ー ッ イ エ イ 。 ッ ヲ Alan Turing is constructedfrom anumberof pictures("tiles") of greatcomputerscientistsandmathematicians.

Supposeyou were askedin an interview to designa programthattakes。 ョ ョ 。 ァ ・ anda collectionof s x s-sizedtiles andproducea mosaicfrom the tiles thatresemblesthe image.A goodway to beginmaybe topartition the imageinto s x s-sizedsquares, computethe averagecolorof eachsuchimagesquare, and thenfind the tile that is closestto it inthe color space.Heredistancein color spacecanbe L2-norm overRed-Green-Blue(RGB) intensitiesfor thecolor. As you look morecarefullyatthe problem, you might concludethat it would bebetterto matcheachtile with an imagesquarethat hasa similar structure. Oneway couldbeto performa coarsepixelization(2 x 2 or 3 x 3) of ・ 。 」 ィ ャ 。 ァ ・ squareandfinding thetile thatis "closest"to theimagesquareundera distance

V1TABLEOFCONTENTS

III Solutions 109

l Searching·110

2 Sorting'123

3 Meta-algorithms·130

4 Algorithms on Graphs·144

5 Algorithms on Strings·156

6 Intractability·160

7 Parallel Computing·167

Prologue

8 Design Problems·174

9 DiscreteMathematics·186

10 Probability·194

11 Programming·206

Index of Problems·212

セn rNN |tセ cNpエ n .sO R.."

RZZtゥエQS[_SRl[エRS

77。 オ ARf / /

somew e //

100\ I inynte ェI

|I A NEWMo):>E.LI) ../ ッセ cッエ p QG a ion

/(!?Cn MY aN エ ッs

MMャゥエB iauc. H G.VER.'fU ../" PPlCKE'T oN

| セ THE int n・t

/

Figure1. Evolutionof a computerscientist

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

2PROLOGUE 3

function definedover all pixel colors (for example, L2-normover RGBaluesfor eachpixel). DependingP ィ ッ キ you representthe tiles, you

eI1dupwith theproblemof findirlg theclosestpointkomasetof pohtsin a k-dimensionalspace.

If therearem tiles aRdthe imageis partitiORedinto nsquaresrthenabrute-forceapproachwouldhave o H ュ ᄋ I time complexity.You couldimprove P エ ィ ゥ ウ by first indexhgthe tiles ushgaIIappropriatesearchtree.Amoredetailed、 ゥ ウ ウ ゥ ッ ョ on this approachis presentedin Prob-

lem 8.1 andits solution.If h aE-60miRUtehterviewyyoucanwork thToughtheaboveideasr

write somepseudocodefor your algorithm, and analyzeits complex-iiF ケッオキッ 、 havehada fairly successfulゥ イ キ N i ョ Q ー 。m イ エ ゥ 」 オ Q ャ 。 イ Z

would have 、 ・ ュ Q P i Q ウ エ イ 。 エ ・ 、 エ ッ your ゥ ョ Q エ ・ イ カ カ ゥ ・ キ ・ イ エ ィ 。 エ you possessseveral

key skills:

_ Theability to rigorouslyformulatereal-worldproblems.t ィ ・ skills to solveproblemsanddesignalgorithms.

t ィ ・ tools to go from analgorithmto aworking program.t ィ ・ malyticaltechMquesrequiredto determhethecomputatiomlcomplexityof your solution.

Book Overview

a ャ ッ イ ゥ エ ィ ュ ウ for Interviews(AFI) aimsto help engineersinterviewingforSOLar-e、 ・ カ ・ ャ ッ ー ョ ュ エ ー ュ ョ ウ N t ィ ・ ゥ ョ t ヲ ッ f i is algorithmdesign.Theentirebookis ーイ・ウ・ エ・、 throughproblemsinterspersedwith、 ゥ ウ 」 オ ウ ウ ゥ ッ ョ ウ N Q ・ problemscoverkey comeptsmd arewell-motivatedrchallenging, andfun to solve.

We do not emphasizeplatformsand ー イ ッ ァ イ 。 ュ ュ ゥ ャ 。 ョ ァ オ 。 ァ ・ ウ sincethey differ acrossjobsy md cmbe acquiredfairly emly.II1terviewsat

st largesoftwarecompmiesfocusmoreonalgorithmsrproblemsolv-iLaJdesignskills than o ウ ー ・ 」 ゥ ヲ ゥ 」 domainknowledge. Also, ーi ᆳfobsaM ー イ ッ ァ ュ ュ Q ィ ァ ャ 。 ュ ァ ・ ウ cmchmgequickly asrequirementschmgebutthequalitiesmmtiORedabovewill alwaysbehmdameI1taltoanvsuccessfulsoftwareendeavor.

JTM questiomwe pmentshouldallbe solvablewithh a om houriew and in イ ョ 。 」 。 ウ ・ ウ take ウ ャ ャ ケ less time. A question

may take more or lesstime to completeFdepmdhgOIIthe amOUIIt ofodingthat is askedfor.

ウッャ、ッュカ。イケィエ・ュウ ofdetailm-for somepdlemsweーイ ・ ウ ・ エ

detailedimplementationsin j 。 カ 。 O c i p ケ エ ィ ッ ヲ ッ イ othersrwe siTPlysketchsolutions. Someuse fairly technicalmachinery, e.g., max-t1ow,raI1domizedmalysisyetc.Youwill enComtersuchproblemsonly if you

claim specializedknowledge, e.g., graphalgorithms, complexitytheory,etc.

Interviewing is about more than being able to design algorithmsquickly. You alsoneedto know how to presentyourself, how to askforhelpwhenyou arestuck, how to comeacrossasbeingexcitedaboutthecompany, andknowingwhatyou cando for them. We discussthenon-technicalaspectsof interviewingin Chapter12. You canpracticewithfriendsor by yourself; in eithercase, besureto time yourself. Interviewat as manyplacesas you canwithout it taking away from your job orclasses. The experiencewill help you and you may discoveryou likecompaniesthatyou did not know muchabout.

Although an interviewermay occasionallyask a questiondirectlyfrom AFI, you shouldnot baseyour preparationon ュ ・ ュ ッ イ ゥ ョ ァ ウ ッ ャ オ

tions from AFI. We sincerelyhopethat readingthis bookwill be enjoy-ableandimproveyour algorithmdesignskills. The endgoal is to makeyou abetterengineeraswell asbetterpreparedfor softwareinterviews.

Level andPrerequisites

Most of AFI requiresits readersto havebasicfamiliarity with algorithmstaughtin a typical オ ョ 、 ・ イ ァ イ オ 。 エ ・ M i ・ カ ・ ャ algorithms 」 ャ 。 ウ ウ N ャ ・ chapterso ュ ・ エ 。 M 。 ャ ァ ッ イ ゥ エ ィ ュ ウ ァ イ ィ ウ and intractability usemore advancedma-chineryandmayrequireadditionalreview.

Eachchapterbeginswith a reviewof keyconcepts.This reviewis notmeantto becomprehensiveandif youarenotfamiliar with thematerial,you shouldfirst studythe correspondingchapterin analgorithmstext-book. Therearedozensof suchtextsandourpreferenceis to masteroneor two goodbooksratherthan ウ オ ー ・ イ 」 ゥ 。 ャ ャ ケ samplemany. We like Algo-rithms by Dasgupta, Papadirnitriou, andVaziranibecauseit is succinctandbeautifullywritten; Introductionto Algorithmsby Cormen, Leiserson,Rivest, andSteinis moredetailedandservesasa goodreference.

Sinceour focusis onproblemsthatcanbesolvedin aninterviewrel-atively completely, there are many elegantalgorithm designproblemswhichwe do not include.Similarly, we do nothaveanystraightforwardreview-typeproblems;you maywant to brushup エ ィ ・ ウ ・ usingintro-ductoryprogramminganddata-structurestexts.

The field of algorithmsis vastandtherearemanyspecializedtopics,suchas computationalgeometry, numericalanalysis, logic 。 ャ ァ ッ イ ゥ ャ ュ ウ

etc. Unlessyou claim knowledgeof suchtopics, it is highly unlikely thatyou will beaskeda questionwhich requiresesotericknowledge.Whileaninterviewproblemmayseemspecializedatfirst glance, it is invariablythe casethat thebasicalgorithmsdescribedin thisbookaresufficienttosolveit.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

2PROLOGUE 3

function definedover all pixel colors (for example, L2-normover RGBaluesfor eachpixel). DependingP ィ ッ キ you representthe tiles, you

eI1dupwith theproblemof findirlg theclosestpointkomasetof pohtsin a k-dimensionalspace.

If therearem tiles aRdthe imageis partitiORedinto nsquaresrthenabrute-forceapproachwouldhave o H ュ ᄋ I time complexity.You couldimprove P エ ィ ゥ ウ by first indexhgthe tiles ushgaIIappropriatesearchtree.Amoredetailed、 ゥ ウ ウ ゥ ッ ョ on this approachis presentedin Prob-

lem 8.1 andits solution.If h aE-60miRUtehterviewyyoucanwork thToughtheaboveideasr

write somepseudocodefor your algorithm, and analyzeits complex-iiF ケッオキッ 、 havehada fairly successfulゥ イ キ N i ョ Q ー 。m イ エ ゥ 」 オ Q ャ 。 イ Z

would have 、 ・ ュ Q P i Q ウ エ イ 。 エ ・ 、 エ ッ your ゥ ョ Q エ ・ イ カ カ ゥ ・ キ ・ イ エ ィ 。 エ you possessseveral

key skills:

_ Theability to rigorouslyformulatereal-worldproblems.t ィ ・ skills to solveproblemsanddesignalgorithms.

t ィ ・ tools to go from analgorithmto aworking program.t ィ ・ malyticaltechMquesrequiredto determhethecomputatiomlcomplexityof your solution.

Book Overview

a ャ ッ イ ゥ エ ィ ュ ウ for Interviews(AFI) aimsto help engineersinterviewingforSOLar-e、 ・ カ ・ ャ ッ ー ョ ュ エ ー ュ ョ ウ N t ィ ・ ゥ ョ t ヲ ッ f i is algorithmdesign.Theentirebookis ーイ・ウ・ エ・、 throughproblemsinterspersedwith、 ゥ ウ 」 オ ウ ウ ゥ ッ ョ ウ N Q ・ problemscoverkey comeptsmd arewell-motivatedrchallenging, andfun to solve.

We do not emphasizeplatformsand ー イ ッ ァ イ 。 ュ ュ ゥ ャ 。 ョ ァ オ 。 ァ ・ ウ sincethey differ acrossjobsy md cmbe acquiredfairly emly.II1terviewsat

st largesoftwarecompmiesfocusmoreonalgorithmsrproblemsolv-iLaJdesignskills than o ウ ー ・ 」 ゥ ヲ ゥ 」 domainknowledge. Also, ーi ᆳfobsaM ー イ ッ ァ ュ ュ Q ィ ァ ャ 。 ュ ァ ・ ウ cmchmgequickly asrequirementschmgebutthequalitiesmmtiORedabovewill alwaysbehmdameI1taltoanvsuccessfulsoftwareendeavor.

JTM questiomwe pmentshouldallbe solvablewithh a om houriew and in イ ョ 。 」 。 ウ ・ ウ take ウ ャ ャ ケ less time. A question

may take more or lesstime to completeFdepmdhgOIIthe amOUIIt ofodingthat is askedfor.

ウッャ、ッュカ。イケィエ・ュウ ofdetailm-for somepdlemsweーイ ・ ウ ・ エ

detailedimplementationsin j 。 カ 。 O c i p ケ エ ィ ッ ヲ ッ イ othersrwe siTPlysketchsolutions. Someuse fairly technicalmachinery, e.g., max-t1ow,raI1domizedmalysisyetc.Youwill enComtersuchproblemsonly if you

claim specializedknowledge, e.g., graphalgorithms, complexitytheory,etc.

Interviewing is about more than being able to design algorithmsquickly. You alsoneedto know how to presentyourself, how to askforhelpwhenyou arestuck, how to comeacrossasbeingexcitedaboutthecompany, andknowingwhatyou cando for them. We discussthenon-technicalaspectsof interviewingin Chapter12. You canpracticewithfriendsor by yourself; in eithercase, besureto time yourself. Interviewat as manyplacesas you canwithout it taking away from your job orclasses. The experiencewill help you and you may discoveryou likecompaniesthatyou did not know muchabout.

Although an interviewermay occasionallyask a questiondirectlyfrom AFI, you shouldnot baseyour preparationon ュ ・ ュ ッ イ ゥ ョ ァ ウ ッ ャ オ

tions from AFI. We sincerelyhopethat readingthis bookwill be enjoy-ableandimproveyour algorithmdesignskills. The endgoal is to makeyou abetterengineeraswell asbetterpreparedfor softwareinterviews.

Level andPrerequisites

Most of AFI requiresits readersto havebasicfamiliarity with algorithmstaughtin a typical オ ョ 、 ・ イ ァ イ オ 。 エ ・ M i ・ カ ・ ャ algorithms 」 ャ 。 ウ ウ N ャ ・ chapterso ュ ・ エ 。 M 。 ャ ァ ッ イ ゥ エ ィ ュ ウ ァ イ ィ ウ and intractability usemore advancedma-chineryandmayrequireadditionalreview.

Eachchapterbeginswith a reviewof keyconcepts.This reviewis notmeantto becomprehensiveandif youarenotfamiliar with thematerial,you shouldfirst studythe correspondingchapterin analgorithmstext-book. Therearedozensof suchtextsandourpreferenceis to masteroneor two goodbooksratherthan ウ オ ー ・ イ 」 ゥ 。 ャ ャ ケ samplemany. We like Algo-rithms by Dasgupta, Papadirnitriou, andVaziranibecauseit is succinctandbeautifullywritten; Introductionto Algorithmsby Cormen, Leiserson,Rivest, andSteinis moredetailedandservesasa goodreference.

Sinceour focusis onproblemsthatcanbesolvedin aninterviewrel-atively completely, there are many elegantalgorithm designproblemswhichwe do not include.Similarly, we do nothaveanystraightforwardreview-typeproblems;you maywant to brushup エ ィ ・ ウ ・ usingintro-ductoryprogramminganddata-structurestexts.

The field of algorithmsis vastandtherearemanyspecializedtopics,suchas computationalgeometry, numericalanalysis, logic 。 ャ ァ ッ イ ゥ ャ ュ ウ

etc. Unlessyou claim knowledgeof suchtopics, it is highly unlikely thatyou will beaskeda questionwhich requiresesotericknowledge.Whileaninterviewproblemmayseemspecializedatfirst glance, it is invariablythe casethat thebasicalgorithmsdescribedin thisbookaresufficienttosolveit.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

4

Acknowledgments

The problemsin this book comefrom diversesources-ourown expe-riences, colleagues, friends, papers, books, Internetbulletinboards, etc.To paraphrasePaulHalmosfrom his キ ッ 、 ・ イ ヲ オ ャ bookProblemsfor Math-ematicians, Youngand Old: "I do not give credits-whodiscoveredwhat?Who wasfirst? Whosesolutionis thebest?It would not be fair to givecreditin some」 。 ウ ・ ウ Q 、 notin others.No oneknowswhodiscoveredthetheoremthatbearsPythagoras'nameandit doesnot matter.Thebeautyof thesubjectspeaksfor itself andsobeit."

Onepersonwhosehelpandsupporthasimprovedthequality of thisbookandmadeit fun to readis our cartoonist, editor, andproofreader,Nidhi Rohatgi. Severalof our friends and studentsgavefeedbackonthis book-wewould especiallylike to thankIan Varley, who wrote so-lutionsto severalproblems, andSenthilChellappan, GayatriRamachan-dran, andAlper Senfor proofreadingseveralchapters.

We bothwant to thankall thepeoplewho havebeena sourceof en-lightenmentand ゥ ョ ウ ー ゥ イ 。 エ ゥ ッ エ ッ us throughtheyears.

1/ Adnan Aziz, would like to thank teachers, friends, and studentsfrom IIT Kanpur, UC Berkeley, andUT Austin. I would especiallyliketo thankmy friends Vineet GuptaandVigyan Singhal, andmy teach-ersRobertSolovay, RobertBrayton, RichardKarp/ RaimundSeidel, andSomenathBiswasfor introducingme to the joys of algorithms. My co-author, Amit Prakash, hasbeenawonderfulcollaborator-thisbookis atestamentto his intellect, creativity, andenthusiasm.

1/ Amit Prakash, have my co-authorand mentor, Adnan Aziz, tothankthemostfor thisbook. To a greatextent, my problemsolvingskillshavebeenshapedby Adnan. There havebeenoccasionsin life whenI キッオャ、 ッエ have ュ 。 、 ・ h ッ オ ァ ィ without his help. He is also the bestpossiblecollaboratorI canthink of for anyintellectualendeavor.

Over the years, I havebeenfortunateto have great teachersat IITKanpurandUT Austin. I would especiallylike to thankProfessorsScottNettles, Vijaya Ramachandran, andGustavode Veciana. I would alsolike to thankmy friends and colleaguesat Google, Microsoft, and UTAustin for all the stimulatingconversationsand problemsolving ses-sions.Lastly andmostimportantl)T, I wantto thankmy family who havebeena constantsourceof support, ・ク」ゥエ・ュ・ O andjoy for all my life andespeciallyduringtheprocessof writing this book.

ADNAN AZIZ。 、 ャ ` 。 ァ ッ イ ゥ エ ィ ュ ウ ヲ ッ イ ゥ ョ エ ・ イ カ ゥ ・ キ ウ N 」 ッ ュ

AMIT [email protected]

ProblemSolvingTechniques

It'snot thatI/m sosmarttit's justthatI staywith problemslonger.

A. Einstein.

-Developingproblemsolvingskills is1ikekamizlgto p1aya m ·ゥョウエイオュ・ョエ M。 bookor a teachercmpohtyou h theright directiOIL buto ャ ケ your haydworkwill takeyou whereyou want to g0·Likea m-

/ ケッオョ エッィッキオョ、・ 」ッョ」・ーエウ but theoryis no substitutefor practice;forth1sreasonrAFI consistsprimarily of problems

Greatproblemsokershaveski11sthatcarmotbecapturedby a setofrules.Stillywhmfacedwith a cEIdleI1gingalgoyithmdesigIIprob1emit isrlpfu1Mwea ウュ。QQウ・エ、ァュ ゥーャ・ウ thatmaybe applicablewe eIImeratea c01lectionof suchprhciplesh Table1.ofteIL youmayhaveto usemorethanoneof thesetechdques.

Wewill I1ow look atsomeconcreteexamplesof howthesetechRiquesanbeapplied.

DIVIDE-AND-CONQUER AND GENERALIZATION

A triomhois formedby joining threeunit-sizedsquaresin m L-shape.a ュ オ エ ・ 、 」 ィ ・ ウ ッ 。 イ H ィ ・ 」 ・ ヲ ッ イ X x 8Mboard)is madeup of 64unit-sizedsquaresarzmgedm m 8× 8squareymiI111sthetopleftsquaye-sup-R Z Q R ッ オ 。 イN ・ 。 ウ ォ

ッ ュ ャ ゥ ョ Q P ウ エ ィ 。 エ coversthe8 x 8 m「 ッ 。イ、N (Sincethereare63 squaresh the8 × 8Mboardand-wehaventriomhosravalid phcementcanmthaveoverlappingtriommosor trioIIlinos which extendout of the 8 × 8Mboard.)

Divide-aI1d-COIlqueris a goodstrategyto attack thisproblem-k1steadof the8 × 8Lfboardr1etFsconsiderm n × nLfboard-A2× 2Mboardc-nbecoveredキ エ イ ゥ ッ ュ ゥ イ ッ ヲ エ ャ 。 ュ ・ exactshape. y ッ オ ュ T Q z ;ゥ R_ ZSZRZZZ[ ゥZZ_ ィ。エ エ。 ッュ ュゥョ ャゥョo Q o ーi ュョ・ョ ュ ョ

s ウゥョァ canbeusedto 」 ッ ュ ー オ エ ・ a ーャ。」 ・ュ ャ・ョ ャエ for an イz i ク Q

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

4

Acknowledgments

The problemsin this book comefrom diversesources-ourown expe-riences, colleagues, friends, papers, books, Internetbulletinboards, etc.To paraphrasePaulHalmosfrom his キ ッ 、 ・ イ ヲ オ ャ bookProblemsfor Math-ematicians, Youngand Old: "I do not give credits-whodiscoveredwhat?Who wasfirst? Whosesolutionis thebest?It would not be fair to givecreditin some」 。 ウ ・ ウ Q 、 notin others.No oneknowswhodiscoveredthetheoremthatbearsPythagoras'nameandit doesnot matter.Thebeautyof thesubjectspeaksfor itself andsobeit."

Onepersonwhosehelpandsupporthasimprovedthequality of thisbookandmadeit fun to readis our cartoonist, editor, andproofreader,Nidhi Rohatgi. Severalof our friends and studentsgavefeedbackonthis book-wewould especiallylike to thankIan Varley, who wrote so-lutionsto severalproblems, andSenthilChellappan, GayatriRamachan-dran, andAlper Senfor proofreadingseveralchapters.

We bothwant to thankall thepeoplewho havebeena sourceof en-lightenmentand ゥ ョ ウ ー ゥ イ 。 エ ゥ ッ エ ッ us throughtheyears.

1/ Adnan Aziz, would like to thank teachers, friends, and studentsfrom IIT Kanpur, UC Berkeley, andUT Austin. I would especiallyliketo thankmy friends Vineet GuptaandVigyan Singhal, andmy teach-ersRobertSolovay, RobertBrayton, RichardKarp/ RaimundSeidel, andSomenathBiswasfor introducingme to the joys of algorithms. My co-author, Amit Prakash, hasbeenawonderfulcollaborator-thisbookis atestamentto his intellect, creativity, andenthusiasm.

1/ Amit Prakash, have my co-authorand mentor, Adnan Aziz, tothankthemostfor thisbook. To a greatextent, my problemsolvingskillshavebeenshapedby Adnan. There havebeenoccasionsin life whenI キッオャ、 ッエ have ュ 。 、 ・ h ッ オ ァ ィ without his help. He is also the bestpossiblecollaboratorI canthink of for anyintellectualendeavor.

Over the years, I havebeenfortunateto have great teachersat IITKanpurandUT Austin. I would especiallylike to thankProfessorsScottNettles, Vijaya Ramachandran, andGustavode Veciana. I would alsolike to thankmy friends and colleaguesat Google, Microsoft, and UTAustin for all the stimulatingconversationsand problemsolving ses-sions.Lastly andmostimportantl)T, I wantto thankmy family who havebeena constantsourceof support, ・ク」ゥエ・ュ・ O andjoy for all my life andespeciallyduringtheprocessof writing this book.

ADNAN AZIZ。 、 ャ ` 。 ァ ッ イ ゥ エ ィ ュ ウ ヲ ッ イ ゥ ョ エ ・ イ カ ゥ ・ キ ウ N 」 ッ ュ

AMIT [email protected]

ProblemSolvingTechniques

It'snot thatI/m sosmarttit's justthatI staywith problemslonger.

A. Einstein.

-Developingproblemsolvingskills is1ikekamizlgto p1aya m ·ゥョウエイオュ・ョエ M。 bookor a teachercmpohtyou h theright directiOIL buto ャ ケ your haydworkwill takeyou whereyou want to g0·Likea m-

/ ケッオョ エッィッキオョ、・ 」ッョ」・ーエウ but theoryis no substitutefor practice;forth1sreasonrAFI consistsprimarily of problems

Greatproblemsokershaveski11sthatcarmotbecapturedby a setofrules.Stillywhmfacedwith a cEIdleI1gingalgoyithmdesigIIprob1emit isrlpfu1Mwea ウュ。QQウ・エ、ァュ ゥーャ・ウ thatmaybe applicablewe eIImeratea c01lectionof suchprhciplesh Table1.ofteIL youmayhaveto usemorethanoneof thesetechdques.

Wewill I1ow look atsomeconcreteexamplesof howthesetechRiquesanbeapplied.

DIVIDE-AND-CONQUER AND GENERALIZATION

A triomhois formedby joining threeunit-sizedsquaresin m L-shape.a ュ オ エ ・ 、 」 ィ ・ ウ ッ 。 イ H ィ ・ 」 ・ ヲ ッ イ X x 8Mboard)is madeup of 64unit-sizedsquaresarzmgedm m 8× 8squareymiI111sthetopleftsquaye-sup-R Z Q R ッ オ 。 イN ・ 。 ウ ォ

ッ ュ ャ ゥ ョ Q P ウ エ ィ 。 エ coversthe8 x 8 m「 ッ 。イ、N (Sincethereare63 squaresh the8 × 8Mboardand-wehaventriomhosravalid phcementcanmthaveoverlappingtriommosor trioIIlinos which extendout of the 8 × 8Mboard.)

Divide-aI1d-COIlqueris a goodstrategyto attack thisproblem-k1steadof the8 × 8Lfboardr1etFsconsiderm n × nLfboard-A2× 2Mboardc-nbecoveredキ エ イ ゥ ッ ュ ゥ イ ッ ヲ エ ャ 。 ュ ・ exactshape. y ッ オ ュ T Q z ;ゥ R_ ZSZRZZZ[ ゥZZ_ ィ。エ エ。 ッュ ュゥョ ャゥョo Q o ーi ュョ・ョ ュ ョ

s ウゥョァ canbeusedto 」 ッ ュ ー オ エ ・ a ーャ。」 ・ュ ャ・ョ ャエ for an イz i ク Q

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

6 PROBLEMSOLVINGTECHNIQUES 7

Technique Description

Divide-and- Canyou divide theprobleminto two or moreconquer smaller independentsUbproblemsand solve

the original problem using solutions to thesubproblems?

Recursion, dynamic If you haveaccessto solutionsfor smallerin-programmmg stancesof agivenproblem, canyoueasilycon-

structasolutionto theproblem?

Caseanalysis Canyou split theinput/executioninto anum-berof casesandsolveeachcasein isolation?

Generalization Is therea problemthat subsumesyour prob-lem andis easierto solve?

Data-structures Is therea data-structurethat directly mapstothegivenproblem?

Iterativerefinement Most problemscan be solvedusing a brute-force approach.Canyou formalizesucha so-lution andimprove オ ー ゥ エ _

Smallexamples Canyou find a solution to small concretein-stancesof the problem and thenbuild a so-lution that canbe generalizedto 。 イ 「 ゥ エ イ 。 イ ケ

stances?

Reduction Canyouuseaproblemwith aknownsolutionasa subroutine?

Graphmodeling Canyou describeyour problemusinga graphandsolveit usinganexistingalgorithm?

Write an ・ ア オ 。 エ ゥ ッ Canyouexpressイ ・ ャ 。 エ ゥ ッ ウ ィ ゥ ー ウ in yourproblemin theform of equations(or inequalities)?

Auxiliary elements Canyou addsomenewelementto your prob-lem to getcloserto a solution?

Variation Canyousolveaslightly differentproblemandmapits solutionto yourproblem?

Parallelism Can you decomposeyour probleminto sub-problemsthatcanbesolvedindependentlyondifferentmachines?

Caching Canyou storesomeof your computationandlook it up laterto savework?

Symmetry Is theresymmetryin the input spaceor solu-tion spacethat 」 。 イ 「 ・ ・ ク ー ャ ッ ・ 、 _

Table1. cッュュッ ーイッ「ャ・ュ solvingtechniques.

Mboard-Howeveryou wmquickly seetht tkislheof reasonhgdoesnot leadyouanywhere.

Anotherhypothesisis thatif a placementexists forann ク m「 ッ。イ、thenonealsoexists fora 2n x R m「 ッ。イ、N This doeswork: take4 n x n

Mboardsandarrangeエ ィ ・ ュ ッ イ ュ 。 ク R ウ ア オ 。 イ ・ ゥ ュ オ 」 ィ 。 キ 、 エ ィ ゥthreeof theMboardshavetheEIIUSSIngsquareset towardsthe center。 ッ ョ ・ Mboard has its ュ ゥ ウ ウ ゥ 。 イ ・ outward to 」 ッ ゥ ョ ・ with themissingcornerof a RクR Q「 ッ。イ、N Thegapin the 」 ・ エ ・ イ canbecoveredwith a エイゥッ 。ョ、 by hypothesis, we セ 。 セ coverthe T ᅲ m 「 ッ 。 イ 、 ウwith triominosaswell. h ・ イ ョ ・ ・ 。 ー ャ 。 」 ・ ュ ・ ョ Q エ ・ ゥ ウ エ ウ ヲ ッ イ 。 ョ ケ エ ィ 。 エ ゥ ウ apowero ヲ R N i ョ ー 。 i

o i Q usedゥ ョ エ ィ ・ pm f cm be dimetly coded tofhd the actuai COLe-::Laswell. o 「 ウ ・ p イ ッ 「 ャ ・ ュ 、 ・ イ イ ュ ッ

・ ャ ャ 。 ウ ァ ・ ョ ・ ・ イ 。 ャ ゥ コ コ 。 エ ゥ ッ ョ (from 8 x 8 エPR クR IN

RECURSIONAND DYNAMIC PROGRAMMING

Supposeyouwereto desig1aI1algorithmthattakesaz111npareIIthesized・ s s ゥ c o Q Q エ ァ。、 、 ゥ エ ゥ oョ。 ュオ ャゥ」。 エゥ o ー・イ。エッ イN

エィ・ー。 イ・ョ ャエ ィ・ sャ コ。 エゥッ i Q エZィ 。 エュ ャ。クゥ ュ ャゥ コ・ ウ the カ 。 ャ オ ・ ・ of the ・ ク ー イ ・ ウ ウ ゥ ッ ョ N Formpleytheexpression5-3·4+6yieldsmy of thefollowiIIgva111es:

-25 = U HS (4 + 6))

-13 = U HHS . 4) + 6)

20 = (5 - 3) . (4 + 6)

-1 = HU HS . TII V

14 = ((5 - 3) . 4) + 6

s [Z Rc s m c ッュ ーオ エ ・ エィ・ ー 。 イイ・ョN ュ ・ュ i Q エ エィ・ウゥコ。エゥッ i Q。xQ iュ ョ Q ゥコ・ ウゥエウ ウカ。 。ャオ・ Q ・ ゥ エ is easyto ider t theoptimumtop level

parenthesization• pareRtheSIzeoneachsideof theoperatorsazlddetermmt キ ィ ゥ operatorイ i

・ 」 オ イ s ャ カ ・ 」 ッ ュ ー オ エ 。 エ ゥ ッ ョ of the イ ョ 。 ゥ ュ i i i コ ャ ョ Q Y ー 。 イ ・ ョ エ ィ ・ ウ ゥ コ 。 エ ゥ ュ Q forオ 「 ・ ー イ ・ o ュ S leadsto repeatedcalls with idmtical 。 イ ァ オ ュ ・ ウ N Dy-

programmingavoidstheserepeatedcomputations;referto Prob-lem3.11fora detailedexposition-I

CASE ANALYSIS

y ッ オ are ァ ゥ v ュ ・ ュ 。 ウ ・ エ S ofE distincthtegmmdaCPUthathasaspecialmstruetiOIL SORt-Ethatcm sort5htegersh OIIe cycle.Yourtask isto ゥ 、 ・ ョ エ ゥ エ ィ ・ 3largestintegersh S ushgSORt-5tocompayeandsortsubsetsof afurthermoreryou mustmiIIimize the numberof calls toSORT5.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

6 PROBLEMSOLVINGTECHNIQUES 7

Technique Description

Divide-and- Canyou divide theprobleminto two or moreconquer smaller independentsUbproblemsand solve

the original problem using solutions to thesubproblems?

Recursion, dynamic If you haveaccessto solutionsfor smallerin-programmmg stancesof agivenproblem, canyoueasilycon-

structasolutionto theproblem?

Caseanalysis Canyou split theinput/executioninto anum-berof casesandsolveeachcasein isolation?

Generalization Is therea problemthat subsumesyour prob-lem andis easierto solve?

Data-structures Is therea data-structurethat directly mapstothegivenproblem?

Iterativerefinement Most problemscan be solvedusing a brute-force approach.Canyou formalizesucha so-lution andimprove オ ー ゥ エ _

Smallexamples Canyou find a solution to small concretein-stancesof the problem and thenbuild a so-lution that canbe generalizedto 。 イ 「 ゥ エ イ 。 イ ケ

stances?

Reduction Canyouuseaproblemwith aknownsolutionasa subroutine?

Graphmodeling Canyou describeyour problemusinga graphandsolveit usinganexistingalgorithm?

Write an ・ ア オ 。 エ ゥ ッ Canyouexpressイ ・ ャ 。 エ ゥ ッ ウ ィ ゥ ー ウ in yourproblemin theform of equations(or inequalities)?

Auxiliary elements Canyou addsomenewelementto your prob-lem to getcloserto a solution?

Variation Canyousolveaslightly differentproblemandmapits solutionto yourproblem?

Parallelism Can you decomposeyour probleminto sub-problemsthatcanbesolvedindependentlyondifferentmachines?

Caching Canyou storesomeof your computationandlook it up laterto savework?

Symmetry Is theresymmetryin the input spaceor solu-tion spacethat 」 。 イ 「 ・ ・ ク ー ャ ッ ・ 、 _

Table1. cッュュッ ーイッ「ャ・ュ solvingtechniques.

Mboard-Howeveryou wmquickly seetht tkislheof reasonhgdoesnot leadyouanywhere.

Anotherhypothesisis thatif a placementexists forann ク m「 ッ。イ、thenonealsoexists fora 2n x R m「 ッ。イ、N This doeswork: take4 n x n

Mboardsandarrangeエ ィ ・ ュ ッ イ ュ 。 ク R ウ ア オ 。 イ ・ ゥ ュ オ 」 ィ 。 キ 、 エ ィ ゥthreeof theMboardshavetheEIIUSSIngsquareset towardsthe center。 ッ ョ ・ Mboard has its ュ ゥ ウ ウ ゥ 。 イ ・ outward to 」 ッ ゥ ョ ・ with themissingcornerof a RクR Q「 ッ。イ、N Thegapin the 」 ・ エ ・ イ canbecoveredwith a エイゥッ 。ョ、 by hypothesis, we セ 。 セ coverthe T ᅲ m 「 ッ 。 イ 、 ウwith triominosaswell. h ・ イ ョ ・ ・ 。 ー ャ 。 」 ・ ュ ・ ョ Q エ ・ ゥ ウ エ ウ ヲ ッ イ 。 ョ ケ エ ィ 。 エ ゥ ウ apowero ヲ R N i ョ ー 。 i

o i Q usedゥ ョ エ ィ ・ pm f cm be dimetly coded tofhd the actuai COLe-::Laswell. o 「 ウ ・ p イ ッ 「 ャ ・ ュ 、 ・ イ イ ュ ッ

・ ャ ャ 。 ウ ァ ・ ョ ・ ・ イ 。 ャ ゥ コ コ 。 エ ゥ ッ ョ (from 8 x 8 エPR クR IN

RECURSIONAND DYNAMIC PROGRAMMING

Supposeyouwereto desig1aI1algorithmthattakesaz111npareIIthesized・ s s ゥ c o Q Q エ ァ。、 、 ゥ エ ゥ oョ。 ュオ ャゥ」。 エゥ o ー・イ。エッ イN

エィ・ー。 イ・ョ ャエ ィ・ sャ コ。 エゥッ i Q エZィ 。 エュ ャ。クゥ ュ ャゥ コ・ ウ the カ 。 ャ オ ・ ・ of the ・ ク ー イ ・ ウ ウ ゥ ッ ョ N Formpleytheexpression5-3·4+6yieldsmy of thefollowiIIgva111es:

-25 = U HS (4 + 6))

-13 = U HHS . 4) + 6)

20 = (5 - 3) . (4 + 6)

-1 = HU HS . TII V

14 = ((5 - 3) . 4) + 6

s [Z Rc s m c ッュ ーオ エ ・ エィ・ ー 。 イイ・ョN ュ ・ュ i Q エ エィ・ウゥコ。エゥッ i Q。xQ iュ ョ Q ゥコ・ ウゥエウ ウカ。 。ャオ・ Q ・ ゥ エ is easyto ider t theoptimumtop level

parenthesization• pareRtheSIzeoneachsideof theoperatorsazlddetermmt キ ィ ゥ operatorイ i

・ 」 オ イ s ャ カ ・ 」 ッ ュ ー オ エ 。 エ ゥ ッ ョ of the イ ョ 。 ゥ ュ i i i コ ャ ョ Q Y ー 。 イ ・ ョ エ ィ ・ ウ ゥ コ 。 エ ゥ ュ Q forオ 「 ・ ー イ ・ o ュ S leadsto repeatedcalls with idmtical 。 イ ァ オ ュ ・ ウ N Dy-

programmingavoidstheserepeatedcomputations;referto Prob-lem3.11fora detailedexposition-I

CASE ANALYSIS

y ッ オ are ァ ゥ v ュ ・ ュ 。 ウ ・ エ S ofE distincthtegmmdaCPUthathasaspecialmstruetiOIL SORt-Ethatcm sort5htegersh OIIe cycle.Yourtask isto ゥ 、 ・ ョ エ ゥ エ ィ ・ 3largestintegersh S ushgSORt-5tocompayeandsortsubsetsof afurthermoreryou mustmiIIimize the numberof calls toSORT5.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

8 PROBLEMSOLVINGTECHNIQUES 9

If all we hadto computewasthe largestintegerin the set, the opti-mumapproachwouldbe to form 5 、ゥ ッゥョエ subsets81,…, 85 of 8/ sorteachsubset, and then sort {max 81,…, max85}. This takes6 calls toSORT5but leavesambiguityaboutthesecondandthird largestintegers.

It may seemlike manycalls to SORT5 are still needed.Howeverifyou do a carefulcaseanalysisandeliminateall ク X for whichthereareat least3 integersin 8 larger than X/ P ャ ケ 5 integersremainandhencejust onemorecall to SORT5is neededto computethe result. Detailsaregivenin thesolutionto Problem2.5.

FIND A GOOD DATA STRUCTURE

Supposeyou are given a setof files, eachcontainingstockquoteinfor-mation.Eachline containsstartswith a エゥュ・ウエ ーN Thefiles are ゥ ョ 、 ゥ イ ゥ 、 ᆳ

ually sortedby this value.You areto designanalgorithmthatcombinesthesequotesinto a single file R containingthesequotes, sortedby theョ ・ ウ エ 。 ュ ー ウ N

This problemcanbe solvedby amultistagemergeprocess, but thereis a trivial solutionusing a min-heapdatastructure, wherequotesareorderedby timestamp.Firstbuild themin-heapwith thefirst quotefromeachfile; theniterativelyextracttheminimumentrye from themin-heap/キイゥエ・ エッ R, and add in the next entry in the file correspondingto e.Detailsaregivenin Problem2.10.

ITERATIVE REFINEMENT OF BRUTE-FORCESOLUTION

Considertheproblemof stringsearch(cf Problem5.1): giventwo stringss (searchstring) andT (text), find all occurrencesof s in T. Sincescanoccurat any offset in T , thebrute-forcesolutionis to testfor a matchatevery offset. This algorithm is perfectlycorrect; its time complexity isoHN m)/ wheren andm arethelengthsof sandT.

After trying someexamples, youmayseethatthereareseveralwaysin which to ゥ イ ー イ ッ カ ・ the time complexity of the brute-forcealgorithm.For example, if thecharacterT[i] is not presentin s you cansuitablyad-vancethematching.Furthermore, this skippingworksbetterif wematchthe searchstring from its endandwork backwards.Theserefinementswill makethealgorithmveryfast(linear-time)onrandomtextandsearchstrings;however, theworstcase」 ッ ュ ー ャ ・ ク ケ remainso H ᄋ ュ I N

You can make the additional ッ 「 ウ ・ イ イ 。 エ ゥ ッ ョ that a partial match of swhich doesnot resultin a full matchimplies otheroffsetswhich cannotleadto full matches.For ・ ク ョ ー ャ ・ if ウ 「、 「・。「・ andiff startingback-wards, wehavea partialmatchup エ ッ 「 ・ 「 ・ that 、 ッ ・ ウ ッ エ resultin a fullmatch, we know that thenextpossiblematchingoffsethasto be at least3 positionsahead(wherewe canmatchthe ウ ・ 」 ッ ョ 、 「 ・ from the partialmatch).

By puttiI1g togethertheserefinemeI1tsyou will havearrived at thefamousBoyer-Moorestringsearchalgorithm-itsworstmcasetime C m-plexityisoh+m)(whichisthebestpossibleffomatheoreticalperspecmtivek it is alsooneof thefasteststrhgsearchdFrithmsh practice.

SMALL EXAMPLES

Problemsthat seemdifficult to solve in the abstract, canbecomemuchュ ッ イ 。 」 エ 。 「 ャ ・ whenyouexaminesmallconcreteinstances.For ゥ ョ ウ エ 。 ョ」 ッ ウ ゥ 、 ・ イ エ ャ ッ ャ ャ ッ キ ゥ ァ problem:there 。 P P 」 ャ ッ 、 ッ ッ イ ウ 。 ャ ッ 。 イ ゥ ᆳdorr numberedfrom1t0500.ApersOI1walksthroughthe corridorandopenseachdoor.AIIotherpersonwalksthroughtke corridorandcloseseveryalternatedoor. c ッ ョ エ ゥ ョ オ ゥ ョ ァ エ ィ ゥ ウ ュ ュ ・ エ ィ ・ i-th personcaI1dtogglesthepositionof everyt-th doorstarthgfromdoort.y;to determineexactlyhow manydoorsareopmafterthe 500-thpersOIIhaswalkedthroughthecorridor.

It isverydifficult to solvethisproblemushgabstractvariables.How-everif you try theproblemfor ljp374710?md20doorsrit takesmderammteto seethatthedoorsthatremahopmarel?479716.··Fregard-lessof thetotalI1umberof doors.Thepatternis obvious-thedoorsthatイ ・ 。 ュ ッ ー 。 イ ・ ッ ウ ・ numberedby perfectsquares. Onceyou makethscOImeetlOIL1tlseasytOEZ?veit for thegeneralcase-HeIIcethetotdnumberof opendoorsis ly500J= 22. r・ヲ・イセエッ Problem9A for a detailedsolution.

REDUCTION

c ッ ョ ウ ゥ 、 ・ エ ィ ・ probkmof fiMing if om ウ エ ァ is a rotationof the other,e.g., B」。イB Q、 Harc"arerotatiORsof eachotherA I1aturdapproachmaybeto rotatethe first strhgbyeverypomibleoffsetaM ttmcomar4キ ゥ エ ィ ・ secondウ エ ァ N This algorithmwould havequadraticエ ゥ 」 ッ ュ ᆳplexity.

You may I1otice that this problemis quite ウ ョ ゥ ャ 。 イ to string searchwhich cmbedomh1inear-tmeralbeitmhga somewhatcomplexalmgorithm.Soit would beI1aturdto try to reducethis problemto stringsearch.IndeedrifwecomatemtethesecondstringwithitselfaMsearcEfor thefirst styingh tke resultingstring, wewill find amatchiff thetwooriginalstrhgsarerotatiOI1sof eachother.Thisreductionyieldsa linear-time algorithmfor ourproblem;detailsaregiveRiRProbkII15.4.

Usually you try to reduceyour ー イ ッ 「 ャ ・ セ to an easierproblem. Butsometmesryou needto reducea problembmWI1tobedifficult to yourgiveI1problemto showthatyourproblemis difficult.Suchprobkmsaredescribedin Chapter6.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

8 PROBLEMSOLVINGTECHNIQUES 9

If all we hadto computewasthe largestintegerin the set, the opti-mumapproachwouldbe to form 5 、ゥ ッゥョエ subsets81,…, 85 of 8/ sorteachsubset, and then sort {max 81,…, max85}. This takes6 calls toSORT5but leavesambiguityaboutthesecondandthird largestintegers.

It may seemlike manycalls to SORT5 are still needed.Howeverifyou do a carefulcaseanalysisandeliminateall ク X for whichthereareat least3 integersin 8 larger than X/ P ャ ケ 5 integersremainandhencejust onemorecall to SORT5is neededto computethe result. Detailsaregivenin thesolutionto Problem2.5.

FIND A GOOD DATA STRUCTURE

Supposeyou are given a setof files, eachcontainingstockquoteinfor-mation.Eachline containsstartswith a エゥュ・ウエ ーN Thefiles are ゥ ョ 、 ゥ イ ゥ 、 ᆳ

ually sortedby this value.You areto designanalgorithmthatcombinesthesequotesinto a single file R containingthesequotes, sortedby theョ ・ ウ エ 。 ュ ー ウ N

This problemcanbe solvedby amultistagemergeprocess, but thereis a trivial solutionusing a min-heapdatastructure, wherequotesareorderedby timestamp.Firstbuild themin-heapwith thefirst quotefromeachfile; theniterativelyextracttheminimumentrye from themin-heap/キイゥエ・ エッ R, and add in the next entry in the file correspondingto e.Detailsaregivenin Problem2.10.

ITERATIVE REFINEMENT OF BRUTE-FORCESOLUTION

Considertheproblemof stringsearch(cf Problem5.1): giventwo stringss (searchstring) andT (text), find all occurrencesof s in T. Sincescanoccurat any offset in T , thebrute-forcesolutionis to testfor a matchatevery offset. This algorithm is perfectlycorrect; its time complexity isoHN m)/ wheren andm arethelengthsof sandT.

After trying someexamples, youmayseethatthereareseveralwaysin which to ゥ イ ー イ ッ カ ・ the time complexity of the brute-forcealgorithm.For example, if thecharacterT[i] is not presentin s you cansuitablyad-vancethematching.Furthermore, this skippingworksbetterif wematchthe searchstring from its endandwork backwards.Theserefinementswill makethealgorithmveryfast(linear-time)onrandomtextandsearchstrings;however, theworstcase」 ッ ュ ー ャ ・ ク ケ remainso H ᄋ ュ I N

You can make the additional ッ 「 ウ ・ イ イ 。 エ ゥ ッ ョ that a partial match of swhich doesnot resultin a full matchimplies otheroffsetswhich cannotleadto full matches.For ・ ク ョ ー ャ ・ if ウ 「、 「・。「・ andiff startingback-wards, wehavea partialmatchup エ ッ 「 ・ 「 ・ that 、 ッ ・ ウ ッ エ resultin a fullmatch, we know that thenextpossiblematchingoffsethasto be at least3 positionsahead(wherewe canmatchthe ウ ・ 」 ッ ョ 、 「 ・ from the partialmatch).

By puttiI1g togethertheserefinemeI1tsyou will havearrived at thefamousBoyer-Moorestringsearchalgorithm-itsworstmcasetime C m-plexityisoh+m)(whichisthebestpossibleffomatheoreticalperspecmtivek it is alsooneof thefasteststrhgsearchdFrithmsh practice.

SMALL EXAMPLES

Problemsthat seemdifficult to solve in the abstract, canbecomemuchュ ッ イ 。 」 エ 。 「 ャ ・ whenyouexaminesmallconcreteinstances.For ゥ ョ ウ エ 。 ョ」 ッ ウ ゥ 、 ・ イ エ ャ ッ ャ ャ ッ キ ゥ ァ problem:there 。 P P 」 ャ ッ 、 ッ ッ イ ウ 。 ャ ッ 。 イ ゥ ᆳdorr numberedfrom1t0500.ApersOI1walksthroughthe corridorandopenseachdoor.AIIotherpersonwalksthroughtke corridorandcloseseveryalternatedoor. c ッ ョ エ ゥ ョ オ ゥ ョ ァ エ ィ ゥ ウ ュ ュ ・ エ ィ ・ i-th personcaI1dtogglesthepositionof everyt-th doorstarthgfromdoort.y;to determineexactlyhow manydoorsareopmafterthe 500-thpersOIIhaswalkedthroughthecorridor.

It isverydifficult to solvethisproblemushgabstractvariables.How-everif you try theproblemfor ljp374710?md20doorsrit takesmderammteto seethatthedoorsthatremahopmarel?479716.··Fregard-lessof thetotalI1umberof doors.Thepatternis obvious-thedoorsthatイ ・ 。 ュ ッ ー 。 イ ・ ッ ウ ・ numberedby perfectsquares. Onceyou makethscOImeetlOIL1tlseasytOEZ?veit for thegeneralcase-HeIIcethetotdnumberof opendoorsis ly500J= 22. r・ヲ・イセエッ Problem9A for a detailedsolution.

REDUCTION

c ッ ョ ウ ゥ 、 ・ エ ィ ・ probkmof fiMing if om ウ エ ァ is a rotationof the other,e.g., B」。イB Q、 Harc"arerotatiORsof eachotherA I1aturdapproachmaybeto rotatethe first strhgbyeverypomibleoffsetaM ttmcomar4キ ゥ エ ィ ・ secondウ エ ァ N This algorithmwould havequadraticエ ゥ 」 ッ ュ ᆳplexity.

You may I1otice that this problemis quite ウ ョ ゥ ャ 。 イ to string searchwhich cmbedomh1inear-tmeralbeitmhga somewhatcomplexalmgorithm.Soit would beI1aturdto try to reducethis problemto stringsearch.IndeedrifwecomatemtethesecondstringwithitselfaMsearcEfor thefirst styingh tke resultingstring, wewill find amatchiff thetwooriginalstrhgsarerotatiOI1sof eachother.Thisreductionyieldsa linear-time algorithmfor ourproblem;detailsaregiveRiRProbkII15.4.

Usually you try to reduceyour ー イ ッ 「 ャ ・ セ to an easierproblem. Butsometmesryou needto reducea problembmWI1tobedifficult to yourgiveI1problemto showthatyourproblemis difficult.Suchprobkmsaredescribedin Chapter6.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

10 PROBLEMSOLVINGTECHNIQUES 11

GRAPH MODELING

Drawingpicturesis a greatway to brainstormfor a potentialウ ッ ャ オ エ ゥ ッ ョ Nthe relati(;nshipsin a given problemcanbe representedusinga graph,auiteoftentheproblemcanbereducedto a well-knowngraphproblem.ッ イ example, supposeyou are given a setof barterratesbetweencom-

moditiesandyouaresupposedto find out ゥ ヲ Q arbitrageexists, i.e., thereis a wayby which you canstartwith オ ョ ゥ エ ウ of somecommodityC andperforma seriesof barterswhich resultsin havingmore エ ィ 。 ョ オ ョ ゥ エ ウ ofC.

We canmodelthe problemwith a graphwherecommoditiescorre-spondto vertices, barterscorrespondt? edges, Nセセ the :d?e,:eight セ ウウ セ エ to the logarithmof thebarterrate. If we canfind a cyclein the graphwith apositkeweightrwewouldhavefOUI1d sucha seriesof exchnges.Sucha cycle canbe solvedusingthe b ・ ャ ャ ュ 。 f ッ イ 、 algorithm (cf. Prob-lem4.19).

After some(or a lot) of エ イ M 。 ョ 、 M ・ イ イ ッ イ you may beginto wonderifa sucha configurationexists. Provhard. Howeverif you think of the 8 x 8 squareboardas a chessboard,you will observethat the removedcomersareof the samecolor. Hencetheboardconsistsof either30white squaresand32blacksquaresor viceversa. Sincea dominowill alwayscover two adjacentsquares, anyar-rangementof dominoesmustcoverthesamenumberof blackandwhitesquares.Henceno suchconfigurationexists.

The ッイ ゥョ。ャ problemdid not talk about the colors of the squares.Adding thesecolorsto the squaresmakesit easyto proveimpossibility,illustratingthestrategyof addingauxiliaryelements.

VARIATION

WRITEANEQUATION

Someproblemscanbe solvedby expressingthem in the languageofmathematics.For example, supposeyou were 。 ウ ォ ・ セ to write an algo-rithm thatcomputedbinomial 」 ッ ・ ヲ ヲ ゥ 」 ゥ ・ ョ g I ]

The problemwith computingthe binomial coefficientdirectly fromthe definition is that the factorial function grows very quickly and canoverflow an integervariable. If we use floating point イ ・ ー イ ・ ウ ・ エ 。 エ ゥ ッ ョ ウ

for numbers, we loseprecisionandtheproblemof overflow doesョ _ セ goaway. Theseーイ ッ 「 ャ ・ セ ウ potentiallyexist evenif the final value.of G) ゥセsmall. One 」 セ try to factor thenumeratoranddenominatorandtry andcancelout 」 ッ ュ ュ ッ エ ・ イ ュ ウ but factorizationis itself a hardproblem.

Thebinomialcoefficientssatisfytheadditionformula:

Supposewe wereaskedto designanalgorithmwhich takesasinput anundirectedgraphandproducesasoutputablackor whitecoloringof theverticessuchthat for everyvertex, at leasthalf of its neighborsdiffer incolor ヲ イ ッ ュ N

We could try to solve this problemby assigningarbitrary colors toverticesandthenflipping colorswhereverconstraintsarenotmet. How-everthis approachdoesnot convergeP 。 ャ ャ examples.

It turnsoutwecandefinea slightly differentproblemwhosesolutionwill yield the coloringwe arelooking for. Define anedgeto bediverseifits endshavedifferentcolors. It is easyto verify thata color assignmentthatmaximizesthenumberof diverseedgesalsosatisfiesthe constraintof the originalproblem.Thenumberof diverseedgescanbe ュ 。 ャ ゥ コ ・ 、

greedilyflipping the colorsof verticesthatwould leadto a highernum-berof diverseedges;detailsare ァ ゥ カ ・ ゥ ョ Problem4.11.

PARALLELISM|

Qᄋb

QᄋᄋT

ォ//IlI\

+|

ャャャO

ャ-kOiャゥエ|ャゥャOnkO

iiiM

In thecontextof interview ア オ ・ ウ エ ゥ ッ ウ parallelismis usefulwhendealingwith scale, i.e., whentheproblemis solargethatit ゥ ウ ー ッ ウ ウ ゥ 「 ャ ・ to solveit on a singlemachineor it would takea very long time. Thekey insightyouneedto displayis how to decomposetheproblemsuchthat(1.) eachsubproblemcanbesolvedrelativelyindependentlyand(2.) constructingthesolutionto the ッ イ ゥ ョ 。 ャ problemfrom solutionsto thesubproblemsisnotexpensivein termsof CPUtime, mainmemory, andnetworkusage.

Considertheproblemof sortingapetascaleintegerarray.If weknowthe distribution of the numbers, the bestapproachwould be to defineequal-sizedrangesof integersand sendone rangeto one machineforsorting. The sortednumberswould just needto be concatenatedin thecorrectorder. If the distribution ゥ ウ ッ エ known thenwe cansendequal-sizedarbitrarysubsetsto eachmachineandthenmergethesortedresults

This identity leads to a straightforwardrecursionfor computing H セ Iwhich avoids the problemsmentionedabove. Dynamic programminghas to be usedto achievegood time complexity-detailsare in p イ ッ 「

lem9.1.

AUXILIARY ELEMENTS

Consideran8 x 8squareboardin whichtwo squaresP 、 ゥ 。 ァ ッ ョ 。 ャ ャ ケ oppo-sitecornersareremoved.You aregivenasetof thirty-one2 x 1 dominoesandareaskedto covertheboardwith them.

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

10 PROBLEMSOLVINGTECHNIQUES 11

GRAPH MODELING

Drawingpicturesis a greatway to brainstormfor a potentialウ ッ ャ オ エ ゥ ッ ョ Nthe relati(;nshipsin a given problemcanbe representedusinga graph,auiteoftentheproblemcanbereducedto a well-knowngraphproblem.ッ イ example, supposeyou are given a setof barterratesbetweencom-

moditiesandyouaresupposedto find out ゥ ヲ Q arbitrageexists, i.e., thereis a wayby which you canstartwith オ ョ ゥ エ ウ of somecommodityC andperforma seriesof barterswhich resultsin havingmore エ ィ 。 ョ オ ョ ゥ エ ウ ofC.

We canmodelthe problemwith a graphwherecommoditiescorre-spondto vertices, barterscorrespondt? edges, Nセセ the :d?e,:eight セ ウウ セ エ to the logarithmof thebarterrate. If we canfind a cyclein the graphwith apositkeweightrwewouldhavefOUI1d sucha seriesof exchnges.Sucha cycle canbe solvedusingthe b ・ ャ ャ ュ 。 f ッ イ 、 algorithm (cf. Prob-lem4.19).

After some(or a lot) of エ イ M 。 ョ 、 M ・ イ イ ッ イ you may beginto wonderifa sucha configurationexists. Provhard. Howeverif you think of the 8 x 8 squareboardas a chessboard,you will observethat the removedcomersareof the samecolor. Hencetheboardconsistsof either30white squaresand32blacksquaresor viceversa. Sincea dominowill alwayscover two adjacentsquares, anyar-rangementof dominoesmustcoverthesamenumberof blackandwhitesquares.Henceno suchconfigurationexists.

The ッイ ゥョ。ャ problemdid not talk about the colors of the squares.Adding thesecolorsto the squaresmakesit easyto proveimpossibility,illustratingthestrategyof addingauxiliaryelements.

VARIATION

WRITEANEQUATION

Someproblemscanbe solvedby expressingthem in the languageofmathematics.For example, supposeyou were 。 ウ ォ ・ セ to write an algo-rithm thatcomputedbinomial 」 ッ ・ ヲ ヲ ゥ 」 ゥ ・ ョ g I ]

The problemwith computingthe binomial coefficientdirectly fromthe definition is that the factorial function grows very quickly and canoverflow an integervariable. If we use floating point イ ・ ー イ ・ ウ ・ エ 。 エ ゥ ッ ョ ウ

for numbers, we loseprecisionandtheproblemof overflow doesョ _ セ goaway. Theseーイ ッ 「 ャ ・ セ ウ potentiallyexist evenif the final value.of G) ゥセsmall. One 」 セ try to factor thenumeratoranddenominatorandtry andcancelout 」 ッ ュ ュ ッ エ ・ イ ュ ウ but factorizationis itself a hardproblem.

Thebinomialcoefficientssatisfytheadditionformula:

Supposewe wereaskedto designanalgorithmwhich takesasinput anundirectedgraphandproducesasoutputablackor whitecoloringof theverticessuchthat for everyvertex, at leasthalf of its neighborsdiffer incolor ヲ イ ッ ュ N

We could try to solve this problemby assigningarbitrary colors toverticesandthenflipping colorswhereverconstraintsarenotmet. How-everthis approachdoesnot convergeP 。 ャ ャ examples.

It turnsoutwecandefinea slightly differentproblemwhosesolutionwill yield the coloringwe arelooking for. Define anedgeto bediverseifits endshavedifferentcolors. It is easyto verify thata color assignmentthatmaximizesthenumberof diverseedgesalsosatisfiesthe constraintof the originalproblem.Thenumberof diverseedgescanbe ュ 。 ャ ゥ コ ・ 、

greedilyflipping the colorsof verticesthatwould leadto a highernum-berof diverseedges;detailsare ァ ゥ カ ・ ゥ ョ Problem4.11.

PARALLELISM|

Qᄋb

QᄋᄋT

ォ//IlI\

+|

ャャャO

ャ-kOiャゥエ|ャゥャOnkO

iiiM

In thecontextof interview ア オ ・ ウ エ ゥ ッ ウ parallelismis usefulwhendealingwith scale, i.e., whentheproblemis solargethatit ゥ ウ ー ッ ウ ウ ゥ 「 ャ ・ to solveit on a singlemachineor it would takea very long time. Thekey insightyouneedto displayis how to decomposetheproblemsuchthat(1.) eachsubproblemcanbesolvedrelativelyindependentlyand(2.) constructingthesolutionto the ッ イ ゥ ョ 。 ャ problemfrom solutionsto thesubproblemsisnotexpensivein termsof CPUtime, mainmemory, andnetworkusage.

Considertheproblemof sortingapetascaleintegerarray.If weknowthe distribution of the numbers, the bestapproachwould be to defineequal-sizedrangesof integersand sendone rangeto one machineforsorting. The sortednumberswould just needto be concatenatedin thecorrectorder. If the distribution ゥ ウ ッ エ known thenwe cansendequal-sizedarbitrarysubsetsto eachmachineandthenmergethesortedresults

This identity leads to a straightforwardrecursionfor computing H セ Iwhich avoids the problemsmentionedabove. Dynamic programminghas to be usedto achievegood time complexity-detailsare in p イ ッ 「

lem9.1.

AUXILIARY ELEMENTS

Consideran8 x 8squareboardin whichtwo squaresP 、 ゥ 。 ァ ッ ョ 。 ャ ャ ケ oppo-sitecornersareremoved.You aregivenasetof thirty-one2 x 1 dominoesandareaskedto covertheboardwith them.

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

12

usinga min-heap.For detailson petascalesorting, pleaserefer to Prob-lem2.2.

CACHING

Cachingis a greattool wheneverthereis a possibility of repeatingcom-putations.For example, the centralideabehinddynamicprogrammingis cachingresultsfrom intermediatecomputations.Cachingbecomesex-tremely useful in anothersettingwhere requestscome to a serviceinan online fashionand a small numberof requeststakeup a significantamountof computepower.Workloadsonwebservicesexhibit thisprop-erty; Problem7.1 describesonesuchproblem.

s y m Q e t r y

While symmetryis a simpleconceptit canbeusedto solvevery difficultproblems, sometimesin less than intuitive ways. Considera 2-playerァ ョ ・ in which playersalternatelytakebites from a chocolatebar. Thechocolatebaris an ク m rectangle;abite mustremovea squareandallsquaresaboveandto theright in thechocolatebar. Thefirst ー i イ ・ イ to eatthelower leftmostsquareloses(think of it asbeingpoisoned).

Supposewe areaskedwhetherwe would preferto play first or sec-ond. One approachis to makethe ッ 「 ウ ・ イ イ 。 エ ゥ ッ ョ that the gameis sym-metrical for Player1 andPlayer2/ exceptfor their startingstate. If weassumethatthereis nowinningstrategyfor Player1/ thentheremustbea way for Player2 to win if Player1bitesthetop right squarein his firstmove. WhatevermovePlayer2 makesafter thatcanalwaysbemadebyPlayer1 ashis ヲ ウ エ move. HencePlayer1 canalwayswin. For a detaileddiscussion, refer to theProblem9.13.

CONCLUSION

In addition to developingintuition for which techniquemay apply towhich problem, it is alsoimportantto know whenyour techniqueis notキ ッ イ Q Y andquickly moveto your nextbestguess.In aninterview set-ting/ evenif you do not endup solving the problementirely, you willgetcredit for applyingtheseエ ・ 」 ュ オ ・ ウ in a systematicway andclearlycommunicatingyour approachto the problem. We covernontechnicalaspectsof problemsolvingin Chapter12.

Part I

Problems

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

12

usinga min-heap.For detailson petascalesorting, pleaserefer to Prob-lem2.2.

CACHING

Cachingis a greattool wheneverthereis a possibility of repeatingcom-putations.For example, the centralideabehinddynamicprogrammingis cachingresultsfrom intermediatecomputations.Cachingbecomesex-tremely useful in anothersettingwhere requestscome to a serviceinan online fashionand a small numberof requeststakeup a significantamountof computepower.Workloadsonwebservicesexhibit thisprop-erty; Problem7.1 describesonesuchproblem.

s y m Q e t r y

While symmetryis a simpleconceptit canbeusedto solvevery difficultproblems, sometimesin less than intuitive ways. Considera 2-playerァ ョ ・ in which playersalternatelytakebites from a chocolatebar. Thechocolatebaris an ク m rectangle;abite mustremovea squareandallsquaresaboveandto theright in thechocolatebar. Thefirst ー i イ ・ イ to eatthelower leftmostsquareloses(think of it asbeingpoisoned).

Supposewe areaskedwhetherwe would preferto play first or sec-ond. One approachis to makethe ッ 「 ウ ・ イ イ 。 エ ゥ ッ ョ that the gameis sym-metrical for Player1 andPlayer2/ exceptfor their startingstate. If weassumethatthereis nowinningstrategyfor Player1/ thentheremustbea way for Player2 to win if Player1bitesthetop right squarein his firstmove. WhatevermovePlayer2 makesafter thatcanalwaysbemadebyPlayer1 ashis ヲ ウ エ move. HencePlayer1 canalwayswin. For a detaileddiscussion, refer to theProblem9.13.

CONCLUSION

In addition to developingintuition for which techniquemay apply towhich problem, it is alsoimportantto know whenyour techniqueis notキ ッ イ Q Y andquickly moveto your nextbestguess.In aninterview set-ting/ evenif you do not endup solving the problementirely, you willgetcredit for applyingtheseエ ・ 」 ュ オ ・ ウ in a systematicway andclearlycommunicatingyour approachto the problem. We covernontechnicalaspectsof problemsolvingin Chapter12.

Part I

Problems

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

1.1. COMPUTINGSQUAREROOTS 15

Chapter1

Searching

Searchingis abasictool thateveryprogrammershouldkeepin mindfor usein awidevarietyofsituations.

"TheArt of ComputerProgramming, Volume3 - Sorting

andSearching," D. Knuth, 1973

Given an arbitrary collectionof ォ・ケウ the only way to determineif asearchkey is presentis by examhhgeachdemeI1twhich yields o H I

complexity.If the collectionisHorgmizedHFsearchingcmbe spedupdramatically.Of course, insertsanddeleteshaveto preservethe organi-zation;thereareseveralwaysof achievingthis.

Binary Search

Bhafysearchis at theheartof moreinterviewquestiomthm my othershglealgorithm.Flmdamentally}binarysearchis a mturddivide-md-COI1querstrategyfor searchhg.Theideais to eliminatehalf thekeysfromconsiderationby keepingthe keysin a sortedarray. If the searchkey isI10t equalto themiddleelementof thearray}OI1eof tke Wo setsof keysto エ ィ ・ セ ャ ・ ヲ エ andto the right of themiddleelementcanbeeliminatedfromfurtherconsideration.

Questionsbasedonbinarysearchareidealfrom theinterviewersper-spective: it is a basictechniquethat everyreasonablecandidateis sup-

ウ ・ 、 to know andit canbe ゥ ュ ー ャ ・ ュ ・ エ ・ 、 in a few lines of code.On theィ ・ イ hand, binarysearchis muchtrickier to ゥ ュ ー ャ ・ ュ ・ エ correctlythanit

appears-youshouldimplementit aswell aswrite cornercaseteststoensureyou understandit ー イ ッ ー ・ イ A

Many publishedimplementationsare ゥ ョ 」 ッ イ イ ・ 」 エ ウ オ 「 エ ャ ・ andnot-so-subtleways-astudyreportedthat it is correctly implementedin onlyfive out of twentytextbooks.JonBentley, in hisbookProgrammingPearlsreportedthathe assignedbinarysearchin a coursefor professionalpro-grammersand ヲ ッ オ 、 that90% ー ・ イ 」 ・ ヲ 。 ゥ ャ ・ 、 to 」 ッ 、 ・ 」 ッ イ イ ・ 」 エ ャ ケ despitehaving ample time. (Bentley's studentswould havebeengratified toknowthathisown ー オ 「 ャ ゥ ウ ィ ・ 、 ョ ー ャ ・ ュ ・ ョ エ 。 エ ゥ ッ ョ of binarysearch, in achap-ter titled "Writing CorrectPrograms"/ containedabugthatremainedun-detectedfor overtwentyyears.)

Binarysearchcanbewritten in manyways-recursive, iterative, 、ゥferent idioms for conditionals, etc. Here is an iterative ゥ ュ ー ャ ・ ュ ・ ョ エ 。 エ ゥ ッadaptedfrom Bentley'sbook, which includeshisbug.

1 I public class BinSearch {2 I static int search( int [] A, int K ) {3 I int I = 0;4 I int u = A. i・ エィ -1;5 lint m;6 I while ( I <= U ) {

7 I m = (l+u) /2;8 I if (A[m] < K) {9 I I = m + 1;

10 I } else if (A[m] == K)11 I return m;12 I } else {13 I u = m-l;14 I15 I }16 I return -1;1718

ャ・ error is in the assignmentm = (1+u) /2; it canleadto ッ カ ・ イ ッ キ

andshouldbereplacedby m = 1 + (u-l) /2.The time complexityof binarysearchis givenby B H I = 」 bH ORIN

This solvesto B H I = oHャッァ O which is far superiorto the O(n) ap-proachneededwhen the keys are unsorted. A disadvantageof bi-nary searchis that it requiresa sorted 。 イ イ 。 ケ ャ 、 sortingan arraytakeso H ャ ッ ァ エ ゥ ュ ・ N Howeverif therearemanysearchesto perform, thetimetakento sortis not anissue.

Webeginwith a problemthaton thefaceof it hasnothingto dowithbinarysearch.

1.1 COMPUTING SQUARE ROOTS

Squareroot computationscanbe implementedusingsophisticatednu-merical techniquesinvolving iterativemethodsand logarithms. How-everif you wereaskedto ゥ ュ ー ャ ・ ュ ・ エ a squareroot function, you wouldnotbeexpectedto know thesetechniques.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

1.1. COMPUTINGSQUAREROOTS 15

Chapter1

Searching

Searchingis abasictool thateveryprogrammershouldkeepin mindfor usein awidevarietyofsituations.

"TheArt of ComputerProgramming, Volume3 - Sorting

andSearching," D. Knuth, 1973

Given an arbitrary collectionof ォ・ケウ the only way to determineif asearchkey is presentis by examhhgeachdemeI1twhich yields o H I

complexity.If the collectionisHorgmizedHFsearchingcmbe spedupdramatically.Of course, insertsanddeleteshaveto preservethe organi-zation;thereareseveralwaysof achievingthis.

Binary Search

Bhafysearchis at theheartof moreinterviewquestiomthm my othershglealgorithm.Flmdamentally}binarysearchis a mturddivide-md-COI1querstrategyfor searchhg.Theideais to eliminatehalf thekeysfromconsiderationby keepingthe keysin a sortedarray. If the searchkey isI10t equalto themiddleelementof thearray}OI1eof tke Wo setsof keysto エ ィ ・ セ ャ ・ ヲ エ andto the right of themiddleelementcanbeeliminatedfromfurtherconsideration.

Questionsbasedonbinarysearchareidealfrom theinterviewersper-spective: it is a basictechniquethat everyreasonablecandidateis sup-

ウ ・ 、 to know andit canbe ゥ ュ ー ャ ・ ュ ・ エ ・ 、 in a few lines of code.On theィ ・ イ hand, binarysearchis muchtrickier to ゥ ュ ー ャ ・ ュ ・ エ correctlythanit

appears-youshouldimplementit aswell aswrite cornercaseteststoensureyou understandit ー イ ッ ー ・ イ A

Many publishedimplementationsare ゥ ョ 」 ッ イ イ ・ 」 エ ウ オ 「 エ ャ ・ andnot-so-subtleways-astudyreportedthat it is correctly implementedin onlyfive out of twentytextbooks.JonBentley, in hisbookProgrammingPearlsreportedthathe assignedbinarysearchin a coursefor professionalpro-grammersand ヲ ッ オ 、 that90% ー ・ イ 」 ・ ヲ 。 ゥ ャ ・ 、 to 」 ッ 、 ・ 」 ッ イ イ ・ 」 エ ャ ケ despitehaving ample time. (Bentley's studentswould havebeengratified toknowthathisown ー オ 「 ャ ゥ ウ ィ ・ 、 ョ ー ャ ・ ュ ・ ョ エ 。 エ ゥ ッ ョ of binarysearch, in achap-ter titled "Writing CorrectPrograms"/ containedabugthatremainedun-detectedfor overtwentyyears.)

Binarysearchcanbewritten in manyways-recursive, iterative, 、ゥferent idioms for conditionals, etc. Here is an iterative ゥ ュ ー ャ ・ ュ ・ ョ エ 。 エ ゥ ッadaptedfrom Bentley'sbook, which includeshisbug.

1 I public class BinSearch {2 I static int search( int [] A, int K ) {3 I int I = 0;4 I int u = A. i・ エィ -1;5 lint m;6 I while ( I <= U ) {

7 I m = (l+u) /2;8 I if (A[m] < K) {9 I I = m + 1;

10 I } else if (A[m] == K)11 I return m;12 I } else {13 I u = m-l;14 I15 I }16 I return -1;1718

ャ・ error is in the assignmentm = (1+u) /2; it canleadto ッ カ ・ イ ッ キ

andshouldbereplacedby m = 1 + (u-l) /2.The time complexityof binarysearchis givenby B H I = 」 bH ORIN

This solvesto B H I = oHャッァ O which is far superiorto the O(n) ap-proachneededwhen the keys are unsorted. A disadvantageof bi-nary searchis that it requiresa sorted 。 イ イ 。 ケ ャ 、 sortingan arraytakeso H ャ ッ ァ エ ゥ ュ ・ N Howeverif therearemanysearchesto perform, thetimetakento sortis not anissue.

Webeginwith a problemthaton thefaceof it hasnothingto dowithbinarysearch.

1.1 COMPUTING SQUARE ROOTS

Squareroot computationscanbe implementedusingsophisticatednu-merical techniquesinvolving iterativemethodsand logarithms. How-everif you wereaskedto ゥ ュ ー ャ ・ ュ ・ エ a squareroot function, you wouldnotbeexpectedto know thesetechniques.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

16 CHAPTER1. SEARCHING 1.7. INTERSECTTWOSORTEDARRAYS 17

Problem1.1:Implementa fasthtegersquareroot functiOI1thattakesin a 32-bit オ ョ ウ ゥ ァ ョ ・ 、 ャ エ ・ ァ ・ イ andreturnsanother32-bitunsignedintegerthatis thefloor of thesquareroot of theinput.

TherearemanyvariaI1tsof searchhga sortedarraythat requirea littlemoretUinkhandcmteopportunitiesformissingcomermesFortheヲ ッ ャ ャ ッ キ ゥ ー イ ッ 「 ャ ・ ュ ウ A is a sortedarrayof ゥ イ ァ ・ イ ウ N

1.2 SEARCHA SORTEDARRAY FOR k

Write a methodthat takesa sortedarrayA of integersanda key k mdretumsthe hdexof first occurrmceof k h A.Retum-l if k doesRotappearin A. Write teststo verify your code.

1.3 SEARCHA SORTEDARRAY FOR THE FIRST ELEMENT LARGER

THANk

DesignamfacieIItalgorithm1hatfiMsthe iMex ofthe ヲ ゥ イ ウ エ ッ 」 」 オ イ イ ・

。 ョ ・ ャ ・ ュ ャ ・ i Q エ larger エ ィ 。 ョ Q a ウ ー・ cQ ・、 key k; return M i ゥ ヲ everyelementisless エ ィ 。 ョ ャ ッ イ equalエ ッ ォ N

1.4 SEARCHA SORTEDARRAY FOR A[i] = i

RZRUR [RSRZゥコエエ[ZQ[RRャエZコエ[ZA[i] = i or ゥ ョ 、 ゥ 」 。 エ ゥ エ ィ 。 エ no ウ ィ indexexists.

1.5 SEARCHAN ARRAY OF UNKNOWN LENGTH

supposeyou dOROtknowthelenghofAhadvame;accemingA[tlfori beymdtheendof thearraythrowsm ・ク」・ーエQP

Problem1.5: Find the index of the first occurrencein A of a specifiedkey k; return-1 if k doesnot appearin A.

1.6 MISSING ELEMENT, LIMITED RESOURCES

ャ ・ storagecapacityof harddrivesdwarfsthatof RAM. This 」 。 ャ ・ 。 、 tointerestingtime-spacetradeoffs.

Problem1.6: Givena file containingroughly300million socialsecurityIIIbers(9-digitI1umbers)yfiI1d a 9-digit numberthat ismth the file.

You haveunlimited drive spacebut only2megabytesof RAM at yo r

disposal.

1.7 INTERSECT t ッ SORTEDARRAYS

A natural ゥ ュ ー ャ ・ ュ ・ ョ エ 。 エ ゥ ッ ヲ ッ イ a searchengineis to retrievedocumentsthatmatchthesetof wordsin a queryby ュ 。 ゥ ョ ゥ ョ ァ aninvertedindex.Eachpageis assignedan integeridentifier, its dOGument-id.An ゥ ョ カ カ ・ イ 、

ゥ i ャ 、 ・ ゥ ウ a mapping エ ィ 。 エ takesa word 。 ョ 、 returnsa sorted 。 イ イ 。 ケ ッ ヲ

p 。 ァ ・ M ゥ 、 ウ キ ィ ゥ 」 ィ 」 o i ャ エ 。 ゥ ョ エ ィ ・ sortordercouldbe, for ・ ク ャ ー ャ ・ thepagerank in descendingorder. When a querycontainsmultiple words, thesearchenginefinds the sortedarrayfor eachword and thencomputesthe intersectionof thesearrays-theseare the pagescontainingall thewords in the query. The mostcomputationallyintensivestepof doingthis is finding theintersectionof thesortedarrays.

Problem1.7: Given sortedarraysA andB of lengthsnandm respec-エ ゥ カ ・ ャ イ ・ エ オ イ ョ an arrayC coャ ョゥ ・ャ・ュ・ョエウ commonto A andB. ThearrayC shouldbefree of duplicates.How would youperformthis inter-sectionゥ ヲ N H Q N I ュ and H R N I ᆱ ュ _

Hashing

Hashingis anotherapproachto searching.Hashingis qualitativelydif-ferentfrom binarysearch-theideaof hashingis to storekeysin anarrayof lengthm. Keysarestoredin arraylocationsbasedon the "hashcode"of thekey. Thehashcodeis anintegercomputedfrom thekeyby ahashfunction. If the hashfunction is chosenwell, the keys are distributedacrossthearraylocationsuniformly randomly.

Thereis 。ャキ。ケウ ・ possibilityof two keysmappingto thesameloca-エ ゥ ッ ゥ ョ whichcasea"collision" is saidto ッ 」 」 オ イ N G ィ ・ standardmechanismto dealwith collisionsis to maintaina linked list of keysateachlocation.Lookups, inserts, anddeletestake P H Q O m) complexity, キィ・イ・ゥウ thenumberof keys.If the"load" n/mgrowslarge, thetablecanberehashedto onewith a largernumberof locations;thekeysaremovedto thenewtable.Rehashingis expensive(e H K m) time)butif it is performedinfre-quently(for example, if performedeverytime theloadincreasesby 2x),its amortizedcostis low.

Comparedtobinarysearchtrees(discussedonPage20), insertinganddeletingin ahashtableis moreefficient (assumingtheloadis constant).One disadvantageof hashingis the needfor a goodhashfunction butthis is rarely an issuein practice. Similarly, rehashingis not a problemoutsideof realtimesystemsandevenfor suchsystems, a separatethreadcanperformtherehashing.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

16 CHAPTER1. SEARCHING 1.7. INTERSECTTWOSORTEDARRAYS 17

Problem1.1:Implementa fasthtegersquareroot functiOI1thattakesin a 32-bit オ ョ ウ ゥ ァ ョ ・ 、 ャ エ ・ ァ ・ イ andreturnsanother32-bitunsignedintegerthatis thefloor of thesquareroot of theinput.

TherearemanyvariaI1tsof searchhga sortedarraythat requirea littlemoretUinkhandcmteopportunitiesformissingcomermesFortheヲ ッ ャ ャ ッ キ ゥ ー イ ッ 「 ャ ・ ュ ウ A is a sortedarrayof ゥ イ ァ ・ イ ウ N

1.2 SEARCHA SORTEDARRAY FOR k

Write a methodthat takesa sortedarrayA of integersanda key k mdretumsthe hdexof first occurrmceof k h A.Retum-l if k doesRotappearin A. Write teststo verify your code.

1.3 SEARCHA SORTEDARRAY FOR THE FIRST ELEMENT LARGER

THANk

DesignamfacieIItalgorithm1hatfiMsthe iMex ofthe ヲ ゥ イ ウ エ ッ 」 」 オ イ イ ・

。 ョ ・ ャ ・ ュ ャ ・ i Q エ larger エ ィ 。 ョ Q a ウ ー・ cQ ・、 key k; return M i ゥ ヲ everyelementisless エ ィ 。 ョ ャ ッ イ equalエ ッ ォ N

1.4 SEARCHA SORTEDARRAY FOR A[i] = i

RZRUR [RSRZゥコエエ[ZQ[RRャエZコエ[ZA[i] = i or ゥ ョ 、 ゥ 」 。 エ ゥ エ ィ 。 エ no ウ ィ indexexists.

1.5 SEARCHAN ARRAY OF UNKNOWN LENGTH

supposeyou dOROtknowthelenghofAhadvame;accemingA[tlfori beymdtheendof thearraythrowsm ・ク」・ーエQP

Problem1.5: Find the index of the first occurrencein A of a specifiedkey k; return-1 if k doesnot appearin A.

1.6 MISSING ELEMENT, LIMITED RESOURCES

ャ ・ storagecapacityof harddrivesdwarfsthatof RAM. This 」 。 ャ ・ 。 、 tointerestingtime-spacetradeoffs.

Problem1.6: Givena file containingroughly300million socialsecurityIIIbers(9-digitI1umbers)yfiI1d a 9-digit numberthat ismth the file.

You haveunlimited drive spacebut only2megabytesof RAM at yo r

disposal.

1.7 INTERSECT t ッ SORTEDARRAYS

A natural ゥ ュ ー ャ ・ ュ ・ ョ エ 。 エ ゥ ッ ヲ ッ イ a searchengineis to retrievedocumentsthatmatchthesetof wordsin a queryby ュ 。 ゥ ョ ゥ ョ ァ aninvertedindex.Eachpageis assignedan integeridentifier, its dOGument-id.An ゥ ョ カ カ ・ イ 、

ゥ i ャ 、 ・ ゥ ウ a mapping エ ィ 。 エ takesa word 。 ョ 、 returnsa sorted 。 イ イ 。 ケ ッ ヲ

p 。 ァ ・ M ゥ 、 ウ キ ィ ゥ 」 ィ 」 o i ャ エ 。 ゥ ョ エ ィ ・ sortordercouldbe, for ・ ク ャ ー ャ ・ thepagerank in descendingorder. When a querycontainsmultiple words, thesearchenginefinds the sortedarrayfor eachword and thencomputesthe intersectionof thesearrays-theseare the pagescontainingall thewords in the query. The mostcomputationallyintensivestepof doingthis is finding theintersectionof thesortedarrays.

Problem1.7: Given sortedarraysA andB of lengthsnandm respec-エ ゥ カ ・ ャ イ ・ エ オ イ ョ an arrayC coャ ョゥ ・ャ・ュ・ョエウ commonto A andB. ThearrayC shouldbefree of duplicates.How would youperformthis inter-sectionゥ ヲ N H Q N I ュ and H R N I ᆱ ュ _

Hashing

Hashingis anotherapproachto searching.Hashingis qualitativelydif-ferentfrom binarysearch-theideaof hashingis to storekeysin anarrayof lengthm. Keysarestoredin arraylocationsbasedon the "hashcode"of thekey. Thehashcodeis anintegercomputedfrom thekeyby ahashfunction. If the hashfunction is chosenwell, the keys are distributedacrossthearraylocationsuniformly randomly.

Thereis 。ャキ。ケウ ・ possibilityof two keysmappingto thesameloca-エ ゥ ッ ゥ ョ whichcasea"collision" is saidto ッ 」 」 オ イ N G ィ ・ standardmechanismto dealwith collisionsis to maintaina linked list of keysateachlocation.Lookups, inserts, anddeletestake P H Q O m) complexity, キィ・イ・ゥウ thenumberof keys.If the"load" n/mgrowslarge, thetablecanberehashedto onewith a largernumberof locations;thekeysaremovedto thenewtable.Rehashingis expensive(e H K m) time)butif it is performedinfre-quently(for example, if performedeverytime theloadincreasesby 2x),its amortizedcostis low.

Comparedtobinarysearchtrees(discussedonPage20), insertinganddeletingin ahashtableis moreefficient (assumingtheloadis constant).One disadvantageof hashingis the needfor a goodhashfunction butthis is rarely an issuein practice. Similarly, rehashingis not a problemoutsideof realtimesystemsandevenfor suchsystems, a separatethreadcanperformtherehashing.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

18CHAPTER1. SEARCHING 1.13. ROBOTBATTERYCAPACITY 19

1.8 ANAGRAMS

a 。ァイ Qウ arepopularword play puzzlesrwhereby rearranghglettersof onesetof words you getmothersetof words.ForexampleyHel

ュ anigramforutwdveplus oneFF cュウウキッイ、ーャ i・ en-tziz: Z ゥ ォ ・ be able to ァ ・ イ Qq B ゥ カ ・ イ Q setof letters.

i213:23eZ1132UIZtLZZ;ZJEaZZ1.9 SEARCHFORA PAIR WHICH SUMS TO 8

Let A bea sortedarrayof integersand8 a targetinteger.

Problem1.9:Designm efficientalgorithmfor determiniI1gif thereexistapairof hdiceskjhotmcessadlydisthct)suchthat a ュ a { ェ } =8.

1.10 ANONYMOUS LETTER

A hashcanbe viewed as a dictionary. As a result, hashing」 ッ ュ ュ 。 ャ ケappearswhenprocessingwith strings.

Z コ エ エ コ Z Q S R R R R R R Q Z Z イ ゥ コ コ コ エi エ ィ ッ 、 is to returntrueif L cmbewritter111shgllf mdfalseotherwise.Q W 。 イ appearsk timesin L, it ュ オ 。 ー ー エ least ォ I

1.11 PAIRING USERSBY ATTRIBUTES

You arebuilding a ウッ」ゥ ・エキッヲ ヲ attributes. You would like エ 。 ー 。 ゥ イ eachuserwith 。 イ ャ ッ エ ィ ・ イ unpaired

l s ・ イ Nゥ p ・ 」 ゥ ヲ ケ you aregiven 。 オ ・ イ ッ ヲ オ ウ ・ キ ィ ・ イ ・

R S ォ [ W ケ Z Z Z Z イ コ Z R Z [ [ _ ヲ s コ Z R [ Z Z Z f [ エ Z Z コ Z コ [ Z [ [ Te [ コ コ Z Z Z Z イ _ Q z Z [ Z Z Z ゥ [ Z Z [ イ Z Z Z R _ Z R ゥ エ Z エ Z [ Z Z イ _ [ [ Z [ Z エ コ [ イ イエ ィ ・ オ ョ ャ ー 。 ゥ イ ・ 、 set.

_ エ Z W S Z Q R j z [ Q R R Q Z R R [ コ Z [ [ ゥ ゥof attributesaswell?

1.12 MISSING ELEMENT

Hashingcanbeusedto find anelementwhich is not ー イ ・ ウ ・ エ in a g1Ven

set.

Problem1.12: GivenanarrayA of integers, find 。 ゥ ョ エ ・ ァ ・ イ k that ゥ ウ 。 エ

ー イ ・ ウ ・ エ ゥ ョ a N Assumethattheintegersare32-bitsignedintegers.

1.13 ROBOT BATTERY CAPACITY

A robot needsto travel along a path that includesseveralascentsanddescents.When it goesup, it usesits batteryas a sourceof energyandwhenit goesdown, it recoversthe ー 。 エ ・ ゥ 。 ャ energybackinto thebattery.The batteryrechargingprocessis ideal: on descending, everyJoule ofgravitationalpotentialenergyconvertsinto a Jouleof electricalenergythat is storedin thebattery. Thebatteryhasa limited capacityandonceit reachesits storagecapacity, theenergygeneratedfrom therobotgoingdownis lost.

Problem 1.13: Given a robot with the energy regenerationabilitydescribedabove, the massof the robot m and a sequenceof three-dimensionalco-ordinatesthat the robot needsto traverse, how wouldyou determinethe minimum batterycapacityneededfor the robot tocompletethe trajectory? (Assumethe robot startswith a ヲ オャャ ケ 」ィ。 イァ ・、

batteryandthebatteryis used。 ャ ケ for ッ カ カ ・ イ ュ ゥ i ァ gravity.)

1.14 SEARCHFOR MAJORITY

Thereare severalapplicationswhereyou want to identify tokensin agivenstreamthathavemorethana certainfraction of the total numberof occurrencesin a relativelyinexpensivemanner.For ・ ク ー i ・ we maywantto identify theusersusingthelargestfractionof thenetworkband-width or IP addressesoriginatingthemostHTTP requests.Herewe willtry to solvea simplifiedversionof this problemcalled"majority-find".

Problem1.14: You are readinga sequenceof words from a very longstream.You knowapriori thatmorethanhalf thewordsarerepetitionsofasinglewordW but thepositionswhereW occursareunknown.Designan efficient algorithmthat readsthis streamonly P 」 ・ andusesonly aconstantamountof memoryto identify W.

1.15 SEARCHFORFREQUENTITEMS

In practice, wemaynotbeinterestedin just themajority tokenbutall thetokenswhosecountexceedssay1% of the total tokencount. It is easyto showthat it ゥ ウ ー ッ ウ ウ ゥ 「 ャ ・ to do this in a singlepasswhenyou havelimited memorybutif youareallowedto passthroughthestreamtwice,it is possibleto ゥ 、 ・ ョ エ ゥ t thecommontokens.

Problem1.15: You arereadinga sequenceof stringsseparatedby whitespacefrom averylargestream.You areallowedto readthe ウ エ イ ・ ョ twice.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

18CHAPTER1. SEARCHING 1.13. ROBOTBATTERYCAPACITY 19

1.8 ANAGRAMS

a 。ァイ Qウ arepopularword play puzzlesrwhereby rearranghglettersof onesetof words you getmothersetof words.ForexampleyHel

ュ anigramforutwdveplus oneFF cュウウキッイ、ーャ i・ en-tziz: Z ゥ ォ ・ be able to ァ ・ イ Qq B ゥ カ ・ イ Q setof letters.

i213:23eZ1132UIZtLZZ;ZJEaZZ1.9 SEARCHFORA PAIR WHICH SUMS TO 8

Let A bea sortedarrayof integersand8 a targetinteger.

Problem1.9:Designm efficientalgorithmfor determiniI1gif thereexistapairof hdiceskjhotmcessadlydisthct)suchthat a ュ a { ェ } =8.

1.10 ANONYMOUS LETTER

A hashcanbe viewed as a dictionary. As a result, hashing」 ッ ュ ュ 。 ャ ケappearswhenprocessingwith strings.

Z コ エ エ コ Z Q S R R R R R R Q Z Z イ ゥ コ コ コ エi エ ィ ッ 、 is to returntrueif L cmbewritter111shgllf mdfalseotherwise.Q W 。 イ appearsk timesin L, it ュ オ 。 ー ー エ least ォ I

1.11 PAIRING USERSBY ATTRIBUTES

You arebuilding a ウッ」ゥ ・エキッヲ ヲ attributes. You would like エ 。 ー 。 ゥ イ eachuserwith 。 イ ャ ッ エ ィ ・ イ unpaired

l s ・ イ Nゥ p ・ 」 ゥ ヲ ケ you aregiven 。 オ ・ イ ッ ヲ オ ウ ・ キ ィ ・ イ ・

R S ォ [ W ケ Z Z Z Z イ コ Z R Z [ [ _ ヲ s コ Z R [ Z Z Z f [ エ Z Z コ Z コ [ Z [ [ Te [ コ コ Z Z Z Z イ _ Q z Z [ Z Z Z ゥ [ Z Z [ イ Z Z Z R _ Z R ゥ エ Z エ Z [ Z Z イ _ [ [ Z [ Z エ コ [ イ イエ ィ ・ オ ョ ャ ー 。 ゥ イ ・ 、 set.

_ エ Z W S Z Q R j z [ Q R R Q Z R R [ コ Z [ [ ゥ ゥof attributesaswell?

1.12 MISSING ELEMENT

Hashingcanbeusedto find anelementwhich is not ー イ ・ ウ ・ エ in a g1Ven

set.

Problem1.12: GivenanarrayA of integers, find 。 ゥ ョ エ ・ ァ ・ イ k that ゥ ウ 。 エ

ー イ ・ ウ ・ エ ゥ ョ a N Assumethattheintegersare32-bitsignedintegers.

1.13 ROBOT BATTERY CAPACITY

A robot needsto travel along a path that includesseveralascentsanddescents.When it goesup, it usesits batteryas a sourceof energyandwhenit goesdown, it recoversthe ー 。 エ ・ ゥ 。 ャ energybackinto thebattery.The batteryrechargingprocessis ideal: on descending, everyJoule ofgravitationalpotentialenergyconvertsinto a Jouleof electricalenergythat is storedin thebattery. Thebatteryhasa limited capacityandonceit reachesits storagecapacity, theenergygeneratedfrom therobotgoingdownis lost.

Problem 1.13: Given a robot with the energy regenerationabilitydescribedabove, the massof the robot m and a sequenceof three-dimensionalco-ordinatesthat the robot needsto traverse, how wouldyou determinethe minimum batterycapacityneededfor the robot tocompletethe trajectory? (Assumethe robot startswith a ヲ オャャ ケ 」ィ。 イァ ・、

batteryandthebatteryis used。 ャ ケ for ッ カ カ ・ イ ュ ゥ i ァ gravity.)

1.14 SEARCHFOR MAJORITY

Thereare severalapplicationswhereyou want to identify tokensin agivenstreamthathavemorethana certainfraction of the total numberof occurrencesin a relativelyinexpensivemanner.For ・ ク ー i ・ we maywantto identify theusersusingthelargestfractionof thenetworkband-width or IP addressesoriginatingthemostHTTP requests.Herewe willtry to solvea simplifiedversionof this problemcalled"majority-find".

Problem1.14: You are readinga sequenceof words from a very longstream.You knowapriori thatmorethanhalf thewordsarerepetitionsofasinglewordW but thepositionswhereW occursareunknown.Designan efficient algorithmthat readsthis streamonly P 」 ・ andusesonly aconstantamountof memoryto identify W.

1.15 SEARCHFORFREQUENTITEMS

In practice, wemaynotbeinterestedin just themajority tokenbutall thetokenswhosecountexceedssay1% of the total tokencount. It is easyto showthat it ゥ ウ ー ッ ウ ウ ゥ 「 ャ ・ to do this in a singlepasswhenyou havelimited memorybutif youareallowedto passthroughthestreamtwice,it is possibleto ゥ 、 ・ ョ エ ゥ t thecommontokens.

Problem1.15: You arereadinga sequenceof stringsseparatedby whitespacefrom averylargestream.You areallowedto readthe ウ エ イ ・ ョ twice.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

20 CHAPTER1. SEARCHING 1.18. SEARCHINGTWOSORTEDARRAYS 21

Deviseanalgorithmthatusesonly O(k) memoryto identify all thewordsthatoccurmorethan I セ l timesin thestream, キィ・イ・ゥウ the i ・ エ ィ ofthestream.

Binary SearchTrees

A problemwith arraysis addingand deletingelementsto an array iscomputationallyexpensive, particularly when the array needsto staysorted. Binary SearchTrees(BSTs) are similar to arraysin that the keysare in a sortedorderbut they areeasierto performinsertionsanddele-tions into. BSTs requiremore spacethanarrayssinceeachnodehastohavea pointerto its childrenandits ー 。 イ ・ N

Thekey lookup, insert, anddeleteoperationsfor BSTstaketime pro-portionalto the height of the tree, which canin worst-casebe X H I ifゥョウ・イエウ ャ、 deletesare ョ 。 ゥ Z イ ・ ャ ケ implemented. However there 。 イ ・ l

plementationsof insertanddeletewhich guaranteethe tree has ィ ・ ゥ ァH ャ ッ ァ N Theserequirestoringand 、 。 エ ゥ 。 、 、 ゥ エ ゥ ッ ョ 。 ャ dataat the treenodes. Red-blacktreesarean ・ ク i ・ of suchbalancedBSTs andtheyaretheworkhorseof moderndata-structurelibraries-forexample, theyareusedin theC++ STL library to implementsets.

Keepin mind thatBSTsare, in certainrespects, qualitativelydifferentfrom the treesdescribedin Chapter5 (Algorithms on Graphs) 。 ョ 、 ゥ ウ

importantto understandthesedifferences.Specifically, in a BST, thereispositionalityaswell asorderassociatedwith thechildrenof nodes.Fur-thermore, thevaluesstoredat nodeshaveto respecttheBST property-thekey storedat a nodeis greaterthanor equalto thekeysstoredin thenodesof its left subchildandlessthanor equalto thevaluesstoredin thenodesof its right subchild.

1.16 SEARCHBST FOR A KEY

Searchingfor a key in aBST is verysimilar to searchingin a sortedarray.Recursionis morenaturalbut for performance, awhile-loopis preferred.

Problem1.16: Given a BST t イウエ write a recursivefunction thatsearchesfor key K , thenwrite aniterativefunction.

1.17 SEARCHBST FORx> k

BSTsoffer morethanthe 。 「 ゥ ャ ゥ エ ッ searchfor a key-theycanbeusedtofind themin andmaxelements, look for the successoror predecessorofa given searchkey (which mayormay not be presentin the BST), andenumeratetheelementsin a sortedorder.

Problem1.17: Givena BSTT anda key K , write amethodthatsearchesfor the first entrylargerthanK.

1.18 SEARCHINGTWO SORTEDARRAYS

GiveI1a sortedarray Ar if you want to fhd the bth smaHestelementFY?u cm simply retum a { ォ Q } which is an 0(1) ッ ー ・ イ 。 エ ゥ ッ i ヲ you aregiventwo sortedarraysof i ・ ァ エ ィ 。 ョ 、 m andyouneedto ヲ ャ 、 the k-thsmallestelementh the uniOI1of the Wo arraysryou could poteI1tidlymergethetwo sorted。 イ イ 。 ケ ウ Q 、 thmlookfor themswerbutthatwouldtakeO(n+m)time.Youcanbuildthemergedarray P ャ ケ till the first k・ャ・ュ・ Ntィゥウ wouldbea O(k)operation-cmyoudobetterthanthis?

Problem118:YouaregivenWo sortedarraysof lengthsm andn.Givea oHャッァュKャセI timealgorithmfor 」 ッ ュ ー オ エ ゥ エ ィ ・ k-th ウ ュ 。 ャ ャ ・ ウ エ ョ ・ ョ エiI1the uniOI1of theWo arrays.keepiRmiIId that the elementsmayberepeated.

1.19 INTERSECTINGLINES

slfposeyou are designinga rectmgularprMed circuit board (PCB)item you aresupposedto conz1ecta setof pohtsfrom one ・ 、 エ ッ an-

ッ エ ィ ・ イ ウ ・ エ ッ ヲ ー ッ ゥ ョ エ ウ _ エ エ ィ ・ ッ ー ー ・ 、 ァ ・ M t ィ ・ ュ ・ エ 。 ャ ャ ゥ ョ ・ ウ 」 ッ ュ ・ 」 エ ォ エ ィ ・points shouldI10tmtersectwith eachother;otherwisertherewill be ashortcircuit. yッオ イェッ 「ゥ ウ エ ッ、・ エ・ イ Gュ

ャゥョャ・ ウ ッA Q thePCBsurfacein away エ ィ 。 エ avoidsshortcircuits. Let'sassumewe comecteachpair usinga straightline of metal.It is a ーイッカ・ ヲm

thatyoucmcom1ectthepairswithut intersectiOI1(usingeitherstraightedlhes)iEyoucmCORRectthemusingstraightlinesthatdo not

intersect.

ー Q・ュゥMQYZhowwPQQQ、ケッオ、・エ・イュゥョ・ゥヲ。ァゥカ・ュ・エッヲウエュゥァィエャゥョ・ウintersectin a givenrectangleor not?

1.20 CONTAINED INTERVALS

h variousapplications(suchashyhgout computerchips)Fit ゥ ウ ャ ーtanttofiMwhmagivemhapeis 」 ッ ュ ー Q ケ 」 ッ ュ ゥ ョ ・ 、 ゥ ョ ウ ゥ 、 ・ mohershape.l ・ ィ f s T o 。 ュ p ャ ・ イ v カ ・ イ ・ イ ウ G ウ ゥ ッ ョ of エ ィ ゥ ウ p イ ッ o ィ i ・ イ ・ キ ・ 。 イ Nc ・ ュ ・ 、 with line ウ ・ ァ ュ ・ ョ エ ウ 。 ャ ッ i Q Y a straightャ ゥ ョ ャ ・ ッ

Problem130:Writea fUIICHon that takesa setof opmhtervalson therealline H ゥ bi ) for i E サP Q _M I} and determinesif thereexistssomeinterval H _ 「 ャ I エ ィ 。 エ ゥ ウ completelycORtainedinsideamtherintervalH ュ bm ). If ウ ィ pairsof intervalsexist, then イ ・ エ ョ onesuchー。 ゥ

1.21 VIEW FROM t e TOP

tィ 。 ウゥューャゥヲゥ・、 mュッヲ ?prob1mhtoftm comesuph computerァ イ ィ i c s M ケ ッ オ aregIveIIamillIOI1overlapphglinesegmentsof differeRt

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

20 CHAPTER1. SEARCHING 1.18. SEARCHINGTWOSORTEDARRAYS 21

Deviseanalgorithmthatusesonly O(k) memoryto identify all thewordsthatoccurmorethan I セ l timesin thestream, キィ・イ・ゥウ the i ・ エ ィ ofthestream.

Binary SearchTrees

A problemwith arraysis addingand deletingelementsto an array iscomputationallyexpensive, particularly when the array needsto staysorted. Binary SearchTrees(BSTs) are similar to arraysin that the keysare in a sortedorderbut they areeasierto performinsertionsanddele-tions into. BSTs requiremore spacethanarrayssinceeachnodehastohavea pointerto its childrenandits ー 。 イ ・ N

Thekey lookup, insert, anddeleteoperationsfor BSTstaketime pro-portionalto the height of the tree, which canin worst-casebe X H I ifゥョウ・イエウ ャ、 deletesare ョ 。 ゥ Z イ ・ ャ ケ implemented. However there 。 イ ・ l

plementationsof insertanddeletewhich guaranteethe tree has ィ ・ ゥ ァH ャ ッ ァ N Theserequirestoringand 、 。 エ ゥ 。 、 、 ゥ エ ゥ ッ ョ 。 ャ dataat the treenodes. Red-blacktreesarean ・ ク i ・ of suchbalancedBSTs andtheyaretheworkhorseof moderndata-structurelibraries-forexample, theyareusedin theC++ STL library to implementsets.

Keepin mind thatBSTsare, in certainrespects, qualitativelydifferentfrom the treesdescribedin Chapter5 (Algorithms on Graphs) 。 ョ 、 ゥ ウ

importantto understandthesedifferences.Specifically, in a BST, thereispositionalityaswell asorderassociatedwith thechildrenof nodes.Fur-thermore, thevaluesstoredat nodeshaveto respecttheBST property-thekey storedat a nodeis greaterthanor equalto thekeysstoredin thenodesof its left subchildandlessthanor equalto thevaluesstoredin thenodesof its right subchild.

1.16 SEARCHBST FOR A KEY

Searchingfor a key in aBST is verysimilar to searchingin a sortedarray.Recursionis morenaturalbut for performance, awhile-loopis preferred.

Problem1.16: Given a BST t イウエ write a recursivefunction thatsearchesfor key K , thenwrite aniterativefunction.

1.17 SEARCHBST FORx> k

BSTsoffer morethanthe 。 「 ゥ ャ ゥ エ ッ searchfor a key-theycanbeusedtofind themin andmaxelements, look for the successoror predecessorofa given searchkey (which mayormay not be presentin the BST), andenumeratetheelementsin a sortedorder.

Problem1.17: Givena BSTT anda key K , write amethodthatsearchesfor the first entrylargerthanK.

1.18 SEARCHINGTWO SORTEDARRAYS

GiveI1a sortedarray Ar if you want to fhd the bth smaHestelementFY?u cm simply retum a { ォ Q } which is an 0(1) ッ ー ・ イ 。 エ ゥ ッ i ヲ you aregiventwo sortedarraysof i ・ ァ エ ィ 。 ョ 、 m andyouneedto ヲ ャ 、 the k-thsmallestelementh the uniOI1of the Wo arraysryou could poteI1tidlymergethetwo sorted。 イ イ 。 ケ ウ Q 、 thmlookfor themswerbutthatwouldtakeO(n+m)time.Youcanbuildthemergedarray P ャ ケ till the first k・ャ・ュ・ Ntィゥウ wouldbea O(k)operation-cmyoudobetterthanthis?

Problem118:YouaregivenWo sortedarraysof lengthsm andn.Givea oHャッァュKャセI timealgorithmfor 」 ッ ュ ー オ エ ゥ エ ィ ・ k-th ウ ュ 。 ャ ャ ・ ウ エ ョ ・ ョ エiI1the uniOI1of theWo arrays.keepiRmiIId that the elementsmayberepeated.

1.19 INTERSECTINGLINES

slfposeyou are designinga rectmgularprMed circuit board (PCB)item you aresupposedto conz1ecta setof pohtsfrom one ・ 、 エ ッ an-

ッ エ ィ ・ イ ウ ・ エ ッ ヲ ー ッ ゥ ョ エ ウ _ エ エ ィ ・ ッ ー ー ・ 、 ァ ・ M t ィ ・ ュ ・ エ 。 ャ ャ ゥ ョ ・ ウ 」 ッ ュ ・ 」 エ ォ エ ィ ・points shouldI10tmtersectwith eachother;otherwisertherewill be ashortcircuit. yッオ イェッ 「ゥ ウ エ ッ、・ エ・ イ Gュ

ャゥョャ・ ウ ッA Q thePCBsurfacein away エ ィ 。 エ avoidsshortcircuits. Let'sassumewe comecteachpair usinga straightline of metal.It is a ーイッカ・ ヲm

thatyoucmcom1ectthepairswithut intersectiOI1(usingeitherstraightedlhes)iEyoucmCORRectthemusingstraightlinesthatdo not

intersect.

ー Q・ュゥMQYZhowwPQQQ、ケッオ、・エ・イュゥョ・ゥヲ。ァゥカ・ュ・エッヲウエュゥァィエャゥョ・ウintersectin a givenrectangleor not?

1.20 CONTAINED INTERVALS

h variousapplications(suchashyhgout computerchips)Fit ゥ ウ ャ ーtanttofiMwhmagivemhapeis 」 ッ ュ ー Q ケ 」 ッ ュ ゥ ョ ・ 、 ゥ ョ ウ ゥ 、 ・ mohershape.l ・ ィ f s T o 。 ュ p ャ ・ イ v カ ・ イ ・ イ ウ G ウ ゥ ッ ョ of エ ィ ゥ ウ p イ ッ o ィ i ・ イ ・ キ ・ 。 イ Nc ・ ュ ・ 、 with line ウ ・ ァ ュ ・ ョ エ ウ 。 ャ ッ i Q Y a straightャ ゥ ョ ャ ・ ッ

Problem130:Writea fUIICHon that takesa setof opmhtervalson therealline H ゥ bi ) for i E サP Q _M I} and determinesif thereexistssomeinterval H _ 「 ャ I エ ィ 。 エ ゥ ウ completelycORtainedinsideamtherintervalH ュ bm ). If ウ ィ pairsof intervalsexist, then イ ・ エ ョ onesuchー。 ゥ

1.21 VIEW FROM t e TOP

tィ 。 ウゥューャゥヲゥ・、 mュッヲ ?prob1mhtoftm comesuph computerァ イ ィ i c s M ケ ッ オ aregIveIIamillIOI1overlapphglinesegmentsof differeRt

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

22CHAPTER1. SEARCHING

colorssituatedat 、 ゥ ヲ ヲ ・ イ ・ エ heights. i ュ ー ャ ・ ュ ・ エ a function thatdrawsthelinesasseenfrom thetop.

1.22 COMPLETIONSEARCH

Y()11 セ イ ー キ ッ イ ォ ゥ ョ ゥ ョ the financeoffice for ABC corporation.Thereare・ izegi mboyee i received $hs ゥ ゥ ョ c ッ ュ ャ ー ・ ョ ョ ウ 。c ッ ュ カ ・ ョ ウ 。 ゥ ッ i Q was$8.

T Q ゥ ウ カ ・ 。 エ ィ ・ corporationneedsto cut ー 。 ケ イ ッ ャ ャ ・ ョ ウ ・ ウ to $S'. Thec e o キ 。 ョ エ ウ エ ッ ー オ エ 。 」 。 ー ッ ョ salaries-everyemployeewhoearnedmorethan D ャ 。 ウ エ yearwill be paid D エ ィ ゥ ウ year; employeeswho earnedIエィ。ョDキゥャャ seeno changein their salary.

For examplerif(S17S27SLS4A)=(90730?100740720)aIIdSf =210,then60 is a suitablevaluefor N

Problem1.22: Designanefficient algorithmfor finding sucha ゥヲ one

exists.

1.23 MATRIX SEARCH

Let A be an n x n matrix whose ・ イ ウ arerealnumbers. a ウ ウ オ ・ thatalonganycolumnandalonganyrow of A, theentriesappearill mcreas-

ing sortedorder.

Problem1.23:DesigI1mefficieI1t algorithmthatdecideswhethera realmber Z appearsh A.How mmy mtries of A doesyour algorithm

inspecththeworst-case?Cmyou provea tigM lowerbomdthat mysuJhalgorithmhasto considerintheworst-case?

1.24 CHECKING SIMPLICITY

A polygonis definedto besimpleif noneof its edgesintersectwith eachotherexceptfor their endpoints.

Problem1.24: Give an0 (n ャッァ エゥュ・ algorithmto 、 ・ エ ・ イ イ イ ・ if a poly-gonwith n verticesis ウ ー i ・ N

Chapter2

Sorting

A descriptionis givenof anewmethodof sortingin therandom-accessstoreof acomputer.Themethodscomparesveryfavourablywith otherknownmethodsin speed, ineconomyof storage, andin easeof programming.

"Quicksort," C. Hoare, 1962

Sorting-…-rearranginga collectionof itemsinto increasingor decreasingorder-isa commonproblemin computing. Sortingis usedto prepro-cessthecollectionto makesearchingfaster(aswesawwith binarysearchthroughan array), aswell as to identify itemsthat aresimilar (e.g., stu-dentsaresortedon testscores).

NaIve sortingalgorithmsrun in 8 HRI time. There are 。 オ ュ 「 ・ イ ofsortingalgorithmswhich イ オ ゥ ョ o H N log n) time-Mergesort, Heapsort,andQuicksortareexamples.Eachhasits advantagesanddisadvantages:for example, Heapsortis in-placebut not stable;Mergesortis stablebutnot in-place.Most sortingroutinesarebasedon a comparefunction thattakestwo items as input and returns1 if the first item is smallerthanthe seconditem, 0 if theyareequaland-1 otherwise.Howeverit is alsopossibleto usenumericalattributesdirectly, e.g., in Radixsort.

2.1 GOOD SORTINGALGORITHMS

What is the most efficient sorting algorithm for eachof the followingsituations:

a smallarrayof integers.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

22CHAPTER1. SEARCHING

colorssituatedat 、 ゥ ヲ ヲ ・ イ ・ エ heights. i ュ ー ャ ・ ュ ・ エ a function thatdrawsthelinesasseenfrom thetop.

1.22 COMPLETIONSEARCH

Y()11 セ イ ー キ ッ イ ォ ゥ ョ ゥ ョ the financeoffice for ABC corporation.Thereare・ izegi mboyee i received $hs ゥ ゥ ョ c ッ ュ ャ ー ・ ョ ョ ウ 。c ッ ュ カ ・ ョ ウ 。 ゥ ッ i Q was$8.

T Q ゥ ウ カ ・ 。 エ ィ ・ corporationneedsto cut ー 。 ケ イ ッ ャ ャ ・ ョ ウ ・ ウ to $S'. Thec e o キ 。 ョ エ ウ エ ッ ー オ エ 。 」 。 ー ッ ョ salaries-everyemployeewhoearnedmorethan D ャ 。 ウ エ yearwill be paid D エ ィ ゥ ウ year; employeeswho earnedIエィ。ョDキゥャャ seeno changein their salary.

For examplerif(S17S27SLS4A)=(90730?100740720)aIIdSf =210,then60 is a suitablevaluefor N

Problem1.22: Designanefficient algorithmfor finding sucha ゥヲ one

exists.

1.23 MATRIX SEARCH

Let A be an n x n matrix whose ・ イ ウ arerealnumbers. a ウ ウ オ ・ thatalonganycolumnandalonganyrow of A, theentriesappearill mcreas-

ing sortedorder.

Problem1.23:DesigI1mefficieI1t algorithmthatdecideswhethera realmber Z appearsh A.How mmy mtries of A doesyour algorithm

inspecththeworst-case?Cmyou provea tigM lowerbomdthat mysuJhalgorithmhasto considerintheworst-case?

1.24 CHECKING SIMPLICITY

A polygonis definedto besimpleif noneof its edgesintersectwith eachotherexceptfor their endpoints.

Problem1.24: Give an0 (n ャッァ エゥュ・ algorithmto 、 ・ エ ・ イ イ イ ・ if a poly-gonwith n verticesis ウ ー i ・ N

Chapter2

Sorting

A descriptionis givenof anewmethodof sortingin therandom-accessstoreof acomputer.Themethodscomparesveryfavourablywith otherknownmethodsin speed, ineconomyof storage, andin easeof programming.

"Quicksort," C. Hoare, 1962

Sorting-…-rearranginga collectionof itemsinto increasingor decreasingorder-isa commonproblemin computing. Sortingis usedto prepro-cessthecollectionto makesearchingfaster(aswesawwith binarysearchthroughan array), aswell as to identify itemsthat aresimilar (e.g., stu-dentsaresortedon testscores).

NaIve sortingalgorithmsrun in 8 HRI time. There are 。 オ ュ 「 ・ イ ofsortingalgorithmswhich イ オ ゥ ョ o H N log n) time-Mergesort, Heapsort,andQuicksortareexamples.Eachhasits advantagesanddisadvantages:for example, Heapsortis in-placebut not stable;Mergesortis stablebutnot in-place.Most sortingroutinesarebasedon a comparefunction thattakestwo items as input and returns1 if the first item is smallerthanthe seconditem, 0 if theyareequaland-1 otherwise.Howeverit is alsopossibleto usenumericalattributesdirectly, e.g., in Radixsort.

2.1 GOOD SORTINGALGORITHMS

What is the most efficient sorting algorithm for eachof the followingsituations:

a smallarrayof integers.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

24 CHAPTER2. SORTING 2.6. LEASTd i s l n c e s o r t i n g 25

2.7 PRIVACY AND a n o n y ャ i z a t i o n

o u エ B a N r r a n セ

t セ セ s Q a ャ G エ j g U エ

o セ ヲ

\

latエr NN •QG P s l Z

ve hhセg cs ャ G g o セ G ゥU Q G 「 Q U AS mッカ eZNᄃ

seve aャ hッu S

I aエ vヲh Gy CLOSETo ヲヲ ovi a

L.INEf\ R SOON&> oNウ ッ セ ョ ャ c a N ウ

H G N N ッ ᆪ N Of s w g i ゥ

IS ᆪャ | FleAN1' \."im ッ r t セ n t e

cos, of c N o i G T p p | ャ | ヲ [ N

Figure2.

TheMassachusettsGroupInsuranceCommissionhadabright ideabackin themid 1990s-itdecidedto release"anonymized"dataon stateem-

2.6 LEAST DISTANCE SORTING

You comeacrossa collectionof 20 stonestatuesin a line. You want tosort themby height, with the shorteststatueon the ャ ・ ヲ エ N ャ ・ statuesareveryheavyandyouwantto movethemtheleastpossibledistance.

Problem2.6: Designa sorting algorithmthat minimizesthe total dis-tancethatthestatuesaremoved.

ッ エ change-ifA beatsB in onetime-trial andBbeatsC in anothertime-trial, thenA is guaranteedto beatC if theyarein thesametime-trial.

Problem2.5: What is theminimumnumberof time-trialsneededto de-terminewho to sendto theOlympics?

a largearraywhoseentriesarerandomnumbers.a largearrayof htegersthatis alreadyalmostsorted.a large collection of htegersthat are drawRfroma very smallrange.

-Aljfze collectionofnumbersmostofwhichareduplicatesM s エ 。 「 ゥ ゥ ゥ エ ケ ゥ ウ オ ゥ i.e., therelativeorderof two recordsthathave

thesamesorthgkey shouldmtbechanged.

2 .4 FINDING THE MIN AND MAX SIMULTANEOUSLY

iven a setof numbers, you canfind eitherthe min or maxof the setinN-lcomParisomseach.whmyouneedto fiI1d bothycmyoudobetterthan2N - 3 comparisons?

Problem2.4: Find themin andmaxelementsfrom a setof N elementsオ ウ ゥ ッ morethan3N/2 - 1comparisons.

2.5 EFFICIENT TRIALS

You arethe coachof a cycling エ ・ ョ with 25 membersandneedto deter-mine the fastest, second-fastest, andthird-fastestcyclistsfor selectiontotheOlympic エ ・

You will beevaluatingthecyclistsusinga time-trial courseP キ ィ ゥ 」 ィdy5cyclistscmraceata time.YoucmusethecompletiOIItimesfrom a

time-trialto rmk the5cyclistsamORgstthemselves-notiesarepossiblee cOI1ditionscaRChmgeover timer you camotcompareperfop

mmcesacrossdiffereI1t time-trials.Therelativespeedsof cyclistsdoes

2.3 FINDING t e WINNER AND RUNNER-UP

Thereare128playersparticipathgh atenI1istourI1ameIItAssumethatthe uz beatsyry relatimshipis tymsitiver i.e-F for allplayersAF By andCrif A beatsBandBbeatsC, thenA beatsC.

Problem2.3: What is the leastnumberof matcheswe needto organizeto fhd thebestplayer?HowmaI137matchesdoyouI1eedto fhd thebestandthesecondbestplayer?

2.2 TERASORT

The sorthgalgorithmsalludedto aboveassumethat all the datayouneedto sort will fit h the RAM.What if your datawill mt ヲ ゥ エ エ ィ ・

memory?

Problem2.2: Sorta file containing1012 100bytestrings.

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

24 CHAPTER2. SORTING 2.6. LEASTd i s l n c e s o r t i n g 25

2.7 PRIVACY AND a n o n y ャ i z a t i o n

o u エ B a N r r a n セ

t セ セ s Q a ャ G エ j g U エ

o セ ヲ

\

latエr NN •QG P s l Z

ve hhセg cs ャ G g o セ G ゥU Q G 「 Q U AS mッカ eZNᄃ

seve aャ hッu S

I aエ vヲh Gy CLOSETo ヲヲ ovi a

L.INEf\ R SOON&> oNウ ッ セ ョ ャ c a N ウ

H G N N ッ ᆪ N Of s w g i ゥ

IS ᆪャ | FleAN1' \."im ッ r t セ n t e

cos, of c N o i G T p p | ャ | ヲ [ N

Figure2.

TheMassachusettsGroupInsuranceCommissionhadabright ideabackin themid 1990s-itdecidedto release"anonymized"dataon stateem-

2.6 LEAST DISTANCE SORTING

You comeacrossa collectionof 20 stonestatuesin a line. You want tosort themby height, with the shorteststatueon the ャ ・ ヲ エ N ャ ・ statuesareveryheavyandyouwantto movethemtheleastpossibledistance.

Problem2.6: Designa sorting algorithmthat minimizesthe total dis-tancethatthestatuesaremoved.

ッ エ change-ifA beatsB in onetime-trial andBbeatsC in anothertime-trial, thenA is guaranteedto beatC if theyarein thesametime-trial.

Problem2.5: What is theminimumnumberof time-trialsneededto de-terminewho to sendto theOlympics?

a largearraywhoseentriesarerandomnumbers.a largearrayof htegersthatis alreadyalmostsorted.a large collection of htegersthat are drawRfroma very smallrange.

-Aljfze collectionofnumbersmostofwhichareduplicatesM s エ 。 「 ゥ ゥ ゥ エ ケ ゥ ウ オ ゥ i.e., therelativeorderof two recordsthathave

thesamesorthgkey shouldmtbechanged.

2 .4 FINDING THE MIN AND MAX SIMULTANEOUSLY

iven a setof numbers, you canfind eitherthe min or maxof the setinN-lcomParisomseach.whmyouneedto fiI1d bothycmyoudobetterthan2N - 3 comparisons?

Problem2.4: Find themin andmaxelementsfrom a setof N elementsオ ウ ゥ ッ morethan3N/2 - 1comparisons.

2.5 EFFICIENT TRIALS

You arethe coachof a cycling エ ・ ョ with 25 membersandneedto deter-mine the fastest, second-fastest, andthird-fastestcyclistsfor selectiontotheOlympic エ ・

You will beevaluatingthecyclistsusinga time-trial courseP キ ィ ゥ 」 ィdy5cyclistscmraceata time.YoucmusethecompletiOIItimesfrom a

time-trialto rmk the5cyclistsamORgstthemselves-notiesarepossiblee cOI1ditionscaRChmgeover timer you camotcompareperfop

mmcesacrossdiffereI1t time-trials.Therelativespeedsof cyclistsdoes

2.3 FINDING t e WINNER AND RUNNER-UP

Thereare128playersparticipathgh atenI1istourI1ameIItAssumethatthe uz beatsyry relatimshipis tymsitiver i.e-F for allplayersAF By andCrif A beatsBandBbeatsC, thenA beatsC.

Problem2.3: What is the leastnumberof matcheswe needto organizeto fhd thebestplayer?HowmaI137matchesdoyouI1eedto fhd thebestandthesecondbestplayer?

2.2 TERASORT

The sorthgalgorithmsalludedto aboveassumethat all the datayouneedto sort will fit h the RAM.What if your datawill mt ヲ ゥ エ エ ィ ・

memory?

Problem2.2: Sorta file containing1012 100bytestrings.

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

26 CHAPTER2. SORTING 2.10. MERGINGSORTEDARRAYS 27

ployeesthat showedeveryshglekospitalvisit theyhad.Thegoalwasto help the researchers.The state ウ ー ・ エ time removingidentifiers suchas name, addressymd social securityIIUmber-TM Governorof MaSEsachlmttsassuredthe public that this wassuffideI1t to pmtectpatmtprivacy-TheI1agraduatestudeI1trLataI1yasweeIIey>sawsignificmtpitafalls h this approach.Sherequesteda copyof the dataaRdby COIlathgthedatahmultipleColumRSrshewasableto idmtify thehealthrecordsof theGoverI1or.ThisdemonstratedthatextremecareI1eedsto betakerl

OIIymizing data.Oneway of msuriIIg privacyis to aggregatedatasuchthat any recordcmbemappedto at leastk iI1dividualSFfor some

largevalueof k.

Problem2.7:Supposeyou aregiveIIa matrix My whereeachrow rep-resentsm iI1dividual mdeachColumrepreseI1tsm attributeaboutthehdividualsuchasageor geI1der.GiveI1asetof ColumI1sto bedeletedyvouwmt to determheif eachrow hasat leastk duplicaterows with・ ク エ ャ ケ the samecontentsin the イ ・ ュ 。 ゥ ョ ゥ c ュ ョ ウ N How would youverify this efficiently?

2.8 VARIABLE LENGTH SORT

Most sortingalgorithmsイ P 。 basic ウ キ 。 ー N When recordsareofdifferentlengths, theswapstepbecomesnontrivial.

Problem2.8: Sort lines of a text file that hasa million lines suchthatthe averagelengthof a line is 100 charactersbut the longestline is onemillion characterslong.

2.9 UNIQUE ELEMENTS

supposeyou aregiveI1asetof mmesmdyour job is to producea setofUI1iqm first names.Ifyou just removethelastIlamefrom all thenayou mayhavesomeduplicatefirst names.

Problem2.9: How would you createa setof first namesthathaseachnameoccurring ャ ケ ッ ョ 」 ・ _ Specifically, designanefficient algorithmforremovingall theduplicatesfrom anarray.

ヲ 。 ク M ィ ・ 。 ー

Anotherdata-structurethatis usefulin diverse」 ッ エ ・ ク エ ウ is themax-heap,sometimesalsoreferredto asthepriority queue.(Thereis norelationshipbetweentheheapdata-structureandthe ー ッ イ エ ゥ ッ ッ ヲ memoryin a processbythesamemme.)AheapisakiMofabimrytree-itsupportsO(logn)iI1sertsmdCOI1stmttime lookupfor themaxelement.(Thembheapis

a completelysymmetricversionof the data-structureandsupportscon-stanttime lookupsfor themin element.) Searchingfor arbitrarykeyshaso H I time 」 ッ ュ ー ャ ・ ク ゥ エ ケ M 。 エ ィ ゥ エ ィ 。 エ canbe donewith a heapcanbedonewith a balancedBST with the samecomplexitybut with possiblysomespaceandtime overhead.

2.10 MERGING SORTEDARRAYS

You are given 500 files, eachcontainingstockquoteinformationfor anSP500company.Eachline containsanupdateof thefollowing form:

1232111 131 B 1000 2702212313 246 S 100 111.01

Thefirst numberis theupdatetime expressedasthenumberof millisec-ondssincethe startof the day'strading. Eachfile individually is sortedby this value. Your task is to createa single file containingall the up-datessortedby the updatetime. The individual files areof the orderof1-100megabytes;thecombinedfile will beof theorderof 5 gigabytes.

Problem2.10: Design an algorithm that takes the files as describedaboveandwrites a single file containingthe lines appearingin the in-dividual files sortedby theupdatetime. The algorithmshoulduseverylittle memory, ideally of theorderof a few kilobytes.

2.11 ApPROXIMATE SORT

cッ ウゥ、・イ a situationwhereyour datais almostウ ッ イ エ ・ 、 M ヲ ッ イ ・ ク ー i ・ youarereceivingtime-stampedstockquotesandearlierquotesmayarriveaf-terlaterquotesbecauseof differencesin serverloadsandnetworkroutes.Whatwouldbethemostefficientway of restoringthetotal order?

Problem2.11: Thereis a very long streamof integersarriving asan in-put suchthateachintegeris at mostonethousandpositionsawayfromits correctly sortedposition. Designan algorithm that outputsthe in-tegersin the correctorderandusesonly a constantamountof storage,i.e., the memoryusedshouldbe independentof the numberof integersprocessed.

2.12 RUNNING AVERAGES

Supposeyou aregivena real-valuedtime series(e.g., temperaturemea-suredby a sensor)with somenoise addedto it. In order to extractmeaningfultrendsfrom noisytime seriesdata, it is necessaryto performsmoothing.If thenoisehasa Gaussiandistributionandthenoiseaddedto successivesamplesis independentand identically distributed, then

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

26 CHAPTER2. SORTING 2.10. MERGINGSORTEDARRAYS 27

ployeesthat showedeveryshglekospitalvisit theyhad.Thegoalwasto help the researchers.The state ウ ー ・ エ time removingidentifiers suchas name, addressymd social securityIIUmber-TM Governorof MaSEsachlmttsassuredthe public that this wassuffideI1t to pmtectpatmtprivacy-TheI1agraduatestudeI1trLataI1yasweeIIey>sawsignificmtpitafalls h this approach.Sherequesteda copyof the dataaRdby COIlathgthedatahmultipleColumRSrshewasableto idmtify thehealthrecordsof theGoverI1or.ThisdemonstratedthatextremecareI1eedsto betakerl

OIIymizing data.Oneway of msuriIIg privacyis to aggregatedatasuchthat any recordcmbemappedto at leastk iI1dividualSFfor some

largevalueof k.

Problem2.7:Supposeyou aregiveIIa matrix My whereeachrow rep-resentsm iI1dividual mdeachColumrepreseI1tsm attributeaboutthehdividualsuchasageor geI1der.GiveI1asetof ColumI1sto bedeletedyvouwmt to determheif eachrow hasat leastk duplicaterows with・ ク エ ャ ケ the samecontentsin the イ ・ ュ 。 ゥ ョ ゥ c ュ ョ ウ N How would youverify this efficiently?

2.8 VARIABLE LENGTH SORT

Most sortingalgorithmsイ P 。 basic ウ キ 。 ー N When recordsareofdifferentlengths, theswapstepbecomesnontrivial.

Problem2.8: Sort lines of a text file that hasa million lines suchthatthe averagelengthof a line is 100 charactersbut the longestline is onemillion characterslong.

2.9 UNIQUE ELEMENTS

supposeyou aregiveI1asetof mmesmdyour job is to producea setofUI1iqm first names.Ifyou just removethelastIlamefrom all thenayou mayhavesomeduplicatefirst names.

Problem2.9: How would you createa setof first namesthathaseachnameoccurring ャ ケ ッ ョ 」 ・ _ Specifically, designanefficient algorithmforremovingall theduplicatesfrom anarray.

ヲ 。 ク M ィ ・ 。 ー

Anotherdata-structurethatis usefulin diverse」 ッ エ ・ ク エ ウ is themax-heap,sometimesalsoreferredto asthepriority queue.(Thereis norelationshipbetweentheheapdata-structureandthe ー ッ イ エ ゥ ッ ッ ヲ memoryin a processbythesamemme.)AheapisakiMofabimrytree-itsupportsO(logn)iI1sertsmdCOI1stmttime lookupfor themaxelement.(Thembheapis

a completelysymmetricversionof the data-structureandsupportscon-stanttime lookupsfor themin element.) Searchingfor arbitrarykeyshaso H I time 」 ッ ュ ー ャ ・ ク ゥ エ ケ M 。 エ ィ ゥ エ ィ 。 エ canbe donewith a heapcanbedonewith a balancedBST with the samecomplexitybut with possiblysomespaceandtime overhead.

2.10 MERGING SORTEDARRAYS

You are given 500 files, eachcontainingstockquoteinformationfor anSP500company.Eachline containsanupdateof thefollowing form:

1232111 131 B 1000 2702212313 246 S 100 111.01

Thefirst numberis theupdatetime expressedasthenumberof millisec-ondssincethe startof the day'strading. Eachfile individually is sortedby this value. Your task is to createa single file containingall the up-datessortedby the updatetime. The individual files areof the orderof1-100megabytes;thecombinedfile will beof theorderof 5 gigabytes.

Problem2.10: Design an algorithm that takes the files as describedaboveandwrites a single file containingthe lines appearingin the in-dividual files sortedby theupdatetime. The algorithmshoulduseverylittle memory, ideally of theorderof a few kilobytes.

2.11 ApPROXIMATE SORT

cッ ウゥ、・イ a situationwhereyour datais almostウ ッ イ エ ・ 、 M ヲ ッ イ ・ ク ー i ・ youarereceivingtime-stampedstockquotesandearlierquotesmayarriveaf-terlaterquotesbecauseof differencesin serverloadsandnetworkroutes.Whatwouldbethemostefficientway of restoringthetotal order?

Problem2.11: Thereis a very long streamof integersarriving asan in-put suchthateachintegeris at mostonethousandpositionsawayfromits correctly sortedposition. Designan algorithm that outputsthe in-tegersin the correctorderandusesonly a constantamountof storage,i.e., the memoryusedshouldbe independentof the numberof integersprocessed.

2.12 RUNNING AVERAGES

Supposeyou aregivena real-valuedtime series(e.g., temperaturemea-suredby a sensor)with somenoise addedto it. In order to extractmeaningfultrendsfrom noisytime seriesdata, it is necessaryto performsmoothing.If thenoisehasa Gaussiandistributionandthenoiseaddedto successivesamplesis independentand identically distributed, then

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

2.13 CIRCUIT SIMULATION

therunningaveragedoesa goodjob of ウュッッエィゥ N Howeverif thenoise」 。 ィ 。 カ ・ anarbitrarydistribution, thentherunningmediandoesabetterjob.

Problem2.12: Givena sequenceof trillion realnumberson a disk, howwould you computethe runningmeanof every thousandentries, i.e.,the first point would be the meanof {P } 。{YYY} the secondpointwouldbethemeanッ ヲ { Q } . . . ,a[1000] , thethird pointwouldbe エ ャ ョ ・ 。 ョ

of { R } W { Q P P Q } etc.? Repeatthe calculationfor medianratherthanャ ・ 。 ョ N

28 CHAPTER2. SORTING

Chapter3

Meta-algorithtns

While performingtiming analysisof a digital circuit, a componentischaracterizedby a Booleanヲ オ ョ 」 エ ゥ ッ ッ ヲ the Booleanvaluesat its inputsandthedelayof ー イ 。 ァ 。 エ ゥ ョ ァ changesfrom theinputsto theoutput. Forexample, a gatemay computethe AND function andhavea delayof 1nanosecondfrom eachinput to the outputor a wire may simply prop-agatesignal from one end to anotherin P N U 。 ョ ッ ウ ・ 」 ッ ョ 、 ウ N In order tosimulatehow the entire circuit would behavewhena set of inputs aregiven to the circuit, we use"event 、 イ ・ ョ simulation". Hereeacheventrepresentsa changein the signalvalueandtriggersoneor moreeventsin the future.

Problem2.13: You aregivenasetof nodes, V1 . . . ,Vn suchthatthevaluefor eachnodeat time to is O. An event(t ,v,p) is a triplet that representschangein thevaluefor nodev at time t to ー ッ エ ・ エ ゥ 。 ャ p (p canbeeither0 or1). You aregivena setof input events.Eachョ ッ 、 ・ 。 ャ ウ ッ hasa functionF i associatedwith it thatmapsan input eventto a setof outputevents(outputeventscanhappenonly after aninput event). How would youefficiently computeall theeventsthatwill happenasa resultof theinputevents?

Theimportantfact to observeisthatwehaveattemptedto solveamaximizationprobleminvolvingaparticularvalueof x andaparticularvalueof N by firstsolvingthegeneralprobleminvolving anarbitraryvalueof xandanarbitraryvalueof N.

"DynamicProgramming,"R. Bellman, 1957

DynamicProgramming

Therearea numberof approachesto designingalgorithms: exhaustivesearch, divide-and-conquer, greed)T, randomized, parallelization, back-tracking, heuristic, reduction, approximation, etc.

Problemswhich are naturally solvedusing dynamicprogramming(DP) area popularchoicefor hardinterview questions.DP is a generaltechniquefor solvingcomplexoptimizationproblemsthatcanbedecom-posedinto overlappingsubproblems.Like divide-and-conquer, wesolvetheproblembycombiningthesolutionsof multiplesmallerproblemsbutwhatmakesDP efficient is thatwe areableto reusethe intermediatere-sultsandoftendramaticallyreducethetime complexityby doingsol.

To illustrate the idea, considerthe simpleproblemof computingFi-bonaccinumbersdefinedby Fn = f ョ M Q f ョ R f ッ P andF1 = 1. A

lThe word Bーイッァイ。ュュゥョァB 、ケョ。ュゥ」 programmingdoesnot refer to computerprogramming-theword was chosenby RichardBellmanto describeaprogramin thesenseof aschedule.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

2.13 CIRCUIT SIMULATION

therunningaveragedoesa goodjob of ウュッッエィゥ N Howeverif thenoise」 。 ィ 。 カ ・ anarbitrarydistribution, thentherunningmediandoesabetterjob.

Problem2.12: Givena sequenceof trillion realnumberson a disk, howwould you computethe runningmeanof every thousandentries, i.e.,the first point would be the meanof {P } 。{YYY} the secondpointwouldbethemeanッ ヲ { Q } . . . ,a[1000] , thethird pointwouldbe エ ャ ョ ・ 。 ョ

of { R } W { Q P P Q } etc.? Repeatthe calculationfor medianratherthanャ ・ 。 ョ N

28 CHAPTER2. SORTING

Chapter3

Meta-algorithtns

While performingtiming analysisof a digital circuit, a componentischaracterizedby a Booleanヲ オ ョ 」 エ ゥ ッ ッ ヲ the Booleanvaluesat its inputsandthedelayof ー イ 。 ァ 。 エ ゥ ョ ァ changesfrom theinputsto theoutput. Forexample, a gatemay computethe AND function andhavea delayof 1nanosecondfrom eachinput to the outputor a wire may simply prop-agatesignal from one end to anotherin P N U 。 ョ ッ ウ ・ 」 ッ ョ 、 ウ N In order tosimulatehow the entire circuit would behavewhena set of inputs aregiven to the circuit, we use"event 、 イ ・ ョ simulation". Hereeacheventrepresentsa changein the signalvalueandtriggersoneor moreeventsin the future.

Problem2.13: You aregivenasetof nodes, V1 . . . ,Vn suchthatthevaluefor eachnodeat time to is O. An event(t ,v,p) is a triplet that representschangein thevaluefor nodev at time t to ー ッ エ ・ エ ゥ 。 ャ p (p canbeeither0 or1). You aregivena setof input events.Eachョ ッ 、 ・ 。 ャ ウ ッ hasa functionF i associatedwith it thatmapsan input eventto a setof outputevents(outputeventscanhappenonly after aninput event). How would youefficiently computeall theeventsthatwill happenasa resultof theinputevents?

Theimportantfact to observeisthatwehaveattemptedto solveamaximizationprobleminvolvingaparticularvalueof x andaparticularvalueof N by firstsolvingthegeneralprobleminvolving anarbitraryvalueof xandanarbitraryvalueof N.

"DynamicProgramming,"R. Bellman, 1957

DynamicProgramming

Therearea numberof approachesto designingalgorithms: exhaustivesearch, divide-and-conquer, greed)T, randomized, parallelization, back-tracking, heuristic, reduction, approximation, etc.

Problemswhich are naturally solvedusing dynamicprogramming(DP) area popularchoicefor hardinterview questions.DP is a generaltechniquefor solvingcomplexoptimizationproblemsthatcanbedecom-posedinto overlappingsubproblems.Like divide-and-conquer, wesolvetheproblembycombiningthesolutionsof multiplesmallerproblemsbutwhatmakesDP efficient is thatwe areableto reusethe intermediatere-sultsandoftendramaticallyreducethetime complexityby doingsol.

To illustrate the idea, considerthe simpleproblemof computingFi-bonaccinumbersdefinedby Fn = f ョ M Q f ョ R f ッ P andF1 = 1. A

lThe word Bーイッァイ。ュュゥョァB 、ケョ。ュゥ」 programmingdoesnot refer to computerprogramming-theword was chosenby RichardBellmanto describeaprogramin thesenseof aschedule.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

It is easyto definearecurrencerelationshipヲ ッ イ a (i , j). Thisis essentiallythe largestsequeI1cesumtill j-l addedto A[kl(or zero if that sumhappensto be negative). h ・ ョ 」 ・ a H ゥ j) = ュ。クHo aHゥ ェ - 1) + A[j]).Using this relationship, we cantabulate a H ャ ェ I for j {Q ゥョ linear-time. Oncewehaveall theseカ 。 ャ オ ・ s the 。 ョ ャ ウ キ ・ イ エ ッ our P イ ゥ ァ ゥ ョ 。 ャ ー イ ッ 「 ャ ・ ュ

ゥ ウ simply ュ ャ 。 x ェ { Q W z

pass.Herearetwo variantsof thesubarraymaximizationproblemthat 」 Q

be solvedwith minor variationsof the aboveapproach:find ゥ ョ 、 ゥ 」 ・ ウ

andbsuchthat R _ ] a h ャ ゥ ウ H Q N I closestto°and(2.) closestto t.A commonmistakethat peoplemakewhile solvingDP problemsis

trying to thhk of the recursivecaseby splitting the problemirlto twoequalhalvesFOlaQ11icksortr i.e-F somehowsolve the subproblemsforarrays a { ャ O R } and a { ョ O R Q 。 ョ 、 combinethe results.Howeverinmost 」 。 ウ セ ウ エ ィ ・ セ ・ two subproblemsarenotsufficientto solvetheoriginalproblem.

31

Figure3. "Be fearfulwhenothersaregreedy"-W. Buffett

エ G ^ G ヲ n i ャ p セ V H a ャ i エ

w i エ N | N 。 v e D ャ G ッ イ Q

イ セ イ ゥ TO c ヲ Nossセ セ R. tVE R,

/

3.2 FROGCROSSING

3.1 LONGESTNONDECREASINGSUBSEQUENCE

In genomics, given two genesequences, we try to find if partsof onegeneare the sameas the other. Thus it is importantto ャ 、 the longestcommonsubsequenceof thetwo sequences.Onewayto solvethis prob-lemis to constructanewsequencewherefor eachliteral in onesequence,we insert its positioninto the other ウ ・ ア オ ・ 」 ・ andthenfind the longestnondecreasingsubsequenceof this new subsequence.For example, ifthe エ キ ッ オ ・ ョ 」 ・ ウ are (1,3,5,2,7) and (1,2,3,5,7), we would constructanewウ ・ ア オ ・ 」 ・ wherefor eachーッ ウ ゥ エ ゥ ッ ゥ ョ the first sequence, we wouldlist its positionin thesecondウ ・ ア オ ・ 」 ・ like so, (1,3,4,2,5). Thenwe findthe Q P ァ ・ ウ エ ョ ッ ョ 、 ・ 」 イ ・ 。 ウ ゥ ウ ・ ア オ ・ ョ 」 ・ which is (1,3,4,5). Now, if we usethe numbersof the new sequenceas indicesinto the secondsequence,we get (1,3,5,7) which is our Q P ァ ・ ウ エ commonウ ウ ・ ア オ ・ ョ 」 ・ N

Problem3.1: Given anarrayof integersA of lengthn, find the longestsequence(h ,… ik) suchthat i j < ゥ ェ Q and a { ゥ ェ } a { ゥ ェ ヲ ッ イ any j[1 , ォ Q}N

3.1. LONGESTNONDECREASINGSUBSEQUENCECHAPTER3. mel algorithms

function to computeFn that recursivelyinvokesitself to computeセ M QandFn -2 would havea time complexitythat is exponentialin n. How-everif wemaketheobservationthatrecursionleadsto computing ヲ ッ イ

i E {P M 1] repeatedly, we cansavethe 」 ッ ュ ー オ エ 。 エ ゥ ッ エ ゥ ュ ・ by ウ エ ッ イ ゥ i Q

theseresults。 ョ ャ 、 イ ・ オ ウ ウ ゥ ョ ァ them.This makesthetime complexitylinearin。 ャ 「 ・ ゥ エ at the expenseof o H ウ エ ッ イ 。 ァ ・ N Note that the recursiveimple-

mentationrequireso H I storagetoo, thoughon thestackratherthantheheapandthat the function is not tail イ ・ 」 オ イ カ ・ sincethe last operationperformedis + andnot a recursivecall.

Thekey to solvinganyDPproblemefficiently is finding therightwayto breaktheprobleminto subproblemssuchthat

エ ィ ・ biggerproblemcanbe solvedrelativelyeasilyoncesolutiontoall the subproblemsareavailable, and

- youneedto solveasfew subproblemsaspossible.In somecases, this ュ イ ・ ア オ ゥ イ ・ ウ ッ ャ カ ゥ 。 slightly different ッ ー エ ゥ ュ ゥ コ ゥ ッ

problemtharItheoriginal proMem.ForexampleyCOI1siderthe follow-ing problem: ァ ゥ カ ・ 。 ョ arrayof integersA of length ヲ ゥ ョ 、 the intervalindicesa andbsuchthat R Z セ ] a { ゥ } is maximized.

Letrstry to solvethis problemassumiRgwehavethes0111tiORforthesubarraya { ャ M 1]. In this case, evenif we knewthelargestsumsubar-ray for array a { ャ M i } it doesnothelpussolvetheproblemfor a { ャ } N

Now, considera variantof this problem.Let

30

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

It is easyto definearecurrencerelationshipヲ ッ イ a (i , j). Thisis essentiallythe largestsequeI1cesumtill j-l addedto A[kl(or zero if that sumhappensto be negative). h ・ ョ 」 ・ a H ゥ j) = ュ。クHo aHゥ ェ - 1) + A[j]).Using this relationship, we cantabulate a H ャ ェ I for j {Q ゥョ linear-time. Oncewehaveall theseカ 。 ャ オ ・ s the 。 ョ ャ ウ キ ・ イ エ ッ our P イ ゥ ァ ゥ ョ 。 ャ ー イ ッ 「 ャ ・ ュ

ゥ ウ simply ュ ャ 。 x ェ { Q W z

pass.Herearetwo variantsof thesubarraymaximizationproblemthat 」 Q

be solvedwith minor variationsof the aboveapproach:find ゥ ョ 、 ゥ 」 ・ ウ

andbsuchthat R _ ] a h ャ ゥ ウ H Q N I closestto°and(2.) closestto t.A commonmistakethat peoplemakewhile solvingDP problemsis

trying to thhk of the recursivecaseby splitting the problemirlto twoequalhalvesFOlaQ11icksortr i.e-F somehowsolve the subproblemsforarrays a { ャ O R } and a { ョ O R Q 。 ョ 、 combinethe results.Howeverinmost 」 。 ウ セ ウ エ ィ ・ セ ・ two subproblemsarenotsufficientto solvetheoriginalproblem.

31

Figure3. "Be fearfulwhenothersaregreedy"-W. Buffett

エ G ^ G ヲ n i ャ p セ V H a ャ i エ

w i エ N | N 。 v e D ャ G ッ イ Q

イ セ イ ゥ TO c ヲ Nossセ セ R. tVE R,

/

3.2 FROGCROSSING

3.1 LONGESTNONDECREASINGSUBSEQUENCE

In genomics, given two genesequences, we try to find if partsof onegeneare the sameas the other. Thus it is importantto ャ 、 the longestcommonsubsequenceof thetwo sequences.Onewayto solvethis prob-lemis to constructanewsequencewherefor eachliteral in onesequence,we insert its positioninto the other ウ ・ ア オ ・ 」 ・ andthenfind the longestnondecreasingsubsequenceof this new subsequence.For example, ifthe エ キ ッ オ ・ ョ 」 ・ ウ are (1,3,5,2,7) and (1,2,3,5,7), we would constructanewウ ・ ア オ ・ 」 ・ wherefor eachーッ ウ ゥ エ ゥ ッ ゥ ョ the first sequence, we wouldlist its positionin thesecondウ ・ ア オ ・ 」 ・ like so, (1,3,4,2,5). Thenwe findthe Q P ァ ・ ウ エ ョ ッ ョ 、 ・ 」 イ ・ 。 ウ ゥ ウ ・ ア オ ・ ョ 」 ・ which is (1,3,4,5). Now, if we usethe numbersof the new sequenceas indicesinto the secondsequence,we get (1,3,5,7) which is our Q P ァ ・ ウ エ commonウ ウ ・ ア オ ・ ョ 」 ・ N

Problem3.1: Given anarrayof integersA of lengthn, find the longestsequence(h ,… ik) suchthat i j < ゥ ェ Q and a { ゥ ェ } a { ゥ ェ ヲ ッ イ any j[1 , ォ Q}N

3.1. LONGESTNONDECREASINGSUBSEQUENCECHAPTER3. mel algorithms

function to computeFn that recursivelyinvokesitself to computeセ M QandFn -2 would havea time complexitythat is exponentialin n. How-everif wemaketheobservationthatrecursionleadsto computing ヲ ッ イ

i E {P M 1] repeatedly, we cansavethe 」 ッ ュ ー オ エ 。 エ ゥ ッ エ ゥ ュ ・ by ウ エ ッ イ ゥ i Q

theseresults。 ョ ャ 、 イ ・ オ ウ ウ ゥ ョ ァ them.This makesthetime complexitylinearin。 ャ 「 ・ ゥ エ at the expenseof o H ウ エ ッ イ 。 ァ ・ N Note that the recursiveimple-

mentationrequireso H I storagetoo, thoughon thestackratherthantheheapandthat the function is not tail イ ・ 」 オ イ カ ・ sincethe last operationperformedis + andnot a recursivecall.

Thekey to solvinganyDPproblemefficiently is finding therightwayto breaktheprobleminto subproblemssuchthat

エ ィ ・ biggerproblemcanbe solvedrelativelyeasilyoncesolutiontoall the subproblemsareavailable, and

- youneedto solveasfew subproblemsaspossible.In somecases, this ュ イ ・ ア オ ゥ イ ・ ウ ッ ャ カ ゥ 。 slightly different ッ ー エ ゥ ュ ゥ コ ゥ ッ

problemtharItheoriginal proMem.ForexampleyCOI1siderthe follow-ing problem: ァ ゥ カ ・ 。 ョ arrayof integersA of length ヲ ゥ ョ 、 the intervalindicesa andbsuchthat R Z セ ] a { ゥ } is maximized.

Letrstry to solvethis problemassumiRgwehavethes0111tiORforthesubarraya { ャ M 1]. In this case, evenif we knewthelargestsumsubar-ray for array a { ャ M i } it doesnothelpussolvetheproblemfor a { ャ } N

Now, considera variantof this problem.Let

30

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

3.3 CUTTING PAPER

Wenowconsideranoptimumplanningproblemin two dimensions.Youaregivenan L x lV rectangularpieceof kite-paper, whereL andWarepositive integersand a list of n kinds of kites that canbe madeusingthe paper. The i-th kite 、 ・ ョ i {Q } requiresan li ク イ・」エ。ョァャ・

of kite-paper;this kite sellsfor Pi' Assumeャ ゥ G ゥ Pi arepositiveintegers.You havea machinethat cancut rectangularpiecesof kite-papereitherhorizontallyor vertically.

Problem3.3: Designan algorithm that computesa ーイッ エ maximizingstrategyfor cutting thekite-paper.You canmakeasmanyinstancesof agivenkite asyouwant. Thereis no costto cutting ォ ・ M ー 。 ー ・ イ N

DPis oftenusedto computea ーi。 ヲッイ performinga taskthatconsistsof a seriesof actionsin an optimumway. Here is an examplewith aninterestingtwist.

Problem3.2: Thereis a river that ゥ ウ ュ ・ エ ・ イ ウ wide. At everymeterfromtheedge, theremayormaynotbea stone.A frog needsto crosstheriver.Howeverthe frog hasthe limitation that ゥ ヲ ィ 。 ウ just jumpedx meters,thenits イ エ jump mustbebetweenx-I and ク Q meters, inclusive.Assumethe first jump canbe of ャ ケ 1 meter. Given the positionof thestones, how would you determinewhetherthe frog canmakeit to theotherendor not?Analyzethe runtimeof your algorithm.

33

Table2. Numberof ElectoralCollegevotesperstateandWashington, DC

Alabama 9 Indiana 11 Nebraska 5 SouthCarolina 8Alaska 3 QP t。 7 Nevada 5 SouthDakota 3Arizona 10 Kansas 6NNNeeewwwJMHeraesmxeiypcoshire

4 Tennessee 11Arkansas 6 k・ i」ォケ 8 15 Texas 34California 55 Louisiana 9 5 Utah 5Colorado 9 m。 ャ・ 4 NewYork 31 Vermont 3cッョ ャ・」エゥ」オエ 7 Maryland 10 NorthCarolina 15

キュv ヲvゥャa。・クウァウウ」ィエオッvオゥョク aksgapto1mna13Delaware 3 Massachusetts 12 NorthDakota 3 11Florida 27 Michigan 17 Ohio 20 5Georgia 15 m ュ・ウッエ。 10 oォャ ャッイョ。 7 10Hawaii 4 Mississippi 6 Oregon 7

WTwoaytsaOhlゥ・ iャQ・ァQ」ァ ttOoIrUsDC3Idaho 4 Missouri 11 pmemodseyIlvdmanida 21 3Illinois 21 Montana 3 4 538

3.5. TIESIN A PRESIDENTIALELECTION

3.5 TIES IN A PRESIDENTIAL ELECTION

TheUSPresideIItis electedby themembersof theElectoralCollege.21eumberof electorsperstateandWashiI1gtOILDCF aregivezlhTable2.

A11electorsfrom eachstateaswell aswashingtOIUDC casttheirvoteforthesamecandidate.

probkm3.5:Supposetherearetwo cmdidateshthepresidentialdeemFT eowwPQQQ、ケッ イ。ュュ。エゥ」。ャャケ 、 Gュゥョ・ if a tie is a possibil-........ .

CHAPTER3. mel algorithms32

3 .4 ord BREAKING

Supposeyou aredesigninga searchengine. In additionto gettingkey-wordsfrom apage'scontent, youwouldlike to getkeywordsfrom URLs.For example, bedbathandbeyond.com shouldbe associatedwith "bedbathandbeyond"(in this versionof theproblemwe alsoallow "bedbathandbeyond"to beassociatedwith it).

Problem3.4: Given a dictionary that can tell you whethera string isa valid word or not in constanttime and given a ウエ QY s of ャ ・ ョ ァ エ ィprovideanefficientalgorithmthat 」 。 エ ・ ャ ャ whethers canbereconstitutedas a ウ・アオ・ 」・ of valid words. In the eventthat the string is valid, youralgorithmshouldoutputthecorrespondingsequenceof words.

The next threeproblemshave a very similar structure. Given a set ofobjectsof differentsizes, youneedto partitionthemin variousways.Thesolutionsalsohavethesamecommonthemethatyouneedto exploreallpossiblepartitionsin a way thatyou cantakeadvantageof overlappingsubproblems.

3.6 RED OR BLUE HOUSE MAJORITY

supposeyouwantto p1aceabetontheoutcomeof thecomingelections.specifiedly}youarebetthgif theUSHouseof Representativeswill havea Democraticor a Republicmmajority.Aーッ ャ ャ ゥ 」 ッ ュ ー 。 ィ 。 ウ com-putedthe probabiHtyof winRing for eachcmdidateh the individualdectiom.Youa?einterestediRjustonemmber-whatistheprobabilitythattheRepubhcmPaz-tyis goingto havea majorityh theHouse?

Problem3.6: Giventhata partyneeds223or moreseatsto win a maior-ケ ュ ・ FOuseyhowwouldyoucomputetheprobabilityofaItepubIL

-f ASS?m?eachraceis indepmdentmd thattheprobabilityof aRepublicanwinning theracei is Pi'

3.7 LOAD BALANCING

supposeyouwanttobuild a1argedistributedstoragesystemmtheweb.MiniOI1s of userswiH storeterabytesof dataon your servers.Onewayto desig1tkesystemwouldbeto hastleach11ser-Fsloghidr partitiOI1thehashrmgesinto equal-sizedbucketsrandstorethedatafor eachbucket

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

3.3 CUTTING PAPER

Wenowconsideranoptimumplanningproblemin two dimensions.Youaregivenan L x lV rectangularpieceof kite-paper, whereL andWarepositive integersand a list of n kinds of kites that canbe madeusingthe paper. The i-th kite 、 ・ ョ i {Q } requiresan li ク イ・」エ。ョァャ・

of kite-paper;this kite sellsfor Pi' Assumeャ ゥ G ゥ Pi arepositiveintegers.You havea machinethat cancut rectangularpiecesof kite-papereitherhorizontallyor vertically.

Problem3.3: Designan algorithm that computesa ーイッ エ maximizingstrategyfor cutting thekite-paper.You canmakeasmanyinstancesof agivenkite asyouwant. Thereis no costto cutting ォ ・ M ー 。 ー ・ イ N

DPis oftenusedto computea ーi。 ヲッイ performinga taskthatconsistsof a seriesof actionsin an optimumway. Here is an examplewith aninterestingtwist.

Problem3.2: Thereis a river that ゥ ウ ュ ・ エ ・ イ ウ wide. At everymeterfromtheedge, theremayormaynotbea stone.A frog needsto crosstheriver.Howeverthe frog hasthe limitation that ゥ ヲ ィ 。 ウ just jumpedx meters,thenits イ エ jump mustbebetweenx-I and ク Q meters, inclusive.Assumethe first jump canbe of ャ ケ 1 meter. Given the positionof thestones, how would you determinewhetherthe frog canmakeit to theotherendor not?Analyzethe runtimeof your algorithm.

33

Table2. Numberof ElectoralCollegevotesperstateandWashington, DC

Alabama 9 Indiana 11 Nebraska 5 SouthCarolina 8Alaska 3 QP t。 7 Nevada 5 SouthDakota 3Arizona 10 Kansas 6NNNeeewwwJMHeraesmxeiypcoshire

4 Tennessee 11Arkansas 6 k・ i」ォケ 8 15 Texas 34California 55 Louisiana 9 5 Utah 5Colorado 9 m。 ャ・ 4 NewYork 31 Vermont 3cッョ ャ・」エゥ」オエ 7 Maryland 10 NorthCarolina 15

キュv ヲvゥャa。・クウァウウ」ィエオッvオゥョク aksgapto1mna13Delaware 3 Massachusetts 12 NorthDakota 3 11Florida 27 Michigan 17 Ohio 20 5Georgia 15 m ュ・ウッエ。 10 oォャ ャッイョ。 7 10Hawaii 4 Mississippi 6 Oregon 7

WTwoaytsaOhlゥ・ iャQ・ァQ」ァ ttOoIrUsDC3Idaho 4 Missouri 11 pmemodseyIlvdmanida 21 3Illinois 21 Montana 3 4 538

3.5. TIESIN A PRESIDENTIALELECTION

3.5 TIES IN A PRESIDENTIAL ELECTION

TheUSPresideIItis electedby themembersof theElectoralCollege.21eumberof electorsperstateandWashiI1gtOILDCF aregivezlhTable2.

A11electorsfrom eachstateaswell aswashingtOIUDC casttheirvoteforthesamecandidate.

probkm3.5:Supposetherearetwo cmdidateshthepresidentialdeemFT eowwPQQQ、ケッ イ。ュュ。エゥ」。ャャケ 、 Gュゥョ・ if a tie is a possibil-........ .

CHAPTER3. mel algorithms32

3 .4 ord BREAKING

Supposeyou aredesigninga searchengine. In additionto gettingkey-wordsfrom apage'scontent, youwouldlike to getkeywordsfrom URLs.For example, bedbathandbeyond.com shouldbe associatedwith "bedbathandbeyond"(in this versionof theproblemwe alsoallow "bedbathandbeyond"to beassociatedwith it).

Problem3.4: Given a dictionary that can tell you whethera string isa valid word or not in constanttime and given a ウエ QY s of ャ ・ ョ ァ エ ィprovideanefficientalgorithmthat 」 。 エ ・ ャ ャ whethers canbereconstitutedas a ウ・アオ・ 」・ of valid words. In the eventthat the string is valid, youralgorithmshouldoutputthecorrespondingsequenceof words.

The next threeproblemshave a very similar structure. Given a set ofobjectsof differentsizes, youneedto partitionthemin variousways.Thesolutionsalsohavethesamecommonthemethatyouneedto exploreallpossiblepartitionsin a way thatyou cantakeadvantageof overlappingsubproblems.

3.6 RED OR BLUE HOUSE MAJORITY

supposeyouwantto p1aceabetontheoutcomeof thecomingelections.specifiedly}youarebetthgif theUSHouseof Representativeswill havea Democraticor a Republicmmajority.Aーッ ャ ャ ゥ 」 ッ ュ ー 。 ィ 。 ウ com-putedthe probabiHtyof winRing for eachcmdidateh the individualdectiom.Youa?einterestediRjustonemmber-whatistheprobabilitythattheRepubhcmPaz-tyis goingto havea majorityh theHouse?

Problem3.6: Giventhata partyneeds223or moreseatsto win a maior-ケ ュ ・ FOuseyhowwouldyoucomputetheprobabilityofaItepubIL

-f ASS?m?eachraceis indepmdentmd thattheprobabilityof aRepublicanwinning theracei is Pi'

3.7 LOAD BALANCING

supposeyouwanttobuild a1argedistributedstoragesystemmtheweb.MiniOI1s of userswiH storeterabytesof dataon your servers.Onewayto desig1tkesystemwouldbeto hastleach11ser-Fsloghidr partitiOI1thehashrmgesinto equal-sizedbucketsrandstorethedatafor eachbucket

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

3.9 OPTIMUM BUFFERINSERTION

You are given a tree-structuredlogic circuit that canbe modeledas arooted tree, exactly as in Problem3.8. Signals degradeas they passthroughsuccessivegates.

You canovercomethis degradationby "buffering" gates-bufferingenhancesits outputbutdoesnot changeits logical functionality.

ProblemS h ッ キ would you ・ ヲ ヲ ゥ 」 ゥ ・ ャ ケ computethe least ョ オ イ ・ イ ofgatesto buffer in the circuit so that after buffering, everypathof k ormoregateshasat leastonebufferedgate?More formally, givena rooted

of usersononeserver.For this scheme, mappingauserto theserverthatservestheuseris a simplehashcomputation.

However if a small numberof usersoccupya large fraction of thestoragespace, hashingwill not achieveabalancedpartition. Oneway tosolvethisproblemis to makethehashbucketshaveanonuniformwidthbasedP エ ィ ・ loadin thathashrange.

Problem3.7: You haven userswith uniquehashesh1 throughhn andm servers, numbered1 to m. User i hasB i bytesto store. You needtofind numbersK 1 throughK m suchthat all userswith hashesbetweenK j and k ェ Q get assignedto serverj. Designan algorithmto find thenumbersK 1 throughK m that minimizesthe load on the mostheavilyloadedserver.

Sofar wehaveappliedDP to one-dimensionalandtwo-dimensionalob-jects.Hereareapplicationsof DP to trees.

3.8 VOLTAGE SELECTION

You are given a logic circuit that canbe modeledas a rootedtree-theleavesare the primary inputs, the internalnodesare the gates, andtheroot is thesingleoutputof thecircuit.

Eachgatecanbe poweredby a high or low supplyvoltage. A gatepoweredby a lower supply voltage consumesless power but has aweakeroutputsignal. You wantto minimize powerwhile ensuringthatthe circuit is reliable. To ensurereliability, you shouldnot havea gatepoweredby a low supplyvoltagedrive anothergatepoweredby a lowsupplyvoltage. All gatesconsume1 nanowattwhenconnectedto thelow supplyvoltageand2nanowattswhenconnectedto thehigh supplyvoltage.

Problem3.8: Designan efficient algorithm that takesas input a logiccircuit andselectssupplyvoltagesfor eachgateto minimizepower 」 ッsumptionwhile ensuringreliableoperation.

353.10. TRIANGULATION

Givenanオ 。 イ ・ ョ エ ィ ・ ・ 、 expressionof エャ ッイュッ QᄚQG . . ᄚ MQwherevo,… ,vn -1 areoperandswith knownrealvaluesand o ョ N N N P B N ⦅ I

arespecifiedoperatiomrwewaI1ttopareI1thesizetheexpresmnsOajtAmaximizeits value.

Problem3.11:Devisem algorithmto solvethis problemh the specidcasethat theoperandsayeaHpositiveandthe OR1y operatiomare ᄋ 。 ュ i QN

Explainhow you キ カ ッ オ ャ 、 modify your algorithmto dealwith thecasein which theoperandscanbepositive 。 ョ ャ 、 ョ ・ ァ 。 エ ゥ カ ・ 。 イ ャ 、 K 。 ョ ャ 、 。 イ ・ theo Q ケ ッ ー ・ イ 。 エ ゥ ッ ョ ウ N

Suggesthow you would generalizeyour approachエ ッ ャ 」 ャ オ 、 ・ multi-plicationazlddivisiOI1(pretenddivide-bTzeroneveroccurs).

-25 = U HSNHTKVII

-13 = U HHS . TI VI

20 = (5-3)·(4+6)

-1 = HU HSNTII + 6

14 = ((5 - 3) . TI V

3.11 MAXIMIZING EXPRESSIONS

The valueof anarithmeticexpressiondependsオ ー ッ エ ィ ・ orderin whichthe operatiOIlsayeperformed-Forexamplerdepmdizlgオ ー ッ ィ ッ キ ッ ョ ・ー 。 イ ・ エ ィ ・ ウ ゥ コ ・ ウ theexpression5 - 3 . T V onecanobtainanyoneof thefollowing values:

3.10 TRIANGULATION

Let P bea convexーッ ャ ッ ョ with n verticesspecifiedby their x andy co-ordinates.A triangulationof P is a collectionof M 3diagonalsof P suchthatI1O Wo diagonalsintersectrexceptpossiblyat their endpohts.Ob-servethata triangulationsplits thepolygon'sinterior ゥ ョ エ ッ M 2 disjointtriangles.Definethe costof a エイゥ。 オャ。エゥッコQエッ be thesumof tke1engthsof thediagonalsthatit is madeup of.

probkm3.10:Desigz1meffideI1t algorithmfor fhdhga trimgulatiORthatminimizesthecost.

treerhow would you color the edgesof the graphhgreenor red suchthtmpathfrom amdeto my mcestor-coz1tahsmorethank successiverededgesandthenumberof greenedgesis minimized?

DP cmalsobeappliedto geometriccORstr1ICHor1Syasillustratedby thisproblem:

CHAPTER3. mel algorithms34

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

3.9 OPTIMUM BUFFERINSERTION

You are given a tree-structuredlogic circuit that canbe modeledas arooted tree, exactly as in Problem3.8. Signals degradeas they passthroughsuccessivegates.

You canovercomethis degradationby "buffering" gates-bufferingenhancesits outputbutdoesnot changeits logical functionality.

ProblemS h ッ キ would you ・ ヲ ヲ ゥ 」 ゥ ・ ャ ケ computethe least ョ オ イ ・ イ ofgatesto buffer in the circuit so that after buffering, everypathof k ormoregateshasat leastonebufferedgate?More formally, givena rooted

of usersononeserver.For this scheme, mappingauserto theserverthatservestheuseris a simplehashcomputation.

However if a small numberof usersoccupya large fraction of thestoragespace, hashingwill not achieveabalancedpartition. Oneway tosolvethisproblemis to makethehashbucketshaveanonuniformwidthbasedP エ ィ ・ loadin thathashrange.

Problem3.7: You haven userswith uniquehashesh1 throughhn andm servers, numbered1 to m. User i hasB i bytesto store. You needtofind numbersK 1 throughK m suchthat all userswith hashesbetweenK j and k ェ Q get assignedto serverj. Designan algorithmto find thenumbersK 1 throughK m that minimizesthe load on the mostheavilyloadedserver.

Sofar wehaveappliedDP to one-dimensionalandtwo-dimensionalob-jects.Hereareapplicationsof DP to trees.

3.8 VOLTAGE SELECTION

You are given a logic circuit that canbe modeledas a rootedtree-theleavesare the primary inputs, the internalnodesare the gates, andtheroot is thesingleoutputof thecircuit.

Eachgatecanbe poweredby a high or low supplyvoltage. A gatepoweredby a lower supply voltage consumesless power but has aweakeroutputsignal. You wantto minimize powerwhile ensuringthatthe circuit is reliable. To ensurereliability, you shouldnot havea gatepoweredby a low supplyvoltagedrive anothergatepoweredby a lowsupplyvoltage. All gatesconsume1 nanowattwhenconnectedto thelow supplyvoltageand2nanowattswhenconnectedto thehigh supplyvoltage.

Problem3.8: Designan efficient algorithm that takesas input a logiccircuit andselectssupplyvoltagesfor eachgateto minimizepower 」 ッsumptionwhile ensuringreliableoperation.

353.10. TRIANGULATION

Givenanオ 。 イ ・ ョ エ ィ ・ ・ 、 expressionof エャ ッイュッ QᄚQG . . ᄚ MQwherevo,… ,vn -1 areoperandswith knownrealvaluesand o ョ N N N P B N ⦅ I

arespecifiedoperatiomrwewaI1ttopareI1thesizetheexpresmnsOajtAmaximizeits value.

Problem3.11:Devisem algorithmto solvethis problemh the specidcasethat theoperandsayeaHpositiveandthe OR1y operatiomare ᄋ 。 ュ i QN

Explainhow you キ カ ッ オ ャ 、 modify your algorithmto dealwith thecasein which theoperandscanbepositive 。 ョ ャ 、 ョ ・ ァ 。 エ ゥ カ ・ 。 イ ャ 、 K 。 ョ ャ 、 。 イ ・ theo Q ケ ッ ー ・ イ 。 エ ゥ ッ ョ ウ N

Suggesthow you would generalizeyour approachエ ッ ャ 」 ャ オ 、 ・ multi-plicationazlddivisiOI1(pretenddivide-bTzeroneveroccurs).

-25 = U HSNHTKVII

-13 = U HHS . TI VI

20 = (5-3)·(4+6)

-1 = HU HSNTII + 6

14 = ((5 - 3) . TI V

3.11 MAXIMIZING EXPRESSIONS

The valueof anarithmeticexpressiondependsオ ー ッ エ ィ ・ orderin whichthe operatiOIlsayeperformed-Forexamplerdepmdizlgオ ー ッ ィ ッ キ ッ ョ ・ー 。 イ ・ エ ィ ・ ウ ゥ コ ・ ウ theexpression5 - 3 . T V onecanobtainanyoneof thefollowing values:

3.10 TRIANGULATION

Let P bea convexーッ ャ ッ ョ with n verticesspecifiedby their x andy co-ordinates.A triangulationof P is a collectionof M 3diagonalsof P suchthatI1O Wo diagonalsintersectrexceptpossiblyat their endpohts.Ob-servethata triangulationsplits thepolygon'sinterior ゥ ョ エ ッ M 2 disjointtriangles.Definethe costof a エイゥ。 オャ。エゥッコQエッ be thesumof tke1engthsof thediagonalsthatit is madeup of.

probkm3.10:Desigz1meffideI1t algorithmfor fhdhga trimgulatiORthatminimizesthecost.

treerhow would you color the edgesof the graphhgreenor red suchthtmpathfrom amdeto my mcestor-coz1tahsmorethank successiverededgesandthenumberof greenedgesis minimized?

DP cmalsobeappliedto geometriccORstr1ICHor1Syasillustratedby thisproblem:

CHAPTER3. mel algorithms34

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

3.13 MINIMIZE WAITING TIME

A databasehasto respondto n ウゥョ ュュ」ャゥ・ョエ SQL queries hservicetime requiredfor queryi is kmillisecoMsaM is kmWI1m?d

ance. The Q P ッ ォ オ ー ウ areprocessedウ ・ ア オ ・ ョ エ ゥ 。 。 ャ ャ ャ ケ 「 オ エ can 「 ・ processedin。 ョョ カ ッ イ 、 ・ イ N w ・ キ ゥ ウ ィ エ ッ ュ ゥ イis Q ime client ゥ エ 。 ォ ・ ウ エ ッ イ N ・ エ オ イ ョ N For example, if the lookupsares edゥ ッ イ 、 ・ イ ッ ヲ ャ 」 イ ・ 。 ウ ゥ ョ ァ i , thenthe clientmakingthe i-th queryhasto wait

R Z セ ] Q tj milliseconds

Problem3.13: Designanefficient algorithmfor 」 ッ ュ ー オ エ ゥ ョ ァ ャ ッ ー エ ゥ ュ オ ュ

orderfor processingthe queries.

3.12 SCHEDULINGTUTORS

Gッオ areresponsiblefor schedulingtutorsfor the day at a tutoringcom-f ッ イ 。 」 ィ day, you havereceiveda numberof イ エ ウ for tutors

Eachwquesthasa specifiedstarttime md eachlessORisthirty miI111tesQP ylオ havemore tutors ttm reqlmts.Eachtutor cm startworkat any time. Howevertutors are 」 ッ ウ エ イ 。 ゥ ョ ・ 、 to work only one stretchwhich camotbe lOI1ger thantwo hoursmd eachtutor caI1serviceッ ャ ケ

onerequestata time.

Problem3.12: Given a set of requestsfor the day, designan efficientalzorithmto computethe leastr1umberof tutorsI1ecessaryto scheduleall the requestsfor theday.

GreedyAlgorithms

A greedyalgorithm is onewhich makesdecisiOI1sthat are locally opm 。ョ、 ・カ・イ changesthem.Thisapproachdoesmt wor-k gen p

ally. Forexample, considerュ 。 ォ ゥ 」 ィ 。 ョ ァ ・ for 48pencein theold b ゥ エ ゥ ウ ィmcywherethecoim cameh 30724?127673Flpmce、 ・ r ッ ュ ィ 。 エ Q P s

A greedyalgorithmwould iterative137choosethe largestdeI1omhat1OIIcohthatis lessthmor equalto theamountof chmgethatremahstohmade.If we try this for 48 ー・ 」・ we get30,12,6. Howevertheoptimum

wouldbe 24,24.In its most general form, the coin changing problem is NP-

Mrd(4.ChapteJ6)butfor some coimgesr the greedy ヲ ャ ヲ イ ゥ エ ュ ュoptimum-e-E-rif tt1edenomiRatiomareoftheform サ ャ a t R W ス N Ad hoc

gumentscanbe appliedto show エ ィ 。 エ ゥ ウ alsooptimumfor VS coinsiiZgeneralproblem」 。 ・ solvedin pseudopolynomialtime usingDPin amannerウ ョ ゥ ャ 。 イ to Problem6.1.

37

A user interface(VI) designeris trying to designa menusystemthatcustomersuseto trigger certaintasks.Hewantsto minimizetheaverageamountof time it takesfor a customerto performtasks.

If a menuitem is at the i-th ー ッ ウ ゥ エ ゥ ッ ゥ エ takesi units of time for theuserto reachthere(linearscan)andit takesc units of time to click on it.

3.15 EFFICIENT USERINTERFACE

3.14. HUFFMANCODING

3.14 HUFFMAN CODING

In 1951, David A. Huffman andhis classmatesin a graduatecourseoninformation theory at MIT were given the choiceof a term paperor afinal exam. For the term ー 。 ー ・ h オ ヲ ヲ ュ 。 ョ G ウ professor, RobertM. Fano,hadgiventheproblemof finding analgorithmfor assigningbinarycodesto symbolssuchthat a given set of symbolscanbe representedin thesmallestnumberof bits.

h オ ヲ ャ 。 ョ workedontheproblemfor months, developinganumberofapproachesbutnonethathecouldproveto bethemostefficient. Finally,he despairedof ever reachinga solutionand decidedto startstudyingfor thefinal. Justashewasthrowinghisnotesin thegarbage, theideaofusinga frequency-sortedbinarytreecameto him andhequickly provedthis methodto bethemostefficient.

Huffman's solutionprovedto bea significantimprovementoverthe"Shannon-Fanocodes"proposedby his professorRobertM. Fano 。 ャ ッ ァ

with ClaudeE. sィ ュッョMエィ・ inventorof InformationTheory.Let's look atanapplicationof h オ ヲ ャ 。 ョ coding.Wewantto compress

a largepieceof Englishtextby building a variablelengthcodebookforeachpossiblecharacter.Considerthe casewhereeachcharacterin thetext is independentof all othercharacters(we can achievebettercom-ー イ ・ ウ ウ ゥ ッ ゥ ヲ we do notmakethis assumptionbut for this problemwewillignorethis fact).

Oneway of doing this kind of compressionis to mapeachcharacterto a bit stringsuchthatno bit string is a prefix of another(for example,011 is a prefix of 0110butnot a prefix of 1100).

We cansimply encodethe text by appendingthebit stringsfor eachcharacterin thetext. While decodingthestring, we cankeepreadingthebits until we find a string that is in our codebook andthenrepeatthisprocessuntil theentiretext is decoded.

Sinceour objectiveis to compressthe text, we would like to assignthe shorterstringsto moreprobablecharactersandthe longerstringstolessprobablecharacters.

Problem3.14: Givena setof symbolswith correspondingprobabilities,find a prefix codeassignmentthatminimizesthe expectedlengthof theencodedstring.

CHAPTER3. mel ᄋalgorithms36

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

3.13 MINIMIZE WAITING TIME

A databasehasto respondto n ウゥョ ュュ」ャゥ・ョエ SQL queries hservicetime requiredfor queryi is kmillisecoMsaM is kmWI1m?d

ance. The Q P ッ ォ オ ー ウ areprocessedウ ・ ア オ ・ ョ エ ゥ 。 。 ャ ャ ャ ケ 「 オ エ can 「 ・ processedin。 ョョ カ ッ イ 、 ・ イ N w ・ キ ゥ ウ ィ エ ッ ュ ゥ イis Q ime client ゥ エ 。 ォ ・ ウ エ ッ イ N ・ エ オ イ ョ N For example, if the lookupsares edゥ ッ イ 、 ・ イ ッ ヲ ャ 」 イ ・ 。 ウ ゥ ョ ァ i , thenthe clientmakingthe i-th queryhasto wait

R Z セ ] Q tj milliseconds

Problem3.13: Designanefficient algorithmfor 」 ッ ュ ー オ エ ゥ ョ ァ ャ ッ ー エ ゥ ュ オ ュ

orderfor processingthe queries.

3.12 SCHEDULINGTUTORS

Gッオ areresponsiblefor schedulingtutorsfor the day at a tutoringcom-f ッ イ 。 」 ィ day, you havereceiveda numberof イ エ ウ for tutors

Eachwquesthasa specifiedstarttime md eachlessORisthirty miI111tesQP ylオ havemore tutors ttm reqlmts.Eachtutor cm startworkat any time. Howevertutors are 」 ッ ウ エ イ 。 ゥ ョ ・ 、 to work only one stretchwhich camotbe lOI1ger thantwo hoursmd eachtutor caI1serviceッ ャ ケ

onerequestata time.

Problem3.12: Given a set of requestsfor the day, designan efficientalzorithmto computethe leastr1umberof tutorsI1ecessaryto scheduleall the requestsfor theday.

GreedyAlgorithms

A greedyalgorithm is onewhich makesdecisiOI1sthat are locally opm 。ョ、 ・カ・イ changesthem.Thisapproachdoesmt wor-k gen p

ally. Forexample, considerュ 。 ォ ゥ 」 ィ 。 ョ ァ ・ for 48pencein theold b ゥ エ ゥ ウ ィmcywherethecoim cameh 30724?127673Flpmce、 ・ r ッ ュ ィ 。 エ Q P s

A greedyalgorithmwould iterative137choosethe largestdeI1omhat1OIIcohthatis lessthmor equalto theamountof chmgethatremahstohmade.If we try this for 48 ー・ 」・ we get30,12,6. Howevertheoptimum

wouldbe 24,24.In its most general form, the coin changing problem is NP-

Mrd(4.ChapteJ6)butfor some coimgesr the greedy ヲ ャ ヲ イ ゥ エ ュ ュoptimum-e-E-rif tt1edenomiRatiomareoftheform サ ャ a t R W ス N Ad hoc

gumentscanbe appliedto show エ ィ 。 エ ゥ ウ alsooptimumfor VS coinsiiZgeneralproblem」 。 ・ solvedin pseudopolynomialtime usingDPin amannerウ ョ ゥ ャ 。 イ to Problem6.1.

37

A user interface(VI) designeris trying to designa menusystemthatcustomersuseto trigger certaintasks.Hewantsto minimizetheaverageamountof time it takesfor a customerto performtasks.

If a menuitem is at the i-th ー ッ ウ ゥ エ ゥ ッ ゥ エ takesi units of time for theuserto reachthere(linearscan)andit takesc units of time to click on it.

3.15 EFFICIENT USERINTERFACE

3.14. HUFFMANCODING

3.14 HUFFMAN CODING

In 1951, David A. Huffman andhis classmatesin a graduatecourseoninformation theory at MIT were given the choiceof a term paperor afinal exam. For the term ー 。 ー ・ h オ ヲ ヲ ュ 。 ョ G ウ professor, RobertM. Fano,hadgiventheproblemof finding analgorithmfor assigningbinarycodesto symbolssuchthat a given set of symbolscanbe representedin thesmallestnumberof bits.

h オ ヲ ャ 。 ョ workedontheproblemfor months, developinganumberofapproachesbutnonethathecouldproveto bethemostefficient. Finally,he despairedof ever reachinga solutionand decidedto startstudyingfor thefinal. Justashewasthrowinghisnotesin thegarbage, theideaofusinga frequency-sortedbinarytreecameto him andhequickly provedthis methodto bethemostefficient.

Huffman's solutionprovedto bea significantimprovementoverthe"Shannon-Fanocodes"proposedby his professorRobertM. Fano 。 ャ ッ ァ

with ClaudeE. sィ ュッョMエィ・ inventorof InformationTheory.Let's look atanapplicationof h オ ヲ ャ 。 ョ coding.Wewantto compress

a largepieceof Englishtextby building a variablelengthcodebookforeachpossiblecharacter.Considerthe casewhereeachcharacterin thetext is independentof all othercharacters(we can achievebettercom-ー イ ・ ウ ウ ゥ ッ ゥ ヲ we do notmakethis assumptionbut for this problemwewillignorethis fact).

Oneway of doing this kind of compressionis to mapeachcharacterto a bit stringsuchthatno bit string is a prefix of another(for example,011 is a prefix of 0110butnot a prefix of 1100).

We cansimply encodethe text by appendingthebit stringsfor eachcharacterin thetext. While decodingthestring, we cankeepreadingthebits until we find a string that is in our codebook andthenrepeatthisprocessuntil theentiretext is decoded.

Sinceour objectiveis to compressthe text, we would like to assignthe shorterstringsto moreprobablecharactersandthe longerstringstolessprobablecharacters.

Problem3.14: Givena setof symbolswith correspondingprobabilities,find a prefix codeassignmentthatminimizesthe expectedlengthof theencodedstring.

CHAPTER3. mel ᄋalgorithms36

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Eachmenuitem canhavemultiple levelsof sub-menusanda sub-menucanbereachedby clicking onits parentmenuitem.

The designeris providedwith a userstudy that detailshow oftenuserswant tasksto be triggered. (In a real application, we would alsoworry aboutgroupingrelateditems in the samesub-menuaswell butfor this problemwewill ignoregroupingrequirements.)

Problem3.15: How shouldthemenusystembe designedso asto min-imize the averageUI interactiontime if c = I? How would you do it ifc> I?

3.17 POINTSCOVERINGINTERVALS

Consideran engineerresponsiblefor a numberof taskson the factoryfloor. Eachtaskstartsat a fixed time andendsat a fixed time. The en-gineerwantsto visit the floor to checkon the tasks.Your taskis to helphim minimize thenumberof visits hemakes.In eachvisit, hecancheckon all the taskstakingplaceat the time of thevisit. A visit takesplaceata fixed time andhe canonly checkon taskstaking placeat exactlythattime.

More formally, modelthe tasksas 」 ャ ッ ウ ・ 、 intervalson the reallineh } i = Q WN A setS of visit times11covers"thetasksif {。 「ゥ}ョs

ヲッイ i = Q ー M

Problem3.17: Designanefficient algorithmfor finding aminimumcar-dinality setof visit timesthatcoversall the tasks.

3.16 PACKING FOR USPSPRIORITY MAIL

ャ・ UnitedStatesPostalServicemakesfixed-sizemailshippingboxes-you pay a fixed price for a ァゥカ・ 「ック andcanship anythingyou wantthat fits in thebox. Supposeyou havea setof n itemsthatyou needtoshipandhavealargesupplyof the4 x 12 x 8 inchpriority mail shippingboxes.Eachitemwill fit in suchaboxbutall of themcombinedmaytakemultiple boxes. Naturally, you want to minimize the numberof boxesyouuse.

The ヲ ゥ イ ウ エ M エ heuristicis a greedyalgorithmfor this problem-itpro-cessesthe itemsin thesequencein which they arefirst ァ ゥ カ ・ 。 ョ 、 placesthemin thefirst box in whichtheyfit , scanningthroughboxesin increas-ing order. First-fit is not optimumbut ゥ エ ・ カ ・ イ takesmorethantwice asmanyboxesastheminimumpossible.

Problem3.16: iューャ・ュ・ エ first-fit to run in 0 H ッァI time.

393.18. RAYSCOVERINGARCS

3.18 RAYS COVERING ARCS

l・ エG ウ ケッオ areresponsiblefor the securityof a castle. The castlehas

コ [ Z Q s 。 コ コ コ コ エ エ i z j [ [ R R S S R W R Z Z Z R RTC (Thearcsfo<differeI1t robotsmayoverlap-)Youwantto mOI1itor

エィ・イッ「_エウ「ケゥ ウエ。ャャュァ」。ュ・イ。ウ attheceI1terofthecastlethatlookouttothepemI1eter.Eachcameracmlook alonga ray.TosavecostFyouwouldlike to minimize thenumberof cameras.

More formally, let { X } i = Q W「・ n arcs, wherethe i-th 。 ゥ ウthe ウ ー ッ ゥ ョ エ ウ on the ー ゥ ュ ・ エ ・ イ イ 」 ャ ・ thatsubtend。 ョ ォin theinterval { X ゥ } at thecenter.

A rayis a setof pohtsthatd subtmdthesameangleto theoriiI1-キ ・ ゥ 、 ・ ケ 。 t by the angleit ュ 。 ォ ・ ウ 。 ・ エ ッ エ ィ ・ ク M 。 ク ゥ ウ AsetRofrays"covers"thearcsif { X ゥ } ョ r ヲ ッ イ i = 1,… , no

probkm318: d ァ ョ 。 ョ ・ ヲ ヲ ゥ ャ ァ o ュ ヲ ッ イ ヲ ゥ ョ 、 ゥ ョ ァ 。 ュ Q ゥ i、 ゥ iョ 。 。 ャ ゥ エ ケ coveringエ ィ ・ setof rays.

3.19 k-CLUSTERING

A k-clusteringof a set O is a collectiOR{0170?··Ok}ofmnemptysubsetsH Q Q G 」 」 ャ オ Q s エ G I of P ゥ 」 ィ ィ 。 ウ エ ィ ・ ヲ ッ ャ ャ ッ キ ゥ ョ Q p ゥ p。 ョ ャ 、 0i n P ェ W QZN P] スゥ]ェ I .

Let d be a function (the 11distance")from 0 x 0 to z キ ィ ・ イ ・ Z+ istheset ッ ヲ ッ ョ ョ ・ ァ 。 エ ゥ カ ・ integers.

The needto computea k-clusteringFiRwhich elementsthat are far

コ エ 。 コ エ Z エ Q R R エ エ ゥ R W S S S R [ Z estore, etc.

Define the sepamtionsc of a k-clusteringC to be tke distmcebe-tweenthe two objectsin different clusterswhich are closest, i.e.. NGiᆳュゥョサ、H アIャー oゥ ア oェ ゥ ェスN iイ ゥエゥカ・ャSU the separatiF7·y uu

of how gooda job the clusterhgdoesof keephgthhgsWHet1arefar apartin differentclusters.

Thereis a ョ。エ 。ャ greedyalgorithmto computethe clustering: startwith 101 clusters, i.e., one clusterper ・ ャ ・ ュ ・ エ of O. Look for the paiof elemmtsh di{femt clusterswhich areclosest。 m ュ ・ イ ァ ・ ・ ゥ イ twoclusters;repeatthis mergea total of n - k ョ・ウ to obtaink clusters

This algorithmcmkmadeエ o

A ッ ウ エ ッ イ ・ エ ィ ・ 、 ゥ ウ エ 。 ョ ュ c ・ ウ 「 ・ ュ ァ 」 ッ ョ ウ ゥ 、 ・ イ anda union-find data-structureto representandmergethesubsets.

probkm3.19:Provethattheresulthgclusterhasthemaximumsepara-tion of all possiblek-clusterings.

CHAPTER3. mel algorithms38

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Eachmenuitem canhavemultiple levelsof sub-menusanda sub-menucanbereachedby clicking onits parentmenuitem.

The designeris providedwith a userstudy that detailshow oftenuserswant tasksto be triggered. (In a real application, we would alsoworry aboutgroupingrelateditems in the samesub-menuaswell butfor this problemwewill ignoregroupingrequirements.)

Problem3.15: How shouldthemenusystembe designedso asto min-imize the averageUI interactiontime if c = I? How would you do it ifc> I?

3.17 POINTSCOVERINGINTERVALS

Consideran engineerresponsiblefor a numberof taskson the factoryfloor. Eachtaskstartsat a fixed time andendsat a fixed time. The en-gineerwantsto visit the floor to checkon the tasks.Your taskis to helphim minimize thenumberof visits hemakes.In eachvisit, hecancheckon all the taskstakingplaceat the time of thevisit. A visit takesplaceata fixed time andhe canonly checkon taskstaking placeat exactlythattime.

More formally, modelthe tasksas 」 ャ ッ ウ ・ 、 intervalson the reallineh } i = Q WN A setS of visit times11covers"thetasksif {。 「ゥ}ョs

ヲッイ i = Q ー M

Problem3.17: Designanefficient algorithmfor finding aminimumcar-dinality setof visit timesthatcoversall the tasks.

3.16 PACKING FOR USPSPRIORITY MAIL

ャ・ UnitedStatesPostalServicemakesfixed-sizemailshippingboxes-you pay a fixed price for a ァゥカ・ 「ック andcanship anythingyou wantthat fits in thebox. Supposeyou havea setof n itemsthatyou needtoshipandhavealargesupplyof the4 x 12 x 8 inchpriority mail shippingboxes.Eachitemwill fit in suchaboxbutall of themcombinedmaytakemultiple boxes. Naturally, you want to minimize the numberof boxesyouuse.

The ヲ ゥ イ ウ エ M エ heuristicis a greedyalgorithmfor this problem-itpro-cessesthe itemsin thesequencein which they arefirst ァ ゥ カ ・ 。 ョ 、 placesthemin thefirst box in whichtheyfit , scanningthroughboxesin increas-ing order. First-fit is not optimumbut ゥ エ ・ カ ・ イ takesmorethantwice asmanyboxesastheminimumpossible.

Problem3.16: iューャ・ュ・ エ first-fit to run in 0 H ッァI time.

393.18. RAYSCOVERINGARCS

3.18 RAYS COVERING ARCS

l・ エG ウ ケッオ areresponsiblefor the securityof a castle. The castlehas

コ [ Z Q s 。 コ コ コ コ エ エ i z j [ [ R R S S R W R Z Z Z R RTC (Thearcsfo<differeI1t robotsmayoverlap-)Youwantto mOI1itor

エィ・イッ「_エウ「ケゥ ウエ。ャャュァ」。ュ・イ。ウ attheceI1terofthecastlethatlookouttothepemI1eter.Eachcameracmlook alonga ray.TosavecostFyouwouldlike to minimize thenumberof cameras.

More formally, let { X } i = Q W「・ n arcs, wherethe i-th 。 ゥ ウthe ウ ー ッ ゥ ョ エ ウ on the ー ゥ ュ ・ エ ・ イ イ 」 ャ ・ thatsubtend。 ョ ォin theinterval { X ゥ } at thecenter.

A rayis a setof pohtsthatd subtmdthesameangleto theoriiI1-キ ・ ゥ 、 ・ ケ 。 t by the angleit ュ 。 ォ ・ ウ 。 ・ エ ッ エ ィ ・ ク M 。 ク ゥ ウ AsetRofrays"covers"thearcsif { X ゥ } ョ r ヲ ッ イ i = 1,… , no

probkm318: d ァ ョ 。 ョ ・ ヲ ヲ ゥ ャ ァ o ュ ヲ ッ イ ヲ ゥ ョ 、 ゥ ョ ァ 。 ュ Q ゥ i、 ゥ iョ 。 。 ャ ゥ エ ケ coveringエ ィ ・ setof rays.

3.19 k-CLUSTERING

A k-clusteringof a set O is a collectiOR{0170?··Ok}ofmnemptysubsetsH Q Q G 」 」 ャ オ Q s エ G I of P ゥ 」 ィ ィ 。 ウ エ ィ ・ ヲ ッ ャ ャ ッ キ ゥ ョ Q p ゥ p。 ョ ャ 、 0i n P ェ W QZN P] スゥ]ェ I .

Let d be a function (the 11distance")from 0 x 0 to z キ ィ ・ イ ・ Z+ istheset ッ ヲ ッ ョ ョ ・ ァ 。 エ ゥ カ ・ integers.

The needto computea k-clusteringFiRwhich elementsthat are far

コ エ 。 コ エ Z エ Q R R エ エ ゥ R W S S S R [ Z estore, etc.

Define the sepamtionsc of a k-clusteringC to be tke distmcebe-tweenthe two objectsin different clusterswhich are closest, i.e.. NGiᆳュゥョサ、H アIャー oゥ ア oェ ゥ ェスN iイ ゥエゥカ・ャSU the separatiF7·y uu

of how gooda job the clusterhgdoesof keephgthhgsWHet1arefar apartin differentclusters.

Thereis a ョ。エ 。ャ greedyalgorithmto computethe clustering: startwith 101 clusters, i.e., one clusterper ・ ャ ・ ュ ・ エ of O. Look for the paiof elemmtsh di{femt clusterswhich areclosest。 m ュ ・ イ ァ ・ ・ ゥ イ twoclusters;repeatthis mergea total of n - k ョ・ウ to obtaink clusters

This algorithmcmkmadeエ o

A ッ ウ エ ッ イ ・ エ ィ ・ 、 ゥ ウ エ 。 ョ ュ c ・ ウ 「 ・ ュ ァ 」 ッ ョ ウ ゥ 、 ・ イ anda union-find data-structureto representandmergethesubsets.

probkm3.19:Provethattheresulthgclusterhasthemaximumsepara-tion of all possiblek-clusterings.

CHAPTER3. mel algorithms38

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Note that the algorithmaboveis very ウ ー ャ ゥ ウ エ ゥ 」 Z it doesnot attempttobalanceclustersizes, look at distancesoutsideof pairwiseclosestones,exploit my structurehthedistancefmction(e.g-rthe trimgle hequal-ity)F etc.II1arealisticsettiI1gy thesemd mazqmoreconsider-at-onareエ 。 ォ ・ ョ ャ エ ッ account.

3.20 PARTY PLANNING

LeOIla is holdhga partymdis tryhgto selectpeopleto hvite from herfrieRd cirk-SMhasN trieI1ds andshekmws which pairs of frimdsalreadyhoweachother.Leomwmtsto hvite asmmyfrimds aspos-siblebut shewaIIts eachhviteeto how at leastsix otherinviteesandッ エ know six otherinvitees.

Problem3.20: Deviseanefficient algorithm 。 エ takesasinput Leona'sN friendsanda setof pairsof friendswho know eachotherandreturns

Q invitation list thatmeetstheabovecriteria.

40 CHAPTER3. mel Malgorithms

Chapter4

Algorithtnson Graphs

Concerningthesebridges, it wasaskedwhetheranyonecouldarrangearoutein suchawaythathewould crosseachbridgeonceandonly once.

"Thesolutionof aproblemrelatingto thegeometryof

ーッウゥエゥッ B 1. Euler, 1741

A graphis a setof verticesanda setof edgesconnectingthesevertices.Mathematically, a directedgraphis a tuple (V,E) , whereV is a set ofverticesandE c V x V is the setof edges.An undirectedgraphis alsoa tuple (V,E); howeverE is a setof unorderedpairs of V. Graphsareoften decorated, e.g., by addinglengthsto edges, weightsto vertices, astartvertex, etc.

Graphsnaturallyarisewhenmodelinggeometricproblems, suchasdeterminingconnectedcities. Howevertheyaremoregeneralsincetheycanbeusedto modelmanykinds of イ ・ ャ 。 エ ゥ ッ ウ ィ ゥ ー ウ N

A graphcanbe representedin two ways-usinganadjacencylist oranadjacencymatrix. In theadjacencylist イ・ーイ・ウ・ョエ。エゥッ ヲッイ eachvertexv,a list of vertices 。 、 ェ 。 」 エ ッ ゥ ウ stored.The 。 」・ョ」ケ matrix representa-tion usesa IVI xlVI b ッ ッ ャ ・ 。 カ 。 ャ オ ・ 、 matrix indexedby vertices, with a 1indicatingthepresenceof anedge.Thecomplexityof a graphalgorithmis measuredin termsof thenumberof verticesandedges.

A tree(sometimescalledafreetree)is a specialkind of graph-itis anundirectedgraphthat is connectedbuthasno cycles. (Many equivalent、 ・ ゥ エ ゥ ッ ョ ウ exist, e.g., a graphis a free treeiff thereexistsa uniquepathbetweeneverypair of vertices.) Thereare a numberof カ 。 イ ゥ 。 エ ウ P エ ィ ・

basicideaof a tree-e.g., a rootedtreeis onewherea designatedvertex

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Note that the algorithmaboveis very ウ ー ャ ゥ ウ エ ゥ 」 Z it doesnot attempttobalanceclustersizes, look at distancesoutsideof pairwiseclosestones,exploit my structurehthedistancefmction(e.g-rthe trimgle hequal-ity)F etc.II1arealisticsettiI1gy thesemd mazqmoreconsider-at-onareエ 。 ォ ・ ョ ャ エ ッ account.

3.20 PARTY PLANNING

LeOIla is holdhga partymdis tryhgto selectpeopleto hvite from herfrieRd cirk-SMhasN trieI1ds andshekmws which pairs of frimdsalreadyhoweachother.Leomwmtsto hvite asmmyfrimds aspos-siblebut shewaIIts eachhviteeto how at leastsix otherinviteesandッ エ know six otherinvitees.

Problem3.20: Deviseanefficient algorithm 。 エ takesasinput Leona'sN friendsanda setof pairsof friendswho know eachotherandreturns

Q invitation list thatmeetstheabovecriteria.

40 CHAPTER3. mel Malgorithms

Chapter4

Algorithtnson Graphs

Concerningthesebridges, it wasaskedwhetheranyonecouldarrangearoutein suchawaythathewould crosseachbridgeonceandonly once.

"Thesolutionof aproblemrelatingto thegeometryof

ーッウゥエゥッ B 1. Euler, 1741

A graphis a setof verticesanda setof edgesconnectingthesevertices.Mathematically, a directedgraphis a tuple (V,E) , whereV is a set ofverticesandE c V x V is the setof edges.An undirectedgraphis alsoa tuple (V,E); howeverE is a setof unorderedpairs of V. Graphsareoften decorated, e.g., by addinglengthsto edges, weightsto vertices, astartvertex, etc.

Graphsnaturallyarisewhenmodelinggeometricproblems, suchasdeterminingconnectedcities. Howevertheyaremoregeneralsincetheycanbeusedto modelmanykinds of イ ・ ャ 。 エ ゥ ッ ウ ィ ゥ ー ウ N

A graphcanbe representedin two ways-usinganadjacencylist oranadjacencymatrix. In theadjacencylist イ・ーイ・ウ・ョエ。エゥッ ヲッイ eachvertexv,a list of vertices 。 、 ェ 。 」 エ ッ ゥ ウ stored.The 。 」・ョ」ケ matrix representa-tion usesa IVI xlVI b ッ ッ ャ ・ 。 カ 。 ャ オ ・ 、 matrix indexedby vertices, with a 1indicatingthepresenceof anedge.Thecomplexityof a graphalgorithmis measuredin termsof thenumberof verticesandedges.

A tree(sometimescalledafreetree)is a specialkind of graph-itis anundirectedgraphthat is connectedbuthasno cycles. (Many equivalent、 ・ ゥ エ ゥ ッ ョ ウ exist, e.g., a graphis a free treeiff thereexistsa uniquepathbetweeneverypair of vertices.) Thereare a numberof カ 。 イ ゥ 。 エ ウ P エ ィ ・

basicideaof a tree-e.g., a rootedtreeis onewherea designatedvertex

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

is calledtheroot, anorderedtreeis a rootedtreein whicheachvertexhasanorderingon its children, etc.

434.2. ORDERNODESIN A BINARYTREEBY DEPTH

4.2 ORDERNODESIN A BINARY TREE BY DEPTH

Thereare varioustraversalsthat canbe performed 。 tree: ゥ ョ M ッ イ

ー イ ・ M ッ イ 、 ・ 。 ョ 、 post-orderarethreenaturalexamples.

Problem 4.2: How would you efficiently return an array A[O. . .h] ,whereh is the heightof the tree andA [i] is the headof a linked list ofall thenodesin the treethatareatheighti?

4.3 CONNECTEDNESS

A connectedgraphis one for which, given any verticesu 。ョ、 エィ・イ・

existsa pathfrom u エッN The notion of connectednessholds for bothdirectedandundirectedgraphs-forundirectedgraphs, we sometimessimply saythereexistsa ー。 「・エキ・・ オ andv.

iョ エオゥエゥ イ・ャケ some graphs are more connectedthan others-e.g., aclique is moreconnectedthana tree. To bemorequantitative, we couldreferto agraphasbeing2V-connectedif it remainsconnectedevenif anysingleedgeis removed.A graphis 23-connectedif thereexistsanedgewhoseremovalleavesthe graphconnected.

One applicationof this ideais in fault tolerancefor datanetworks.Supposeyou are given a set of datacentersconnectedthrougha set ofdedicatedpoint-to-pointlinks. You want to be able to reachfrom anydatacenterto anyotherdatacenterthrougha combinationof thesededi-catedlinks. Sometimesoneof theselinks canbecometemporarilyout ofserviceandyou wantto ensurethatyour networkcansustainup to onefaulty link. How canyouverify this?

Problem4.3: Let G = (V,E) be a 」 o ョョ・ 」エ・、 オョ、 ゥ イ・」エ・、 ァ イ。ー ィN Howwouldyouefficiently checkif G ゥ ウR M」 ッョ ョ・」エ ・、 _ c。イ Q youmakeyour al-ァ o イ ゥ

T p c b WIRING

Consideracollectionof p electricalpins. Foreachpair of pins, theremayッ イ ュ 。 ケ ッ エ bea wire joining them. Thereare ー 。 ゥ イ ウ of pinswith a wirejoining them.

Problem4.4: Give an 0 Hー I time algorithmthat determinesif it ispossibleto placesomeof the pins on the left half of a PCB andthe resto エ ィ ・ right half suchthat eachwire is betweena pin on the left andapin on the right. Your algorithmshouldreturna placement, shouldoneexist.

Problem4.1: Givena two-dimensionalmatrixofblackandwhiteentriesrepresentingamazewith designatedentranceandexitpoints, find apathfrom theentranceto theexit, if oneexists.

• uDAnセolor ャGヲN o p eヲ Se

」Nヲ^ clasᆪャヲ GQエ| n fNウinHGI fッu cat・ エQ P ウ

of セ | s ォ N ".

ヲ a ャ ャ s r e e c Q r a p | Q h e o | s

CHAPTER4. ALGORITHMSONGRAPHS

Figure4. Thepowerof obscureproofs

N セ n ・ N N i N nッ ッ セ l v s N w l r o v e Nャ | ゥ G エ カ n エ n t

m u ヲ N ゥ | B j e N s Q P P

H ゥ h l i N i o n セ ゥ ゥ N エ N s ャ B h セ 1."$.c N o m o B B i tS ヲ I o c m

l ᄃ イ セ セ y ァ r N t セ

セ セ i ・ a エ n エ ^v _ エ ^ V [ Q Z [ 「 Ir e ッ _ セ セ ・ N t po 」N オ|GI|

p イ t<,VI.:\ s a G ヲ エ 。 N H I A I

l?:.'1 a.. i=oR, \1 ..‘

4.1 SEARCHINGA MAZE

It ismturdtoapplygraphmodelsmd algorithmsto spatialproblems-Considerablackmdwhite digitizedimageof amaze-wMtepixelsrep-reseI1topenareasmdblackspacesarewalls.UmreareWo specialpixels:oneis designatedtheentranceandtheotheris theexit

GraphSearch

Computhgverticeswhich arereachablefrom otherverticesis a fuzvdameRtaloneratioI1·Thereafetwobasicalgorithms-DepthFirstSearch(DFS)anddfeadtkFirstsearch(BFS)·Bothare ャ ィ ・ 。 イ M エ ゥ ・ M o H i v i + lEI)·They differ from eadotherh termsof the additionaLmformatiOI1theyprovidere.g-rBFScmbeusedto computedistmcesfrom thestartvertexmdDFScmbeusedto checkfor thepreseRceof cycles.

42

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

is calledtheroot, anorderedtreeis a rootedtreein whicheachvertexhasanorderingon its children, etc.

434.2. ORDERNODESIN A BINARYTREEBY DEPTH

4.2 ORDERNODESIN A BINARY TREE BY DEPTH

Thereare varioustraversalsthat canbe performed 。 tree: ゥ ョ M ッ イ

ー イ ・ M ッ イ 、 ・ 。 ョ 、 post-orderarethreenaturalexamples.

Problem 4.2: How would you efficiently return an array A[O. . .h] ,whereh is the heightof the tree andA [i] is the headof a linked list ofall thenodesin the treethatareatheighti?

4.3 CONNECTEDNESS

A connectedgraphis one for which, given any verticesu 。ョ、 エィ・イ・

existsa pathfrom u エッN The notion of connectednessholds for bothdirectedandundirectedgraphs-forundirectedgraphs, we sometimessimply saythereexistsa ー。 「・エキ・・ オ andv.

iョ エオゥエゥ イ・ャケ some graphs are more connectedthan others-e.g., aclique is moreconnectedthana tree. To bemorequantitative, we couldreferto agraphasbeing2V-connectedif it remainsconnectedevenif anysingleedgeis removed.A graphis 23-connectedif thereexistsanedgewhoseremovalleavesthe graphconnected.

One applicationof this ideais in fault tolerancefor datanetworks.Supposeyou are given a set of datacentersconnectedthrougha set ofdedicatedpoint-to-pointlinks. You want to be able to reachfrom anydatacenterto anyotherdatacenterthrougha combinationof thesededi-catedlinks. Sometimesoneof theselinks canbecometemporarilyout ofserviceandyou wantto ensurethatyour networkcansustainup to onefaulty link. How canyouverify this?

Problem4.3: Let G = (V,E) be a 」 o ョョ・ 」エ・、 オョ、 ゥ イ・」エ・、 ァ イ。ー ィN Howwouldyouefficiently checkif G ゥ ウR M」 ッョ ョ・」エ ・、 _ c。イ Q youmakeyour al-ァ o イ ゥ

T p c b WIRING

Consideracollectionof p electricalpins. Foreachpair of pins, theremayッ イ ュ 。 ケ ッ エ bea wire joining them. Thereare ー 。 ゥ イ ウ of pinswith a wirejoining them.

Problem4.4: Give an 0 Hー I time algorithmthat determinesif it ispossibleto placesomeof the pins on the left half of a PCB andthe resto エ ィ ・ right half suchthat eachwire is betweena pin on the left andapin on the right. Your algorithmshouldreturna placement, shouldoneexist.

Problem4.1: Givena two-dimensionalmatrixofblackandwhiteentriesrepresentingamazewith designatedentranceandexitpoints, find apathfrom theentranceto theexit, if oneexists.

• uDAnセolor ャGヲN o p eヲ Se

」Nヲ^ clasᆪャヲ GQエ| n fNウinHGI fッu cat・ エQ P ウ

of セ | s ォ N ".

ヲ a ャ ャ s r e e c Q r a p | Q h e o | s

CHAPTER4. ALGORITHMSONGRAPHS

Figure4. Thepowerof obscureproofs

N セ n ・ N N i N nッ ッ セ l v s N w l r o v e Nャ | ゥ G エ カ n エ n t

m u ヲ N ゥ | B j e N s Q P P

H ゥ h l i N i o n セ ゥ ゥ N エ N s ャ B h セ 1."$.c N o m o B B i tS ヲ I o c m

l ᄃ イ セ セ y ァ r N t セ

セ セ i ・ a エ n エ ^v _ エ ^ V [ Q Z [ 「 Ir e ッ _ セ セ ・ N t po 」N オ|GI|

p イ t<,VI.:\ s a G ヲ エ 。 N H I A I

l?:.'1 a.. i=oR, \1 ..‘

4.1 SEARCHINGA MAZE

It ismturdtoapplygraphmodelsmd algorithmsto spatialproblems-Considerablackmdwhite digitizedimageof amaze-wMtepixelsrep-reseI1topenareasmdblackspacesarewalls.UmreareWo specialpixels:oneis designatedtheentranceandtheotheris theexit

GraphSearch

Computhgverticeswhich arereachablefrom otherverticesis a fuzvdameRtaloneratioI1·Thereafetwobasicalgorithms-DepthFirstSearch(DFS)anddfeadtkFirstsearch(BFS)·Bothare ャ ィ ・ 。 イ M エ ゥ ・ M o H i v i + lEI)·They differ from eadotherh termsof the additionaLmformatiOI1theyprovidere.g-rBFScmbeusedto computedistmcesfrom thestartvertexmdDFScmbeusedto checkfor thepreseRceof cycles.

42

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

4.8 TREE DIAMETER

Packetsin EthernetLANs are routedaccordingto the uniquepathin atreewhoseverticescorrespoMto climts andedgescorrespondto phySEical commetiombetweeIItheclients.II1thisproblemrwewaI1t to desigI1

4.5 EXTENDED CONTACTS

You are ァ ゥ カ ・ 。 socialnetwork. Specifically, it consistsof a setof indi-vidualsmd for eachhdividualy a list of his contacts-(TheCOIItact relamゥゥッ ゥー medMbesymmetric-AmaybeaCOI1tactofBbutBmaynotbeacOLtactof A)Let's defim C tobeanexteMedcontactofAifhe iseithera cOI1tactof A or a cORtactof aI1exteI1dedcontactof A.

Problem4.5: Deviseanefficient algorithmwhichtakesa socialnetworkandcomputesfor eachindividualhis extended」 ッ エ 。 」 エ ウ N

4.7 EPHEMERAL STATE IN A FINITE STATE MACHINE

A finite statemachime(FSM)isa setof statesSF a setof hputsIF md atrmsitioMmctimT :S × IHS.If T(S?i)=urwe saythat Slgods to

n 。ーーャゥ」。エゥッ ッヲ input i. The エ イ 。 ョ ャ ウ ゥ エ ゥ ッ i Q ᆪ オ ョ ョ 」 エ ゥ ッ t 」 。 ョ Q be ァ ・ ョ ・ ・ イ 。 ャ ゥ コ コ ・ 、 d

エッウ ・ョ」・ ウ of ゥ イ Q

ッ エ ィ ・ イ セ ゥ ウ ・ T(s, ,,) セ T(T(s, (io, i1 ,. .. ,in-2) I' i n-1)'Thestateeissaidtobeephemeralifthereisasequeme?fhputsqsuch

thattheredoesnotexistaninputsequence(3 for キ セ ゥ セ ィ t j A | _ I (3) = e.Informally, e is ephemeralif thereis a possibilityof theFSM startingat eandgetthgto a statef from which it caI1I10treMm to e.

Problem4.7: Designanefficient algorithmwhich takesanFSM andre-turnsthesetof ephemeralstates.

45

4.11 ASSIGNING RADIO FREQUENCIES

If two neighboringradio stationsare using the sameradio frequency,therewouldbe a regiongeographicallybetweenthemwherethe signalfrom bothstationswouldbeequallystrongandtheresultinginterferencewouldcauseneitherof thesignalsto beusable.Henceneighboringradiostationstry to pick different frequencies.Considerthe problemwhere

4.9 TIMING ANALYSIS

4.10 TEAM PHOTODAY-1

You area photographerfor a soccermeet. You will betakingpicturesofpairs of opposingteams. Eachteamhas 20 playerson its roster. Eachpicturewill consistof two rows of players, onerow for eachof the twoteams. You want to placethe playersso that if PlayerA standsbehindPlayerB, hemustbetaller thanPlayerB.

Problem4.10: Describean efficient method that takes as input twoエ ・ ュ ャ 、 theheightsof the playersin the teamsandchecksif it is pos-sible to placeplayersto takethe picture-if it is possible, your functionshouldprint which teamcomesto the front andthe orderin which theplayersappear.How would you generalizeyour approachto determinethe largestnumberof teamsthat canbe photographedsimultaneouslysubjectto thesameconstraints?

A combinationallogic network consistsof primary inputs and logicgates. Someof the gatesmaybe designatedasbeingprimary outputs.Eachgatehasan outputanda numberof inputs-theseinputsmaybeー イ ャ 。 イ ケ inputsor theoutputsof othergates.A cycleof gatesis definedasa sequenceof gates(90, YQ _ Q 90) startingandendingat the samegatesuchthatfor eachconsecutivepair of gatesin thesequence, thefirstgateis aninput to thesecondgate.Cyclesof gatesaredisallowed.

Eachgatehasafixed delay.A changeatthe ー イ ゥ ュ 。 イ ケ ャ ー オ エ propagatesthroughthelogic networkandeventuallytheoutputof everygatestopschanging.

Problem4.9: Givena logic networkwith primaryinputschanging, findthesmallesttime afterwhich all theprimaryoutputsno longerchange.

analgorithmfor finding the "worst-case"route, i.e., エィ・ BQ P clientsthatarefurthestapart.

Problem4.8: Let T be a tree, whereeachedgeis labeledwith a real-valueddistance.Define the diameterof T to be the lengthof a longestpathin T. Designanefficient algorithmto computethediameterof T.

4.9. TIMING ANALYSISCHAPTER4. ALGORITHMSONGRAPHS

4.6 EULER TOUR

LeonhardEulerwrote a papertitled HSevenBridgesof kbenigsbergHm1736.Itis cOI1sideredto be the first paperiI1graphtheory.Theprobm

assetin thecity of k ・ ョ ゥ ァ ウ 「 ・ イ ァ whichwassituatedonbothsidesof the p イ ・ i r ゥ カ ・ イ md heludedtwo islandswhich werecomectedtoeachothermdthemahlandby seveIIbridges.Eulerposedtheproblof fhdhga walk throughthe city tkatwould crosseachbridgeexactly

e. In the ー 。 ー ・ e オ ャ ・ イ demonstratedエ ィ 。 エ キ 。 ウ impossibleto do so.More gemmlly}m Euler tour of a conmeteddirectedgraphGt

(VJ)is a cyclethatheludeseachedgeof G exactlyORce;itmayrepeaL

verticesmorethan P 」 ・ ッ

Problem4.6: Designa linear-timealgorithmto ャ 、 anEulertour if one

exists.

44

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

4.8 TREE DIAMETER

Packetsin EthernetLANs are routedaccordingto the uniquepathin atreewhoseverticescorrespoMto climts andedgescorrespondto phySEical commetiombetweeIItheclients.II1thisproblemrwewaI1t to desigI1

4.5 EXTENDED CONTACTS

You are ァ ゥ カ ・ 。 socialnetwork. Specifically, it consistsof a setof indi-vidualsmd for eachhdividualy a list of his contacts-(TheCOIItact relamゥゥッ ゥー medMbesymmetric-AmaybeaCOI1tactofBbutBmaynotbeacOLtactof A)Let's defim C tobeanexteMedcontactofAifhe iseithera cOI1tactof A or a cORtactof aI1exteI1dedcontactof A.

Problem4.5: Deviseanefficient algorithmwhichtakesa socialnetworkandcomputesfor eachindividualhis extended」 ッ エ 。 」 エ ウ N

4.7 EPHEMERAL STATE IN A FINITE STATE MACHINE

A finite statemachime(FSM)isa setof statesSF a setof hputsIF md atrmsitioMmctimT :S × IHS.If T(S?i)=urwe saythat Slgods to

n 。ーーャゥ」。エゥッ ッヲ input i. The エ イ 。 ョ ャ ウ ゥ エ ゥ ッ i Q ᆪ オ ョ ョ 」 エ ゥ ッ t 」 。 ョ Q be ァ ・ ョ ・ ・ イ 。 ャ ゥ コ コ ・ 、 d

エッウ ・ョ」・ ウ of ゥ イ Q

ッ エ ィ ・ イ セ ゥ ウ ・ T(s, ,,) セ T(T(s, (io, i1 ,. .. ,in-2) I' i n-1)'Thestateeissaidtobeephemeralifthereisasequeme?fhputsqsuch

thattheredoesnotexistaninputsequence(3 for キ セ ゥ セ ィ t j A | _ I (3) = e.Informally, e is ephemeralif thereis a possibilityof theFSM startingat eandgetthgto a statef from which it caI1I10treMm to e.

Problem4.7: Designanefficient algorithmwhich takesanFSM andre-turnsthesetof ephemeralstates.

45

4.11 ASSIGNING RADIO FREQUENCIES

If two neighboringradio stationsare using the sameradio frequency,therewouldbe a regiongeographicallybetweenthemwherethe signalfrom bothstationswouldbeequallystrongandtheresultinginterferencewouldcauseneitherof thesignalsto beusable.Henceneighboringradiostationstry to pick different frequencies.Considerthe problemwhere

4.9 TIMING ANALYSIS

4.10 TEAM PHOTODAY-1

You area photographerfor a soccermeet. You will betakingpicturesofpairs of opposingteams. Eachteamhas 20 playerson its roster. Eachpicturewill consistof two rows of players, onerow for eachof the twoteams. You want to placethe playersso that if PlayerA standsbehindPlayerB, hemustbetaller thanPlayerB.

Problem4.10: Describean efficient method that takes as input twoエ ・ ュ ャ 、 theheightsof the playersin the teamsandchecksif it is pos-sible to placeplayersto takethe picture-if it is possible, your functionshouldprint which teamcomesto the front andthe orderin which theplayersappear.How would you generalizeyour approachto determinethe largestnumberof teamsthat canbe photographedsimultaneouslysubjectto thesameconstraints?

A combinationallogic network consistsof primary inputs and logicgates. Someof the gatesmaybe designatedasbeingprimary outputs.Eachgatehasan outputanda numberof inputs-theseinputsmaybeー イ ャ 。 イ ケ inputsor theoutputsof othergates.A cycleof gatesis definedasa sequenceof gates(90, YQ _ Q 90) startingandendingat the samegatesuchthatfor eachconsecutivepair of gatesin thesequence, thefirstgateis aninput to thesecondgate.Cyclesof gatesaredisallowed.

Eachgatehasafixed delay.A changeatthe ー イ ゥ ュ 。 イ ケ ャ ー オ エ propagatesthroughthelogic networkandeventuallytheoutputof everygatestopschanging.

Problem4.9: Givena logic networkwith primaryinputschanging, findthesmallesttime afterwhich all theprimaryoutputsno longerchange.

analgorithmfor finding the "worst-case"route, i.e., エィ・ BQ P clientsthatarefurthestapart.

Problem4.8: Let T be a tree, whereeachedgeis labeledwith a real-valueddistance.Define the diameterof T to be the lengthof a longestpathin T. Designanefficient algorithmto computethediameterof T.

4.9. TIMING ANALYSISCHAPTER4. ALGORITHMSONGRAPHS

4.6 EULER TOUR

LeonhardEulerwrote a papertitled HSevenBridgesof kbenigsbergHm1736.Itis cOI1sideredto be the first paperiI1graphtheory.Theprobm

assetin thecity of k ・ ョ ゥ ァ ウ 「 ・ イ ァ whichwassituatedonbothsidesof the p イ ・ i r ゥ カ ・ イ md heludedtwo islandswhich werecomectedtoeachothermdthemahlandby seveIIbridges.Eulerposedtheproblof fhdhga walk throughthe city tkatwould crosseachbridgeexactly

e. In the ー 。 ー ・ e オ ャ ・ イ demonstratedエ ィ 。 エ キ 。 ウ impossibleto do so.More gemmlly}m Euler tour of a conmeteddirectedgraphGt

(VJ)is a cyclethatheludeseachedgeof G exactlyORce;itmayrepeaL

verticesmorethan P 」 ・ ッ

Problem4.6: Designa linear-timealgorithmto ャ 、 anEulertour if one

exists.

44

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

wehavejust two frequenciesavailableandwe aregivenaneighborhoodgraphof asetof radiostations.We aresupposedto assignthefrequenciesto theradiostationssuchthattheinterferenceis minimized.Supposeweare ゥ ョ エ ・ イ ・ ウ エ ・ 、 。 simplerproblemwherewe arehappyif for any ァ ゥ カ ・

radiostation, themajorityof its neighborsusea differentfrequencyfromthegiven ウ エ 。 エ ゥ ッ t ィ ゥ ウ canbemodeledasa graphcoloringproblem.

Let g H v E) be an undirectedgraph. A エキッ」ッャッイゥョァ of G is afunction assigningeachvertexof G to blackor white. Call a two-coloringdiverseif eachvertexhasat leasthalf its neighborsoppositein color toitself.

Problem4.11: Doeseverygraphhavea diversecoloring? How wouldyou computea diversecoloring, if it exists?

AdvancedGraphAlgorithms

Up to this pointwe lookedatbasicsearchandcombinatorialproper-ties of graphs.The algorithmswe consideredwere alllinear-timecom-plexity andrelativelystraightforward-themajorchallengewasin modeling theproblem。 ー ー イ イ ゥ 。 エ ・ ャ ケ N

Thereareessentiallyfour problemson graphsthatcanbesolvedeffi-cient1y, i.e., in polynomialtime. All otherproblemsareeithervariantsoftheseor very ャ ゥ ォ ・ ャ ケ ッ エ solvableby polynomialtime algorithms.

- Matching-givenanundirectedgraph, find a maximumcollectionof edgessubjectto the constraintthateveryvertexis incidentto atmostone edge. The matchingproblemfor bipartite graphsis es-peciallycommonandthe algorithmfor this problemis muchsim-pIer エ ィ Q for the generalcase.A commonvariantis themaximumweightedmatchingproblemin which edgeshaveweights and amaximumweightedgesetis sought, subjectto the matchingcon-straint.

- Shortestpaths-givena graph, directedor オ 、 ゥ イ ・ 」 エ ・ 、 with costsontheedges, find theminimumcostpathfrom a givenvertexto allvertices.Variantsincludecomputingthe shortestpathfor all pairsof vertices, thecasewherecostsareall nonnegative, andconstraintson thenumberof edges.

- Max flow-given a directedgraphwith a capacityfor eachedge,find themaximumflow from a givensourceto a givensink, wherea flow is a function mappingedgesto numberssatisfyingconser-vation H ッ キ into a vertexequalsthe flow out of it) and the edgecapacities.

- Minimum spanningtree-givena connectedundirectedgraph(V,E) with weights on eachedge, find a subsetE 1 of the edgeswith minimumtotalweightsuchthat H e

QI is connected.

4.13 COUNTING SHORTESTPATHS

47

You aregivena mapto a mazeof roomsinterconnectedby one-waycor-ridors. The map specifiesa setof entranceroomsanda treasureroom.

4.15 SHORTESTPATHS IN THE PRESENCEOF RANDOMIZATION

You are ァ ゥ カ ・ 。 mapwith a set of cities connectedby roadsof knownlengths.

A stormhasmadesomeroadsuncrossable.For eachroad, youknowtheprobabilityof theroadbeinguncrossable.A givenpathconsistingofa setof roadsis considereduncrossableif anyof the roadsin thepathisuncrossable.

Problem4.14: Find apathbetweena givenpairof citiesthatis themini-mumlengthpathamongstall thepathsfor whichtheprobabilityofbeingcrossableis greaterthan0.9.

4.14 RANDOM DIRECTED ACYCLIC GRAPH

Theremaybe manyshortestpathsbetweentwo verticesin a graph. Itis commonly the casethat a single shortestー。 ゥ ウ required, possiblyonewith the fewestedges, as in Problem4.12. Sometimeswe want toknowthenumberof shortestpaths, e.g., whenanalyzingthestructureofa Booleanfunctionor checkingthestability of a system.

Problem4.13: Developanefficientalgorithmthatcomputesthenumberof shortestpathsbetweenverticess and t in an undirectedgraphwithunit costedges.

4.12 SHORTESTPATH WITH FEWESTEDGES

4.12. SHORTESTPATHWITHFEWESTEDGES

Eachof thesehasa polynomialtime algorithmandcanbesolvedef-ficient1y in practicefor very largegraphs.

In the usualformulation of the shortestpath problem, the numberofedgesin thepathis not a consideration.

Heuristically, if we did want to avoid ー 。 ウ with a largenumberofedges, we canadda small amountto the cost of eachedge. Howeverdepending エ ィ ・ structureof thegraphandtheedgecosts, this maynotresultin theshortestpath.

Problem4.12: Designan algorithmwhich takesas input a graphG =

(V,E) , directedor undirected, anonnegativecostfunctiononE andver-ticess andt; your algorithmshouldoutputapathwith thefewestedgesamongstall shortestpathsfrom s to t.

CHAPTER4. ALGORITHMSONGRAPHS46

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

wehavejust two frequenciesavailableandwe aregivenaneighborhoodgraphof asetof radiostations.We aresupposedto assignthefrequenciesto theradiostationssuchthattheinterferenceis minimized.Supposeweare ゥ ョ エ ・ イ ・ ウ エ ・ 、 。 simplerproblemwherewe arehappyif for any ァ ゥ カ ・

radiostation, themajorityof its neighborsusea differentfrequencyfromthegiven ウ エ 。 エ ゥ ッ t ィ ゥ ウ canbemodeledasa graphcoloringproblem.

Let g H v E) be an undirectedgraph. A エキッ」ッャッイゥョァ of G is afunction assigningeachvertexof G to blackor white. Call a two-coloringdiverseif eachvertexhasat leasthalf its neighborsoppositein color toitself.

Problem4.11: Doeseverygraphhavea diversecoloring? How wouldyou computea diversecoloring, if it exists?

AdvancedGraphAlgorithms

Up to this pointwe lookedatbasicsearchandcombinatorialproper-ties of graphs.The algorithmswe consideredwere alllinear-timecom-plexity andrelativelystraightforward-themajorchallengewasin modeling theproblem。 ー ー イ イ ゥ 。 エ ・ ャ ケ N

Thereareessentiallyfour problemson graphsthatcanbesolvedeffi-cient1y, i.e., in polynomialtime. All otherproblemsareeithervariantsoftheseor very ャ ゥ ォ ・ ャ ケ ッ エ solvableby polynomialtime algorithms.

- Matching-givenanundirectedgraph, find a maximumcollectionof edgessubjectto the constraintthateveryvertexis incidentto atmostone edge. The matchingproblemfor bipartite graphsis es-peciallycommonandthe algorithmfor this problemis muchsim-pIer エ ィ Q for the generalcase.A commonvariantis themaximumweightedmatchingproblemin which edgeshaveweights and amaximumweightedgesetis sought, subjectto the matchingcon-straint.

- Shortestpaths-givena graph, directedor オ 、 ゥ イ ・ 」 エ ・ 、 with costsontheedges, find theminimumcostpathfrom a givenvertexto allvertices.Variantsincludecomputingthe shortestpathfor all pairsof vertices, thecasewherecostsareall nonnegative, andconstraintson thenumberof edges.

- Max flow-given a directedgraphwith a capacityfor eachedge,find themaximumflow from a givensourceto a givensink, wherea flow is a function mappingedgesto numberssatisfyingconser-vation H ッ キ into a vertexequalsthe flow out of it) and the edgecapacities.

- Minimum spanningtree-givena connectedundirectedgraph(V,E) with weights on eachedge, find a subsetE 1 of the edgeswith minimumtotalweightsuchthat H e

QI is connected.

4.13 COUNTING SHORTESTPATHS

47

You aregivena mapto a mazeof roomsinterconnectedby one-waycor-ridors. The map specifiesa setof entranceroomsanda treasureroom.

4.15 SHORTESTPATHS IN THE PRESENCEOF RANDOMIZATION

You are ァ ゥ カ ・ 。 mapwith a set of cities connectedby roadsof knownlengths.

A stormhasmadesomeroadsuncrossable.For eachroad, youknowtheprobabilityof theroadbeinguncrossable.A givenpathconsistingofa setof roadsis considereduncrossableif anyof the roadsin thepathisuncrossable.

Problem4.14: Find apathbetweena givenpairof citiesthatis themini-mumlengthpathamongstall thepathsfor whichtheprobabilityofbeingcrossableis greaterthan0.9.

4.14 RANDOM DIRECTED ACYCLIC GRAPH

Theremaybe manyshortestpathsbetweentwo verticesin a graph. Itis commonly the casethat a single shortestー。 ゥ ウ required, possiblyonewith the fewestedges, as in Problem4.12. Sometimeswe want toknowthenumberof shortestpaths, e.g., whenanalyzingthestructureofa Booleanfunctionor checkingthestability of a system.

Problem4.13: Developanefficientalgorithmthatcomputesthenumberof shortestpathsbetweenverticess and t in an undirectedgraphwithunit costedges.

4.12 SHORTESTPATH WITH FEWESTEDGES

4.12. SHORTESTPATHWITHFEWESTEDGES

Eachof thesehasa polynomialtime algorithmandcanbesolvedef-ficient1y in practicefor very largegraphs.

In the usualformulation of the shortestpath problem, the numberofedgesin thepathis not a consideration.

Heuristically, if we did want to avoid ー 。 ウ with a largenumberofedges, we canadda small amountto the cost of eachedge. Howeverdepending エ ィ ・ structureof thegraphandtheedgecosts, this maynotresultin theshortestpath.

Problem4.12: Designan algorithmwhich takesas input a graphG =

(V,E) , directedor undirected, anonnegativecostfunctiononE andver-ticess andt; your algorithmshouldoutputapathwith thefewestedgesamongstall shortestpathsfrom s to t.

CHAPTER4. ALGORITHMSONGRAPHS46

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Someof the roomsare ウ ー ・ 」 ゥ 。 ャ M キ ィ ・ ケ ッ オ arrive at a specialイ ッ ッ ュ ッ オ

are イ 。 ョ Q 、 ッ ュ ャ ケ エ イ 。 ョ Q s ー ッ イ エ ・ 、 out of it throughッ イ ャ ・ of the P iョ ・ ・ャ ・ 。 、 ゥ ョ Q out of ゥエ N t ャ Q ・ ュ 。 ー 、 ・ ウ ゥ Q 。 エ ・ ウ which roomsare s ー ャ 」 ゥ 。 ャ N You arealso エ ッ ャ 、 エ ィ 。 エ thewaythemazeゥ ウ 、 ・ ウ ゥ ァ ョ ・ 、 ゥ ウ thatonceyou leavearoom,thereis noway of comingbackto it.

Problem4.15: Finda strategywhichgetsyouto thetreasureroomin theminimumexpectedtime.

4.18 STABLE ASSIGNMENT

Considera departmentwith N graduateウ エ オ 、 ・ ョ エ ウ ャ 、 N professors.Eachstudenthasorderedall the professorsbasedon how keenhe is towork with them.Eachprofessorhasanorderedlist of all thestudents.

Problem4.18: Devisean algorithmwhich takesthe preferencesof theウ エ オ 、 ・ ョ エ ウ ャ 、 the professorsandpairsa studentwith his adviser. Thereshould be ョ 、 ・ ョ エ M 。 、 カ ゥ ウ ・ イ pair , (sO ,aD) and H ウ ャ Q I such that sOーイ・ヲ・イウQ to o 。 ョ 、 Q preferssO to s1.

4.16 TRAVELING SALESMAN WITH A CHOICE

s ーッウ・ you are 。 ・ ウ イ ュ 。

ゥ i ケ ッ オ can makep(i) ーイッ エN The cost of going from city i to city j isc(i , j) > O. You wantto establisha routefor ケ ッ ウ ・ ャ ヲ suchthatyou startfrom a city, visit a setof cities, andthencomebackto the ッ イ ゥ ョ 。 ャ city.You can chooseto ignore certaincities if you like. Your objectiveis tomaximizethe ratio of profit-to-cost.

Problem4.16: Devisean efficient algorithmfor finding a routewhichmaximizesthe ratio of the totalprofit to the total cost.

49

4.19 ARBITRAGE

4.20 BIRKHOFF-VON NEUMANN DECOMPOSITION

A crossbaris a pieceof networkinghardwarewhich has a numberofinputsandoutputs. It cansimultaneouslytransferpacketsfrom inputsto outputsin a singlecycle, aslongasno morethanonepacketleavesaninputandnomorethanonepacketarrivesatanygivenoutput. (Assumeall packetsareof thesamei ・ ァ エ ィ andtakeequallylong to transfer.)

Problem4.20: You aregivenanN x N matrix of nonnegativeintegers;A[i , j] encodesthe numberof packetsat input i that needto be trans-ferredto outputj. What is theleastnumberof cyclesneededto performthe transferencodedby A?

4.21 CHANNEL CAPACITY

You areexploringthe remotevalleys of PapuaNew Guinea, oneof thelast unchartedplacesin the world. You comeacrossa tribe that doesnot havemoney-insteadit relies on the bartersystem. There are Ncommoditieswhich are tradedand the exchangeratesare specifiedbya two-dimensionalmatrix. For example, threesheepcanbe exchangedfor sevengoats;four goatscanbe exchangedfor 200 poundsof wheat,etc.

Problem4.19: Devisean efficient algorithm to determinewhetherornot thereexistsan arbitrage-away to startwith a singleunit of somecommodity0 andconvertit backto morethanoneunit of 0 throughasequenceof exchanges.Assumethereareno transactioncosts, ratesdonot fluctuate, andthatfractionalquantitiesof itemscanbesold.

4.19. ARBITRAGE

Supposewe havethe capabilityof transmittingoneof the five symbols,A ,B ,0 ,D ,E , througha communicationchannel. In the absenceof er-rors, we cancommunicatelog2(5)bitswith eachsymbol.

Now, supposethe channelis noisy-specifically, the receivercan-not differentiate between the following pairs of symbols: IT{(A ,B), (B ,O) ,(O,D) , Hd eI A)}. We can still achieveerror-free」 ッ ュ ュ オ ゥ 」 。 エ ゥ ッ ョ by arrangingwith the receiverto only transmittwo outof the five ウケュ「ッャウ M・NァN A andO. We cannottransmitmore thantwosymbolsand guaranteethatwe do not makeerrorsbecausethensomepair mustbe in IT. In this fashion, we arelimited to log2(2) = 1 bit persymboltransmitted.

Problem4.21: Design a schemefor the given channel「 ケ n ィ ゥ 」 ィ thetransmitterandreceivercanachievemore than 1 bit per symbol trans-mitted.

CHAPTER4. ALGORITHMSONGRAPHS

4.17 ROAD NETWORK

The TexasDepartmeIItof TrmsportatiORisCOI1siderhgaddhga I1ewsectionof highwayto the 。 ウ HighwaySystem.Eachhighwaysectionconnectstwo cities.

Thestateofficials havesubmitteda numberof proposalsfor thenewhighway-eachproposalincludesthepair of citiesbeingconnectedandthelengthof thesection.

Problem4.17: Devise an efficient algorithm which takes the existingnetwork, the proposalsfor new highways, andreturnsone of the pro-posedhighwayswhichminimizestheshortestdriving distancebetweenthecitiesof EI PasoandCorpusChristi.

48

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Someof the roomsare ウ ー ・ 」 ゥ 。 ャ M キ ィ ・ ケ ッ オ arrive at a specialイ ッ ッ ュ ッ オ

are イ 。 ョ Q 、 ッ ュ ャ ケ エ イ 。 ョ Q s ー ッ イ エ ・ 、 out of it throughッ イ ャ ・ of the P iョ ・ ・ャ ・ 。 、 ゥ ョ Q out of ゥエ N t ャ Q ・ ュ 。 ー 、 ・ ウ ゥ Q 。 エ ・ ウ which roomsare s ー ャ 」 ゥ 。 ャ N You arealso エ ッ ャ 、 エ ィ 。 エ thewaythemazeゥ ウ 、 ・ ウ ゥ ァ ョ ・ 、 ゥ ウ thatonceyou leavearoom,thereis noway of comingbackto it.

Problem4.15: Finda strategywhichgetsyouto thetreasureroomin theminimumexpectedtime.

4.18 STABLE ASSIGNMENT

Considera departmentwith N graduateウ エ オ 、 ・ ョ エ ウ ャ 、 N professors.Eachstudenthasorderedall the professorsbasedon how keenhe is towork with them.Eachprofessorhasanorderedlist of all thestudents.

Problem4.18: Devisean algorithmwhich takesthe preferencesof theウ エ オ 、 ・ ョ エ ウ ャ 、 the professorsandpairsa studentwith his adviser. Thereshould be ョ 、 ・ ョ エ M 。 、 カ ゥ ウ ・ イ pair , (sO ,aD) and H ウ ャ Q I such that sOーイ・ヲ・イウQ to o 。 ョ 、 Q preferssO to s1.

4.16 TRAVELING SALESMAN WITH A CHOICE

s ーッウ・ you are 。 ・ ウ イ ュ 。

ゥ i ケ ッ オ can makep(i) ーイッ エN The cost of going from city i to city j isc(i , j) > O. You wantto establisha routefor ケ ッ ウ ・ ャ ヲ suchthatyou startfrom a city, visit a setof cities, andthencomebackto the ッ イ ゥ ョ 。 ャ city.You can chooseto ignore certaincities if you like. Your objectiveis tomaximizethe ratio of profit-to-cost.

Problem4.16: Devisean efficient algorithmfor finding a routewhichmaximizesthe ratio of the totalprofit to the total cost.

49

4.19 ARBITRAGE

4.20 BIRKHOFF-VON NEUMANN DECOMPOSITION

A crossbaris a pieceof networkinghardwarewhich has a numberofinputsandoutputs. It cansimultaneouslytransferpacketsfrom inputsto outputsin a singlecycle, aslongasno morethanonepacketleavesaninputandnomorethanonepacketarrivesatanygivenoutput. (Assumeall packetsareof thesamei ・ ァ エ ィ andtakeequallylong to transfer.)

Problem4.20: You aregivenanN x N matrix of nonnegativeintegers;A[i , j] encodesthe numberof packetsat input i that needto be trans-ferredto outputj. What is theleastnumberof cyclesneededto performthe transferencodedby A?

4.21 CHANNEL CAPACITY

You areexploringthe remotevalleys of PapuaNew Guinea, oneof thelast unchartedplacesin the world. You comeacrossa tribe that doesnot havemoney-insteadit relies on the bartersystem. There are Ncommoditieswhich are tradedand the exchangeratesare specifiedbya two-dimensionalmatrix. For example, threesheepcanbe exchangedfor sevengoats;four goatscanbe exchangedfor 200 poundsof wheat,etc.

Problem4.19: Devisean efficient algorithm to determinewhetherornot thereexistsan arbitrage-away to startwith a singleunit of somecommodity0 andconvertit backto morethanoneunit of 0 throughasequenceof exchanges.Assumethereareno transactioncosts, ratesdonot fluctuate, andthatfractionalquantitiesof itemscanbesold.

4.19. ARBITRAGE

Supposewe havethe capabilityof transmittingoneof the five symbols,A ,B ,0 ,D ,E , througha communicationchannel. In the absenceof er-rors, we cancommunicatelog2(5)bitswith eachsymbol.

Now, supposethe channelis noisy-specifically, the receivercan-not differentiate between the following pairs of symbols: IT{(A ,B), (B ,O) ,(O,D) , Hd eI A)}. We can still achieveerror-free」 ッ ュ ュ オ ゥ 」 。 エ ゥ ッ ョ by arrangingwith the receiverto only transmittwo outof the five ウケュ「ッャウ M・NァN A andO. We cannottransmitmore thantwosymbolsand guaranteethatwe do not makeerrorsbecausethensomepair mustbe in IT. In this fashion, we arelimited to log2(2) = 1 bit persymboltransmitted.

Problem4.21: Design a schemefor the given channel「 ケ n ィ ゥ 」 ィ thetransmitterandreceivercanachievemore than 1 bit per symbol trans-mitted.

CHAPTER4. ALGORITHMSONGRAPHS

4.17 ROAD NETWORK

The TexasDepartmeIItof TrmsportatiORisCOI1siderhgaddhga I1ewsectionof highwayto the 。 ウ HighwaySystem.Eachhighwaysectionconnectstwo cities.

Thestateofficials havesubmitteda numberof proposalsfor thenewhighway-eachproposalincludesthepair of citiesbeingconnectedandthelengthof thesection.

Problem4.17: Devise an efficient algorithm which takes the existingnetwork, the proposalsfor new highways, andreturnsone of the pro-posedhighwayswhichminimizestheshortestdriving distancebetweenthecitiesof EI PasoandCorpusChristi.

48

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

4.24 R s a t

4.23 DANCING WITH THE STARS

4.25 THEORY OF EQUALITY

Programsare usually checkedusing testing-aI1umber of manuallywritten or randomtest casesare applied to the programand the pro-gram's resultsarecheckedby assertionsor visual ゥ ョ ウ ー ・ 」 エ ゥ ッ

Formalverificationconsistsof examininga programandanalyticallydeterminingif thereexistsaninput for which an assertionfails. Formal

51

verificationof generalprogramsis undecidable.Howevertherearesig-nificant subclassesof generalprogramsfor which the verificationprob-lem is decidable.

Considerthe following problem: ァ ゥ カ ・ 。 setof variablesXl ,… ,X n,equalityconstraintsof theform Xi = Xj , andinequalityconstraintsof theform Xi G W x ェ is it possibleto satisfyall theconstraintssimultaneously?For example, the 」 ッ ョ ウ エ イ 。 ゥ ョ ] X2,X2 = xS M X4,Xl G W x T cannotbesatisfiedsimultaneously.

Suchconstraintsarisein checkingthe equivalenceof loop-freepro-gramswith uninterpretedfunctions.

Problem4.25: Designan efficient algorithmthat takesas input a col-lection of equality and inequality constraintsand decideswhethertheconstraintscanbesatisfiedsimultaneously.

4.25. THEORYOFEQUALITYCHAPTER4. ALGORITHMSONGRAPHS

You are orgmizhga celebritydmcecharity.specifiedy;aI1umberofcelebritieshaveofferedto be ー ャ ・ イ ウ for aballroomdance.The generalpublichasbeenゥ ョ イ ゥ エ ・ 、 to offerbidsonhowmuchtheyarewilling to payfor a dancewith eachcelebrity.

Somerules governingthe dance 。 イ ・ H Q N I eachcelebritywill danceッ 」 ・ at themost, (2.) eachbidderwill dance 」 ・ at themost, and(3.) thecelebritiesandthebiddersaredisjoint.

Problem4.23: Designan algorithmfor pairingbidderswith celebritiesto maximizethe revenuefrom the dance.

A Booleanlogic expressionis said to be in 」 ッ ェ オ 」 エ ゥ カ ・ normal form(CNF)if complemeRtationis ody appliedto variables;theッ ー ・ イ 。 エ Q Pis appliedto カ 。 イ ゥ 。 「 ャ ・ ウ ・ ゥ イ ョ エ ゥ ッ ョ N For example, H K「K I . Hf

b) . H ・ G 、I is in CNF. The terms K b+ ・ G ヲ 「 。ョ、K c' + darereferredto asclauses.

Determiningwhetheran ・ ク ー イ ・ ウ ウ ゥ ッ ゥ ョ CNF is satisfiableis conjec-turedto beintractable-i.e., no polynomialtime algorithmexistsfor thisproblem. Howeversomevariantsof CNF canbe solvedin polynomialエ ゥ ョ N

Problem4.24: Designa linear-timealgorithmfor checkingif a CNF inwhicheachclausecontainsno morethantwo variablesis satisfiable.

4.22 TEAM PHOTODAY-2

TMs pfoblemis a COI1timlatior1ofProblem4.1Or wherewe waI1tedmahwrithmto find the maximumnumberof teamsthat could be put inone ー ィ ッ エ ッ ァ イ ィ subjectto a placementconstraint.

Problem4.22: Designan efficient algorithm for computingthe mini-mum numberof subsetsof teamsso that the teamsin eachsubsetcanbeorganizedto appearin onephotograph, subjectto theplacementcon-straintandeachteamappearsin somesubset.

50

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

4.24 R s a t

4.23 DANCING WITH THE STARS

4.25 THEORY OF EQUALITY

Programsare usually checkedusing testing-aI1umber of manuallywritten or randomtest casesare applied to the programand the pro-gram's resultsarecheckedby assertionsor visual ゥ ョ ウ ー ・ 」 エ ゥ ッ

Formalverificationconsistsof examininga programandanalyticallydeterminingif thereexistsaninput for which an assertionfails. Formal

51

verificationof generalprogramsis undecidable.Howevertherearesig-nificant subclassesof generalprogramsfor which the verificationprob-lem is decidable.

Considerthe following problem: ァ ゥ カ ・ 。 setof variablesXl ,… ,X n,equalityconstraintsof theform Xi = Xj , andinequalityconstraintsof theform Xi G W x ェ is it possibleto satisfyall theconstraintssimultaneously?For example, the 」 ッ ョ ウ エ イ 。 ゥ ョ ] X2,X2 = xS M X4,Xl G W x T cannotbesatisfiedsimultaneously.

Suchconstraintsarisein checkingthe equivalenceof loop-freepro-gramswith uninterpretedfunctions.

Problem4.25: Designan efficient algorithmthat takesas input a col-lection of equality and inequality constraintsand decideswhethertheconstraintscanbesatisfiedsimultaneously.

4.25. THEORYOFEQUALITYCHAPTER4. ALGORITHMSONGRAPHS

You are orgmizhga celebritydmcecharity.specifiedy;aI1umberofcelebritieshaveofferedto be ー ャ ・ イ ウ for aballroomdance.The generalpublichasbeenゥ ョ イ ゥ エ ・ 、 to offerbidsonhowmuchtheyarewilling to payfor a dancewith eachcelebrity.

Somerules governingthe dance 。 イ ・ H Q N I eachcelebritywill danceッ 」 ・ at themost, (2.) eachbidderwill dance 」 ・ at themost, and(3.) thecelebritiesandthebiddersaredisjoint.

Problem4.23: Designan algorithmfor pairingbidderswith celebritiesto maximizethe revenuefrom the dance.

A Booleanlogic expressionis said to be in 」 ッ ェ オ 」 エ ゥ カ ・ normal form(CNF)if complemeRtationis ody appliedto variables;theッ ー ・ イ 。 エ Q Pis appliedto カ 。 イ ゥ 。 「 ャ ・ ウ ・ ゥ イ ョ エ ゥ ッ ョ N For example, H K「K I . Hf

b) . H ・ G 、I is in CNF. The terms K b+ ・ G ヲ 「 。ョ、K c' + darereferredto asclauses.

Determiningwhetheran ・ ク ー イ ・ ウ ウ ゥ ッ ゥ ョ CNF is satisfiableis conjec-turedto beintractable-i.e., no polynomialtime algorithmexistsfor thisproblem. Howeversomevariantsof CNF canbe solvedin polynomialエ ゥ ョ N

Problem4.24: Designa linear-timealgorithmfor checkingif a CNF inwhicheachclausecontainsno morethantwo variablesis satisfiable.

4.22 TEAM PHOTODAY-2

TMs pfoblemis a COI1timlatior1ofProblem4.1Or wherewe waI1tedmahwrithmto find the maximumnumberof teamsthat could be put inone ー ィ ッ エ ッ ァ イ ィ subjectto a placementconstraint.

Problem4.22: Designan efficient algorithm for computingthe mini-mum numberof subsetsof teamsso that the teamsin eachsubsetcanbeorganizedto appearin onephotograph, subjectto theplacementcon-straintandeachteamappearsin somesubset.

50

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter5

Algori thm.s on Strings

A generalpurposecomputerprogramandspecialpurposeapparatusfor matchingstringsof。ャーィ。 オュ・イゥ」 charactersaredisclosed.

H4iエO

g9KItrᄋMゥ

wml

Amoo

REG

kMRᄋxeT

Algorithms that operate P ウ エ イ ゥ ョ ァ ウ are of greatpracticaland founda-tional ゥ ュ ー ッ イ エ ャ 」 ・ N Practicalapplicationsincludeweb search, 」ッューゥャ。エゥッ ョ。エオイ。ャャ。ョ 。ァ・ processing, text editors, andDNA analysis.Froma theoreticalperspective, anyprogramcanbeviewedasimplementingafunction from{O ,1}-valuedstringsto {O ,1}-valuedstrings, accordingtocertainstringrewriting rules.

5.1 FIND ALL OCCURRENCESOF A SUBSTRING

A good string searchalgorithm ゥ ウ ョ 、 ョ ・ ョ エ 。 ャ to the performanceofmanyapplicationsandthereareseveral・ ャ ・ ァ 。 。 ャ ァ ッ イ ゥ エ ィ ュ ウ proposedforit, eachwith its owntradeoffs.As aresult, thereis no oneperfectanswerエ ッ N If someoneasksyou this ア オ ・ ウ エ ゥ ッ ゥ ョ aninterview, thebestway toapproachthis problemwouldbeto work throughone goodalgorithmindetail anddiscussthebreadthof otheralgorithmsfor solvingthis prob-lem.

Problem5.1: Given two strings s (searchstring) andT (text), find alloccurrencesof s in T.

5.2. STRINGMATCHINGWITHUNIQUE CHARACTERS 53

5.2 STRING matching W Qt h UNIQUE CHARACTERS

Supposewe arelookingfor asearchstringS in anotherstringT. A naIvealgorithmwould try to matchall thecharactersin S to charactersin T ateachッ ヲ ヲ ウ ・ エ N ィ ・ worst-casecomplexityof thenaIvealgorithmis 8(181 .ITI)-considerthecasewhereSis R o ウ andT ゥウM 1 Os followed by a1.

Problem5.2: Theworst-casebehaviorfor the naIvealgorithmrequiresmany duplicatedcharacters. Supposeno characteroccursmore thanoncein the searchstring. Devisean algorithmto efficiently searchforall occurrencesof thesearchstringin thetext string.

5.3 ROTATE A STRING

Let A beastringof lengthn. If we haveenoughmemoryto makea copyof A, rotatingA by i positionsis エイ ゥ。ャ[ wejustcomputeB[j] = A[(i + j)mod N If we aregiven 。 ャ ケ a constantamountof additionalmemoryc,we canrotatethe stringby c positionsa total of k = I セ l timesbut thisincreasesthetime complexityto X H N k).

ProblemU d ・ ウ ゥ ァ ョ a XHI algorithmfor イ ッ エ 。 エ ゥ 。 ウ エ ァ of n letterstothe left by i positions.You areallowedonly a constantnumberof bytesof additionalstorage.

5.4 TEST ROTATION

In Problem5.3, we facedthe problemof ・ ヲ ヲ ゥ 」 ゥ ・ ョ エ ャ ケ ー ャ ・ ュ ・ ョ エ ゥ ョ ァ rota-tion with a limited amountof memory.We now considerthe problemoftestingif onestringis a rotationof another.

Problem5.4: Developa linear-timealgorithmfor checkingif a stringS is a cyclic rotationof anotherstring R. (For example, arc is a cyclicrotationof car.)

5.5 NORMALIZE URLs

A URL is describedcanonicallyin thefollowing way:\ ー イ ッ エ ッ 」 ッ ^ Z i i \ ィ ッ ウ エ ョ 。 ュ ・ ^ Z [<port>J/<path>

There may be a number of different URL strings that are se-mantically ・ ア オ ゥ カ 。 ャ ・ エ N For example, cnn.com is ・アオゥカ。ャ・エ toィ エ エ ー Z O O 」 N comandhttp://www.ece.utexas.edu.I iゥ 、・クNィエュャ

to http://www.ece.utexas.edu.a ー ー ャ ゥ 」 。 エ ゥ ッ イ ャ s such as web searchwhich dealwith URLs needエ ッ ー ・ イ ヲ ッ イ ュ エ イ G 。 ョ ョ ウ ヲ ッ イ ュ ョ 。 。 エ ゥ ッ i ョ ウ ウ エ ッ a URL stringエ ッ イ Q P イ ュ ャ 。 ャ ゥ ゥ コ ・ ゥエ N The エ イ 。 G 。 ョ i ャ ウ ヲ ッ イ G イ ョ 。 エ ゥ ッ イ ョ ウ ウ mayvaryfrom applicationto appli-cation.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter5

Algori thm.s on Strings

A generalpurposecomputerprogramandspecialpurposeapparatusfor matchingstringsof。ャーィ。 オュ・イゥ」 charactersaredisclosed.

H4iエO

g9KItrᄋMゥ

wml

Amoo

REG

kMRᄋxeT

Algorithms that operate P ウ エ イ ゥ ョ ァ ウ are of greatpracticaland founda-tional ゥ ュ ー ッ イ エ ャ 」 ・ N Practicalapplicationsincludeweb search, 」ッューゥャ。エゥッ ョ。エオイ。ャャ。ョ 。ァ・ processing, text editors, andDNA analysis.Froma theoreticalperspective, anyprogramcanbeviewedasimplementingafunction from{O ,1}-valuedstringsto {O ,1}-valuedstrings, accordingtocertainstringrewriting rules.

5.1 FIND ALL OCCURRENCESOF A SUBSTRING

A good string searchalgorithm ゥ ウ ョ 、 ョ ・ ョ エ 。 ャ to the performanceofmanyapplicationsandthereareseveral・ ャ ・ ァ 。 。 ャ ァ ッ イ ゥ エ ィ ュ ウ proposedforit, eachwith its owntradeoffs.As aresult, thereis no oneperfectanswerエ ッ N If someoneasksyou this ア オ ・ ウ エ ゥ ッ ゥ ョ aninterview, thebestway toapproachthis problemwouldbeto work throughone goodalgorithmindetail anddiscussthebreadthof otheralgorithmsfor solvingthis prob-lem.

Problem5.1: Given two strings s (searchstring) andT (text), find alloccurrencesof s in T.

5.2. STRINGMATCHINGWITHUNIQUE CHARACTERS 53

5.2 STRING matching W Qt h UNIQUE CHARACTERS

Supposewe arelookingfor asearchstringS in anotherstringT. A naIvealgorithmwould try to matchall thecharactersin S to charactersin T ateachッ ヲ ヲ ウ ・ エ N ィ ・ worst-casecomplexityof thenaIvealgorithmis 8(181 .ITI)-considerthecasewhereSis R o ウ andT ゥウM 1 Os followed by a1.

Problem5.2: Theworst-casebehaviorfor the naIvealgorithmrequiresmany duplicatedcharacters. Supposeno characteroccursmore thanoncein the searchstring. Devisean algorithmto efficiently searchforall occurrencesof thesearchstringin thetext string.

5.3 ROTATE A STRING

Let A beastringof lengthn. If we haveenoughmemoryto makea copyof A, rotatingA by i positionsis エイ ゥ。ャ[ wejustcomputeB[j] = A[(i + j)mod N If we aregiven 。 ャ ケ a constantamountof additionalmemoryc,we canrotatethe stringby c positionsa total of k = I セ l timesbut thisincreasesthetime complexityto X H N k).

ProblemU d ・ ウ ゥ ァ ョ a XHI algorithmfor イ ッ エ 。 エ ゥ 。 ウ エ ァ of n letterstothe left by i positions.You areallowedonly a constantnumberof bytesof additionalstorage.

5.4 TEST ROTATION

In Problem5.3, we facedthe problemof ・ ヲ ヲ ゥ 」 ゥ ・ ョ エ ャ ケ ー ャ ・ ュ ・ ョ エ ゥ ョ ァ rota-tion with a limited amountof memory.We now considerthe problemoftestingif onestringis a rotationof another.

Problem5.4: Developa linear-timealgorithmfor checkingif a stringS is a cyclic rotationof anotherstring R. (For example, arc is a cyclicrotationof car.)

5.5 NORMALIZE URLs

A URL is describedcanonicallyin thefollowing way:\ ー イ ッ エ ッ 」 ッ ^ Z i i \ ィ ッ ウ エ ョ 。 ュ ・ ^ Z [<port>J/<path>

There may be a number of different URL strings that are se-mantically ・ ア オ ゥ カ 。 ャ ・ エ N For example, cnn.com is ・アオゥカ。ャ・エ toィ エ エ ー Z O O 」 N comandhttp://www.ece.utexas.edu.I iゥ 、・クNィエュャ

to http://www.ece.utexas.edu.a ー ー ャ ゥ 」 。 エ ゥ ッ イ ャ s such as web searchwhich dealwith URLs needエ ッ ー ・ イ ヲ ッ イ ュ エ イ G 。 ョ ョ ウ ヲ ッ イ ュ ョ 。 。 エ ゥ ッ i ョ ウ ウ エ ッ a URL stringエ ッ イ Q P イ ュ ャ 。 ャ ゥ ゥ コ ・ ゥエ N The エ イ 。 G 。 ョ i ャ ウ ヲ ッ イ G イ ョ 。 エ ゥ ッ イ ョ ウ ウ mayvaryfrom applicationto appli-cation.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Problem5.5: ImplementafunctionwhichtakesaURL asinputandper-forms the following transformationson it: (1.) makehostnameandpro-tocollowercase, (2.) if it endsin index.htmlor default.html, removethefilename, (3.) if protocolfield is missing, add''http://'' at thebeginning,and (4.) replaceconsecutive'I' charactersby a single'I' in the "path"segmentof theURL.

5.8 EDIT DISTANCES

Spell checkersmake suggestionsfor misspelledwords. Given a ュゥウ

spelledstring s, a spell checkershouldreturn キ ッ イ 、 ウ エ ィ ・ dictionarywhich arecloseto s.

Onedefinition of closenessis the numberof "edits" it would taketotransformthemisspelledword into a correctword, whereasingleedit isthedeletionor insertionof a singlecharacter.

Problem5.8: Giventwo stringsA andB, computetheminimumnum-berof editsneededto transformA into B.

5.7 PRETTY PRINTING

Considerthe problemof arranginga pieceof text in a fixed width font(i.e., eachcharacterhasthesamewidth) in a rectangularspace.Breakingwords acrossline boundariesis visually displeasing.If we avoid wordbreaking, thenwe may ヲ イ ・ ア オ ・ ャ ケ beleft with manyspacesat theendoflines (sincethenextword will not fit in the remainingspace).Howeverif we arecleveraboutwherewebreakthelines, we canreducethis effect.

Problem5.7: Given a long pieceof text, decomposeit into lines suchthat no word spansacrosstwo lines and the total wastedspaceat theendof eachline is minimized.

555.9. REGULAREXPRESSIONMATCHING

5.9 REGULAR EXPRESSIONMATCHING

a イ ・ 、 。 イ expressionis a ウ・アオ・ 」・ of charactersthat defines a set ofmatchingstrings.Forthis problemrwe defhea simplesubsetof a fullregular ・ ク ー イ ・ ウ ウ ゥ i 。 ョ 。 ァ ・ Z

a ャ ー ィ 。 「 ・ エ ゥ 」 。 ャ andnumericalcharactersmatch エ ィ ・ ュ ウ ・ ャ イ ・ ウ N For ex-ー i ・ aW9 will matchthatstringof 3 lettersキ ィ ・ イ ・ カ ・ イ 。 ー ー ・ 。 イ ウ N

Themetacharacters". and$ standfor thebeginningandendof thestring. For example, .....aW9matchesaW9 P ャケ at thestartof a stringaW9$ matchesaW9 ッイ 。エ the endof a string, and.....aW9$ ュ。エ」ィa stringonly if it is exactlyequalto aW9.

ュ・エ。」ィ。イ。」エ・イ . matchesany singlecharacter.For example,a.9matchesa89andxyaW9123butnot aw89.

- The metacharacter* specifiesa イ・ー・エ ゥッ ッヲ the single previousperiodor a literal character.For ・ ク ョ ー ャ ・ a.*9 matchesaw89.

By definition, regularexpressionr matchesstring s if s containsasubstringstartingatanypositionmatchingr. Forexample, aW9 anda.9matchstringxyaW9123but .....aW9doesnot.

Problem5.9:Desigz1malgorithmthattakesstrings s ャ 、 イ ャ 、 returnsif r matchess. (Assumer is a well-formed イ ・ ャ 。 イ expression.)

CHAPTER5. ALGORITHMSONSTRINGS

5.6 LONGESTPALINDROME SUBSEQUENCE

A palindromeis a stringwhich is equalto itself whenreversed.For ex-ample, thehumany 」 ィ イ ッ ュ ッ ウ ッ ュ ・ containsagenewith theaminoacidse-quence (0,A ,0 ,A ,A ,T ,T ,0 ,0 ,0 ,A ,T ,G,G,G,T ,T ,G,T ,G,G,A ,G),which ゥ ョ ・ ャ ・ ウ the ー 。 ャ ゥ ョ ャ 、 イ ッ ュ ゥ 」 ウ オ 「 ウ ・ ア オ ・ 」 ・ ウ (T,G,G,G,T) 。

H t g t I N Palindromicsubsequencesin DNA are significant becausetheyinfluencetheability of thestrandto loopbackonitself.

Problem5.6: Deviseanefficient algorithmthat takesa DNA sequenced { ャ 。 ョ 、 returns the i ・ エ ィ of the longest palindromic subse-quence.

54

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Problem5.5: ImplementafunctionwhichtakesaURL asinputandper-forms the following transformationson it: (1.) makehostnameandpro-tocollowercase, (2.) if it endsin index.htmlor default.html, removethefilename, (3.) if protocolfield is missing, add''http://'' at thebeginning,and (4.) replaceconsecutive'I' charactersby a single'I' in the "path"segmentof theURL.

5.8 EDIT DISTANCES

Spell checkersmake suggestionsfor misspelledwords. Given a ュゥウ

spelledstring s, a spell checkershouldreturn キ ッ イ 、 ウ エ ィ ・ dictionarywhich arecloseto s.

Onedefinition of closenessis the numberof "edits" it would taketotransformthemisspelledword into a correctword, whereasingleedit isthedeletionor insertionof a singlecharacter.

Problem5.8: Giventwo stringsA andB, computetheminimumnum-berof editsneededto transformA into B.

5.7 PRETTY PRINTING

Considerthe problemof arranginga pieceof text in a fixed width font(i.e., eachcharacterhasthesamewidth) in a rectangularspace.Breakingwords acrossline boundariesis visually displeasing.If we avoid wordbreaking, thenwe may ヲ イ ・ ア オ ・ ャ ケ beleft with manyspacesat theendoflines (sincethenextword will not fit in the remainingspace).Howeverif we arecleveraboutwherewebreakthelines, we canreducethis effect.

Problem5.7: Given a long pieceof text, decomposeit into lines suchthat no word spansacrosstwo lines and the total wastedspaceat theendof eachline is minimized.

555.9. REGULAREXPRESSIONMATCHING

5.9 REGULAR EXPRESSIONMATCHING

a イ ・ 、 。 イ expressionis a ウ・アオ・ 」・ of charactersthat defines a set ofmatchingstrings.Forthis problemrwe defhea simplesubsetof a fullregular ・ ク ー イ ・ ウ ウ ゥ i 。 ョ 。 ァ ・ Z

a ャ ー ィ 。 「 ・ エ ゥ 」 。 ャ andnumericalcharactersmatch エ ィ ・ ュ ウ ・ ャ イ ・ ウ N For ex-ー i ・ aW9 will matchthatstringof 3 lettersキ ィ ・ イ ・ カ ・ イ 。 ー ー ・ 。 イ ウ N

Themetacharacters". and$ standfor thebeginningandendof thestring. For example, .....aW9matchesaW9 P ャケ at thestartof a stringaW9$ matchesaW9 ッイ 。エ the endof a string, and.....aW9$ ュ。エ」ィa stringonly if it is exactlyequalto aW9.

ュ・エ。」ィ。イ。」エ・イ . matchesany singlecharacter.For example,a.9matchesa89andxyaW9123butnot aw89.

- The metacharacter* specifiesa イ・ー・エ ゥッ ッヲ the single previousperiodor a literal character.For ・ ク ョ ー ャ ・ a.*9 matchesaw89.

By definition, regularexpressionr matchesstring s if s containsasubstringstartingatanypositionmatchingr. Forexample, aW9 anda.9matchstringxyaW9123but .....aW9doesnot.

Problem5.9:Desigz1malgorithmthattakesstrings s ャ 、 イ ャ 、 returnsif r matchess. (Assumer is a well-formed イ ・ ャ 。 イ expression.)

CHAPTER5. ALGORITHMSONSTRINGS

5.6 LONGESTPALINDROME SUBSEQUENCE

A palindromeis a stringwhich is equalto itself whenreversed.For ex-ample, thehumany 」 ィ イ ッ ュ ッ ウ ッ ュ ・ containsagenewith theaminoacidse-quence (0,A ,0 ,A ,A ,T ,T ,0 ,0 ,0 ,A ,T ,G,G,G,T ,T ,G,T ,G,G,A ,G),which ゥ ョ ・ ャ ・ ウ the ー 。 ャ ゥ ョ ャ 、 イ ッ ュ ゥ 」 ウ オ 「 ウ ・ ア オ ・ 」 ・ ウ (T,G,G,G,T) 。

H t g t I N Palindromicsubsequencesin DNA are significant becausetheyinfluencetheability of thestrandto loopbackonitself.

Problem5.6: Deviseanefficient algorithmthat takesa DNA sequenced { ャ 。 ョ 、 returns the i ・ エ ィ of the longest palindromic subse-quence.

54If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter6

Intractability

All of thegeneralmethodspresentlyknownforcomputhgthechromaticRumberof agraph,decidingwhetheragraphhasa h。ュゥ Qエ ッ ゥ。ョ

circuit! or solvingasystemof linearinequalitiesin whichthevariablesare 」ッ ウエイ。ゥョ・、 to be0 orI! requireacombinatorialsearchfor whichtheworst-casetime requirementgrowsexponentiallywith thelengthof theinput. InthispaperFwe give theoremswhichstrORglysuggest!butdonot imply! thattheseproblems!aswell asmanyothers!will remainintractableperpetually.

IIReducibility AmongCombinatorialProblems/'R. Karp! 1972

h mgheeriI1gsetthgsryou will sometimesenc01mter-problemsthatm bedirectly solvedushgetikieI1t textbookalgorithmssuckasbhary

ch md shortestpaths-Aswe havesemiI1the earlier 」 ィ 。 ー エ ・ イ ウ fis often difficult to identify suchproblemsbecausethe core 。 ャ ァ ッ イ ィ ュ ゥ 」problemis obscuredbydetails.MoregeI1erally}youmayeI1Comterprob-lemswhich canbetransformedinto ・ ア オ ゥ カ 。 ャ ・ エ problemswhichhaveanefficient textbook 。 ャ ァ ッ イ ィ ュ or ー イ ッ 「 ャ ・ セ ウ which canbesolvedefficientlyusingmeta-algorithmssuchas d

It is very ofteRthecasehoweverthat the pyoblemyou are ァ ゥ カ ・

intfactable-i.e-Ftheremaynot existm effideI1t algorithmfor the probmlem-Complexitytheory addressestheseproblems-7sometlave bemー イ ッ カ ・ エ ッ nothaveanefficient ウ ッ ャ オ エ ゥ ッ H ウ オ 」 ィ ascheckingthevalidity ofrelationshipsゥ ョ カ ッ ャ カ ゥ ョ ァ K \ ッ ョ the integers)but the vastmajorityareORly cOnjecturedto be iIItractable The CNF-SATproblem(4.Prob-

6.1. 0-1 KNAPSACK 57

vIュj

ュd y n a ャ

p d o a m m i n g

a l g o r m D Z

o HヲQセRNHII

SE.:WNG ON £BAY:HiI

STILL wセ ャ\ AngONYOOR r jヲ eヲ

\

HEt..L UP.

Figure5. P = N P! by XKCD

lem6.5) is anexampleof a problemthatis 」 ッ ェ ・ 」 エ オ イ ・ 、 to beintractable.When facedwith a problemthat appearsto be intractable!the first

thing to do is to prove intractability! typically by efficiently reducingaproblemthat is intractableto i t. Often this reductiongives insight intothecauseof intractability.

Unlessyou are a complexity theorist, proving a problemto be in-tractableis a startingpoint! not an endpoint. Remembersomethingisa problemonly if it hasa solution. Therearea numberof approachestosolvingintractableproblems:

- Brute-forcesolutionswhich are typically ・ ク ー ッ ョ ・ エ ゥ 。 ャ but may beacceptable!if the instancesencounteredaresmall.

- Branch-and-boundtechniqueswhich prunemuchof the complex-ity of abrute-forcesearch.a ー ー イ ッ ク ゥ ュ 。 エ ゥ ッ 。 ャ ァ ッ イ ゥ エ ィ ュ ウ whichreturnasolutionthatis provablycloseto optimum.

- Heuristicsbasedoninsight! commoncaseanalysis!andcarefultun-ing thatmaysolvetheproblemreasonablywell.p 。 イ 。 ャ ャ ・ ャ algorithms!whereina largenumberof computerscanworkP ウ オ 「 ー 。 イ エ ウ simultaneously.

6.1 0-1 KNAPSACK

A thief hasto choosefrom n items. Itemi canbe sold ヲ ッ イ 、 ッ ャ ャ 。 イ ウ andweighs ー ッ オ ョ 、 ウ H 。 ョ 、 。 イ ・ integers). The thief wants to take asvaluablea load as possiblebut he can carry at most W poundsin hisknapsack.

Problem6.1: Designanalgorithmthatwill selecta subsetof itemsthathasmaximumvalue andweighsat mostW pounds. (This problemis

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter6

Intractability

All of thegeneralmethodspresentlyknownforcomputhgthechromaticRumberof agraph,decidingwhetheragraphhasa h。ュゥ Qエ ッ ゥ。ョ

circuit! or solvingasystemof linearinequalitiesin whichthevariablesare 」ッ ウエイ。ゥョ・、 to be0 orI! requireacombinatorialsearchfor whichtheworst-casetime requirementgrowsexponentiallywith thelengthof theinput. InthispaperFwe give theoremswhichstrORglysuggest!butdonot imply! thattheseproblems!aswell asmanyothers!will remainintractableperpetually.

IIReducibility AmongCombinatorialProblems/'R. Karp! 1972

h mgheeriI1gsetthgsryou will sometimesenc01mter-problemsthatm bedirectly solvedushgetikieI1t textbookalgorithmssuckasbhary

ch md shortestpaths-Aswe havesemiI1the earlier 」 ィ 。 ー エ ・ イ ウ fis often difficult to identify suchproblemsbecausethe core 。 ャ ァ ッ イ ィ ュ ゥ 」problemis obscuredbydetails.MoregeI1erally}youmayeI1Comterprob-lemswhich canbetransformedinto ・ ア オ ゥ カ 。 ャ ・ エ problemswhichhaveanefficient textbook 。 ャ ァ ッ イ ィ ュ or ー イ ッ 「 ャ ・ セ ウ which canbesolvedefficientlyusingmeta-algorithmssuchas d

It is very ofteRthecasehoweverthat the pyoblemyou are ァ ゥ カ ・

intfactable-i.e-Ftheremaynot existm effideI1t algorithmfor the probmlem-Complexitytheory addressestheseproblems-7sometlave bemー イ ッ カ ・ エ ッ nothaveanefficient ウ ッ ャ オ エ ゥ ッ H ウ オ 」 ィ ascheckingthevalidity ofrelationshipsゥ ョ カ ッ ャ カ ゥ ョ ァ K \ ッ ョ the integers)but the vastmajorityareORly cOnjecturedto be iIItractable The CNF-SATproblem(4.Prob-

6.1. 0-1 KNAPSACK 57

vIュj

ュd y n a ャ

p d o a m m i n g

a l g o r m D Z

o HヲQセRNHII

SE.:WNG ON £BAY:HiI

STILL wセ ャ\ AngONYOOR r jヲ eヲ

\

HEt..L UP.

Figure5. P = N P! by XKCD

lem6.5) is anexampleof a problemthatis 」 ッ ェ ・ 」 エ オ イ ・ 、 to beintractable.When facedwith a problemthat appearsto be intractable!the first

thing to do is to prove intractability! typically by efficiently reducingaproblemthat is intractableto i t. Often this reductiongives insight intothecauseof intractability.

Unlessyou are a complexity theorist, proving a problemto be in-tractableis a startingpoint! not an endpoint. Remembersomethingisa problemonly if it hasa solution. Therearea numberof approachestosolvingintractableproblems:

- Brute-forcesolutionswhich are typically ・ ク ー ッ ョ ・ エ ゥ 。 ャ but may beacceptable!if the instancesencounteredaresmall.

- Branch-and-boundtechniqueswhich prunemuchof the complex-ity of abrute-forcesearch.a ー ー イ ッ ク ゥ ュ 。 エ ゥ ッ 。 ャ ァ ッ イ ゥ エ ィ ュ ウ whichreturnasolutionthatis provablycloseto optimum.

- Heuristicsbasedoninsight! commoncaseanalysis!andcarefultun-ing thatmaysolvetheproblemreasonablywell.p 。 イ 。 ャ ャ ・ ャ algorithms!whereina largenumberof computerscanworkP ウ オ 「 ー 。 イ エ ウ simultaneously.

6.1 0-1 KNAPSACK

A thief hasto choosefrom n items. Itemi canbe sold ヲ ッ イ 、 ッ ャ ャ 。 イ ウ andweighs ー ッ オ ョ 、 ウ H 。 ョ 、 。 イ ・ integers). The thief wants to take asvaluablea load as possiblebut he can carry at most W poundsin hisknapsack.

Problem6.1: Designanalgorithmthatwill selecta subsetof itemsthathasmaximumvalue andweighsat mostW pounds. (This problemis

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

6.3 FACILITY LOCATION PROBLEM

6.2 TRAVELING SALESMAN IN THE PLANE

calledthe 0-1 knapsackproblembecauseeachitem musteitherbe takenor left behind-thethief cannottake a fractional amountof an item ortakeanitemmorethanonce.)

The following two problemsexhibit structurethat canbe exploitedto comeup with fast algorithmsthat return a solution that is within aconstantfactor of theoptimum(2 in bothcases).

59

6.7 HARDY-RAMANUJAN NUMBER

「 ・ mathematicianG. H. Hardywasonhis way to visit his collaboratorS. r 。ョ 。ョ who wasin the hospital. Hardyremarkedto Ramanujanthathetraveledin taxi cabnumber1729whichseemeda dull oneandhehopedit wasnotabadomen.To this, Ramanujanrepliedthat1729wasavery interestingnumber-itwasthe smallestnumberexpressibleasthesumof cubesof two numbersin two differentways. Indeed, 103 + 93 =123 + Q S QWRYN

Q , , セセRZ クMセ ヲ z ••…•• x

6.6 SCHEDULING

We needto scheduleN lecturesin M classrooms.Someof thoselecturesareprerequisitesfor others.

Problem6.6: How would you choosewhenandwhereto hold the lec-turesin orderto finish all the lecturesassoonaspossible?

6.5 CNF-SAT

TheCNF-SATproblemwas 、 ・ ヲ ゥ ョ ・ 、 p イ ッ 「 ャ ・ ュ 4.24. In thatproblem, weaskedfor a ャゥョ・。イ エゥュ・ algorithmfor the specialcasewhereeachclausehadexactlytwo literals.

Problem6.5: Designanalgorithmfor CNF-SAT.Your algorithmshouldusebranch-and-boundto prunepartial assignmentsthat can ・ 。 ウ ゥ 「 ・

shownto beunsatisfiable.

ャ ・ following problemsillustrate the use of heuristic searchandpruningprinciples.

constructedasfollows: thefirst elementis x; eachsucceedingelementiseither the squareof somepreviouslycomputedelementor the productof any two previouslycomputedelements.The numberof multiplica-tions to evaluatexn is thenumberof termsin theshortestsuchprogramsequenceminus one. No efficient methodis known for the problemofdeterminingthe minimum numberof multiplicationsneededto evalu-atexn ; theproblemfor multiple ・ ク ー ッ ・ ウ is knownto beNP-complete.

Problem6.4: How wouldyou determinetheminimumnumberof mul-tiplicationsto evaluatex 30?

A straight-lineprogramfor 」 ッ ュ ー オ エ ゥ ョ ァ ゥ ウ a finite ウ ・ ア オ ・ 」 ・

6.4. COMPUTINGX N

6.4 COMPUTING xn

CHAPTER6. intracl bility

Let a ッ a M Q be a setof 」ゥエゥ・ウN We are trying to selectk cities tolocatewarehouses.We キ ャエ to choosethe k cities in sucha way thatthe cities are close to the warehouses.Let's saywe define the cost ofa warehouseassignmentto be the maximumdistanceof any city to awarehouse.

The problemof finding a warehouseassignmentthat hasthe mini-mumcostis knownto beNP-complete.

Problem6.3: Designa fast algorithmfor selectingwarehouselocationsthatis provablywithin a constantfactor of theoptimumsolution.

Thefollowing two problemsarebestsolvedusingbranch-and-boundwith intelligentboundingandbranchselection.

Supposea salesmanneedsto visit a set of cities Ao,A1,… ,An - 1. Foranyセ orderedpair of cities (Ai ,A j ), thereis a cost c(Ai ,A j ) of travelingfrom thefirst to the ウ ・ 」 ッ 、 city. Weneedto designalow costtour for thesalesman.

a エ ッ ゥ ウ asequenceof cities (Bo,B 1 ,. . . b 「 b iエ 」。イ エ。イ エ。 エ。

」ゥエケ。ョ ャ、 the ウ 。 ャ ・ ウ ュ ャ 。 ョ ャ 」 。 ョ カ ゥ ウ ゥ エ エ thecities in anyorder.All thecitiesmustappearin the ウ ウ ・ ア オ ・ ョ 」 ・ (Bo,B 1 ,… ,Bn - 1). (Note that this impliesthatall thecitiesin this subsequencearedistinct.)

The costof the tour is the sumof the costsof the ウ オ 」 」 ・ ウ ウ ゥ カ ・ pairs(Bi ,B i +1 ュッ、 ゥ ッ エッMQN

Determiningtheminimumcosttour is aclassicNP-completeproblemandthe problemremainshardevenif we just askfor a tour whosecostis within a given ュ オ ャ エ i ・ IvI of theminimumcosttour. Howeverthereis aspecialcasefor which this problem」 。 「 ・ efficiently solved.

Problem6.2: Supposeall thecitiesarelocatedin someEuclideanspaceandthe costof travelingfrom onecity to anotheris a constantmultipleof the distancebetweenthe cities. Give an efficient procedurefor com-putinga tourwhosecost ゥ ウ ャ 。 イ ャ エ ・ ・ 、 to bewithin a factorof two of thecostof anoptimumtour.

58

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

6.3 FACILITY LOCATION PROBLEM

6.2 TRAVELING SALESMAN IN THE PLANE

calledthe 0-1 knapsackproblembecauseeachitem musteitherbe takenor left behind-thethief cannottake a fractional amountof an item ortakeanitemmorethanonce.)

The following two problemsexhibit structurethat canbe exploitedto comeup with fast algorithmsthat return a solution that is within aconstantfactor of theoptimum(2 in bothcases).

59

6.7 HARDY-RAMANUJAN NUMBER

「 ・ mathematicianG. H. Hardywasonhis way to visit his collaboratorS. r 。ョ 。ョ who wasin the hospital. Hardyremarkedto Ramanujanthathetraveledin taxi cabnumber1729whichseemeda dull oneandhehopedit wasnotabadomen.To this, Ramanujanrepliedthat1729wasavery interestingnumber-itwasthe smallestnumberexpressibleasthesumof cubesof two numbersin two differentways. Indeed, 103 + 93 =123 + Q S QWRYN

Q , , セセRZ クMセ ヲ z ••…•• x

6.6 SCHEDULING

We needto scheduleN lecturesin M classrooms.Someof thoselecturesareprerequisitesfor others.

Problem6.6: How would you choosewhenandwhereto hold the lec-turesin orderto finish all the lecturesassoonaspossible?

6.5 CNF-SAT

TheCNF-SATproblemwas 、 ・ ヲ ゥ ョ ・ 、 p イ ッ 「 ャ ・ ュ 4.24. In thatproblem, weaskedfor a ャゥョ・。イ エゥュ・ algorithmfor the specialcasewhereeachclausehadexactlytwo literals.

Problem6.5: Designanalgorithmfor CNF-SAT.Your algorithmshouldusebranch-and-boundto prunepartial assignmentsthat can ・ 。 ウ ゥ 「 ・

shownto beunsatisfiable.

ャ ・ following problemsillustrate the use of heuristic searchandpruningprinciples.

constructedasfollows: thefirst elementis x; eachsucceedingelementiseither the squareof somepreviouslycomputedelementor the productof any two previouslycomputedelements.The numberof multiplica-tions to evaluatexn is thenumberof termsin theshortestsuchprogramsequenceminus one. No efficient methodis known for the problemofdeterminingthe minimum numberof multiplicationsneededto evalu-atexn ; theproblemfor multiple ・ ク ー ッ ・ ウ is knownto beNP-complete.

Problem6.4: How wouldyou determinetheminimumnumberof mul-tiplicationsto evaluatex 30?

A straight-lineprogramfor 」 ッ ュ ー オ エ ゥ ョ ァ ゥ ウ a finite ウ ・ ア オ ・ 」 ・

6.4. COMPUTINGX N

6.4 COMPUTING xn

CHAPTER6. intracl bility

Let a ッ a M Q be a setof 」ゥエゥ・ウN We are trying to selectk cities tolocatewarehouses.We キ ャエ to choosethe k cities in sucha way thatthe cities are close to the warehouses.Let's saywe define the cost ofa warehouseassignmentto be the maximumdistanceof any city to awarehouse.

The problemof finding a warehouseassignmentthat hasthe mini-mumcostis knownto beNP-complete.

Problem6.3: Designa fast algorithmfor selectingwarehouselocationsthatis provablywithin a constantfactor of theoptimumsolution.

Thefollowing two problemsarebestsolvedusingbranch-and-boundwith intelligentboundingandbranchselection.

Supposea salesmanneedsto visit a set of cities Ao,A1,… ,An - 1. Foranyセ orderedpair of cities (Ai ,A j ), thereis a cost c(Ai ,A j ) of travelingfrom thefirst to the ウ ・ 」 ッ 、 city. Weneedto designalow costtour for thesalesman.

a エ ッ ゥ ウ asequenceof cities (Bo,B 1 ,. . . b 「 b iエ 」。イ エ。イ エ。 エ。

」ゥエケ。ョ ャ、 the ウ 。 ャ ・ ウ ュ ャ 。 ョ ャ 」 。 ョ カ ゥ ウ ゥ エ エ thecities in anyorder.All thecitiesmustappearin the ウ ウ ・ ア オ ・ ョ 」 ・ (Bo,B 1 ,… ,Bn - 1). (Note that this impliesthatall thecitiesin this subsequencearedistinct.)

The costof the tour is the sumof the costsof the ウ オ 」 」 ・ ウ ウ ゥ カ ・ pairs(Bi ,B i +1 ュッ、 ゥ ッ エッMQN

Determiningtheminimumcosttour is aclassicNP-completeproblemandthe problemremainshardevenif we just askfor a tour whosecostis within a given ュ オ ャ エ i ・ IvI of theminimumcosttour. Howeverthereis aspecialcasefor which this problem」 。 「 ・ efficiently solved.

Problem6.2: Supposeall thecitiesarelocatedin someEuclideanspaceandthe costof travelingfrom onecity to anotheris a constantmultipleof the distancebetweenthe cities. Give an efficient procedurefor com-putinga tourwhosecost ゥ ウ ャ 。 イ ャ エ ・ ・ 、 to bewithin a factorof two of thecostof anoptimumtour.

58If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

6.10 PRIMALITY CHECKING

6.9 NEARESTPOINTSIN THE PLANE

Problem6.7: Given 。 。 イ 「 ゥ エ イ 。 イ ケ positive ゥ ョ エ ・ ァ ・ イ ィ ッ キ would you de-termineif it canbeexpressedasa sumof two cubes?

616.10. PRIMALITYCHECKING

In an interview context, if you are asked エ ッ ー ャ ・ ュ ・ エ primalitychecking, you are just expectedto providesome ウ ャ ー ャ ・ improvementsoverthebasicbrute-forceapproach.

Problem6.10: Implementa function which takesa ョオュ「・イ ャ、 re-turnswhetherthenumberis prime or not. What is the runtimeof youralgorithm?

CHAPTER6. intracl bility

Primality checkinghas receiveda greatdeal of attentionfrom mathe-maticiansandtheoreticalcomputerscientistsandtherearea numberofhighly sophisticatedapproachesto efficiently solvingthis problem.Onereasonfor this is thatnumbertheoryplaysa key role in cryptography.

Thebrute-forceapproachto checkingif n is a primeis to divide n byeverysmallernumber. The sizeof input hereis thenumberof bits inandhencethebrute-forcealgorithmhasexponentialtime complexity.

Insteadof havingsingleintegersin the array, if you haveintegralpointsin a two-dimensionalplane, the problem of finding a closestpair ofpoints becomessignificantly more difficult. There are fast exactalgo-rithms for this problembut they are tricky to analyzeand ゥ ュ ー ャ ・ ュ ・ エ

Canyou designa heuristicfor identifying theclosestpair of points?

Problem 6.9: You are given a list of pairs of points in the two-dimensionalCartesianplane. Each point has integer x and y co-ordinates.How wouldyoufind thetwo closestpoints?

60

6.8 COLLATZ CONJECTURE

LotharCollatzproposedthis remarkable」 ッ ・ 」 エ オ イ ・ in 1937: "Define C :{I ,2,3,…,} 1----+ {I ,2,3,…,} as follows: if n is even, C HI ]OR elsec H I ] S Q N Thenfor anychoiceof cRH I = I , for somei".

For example, if we startwith thenumberQ Q ャ 、 iterativelycomputeC2(11), we getthesequence11,34,17,52,26,13,40,20,10,5,16,8,4,2,1.

Despiteintenseefforts, theCollatz 」 ッ ・ 」 エ ・ hasnotbeenprovedordisproved.

Supposeyou aregiven the taskof proving or disprovingtheCollatz」 ッ ェ ・ 」 エ オ イ ・ for the first billion integers. A direct approachwould be tocomputetheconvergencesequencefor eachnumberin this set.

Problem6.8: How would you provethatCollatzhypothesisworks forat leastthefirst N ゥョエ・ァ・イウ_ tィ 。エ is theruntimeof your algorithm?

The following problemshavethe propertythat they can, in princi-pIe, bothbe solvedin polynomialtime. Howeverthe polynomialtimesolutionsare not straightforwardand in the contextof an interview, aheuristicsolutionmaybepreferable.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

6.10 PRIMALITY CHECKING

6.9 NEARESTPOINTSIN THE PLANE

Problem6.7: Given 。 。 イ 「 ゥ エ イ 。 イ ケ positive ゥ ョ エ ・ ァ ・ イ ィ ッ キ would you de-termineif it canbeexpressedasa sumof two cubes?

616.10. PRIMALITYCHECKING

In an interview context, if you are asked エ ッ ー ャ ・ ュ ・ エ primalitychecking, you are just expectedto providesome ウ ャ ー ャ ・ improvementsoverthebasicbrute-forceapproach.

Problem6.10: Implementa function which takesa ョオュ「・イ ャ、 re-turnswhetherthenumberis prime or not. What is the runtimeof youralgorithm?

CHAPTER6. intracl bility

Primality checkinghas receiveda greatdeal of attentionfrom mathe-maticiansandtheoreticalcomputerscientistsandtherearea numberofhighly sophisticatedapproachesto efficiently solvingthis problem.Onereasonfor this is thatnumbertheoryplaysa key role in cryptography.

Thebrute-forceapproachto checkingif n is a primeis to divide n byeverysmallernumber. The sizeof input hereis thenumberof bits inandhencethebrute-forcealgorithmhasexponentialtime complexity.

Insteadof havingsingleintegersin the array, if you haveintegralpointsin a two-dimensionalplane, the problem of finding a closestpair ofpoints becomessignificantly more difficult. There are fast exactalgo-rithms for this problembut they are tricky to analyzeand ゥ ュ ー ャ ・ ュ ・ エ

Canyou designa heuristicfor identifying theclosestpair of points?

Problem 6.9: You are given a list of pairs of points in the two-dimensionalCartesianplane. Each point has integer x and y co-ordinates.How wouldyoufind thetwo closestpoints?

60

6.8 COLLATZ CONJECTURE

LotharCollatzproposedthis remarkable」 ッ ・ 」 エ オ イ ・ in 1937: "Define C :{I ,2,3,…,} 1----+ {I ,2,3,…,} as follows: if n is even, C HI ]OR elsec H I ] S Q N Thenfor anychoiceof cRH I = I , for somei".

For example, if we startwith thenumberQ Q ャ 、 iterativelycomputeC2(11), we getthesequence11,34,17,52,26,13,40,20,10,5,16,8,4,2,1.

Despiteintenseefforts, theCollatz 」 ッ ・ 」 エ ・ hasnotbeenprovedordisproved.

Supposeyou aregiven the taskof proving or disprovingtheCollatz」 ッ ェ ・ 」 エ オ イ ・ for the first billion integers. A direct approachwould be tocomputetheconvergencesequencefor eachnumberin this set.

Problem6.8: How would you provethatCollatzhypothesisworks forat leastthefirst N ゥョエ・ァ・イウ_ tィ 。エ is theruntimeof your algorithm?

The following problemshavethe propertythat they can, in princi-pIe, bothbe solvedin polynomialtime. Howeverthe polynomialtimesolutionsare not straightforwardand in the contextof an interview, aheuristicsolutionmaybepreferable.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

7.1 SERVLET WITH CACHING

Problem7.1: Designa servletwhich implementsanonlinespellcorrec-エ ゥ ッ ウ オ ァ ァ ・ ウ エ ・ イ N Specifically, it takesasinput a string s andcomputesanarrayof entriesin its dictionarywhich areclosestto the stringusingtheedit distancespecifiedin Problem5.8.

Sincecomputingtheeditdistancess to eachentryin thedictionaryistime consuming, you shouldimplementa cachingstrategy.Specifically,cachethemost イ ・ 」 ・ ャ ケ computedresult.

Parallelismcanalsobeusedfor fault tolerance-forexample, if a ma-chinefails in a clusterthatis servingwebpages, theotherscantakeover.

Concreteapplicationsof parallelcomputingincludegraphicuserin-terfaces(a dedicatedthreadhandlesVI actionsresultingin increasedre-sponsiveness), Javavirtual machines(a separatethreadhandlesgarbagecollectionwhich would otherwiseleadto blocking), web servers(a sin-gle logical threadhandlesa singleclient request), scientificcomputing(alargematrix ュ オ ャ エ ゥ ー ャ ゥ 」 。 エ ゥ 」 。 ョ besplit acrossa cluster), andweb search(multiple machinescrawl, index, andretrievewebpages).

Therearetwo primarymodelsfor parallelcomputation-thesharedmemorymodel, in whicheachprocessorcanaccessanylocationin mem-ッ イ ケ ャ 、 the distributedmemorymodel, in which a processormustex-plicitly senda messageto anotherprocessorto accessits memory. Theformeris moreappropriatein themulticoresettingandthelatteris moreaccuratefor a cluster.Thequestionsin this chaptertargeta sharedmem-ory model. We coversomeproblemsrelatedto the distributedmemorymodelsuchasleaderelectionandhostdiscoveryaswell asapplicationssuchaswebsearchin Chapter8.

Writing correctparallelprogramsis challengingbecauseof thesubtleinteractionsbetweenparallelcomponents.Oneof the key challengesisraces-two」 ッ ョ 」 オ イ イ ・ ゥ ョ ウ エ イ オ 」 エ ゥ ッ ョ sequencesaccessthesameaddressinmemoryand at least one of them writes to that address. Other chal-lengesto correctnessare starvation(a processorneedsa resourcebutnevergetsit/ e.g., Problem7.5)/ deadlock(A andB acquireresourcesMandN respectivelyand then try to acquireNandM respectively, e.g.,Problem7.10)/andlivelock (aprocessorkeepsretryinganoperationthatalwaysfails). Bugscausedby theseissuesarevery difficult to find usingtesting;debuggingthemis alsoverydifficult becausetheymaynotbere-produciblesincetheyareloaddependent. It is alsooftentruethatit is notpossibleto realizethe performanceimplied by parallelism-sometimesa critical taskcannotbe parallelized, making it ゥ イ ー ッ ウ ウ ゥ 「 ャ ・ to improveperformance, regardlessof the numberof processorsadded. s ャ ゥ ャ 。 イ ャ ケ

theoverheadof 」 ッ ュ ュ オ ゥ 」 。 エ ゥ ョ ァ intermediateresultsbetweenprocessorscanexceedtheperformancebenefits.

637.1. SERVLETWITHCACHING

Theactivity of acomputermustincludetheproperイ ・ 。 」 エ ゥ ァ to apossiblygreatvarietyofmessagesthatcanbe ウ・ エッ it atunpredictablemoments, asituationwhichoccursin processcontrol, traffic control, stockcontrol, bankingapplications, automizationof informationflowin largeorganizations, centralizedcomputerserviceand, finally, all informationsystemsinwhichanumberof computersarecoupledtoeachother.

"Cooperatingウ・アオ・ ゥ。ャ processes," E. d ォウエイ。

1965

Parallelcomputationhasbecomeincreasinglycommon. For ・ ク ャ ー ャ ・

laptopsanddesktopscomewith multicoreprocessorsh whicheachcoreis a completeprocessormd accessesshamdmemory.High-mdcomprtationis oftenperformedusingclustersconsistingof individualcomput-erscommmidtingthrougha mtvmrk Parallelismprovidesa numberof benefits:

_ High performance-moreprocessorsworking on a task (usually)meansit is completedfaster.

_ Betteruseof resources-aprogramcanexecutewhile anotherwaitson thedisk or network.

_ Fairness-lettingdifferent users or programs share a machineratherthanhaveoneprogramrun at a time to completion.

_ Convenience-itis oftenconceptuallymorestraightforwardto ac-complistIataskushgasetof concurreIItprogramsfor thesubtasksratherthanhavea singleprogrammanageall thesubtasks.

ParallelCom.puting

Chapter7

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

7.1 SERVLET WITH CACHING

Problem7.1: Designa servletwhich implementsanonlinespellcorrec-エ ゥ ッ ウ オ ァ ァ ・ ウ エ ・ イ N Specifically, it takesasinput a string s andcomputesanarrayof entriesin its dictionarywhich areclosestto the stringusingtheedit distancespecifiedin Problem5.8.

Sincecomputingtheeditdistancess to eachentryin thedictionaryistime consuming, you shouldimplementa cachingstrategy.Specifically,cachethemost イ ・ 」 ・ ャ ケ computedresult.

Parallelismcanalsobeusedfor fault tolerance-forexample, if a ma-chinefails in a clusterthatis servingwebpages, theotherscantakeover.

Concreteapplicationsof parallelcomputingincludegraphicuserin-terfaces(a dedicatedthreadhandlesVI actionsresultingin increasedre-sponsiveness), Javavirtual machines(a separatethreadhandlesgarbagecollectionwhich would otherwiseleadto blocking), web servers(a sin-gle logical threadhandlesa singleclient request), scientificcomputing(alargematrix ュ オ ャ エ ゥ ー ャ ゥ 」 。 エ ゥ 」 。 ョ besplit acrossa cluster), andweb search(multiple machinescrawl, index, andretrievewebpages).

Therearetwo primarymodelsfor parallelcomputation-thesharedmemorymodel, in whicheachprocessorcanaccessanylocationin mem-ッ イ ケ ャ 、 the distributedmemorymodel, in which a processormustex-plicitly senda messageto anotherprocessorto accessits memory. Theformeris moreappropriatein themulticoresettingandthelatteris moreaccuratefor a cluster.Thequestionsin this chaptertargeta sharedmem-ory model. We coversomeproblemsrelatedto the distributedmemorymodelsuchasleaderelectionandhostdiscoveryaswell asapplicationssuchaswebsearchin Chapter8.

Writing correctparallelprogramsis challengingbecauseof thesubtleinteractionsbetweenparallelcomponents.Oneof the key challengesisraces-two」 ッ ョ 」 オ イ イ ・ ゥ ョ ウ エ イ オ 」 エ ゥ ッ ョ sequencesaccessthesameaddressinmemoryand at least one of them writes to that address. Other chal-lengesto correctnessare starvation(a processorneedsa resourcebutnevergetsit/ e.g., Problem7.5)/ deadlock(A andB acquireresourcesMandN respectivelyand then try to acquireNandM respectively, e.g.,Problem7.10)/andlivelock (aprocessorkeepsretryinganoperationthatalwaysfails). Bugscausedby theseissuesarevery difficult to find usingtesting;debuggingthemis alsoverydifficult becausetheymaynotbere-produciblesincetheyareloaddependent. It is alsooftentruethatit is notpossibleto realizethe performanceimplied by parallelism-sometimesa critical taskcannotbe parallelized, making it ゥ イ ー ッ ウ ウ ゥ 「 ャ ・ to improveperformance, regardlessof the numberof processorsadded. s ャ ゥ ャ 。 イ ャ ケ

theoverheadof 」 ッ ュ ュ オ ゥ 」 。 エ ゥ ョ ァ intermediateresultsbetweenprocessorscanexceedtheperformancebenefits.

637.1. SERVLETWITHCACHING

Theactivity of acomputermustincludetheproperイ ・ 。 」 エ ゥ ァ to apossiblygreatvarietyofmessagesthatcanbe ウ・ エッ it atunpredictablemoments, asituationwhichoccursin processcontrol, traffic control, stockcontrol, bankingapplications, automizationof informationflowin largeorganizations, centralizedcomputerserviceand, finally, all informationsystemsinwhichanumberof computersarecoupledtoeachother.

"Cooperatingウ・アオ・ ゥ。ャ processes," E. d ォウエイ。

1965

Parallelcomputationhasbecomeincreasinglycommon. For ・ ク ャ ー ャ ・

laptopsanddesktopscomewith multicoreprocessorsh whicheachcoreis a completeprocessormd accessesshamdmemory.High-mdcomprtationis oftenperformedusingclustersconsistingof individualcomput-erscommmidtingthrougha mtvmrk Parallelismprovidesa numberof benefits:

_ High performance-moreprocessorsworking on a task (usually)meansit is completedfaster.

_ Betteruseof resources-aprogramcanexecutewhile anotherwaitson thedisk or network.

_ Fairness-lettingdifferent users or programs share a machineratherthanhaveoneprogramrun at a time to completion.

_ Convenience-itis oftenconceptuallymorestraightforwardto ac-complistIataskushgasetof concurreIItprogramsfor thesubtasksratherthanhavea singleprogrammanageall thesubtasks.

ParallelCom.puting

Chapter7

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

7.4 TIMER

ャ ・ following class, SimpleWebServer, implementspart of a simpleHTTP server:

Problem7.2: Supposeyoufind thatSimpleWebServerhaspoorperfor-mancebecauseーイ ッ 」 ・ ウ ウ ・ ア frequentlyblockson 10. What stepscouldyou taketo improveSimpleWebServer'sperformance?

65

7.7 READERS-WRITERSWITH FAIRNESS

The specificationsto bothProblems7.5 and7.6 canleadto starvation-thefirst maystarveキ イ ゥ エ ・ イ ウ ャ 、 thesecondmaystarvereaders.Thethird

7.5 READERS-WRITERS

Consideranobjects which is readfrom andwritten to by manythreads.(Forexample, s couldbethecachefrom Problem7.1.) Youneedto ensurethatno threadmayaccesss for readingor writing while anotherthreadis writing to s. (Two or morereadersmayaccesss at thesametime.)

Oneway to achievethis is by protectings with a mutexthatensuresthatno threadcanaccesss at thesametime asanotherwriter. Howeverthis solution is suboptimalbecauseit is possiblethat a readerRl haslocked s and anotherreaderR2 wantsto accessウN ャ・イ・ is no needtomakeR2 wait until Rl is donereading;instead, R2 shouldstartreadingright away.

This motivatesthe first readers-writersproblem: protects with theaddedconstraintthat no readeris to be kept waiting if s is currentlyopenedfor reading.

Problem 7.5: Implement a synchronizationmechanismfor the firstreaders-writersproblem.

7.6 readersMwriters tith WRITE PREFERENCE

Supposewe havean objects as in Problem W i ョ the solutionto Prob-lem 7.5, a readerRl mayhavethe lock; if a writer W is waiting for thelock andthena readerR2 requestsaccess, R2 will begivenpriority overW. If this happensoften enough, W will starve. Instead, supposewewantW to startassoonaspossible.

This motivatesthe secondreaders-writersproblem: protect s with"writer-preference", ゥN ・N ッ キイゥエ・ ッョ」・ addedto thequeue, is to bekeptwaiting longerthanabsolutelynecessary.

Problem7.6: Implementa synchronizationmechanismfor the secondreaders-writersproblem.

Problem7.4: Developa Timer classthatmanagesthe executionof de-ferredtasks.Specifically, at creation, theconstructorof Timer is passedan object which includesa Run methodand a namefield (which is astring). The Timer classmustsupport-(l.)startinga threadat a giventime in the future; the threadis identifiedby nameand(2.) cancelingathreadwith a givenname(you canignore the requestif the threadhasalreadystarted).

7.5. READERS-WRITERSCHAPTER 7. PARALLEL COMPUTING

Consideraweb-basedcalendarin which theserverhostingthecalendarhasto performataskwhmthenextcaleI1dareveI1ttakesplace-(Thetaskcouldbesendinganemailor anSMS.)Your job is to designa facility thatmanagestheexecutionof suchtasks.

7.3 ASYNCHRONOUSCALLBACKS

It is commonin a distributedcomputingenvironmentfor the responsesto not return in the sameorder as the requestswere made. One wayto handlethis is throughan "asynchronouscallback"-amethodto beinvokedon response.

Problem7.3: ImplementaRequestorclass.Theclasshasto ゥ ュ ー ャ ・ ュ ・ エ

a Dispatchmethodwhich takesa Requestorobject. The Requestorob;ect includes a request string, a ProcessResponse(stringイ ウ ー ッ ュ ・ ュ ・ エ ィ ッ 、 ケ and m Executemethodthat takesa string mdreturnsa string.

Dispatch is to create a new thread which ゥ ョ t ッ ォ ・ ウ Executeon request. When Execute returns, Dispatch ゥ ョ t ッ ォ ・ ウ theProcessResponsemethodon theresponse.

The Executemethodmaytakem hdetermhateam01mtof time toreturn;it mayneverreturn. You needto havea エゥュ・ ッオエ mechanismforthis: assumetheRequestorobjectshaveanErrormethodthatyoucaninvoke.

1 \ public class SimpleWebServer{2 I - fi n a1 s t ati c in t PORT = 8080;3 I public s t ati c void main (String [] args) throws IOException

4 I Se• Socket s……k = new s イ s ッ H p o r t I ;5 I for (;;) {6 I Socket sock = serversock.accept() ;7 I ProcessReq(sock);89

10

7.2 THREAD POOLS

64

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

7.4 TIMER

ャ ・ following class, SimpleWebServer, implementspart of a simpleHTTP server:

Problem7.2: Supposeyoufind thatSimpleWebServerhaspoorperfor-mancebecauseーイ ッ 」 ・ ウ ウ ・ ア frequentlyblockson 10. What stepscouldyou taketo improveSimpleWebServer'sperformance?

65

7.7 READERS-WRITERSWITH FAIRNESS

The specificationsto bothProblems7.5 and7.6 canleadto starvation-thefirst maystarveキ イ ゥ エ ・ イ ウ ャ 、 thesecondmaystarvereaders.Thethird

7.5 READERS-WRITERS

Consideranobjects which is readfrom andwritten to by manythreads.(Forexample, s couldbethecachefrom Problem7.1.) Youneedto ensurethatno threadmayaccesss for readingor writing while anotherthreadis writing to s. (Two or morereadersmayaccesss at thesametime.)

Oneway to achievethis is by protectings with a mutexthatensuresthatno threadcanaccesss at thesametime asanotherwriter. Howeverthis solution is suboptimalbecauseit is possiblethat a readerRl haslocked s and anotherreaderR2 wantsto accessウN ャ・イ・ is no needtomakeR2 wait until Rl is donereading;instead, R2 shouldstartreadingright away.

This motivatesthe first readers-writersproblem: protects with theaddedconstraintthat no readeris to be kept waiting if s is currentlyopenedfor reading.

Problem 7.5: Implement a synchronizationmechanismfor the firstreaders-writersproblem.

7.6 readersMwriters tith WRITE PREFERENCE

Supposewe havean objects as in Problem W i ョ the solutionto Prob-lem 7.5, a readerRl mayhavethe lock; if a writer W is waiting for thelock andthena readerR2 requestsaccess, R2 will begivenpriority overW. If this happensoften enough, W will starve. Instead, supposewewantW to startassoonaspossible.

This motivatesthe secondreaders-writersproblem: protect s with"writer-preference", ゥN ・N ッ キイゥエ・ ッョ」・ addedto thequeue, is to bekeptwaiting longerthanabsolutelynecessary.

Problem7.6: Implementa synchronizationmechanismfor the secondreaders-writersproblem.

Problem7.4: Developa Timer classthatmanagesthe executionof de-ferredtasks.Specifically, at creation, theconstructorof Timer is passedan object which includesa Run methodand a namefield (which is astring). The Timer classmustsupport-(l.)startinga threadat a giventime in the future; the threadis identifiedby nameand(2.) cancelingathreadwith a givenname(you canignore the requestif the threadhasalreadystarted).

7.5. READERS-WRITERSCHAPTER 7. PARALLEL COMPUTING

Consideraweb-basedcalendarin which theserverhostingthecalendarhasto performataskwhmthenextcaleI1dareveI1ttakesplace-(Thetaskcouldbesendinganemailor anSMS.)Your job is to designa facility thatmanagestheexecutionof suchtasks.

7.3 ASYNCHRONOUSCALLBACKS

It is commonin a distributedcomputingenvironmentfor the responsesto not return in the sameorder as the requestswere made. One wayto handlethis is throughan "asynchronouscallback"-amethodto beinvokedon response.

Problem7.3: ImplementaRequestorclass.Theclasshasto ゥ ュ ー ャ ・ ュ ・ エ

a Dispatchmethodwhich takesa Requestorobject. The Requestorob;ect includes a request string, a ProcessResponse(stringイ ウ ー ッ ュ ・ ュ ・ エ ィ ッ 、 ケ and m Executemethodthat takesa string mdreturnsa string.

Dispatch is to create a new thread which ゥ ョ t ッ ォ ・ ウ Executeon request. When Execute returns, Dispatch ゥ ョ t ッ ォ ・ ウ theProcessResponsemethodon theresponse.

The Executemethodmaytakem hdetermhateam01mtof time toreturn;it mayneverreturn. You needto havea エゥュ・ ッオエ mechanismforthis: assumetheRequestorobjectshaveanErrormethodthatyoucaninvoke.

1 \ public class SimpleWebServer{2 I - fi n a1 s t ati c in t PORT = 8080;3 I public s t ati c void main (String [] args) throws IOException

4 I Se• Socket s……k = new s イ s ッ H p o r t I ;5 I for (;;) {6 I Socket sock = serversock.accept() ;7 I ProcessReq(sock);89

10

7.2 THREAD POOLS

64

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

7.10 DINING PHILOSOPHERS

7.8 PRODUCER-CONSUMERQUEUE

Two threads, the producerP andthe consumerQ, sharea fixed lengtharrayof stringsA. Theproducergeneratesstringsoneat a time which itwritesinto A; theconsumerremovesstringsfrom A, oneat a time.

Problem7.8: Designa synchronizationmechanismfor A whichensuresthat P doesnot attemptto adda string into the arrayif it is ヲオャャ ャ、 Cdoesnot try to removedatafrom anemptybuffer.

readers-writersproblemaddsthe constraintthat no threadshallbe al-lowed to starve-theoperationof obtaininga lock on s always termi-natesin aboundedamountof time.

Problem7.7: iューャ・ュ・ エ a synchronizationmechanismfor the thirdreaders-writersproblem. It is acceptable(indeednecessary)that in thissolution, both readersandwriters haveto wait longer than absolutelynecessary.(Readersmaywait evenif s is openedfor readandwritersmaywait evenif no oneelsehasa lock on s.)

1/A Protocolfor PacketNetworkIntercommunication," V. Cerf

andR. Kahn, 1974

DesignProbletns

Chapter8

We havedescribedasimplebutverypowerfulandflexibleprotocolwhichprovidesforvariationin individualnetworkpacketsizes, transmissionfailures, sequencing, flowcontrol, andthecreationanddestructionof process-to-processassociations.

This chapteris cOI1cerzledwith systemdesigI1problems.Eachques-tion canbe a largeopen-endedsoftwareproject. During the ゥ エ イ カ ゥ ・ キ

you shouldprovidea higklevel sketchof sucha systemwith thoughtson variousdesignchoices, the tradeoffs, key algorithms, and the 、 セ エ 。 ᆳstructuresゥ ョ カ ッ ャ イ ・ 、 N

8.1 MOSAIC

Onepopularform of computerart is photomosaicswhereyou aregivenacollectionof imagescalled"tiles" N ャ ・ ァ ゥ カ ・ ョ a targetimage, youwantto build motherimagewhichdoselyapproximatesthe targetmagebutis actuallybuilt by juxtaposingthe tiles. Herethe ア オ 。 ャ y of approxima-tion is mostlydefinedby humanperception.It is oftenthe 」 。 ウ セ thatwitha givensetof tiles, a usermaywantto build severalmosaics.

Problem8.1: How would you 、 ・ ウ ゥ 。 softwarethat produceshighqualitymosaicswith minimal computeエ ョ ・ _

CHAPTER7. PARALLELCOMPV{TING

7.9 BARBER SHOP

Considera barbershopwith a singlebarberB , onebarber 」 ィ 。 ゥ 。 ョ 、

」 ィ 。 ゥ イ ウ for customerswho are waiting for their turn for a haircut. Ifthere are no customers, the barbersleepsin his chair. On entering, acustomereitherawakensthebarberor if the barberis cutting someoneelse'shair, he sits down in one of the chairsfor waiting customers.Ifall of the waiting chairsare taken, the newly arrived customersimplyleaves.

Problem7.8: Assumethereis athreadfor eachcustomerandfor thebar-ber. Model the systemusingsemaphoresandmutexesto ensurecorrectbehavior.

In the dining philosophersproblemn threads, numbered0 to M I , runconcurrently. Thereare n resources, numbered0 エッM 1. Threadi re-quires resourcesi and i + 1 ュッ、「・ヲッイ・ it can ゥ ョ t ッ ォ ・ a methodm.(Theproblemgetsits namebecauseit modelsー ィ ゥ ャ ッ ウ ッ ー ィ ・ イ ウ sitting at aroundtable, alternatingbetweenthinking, eating, 。ョ、 nB 。ゥエゥョァN Thereisa singlechopstickbetweeneachpair of philosophers.To eat, a philoso-phermusthold two chopsticks-oneplacedimmediatelyto his left andoneimmediatelyto his right.)

Problem7.10: i ュ ー ャ ・ ュ ・ エ a synchronizationmechanismfor the diningphilosophersproblem.

66

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

7.10 DINING PHILOSOPHERS

7.8 PRODUCER-CONSUMERQUEUE

Two threads, the producerP andthe consumerQ, sharea fixed lengtharrayof stringsA. Theproducergeneratesstringsoneat a time which itwritesinto A; theconsumerremovesstringsfrom A, oneat a time.

Problem7.8: Designa synchronizationmechanismfor A whichensuresthat P doesnot attemptto adda string into the arrayif it is ヲオャャ ャ、 Cdoesnot try to removedatafrom anemptybuffer.

readers-writersproblemaddsthe constraintthat no threadshallbe al-lowed to starve-theoperationof obtaininga lock on s always termi-natesin aboundedamountof time.

Problem7.7: iューャ・ュ・ エ a synchronizationmechanismfor the thirdreaders-writersproblem. It is acceptable(indeednecessary)that in thissolution, both readersandwriters haveto wait longer than absolutelynecessary.(Readersmaywait evenif s is openedfor readandwritersmaywait evenif no oneelsehasa lock on s.)

1/A Protocolfor PacketNetworkIntercommunication," V. Cerf

andR. Kahn, 1974

DesignProbletns

Chapter8

We havedescribedasimplebutverypowerfulandflexibleprotocolwhichprovidesforvariationin individualnetworkpacketsizes, transmissionfailures, sequencing, flowcontrol, andthecreationanddestructionof process-to-processassociations.

This chapteris cOI1cerzledwith systemdesigI1problems.Eachques-tion canbe a largeopen-endedsoftwareproject. During the ゥ エ イ カ ゥ ・ キ

you shouldprovidea higklevel sketchof sucha systemwith thoughtson variousdesignchoices, the tradeoffs, key algorithms, and the 、 セ エ 。 ᆳstructuresゥ ョ カ ッ ャ イ ・ 、 N

8.1 MOSAIC

Onepopularform of computerart is photomosaicswhereyou aregivenacollectionof imagescalled"tiles" N ャ ・ ァ ゥ カ ・ ョ a targetimage, youwantto build motherimagewhichdoselyapproximatesthe targetmagebutis actuallybuilt by juxtaposingthe tiles. Herethe ア オ 。 ャ y of approxima-tion is mostlydefinedby humanperception.It is oftenthe 」 。 ウ セ thatwitha givensetof tiles, a usermaywantto build severalmosaics.

Problem8.1: How would you 、 ・ ウ ゥ 。 softwarethat produceshighqualitymosaicswith minimal computeエ ョ ・ _

CHAPTER7. PARALLELCOMPV{TING

7.9 BARBER SHOP

Considera barbershopwith a singlebarberB , onebarber 」 ィ 。 ゥ 。 ョ 、

」 ィ 。 ゥ イ ウ for customerswho are waiting for their turn for a haircut. Ifthere are no customers, the barbersleepsin his chair. On entering, acustomereitherawakensthebarberor if the barberis cutting someoneelse'shair, he sits down in one of the chairsfor waiting customers.Ifall of the waiting chairsare taken, the newly arrived customersimplyleaves.

Problem7.8: Assumethereis athreadfor eachcustomerandfor thebar-ber. Model the systemusingsemaphoresandmutexesto ensurecorrectbehavior.

In the dining philosophersproblemn threads, numbered0 to M I , runconcurrently. Thereare n resources, numbered0 エッM 1. Threadi re-quires resourcesi and i + 1 ュッ、「・ヲッイ・ it can ゥ ョ t ッ ォ ・ a methodm.(Theproblemgetsits namebecauseit modelsー ィ ゥ ャ ッ ウ ッ ー ィ ・ イ ウ sitting at aroundtable, alternatingbetweenthinking, eating, 。ョ、 nB 。ゥエゥョァN Thereisa singlechopstickbetweeneachpair of philosophers.To eat, a philoso-phermusthold two chopsticks-oneplacedimmediatelyto his left andoneimmediatelyto his right.)

Problem7.10: i ュ ー ャ ・ ュ ・ エ a synchronizationmechanismfor the diningphilosophersproblem.

66

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

8.4 SPELL CHECKER

Designinga good spelling correctionsystemcan be challenging. Wediscussedspelling correctionin the contextof the edit distance(Prob-lem 5.8). Howeverin that problem, we just consideredthe problemofcomputingthe edit distancebetweena pair of strings. A spell checkermust find a set of words that are closestto a given word from the en-tire dictionary. Furthermore, edit distancemaynotbe the right distancefunction whenperformingspellingcorrection-itdoesnot takeinto ac-」ッオ エィ・ commonlymisspelledwords or the proximity of letterson akeyboard.

Problem8.4: How wouldyoubuild a spellingcorrectionsystem?

8.2 SEARCHENGINE

Modernkeyword-basedsearchenginesmaintaina collectionof severalbillion documents.Oneof the key computationsperformedby a searchengineis to retrieveall the documentsthat containthe keywordscon-tainedin a givenquery.This is a nontrivial taskbecauseit mustbedonewithin few tensof milliseconds.

In this problem, we considera smallerversionof theproblemwherethecollectionof documentscanfit within theRAM of a singlecomputer.

Problem8.2: Givenamillion documentswith anaveragesizeof 10kilo-bytes, 、・ウゥ 。 programthat canefficiently return the subsetof docu-mentscontaininga givensetof words.

698.5. STEMMING

8.6 DISTRIBUTED THROTTLING

LetFssayyou haveN machinescrawliI1g theworld wide websuchthatthempomibilityforagmnURL!sュ ウ ゥ ・ 、 to thecrawlerwith id equaltoHash(URL)modlv.Downloadmgapagetakes。 キ 。 ケ 「 。 、 キ ゥ 、 エ ィ ヲ イ ッ ュk semrhosthgit.TEmforeyou wmt to mmthat h my givenmmuteryourcrawler-sReverrequestmorethmB bytesfrom my host.

Problem8.6: How would you ゥ ュ ー ャ ・ ュ ・ 」 イ 。 キ ャ ゥ ョ ァ undersucha con-straint?

8.5 STEMMING

WEeRausersubmitsthe ア オ B 」 ッ ュ ー オ エ 。 エ ゥ ッ ョ B to a searchengine, it ist i エ ・ ー ッ ウ ・ ォ ュ ゥ ァ ィ エ 「 ・ ゥ ョ エ ・ イ ・ ウ エ ・ 、 、 ッ 」 オ ュ ・ ョ エ ウ 」 ッ ョ エ 。 ゥ ョ ゥ ァ エ ィ ・ キ ッ イ 、

エ ・ イ ウ G B 」 ッ ュ ー オ エ ・ B and B 」 ッ ュ ー オ エ ゥ ョ ァ B 。 i ウ ッ N If you have severalォ ・ ュ イ 、 ウ m a query}1tbecomesdifficult to searchfor a11combimtionsof all variantsof thewordsh thequery-

One way to solve this problemis to reduceall variantsof a Q"ivpn

word エ ッ ・ common イ ッ ッ 「 ッ エ ィ h the query str-hg md h エィ[[ Zre?s ュ ヲ ー イ ッ 」 ・ ウ ウ ゥ ウ 」 、 stemming.An exampleof stemmingキ ャ 、

セ t computers,computer,compute} •• comput. It is almostimpossibletosuedmtlycaptur?allーッ 「 ャ ・ variantsof all words in the e ァ ャ ゥ ウ ィ 1guagebuta few SImpler111escaagetusa majority of thecases.

Problem8.5: Designa stemmingalgorithm that runs fast and doesaイ・。ウッ 。「ャ・ job.

8.7 IMPLEMENT PAGERANK

PageRankalgorithmassignsa イ。 エッ web pagesbasedon the number

Z Z Z Q [ [1.BuildamatrixAbasedo エ ィ ・ ィ ケ ー ・ イ ャ ゥ ウ エ イ オ 」 エ オ イ ・ of thewebwitha ゥ ェ ゥ ヲ エ ィ ・ イ ・ ゥ ウ _ Q ゥ ョ ォ ヲ ッ イ ュ 「 ー 。 ァ ・ i エッキ・「ー。ァ・ェイ。m ゥウ thetotalIlumberof unIqmoutgoinglhks from paget.

2. Solvefor X ウ。エゥウ tゥョ

x] N [1] + (1 - E)AT . X.

Here ゥ ウ a scalarconstantH ・ N ァ N I and[1] representsa columntor of Is. ThevalueX[i] is the イ セ ッ ヲ the ゥ M ー 。 ァ ・ N

The most 」 ッ ュ ュ ッ ャ ケ usedapproachto sokhgthe aboveequatiOI1isto startwith a value of Xy whereeachcompomntisi(where ゥ ウ thenumberof pages)aRdthenperformthefollowiI1g iteration:

x ォ ] N [1] + (1 - E)AT . xォ Qᄋ

CHAPTER8. DESIGNPROBLEMS68

8.3 IP FORWARDING

Therearemanyapplicationswhereinsteadof anexactmatchof strings,we arelooking for a prefix match, i.e., givena setof stringsanda searchstring, we wantto find a stringfrom the setthat is a prefix of the searchstring. Oneapplicationof this is InternetProtocol(IP) routelookup ー イ ッ 「ャ ・ ュ N ャ ・ Q IP packetarrivesat a router, the routerlooks up thenexthop for thepacketby searchingthe destinationIP addressof thepacketin its routing table. The routing table is specifiedas a setof prefixes Pthe IP addressandthe routeris supposedto identify the longestmatch-ing prefix. If this taskis to beperformedonly once, it is impossibleto dobetterthantestingeachprefix. Howeveran Internetcore routerneedsto lookup millions of destinationaddressesッ ョ ャ ・ setof prefixeseverysecond.Henceit canbeadvantageousto do someprecomputation.

Problem8.3: You are ァ ゥ カ ・ 。 large setof stringsS in advance.Givena querystringQ, how would you designa systemthat canidentify thelongeststring ー s thatis a prefix of Q?

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

8.4 SPELL CHECKER

Designinga good spelling correctionsystemcan be challenging. Wediscussedspelling correctionin the contextof the edit distance(Prob-lem 5.8). Howeverin that problem, we just consideredthe problemofcomputingthe edit distancebetweena pair of strings. A spell checkermust find a set of words that are closestto a given word from the en-tire dictionary. Furthermore, edit distancemaynotbe the right distancefunction whenperformingspellingcorrection-itdoesnot takeinto ac-」ッオ エィ・ commonlymisspelledwords or the proximity of letterson akeyboard.

Problem8.4: How wouldyoubuild a spellingcorrectionsystem?

8.2 SEARCHENGINE

Modernkeyword-basedsearchenginesmaintaina collectionof severalbillion documents.Oneof the key computationsperformedby a searchengineis to retrieveall the documentsthat containthe keywordscon-tainedin a givenquery.This is a nontrivial taskbecauseit mustbedonewithin few tensof milliseconds.

In this problem, we considera smallerversionof theproblemwherethecollectionof documentscanfit within theRAM of a singlecomputer.

Problem8.2: Givenamillion documentswith anaveragesizeof 10kilo-bytes, 、・ウゥ 。 programthat canefficiently return the subsetof docu-mentscontaininga givensetof words.

698.5. STEMMING

8.6 DISTRIBUTED THROTTLING

LetFssayyou haveN machinescrawliI1g theworld wide websuchthatthempomibilityforagmnURL!sュ ウ ゥ ・ 、 to thecrawlerwith id equaltoHash(URL)modlv.Downloadmgapagetakes。 キ 。 ケ 「 。 、 キ ゥ 、 エ ィ ヲ イ ッ ュk semrhosthgit.TEmforeyou wmt to mmthat h my givenmmuteryourcrawler-sReverrequestmorethmB bytesfrom my host.

Problem8.6: How would you ゥ ュ ー ャ ・ ュ ・ 」 イ 。 キ ャ ゥ ョ ァ undersucha con-straint?

8.5 STEMMING

WEeRausersubmitsthe ア オ B 」 ッ ュ ー オ エ 。 エ ゥ ッ ョ B to a searchengine, it ist i エ ・ ー ッ ウ ・ ォ ュ ゥ ァ ィ エ 「 ・ ゥ ョ エ ・ イ ・ ウ エ ・ 、 、 ッ 」 オ ュ ・ ョ エ ウ 」 ッ ョ エ 。 ゥ ョ ゥ ァ エ ィ ・ キ ッ イ 、

エ ・ イ ウ G B 」 ッ ュ ー オ エ ・ B and B 」 ッ ュ ー オ エ ゥ ョ ァ B 。 i ウ ッ N If you have severalォ ・ ュ イ 、 ウ m a query}1tbecomesdifficult to searchfor a11combimtionsof all variantsof thewordsh thequery-

One way to solve this problemis to reduceall variantsof a Q"ivpn

word エ ッ ・ common イ ッ ッ 「 ッ エ ィ h the query str-hg md h エィ[[ Zre?s ュ ヲ ー イ ッ 」 ・ ウ ウ ゥ ウ 」 、 stemming.An exampleof stemmingキ ャ 、

セ t computers,computer,compute} •• comput. It is almostimpossibletosuedmtlycaptur?allーッ 「 ャ ・ variantsof all words in the e ァ ャ ゥ ウ ィ 1guagebuta few SImpler111escaagetusa majority of thecases.

Problem8.5: Designa stemmingalgorithm that runs fast and doesaイ・。ウッ 。「ャ・ job.

8.7 IMPLEMENT PAGERANK

PageRankalgorithmassignsa イ。 エッ web pagesbasedon the number

Z Z Z Q [ [1.BuildamatrixAbasedo エ ィ ・ ィ ケ ー ・ イ ャ ゥ ウ エ イ オ 」 エ オ イ ・ of thewebwitha ゥ ェ ゥ ヲ エ ィ ・ イ ・ ゥ ウ _ Q ゥ ョ ォ ヲ ッ イ ュ 「 ー 。 ァ ・ i エッキ・「ー。ァ・ェイ。m ゥウ thetotalIlumberof unIqmoutgoinglhks from paget.

2. Solvefor X ウ。エゥウ tゥョ

x] N [1] + (1 - E)AT . X.

Here ゥ ウ a scalarconstantH ・ N ァ N I and[1] representsa columntor of Is. ThevalueX[i] is the イ セ ッ ヲ the ゥ M ー 。 ァ ・ N

The most 」 ッ ュ ュ ッ ャ ケ usedapproachto sokhgthe aboveequatiOI1isto startwith a value of Xy whereeachcompomntisi(where ゥ ウ thenumberof pages)aRdthenperformthefollowiI1g iteration:

x ォ ] N [1] + (1 - E)AT . xォ Qᄋ

CHAPTER8. DESIGNPROBLEMS68

8.3 IP FORWARDING

Therearemanyapplicationswhereinsteadof anexactmatchof strings,we arelooking for a prefix match, i.e., givena setof stringsanda searchstring, we wantto find a stringfrom the setthat is a prefix of the searchstring. Oneapplicationof this is InternetProtocol(IP) routelookup ー イ ッ 「ャ ・ ュ N ャ ・ Q IP packetarrivesat a router, the routerlooks up thenexthop for thepacketby searchingthe destinationIP addressof thepacketin its routing table. The routing table is specifiedas a setof prefixes Pthe IP addressandthe routeris supposedto identify the longestmatch-ing prefix. If this taskis to beperformedonly once, it is impossibleto dobetterthantestingeachprefix. Howeveran Internetcore routerneedsto lookup millions of destinationaddressesッ ョ ャ ・ setof prefixeseverysecond.Henceit canbeadvantageousto do someprecomputation.

Problem8.3: You are ァ ゥ カ ・ 。 large setof stringsS in advance.Givena querystringQ, how would you designa systemthat canidentify thelongeststring ー s thatis a prefix of Q?

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

8.8 SCALABLE PRIORITY SYSTEM

Problem8.7: How would you designa systemthat can computeエ ィ セranksfor a collectionof a billion web pagesin a reasonableamountoftime?

8.10 ONLINE ADVERTISING SYSTEM

Jingle, a searchenginestartup, キセエウ to セ ッ ョ ・ エ セ コ ・ its searchresultsbydisplayingadvertisementsalongsidesearchresults.

Problem8.10: Designanonlineadvertisingsystemfor Jingle.

71

Jingleis developinga searchfeaturefor breakingnews.New articlesarecollectedfrom avarietyof onlinenewssourcessuchasnewspapers, bul-letin boards, blogs, etc. by a singlelab machineat Jingle. Everyminute,roughly one thousandarticlesarepostedandeacharticle is a 100 kilo-bytesin size.

8.14 ISBN CACHE

The InternationalStandardBook Number(ISBN) is a uniquecommer-cial bookidentifierbasedon the 9-digit standardbooknumberingcodedevelopedby ProfessorGordonFosterfrom Trinity University, Dublin.The la-digit ISBNwasratifiedby theISOin 1974;since2007, ISBNshavecontained13 digits. Thelastdigit in a la-digit ISBN is thecheckdigit-itis thesumof thefirst 9 digits, modulo11;a 10is イ・ーイ・ウ・ ・、 by anX. For13 digit ISBNs, the lastdigit is alsoa checkdigit but is guaranteedto bebetweena and9.

Problem8.14: Implementa cachefor lookingup pricesof booksidenti-fied by their ISBN. Usetheleast-recently-usedstrategyfor cacheevictionpolicy.

Problem8.13: Designa driving directionsservicewith a webinterface.

8.13 DRIVING DIRECTIONS

8.15 DISTRIBUTING LARGE FILES

8.11. reco }| mendation SYSTEM

As a partof their charterto collect all the informationin the world andmakeit universallyaccessible, Jinglewantsto developa driving direc-tions service. Usersentera startandfinish address;driving directionsservicereturnsdirections.

8.12 ONLINE POKER

Clump e エ ・ イ ー イ ゥ ウ ・ ウ hasa largenumberof casinos.Their CEO wantstocreatea websiteby which gamblerscanplaypokeronline.

Problem8.12: Designanonlinepokerplayingservicefor ClumpEnter-prlses.

8.11 RECOMMENDATION SYSTEM

Jinglewantsto generatemorepageviews on its newssite. Oneideatheproductmanagerhas is to put in a sidebarof clickable snippetsfromarticlesthatarelikely to beof interestto thereader.

Problem8.11: Designa systemthatautomaticallygeneratesthesidebar.

CHAPTER8. DESIGNPROBLEMS

8.9 LATENCY REDUCTION

Gィ ・ Paretodistributionis definedasfollows:O G |

P[X > x] = Q H .v;) , if x > xm

- 1 if ク ク ュ ᄋ

MaiI1taiIIhg priority hadistributedsystemcanbetricky-COI1siderthecrawler-sfor asearchengimvisithgwebpagesin someprioritizedorderor eventdriven simulationin moleculardynamics. In both cases, wecouldbe dealingwith billions of entitieswith a given priority andweneedto do three things efficiently: (1.) find the highestpriority ・ ケ

(2.) insertnewentitieswith agivenpriority, and(3.) deletecertainentitiesspecifiedby a オ ゥアオ・ id.

Problem8.8: Howwouldyoudesignasystemthatcanimplementtheserequirementswhenthedatacannotfit into a singlemachine'smemory?

Here 。 ョ 、 Xm areparametersof thedistribution. It is oneof theheavy-taileddistributionsthatcommonlyoccurin variousworkloads.

Supposeyou arerunninga serviceon k serversandthatanyservicerequestcanbeprocessedby my of the servers.AgiveIIservercmpro-cessonly onerequestata time. Dependingontherequestr , aservermaytaketime t( ), where エ H I follows a p。イ ・エ ッ 、ゥ ウエ イゥ「オ Q エゥ o

pイッ「ャ・ュXNY Z You haveaservicelevel 。 ァ イ ・ ・ ュ ャ ・ ョ Q エ キ ゥ エ ィ ケ ッ オ Q イ clientswhichrequires エ ィ 。 エ 99% of the イ ・ ア オ ・ ウ エ ウ are servicedin less エ ィ 。 イ Q ッ イ ャ ・ second.How would you designthe systemto meetthis requirementwith mini-mal cost?

70

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

8.8 SCALABLE PRIORITY SYSTEM

Problem8.7: How would you designa systemthat can computeエ ィ セranksfor a collectionof a billion web pagesin a reasonableamountoftime?

8.10 ONLINE ADVERTISING SYSTEM

Jingle, a searchenginestartup, キセエウ to セ ッ ョ ・ エ セ コ ・ its searchresultsbydisplayingadvertisementsalongsidesearchresults.

Problem8.10: Designanonlineadvertisingsystemfor Jingle.

71

Jingleis developinga searchfeaturefor breakingnews.New articlesarecollectedfrom avarietyof onlinenewssourcessuchasnewspapers, bul-letin boards, blogs, etc. by a singlelab machineat Jingle. Everyminute,roughly one thousandarticlesarepostedandeacharticle is a 100 kilo-bytesin size.

8.14 ISBN CACHE

The InternationalStandardBook Number(ISBN) is a uniquecommer-cial bookidentifierbasedon the 9-digit standardbooknumberingcodedevelopedby ProfessorGordonFosterfrom Trinity University, Dublin.The la-digit ISBNwasratifiedby theISOin 1974;since2007, ISBNshavecontained13 digits. Thelastdigit in a la-digit ISBN is thecheckdigit-itis thesumof thefirst 9 digits, modulo11;a 10is イ・ーイ・ウ・ ・、 by anX. For13 digit ISBNs, the lastdigit is alsoa checkdigit but is guaranteedto bebetweena and9.

Problem8.14: Implementa cachefor lookingup pricesof booksidenti-fied by their ISBN. Usetheleast-recently-usedstrategyfor cacheevictionpolicy.

Problem8.13: Designa driving directionsservicewith a webinterface.

8.13 DRIVING DIRECTIONS

8.15 DISTRIBUTING LARGE FILES

8.11. reco }| mendation SYSTEM

As a partof their charterto collect all the informationin the world andmakeit universallyaccessible, Jinglewantsto developa driving direc-tions service. Usersentera startandfinish address;driving directionsservicereturnsdirections.

8.12 ONLINE POKER

Clump e エ ・ イ ー イ ゥ ウ ・ ウ hasa largenumberof casinos.Their CEO wantstocreatea websiteby which gamblerscanplaypokeronline.

Problem8.12: Designanonlinepokerplayingservicefor ClumpEnter-prlses.

8.11 RECOMMENDATION SYSTEM

Jinglewantsto generatemorepageviews on its newssite. Oneideatheproductmanagerhas is to put in a sidebarof clickable snippetsfromarticlesthatarelikely to beof interestto thereader.

Problem8.11: Designa systemthatautomaticallygeneratesthesidebar.

CHAPTER8. DESIGNPROBLEMS

8.9 LATENCY REDUCTION

Gィ ・ Paretodistributionis definedasfollows:O G |

P[X > x] = Q H .v;) , if x > xm

- 1 if ク ク ュ ᄋ

MaiI1taiIIhg priority hadistributedsystemcanbetricky-COI1siderthecrawler-sfor asearchengimvisithgwebpagesin someprioritizedorderor eventdriven simulationin moleculardynamics. In both cases, wecouldbe dealingwith billions of entitieswith a given priority andweneedto do three things efficiently: (1.) find the highestpriority ・ ケ

(2.) insertnewentitieswith agivenpriority, and(3.) deletecertainentitiesspecifiedby a オ ゥアオ・ id.

Problem8.8: Howwouldyoudesignasystemthatcanimplementtheserequirementswhenthedatacannotfit into a singlemachine'smemory?

Here 。 ョ 、 Xm areparametersof thedistribution. It is oneof theheavy-taileddistributionsthatcommonlyoccurin variousworkloads.

Supposeyou arerunninga serviceon k serversandthatanyservicerequestcanbeprocessedby my of the servers.AgiveIIservercmpro-cessonly onerequestata time. Dependingontherequestr , aservermaytaketime t( ), where エ H I follows a p。イ ・エ ッ 、ゥ ウエ イゥ「オ Q エゥ o

pイッ「ャ・ュXNY Z You haveaservicelevel 。 ァ イ ・ ・ ュ ャ ・ ョ Q エ キ ゥ エ ィ ケ ッ オ Q イ clientswhichrequires エ ィ 。 エ 99% of the イ ・ ア オ ・ ウ エ ウ are servicedin less エ ィ 。 イ Q ッ イ ャ ・ second.How would you designthe systemto meetthis requirementwith mini-mal cost?

70

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Jinglewould like to servethesearticlesfrom a datacenterconsistingof a thousandservers.Forperformancereasons, eachservershouldhavea copy of articlesthat wererecentlyadded. The datacenteris far awayfrom thelab machine.

Problem8.15: Suggestan efficient way of gettingthe articlesaddedinthepastfive minutesfrom thelab machineto theservers.

8.17 HOST DISCOVERY

You areto deviseaprotocolbywhicha collectionof hostsontheInternetcandiscovereachother. Hostscancommunicatewith eachotherusingTCP connections.For host A to communicatewith host B, it needstoknow B's IP address.

Eachhoststartsoff with a setof IP 。 、 、 イ ・ ウ ウ ・ ウ ャ 、 the protocolcodethatyou implementwhichwill run on a fixed port acrossall thehosts.

Problem8.17: Devise a protocol by which hostscan discoverall thehostsparticipatingin the protocol. The protocolshouldbe fast andeffi-cientlike in Problem8.16.

8.16 LEADER ELECTION

You areto devisea protocolby whicha collectionof hostsontheInternetcanelect a leader. Hostscancommunicatewith eachotherusingTCPconnections.For hostA to communicatewith hostB , it needsto knowB's IP address.Eachhost startsoff with a set of IP addressesand theprotocolcodethatyou implementthatwill run on a fixed port acrossallthehosts.

Problem8.16: Devise a protocol by which hosts can elect a uniqueleader from all the hosts participatingin the protocol. The protocolshouldbefast, in that it convergesquickly; it shouldbe efficient, in thatit shouldnot involve too manyconnections, too manydataexchanges,andtoo muchdataexchanged.

Chapter9

Thereis required, finally, theratio betweenthefluxion of anyquantityx youwill andthefluxion of its powerx n

. Let xflow till it becomesx ッ。ョ、

resolvethepower H P I エ ッ

theinfinite seriesク ョ ック ョ M Q セHョRMョIPRクョ RK

ゥH S⦅SR IPS ク ョMS . . .

"On theQuadratureofCurves," I. Newton, 1693

DiscreteMathem.atics

Discretemathematicscomesup in algorithmdesignin manyplacessuchascombinatorialoptimization, complexityanalysis, andprobabilityesti-mation. Discretemathematicsis alsothesourceof someof themostfunpuzzlesandinterview ア オ ・ ウ エ ゥ ッ ョ ウ N ャ ・ solutionscanrangefrom simpleapplicationof thepigeon-holeprincipleto complexinductive イ ・ 。 ウ ッ ョ ィ Q N

Someof the problemsin this chapterfall into the categoryof brah1teaserswhereall youneedis oneahamomentto solvetheproblem.SuchproblemshavefalleIIout of fashionbecauseit is hardto judgea caIIdimdate's ability basedon whetherhe is able to makea tricky ッ 「 ウ ・ イ イ 。 エ ゥ ッ ョ

in a shortperiodof time. Howevertheyareaskedenoughtimesthatwefeel it is importantto coverthem. Also, theseproblemsarequite a lot offun to solve.

9.1 COMPUTING THE BINOMIAL COEFFICIENTS

t ィ ・ ウ ケ ュ ャ 「 ッ ャ H セ I ゥ ウ Q ッ イ o イ イ ュ ュ Q ヲ ッ イ (2; a y iUj Qt _ j[Z NZエ ッ choosea ォュ M・ャ・ュ ャ・ョ Q エ subsetfrom an イ z ・ ャ ・ ュ ・ ョ エ set.

CHAPTER8. DESIGNPROBLEMS72

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Jinglewould like to servethesearticlesfrom a datacenterconsistingof a thousandservers.Forperformancereasons, eachservershouldhavea copy of articlesthat wererecentlyadded. The datacenteris far awayfrom thelab machine.

Problem8.15: Suggestan efficient way of gettingthe articlesaddedinthepastfive minutesfrom thelab machineto theservers.

8.17 HOST DISCOVERY

You areto deviseaprotocolbywhicha collectionof hostsontheInternetcandiscovereachother. Hostscancommunicatewith eachotherusingTCP connections.For host A to communicatewith host B, it needstoknow B's IP address.

Eachhoststartsoff with a setof IP 。 、 、 イ ・ ウ ウ ・ ウ ャ 、 the protocolcodethatyou implementwhichwill run on a fixed port acrossall thehosts.

Problem8.17: Devise a protocol by which hostscan discoverall thehostsparticipatingin the protocol. The protocolshouldbe fast andeffi-cientlike in Problem8.16.

8.16 LEADER ELECTION

You areto devisea protocolby whicha collectionof hostsontheInternetcanelect a leader. Hostscancommunicatewith eachotherusingTCPconnections.For hostA to communicatewith hostB , it needsto knowB's IP address.Eachhost startsoff with a set of IP addressesand theprotocolcodethatyou implementthatwill run on a fixed port acrossallthehosts.

Problem8.16: Devise a protocol by which hosts can elect a uniqueleader from all the hosts participatingin the protocol. The protocolshouldbefast, in that it convergesquickly; it shouldbe efficient, in thatit shouldnot involve too manyconnections, too manydataexchanges,andtoo muchdataexchanged.

Chapter9

Thereis required, finally, theratio betweenthefluxion of anyquantityx youwill andthefluxion of its powerx n

. Let xflow till it becomesx ッ。ョ、

resolvethepower H P I エ ッ

theinfinite seriesク ョ ック ョ M Q セHョRMョIPRクョ RK

ゥH S⦅SR IPS ク ョMS . . .

"On theQuadratureofCurves," I. Newton, 1693

DiscreteMathem.atics

Discretemathematicscomesup in algorithmdesignin manyplacessuchascombinatorialoptimization, complexityanalysis, andprobabilityesti-mation. Discretemathematicsis alsothesourceof someof themostfunpuzzlesandinterview ア オ ・ ウ エ ゥ ッ ョ ウ N ャ ・ solutionscanrangefrom simpleapplicationof thepigeon-holeprincipleto complexinductive イ ・ 。 ウ ッ ョ ィ Q N

Someof the problemsin this chapterfall into the categoryof brah1teaserswhereall youneedis oneahamomentto solvetheproblem.SuchproblemshavefalleIIout of fashionbecauseit is hardto judgea caIIdimdate's ability basedon whetherhe is able to makea tricky ッ 「 ウ ・ イ イ 。 エ ゥ ッ ョ

in a shortperiodof time. Howevertheyareaskedenoughtimesthatwefeel it is importantto coverthem. Also, theseproblemsarequite a lot offun to solve.

9.1 COMPUTING THE BINOMIAL COEFFICIENTS

t ィ ・ ウ ケ ュ ャ 「 ッ ャ H セ I ゥ ウ Q ッ イ o イ イ ュ ュ Q ヲ ッ イ (2; a y iUj Qt _ j[Z NZエ ッ choosea ォュ M・ャ・ュ ャ・ョ Q エ subsetfrom an イ z ・ ャ ・ ュ ・ ョ エ set.

CHAPTER8. DESIGNPROBLEMS72If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

9.5 HEIGHT DETERMINATION

You aregivez1aIlumber-of ideIItical balls anda building with N Eoors-Youhowthattkereis m htegerX <N suchthattheballwillbImk if it

9.2 CLIMBING STAIRS

You areclimbingastaircasewith N steps.EverytimeyoucanJumpovereitheronestepor two steps.

Problem9.2: How manywaysarethereto getto thetop of thestaircase?

9.4 500 DOORS

There are500 closeddoorsoff a corridor. A ー・イウッ キ。ャォウ throughtheッ イ andopenseachdoor. Another ー ・ イ ウ キ 。 ャ ォ ウ throughthe corri-

darandcloseseveryalternatedoor. c ッ エ ゥ ョ オ ゥ ョ ァ in this ュ 。 ョ ョ エ ィ ・ i-thー ・ イ ウ ッ 」 ッ ュ ・ ウ andtogglesthe ー 。 ウ ゥ ッ ョ of every i-th door startingfrom

door i.Problem9.4: How manydoorswill be openat the endafter the500-thー ・ イ ウ ッ ィ 。 ウ passedthroughthedoors?

75

9.6 BETTING ON CARD COLORS

A deckof 52playingcardsis shuffled.Thedeckis placedface-downonatable.You canplaceabeton thecolor of thetop cardatevenodds.Afteryou haveplacedyour 「 ・ エ ィ ・ card is revealedto you and discarded.Betting continuestill the deck is exhausted. o ョ ャ ケ card, you canbetanyamountfrom 0 to all the moneyyou haveandthe oddsarealwayseven.

Problem9.6: You beginwith one dollar. It is known that if you canbetarbitraryfractionsof themoneyyou have, themaximumamountofmoneythatyou ァ オ 。 イ 。 ョ エ ・ セ ⦅ ケ ッ オ canwin/ regardlessof theorderin whichthecards。 ー ー ・ 。 ゥ ウ R U R O H I Y N P X Q S R Y U U N Howeveryou areallowedtobetonly in pennyincrements.Write aprogramto computea tight lowerboundon theamountyou canwin underthis restriction.

Invariants

Thefollowing problemwaspopularat interviewsin theearly1990s:youaregivena chessboardwith two squaresat the oppositeendsof a diag-analremoved, leaving62 squares.You aregiven 31 rectangulardomi-noes.Eachcancoverexactlytwo squares.How would you coverall the62 squareswith thedominoes?

It is easyto spendhourstrying unsuccessfullyto find sucha cover-ゥ ョ ァ N ャ ゥ ウ will teachyou thata problemmaybe intentionallywordedtomisleadyou into following a futile path.

Thereis a simpleargumentthatno covering・ ク ゥ ウ エ ウ M エ ィ ・ two squaresremovedwill alwayshavethesamecolor, sotherewill beeither30blackand32 white squaresto be coveredor 32 blackand30 white squarestobe covered.Eachdominowill coveroneblackandonewhite square, sothenumberof blackandwhite squarescoveredasyou successivelyputdown the dominoesis equal. Henceit is impossibleto coverthe givenchessboard.

ャ ゥ ウ proofof impossibilityis anexampleof invariantanalysis.a ゥ ᆳ

variantis a functionof thestateof a systembeinganalyzedthatremainsconstantin the presenceof (possiblyrestricted)updatesto the state.In-variantanalysisis particularly ー ッ キ ・ イ i atprovingimpossibilityresultsaswejustsawwith thechessboardtiling problem.Thechallengeis find-ing a simpleinvariant.

is droppedfrom anyfloor X or higherbutwill remainintactif droppedfrom a floor belowX.

Problem9.5: GivenK ballsand n ッ ッ イ ウ whatis theminimumnumberof ball dropsthatarerequiredto determineX in theworst-case?

9.6. BETTINGONCARDCOLORSCHAPTER9. DISCRETEMATHEMATICS

9.3 r a Q s e y THEORY

In 1930/ fイ。ョ ャォ r。ュ ョウ ウ・ ケ wrote a ー。 ー・ イ titled 1/ o イ ャ 。 ー イ ッ 「 ャ ・ ュ in formal

Q P キ

E 「 。 ュ ウ Q ウ ・ ケ t ィ Q ・ y ィゥ ウィッ oイN h ・ー イッカ ・、キ ィ。 エゥ ウョッキ」 。ャャ・、 r。ュ ュウ s ・ ケGウtheoremasm intermediatelemmah abiggeyproof-Theproblembel willustratesRamsey's theorem.

Problem9.3: Thereare six guestsat a party suchthat any two guestseitherknow eachotheror 、 ッ 。 エ know eachother. Provethat thereis asubsetof threeguestswho eitherall howeachotheror all 、 ッ ッ エ ォ ョ ッ キ

eachother.

It is not obvious that the ・ ク ー イ ・ ウ ウ ゥ ッ 、 ・ ヲ ゥ ョ ゥ ョ ァ G) alwaysyields aninteger. f エ ィ ・ イ ュ ッ イ ・ direct 」 ッ ュ ー オ エ 。 エ ゥ ッ ッ ヲ G) from this expression

quickly resultsin エ ャ Qエ ケ ー ・ ウ 。 イ ・ オ ウ ウ ・ 、 evenif the ヲ ゥ ョ ャ 。 ャ result ヲ ゥ エ ウ inan ャ エ ・ ァ ・ イ N If ヲ ャ ッ 。 エ ウ areusedthe ・ ク ー イ ・ ウ s ゥ ッ i Q ュ ャ 。 ケ イ ャ o エ yield aninteger.

ProblemY d ・ ョ an ・ ヲ ヲ ゥ 」 ゥ ・ 。 ャ ァ ッ イ ゥ エ ィ ュ for 」 ッ ュ ー オ エ ゥ H セ I thathasthepropertythat it neveroverflowsif G) canbe イ ・ ー イ ・ ウ ・ 、 asan ゥ エ

assume。 ョ 、 k are ints.

74

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

9.5 HEIGHT DETERMINATION

You aregivez1aIlumber-of ideIItical balls anda building with N Eoors-Youhowthattkereis m htegerX <N suchthattheballwillbImk if it

9.2 CLIMBING STAIRS

You areclimbingastaircasewith N steps.EverytimeyoucanJumpovereitheronestepor two steps.

Problem9.2: How manywaysarethereto getto thetop of thestaircase?

9.4 500 DOORS

There are500 closeddoorsoff a corridor. A ー・イウッ キ。ャォウ throughtheッ イ andopenseachdoor. Another ー ・ イ ウ キ 。 ャ ォ ウ throughthe corri-

darandcloseseveryalternatedoor. c ッ エ ゥ ョ オ ゥ ョ ァ in this ュ 。 ョ ョ エ ィ ・ i-thー ・ イ ウ ッ 」 ッ ュ ・ ウ andtogglesthe ー 。 ウ ゥ ッ ョ of every i-th door startingfrom

door i.Problem9.4: How manydoorswill be openat the endafter the500-thー ・ イ ウ ッ ィ 。 ウ passedthroughthedoors?

75

9.6 BETTING ON CARD COLORS

A deckof 52playingcardsis shuffled.Thedeckis placedface-downonatable.You canplaceabeton thecolor of thetop cardatevenodds.Afteryou haveplacedyour 「 ・ エ ィ ・ card is revealedto you and discarded.Betting continuestill the deck is exhausted. o ョ ャ ケ card, you canbetanyamountfrom 0 to all the moneyyou haveandthe oddsarealwayseven.

Problem9.6: You beginwith one dollar. It is known that if you canbetarbitraryfractionsof themoneyyou have, themaximumamountofmoneythatyou ァ オ 。 イ 。 ョ エ ・ セ ⦅ ケ ッ オ canwin/ regardlessof theorderin whichthecards。 ー ー ・ 。 ゥ ウ R U R O H I Y N P X Q S R Y U U N Howeveryou areallowedtobetonly in pennyincrements.Write aprogramto computea tight lowerboundon theamountyou canwin underthis restriction.

Invariants

Thefollowing problemwaspopularat interviewsin theearly1990s:youaregivena chessboardwith two squaresat the oppositeendsof a diag-analremoved, leaving62 squares.You aregiven 31 rectangulardomi-noes.Eachcancoverexactlytwo squares.How would you coverall the62 squareswith thedominoes?

It is easyto spendhourstrying unsuccessfullyto find sucha cover-ゥ ョ ァ N ャ ゥ ウ will teachyou thata problemmaybe intentionallywordedtomisleadyou into following a futile path.

Thereis a simpleargumentthatno covering・ ク ゥ ウ エ ウ M エ ィ ・ two squaresremovedwill alwayshavethesamecolor, sotherewill beeither30blackand32 white squaresto be coveredor 32 blackand30 white squarestobe covered.Eachdominowill coveroneblackandonewhite square, sothenumberof blackandwhite squarescoveredasyou successivelyputdown the dominoesis equal. Henceit is impossibleto coverthe givenchessboard.

ャ ゥ ウ proofof impossibilityis anexampleof invariantanalysis.a ゥ ᆳ

variantis a functionof thestateof a systembeinganalyzedthatremainsconstantin the presenceof (possiblyrestricted)updatesto the state.In-variantanalysisis particularly ー ッ キ ・ イ i atprovingimpossibilityresultsaswejustsawwith thechessboardtiling problem.Thechallengeis find-ing a simpleinvariant.

is droppedfrom anyfloor X or higherbutwill remainintactif droppedfrom a floor belowX.

Problem9.5: GivenK ballsand n ッ ッ イ ウ whatis theminimumnumberof ball dropsthatarerequiredto determineX in theworst-case?

9.6. BETTINGONCARDCOLORSCHAPTER9. DISCRETEMATHEMATICS

9.3 r a Q s e y THEORY

In 1930/ fイ。ョ ャォ r。ュ ョウ ウ・ ケ wrote a ー。 ー・ イ titled 1/ o イ ャ 。 ー イ ッ 「 ャ ・ ュ in formal

Q P キ

E 「 。 ュ ウ Q ウ ・ ケ t ィ Q ・ y ィゥ ウィッ oイN h ・ー イッカ ・、キ ィ。 エゥ ウョッキ」 。ャャ・、 r。ュ ュウ s ・ ケGウtheoremasm intermediatelemmah abiggeyproof-Theproblembel willustratesRamsey's theorem.

Problem9.3: Thereare six guestsat a party suchthat any two guestseitherknow eachotheror 、 ッ 。 エ know eachother. Provethat thereis asubsetof threeguestswho eitherall howeachotheror all 、 ッ ッ エ ォ ョ ッ キ

eachother.

It is not obvious that the ・ ク ー イ ・ ウ ウ ゥ ッ 、 ・ ヲ ゥ ョ ゥ ョ ァ G) alwaysyields aninteger. f エ ィ ・ イ ュ ッ イ ・ direct 」 ッ ュ ー オ エ 。 エ ゥ ッ ッ ヲ G) from this expression

quickly resultsin エ ャ Qエ ケ ー ・ ウ 。 イ ・ オ ウ ウ ・ 、 evenif the ヲ ゥ ョ ャ 。 ャ result ヲ ゥ エ ウ inan ャ エ ・ ァ ・ イ N If ヲ ャ ッ 。 エ ウ areusedthe ・ ク ー イ ・ ウ s ゥ ッ i Q ュ ャ 。 ケ イ ャ o エ yield aninteger.

ProblemY d ・ ョ an ・ ヲ ヲ ゥ 」 ゥ ・ 。 ャ ァ ッ イ ゥ エ ィ ュ for 」 ッ ュ ー オ エ ゥ H セ I thathasthepropertythat it neveroverflowsif G) canbe イ ・ ー イ ・ ウ ・ 、 asan ゥ エ

assume。 ョ 、 k are ints.

74

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

9.7 EVENORODD

LetAbeamultisetof htegers.Considerthefollowhg process:raIIdomlyselecttwo elementsof A. If theyarebothevenor bothodd, removethemfrom the setandinserta new eveninteger;if not, removejust the eveninteger.

Problem9.7: What canyou sayaboutthe last remainingintegeras aᆪu ョ、ゥッ ッヲ thenumbersof evenandoddintegersinitially in A?

Let's considera more sophisticatedexamplenow, namely the MUpuzzle. The following rulesmaybe appliedto transforma string overthealphabet{M,I ,U}:

1.If a strhgendswith m I, a U maybe appmded(xIHXIU).Forexample-MIto MIU.

2.A strimzaftera starthgM maybe completelyduplicated(MXMMxx). For example-MIUto MIUIU.

3. ThreeconsecutiveIs (III) maybereplacedwith a singleU (xIIly f-+

xUy). For example-MIIIUto MUU.4.TwocOIISecutiveLIs mayberemovedULIUYH XY).For example-

MUUII to MILProblem: Is it possibleto convertMI into MU by repeated。 ー ー ャ ゥ 」 。 エ ゥ ッ ッ ヲ

thesefour transformationrules?You cmtry differeIIt strategiesto fiI1d theright sequenceof transfor-

IIlatiom mdaftera while youmaybeghto suspectthat it is impossibleto performthis conversion.showhgthatmsequeRceof trmsforma-tiom will implementtke trmsformatiORseemsdauntingat first-afterall, therearehfidtely manytrmsformatiom.HowevercOImiderthefol-loWhg ゥ i ュ the rョ・カ ・イ 。ュ ャオャエゥー i・ of three.

We prove the invariantby inductionon the numberof transforma-エ ゥ ッ ュ ー j イ ュ ・ 、 m i N For the 「 。 ウ ・ ・ MI has1 I, which is not a mul-tiple of three.

For the hductivestepr trmsformatiom1md4donot chmgethemberof Is, so inductiongoesthroughin this case.The numberof Is

afterappliedmoftrmsfofIRatioI12istwice thenumberof Is.Sorif thenumberof Is キ。ウ ッエ a multiple of three, i.e., wasof theform 3 N ャッイ

Sᄋ R thenエ ィ ・ オ ュ 「 ・ イ of Is aftertransformation2 is either2· HSᄋKQI3 . (2 NI R or 2 . (3 . ョ RI = 3 . (2 N QI + 1, neitherof which is amultiple of 3.TransformatiOI13reducesthe numberof Is by threeandinductiongoesthroughin this casetoo.

SinceMU hasaiウセ which is a multiple of three, it is impossibleto getfrom MI to MU.

9.9 COMMON KNOWLEDGE

77

9.1a HERSHEY BAR

A Hersheybaris modeledasm x n rectangleof m .n pieces.You cantakeabarandbreakit alongahorizontalor verticalaxis into two bars.

Problem9.10: How wouldyoubreaka 4 x 4barinto 16piecesusingasfew breaksaspossible?

An explorercomesto anislandwith 100inhabitants.Exactlyhalf エ ィ Bhabitantshaveblue eyesandhalf the inhabitantshavegreeneyes. Thegreeneyesare indicative of a diseasethat brings all the island ゥ ャ 。 「 ゥ ᆳ

tantsin danger.Thereis anunderstandingon the islandthatwheneversomeonelearnsthat they havegreeneyes, they must leavethe island;theyneverleavethe islandfor anyother イ ・ 。 ウ ッ N Theinhabitantsaretoopolite to inform anyoneelseof eyecolor. Thereareno othermeansforthe inhabitantsto observethecolor of their eyesP エ ィ ・ island.

ャ ・ ャ 。 「 ゥ エ ャ エ s assembleeachdayat exactly9:00AM , theyseeeachother, andthengobackto their ownhouses.Theyneverseeanyoneelsefor the restof the day. Furthermore, they arecapableof instantlogicalreasonIng.

ャ ・ explorervisits one of their daily assembliesand says, "That'sinteresting-someof you haveblue eyesand someof you havegreeneyes".

Problem9.9: What would follow after this event? In ー 。 イ エ ゥ 」 オ ャ 。 キ ィ ケ

doesthis ッ 「 ウ ・ イ イ 。 エ ゥ ッ ョ saddentheinhabitants?ャ ・ explorerseemsto haveaddedno newknowledgeto the system

sinceeachinhabitantcanalreadytell that 。 ュ ァ ウ エ theinhabitants, somehaveblueeyesandsomehavegreeneyes.

9.8. GASSINGUP

9.8 GASSING UP

c ッ ウ ゥ 、 ・ イ a circular routethat 」 ッ ョ ョ ・ 」 エ ウ 」 ゥ エ ゥ ・ ウ N You needto visit all then 」ゥエゥ・ウ ャ、 comebackto the startingcity. In eachcit)', a 」・イエ。ゥョ ッオ エ

of gasolineis kept for you suchthat the total amountof gasolineP エ ィ ・

routeis exactlyequalto theamountof gasolineneededto go aroundthecircularrouteonce.

Problem9.8: Is it alwayspossibleto ヲ 。 starting ー ッ ャ エ on the routesuchthatyoucanstarttherewith anemptytankandcompletetheroute?How canyou efficiently find this city?

CHAPTER9. DISCRETEMATHEMATICS76

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

9.7 EVENORODD

LetAbeamultisetof htegers.Considerthefollowhg process:raIIdomlyselecttwo elementsof A. If theyarebothevenor bothodd, removethemfrom the setandinserta new eveninteger;if not, removejust the eveninteger.

Problem9.7: What canyou sayaboutthe last remainingintegeras aᆪu ョ、ゥッ ッヲ thenumbersof evenandoddintegersinitially in A?

Let's considera more sophisticatedexamplenow, namely the MUpuzzle. The following rulesmaybe appliedto transforma string overthealphabet{M,I ,U}:

1.If a strhgendswith m I, a U maybe appmded(xIHXIU).Forexample-MIto MIU.

2.A strimzaftera starthgM maybe completelyduplicated(MXMMxx). For example-MIUto MIUIU.

3. ThreeconsecutiveIs (III) maybereplacedwith a singleU (xIIly f-+

xUy). For example-MIIIUto MUU.4.TwocOIISecutiveLIs mayberemovedULIUYH XY).For example-

MUUII to MILProblem: Is it possibleto convertMI into MU by repeated。 ー ー ャ ゥ 」 。 エ ゥ ッ ッ ヲ

thesefour transformationrules?You cmtry differeIIt strategiesto fiI1d theright sequenceof transfor-

IIlatiom mdaftera while youmaybeghto suspectthat it is impossibleto performthis conversion.showhgthatmsequeRceof trmsforma-tiom will implementtke trmsformatiORseemsdauntingat first-afterall, therearehfidtely manytrmsformatiom.HowevercOImiderthefol-loWhg ゥ i ュ the rョ・カ ・イ 。ュ ャオャエゥー i・ of three.

We prove the invariantby inductionon the numberof transforma-エ ゥ ッ ュ ー j イ ュ ・ 、 m i N For the 「 。 ウ ・ ・ MI has1 I, which is not a mul-tiple of three.

For the hductivestepr trmsformatiom1md4donot chmgethemberof Is, so inductiongoesthroughin this case.The numberof Is

afterappliedmoftrmsfofIRatioI12istwice thenumberof Is.Sorif thenumberof Is キ。ウ ッエ a multiple of three, i.e., wasof theform 3 N ャッイ

Sᄋ R thenエ ィ ・ オ ュ 「 ・ イ of Is aftertransformation2 is either2· HSᄋKQI3 . (2 NI R or 2 . (3 . ョ RI = 3 . (2 N QI + 1, neitherof which is amultiple of 3.TransformatiOI13reducesthe numberof Is by threeandinductiongoesthroughin this casetoo.

SinceMU hasaiウセ which is a multiple of three, it is impossibleto getfrom MI to MU.

9.9 COMMON KNOWLEDGE

77

9.1a HERSHEY BAR

A Hersheybaris modeledasm x n rectangleof m .n pieces.You cantakeabarandbreakit alongahorizontalor verticalaxis into two bars.

Problem9.10: How wouldyoubreaka 4 x 4barinto 16piecesusingasfew breaksaspossible?

An explorercomesto anislandwith 100inhabitants.Exactlyhalf エ ィ Bhabitantshaveblue eyesandhalf the inhabitantshavegreeneyes. Thegreeneyesare indicative of a diseasethat brings all the island ゥ ャ 。 「 ゥ ᆳ

tantsin danger.Thereis anunderstandingon the islandthatwheneversomeonelearnsthat they havegreeneyes, they must leavethe island;theyneverleavethe islandfor anyother イ ・ 。 ウ ッ N Theinhabitantsaretoopolite to inform anyoneelseof eyecolor. Thereareno othermeansforthe inhabitantsto observethecolor of their eyesP エ ィ ・ island.

ャ ・ ャ 。 「 ゥ エ ャ エ s assembleeachdayat exactly9:00AM , theyseeeachother, andthengobackto their ownhouses.Theyneverseeanyoneelsefor the restof the day. Furthermore, they arecapableof instantlogicalreasonIng.

ャ ・ explorervisits one of their daily assembliesand says, "That'sinteresting-someof you haveblue eyesand someof you havegreeneyes".

Problem9.9: What would follow after this event? In ー 。 イ エ ゥ 」 オ ャ 。 キ ィ ケ

doesthis ッ 「 ウ ・ イ イ 。 エ ゥ ッ ョ saddentheinhabitants?ャ ・ explorerseemsto haveaddedno newknowledgeto the system

sinceeachinhabitantcanalreadytell that 。 ュ ァ ウ エ theinhabitants, somehaveblueeyesandsomehavegreeneyes.

9.8. GASSINGUP

9.8 GASSING UP

c ッ ウ ゥ 、 ・ イ a circular routethat 」 ッ ョ ョ ・ 」 エ ウ 」 ゥ エ ゥ ・ ウ N You needto visit all then 」ゥエゥ・ウ ャ、 comebackto the startingcity. In eachcit)', a 」・イエ。ゥョ ッオ エ

of gasolineis kept for you suchthat the total amountof gasolineP エ ィ ・

routeis exactlyequalto theamountof gasolineneededto go aroundthecircularrouteonce.

Problem9.8: Is it alwayspossibleto ヲ 。 starting ー ッ ャ エ on the routesuchthatyoucanstarttherewith anemptytankandcompletetheroute?How canyou efficiently find this city?

CHAPTER9. DISCRETEMATHEMATICS76

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

9.13 m クchomp

SolveProblem9.11 if therectangleis of 、 ゥ ュ ・ ョ ウ ゥ ッ ュ ク N

Problem9.13: Assumingthe playershave infinite computationalre-sourcesat their disposal, whowill win thegame?

Y N Q R ク 2 CHOMP

SolveProblem9.11if therectangleis of 、 ゥ ュ ・ ョ ウ ゥ ッ ョ ク 2.

Problem9.12: Assumingthe playershave infinite computationalre-sourcesat their disposal, who will win thegame?

799.16. SPACE-TIMEINTERSECTIONS

9.16 SPACE-TIMEINTERSECTIONS

Adamstartsclimbingamountainat9:00AM onSaturday.Hereachesthesummitat5:00PM. He campsat thesummitovernightanddescendsthemountainon s オ 、 。 ケ N He beginsandendsat the sametime andfollowsexactlythesameroute. His speedsmayvary andhemaytakebreaksatdifferentplaces.

Problem9.16: Provethatthereis a time andaplacesuchthat a 、 ゥ ウ at・ ク 。 」 エ エ ィ ・ sameplaceat thesametime onSaturdayasheis onSunday.

CHAPTER9. DISCRETEMATHEMATICS

9.15 PICKING UP COINS-II

Problem9.14 doesnot ask for the optimum profit. Let's explore thestrategiesthatwouldmaximizethewinnings.

Problem9.15: Derive an efficient algorithm for computingthe maxi-ュ オ ュ ョ ッ オ ョ エ of moneyF canwin.

9.14 PICKING UP COINS-I

Therearefifty coinsin a line-thesecouldbepennies, nickels, dimes, orquarters.Two players, F andS, take turnsat choosingonecoin each-they can only choosefrom the two coins at the endsof the line. Thegameendswhenall the coinshavebeenpickedup. The playerwhosecoinshavethe higher total valuewins. Eachplayermustselecta coinwhenit is his tum, sothegameendsin fifty turns.

Problem9.14: If you want to ensureyou do not lose, would you ratherbeF or S?

Y N Q Q ᅲ c h o m p

Considerann クイ・」エ。ョァャ・ in theupperright quadrantin theCartesianplane, with thelower leftmostpointat (0,0). Theblock (0,0) is knowntocontainpoison.Two playerstaketurnsat takinga bite out of the rectan-gle. A bite removesa squareandall squaresaboveandto the right. Thefirst playerto eatthesquareat (0,0) loses.

Problem9.11: Assumingthe playershave infinite computationalre-sourcesat their disposal, who will win thegame?

78

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

9.13 m クchomp

SolveProblem9.11 if therectangleis of 、 ゥ ュ ・ ョ ウ ゥ ッ ュ ク N

Problem9.13: Assumingthe playershave infinite computationalre-sourcesat their disposal, whowill win thegame?

Y N Q R ク 2 CHOMP

SolveProblem9.11if therectangleis of 、 ゥ ュ ・ ョ ウ ゥ ッ ョ ク 2.

Problem9.12: Assumingthe playershave infinite computationalre-sourcesat their disposal, who will win thegame?

799.16. SPACE-TIMEINTERSECTIONS

9.16 SPACE-TIMEINTERSECTIONS

Adamstartsclimbingamountainat9:00AM onSaturday.Hereachesthesummitat5:00PM. He campsat thesummitovernightanddescendsthemountainon s オ 、 。 ケ N He beginsandendsat the sametime andfollowsexactlythesameroute. His speedsmayvary andhemaytakebreaksatdifferentplaces.

Problem9.16: Provethatthereis a time andaplacesuchthat a 、 ゥ ウ at・ ク 。 」 エ エ ィ ・ sameplaceat thesametime onSaturdayasheis onSunday.

CHAPTER9. DISCRETEMATHEMATICS

9.15 PICKING UP COINS-II

Problem9.14 doesnot ask for the optimum profit. Let's explore thestrategiesthatwouldmaximizethewinnings.

Problem9.15: Derive an efficient algorithm for computingthe maxi-ュ オ ュ ョ ッ オ ョ エ of moneyF canwin.

9.14 PICKING UP COINS-I

Therearefifty coinsin a line-thesecouldbepennies, nickels, dimes, orquarters.Two players, F andS, take turnsat choosingonecoin each-they can only choosefrom the two coins at the endsof the line. Thegameendswhenall the coinshavebeenpickedup. The playerwhosecoinshavethe higher total valuewins. Eachplayermustselecta coinwhenit is his tum, sothegameendsin fifty turns.

Problem9.14: If you want to ensureyou do not lose, would you ratherbeF or S?

Y N Q Q ᅲ c h o m p

Considerann クイ・」エ。ョァャ・ in theupperright quadrantin theCartesianplane, with thelower leftmostpointat (0,0). Theblock (0,0) is knowntocontainpoison.Two playerstaketurnsat takinga bite out of the rectan-gle. A bite removesa squareandall squaresaboveandto the right. Thefirst playerto eatthesquareat (0,0) loses.

Problem9.11: Assumingthe playershave infinite computationalre-sourcesat their disposal, who will win thegame?

78If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

10.2 RESERVOIRSAMPLING

You arebuilding a packetsniffer for your networkthat shouldbe ableto provide a uniform sampleof packetsfor any networksession. You

10.1 OFFLINE SAMPLING

Let A beanarrayof 、ゥウエゥョ」エ elements.We wantto computea randomsubsetof k elements.

Problem10.1: Designan algorlihmthatreturnsa subsetof k elements;all subsetsshouldbeequallylikely. Useasfew callsto therandomnum-bergeneratoraspossibleanduseonly 0(1) additionalstorage.(You canreturntheresultin thesamearrayasinput.)

8110.1. OFFLINESAMPLING

ues, we cantalk abouttheprobabilityof X takingaparticularvalue, i.e.,p(X = 7i)' If X takesa 」ッイ Q オウ rangeof valuesandFx is differen-tiable, we talk of Q N 。 ウ beingthe ー イ ッ 「 エ ケ densityfunction.

The expectedvalue E[X] of a randomvariableX taking a finite setof valuesT = {70 ,71,… ,7n-1} is simply 2::

T;ET 7i . p(X = 7i), i.e., it is

the averagevalue of X/ weightedby probabilliies. The ョ ッ エ ゥ ッ ッ ヲ ex-pectationgeneralizesto countablesetsof values. For a randomvari-able taking a 」ッ ゥョオッオウ setof values, the sumis replacedwlih an in-tegral and the weighting function is the probability density function.The variancevar(X) of a randomvariable X is the expectedvalue of(IX - E[X])2. Someof the key resultsin probability haveto do withboundson the probability of events, e.g., the Chebyshevboundwhichウ 。 ケ ウ 。 エ Pr(IX - e{x}i ォvv

There are a number of famous classesof random variables-theBernoulli randomvariabletakesonly two values, 0 and1; li is used, forexample, in modeling 」 エ ッ ウ ウ ・ ウ N The Poissonrandomvariabletakesnonnegativevalues-itmodelsthe numberof eventsin a fixed periodof timet e.g., the numberof HTTP requestsin a minute. The Gaussianrandomvariabletakesall realvalues M エ ィ ・ sumof a seriesof identicallydistributedindependentrandomvariablesエ ・ 、 ウ to Gaussian.

For themostpart, probability 」 ッ イ イ ・ ウ ー ッ エ ッ our intuition; therearehowevernotableexceptions.For example, at first ァ ャ 。 ョ ャ ゥ would seemョ ー ッ ウ ウ ゥ 「 ャ ・ for there to exists three ウ ゥ 、 ・ 、 dice A,B ,0 suchthat A is

more likely to roll a highernumberthan B/ B is more likely to roll ahighernumberthan0/ and0 is morelikely to roll ahighernumberthanA. Howeverif A hassides2,2,4,4,9,9/ B hassides1,1,6,6,8,8/ anddice 0 hassides3,3,5,5,7,7/ thenthe probability that A rolls a highernumberthanB is セ / the probabilitythatB iウ a ィゥァ ィ・

ゥ ウ セ / andtheprobabilitythat0 rolls ahighernumberthanA is セ

"TheoryOf TheMotion Of TheHeavenlyBodiesMovingAboutTheSunIn Conic

Sections/"C. Gauss, 1809

Probabilitycomesoften in algorithmsandsoftware ・ ァ ゥ ョ ・ ・ イ ゥ ョ ァ eliherwhenyou aretryhg to modela rmdomeveIItr suchasa client requestor desigI1aI1efficientalgorithmFsuchasQ1licksortGiverltherichI1essofthe subject, li providesa largenumberof ゥ ョ エ ・ イ ・ ウ エ ゥ ー オ コ コ ャ ・ ウ andinter-view questions.

To a first 。 ー ー イ ッ ク ゥ ュ 。 エ ゥ ッ 。 probabilliy measureis a function p fromsubsetsof a setE of eveI1tsto [071lthathasthepropeytiesthatp(E)=1andp(A U B) = ーHaI ーHbI for 、 ゥ ッ ゥ ョ エ A and b N v 。 。 イ ゥ ッ オ ウ ウ ー イ ッ ー ・ イ エ ゥ ・ ウ ウ 。 ョ Q

ョ ッ エ 。 エ ゥ ッ イ Q s 」 。 Q begiven 。 イ ッ オ イ Q 、 these」 ッ 」 ・ ー エ ウ N For ・ ク ー ャ ・ it is easytoprovetkatp(AU B)=p(A)+p(B)-p(An B)·

A randomvariableX is a functionfrom E to H W I [ ゥ エ 」 。 イ Q 「 ・ ゥ 、 ・ ョ Q

エ ゥ ヲ ゥ ・ 、 with a 」オュオ Q ャ。エ エゥ カ・ 、ゥ ウエ イゥ 「オ エゥッ i Q ヲ オョ 」エゥ o Fx : •• [0 ,1]/ wherefX(7) = ーHxM Q HH 7J) ). WhenX takesa finlie or 」 ッ オ ョ エ ッ ヲ カ 。 ャ M

Therefore, thatwill bethemostprobablesystemof valuesoftheunknownquantitiesp,q,r ,s,etc., in whichthesumof thesquaresof thedifferencesbetweentheobservedandcomputedvaluesof thefunctions Q vi V" ,etc. is aminimum, if thesamedegreeofaccuracyis presumedin all theッ「ウ・イ イ。エゥッョウN

Probability

Chapter10

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

10.2 RESERVOIRSAMPLING

You arebuilding a packetsniffer for your networkthat shouldbe ableto provide a uniform sampleof packetsfor any networksession. You

10.1 OFFLINE SAMPLING

Let A beanarrayof 、ゥウエゥョ」エ elements.We wantto computea randomsubsetof k elements.

Problem10.1: Designan algorlihmthatreturnsa subsetof k elements;all subsetsshouldbeequallylikely. Useasfew callsto therandomnum-bergeneratoraspossibleanduseonly 0(1) additionalstorage.(You canreturntheresultin thesamearrayasinput.)

8110.1. OFFLINESAMPLING

ues, we cantalk abouttheprobabilityof X takingaparticularvalue, i.e.,p(X = 7i)' If X takesa 」ッイ Q オウ rangeof valuesandFx is differen-tiable, we talk of Q N 。 ウ beingthe ー イ ッ 「 エ ケ densityfunction.

The expectedvalue E[X] of a randomvariableX taking a finite setof valuesT = {70 ,71,… ,7n-1} is simply 2::

T;ET 7i . p(X = 7i), i.e., it is

the averagevalue of X/ weightedby probabilliies. The ョ ッ エ ゥ ッ ッ ヲ ex-pectationgeneralizesto countablesetsof values. For a randomvari-able taking a 」ッ ゥョオッオウ setof values, the sumis replacedwlih an in-tegral and the weighting function is the probability density function.The variancevar(X) of a randomvariable X is the expectedvalue of(IX - E[X])2. Someof the key resultsin probability haveto do withboundson the probability of events, e.g., the Chebyshevboundwhichウ 。 ケ ウ 。 エ Pr(IX - e{x}i ォvv

There are a number of famous classesof random variables-theBernoulli randomvariabletakesonly two values, 0 and1; li is used, forexample, in modeling 」 エ ッ ウ ウ ・ ウ N The Poissonrandomvariabletakesnonnegativevalues-itmodelsthe numberof eventsin a fixed periodof timet e.g., the numberof HTTP requestsin a minute. The Gaussianrandomvariabletakesall realvalues M エ ィ ・ sumof a seriesof identicallydistributedindependentrandomvariablesエ ・ 、 ウ to Gaussian.

For themostpart, probability 」 ッ イ イ ・ ウ ー ッ エ ッ our intuition; therearehowevernotableexceptions.For example, at first ァ ャ 。 ョ ャ ゥ would seemョ ー ッ ウ ウ ゥ 「 ャ ・ for there to exists three ウ ゥ 、 ・ 、 dice A,B ,0 suchthat A is

more likely to roll a highernumberthan B/ B is more likely to roll ahighernumberthan0/ and0 is morelikely to roll ahighernumberthanA. Howeverif A hassides2,2,4,4,9,9/ B hassides1,1,6,6,8,8/ anddice 0 hassides3,3,5,5,7,7/ thenthe probability that A rolls a highernumberthanB is セ / the probabilitythatB iウ a ィゥァ ィ・

ゥ ウ セ / andtheprobabilitythat0 rolls ahighernumberthanA is セ

"TheoryOf TheMotion Of TheHeavenlyBodiesMovingAboutTheSunIn Conic

Sections/"C. Gauss, 1809

Probabilitycomesoften in algorithmsandsoftware ・ ァ ゥ ョ ・ ・ イ ゥ ョ ァ eliherwhenyou aretryhg to modela rmdomeveIItr suchasa client requestor desigI1aI1efficientalgorithmFsuchasQ1licksortGiverltherichI1essofthe subject, li providesa largenumberof ゥ ョ エ ・ イ ・ ウ エ ゥ ー オ コ コ ャ ・ ウ andinter-view questions.

To a first 。 ー ー イ ッ ク ゥ ュ 。 エ ゥ ッ 。 probabilliy measureis a function p fromsubsetsof a setE of eveI1tsto [071lthathasthepropeytiesthatp(E)=1andp(A U B) = ーHaI ーHbI for 、 ゥ ッ ゥ ョ エ A and b N v 。 。 イ ゥ ッ オ ウ ウ ー イ ッ ー ・ イ エ ゥ ・ ウ ウ 。 ョ Q

ョ ッ エ 。 エ ゥ ッ イ Q s 」 。 Q begiven 。 イ ッ オ イ Q 、 these」 ッ 」 ・ ー エ ウ N For ・ ク ー ャ ・ it is easytoprovetkatp(AU B)=p(A)+p(B)-p(An B)·

A randomvariableX is a functionfrom E to H W I [ ゥ エ 」 。 イ Q 「 ・ ゥ 、 ・ ョ Q

エ ゥ ヲ ゥ ・ 、 with a 」オュオ Q ャ。エ エゥ カ・ 、ゥ ウエ イゥ 「オ エゥッ i Q ヲ オョ 」エゥ o Fx : •• [0 ,1]/ wherefX(7) = ーHxM Q HH 7J) ). WhenX takesa finlie or 」 ッ オ ョ エ ッ ヲ カ 。 ャ M

Therefore, thatwill bethemostprobablesystemof valuesoftheunknownquantitiesp,q,r ,s,etc., in whichthesumof thesquaresof thedifferencesbetweentheobservedandcomputedvaluesof thefunctions Q vi V" ,etc. is aminimum, if thesamedegreeofaccuracyis presumedin all theッ「ウ・イ イ。エゥッョウN

Probability

Chapter10

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

10.6 FORMING A TRIANGLE FROM RANDOM LENGTHS

alwayswant to get k packetsirrespectiveof the length (assumingeachsessionis longerthank).

Problem10.2: Computea randomsubsetof size k from a set of un-knownsizewhich is ーイ・ウ・ エ・、 asasequenceof elements.

10.3 ONLINE SAMPLING

Computea randomsubsetof size k from the integersin the interval{ P Q } ᄋ y ッ オ shouldretumtheresulth m arrayof leI1gthk.You mayuseonly 」 ッ ョ ウ エ Q エ additionalstorage.All subsetsshouldbeequallylikelyandall permutationsof thearrayshould「 ・ 。 ャ ャ ケ likely.

83

Supposewe createa random ー ・ イ ュ オ エ 。 エ ゥ ッ ヲ (1, 2,… ,n) as in Prob-lem 10.51 i.e'l eachpermutationhasequalprobability.

Problem 10.8: What is the ・ ク ー ・ 」 エ ・ 、 オ ュ 「 ・ イ of numbers that getmappedto themselves?What is the expectedlength of the largestin-creasingウ オ 「 ウ ・ ア オ ・ ョ 」 ・ ] H x ャ N N Xl) in a randomlychosenpermuta-エ ゥ ッ i whereXl is thefirst elementof thepermutationandXk ,k > 1 is thefirst elementthatis largerthanXk-l.

10.8 RANDOM PERMUTATIONS

10.9 UNIFORM RANDOM NUMBER GENERATION

Sometimesyoumaynothavetheperfectrandomnumbergeneratoryouneed.For examplel it would requirea bit of thinking to devisean algo-rithm to pick oneout of five ヲ イ ゥ ・ 、 ウ who getsto bethedesignateddriverby a coin flip suchthattheprocessis fair to everyone.

Problem10.9: How would you implementa randomnumbergenera-tor that generatesa randominteger 「 ・ エ キ ・ ・ ョ 。 ョ 、 bl given a randomnumbergeneratorthatproduceseitherzeroor onewith equalprobabil-

ケ N What would be the runtimeof this algorithml assumingeachcall tothegivenrandomnumbergeneratortakes0(1) time?

Supposeyou want to write a load test for your server. You looked attheinter-arrivaltime of requeststo your serveroveraperiodof oneyearandfrom this datayou havecomputeda histogramof the distribution

10.10 NONUNIFORM RANDOM NUMBER GENERATION

Canyoudeterminewhichof theabovetwo methodsof generatingUlandU2 is morelikely to producea trianglewithout computingtheexactprobabilities?

10.7 BALLS AND BINS

Supposeyouhaveキ ・ 「 serverstalkingto m clientssuchthateachclientpicks a serveruniformly at random.If you 、 ッ ッ エ endup wastingyourservercapacitYI this is a niceway of pairingserversto clientssinceyoudo not needto centralizeanything. But thereis a chancethat someofyourserversareidle while clientsarewaitingto beserved.How likely is

エィ。エ therewill beserversthatarenot doinganything?This problemisoftenmodeledusingballsandbins.

Problem10.7: If you throw m balls into n bins randomlYI how wouldyoucomputetheexpectednumberof bins thatdonothaveanyballs?

10.7. BALLSANDBINSCHAPTER10. PROBABILITY

10.5 RANDOM permutations MR

In Problem10.41we sawthatgeneratingrandompermutationsis not as

straightforwardasit seems.

Problem10.5: Designanalgorithmthatcreatesrandompermutationsof{I , R NNN スN Eachpermutationshouldbe ・ ア エ Z ャ ケ likely. You aregivenaraI1domIIUmber geI1eratioI1fUIICtiOIUuseasfew callsto it aspossible-

Supposeyou pick Wo IIumbersu1mdd uniformly rmdomlymdh-dependeI1tlyh the interval [0711·TheseIIumbers divide tEIe htervalinto threesegments-thefirst of length i t H uI ,u2)I thesecondof i ・ エ ィ

max(uI ,u2) Mュゥ オi u2)1andthethird of i ・ エ ィ I-max(uI ,u2). Whatis the ー イ ッ 「 。 「 ゥ ャ ケ that thesethreesegmentscanbe assembledinto a tri-angle?

Repeatthecomputationfor the 」 。 ウ セ キ ィ ・ イ セ キ セ ー ゥ セ ォ オ i オ ゥ ヲ ッ イ ュ ャ ケ ran-domly from [0,1] and エ ィ ・ l R オ ョ ゥ ヲ ッ イ

10.4 RANDOM permutations MQ

Considerestimatingthe probabilityof キゥョョゥ 。 game ッ ヲ 「 ャ 。 i asウオュゥョ QYエ ィ・ cardswere ウ エ Q オ ヲ ヲ ・ 、 ー ・ イ G ヲ ・ 」 エ ャ ケ オ ョ Q ゥ ヲ ッ イ ュ Q Q ケ 「 ・ ヲ ッ イ ・ 、 ・ 。 ャ ゥ i Q Y ィ 。 ョ ャ 、 S

。ョ Q、 ・カ・ イケ ヲ ッョ・ ・ゥ ウー ャ。 ケゥョ QY イ。エゥ ゥッ ョ。 QQ ケN Oneway to do this wouldbeto gen-eratea few raI1dompermutatiommdcomputethe chmcesof キ ィ ゥ ョ ァ

in eachcasewhereyou aredealtthe given cards. Hereit would be im-portmtthatthe processyou useto geI1eratea randompermutat1ORcangenerateanypermutationwith equalーイ ッ 「 。 「 ゥ ャ ゥ エ t ィ ゥ ウ canbe tricky.

Problem10.4: Does the following processyield a uniformly randompermutationof A? "For i サi R _ swap a キ ゥ エ ィ a randomlychosemelementof AH(The rmdomlychoseI1elemeI1tcouldbei itself.)

82

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

10.6 FORMING A TRIANGLE FROM RANDOM LENGTHS

alwayswant to get k packetsirrespectiveof the length (assumingeachsessionis longerthank).

Problem10.2: Computea randomsubsetof size k from a set of un-knownsizewhich is ーイ・ウ・ エ・、 asasequenceof elements.

10.3 ONLINE SAMPLING

Computea randomsubsetof size k from the integersin the interval{ P Q } ᄋ y ッ オ shouldretumtheresulth m arrayof leI1gthk.You mayuseonly 」 ッ ョ ウ エ Q エ additionalstorage.All subsetsshouldbeequallylikelyandall permutationsof thearrayshould「 ・ 。 ャ ャ ケ likely.

83

Supposewe createa random ー ・ イ ュ オ エ 。 エ ゥ ッ ヲ (1, 2,… ,n) as in Prob-lem 10.51 i.e'l eachpermutationhasequalprobability.

Problem 10.8: What is the ・ ク ー ・ 」 エ ・ 、 オ ュ 「 ・ イ of numbers that getmappedto themselves?What is the expectedlength of the largestin-creasingウ オ 「 ウ ・ ア オ ・ ョ 」 ・ ] H x ャ N N Xl) in a randomlychosenpermuta-エ ゥ ッ i whereXl is thefirst elementof thepermutationandXk ,k > 1 is thefirst elementthatis largerthanXk-l.

10.8 RANDOM PERMUTATIONS

10.9 UNIFORM RANDOM NUMBER GENERATION

Sometimesyoumaynothavetheperfectrandomnumbergeneratoryouneed.For examplel it would requirea bit of thinking to devisean algo-rithm to pick oneout of five ヲ イ ゥ ・ 、 ウ who getsto bethedesignateddriverby a coin flip suchthattheprocessis fair to everyone.

Problem10.9: How would you implementa randomnumbergenera-tor that generatesa randominteger 「 ・ エ キ ・ ・ ョ 。 ョ 、 bl given a randomnumbergeneratorthatproduceseitherzeroor onewith equalprobabil-

ケ N What would be the runtimeof this algorithml assumingeachcall tothegivenrandomnumbergeneratortakes0(1) time?

Supposeyou want to write a load test for your server. You looked attheinter-arrivaltime of requeststo your serveroveraperiodof oneyearandfrom this datayou havecomputeda histogramof the distribution

10.10 NONUNIFORM RANDOM NUMBER GENERATION

Canyoudeterminewhichof theabovetwo methodsof generatingUlandU2 is morelikely to producea trianglewithout computingtheexactprobabilities?

10.7 BALLS AND BINS

Supposeyouhaveキ ・ 「 serverstalkingto m clientssuchthateachclientpicks a serveruniformly at random.If you 、 ッ ッ エ endup wastingyourservercapacitYI this is a niceway of pairingserversto clientssinceyoudo not needto centralizeanything. But thereis a chancethat someofyourserversareidle while clientsarewaitingto beserved.How likely is

エィ。エ therewill beserversthatarenot doinganything?This problemisoftenmodeledusingballsandbins.

Problem10.7: If you throw m balls into n bins randomlYI how wouldyoucomputetheexpectednumberof bins thatdonothaveanyballs?

10.7. BALLSANDBINSCHAPTER10. PROBABILITY

10.5 RANDOM permutations MR

In Problem10.41we sawthatgeneratingrandompermutationsis not as

straightforwardasit seems.

Problem10.5: Designanalgorithmthatcreatesrandompermutationsof{I , R NNN スN Eachpermutationshouldbe ・ ア エ Z ャ ケ likely. You aregivenaraI1domIIUmber geI1eratioI1fUIICtiOIUuseasfew callsto it aspossible-

Supposeyou pick Wo IIumbersu1mdd uniformly rmdomlymdh-dependeI1tlyh the interval [0711·TheseIIumbers divide tEIe htervalinto threesegments-thefirst of length i t H uI ,u2)I thesecondof i ・ エ ィ

max(uI ,u2) Mュゥ オi u2)1andthethird of i ・ エ ィ I-max(uI ,u2). Whatis the ー イ ッ 「 。 「 ゥ ャ ケ that thesethreesegmentscanbe assembledinto a tri-angle?

Repeatthecomputationfor the 」 。 ウ セ キ ィ ・ イ セ キ セ ー ゥ セ ォ オ i オ ゥ ヲ ッ イ ュ ャ ケ ran-domly from [0,1] and エ ィ ・ l R オ ョ ゥ ヲ ッ イ

10.4 RANDOM permutations MQ

Considerestimatingthe probabilityof キゥョョゥ 。 game ッ ヲ 「 ャ 。 i asウオュゥョ QYエ ィ・ cardswere ウ エ Q オ ヲ ヲ ・ 、 ー ・ イ G ヲ ・ 」 エ ャ ケ オ ョ Q ゥ ヲ ッ イ ュ Q Q ケ 「 ・ ヲ ッ イ ・ 、 ・ 。 ャ ゥ i Q Y ィ 。 ョ ャ 、 S

。ョ Q、 ・カ・ イケ ヲ ッョ・ ・ゥ ウー ャ。 ケゥョ QY イ。エゥ ゥッ ョ。 QQ ケN Oneway to do this wouldbeto gen-eratea few raI1dompermutatiommdcomputethe chmcesof キ ィ ゥ ョ ァ

in eachcasewhereyou aredealtthe given cards. Hereit would be im-portmtthatthe processyou useto geI1eratea randompermutat1ORcangenerateanypermutationwith equalーイ ッ 「 。 「 ゥ ャ ゥ エ t ィ ゥ ウ canbe tricky.

Problem10.4: Does the following processyield a uniformly randompermutationof A? "For i サi R _ swap a キ ゥ エ ィ a randomlychosemelementof AH(The rmdomlychoseI1elemeI1tcouldbei itself.)

82

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

10.11 EXPECTEDNUMBER OF DICE ROLLS

Figure6. FINANCIALENGINEERING:anoxymoronwidely usedcirca 2008.

85

Problem10.14: Supposethe price of Jinglestock100daysin the futureis a normal randomvariablewith mean$300and deviation$20. Whatwouldbethefair priceof an ッ ー エ ゥ エ ッ buya singleshareof Jingleat$300in 100daysworth today?(Ignoretheimpactof interestrates.)

10.13 OPTIONPRICING WITH INTEREST

Considerthe sameproblemas Problem10.12, with the existenceof athird assetclass, a bond. A $1 bond pays$1.02 in 100 days. You canborrowmoneyat this rateor lendit at this rate.

Problem10.13: Showthereis auniquearbitrage-freepricefor theoptionandcomputethis price.

10.12. OPTIONPRICING-DISCRETECASE

10.14 OPTIONPRICING-…·…·CONTINUOUSCASE

Consideran option to buy a stock S that currentlytradesat $100. Theoptionis to buy the stockat $100in 100days.

Supposewe know thereare only two possibleoutcomes-Swill goto $120or to $70.

An arbitrageis a situationwhereyou can startwith a portfolio (x s

sharesandX o options)which hasnegativevalue (sinceyou areallowedto short sharesand sell options, both X s and X o maybe negative)andregardlessof themovementin the shareprice, theportfolio haspositivevalue.

For ・ ク i ・ if the option is priced at $26, we canmakemoneybybuyingonesharefor $100andsellingfour options-theinitial outlayonthe portfolio is 100 - 4 x 26 = -4. If the stockgoesup, our portfolio isworth 120- 20 x -4 = $80. If thestockgoesdown, theportfolio is worth$70. In eithercase, we makemoneywith no initial investment, i.e., theoptionpriceallowsfor anarbitrage.

Problem10.12: For what ッ ー エ ゥ ッ ー イ ゥ 」 ・ H ウ I arethereno opportunitiesforarbitrage?

caneitherbuy or sell the ッ ー エ ゥ ッ ゥ ョ 」 ッ オ ョ 」 エ ゥ ッ ョ with other transactionsandcomeup with a schemeof makingmoneyin a guaranteedfashion.A fair pricefor an ッ ー エ ゥ キ ッ オ ャ 、 bea pricesuchthatno arbitrageschemecanbedesignedaroundi t.

Wenowconsiderproblemsrelatedto determiningthefair pricefor anoptionfor a stock, giventhedistributionof thestockpricefor aperiodofエ ゥ ャ ・ ッ

10.12 OPTIONPRICING-DISCRETECASE

CHAPTER10. PROBABILITY

t f i ・ ヲ l m j [ m a セ | セ n ・ N N e ゥ ヲ a ァ

OfF セ 'fotJ CAN P セD

Q^ |セf エエ。n」e V Bi i エ Qセ

「 iカ aN |カ fヲ エQ ^U

Bob repeatedlyrolls an unbiased6-sideddice. He stopswhenhe hasrolled all the six numberson thedice. How manyrolls will it take, P ュaverage, for Bob to seeall thesix numbers?

G O P t a セ G ヲ ッ u r

A1> Dl'OONAL SMP>.If1AN t> ャn |i sゥ IN A「 セn i| キ Gゥ h aN

_ O^NNcks eッ

セhセセ ・N r iセcセ lNsヲ ヲ| n エ^ oセ

Optionpricing

A call optiongivesthe ownerthe right to buy something-ashare, abarrelof oil, an ounceof gold-ata predeterminedprice at a predeter-minedtime in thefuture. If the ッ ー エ ゥ ッ ゥ ウ not pricedfairly, anarbitrageur

of the inter-arrivaltime of requests.Now, in your load testyou want togeneraterequestsfor your serversuchthat the inter-arrivaltimescomefrom thesamedistributiOI1thatyouhaveseeniIIyour data.Howwouldyou generatetheseinter-arrival エ ・ ウ _

Problem10.10: Given the probabilitydistributionof a discreterandomvariableX anda uniform [0 ,1] randomnumber ァ ・ ョ ・ イ 。 エ ッ ィ ッ キ wouldyou generateinstancesof X thatfollow the givendistribution?

84

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

10.11 EXPECTEDNUMBER OF DICE ROLLS

Figure6. FINANCIALENGINEERING:anoxymoronwidely usedcirca 2008.

85

Problem10.14: Supposethe price of Jinglestock100daysin the futureis a normal randomvariablewith mean$300and deviation$20. Whatwouldbethefair priceof an ッ ー エ ゥ エ ッ buya singleshareof Jingleat$300in 100daysworth today?(Ignoretheimpactof interestrates.)

10.13 OPTIONPRICING WITH INTEREST

Considerthe sameproblemas Problem10.12, with the existenceof athird assetclass, a bond. A $1 bond pays$1.02 in 100 days. You canborrowmoneyat this rateor lendit at this rate.

Problem10.13: Showthereis auniquearbitrage-freepricefor theoptionandcomputethis price.

10.12. OPTIONPRICING-DISCRETECASE

10.14 OPTIONPRICING-…·…·CONTINUOUSCASE

Consideran option to buy a stock S that currentlytradesat $100. Theoptionis to buy the stockat $100in 100days.

Supposewe know thereare only two possibleoutcomes-Swill goto $120or to $70.

An arbitrageis a situationwhereyou can startwith a portfolio (x s

sharesandX o options)which hasnegativevalue (sinceyou areallowedto short sharesand sell options, both X s and X o maybe negative)andregardlessof themovementin the shareprice, theportfolio haspositivevalue.

For ・ ク i ・ if the option is priced at $26, we canmakemoneybybuyingonesharefor $100andsellingfour options-theinitial outlayonthe portfolio is 100 - 4 x 26 = -4. If the stockgoesup, our portfolio isworth 120- 20 x -4 = $80. If thestockgoesdown, theportfolio is worth$70. In eithercase, we makemoneywith no initial investment, i.e., theoptionpriceallowsfor anarbitrage.

Problem10.12: For what ッ ー エ ゥ ッ ー イ ゥ 」 ・ H ウ I arethereno opportunitiesforarbitrage?

caneitherbuy or sell the ッ ー エ ゥ ッ ゥ ョ 」 ッ オ ョ 」 エ ゥ ッ ョ with other transactionsandcomeup with a schemeof makingmoneyin a guaranteedfashion.A fair pricefor an ッ ー エ ゥ キ ッ オ ャ 、 bea pricesuchthatno arbitrageschemecanbedesignedaroundi t.

Wenowconsiderproblemsrelatedto determiningthefair pricefor anoptionfor a stock, giventhedistributionof thestockpricefor aperiodofエ ゥ ャ ・ ッ

10.12 OPTIONPRICING-DISCRETECASE

CHAPTER10. PROBABILITY

t f i ・ ヲ l m j [ m a セ | セ n ・ N N e ゥ ヲ a ァ

OfF セ 'fotJ CAN P セD

Q^ |セf エエ。n」e V Bi i エ Qセ

「 iカ aN |カ fヲ エQ ^U

Bob repeatedlyrolls an unbiased6-sideddice. He stopswhenhe hasrolled all the six numberson thedice. How manyrolls will it take, P ュaverage, for Bob to seeall thesix numbers?

G O P t a セ G ヲ ッ u r

A1> Dl'OONAL SMP>.If1AN t> ャn |i sゥ IN A「 セn i| キ Gゥ h aN

_ O^NNcks eッ

セhセセ ・N r iセcセ lNsヲ ヲ| n エ^ oセ

Optionpricing

A call optiongivesthe ownerthe right to buy something-ashare, abarrelof oil, an ounceof gold-ata predeterminedprice at a predeter-minedtime in thefuture. If the ッ ー エ ゥ ッ ゥ ウ not pricedfairly, anarbitrageur

of the inter-arrivaltime of requests.Now, in your load testyou want togeneraterequestsfor your serversuchthat the inter-arrivaltimescomefrom thesamedistributiOI1thatyouhaveseeniIIyour data.Howwouldyou generatetheseinter-arrival エ ・ ウ _

Problem10.10: Given the probabilitydistributionof a discreterandomvariableX anda uniform [0 ,1] randomnumber ァ ・ ョ ・ イ 。 エ ッ ィ ッ キ wouldyou generateinstancesof X thatfollow the givendistribution?

84

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

10.17 SELECTINGA RED CARD

10.18 SELECTINGTHE BEST SECRETARY

10.16 ONCE OR TWICE

8710.19. DIFFERENTIATINGBIASES

Problem10.18:Cmyoucomeupwith astrategythatresultsh yourse-ャ ・ ・ エ ィ ァ エ ィ ・ 「 m ュ ュ イ ケ キ ゥ エ ィ ー イ ッ 「 。 「 ゥ ャ ゥ エ ケ ァ ュ エ ュ ゥ _ w ィ 。 エ j m ・ ァ ケ

zesthe ー イ ッ 「 エ ケ of ウ ・ ャ ・ ァ the 「 ・ ウ エ イ j _

10.19 DIFFERENTIATING BIASES

Two cohsthat are ideI1tical h 。ーー・。イ。 」・ are placedin a black clothbag.Oneis biasedtowardsheads-itcomesup headswith probabi1ity0.6.The otherisbiasedtowardstails-itcomesupheadswithprobability0.4.Forbothcohsrtheoutcomesof successivetossesareindependeIIt.

probkm10.19:Youselecta cohat rmdomfrom thebagmd tossit5times.Itcomesupheads3thes-whatis theprobabilitythatit wasthecoin tkatwasbiasedtowardstails?Howmmytimesdoyouneedto tossthecoir1thatis biasedtowardstailsbefomitcomesupwith a majorityoftails with probabilitygreaterエ ィ 。 ョ _

10.20 THE COMPLEXITY OF AND-OR FORMULAS

s ーッウ・キ・キ。ョエエッ・カ。ャオ。エ・。ュクーイ・ウウゥッョッヲエィ・ヲッイュHa bIvHc dIャイwhere1\ andV are b ッ ッ ャ ・ 。 n d andOR イ ・ ウ ー ・ 」 ・ ャ ケ andA , B , C , D 。Boolearlvariables.ItiS I1aturdto uselazy maltiati071ri.e-rwheI1evaluat-ing A < BF if we evaluateA first andit evaluatesto falseFthm we skipevaluatingB.

w ・ i ャ o w 、 ・ ヲ ゥ ョ ャ ・ a restrictedsetof expressions:La expressions。 イ ゥ オ ウ Q S

b o ッャ・。ョ カ。イ 。イ ゥ。 「ャ・ウ[ 。 Q ・ク ーイ・ウ s ウゥッ ゥ sッヲエ ィ・ヲッイュ HH ᄁ。 ャ| I IvH H o QIIOF

w ィ・ イ ・ᄁ Q_ GiOj T Aj Q

in anLk expressionaredistinct.we wmt to desig1mdgorithmfor evahlatiI1gm LK expressionmd

want to miMIRize thenumberof variablesthat it reads.Wedo not carehowmuchtime thealgorithmspendstraversingthe ・ ク ー イ ・ ウ ウ ゥ ッ ョ N M

Problem10.20:Provethata determhisticalgorithm-oneh which thechoiceof themxt variableto readis a deterministicfumtiOI1of theval-tesreadso far-mustreada114kvariablesh the worst-case.Cmyouuesigrlarmdomizeddgorithmthat readsfewervariabIeson m aver-age, regardlessof thevaluesassignedto thevariables?

CHAPTER10. PROBABILITY

A deckof 52playingcardsis shuffled.Thedeckis placedface-downonatable.You aretrying to selecta redcard.You caneitherexamineor selectthecardthatis currentlyat the top of thedeck. If you chooseto examinethe top card, its value is revealedand it is set face- up. If you chooseto selectthe top card, the gameendsthere-youwin if you selecta redcardandloseif thecardis black. Onceyou examinea card, it cannotbeselected.If youhaveturnedover51 cards, youmustselectthelastcard.

Problem10.17: What is the strategythat optimizesthe likelihood ofyour selectinga facecard?

Supposeyou areto choosea secretaryfrom a pool of n secretarieswhoyou interviewin a randomorder.Givenanytwo secretaries, youcantellwho is 「 ・ エ エ ・ イ ャ 、 the "is better"relationshipis transitive. Onceyou in-terviewa secretary, you canselectherasyour secretaryandtheselectionprocessstops.Alternately, you canmoveon to thenextone (but cannotgobackto a previoussecretary).

Supposeyou are playing a gameagainsta dealer. In order to play thegame, youmustpay$1. The dealergetsa randomcardfrom a full deck.You are showna randomlyselectedcard from anotherfull deck. Youhavethechoiceof takingthecardor exchangingit for anothercardwhichis alsorandomlyselectedfrom a full deck. You win the gameif andonlyif the face value of your card is larger than that of dealer. If you win/you get 、 ッ ャ ャ 。 イ ウ N (The facevalueof an aceis 1; the facevaluesof Jack,Queen, andKing are12,13, and14/ respectively.)

Problem10.16: Whatwouldbethevalueof ウ オ 」 ィ thatit is a fair game,i.e., for a rational ー ャ 。 ケ ・ エ ィ ・ expectedgainis O.

10.15 OPTIMUM BIDDING

Consideran auctionfor an item in which the reserveprice is setby thesellertobearandomvariableX thatis uniformly distributedin therange[0,400]. You canplaceabid B; if yourbid is greaterthanor equalto thereserveprice, youwin the auctionandhaveto payB. You canthenselltheitemfor an80%markupoverwhatyoupaidfor i t.

Problem10.15: How muchshouldyou offer for theitem?

86

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

10.17 SELECTINGA RED CARD

10.18 SELECTINGTHE BEST SECRETARY

10.16 ONCE OR TWICE

8710.19. DIFFERENTIATINGBIASES

Problem10.18:Cmyoucomeupwith astrategythatresultsh yourse-ャ ・ ・ エ ィ ァ エ ィ ・ 「 m ュ ュ イ ケ キ ゥ エ ィ ー イ ッ 「 。 「 ゥ ャ ゥ エ ケ ァ ュ エ ュ ゥ _ w ィ 。 エ j m ・ ァ ケ

zesthe ー イ ッ 「 エ ケ of ウ ・ ャ ・ ァ the 「 ・ ウ エ イ j _

10.19 DIFFERENTIATING BIASES

Two cohsthat are ideI1tical h 。ーー・。イ。 」・ are placedin a black clothbag.Oneis biasedtowardsheads-itcomesup headswith probabi1ity0.6.The otherisbiasedtowardstails-itcomesupheadswithprobability0.4.Forbothcohsrtheoutcomesof successivetossesareindependeIIt.

probkm10.19:Youselecta cohat rmdomfrom thebagmd tossit5times.Itcomesupheads3thes-whatis theprobabilitythatit wasthecoin tkatwasbiasedtowardstails?Howmmytimesdoyouneedto tossthecoir1thatis biasedtowardstailsbefomitcomesupwith a majorityoftails with probabilitygreaterエ ィ 。 ョ _

10.20 THE COMPLEXITY OF AND-OR FORMULAS

s ーッウ・キ・キ。ョエエッ・カ。ャオ。エ・。ュクーイ・ウウゥッョッヲエィ・ヲッイュHa bIvHc dIャイwhere1\ andV are b ッ ッ ャ ・ 。 n d andOR イ ・ ウ ー ・ 」 ・ ャ ケ andA , B , C , D 。Boolearlvariables.ItiS I1aturdto uselazy maltiati071ri.e-rwheI1evaluat-ing A < BF if we evaluateA first andit evaluatesto falseFthm we skipevaluatingB.

w ・ i ャ o w 、 ・ ヲ ゥ ョ ャ ・ a restrictedsetof expressions:La expressions。 イ ゥ オ ウ Q S

b o ッャ・。ョ カ。イ 。イ ゥ。 「ャ・ウ[ 。 Q ・ク ーイ・ウ s ウゥッ ゥ sッヲエ ィ・ヲッイュ HH ᄁ。 ャ| I IvH H o QIIOF

w ィ・ イ ・ᄁ Q_ GiOj T Aj Q

in anLk expressionaredistinct.we wmt to desig1mdgorithmfor evahlatiI1gm LK expressionmd

want to miMIRize thenumberof variablesthat it reads.Wedo not carehowmuchtime thealgorithmspendstraversingthe ・ ク ー イ ・ ウ ウ ゥ ッ ョ N M

Problem10.20:Provethata determhisticalgorithm-oneh which thechoiceof themxt variableto readis a deterministicfumtiOI1of theval-tesreadso far-mustreada114kvariablesh the worst-case.Cmyouuesigrlarmdomizeddgorithmthat readsfewervariabIeson m aver-age, regardlessof thevaluesassignedto thevariables?

CHAPTER10. PROBABILITY

A deckof 52playingcardsis shuffled.Thedeckis placedface-downonatable.You aretrying to selecta redcard.You caneitherexamineor selectthecardthatis currentlyat the top of thedeck. If you chooseto examinethe top card, its value is revealedand it is set face- up. If you chooseto selectthe top card, the gameendsthere-youwin if you selecta redcardandloseif thecardis black. Onceyou examinea card, it cannotbeselected.If youhaveturnedover51 cards, youmustselectthelastcard.

Problem10.17: What is the strategythat optimizesthe likelihood ofyour selectinga facecard?

Supposeyou areto choosea secretaryfrom a pool of n secretarieswhoyou interviewin a randomorder.Givenanytwo secretaries, youcantellwho is 「 ・ エ エ ・ イ ャ 、 the "is better"relationshipis transitive. Onceyou in-terviewa secretary, you canselectherasyour secretaryandtheselectionprocessstops.Alternately, you canmoveon to thenextone (but cannotgobackto a previoussecretary).

Supposeyou are playing a gameagainsta dealer. In order to play thegame, youmustpay$1. The dealergetsa randomcardfrom a full deck.You are showna randomlyselectedcard from anotherfull deck. Youhavethechoiceof takingthecardor exchangingit for anothercardwhichis alsorandomlyselectedfrom a full deck. You win the gameif andonlyif the face value of your card is larger than that of dealer. If you win/you get 、 ッ ャ ャ 。 イ ウ N (The facevalueof an aceis 1; the facevaluesof Jack,Queen, andKing are12,13, and14/ respectively.)

Problem10.16: Whatwouldbethevalueof ウ オ 」 ィ thatit is a fair game,i.e., for a rational ー ャ 。 ケ ・ エ ィ ・ expectedgainis O.

10.15 OPTIMUM BIDDING

Consideran auctionfor an item in which the reserveprice is setby thesellertobearandomvariableX thatis uniformly distributedin therange[0,400]. You canplaceabid B; if yourbid is greaterthanor equalto thereserveprice, youwin the auctionandhaveto payB. You canthenselltheitemfor an80%markupoverwhatyoupaidfor i t.

Problem10.15: How muchshouldyou offer for theitem?

86

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter11

Progralllllllng

First, sincewe areprogrammers, wenaturallydesignedthesystemto makeiteasyto write, test, andrunprograms.

gd4ョョオ

-mMUュオ

e-tumュr

lo

クMゥajNmkyt‘LEb

34FDィゥ

Thefocusof thisbookis algorithmdesigI1problemsthatariseh softwarehterviews-HoweverbasicprogrammhgquestiomareaI1iIItegralpartof softwareinterviewsfor manycompanies.

You shouldbe readyto answerquestions 。 ョ ケ skill you claim onvourresume.In ー 。 イ エ ゥ 」 オ Q ャ 。 イ セ 、 ッ イ Q ッ エ キ イ ゥ エ ・ エ ィ 。 エ ケ ッ オ ォ ョ ッ キ ウ ッ ュ ョ ・ エ ィ ゥ ョ Q Y u i Q ャ ・ ウ ウ

o オ 。 イN ・ 」 ッ イ Q ヲ ゥ 、 ・ イ エ ィ 。 。 エ ケ ッ オ canar[ ョ Q ォョッキャ・、ァ ・ ッヲp ・ ・ イQGQ comesfrom cut g-and-pastingPerlcodefromth web to fM large duplicatefiksr ttm do mt include p ・ イ Q in ケ ッ オ r

・ ・ ィ ーエケ イュ ・ v イゥエゥカ ・ ・sjf。iュケ ウ ャゥョ ォ・、 ャゥ 。ウケューエッエゥ」 complexity, etc. Although itis likely that you arefamiliar with this materialrasyou will seeFit 」 。stillbe the sourceof challmghginterview problems-Thenwe classify

111y askedquestionsF:providerefereI1cesfor placesto readaboutthemr and give sampleproblems-Amore comprehmsivelist of suchproblemsis availableP エ ィ ・ companionwebsite.

Bit Fiddling

ャ ・ following problemsinvolve manipulationof bit-level data. Bit fid-

11.1. COMPUTINGTHEPARITYOFA LONG 89

dling questionsare often askedin interviewsand one importantthingto notehereis that it is very easyto introduce(andmiss)errorsin codethatmanipulatesbit-level data-whenyou play with bits, expectto getbitten.

11.1 COMPUTING THE PARITY OF A LONG

The parity of a sequenceof bits is 1 if thenumberof Is in the sequenceis odd; ッ エ ィ ・ イ イ ゥ ウ ・ it is O. By keepingthe parity of everyword of data,you cancheckfor singlebit errorsin storageor transmission.It is fairlystraightforwardto write a codethatcomputestheparityof a long.

Problem11.1: How would you computeparity if you hadto performthecomputationfor avery largenumberof longs?

11.2 REVERSINGTHE BITS IN A LONG

Thereareseveralvariantsof theparity problemposedabove, e.g., com-putingthenumberofbitssetto 1in a long. Hereis abit fiddling problemthatis concernedwith restructuring:

Problem11.2: Write a functionwhichtakesa longx andreturnsa longthathasthebits of x reversed.

11.3 RUN-LENGTH ENCODING

Considertheproblemof compressingblackandwhitebitmap ゥ イ 。 ァ ・ ウ N Aク m pixelblack-and-whiteimagecanberepresentedゥ ョ ク m/8bytes,where eachpixel is representedby a singlebit. If you know that theimageconsistsof largeblockswhich havethe samecolor, thenonewayto compresstheimageisbyjustcountingthe i ・ エ ィ of alternatesequenceof onesandzeroes.For example, 111111000011becomes6,4,2.

Problem11.3: How would you mostefficiently do run-lengthencodingon a largebit sequencerepresentedasabytearray?

Arrays

The simplestdata-structureis the array, which is a contiguousblock ofmemory.GivenanarrayA whichholdsn objects, A[i] denotesthei-th ob-jectstoredin thearray.Retrieving。 ョ 、 、 。 エ ゥ ョ ァ A[i] takesconstanttime.Howeverthe size of the arrayis fixed, which makesaddingmore thann objectsimpossible.Deletionof the objectat locationi canbehandledby havinganauxiliary Booleanassociatedwith the ャ ッ 」 。 エ ゥ ッ ゥ indicatingwhetherthe entry is valid or not. Insertionof anobjectinto an arrayof

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter11

Progralllllllng

First, sincewe areprogrammers, wenaturallydesignedthesystemto makeiteasyto write, test, andrunprograms.

gd4ョョオ

-mMUュオ

e-tumュr

lo

クMゥajNmkyt‘LEb

34FDィゥ

Thefocusof thisbookis algorithmdesigI1problemsthatariseh softwarehterviews-HoweverbasicprogrammhgquestiomareaI1iIItegralpartof softwareinterviewsfor manycompanies.

You shouldbe readyto answerquestions 。 ョ ケ skill you claim onvourresume.In ー 。 イ エ ゥ 」 オ Q ャ 。 イ セ 、 ッ イ Q ッ エ キ イ ゥ エ ・ エ ィ 。 エ ケ ッ オ ォ ョ ッ キ ウ ッ ュ ョ ・ エ ィ ゥ ョ Q Y u i Q ャ ・ ウ ウ

o オ 。 イN ・ 」 ッ イ Q ヲ ゥ 、 ・ イ エ ィ 。 。 エ ケ ッ オ canar[ ョ Q ォョッキャ・、ァ ・ ッヲp ・ ・ イQGQ comesfrom cut g-and-pastingPerlcodefromth web to fM large duplicatefiksr ttm do mt include p ・ イ Q in ケ ッ オ r

・ ・ ィ ーエケ イュ ・ v イゥエゥカ ・ ・sjf。iュケ ウ ャゥョ ォ・、 ャゥ 。ウケューエッエゥ」 complexity, etc. Although itis likely that you arefamiliar with this materialrasyou will seeFit 」 。stillbe the sourceof challmghginterview problems-Thenwe classify

111y askedquestionsF:providerefereI1cesfor placesto readaboutthemr and give sampleproblems-Amore comprehmsivelist of suchproblemsis availableP エ ィ ・ companionwebsite.

Bit Fiddling

ャ ・ following problemsinvolve manipulationof bit-level data. Bit fid-

11.1. COMPUTINGTHEPARITYOFA LONG 89

dling questionsare often askedin interviewsand one importantthingto notehereis that it is very easyto introduce(andmiss)errorsin codethatmanipulatesbit-level data-whenyou play with bits, expectto getbitten.

11.1 COMPUTING THE PARITY OF A LONG

The parity of a sequenceof bits is 1 if thenumberof Is in the sequenceis odd; ッ エ ィ ・ イ イ ゥ ウ ・ it is O. By keepingthe parity of everyword of data,you cancheckfor singlebit errorsin storageor transmission.It is fairlystraightforwardto write a codethatcomputestheparityof a long.

Problem11.1: How would you computeparity if you hadto performthecomputationfor avery largenumberof longs?

11.2 REVERSINGTHE BITS IN A LONG

Thereareseveralvariantsof theparity problemposedabove, e.g., com-putingthenumberofbitssetto 1in a long. Hereis abit fiddling problemthatis concernedwith restructuring:

Problem11.2: Write a functionwhichtakesa longx andreturnsa longthathasthebits of x reversed.

11.3 RUN-LENGTH ENCODING

Considertheproblemof compressingblackandwhitebitmap ゥ イ 。 ァ ・ ウ N Aク m pixelblack-and-whiteimagecanberepresentedゥ ョ ク m/8bytes,where eachpixel is representedby a singlebit. If you know that theimageconsistsof largeblockswhich havethe samecolor, thenonewayto compresstheimageisbyjustcountingthe i ・ エ ィ of alternatesequenceof onesandzeroes.For example, 111111000011becomes6,4,2.

Problem11.3: How would you mostefficiently do run-lengthencodingon a largebit sequencerepresentedasabytearray?

Arrays

The simplestdata-structureis the array, which is a contiguousblock ofmemory.GivenanarrayA whichholdsn objects, A[i] denotesthei-th ob-jectstoredin thearray.Retrieving。 ョ 、 、 。 エ ゥ ョ ァ A[i] takesconstanttime.Howeverthe size of the arrayis fixed, which makesaddingmore thann objectsimpossible.Deletionof the objectat locationi canbehandledby havinganauxiliary Booleanassociatedwith the ャ ッ 」 。 エ ゥ ッ ゥ indicatingwhetherthe entry is valid or not. Insertionof anobjectinto an arrayof

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

i・ ァエィョ」ュ behandledby allocathgamw arraywith additiORalmem-ory mdcopyMgovertheeI1triesfrom t21eoriginal array.Thismakestheworst-casetime of iIIsertiOI1highbut if theRew arrayhas, for ・ ク ー i ・twice thespaceoftheorighalarmy}tkeaveragetimefomsertIOI11Scon-ウ エ Q エ sincetheexpeElseof copyhgthearrayis infrequent-

Linked Lists

The next basic data-structurewe consideris the linked list. A singlylinked list is a data-structurethatcontainsa ウ・アオ・ 」・ of nodessuchthate chnodecORtainsm obiectmd a refezmceto themxt mdeh the list.Thefirst nodeis referredto astheheadandthelastnodeis referredto as

11.6 REVERSEALL THE WORDSIN A SENTENCE

GiveRastring CORtahklga setof wordsseparatedby white spacerwewould like to trmstormit to a strhghwtlich the wordsappearh theイ ・ イ ウ ・ order. For example, IIAlice likes b ッ 「 B B b ッ 「 likes Alice". Wedo not needto keepthe ッ イ ゥ ョ 。 ャ string.

Problem11.6: Implementa function for reversingthewordsin a stringthatis in-place, i.e., usesonly constantadditionalstorage.

91

ComplexityAnalysis

The runtime of an algorithm dependson the size of its input. One」 ッ ャ ヲ ヲ ゥ ッ ョ approachto capturethe runtimedependencyis by expressingasymptoticboundson the worst-caseruntime as a function of the in-put size. Specifically, the runtimeof an algorithmon an input of sizenis oHヲH II if for sufficiently largen, the runtimeis not more than f(n)timesa constant. Thebig-Onotationsimply indicatesanupperbound;if

・ runtimeis ーイッーッイエゥッ 。ャ to ヲ H エ ィ ・ complexityis writtenas X H ヲ H I I N

(Note that thebig-O notationis widely usedwhere8 is moreappropri-ate.)

11.7. REVERSINGA SINGLYLINKED LIST

11.9 DELETION FROM A SINGLY LINKED LIST

Givena nodein a singly linked list, deletingit in constanttime appearsimpossiblebecauseits predecessor'snext field hasto be updated.Sur-ー イ ゥ ウ ゥ ョ ァ ャ ゥ エ canbedonewith onesmallcaveat-thenodeto deletecannotbethelastonein thelist andit is easyto copythevaluepartof 。 ッ 、 ・ N

Problem11.9: Let v bea nodein a singly linked list. Node ゥ ウ not thetail; deleteit in 0(1) time.

11.8 CHECKING FOR CYCLICITY

While alinked list is supposedtobeasequenceof nodesendingin anull"it is possibleto introducea cycle in a linked list by makingthenextfieldof anelementreferenceto oneof theearliernodes.

Problem11.8: Givena referenceto theheadof a singly linked list, howwould you determinewhetherthis list endsin a null or reachesa cycleof nodes?(You donotknow thelengthof thelist.)

the tail; the tail's next field is a referenceto null. (Therearemanyvari-ants, e.g., in a doubly linked ャ ゥ ウ ・ 。 」 ィ nodehasa link to its predecessor;ウ ゥ ュ ゥ ャ 。 イ ャ 。 sentinelnodeor a self-loopcanbeusedin placeof null.)

11.7 REVERSINGA SINGLY LINKED LIST

Supposeyouweregivena singly linked list of integerssortedin ascend-ing orderandyou needto returna list with the elementssortedin de-scendingorder. Supposememoryis scarcebut you canreusenodesintheoriginallist.

Problem11.7: Give a linear-timenonrecursiveprocedurethat reversesa singly linked list. The procedureshoulduseno more than constantstoragebeyondthatneededfor the list itself.

CHAPTER11. PROGRAMMING

11.5 INVERT A PERMUTATION

Every Q M Q P エ ッ mappingis ゥョ イ・イエゥ「ャ・ i.e., if f is 1-1 onto, thenthereexistsa uniquefunction....j-l-such that f-l(f(x)) = x. iョ ケ。イエゥ」オャ。 ヲッイ anyー ・ イ ュ 。 エ ゥ ッ ョ II , thereexistsauniqueー ・ イ ュ 。 エ ゥ ッ ョ II-I thatis theinverse

of II.Givena ー・イュオエ。エゥッ イ・ーイ・ウ・ョエ・、 セケ anarrayA, you 」 セ computeits

inverseB by simplyassigningB {}j = i for all valuesof i

Problem11.5: GivenanarrayA of ints representinga permutationII ,updateA to representII- 1using P ャ ケ constantadditionalstorage.

11.4 PERMUTING THE ELEMENTS OF AN ARRAY

A permutationof i ・ エ ィ n is a 1-1 ontomapping1r from {O , Q WMiスtoitself We cm イ ・ ー イ ・ ウ ・ 。 ー ・ イ ュ オ エ 。 エ ゥ ッ 。 ョ array i i Z i i { ゥ } =HゥIᄋa permutatiORcmbeappliedto m arrayA of ・ ャ ・ ュ ・ ョ エ ウ Z II(A) is

defined-by II H a I = A [II[i ]J. aーーャケゥ 。 permutationto a givenarrayis easyif you ィ 。 セ セ additionalstorageto write theresultingarray.

Problem11.4: GivenanarrayA of integersanda ー・イュオエ。エゥ ii com-puteII(A) usingonly constantadditionalstorage.

90

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

i・ ァエィョ」ュ behandledby allocathgamw arraywith additiORalmem-ory mdcopyMgovertheeI1triesfrom t21eoriginal array.Thismakestheworst-casetime of iIIsertiOI1highbut if theRew arrayhas, for ・ ク ー i ・twice thespaceoftheorighalarmy}tkeaveragetimefomsertIOI11Scon-ウ エ Q エ sincetheexpeElseof copyhgthearrayis infrequent-

Linked Lists

The next basic data-structurewe consideris the linked list. A singlylinked list is a data-structurethatcontainsa ウ・アオ・ 」・ of nodessuchthate chnodecORtainsm obiectmd a refezmceto themxt mdeh the list.Thefirst nodeis referredto astheheadandthelastnodeis referredto as

11.6 REVERSEALL THE WORDSIN A SENTENCE

GiveRastring CORtahklga setof wordsseparatedby white spacerwewould like to trmstormit to a strhghwtlich the wordsappearh theイ ・ イ ウ ・ order. For example, IIAlice likes b ッ 「 B B b ッ 「 likes Alice". Wedo not needto keepthe ッ イ ゥ ョ 。 ャ string.

Problem11.6: Implementa function for reversingthewordsin a stringthatis in-place, i.e., usesonly constantadditionalstorage.

91

ComplexityAnalysis

The runtime of an algorithm dependson the size of its input. One」 ッ ャ ヲ ヲ ゥ ッ ョ approachto capturethe runtimedependencyis by expressingasymptoticboundson the worst-caseruntime as a function of the in-put size. Specifically, the runtimeof an algorithmon an input of sizenis oHヲH II if for sufficiently largen, the runtimeis not more than f(n)timesa constant. Thebig-Onotationsimply indicatesanupperbound;if

・ runtimeis ーイッーッイエゥッ 。ャ to ヲ H エ ィ ・ complexityis writtenas X H ヲ H I I N

(Note that thebig-O notationis widely usedwhere8 is moreappropri-ate.)

11.7. REVERSINGA SINGLYLINKED LIST

11.9 DELETION FROM A SINGLY LINKED LIST

Givena nodein a singly linked list, deletingit in constanttime appearsimpossiblebecauseits predecessor'snext field hasto be updated.Sur-ー イ ゥ ウ ゥ ョ ァ ャ ゥ エ canbedonewith onesmallcaveat-thenodeto deletecannotbethelastonein thelist andit is easyto copythevaluepartof 。 ッ 、 ・ N

Problem11.9: Let v bea nodein a singly linked list. Node ゥ ウ not thetail; deleteit in 0(1) time.

11.8 CHECKING FOR CYCLICITY

While alinked list is supposedtobeasequenceof nodesendingin anull"it is possibleto introducea cycle in a linked list by makingthenextfieldof anelementreferenceto oneof theearliernodes.

Problem11.8: Givena referenceto theheadof a singly linked list, howwould you determinewhetherthis list endsin a null or reachesa cycleof nodes?(You donotknow thelengthof thelist.)

the tail; the tail's next field is a referenceto null. (Therearemanyvari-ants, e.g., in a doubly linked ャ ゥ ウ ・ 。 」 ィ nodehasa link to its predecessor;ウ ゥ ュ ゥ ャ 。 イ ャ 。 sentinelnodeor a self-loopcanbeusedin placeof null.)

11.7 REVERSINGA SINGLY LINKED LIST

Supposeyouweregivena singly linked list of integerssortedin ascend-ing orderandyou needto returna list with the elementssortedin de-scendingorder. Supposememoryis scarcebut you canreusenodesintheoriginallist.

Problem11.7: Give a linear-timenonrecursiveprocedurethat reversesa singly linked list. The procedureshoulduseno more than constantstoragebeyondthatneededfor the list itself.

CHAPTER11. PROGRAMMING

11.5 INVERT A PERMUTATION

Every Q M Q P エ ッ mappingis ゥョ イ・イエゥ「ャ・ i.e., if f is 1-1 onto, thenthereexistsa uniquefunction....j-l-such that f-l(f(x)) = x. iョ ケ。イエゥ」オャ。 ヲッイ anyー ・ イ ュ 。 エ ゥ ッ ョ II , thereexistsauniqueー ・ イ ュ 。 エ ゥ ッ ョ II-I thatis theinverse

of II.Givena ー・イュオエ。エゥッ イ・ーイ・ウ・ョエ・、 セケ anarrayA, you 」 セ computeits

inverseB by simplyassigningB {}j = i for all valuesof i

Problem11.5: GivenanarrayA of ints representinga permutationII ,updateA to representII- 1using P ャ ケ constantadditionalstorage.

11.4 PERMUTING THE ELEMENTS OF AN ARRAY

A permutationof i ・ エ ィ n is a 1-1 ontomapping1r from {O , Q WMiスtoitself We cm イ ・ ー イ ・ ウ ・ 。 ー ・ イ ュ オ エ 。 エ ゥ ッ 。 ョ array i i Z i i { ゥ } =HゥIᄋa permutatiORcmbeappliedto m arrayA of ・ ャ ・ ュ ・ ョ エ ウ Z II(A) is

defined-by II H a I = A [II[i ]J. aーーャケゥ 。 permutationto a givenarrayis easyif you ィ 。 セ セ additionalstorageto write theresultingarray.

Problem11.4: GivenanarrayA of integersanda ー・イュオエ。エゥ ii com-puteII(A) usingonly constantadditionalstorage.

90

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

11.10 BINARY SEARCH

Binarysearch, whichis the ウ オ ・ 」 エ of anumberof problemsin Chapter1,is a techniquefor searchingfor a givenkey in a sortedarray.

Problem11.10: What is thetime complexityof the ヲ ッ ャ ャ ッ キ ゥ ョ ァ ー ャ ・ ュ ・ ョ ᆳ

tationof binarysearch?

1 I boolean search(array AI int K) {2 I if (A. size() == 0)3 I return false;4 I5 I if (A. size() == 1)6 I return (A[O] == K) ;7 I8 lint m = A.size()/2;9 I

10 I return (A[m] == K) ? true11 I ( (A[m] < K) ?12 I search(A[m+ 1: :A. size() ] I K)13 I search(A[ 0: :m], K)14 I15

Generallyspeaking, if an algorithmhasa runtime that is a polyno-mial, i.e., O(nk ) for somefixed k, where ゥ ウ the size of the input, it isconsideredto beefficient; otherwise, it is inefficient. Notableexceptionsexist-forexample, thesimplexalgorithmfor linearprogrammingis notpolynomialbutworksverywell in practice;theAKS primality checkingalgorithmis polynomialbuthasahigh k.

As an example, searchingan unsortedarrayof integersof ャ ・ ョ ァ エ ィfor a ァゥカ・ ゥョエ・ァ・イ hasan asymptoticcomplexity of X H I sincein theworst-case, the givenintegermaynotbepresent.

Similarly, considerthe naIve algorithmfor ー イ ゥ ュ 。 エ ケ which tries allnumbersfrom 2 to the squareroot of the input. What is its complexity?In thebestcase, the input is divisible by 2. Howeverin the worst-case,the input maybea prime, so the algorithmperforms M i ゥ エ ・ イ 。 エ ゥ ッ ョ ウ N Fur-thermore, since the number イ ・ ア オ ゥ イ ・ ウ only log2 n bits to encode, thisalgorithm's complexityis actually ・ ク ー ッ ・ ョ エ ゥ 。 ャ in thesizeof theinput.

As a rule, algorithmsshouldbe designedwith the goal of reducingthe worst-casecomplexityratherthanaverage-casecomplexityfor sev-eral イ ・ 。 ウ ッ ョ ウ H Q N I it is very difficult to definemeaningfuldistributionsontheinputs, (2.) pathologicalinputsaremorelikely thanstatisticalmodelsmay predict (for example, worst-caseinput for a naIve implementationof Quicksortis onewhereall entriesarethesame, which is not at all un-likely), and(3.) malicioususersmayexploitbadworst-caseperformanceto createdenial-of-serviceattacks.

9311.11. PROGRAMMINGLANGUAGES

It is not uncommoninセッョ m somecompaniesto quiz the candidatesabouttheir knowledgeof computersciencedirectly ratherthan askingthemto solveprobkIRS-Intke restof this chapteIJ, we covera numberof armeasmdprovidea list of questionsthatcmhelp a cmdidatepreparef

ch an interview.Theanswersto thesequestionscaI1beeasi1yfOUI1din standardエ ・ ッ ッ ォ ウ for thatfield. Henceinsteadof ー イ ッ カ ゥ 、 ゥ 。 ョ ウ キ ・ イ ウto thesequest1onswe pohtour readersto textbooksthatwe cOI1sideragoodreferencefor thatfield.

11.11 PROGRAMMING LANGUAGES

Basics

w ・ ォ ・ TheC Progro771771i?1gLanguageby kemighmandRitchiefor C?for Java, Java Preciselyby s ッ ヲ エ coversthe corelanguagemd1ibmiejsuccinctly.

-Whatarethetypesh Cr JavaFC++r andPer-l?Howmanybits doesエ 。 ォ ・ to representthebasictypes?

v ィ 。 エ 、 ッ ヲ ャ ッ o 。 ー ッ ゥ ョ エ エ ョ オ ュ ュ Q G ウ QQ P ォ ・ in i i o イュ S ケ イ _ s p ・ 」 ゥ ヲ ゥィ ッ キ ュ 。 ケ 「 ゥ エ s aTethereimdoubleaMwhatsequencetothebitsappearml

w ィ 。 エ is two's-complementnotation?- How wouldyou implementabit-vectorclass?

d ッ ・ ウ thecheckx == x + 1 alwaysreturnfalsefor integerx?-matdoesa cmuctlookk h memory?What ・ ウ a C++

object Q P ャ ゥ ォ ・ in memory? What doesa Javaobjectlook like inmemory(

キ ィ エ ィ ・ 、 ゥ ヲ ヲ ・ イ ・ ョ イ ー イ ・ エ ・ 、 anda compiled Qguage(

w ィ 。 エ is garbagecollection?

- How would you determineif call stackgrowsup or downrelativeto memoryaddresses?

- Give 。 ョ ・ ク ー i ・ of a memoryleakin Java.w ィ 。 エ ゥ ウ エ 。 ゥ Q イ ・ 」 オ イ ウ ゥ ッ イ Q _ h ッ キ 」 。 ョ 、 エ beremovedautomatically?i ウ エ ィ ・ ョ 。 エ オ イ ・ ・ 」 オ オ イ ウ ィ v ・ ー イ N

y ッ オ イ ュ 。 i ャ 。 ァ ・ イ readsan online article エ ィ 。 エ ウ 。 ケ ウ ゥ エ ゥ ウ lOx ヲ 。 ウ エ ・ イ エ Ocodein Pythonthanin c K N He wantsyou to codeexclusivelyinPythonfrom now on. Whatwouldyou sayto him?What doesanexecutablelook like asa sequenceof bytes?

Libraries

A programmerキ ッ イ ・ ・ ァ オ Q 。 イ ャG Q Q ケ ゥ ュ i Q ー i ・ i ュ ・。 ウ KMP string ュ ャ 。 エ 」 ィ ゥ ョ ァ P イ d ゥ ェ ォ ウ エ イ 。 G ウ ウ ィ ッ イ エ ・ ウ エ ー 。 エ ィ 」 ッ ュ ー オ エ 。 エ ゥ ッ ョ quickly

CHAPTER11. PROGRAMMING92

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

11.10 BINARY SEARCH

Binarysearch, whichis the ウ オ ・ 」 エ of anumberof problemsin Chapter1,is a techniquefor searchingfor a givenkey in a sortedarray.

Problem11.10: What is thetime complexityof the ヲ ッ ャ ャ ッ キ ゥ ョ ァ ー ャ ・ ュ ・ ョ ᆳ

tationof binarysearch?

1 I boolean search(array AI int K) {2 I if (A. size() == 0)3 I return false;4 I5 I if (A. size() == 1)6 I return (A[O] == K) ;7 I8 lint m = A.size()/2;9 I

10 I return (A[m] == K) ? true11 I ( (A[m] < K) ?12 I search(A[m+ 1: :A. size() ] I K)13 I search(A[ 0: :m], K)14 I15

Generallyspeaking, if an algorithmhasa runtime that is a polyno-mial, i.e., O(nk ) for somefixed k, where ゥ ウ the size of the input, it isconsideredto beefficient; otherwise, it is inefficient. Notableexceptionsexist-forexample, thesimplexalgorithmfor linearprogrammingis notpolynomialbutworksverywell in practice;theAKS primality checkingalgorithmis polynomialbuthasahigh k.

As an example, searchingan unsortedarrayof integersof ャ ・ ョ ァ エ ィfor a ァゥカ・ ゥョエ・ァ・イ hasan asymptoticcomplexity of X H I sincein theworst-case, the givenintegermaynotbepresent.

Similarly, considerthe naIve algorithmfor ー イ ゥ ュ 。 エ ケ which tries allnumbersfrom 2 to the squareroot of the input. What is its complexity?In thebestcase, the input is divisible by 2. Howeverin the worst-case,the input maybea prime, so the algorithmperforms M i ゥ エ ・ イ 。 エ ゥ ッ ョ ウ N Fur-thermore, since the number イ ・ ア オ ゥ イ ・ ウ only log2 n bits to encode, thisalgorithm's complexityis actually ・ ク ー ッ ・ ョ エ ゥ 。 ャ in thesizeof theinput.

As a rule, algorithmsshouldbe designedwith the goal of reducingthe worst-casecomplexityratherthanaverage-casecomplexityfor sev-eral イ ・ 。 ウ ッ ョ ウ H Q N I it is very difficult to definemeaningfuldistributionsontheinputs, (2.) pathologicalinputsaremorelikely thanstatisticalmodelsmay predict (for example, worst-caseinput for a naIve implementationof Quicksortis onewhereall entriesarethesame, which is not at all un-likely), and(3.) malicioususersmayexploitbadworst-caseperformanceto createdenial-of-serviceattacks.

9311.11. PROGRAMMINGLANGUAGES

It is not uncommoninセッョ m somecompaniesto quiz the candidatesabouttheir knowledgeof computersciencedirectly ratherthan askingthemto solveprobkIRS-Intke restof this chapteIJ, we covera numberof armeasmdprovidea list of questionsthatcmhelp a cmdidatepreparef

ch an interview.Theanswersto thesequestionscaI1beeasi1yfOUI1din standardエ ・ ッ ッ ォ ウ for thatfield. Henceinsteadof ー イ ッ カ ゥ 、 ゥ 。 ョ ウ キ ・ イ ウto thesequest1onswe pohtour readersto textbooksthatwe cOI1sideragoodreferencefor thatfield.

11.11 PROGRAMMING LANGUAGES

Basics

w ・ ォ ・ TheC Progro771771i?1gLanguageby kemighmandRitchiefor C?for Java, Java Preciselyby s ッ ヲ エ coversthe corelanguagemd1ibmiejsuccinctly.

-Whatarethetypesh Cr JavaFC++r andPer-l?Howmanybits doesエ 。 ォ ・ to representthebasictypes?

v ィ 。 エ 、 ッ ヲ ャ ッ o 。 ー ッ ゥ ョ エ エ ョ オ ュ ュ Q G ウ QQ P ォ ・ in i i o イュ S ケ イ _ s p ・ 」 ゥ ヲ ゥィ ッ キ ュ 。 ケ 「 ゥ エ s aTethereimdoubleaMwhatsequencetothebitsappearml

w ィ 。 エ is two's-complementnotation?- How wouldyou implementabit-vectorclass?

d ッ ・ ウ thecheckx == x + 1 alwaysreturnfalsefor integerx?-matdoesa cmuctlookk h memory?What ・ ウ a C++

object Q P ャ ゥ ォ ・ in memory? What doesa Javaobjectlook like inmemory(

キ ィ エ ィ ・ 、 ゥ ヲ ヲ ・ イ ・ ョ イ ー イ ・ エ ・ 、 anda compiled Qguage(

w ィ 。 エ is garbagecollection?

- How would you determineif call stackgrowsup or downrelativeto memoryaddresses?

- Give 。 ョ ・ ク ー i ・ of a memoryleakin Java.w ィ 。 エ ゥ ウ エ 。 ゥ Q イ ・ 」 オ イ ウ ゥ ッ イ Q _ h ッ キ 」 。 ョ 、 エ beremovedautomatically?i ウ エ ィ ・ ョ 。 エ オ イ ・ ・ 」 オ オ イ ウ ィ v ・ ー イ N

y ッ オ イ ュ 。 i ャ 。 ァ ・ イ readsan online article エ ィ 。 エ ウ 。 ケ ウ ゥ エ ゥ ウ lOx ヲ 。 ウ エ ・ イ エ Ocodein Pythonthanin c K N He wantsyou to codeexclusivelyinPythonfrom now on. Whatwouldyou sayto him?What doesanexecutablelook like asa sequenceof bytes?

Libraries

A programmerキ ッ イ ・ ・ ァ オ Q 。 イ ャG Q Q ケ ゥ ュ i Q ー i ・ i ュ ・。 ウ KMP string ュ ャ 。 エ 」 ィ ゥ ョ ァ P イ d ゥ ェ ォ ウ エ イ 。 G ウ ウ ィ ッ イ エ ・ ウ エ ー 。 エ ィ 」 ッ ュ ー オ エ 。 エ ゥ ッ ョ quickly

CHAPTER11. PROGRAMMING92

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

will not advancevery far. Solutionsto suchproblemsare well-knownand havehigh quality, thoroughlytested, and debuggedimplementa-tions, often availableas opensource. Programmersshouldknow andusetheselibraries.

g ゥ カ ・ anexampleof a functionwhich is in theC standardlibrary.- Give anexampleof a commonlyusedfunctionwhich ゥ ウ ッ エ in the

C standardlibrary.- What library wouldyouuseto determinethecurrentdatein Java?- What library would you use in Javaif you had to implementa

tinyurl service?h ッ キ doesSTL implementsets?

- How doesthelibrary codecomputetrigonometricfunctions?t ィ ・ strtok(char J char *s2) function in the C standardlibrary successivelyreturnsoccurrencesof the charactersin s2 instring s1; it returnsnull if thereareno more occurrences.Whatmakesthis a dangerousfunction to use in a multithreadedpro-gram?

11.12 DEBUGGING AND TESTING

Debuggingandtestingaretopicswhich arenot usuallythe focusof uni-versity teaching.We highly recommendThe Practiceof p イ ッ ァ イ 。 ュ ュ ァ 「 ケ

KernighanandPike, whichteachesmuchmorethanjustwriting code-itcoverstesting, debugging, ーッイエ。「ゥャゥエ ー・イヲッイュ。ョ」・ anddesignalterna-エ ゥ イ ・ ウ N

- Whatwasyour lastbug?Whatwasyourhardestbug?- How would you debuga distributedprogram?a programworkssometimesandfails othertimes-why?a programworks sometimesand fails other times on the exactsameinput-why?

- How would you determinewhere a programspendsmost of itstime?

- How doesJUnit maketheprocessof testingeasier?l ゥ ウ エ five waysin which C codecanbenonportable.What canyoudo to makethecodeportable?

- Write testsfor ゥ ュ ー ャ ・ ュ ・ エ 。 エ ゥ ッ ヲ an isupperfunction.- Shouldyou testprivatemethods?Shouldyou testoneline meth-

ods?- If you ヲ Q 、 andfix an errorby addingdebugcode, shouldyou re-

movethe debugcodeafterwards?Shouldyou leavethemin witha conditional」 ッ ュ ー ゥ ャ 。 エ ゥ ヲ ャ 。 ァ or with a runtimeflag?

- What is abuffer overflow andhow canhackersexploit it?- How canyou useValgrind to solvesegfaultproblems?- How doesValgrind catchaccessuninitializedmemory?

11.15 OPERATINGSYSTEMS

9511.13. BESTPRACTICES

11.13 BEST PRACTICES

Ourfavoritebestpracticesbookis e 」 エ ゥ カ ・ Javaby Bloch-it coversmany

Z R コ ゥ 」1, c ッョ」オイイ・ョ」ケ セ and ァ ・ ョ ・ イ ゥ 」 ウ arejust a few examples.e ェ ctiveC++ by

Meyeris highly thoughtof for C++. d ・ ウ ョ p 。 エ エ ・ イ ウ Z ElementsofReusableObject-Orientedsセ コエケ。イ・ by Gammaet al.is a very popularintJoductiOIIto patterns.

g ゥ カ ・ an?xmpleof a ーイ ャ・ュ you solvedwhereyou madegoode of object-orientedprogramming.

w ィ 。 エ is thefactorypattern?What is the publish-subscribemodel?Q カ ・ m exampleof how iIIEMIttanceviolatesmcapsulation-

w ィ 。 エ doJavaboundedwildcardsbuyyou?w ィ ケ should you dways override the equalsmd hashfuI1dior1methodsfor Javaclasses?

11.14 PROGRAMMING LANGUAGE IMPLEMENTATION

we recommeIIdprogm771771inLanguagesィ ァュ。エゥ」ウ by Scott-it coversコ エ ケ 。 ョ 、 」 エ ゥ 」 ・ ッ ヲ イ 。 ュ ュ ゥ イ 。 ァ ・ ウ 。 ウ カ ・ イ M エ ッ M

g ゥ カ ・ m exampleofdanguagewhichcamothparsedbyaI1ycom-puter

-WhatproblemsdoesdyI1amic1inkagesolve?Whatproblemsdoesit introduce?

- What is a ヲオョ」エゥッョ。ャャ。ョ 。ァ・_

wィ 。エ is a virtual function?h ッ キ is methoddispatchimplementedin Java?w ィ 。 エ ゥ ウ 。 オ Q エ ッ ュ ュ 。 Q 。 。 イ 「 。 c P Q Q ・ 」 エ ゥ ッ ョ i Q ィ ッ ュ

w ィ 。 エ ゥ ウ 。 エ ケ ー ・ M ウ 。 ヲ G ・ ャ 。 ョ Q Y エ エ 。 ァ ・ ャ ?キO ィ。エ ゥ。 。エ ゥゥウ エィ・ Q ・、 ゥ ヲヲ N G・ョ ョ W ・・ョ 。ャ イ。ョ 、。ー 。 イウ・

ォTコ Z amn ュ p i・ ャ。 i

gゥカ・ 。ョ Q・ 。ュー i・ of a i。ョ QY オ 。ァ ・キィゥ」ィ」。 ョョ ョッo エ「 ・ー。 イウ ・、 by aparser.

Modernopemtingsystemsby TaI1eIIbaumis widelyused;oneof its clamsエ ッ ュ ・ ゥ ウ l ゥ ョ オ ク キ 。 ウ 、 ッ ー ・ 、 from theMinix OSdevelopedin an

lier versionof thisbook.

- What is a systemcall?- How is a systemcall differentfrom a library call?

CHAPTER11. PROGRAMMING94

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

will not advancevery far. Solutionsto suchproblemsare well-knownand havehigh quality, thoroughlytested, and debuggedimplementa-tions, often availableas opensource. Programmersshouldknow andusetheselibraries.

g ゥ カ ・ anexampleof a functionwhich is in theC standardlibrary.- Give anexampleof a commonlyusedfunctionwhich ゥ ウ ッ エ in the

C standardlibrary.- What library wouldyouuseto determinethecurrentdatein Java?- What library would you use in Javaif you had to implementa

tinyurl service?h ッ キ doesSTL implementsets?

- How doesthelibrary codecomputetrigonometricfunctions?t ィ ・ strtok(char J char *s2) function in the C standardlibrary successivelyreturnsoccurrencesof the charactersin s2 instring s1; it returnsnull if thereareno more occurrences.Whatmakesthis a dangerousfunction to use in a multithreadedpro-gram?

11.12 DEBUGGING AND TESTING

Debuggingandtestingaretopicswhich arenot usuallythe focusof uni-versity teaching.We highly recommendThe Practiceof p イ ッ ァ イ 。 ュ ュ ァ 「 ケ

KernighanandPike, whichteachesmuchmorethanjustwriting code-itcoverstesting, debugging, ーッイエ。「ゥャゥエ ー・イヲッイュ。ョ」・ anddesignalterna-エ ゥ イ ・ ウ N

- Whatwasyour lastbug?Whatwasyourhardestbug?- How would you debuga distributedprogram?a programworkssometimesandfails othertimes-why?a programworks sometimesand fails other times on the exactsameinput-why?

- How would you determinewhere a programspendsmost of itstime?

- How doesJUnit maketheprocessof testingeasier?l ゥ ウ エ five waysin which C codecanbenonportable.What canyoudo to makethecodeportable?

- Write testsfor ゥ ュ ー ャ ・ ュ ・ エ 。 エ ゥ ッ ヲ an isupperfunction.- Shouldyou testprivatemethods?Shouldyou testoneline meth-

ods?- If you ヲ Q 、 andfix an errorby addingdebugcode, shouldyou re-

movethe debugcodeafterwards?Shouldyou leavethemin witha conditional」 ッ ュ ー ゥ ャ 。 エ ゥ ヲ ャ 。 ァ or with a runtimeflag?

- What is abuffer overflow andhow canhackersexploit it?- How canyou useValgrind to solvesegfaultproblems?- How doesValgrind catchaccessuninitializedmemory?

11.15 OPERATINGSYSTEMS

9511.13. BESTPRACTICES

11.13 BEST PRACTICES

Ourfavoritebestpracticesbookis e 」 エ ゥ カ ・ Javaby Bloch-it coversmany

Z R コ ゥ 」1, c ッョ」オイイ・ョ」ケ セ and ァ ・ ョ ・ イ ゥ 」 ウ arejust a few examples.e ェ ctiveC++ by

Meyeris highly thoughtof for C++. d ・ ウ ョ p 。 エ エ ・ イ ウ Z ElementsofReusableObject-Orientedsセ コエケ。イ・ by Gammaet al.is a very popularintJoductiOIIto patterns.

g ゥ カ ・ an?xmpleof a ーイ ャ・ュ you solvedwhereyou madegoode of object-orientedprogramming.

w ィ 。 エ is thefactorypattern?What is the publish-subscribemodel?Q カ ・ m exampleof how iIIEMIttanceviolatesmcapsulation-

w ィ 。 エ doJavaboundedwildcardsbuyyou?w ィ ケ should you dways override the equalsmd hashfuI1dior1methodsfor Javaclasses?

11.14 PROGRAMMING LANGUAGE IMPLEMENTATION

we recommeIIdprogm771771inLanguagesィ ァュ。エゥ」ウ by Scott-it coversコ エ ケ 。 ョ 、 」 エ ゥ 」 ・ ッ ヲ イ 。 ュ ュ ゥ イ 。 ァ ・ ウ 。 ウ カ ・ イ M エ ッ M

g ゥ カ ・ m exampleofdanguagewhichcamothparsedbyaI1ycom-puter

-WhatproblemsdoesdyI1amic1inkagesolve?Whatproblemsdoesit introduce?

- What is a ヲオョ」エゥッョ。ャャ。ョ 。ァ・_

wィ 。エ is a virtual function?h ッ キ is methoddispatchimplementedin Java?w ィ 。 エ ゥ ウ 。 オ Q エ ッ ュ ュ 。 Q 。 。 イ 「 。 c P Q Q ・ 」 エ ゥ ッ ョ i Q ィ ッ ュ

w ィ 。 エ ゥ ウ 。 エ ケ ー ・ M ウ 。 ヲ G ・ ャ 。 ョ Q Y エ エ 。 ァ ・ ャ ?キO ィ。エ ゥ。 。エ ゥゥウ エィ・ Q ・、 ゥ ヲヲ N G・ョ ョ W ・・ョ 。ャ イ。ョ 、。ー 。 イウ・

ォTコ Z amn ュ p i・ ャ。 i

gゥカ・ 。ョ Q・ 。ュー i・ of a i。ョ QY オ 。ァ ・キィゥ」ィ」。 ョョ ョッo エ「 ・ー。 イウ ・、 by aparser.

Modernopemtingsystemsby TaI1eIIbaumis widelyused;oneof its clamsエ ッ ュ ・ ゥ ウ l ゥ ョ オ ク キ 。 ウ 、 ッ ー ・ 、 from theMinix OSdevelopedin an

lier versionof thisbook.

- What is a systemcall?- How is a systemcall differentfrom a library call?

CHAPTER11. PROGRAMMING94If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

11.16 TOOLS

Building andmaintainingprogramsThereis a paucityof booksonprogrammhgtools;OI1ebookwehave

usedis EssentialOpenSourceToolsetby Zeller andKrinke.

w ィ 。 エ versioncontrolsystemdo youuse?_ What coveragetool do youuse?_ Whatbuild systemdo you use?w ィ 。 エ documentationsystemdo you use?w ィ 。 エ bugtrackingsystemdo you use?- How is 「 イ 。 ョ 」 ィ ゥ ョ ァ ー ャ ・ ュ ・ ョ エ ・ 、 in a versioncontrolsystem?

a イ ・ deltasinhebranchhgfoy a revisiOI1treestoredout forwardsor backwards?What arethebenefitsof eachapproach?

ャ 。 エ 。 イ ・ 。 、 カ 。 ョ エ 。 ァ ・ ウ and 、 ゥ ウ 。 、 カ 。 ョ エ カ ・ イ ウ ゥ 」 ッ ョ エ イ ッ ャ

systemthatlocksfiles?

Shell toolsThereare scores, if not hundredsof books エ ィ ・ Unix shell andre-

lated tools. We haveenjoyedLINUX 101 Hacksby n 。 エ 。 イ 。 ョ N It intro-ducestheseエ ッ ッ ャ ウ イ ッ オ ァ ィ オ ウ ・ ィ ャ hacksrsuchas the useof fiad to fhdall files thathavemt beenmodified in the past100daysandarelargerthan100megabytesin size, sortingthe passwordfile P エィ・ third field,etc.

- Write a regular ・ ク ー イ ・ ウ ウ ゥ ッ ヲ ッ イ identifying socialsecuritynumbers

in a file.wイゥエ・ a commandthat prints out lines in a text file which containthestringsfoo andbarin anyorder.w イ ゥ エ ・ a commaI1dwhich replacesevery occurrenceof a foo f017lowedby a bar (with possiblysomeothercharactersin between)

bywidget.

11.18 SYSTEMS

11.17 COMPUTERARCHITECTURE

97

- Given a text file with two columnsof integers, i.e., two integersencodedin ASCII perline, write a filter which sortsャ ゥ ョ ・ ウ エ ィ ・ fileby thesecondinteger.

- How wouldyou taketwo documentsin PDFandcreateanewdoc-umentwhich consistsof the pagesof the two original documentsinterleavedin order?

- How wouldyouwrite a programwhichcheckseveryhourif a ᆳwork connectionis up?

- How wouldyouwrite aprogramwhichchecksthepriceof aNikonD40 DLSReachdayon amazon.com?

Professionalprogrammersusemanysoftwaresystemseverydayandit isreasonableto expectthat theyshouldhavesomeunderstandingof howthesesystemswork.

11.17. COMPUTERARCHITECTURE

ComputerArchitecture:A QuantitativeApproachandComputerOrganizationand Design, TheHardware/Softwareiョエ・ 」・ bothby PattersonandHen-nessy, arethedefinitiveworks in this field.

- What is pipelining?Describea 5-stagepipeline.- What is a multi-issueprocessor?- What is the differencebetweena superscalaranda VLIW proces-

sor?Whereis eachappropriate?w ィ 。 エ is a multicoremachine?•Wh at is thesignificanceof theprivilegedbit?- How is kernelmodedifferentfrom イ オ ゥ ョ ァ asroot?- What dobig-endianandlittle-endiannotationsmean?

y ッ オ rewrite somemachinecodeto reducethe numberof instruc-tions to performa 」ッューオエ。エゥ 。ョ、 performancedrops. Canyouexplainthis?y ッ オ benchmarka 3.0 gigahertzPentium4 andfind it to benotice-ably slowerthana 2.4 gigahertzPentiumPro-Why?

- You find thesamecomputationonthesameoperatingsystemwiththesameloadtakeslongeronhotdays-Why?

- How large andfast are the registerfile , L1 cache, L2 cache, mainmemory, anddisk on currentmachines?

- How manyinstructionsarein-flight in a modemcore?- What is branchprediction?- Why is predictionbasedon theprogram」 ッ オ ・ イ insufficient?- What is prefetching?Whatis a reasonablecriterionfor prefetching?

CHAPTER11. PROGRAMMING

- What is a devicedriver?w ィ 。 エ is livelock?vmatis deadlock?Giveexamplesof eact1·

_ What is a race?What canyou do to preventraces?-What is a mutex?Whatare semaphores?Howare they imple-

mented?-Gveexamplesッ ヲ ウ ケ ウ エ ・ ュ 、 ャ ウ エ ィ 。 エ 。 イ ・ r ッ エ イ ・ ャ 。 エ ・ 、 エ ッ ィ ー オ エ M P Q オ エ

-Give examplesof library fUIIdiom that call a systemfumtiOI1allthe time, noneof thetime, andsomeof thetime.w ィ 。 エ ゥエ ゥ s エ Z ィ Z ィ ・ エ ゥ・ エ ゥ ュ ・ Q 。 ァ 「 「 ・ エ キ ・ ・ ョ エZ ィ ・・ s ケ ウ エ ・ ・ ュ ュ Q 」 。 ャ ャ エ Z ィ ・・ c ウ ゥ 、 ・ 。 ョ d

thereceiptof thepacketOI1theservefOI1alocalareanetwork?-Howfastcmyouwrite a gigabyteof datafrom RAM to disk?

- How doesTCP/IP work?

96

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

11.16 TOOLS

Building andmaintainingprogramsThereis a paucityof booksonprogrammhgtools;OI1ebookwehave

usedis EssentialOpenSourceToolsetby Zeller andKrinke.

w ィ 。 エ versioncontrolsystemdo youuse?_ What coveragetool do youuse?_ Whatbuild systemdo you use?w ィ 。 エ documentationsystemdo you use?w ィ 。 エ bugtrackingsystemdo you use?- How is 「 イ 。 ョ 」 ィ ゥ ョ ァ ー ャ ・ ュ ・ ョ エ ・ 、 in a versioncontrolsystem?

a イ ・ deltasinhebranchhgfoy a revisiOI1treestoredout forwardsor backwards?What arethebenefitsof eachapproach?

ャ 。 エ 。 イ ・ 。 、 カ 。 ョ エ 。 ァ ・ ウ and 、 ゥ ウ 。 、 カ 。 ョ エ カ ・ イ ウ ゥ 」 ッ ョ エ イ ッ ャ

systemthatlocksfiles?

Shell toolsThereare scores, if not hundredsof books エ ィ ・ Unix shell andre-

lated tools. We haveenjoyedLINUX 101 Hacksby n 。 エ 。 イ 。 ョ N It intro-ducestheseエ ッ ッ ャ ウ イ ッ オ ァ ィ オ ウ ・ ィ ャ hacksrsuchas the useof fiad to fhdall files thathavemt beenmodified in the past100daysandarelargerthan100megabytesin size, sortingthe passwordfile P エィ・ third field,etc.

- Write a regular ・ ク ー イ ・ ウ ウ ゥ ッ ヲ ッ イ identifying socialsecuritynumbers

in a file.wイゥエ・ a commandthat prints out lines in a text file which containthestringsfoo andbarin anyorder.w イ ゥ エ ・ a commaI1dwhich replacesevery occurrenceof a foo f017lowedby a bar (with possiblysomeothercharactersin between)

bywidget.

11.18 SYSTEMS

11.17 COMPUTERARCHITECTURE

97

- Given a text file with two columnsof integers, i.e., two integersencodedin ASCII perline, write a filter which sortsャ ゥ ョ ・ ウ エ ィ ・ fileby thesecondinteger.

- How wouldyou taketwo documentsin PDFandcreateanewdoc-umentwhich consistsof the pagesof the two original documentsinterleavedin order?

- How wouldyouwrite a programwhichcheckseveryhourif a ᆳwork connectionis up?

- How wouldyouwrite aprogramwhichchecksthepriceof aNikonD40 DLSReachdayon amazon.com?

Professionalprogrammersusemanysoftwaresystemseverydayandit isreasonableto expectthat theyshouldhavesomeunderstandingof howthesesystemswork.

11.17. COMPUTERARCHITECTURE

ComputerArchitecture:A QuantitativeApproachandComputerOrganizationand Design, TheHardware/Softwareiョエ・ 」・ bothby PattersonandHen-nessy, arethedefinitiveworks in this field.

- What is pipelining?Describea 5-stagepipeline.- What is a multi-issueprocessor?- What is the differencebetweena superscalaranda VLIW proces-

sor?Whereis eachappropriate?w ィ 。 エ is a multicoremachine?•Wh at is thesignificanceof theprivilegedbit?- How is kernelmodedifferentfrom イ オ ゥ ョ ァ asroot?- What dobig-endianandlittle-endiannotationsmean?

y ッ オ rewrite somemachinecodeto reducethe numberof instruc-tions to performa 」ッューオエ。エゥ 。ョ、 performancedrops. Canyouexplainthis?y ッ オ benchmarka 3.0 gigahertzPentium4 andfind it to benotice-ably slowerthana 2.4 gigahertzPentiumPro-Why?

- You find thesamecomputationonthesameoperatingsystemwiththesameloadtakeslongeronhotdays-Why?

- How large andfast are the registerfile , L1 cache, L2 cache, mainmemory, anddisk on currentmachines?

- How manyinstructionsarein-flight in a modemcore?- What is branchprediction?- Why is predictionbasedon theprogram」 ッ オ ・ イ insufficient?- What is prefetching?Whatis a reasonablecriterionfor prefetching?

CHAPTER11. PROGRAMMING

- What is a devicedriver?w ィ 。 エ is livelock?vmatis deadlock?Giveexamplesof eact1·

_ What is a race?What canyou do to preventraces?-What is a mutex?Whatare semaphores?Howare they imple-

mented?-Gveexamplesッ ヲ ウ ケ ウ エ ・ ュ 、 ャ ウ エ ィ 。 エ 。 イ ・ r ッ エ イ ・ ャ 。 エ ・ 、 エ ッ ィ ー オ エ M P Q オ エ

-Give examplesof library fUIIdiom that call a systemfumtiOI1allthe time, noneof thetime, andsomeof thetime.w ィ 。 エ ゥエ ゥ s エ Z ィ Z ィ ・ エ ゥ・ エ ゥ ュ ・ Q 。 ァ 「 「 ・ エ キ ・ ・ ョ エZ ィ ・・ s ケ ウ エ ・ ・ ュ ュ Q 」 。 ャ ャ エ Z ィ ・・ c ウ ゥ 、 ・ 。 ョ d

thereceiptof thepacketOI1theservefOI1alocalareanetwork?-Howfastcmyouwrite a gigabyteof datafrom RAM to disk?

- How doesTCP/IP work?

96

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

98 CHAPTER11. PROGRAMMING

d・ウ」イゥ「・ howanoperatingsystemis implemented.Specificallyde-scribehow l ゥ ョ オ ク ャ ー ャ ・ ュ ・ ョ エ ウ processesandI/O.

- How doesa web browserwork? Specifically, describehow auto-」 ッ ュ ー ャ ・ エ ゥ ッ H ウ オ 」 ィ asin a searchenginequerybox) is implemented.

- How doesthe Internetwork? Specificallydescribethe rolesof theTCP/IP protocol, routers, andDNS.

- How is asocialnetworkingsitebuilt? Specificallycommentonscal-。 「 ゥ ャ ゥ エ ウ ー 。 ュ prevention, andresilienceto denial-of-service.

Part II

The Interview

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

98 CHAPTER11. PROGRAMMING

d・ウ」イゥ「・ howanoperatingsystemis implemented.Specificallyde-scribehow l ゥ ョ オ ク ャ ー ャ ・ ュ ・ ョ エ ウ processesandI/O.

- How doesa web browserwork? Specifically, describehow auto-」 ッ ュ ー ャ ・ エ ゥ ッ H ウ オ 」 ィ asin a searchenginequerybox) is implemented.

- How doesthe Internetwork? Specificallydescribethe rolesof theTCP/IP protocol, routers, andDNS.

- How is asocialnetworkingsitebuilt? Specificallycommentonscal-。 「 ゥ ャ ゥ エ ウ ー 。 ュ prevention, andresilienceto denial-of-service.

Part II

The Interview

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

12.2 ApPROACHINGTHE PROBLEM

No matterhowwell preparedyouare, thereis a goodchancethattheso-lution to aninterviewproblemwill not occurto you immediately.Whenthis happens, thereareseveralthingsyoucando.

Clarify the question:This mayseemobvious「 オ エ ゥ ウ amazinghowmanyinterviewsgobadlybecausethecandidatespendsmostof thetimetrying to solve the wrong problem. If a questionseemsexceptionallyhard, thereis a goodchanceyouhavemisunderstoodthequestion.

t i ・ ウ エ キ 。 ケ ッ ヲ ゥ イ ゥ エ ィ ・ questionis to statea concreteinstanceof theproblem.For example!if the questionis 'find エ ィ ・ イ ウ エ occurrenceofanumbergreaterthan k in asortedarray"! you could askthe ヲ ッ ャ ャ ッ キ ゥ h

theinputarray is [2,20,30] andk is 3, thenareyousupposedto return 1 (indexof20)?"

Work on small examples:ConsiderProblem9.4. This problemmayseemprettyhardat firs t. But if you startworking out which doorsaregoingto beopenfor up to thefifth door!youwill seethat P ャ ケ 、 ッ ッ イ 1 anddoor4 areopen.Thismaysuggestto you thatthedooris openP ャ ケ if itsindex is a perfect square.Onceyou havethis realization, it is relativelyeasyto provethe correctnessof this assertion.This maynot be true forall theproblems.Howeverthereis a largeclassof problemswhereafterworkingoutthesolutionfor afew smallexamples!youmayseeapatternemerge.

Spelloutthebrute-forcesolution:Problemsthatareputto you in aninterview tend to havean obviousbrute-forcesolutionthat hasa largeruntimecomparedto moresophisticatedsolutions.Forexample!insteadof trying to work out a 、ケョ ョゥ」 programmingsolution for a problem(suchasProblem3.4)! try all thepossibleconfigurations.Therearesev-eral advantagesto this: (1.) it helpsyou exploreopportunitiesfor opti-mizationandhencereachabettersolution! (2.) it givesyou anopportu-nity to demonstratesomeproblem ウ ッ ャ イ ゥ ョ ァ andcodingskills! and (3.) itestablishesthatbothyouandtheinterviewerarethinkingaboutthesameproblem.Bewarnedthat this strategycansometimesbedetrimentalif ittakestoo long to describeeventhebrute-forceapproachandleavesyouwith lesstime to work on theoptimalsolution.

Think outloud: Oneof theworstthingsyoucando in aninterviewisto freezeup while solvingtheproblem.It is alwaysa goodideato thinkout loud while searchingfor a solution. P ッ ョ ・ hand!this increasesthechancesof you finding the right solutionbecauseit forces you to putyo

Chapter12

StrategiesFor A GreatInterview

A typical onehour interview with a single interviewerconsistsof fiveminutesof introductionsand questionsabout the candidate'sイ ・ ウ オ ュ ᆳ

This is followed by five to fifteen minutesof questioningon basicpro-ァ イ 。 ュ ュ ゥ ョ ァ 」 ッ 」 ・ ー エ ウ N

The core of the interview is one or two detailedalgorithm designquestionswherethe candidateis expectedto presenta detailedsolutiono 。 whiteboardor paper. DependingP エ ィ ・ interviewerandthe ques-tion, thesolutionmayberequiredto includesyntacticallycorrectcodeina languagethatthecandidateis comfortablewith.

The reasonfor 。 ウ ォ Q Y such ア オ ・ ウ エ ゥ ッ ウ is that algorithmsand associ-ateddata-structuresunderlieall software. They are oftenhiddenin li-brary calls. They 」 。 「 ・ a small part of a codebasedominatedby 10andformat conversion.But they are the crucial 」 ッ ュ ー ッ ョ ・ エ in termsofperformanceandintricacy.

The ュ ッ ウ エ ョ ー ッ イ エ ャ エ partof interviewpreparationis to know thema-terial andpracticesolvingproblems.Howeverthe ョ ッ エ ・ 」 ィ ョ ゥ 」 。 ャ aspectsof interviewing cannotbe underplayedeither. Thereare a numberofthingsthatcould go wrongin aninterviewandit ゥ ウ ー ッ イ エ 。 ョ エ to haveastrategyto dealwith them.

12.1 BEFORETHE INTERVIEW

Oneof thebestwaysof preparingfor an interview is mock interviews.Get a friend to askyou ア オ ・ ウ エ ゥ ッ ウ from this book (or any othersource)andhaveyou solve the problemson a whiteboardor paper. Ask yourfriend to takenotesand give you detailedfeedback, bothpositive andnegative.Also askyour friend to providehints from the solutionif youarestuck. This will helpyou overcomeanyfear or problemareaswell inadvance.

12.2. APPROACHINGTHEPROBLEM 101

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

12.2 ApPROACHINGTHE PROBLEM

No matterhowwell preparedyouare, thereis a goodchancethattheso-lution to aninterviewproblemwill not occurto you immediately.Whenthis happens, thereareseveralthingsyoucando.

Clarify the question:This mayseemobvious「 オ エ ゥ ウ amazinghowmanyinterviewsgobadlybecausethecandidatespendsmostof thetimetrying to solve the wrong problem. If a questionseemsexceptionallyhard, thereis a goodchanceyouhavemisunderstoodthequestion.

t i ・ ウ エ キ 。 ケ ッ ヲ ゥ イ ゥ エ ィ ・ questionis to statea concreteinstanceof theproblem.For example!if the questionis 'find エ ィ ・ イ ウ エ occurrenceofanumbergreaterthan k in asortedarray"! you could askthe ヲ ッ ャ ャ ッ キ ゥ h

theinputarray is [2,20,30] andk is 3, thenareyousupposedto return 1 (indexof20)?"

Work on small examples:ConsiderProblem9.4. This problemmayseemprettyhardat firs t. But if you startworking out which doorsaregoingto beopenfor up to thefifth door!youwill seethat P ャ ケ 、 ッ ッ イ 1 anddoor4 areopen.Thismaysuggestto you thatthedooris openP ャ ケ if itsindex is a perfect square.Onceyou havethis realization, it is relativelyeasyto provethe correctnessof this assertion.This maynot be true forall theproblems.Howeverthereis a largeclassof problemswhereafterworkingoutthesolutionfor afew smallexamples!youmayseeapatternemerge.

Spelloutthebrute-forcesolution:Problemsthatareputto you in aninterview tend to havean obviousbrute-forcesolutionthat hasa largeruntimecomparedto moresophisticatedsolutions.Forexample!insteadof trying to work out a 、ケョ ョゥ」 programmingsolution for a problem(suchasProblem3.4)! try all thepossibleconfigurations.Therearesev-eral advantagesto this: (1.) it helpsyou exploreopportunitiesfor opti-mizationandhencereachabettersolution! (2.) it givesyou anopportu-nity to demonstratesomeproblem ウ ッ ャ イ ゥ ョ ァ andcodingskills! and (3.) itestablishesthatbothyouandtheinterviewerarethinkingaboutthesameproblem.Bewarnedthat this strategycansometimesbedetrimentalif ittakestoo long to describeeventhebrute-forceapproachandleavesyouwith lesstime to work on theoptimalsolution.

Think outloud: Oneof theworstthingsyoucando in aninterviewisto freezeup while solvingtheproblem.It is alwaysa goodideato thinkout loud while searchingfor a solution. P ッ ョ ・ hand!this increasesthechancesof you finding the right solutionbecauseit forces you to putyo

Chapter12

StrategiesFor A GreatInterview

A typical onehour interview with a single interviewerconsistsof fiveminutesof introductionsand questionsabout the candidate'sイ ・ ウ オ ュ ᆳ

This is followed by five to fifteen minutesof questioningon basicpro-ァ イ 。 ュ ュ ゥ ョ ァ 」 ッ 」 ・ ー エ ウ N

The core of the interview is one or two detailedalgorithm designquestionswherethe candidateis expectedto presenta detailedsolutiono 。 whiteboardor paper. DependingP エ ィ ・ interviewerandthe ques-tion, thesolutionmayberequiredto includesyntacticallycorrectcodeina languagethatthecandidateis comfortablewith.

The reasonfor 。 ウ ォ Q Y such ア オ ・ ウ エ ゥ ッ ウ is that algorithmsand associ-ateddata-structuresunderlieall software. They are oftenhiddenin li-brary calls. They 」 。 「 ・ a small part of a codebasedominatedby 10andformat conversion.But they are the crucial 」 ッ ュ ー ッ ョ ・ エ in termsofperformanceandintricacy.

The ュ ッ ウ エ ョ ー ッ イ エ ャ エ partof interviewpreparationis to know thema-terial andpracticesolvingproblems.Howeverthe ョ ッ エ ・ 」 ィ ョ ゥ 」 。 ャ aspectsof interviewing cannotbe underplayedeither. Thereare a numberofthingsthatcould go wrongin aninterviewandit ゥ ウ ー ッ イ エ 。 ョ エ to haveastrategyto dealwith them.

12.1 BEFORETHE INTERVIEW

Oneof thebestwaysof preparingfor an interview is mock interviews.Get a friend to askyou ア オ ・ ウ エ ゥ ッ ウ from this book (or any othersource)andhaveyou solve the problemson a whiteboardor paper. Ask yourfriend to takenotesand give you detailedfeedback, bothpositive andnegative.Also askyour friend to providehints from the solutionif youarestuck. This will helpyou overcomeanyfear or problemareaswell inadvance.

12.2. APPROACHINGTHEPROBLEM 101If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Searchfor isomorphicproblems:Evenif youmaynot haveseentheexactproblem, you mayhaveseenanotherproblemwith similarmathe-maticalstructure. Seeif this seemslike a good fit for generalalgorith-mic techniques, such as, divide-and-conquer, dynamic programming,greedy, etc. Canyou map the problemto a graph? Canyou map it toan objectivefunction anda set of constraints, suchas an integerlinearprogram?

12.3 PRESENTINGTHE SOLUTION

Onceyouhavea solution, it is importantto presentit well anddo a com-prehensivejob at it. A lot of thesethingsbecomesimpler if you useahigher levellanguagesuchas Java. Howeveryou shouldusethe lan-guagewith whichyouaremostfamiliar. In most ウ 」 ・ ョ 。 イ ゥ ッ ウ ゥ ウ perfectlyfine to write a pseudocodeaswell. Here are somethoughtsthat couldhelp:

Testfor cornercases:For a numberof problems, your generalideamaywork for the majority of the casesbut theremaybe a few obscureinputs where your algorithm (or code) would fail. For example, youcould write a binary searchcodethat crashesif エ ィ ・ ャ ー オ エ is an emptyarray or you may do arithmeticwithout consideringthe possibility ofintegeroverflow. It is importantto checkfor thesethingscarefully. Onewayof doingthis is to constructa few test 」 。 ウ ・ ウ ャ 、 work out theoutputof your algorithm for them. In many cases, the code to handlesomeobscurecornercasesmaybetoo complicated.In suchcases, you shouldat leastmentionto theinterviewerthatyouareawareof theproblemandyou couldtry to addressit if theyareinterested.

Functionsignature:Severalcandidatestend to get this キ イ ッ ョ ァ ャ 、

getting your function signaturewrong reflectsbadly on you. For ex-ample, it would be bad if you arewriting the codein C languageandyour functionreturns。 。 イ イ 。 ケ butyou fail to returnthesizeof thearrayalongwith the pointer. Anotherplacewherefunction signaturescould「 ・ ー ッ イ エ 。 ョ エ is knowing when to passparametersby valueversusbyreference.

Memorymanagement:If you allocateュ ・ ュ ッ イ ケ ケ ッ オ イ function, youmustensurethatin everyexecutionpath, thismemoryis de-allocated.Ingeneral, it is bestto avoidmemorymanagementoperationsall エ ッ ァ ・ エ ィ ・If you mustdo this, 」ッ ウゥ、・イ useof scopedpointers.

Syntax: In almostall cases, the interviewersarenot evaluatingyouon the correctnessof the ウ ケ エ 。 ク of your 」 ッ 、 ・ N ャ ・ editorsandcompilersdo a greatjob at helpingyou get the syntaxright. Howeveryou cannotunderplaythe possibilityof an interviewerleavingwith the impressionthat you got mostof the syntax キ イ ッ ァ sinceyou do not havemuchex-ー・イゥ・ 」・ writing code.Henceonceyou aredonewriting your code, you

12.4 KNOW YOUR INTERVIEWERS

103

12.5 GENERAL CONVERSATION

Ofteninterviewerswill spendsometime askingquestionsaboutyourpastprojects, dissertation, etc. Thepoint of this conversationis:

Canthe candidateclearlycommunicatea complexidea: This is oneof the mostimportantskills for working in anengineeringteam. If youhavea grandideato redesigna big system, canyou communicateit toyour colleaguesandbring themonboard?It is bestto practicehow youwantto presentsomeof yourbestwork in advance.Beingprecise, clear,andhavingconcreteexamplescango a long way here. For candidateswho haveto communicatein a languagethat is not their first language,it maybe importantto speakslowly andperhapsusethewhiteboardtoaugmenttheirwords.

Is the candidatepassionateabouthis work: We alwayswant ourcolleaguesto be passionate, full of energy, and inspiring to work with.If you areso ー 。 ウ ウ ゥ ッ 。 エ ・ aboutyour work that your eyeslight up whiledescribingyour work, it cango a longway in termsof establishingyouas a greatcolleague. Hencewhen you are askedto describea projectfrom thepast, it is bestto pick somethingthatyou arepassionateaboutratherthana projectthatwascomplexbutdid not interestyou.

Is therea potentialinterestmatchwith someproject: During a gen-eralconversation, the interviewermaygaugeareasof strengthsfor a po-tentialprojectmatch. If you know the requirements ofthejob, you may

shouldmakea passover it to avoid any obvioussyntaxerrorsbeforeclaimingyou aredone.

12.4. KNOWYOURINTERVIEWERS

If theorganizationcansharesomeinformationaboutthebackgroundofyour interviewers, it canhelp you a greatdeal. For freshgraduates, it isalsoimportantto think .from the perspectiveof the interviewers.Hencewehighly recommendreadingthenextchapter ゥ ョ エ ・ イ カ ゥ ・ キ ゥ ョ ァ from theperspectiveof aninterviewer.

It is alsoimportantto notethat onceyou acethe interview, you willhavean offer andyou would havean importantdecisionto make-isthis the organizationwhereyou want to work? Interviewsare the besttime to collect this information. Basedon your interactionwith the in-terviewers, you can get a pretty good idea of their intellect as well ashow ー ャ ・ 。 ウ 。 エ ィ ・ organizationcouldbe. Most interviewsendwith theinterviewersletting the candidatesaskquestions.You shouldmakethebestuseof this timeby (1.) gettingtheinformationyou wouldneedand(2.) communicatingto the interviewerthatyou areinterestedゥ エ ィ ・ job.Preparea list of questionsin advancethatbothgetsyouhelpful informa-tion aswell asshowsyour knowledgeandinterestin theorganization.

CHAPTER12. STRATEGIESFORA GREATINTERVIEW102

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Searchfor isomorphicproblems:Evenif youmaynot haveseentheexactproblem, you mayhaveseenanotherproblemwith similarmathe-maticalstructure. Seeif this seemslike a good fit for generalalgorith-mic techniques, such as, divide-and-conquer, dynamic programming,greedy, etc. Canyou map the problemto a graph? Canyou map it toan objectivefunction anda set of constraints, suchas an integerlinearprogram?

12.3 PRESENTINGTHE SOLUTION

Onceyouhavea solution, it is importantto presentit well anddo a com-prehensivejob at it. A lot of thesethingsbecomesimpler if you useahigher levellanguagesuchas Java. Howeveryou shouldusethe lan-guagewith whichyouaremostfamiliar. In most ウ 」 ・ ョ 。 イ ゥ ッ ウ ゥ ウ perfectlyfine to write a pseudocodeaswell. Here are somethoughtsthat couldhelp:

Testfor cornercases:For a numberof problems, your generalideamaywork for the majority of the casesbut theremaybe a few obscureinputs where your algorithm (or code) would fail. For example, youcould write a binary searchcodethat crashesif エ ィ ・ ャ ー オ エ is an emptyarray or you may do arithmeticwithout consideringthe possibility ofintegeroverflow. It is importantto checkfor thesethingscarefully. Onewayof doingthis is to constructa few test 」 。 ウ ・ ウ ャ 、 work out theoutputof your algorithm for them. In many cases, the code to handlesomeobscurecornercasesmaybetoo complicated.In suchcases, you shouldat leastmentionto theinterviewerthatyouareawareof theproblemandyou couldtry to addressit if theyareinterested.

Functionsignature:Severalcandidatestend to get this キ イ ッ ョ ァ ャ 、

getting your function signaturewrong reflectsbadly on you. For ex-ample, it would be bad if you arewriting the codein C languageandyour functionreturns。 。 イ イ 。 ケ butyou fail to returnthesizeof thearrayalongwith the pointer. Anotherplacewherefunction signaturescould「 ・ ー ッ イ エ 。 ョ エ is knowing when to passparametersby valueversusbyreference.

Memorymanagement:If you allocateュ ・ ュ ッ イ ケ ケ ッ オ イ function, youmustensurethatin everyexecutionpath, thismemoryis de-allocated.Ingeneral, it is bestto avoidmemorymanagementoperationsall エ ッ ァ ・ エ ィ ・If you mustdo this, 」ッ ウゥ、・イ useof scopedpointers.

Syntax: In almostall cases, the interviewersarenot evaluatingyouon the correctnessof the ウ ケ エ 。 ク of your 」 ッ 、 ・ N ャ ・ editorsandcompilersdo a greatjob at helpingyou get the syntaxright. Howeveryou cannotunderplaythe possibilityof an interviewerleavingwith the impressionthat you got mostof the syntax キ イ ッ ァ sinceyou do not havemuchex-ー・イゥ・ 」・ writing code.Henceonceyou aredonewriting your code, you

12.4 KNOW YOUR INTERVIEWERS

103

12.5 GENERAL CONVERSATION

Ofteninterviewerswill spendsometime askingquestionsaboutyourpastprojects, dissertation, etc. Thepoint of this conversationis:

Canthe candidateclearlycommunicatea complexidea: This is oneof the mostimportantskills for working in anengineeringteam. If youhavea grandideato redesigna big system, canyou communicateit toyour colleaguesandbring themonboard?It is bestto practicehow youwantto presentsomeof yourbestwork in advance.Beingprecise, clear,andhavingconcreteexamplescango a long way here. For candidateswho haveto communicatein a languagethat is not their first language,it maybe importantto speakslowly andperhapsusethewhiteboardtoaugmenttheirwords.

Is the candidatepassionateabouthis work: We alwayswant ourcolleaguesto be passionate, full of energy, and inspiring to work with.If you areso ー 。 ウ ウ ゥ ッ 。 エ ・ aboutyour work that your eyeslight up whiledescribingyour work, it cango a longway in termsof establishingyouas a greatcolleague. Hencewhen you are askedto describea projectfrom thepast, it is bestto pick somethingthatyou arepassionateaboutratherthana projectthatwascomplexbutdid not interestyou.

Is therea potentialinterestmatchwith someproject: During a gen-eralconversation, the interviewermaygaugeareasof strengthsfor a po-tentialprojectmatch. If you know the requirements ofthejob, you may

shouldmakea passover it to avoid any obvioussyntaxerrorsbeforeclaimingyou aredone.

12.4. KNOWYOURINTERVIEWERS

If theorganizationcansharesomeinformationaboutthebackgroundofyour interviewers, it canhelp you a greatdeal. For freshgraduates, it isalsoimportantto think .from the perspectiveof the interviewers.Hencewehighly recommendreadingthenextchapter ゥ ョ エ ・ イ カ ゥ ・ キ ゥ ョ ァ from theperspectiveof aninterviewer.

It is alsoimportantto notethat onceyou acethe interview, you willhavean offer andyou would havean importantdecisionto make-isthis the organizationwhereyou want to work? Interviewsare the besttime to collect this information. Basedon your interactionwith the in-terviewers, you can get a pretty good idea of their intellect as well ashow ー ャ ・ 。 ウ 。 エ ィ ・ organizationcouldbe. Most interviewsendwith theinterviewersletting the candidatesaskquestions.You shouldmakethebestuseof this timeby (1.) gettingtheinformationyou wouldneedand(2.) communicatingto the interviewerthatyou areinterestedゥ エ ィ ・ job.Preparea list of questionsin advancethatbothgetsyouhelpful informa-tion aswell asshowsyour knowledgeandinterestin theorganization.

CHAPTER12. STRATEGIESFORA GREATINTERVIEW102If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

want to steerthe conversationin that direction. Howeverin the com-puting industry, things changeso fast that most teamsprefer a stronggeneralist.

Also, it is a good idea to maintaina homepagewith links to yourprojectsandarticles;thingsthatcanhelp interviewerslearnmoreaboutyou.

12.6 P e r GRANDFATHERLY ADVICE

Keep a positivespirit: A cheerfuloptimistic attitudecango a longway. Thereis reallynopointcomplaininghowdifficult yourjourneywasor howyou 。 イ ・ ッ エ a morning ー ・ イ ウ ッ

Grooming: Most softwarecompanieshavea relaxeddress-code, sonewgraduatesmaywonderif theywilllook foolishby overdressing.Thedamagedonewhenyou aremorecasualthanexpectedis far morethanthe minor embarrassmentyou may feel beingoverdressed.Thereforeitis alwaysa goodideato err P エ ィ ・ sideof cautionanddressformally foryour interviews.At theveryminimum, becleanandwell-groomed.

Keepmoneyandperksout of the interview: Money is a big factorin any job but it is bestleft to be discussedwith the HumanResourcesdivision after an offer is made;the sameis true for vacationtime, daycaresupport, etc.

Be awareof yourbodylanguage:Think of a friend or coworkerwhois slouchedall the time or absent-mindedlydoesthingsthatmayoffendothers.

104 CHAPTER12. STRATEGIESFORA GREATINTERVIEW

Chapter13

ConductingAn Interview

For someoneat thebeginningof their career, interviewingcanfeellike ahugeresponsibility.If youhire abadcandidate, it canbevery expensivefor theorganization,notjustbecausetheemployeewouldnotbe ー イ ッ 、tive butmoresobecausetheemployeewouldbea drainon theproduc-tivity of everyoneelsewho is trying to train andmentorthe employee.f ゥ イ ァ ウ ッ ュ ャ ・ ッ ョ ・ ・ afterabadhiring 、 ・ 」 ゥ ウ ゥ ッ i ャ ゥ ウ extremelyー 。 ゥ ャ ヲ オ ャ 。 ョ 、 、 ・ エ イ ゥ ᆳ

ュ ャ ・ ョ ャ エ 。 ャ エ ッ the moraleof both the エ ・ 。 ョ 、 the individual. On the otherhand, if you discardgoodcandidatestoo often, it canbeproblematicfora rapidly growingorganization, not to mentionthemoral responsibilityof not crushingsomeone's dreamsand aspirationsunnecessarily.Hereare somethoughtsthat could potentiallyhelp you makethis processalittle easier.

13.1 OBJECTIVE

Theultimategoal of any interview is to determineif a given candidatetakesup thejob andis appropriatelytrained, whatarethe chancesthatthecandidatewill beasuccessfulemployeeof thecompany.Usuallythismeansyou want incrediblysmartpeoplewho canget thingsdone. It isimportantto designthewholeprocesswith thisasthecentraltheme.Ide-。 ャ ャ ケ ッ オ イ interviewsshouldbedesignedsuchthatyouscorea goodcan-didate1.0 anda badcandidate0.0. A commonmistakemadeby noviceinterviewersis to not be decisive. Unlessthe candidatewalks on wateror completelydisappointsthe interviewer, the novice interviewerstrynot to makea 、・」ゥウゥッョ ャ、 scorethecandidatessomewherein themid-dIe. This essentiallymeansthat the interviewwasa wastedeffort. Oneway of makingthis easierfor theinterviewersis to imagineif this candi-datereplacessomeoneproductivein their エ ・ Q N If this feels like a goodchange, thenyou shouldgive the candidatea high score, otherwise, alow score.

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

want to steerthe conversationin that direction. Howeverin the com-puting industry, things changeso fast that most teamsprefer a stronggeneralist.

Also, it is a good idea to maintaina homepagewith links to yourprojectsandarticles;thingsthatcanhelp interviewerslearnmoreaboutyou.

12.6 P e r GRANDFATHERLY ADVICE

Keep a positivespirit: A cheerfuloptimistic attitudecango a longway. Thereis reallynopointcomplaininghowdifficult yourjourneywasor howyou 。 イ ・ ッ エ a morning ー ・ イ ウ ッ

Grooming: Most softwarecompanieshavea relaxeddress-code, sonewgraduatesmaywonderif theywilllook foolishby overdressing.Thedamagedonewhenyou aremorecasualthanexpectedis far morethanthe minor embarrassmentyou may feel beingoverdressed.Thereforeitis alwaysa goodideato err P エ ィ ・ sideof cautionanddressformally foryour interviews.At theveryminimum, becleanandwell-groomed.

Keepmoneyandperksout of the interview: Money is a big factorin any job but it is bestleft to be discussedwith the HumanResourcesdivision after an offer is made;the sameis true for vacationtime, daycaresupport, etc.

Be awareof yourbodylanguage:Think of a friend or coworkerwhois slouchedall the time or absent-mindedlydoesthingsthatmayoffendothers.

104 CHAPTER12. STRATEGIESFORA GREATINTERVIEW

Chapter13

ConductingAn Interview

For someoneat thebeginningof their career, interviewingcanfeellike ahugeresponsibility.If youhire abadcandidate, it canbevery expensivefor theorganization,notjustbecausetheemployeewouldnotbe ー イ ッ 、tive butmoresobecausetheemployeewouldbea drainon theproduc-tivity of everyoneelsewho is trying to train andmentorthe employee.f ゥ イ ァ ウ ッ ュ ャ ・ ッ ョ ・ ・ afterabadhiring 、 ・ 」 ゥ ウ ゥ ッ i ャ ゥ ウ extremelyー 。 ゥ ャ ヲ オ ャ 。 ョ 、 、 ・ エ イ ゥ ᆳ

ュ ャ ・ ョ ャ エ 。 ャ エ ッ the moraleof both the エ ・ 。 ョ 、 the individual. On the otherhand, if you discardgoodcandidatestoo often, it canbeproblematicfora rapidly growingorganization, not to mentionthemoral responsibilityof not crushingsomeone's dreamsand aspirationsunnecessarily.Hereare somethoughtsthat could potentiallyhelp you makethis processalittle easier.

13.1 OBJECTIVE

Theultimategoal of any interview is to determineif a given candidatetakesup thejob andis appropriatelytrained, whatarethe chancesthatthecandidatewill beasuccessfulemployeeof thecompany.Usuallythismeansyou want incrediblysmartpeoplewho canget thingsdone. It isimportantto designthewholeprocesswith thisasthecentraltheme.Ide-。 ャ ャ ケ ッ オ イ interviewsshouldbedesignedsuchthatyouscorea goodcan-didate1.0 anda badcandidate0.0. A commonmistakemadeby noviceinterviewersis to not be decisive. Unlessthe candidatewalks on wateror completelydisappointsthe interviewer, the novice interviewerstrynot to makea 、・」ゥウゥッョ ャ、 scorethecandidatessomewherein themid-dIe. This essentiallymeansthat the interviewwasa wastedeffort. Oneway of makingthis easierfor theinterviewersis to imagineif this candi-datereplacessomeoneproductivein their エ ・ Q N If this feels like a goodchange, thenyou shouldgive the candidatea high score, otherwise, alow score.

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

A seCOIldaryobjectiveof the interview processmight be to tum thecandidateinto a goodbrandambassadorfor your organization. Evenif the candidateis not a ァッッ、 エ for your P 。 イ 。 エ ゥ ッ エ ィ ・ ケ mayknowotherswhowouldbe.Itis importantfor thecandidateto havem overallpositiveexperienceduriI1g theprocess-Itis fairly obviousthatit is abadideato aska cmdidatea problemmdthenstartcheckhgemailor hSIlltthecandidateoveramistaketheymadebutyouwouldbesurprisedhowoftenthis happensin someorganizations.

13.2 WHAT TO ASK

Oneimportant ア オ ・ ウ エ ゥ ッ ケ ッ オ shouldask ケ ッ オ イ ウ ・ ャ ヲ ゥ ウ how muchtrainingtimeyourwork mvirOI1meI1tallows.ForexampleriI1a ウ エ エ オ ー ュ 。 ケ 「 ・very importmtthata I1ewpersOI1isproductiveright from thefirst weekwhereassomeorzanizatiomallow a mOIIt21of trainhgtimemdyet mmッエィ・イ ッヲ organizationsallow for a few monthsof training andrampup time.Forexamplerh astartuprit wouldbekmportmtto testthecm-didateOI1thespecific エ ・ 」 ィ ョ ッyou areworking P キ ィ ・ イ ・ 。 ウ in most i ・ ッ イ ァ 。 。 エ ゥ ッ ョ ウ the 「 ゥ ョ ァ

h do ismtemphasizeッ ィ ・ domainknowledgeandtestthecandidateon their basicproblemsolving abilities andfundamentalsof computer

s c Q ・ 」 ・ ッ

Most big organizationshave a ヲ。ゥセャケ セエイオ」エオZセ、 ゥョエ・イZセ・キ ーイッ」セウウwherespecificiI1terviewersarerespomiblefor probhgspecificareas.ForexampleFyoumaybeaskedto evaluatetEIe cmdidateOReitherthir cod-hgskillsr algorithmkI1owledgeFcritical thinkhgror theability to desig1complexsystems. We hope エ ィ 。 エ 「 ッ ッ ォ gives you accessto a ヲ。largecollectioz1ofproblemsto choosefrom for eackof thecategories.AsyoLappmchthe decidmofpickhg om problemfrom a setof prob-lems, keepthe following in mind:

-Nosinglepohtoffailure-ifyouaregohgtoaskjustOI1equestiORryoushouldmtpick a problemwherethecandidatewouldpassthehterviewif theygetorle particularhsight-Thebestof the cmdi-datescanmissonesimpleinsight. Thereshouldbe at leasttwo orthreeopportunitiesfor the candidatesto redeemthemselves.Forexamplerthe problemsh the dymamieprogrammmgsect1OI1cmdmostalwaysbe solvedthough(1.)agreedyalgorithm that isfastbutsuboptimumor (2.) abrute-forcealgorithmthatis slowbutoptimum. In suchcases, evenif the candidatecannotget the keyゥ T ィ エ theycanstill demonstratesomeproblemsolvingabilities.

- No unnecessarydomainknowledge-itゥ ウ ッ エ a goodideato quiza candidateon advancedgraphalgorithmsif the job doesnot re-quire it and the candidatedoesnot claim any specialknowledge

of the field. H G ィ ・ exceptionto this rule is if you want to test thecandidate'sresponseto a high-stresssituation.)

- Covermultiple areas-evenif you are responsiblefor testingthecandidateonjust algorithms, you couldeasilypick a problemthatalsoexposessomeaspectsof designandcoding.

- Possiblemultiple solutions-ifa givenproblemhasmultiple goodsolutions, the chancesof a goodcandidatecomingup with a goodsolution increases. It also gives you more freedom as an inter-viewer to maneuverthe intervieweein the directionof oneof thegood solutions. Also, a greatcandidatemay finish with one so-lution soonenoughto discussotherapproachesandthe tradeoffsinvolved.

Oftennewinterviewershaveanincorrectnotionof howtoughor'easya problemis for a thirty minute or onehour interview. It is usually agoodideato calibratethe toughnessof a problemby askingoneof yourcolleaguesto solveit andseehowmuchdifficulty theyhavewith it.

107

13.3 CONDUCTING THE INTERVIEW

Conductinga goodinterviewis like juggling a lot of piecesエ ッ ァ ・ エ ィ ・ a エ

high level, you want to askyourquestionandevaluatethe candidate'sresponses.Sincesomanythingscanhappenin an interview thatcouldhelp you make a decision, it is important to take notes. At the sametime, it is importantto keepa conversationgoingwith the 」 。 ョ 、 ゥ 、 。 エ ・ ャ 、

help them out whereverthey get stuck. What works bestis to haveaseriesof hints workedout prior to the interview andyou providethesehintsprogressivelyasneeded.Comingupwith theright setof hints ュrequiresomethinking. You do not want to give awaythe problem, yetfind awayfor thecandidateto makeprogress.Therearea few situationsthatcanthrow you off board:

A candidatethat getsstuckandshutsup: Somecandidatescangetintimidatedby theproblemor the processandjust shutup. Usually, insuch ウ ゥ エ オ 。 エ ゥ ッ ウ the candidate'sperformancemay not reflect their truecaliber. In suchsituations, it is importantto put the candidateat easeby mentioningthat theproblemis toughanda goodway of proceedingwouldbeto think out loud, soyou canguidetheir thinking.

A verbosecandidate:Theotherclassof candidatesthatcanイ ・ ョ 、 ・ イ Q

interviewineffectiveis thecandidateswho go onin エ 。 ァ ・ エ ゥ 。 ャ directionsandkeepon talking without makingprogress.Herealsoit is importantto takecontrolof theconversationandassertthatthis line of conversationis notmakinganyprogresstowardstheproblem.

An overconfidentcandidate:It is notuncommonto meetcandidateswho weakentheir own caseby insistingthat their wrongansweris cor-recto In orderto givethecandidatea fair chance, it is importantto demon-

13.3. CONDUCTINGTHEINTERVIEWCHAPTER13. CONDUCTINGANINTERVIEW106

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

A seCOIldaryobjectiveof the interview processmight be to tum thecandidateinto a goodbrandambassadorfor your organization. Evenif the candidateis not a ァッッ、 エ for your P 。 イ 。 エ ゥ ッ エ ィ ・ ケ mayknowotherswhowouldbe.Itis importantfor thecandidateto havem overallpositiveexperienceduriI1g theprocess-Itis fairly obviousthatit is abadideato aska cmdidatea problemmdthenstartcheckhgemailor hSIlltthecandidateoveramistaketheymadebutyouwouldbesurprisedhowoftenthis happensin someorganizations.

13.2 WHAT TO ASK

Oneimportant ア オ ・ ウ エ ゥ ッ ケ ッ オ shouldask ケ ッ オ イ ウ ・ ャ ヲ ゥ ウ how muchtrainingtimeyourwork mvirOI1meI1tallows.ForexampleriI1a ウ エ エ オ ー ュ 。 ケ 「 ・very importmtthata I1ewpersOI1isproductiveright from thefirst weekwhereassomeorzanizatiomallow a mOIIt21of trainhgtimemdyet mmッエィ・イ ッヲ organizationsallow for a few monthsof training andrampup time.Forexamplerh astartuprit wouldbekmportmtto testthecm-didateOI1thespecific エ ・ 」 ィ ョ ッyou areworking P キ ィ ・ イ ・ 。 ウ in most i ・ ッ イ ァ 。 。 エ ゥ ッ ョ ウ the 「 ゥ ョ ァ

h do ismtemphasizeッ ィ ・ domainknowledgeandtestthecandidateon their basicproblemsolving abilities andfundamentalsof computer

s c Q ・ 」 ・ ッ

Most big organizationshave a ヲ。ゥセャケ セエイオ」エオZセ、 ゥョエ・イZセ・キ ーイッ」セウウwherespecificiI1terviewersarerespomiblefor probhgspecificareas.ForexampleFyoumaybeaskedto evaluatetEIe cmdidateOReitherthir cod-hgskillsr algorithmkI1owledgeFcritical thinkhgror theability to desig1complexsystems. We hope エ ィ 。 エ 「 ッ ッ ォ gives you accessto a ヲ。largecollectioz1ofproblemsto choosefrom for eackof thecategories.AsyoLappmchthe decidmofpickhg om problemfrom a setof prob-lems, keepthe following in mind:

-Nosinglepohtoffailure-ifyouaregohgtoaskjustOI1equestiORryoushouldmtpick a problemwherethecandidatewouldpassthehterviewif theygetorle particularhsight-Thebestof the cmdi-datescanmissonesimpleinsight. Thereshouldbe at leasttwo orthreeopportunitiesfor the candidatesto redeemthemselves.Forexamplerthe problemsh the dymamieprogrammmgsect1OI1cmdmostalwaysbe solvedthough(1.)agreedyalgorithm that isfastbutsuboptimumor (2.) abrute-forcealgorithmthatis slowbutoptimum. In suchcases, evenif the candidatecannotget the keyゥ T ィ エ theycanstill demonstratesomeproblemsolvingabilities.

- No unnecessarydomainknowledge-itゥ ウ ッ エ a goodideato quiza candidateon advancedgraphalgorithmsif the job doesnot re-quire it and the candidatedoesnot claim any specialknowledge

of the field. H G ィ ・ exceptionto this rule is if you want to test thecandidate'sresponseto a high-stresssituation.)

- Covermultiple areas-evenif you are responsiblefor testingthecandidateonjust algorithms, you couldeasilypick a problemthatalsoexposessomeaspectsof designandcoding.

- Possiblemultiple solutions-ifa givenproblemhasmultiple goodsolutions, the chancesof a goodcandidatecomingup with a goodsolution increases. It also gives you more freedom as an inter-viewer to maneuverthe intervieweein the directionof oneof thegood solutions. Also, a greatcandidatemay finish with one so-lution soonenoughto discussotherapproachesandthe tradeoffsinvolved.

Oftennewinterviewershaveanincorrectnotionof howtoughor'easya problemis for a thirty minute or onehour interview. It is usually agoodideato calibratethe toughnessof a problemby askingoneof yourcolleaguesto solveit andseehowmuchdifficulty theyhavewith it.

107

13.3 CONDUCTING THE INTERVIEW

Conductinga goodinterviewis like juggling a lot of piecesエ ッ ァ ・ エ ィ ・ a エ

high level, you want to askyourquestionandevaluatethe candidate'sresponses.Sincesomanythingscanhappenin an interview thatcouldhelp you make a decision, it is important to take notes. At the sametime, it is importantto keepa conversationgoingwith the 」 。 ョ 、 ゥ 、 。 エ ・ ャ 、

help them out whereverthey get stuck. What works bestis to haveaseriesof hints workedout prior to the interview andyou providethesehintsprogressivelyasneeded.Comingupwith theright setof hints ュrequiresomethinking. You do not want to give awaythe problem, yetfind awayfor thecandidateto makeprogress.Therearea few situationsthatcanthrow you off board:

A candidatethat getsstuckandshutsup: Somecandidatescangetintimidatedby theproblemor the processandjust shutup. Usually, insuch ウ ゥ エ オ 。 エ ゥ ッ ウ the candidate'sperformancemay not reflect their truecaliber. In suchsituations, it is importantto put the candidateat easeby mentioningthat theproblemis toughanda goodway of proceedingwouldbeto think out loud, soyou canguidetheir thinking.

A verbosecandidate:Theotherclassof candidatesthatcanイ ・ ョ 、 ・ イ Q

interviewineffectiveis thecandidateswho go onin エ 。 ァ ・ エ ゥ 。 ャ directionsandkeepon talking without makingprogress.Herealsoit is importantto takecontrolof theconversationandassertthatthis line of conversationis notmakinganyprogresstowardstheproblem.

An overconfidentcandidate:It is notuncommonto meetcandidateswho weakentheir own caseby insistingthat their wrongansweris cor-recto In orderto givethecandidatea fair chance, it is importantto demon-

13.3. CONDUCTINGTHEINTERVIEWCHAPTER13. CONDUCTINGANINTERVIEW106

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

13.4 SCORINGAND REPORTING

strateto thecandidatethattheyaremakingamistake, sotheycancorrecti t. Often thebestway of doing this is to constructa testcasewherethecandidate'ssolutionbreaksdown.

At theendof aninterview, mosttimestheinterviewershavea goodideaof how theywantto scorethecandidate.But, in general, it is a goodideato keepョ ッ エ ・ ウ ャ 、 revisit thembeforemakinga final decision.It is oftenagoodideato standardizehowyouscorebasedonthingslike whichhintsyou hadto give to makeprogressor how manyof your intendedques-tionswasthecandidateableto getto. While isolatedminormistakescanbe ignoredin mostcases, sometimeswhenyou look at all the mistakestogether, a coherentpicture of weaknessin a certainareamay emerge,suchas consistentlack of attentionto details or unfamiliarity with thesyntaxof a language.

In casesof indecision, we havefound エ ィ 。 エ ゥ ウ alwaysbetterto erron the sideof cautionandwait for thenextcandidateinsteadof makinga badhiring 、 ・ 」 ゥ ウ ゥ ッ ョ N ャ ・ ultimate litmus testis alwaysimaginingthecandidatereplacingavaluablememberof your エ ・ 。 ョ 、 whetheror notthatseemslike a welcomechange.

Part III

Solutions

CHAPTER13. CONDUCTINGANi n t e r v i e ヲ108

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

13.4 SCORINGAND REPORTING

strateto thecandidatethattheyaremakingamistake, sotheycancorrecti t. Often thebestway of doing this is to constructa testcasewherethecandidate'ssolutionbreaksdown.

At theendof aninterview, mosttimestheinterviewershavea goodideaof how theywantto scorethecandidate.But, in general, it is a goodideato keepョ ッ エ ・ ウ ャ 、 revisit thembeforemakinga final decision.It is oftenagoodideato standardizehowyouscorebasedonthingslike whichhintsyou hadto give to makeprogressor how manyof your intendedques-tionswasthecandidateableto getto. While isolatedminormistakescanbe ignoredin mostcases, sometimeswhenyou look at all the mistakestogether, a coherentpicture of weaknessin a certainareamay emerge,suchas consistentlack of attentionto details or unfamiliarity with thesyntaxof a language.

In casesof indecision, we havefound エ ィ 。 エ ゥ ウ alwaysbetterto erron the sideof cautionandwait for thenextcandidateinsteadof makinga badhiring 、 ・ 」 ゥ ウ ゥ ッ ョ N ャ ・ ultimate litmus testis alwaysimaginingthecandidatereplacingavaluablememberof your エ ・ 。 ョ 、 whetheror notthatseemslike a welcomechange.

Part III

Solutions

CHAPTER13. CONDUCTINGANi n t e r v i e ヲ108If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

111

m= (1+u)/2;if (A[m] < K) {

1 = m + 1;} else if (A[m] == K) {

イ・エオイョ Q[

} else {u = m-1;

789012345678

Ma

ゥQTQTTゥQa

ゥQT

return -1;

Solution1.3: A straightforwardway to find an elementlarger than agivenvalue k is to look for k via a binarysearchandthen, if k is found,walk thearrayforward (linearly)until eitherthefirst elementlargerthank is ・ョ」ッオ エ・イ・、 or the endof the arrayis reached.If k is not found, abinarysearchwill endup pointing to eitherthe next largestvalue afterK in the array, in which caseno further action is requiredor the nextsmallestvaluein which casethenextelementis thenextlargestvalue.

The キッ 」。ウ・ イオ ゥュ・ of this algorithm is eHIMエィ・ input of allvaluesmatchingK , exceptfor the last one (which is greaterthanK) , istheworst-case.

Thesolutionto this problemis to replacethelinearscanwith abinarysearchin the secondpart of the algorithm, which leadsto the desiredelementto befoundin o H ャ ッ ァ I time.

Solution 1.4: Since the array containsdistinct integersand is sorted,for any i > 0, a{ゥ} M 1] + 1. ThereforeB[i] = A[i] - i is alsonondecreasing.It follows thatwe cando a binary searchfor a in B tofind anindexsuchthatA[i] = i. (We donotneedto actuallycreateB, wecansimplyuseA[i] - i whereverB[i] is referenced.)

Solution1.5: The key ideahereis to simultaneouslydo abinarysearchfor theendof thearrayaswell asthekey. We try to look for A[2k ] in thek-th stepandcatch・ ク 」 ・ ー ッ ョ ウ for successivevaluesof k till eitherwehitanexceptionor we hit a numbergreaterthanor equalto b. Thenwe doa binary searchfor b betweenindices 2k - 1 and 2k . The runtime of thesearchalgorithmis 0 Hャ ッァ N In code:

int BinarySearchlnUnboundedArray(int* A , int b) {int k = 0;while (true) {

int c;try {

c = A[(l « k)if (c == b) {

return (1 « k)

-1];

GGGMアオa

Gdヲo

Oqo

Solution1.1: One of the fastestways to invert a ヲ。ウエMァイッキゥ ュッョッᆳ

tone ヲ オ ョ 」 エ ゥ ッ H ウ オ 」 ィ ashesquarefmction)is to do a binarysearchm apmcomputediableofthefmctiOR.Sincethe squamroot for the largestS R ゥ エ オ ョ ョ ・ 、 ゥ ョ ・ イ canbe representedin 16 「 webuild an arrayof leRgth216suchthat i-th elementh the arrayisz2.Whenwewaatto

mputesquareroot for a ァゥカ・ョュュ「・イ キ・ look for thelargestnum-ber iRthe arrav that is still smallerthmn.Becausethe s ア オ 。 イ ・ root ゥ S

。 エゥ カ・ ウュ。ャ lゥエ is ヲ 。 エ ッ computeit P エ ィ ・ fly thanto precomputeit.

Iunsigned int sqrLsearch(unsignedint input) { Iint begin = 0;int end = 65536;while (begin + 1 < end){

int 'mid = begin + (end - bef? in) !. 2;unsigned ゥ セ t mid_sqr = mid * mid;

if (mld_sqr == input) {return mid;

} else if (mid_sqr > input) {end = mid;

} else {begin = mid;

Searching

return begin;

QRSTUVWXY

オョオ

オュ

Solution1.2:

public class b ゥ ョ s ・ 。 イ サstatic int search( int [] A , int K ) {

in t 1 = 0;int u = A.length-1;int m;while ( 1 <= U ) {

Chapter1

Saエー「ヲッ

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

111

m= (1+u)/2;if (A[m] < K) {

1 = m + 1;} else if (A[m] == K) {

イ・エオイョ Q[

} else {u = m-1;

789012345678

Ma

ゥQTQTTゥQa

ゥQT

return -1;

Solution1.3: A straightforwardway to find an elementlarger than agivenvalue k is to look for k via a binarysearchandthen, if k is found,walk thearrayforward (linearly)until eitherthefirst elementlargerthank is ・ョ」ッオ エ・イ・、 or the endof the arrayis reached.If k is not found, abinarysearchwill endup pointing to eitherthe next largestvalue afterK in the array, in which caseno further action is requiredor the nextsmallestvaluein which casethenextelementis thenextlargestvalue.

The キッ 」。ウ・ イオ ゥュ・ of this algorithm is eHIMエィ・ input of allvaluesmatchingK , exceptfor the last one (which is greaterthanK) , istheworst-case.

Thesolutionto this problemis to replacethelinearscanwith abinarysearchin the secondpart of the algorithm, which leadsto the desiredelementto befoundin o H ャ ッ ァ I time.

Solution 1.4: Since the array containsdistinct integersand is sorted,for any i > 0, a{ゥ} M 1] + 1. ThereforeB[i] = A[i] - i is alsonondecreasing.It follows thatwe cando a binary searchfor a in B tofind anindexsuchthatA[i] = i. (We donotneedto actuallycreateB, wecansimplyuseA[i] - i whereverB[i] is referenced.)

Solution1.5: The key ideahereis to simultaneouslydo abinarysearchfor theendof thearrayaswell asthekey. We try to look for A[2k ] in thek-th stepandcatch・ ク 」 ・ ー ッ ョ ウ for successivevaluesof k till eitherwehitanexceptionor we hit a numbergreaterthanor equalto b. Thenwe doa binary searchfor b betweenindices 2k - 1 and 2k . The runtime of thesearchalgorithmis 0 Hャ ッァ N In code:

int BinarySearchlnUnboundedArray(int* A , int b) {int k = 0;while (true) {

int c;try {

c = A[(l « k)if (c == b) {

return (1 « k)

-1];

GGGMアオa

Gdヲo

Oqo

Solution1.1: One of the fastestways to invert a ヲ。ウエMァイッキゥ ュッョッᆳ

tone ヲ オ ョ 」 エ ゥ ッ H ウ オ 」 ィ ashesquarefmction)is to do a binarysearchm apmcomputediableofthefmctiOR.Sincethe squamroot for the largestS R ゥ エ オ ョ ョ ・ 、 ゥ ョ ・ イ canbe representedin 16 「 webuild an arrayof leRgth216suchthat i-th elementh the arrayisz2.Whenwewaatto

mputesquareroot for a ァゥカ・ョュュ「・イ キ・ look for thelargestnum-ber iRthe arrav that is still smallerthmn.Becausethe s ア オ 。 イ ・ root ゥ S

。 エゥ カ・ ウュ。ャ lゥエ is ヲ 。 エ ッ computeit P エ ィ ・ fly thanto precomputeit.

Iunsigned int sqrLsearch(unsignedint input) { Iint begin = 0;int end = 65536;while (begin + 1 < end){

int 'mid = begin + (end - bef? in) !. 2;unsigned ゥ セ t mid_sqr = mid * mid;

if (mld_sqr == input) {return mid;

} else if (mid_sqr > input) {end = mid;

} else {begin = mid;

Searching

return begin;

QRSTUVWXY

オョオ

オュ

Solution1.2:

public class b ゥ ョ s ・ 。 イ サstatic int search( int [] A , int K ) {

in t 1 = 0;int u = A.length-1;int m;while ( 1 <= U ) {

Chapter1

Saエー「ヲッ

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

112 CHAPTER1. SEARCHING

9 I } e I s e if (c >= b) {10 I break;11112 I }13 I catch (exception e)14 I break;15 I }16 I k++;17 I }18 I II Now do a binary search between indices 2/\(k-1) and (2/\k)

19 I int begin = 1 « (k -1);20 lin tend = (1 << k) - 1;21 I while (begin + 1 > end) {22 I int mid = begin + (end - begin) I 2;23 I try {24 I if (A[mid] == b) {25 I return mid;26 I } else if (A[mid] < b) {27 I begin = mid;28 I } else {29 I end = mid;30 I31 I }32 I catch H ・ ク 」 ・ ー エ ゥ ッ ・ I

33 I end = mid;34 135 I }36 I I I Nothing matched b37 I return -1;38

Solution1.6: In the first step, we build an arrayof 216 integersthat isinitializedto P ャ 、 for everynumberin thefilet wetakeits 16mostsignif-icantbit to indexinto this arrayandincrementthatnumber.Sincetherearelessthan232 numbersin thefilet thereis boundto beonenumberinthearraythat is lessthan216. This tells us thatthereis at leastonenum-bermissingamongthe possiblenumberswith thoseupperbits. In thesecondpass, we canfocus ャ ケ ッ ョ thenumbersthatmatchthis criterionanduseabit-vectorof size216 to identify oneof themissingnumbers.

Solution 1.7: The simplest algorithm is a "loop ェ ッ ゥ ョ ゥ N ・ N キ 。 ャ ォ ゥ ァ

throughall the elementsof one array and comparingthem to the ele-mentsof theotherarray.This hasO(m· n) time complexity, regardlessofwhetherthearraysaresortedor オ ョ ウ ッ イ 、 Z

1 I for each unique element in A2 I for each unique element in B31 ifA=B4 I include A in output

113

Howeversinceboth the arraysare sorted, we can makesomeOD-

timizations. First, ゥョ ・ rigM array}we cm usebinary searchto fiAwhetheytheelementexIStsratherthanscaImhgtheentirearray:

1 I for each unique element in A2 I use binary search to find A in B3 I if found I include A in output

Now our algorithmshouldbe o H N log2 m). We shouldchoosethelarger set for the izmer loop (i.e-F bimry search)siRceifn < m therlュ ャ ッ ァ H I ᄏ ャ ッ ァ H ュ I N

ャ ゥ ウ is the bestsolution if one set is much smallerthan the otherHoweverit ismotopHmalfor caseswherethesetsizesaresimilar-becausewe aremotusingthefact thatbotharraysaresortedto our advmtage-II1tkat caseFa linearscmthroughboththearrayshtandemwill workbestasshownin this Pythoncode:

23456789

101112131415161718192021

U I::.l N エ イ y l i i ャ 。 rIn t e rsect (n I ml

a I b):#construct sorted sets of random numbersof size n ond mA = []for i in range(n) :

A. append(random.randint(a I b))A. sort ()

B = []for j in range(m) :

B. append(random.randint(a I b))B. sort ()

return LinearIntersect(AI B)

アオaエfS

uqjョオ

GGO

TWm

アオ

def LinearIntersect(AI B):output = []ACounter = 0BCounter = 0lastMatch = Nonewhik AC01mter<len(A)andBC01mter<len(B):

if A[ACounter] == B[BCounter] and A[ACounter] !=ャ 。 ウ エ ャ | 。 エ 」 ィ Z

lastMatch = A[ ACounter]output.append(lastMatch)ACounter = ACounter + 1BCounter = BCounter + 1

elif A[ACounter] < B[BCounter]:ACounter = ACounter + 1

else:BCounter = BCounter + 1

return output

The runtimefor this algorithmis o H ュ I .

Solution1.8: A simpleway to approachthis problemis to hasheach

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

112 CHAPTER1. SEARCHING

9 I } e I s e if (c >= b) {10 I break;11112 I }13 I catch (exception e)14 I break;15 I }16 I k++;17 I }18 I II Now do a binary search between indices 2/\(k-1) and (2/\k)

19 I int begin = 1 « (k -1);20 lin tend = (1 << k) - 1;21 I while (begin + 1 > end) {22 I int mid = begin + (end - begin) I 2;23 I try {24 I if (A[mid] == b) {25 I return mid;26 I } else if (A[mid] < b) {27 I begin = mid;28 I } else {29 I end = mid;30 I31 I }32 I catch H ・ ク 」 ・ ー エ ゥ ッ ・ I

33 I end = mid;34 135 I }36 I I I Nothing matched b37 I return -1;38

Solution1.6: In the first step, we build an arrayof 216 integersthat isinitializedto P ャ 、 for everynumberin thefilet wetakeits 16mostsignif-icantbit to indexinto this arrayandincrementthatnumber.Sincetherearelessthan232 numbersin thefilet thereis boundto beonenumberinthearraythat is lessthan216. This tells us thatthereis at leastonenum-bermissingamongthe possiblenumberswith thoseupperbits. In thesecondpass, we canfocus ャ ケ ッ ョ thenumbersthatmatchthis criterionanduseabit-vectorof size216 to identify oneof themissingnumbers.

Solution 1.7: The simplest algorithm is a "loop ェ ッ ゥ ョ ゥ N ・ N キ 。 ャ ォ ゥ ァ

throughall the elementsof one array and comparingthem to the ele-mentsof theotherarray.This hasO(m· n) time complexity, regardlessofwhetherthearraysaresortedor オ ョ ウ ッ イ 、 Z

1 I for each unique element in A2 I for each unique element in B31 ifA=B4 I include A in output

113

Howeversinceboth the arraysare sorted, we can makesomeOD-

timizations. First, ゥョ ・ rigM array}we cm usebinary searchto fiAwhetheytheelementexIStsratherthanscaImhgtheentirearray:

1 I for each unique element in A2 I use binary search to find A in B3 I if found I include A in output

Now our algorithmshouldbe o H N log2 m). We shouldchoosethelarger set for the izmer loop (i.e-F bimry search)siRceifn < m therlュ ャ ッ ァ H I ᄏ ャ ッ ァ H ュ I N

ャ ゥ ウ is the bestsolution if one set is much smallerthan the otherHoweverit ismotopHmalfor caseswherethesetsizesaresimilar-becausewe aremotusingthefact thatbotharraysaresortedto our advmtage-II1tkat caseFa linearscmthroughboththearrayshtandemwill workbestasshownin this Pythoncode:

23456789

101112131415161718192021

U I::.l N エ イ y l i i ャ 。 rIn t e rsect (n I ml

a I b):#construct sorted sets of random numbersof size n ond mA = []for i in range(n) :

A. append(random.randint(a I b))A. sort ()

B = []for j in range(m) :

B. append(random.randint(a I b))B. sort ()

return LinearIntersect(AI B)

アオaエfS

uqjョオ

GGO

TWm

アオ

def LinearIntersect(AI B):output = []ACounter = 0BCounter = 0lastMatch = Nonewhik AC01mter<len(A)andBC01mter<len(B):

if A[ACounter] == B[BCounter] and A[ACounter] !=ャ 。 ウ エ ャ | 。 エ 」 ィ Z

lastMatch = A[ ACounter]output.append(lastMatch)ACounter = ACounter + 1BCounter = BCounter + 1

elif A[ACounter] < B[BCounter]:ACounter = ACounter + 1

else:BCounter = BCounter + 1

return output

The runtimefor this algorithmis o H ュ I .

Solution1.8: A simpleway to approachthis problemis to hasheach

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

115

Qᄋ

71QU,?fnORU,4·,quB

G。l-m41-ra

PairSum(ar1, 4) = (0 , 0)p 。 ゥ イ s オ ュ H 。 イ Q ] (0 , 1)PairSum(ar1, QPI HS 3)PairSum(ar1, 13) = (4 , 5)PairSum(ar1, 15) = (5 , 6)PairSum(ar1, 17) = None

This algorithmruns in 0 HI time since it makesonly a single passthroughthelist andthework doneinsidetheloop is constant(assumingwe havea nice hashfunction that gives us a constanttime hashinsertandlookup).

Solution1.10: Hereessentiallyweneedto efficiently イ ・ ー イ ・ ウ ・ エ エ キ ッ ュ オ ャ ᆳ

tisets(onefor charactersin the anonymousletter and P ・ for charactersin themagazine)andseeif oneis a subsetof the other.

The most direct way of doing this would be to build a hashtableNI, wherethe key is a characterandits value is the numberof times itappearsin the ュ 。 ァ 。 コ ゥ イ N Oncethis is built, we canscanthe anonymousletter characterby characteranddecrementthe correspondingcountin111[. If the countgoesto zero, we deletethe characterfrom NI. We canwrite theanonymousletterwith charactersin themagazineiff we cangoover the entire anonymousletter andfind everycharacterin NI with apositivecount.

If the charactersarecodedin ASCII, we could do awaywith NI andusea256entryintegerarrayA, with A[i] 「 ・ ゥ ウ ・ エ to thenumberof timesthe characteri appearsin themagazine.

One way to improve performanceof the approachoutlined abovewhenthemagazineis very long is to processthemagazinein segments;in thisway, if thelettercanbewrittenwith a relativelysmallinitial prefixof themagazine, thewholemagazinedoesnothaveto beprocessed.Thesegmentsmaybe of fixed sizeor a doublingstrategymaybe employed.This doesnothelp theworst-casecomplexity(sinceit ュ 。 ケ ッ エ bepossi-ble to write theletterwith thecharactersin themagazineandthis cannotbedeterminedwithoutinspectingtheentiremagazine)butspeedsupthebest-caseandpossiblytheaverage-case.

Solution1.11: Hereessentiallyeachuseris associatedwith a setof at-エ イ ゥ 「 オ エ ・ ウ ャ 、 we needto find usersassociatedwith a given set of at-tributesquickly. A hashtablewould be a perfectsolutionherebut weneeda hashfunction over the set of attributes. There are a couple ofgoodways of doing this. If the numberof attributesis small, thenwecan イ ・ ー イ ・ ウ ・ エ the setas a 「ゥエNNN[ イ・」エッ キィ・イ・ eachbit イ ・ ー イ ・ ウ ・ エ ウ a specificattribute. Oncewe havethis canonicalイ ・ ー イ ・ ウ ・ エ 。 エ ゥ ッ ョ of set, then it iseasyto useany hashfunction that transformsthis bit-vector into a de-

SEARCHING

ord basedon its sorted イ ・ ー イ ・ ウ ・ エ 。 エ ゥ ッ ョ (i.e., "logarithm" and "algo-rithm" wouldbothbehashedas B 。 ァ ィ ゥ ャ ュ ッ B I N ャ ゥ ウ ensuresthat all the

イ 。 ュ ウ of a givenwordmapto thesamehashvalue

CHAPTER1.114

def anagrams( dictionary):output = []map = {}# for each wordfor word in dictionary:

# sort the letterssorted word = sortchars(word)f add-thE z 、 to the list held in a dictionary# under its sorted keyif sorted_word not in map:

map[sorted_word]= [word]else:

map[ sorted_word]. append(word)# for each dictionary keyfor k in map.keys():

# retu イ セ エ ィ セ lis t if it has more than one itemif len(map[k]) > 1:

output.append(map[k])return output

ᄋᄋ

ᄋᄋゥ

bGGG,GlJRv

Maeュッ

カQᄋ}

nuvᄋ

ウヲイエ

bjf

。ウゥhitn

eavaa

bour--sto-eqオエエF

エa

rTAeAU

QRSTUVWXY

um

オュオオュ

ョョdmエ

a ウ ー i ・ run:

>> anagrams( ( " algorithm" ッ 、 B B ャ ッ ァ 。 イ ゥ ョ B "dog" , "snute"))[[PalgLithIIl' , FlogarithmPLUgor, 'dogP]]

Solution Q t ィ ゥ ウ couldbe easilydonein o H エ ゥ ュ ・ by searchingforallpossiblevaluesof i adjsuchthata h } a { ェ } =K.

we coulddo significmtly betterby stormgthevaluesfrom thearrayiz1ahast1table.Ther1foreachmw valuerwe checkto seeif its complepmeI1tiip k minusthevalue)hasalreadybeerIseenandif SOy whatis the

、 ・ x j ス e h サ ・ イ ・ ゥ ウ 。 p ケ エ ebuilt-in dictionaryobject 。 ウ エ ィ ・ hashtable:

def PairSum(arr , K):h = {}for i in range(len(arr) ) :

complement= K - arr [i]h [ arr[ i]] = iif complement in h:

return h[complement], i

Tアオa

fSTu

This givesthe following results, By ィ・イ・Nエセ・ イ・Nエオイセ エ[ valuesof the func-エ ゥ ッ ュ イ ・ 。 ・ エ ョ 、 ゥ 」 ・ ウ of ・ ャ ・ ュ ・ エ ィ 。 エ addup to K:

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

115

Qᄋ

71QU,?fnORU,4·,quB

G。l-m41-ra

PairSum(ar1, 4) = (0 , 0)p 。 ゥ イ s オ ュ H 。 イ Q ] (0 , 1)PairSum(ar1, QPI HS 3)PairSum(ar1, 13) = (4 , 5)PairSum(ar1, 15) = (5 , 6)PairSum(ar1, 17) = None

This algorithmruns in 0 HI time since it makesonly a single passthroughthelist andthework doneinsidetheloop is constant(assumingwe havea nice hashfunction that gives us a constanttime hashinsertandlookup).

Solution1.10: Hereessentiallyweneedto efficiently イ ・ ー イ ・ ウ ・ エ エ キ ッ ュ オ ャ ᆳ

tisets(onefor charactersin the anonymousletter and P ・ for charactersin themagazine)andseeif oneis a subsetof the other.

The most direct way of doing this would be to build a hashtableNI, wherethe key is a characterandits value is the numberof times itappearsin the ュ 。 ァ 。 コ ゥ イ N Oncethis is built, we canscanthe anonymousletter characterby characteranddecrementthe correspondingcountin111[. If the countgoesto zero, we deletethe characterfrom NI. We canwrite theanonymousletterwith charactersin themagazineiff we cangoover the entire anonymousletter andfind everycharacterin NI with apositivecount.

If the charactersarecodedin ASCII, we could do awaywith NI andusea256entryintegerarrayA, with A[i] 「 ・ ゥ ウ ・ エ to thenumberof timesthe characteri appearsin themagazine.

One way to improve performanceof the approachoutlined abovewhenthemagazineis very long is to processthemagazinein segments;in thisway, if thelettercanbewrittenwith a relativelysmallinitial prefixof themagazine, thewholemagazinedoesnothaveto beprocessed.Thesegmentsmaybe of fixed sizeor a doublingstrategymaybe employed.This doesnothelp theworst-casecomplexity(sinceit ュ 。 ケ ッ エ bepossi-ble to write theletterwith thecharactersin themagazineandthis cannotbedeterminedwithoutinspectingtheentiremagazine)butspeedsupthebest-caseandpossiblytheaverage-case.

Solution1.11: Hereessentiallyeachuseris associatedwith a setof at-エ イ ゥ 「 オ エ ・ ウ ャ 、 we needto find usersassociatedwith a given set of at-tributesquickly. A hashtablewould be a perfectsolutionherebut weneeda hashfunction over the set of attributes. There are a couple ofgoodways of doing this. If the numberof attributesis small, thenwecan イ ・ ー イ ・ ウ ・ エ the setas a 「ゥエNNN[ イ・」エッ キィ・イ・ eachbit イ ・ ー イ ・ ウ ・ エ ウ a specificattribute. Oncewe havethis canonicalイ ・ ー イ ・ ウ ・ エ 。 エ ゥ ッ ョ of set, then it iseasyto useany hashfunction that transformsthis bit-vector into a de-

SEARCHING

ord basedon its sorted イ ・ ー イ ・ ウ ・ エ 。 エ ゥ ッ ョ (i.e., "logarithm" and "algo-rithm" wouldbothbehashedas B 。 ァ ィ ゥ ャ ュ ッ B I N ャ ゥ ウ ensuresthat all the

イ 。 ュ ウ of a givenwordmapto thesamehashvalue

CHAPTER1.114

def anagrams( dictionary):output = []map = {}# for each wordfor word in dictionary:

# sort the letterssorted word = sortchars(word)f add-thE z 、 to the list held in a dictionary# under its sorted keyif sorted_word not in map:

map[sorted_word]= [word]else:

map[ sorted_word]. append(word)# for each dictionary keyfor k in map.keys():

# retu イ セ エ ィ セ lis t if it has more than one itemif len(map[k]) > 1:

output.append(map[k])return output

ᄋᄋ

ᄋᄋゥ

bGGG,GlJRv

Maeュッ

カQᄋ}

nuvᄋ

ウヲイエ

bjf

。ウゥhitn

eavaa

bour--sto-eqオエエF

エa

rTAeAU

QRSTUVWXY

um

オュオオュ

ョョdmエ

a ウ ー i ・ run:

>> anagrams( ( " algorithm" ッ 、 B B ャ ッ ァ 。 イ ゥ ョ B "dog" , "snute"))[[PalgLithIIl' , FlogarithmPLUgor, 'dogP]]

Solution Q t ィ ゥ ウ couldbe easilydonein o H エ ゥ ュ ・ by searchingforallpossiblevaluesof i adjsuchthata h } a { ェ } =K.

we coulddo significmtly betterby stormgthevaluesfrom thearrayiz1ahast1table.Ther1foreachmw valuerwe checkto seeif its complepmeI1tiip k minusthevalue)hasalreadybeerIseenandif SOy whatis the

、 ・ x j ス e h サ ・ イ ・ ゥ ウ 。 p ケ エ ebuilt-in dictionaryobject 。 ウ エ ィ ・ hashtable:

def PairSum(arr , K):h = {}for i in range(len(arr) ) :

complement= K - arr [i]h [ arr[ i]] = iif complement in h:

return h[complement], i

Tアオa

fSTu

This givesthe following results, By ィ・イ・Nエセ・ イ・Nエオイセ エ[ valuesof the func-エ ゥ ッ ュ イ ・ 。 ・ エ ョ 、 ゥ 」 ・ ウ of ・ ャ ・ ュ ・ エ ィ 。 エ addup to K:

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

116 CHAPTER1. SEARCHING

siredhashspace.Howeverif thespaceof possibleattributesis large, thenthebestway

to イ ・ ー イ ・ ウ ・ エ a setcanonicallywould be to sort the attributes. For thissorting, anyarbitraryorderingof attributeswill work. We can イ ・ ー イ ・ ウ ・thesortedlist of attributesin astringconcatenatingall theattributes.

Incidentally, if we want to groupusersbasedP ウ ゥ ュ ゥ ャ 。 イ ratherthanidenticalattributes, the problembecomessignificantlymoredifficul t. Acommonapproachis min-hashing. Essentially, we constructa setof kindependentィ 。 ウ ィ 」 エ ゥ ッ ョ ウ (k is chosenbasedonhow similarwe wantthesetsto be). Thenfor eachset8 we define

m k H ウ I ] ー ゥ イ ィ H I .

If two sets81 and82 havesimilarsetof attributesthenwith highprobabil-ity Mk(81) = Mk(82). Basedon this, we mapeachsetof attributes8 to asequenceof hashesM 1(8) . . .M k (8). Now theproblemhasbeenreducedto pairing usersthat havethe samehashsequence, which is similar tothe ッ イ ゥ ァ ャ 。 ャ problem. Here k canbevaried 。 ー ー イ イ ゥ 。 エ ・ ャ ケ to increaseordecreasetheprobabilityof matchfor a pair of slightly differentattributesets.

Solution1.12: The ideahereis very similar to hashing.Consideraverysimplehashfunction F(x) = x mod (n + 1). We canbuild a bit-vectorof length ョ Q that is initialized to 0 andfor everyelementin A, we setbit F(A[i]) to 1. Sincethereare ッ ョ ャ ケ ・ ャ ・ ュ ・ ョ エ ウ in the array, therehasto be at leastonebit in thevectorthat is not set. That would give us thenumberthatis not therein thearray.

An evensimplerapproachis to find themax (or min) elementin thearrayandreturnonemore (less)thanthat element. This approachwillnotwork if theextremalelementsarethe largest(smallest)valuesin thesetthattheentriesaredrawnfrom.

Solution1.13: Sincetheenergyis only relatedto theheightof therobot,we canignore ク ャ 、 Y co-ordinates.Let's saythat the pointswheretherobotgoesin successiveorderhaveheightshI,. . . ,hn . Let's assumethatthebatterycapacityis suchthatwith full battery, the robotcanclimb upB meters.Thenthe robotwill run out of batteryiff thereexist integersi and j suchthat i < j and ィ ェ ^ B. In otherwords, in orderto gofrom point i to point j , therobotneedsto climb morethanB points.So,wewould like to pick B suchthatfor anyi < j , wehaveB セ ィ ェ - hi'

If we 、 ゥ 、 ッ エ havethe constraintthat i < j , thenwe could just com-puteB asmax(h) min(h)butthismaybeanoverestimate:considerthecasewhentherobot is justgoingdownwards.

We cancomputetheminimumB in 0 (n) time if wekeepthe イ オ イ

117

min aswe do a sweep.In code:

1 'double BatteryCapacity(vector<double> h)2 I if (h.size() < 2) {3 I return 0;456789

1011121314 I }15 I return result;16

double min = h [0] ;double result = 0;for (i n t i = 1; i < h. s i z e(); ++ i )

if (h[i] - min> result) {result = h[ i] - min;

ィオMᆳ>hョ

ュョ(mrI

1,3.

‘A

Solution1.14: Let'sfirst considerjust thestrictmajoritycase.Thisprob-lem hasan elegantsolutionwhenyou makethe following observation:if you take any two distinct elementsfrom the streamand throw themaway, the majority ・ ャ ・ ュ ・ イ ・ ュ 。 ゥ ョ ウ the majorityof the イ ・ ュ 。 ゥ ョ ゥ ・ ャ ・ ᆳ

ments(weassumedtherewasamajorityelementtobeginwith). Therea-soninggoesasfollows: let's saythe majority element occurredm timesout of ・ ャ ・ ュ ・ ョ エ ウ in the streamsuchthat m/n > 1/2. The two distinctelementsthatwe chooseto throw canhaveat mostoneof the majorityelements.Henceafter discardingthem, the ratio of the previouslyma-jority elementcouldbeeitherm/ HM 2) or (m - 1)/ HM 2). It is easytoverify thatif m/n> 1/2, then ュ O H M R ᄏ H ュ M Q I O H M 2) > 1/2.

Now, aswe readthestreamfrom beginningto theend, assoonasweencountermorethanonedistinctelement, wecandiscardoneinstanceofeachelementandwhatwe areleft with in the endmustbe the majorityelement.

1 I string FindMajority(stream* s) {2 I string candidateI next_word;3 I int count = 0;4 I while H ウ ^ g ・ エ n ・ ク エ H F ョ ・ ク エ ⦅ キ ッ イ 、 I )5 I if (count == 0) {6 I candidate= next_word;7 I count = 1;8 I } else if (candidate== next_word)9 I count++;

10 I } else {11 I 」ッオョエ [

12 I13 I }14 I return candidate;15

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

116 CHAPTER1. SEARCHING

siredhashspace.Howeverif thespaceof possibleattributesis large, thenthebestway

to イ ・ ー イ ・ ウ ・ エ a setcanonicallywould be to sort the attributes. For thissorting, anyarbitraryorderingof attributeswill work. We can イ ・ ー イ ・ ウ ・thesortedlist of attributesin astringconcatenatingall theattributes.

Incidentally, if we want to groupusersbasedP ウ ゥ ュ ゥ ャ 。 イ ratherthanidenticalattributes, the problembecomessignificantlymoredifficul t. Acommonapproachis min-hashing. Essentially, we constructa setof kindependentィ 。 ウ ィ 」 エ ゥ ッ ョ ウ (k is chosenbasedonhow similarwe wantthesetsto be). Thenfor eachset8 we define

m k H ウ I ] ー ゥ イ ィ H I .

If two sets81 and82 havesimilarsetof attributesthenwith highprobabil-ity Mk(81) = Mk(82). Basedon this, we mapeachsetof attributes8 to asequenceof hashesM 1(8) . . .M k (8). Now theproblemhasbeenreducedto pairing usersthat havethe samehashsequence, which is similar tothe ッ イ ゥ ァ ャ 。 ャ problem. Here k canbevaried 。 ー ー イ イ ゥ 。 エ ・ ャ ケ to increaseordecreasetheprobabilityof matchfor a pair of slightly differentattributesets.

Solution1.12: The ideahereis very similar to hashing.Consideraverysimplehashfunction F(x) = x mod (n + 1). We canbuild a bit-vectorof length ョ Q that is initialized to 0 andfor everyelementin A, we setbit F(A[i]) to 1. Sincethereare ッ ョ ャ ケ ・ ャ ・ ュ ・ ョ エ ウ in the array, therehasto be at leastonebit in thevectorthat is not set. That would give us thenumberthatis not therein thearray.

An evensimplerapproachis to find themax (or min) elementin thearrayandreturnonemore (less)thanthat element. This approachwillnotwork if theextremalelementsarethe largest(smallest)valuesin thesetthattheentriesaredrawnfrom.

Solution1.13: Sincetheenergyis only relatedto theheightof therobot,we canignore ク ャ 、 Y co-ordinates.Let's saythat the pointswheretherobotgoesin successiveorderhaveheightshI,. . . ,hn . Let's assumethatthebatterycapacityis suchthatwith full battery, the robotcanclimb upB meters.Thenthe robotwill run out of batteryiff thereexist integersi and j suchthat i < j and ィ ェ ^ B. In otherwords, in orderto gofrom point i to point j , therobotneedsto climb morethanB points.So,wewould like to pick B suchthatfor anyi < j , wehaveB セ ィ ェ - hi'

If we 、 ゥ 、 ッ エ havethe constraintthat i < j , thenwe could just com-puteB asmax(h) min(h)butthismaybeanoverestimate:considerthecasewhentherobot is justgoingdownwards.

We cancomputetheminimumB in 0 (n) time if wekeepthe イ オ イ

117

min aswe do a sweep.In code:

1 'double BatteryCapacity(vector<double> h)2 I if (h.size() < 2) {3 I return 0;456789

1011121314 I }15 I return result;16

double min = h [0] ;double result = 0;for (i n t i = 1; i < h. s i z e(); ++ i )

if (h[i] - min> result) {result = h[ i] - min;

ィオMᆳ>hョ

ュョ(mrI

1,3.

‘A

Solution1.14: Let'sfirst considerjust thestrictmajoritycase.Thisprob-lem hasan elegantsolutionwhenyou makethe following observation:if you take any two distinct elementsfrom the streamand throw themaway, the majority ・ ャ ・ ュ ・ イ ・ ュ 。 ゥ ョ ウ the majorityof the イ ・ ュ 。 ゥ ョ ゥ ・ ャ ・ ᆳ

ments(weassumedtherewasamajorityelementtobeginwith). Therea-soninggoesasfollows: let's saythe majority element occurredm timesout of ・ ャ ・ ュ ・ ョ エ ウ in the streamsuchthat m/n > 1/2. The two distinctelementsthatwe chooseto throw canhaveat mostoneof the majorityelements.Henceafter discardingthem, the ratio of the previouslyma-jority elementcouldbeeitherm/ HM 2) or (m - 1)/ HM 2). It is easytoverify thatif m/n> 1/2, then ュ O H M R ᄏ H ュ M Q I O H M 2) > 1/2.

Now, aswe readthestreamfrom beginningto theend, assoonasweencountermorethanonedistinctelement, wecandiscardoneinstanceofeachelementandwhatwe areleft with in the endmustbe the majorityelement.

1 I string FindMajority(stream* s) {2 I string candidateI next_word;3 I int count = 0;4 I while H ウ ^ g ・ エ n ・ ク エ H F ョ ・ ク エ ⦅ キ ッ イ 、 I )5 I if (count == 0) {6 I candidate= next_word;7 I count = 1;8 I } else if (candidate== next_word)9 I count++;

10 I } else {11 I 」ッオョエ [

12 I13 I }14 I return candidate;15

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

It may seemthe abovecodeis エ。ォ HI エゥュ・ 」・ the inner loopmaytakek steps(decrementingcountfor all k entries)andtheloop goeson for ・ ウ N Howeverif you note that eachword in the streamcanonly beerasedonce, エィ・ ィ・ total time spenterasingeverythingis 0 HI

andtherestof thestepsinsidetheloop run in constanttime.The abovecode providesus with a k - 1 sizesetof words that is a

supersetof thewordsthatoccurmore エ ィ 。 ョ O ォ times. In orderto gettheexactset, we needto makeanotherpassover the ウ エ イ ・ 。 ュ ャ 、 countthenumberof timeseachword in thehashtableactuallyoccursso thatwekeeponly thewordswhich occurmorethann/k times.

The codeaboveassumesthere is a majority word in the stream;ifno word has a strict majority, it still returnsa string but there are noュ ・ 。 ョ ゥ ョ ァ ヲ オ ャ 。 イ 。 ョ エ ・ ・ ウ onwhatthat ウ エ Q Y キ ッ オ ャ 、 be.

Solution1.15: This is ・ウウ・ ゥ。ャャケ a generalizationof Problem1.14.Hereinsteadof discardingtwo distinctwords, we discardk distinctwordsatany given エ ゥ ュ ・ ャ 、 we areguaranteedthat all the wordsthat occurredmorethan1/k timesthelengthof thestreambeforediscardingcontinueto havemorethan1/k ヲイ。」エゥッ ッヲ copies.For implementingthis ウ エ イ 。 エ ・ ァ

weneedto keepahashtableof currentk candidates.Hereis anexamplecode:

1 I void FindFrequentItems(stream* s , hash_map<string, int >*word_set, in t k) {

2 I キッイ、⦅ウ・エ ^」ャ・。イ () ;3 I ウエイゥ キッイ、[

4 I キィ ゥQ ・Hウ ^g・エn・クエwッイ、HFキッイ、II {5 I hash_map<string, int >:: iterator i = word_set->find(word);6 I if (i == word ウ ・ エ ^ ・ ョ 、 ()) {7 I if (word_set->size() == k) {8 I II Hash table is full , decrement all counts, which9 I II is equivalent to discarding 7c distinct words.

10 I for (hash_map<string, int>::iterator j = キッイ、⦅ウ・エ

begin() ;11 I j != word_set->end() ;12 I ++j) {13 I Hェ ^ウ・」ッョ、I ;14 I if H ェ ^ ウ ・ 」 ッ ョ 、 ==0) {15 I キッイ、⦅ウ・エ ^・イ。ウ・HェI[

16 I17 I }18 I } else {19 I (*word_set)[word] = 1;20 I }21 I } else {22 I ゥ ^ウ・」ッョ、KK[

232425

return NULL;

119

Solution1.17: This is similar to Problem1.16but you just haveto con-tinue your binary searchtill the end evenif you find the elementthatyouwerelookingfor andalsokeeptrackof thelastelementthatmetthecriteria.

Recursionaddstheoverheadof functioncalls. The codeaboveis notliterally tail recursive, which meansthat an optimizing compileris un-likely to removethe recursivecalls; howevertherestill is a straightfor-warditerativesolution:

Solution 1.18: This problemrequiressomecreativeuseof the binarysearchidea.Let's saythatthe two arraysareAl andA2 andsaythat l of

1 I Node* SearchBST(Node* root , in t key) {2 I Node* result = NULL;3 I whi1e(root!= NULL) {4 I if H イ ッ ッ エ ^ ォ ・ ケ > key)5 I result = root;6 I root = イッッエ ^ャ・ヲエ[

7 I } else {8 I root = root->right;9 I

10 I }

11 I return result;12

1 INode* SearchBST(Node* root , in t key) {2 I whi1e(root != NULL) {3 I if (root->key == key) {4 I return root;5 I } else if H イ ッ ッ エ ^ ォ ・ ケ < key)6 I root = イッッエ ^ャ・ヲエ[

7 I } else {8 I root = root->right;9

1011

Solution1.16: A recursivesolutionis natural:

1 INode* SearchBST(Node* root , in t key)2 I if (r 0 0 t == nuI {3 I return NULL;4 I } else if H イ ッ ッ エ ^ ォ ・ ケ == key) {5 I return root;6 I } else if (root->key < key) {7 I return SearchBSTH イ ッ ッ エ ^ ャ ・ ヲ エ key);8 I } else {9 I return SearchBST(root->right , key);

1011

CHAPTER1. SEARCHING118

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

It may seemthe abovecodeis エ。ォ HI エゥュ・ 」・ the inner loopmaytakek steps(decrementingcountfor all k entries)andtheloop goeson for ・ ウ N Howeverif you note that eachword in the streamcanonly beerasedonce, エィ・ ィ・ total time spenterasingeverythingis 0 HI

andtherestof thestepsinsidetheloop run in constanttime.The abovecode providesus with a k - 1 sizesetof words that is a

supersetof thewordsthatoccurmore エ ィ 。 ョ O ォ times. In orderto gettheexactset, we needto makeanotherpassover the ウ エ イ ・ 。 ュ ャ 、 countthenumberof timeseachword in thehashtableactuallyoccursso thatwekeeponly thewordswhich occurmorethann/k times.

The codeaboveassumesthere is a majority word in the stream;ifno word has a strict majority, it still returnsa string but there are noュ ・ 。 ョ ゥ ョ ァ ヲ オ ャ 。 イ 。 ョ エ ・ ・ ウ onwhatthat ウ エ Q Y キ ッ オ ャ 、 be.

Solution1.15: This is ・ウウ・ ゥ。ャャケ a generalizationof Problem1.14.Hereinsteadof discardingtwo distinctwords, we discardk distinctwordsatany given エ ゥ ュ ・ ャ 、 we areguaranteedthat all the wordsthat occurredmorethan1/k timesthelengthof thestreambeforediscardingcontinueto havemorethan1/k ヲイ。」エゥッ ッヲ copies.For implementingthis ウ エ イ 。 エ ・ ァ

weneedto keepahashtableof currentk candidates.Hereis anexamplecode:

1 I void FindFrequentItems(stream* s , hash_map<string, int >*word_set, in t k) {

2 I キッイ、⦅ウ・エ ^」ャ・。イ () ;3 I ウエイゥ キッイ、[

4 I キィ ゥQ ・Hウ ^g・エn・クエwッイ、HFキッイ、II {5 I hash_map<string, int >:: iterator i = word_set->find(word);6 I if (i == word ウ ・ エ ^ ・ ョ 、 ()) {7 I if (word_set->size() == k) {8 I II Hash table is full , decrement all counts, which9 I II is equivalent to discarding 7c distinct words.

10 I for (hash_map<string, int>::iterator j = キッイ、⦅ウ・エ

begin() ;11 I j != word_set->end() ;12 I ++j) {13 I Hェ ^ウ・」ッョ、I ;14 I if H ェ ^ ウ ・ 」 ッ ョ 、 ==0) {15 I キッイ、⦅ウ・エ ^・イ。ウ・HェI[

16 I17 I }18 I } else {19 I (*word_set)[word] = 1;20 I }21 I } else {22 I ゥ ^ウ・」ッョ、KK[

232425

return NULL;

119

Solution1.17: This is similar to Problem1.16but you just haveto con-tinue your binary searchtill the end evenif you find the elementthatyouwerelookingfor andalsokeeptrackof thelastelementthatmetthecriteria.

Recursionaddstheoverheadof functioncalls. The codeaboveis notliterally tail recursive, which meansthat an optimizing compileris un-likely to removethe recursivecalls; howevertherestill is a straightfor-warditerativesolution:

Solution 1.18: This problemrequiressomecreativeuseof the binarysearchidea.Let's saythatthe two arraysareAl andA2 andsaythat l of

1 I Node* SearchBST(Node* root , in t key) {2 I Node* result = NULL;3 I whi1e(root!= NULL) {4 I if H イ ッ ッ エ ^ ォ ・ ケ > key)5 I result = root;6 I root = イッッエ ^ャ・ヲエ[

7 I } else {8 I root = root->right;9 I

10 I }

11 I return result;12

1 INode* SearchBST(Node* root , in t key) {2 I whi1e(root != NULL) {3 I if (root->key == key) {4 I return root;5 I } else if H イ ッ ッ エ ^ ォ ・ ケ < key)6 I root = イッッエ ^ャ・ヲエ[

7 I } else {8 I root = root->right;9

1011

Solution1.16: A recursivesolutionis natural:

1 INode* SearchBST(Node* root , in t key)2 I if (r 0 0 t == nuI {3 I return NULL;4 I } else if H イ ッ ッ エ ^ ォ ・ ケ == key) {5 I return root;6 I } else if (root->key < key) {7 I return SearchBSTH イ ッ ッ エ ^ ャ ・ ヲ エ key);8 I } else {9 I return SearchBST(root->right , key);

1011

CHAPTER1. SEARCHING118

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution1.19: Considertwo linesy ] 「ゥク andy ] ェ 「ェク suchthatN The i-th line intersectsthe line x = °at H P I andthe j-th lineintersectsthe line x = °at H P ェ I G s ゥ ュ ゥ ャ 。 イ エ ィ ・ ウ ・ lines intersectx = 1 at

the k smallestelementsof theunioncomefrom the first arrayand l - kelementscomefrom the secondarray. If this wereindeedtrue, thenwewould seethat Al [l - Q} aR{ォ -l] andA2[l - k] - Q aャ [l] (barringsomecornercaseswherewe reachtheendof thearray).

The other ゥ ョ エ ・ イ ・ ウ エ ゥ 「 ウ ・ イ カ 。 エ ゥ ッ ョ we canmakeis that if Al [l - 1] >A2[k -l] , thenwe shoulduseat leastonemoreelementfrom thesecondarrayin the k smallestelements.Similarly, if A2 [l - k - 1] > Al [l] , thenwe shoulduseat leastonemoreelementfrom thefirst array.Usingtheseエ キ ッ ャ ・ ア オ 。 ャ ゥ エ ゥ ・ ウ we canessentiallydo a binary searchon l. Note thatthis problemgivesyouplentyof cornercasesto worry about.In code:

1 lint FindOrderStat(const vector<int>& a1 ,2 I const vector<int>& a2 ,3 I unsigned int k) {4 I II Check the validity of input.5 I assert(a1.size()+ a2.size() >= k);6 I assert(k > 0);7 I I I Find an index begin <= 1 < end such that a1 [0].. a1 [1-1]8 I II and a2[O]..a2[k-l-1] are the smallest k numbers.9 I unsigned in t begin = max(O, k - a2. size0 ) ;

10 I unsigned intend = min( a1.size() I k);11 I while (begin < end) {12 I unsigned 1 = begin + (end - begin)12;13 I I I cM。ョ ・ include a1 [1] in the k smallest numbers?14 I if ((1 < al. size H I I H ォ M Q > PI H。Q{Q} < a2[k-1-1]))15 I begin = 1 + 1;16 I } e1;e if ((1> P I H ォ M Q < a2.size() I H 。 Q { Q M Q } > a2[

k-1])) {17 I II This is the case where we can discard a[l -l]18 I II from the set of k smallest numbers.19 I end = 1;20 I } else {21 I I I We found our 。 ョ ウ ・ イ since both the inequalities were

false.22 I begin = 1;23 I break;24 I25 I }26 I if (begin == 0) {27 I return a2 { ォ Q } [

28 I } else if (begin == k)29 I return a1[k-1];30 I } else {31 I return max(a1[begin -1]' a2 [k - begin -1]);3233

121

fH I = HM ォIᄋ zォᄋ

Using the above ・ ク ー イ ・ ウ ウ ゥ ッ we can searchfor the value of k suchthat f H s ォ I X G f H s ォ Q I by performingbinarysearchfor k (sincetheruntimeof this solutionis already X H ャ ッ ァ n) , we cando a linear searchaswell for simplicity). Oncewe have ヲ ッ オ ャ 、 the right valueof k, we cancomputethevalueof y by simply solvingthe ・ ア オ 。 エ ゥ ッ ヲ ッ イ f H I above.

H Q K bi ) and H Q ェ + bj ). Lines i and j intersectiff

HH ェIFH K「 ゥ \ ェ IIQHHゥ \ ェIFH 「 ゥ ^ ェK IIN

In otherwords, for the lines to intersect, ゥヲ \ ェ thenit mustbe thecasethat H \ ェ 「 ェ I or vice versa(ignoringthe trivial casewheretheyintersecton oneof theboundaries).

Henceif we sort the pairs H ゥ bi ) by ゥ and test that for successivepairs H 「 ゥ I 。 ョ 、 H ェ bj ) ゥヲ \ ェ + bj , we know that they do notintersect.If we do find a カゥッャ。エゥッ ッヲ this ゥ ョ ・ ア オ 。 ャ ゥ エ エ ィ ・ ョ wehavefoundoneof the intersectingpairs.

Sorting takes0 Hャッァ n) time and comparingsuccessivepairs takeso H I time. Hencethis canbedonein o H ャ ッ ァ エ ゥ ュ ・ N

Solution 1.20: One way to solve this is to sort the intervalsby theirlower boundaryand seeif their upperboundaryis also sortedin thesameorder. If not, we are sureto find somepair of indices l ,m where

ュ and 「 コ 「 ュ N This would be the pair we are looking for. Ifthe upperboundariesare also sorted, thenwe are guaranteedエ ィ 。 エ o

interval is completelycontainedin anotherinterval. Sincethis involvessortingfollowedby alinear ウ 」 。 ョ t ・ canget エ ャ 、 ッ ョ ・ in O(n ャ ッ ァ I time.

Solution1.21: The keyideahereis to sorttheendpointsof thelinesanddo a sweepfrom left to right. As we do the sweep, we maintaina listof lines that intersectthecurrentpositionaswell asthehighestline andits color. In order to quickly lookup the highestline amongthe set ofintersectinglines, we cankeepanorderedbinarytreedata-structureandto lookup thelinesby theendpointquickly, wecanmaintainahashtable.

Solution1.22: Define f H I to be Q [ l Q i i ャ ゥ H s ゥ I N We are ャ ッ ッ ォ ゥ ヲ ッ イ avalueof ウオ」ィ that f H I = S'. Clearly, F monotonicallyincreaseswithN Also, since°\ sG s the valueof ゥ ウ going to bebetween°andmaX(Si)'Hencewecanperformabinarysearchlike operationfor findingthecorrectvalueof 「 ・ エ キ ・ ・ ョ 0 andmax(si)'

Assumethat the Sl,… ,Sn arealreadysorted, i.e., for all i , Si セ s ゥK Qᄋ

Computethe running ウ オ ュ ] 1:7=1 Si'Now, supposes ォ s ォ Q N Consequently,

CHAPTER 1. SEARCHING120

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution1.19: Considertwo linesy ] 「ゥク andy ] ェ 「ェク suchthatN The i-th line intersectsthe line x = °at H P I andthe j-th lineintersectsthe line x = °at H P ェ I G s ゥ ュ ゥ ャ 。 イ エ ィ ・ ウ ・ lines intersectx = 1 at

the k smallestelementsof theunioncomefrom the first arrayand l - kelementscomefrom the secondarray. If this wereindeedtrue, thenwewould seethat Al [l - Q} aR{ォ -l] andA2[l - k] - Q aャ [l] (barringsomecornercaseswherewe reachtheendof thearray).

The other ゥ ョ エ ・ イ ・ ウ エ ゥ 「 ウ ・ イ カ 。 エ ゥ ッ ョ we canmakeis that if Al [l - 1] >A2[k -l] , thenwe shoulduseat leastonemoreelementfrom thesecondarrayin the k smallestelements.Similarly, if A2 [l - k - 1] > Al [l] , thenwe shoulduseat leastonemoreelementfrom thefirst array.Usingtheseエ キ ッ ャ ・ ア オ 。 ャ ゥ エ ゥ ・ ウ we canessentiallydo a binary searchon l. Note thatthis problemgivesyouplentyof cornercasesto worry about.In code:

1 lint FindOrderStat(const vector<int>& a1 ,2 I const vector<int>& a2 ,3 I unsigned int k) {4 I II Check the validity of input.5 I assert(a1.size()+ a2.size() >= k);6 I assert(k > 0);7 I I I Find an index begin <= 1 < end such that a1 [0].. a1 [1-1]8 I II and a2[O]..a2[k-l-1] are the smallest k numbers.9 I unsigned in t begin = max(O, k - a2. size0 ) ;

10 I unsigned intend = min( a1.size() I k);11 I while (begin < end) {12 I unsigned 1 = begin + (end - begin)12;13 I I I cM。ョ ・ include a1 [1] in the k smallest numbers?14 I if ((1 < al. size H I I H ォ M Q > PI H。Q{Q} < a2[k-1-1]))15 I begin = 1 + 1;16 I } e1;e if ((1> P I H ォ M Q < a2.size() I H 。 Q { Q M Q } > a2[

k-1])) {17 I II This is the case where we can discard a[l -l]18 I II from the set of k smallest numbers.19 I end = 1;20 I } else {21 I I I We found our 。 ョ ウ ・ イ since both the inequalities were

false.22 I begin = 1;23 I break;24 I25 I }26 I if (begin == 0) {27 I return a2 { ォ Q } [

28 I } else if (begin == k)29 I return a1[k-1];30 I } else {31 I return max(a1[begin -1]' a2 [k - begin -1]);3233

121

fH I = HM ォIᄋ zォᄋ

Using the above ・ ク ー イ ・ ウ ウ ゥ ッ we can searchfor the value of k suchthat f H s ォ I X G f H s ォ Q I by performingbinarysearchfor k (sincetheruntimeof this solutionis already X H ャ ッ ァ n) , we cando a linear searchaswell for simplicity). Oncewe have ヲ ッ オ ャ 、 the right valueof k, we cancomputethevalueof y by simply solvingthe ・ ア オ 。 エ ゥ ッ ヲ ッ イ f H I above.

H Q K bi ) and H Q ェ + bj ). Lines i and j intersectiff

HH ェIFH K「 ゥ \ ェ IIQHHゥ \ ェIFH 「 ゥ ^ ェK IIN

In otherwords, for the lines to intersect, ゥヲ \ ェ thenit mustbe thecasethat H \ ェ 「 ェ I or vice versa(ignoringthe trivial casewheretheyintersecton oneof theboundaries).

Henceif we sort the pairs H ゥ bi ) by ゥ and test that for successivepairs H 「 ゥ I 。 ョ 、 H ェ bj ) ゥヲ \ ェ + bj , we know that they do notintersect.If we do find a カゥッャ。エゥッ ッヲ this ゥ ョ ・ ア オ 。 ャ ゥ エ エ ィ ・ ョ wehavefoundoneof the intersectingpairs.

Sorting takes0 Hャッァ n) time and comparingsuccessivepairs takeso H I time. Hencethis canbedonein o H ャ ッ ァ エ ゥ ュ ・ N

Solution 1.20: One way to solve this is to sort the intervalsby theirlower boundaryand seeif their upperboundaryis also sortedin thesameorder. If not, we are sureto find somepair of indices l ,m where

ュ and 「 コ 「 ュ N This would be the pair we are looking for. Ifthe upperboundariesare also sorted, thenwe are guaranteedエ ィ 。 エ o

interval is completelycontainedin anotherinterval. Sincethis involvessortingfollowedby alinear ウ 」 。 ョ t ・ canget エ ャ 、 ッ ョ ・ in O(n ャ ッ ァ I time.

Solution1.21: The keyideahereis to sorttheendpointsof thelinesanddo a sweepfrom left to right. As we do the sweep, we maintaina listof lines that intersectthecurrentpositionaswell asthehighestline andits color. In order to quickly lookup the highestline amongthe set ofintersectinglines, we cankeepanorderedbinarytreedata-structureandto lookup thelinesby theendpointquickly, wecanmaintainahashtable.

Solution1.22: Define f H I to be Q [ l Q i i ャ ゥ H s ゥ I N We are ャ ッ ッ ォ ゥ ヲ ッ イ avalueof ウオ」ィ that f H I = S'. Clearly, F monotonicallyincreaseswithN Also, since°\ sG s the valueof ゥ ウ going to bebetween°andmaX(Si)'Hencewecanperformabinarysearchlike operationfor findingthecorrectvalueof 「 ・ エ キ ・ ・ ョ 0 andmax(si)'

Assumethat the Sl,… ,Sn arealreadysorted, i.e., for all i , Si セ s ゥK Qᄋ

Computethe running ウ オ ュ ] 1:7=1 Si'Now, supposes ォ s ォ Q N Consequently,

CHAPTER 1. SEARCHING120

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

The most expensiveッ ー ・ イ 。 エ ゥ ッ ヲ ッ イ this entire solution is sorting the..s, hencethe runtimeis oHャッァ n). Howeverif we aregiven エ ィ ・ ウ ゥ ョ

ad+mceandweaye 。ャャッキ・、ーイ・ーイッ」・ ゥョァ thenfor eachvalueof 8', thesearchwould just take0 HャッァI time.

Solution1.23: A solutionto this problemis discussedin the contextoffinding Hardy-Ramanujannumbers(Problem6.7).

Solution1.24: Giventwo line segmentsin a two-dimensionalplane, wecantestforhtersectioneasilyiMomtmttime-Givenn line segmerltsofapolygORywe canhdif my of thesegmtsゥ エ ・ イ ウ ・ 」 エ in 0 (n2

) timebysimply testingeachー。 ゥ h ッ キ ・ カ ・ イ doingthis in 0 H ッァ n) time requiresa fairly complexalgorithm.

COIISiderWo line segI1mtsmd the two farthestvertical liz1es thatch hter-sectwith both the lhe seFI1eI1ts(onevertical lhe is the left-

mostverticallinethatstill ゥ ョ Q エ ・ イ ウ ・ 」 エ ウ with both Q ゥ Q ・ ウ Q 、 theotheroneis・ ィ エ ュ ッ エ I N t ィ ・ two ャ ゥ ョ ゥ ョ ・ ュ ・ ウ キ o オ ャ 、 ゥ i Q エ ・ イ ウ ・ 」 エ ゥ ヲ ヲ エ ィ ・ ゥ イ t v ・ イ エ

o イ 、 ・ イ ゥ イ 」 ィ 。 ョ ァ ・ s 「 ・ エ キ ・ ・ ョ thetwo v?rticallhe?The kev ideais to usea sweephner avertkallinethatmoves from

leftto イ ゥ ァ ィ ゥ エ ィ イ P ィ eachendpoint. We orderthepolygonvertices(end-points of ャ ゥ ョ ・ ュ ・ ョ エ ウ I from left to right first by ゥ ・ 。 ウ ゥ ョ ァ the x co-:rdhateFttmbyinmaSingheUco-ordinate.Nowrmagimaver-tical

viIIg from left to right throughthese27z eI1dpohts.ForeachoositiOI1ofthisverticalliI1er wekeepm orderedlist of hter-

secthgline;egmmtsThelist is ウ ッ 、 by the 」 ッ M ッ イ 、 ゥ ョ 。 エ ・ of the firsteI1dpoiI1tof tkekmsePIleI1t.Aswereachthestarthgpohtsof theI1Wャ ゥ ョ ・ l ・ ァ ュ エ ウ ケ キ ・ ゥ ュ 、 エ ィ ・ ュ 「 ケ 、 ッ ゥ ョ ァ 。 「 ゥ ュ イ ケ ュ イ 」 ィ ヲ ッ イ エ ィ ・ ュ Aswereachthe eI1d of a liI1e segmeIItrwe removeit from the list.Thesortedlist canbemaintainedusingabalancedBST.

WKen my lim segIlentmdsrwe testif its verticalorder-hgchmgedmmoaredto the other lines h the list (which cmbe doneiacOI1stmtエ ゥ ゥ 、 ケ just eomp。 g thenearesttwo lines). The ャゥョ ゥョ Qo 、 ・イ Q ァ」ィ。ョァ ・、 for someャ ゥ ョ ャ ・ ウ ・ ョ ・ ョ エ N .

122 CHAPTER1. SEARCHING

Chapter2

Sorting

Solution2.1: In general, Quicksortis consideredone of the mosteffi-」 エ sorting 。 ャ ァ ッ イ ゥ エ ィ ュ ウ 」 ・ it hasa runtimeof e(n ャッァ I andit sortsin-place(sorteddatais not copiedto someotherbuffer). So, for a largesetof randomintegers, Quicksortwouldbeour choice.

Quicksort has to be implementedcarefully-for example, in anaIve ゥ ュ ー ャ ・ ュ ・ エ 。 エ ゥ ッ ョ an array with many duplicate elementsleadsto quadraticruntimes (and a high likelihood of stackspacebeing ex-haustedbecauseof thenumberof recursivecalls)-thiscanbemanagedby puttingall keysequalto the pivot in thecorrectplace. Similarly, it is

ャ ー ッ イ エ 。 ョ エ to call thesmallersubproblemfirst-this, in 」 ッ オ ョ 」 エ ゥ ッ キ ゥ エ ィ

tail イ ・ 」 ウ ゥ ッ ョ ensuresthatthestackdepthis o H ャ ッ ァ R I .Howevertherearecaseswhereothersolutionsaremorepreferable:

s ュ 。 ャ ャ set-foravery smallset(for example, 3-4 integers), a simpleゥ ュ ー ャ ・ ュ ・ エ 。 エ ゥ ッ ョ suchas insertionsort is easierto code, andrunsfaster.aャュッウエ sortedarray-if every elementis known to be at most kplacesfrom its finallocation, a min-heapcanbe usedto get anO(n log2 k) algorithm (Problem2.11); alternativesarebubblesortandinsertionsort.

- Numbers from a small range, small number of distinct keys-countingsort, which recordsfor eachelement, the numberof el-ementslessthanit. This countcanbekeptin anarray(if thelargestnumberis comparablein value to the sizeof the setbeingsorted)or a BST, wherethe keysarethe numbersandthevaluesaretheirfrequencies.

- Many duplicates-wecanaddthe keys to a BST, with linked listsfor elementswhichhavethe samekey; the sortedresultcanbede-rived from anin-orderwalk of theBST

- Stabilityis required-mostusefulsortingalgorithmsarenotstable.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

The most expensiveッ ー ・ イ 。 エ ゥ ッ ヲ ッ イ this entire solution is sorting the..s, hencethe runtimeis oHャッァ n). Howeverif we aregiven エ ィ ・ ウ ゥ ョ

ad+mceandweaye 。ャャッキ・、ーイ・ーイッ」・ ゥョァ thenfor eachvalueof 8', thesearchwould just take0 HャッァI time.

Solution1.23: A solutionto this problemis discussedin the contextoffinding Hardy-Ramanujannumbers(Problem6.7).

Solution1.24: Giventwo line segmentsin a two-dimensionalplane, wecantestforhtersectioneasilyiMomtmttime-Givenn line segmerltsofapolygORywe canhdif my of thesegmtsゥ エ ・ イ ウ ・ 」 エ in 0 (n2

) timebysimply testingeachー。 ゥ h ッ キ ・ カ ・ イ doingthis in 0 H ッァ n) time requiresa fairly complexalgorithm.

COIISiderWo line segI1mtsmd the two farthestvertical liz1es thatch hter-sectwith both the lhe seFI1eI1ts(onevertical lhe is the left-

mostverticallinethatstill ゥ ョ Q エ ・ イ ウ ・ 」 エ ウ with both Q ゥ Q ・ ウ Q 、 theotheroneis・ ィ エ ュ ッ エ I N t ィ ・ two ャ ゥ ョ ゥ ョ ・ ュ ・ ウ キ o オ ャ 、 ゥ i Q エ ・ イ ウ ・ 」 エ ゥ ヲ ヲ エ ィ ・ ゥ イ t v ・ イ エ

o イ 、 ・ イ ゥ イ 」 ィ 。 ョ ァ ・ s 「 ・ エ キ ・ ・ ョ thetwo v?rticallhe?The kev ideais to usea sweephner avertkallinethatmoves from

leftto イ ゥ ァ ィ ゥ エ ィ イ P ィ eachendpoint. We orderthepolygonvertices(end-points of ャ ゥ ョ ・ ュ ・ ョ エ ウ I from left to right first by ゥ ・ 。 ウ ゥ ョ ァ the x co-:rdhateFttmbyinmaSingheUco-ordinate.Nowrmagimaver-tical

viIIg from left to right throughthese27z eI1dpohts.ForeachoositiOI1ofthisverticalliI1er wekeepm orderedlist of hter-

secthgline;egmmtsThelist is ウ ッ 、 by the 」 ッ M ッ イ 、 ゥ ョ 。 エ ・ of the firsteI1dpoiI1tof tkekmsePIleI1t.Aswereachthestarthgpohtsof theI1Wャ ゥ ョ ・ l ・ ァ ュ エ ウ ケ キ ・ ゥ ュ 、 エ ィ ・ ュ 「 ケ 、 ッ ゥ ョ ァ 。 「 ゥ ュ イ ケ ュ イ 」 ィ ヲ ッ イ エ ィ ・ ュ Aswereachthe eI1d of a liI1e segmeIItrwe removeit from the list.Thesortedlist canbemaintainedusingabalancedBST.

WKen my lim segIlentmdsrwe testif its verticalorder-hgchmgedmmoaredto the other lines h the list (which cmbe doneiacOI1stmtエ ゥ ゥ 、 ケ just eomp。 g thenearesttwo lines). The ャゥョ ゥョ Qo 、 ・イ Q ァ」ィ。ョァ ・、 for someャ ゥ ョ ャ ・ ウ ・ ョ ・ ョ エ N .

122 CHAPTER1. SEARCHING

Chapter2

Sorting

Solution2.1: In general, Quicksortis consideredone of the mosteffi-」 エ sorting 。 ャ ァ ッ イ ゥ エ ィ ュ ウ 」 ・ it hasa runtimeof e(n ャッァ I andit sortsin-place(sorteddatais not copiedto someotherbuffer). So, for a largesetof randomintegers, Quicksortwouldbeour choice.

Quicksort has to be implementedcarefully-for example, in anaIve ゥ ュ ー ャ ・ ュ ・ エ 。 エ ゥ ッ ョ an array with many duplicate elementsleadsto quadraticruntimes (and a high likelihood of stackspacebeing ex-haustedbecauseof thenumberof recursivecalls)-thiscanbemanagedby puttingall keysequalto the pivot in thecorrectplace. Similarly, it is

ャ ー ッ イ エ 。 ョ エ to call thesmallersubproblemfirst-this, in 」 ッ オ ョ 」 エ ゥ ッ キ ゥ エ ィ

tail イ ・ 」 ウ ゥ ッ ョ ensuresthatthestackdepthis o H ャ ッ ァ R I .Howevertherearecaseswhereothersolutionsaremorepreferable:

s ュ 。 ャ ャ set-foravery smallset(for example, 3-4 integers), a simpleゥ ュ ー ャ ・ ュ ・ エ 。 エ ゥ ッ ョ suchas insertionsort is easierto code, andrunsfaster.aャュッウエ sortedarray-if every elementis known to be at most kplacesfrom its finallocation, a min-heapcanbe usedto get anO(n log2 k) algorithm (Problem2.11); alternativesarebubblesortandinsertionsort.

- Numbers from a small range, small number of distinct keys-countingsort, which recordsfor eachelement, the numberof el-ementslessthanit. This countcanbekeptin anarray(if thelargestnumberis comparablein value to the sizeof the setbeingsorted)or a BST, wherethe keysarethe numbersandthevaluesaretheirfrequencies.

- Many duplicates-wecanaddthe keys to a BST, with linked listsfor elementswhichhavethe samekey; the sortedresultcanbede-rived from anin-orderwalk of theBST

- Stabilityis required-mostusefulsortingalgorithmsarenotstable.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution2.2: v ャ・ ウッイエゥョァ datathat 」 ュ ッ エ エ into theRAM of a singlemachine, wehaveto partitionthedatainto smallerblocksthat キ ッ オ ャ 、ゥ ィ ・ memory, sorteachblock ゥ ョ 、 ゥ カ ゥ 、 ャ ャ ケ andthencombinetheblocks.If a clusterof machinesis available, theblockscanbe sortedin parallelor theycanbereadin sequence 。 ウ Q Y ャ ・ machineandthenstoredonthedisk.

Thereare two popularapproachesfor doing this. If we know therough distribution of the data h advmce(e.g-yit is distributedmimformly), it can be ー 。 イ エ ゥ エ ゥ ・ 、 into 」 ッ ョ エ ゥ ッ オ ウ subrangesof approxi-ュ ・ ャ ケ 。 ャ コ ・ in the first pass.This hasthe 。 、 カ 。 ョ エ 。 ァ ・ 。 エ ッ ョ 」 ・

individualblocksaresortedrwecmcombineエ ィ ・ ュ ェ オ ウ エ 「 ケ 」 ッ ュ 。 エ ・ ュ エ ゥ ッAnotherslightly moreexpensiveapproachthatdoesnot イ ・ ア オ ゥ セ セ N セ ケ

knowledgeof distributionis to readthe input datain ウ ・ ア オ ・ 」 ・ till thememoryis full , sort it, write it, andthenreadthe nextblock till we aredonewith the file. This requiresus to mergethe sortedblocks in theeI1dlike Mergesort.Herershcewe couldbepoteI1tidlymerginga largenumberof sortedfiles, usinga min-heapis helpful. Essentially, we keepthesmallestunreadentryfrom eachfile in theheap, thenwe extractthemin elementfrom theheap, replaceit with thenextentryfrom thesamefile , andwrite out themin valueto theoutputfile.

TheUnix sortprogramis very robust;it makesuseof thediskwhenneededandcancombinea setof files into a singlesortedfile.

Solution2.3: First, we considerthe problemof finding thebestplayer.Each game ・ ャ ゥ ョ 。 エ ・ ウ one player and there are 128 players; so, 127matchesarenecessaryandalsosufficient.

To find thesecondbest,wenotethatthe onlycandidatesaretheplay-erswho arebeatenby theplayerwho is eventuallydeterminedto bethebest-everyoneelselost to someonewho ゥ ウ ッ エ thebest.

To find thebestーャ 。 ケ ・ エ ィ ・ orderin whichwe organizethematchesisinconsequential-weェ ャ ー ゥ 」 ォ pairsfrom thesetof candidatesandwho-ever Q P ウ is removedfro;n thepool of candidates.Howeverif we pro-ceedin an arbitraryorder, we might startwith thebest ー ャ 。 ケ ・ キ ィ ッ de-feats127 other ー i ャ ・ イ ウ andthenthe playerswho lost needto play 126matchesamongstthemselvesto find thesecondbest.

We cando muchbetterby organizingthe matchesasabinarytree-wepairoff playersarbitrarilywhoplay64matches.Afterthesematchesyweareleft with 64caI1didates;wepairthemoff again。 イ 「 ゥ エ イ 。 イ ゥ ャ ケ ャ 、 theyplay32matches.Proceedingin this ヲ 。 ウ ィ ゥ ッ weorganizethe127matchesneededto find thebestpIa

124 CHAPTER2. SORTING

Mergesort, carefullyimplemented, canbemadestable;anotherso-lutidi1is to addthe iMex as m iRteger イ 。 エ ッ the keys to breakties.

125

7matches.Thereforewe canfind thesecondbestplayerby organizing6matchesbetweenthe 7 playerswho lost to thebestplayer, for a total of134matches.

Solution2.4: Split the numbersinto pairs of two and then group thehighervaluesof thepairsinto onesetandthe lower valuesinto anotherset. Find themin of thelower groupandthemaxof thehighergroup.

Solution 2.5: Let's start with five time-trials with no cy-clist being in more than one of these five initial time-trials.Let the rankings be A1,A2,A3,A4,A5, B1,B2,B3,B4,B5,01,02,03,04,05, D1 ,D2,D3,D4,D5, and E1,E2,E3,E4,E5,where the first cyclist is the fastest. Note that we can eliminateA4,A5,B4,B5,04,05,D4,D5,E4,E5 at this stage.

Now, we racethe winnersfrom eachof the initial time-trials. With-out lossof generality, assumethe outcomeis A1,B1,01,D1 ,E1. At thispoint, we caneliminateD1 andE1 aswell as D2,D3 andE2,E3. Fur-エ ィ ・ イ ュ ッ A ウ ゥ ョ 」 ・ 01 wasthird, PR ャ、 03cannotbein thetop three;s ゥ ュilarly, B3 cannotbea contender.

Weneedto find the 「 ・ ウ エ ャ 、 thesecondbestfrom A2,A3,B1,B2,01,which we candeterminewith one more time-trial, for a total of sevenエ ゥ ュ ・ エ イ ゥ 。 ャ ウ N

Notethatweneedtime-trialsto determinetheoverallwinner, andthesequenceof time-trialsto determinethewinneris essentiallyunique-ifsomecyclistdid notparticipatein thefirst five time-trials, hewouldhaveto participatein thesixthone.But then P ・ of thewinnersof thefirst fivetime-trialswould not participatein the sixth time-trial andhemight bethe overallwinner. The first six time-trialsdo not determinethe secondandthethird fastestcyclists, hencea seventhraceis needed.

Solution2.6: Whenever theswapoperationfor theobjectsbeingsortedis expensive, oneof the bestthings to do is indirect sort, i.e., sort refer-encesto theobjectsfirst andthenapplythepermutationthatwasappliedto thereferencesin theend.

In the caseof statues, we canassignincreasingindicesto the statuesfrom left to right andthensort the pairsof statueheightandindex. Theindicesin thesortedpairswouldgiveusthe ー ・ イ ュ オ エ 。 エ ゥ エ ッ apply. Whileapplyingpermutation, we would want to performit in a way that wemoveeachstatuethe minimum possibledistance.We canachievethisif eachstatueis movedexactlyto its correctdestinationexactlyonce(nointermediateswaps).

Solution2.7: Thesimplestwayof doingthiswouldbeto definealexico-graphicorderingovertherows (wherewe ignorethecontents

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution2.2: v ャ・ ウッイエゥョァ datathat 」 ュ ッ エ エ into theRAM of a singlemachine, wehaveto partitionthedatainto smallerblocksthat キ ッ オ ャ 、ゥ ィ ・ memory, sorteachblock ゥ ョ 、 ゥ カ ゥ 、 ャ ャ ケ andthencombinetheblocks.If a clusterof machinesis available, theblockscanbe sortedin parallelor theycanbereadin sequence 。 ウ Q Y ャ ・ machineandthenstoredonthedisk.

Thereare two popularapproachesfor doing this. If we know therough distribution of the data h advmce(e.g-yit is distributedmimformly), it can be ー 。 イ エ ゥ エ ゥ ・ 、 into 」 ッ ョ エ ゥ ッ オ ウ subrangesof approxi-ュ ・ ャ ケ 。 ャ コ ・ in the first pass.This hasthe 。 、 カ 。 ョ エ 。 ァ ・ 。 エ ッ ョ 」 ・

individualblocksaresortedrwecmcombineエ ィ ・ ュ ェ オ ウ エ 「 ケ 」 ッ ュ 。 エ ・ ュ エ ゥ ッAnotherslightly moreexpensiveapproachthatdoesnot イ ・ ア オ ゥ セ セ N セ ケ

knowledgeof distributionis to readthe input datain ウ ・ ア オ ・ 」 ・ till thememoryis full , sort it, write it, andthenreadthe nextblock till we aredonewith the file. This requiresus to mergethe sortedblocks in theeI1dlike Mergesort.Herershcewe couldbepoteI1tidlymerginga largenumberof sortedfiles, usinga min-heapis helpful. Essentially, we keepthesmallestunreadentryfrom eachfile in theheap, thenwe extractthemin elementfrom theheap, replaceit with thenextentryfrom thesamefile , andwrite out themin valueto theoutputfile.

TheUnix sortprogramis very robust;it makesuseof thediskwhenneededandcancombinea setof files into a singlesortedfile.

Solution2.3: First, we considerthe problemof finding thebestplayer.Each game ・ ャ ゥ ョ 。 エ ・ ウ one player and there are 128 players; so, 127matchesarenecessaryandalsosufficient.

To find thesecondbest,wenotethatthe onlycandidatesaretheplay-erswho arebeatenby theplayerwho is eventuallydeterminedto bethebest-everyoneelselost to someonewho ゥ ウ ッ エ thebest.

To find thebestーャ 。 ケ ・ エ ィ ・ orderin whichwe organizethematchesisinconsequential-weェ ャ ー ゥ 」 ォ pairsfrom thesetof candidatesandwho-ever Q P ウ is removedfro;n thepool of candidates.Howeverif we pro-ceedin an arbitraryorder, we might startwith thebest ー ャ 。 ケ ・ キ ィ ッ de-feats127 other ー i ャ ・ イ ウ andthenthe playerswho lost needto play 126matchesamongstthemselvesto find thesecondbest.

We cando muchbetterby organizingthe matchesasabinarytree-wepairoff playersarbitrarilywhoplay64matches.Afterthesematchesyweareleft with 64caI1didates;wepairthemoff again。 イ 「 ゥ エ イ 。 イ ゥ ャ ケ ャ 、 theyplay32matches.Proceedingin this ヲ 。 ウ ィ ゥ ッ weorganizethe127matchesneededto find thebestpIa

124 CHAPTER2. SORTING

Mergesort, carefullyimplemented, canbemadestable;anotherso-lutidi1is to addthe iMex as m iRteger イ 。 エ ッ the keys to breakties.

125

7matches.Thereforewe canfind thesecondbestplayerby organizing6matchesbetweenthe 7 playerswho lost to thebestplayer, for a total of134matches.

Solution2.4: Split the numbersinto pairs of two and then group thehighervaluesof thepairsinto onesetandthe lower valuesinto anotherset. Find themin of thelower groupandthemaxof thehighergroup.

Solution 2.5: Let's start with five time-trials with no cy-clist being in more than one of these five initial time-trials.Let the rankings be A1,A2,A3,A4,A5, B1,B2,B3,B4,B5,01,02,03,04,05, D1 ,D2,D3,D4,D5, and E1,E2,E3,E4,E5,where the first cyclist is the fastest. Note that we can eliminateA4,A5,B4,B5,04,05,D4,D5,E4,E5 at this stage.

Now, we racethe winnersfrom eachof the initial time-trials. With-out lossof generality, assumethe outcomeis A1,B1,01,D1 ,E1. At thispoint, we caneliminateD1 andE1 aswell as D2,D3 andE2,E3. Fur-エ ィ ・ イ ュ ッ A ウ ゥ ョ 」 ・ 01 wasthird, PR ャ、 03cannotbein thetop three;s ゥ ュilarly, B3 cannotbea contender.

Weneedto find the 「 ・ ウ エ ャ 、 thesecondbestfrom A2,A3,B1,B2,01,which we candeterminewith one more time-trial, for a total of sevenエ ゥ ュ ・ エ イ ゥ 。 ャ ウ N

Notethatweneedtime-trialsto determinetheoverallwinner, andthesequenceof time-trialsto determinethewinneris essentiallyunique-ifsomecyclistdid notparticipatein thefirst five time-trials, hewouldhaveto participatein thesixthone.But then P ・ of thewinnersof thefirst fivetime-trialswould not participatein the sixth time-trial andhemight bethe overallwinner. The first six time-trialsdo not determinethe secondandthethird fastestcyclists, hencea seventhraceis needed.

Solution2.6: Whenever theswapoperationfor theobjectsbeingsortedis expensive, oneof the bestthings to do is indirect sort, i.e., sort refer-encesto theobjectsfirst andthenapplythepermutationthatwasappliedto thereferencesin theend.

In the caseof statues, we canassignincreasingindicesto the statuesfrom left to right andthensort the pairsof statueheightandindex. Theindicesin thesortedpairswouldgiveusthe ー ・ イ ュ オ エ 。 エ ゥ エ ッ apply. Whileapplyingpermutation, we would want to performit in a way that wemoveeachstatuethe minimum possibledistance.We canachievethisif eachstatueis movedexactlyto its correctdestinationexactlyonce(nointermediateswaps).

Solution2.7: Thesimplestwayof doingthiswouldbeto definealexico-graphicorderingovertherows (wherewe ignorethecontents

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

126 CHAPTER2. SORTING

numberof duplicate rowsfor eachuniquerow easily in a linear pass.In caseit is expensiveto swapthe rows (sinceeachrow containslargeamountsof data), it might be more efficient to hashthe contentsof theイ ッ キ ャ 、 sortthehashvaluesinstead.

Solution2.8: Almost all sortingalgorithmsrely on swappingrecords.Howeverthis becomescomplicatedwhen the recordsize varies. Onewayof dealingwith this problemis to allocatefor themaximumpossiblesizefor eachrecord-thiscanbeverywastefulif thereis a largevariationin thesizes.

Here also indirect sort canbe ィ ・ ャ ー ヲ オ ャ ォ ・ ・ ー the recordsin a com-pact form in the memory and build anotherarray of ー ッ ャ エ ・ イ ウ to therecords. Thenwe just sort the pointers オ ウ ゥ エ ィ ・ comparefunction P

the de-referencedpointersandfinally write the databy de-referencingthesortedーッ ャ エ ・ イ ウ N

Solution2.9: An efficient way of eliminating duplicatesfrom any setof records, wherea "less-than"operationcanbe defined, is to sort therecordsandtheneliminatetheduplicatesin asinglepassoverthedata.

Sortingcanbedonein 0 (n ャッァ エゥュ・[ thesubsequenteliminationofduplicatestakesO(n) time. If the eliminationof duplicatesis donein-place, it wouldbemoreefficient thanwriting theuniquesetin aseparatearraysincewewouldachievebettercacheperformance.Hereis thecodethatdoesin-placeduplicateremoval:

,EaEthLTLFOneᄋᄋᄋFeTLezᄋᄋᄋᄋQU,vj

t+KT·1aft

-F

Jィオィ

エエjイlTョッ--nu-サ

・ゥj

qu

baイエエ

Mf

・オカGQJU<aincrrttc·1rJ'uuaa

MイイMコオ

DZ1·t・MM

LL·'-1A-tA。アゥ

ャQ

nty·]MオコM。i

・イvjMf

·I-]zra--r·]Mゥᄋ

umBエb

eエウH

イKQ

Mj

。ᄋ}

エッ

イta

-zr-Ite--oezsrflJrᄋ

aQUゥ

SafSヲo

jッッョケョオ

1i

Anotherefficientwayis to usehashtableswherewestoreeachrecordエ ッ ahashtableasthekeywith no カ 。 ャ オ ・ ャ 、 thenwrite out all thekeys

in thehashtable. Sincehashtableinsertscanbe done ゥ i ャ o H Q I time 。 ョ Q

ゥ エ ・ イ 。 エ ゥ ョ ァ over all エ ィ ・ keys also takesonly (n) time, this solutionscalesmuchbetterthanthe sortingapproach.However, in practice, for smallsizeof inputs, thesortingapproachmightwork fastersinceit canbedonein-place.

Solution2.10: While mergingk sorted 。 イ イ ウ we needto repeatedlypick the smallestelementamongstthe smallestremainingrecordsfromeacharray. A ュ ャMィ・。ー is ideal for maintaininga setof recordswhen

127

we repeatedlyinsertaIId queryfor thesmallestrecord(bothextract-mhand imert would take H ャ ッ ァ ォ I エ ゥ ュ ・ I N h ュ 」 ・ we caI1dothe mergehoHャッァ k) time, wheren is thetotalnumberof recordsゥ Q ー オ エ lis thecodefor this:

セ i 「 g ッ イ ー 。 ゥ イ \ ゥ ゥ ョ m 。 Hconst pair<int I in t>& b)

3 I if (a. first> b. first) {4 I return true;

セ I }: セ セ ⦅ セ _. if (a. fi r s t == b. fi r s エ 。 . second > b. second)b I return true;7 I } else {8 I return false;9

10111213141516

OquqjョオMM

ョ、a

afS

uO

uqj

uゥ

アオa

fSヲ「

ゥQゥゥ

GG

TY

TY

アオ

jアオアオアオ

Sアオ

void Q | ・ イ ァ ・ s ッ イ エ ・ 、 v ・ 」 エ ッ イ ウ (const vector<vector<int > >& sorted_inputIvector<int >* output) {

//ThE firs t numbEr is thE S7710llESt number rE7710iningund//tht second numbEr repftsents array from zuhich i エウ

taken.vector<pair \ ゥ I in t > > min_heap;//WE KEEponindEX oythtnumbErs rtad from eochorroy-vector< int>curr?nt-read-index(sorted-input-size());ヲ o Z H セ ョ エ i = 0; i セ セッイエ・、⦅ゥョーセエNウゥコ・HIO i++) {

if H ウ ッ イ エ ・ 、 ゥ ョ ー オ エ { i ] . size() ). 0) {

min-heap-push-back(make-pair(sorted-input[i][OLi));}rent-read-iRdex[i]z1;

3738

make_heap(min_heap.begin() I min_heap.end() I Greater);

while (min_heap.size() > 0) {ー。ゥイセゥョエ セ int> min = min_heap[0];pop-heap(min-heap.begin(), min-heap.eI1d()r Greater);min_heap.pop_back() ;output->push_back(min.first);if (current_read_index{ ュ ゥ ョ N 」 ッ ョ 、 } <

sorted_input[min. second].size()) {II There are more inputs to be read. Read the next

numtJ erII and insert it in the heap.min ヲ ャ イ ウ エ ] ウ ッ イ エ ・ 、 M ゥ オ エ { ュ ゥ ョ 」 ッ ョ 、 } [ 」オイイ・ イ・。、⦅ゥョ、・ク

[min. second]];current_read_index[min. second]++;min_heap.push_back(min);

}push-heap(miILheap-begin(), min-heap.end(), Greater);

ョケョu

OMョ、a

344444A

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

126 CHAPTER2. SORTING

numberof duplicate rowsfor eachuniquerow easily in a linear pass.In caseit is expensiveto swapthe rows (sinceeachrow containslargeamountsof data), it might be more efficient to hashthe contentsof theイ ッ キ ャ 、 sortthehashvaluesinstead.

Solution2.8: Almost all sortingalgorithmsrely on swappingrecords.Howeverthis becomescomplicatedwhen the recordsize varies. Onewayof dealingwith this problemis to allocatefor themaximumpossiblesizefor eachrecord-thiscanbeverywastefulif thereis a largevariationin thesizes.

Here also indirect sort canbe ィ ・ ャ ー ヲ オ ャ ォ ・ ・ ー the recordsin a com-pact form in the memory and build anotherarray of ー ッ ャ エ ・ イ ウ to therecords. Thenwe just sort the pointers オ ウ ゥ エ ィ ・ comparefunction P

the de-referencedpointersandfinally write the databy de-referencingthesortedーッ ャ エ ・ イ ウ N

Solution2.9: An efficient way of eliminating duplicatesfrom any setof records, wherea "less-than"operationcanbe defined, is to sort therecordsandtheneliminatetheduplicatesin asinglepassoverthedata.

Sortingcanbedonein 0 (n ャッァ エゥュ・[ thesubsequenteliminationofduplicatestakesO(n) time. If the eliminationof duplicatesis donein-place, it wouldbemoreefficient thanwriting theuniquesetin aseparatearraysincewewouldachievebettercacheperformance.Hereis thecodethatdoesin-placeduplicateremoval:

,EaEthLTLFOneᄋᄋᄋFeTLezᄋᄋᄋᄋQU,vj

t+KT·1aft

-F

Jィオィ

エエjイlTョッ--nu-サ

・ゥj

qu

baイエエ

Mf

・オカGQJU<aincrrttc·1rJ'uuaa

MイイMコオ

DZ1·t・MM

LL·'-1A-tA。アゥ

ャQ

nty·]MオコM。i

・イvjMf

·I-]zra--r·]Mゥᄋ

umBエb

eエウH

イKQ

Mj

。ᄋ}

エッ

イta

-zr-Ite--oezsrflJrᄋ

aQUゥ

SafSヲo

jッッョケョオ

1i

Anotherefficientwayis to usehashtableswherewestoreeachrecordエ ッ ahashtableasthekeywith no カ 。 ャ オ ・ ャ 、 thenwrite out all thekeys

in thehashtable. Sincehashtableinsertscanbe done ゥ i ャ o H Q I time 。 ョ Q

ゥ エ ・ イ 。 エ ゥ ョ ァ over all エ ィ ・ keys also takesonly (n) time, this solutionscalesmuchbetterthanthe sortingapproach.However, in practice, for smallsizeof inputs, thesortingapproachmightwork fastersinceit canbedonein-place.

Solution2.10: While mergingk sorted 。 イ イ ウ we needto repeatedlypick the smallestelementamongstthe smallestremainingrecordsfromeacharray. A ュ ャMィ・。ー is ideal for maintaininga setof recordswhen

127

we repeatedlyinsertaIId queryfor thesmallestrecord(bothextract-mhand imert would take H ャ ッ ァ ォ I エ ゥ ュ ・ I N h ュ 」 ・ we caI1dothe mergehoHャッァ k) time, wheren is thetotalnumberof recordsゥ Q ー オ エ lis thecodefor this:

セ i 「 g ッ イ ー 。 ゥ イ \ ゥ ゥ ョ m 。 Hconst pair<int I in t>& b)

3 I if (a. first> b. first) {4 I return true;

セ I }: セ セ ⦅ セ _. if (a. fi r s t == b. fi r s エ 。 . second > b. second)b I return true;7 I } else {8 I return false;9

10111213141516

OquqjョオMM

ョ、a

afS

uO

uqj

uゥ

アオa

fSヲ「

ゥQゥゥ

GG

TY

TY

アオ

jアオアオアオ

Sアオ

void Q | ・ イ ァ ・ s ッ イ エ ・ 、 v ・ 」 エ ッ イ ウ (const vector<vector<int > >& sorted_inputIvector<int >* output) {

//ThE firs t numbEr is thE S7710llESt number rE7710iningund//tht second numbEr repftsents array from zuhich i エウ

taken.vector<pair \ ゥ I in t > > min_heap;//WE KEEponindEX oythtnumbErs rtad from eochorroy-vector< int>curr?nt-read-index(sorted-input-size());ヲ o Z H セ ョ エ i = 0; i セ セッイエ・、⦅ゥョーセエNウゥコ・HIO i++) {

if H ウ ッ イ エ ・ 、 ゥ ョ ー オ エ { i ] . size() ). 0) {

min-heap-push-back(make-pair(sorted-input[i][OLi));}rent-read-iRdex[i]z1;

3738

make_heap(min_heap.begin() I min_heap.end() I Greater);

while (min_heap.size() > 0) {ー。ゥイセゥョエ セ int> min = min_heap[0];pop-heap(min-heap.begin(), min-heap.eI1d()r Greater);min_heap.pop_back() ;output->push_back(min.first);if (current_read_index{ ュ ゥ ョ N 」 ッ ョ 、 } <

sorted_input[min. second].size()) {II There are more inputs to be read. Read the next

numtJ erII and insert it in the heap.min ヲ ャ イ ウ エ ] ウ ッ イ エ ・ 、 M ゥ オ エ { ュ ゥ ョ 」 ッ ョ 、 } [ 」オイイ・ イ・。、⦅ゥョ、・ク

[min. second]];current_read_index[min. second]++;min_heap.push_back(min);

}push-heap(miILheap-begin(), min-heap.end(), Greater);

ョケョu

OMョ、a

344444A

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution 2.11: The easiestway of looking at this problemis that weneedto storethe numbersin memorytill aU the numberssmallerthanthis numberhavearrived. Oncethosenumbershavearrivedandhavebeenwritten to the output file , we can go 。 ィ ・ 。 、 ャ 、 write this num-ber. Sincewe do not know how the numbersare shuffled, it is hardtotell whenall thenumberssmallerthana givennumberhavearrivedandhavebeenwrittento theoutput. Howeversincewearetold thatnonum-ber is off by morethanonethousandpositionsfrom its correctlysortedposition, if morethana thousandnumbersgreaterthana givennumberhave arrived and all the numberssmallerthan the given numberthatarrivedhavebeenwritten, we canbe surethat thereareno moreothersmallernumbersthat are going to arrive. Henceit is safeto write theァ ゥ カ ・ オ ュ 「 ・ イ ウ N

This essentiallygivesus the strategyto alwayskeep1001numbersin a min-heap. As soonaswe reada new number, we insert the newnumberandthenextractthemin from theheapandwrite theoutput.

Solution 2.12: While it takesO(k) time to computethe averageof awindow of sizek, thesuccessiveaveragesfor theslidingwindow canbecomputedinexpensivelyby maintainingthe sumover the sliding win-dow. When the キ カ ゥ ョ ャ 、 ッ キ カ ゥ ウ slid by ッ イ ャ ・ ー ッ ウ ゥ エ ゥ ッ the new sum 」 。 ョ Q bec ッ ュ ー オ エ ・ 、 ャ ゥ ォ ・ エ ィ ゥ s Z ウ オ ュ ゥ Q = s オl W W

イ ュゥ i QY averagecanbecomputedin O(n) time.Computingthe runningmedianis abit more ゥ ョ イ ッ ャ カ ・ 、 but the same

ideais applicablethereaswell. Whenwe slidethewindowby oneposi-tion, we deletethefirst elementfrom the list andinsertthenextelement.ィ・イ・ヲッイ・ we needto maintaina setin a way that allows us to find themedianeasilyin the ー イ ・ ウ ・ 」 ・ of insertsanddeletes.Thiscanbeachievedwith a balancedBST (an AVL treeor a red-blacktreecould do the job).Both insert anddeleteare O(logk) operations.Finding the medianaf-ter anupdateamountsto looking for thesuccessoror predecessorof theexistingmediandependingP キ ィ ・ エ ィ ・ イ theupdateinvolvedanelementthat was larger or smaller than the current ュ ・ 、 ゥ 。 ョ N Thereforewe canc ッ ュ ー オ エ ・ the イ オ ョ ゥ ャ ャ Q

Alternately, we could ェ オ ウ エ usean P イ 、 、 ・ イ M s エ 。 エ ゥ ウ エ ゥ 」 エ イ ・ ・ which is simply。 「 。 ャ 。 ョ 」 ・ 、 BST キゥ エャ Q some 。 、 、 ゥ エ ゥ ッ i ャ 。 ャ ゥ ョ Q ヲ ッ イ ュ 。 エ ゥ ッ ョ storedat eachnode.Specifically, in an order-statistictree, eachnoderecordsthe numberofnodesin thesubtreestoredat thatnode

128 CHAPTER2. SORTING129

thataregoing エ ッ 。 ー ー ・ ョ asa direct resultof ・ カ ・ エ ィ 。 エ havehappened

l z i z R Q R R R R Z R e エ エ ゥ u ヲ エ コaomganynewwork.

エ ィィj。jjZ イ[ [ エ[[ エZRS[Z _イイRRャuRイZZ ゥZ Z コ[イ Wzコ[ZZ_ コ jUZZ ZRイj z :R [R Zlエ[QQZ [ Q_ ZZ [ReZ zz [RエZ_Q Z_queue.Forthmappi1catlORagamramiRwheapworksmostefacieIItly.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution 2.11: The easiestway of looking at this problemis that weneedto storethe numbersin memorytill aU the numberssmallerthanthis numberhavearrived. Oncethosenumbershavearrivedandhavebeenwritten to the output file , we can go 。 ィ ・ 。 、 ャ 、 write this num-ber. Sincewe do not know how the numbersare shuffled, it is hardtotell whenall thenumberssmallerthana givennumberhavearrivedandhavebeenwrittento theoutput. Howeversincewearetold thatnonum-ber is off by morethanonethousandpositionsfrom its correctlysortedposition, if morethana thousandnumbersgreaterthana givennumberhave arrived and all the numberssmallerthan the given numberthatarrivedhavebeenwritten, we canbe surethat thereareno moreothersmallernumbersthat are going to arrive. Henceit is safeto write theァ ゥ カ ・ オ ュ 「 ・ イ ウ N

This essentiallygivesus the strategyto alwayskeep1001numbersin a min-heap. As soonaswe reada new number, we insert the newnumberandthenextractthemin from theheapandwrite theoutput.

Solution 2.12: While it takesO(k) time to computethe averageof awindow of sizek, thesuccessiveaveragesfor theslidingwindow canbecomputedinexpensivelyby maintainingthe sumover the sliding win-dow. When the キ カ ゥ ョ ャ 、 ッ キ カ ゥ ウ slid by ッ イ ャ ・ ー ッ ウ ゥ エ ゥ ッ the new sum 」 。 ョ Q bec ッ ュ ー オ エ ・ 、 ャ ゥ ォ ・ エ ィ ゥ s Z ウ オ ュ ゥ Q = s オl W W

イ ュゥ i QY averagecanbecomputedin O(n) time.Computingthe runningmedianis abit more ゥ ョ イ ッ ャ カ ・ 、 but the same

ideais applicablethereaswell. Whenwe slidethewindowby oneposi-tion, we deletethefirst elementfrom the list andinsertthenextelement.ィ・イ・ヲッイ・ we needto maintaina setin a way that allows us to find themedianeasilyin the ー イ ・ ウ ・ 」 ・ of insertsanddeletes.Thiscanbeachievedwith a balancedBST (an AVL treeor a red-blacktreecould do the job).Both insert anddeleteare O(logk) operations.Finding the medianaf-ter anupdateamountsto looking for thesuccessoror predecessorof theexistingmediandependingP キ ィ ・ エ ィ ・ イ theupdateinvolvedanelementthat was larger or smaller than the current ュ ・ 、 ゥ 。 ョ N Thereforewe canc ッ ュ ー オ エ ・ the イ オ ョ ゥ ャ ャ Q

Alternately, we could ェ オ ウ エ usean P イ 、 、 ・ イ M s エ 。 エ ゥ ウ エ ゥ 」 エ イ ・ ・ which is simply。 「 。 ャ 。 ョ 」 ・ 、 BST キゥ エャ Q some 。 、 、 ゥ エ ゥ ッ i ャ 。 ャ ゥ ョ Q ヲ ッ イ ュ 。 エ ゥ ッ ョ storedat eachnode.Specifically, in an order-statistictree, eachnoderecordsthe numberofnodesin thesubtreestoredat thatnode

128 CHAPTER2. SORTING129

thataregoing エ ッ 。 ー ー ・ ョ asa direct resultof ・ カ ・ エ ィ 。 エ havehappened

l z i z R Q R R R R Z R e エ エ ゥ u ヲ エ コaomganynewwork.

エ ィィj。jjZ イ[ [ エ[[ エZRS[Z _イイRRャuRイZZ ゥZ Z コ[イ Wzコ[ZZ_ コ jUZZ ZRイj z :R [R Zlエ[QQZ [ Q_ ZZ [ReZ zz [RエZ_Q Z_queue.Forthmappi1catlORagamramiRwheapworksmostefacieIItly.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution S l ・ エ p { 「 ・ true iff thereis a stonein theriver at x meters.Let'sdefineF[x] [y] tobeaBooleanvariablethatis trueiff it is possible

for the frog to reachx metersfrom the shorewith the last jump beingymeters. We can say that F[O] [y] is true ゥ ケ = O. Also, F[x] [y] canbetrue iff P[x] is true (there is a stonethere) and that either F[x - y][y] ,F[x - ケ}{ K 1], or F[x - y][y • 1] is true. u ウ ゥ d p we cancomputethevaluesof F[n] [y] for all possiblevaluesof y. One ゥ ョ エ ・ イ ・ ウ エ ゥ エ ィ ゥ エ ッ ョ ッ エ ・

hereis thatwhile jumpingthe ヲ ゥ イ ウ エ ュ ・ エ ・ イ ウ thelargestjump sizecouldbeatmostP N Hencewejustneedto worry aboutvaluesof ケ Q P ャ ᄋ

This givesusa runtimeof 0 H QN UIN Hereis a possibleimplementation:

131

previous_index[i] = prev_index;if (max_length < length) {

max_length = length;longest_sequence_end= i;

assert(output != NULL);output->c1ear() ;I I Buil d the reverse of the 1angest sequence by goi ng

backwards from the end.while (longest_sequence_end>= 0) {

output->push_back(input [longest_sequence_end]);longest_sequence_end= previous_index[

longest_sequence_end];

i i ク attainable jump size.int m= sqrt(2 * p.size());vector<vector<boo!> > f (p. size() + 1);II The first block can only be イ・。」ィ・、 ゥエィ jump ofII size 1 and no block can be reached with jump ofII size O.for (int j = 0; j <=m; j++) {

f[O].push_back(false);

std : : reverse(output->begin(), output->end() ) ;

bool isReachable(const vector<boo1>& p) {if (p. si z e () == 0) {

return true;

for (int i = 1; i <p.size(); i++) {for (int j = 1; j <=m; j++) {

f[i].push_back(false);if H ー ゥ - j >= 0) {

for (int i = 1; i <p.size(); i++) {f [i]. push_back(false);

エ イ オ ・ [

jbees

-3244Binur--&[]po([r?A

F?i

epbjᄋ

Ma

123445678901234567890123

ゥQT

ゥQa

ョl

ヲo

ア、

アuaTᄋMf

アオアオ

aaュSヲo

uqjョオMM

GO

Omアオアオ

uウゥョエィゥウ strategyrfill up a tablefor 84·If we wmt the sequmceas・ Q Q ヲ ッ イ ・ 。 、 エ ィ 。 、 、 ゥ ッ ョ to storingthe i ・ ィ of エ ィ ・ オ ・ ョ キ ・ 」 。 ョ

ウ エ ッ M エ ィ ・ ゥ ィ ョ 、 ッ ヲ ャ 。 ウ エ ュA l c::< ::\n ゥュョャ・ュ ャ・ョ ャエ 。エ ゥッ i Q of theidea:

ウ ・ 。 オ ・ ョ 」 ・ N エ ・ イ ・ IS ゥZu ャ ilー

カッゥ、 iッョ ・ウエnッョ、・」イ・。ウゥョァs・アオ・ョ」・ (ョウエ カ・イ \ゥF ゥョーャ カ・イ \ゥ ョエ^J output) {

assert(output 1= NULL);ッ オ エ ー オ エ ^ 」 Q ・ 。 イ () ;if セ H ゥ ョ ー オ エ N size() == 0) {

return;

Solution3.1: Let Si be the i ・ ァ エ ィ of the longestI1ORdeereashgsubse-オ ・ ョ 」 ・ of A thatendsat Am(specifically}A[i]is heludedh this subse-ア オ ・ i N Thenwe canキ エ ィ ・ recurren

ャ ・ ュ ゥ ョ エ Q P ア オ ・ ョ 」 ・ l ・ H エ H I I 1);V r <int> previous M H iャッョァ・ウエs・アオ・セセ・l・セァエィ [0] = 1;int セ max_iEmgth = 1;in t longesL-sequence_end= 0;for (int i=1;i<input-size();i++){

int length = 1;lnt nrev index = -1ヲ セ セ (lnt-j = 0; j < i; A Z I {

if G H ゥ ョ ー セ エ { ェ } <= ゥョー_エ{ゥ}longestsequenceLengthi }]+1 > length){

leRgth=longestsequeI1ceLeRgth[j]+1;

prev_index = j i

So = max ( ⦅AdZセセNN j S ェ QI QIb | ェZa{ェ}セ aャゥj ェ\ゥ /

ィLL「

ne--Gsᄋj.,i

rtLhzッnueTUoivl

nueUQAeQULEQUeッ

lJn

oGe

m・エ。 。ャァッイゥエィ ュN ウ

Chapter3

QRSTUVWXY

uh

オュv

RR

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution S l ・ エ p { 「 ・ true iff thereis a stonein theriver at x meters.Let'sdefineF[x] [y] tobeaBooleanvariablethatis trueiff it is possible

for the frog to reachx metersfrom the shorewith the last jump beingymeters. We can say that F[O] [y] is true ゥ ケ = O. Also, F[x] [y] canbetrue iff P[x] is true (there is a stonethere) and that either F[x - y][y] ,F[x - ケ}{ K 1], or F[x - y][y • 1] is true. u ウ ゥ d p we cancomputethevaluesof F[n] [y] for all possiblevaluesof y. One ゥ ョ エ ・ イ ・ ウ エ ゥ エ ィ ゥ エ ッ ョ ッ エ ・

hereis thatwhile jumpingthe ヲ ゥ イ ウ エ ュ ・ エ ・ イ ウ thelargestjump sizecouldbeatmostP N Hencewejustneedto worry aboutvaluesof ケ Q P ャ ᄋ

This givesusa runtimeof 0 H QN UIN Hereis a possibleimplementation:

131

previous_index[i] = prev_index;if (max_length < length) {

max_length = length;longest_sequence_end= i;

assert(output != NULL);output->c1ear() ;I I Buil d the reverse of the 1angest sequence by goi ng

backwards from the end.while (longest_sequence_end>= 0) {

output->push_back(input [longest_sequence_end]);longest_sequence_end= previous_index[

longest_sequence_end];

i i ク attainable jump size.int m= sqrt(2 * p.size());vector<vector<boo!> > f (p. size() + 1);II The first block can only be イ・。」ィ・、 ゥエィ jump ofII size 1 and no block can be reached with jump ofII size O.for (int j = 0; j <=m; j++) {

f[O].push_back(false);

std : : reverse(output->begin(), output->end() ) ;

bool isReachable(const vector<boo1>& p) {if (p. si z e () == 0) {

return true;

for (int i = 1; i <p.size(); i++) {for (int j = 1; j <=m; j++) {

f[i].push_back(false);if H ー ゥ - j >= 0) {

for (int i = 1; i <p.size(); i++) {f [i]. push_back(false);

エ イ オ ・ [

jbees

-3244Binur--&[]po([r?A

F?i

epbjᄋ

Ma

123445678901234567890123

ゥQT

ゥQa

ョl

ヲo

ア、

アuaTᄋMf

アオアオ

aaュSヲo

uqjョオMM

GO

Omアオアオ

uウゥョエィゥウ strategyrfill up a tablefor 84·If we wmt the sequmceas・ Q Q ヲ ッ イ ・ 。 、 エ ィ 。 、 、 ゥ ッ ョ to storingthe i ・ ィ of エ ィ ・ オ ・ ョ キ ・ 」 。 ョ

ウ エ ッ M エ ィ ・ ゥ ィ ョ 、 ッ ヲ ャ 。 ウ エ ュA l c::< ::\n ゥュョャ・ュ ャ・ョ ャエ 。エ ゥッ i Q of theidea:

ウ ・ 。 オ ・ ョ 」 ・ N エ ・ イ ・ IS ゥZu ャ ilー

カッゥ、 iッョ ・ウエnッョ、・」イ・。ウゥョァs・アオ・ョ」・ (ョウエ カ・イ \ゥF ゥョーャ カ・イ \ゥ ョエ^J output) {

assert(output 1= NULL);ッ オ エ ー オ エ ^ 」 Q ・ 。 イ () ;if セ H ゥ ョ ー オ エ N size() == 0) {

return;

Solution3.1: Let Si be the i ・ ァ エ ィ of the longestI1ORdeereashgsubse-オ ・ ョ 」 ・ of A thatendsat Am(specifically}A[i]is heludedh this subse-ア オ ・ i N Thenwe canキ エ ィ ・ recurren

ャ ・ ュ ゥ ョ エ Q P ア オ ・ ョ 」 ・ l ・ H エ H I I 1);V r <int> previous M H iャッョァ・ウエs・アオ・セセ・l・セァエィ [0] = 1;int セ max_iEmgth = 1;in t longesL-sequence_end= 0;for (int i=1;i<input-size();i++){

int length = 1;lnt nrev index = -1ヲ セ セ (lnt-j = 0; j < i; A Z I {

if G H ゥ ョ ー セ エ { ェ } <= ゥョー_エ{ゥ}longestsequenceLengthi }]+1 > length){

leRgth=longestsequeI1ceLeRgth[j]+1;

prev_index = j i

So = max ( ⦅AdZセセNN j S ェ QI QIb | ェZa{ェ}セ aャゥj ェ\ゥ /

ィLL「

ne--Gsᄋj.,i

rtLhzッnueTUoivl

nueUQAeQULEQUeッ

lJn

oGe

m・エ。 。ャァッイゥエィ ュN ウ

Chapter3

QRSTUVWXY

uh

オュv

RR

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

133

if (V[ prices[i ]. width] [prices[i ] . length] < prices[i ] .price) {

V[ prices[i ] . width] [prices[i ] . length] = prices[i ] .pnce;

ej

ウコ

}

{{N

]]·1J-FLKA-F

jiヲMMMQェ

Gコ。MMTᄋ

bjᄋGァj

ld

)K[[-JI+fLl]-y

サvkサゥ{

)j[[]

IKvIvゥ

MイKK

ゥィ}KKv

li

G「「ᄋ

bj

「ョク

MfooヲエMェQKhn.

,]-]-JK

m・MMkiMM}

ゥェ

TcGzaMMゥQN

\ゥォ\ゥヲi

mfM

ヲゥMヲゥQ

<kvikvi--fltrit<·];<V;<V

STTウᄋᄋa

·1.

,

1j=1j=

Mゥ

MMQjzᄋQj

.,

[][1

1···A=LA1id--YLA1la--J

baイゥエᄋGゥイ。ァ。Mᆳ

]jェエ

エQQTエイゥMMT

nvinv·-

ja

ᄋOG

イilᄋ

aOエ

イeel

n(V(VstᄋMᄋ

aイKゥイK

ョゥ

イᄋQャゥイMMSQ

·100jz

_a

ウj

DQ

O

VAr--joGG

eヲj

float result = V[width][length];return result;

8

9

uQゥ

アオa

afS

OqoqjョオMM

アオa

ュd

Oquqjau

1111111111222222222233

Solution3.4: This is a straightforwardDP problem. If the input stringS hasi ・ ァ エ ィ キ ・ build a tableT of i ・ エ ィ n suchthatT[k] is a Booleanthattellsusif thesubstringS(0,k) canbebrokeninto asetof valid words.

We canbuild a hashtable of all the valid words suchthat we candetermineif a ウエイゥ ゥウ a valid word or not in 」 ッ ョ ウ エ 。 エ ゥ ュ ・ N ThenT[k]is true ゥ エ ィ ・ イ ・ existsa j {P k - 1] suchthatT[j] is trueandS(j ,k) is avalid word.

ャ ゥ ウ will just tell us if we canbreaka given string into valid wordsbut would not give us the words themselves.With a little morebook-keeping, we can achievethat. Essentially, in table T along with theBooleanvalue, we can also store the beginningindex of the last wordin thestring.

If we wantall possibledecompositions, we canstoreall possibleval-uesof j that givesus a correctbreakwith eachposition. Howeverthenumberof possibledecompositionscanbeexponentialhere. For exam-ple, considerthestring エ ウ ゥ エ ウ ゥ エ ウ ゥ エ ウ N ..".

Solution3.5: We needto determineif thereis a subsetof stateswhosee ャ ・ イ 。 ャ Collegevotesaddup to U セ X = 269. This is a versionof the 0-1knapsackproblemdescribedin Problem6.1 andthe DP solutionto thatproblemcanbeused.

m e l M a l g o r i t h m s

if (f [i-j][j]) {f[i][j] = true;

} else if (j ^ ヲ { ゥ ェ } { ェ M ャ } I {f[ i ][ j] = true;

} else if H ェ ャ \ ュ ヲ { ゥ M ェ } { ェ K ャ } I {f[ i][j] = エイオ・セ

CHAPTER3.132

if(f[i][jhz エ イ オ ・ Q > P …() ) {//From this point the frog can cross thEi i ウ ゥ ョ ァ ャ ・ jump.return true セ

return false;

mae

ュオョュオュm

ァュュュュ

Solution S N S Z s ゥ i Q c ・ エ ィ ・ ュ ャ 。 」 ィ ゥ ョ ・ we have 」 。 イ Q P i Q Q y cut a ー ゥ・」 ・ ッヲー。 ー・ r

ゥィ i Q エ ッエ エキッ w ッー ゥ ・」・ウ ・ゥ エZィ ・ イ v W ・ イ o イゥ エゥ

ィ 。 。カ・ ・エ ッ・ xpャィoイュ・Nl・エ V(ZJ)bethe maximumvak

S R Z [ イ エ ゥ 、 j Z Z z j ゥ ゥ ャ Z Q R Z Z Q R Q エ エ [to 0).

カ ・ assertthat

V(x ,y) = max

H セ H v H 。 ケ I + V(x - a,y)) ,{o ク}

イョN セク (V(x , 「I vHク M b)) ,「 {o ケ}

II1otherwordsrthevalueofthepaperisthemaxofthe」 ッ ウ エ ッ ヲ ィ ・ キ

R t z z 」 R R [ z S R [ Q Q ゥ エ j [ R Z R W [ R R [of V of interestin 0 H ᄋ「 ョI time.

QQQO

"uuzヲGエU

float computeMaxCost(intwidth , int length, vector<PaperPrice

> prices) {カイエ ャG \カ・」エッイ\ヲャッ。エ^ > vセiJJ7}nt izO·i<=width;i++){

V スmM「。、H[イ・イ <float >(length + 1 , 0));

ゥ ッ イ H ゥ m ゥ ] P [ ゥ ゥ 」 ・ ウ N size(); i++) {ゥヲ Hー rices[i]·lez1gth<zlength&&prices[i]-width <-

width) {

1

Sa

Bd

uO

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

133

if (V[ prices[i ]. width] [prices[i ] . length] < prices[i ] .price) {

V[ prices[i ] . width] [prices[i ] . length] = prices[i ] .pnce;

ej

ウコ

}

{{N

]]·1J-FLKA-F

jiヲMMMQェ

Gコ。MMTᄋ

bjᄋGァj

ld

)K[[-JI+fLl]-y

サvkサゥ{

)j[[]

IKvIvゥ

MイKK

ゥィ}KKv

li

G「「ᄋ

bj

「ョク

MfooヲエMェQKhn.

,]-]-JK

m・MMkiMM}

ゥェ

TcGzaMMゥQN

\ゥォ\ゥヲi

mfM

ヲゥMヲゥQ

<kvikvi--fltrit<·];<V;<V

STTウᄋᄋa

·1.

,

1j=1j=

Mゥ

MMQjzᄋQj

.,

[][1

1···A=LA1id--YLA1la--J

baイゥエᄋGゥイ。ァ。Mᆳ

]jェエ

エQQTエイゥMMT

nvinv·-

ja

ᄋOG

イilᄋ

aOエ

イeel

n(V(VstᄋMᄋ

aイKゥイK

ョゥ

イᄋQャゥイMMSQ

·100jz

_a

ウj

DQ

O

VAr--joGG

eヲj

float result = V[width][length];return result;

8

9

uQゥ

アオa

afS

OqoqjョオMM

アオa

ュd

Oquqjau

1111111111222222222233

Solution3.4: This is a straightforwardDP problem. If the input stringS hasi ・ ァ エ ィ キ ・ build a tableT of i ・ エ ィ n suchthatT[k] is a Booleanthattellsusif thesubstringS(0,k) canbebrokeninto asetof valid words.

We canbuild a hashtable of all the valid words suchthat we candetermineif a ウエイゥ ゥウ a valid word or not in 」 ッ ョ ウ エ 。 エ ゥ ュ ・ N ThenT[k]is true ゥ エ ィ ・ イ ・ existsa j {P k - 1] suchthatT[j] is trueandS(j ,k) is avalid word.

ャ ゥ ウ will just tell us if we canbreaka given string into valid wordsbut would not give us the words themselves.With a little morebook-keeping, we can achievethat. Essentially, in table T along with theBooleanvalue, we can also store the beginningindex of the last wordin thestring.

If we wantall possibledecompositions, we canstoreall possibleval-uesof j that givesus a correctbreakwith eachposition. Howeverthenumberof possibledecompositionscanbeexponentialhere. For exam-ple, considerthestring エ ウ ゥ エ ウ ゥ エ ウ ゥ エ ウ N ..".

Solution3.5: We needto determineif thereis a subsetof stateswhosee ャ ・ イ 。 ャ Collegevotesaddup to U セ X = 269. This is a versionof the 0-1knapsackproblemdescribedin Problem6.1 andthe DP solutionto thatproblemcanbeused.

m e l M a l g o r i t h m s

if (f [i-j][j]) {f[i][j] = true;

} else if (j ^ ヲ { ゥ ェ } { ェ M ャ } I {f[ i ][ j] = true;

} else if H ェ ャ \ ュ ヲ { ゥ M ェ } { ェ K ャ } I {f[ i][j] = エイオ・セ

CHAPTER3.132

if(f[i][jhz エ イ オ ・ Q > P …() ) {//From this point the frog can cross thEi i ウ ゥ ョ ァ ャ ・ jump.return true セ

return false;

mae

ュオョュオュm

ァュュュュ

Solution S N S Z s ゥ i Q c ・ エ ィ ・ ュ ャ 。 」 ィ ゥ ョ ・ we have 」 。 イ Q P i Q Q y cut a ー ゥ・」 ・ ッヲー。 ー・ r

ゥィ i Q エ ッエ エキッ w ッー ゥ ・」・ウ ・ゥ エZィ ・ イ v W ・ イ o イゥ エゥ

ィ 。 。カ・ ・エ ッ・ xpャィoイュ・Nl・エ V(ZJ)bethe maximumvak

S R Z [ イ エ ゥ 、 j Z Z z j ゥ ゥ ャ Z Q R Z Z Q R Q エ エ [to 0).

カ ・ assertthat

V(x ,y) = max

H セ H v H 。 ケ I + V(x - a,y)) ,{o ク}

イョN セク (V(x , 「I vHク M b)) ,「 {o ケ}

II1otherwordsrthevalueofthepaperisthemaxofthe」 ッ ウ エ ッ ヲ ィ ・ キ

R t z z 」 R R [ z S R [ Q Q ゥ エ j [ R Z R W [ R R [of V of interestin 0 H ᄋ「 ョI time.

QQQO

"uuzヲGエU

float computeMaxCost(intwidth , int length, vector<PaperPrice

> prices) {カイエ ャG \カ・」エッイ\ヲャッ。エ^ > vセiJJ7}nt izO·i<=width;i++){

V スmM「。、H[イ・イ <float >(length + 1 , 0));

ゥ ッ イ H ゥ m ゥ ] P [ ゥ ゥ 」 ・ ウ N size(); i++) {ゥヲ Hー rices[i]·lez1gth<zlength&&prices[i]-width <-

width) {

1

Sa

Bd

uO

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

lHmI]zgefaスHュ。クHlHRW「MQI_ Hb ゥ II )

ゥ]ク Q

In otherwords, we find the right valueof x suchthat if we packthefirst x usersin b- 1 serversandthe remainingin thelast ウ ・ イ カ ・ エ ィ ・ ュ 。 ク

loadon a givenserveris minimized.Using this relationship, we can tabulatethe valuesof L till we get

L(n,m). While computingL(a,b) when the values of L is tabulatedfor alllower valuesof 。 ョ 、 b, we needto find the right value of x tominimize the load. As we increasex, L(x,b - 1) in the aboveexpres-ウ ゥ ゥ ョ 」 イ ・ 。 ウ ・ ウ and the term B ᆪ z Q (B i )) decreases.Hencein order tofind x thatminimizestheir max, we cando a binarysearchfor x whichcanbe done in 0 Hャ ッァI time. Thereforewe can computethe load inO(mnlog(n)) time

Solution3.8: Let V (g) bethevoltagelevelassignedto gateg. Let I (g) bethesetof all gatesthatare ゥ オ エ ウ to g. Let P(g) betheminimumpossiblepowerthat canbe achievedby a legal assignmentof voltages, whereinwe choosea low voltagefor gateg. Let Q(g) be the ュ ゥ ョ オ ュ possiblepowerthatcanbe achievedwhen9 is assigneda high voltage. We can

Solution 3.6: Number the individual elections from 1 to 446. Lett H b) betheprobabilitythatexactlybRepublicanswin out of electionsサ Q R _ ス N

Let Xi be the event that a Republicanwins the i-th race. Thent H 「 I = pイHBᆪゥ\ M b). There are two ways in which the ヲ ゥ イ ウ エ

randomvariablessumup to b: thea-thrandomvariableis 1 andthefirstM 1 variablessumup to b - 1 or the a-thrandomvariableis 0 andtheヲ ゥ イ ウ エ M 1 randomvariablessumup to b. s ャ」・ theseeventsareexclusive,theprobability t H b) is thesumof theprobabilitiesof thesetwo events.To beprecise,

tH ]tH M 1,b- 1) . p tH MQ b) . (1 - p I .

ti 。ウ・ casesfor therecursionareT(O,0) = 1 andT(O,b) = 0, for b > 0.ThereforeT canbe computedusing d s ゥ ョ 」 ・ 「 ッ エ ィ 。 ョ 、 b takevaluesfrom°to thenumberof racesandcomputingt H b) from earliervaluestakesconstanttime, thecomplexityis quadraticin thenumberof races.

Solution S l ・ エ l H b) be the maximumload on a serverwhenuserswith hashhI through ィ 。 イ ・ assignedto servers81 through8b in anop-timal way so that themaxloadis ュ ゥ ョ ョ ゥ コ ・ 、 N We observethe followingrecurrence:

write the follOWing recurrencerelationshipfor P andQ:

135

uウゥ エィ・ウ・ equati?mryve cantabulatethevaluesof p adQ for dlgatesandour mswermgomgto comefrom themaximumof thevaluesof P md Q-for the gateat the root of the tree-SiRcewe perform omstant ッ ー ・ ー ・ イ ァ 。 エ ・ f ィ omdlcomplexityisqqrwhereGisthenumberof Q:ates

Solution3.9:Wecan formulate this DP hamazmersimilar to Solu-ti023.8.ForeachRodeFwe tabulatek values.Letャ v H 「 ・ the m;

umber ッ ヲ 「 イ ウ neededfor the ウ エ イ ・ ・ rootedat nodeu, if thefirst bufferabovethmI1odeappearsJor morehopsaway.TherecurreIIcerelationshipcanbedefinedby

n H I [ ュ ゥ H Q n H 」 ォ I n H 」 ュ Q k)) )」 iH O

P(g) = Q qH I

Q(g) = 2 K ュゥョ HpH I qH I

we cmtabulatethevalueof N for all Rodesfromtheleaf to therootfor all valuesof ャ ォ N t ィ ・ イ Q n H ォI where ゥ ウ the イ ッ o エ ゥ ウ the ュ ゥ ョ ゥ ュョ Q

the overall 」 ッ ュ ー ャ ・ ク ゥ エ ケ ゥ s O(G.k)rwhereG is theI1umberof gates-

30lutionuo:Let'sャ 。 「 ・ ャ エ ヲ エ ゥ 」 ・ ウ of the polygon 1, ftrom an 。 ゥ 「 イ ケ カ カ ・ イ キ 。 ャ ォ ゥ エ 」 ャ ッ キ ゥ ウ ・ N l ・ エ c H ー Q ᄋ N N pー ォ I 「 ・ エ ィc ッ ウ エ of エ イ ゥ 。 ョ Q Y オ ャ 。 。 エ ゥ ョ ァ the ー ッ ャ ケ ァ ッ i r 1formedby verticesPl throughー ォ N Letl H M 「 I 「 ・ th lmgthof thestraightlhe drawI1fromvertex エ ッ vertexb.

Now:, we kn?wthat if エ ィ ・ ョ オ ュ 「 ・ イ of カ ・ イ イ エ エ

o イ ャ ・ ウ s f thecostmzero.Considerm edge H p T W p Q I N Oneof the tria‘

ゥゥ[Z ZRRZRゥZ ゥ ェ[ Aヲ ZQゥ [ Zゥ ゥ ャZ[[ ZZAゥ [ZR Zゥ :E;[ ェ コZ ゥ ゥ[ [ ヲ ゥeZe Z Z[コ [ゥ ェRェ e ;

CHAPTER3. META-ALGORITHMS134

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

lHmI]zgefaスHュ。クHlHRW「MQI_ Hb ゥ II )

ゥ]ク Q

In otherwords, we find the right valueof x suchthat if we packthefirst x usersin b- 1 serversandthe remainingin thelast ウ ・ イ カ ・ エ ィ ・ ュ 。 ク

loadon a givenserveris minimized.Using this relationship, we can tabulatethe valuesof L till we get

L(n,m). While computingL(a,b) when the values of L is tabulatedfor alllower valuesof 。 ョ 、 b, we needto find the right value of x tominimize the load. As we increasex, L(x,b - 1) in the aboveexpres-ウ ゥ ゥ ョ 」 イ ・ 。 ウ ・ ウ and the term B ᆪ z Q (B i )) decreases.Hencein order tofind x thatminimizestheir max, we cando a binarysearchfor x whichcanbe done in 0 Hャ ッァI time. Thereforewe can computethe load inO(mnlog(n)) time

Solution3.8: Let V (g) bethevoltagelevelassignedto gateg. Let I (g) bethesetof all gatesthatare ゥ オ エ ウ to g. Let P(g) betheminimumpossiblepowerthat canbe achievedby a legal assignmentof voltages, whereinwe choosea low voltagefor gateg. Let Q(g) be the ュ ゥ ョ オ ュ possiblepowerthatcanbe achievedwhen9 is assigneda high voltage. We can

Solution 3.6: Number the individual elections from 1 to 446. Lett H b) betheprobabilitythatexactlybRepublicanswin out of electionsサ Q R _ ス N

Let Xi be the event that a Republicanwins the i-th race. Thent H 「 I = pイHBᆪゥ\ M b). There are two ways in which the ヲ ゥ イ ウ エ

randomvariablessumup to b: thea-thrandomvariableis 1 andthefirstM 1 variablessumup to b - 1 or the a-thrandomvariableis 0 andtheヲ ゥ イ ウ エ M 1 randomvariablessumup to b. s ャ」・ theseeventsareexclusive,theprobability t H b) is thesumof theprobabilitiesof thesetwo events.To beprecise,

tH ]tH M 1,b- 1) . p tH MQ b) . (1 - p I .

ti 。ウ・ casesfor therecursionareT(O,0) = 1 andT(O,b) = 0, for b > 0.ThereforeT canbe computedusing d s ゥ ョ 」 ・ 「 ッ エ ィ 。 ョ 、 b takevaluesfrom°to thenumberof racesandcomputingt H b) from earliervaluestakesconstanttime, thecomplexityis quadraticin thenumberof races.

Solution S l ・ エ l H b) be the maximumload on a serverwhenuserswith hashhI through ィ 。 イ ・ assignedto servers81 through8b in anop-timal way so that themaxloadis ュ ゥ ョ ョ ゥ コ ・ 、 N We observethe followingrecurrence:

write the follOWing recurrencerelationshipfor P andQ:

135

uウゥ エィ・ウ・ equati?mryve cantabulatethevaluesof p adQ for dlgatesandour mswermgomgto comefrom themaximumof thevaluesof P md Q-for the gateat the root of the tree-SiRcewe perform omstant ッ ー ・ ー ・ イ ァ 。 エ ・ f ィ omdlcomplexityisqqrwhereGisthenumberof Q:ates

Solution3.9:Wecan formulate this DP hamazmersimilar to Solu-ti023.8.ForeachRodeFwe tabulatek values.Letャ v H 「 ・ the m;

umber ッ ヲ 「 イ ウ neededfor the ウ エ イ ・ ・ rootedat nodeu, if thefirst bufferabovethmI1odeappearsJor morehopsaway.TherecurreIIcerelationshipcanbedefinedby

n H I [ ュ ゥ H Q n H 」 ォ I n H 」 ュ Q k)) )」 iH O

P(g) = Q qH I

Q(g) = 2 K ュゥョ HpH I qH I

we cmtabulatethevalueof N for all Rodesfromtheleaf to therootfor all valuesof ャ ォ N t ィ ・ イ Q n H ォI where ゥ ウ the イ ッ o エ ゥ ウ the ュ ゥ ョ ゥ ュョ Q

the overall 」 ッ ュ ー ャ ・ ク ゥ エ ケ ゥ s O(G.k)rwhereG is theI1umberof gates-

30lutionuo:Let'sャ 。 「 ・ ャ エ ヲ エ ゥ 」 ・ ウ of the polygon 1, ftrom an 。 ゥ 「 イ ケ カ カ ・ イ キ 。 ャ ォ ゥ エ 」 ャ ッ キ ゥ ウ ・ N l ・ エ c H ー Q ᄋ N N pー ォ I 「 ・ エ ィc ッ ウ エ of エ イ ゥ 。 ョ Q Y オ ャ 。 。 エ ゥ ョ ァ the ー ッ ャ ケ ァ ッ i r 1formedby verticesPl throughー ォ N Letl H M 「 I 「 ・ th lmgthof thestraightlhe drawI1fromvertex エ ッ vertexb.

Now:, we kn?wthat if エ ィ ・ ョ オ ュ 「 ・ イ of カ ・ イ イ エ エ

o イ ャ ・ ウ s f thecostmzero.Considerm edge H p T W p Q I N Oneof the tria‘

ゥゥ[Z ZRRZRゥZ ゥ ェ[ Aヲ ZQゥ [ Zゥ ゥ ャZ[[ ZZAゥ [ZR Zゥ :E;[ ェ コZ ゥ ゥ[ [ ヲ ゥeZe Z Z[コ [ゥ ェRェ e ;

CHAPTER3. META-ALGORITHMS134

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

136 CHAPTER3. mel algorithms

we canwrite the following recurrencerelationship:

A(p1,'" I = クZ エlkHaHーSG pクI

A(px,' . . pォ pQー QI lH pQ pRpRRI lH pQ ppク I + lH pR ppク I

If we tabulatethecostof エ イ ゥ 。 ョ ァ オ ャ 。 エ ゥ ッ イ Q of eachpolygon エ ィ 。 エ is a resultofp ゥ 」 ォ ゥ ョ Q Y ウ オ 「 ウ ・ ア オ ・ イ ャ エ ー ッ Q エ ウ P i Q the ッ イ ゥ i ャ 。 ャ polygon, wewouldneedto dothis for roughlyn2polygons.Ifwehavealreadytabulatedthevalueforall smallerpolygomrit will takeus O(n)timefor 、 ッ ゥ ウ ッ N Hencewe」 。 」 ッ ュ ー オ エ ・ th;minimumcostin O(n3

) time.

Solution3.11: We focusonthecasewhereall theoperandsarenonneg-ative integersandthe only operationsare· 。 ョ 、 M

Representthe ・ ク ー イ ・ ウ ウ ゥ ッ ョ ッ 00 V1 ᄚQ ᄚョMR MQ by arraysV =

{ P NNG v MQ} and00, . . . ,On-2'Let MaxHJ]denotethe maximumvalueachievableby someー。 イ ・

thesizationfor thesubexpressionVi 0i Vi °i … OJVj , whereMax[i , j] is just

V[i]ィ・ key to ウ ッ ャ カ ゥ ョ ァ ゥ ウ problemis to イ ・ ョ ゥ コ ・ エ ィ 。 エ ゥ ヲ ッ ー ・ ュ エ ゥ ゥ is

performedlastrthesubexpressiomuoo o u Q P Q ᄋ ᄋ ᄋ ッ エ j u T M ャ 。 r g u Q オ Q

-o q U---l mustbeparenthesizedto bemaximizedhdividually-

i T [ ス _ [ ス m イ エi in { P R } so

Max[07n-1]=max m 。 ク { o j } P T m 。 ク { ゥ Q ョ -1].T { P ョ M R }

Thetotalnumberofrecursivecalls is O((2))mdeachcallrequirfsoh)additiomlcomputatiOI1tocombinetheresultsrleadingtomO(nd)algo-

rithme ヲ ゥ ゥ 」 ゥ ・ i 」 ッ ュ ー オ エ ゥ ョ ァ エ ィ ゥ ウ

ウ オ i エ ウ becached.In code:

1 Ipublic class p 。 イ ・

23456789

10111213

int [] V;char { } [

int [] [] Max;boolean [][] valid;

public Parens(int [] V , 」ィ。イ oーI {this.V = V;this.Op ][

137

456789012344567890123456789012345678QJ0123456789012341i1i1···A1

...QゥQゥ

GGGMG

ィアオアオ

Sアオ

jアオアオ

STTa

aエTTa

aa

BSーィjfSfS

Sュdュィ、

SfSB

ヲo

public int maxExpr(int begin, int end) {

if ( valid[begin][end] ) {return Max[begin][end];

if ( begin == end ) {Max[begin][end] = V[begin];valid[begin][end] = true;return V[ begin];

if ( begin + 1 -- end ) {m 「・ァゥョ} [end] = Hoー {「・ァゥ ]] '+') ?

V[begin] + V[end]V[begin] * v{・ 、}[

valid[begin][end] = true;return Max[begin] [end];

int max = Integer.MIN_VALUE;in t candidateMax= 0;for ( int i = begin + 1; i < end; i++ ) {

int lMax = maxExpr( begin, i);int rMax = maxExpr( i +1 , end);ゥ ヲ H { ゥ } ] ] ) {

candidateMax= lMax + イ Q。ク[

} else {candidateMax= lMax * rMax;

max = (max < candidateMax) ? candidateMax : max;

m 。 ク { 「 ・ ァ ゥ } [end] = max;valid [begin][ end] = true;return max;

public int maxExpr()int N = V.length;Max = new in t [N] [N] ;valid = new boolean [N] [N];return maxExpr(O,N-l);

public static void main( String [] args ) {

int [] vi = {1 , 2 , 3 ,3 , 2 , 1};char [] 01 = {'+','*','*','+', '+'};

Parens expl = new p 。 イ ・ ョ ウ H 01);System.out. println ("Max.....value.....of.....expression.....is :" + expl.

maxExpr( ) );6566

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

136 CHAPTER3. mel algorithms

we canwrite the following recurrencerelationship:

A(p1,'" I = クZ エlkHaHーSG pクI

A(px,' . . pォ pQー QI lH pQ pRpRRI lH pQ ppク I + lH pR ppク I

If we tabulatethecostof エ イ ゥ 。 ョ ァ オ ャ 。 エ ゥ ッ イ Q of eachpolygon エ ィ 。 エ is a resultofp ゥ 」 ォ ゥ ョ Q Y ウ オ 「 ウ ・ ア オ ・ イ ャ エ ー ッ Q エ ウ P i Q the ッ イ ゥ i ャ 。 ャ polygon, wewouldneedto dothis for roughlyn2polygons.Ifwehavealreadytabulatedthevalueforall smallerpolygomrit will takeus O(n)timefor 、 ッ ゥ ウ ッ N Hencewe」 。 」 ッ ュ ー オ エ ・ th;minimumcostin O(n3

) time.

Solution3.11: We focusonthecasewhereall theoperandsarenonneg-ative integersandthe only operationsare· 。 ョ 、 M

Representthe ・ ク ー イ ・ ウ ウ ゥ ッ ョ ッ 00 V1 ᄚQ ᄚョMR MQ by arraysV =

{ P NNG v MQ} and00, . . . ,On-2'Let MaxHJ]denotethe maximumvalueachievableby someー。 イ ・

thesizationfor thesubexpressionVi 0i Vi °i … OJVj , whereMax[i , j] is just

V[i]ィ・ key to ウ ッ ャ カ ゥ ョ ァ ゥ ウ problemis to イ ・ ョ ゥ コ ・ エ ィ 。 エ ゥ ヲ ッ ー ・ ュ エ ゥ ゥ is

performedlastrthesubexpressiomuoo o u Q P Q ᄋ ᄋ ᄋ ッ エ j u T M ャ 。 r g u Q オ Q

-o q U---l mustbeparenthesizedto bemaximizedhdividually-

i T [ ス _ [ ス m イ エi in { P R } so

Max[07n-1]=max m 。 ク { o j } P T m 。 ク { ゥ Q ョ -1].T { P ョ M R }

Thetotalnumberofrecursivecalls is O((2))mdeachcallrequirfsoh)additiomlcomputatiOI1tocombinetheresultsrleadingtomO(nd)algo-

rithme ヲ ゥ ゥ 」 ゥ ・ i 」 ッ ュ ー オ エ ゥ ョ ァ エ ィ ゥ ウ

ウ オ i エ ウ becached.In code:

1 Ipublic class p 。 イ ・

23456789

10111213

int [] V;char { } [

int [] [] Max;boolean [][] valid;

public Parens(int [] V , 」ィ。イ oーI {this.V = V;this.Op ][

137

456789012344567890123456789012345678QJ0123456789012341i1i1···A1

...QゥQゥ

GGGMG

ィアオアオ

Sアオ

jアオアオ

STTa

aエTTa

aa

BSーィjfSfS

Sュdュィ、

SfSB

ヲo

public int maxExpr(int begin, int end) {

if ( valid[begin][end] ) {return Max[begin][end];

if ( begin == end ) {Max[begin][end] = V[begin];valid[begin][end] = true;return V[ begin];

if ( begin + 1 -- end ) {m 「・ァゥョ} [end] = Hoー {「・ァゥ ]] '+') ?

V[begin] + V[end]V[begin] * v{・ 、}[

valid[begin][end] = true;return Max[begin] [end];

int max = Integer.MIN_VALUE;in t candidateMax= 0;for ( int i = begin + 1; i < end; i++ ) {

int lMax = maxExpr( begin, i);int rMax = maxExpr( i +1 , end);ゥ ヲ H { ゥ } ] ] ) {

candidateMax= lMax + イ Q。ク[

} else {candidateMax= lMax * rMax;

max = (max < candidateMax) ? candidateMax : max;

m 。 ク { 「 ・ ァ ゥ } [end] = max;valid [begin][ end] = true;return max;

public int maxExpr()int N = V.length;Max = new in t [N] [N] ;valid = new boolean [N] [N];return maxExpr(O,N-l);

public static void main( String [] args ) {

int [] vi = {1 , 2 , 3 ,3 , 2 , 1};char [] 01 = {'+','*','*','+', '+'};

Parens expl = new p 。 イ ・ ョ ウ H 01);System.out. println ("Max.....value.....of.....expression.....is :" + expl.

maxExpr( ) );6566

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

For the more geI1eralcasesrwe needto keeptrack-of the mhiII111mア 、 maximumvaluesaswell asthepositiveandmgat1vevaluesclosestコ o e m i 。 ォ ・ ウ エ ィ ・ ・ ッ 、 ・ ュ ッ イ ・ 」 ッ ュ ー ャ ゥ 」 。 エ ・ 、 「 、 ッ ・ ウ ョ ッ エcharacterandthecomplexityof thealgorithm.

Solution3.12: w ・ ィ ・ 、 オ ャ ・ エ オ エ ッ イ ウ gmdi137:assoomsthereisarequestthat cannotbe handledby the previously 。 ウ ウ ゥ ・ 、 tutors, we choosea

newtutorMile it is simple tOゥ ュ ーi・ i ュ・

, ial エ ッ prove エZ ィ 。 エ ゥ エ ゥ ウ ッ ー エ ゥ ュ オ ュ ゥ N ・ N we cannotcoverall the requestswith

fewer tutorsII1order-to prove the optimality we will defhethe RotiOI1of slack.

Considera set of requestsj1 ,… ,jn suchthat the requestsare orderedby the time theyReedto bedone.LetS(j)aRdE(j)bethestarthgandendingtime for requestj. Let ti ,… ,t m be the timeswhenwtutor, orderedby time.

Defimthe エ ィ ・ last tutor assignedhasavailableafterhis last re-questis fulfilled astheslackh theschedule.

we clamthatgreedyschedulingis theoptimumscheduling.Wecmprovethis ushginductiOIIovertheRUmberof requests;forour hdue-

ィ ゥ ー イ Z Q エ Z j i z j z z コ エ エ イ ケ ウ エ e ゥゥ イ l ・ M 、 、 ケ ケ ヲ ェ f 。 。 Qア オ ・ ウ エ [ clearlythis is the ウ エ イ 。 エ ・ ァ ケ エ ィ 。 エ オ ウ ・ ウ エ ィ ・ ュ ャ ゥ ョ ャ ゥ ュ ャ オ ュ

;nrl no moreslackis possible.ォ ・ B ゥ ュ ュ ・ 。 エ エ サ エ ィ エ ィ ゥ ゥ ウ ウ エ エ 。 エ エ ・ イ ュ ・

B ャ a t ^ ャ ョ t i ャ G キ ッ ッ キ カ this for = ォKャ。ウ follows: 」 ッ イ ャ s ゥ 、 ・ イ therequestsJll' . . ,Jk[lj T ェ[ エ エィゥ・ ゥ イ ウエ。イエ イm エ エ エ ゥュ・Nco ョウ s ・ 。エ エ .. エ ュ 。 イ NChed1u1ledthe tutors to covertheserequeStSbasedOR the greedystrat-・ ァ カ G N n ッ キ セ whenwe addthe ョ ・ ・ ク エ イ ・ ア オ ・ ウ エ jk Q エ ッ the ャ ゥ ウ エ エ eitherit can「 e

カ・イ・、 bv ウ ャ 。 」 ォ ッ イ ゥ エ ュ 。 ケ イ ・ ア オ ゥ イ ・ anewtutor.h the;asethe O Q Q requestcanbe 」 ッ カ ・ 「 ケ the slack, 」ャ・。 エィゥウ

is the optimumsolution(if weneededatleastm tutorsto coverthefirstYBy we cazmotcoverthe ォ Q requestswith fewer tutors). Also,

c ウゥイ ゥゥョ Q 」ィ・、オ ャ[lゥゥ N。 w ・」 QP エィ 。 カ・。「・ ウ」ィ・、オャ・ キゥエィ ウ thatcoverall ォ ャ

requestsandhaveabiggerslack.meedto pick m additiOI1altutorfor the k +Lth requestr

it mustbe that the m-th tutor did not havethe slackto cover the lastreauest. If thereゥ ウ Q ッ エ ィ ・ イ wayto covertherequestswith m or lesstutorsrthemvecanusethesamesetofMtomtocoverthefirstォ イ ・ ウ エ ウ mdget

ゥ エ コ エ ュ コ イ コ ヲ c Z Q Q Z イ R R S D W S R S l z lmustmaximizetheslack.

138 CHAPTER3. m e ョ N a l g o r i t h m s 139

SolutionS N Q S Z Let'ssay エ ィ 。 エ thetime for エ ィ ・ エ M エ ィ 」 オ ウ ウ エ ッ ュ ャ ・ イ エ ッ beservicedゥ s c ゥ N t ィ ・ イ ィ ・ キ 。 ゥ エ ゥ ョ ァ エ ゥ ュ ・ for エ ィ ・ 」 オ ウ エ ッ ュ ・ イ c ゥ ュ

sumof all thewait timeswouldbe

l エ ・ ェ = R i

Sincewe want to minimize the total wait time for all the customersandCiS musttakevaluesfrom 1 throughn, it follows that thecustomerswho take the smallesttime must get servedfirst. Hencewe mustsortthe customersby their servicetime andthenservethemin the orderofincreasingservicetime.

Solution3.14: Huffmancodingis anoptimumsolutionto this problem(therecouldbeotheroptimumcodesaswell). Huffmancodingproceedsin threesteps:

1. Sortsymbolsin increasingorderof probabilityandcreateabinarytreenodefor eachsymbol.

2. Createanewnodeby combiningthesmallestprobabilitynodesaschildrenandassigningit theprobabilityof thesumof its children.

3. Removethechildrenfrom considerationandaddthenewnodeintothesortedlist of nodestobecombinedandrepeattheentireprocesstill we havea singlerootedbinarytree.

P 」 ・ wehavetherootedtree, wecanassignall theleft edgesas0 andthe right edgesas1. All theoriginal symbolswould bethe leafnodesinthis tree and the pathfrom root to the leaf nodewould give us the bitウ ・ ア オ ・ 」 ・ for thatsymbol.

Now, we needto prove (1.) this encodingis optimumand(2.) find afast implementationof this algorithm.

Forimplementingthis idea, wecanmaintainamin-heapof candidatenodesthat canbe combinedin any given step. Sinceeachcombinationsteprequirestwo extract-minandoneinsertoperationthatcanbedoneino(logn) time, we canfind theHuffmancodesin 0 Hャッァ エゥュ・N

We canprovetheoptimality of hオヲ ャ。ョ codesinductively. For a sin-gle code, obviously h オ ヲ ャ 。 ョ codesareoptimum. Let's saythat for anyprobability 、 ゥ ウ エ イ ゥ 「 オ エ ゥ ッ ョ ッ ァ n symbols, Huffmancodesareoptimum.Giventhis assumption, we will proveit is true for ョ Q N Supposethereis anotherencodingthathasa smallerexpectedlengthof codefor someprobabilitydistributionfor n + 1symbols.

For anyencoding, we canmapthe codesto a binarytreeby creatingthenull stringto root andaddinga left edgefor each0 anda right edgefor each1. We canmakeseveralobservationsaboutthisbinarytree:

- Eachsymbolmust map to a leaf node; otherwise, our prefix as-ウ オ ュ ー エ ゥ ッ キ ゥ ャ ャ beviolated.

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

For the more geI1eralcasesrwe needto keeptrack-of the mhiII111mア 、 maximumvaluesaswell asthepositiveandmgat1vevaluesclosestコ o e m i 。 ォ ・ ウ エ ィ ・ ・ ッ 、 ・ ュ ッ イ ・ 」 ッ ュ ー ャ ゥ 」 。 エ ・ 、 「 、 ッ ・ ウ ョ ッ エcharacterandthecomplexityof thealgorithm.

Solution3.12: w ・ ィ ・ 、 オ ャ ・ エ オ エ ッ イ ウ gmdi137:assoomsthereisarequestthat cannotbe handledby the previously 。 ウ ウ ゥ ・ 、 tutors, we choosea

newtutorMile it is simple tOゥ ュ ーi・ i ュ・

, ial エ ッ prove エZ ィ 。 エ ゥ エ ゥ ウ ッ ー エ ゥ ュ オ ュ ゥ N ・ N we cannotcoverall the requestswith

fewer tutorsII1order-to prove the optimality we will defhethe RotiOI1of slack.

Considera set of requestsj1 ,… ,jn suchthat the requestsare orderedby the time theyReedto bedone.LetS(j)aRdE(j)bethestarthgandendingtime for requestj. Let ti ,… ,t m be the timeswhenwtutor, orderedby time.

Defimthe エ ィ ・ last tutor assignedhasavailableafterhis last re-questis fulfilled astheslackh theschedule.

we clamthatgreedyschedulingis theoptimumscheduling.Wecmprovethis ushginductiOIIovertheRUmberof requests;forour hdue-

ィ ゥ ー イ Z Q エ Z j i z j z z コ エ エ イ ケ ウ エ e ゥゥ イ l ・ M 、 、 ケ ケ ヲ ェ f 。 。 Qア オ ・ ウ エ [ clearlythis is the ウ エ イ 。 エ ・ ァ ケ エ ィ 。 エ オ ウ ・ ウ エ ィ ・ ュ ャ ゥ ョ ャ ゥ ュ ャ オ ュ

;nrl no moreslackis possible.ォ ・ B ゥ ュ ュ ・ 。 エ エ サ エ ィ エ ィ ゥ ゥ ウ ウ エ エ 。 エ エ ・ イ ュ ・

B ャ a t ^ ャ ョ t i ャ G キ ッ ッ キ カ this for = ォKャ。ウ follows: 」 ッ イ ャ s ゥ 、 ・ イ therequestsJll' . . ,Jk[lj T ェ[ エ エィゥ・ ゥ イ ウエ。イエ イm エ エ エ ゥュ・Nco ョウ s ・ 。エ エ .. エ ュ 。 イ NChed1u1ledthe tutors to covertheserequeStSbasedOR the greedystrat-・ ァ カ G N n ッ キ セ whenwe addthe ョ ・ ・ ク エ イ ・ ア オ ・ ウ エ jk Q エ ッ the ャ ゥ ウ エ エ eitherit can「 e

カ・イ・、 bv ウ ャ 。 」 ォ ッ イ ゥ エ ュ 。 ケ イ ・ ア オ ゥ イ ・ anewtutor.h the;asethe O Q Q requestcanbe 」 ッ カ ・ 「 ケ the slack, 」ャ・。 エィゥウ

is the optimumsolution(if weneededatleastm tutorsto coverthefirstYBy we cazmotcoverthe ォ Q requestswith fewer tutors). Also,

c ウゥイ ゥゥョ Q 」ィ・、オ ャ[lゥゥ N。 w ・」 QP エィ 。 カ・。「・ ウ」ィ・、オャ・ キゥエィ ウ thatcoverall ォ ャ

requestsandhaveabiggerslack.meedto pick m additiOI1altutorfor the k +Lth requestr

it mustbe that the m-th tutor did not havethe slackto cover the lastreauest. If thereゥ ウ Q ッ エ ィ ・ イ wayto covertherequestswith m or lesstutorsrthemvecanusethesamesetofMtomtocoverthefirstォ イ ・ ウ エ ウ mdget

ゥ エ コ エ ュ コ イ コ ヲ c Z Q Q Z イ R R S D W S R S l z lmustmaximizetheslack.

138 CHAPTER3. m e ョ N a l g o r i t h m s 139

SolutionS N Q S Z Let'ssay エ ィ 。 エ thetime for エ ィ ・ エ M エ ィ 」 オ ウ ウ エ ッ ュ ャ ・ イ エ ッ beservicedゥ s c ゥ N t ィ ・ イ ィ ・ キ 。 ゥ エ ゥ ョ ァ エ ゥ ュ ・ for エ ィ ・ 」 オ ウ エ ッ ュ ・ イ c ゥ ュ

sumof all thewait timeswouldbe

l エ ・ ェ = R i

Sincewe want to minimize the total wait time for all the customersandCiS musttakevaluesfrom 1 throughn, it follows that thecustomerswho take the smallesttime must get servedfirst. Hencewe mustsortthe customersby their servicetime andthenservethemin the orderofincreasingservicetime.

Solution3.14: Huffmancodingis anoptimumsolutionto this problem(therecouldbeotheroptimumcodesaswell). Huffmancodingproceedsin threesteps:

1. Sortsymbolsin increasingorderof probabilityandcreateabinarytreenodefor eachsymbol.

2. Createanewnodeby combiningthesmallestprobabilitynodesaschildrenandassigningit theprobabilityof thesumof its children.

3. Removethechildrenfrom considerationandaddthenewnodeintothesortedlist of nodestobecombinedandrepeattheentireprocesstill we havea singlerootedbinarytree.

P 」 ・ wehavetherootedtree, wecanassignall theleft edgesas0 andthe right edgesas1. All theoriginal symbolswould bethe leafnodesinthis tree and the pathfrom root to the leaf nodewould give us the bitウ ・ ア オ ・ 」 ・ for thatsymbol.

Now, we needto prove (1.) this encodingis optimumand(2.) find afast implementationof this algorithm.

Forimplementingthis idea, wecanmaintainamin-heapof candidatenodesthat canbe combinedin any given step. Sinceeachcombinationsteprequirestwo extract-minandoneinsertoperationthatcanbedoneino(logn) time, we canfind theHuffmancodesin 0 Hャッァ エゥュ・N

We canprovetheoptimality of hオヲ ャ。ョ codesinductively. For a sin-gle code, obviously h オ ヲ ャ 。 ョ codesareoptimum. Let's saythat for anyprobability 、 ゥ ウ エ イ ゥ 「 オ エ ゥ ッ ョ ッ ァ n symbols, Huffmancodesareoptimum.Giventhis assumption, we will proveit is true for ョ Q N Supposethereis anotherencodingthathasa smallerexpectedlengthof codefor someprobabilitydistributionfor n + 1symbols.

For anyencoding, we canmapthe codesto a binarytreeby creatingthenull stringto root andaddinga left edgefor each0 anda right edgefor each1. We canmakeseveralobservationsaboutthisbinarytree:

- Eachsymbolmust map to a leaf node; otherwise, our prefix as-ウ オ ュ ー エ ゥ ッ キ ゥ ャ ャ beviolated.

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution3.18: If thereis somepoint on thecircle that ゥ ウ ッ エ 」 ッ エ 。 ゥ ョ ・ 、 inatleastoneof then arcs, thentheproblemis identicaltoProblem3.17.So,

Using a balancedBST, we can implementthe searchfor minimum,insertion, anddeletionin O(1ogn) time, ケゥ・ャ、ゥ 。ョoHョャッァ 。ャァッイゥエィュN

Solution3.17: A coveringsetS mustcontainat least P ・ point x suchthat ク 「 ュ ゥ ョ = min{b i }. Any suchpoint coversthe subsetof intervals{ ゥ 「 ゥ } 「 ュ Of course, bmin itself coversall suchintervalsandsothereexistsa minimum cardinalitycoveringthat contains 「 ュ 。 ョ 、 nootherpointsto its left. Consequently, thefollowing procedurecomputesaminimumcoveringsetS:

Solution3.16: This canbe エ ゥ 。 ャ 、 ッ ョ ・ in o H エ ゥ ュ ・ if we do a linearscanfor theboxesfor eachnewobjectto find thefirst boxwhereit wouldエ

In orderto speedthingsup, we canmaintaina list of boxeswhereacertaincapacityis availablefor the first time. For eachbox, we keeparecordwhich containsthe remainingbox capacityandthebox number.We will maintaina sortedlist of boxes, first by box capacity, thenbybox number. Whenwe receivea new item, we look for the first recordwith capacitygreaterthan or equal to the item's weight. We put it inthe correspondingbox, updateits capacity, andreinsertit at the correctposition. In orderto maintaina sortedlist, we canuseabalancedbinarytreesuchthatfind, delete, andinsertareall 0 Hャ ッァ ッー・イ。エゥッョウN

1. Sorteachoperationby theprobabilityof its occurrence.2. Iterateover all possiblebinary tree structureswith ャ ・ 。 カ ・ ウ N Map

eachleft edgein thebinarytreeasa scandown ッ ー ・ イ 。 エ ゥ ッ 。 ョ 、 eachright edgeasa clock to opensub-menuッ ー ・ イ 。 エ ゥ ッ

a) For eachleaf, computethe time it takes to reachthe node(numberof left edges+ c times the numberof right edgesinthepathto thenode).

b) Sortthenodesin theorderof time it takesto reachit.c) Map the actionsto the nodessuchthat the highestprobabil-

ity action is mappedto the lowestvisit time. Computetheexpectedvisit time to thenodes.

3. Find the treestructurethathasthe lowestexpectedエ ャ ・ to visit.Thenumberof オ ゥアオ・ binarytreeswith ャ ・ 。 カ ・ ウ is roughly0 HRN

141

i in I

1= {1, 2 ,.", n};s = {};while (I != {}) {

bmin = min{b [ i] Is = s + {bmin};I = i サ i I a [ i] <= bmin

エゥア

ja

Sイo

Solution3.15:Thisproblemis very similar hstructureto theHuffmmcodingproblemabove if CZ1·If we representeachclick on the sub-

イ ャ Q Q ョ ョ Q B Z Z ャ エ ゥ P ョ Q ェ ョ 、 ・ 。 」 ィ s 」 。 ョ 、 ッ キ ョ o ー ・ イ 。 エ ゥ ッ ョ 。 ウ 。 P thenエ ィ ・ ー 。 エ h1

ュ・ョe [ュュオ [lQcュ 「 ・ イ・ーイ・ウA_[Z l ッイ・ 。 」ィ・。」ィュ・ョ ・ョャ ュ Q Qsエ エイゥョァウオ」ィQm エィイ Q エ o c oュュ・オ p wゥ エ ィヲ ィ・ ・ 「ゥエMウ エ イゥ ァ ウヲ o イ eachactionQ 。 ョ 、 _ 「 オ ゥ ldd

エイ ョ・ョオ systembasedP エ ィ ・ ウ ・ ウ エ ァ ウ wewouldachievetheminimexpectedtime to ゥ イ イ 。 ゥ エ ィ themenu.

WheRC > litIS SIII11lar-to the casewherethereis m asymmetnccostfor a 0 。ョ、 ャゥョ 」ッ、・ (formmPIeritmquiresmorepowertoエ ュ i ュ ゥ エ 。 ャ P I N t ィ ・ イ ・ m ョ ッ | G キ ョ ー ッ ュ ゥ 。 ャ ・ ウ ッ ャ オ エ ゥ ッ i Q ヲ ッ イ

c b ・ ャ Q ィ o j イ [ 、 ・ ウ 」 。ュ i ョ 。 ャ ァ ッ ッ イ ゥ エ エエ ャ ュ ・ :

Oゥ _W エt[ZQNszezfHヲNH_llエ QWURu LotherwordsrHuffmaRCodingisoptimumforTL+1symbols

t ィ ・ イ ・ i ュ ッ エ 「 ・ aI1OIIleafnodethathaslessthantwochildren(othm[[キゥウ・ ・」。ョ、 ・ thenodeandbringits child P i ョ ・ ・ ャ オ ー 。 ョ Q

ィ ・ c ・ イ ・ 、 オ 」 ・ theexpectedcodei ・ エ ィ I N-If we sort thebMary treeleavesh orderof tkeir pathleI18hsrthe

hvo longestpathsmusthavethe sameャ ・ ァ エ ィ (sincethe parentoftheleafwith thelOI1gestpathmusthaveaRother-child)·

-Thetwo IIodeswith thelOI1gestpathsh thetreemustbeassigledto theWO Wmbolswith thesmallestprobability(otherwiserwecm

[ コ エ コ コ エ エ [ R R Q Z [ R R S Z Q Z 、 replace

コ Z エ j z z z l [ S S Q z z [ R Q R Z R W Z [ Q Z [ R R コ コ エirmzusthavetheウ 。 ュ ・ ・ 」 エ ・ 、 codeQ エ ィ 。 ウ エ イ ・ ・ キ ィゥ コ ・ エ ・ エ ィ ・ エ キ ッ ャ ッ キ ・ ウ エ ー イ ッ 「 。 「 ゥ ャ ゥ エ ケ i ・ ウ H ッ エ ィ ・ イ キ ゥ キ ・ canオ ウ ・

optimmtmmdreplaceitwiththe0147mLRTOW OI1siderthat the symbolshaveprobabuMSp Q U p R U

Z [ ヲ ォ [ [ e z ヲ ヲ ェ ケ イ イ コ ゥ コ コ [ j zfor HuffmaIICodhg.Sorwecmeasilyseethat

m e l M a l g o r i t h m s

h H p ャ G p Q I h H ー ャ G B p M Q p ョ p ョ Q I K p p ョ Q ᄋ

H p i p Q I = O(pl ,' . . pョMャ pョ pョKャI pョ pョKャᄋ

Thewaywe constructh オ ヲ ャ 。 ョ codeswe know that

CHAPTER3.140

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution3.18: If thereis somepoint on thecircle that ゥ ウ ッ エ 」 ッ エ 。 ゥ ョ ・ 、 inatleastoneof then arcs, thentheproblemis identicaltoProblem3.17.So,

Using a balancedBST, we can implementthe searchfor minimum,insertion, anddeletionin O(1ogn) time, ケゥ・ャ、ゥ 。ョoHョャッァ 。ャァッイゥエィュN

Solution3.17: A coveringsetS mustcontainat least P ・ point x suchthat ク 「 ュ ゥ ョ = min{b i }. Any suchpoint coversthe subsetof intervals{ ゥ 「 ゥ } 「 ュ Of course, bmin itself coversall suchintervalsandsothereexistsa minimum cardinalitycoveringthat contains 「 ュ 。 ョ 、 nootherpointsto its left. Consequently, thefollowing procedurecomputesaminimumcoveringsetS:

Solution3.16: This canbe エ ゥ 。 ャ 、 ッ ョ ・ in o H エ ゥ ュ ・ if we do a linearscanfor theboxesfor eachnewobjectto find thefirst boxwhereit wouldエ

In orderto speedthingsup, we canmaintaina list of boxeswhereacertaincapacityis availablefor the first time. For eachbox, we keeparecordwhich containsthe remainingbox capacityandthebox number.We will maintaina sortedlist of boxes, first by box capacity, thenbybox number. Whenwe receivea new item, we look for the first recordwith capacitygreaterthan or equal to the item's weight. We put it inthe correspondingbox, updateits capacity, andreinsertit at the correctposition. In orderto maintaina sortedlist, we canuseabalancedbinarytreesuchthatfind, delete, andinsertareall 0 Hャ ッァ ッー・イ。エゥッョウN

1. Sorteachoperationby theprobabilityof its occurrence.2. Iterateover all possiblebinary tree structureswith ャ ・ 。 カ ・ ウ N Map

eachleft edgein thebinarytreeasa scandown ッ ー ・ イ 。 エ ゥ ッ 。 ョ 、 eachright edgeasa clock to opensub-menuッ ー ・ イ 。 エ ゥ ッ

a) For eachleaf, computethe time it takes to reachthe node(numberof left edges+ c times the numberof right edgesinthepathto thenode).

b) Sortthenodesin theorderof time it takesto reachit.c) Map the actionsto the nodessuchthat the highestprobabil-

ity action is mappedto the lowestvisit time. Computetheexpectedvisit time to thenodes.

3. Find the treestructurethathasthe lowestexpectedエ ャ ・ to visit.Thenumberof オ ゥアオ・ binarytreeswith ャ ・ 。 カ ・ ウ is roughly0 HRN

141

i in I

1= {1, 2 ,.", n};s = {};while (I != {}) {

bmin = min{b [ i] Is = s + {bmin};I = i サ i I a [ i] <= bmin

エゥア

ja

Sイo

Solution3.15:Thisproblemis very similar hstructureto theHuffmmcodingproblemabove if CZ1·If we representeachclick on the sub-

イ ャ Q Q ョ ョ Q B Z Z ャ エ ゥ P ョ Q ェ ョ 、 ・ 。 」 ィ s 」 。 ョ 、 ッ キ ョ o ー ・ イ 。 エ ゥ ッ ョ 。 ウ 。 P thenエ ィ ・ ー 。 エ h1

ュ・ョe [ュュオ [lQcュ 「 ・ イ・ーイ・ウA_[Z l ッイ・ 。 」ィ・。」ィュ・ョ ・ョャ ュ Q Qsエ エイゥョァウオ」ィQm エィイ Q エ o c oュュ・オ p wゥ エ ィヲ ィ・ ・ 「ゥエMウ エ イゥ ァ ウヲ o イ eachactionQ 。 ョ 、 _ 「 オ ゥ ldd

エイ ョ・ョオ systembasedP エ ィ ・ ウ ・ ウ エ ァ ウ wewouldachievetheminimexpectedtime to ゥ イ イ 。 ゥ エ ィ themenu.

WheRC > litIS SIII11lar-to the casewherethereis m asymmetnccostfor a 0 。ョ、 ャゥョ 」ッ、・ (formmPIeritmquiresmorepowertoエ ュ i ュ ゥ エ 。 ャ P I N t ィ ・ イ ・ m ョ ッ | G キ ョ ー ッ ュ ゥ 。 ャ ・ ウ ッ ャ オ エ ゥ ッ i Q ヲ ッ イ

c b ・ ャ Q ィ o j イ [ 、 ・ ウ 」 。ュ i ョ 。 ャ ァ ッ ッ イ ゥ エ エエ ャ ュ ・ :

Oゥ _W エt[ZQNszezfHヲNH_llエ QWURu LotherwordsrHuffmaRCodingisoptimumforTL+1symbols

t ィ ・ イ ・ i ュ ッ エ 「 ・ aI1OIIleafnodethathaslessthantwochildren(othm[[キゥウ・ ・」。ョ、 ・ thenodeandbringits child P i ョ ・ ・ ャ オ ー 。 ョ Q

ィ ・ c ・ イ ・ 、 オ 」 ・ theexpectedcodei ・ エ ィ I N-If we sort thebMary treeleavesh orderof tkeir pathleI18hsrthe

hvo longestpathsmusthavethe sameャ ・ ァ エ ィ (sincethe parentoftheleafwith thelOI1gestpathmusthaveaRother-child)·

-Thetwo IIodeswith thelOI1gestpathsh thetreemustbeassigledto theWO Wmbolswith thesmallestprobability(otherwiserwecm

[ コ エ コ コ エ エ [ R R Q Z [ R R S Z Q Z 、 replace

コ Z エ j z z z l [ S S Q z z [ R Q R Z R W Z [ Q Z [ R R コ コ エirmzusthavetheウ 。 ュ ・ ・ 」 エ ・ 、 codeQ エ ィ 。 ウ エ イ ・ ・ キ ィゥ コ ・ エ ・ エ ィ ・ エ キ ッ ャ ッ キ ・ ウ エ ー イ ッ 「 。 「 ゥ ャ ゥ エ ケ i ・ ウ H ッ エ ィ ・ イ キ ゥ キ ・ canオ ウ ・

optimmtmmdreplaceitwiththe0147mLRTOW OI1siderthat the symbolshaveprobabuMSp Q U p R U

Z [ ヲ ォ [ [ e z ヲ ヲ ェ ケ イ イ コ ゥ コ コ [ j zfor HuffmaIICodhg.Sorwecmeasilyseethat

m e l M a l g o r i t h m s

h H p ャ G p Q I h H ー ャ G B p M Q p ョ p ョ Q I K p p ョ Q ᄋ

H p i p Q I = O(pl ,' . . pョMャ pョ pョKャI pョ pョKャᄋ

Thewaywe constructh オ ヲ ャ 。 ョ codeswe know that

CHAPTER3.140

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

supposethis is not so. Without loss of generality, we may assumethata minimum cardinalitycoveringsetS contains ャ ケ right endpointsofarcs, i.e., "clockwise" right endpoints.Therearen suchendpoints.If wechoosea givenright endpointandeliminateall thearcsthatarecoveredby itt the remainingproblemis identical to that in Problem3.17. Thismeanswe cansolvethearc-coveringproblemby n calls to thealgorithmin Solution3.17/yielding an o H R log n) algorithm.

Solution 3.19: Supposeour algorithm computesthe clustering0 ={Ob o R • ,Ok}. Suppose0 is not optimum, i.e., thereis anotherclus-tering P = {P1,P2,. . . ,Pk} which lowersthe separation.Since0 andParedistinct, theremustbe somepair of ッ 「 ェ ・ 」 エ ウ 「 that are assignedtothesameclusterin 0 butdifferentclustersin P-otherwise, either0 andP wouldbe identicalor some キ ッ オ ャ 、 be empty, which wasexplicitlydisallowed.

Let u,v be the lastpair of objects エ ィ キ ・ mergedin our algorithm.Supposex ,y were the next pair our algorithm would have mergedifwe hadperformedonemoreiteration, i.e., computeda ォ QM」ャオウエ・イゥョァN

Observethat d(x ,y) is the separationof 0 since x and yarea pair ofclosestッ 「 ェ ・ ッ エ in thesamecluster.

Now, our algorithmhasー オ エ 。 ョ 、 b in thesamecluster, thereis somesetof pairsof the form サ H X Q I (81,82),.. . ,(8l- 1,8l) , (8l' b)} that our al-gorithmselected.(It maybe that the setis simply サ H 「 I ス if we 、 ゥ イ ・ 」 エ

selected、 H 「 I N I Since 。 ョ 、 barein differentclustersin P/ oneof thesepairs, call ゥ エ ュ オ ウ エ bein distinct Pi 。ョ、 N Thereforethe ウ ・ ー 。 イ 。 エ ゥ ッ ッ ヲ

P is at most 、 H O which is no morethand( オ nッキ、H カI is no morethand(x ,y)/ which is the separationof O. Thereforethe ウ ・ ー 。 イ 。 エ ゥ ッ ッ ヲ Pis no morethanthatof 0/ contradictingthechoiceof P. Therefore0 hasエ ィ ・ ュ 。 ゥ ュ オ ュ ウ ・ ー 。 イ 。 エ ゥ ッ ッ ヲ all k-elusterings.

Solution3.20: We computetheoptimuminvitationlist by iterativelyre-movingpeoplewhocannotmeetLeona/sconstraintsuntil thereis nooneleft to イ ・ ュ ッ カ ・ M エ ィ ・ remainingsetis theuniquemaximumsetof peoplethatLeonacan ゥ ョ イ ゥ エ ・ N

Specifically, we iteratively removeanyonewho hasfewer than sixfriends in thecurrentsetor anyonewho hasfewer thansix peopletheydonotknowin thecurrentset. Theprocessmustconvergesincewe startwith a finite numberof peopleandremoveat leastonepersonin eachゥ エ ・ イ 。 エ ゥ ッ t ィ ・ remainingsetsatisfiesLeona'sconstraintsby construction.

It remainsto showthat the remainingset is maximum. In fact, weshowsome

142 CHAPTER3. mel algorithms

143

in which thepeoplewereremoved.The first pe Pl femovedwas t イ・ョ i ュッ

fewer エ ィ 。 ョ Q six ヲ イ ゥ G ャ ・ ョ Q 、 ウ in the ・ ョ Q エ ャ イ ・ setor the イ Q オ イ ョ Q 「 ・ イ of peopleP1

did

[ R [ Z コ Z u Z Z [ Z j [ エ エ エ ゥ Z コ [ [ Z R Z Z R Z [ _ _ A _ ・ Q P o ァ エ ッ 。 ョ ケ s ・ 。 tiョ、オ」エゥカ・ャケ ウオュ・ the ヲ ゥ イ ウ エ ゥ Q ー ・ イ ウ ッ ョ ウ

to 。 イ Q ケ setthat ウ 。 エ ゥ ウ ヲ ゥ ・ ウ the 」 ッ イ Q ウ エ イ 。 ゥ ョ エ ウ

ConsiderQ O the i-th perso e. It mustbe thateitherfewer

U R U R Q Q S S ゥ f f f Z U T Q R R Ze 't-th ー ・ イ ウ ッ イ ・ ュ ッ カ ・ 、 cannotbelongto a

maximumsetandinductiongoesthrough.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

supposethis is not so. Without loss of generality, we may assumethata minimum cardinalitycoveringsetS contains ャ ケ right endpointsofarcs, i.e., "clockwise" right endpoints.Therearen suchendpoints.If wechoosea givenright endpointandeliminateall thearcsthatarecoveredby itt the remainingproblemis identical to that in Problem3.17. Thismeanswe cansolvethearc-coveringproblemby n calls to thealgorithmin Solution3.17/yielding an o H R log n) algorithm.

Solution 3.19: Supposeour algorithm computesthe clustering0 ={Ob o R • ,Ok}. Suppose0 is not optimum, i.e., thereis anotherclus-tering P = {P1,P2,. . . ,Pk} which lowersthe separation.Since0 andParedistinct, theremustbe somepair of ッ 「 ェ ・ 」 エ ウ 「 that are assignedtothesameclusterin 0 butdifferentclustersin P-otherwise, either0 andP wouldbe identicalor some キ ッ オ ャ 、 be empty, which wasexplicitlydisallowed.

Let u,v be the lastpair of objects エ ィ キ ・ mergedin our algorithm.Supposex ,y were the next pair our algorithm would have mergedifwe hadperformedonemoreiteration, i.e., computeda ォ QM」ャオウエ・イゥョァN

Observethat d(x ,y) is the separationof 0 since x and yarea pair ofclosestッ 「 ェ ・ ッ エ in thesamecluster.

Now, our algorithmhasー オ エ 。 ョ 、 b in thesamecluster, thereis somesetof pairsof the form サ H X Q I (81,82),.. . ,(8l- 1,8l) , (8l' b)} that our al-gorithmselected.(It maybe that the setis simply サ H 「 I ス if we 、 ゥ イ ・ 」 エ

selected、 H 「 I N I Since 。 ョ 、 barein differentclustersin P/ oneof thesepairs, call ゥ エ ュ オ ウ エ bein distinct Pi 。ョ、 N Thereforethe ウ ・ ー 。 イ 。 エ ゥ ッ ッ ヲ

P is at most 、 H O which is no morethand( オ nッキ、H カI is no morethand(x ,y)/ which is the separationof O. Thereforethe ウ ・ ー 。 イ 。 エ ゥ ッ ッ ヲ Pis no morethanthatof 0/ contradictingthechoiceof P. Therefore0 hasエ ィ ・ ュ 。 ゥ ュ オ ュ ウ ・ ー 。 イ 。 エ ゥ ッ ッ ヲ all k-elusterings.

Solution3.20: We computetheoptimuminvitationlist by iterativelyre-movingpeoplewhocannotmeetLeona/sconstraintsuntil thereis nooneleft to イ ・ ュ ッ カ ・ M エ ィ ・ remainingsetis theuniquemaximumsetof peoplethatLeonacan ゥ ョ イ ゥ エ ・ N

Specifically, we iteratively removeanyonewho hasfewer than sixfriends in thecurrentsetor anyonewho hasfewer thansix peopletheydonotknowin thecurrentset. Theprocessmustconvergesincewe startwith a finite numberof peopleandremoveat leastonepersonin eachゥ エ ・ イ 。 エ ゥ ッ t ィ ・ remainingsetsatisfiesLeona'sconstraintsby construction.

It remainsto showthat the remainingset is maximum. In fact, weshowsome

142 CHAPTER3. mel algorithms

143

in which thepeoplewereremoved.The first pe Pl femovedwas t イ・ョ i ュッ

fewer エ ィ 。 ョ Q six ヲ イ ゥ G ャ ・ ョ Q 、 ウ in the ・ ョ Q エ ャ イ ・ setor the イ Q オ イ ョ Q 「 ・ イ of peopleP1

did

[ R [ Z コ Z u Z Z [ Z j [ エ エ エ ゥ Z コ [ [ Z R Z Z R Z [ _ _ A _ ・ Q P o ァ エ ッ 。 ョ ケ s ・ 。 tiョ、オ」エゥカ・ャケ ウオュ・ the ヲ ゥ イ ウ エ ゥ Q ー ・ イ ウ ッ ョ ウ

to 。 イ Q ケ setthat ウ 。 エ ゥ ウ ヲ ゥ ・ ウ the 」 ッ イ Q ウ エ イ 。 ゥ ョ エ ウ

ConsiderQ O the i-th perso e. It mustbe thateitherfewer

U R U R Q Q S S ゥ f f f Z U T Q R R Ze 't-th ー ・ イ ウ ッ イ ・ ュ ッ カ ・ 、 cannotbelongto a

maximumsetandinductiongoesthrough.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter4

Algorithm.s on Graphs

Solution4.1: Model themazeasanundirectedgraph.Eachvertexcor-respondsto a white pixel. We キゥャャ ャ、・ク the verticesbasedon the co-ordinatesof the correspondingpixel; so, カ・イエ・ク ェ correspondsto thematrix entry (i , j). Edgesmodel 。 。 」 ・ エ ー ゥ ク ・ ャ ウ [ ェ is connectedtoverticesVi+1 ,j , ェ KQ Vi-1 ,j , andVi ,j-1 , assumingtheseverticesexist-vertex Va,b 、ッ・ウ ッエ exist if the correspondingpixel is black or the co-ordinatesH _ ャ ゥ ・ outsidetheimage.

Now, run a DFS startingfrom the vertex correspondingto the en-trance. If at some ー ッ ャ エ we discoverthe exit vertex in the DFS, thenthereexistsa pathfrom theentranceto theexit. If we implementrecur-siveDFS thenthe pathwould consistof all the verticesin the call stackcorrespondingto previousrecursivecallsto theDFSroutine.

ャ ゥ ウ problemcanalsobesolvedusingBFSfrom the entrancevertexon the samegraphmodel. The BFS treehasthe propertythat the com-putedpathwill be a shortestpathfrom the entrance.HoweverBFS ismoredifficult to ゥ ュ ー ャ ・ ュ ・ エ ィ 。 ョ DFSsincein DFS, the compilerimplic-itly handlestheDFSstack, whereasin BFS, thequeuehasto beexplicitlycodedup. Sincetheproblemdid notcall for ashortestpath, it is bettertouseDFS.

Solution4.2: If you traversethebinary treein BFS order, thenyou areguaranteedto hit all thenodesat thesamedepthconsecutively.So, you」 。 「 オ ゥ ャ 、 the linked list of all the nodesas you discoverthemin BFSorder. While traversingthe tree, we alsoneedto know whenwe movefrom nodesof depthk to nodesof depthォ Q N This canbeeasilyachievedby keepingtrackof thedepthwheninsertingnodesin thequeue.

Solution 4.3: First, we considerthe problem of checkingif G is 2::1-

connected.If G' = (V, e サH u,v)}) is connected, it mustbe that a path

145

existsuetweenU mdu.Thisis possibleiff Umdulie P 。 cycle in G.ThusG is R ZZQ M」ッョ ・」エ・、 iff thereexistsa 」ケ」ャ・ ャgN

A We 」ュ」ィ・」 ヲッイ the existenceof a cycle in G by runningDFS g

fzees[RRRゥz [Z エエエ RM -………·

The complexityof DFS is (|VH|El);howeverh thecasedescribedaboverthe algorithmrmsh OOV|)time.Thisis becausem undimetedgmppwithmcyclesmhaveatmostiV|-1edges

ッ キ ・ considerthe problem of 」 ィ ・ 」 ォ ゥ ゥ ヲ G is 2'11-connectedClearly}G MotF-cmmetedゥ ヲ ヲ エ ィ ・ イ ・ ウ エ ウ an edgee suchthat G' =Hv e サ・スI is 、 P ョ・」エ・、N The latter conditionholds iff thereis nocycle includingedgee.

We canfind an edge H I that is not on a cycle with DFS. Withoutloss ofge?eralit y>assumeu is discovered fir st-Observeエ ィ 。 エエ ィ ・イ・ ・エ V 、 1of H オ Wj エ ェ I 、 ゥ ウ s 」 ッ ョ ョ ・ 」 エ ウ G ゥ ヲ ヲ re areno back-edgesb m u OJSdzcendantsto u or u's ancestors.

Define ャ H I to be the ュ ゥ エ ャ オ ュ of the discoverytime 、 H I of 。 ョ 、、 I ヲ ッ イ ウ エ ィ 。 エ (t ,w) is a 「 M・、ァ・ ヲイッエ キィ・イ・ t is a descendantor v.

We claim l(v) < 、H エィ・イ・ is a back-edge「 ・ エ キ ・ ・ ョ ッ イ oneof v'sdesceMmtsto U or om of ds ancestoys-Ifャ H I < 、H I thenthereipathfrom UthroughOReof its descmdantsto m aIrestorof tYF i e-r tj lieomcycle i ヲ 「 I _ 、 H ゥ ウ no キ 。 ケ ヲ イ ッ イ 「 。 」 ォ エ ッ オ ェ ュ c ・removalof (u ,v) 、 ッョョ・」エウ u 。ョ、N

Now, we showhow to compute ャ H I efficiently: oncewe havepro-edall of v's children, エィ・ョ ュゥョ H、H ュゥiャコ、ゥャ、ッヲ U J(z)] This

」 ッ A ャ ー オ エ 。 エ ゥ ッ 、 ッ ・ ウ エ add to the asymptoticcomplexity of DF§S1tmmsta comtaI1t additional work per edger s ッ we can check RG Q Q ᆳッ イ Q i ャ ・ 」 エ ・ 、 エ ャ ・ ウ ウ in ャ ゥ ョ ャ ・ 。 イ M M

Solution4.4:Assumhgthepim aremumberedfyom Otop-L createmdirectedgraphCo カ・イエゥ」・ウ Va , . . . Qᄋ Add an edge「 ・ ・ ョ

エ ッ G ゥ ヲ phst md3areconnectedby awire.Assumefor simplicity, G is connected;if not, the 」 ッ ョ ャ ・ 」 エ ・ 、 」 ッ ュ ー ッ

nerltscmbeamlyzedhdepmdeIItly.Run b f s g ウ エ 。 イ エ ィ ァ キ ゥ ゥ エ ィ 。 N a ウ ウ ゥ ァ ョ 。 arbitrarily to lie P エィ・ left

half.AIlvertesatm oddd1stmcefrom 。 areassignedto theright half.WhenperformingBFSon m uz1directedgraphyall mwly discovered

edgeswill eitherbe from verticeswhich are at a distanced from mtoundiscoveredカ 」 ・ ウ (which キ ゥ ャ ャ ・ ョ be at a 、ゥウ 」・ 、 Qヲイ mUJorfromveyti?eswhichareat a 、ゥ ュ」・ dtoverticeswhicharealsoat:distanced.Fzstrassumewe I1everencOURterm edgefrom a distmcekvertexto a distarIcek vertex.IIIthiscasereachwire is from a distance

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter4

Algorithm.s on Graphs

Solution4.1: Model themazeasanundirectedgraph.Eachvertexcor-respondsto a white pixel. We キゥャャ ャ、・ク the verticesbasedon the co-ordinatesof the correspondingpixel; so, カ・イエ・ク ェ correspondsto thematrix entry (i , j). Edgesmodel 。 。 」 ・ エ ー ゥ ク ・ ャ ウ [ ェ is connectedtoverticesVi+1 ,j , ェ KQ Vi-1 ,j , andVi ,j-1 , assumingtheseverticesexist-vertex Va,b 、ッ・ウ ッエ exist if the correspondingpixel is black or the co-ordinatesH _ ャ ゥ ・ outsidetheimage.

Now, run a DFS startingfrom the vertex correspondingto the en-trance. If at some ー ッ ャ エ we discoverthe exit vertex in the DFS, thenthereexistsa pathfrom theentranceto theexit. If we implementrecur-siveDFS thenthe pathwould consistof all the verticesin the call stackcorrespondingto previousrecursivecallsto theDFSroutine.

ャ ゥ ウ problemcanalsobesolvedusingBFSfrom the entrancevertexon the samegraphmodel. The BFS treehasthe propertythat the com-putedpathwill be a shortestpathfrom the entrance.HoweverBFS ismoredifficult to ゥ ュ ー ャ ・ ュ ・ エ ィ 。 ョ DFSsincein DFS, the compilerimplic-itly handlestheDFSstack, whereasin BFS, thequeuehasto beexplicitlycodedup. Sincetheproblemdid notcall for ashortestpath, it is bettertouseDFS.

Solution4.2: If you traversethebinary treein BFS order, thenyou areguaranteedto hit all thenodesat thesamedepthconsecutively.So, you」 。 「 オ ゥ ャ 、 the linked list of all the nodesas you discoverthemin BFSorder. While traversingthe tree, we alsoneedto know whenwe movefrom nodesof depthk to nodesof depthォ Q N This canbeeasilyachievedby keepingtrackof thedepthwheninsertingnodesin thequeue.

Solution 4.3: First, we considerthe problem of checkingif G is 2::1-

connected.If G' = (V, e サH u,v)}) is connected, it mustbe that a path

145

existsuetweenU mdu.Thisis possibleiff Umdulie P 。 cycle in G.ThusG is R ZZQ M」ッョ ・」エ・、 iff thereexistsa 」ケ」ャ・ ャgN

A We 」ュ」ィ・」 ヲッイ the existenceof a cycle in G by runningDFS g

fzees[RRRゥz [Z エエエ RM -………·

The complexityof DFS is (|VH|El);howeverh thecasedescribedaboverthe algorithmrmsh OOV|)time.Thisis becausem undimetedgmppwithmcyclesmhaveatmostiV|-1edges

ッ キ ・ considerthe problem of 」 ィ ・ 」 ォ ゥ ゥ ヲ G is 2'11-connectedClearly}G MotF-cmmetedゥ ヲ ヲ エ ィ ・ イ ・ ウ エ ウ an edgee suchthat G' =Hv e サ・スI is 、 P ョ・」エ・、N The latter conditionholds iff thereis nocycle includingedgee.

We canfind an edge H I that is not on a cycle with DFS. Withoutloss ofge?eralit y>assumeu is discovered fir st-Observeエ ィ 。 エエ ィ ・イ・ ・エ V 、 1of H オ Wj エ ェ I 、 ゥ ウ s 」 ッ ョ ョ ・ 」 エ ウ G ゥ ヲ ヲ re areno back-edgesb m u OJSdzcendantsto u or u's ancestors.

Define ャ H I to be the ュ ゥ エ ャ オ ュ of the discoverytime 、 H I of 。 ョ 、、 I ヲ ッ イ ウ エ ィ 。 エ (t ,w) is a 「 M・、ァ・ ヲイッエ キィ・イ・ t is a descendantor v.

We claim l(v) < 、H エィ・イ・ is a back-edge「 ・ エ キ ・ ・ ョ ッ イ oneof v'sdesceMmtsto U or om of ds ancestoys-Ifャ H I < 、H I thenthereipathfrom UthroughOReof its descmdantsto m aIrestorof tYF i e-r tj lieomcycle i ヲ 「 I _ 、 H ゥ ウ no キ 。 ケ ヲ イ ッ イ 「 。 」 ォ エ ッ オ ェ ュ c ・removalof (u ,v) 、 ッョョ・」エウ u 。ョ、N

Now, we showhow to compute ャ H I efficiently: oncewe havepro-edall of v's children, エィ・ョ ュゥョ H、H ュゥiャコ、ゥャ、ッヲ U J(z)] This

」 ッ A ャ ー オ エ 。 エ ゥ ッ 、 ッ ・ ウ エ add to the asymptoticcomplexity of DF§S1tmmsta comtaI1t additional work per edger s ッ we can check RG Q Q ᆳッ イ Q i ャ ・ 」 エ ・ 、 エ ャ ・ ウ ウ in ャ ゥ ョ ャ ・ 。 イ M M

Solution4.4:Assumhgthepim aremumberedfyom Otop-L createmdirectedgraphCo カ・イエゥ」・ウ Va , . . . Qᄋ Add an edge「 ・ ・ ョ

エ ッ G ゥ ヲ phst md3areconnectedby awire.Assumefor simplicity, G is connected;if not, the 」 ッ ョ ャ ・ 」 エ ・ 、 」 ッ ュ ー ッ

nerltscmbeamlyzedhdepmdeIItly.Run b f s g ウ エ 。 イ エ ィ ァ キ ゥ ゥ エ ィ 。 N a ウ ウ ゥ ァ ョ 。 arbitrarily to lie P エィ・ left

half.AIlvertesatm oddd1stmcefrom 。 areassignedto theright half.WhenperformingBFSon m uz1directedgraphyall mwly discovered

edgeswill eitherbe from verticeswhich are at a distanced from mtoundiscoveredカ 」 ・ ウ (which キ ゥ ャ ャ ・ ョ be at a 、ゥウ 」・ 、 Qヲイ mUJorfromveyti?eswhichareat a 、ゥ ュ」・ dtoverticeswhicharealsoat:distanced.Fzstrassumewe I1everencOURterm edgefrom a distmcekvertexto a distarIcek vertex.IIIthiscasereachwire is from a distance

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution4.6: Let v be anyvertexin G. ConsideranEuler tour T of G.Eachtime the tour enters ュ オ ウ エ exit v by a differentedge. Further-more, eachedgemustbe enteredexactlyonceandexitedexactlyonce.Hencewe canput incoming edgesandoutgoingedgesin a 1-1 corre-spondence, sothein-degreeandout-degreeof v mustbeequal.

Conversely, let thein-degreeandout-degreeof everyvertexv in Gbeequal. ConstructanEuler tour asfollows: startwith anarbitraryvertex.UseDFSto explorefrom this vertexuntil a simplecycleis found. Suchacyclemustexist sincewecannevergettrappedin avertex-ifweenteredanewly discoveredvertex,wecanalwaysexit it becauseof theconstraintthatin-degreeequalsout-degree.

Continue 、 ッ Q Y this till all the edgeshavebeenpartitionedinto dis-joint simple cycles. Now, mergethesecyclesas follows: startwith anycycle. f ッ イ Q ケ vertexonthecurrentcycle, find a cyclethatit is in, whichis not the currentcycle, and adda detourto this new cycle. Iterativelyaddcyclesto thecurrentcycle.

We claim that all 、 ゥ ッ ゥ ョ エ cyclesmustbe mergedby this process.If

k vertexto a distanceォ Q vertex, so all wires arebetweenthe left andright halves.

If any edgeis from a distancek vertex to a distancek vertex, westop-thepins 」 。 ョ ッ エ bepartitionedinto left andrighthalvesasdesired.The reasonis as follows: let u and 「 ・ suchvertices. Considerthe firstcommonancestorゥ ョ the BFSsearchof u andv (suchanancestormustexist sincethe searchstartedat va). The pathsPa,u andPa,v in the BFStreefrom エ ッ u andv areof equallength;thereforethe cycle formedbygoingfrom to thenthroughthe edge H オ _ カ ェ

u カゥ。ー。 _ カ has 。 odd ャ ・ i Q Y エ ィ N Theverticesin anodd i ・ ァ エ ィ cycle cannotbepartitionedinto two setssuchthatall edgesarebetweenthesets.

Solution 4.5: It is naturalto model the network as a graph: verticescorrespondto individualsandanedgeexistsfrom A to B if B is acontactofA.

For 。 Q individual X , we cancomputethe setof X's 」 ッ エ 。 」 エ ウ by run-ning graphsearch(DFS or BFS) from x. Runninggraphsearchfor eachindividualleadsto a O(IVI· (IVI + lEI)) algorithmfor transitiveclosure.

Anotherapproachwhich hascomplexityO(IVI 3) but which maybe

moreefficient for densegraphsis to run anall-pairsshortestpathalgo-rithm with edgeweightsof 1. If thereis a pathfrom u to v, the short-estpathdistancefrom u to v will be finite; otherwise, it will be N Wecanfurther improve the shortestpathcalculationby simply イ ・ 」 ッ イ 、 ゥ ァ

whetherthereis apathfrom u to v or not; in thisway, weneedaBooleanmatrix ratherthananintegermatrix encodingthe distancesbetweenthevertices.

146 CHAPTER4. ALGORITHMSONGRAPHS147

oエf tkere mustbe aIIedge(PFq)masimplecycle S that iSROt h theycle C our processhasconv?rgedtor wherep appearsiI1C (suchm

edgeexistsbecausetke graphls comected)·Wecmmt

ウ エ エ コ エ コ コ オ コ コ エ エ ェ イ 、 ヲ ゥ Zlsohnearmtimeysothealgorithm ゥ ウ ィ ・ 。 f エ ィ ・ M

sol-tion43:ModeltheFSM asa ァ ィM・。」ィ states correspondsto adistinct vertexvs N tQ・ edgesetcOIUISts of preciselythoseedgeswhichomspoMto potentialtramitions「 ・ エ キ ・ ・ i Q ウ エ 。 エ ・ ウ [ ウ ー ・ 」 ゥ 」 。 ャ ャ ケ H カ ウ Ie ゥ t H ウ ゥ I = t. w・キ ・イ 。エ

j、y o[ ZZ R[ A[ [Z[c[R] c[イQ コ RRZ [Z[ZZイイ [Aイt_ ィ ゥ」ィ u QQ ゥウ ウ the ォ ッ ヲ ヲ エ ィ ゥ ウ DAG) ュ 。 ケ エ イ 。 ョt is in 。 ョ Q 、 ッ イ ョ 」 c ・ ゥ エ ゥ ウ ッ オ Q エ ゥ エ will i Q P エ return. Conversely, all stateswithin

the shksccscaI1retumto themselvesrso the statesh the shkSCCSe preciselytheRonephemeralstates;the」 ッ ュ ー ャ ・ ュ ・ エ of this setis the

desiredsetof ephemeralstates.

meSCCDAG of a graphcm be computedh linear-timekom tkegraphmodelandthegraphitself cmbecORStmetedhlhearmtimefromtheFSM, so thewholecomputationis linear.

Solution4.8:Wecm computethe diameterby rumhgBFSfrom eachvertexaM recordingthe largestshortestー ィ distance.This has0 H iv iᄋHiv ャ ャei II = oH iv Q R I c o p i・ ク

[Z[ZA R[Z ゥ_ZZ t イ c Z[ yjコ QR[ Z ZR[ _ Zコ ::;e s· ?esrooted 。 エ 」 ィ ゥ ャ 、 イ ・ ョ areT1 • W T

m. Let

、 Q 、 R ᄋ N N 、 ュ be ・ Q 。 i ュ ・

l ・ エ 「 ・ 。 ャ ッ ョ ァ ー 。 t N e ゥ エ ィ ・ イ 。 ウ ウ ・ ウ エ ィ イ Q イ o ャQゥ エ 、 ッ ・ ウ o エ passthrough Q エ ュ ャ u s エ beentirely キ ゥ エ ィ ゥ ョ ャ ッ ョ ・ of them ウオ「ィ NM S

r [[Z[ RZZZ[[ QA エ Z ZR ZU ZコZ ZZ[[ ] _Z[ [イ イ ヲコエu [ Z[u[l ゥTRR u_ ゥ 、、 NゥTゥ T ゥウオ「 エイ ・・ ウ エ ィ。 エ arefarth?stfrom T-The distmcefrom T to thevertexh Zthatisfarthestfrom itl?simplyA ]K 1. The Q P ・ ウ エ i ・ エ ィ ー 。 エ ィ ゥ ョ t

is [ コ Z エエ [ Z Z [ [ _ Z コ Z Z R コ s Z Z R Z Q ゥ ヲ [ Z u W ャ ゥ u ゥ T T j ヲ [ [ [ [ z o j R _ A t _ S _ Z ヲ イ _ t エ ヲ。 ョ ョ 、 エ ィ ・ Q 。 イ

ー ッ イ エ ゥ o i エ o the S1zeof thetreer i.e-rOOVO- omplexity is prom

Solution4·9:Assumethehputsto theI1etworkstabilizeat time 0.wearetryhgto bOUI1dwheRtheprh1aryoutputsstabilize.

Supposegateg hasa delayD(g).Itwill staUilizeatI10エ ゥ ュ ・ 。 ャ ャ its

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution4.6: Let v be anyvertexin G. ConsideranEuler tour T of G.Eachtime the tour enters ュ オ ウ エ exit v by a differentedge. Further-more, eachedgemustbe enteredexactlyonceandexitedexactlyonce.Hencewe canput incoming edgesandoutgoingedgesin a 1-1 corre-spondence, sothein-degreeandout-degreeof v mustbeequal.

Conversely, let thein-degreeandout-degreeof everyvertexv in Gbeequal. ConstructanEuler tour asfollows: startwith anarbitraryvertex.UseDFSto explorefrom this vertexuntil a simplecycleis found. Suchacyclemustexist sincewecannevergettrappedin avertex-ifweenteredanewly discoveredvertex,wecanalwaysexit it becauseof theconstraintthatin-degreeequalsout-degree.

Continue 、 ッ Q Y this till all the edgeshavebeenpartitionedinto dis-joint simple cycles. Now, mergethesecyclesas follows: startwith anycycle. f ッ イ Q ケ vertexonthecurrentcycle, find a cyclethatit is in, whichis not the currentcycle, and adda detourto this new cycle. Iterativelyaddcyclesto thecurrentcycle.

We claim that all 、 ゥ ッ ゥ ョ エ cyclesmustbe mergedby this process.If

k vertexto a distanceォ Q vertex, so all wires arebetweenthe left andright halves.

If any edgeis from a distancek vertex to a distancek vertex, westop-thepins 」 。 ョ ッ エ bepartitionedinto left andrighthalvesasdesired.The reasonis as follows: let u and 「 ・ suchvertices. Considerthe firstcommonancestorゥ ョ the BFSsearchof u andv (suchanancestormustexist sincethe searchstartedat va). The pathsPa,u andPa,v in the BFStreefrom エ ッ u andv areof equallength;thereforethe cycle formedbygoingfrom to thenthroughthe edge H オ _ カ ェ

u カゥ。ー。 _ カ has 。 odd ャ ・ i Q Y エ ィ N Theverticesin anodd i ・ ァ エ ィ cycle cannotbepartitionedinto two setssuchthatall edgesarebetweenthesets.

Solution 4.5: It is naturalto model the network as a graph: verticescorrespondto individualsandanedgeexistsfrom A to B if B is acontactofA.

For 。 Q individual X , we cancomputethe setof X's 」 ッ エ 。 」 エ ウ by run-ning graphsearch(DFS or BFS) from x. Runninggraphsearchfor eachindividualleadsto a O(IVI· (IVI + lEI)) algorithmfor transitiveclosure.

Anotherapproachwhich hascomplexityO(IVI 3) but which maybe

moreefficient for densegraphsis to run anall-pairsshortestpathalgo-rithm with edgeweightsof 1. If thereis a pathfrom u to v, the short-estpathdistancefrom u to v will be finite; otherwise, it will be N Wecanfurther improve the shortestpathcalculationby simply イ ・ 」 ッ イ 、 ゥ ァ

whetherthereis apathfrom u to v or not; in thisway, weneedaBooleanmatrix ratherthananintegermatrix encodingthe distancesbetweenthevertices.

146 CHAPTER4. ALGORITHMSONGRAPHS147

oエf tkere mustbe aIIedge(PFq)masimplecycle S that iSROt h theycle C our processhasconv?rgedtor wherep appearsiI1C (suchm

edgeexistsbecausetke graphls comected)·Wecmmt

ウ エ エ コ エ コ コ オ コ コ エ エ ェ イ 、 ヲ ゥ Zlsohnearmtimeysothealgorithm ゥ ウ ィ ・ 。 f エ ィ ・ M

sol-tion43:ModeltheFSM asa ァ ィM・。」ィ states correspondsto adistinct vertexvs N tQ・ edgesetcOIUISts of preciselythoseedgeswhichomspoMto potentialtramitions「 ・ エ キ ・ ・ i Q ウ エ 。 エ ・ ウ [ ウ ー ・ 」 ゥ 」 。 ャ ャ ケ H カ ウ Ie ゥ t H ウ ゥ I = t. w・キ ・イ 。エ

j、y o[ ZZ R[ A[ [Z[c[R] c[イQ コ RRZ [Z[ZZイイ [Aイt_ ィ ゥ」ィ u QQ ゥウ ウ the ォ ッ ヲ ヲ エ ィ ゥ ウ DAG) ュ 。 ケ エ イ 。 ョt is in 。 ョ Q 、 ッ イ ョ 」 c ・ ゥ エ ゥ ウ ッ オ Q エ ゥ エ will i Q P エ return. Conversely, all stateswithin

the shksccscaI1retumto themselvesrso the statesh the shkSCCSe preciselytheRonephemeralstates;the」 ッ ュ ー ャ ・ ュ ・ エ of this setis the

desiredsetof ephemeralstates.

meSCCDAG of a graphcm be computedh linear-timekom tkegraphmodelandthegraphitself cmbecORStmetedhlhearmtimefromtheFSM, so thewholecomputationis linear.

Solution4.8:Wecm computethe diameterby rumhgBFSfrom eachvertexaM recordingthe largestshortestー ィ distance.This has0 H iv iᄋHiv ャ ャei II = oH iv Q R I c o p i・ ク

[Z[ZA R[Z ゥ_ZZ t イ c Z[ yjコ QR[ Z ZR[ _ Zコ ::;e s· ?esrooted 。 エ 」 ィ ゥ ャ 、 イ ・ ョ areT1 • W T

m. Let

、 Q 、 R ᄋ N N 、 ュ be ・ Q 。 i ュ ・

l ・ エ 「 ・ 。 ャ ッ ョ ァ ー 。 t N e ゥ エ ィ ・ イ 。 ウ ウ ・ ウ エ ィ イ Q イ o ャQゥ エ 、 ッ ・ ウ o エ passthrough Q エ ュ ャ u s エ beentirely キ ゥ エ ィ ゥ ョ ャ ッ ョ ・ of them ウオ「ィ NM S

r [[Z[ RZZZ[[ QA エ Z ZR ZU ZコZ ZZ[[ ] _Z[ [イ イ ヲコエu [ Z[u[l ゥTRR u_ ゥ 、、 NゥTゥ T ゥウオ「 エイ ・・ ウ エ ィ。 エ arefarth?stfrom T-The distmcefrom T to thevertexh Zthatisfarthestfrom itl?simplyA ]K 1. The Q P ・ ウ エ i ・ エ ィ ー 。 エ ィ ゥ ョ t

is [ コ Z エエ [ Z Z [ [ _ Z コ Z Z R コ s Z Z R Z Q ゥ ヲ [ Z u W ャ ゥ u ゥ T T j ヲ [ [ [ [ z o j R _ A t _ S _ Z ヲ イ _ t エ ヲ。 ョ ョ 、 エ ィ ・ Q 。 イ

ー ッ イ エ ゥ o i エ o the S1zeof thetreer i.e-rOOVO- omplexity is prom

Solution4·9:Assumethehputsto theI1etworkstabilizeat time 0.wearetryhgto bOUI1dwheRtheprh1aryoutputsstabilize.

Supposegateg hasa delayD(g).Itwill staUilizeatI10エ ゥ ュ ・ 。 ャ ャ its

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

eachgatehasstabilizedby processinggatesin topologicalorder, startingfrom the ー イ ゥ ュ 。 イ ケ ャ ー オ エ ウ M ヲ ッ イ eachgate, we canboundwhenit stabilizessincewe havealreadyboundedwhenits inputshavestabilized. Topo-logicalorderingfor a graphcanbecomputedin 0 H ュI time, wherenandm arethenumberof verticesandedgesin thegraph.

The valuewe computeis an upperboundandmaynot be tight be-causeof logicalrelationshipsbetweensignals-forexample, if oneof theinputs to an AND gate is 0, then the output of the AND gatewill beindependentof thechangesat its otherinputs.

Solution4.10: Let A andB ben-dimensionrealvectors;write A < B ifA[i) < B[i) for eachi. The < relationis transitive.

Let (X l, X2,… ,X20) be the heights of the ー ャ 。 ケ ・ イ ウ in Team X 。ョ Q

H u 「y 2,· N N y R o I be the heights of the players in Team Y. The keyッ 「 ウ ・ イ イ 。 エ ゥ ッ ョ is that Team X can be placed in front of Team Y ゥ

50RT(Xl,… ,X20) < 50RT(Yl,… ,Y20).

Now, wedefineaDAG G with verticescorrespondingto theteamsasfollows: thereis anedgefrom vertexX to Y iff UP HxI < 50RT(Y).

Everysequenceof teamswherethesuccessiveteamscanbeplacedinfront of eachothercorrespondsto a pathin G. To find the longestsuchsequence, we simply needto find the longestpathin the DAG G. Wecando this, for example, by topologicallyorderingtheverticesin G; thelongestpathterminatingatvertexv is themaximumof thelongestpathsterminatingat v's faninsconcatenatedwith ゥ エ ウ ・ ャ ヲ N

Thetopologicalorderingcomputationis O(IVI + lE I) anddominatesthecomputationtime.

Solution4.11: Themostobviousapproachis to startwith anarbitrarytwo-coloring.If it is diverse, we aredone.

At this ー ッ ゥ ョ 。 naturalapproachwould be to look for a nondiversevertexv andflipping v'scolorbutthis canresultin someof v'sneighborsbecomingnondiverse.

To provethat this approachworks, we look at diverseedges-edgesbetweenverticesof differentcolors. We claim thata coloringthatmaxi-mizesthenumberof diverseedgesis alsodiverse.

If not, supposex is not diverse. Without lossof generality, supposex is white. Thenby changingx's color to black, the numberof diverseedgesstrictly increases(since x had more white neighborsthan blackneighbors).

Thereforea coloringwhich maximizesthe numberof diverseedgesyields a diversegraph.Sucha coloringmustexist: becausethe graphisfinite, thereare P ャ ケ afinite numberof colorings.We canconstructa col-oringby startingwith anarbitra

148 CHAPTER4. ALGORITHMSONGRAPHS149

Solution4.12: uウオ。ャャ ケd ゥ ェォウエ イ __ Gウウィッ ッイエ エ ・ウ エー。 エィ。ャァッ イゥエ ィュ ョ usesscalarカ 。 Q ᆳ -oイ・ 、ァ ・ャ エ ィNhッキ ・ Q エ」。ョ ャ・ 。 s 「 ・ュッ、ゥヲ ヲゥ ・、 エo 」。ウ・キ ィ ・イ・ e

edgeweightis avectorif addition 。ョ ャ、 comparisoncanbedefinedoverthevectors. In this 」 。 ウ ・ if the ・ 、 ァ ・ 」 ッ ウ エ ゥ s we saythe lengthof the ed eZ[ [v[[ jZ [イZ ・ Zイコイエ Oイサ [_ヲWjャ IN キゥ ・、 ・ヲゥョ・ 。、 、、 、 ゥエゥッi u ・ ェ オウ Q sォエ 」c o ッュ ュ ョーッ

i ョ」 c・ It we sumup the edge ャ ・ ョ ァ エ ィ ウ over a path, we・ ウ ウ ・ エ ゥ 。 ャ ァ ・ エ the total 」 ッ 。 ョ 、 エ ィ ・ オ ュ 「 ・ イ of edgesin the path. The

mpareflIIICtimcm bemstthe lexlcographic(firstby the total costrエィュ ケ thenumberof edges)·Withthisr we cm ylm DijkstraysshortestpatAalgorithmmdfiI1d theshortestpaththatrequirestheleastI1umber-of edges.

?OMtorminga bf sMエ ケー・」 ッュャーu Q エ。 エゥoi Q starting。 エ u.

cッ Q s ゥ、・イ thesetof カ ・ イ エ ゥ 」 ・ ウ Sk 1suchthatfor anyvertex s ォ M ャ ャ theQ o イ エ di

エ ィ 。 エ エ ィ ・ ウ ィ ッ イ エ ・ ウ エ 、 i s エ 。 ョ ャ c ・ 「 ・ エ キ ・ ・ i ャ オ 。 ョ 、 ゥ ウ k. If weknowthenumberof

zttiz;242UZZUdzttfor all カ ・ イ エ ゥ 」 ・ ウ s k M Q エ ィ 。 エ also havem edgetou.Thisis becaueachdistinctpathfrom u to 。 ャ ウ ッ givesusa distinctpathfrom u エッ「ケsimply 。 、 、 ゥ ョ ァ ・ edgeヲ イ ッ ュ エ ッ Y to thepath

BFSrunsin ャ ゥ ョ ・ 。 イ M ・ 。 。 ウ ウ オ ュ ゥ ョ ァ we storethenumberッ ヲ ッ イ エ ᆳes7 a!h from intem ed1ate vertic the 」 ッ ュ ー オ エ 。 エ ゥi ャ o 、 ・ Q ウ proportionalto the iョ オ ュ 「 ・ イ of its outgoingedges. Hencethecompletealgorithmrunsin linear-time

Solution414:TKis is m Np-completeproblemmd hmcetker JnnA

zRRSR QRQRSRlゥエコイエ Zイエapproximatetheprobabilitiesrthmthiscanbesolved、 ォ ゥ ・ ョ

It is mtIIral to solvethis problemushg、 ケ i ョ ゥ 」 programming-weiteratively compukthematrix lifthJ)which is the shortestpathdis-:rzzvert1cesS ュ 、 エ・ ク。 」エャ ケ k.mceis at leastp and themmberof edgesh the pathis

GivenM;(8 ,t) , we cancompute1'\11;+1(8,t) オウゥ エィ・ recurrence

M;+1(8 ,t) = ュゥ ーヲオRRPIHmksjIャゥ、jOーイHオェIIᄋ

merearem infinite mmberof valuesfor pmyrealmmberh [071i-In reality, there are P ャ ケ a ヲゥョ Q ゥ エ・ i ャオイ

Z、 ・ イ エ ィッウ ・ p イ o 「。 「 ゥA ャ ゥエゥ・ ウN hッキ・ ィ・ nun・ ク ー ッ ・ ョ エ ゥ 。 Q 。 イ ャ 、 eachー。 エ ィ canhavea 、 ゥウ エゥョ ャc エ probability, so it is

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

eachgatehasstabilizedby processinggatesin topologicalorder, startingfrom the ー イ ゥ ュ 。 イ ケ ャ ー オ エ ウ M ヲ ッ イ eachgate, we canboundwhenit stabilizessincewe havealreadyboundedwhenits inputshavestabilized. Topo-logicalorderingfor a graphcanbecomputedin 0 H ュI time, wherenandm arethenumberof verticesandedgesin thegraph.

The valuewe computeis an upperboundandmaynot be tight be-causeof logicalrelationshipsbetweensignals-forexample, if oneof theinputs to an AND gate is 0, then the output of the AND gatewill beindependentof thechangesat its otherinputs.

Solution4.10: Let A andB ben-dimensionrealvectors;write A < B ifA[i) < B[i) for eachi. The < relationis transitive.

Let (X l, X2,… ,X20) be the heights of the ー ャ 。 ケ ・ イ ウ in Team X 。ョ Q

H u 「y 2,· N N y R o I be the heights of the players in Team Y. The keyッ 「 ウ ・ イ イ 。 エ ゥ ッ ョ is that Team X can be placed in front of Team Y ゥ

50RT(Xl,… ,X20) < 50RT(Yl,… ,Y20).

Now, wedefineaDAG G with verticescorrespondingto theteamsasfollows: thereis anedgefrom vertexX to Y iff UP HxI < 50RT(Y).

Everysequenceof teamswherethesuccessiveteamscanbeplacedinfront of eachothercorrespondsto a pathin G. To find the longestsuchsequence, we simply needto find the longestpathin the DAG G. Wecando this, for example, by topologicallyorderingtheverticesin G; thelongestpathterminatingatvertexv is themaximumof thelongestpathsterminatingat v's faninsconcatenatedwith ゥ エ ウ ・ ャ ヲ N

Thetopologicalorderingcomputationis O(IVI + lE I) anddominatesthecomputationtime.

Solution4.11: Themostobviousapproachis to startwith anarbitrarytwo-coloring.If it is diverse, we aredone.

At this ー ッ ゥ ョ 。 naturalapproachwould be to look for a nondiversevertexv andflipping v'scolorbutthis canresultin someof v'sneighborsbecomingnondiverse.

To provethat this approachworks, we look at diverseedges-edgesbetweenverticesof differentcolors. We claim thata coloringthatmaxi-mizesthenumberof diverseedgesis alsodiverse.

If not, supposex is not diverse. Without lossof generality, supposex is white. Thenby changingx's color to black, the numberof diverseedgesstrictly increases(since x had more white neighborsthan blackneighbors).

Thereforea coloringwhich maximizesthe numberof diverseedgesyields a diversegraph.Sucha coloringmustexist: becausethe graphisfinite, thereare P ャ ケ afinite numberof colorings.We canconstructa col-oringby startingwith anarbitra

148 CHAPTER4. ALGORITHMSONGRAPHS149

Solution4.12: uウオ。ャャ ケd ゥ ェォウエ イ __ Gウウィッ ッイエ エ ・ウ エー。 エィ。ャァッ イゥエ ィュ ョ usesscalarカ 。 Q ᆳ -oイ・ 、ァ ・ャ エ ィNhッキ ・ Q エ」。ョ ャ・ 。 s 「 ・ュッ、ゥヲ ヲゥ ・、 エo 」。ウ・キ ィ ・イ・ e

edgeweightis avectorif addition 。ョ ャ、 comparisoncanbedefinedoverthevectors. In this 」 。 ウ ・ if the ・ 、 ァ ・ 」 ッ ウ エ ゥ s we saythe lengthof the ed eZ[ [v[[ jZ [イZ ・ Zイコイエ Oイサ [_ヲWjャ IN キゥ ・、 ・ヲゥョ・ 。、 、、 、 ゥエゥッi u ・ ェ オウ Q sォエ 」c o ッュ ュ ョーッ

i ョ」 c・ It we sumup the edge ャ ・ ョ ァ エ ィ ウ over a path, we・ ウ ウ ・ エ ゥ 。 ャ ァ ・ エ the total 」 ッ 。 ョ 、 エ ィ ・ オ ュ 「 ・ イ of edgesin the path. The

mpareflIIICtimcm bemstthe lexlcographic(firstby the total costrエィュ ケ thenumberof edges)·Withthisr we cm ylm DijkstraysshortestpatAalgorithmmdfiI1d theshortestpaththatrequirestheleastI1umber-of edges.

?OMtorminga bf sMエ ケー・」 ッュャーu Q エ。 エゥoi Q starting。 エ u.

cッ Q s ゥ、・イ thesetof カ ・ イ エ ゥ 」 ・ ウ Sk 1suchthatfor anyvertex s ォ M ャ ャ theQ o イ エ di

エ ィ 。 エ エ ィ ・ ウ ィ ッ イ エ ・ ウ エ 、 i s エ 。 ョ ャ c ・ 「 ・ エ キ ・ ・ i ャ オ 。 ョ 、 ゥ ウ k. If weknowthenumberof

zttiz;242UZZUdzttfor all カ ・ イ エ ゥ 」 ・ ウ s k M Q エ ィ 。 エ also havem edgetou.Thisis becaueachdistinctpathfrom u to 。 ャ ウ ッ givesusa distinctpathfrom u エッ「ケsimply 。 、 、 ゥ ョ ァ ・ edgeヲ イ ッ ュ エ ッ Y to thepath

BFSrunsin ャ ゥ ョ ・ 。 イ M ・ 。 。 ウ ウ オ ュ ゥ ョ ァ we storethenumberッ ヲ ッ イ エ ᆳes7 a!h from intem ed1ate vertic the 」 ッ ュ ー オ エ 。 エ ゥi ャ o 、 ・ Q ウ proportionalto the iョ オ ュ 「 ・ イ of its outgoingedges. Hencethecompletealgorithmrunsin linear-time

Solution414:TKis is m Np-completeproblemmd hmcetker JnnA

zRRSR QRQRSRlゥエコイエ Zイエapproximatetheprobabilitiesrthmthiscanbesolved、 ォ ゥ ・ ョ

It is mtIIral to solvethis problemushg、 ケ i ョ ゥ 」 programming-weiteratively compukthematrix lifthJ)which is the shortestpathdis-:rzzvert1cesS ュ 、 エ・ ク。 」エャ ケ k.mceis at leastp and themmberof edgesh the pathis

GivenM;(8 ,t) , we cancompute1'\11;+1(8,t) オウゥ エィ・ recurrence

M;+1(8 ,t) = ュゥ ーヲオRRPIHmksjIャゥ、jOーイHオェIIᄋ

merearem infinite mmberof valuesfor pmyrealmmberh [071i-In reality, there are P ャ ケ a ヲゥョ Q ゥ エ・ i ャオイ

Z、 ・ イ エ ィッウ ・ p イ o 「。 「 ゥA ャ ゥエゥ・ ウN hッキ・ ィ・ nun・ ク ー ッ ・ ョ エ ゥ 。 Q 。 イ ャ 、 eachー。 エ ィ canhavea 、 ゥウ エゥョ ャc エ probability, so it is

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

I10t realisticto considerthepossiblesetof valuesfor p.KISteadrwe cmォ ・ the 。 ー ー イ ッ ッ ヲ binning: we computeM; for a rangeof valuesfor

p, e.g., p = ャセoG n = 0to 100

Solution45:LetFsmodelthemapasagmphG=(KE)suchthateach[ is イ ・ ー イ ・ ウ ・ ・ 、 by vertex v ゥ v andanedge H カ v ェ I e existsiff

thereis awayto go fromroomt to roomj.Letke)bethelengthof theorridor representedby theedgee.

Thekeyideahereis to assig1eachroomm expectedtime to thetrea-sureroomwhenwe follow the ッ ー エ 。 ャ strategy.Let's sayfor roomi, theexpectedtime to the treasureroom is t(i)·Thenfor a mmpedalroomT キ ・ would alwayspick the next room to be the OI1e that givesus theウ ュ 。 ャ ャ ・ ク ー ・ 」 エ ・ 、 ・ to the treasureroom. Henceヲ ッ イ

エ H ゥゥ I ュ ゥ iセ ー H j H H ゥ ェ I ) エ HェIIェZH Ie|O

Ontheotherhandyfor thespecialroomsrtheexpectedtkm is gohgtobeQ ・ averageof theexpectedtimesthroughall theoutgohgedges.Hence

for specialroomi

t(i) ] e e H ャ H H ゥ ェ I I エ H ェ I I

Also, if thetreasureroomisvertexsr thent(s)=0.Usingtheseイ ・ ャ 。 エ ゥ o ᆳウ ャ ゥ イ we cancomputet(Ofor eachvertexi by ィ ゥ エ ゥ 。 ャ ゥ コ ゥ エ H ゥ I ] ヲ ッ イall nodes X andt(8) = O. Thenwe applythe イ ・ ャ 。 ク 。 エ ゥ ヲ ッ イ eachnodebasedononeof theWo aboveequations.Sincethis graphis a DAGy af-ter IVI stepsof イ・ャ。ク。エゥッ we would reacha fixed point. This algorithmwould havea イオイ ョ・ of O(IEI . IV\) sinceeachrelaxationphaseエ 。 ォ ・ S

{ lエゥ ュ・エ ッー ッャッァゥ ゥ」。ャ ウッイ エ of the graph「 y ウ エ 。 イ エ ゥ i Q Y 。 エ ッ 、 ・ X andthencomputingt( i) for nodei in topologicalorder.

Oncewe have all the valuesof t computed, if we are in any roomwherewe haveto makea choice, we choosethecorridorthatminimizestheexpectedエ ョ ・ to the treasureroom.

Solution4.16: Considera directedgraphG = (V,E) , wheretheverticesrespondto thecities. Eachpair of 」 ゥ ゥ ウ 」 ッ ョ ョ ・ 」 エ ・ 、 「 ケ ュ ・ 、 ァ ・ MEveryplancorrespondsto a 」ケ ゥョエャ イ。ーィ。 カゥ」・ versa.S

medtofiadacyclewhichmaximizestherat10ofprofltfor alljobsOMhecycle to thecostof ー ・ イ ヲ ッ イ イ イ ァ thejobs P ィ ・ cycle.

Let ュ 。 ク bethemaximumratioachevable-Wecmfhd pmax「ケ ・ウウᆳing a ratio 。 ョ 、 seeingwhetherit is too largeor too small.

Let pbeaaypOSHiverealIIUmber-Giveeachedgee =(tJ)aweightof p·c(e)-p(j), whrec(e)isthe costof エ 。 ォ ゥ ・ 、 ァ ・ e andp(j) is the

150 CHAPTER4. ALGORITHMSONGRAPHS

ーイッ エ of visiting nodej.If the graphhasa negativecycle キ ゥ エ ィ ゥ ウ weightfunction, we claim

that \ Pmax·Let C besuchacycle. Thenweknowthatpc(C) - p(C) < 0, wherewe

haveextendedc andp to ウ ・ ア オ ・ 」 ・ ウ of edgesin thenaturalway. There-fore for cycleC, wehavep(C)/ 」 HcI ^ i.e., p < ーュ。 x

cッイ Q カ・イウ・ャケ セ ゥ ヲ all the cycles ゥ Q the graphhave a ーッウゥ エゥ カ・ weight, itmustbe エ ィ 。 エ ュ 。 ク G Since ゥ ヲ ュ 。 ク ヲ ー let C be a cyclewhoseー イ ッ エ ᆳ

to-costratio is ーュ tィ・ョ p(C)/ c(C) = pュ。ク キィゥ」ィ implies p( cI」HcI P contradictingtheabsenceッ ヲ P ー ッ ウ ゥ カ ・ weightcycles.

There is a straightforwardalgorithmfor computingthe presenceofnegativeweightcycleswhich runsin O(IVI . IE\) time. We canperformbinarysearchto ヲ ゥ ョ 、 ュ 。 キ ゥ エ ィ 0 asa lowerboundandmaxeEEp(e)/ c(e).Thesearchcanbeterminatedwhenwe have 、 ・ エ ・ イ ュ ゥ ョ ・ 、 G ュ 。 ク to a speci-fied toleranceof

Clearly, it is not advantageousto makeany moveunlessthe ー イ ッ ᆳ

to-costratio is greaterthanone. We canboundthe maximumpossibleprofit-to-costratioby finding theedgethatmaximizesthe ratio of ー イ ッ エ

of visiting its destinationto thecostof traversingtheedge.Supposethiscostis R, thenwe needto performthe searchbetween1.0 andR for theoptimum ratio. In order to narrow down the searchto an interval ofsize キ ・ would need(log(R - 1) - ャッァHI )/ log 2 steps.Sinceeachstepinvolvesfinding anegativecycle, it canbedonein 0 (I v I time usingtheBellman-Fordalgorithm.

Solution4.17: The straightforwardsolutionwould be to computetheshortestpathfrom A to B for eachproposal.

Notethatwe cannotaddall theproposalsat once;otherwise, wemayendup with a shortestpathwhichusesmultiple proposals.

Insteadwe use an all-pairs shortestpath algorithm on the originalgraphto get a matrix S(u,v) of shortestpathdistancesfor eachpair ofvertices. Eachproposalp is a pair of cities x ,y. Thebestwe cando byusingproposalp is min (S(A ,B) ,S(A ,x) +A(y ,B)). This computationisconstanttime, sowe canevaluateall the proposalsin time proportionalto thenumberof proposalsafterwe havecomputedtheshortestpathforeachpair. All-pairs shortestpathcanbecomputedin O(IVI . IEllog IV\)timeby multiplecallsto d ウ エ イ 。 G ウ algorithmor in O(IVI 3 ) timeusingthef ャ ッ ケ 、 。 イ ウ ィ 。 ャ ャ algorithm.

Solution4.18: This is aclassicalproblemandis solvedusinga"proposalalgorithm".

Eachstudentwho doesnot haveanadviser"proposes"to the most-preferredprofessorto whomhehasnotyetproposed.

Eachprofessorthenconsidersall thestudentswhohaveproposedto

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

I10t realisticto considerthepossiblesetof valuesfor p.KISteadrwe cmォ ・ the 。 ー ー イ ッ ッ ヲ binning: we computeM; for a rangeof valuesfor

p, e.g., p = ャセoG n = 0to 100

Solution45:LetFsmodelthemapasagmphG=(KE)suchthateach[ is イ ・ ー イ ・ ウ ・ ・ 、 by vertex v ゥ v andanedge H カ v ェ I e existsiff

thereis awayto go fromroomt to roomj.Letke)bethelengthof theorridor representedby theedgee.

Thekeyideahereis to assig1eachroomm expectedtime to thetrea-sureroomwhenwe follow the ッ ー エ 。 ャ strategy.Let's sayfor roomi, theexpectedtime to the treasureroom is t(i)·Thenfor a mmpedalroomT キ ・ would alwayspick the next room to be the OI1e that givesus theウ ュ 。 ャ ャ ・ ク ー ・ 」 エ ・ 、 ・ to the treasureroom. Henceヲ ッ イ

エ H ゥゥ I ュ ゥ iセ ー H j H H ゥ ェ I ) エ HェIIェZH Ie|O

Ontheotherhandyfor thespecialroomsrtheexpectedtkm is gohgtobeQ ・ averageof theexpectedtimesthroughall theoutgohgedges.Hence

for specialroomi

t(i) ] e e H ャ H H ゥ ェ I I エ H ェ I I

Also, if thetreasureroomisvertexsr thent(s)=0.Usingtheseイ ・ ャ 。 エ ゥ o ᆳウ ャ ゥ イ we cancomputet(Ofor eachvertexi by ィ ゥ エ ゥ 。 ャ ゥ コ ゥ エ H ゥ I ] ヲ ッ イall nodes X andt(8) = O. Thenwe applythe イ ・ ャ 。 ク 。 エ ゥ ヲ ッ イ eachnodebasedononeof theWo aboveequations.Sincethis graphis a DAGy af-ter IVI stepsof イ・ャ。ク。エゥッ we would reacha fixed point. This algorithmwould havea イオイ ョ・ of O(IEI . IV\) sinceeachrelaxationphaseエ 。 ォ ・ S

{ lエゥ ュ・エ ッー ッャッァゥ ゥ」。ャ ウッイ エ of the graph「 y ウ エ 。 イ エ ゥ i Q Y 。 エ ッ 、 ・ X andthencomputingt( i) for nodei in topologicalorder.

Oncewe have all the valuesof t computed, if we are in any roomwherewe haveto makea choice, we choosethecorridorthatminimizestheexpectedエ ョ ・ to the treasureroom.

Solution4.16: Considera directedgraphG = (V,E) , wheretheverticesrespondto thecities. Eachpair of 」 ゥ ゥ ウ 」 ッ ョ ョ ・ 」 エ ・ 、 「 ケ ュ ・ 、 ァ ・ MEveryplancorrespondsto a 」ケ ゥョエャ イ。ーィ。 カゥ」・ versa.S

medtofiadacyclewhichmaximizestherat10ofprofltfor alljobsOMhecycle to thecostof ー ・ イ ヲ ッ イ イ イ ァ thejobs P ィ ・ cycle.

Let ュ 。 ク bethemaximumratioachevable-Wecmfhd pmax「ケ ・ウウᆳing a ratio 。 ョ 、 seeingwhetherit is too largeor too small.

Let pbeaaypOSHiverealIIUmber-Giveeachedgee =(tJ)aweightof p·c(e)-p(j), whrec(e)isthe costof エ 。 ォ ゥ ・ 、 ァ ・ e andp(j) is the

150 CHAPTER4. ALGORITHMSONGRAPHS

ーイッ エ of visiting nodej.If the graphhasa negativecycle キ ゥ エ ィ ゥ ウ weightfunction, we claim

that \ Pmax·Let C besuchacycle. Thenweknowthatpc(C) - p(C) < 0, wherewe

haveextendedc andp to ウ ・ ア オ ・ 」 ・ ウ of edgesin thenaturalway. There-fore for cycleC, wehavep(C)/ 」 HcI ^ i.e., p < ーュ。 x

cッイ Q カ・イウ・ャケ セ ゥ ヲ all the cycles ゥ Q the graphhave a ーッウゥ エゥ カ・ weight, itmustbe エ ィ 。 エ ュ 。 ク G Since ゥ ヲ ュ 。 ク ヲ ー let C be a cyclewhoseー イ ッ エ ᆳ

to-costratio is ーュ tィ・ョ p(C)/ c(C) = pュ。ク キィゥ」ィ implies p( cI」HcI P contradictingtheabsenceッ ヲ P ー ッ ウ ゥ カ ・ weightcycles.

There is a straightforwardalgorithmfor computingthe presenceofnegativeweightcycleswhich runsin O(IVI . IE\) time. We canperformbinarysearchto ヲ ゥ ョ 、 ュ 。 キ ゥ エ ィ 0 asa lowerboundandmaxeEEp(e)/ c(e).Thesearchcanbeterminatedwhenwe have 、 ・ エ ・ イ ュ ゥ ョ ・ 、 G ュ 。 ク to a speci-fied toleranceof

Clearly, it is not advantageousto makeany moveunlessthe ー イ ッ ᆳ

to-costratio is greaterthanone. We canboundthe maximumpossibleprofit-to-costratioby finding theedgethatmaximizesthe ratio of ー イ ッ エ

of visiting its destinationto thecostof traversingtheedge.Supposethiscostis R, thenwe needto performthe searchbetween1.0 andR for theoptimum ratio. In order to narrow down the searchto an interval ofsize キ ・ would need(log(R - 1) - ャッァHI )/ log 2 steps.Sinceeachstepinvolvesfinding anegativecycle, it canbedonein 0 (I v I time usingtheBellman-Fordalgorithm.

Solution4.17: The straightforwardsolutionwould be to computetheshortestpathfrom A to B for eachproposal.

Notethatwe cannotaddall theproposalsat once;otherwise, wemayendup with a shortestpathwhichusesmultiple proposals.

Insteadwe use an all-pairs shortestpath algorithm on the originalgraphto get a matrix S(u,v) of shortestpathdistancesfor eachpair ofvertices. Eachproposalp is a pair of cities x ,y. Thebestwe cando byusingproposalp is min (S(A ,B) ,S(A ,x) +A(y ,B)). This computationisconstanttime, sowe canevaluateall the proposalsin time proportionalto thenumberof proposalsafterwe havecomputedtheshortestpathforeachpair. All-pairs shortestpathcanbecomputedin O(IVI . IEllog IV\)timeby multiplecallsto d ウ エ イ 。 G ウ algorithmor in O(IVI 3 ) timeusingthef ャ ッ ケ 、 。 イ ウ ィ 。 ャ ャ algorithm.

Solution4.18: This is aclassicalproblemandis solvedusinga"proposalalgorithm".

Eachstudentwho doesnot haveanadviser"proposes"to the most-preferredprofessorto whomhehasnotyetproposed.

Eachprofessorthenconsidersall thestudentswhohaveproposedto

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

ィ 。iQ、 tells theomhemostprefersF"Iaccepty01111 andHnoH to tke rest.t ィ T イ ッ ヲ ・ ウ ウ ッ イ is thenprovisiOI1allymatchedto a studeIIt.

T ・ 。 、 Q ア オ イ ッ オ ョ 、 ・ 。 オ 、 ・ ・ ョ Q

ヲ [ [ ] Z R [ j [ イ Z [ [ Z [ Z [ Z [ Z [ [ Q S R Z j Z コ コ [ Z Z Z [ Z Z Z Z Z Z _ Z Z ゥ U Z R [ ヲ ャ R ;_ イ s oイ ッュ。ァ。ゥューャゥ・ウキゥエm・ B。」 B イ・ェ・」エウ the イ

This maymemthatprofessorswhohavealreadyaccepteda studentcmutrade-upFFmdstdmtswhohavealreadybeenacceptedby a prom

fessorcanbe"jilted".This algorithmhastwo key properties:-It 」 ュ カ ・ ウ to a statewhereeveryoneis pded Everyomgets

。 」 」 ・ エ ・ 、 at someーッ Q エ N ッ コ オ ・ a professoracceptsa studeatyheal-way:hasastudmtt ィ ・ イ ・ 」 。 ュ ッ エ ィ 。 ー 、 ・ ュ ッ イ 。 studentbothunpairedsincethe ウ エ ョ エ ュ ャ 。 カ ・ ー イ ッ ー ッ ウ ・ 、 m Q 。 エ ー イ ッ ヲ ・ ウ ウ ッ イ _ Z

イ ョ H I ゥ ョ エ サ ウ ゥ ョ ・ a studentwill eventuallyproposeto everyone, 1Ii

RZZ [コ [IIュ、 「・ゥ ョ ァオョ iョー。 p 。ゥイ ・

tzz[ [[[ ZRQZZ[コヲ」RWSRCompletionofthe。 ャ ァ ッ イ ゥ ゥ エ ゥ ウ m ー ッ ュ 「 ャ ・ forpothRiemammuGaussto prefereachotherovertheir CUI-reIIt paziz19·Ifr ゥ ・ ュ ュ

nrefersGaussエ ッ ィ ゥ ウ 」 オ オ イ ・e ・ ヲ ・ ・ 、 ィ ゥ ウ 」 オ イ イ ・ ッ ヲ ・ s ウ ッ イ 。 オ Q s ウ 。 c c ・ ー 、 r ・ i ュ 。

Z R o Z R ゥ _ Z _ Z Z [ Z Z ] Z コ エ Z R [ R [ Z z Z Z Z ゥ エ Z Z R コ Z f Q a Z [ [ Z コZ Z Z Z [ Z Z :T o t ャTゥhis ー イ ッ ー ィ ・ was 。 ャ イ ・ ー 。 ゥ イ ・ 、 with ウ ッ ュ ・ ッ イ ・ preferredto

r ゥ ・ ュ i ョ N

Solution4.19:Wedefim a weighteddirectedgraph ア ] H k v ᅲ v Iキ ィ c o イ イ イ ・ ウ ー ッ ョ 、 s] H オ l j カI I is the amountof commodityv we can bUy W

」ッエZZu 。ョ。 ゥエ ・ x ゥ s エweightsmultiply out to morethan1L.

Createa セ・キ graph g G H v E) with weight function w' (e)ャッァ H・IN Since ャ ッ ァ 「 = ャッァ ャッァ「f thereis a cycleh G whoseedge

weightsmultiply out to morethaI11iff thereis a cycleh Gfwhoseedge

キ ・ ゥ [ R S R S エ コ コ コ z l ゥ カ ・ weight cycles ゥ ョ w ・ ゥ ゥ ァ ィ Q エ

S Z イ p イ A _ ・ _ Q ゥtrage.

Solution4.20: First, note that the numberof packets。 エ ャ ー u エ i is the

152CHAPTER4. ALGORITHMSONGRAPHS

153

sumof theelementsin row i andthenumberof packetsdestinedthroughoutputj is thesumof all theelementsin columnj.

Let the maximumrow sumbe R-thenit will take at leastR cyclesto transferthepacketsfrom aninput correspondingto R. Similarly, if 0is themaximumcolumnsum, it will takeat least0 cyclesto transferthepacketsto anoutputcorrespondingto 0 , ゥN ・N ] max(R,0) is a lower「 ッ オ 、 ッ エ ィ ・ numberof cycles.

We claim (3 is actuallya tight bound. To do this, we first provethatwe cancreatea matrix A* セ a suchthat everyrow andcolumnof A*sumsup to (3.

The proof is by construction-startingwith A, find a row anda col-umnwhosesumsareless エ ィ 。 ョ 。 ョ 、 incrementthatelementby 1. Eachsuccessivematrix is largerthanits predecessorandtheprocessmustcon-vergeto a matrixwhoserows andcolumnsall sumup to (3.

Now, considera 「ゥー。イエ ・ hypergraphonvertices{(L ,0) ,… , Hl ᆳ

QI Hr oI NN Hr M I)} , wherewe haveA*[i ,j] edgesbetweenvertex(L ,i) and (R ,j). Sincetherow andcolumnsumsareall (3, it follows thatthedegreesof all verticesis (3.

ャゥウ graphhas a perfect ュ 。 エ 」 ィ ゥ ョ ァ M ャ ゥ ウ follows from the theoremthata Mイ・ァオャ。イ bipartitegraphhasa perfectmatchingwhich in turn fol-lows from Birkhoff's 」 ィ 。 イ 。 」 エ ・ イ ゥ コ 。 エ ゥ ッ ッ ヲ bipartitegraphs, namelya per-fect matchingexists ゥ ・ カ ・ イ ケ subsetof sizek hasat leastk neighbors.

A ー・ イヲ・」エ matching is a ー・ イュオ エ。 エゥッョ from ゥ i ャ ー u エ s to outputs-bychoosingthese。 ウ ウ ゥ ァ イ ョ ャ i d ュ Q ・ イ ャ エ ウ 。 ョ ャ 、 ー ・ イ ヲ ッ y G ュ

we canreducethe イ ャ オ ュ ュ ャ 「 ・ イ of packetsエ ッ エ イ G 。 ョ i ャ s ヲ ・ イ from A* by n andtheresultingmatrixhasrowsandcolumnssummingto exactly(3 - 1. In thisway, we canconstructa schedulewhich transfersall thepacketsin A* in(3 cycles.SinceA* セ a this schedulewill alsotransferall thepacketsinA in (3 cycles.

Solution4.21: If the transmitterandreceiverdecideon a restrictedsetof pairsof symbolsratherthanjustsymbols, theycandobetterthan1bitpersymboltransmitted.

The insight is that a pair like (A ,O) and (B ,E) cannotbe mistakenfor eachothersince0 andE cannotconflict.

A formal way of finding the largestset of pairs of symbolswhichcannotbemistakenfor eachotheris to createa conflict graph エ ィ ・ 25pairs {(A , aI N . , (E ,E) }-put an edgebetween(U1 ,U2) and H Q V2) ゥ

H u Q v Q I i i and H u R I i i N

Now, we want to find a maximumindependentsetin this graph-i.e., the largestsubsetof vertices, not two of which areconnectedby anedge.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

ィ 。iQ、 tells theomhemostprefersF"Iaccepty01111 andHnoH to tke rest.t ィ T イ ッ ヲ ・ ウ ウ ッ イ is thenprovisiOI1allymatchedto a studeIIt.

T ・ 。 、 Q ア オ イ ッ オ ョ 、 ・ 。 オ 、 ・ ・ ョ Q

ヲ [ [ ] Z R [ j [ イ Z [ [ Z [ Z [ Z [ Z [ [ Q S R Z j Z コ コ [ Z Z Z [ Z Z Z Z Z Z _ Z Z ゥ U Z R [ ヲ ャ R ;_ イ s oイ ッュ。ァ。ゥューャゥ・ウキゥエm・ B。」 B イ・ェ・」エウ the イ

This maymemthatprofessorswhohavealreadyaccepteda studentcmutrade-upFFmdstdmtswhohavealreadybeenacceptedby a prom

fessorcanbe"jilted".This algorithmhastwo key properties:-It 」 ュ カ ・ ウ to a statewhereeveryoneis pded Everyomgets

。 」 」 ・ エ ・ 、 at someーッ Q エ N ッ コ オ ・ a professoracceptsa studeatyheal-way:hasastudmtt ィ ・ イ ・ 」 。 ュ ッ エ ィ 。 ー 、 ・ ュ ッ イ 。 studentbothunpairedsincethe ウ エ ョ エ ュ ャ 。 カ ・ ー イ ッ ー ッ ウ ・ 、 m Q 。 エ ー イ ッ ヲ ・ ウ ウ ッ イ _ Z

イ ョ H I ゥ ョ エ サ ウ ゥ ョ ・ a studentwill eventuallyproposeto everyone, 1Ii

RZZ [コ [IIュ、 「・ゥ ョ ァオョ iョー。 p 。ゥイ ・

tzz[ [[[ ZRQZZ[コヲ」RWSRCompletionofthe。 ャ ァ ッ イ ゥ ゥ エ ゥ ウ m ー ッ ュ 「 ャ ・ forpothRiemammuGaussto prefereachotherovertheir CUI-reIIt paziz19·Ifr ゥ ・ ュ ュ

nrefersGaussエ ッ ィ ゥ ウ 」 オ オ イ ・e ・ ヲ ・ ・ 、 ィ ゥ ウ 」 オ イ イ ・ ッ ヲ ・ s ウ ッ イ 。 オ Q s ウ 。 c c ・ ー 、 r ・ i ュ 。

Z R o Z R ゥ _ Z _ Z Z [ Z Z ] Z コ エ Z R [ R [ Z z Z Z Z ゥ エ Z Z R コ Z f Q a Z [ [ Z コZ Z Z Z [ Z Z :T o t ャTゥhis ー イ ッ ー ィ ・ was 。 ャ イ ・ ー 。 ゥ イ ・ 、 with ウ ッ ュ ・ ッ イ ・ preferredto

r ゥ ・ ュ i ョ N

Solution4.19:Wedefim a weighteddirectedgraph ア ] H k v ᅲ v Iキ ィ c o イ イ イ ・ ウ ー ッ ョ 、 s] H オ l j カI I is the amountof commodityv we can bUy W

」ッエZZu 。ョ。 ゥエ ・ x ゥ s エweightsmultiply out to morethan1L.

Createa セ・キ graph g G H v E) with weight function w' (e)ャッァ H・IN Since ャ ッ ァ 「 = ャッァ ャッァ「f thereis a cycleh G whoseedge

weightsmultiply out to morethaI11iff thereis a cycleh Gfwhoseedge

キ ・ ゥ [ R S R S エ コ コ コ z l ゥ カ ・ weight cycles ゥ ョ w ・ ゥ ゥ ァ ィ Q エ

S Z イ p イ A _ ・ _ Q ゥtrage.

Solution4.20: First, note that the numberof packets。 エ ャ ー u エ i is the

152CHAPTER4. ALGORITHMSONGRAPHS

153

sumof theelementsin row i andthenumberof packetsdestinedthroughoutputj is thesumof all theelementsin columnj.

Let the maximumrow sumbe R-thenit will take at leastR cyclesto transferthepacketsfrom aninput correspondingto R. Similarly, if 0is themaximumcolumnsum, it will takeat least0 cyclesto transferthepacketsto anoutputcorrespondingto 0 , ゥN ・N ] max(R,0) is a lower「 ッ オ 、 ッ エ ィ ・ numberof cycles.

We claim (3 is actuallya tight bound. To do this, we first provethatwe cancreatea matrix A* セ a suchthat everyrow andcolumnof A*sumsup to (3.

The proof is by construction-startingwith A, find a row anda col-umnwhosesumsareless エ ィ 。 ョ 。 ョ 、 incrementthatelementby 1. Eachsuccessivematrix is largerthanits predecessorandtheprocessmustcon-vergeto a matrixwhoserows andcolumnsall sumup to (3.

Now, considera 「ゥー。イエ ・ hypergraphonvertices{(L ,0) ,… , Hl ᆳ

QI Hr oI NN Hr M I)} , wherewe haveA*[i ,j] edgesbetweenvertex(L ,i) and (R ,j). Sincetherow andcolumnsumsareall (3, it follows thatthedegreesof all verticesis (3.

ャゥウ graphhas a perfect ュ 。 エ 」 ィ ゥ ョ ァ M ャ ゥ ウ follows from the theoremthata Mイ・ァオャ。イ bipartitegraphhasa perfectmatchingwhich in turn fol-lows from Birkhoff's 」 ィ 。 イ 。 」 エ ・ イ ゥ コ 。 エ ゥ ッ ッ ヲ bipartitegraphs, namelya per-fect matchingexists ゥ ・ カ ・ イ ケ subsetof sizek hasat leastk neighbors.

A ー・ イヲ・」エ matching is a ー・ イュオ エ。 エゥッョ from ゥ i ャ ー u エ s to outputs-bychoosingthese。 ウ ウ ゥ ァ イ ョ ャ i d ュ Q ・ イ ャ エ ウ 。 ョ ャ 、 ー ・ イ ヲ ッ y G ュ

we canreducethe イ ャ オ ュ ュ ャ 「 ・ イ of packetsエ ッ エ イ G 。 ョ i ャ s ヲ ・ イ from A* by n andtheresultingmatrixhasrowsandcolumnssummingto exactly(3 - 1. In thisway, we canconstructa schedulewhich transfersall thepacketsin A* in(3 cycles.SinceA* セ a this schedulewill alsotransferall thepacketsinA in (3 cycles.

Solution4.21: If the transmitterandreceiverdecideon a restrictedsetof pairsof symbolsratherthanjustsymbols, theycandobetterthan1bitpersymboltransmitted.

The insight is that a pair like (A ,O) and (B ,E) cannotbe mistakenfor eachothersince0 andE cannotconflict.

A formal way of finding the largestset of pairs of symbolswhichcannotbemistakenfor eachotheris to createa conflict graph エ ィ ・ 25pairs {(A , aI N . , (E ,E) }-put an edgebetween(U1 ,U2) and H Q V2) ゥ

H u Q v Q I i i and H u R I i i N

Now, we want to find a maximumindependentsetin this graph-i.e., the largestsubsetof vertices, not two of which areconnectedby anedge.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

s サHjエjエI (13,(7), HHWセI (l), 13), HセャIIスN

Thereareanumberof suchsetsof cardinality5-e.g.,

Thereforewecansendlog2 5bitswith everytwo symbolswhichamountsto roughly1.16bitspersymboltransmitted.

Solution4.22: In Solution4.10, we showedhow to modelthe problemusinga DAG, with eachvertexcorrespondingto a ーャ。ケ・ pイッ「ャ・ュ 4.22is askingfor a minimum cardinalitysetof vertex 、 ゥ ッ ゥ ョ エ pathsin thisDAG suchthateachvertexappearsonsomepath.

t ャ ー イ ッ 「 ャ ・ ュ canbereducedto a flow problem: let G = Hv セI be aDAG. c ッ イ イ オ 」 エ a flow problemF asfollows: defineG = (V' , セGI fromG = Hv セI by creatingaleft vertexVz andaright vertexVr for eachvertexV E V.

Add anewsourcevertexs, addedgesfrom s to eachleft vertex, andadda ウゥョォ イ・イエ・ク t with edgesfrom eachright vertexto t.

Add edgesH カ コ ヲ ッ イ eachv v N For eachedge(u , カI セ addanedge H カ l z I G

Assignalowerboundandupperboundof 1for eachedgeof theformH v z I イ ); all otheredgeshavea lowerboundof 0 andupperboundof N

By construction, theminimumfeasibleflow for F definesaminimumcardinalitysetof vertexdisjointpaths.

Solution4.23 Z ャ・ problemcandirectly be mappedinto the weightedbipartite matchingproblem: biddersand celebritiesconstitutethe leftandright vertices;an edgeexistsfrom b to c iff b hasofferedmoneytodancewith c 。ョ、 ャ・ weight of an edgeis the amountoffered for thedance.It canbe solvedusingspecializedalgorithms, networkflows, orlinearprogramming.

If the requirementthatbiddersandcelebritiesbedistinct is dropped,the problembecomesa weightedmatchingproblemin a generalgraphwhich is still solvablein polynomialtime.

Solution4.24: Let ¢ beaCNFexpressionof カ 。 イ ゥ 。 「 ャ ・ ウ Xo,… ,Xn-l andm clausesin which eachclausecontainsno morethantwo variables.

Assumewithout lossof generalitythat eachclausein ¢ containsex-actly two distinctvariablessincesingletonclausesforce thevalueof thecorrespondingvariablefor a satisfyingassignment.

Construct the directed graph GrjJ on 2n vertices indexed byx ッ W Q x ッ _ M i G . For eachclause ャ ゥ ャ ェ add an edgefroml/ to ャ ェ ャ 、 l/ to li , wherexi" is interpretedasXi.

Claim: ¢ is satisfiableゥ ヲ ッ イ each エ ィ ・ イ ・ doesnotexistapathfrom V Xi

エ ッ 。 ョ 、 apathfrom v セ ゥ to V Xi •

Proof If an edgeexistsfrom V Xi to V Xj , it meansthat wheneverXi

155

is true, then Xj ュオ 「・ true in a satisfying 。 ウ ウ ゥ ァ ョ エ for ¢. Similar

ttzuzzizzurtzt:113u ヲ j T Q ウ tmm 。 。 ウ ウ i ァ ョ ャ ・ ョ エ for ᄁ ・ ョ s ッ ュ オ Qャ | j o w f c ッ ョ ウ ゥ 、 ・ イ エ ャ CCG?Of G IHf fhOr Soュュ・ エ f エ j xゥ 。ョ、 Z aイ ・ T o エ ィゥョ エ ィ・

Z [コR Z Ze[CCF エィエィィQ ・ イ ・ 」。ョョッエ ・・ x xis s エ a ウs 。 エ QY a ョュ・ エヲ cッョ カ・イウ・ャスゥ コヲ aM t44are P Q ィ h the sameSCCrwe will prove that a

ゥャe ZZRUSQ ゥゥゥStart with m y source SCCh the SCC DAG which contains Q [Zュ イQdoesイョ ッ o エ have 。 ー 。 エ ィ エ o v ャ H s オ 」 ィ 。 カ ・ イ イ エ ・ ク ュ オ Q s エ s エ [ P エ ィ ・ ャ t

l T r would be h the same SCC) Set k to true m d Upd 。 。ャャ ャュ [SS181mentsrincludingsettingkf to false. i エ・ イ 。 エゥ カ ・ャケ perform エ ィ ゥ ウ com-

Z Z [ エ [ [ コ Z Z l ゥ Q [ R コ Z j Z コ e Z Z [ R エ ヲ ] [ Z Z [ Z D Z Z [ コ コ Z [ ヲ ャ | Z R Z _ ヲ イ ヲ _ A f c 、 ォin thesameSCC 。ョ ャ、 we are ッ ョ Q Q ケ イ ・ 、 オ c ィ ァ theSCC dagNlz

Eachclausewill besatisfiedafter this is completedsinceeachclause

ェ ェ z h u z e a e u コ イ コ [ R R R Z エ ゥ

R Q W R l エ ヲ ゥ Z Let 「 ・ a set ッ ヲ 。 ャ ゥ エ ケ and ゥ ョ 。 c o i7...7Zn-I-.Createm mdirectedgraph g o カ ・ イ エ ゥ 」 ・ ウ

x ッ N ,Xn -1; for each・ ア オ 。 ャ ] Xj , addtheedge(Xi ,X,i)

[ コ e R R Z コ Z R Z コ Z e R イ イ _ イ Z エ _ Z _ ᄆ エ Zt ィ ・ イ ッ イ ・ ゥ ヲ ヲ ヲ o イ someゥ ョ ・ ア オ 。 ャ ゥ エ x ー カ ・ イ エ ゥ 」 ・ ウ xp andxq lie in theeSCC, thesetof 」 ッ ョ ウ エ イ 。 ゥ ᄁ is ョッエ ゥヲゥ・、

p

COIlversely;let.therebe k CORRectedcompoI1eI1tsCo ᄋcz aウ[ZejエコエエャcUQR_ZRRRZZヲZZQZエzejol、ゥ ・イ・iQエ SCCsyall iIIequality cOI1strahtsaresatisfiedtoo.

CHAPTER4. ALGORITHMSONGRAPHS154

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

s サHjエjエI (13,(7), HHWセI (l), 13), HセャIIスN

Thereareanumberof suchsetsof cardinality5-e.g.,

Thereforewecansendlog2 5bitswith everytwo symbolswhichamountsto roughly1.16bitspersymboltransmitted.

Solution4.22: In Solution4.10, we showedhow to modelthe problemusinga DAG, with eachvertexcorrespondingto a ーャ。ケ・ pイッ「ャ・ュ 4.22is askingfor a minimum cardinalitysetof vertex 、 ゥ ッ ゥ ョ エ pathsin thisDAG suchthateachvertexappearsonsomepath.

t ャ ー イ ッ 「 ャ ・ ュ canbereducedto a flow problem: let G = Hv セI be aDAG. c ッ イ イ オ 」 エ a flow problemF asfollows: defineG = (V' , セGI fromG = Hv セI by creatingaleft vertexVz andaright vertexVr for eachvertexV E V.

Add anewsourcevertexs, addedgesfrom s to eachleft vertex, andadda ウゥョォ イ・イエ・ク t with edgesfrom eachright vertexto t.

Add edgesH カ コ ヲ ッ イ eachv v N For eachedge(u , カI セ addanedge H カ l z I G

Assignalowerboundandupperboundof 1for eachedgeof theformH v z I イ ); all otheredgeshavea lowerboundof 0 andupperboundof N

By construction, theminimumfeasibleflow for F definesaminimumcardinalitysetof vertexdisjointpaths.

Solution4.23 Z ャ・ problemcandirectly be mappedinto the weightedbipartite matchingproblem: biddersand celebritiesconstitutethe leftandright vertices;an edgeexistsfrom b to c iff b hasofferedmoneytodancewith c 。ョ、 ャ・ weight of an edgeis the amountoffered for thedance.It canbe solvedusingspecializedalgorithms, networkflows, orlinearprogramming.

If the requirementthatbiddersandcelebritiesbedistinct is dropped,the problembecomesa weightedmatchingproblemin a generalgraphwhich is still solvablein polynomialtime.

Solution4.24: Let ¢ beaCNFexpressionof カ 。 イ ゥ 。 「 ャ ・ ウ Xo,… ,Xn-l andm clausesin which eachclausecontainsno morethantwo variables.

Assumewithout lossof generalitythat eachclausein ¢ containsex-actly two distinctvariablessincesingletonclausesforce thevalueof thecorrespondingvariablefor a satisfyingassignment.

Construct the directed graph GrjJ on 2n vertices indexed byx ッ W Q x ッ _ M i G . For eachclause ャ ゥ ャ ェ add an edgefroml/ to ャ ェ ャ 、 l/ to li , wherexi" is interpretedasXi.

Claim: ¢ is satisfiableゥ ヲ ッ イ each エ ィ ・ イ ・ doesnotexistapathfrom V Xi

エ ッ 。 ョ 、 apathfrom v セ ゥ to V Xi •

Proof If an edgeexistsfrom V Xi to V Xj , it meansthat wheneverXi

155

is true, then Xj ュオ 「・ true in a satisfying 。 ウ ウ ゥ ァ ョ エ for ¢. Similar

ttzuzzizzurtzt:113u ヲ j T Q ウ tmm 。 。 ウ ウ i ァ ョ ャ ・ ョ エ for ᄁ ・ ョ s ッ ュ オ Qャ | j o w f c ッ ョ ウ ゥ 、 ・ イ エ ャ CCG?Of G IHf fhOr Soュュ・ エ f エ j xゥ 。ョ、 Z aイ ・ T o エ ィゥョ エ ィ・

Z [コR Z Ze[CCF エィエィィQ ・ イ ・ 」。ョョッエ ・・ x xis s エ a ウs 。 エ QY a ョュ・ エヲ cッョ カ・イウ・ャスゥ コヲ aM t44are P Q ィ h the sameSCCrwe will prove that a

ゥャe ZZRUSQ ゥゥゥStart with m y source SCCh the SCC DAG which contains Q [Zュ イQdoesイョ ッ o エ have 。 ー 。 エ ィ エ o v ャ H s オ 」 ィ 。 カ ・ イ イ エ ・ ク ュ オ Q s エ s エ [ P エ ィ ・ ャ t

l T r would be h the same SCC) Set k to true m d Upd 。 。ャャ ャュ [SS181mentsrincludingsettingkf to false. i エ・ イ 。 エゥ カ ・ャケ perform エ ィ ゥ ウ com-

Z Z [ エ [ [ コ Z Z l ゥ Q [ R コ Z j Z コ e Z Z [ R エ ヲ ] [ Z Z [ Z D Z Z [ コ コ Z [ ヲ ャ | Z R Z _ ヲ イ ヲ _ A f c 、 ォin thesameSCC 。ョ ャ、 we are ッ ョ Q Q ケ イ ・ 、 オ c ィ ァ theSCC dagNlz

Eachclausewill besatisfiedafter this is completedsinceeachclause

ェ ェ z h u z e a e u コ イ コ [ R R R Z エ ゥ

R Q W R l エ ヲ ゥ Z Let 「 ・ a set ッ ヲ 。 ャ ゥ エ ケ and ゥ ョ 。 c o i7...7Zn-I-.Createm mdirectedgraph g o カ ・ イ エ ゥ 」 ・ ウ

x ッ N ,Xn -1; for each・ ア オ 。 ャ ] Xj , addtheedge(Xi ,X,i)

[ コ e R R Z コ Z R Z コ Z e R イ イ _ イ Z エ _ Z _ ᄆ エ Zt ィ ・ イ ッ イ ・ ゥ ヲ ヲ ヲ o イ someゥ ョ ・ ア オ 。 ャ ゥ エ x ー カ ・ イ エ ゥ 」 ・ ウ xp andxq lie in theeSCC, thesetof 」 ッ ョ ウ エ イ 。 ゥ ᄁ is ョッエ ゥヲゥ・、

p

COIlversely;let.therebe k CORRectedcompoI1eI1tsCo ᄋcz aウ[ZejエコエエャcUQR_ZRRRZZヲZZQZエzejol、ゥ ・イ・iQエ SCCsyall iIIequality cOI1strahtsaresatisfiedtoo.

CHAPTER4. ALGORITHMSONGRAPHS154

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter5

Algori thIns on Strings

Solution 5.1: There are severalinterestingalgorithms for substringsearchthatrun in linear-timesuchasKnuth-Marris-Pratt, Boyer-Moore,andRabin-Karpalgorithm. Howeverin practice, for mostapplications,substringsearchruns fasterthanthat. We havefound Boyer-Mooreal-gorithmto bethefastestin our experience.

The Boyer-Moorealgorithmworksby trying to matchcharactersof 8in T at a 」・イ ゥョ offset in the reverseorder (last characterof 8 matchedfirst). Ifwecanmatchall thecharactersin 8, thenwehavefoundamatch;otherwise, we stopat thefirst mismatch.Thekey ideabehindtheBoyer-Moore algorithmis to be ableto skip asmanyoffsetsaspossiblewhenwearedonematchingcharactersatagivenoffset. Wedothisbybuildingtwo tables-goodsuffix shift tableandabadcharactershift table.

For a given character, the badcharactershift table givesus the dis-tanceof the lastoccurrenceof thatcharacterin 8 to therightmoststring.If thecharacterdoesnotoccurin 8, thentheentryin thetableis of i ・ ァ エ ィ

8. Hencewhen we find a characterin T that doesnot matchfor thecurrentoffset, we know how muchwe mustmoveforward so that thischaractercanmatchfor thefirst time.

The goodsuffix shift tableis a little morecomplex. c ッ ョ 」 ・ ー エ オ 。 ャ Q ヲ ッ イ

a given suffix X of 8, it tells us what is the shortestsuffix Y of 8 thatis longer エ ィ ャ x ャ 、 hasX assuffix. In practice, whatwe storeis howfar canwe move safely, ァゥカ・ キ・ havematchedup to ャ ・ エ ィ H x I

charactersbutdid notmatchthenextcharacter.

Solution 5.2: The most naIve way of finding whethera string 8 is asubstringof anotherstringT would be to testcharacterby characteratevery offset in T , if we ヲ ャ 、 a matchfor A. Howeverthis would takeO(m . n) time, wherem is the lengthof A andn is the i ・ エ ィ ofT. Wecandobetterthanthat. If at a certainoffsetwe matcha setof characters

157

in A to thatof T but they 、 ッ 。 エ matchall thecharactersin A sinceA hasall uniquecharacters, thecharactersin T thatmatchedA will not matchA at my otheroffset.Efmcewe caI1skipa few offsets.ThisesseI1tidly

eansthat for everycharacterin T, we compareit with a characteriA at mostonce. This willlead to a ャゥョ ャ・。 イM Mイオ ョウゥョ oH K m) time.

Solution 5.3:This is a specialcaseof applyhga permutatiORwithC.om1mtadditioml storage(cf.Problem114)exceptthat the p ・ イ ュ オ エ 。

Z _ _ _ Q i o ョ Q ウ 。 イ ッ ・ 」 。 s ・ 。

c, ゥ 」 R ゥK」 NNN , (m·i+c) mod n) for 、 ゥ ヲ ヲ ・ イ ・ ョ エ カ 。 ャ セ ・ ウ of cfrom1 エィイッ ィ

IIIber of cycles.SOyesseRtidlyall othercyclesarea shiftedversiORof thefirst cycle-Forexamplerconsiderthecasewhere ] 6 andi = 2.we get (1,3,5) and (2,4,6). Oncewe haveidentified thedifferencebe-tweeIIthelowestmd the seCOI1dlowestdemeI1th anycyclerwe howthenumberof cyclesthereareandtheir startiRgpohts.

Solution5.4: Thekey ideahereis thatif stringA is arotationof anotherstring B, thenA mustbe a substringof B . B. For ・ ク ー i ・ ウ ゥ ョ 」 ・ 」 ゥ ウ

a イッエ。エゥッ ッヲ 」 it is a ウオ「ウエイゥ ッヲ 」 イ」 N Sincesubstringtestcanbe、 o ョ ・ ゥ ョ ャ ゥ ョ ゥ ョ ョ ・ 。

for イ o エ 。 エ ゥ ッ Q in ャ ゥ ョ ・ 。 イ MG M M エ ゥ ュ ャ ・ ッ

Solution5.5: We 。イ・ 。エ providingexplicit solutionto thisproblemheresincethereareno algorithmicideasゥ ョ t ッ ャ カ ・ 、 N Mosttimeswhenthis kindof a questionis asked, youneedto keepa few thingsin mind:

ヲ ウ ゥ ョ ァ ャ ・ pass。 カ ・ イ エ ャ エ イ ゥ

Q ヲ o オ can build ー イ ・ ヲ ゥ tables to match e ャ 、 ・ ク N ィ エ エ ュ ョ ャ 。 ョ ャ 、

セ ・ ヲ ault .ht:n1 in 。 セ カ 。 ョ 」 ・ to speedup theprocess.- You maynot know if youneedto addthep;otocolpart ッ イ 。 エ until

youhavereachedtheeI1dof theAostpart.Hmceit maybea goodideato leavesomespacefor addhgkttp://atthebeghRingofthebuffer.

Solution5.6: This problem 」 。 「 ・ reducedto finding the longestcom-mORSUbsequemebetweeI1thehputstriIIg aI1d its reverse.Wehaveal-readyshownhow this canbedone ・ ヲ ヲ ゥ 」 ゥ ・ イ ャ ケ in Problem3.1.

Solution53:Th!s?mbeemdentIysolveduy dyRamieprogrammgl ・ エ c H I 「 ・ themmII111mwastedspacefor arr-aI1ghgthelast キ ッ イ 、 ウ N Ifwe haveall thevaluesfor C(i) tabulatedfor i \ キ・ cancomputec H Iby finding thenumberof wordswe 」 。 ョ エ in thefirst line thatminimizesc H I .

Solution 5.8: This is anotherinterestingapplicationof dynamicpro-grammmg.

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter5

Algori thIns on Strings

Solution 5.1: There are severalinterestingalgorithms for substringsearchthatrun in linear-timesuchasKnuth-Marris-Pratt, Boyer-Moore,andRabin-Karpalgorithm. Howeverin practice, for mostapplications,substringsearchruns fasterthanthat. We havefound Boyer-Mooreal-gorithmto bethefastestin our experience.

The Boyer-Moorealgorithmworksby trying to matchcharactersof 8in T at a 」・イ ゥョ offset in the reverseorder (last characterof 8 matchedfirst). Ifwecanmatchall thecharactersin 8, thenwehavefoundamatch;otherwise, we stopat thefirst mismatch.Thekey ideabehindtheBoyer-Moore algorithmis to be ableto skip asmanyoffsetsaspossiblewhenwearedonematchingcharactersatagivenoffset. Wedothisbybuildingtwo tables-goodsuffix shift tableandabadcharactershift table.

For a given character, the badcharactershift table givesus the dis-tanceof the lastoccurrenceof thatcharacterin 8 to therightmoststring.If thecharacterdoesnotoccurin 8, thentheentryin thetableis of i ・ ァ エ ィ

8. Hencewhen we find a characterin T that doesnot matchfor thecurrentoffset, we know how muchwe mustmoveforward so that thischaractercanmatchfor thefirst time.

The goodsuffix shift tableis a little morecomplex. c ッ ョ 」 ・ ー エ オ 。 ャ Q ヲ ッ イ

a given suffix X of 8, it tells us what is the shortestsuffix Y of 8 thatis longer エ ィ ャ x ャ 、 hasX assuffix. In practice, whatwe storeis howfar canwe move safely, ァゥカ・ キ・ havematchedup to ャ ・ エ ィ H x I

charactersbutdid notmatchthenextcharacter.

Solution 5.2: The most naIve way of finding whethera string 8 is asubstringof anotherstringT would be to testcharacterby characteratevery offset in T , if we ヲ ャ 、 a matchfor A. Howeverthis would takeO(m . n) time, wherem is the lengthof A andn is the i ・ エ ィ ofT. Wecandobetterthanthat. If at a certainoffsetwe matcha setof characters

157

in A to thatof T but they 、 ッ 。 エ matchall thecharactersin A sinceA hasall uniquecharacters, thecharactersin T thatmatchedA will not matchA at my otheroffset.Efmcewe caI1skipa few offsets.ThisesseI1tidly

eansthat for everycharacterin T, we compareit with a characteriA at mostonce. This willlead to a ャゥョ ャ・。 イM Mイオ ョウゥョ oH K m) time.

Solution 5.3:This is a specialcaseof applyhga permutatiORwithC.om1mtadditioml storage(cf.Problem114)exceptthat the p ・ イ ュ オ エ 。

Z _ _ _ Q i o ョ Q ウ 。 イ ッ ・ 」 。 s ・ 。

c, ゥ 」 R ゥK」 NNN , (m·i+c) mod n) for 、 ゥ ヲ ヲ ・ イ ・ ョ エ カ 。 ャ セ ・ ウ of cfrom1 エィイッ ィ

IIIber of cycles.SOyesseRtidlyall othercyclesarea shiftedversiORof thefirst cycle-Forexamplerconsiderthecasewhere ] 6 andi = 2.we get (1,3,5) and (2,4,6). Oncewe haveidentified thedifferencebe-tweeIIthelowestmd the seCOI1dlowestdemeI1th anycyclerwe howthenumberof cyclesthereareandtheir startiRgpohts.

Solution5.4: Thekey ideahereis thatif stringA is arotationof anotherstring B, thenA mustbe a substringof B . B. For ・ ク ー i ・ ウ ゥ ョ 」 ・ 」 ゥ ウ

a イッエ。エゥッ ッヲ 」 it is a ウオ「ウエイゥ ッヲ 」 イ」 N Sincesubstringtestcanbe、 o ョ ・ ゥ ョ ャ ゥ ョ ゥ ョ ョ ・ 。

for イ o エ 。 エ ゥ ッ Q in ャ ゥ ョ ・ 。 イ MG M M エ ゥ ュ ャ ・ ッ

Solution5.5: We 。イ・ 。エ providingexplicit solutionto thisproblemheresincethereareno algorithmicideasゥ ョ t ッ ャ カ ・ 、 N Mosttimeswhenthis kindof a questionis asked, youneedto keepa few thingsin mind:

ヲ ウ ゥ ョ ァ ャ ・ pass。 カ ・ イ エ ャ エ イ ゥ

Q ヲ o オ can build ー イ ・ ヲ ゥ tables to match e ャ 、 ・ ク N ィ エ エ ュ ョ ャ 。 ョ ャ 、

セ ・ ヲ ault .ht:n1 in 。 セ カ 。 ョ 」 ・ to speedup theprocess.- You maynot know if youneedto addthep;otocolpart ッ イ 。 エ until

youhavereachedtheeI1dof theAostpart.Hmceit maybea goodideato leavesomespacefor addhgkttp://atthebeghRingofthebuffer.

Solution5.6: This problem 」 。 「 ・ reducedto finding the longestcom-mORSUbsequemebetweeI1thehputstriIIg aI1d its reverse.Wehaveal-readyshownhow this canbedone ・ ヲ ヲ ゥ 」 ゥ ・ イ ャ ケ in Problem3.1.

Solution53:Th!s?mbeemdentIysolveduy dyRamieprogrammgl ・ エ c H I 「 ・ themmII111mwastedspacefor arr-aI1ghgthelast キ ッ イ 、 ウ N Ifwe haveall thevaluesfor C(i) tabulatedfor i \ キ・ cancomputec H Iby finding thenumberof wordswe 」 。 ョ エ in thefirst line thatminimizesc H I .

Solution 5.8: This is anotherinterestingapplicationof dynamicpro-grammmg.

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

158 CHAPTER5. ALGORITHMSONSTRINGS

Let S(i , j) representthesubstringof stringS thatcontainsall thechar-actersof S from indexi to j -1 (inclusive).Let theedit distancebetweenエ ャ キ ッ stringsA andB berepresentedby E(A ,B). Let'ssayエ ィ 。 エ 。 ョ 、 「are, イ・ウー・」エゥカ・ャ エィ・ lengthof ウ エ イ ゥ ァ ウ A andB. Wenowmaketwo claims:

- If a { M 1] = B[b - 1] (i.e., the last two charactersof the stringsmatch), エィ・ eHa B) = eHaHo MQI B(O ,b-l)). This is obviouslytrue sinceany set of transformationthat turns A into B can turna H o Q I into B(O ,b- 1) andvice versa.

i ヲ a { M Q } b [b - 1] (i.e., the last two charactersof エ ィ ・ ウ エ イ ゥ

ョ ッ エ ュ 。 エ 」 ィ I then

E(A ,B) = ゥャャ eHaHo M 1),B) ,E(A, bH KQ

We can seethis to be true by observingthat if there is a smallersequenceof ・ t ・ ョ エ ウ that leadsto the transformationof A into B,theremustbea stepwherethelastcharacterof thestringbecomesthe sameas the last characterof B. This could happeneitherbyinsertinga new characterat the endor deletingthe last character.We canreorderthe sequencesuchthat this operationhappensatthe end. The lengthof the ウ ・ ア オ ・ 」 ・ would remainthe sameandwe would still endup with B in the end. In casethis operationwas B 、 ・ ャ ・ エ ・ G エ ィ ・ ョ by deletingthis ッ ー ・ イ 。 エ ゥ ッ we get a sequenceof operationsthat turn a H o M 1) into B. If this ッ ー ・ イ 。 エ ゥ キ 。 ウ 。 ョ

"insert", thenby droppingthis operation, we would havea setoftransformationsthattum A into B (0,b- 1). In eithercase, it wouldbeacontradictionif therewasa sequenceof operationsthatturned

a ゥ ョ エ ッ b キ ィ ゥ ウ ュ ュ e H a H o a-I) ,B) ,E(A,B(O , 「

KQWe can use the above results to tabulate the values of

E(A(O ,k) ,B(O ,Z)) for all valuesof k \ 。ョ、 Z < b in o H N b) timetill we get thevalueof E(A ,B).

Solution5.9: The key to solving this problemis usingrecursioneffec-エゥ イ・ャケN

If the イ ・ ャ i 。 イ ・ ク ー イ ・ ウ ウ ゥ ッ イ startswith "', then s mustmatchthe re-mainderof r; otherwise, s mustmatchr at someーッ ウ ゥ ッ ョ N

Call the function that checkswhether a string S matchesr fromthe beginningmatchHere. This function has to checkseveralcases-(1.) i・ ァエィMo regularexpressionswhich matcheverything, (2.) a regularexpressionstartingwith a*match, (3.) theregular・ ク ー イ ・ ウ ウ ゥ ッ D and(4.) aイ・ ャi。イ expressionstartingwith analphanumericcharacteror dot.

Of these, (1.) and(3.) arebasecases, (4.) is a checkfollowed by a callto matchHere, and(3.) requiresa newmatchStarfunction.

Q S R R R R R S [ [ R R R R Z [ R Rsuffix matchestheremainderof theregularexpression.

; i ャゥ・ class r ー {

3456789

101112131415161718192021222324252627

s t ati C boolean match(String LStrug s)if ( r . charAt ( 0) == ' t\ ') '{

}return matchHere(r.substriRg(1)FS );

int i = 0;do {

if (matchHere(r , s. substring(i) ) )return true;

} while ( i ++ < s. length() );

return false;

s t ati C boolean matchHere(StringLString s){if (r .length() == 0) {

return true;

8901423456789T

ィアオ

jアオ

jアオアオアjアオ

if((r.length()>=2)&&r.charAt(1)=='*'){}rn matchStar(r.charAt(0), r-511bstring(2), s);

if (r. charAt(0) == '$' && r .length() == 1) {}rn S ·length()==0;

if (s.length() > 0 && (r.charAt(O) ] ] I I r.charAt(O)== s. charAt(O))) {

}return matchHere(r.substriI1g(1), S ·substrMg(1));

return f alse ;

ョu

444

st?tic bookan matchStar(charc , String LString s){int i = 0;do {

if (matchHere(r , s. substring(i) )) {return true;

} キィhセ (i < s.length() && (s.charAt(i++) == c II c ==, ) ) ;

return false;

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

158 CHAPTER5. ALGORITHMSONSTRINGS

Let S(i , j) representthesubstringof stringS thatcontainsall thechar-actersof S from indexi to j -1 (inclusive).Let theedit distancebetweenエ ャ キ ッ stringsA andB berepresentedby E(A ,B). Let'ssayエ ィ 。 エ 。 ョ 、 「are, イ・ウー・」エゥカ・ャ エィ・ lengthof ウ エ イ ゥ ァ ウ A andB. Wenowmaketwo claims:

- If a { M 1] = B[b - 1] (i.e., the last two charactersof the stringsmatch), エィ・ eHa B) = eHaHo MQI B(O ,b-l)). This is obviouslytrue sinceany set of transformationthat turns A into B can turna H o Q I into B(O ,b- 1) andvice versa.

i ヲ a { M Q } b [b - 1] (i.e., the last two charactersof エ ィ ・ ウ エ イ ゥ

ョ ッ エ ュ 。 エ 」 ィ I then

E(A ,B) = ゥャャ eHaHo M 1),B) ,E(A, bH KQ

We can seethis to be true by observingthat if there is a smallersequenceof ・ t ・ ョ エ ウ that leadsto the transformationof A into B,theremustbea stepwherethelastcharacterof thestringbecomesthe sameas the last characterof B. This could happeneitherbyinsertinga new characterat the endor deletingthe last character.We canreorderthe sequencesuchthat this operationhappensatthe end. The lengthof the ウ ・ ア オ ・ 」 ・ would remainthe sameandwe would still endup with B in the end. In casethis operationwas B 、 ・ ャ ・ エ ・ G エ ィ ・ ョ by deletingthis ッ ー ・ イ 。 エ ゥ ッ we get a sequenceof operationsthat turn a H o M 1) into B. If this ッ ー ・ イ 。 エ ゥ キ 。 ウ 。 ョ

"insert", thenby droppingthis operation, we would havea setoftransformationsthattum A into B (0,b- 1). In eithercase, it wouldbeacontradictionif therewasa sequenceof operationsthatturned

a ゥ ョ エ ッ b キ ィ ゥ ウ ュ ュ e H a H o a-I) ,B) ,E(A,B(O , 「

KQWe can use the above results to tabulate the values of

E(A(O ,k) ,B(O ,Z)) for all valuesof k \ 。ョ、 Z < b in o H N b) timetill we get thevalueof E(A ,B).

Solution5.9: The key to solving this problemis usingrecursioneffec-エゥ イ・ャケN

If the イ ・ ャ i 。 イ ・ ク ー イ ・ ウ ウ ゥ ッ イ startswith "', then s mustmatchthe re-mainderof r; otherwise, s mustmatchr at someーッ ウ ゥ ッ ョ N

Call the function that checkswhether a string S matchesr fromthe beginningmatchHere. This function has to checkseveralcases-(1.) i・ ァエィMo regularexpressionswhich matcheverything, (2.) a regularexpressionstartingwith a*match, (3.) theregular・ ク ー イ ・ ウ ウ ゥ ッ D and(4.) aイ・ ャi。イ expressionstartingwith analphanumericcharacteror dot.

Of these, (1.) and(3.) arebasecases, (4.) is a checkfollowed by a callto matchHere, and(3.) requiresa newmatchStarfunction.

Q S R R R R R S [ [ R R R R Z [ R Rsuffix matchestheremainderof theregularexpression.

; i ャゥ・ class r ー {

3456789

101112131415161718192021222324252627

s t ati C boolean match(String LStrug s)if ( r . charAt ( 0) == ' t\ ') '{

}return matchHere(r.substriRg(1)FS );

int i = 0;do {

if (matchHere(r , s. substring(i) ) )return true;

} while ( i ++ < s. length() );

return false;

s t ati C boolean matchHere(StringLString s){if (r .length() == 0) {

return true;

8901423456789T

ィアオ

jアオ

jアオアオアjアオ

if((r.length()>=2)&&r.charAt(1)=='*'){}rn matchStar(r.charAt(0), r-511bstring(2), s);

if (r. charAt(0) == '$' && r .length() == 1) {}rn S ·length()==0;

if (s.length() > 0 && (r.charAt(O) ] ] I I r.charAt(O)== s. charAt(O))) {

}return matchHere(r.substriI1g(1), S ·substrMg(1));

return f alse ;

ョu

444

st?tic bookan matchStar(charc , String LString s){int i = 0;do {

if (matchHere(r , s. substring(i) )) {return true;

} キィhセ (i < s.length() && (s.charAt(i++) == c II c ==, ) ) ;

return false;

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

161

Solution6.3: A naturalapproachto this problemis to build theassign-mentonewarehouseat a time. We canpick thefirst warehouseto bethecity for which the costis minimized-thistakes X H R I time sincewe tryeachcity oneat a time andcheckits distanceto everyothercity.

Let's saywehaveselectedthefirst i-I warehouses{Cl' C2 , . . . ,Ci-l}

andare trying to choosethe i-th warehouse.A reasonablechoicefor Ciis the onethat is the farthestfrom the i-I warehousesalreadychosen.This canalsobecomputedin o H R I time.

Let themaximumdistanceヲ イ ッ ュ ャ ケ remainingcities to a warehousebedm . Thenthecostof this assignmentis dm . Let ebea city thathasthisdistanceto the warehouses.In addition, the m warehousecities are allat leastdm apart;otherwise, we would havechosene 。ョ、 ッエ Cm at them-th selection.

At leasttwo of theseュ Q citieshaveto havethesameclosestware-housein an optimumassignment. Let p,q be two suchcities 。 ョ 、 「 ・thewarehousetheyareclosestto. Sinced(p, アI 、H ーI 、H アI ヲッャᆳ

lows that oneof 、 H ー I or 、 H ア I is not lessthandm /2. Hencethe costof this optimumassignmentis at leastdm /2, soour greedyheuristicpro-ducedan 。 ウ ウ ゥ ァ ョ ュ ・ エ ィ 。 エ is within a factor of two of the optimumcostassignment.

Note that the initial selectionof a warehouseis immaterial for the。 イ ャ ュ ・ ョ エ to work butheuristically, it is betterto choosea centralcity asa startingpoint.

Solution6.4: It is naturalto try andsolve ゥ ウ problemby 、 ゥ カ ゥ 、 ・ 。 ョ 、 ᆳ

conquer, e.g., determinethe minimum number of multiplications foreach-of xk and x30/ k , for different valuesof k. The problemis that thesubproblemsare not independent-wecannotjust add the minimumnumberof multiplicationsfor computingx5 andx6 sincebothmayuse

3Z

Insteadwe resortto branch-and-bound:we maintaina setof partialsolutionswhich we try to extendto the final solution. The key to effi-ciencyis pruningoutpartialsolutionsefficiently.

In our context, a partial ウ ッ ャ オ ッ ョ is a list of ・クーッ ・ョエウ thatwe havealreadycomputed.Notethatin aminimumsolution, wewill neverhaveanelementrepeatedin the list. In addition, it sufficesto considerpartialsolutionsin which the ・ ク ー ッ ・ ョ エ ウ occurin increasingordersinceif k > jand xk occursbeforex j in the chain, then xk could not be usedin the、 ・ イ ゥ カ 。 エ ゥ ッ ッ ヲ x j • Hencewe losenothingby advancingthepositionof x k .

Hereis codethatsolvestheproblem:

I import java.11til. LinkedList;public class MinExp {

public static void main( String [] args ){

OMアオaエfS

Chapter6

Intractability

Solution V t ィ ・ M Q ォ ュ ー ウ 。 」 ォ problemis m Np-completeーイ ッ 「 ャ ・ ᆳHowever the dynamic ー イ ッ ァ イ 。 Q Y solution to the problem

Z W R _ [ R R Z Q R _ z z z [ S Q S Z s j b ゥ ウthanor equalエ ッ N We canusetherecurrence

a H I = max H a H M ャ I ュ H a H I I I .

For P エ A[uj]=0.ComputingAM givenA[ilr for all i \f

ウo H W_ z

エ ・N

ャオエゥッョVNR Z aァ ッッ、 キ。 ケ ヲャ。エ エ・、 ー イッ 「ャ・ュ エ ィ。 エc。ュョ「・ ウッャカ・、・ク。」エャケ・ヲ。」ゥ・ tィ・ュゥョゥュョェine・・HmstIー ャ・ュ hasan ・ ヲ ヲ ゥ ャ エ 。 ャ ァ Z ィ ュ and it yields a キ yof ウゥエゥ achcity exac キ ゥ 」 ・ M ウ エ 。 イ エ at anycity Cand ー ・ ・ イ ヲ G ヲorderwalk ゥ ョ Q エ ィ ・ hM4STwith Castheroot.Thistraversdleadsto apathh

hicheachedgeis visitedexactlytwice.。 エ

エ ッ エ ィ ・ ウ エ 。 イ エ ゥ Q Y city, the イ ・ ュ Q 。 ゥ ョ ャ ゥ ョ ァ setof edges」 ッ イ ャ s エ ゥ ゥ エ オ エ ・ 。 エ イ ・ ・ N Thereforethecostof anytravelhgsalesmmproblemis at leastasgreatasthecost

of エ ィ エ m ヲ ャ ュ 。 ォ ・ オ ウ ・ ャ ・ fact thatthedistances「 ュ s ウ ヲ ゥ ・ S

エイゥ ュゥ Q V ィ・ アオ Qァイ T a ・。 エ・イ エ ィ 。ョ エ ィ・ms tN w ィ・ョ w ・ー ・イヲッイュ イ Q Q

ZZQZQSZZZZRSTRQSWSQエ」イエlエッ U

Efemewehaveatourcosthgatmosthvicethecostof theMST WHet1itself wasanupper「 ッ オ 、 エ ィ ・ costof thetyawliI1g salesmanprobl

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

161

Solution6.3: A naturalapproachto this problemis to build theassign-mentonewarehouseat a time. We canpick thefirst warehouseto bethecity for which the costis minimized-thistakes X H R I time sincewe tryeachcity oneat a time andcheckits distanceto everyothercity.

Let's saywehaveselectedthefirst i-I warehouses{Cl' C2 , . . . ,Ci-l}

andare trying to choosethe i-th warehouse.A reasonablechoicefor Ciis the onethat is the farthestfrom the i-I warehousesalreadychosen.This canalsobecomputedin o H R I time.

Let themaximumdistanceヲ イ ッ ュ ャ ケ remainingcities to a warehousebedm . Thenthecostof this assignmentis dm . Let ebea city thathasthisdistanceto the warehouses.In addition, the m warehousecities are allat leastdm apart;otherwise, we would havechosene 。ョ、 ッエ Cm at them-th selection.

At leasttwo of theseュ Q citieshaveto havethesameclosestware-housein an optimumassignment. Let p,q be two suchcities 。 ョ 、 「 ・thewarehousetheyareclosestto. Sinced(p, アI 、H ーI 、H アI ヲッャᆳ

lows that oneof 、 H ー I or 、 H ア I is not lessthandm /2. Hencethe costof this optimumassignmentis at leastdm /2, soour greedyheuristicpro-ducedan 。 ウ ウ ゥ ァ ョ ュ ・ エ ィ 。 エ is within a factor of two of the optimumcostassignment.

Note that the initial selectionof a warehouseis immaterial for the。 イ ャ ュ ・ ョ エ to work butheuristically, it is betterto choosea centralcity asa startingpoint.

Solution6.4: It is naturalto try andsolve ゥ ウ problemby 、 ゥ カ ゥ 、 ・ 。 ョ 、 ᆳ

conquer, e.g., determinethe minimum number of multiplications foreach-of xk and x30/ k , for different valuesof k. The problemis that thesubproblemsare not independent-wecannotjust add the minimumnumberof multiplicationsfor computingx5 andx6 sincebothmayuse

3Z

Insteadwe resortto branch-and-bound:we maintaina setof partialsolutionswhich we try to extendto the final solution. The key to effi-ciencyis pruningoutpartialsolutionsefficiently.

In our context, a partial ウ ッ ャ オ ッ ョ is a list of ・クーッ ・ョエウ thatwe havealreadycomputed.Notethatin aminimumsolution, wewill neverhaveanelementrepeatedin the list. In addition, it sufficesto considerpartialsolutionsin which the ・ ク ー ッ ・ ョ エ ウ occurin increasingordersinceif k > jand xk occursbeforex j in the chain, then xk could not be usedin the、 ・ イ ゥ カ 。 エ ゥ ッ ッ ヲ x j • Hencewe losenothingby advancingthepositionof x k .

Hereis codethatsolvestheproblem:

I import java.11til. LinkedList;public class MinExp {

public static void main( String [] args ){

OMアオaエfS

Chapter6

Intractability

Solution V t ィ ・ M Q ォ ュ ー ウ 。 」 ォ problemis m Np-completeーイ ッ 「 ャ ・ ᆳHowever the dynamic ー イ ッ ァ イ 。 Q Y solution to the problem

Z W R _ [ R R Z Q R _ z z z [ S Q S Z s j b ゥ ウthanor equalエ ッ N We canusetherecurrence

a H I = max H a H M ャ I ュ H a H I I I .

For P エ A[uj]=0.ComputingAM givenA[ilr for all i \f

ウo H W_ z

エ ・N

ャオエゥッョVNR Z aァ ッッ、 キ。 ケ ヲャ。エ エ・、 ー イッ 「ャ・ュ エ ィ。 エc。ュョ「・ ウッャカ・、・ク。」エャケ・ヲ。」ゥ・ tィ・ュゥョゥュョェine・・HmstIー ャ・ュ hasan ・ ヲ ヲ ゥ ャ エ 。 ャ ァ Z ィ ュ and it yields a キ yof ウゥエゥ achcity exac キ ゥ 」 ・ M ウ エ 。 イ エ at anycity Cand ー ・ ・ イ ヲ G ヲorderwalk ゥ ョ Q エ ィ ・ hM4STwith Castheroot.Thistraversdleadsto apathh

hicheachedgeis visitedexactlytwice.。 エ

エ ッ エ ィ ・ ウ エ 。 イ エ ゥ Q Y city, the イ ・ ュ Q 。 ゥ ョ ャ ゥ ョ ァ setof edges」 ッ イ ャ s エ ゥ ゥ エ オ エ ・ 。 エ イ ・ ・ N Thereforethecostof anytravelhgsalesmmproblemis at leastasgreatasthecost

of エ ィ エ m ヲ ャ ュ 。 ォ ・ オ ウ ・ ャ ・ fact thatthedistances「 ュ s ウ ヲ ゥ ・ S

エイゥ ュゥ Q V ィ・ アオ Qァイ T a ・。 エ・イ エ ィ 。ョ エ ィ・ms tN w ィ・ョ w ・ー ・イヲッイュ イ Q Q

ZZQZQSZZZZRSTRQSWSQエ」イエlエッ U

Efemewehaveatourcosthgatmosthvicethecostof theMST WHet1itself wasanupper「 ッ オ 、 エ ィ ・ costof thetyawliI1g salesmanprobl

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

162

ヲPWGG

uqj

ョu

ゥQゥ

mm

カュ

ュュRRmaemァュュオ

CHAPTER6. INTRACTABILITY

int target = new Integer(args[O]);

LinkedList<Integer> initEntry = new LinkedList<Integer>()

initEntry .add(l);

LinkedList<LinkedList<Integer» partials = new LinkedList<LinkedList<Integer>>0 ;

partials.add(initEntry) ;

LinkedList<Integer> shortestDerivation= null;

int shortestSoFar= Integer.MA)CVALUE;

ョオアゥ

Sa

GィオヲP

アオア、ア、アオアmアオ

Sアオ

キ RT、コ[ [ZZ[ ゥtzH[hゥャエpmエゥ。ャウ removeFirst();for (IRteger i:apartial){

ヲ セ イ H M M i ョ エ ・ ァ ・ イ j: aPartial) {iョエ・ ・イ sum. = 1 + J;if H ウ ^ target ) {

continue;else if ( sui ] target ) {i f(shortestSOFar>apartial-size()){

hortestSOFarzapartia1.size();/ウ ィ ッ イ エ ・ ウ エ d ・ イ ゥ イ 。 エ ゥ ッ ョ = new LinkedList<Integer>,

aPartial );shortestDerivation.add( ウ I ;

continue;lse {if ( aPartial . indexOf H ウ I == -1

&&(aPartial.size(O) < ShOrtestSOFar)F i Sm > 。、p。iイ

+t> n ,;: ;nn = new l ゥ ョ ャ ォ ・ 、 l ゥ ウ エ <LinkedList<Integer> extensIOn = m:vvInteger>( aPartial );

extension.addH s u i I ;partials.add(extension);

8901234533444444

Svstem.out. println ( Baウィッイエ・ウエNNNNNN、・イゥカゥ。エゥッョZB +J shortestDerivation.toString());

4647

The code ms h a fmtiomof a seCOM.It?epirtsH x ャ R N x3 . ク U QP x15,x30). i 。 ャ ャ ケ W S X W ー 。 ヲ エ ゥ 。 ャ solutionsareex G

Z イィ イM

R_Z ZR RZ_ _イ[ Z ケZ tZ_Q[ Zス lオS[ l [i [ _RZZ [ZRR Z] ZZ]Z[Iェエ

163

addition, we couldkeepout duplicatepartialsolutions.The codecouldavoidconsideringall pairsi , j andfocusonpairsthatjustinvolve thelastelementsinceotherpairswill havebeenconsideredpreviously.More so-phisticatedboundingcanbe applied: a chainlike (x ,x2 ,x3 ,x6 ,X 7) willrequireat leastthreemore multiplications (since イ ャ = 3) and so thischaincanbesafelypruned.Whenselectingapartialsolutionto 」 ッ ゥ ョ オ ・

searching from, we couldchooseonethat is promising, e.g., theshortestsolution-thismight lead to bettersolutionsfaster and thereforemoreboundingon othersearchpaths.

For handcalculations, thesetechniquesare importantbut they aretrickier to codeandour original codesolvesthe givenproblemreason-ably quickly.

Solution6.5: A reasonableway to proceedis to use「 イ 。 ョ 」 ィ M 。 ョ 、 M 「 ッ オ イ Z

we chooseavariablev/ seeif thereis a satisfyingassignmentキ ィ ・ ョ ] Pandif not, we try v = 1. If thereis no satisfyingassignmentfor v = 0andfor v = 1/ the ・ ク ー イ ・ ウ ウ ゥ ッ ィ ッ エ satisfiable.

P 」 ・ we choose a variable and set its value, the ・ ク ー イ ・ ウ ウ ゥ ッ

simplifies-weneedto removeclauseswherev appearsif we set ャ

andremoveclauseswhere i appearswhenwe setv = O. In addition, ifwe getto a unit 」 ャ 。 オ ウ ・ ッ ョ ・ wherea singleliteral appears, weknow thatin anysatisfyingassignmentfor thecurrent ・ ク ー イ ・ ウ ウ ゥ ッ エ ィ 。 エ literal mustbesetto true; this rule leadsto additionalsimplification. Conversely, ifall theclausesaretrue, wedonotneedto proceedfurther-everyassign-mentto theremainingvariablesmakestheexpressiontrue.

Therearevariouschoicesfor selectingvariables.Onenaturalchoiceis to pick thevariablewhich appearsthemosttimesin clauseswith twoliterals sinceit leadsto the mostunit clauseson simplification. Anotherchoiceis to pick thevariablewhich is themostbinate-i.e., it appearsthemosttimesin negatedandnonnegatedforms.

Solution6.6: We aregivenasetof N unit durationlecturesandJYI class-rooms. The lecturescanbe held simultaneouslyas long asno two lec-turesneedto happenゥ ョ ・ sameclassroomat thesameエ ゥ ュ ・ ャ 、 all theー イ ・ 」 ・ 、 ・ 」 ・ constraintsaremet.

The problemof schedulingtheselecturesso as to minimize the timeエ 。 ォ ・ エ ッ completionis knownto beNP-complete.

ャ ゥ ウ problemis naturallymodeledusinggraphs.We modellecturesasvertices, with an edgefrom vertexu to vertex ゥ ヲ u is a prerequisitefor v. Clearly, thegraphmustbeacyclicfor theprecedenceconstraintstobesatisfied.

If thereis just one lectureroom, we cansimply hold the lecturesintopologicalorderandcompletetheN lecturesin N time (assumingeachlectureis of unit duration).

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

162

ヲPWGG

uqj

ョu

ゥQゥ

mm

カュ

ュュRRmaemァュュオ

CHAPTER6. INTRACTABILITY

int target = new Integer(args[O]);

LinkedList<Integer> initEntry = new LinkedList<Integer>()

initEntry .add(l);

LinkedList<LinkedList<Integer» partials = new LinkedList<LinkedList<Integer>>0 ;

partials.add(initEntry) ;

LinkedList<Integer> shortestDerivation= null;

int shortestSoFar= Integer.MA)CVALUE;

ョオアゥ

Sa

GィオヲP

アオア、ア、アオアmアオ

Sアオ

キ RT、コ[ [ZZ[ ゥtzH[hゥャエpmエゥ。ャウ removeFirst();for (IRteger i:apartial){

ヲ セ イ H M M i ョ エ ・ ァ ・ イ j: aPartial) {iョエ・ ・イ sum. = 1 + J;if H ウ ^ target ) {

continue;else if ( sui ] target ) {i f(shortestSOFar>apartial-size()){

hortestSOFarzapartia1.size();/ウ ィ ッ イ エ ・ ウ エ d ・ イ ゥ イ 。 エ ゥ ッ ョ = new LinkedList<Integer>,

aPartial );shortestDerivation.add( ウ I ;

continue;lse {if ( aPartial . indexOf H ウ I == -1

&&(aPartial.size(O) < ShOrtestSOFar)F i Sm > 。、p。iイ

+t> n ,;: ;nn = new l ゥ ョ ャ ォ ・ 、 l ゥ ウ エ <LinkedList<Integer> extensIOn = m:vvInteger>( aPartial );

extension.addH s u i I ;partials.add(extension);

8901234533444444

Svstem.out. println ( Baウィッイエ・ウエNNNNNN、・イゥカゥ。エゥッョZB +J shortestDerivation.toString());

4647

The code ms h a fmtiomof a seCOM.It?epirtsH x ャ R N x3 . ク U QP x15,x30). i 。 ャ ャ ケ W S X W ー 。 ヲ エ ゥ 。 ャ solutionsareex G

Z イィ イM

R_Z ZR RZ_ _イ[ Z ケZ tZ_Q[ Zス lオS[ l [i [ _RZZ [ZRR Z] ZZ]Z[Iェエ

163

addition, we couldkeepout duplicatepartialsolutions.The codecouldavoidconsideringall pairsi , j andfocusonpairsthatjustinvolve thelastelementsinceotherpairswill havebeenconsideredpreviously.More so-phisticatedboundingcanbe applied: a chainlike (x ,x2 ,x3 ,x6 ,X 7) willrequireat leastthreemore multiplications (since イ ャ = 3) and so thischaincanbesafelypruned.Whenselectingapartialsolutionto 」 ッ ゥ ョ オ ・

searching from, we couldchooseonethat is promising, e.g., theshortestsolution-thismight lead to bettersolutionsfaster and thereforemoreboundingon othersearchpaths.

For handcalculations, thesetechniquesare importantbut they aretrickier to codeandour original codesolvesthe givenproblemreason-ably quickly.

Solution6.5: A reasonableway to proceedis to use「 イ 。 ョ 」 ィ M 。 ョ 、 M 「 ッ オ イ Z

we chooseavariablev/ seeif thereis a satisfyingassignmentキ ィ ・ ョ ] Pandif not, we try v = 1. If thereis no satisfyingassignmentfor v = 0andfor v = 1/ the ・ ク ー イ ・ ウ ウ ゥ ッ ィ ッ エ satisfiable.

P 」 ・ we choose a variable and set its value, the ・ ク ー イ ・ ウ ウ ゥ ッ

simplifies-weneedto removeclauseswherev appearsif we set ャ

andremoveclauseswhere i appearswhenwe setv = O. In addition, ifwe getto a unit 」 ャ 。 オ ウ ・ ッ ョ ・ wherea singleliteral appears, weknow thatin anysatisfyingassignmentfor thecurrent ・ ク ー イ ・ ウ ウ ゥ ッ エ ィ 。 エ literal mustbesetto true; this rule leadsto additionalsimplification. Conversely, ifall theclausesaretrue, wedonotneedto proceedfurther-everyassign-mentto theremainingvariablesmakestheexpressiontrue.

Therearevariouschoicesfor selectingvariables.Onenaturalchoiceis to pick thevariablewhich appearsthemosttimesin clauseswith twoliterals sinceit leadsto the mostunit clauseson simplification. Anotherchoiceis to pick thevariablewhich is themostbinate-i.e., it appearsthemosttimesin negatedandnonnegatedforms.

Solution6.6: We aregivenasetof N unit durationlecturesandJYI class-rooms. The lecturescanbe held simultaneouslyas long asno two lec-turesneedto happenゥ ョ ・ sameclassroomat thesameエ ゥ ュ ・ ャ 、 all theー イ ・ 」 ・ 、 ・ 」 ・ constraintsaremet.

The problemof schedulingtheselecturesso as to minimize the timeエ 。 ォ ・ エ ッ completionis knownto beNP-complete.

ャ ゥ ウ problemis naturallymodeledusinggraphs.We modellecturesasvertices, with an edgefrom vertexu to vertex ゥ ヲ u is a prerequisitefor v. Clearly, thegraphmustbeacyclicfor theprecedenceconstraintstobesatisfied.

If thereis just one lectureroom, we cansimply hold the lecturesintopologicalorderandcompletetheN lecturesin N time (assumingeachlectureis of unit duration).

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

164 CHAPTER6. INTRACTABILITY165

We candevelopheuristicsby observingthe following: at any time,thereis asetof lectureswhoseーイ ・ 」 ・ 、 ・ 」 ・ constraintshavebeensatisfied.If this setis smallerthanM , we canscheduleall of them;otherwise, weneedto selecta subsetto schedule.

ThesubsetselectioncanbebasedP ウ ・ カ ・ イ 。 ャ metrics:- Rankorderlecturesbasedonthelengthof thelongestdependency

chainthattheyareat thestartof.- RankorderlecturesbasedP エ ィ ・ numberof lecturesthat they are

immediateprerequisitesfor.- Rankorderlecturesbasedonthetotalnumberof lecturesthatthey

aredirector indirectprerequisitesfor.We canalsousecombinationsof thesecriteria to orderthe lecturesthatarecurrentlyschedulable.

For example, for eachvertex, we defineits criticality to be the lengthof a ャッ ァ・ウエ pathfrom it to a sink. We schedulelecturesby processingverticesin topologicalorder. At any ー ッ ャ エ in our algorithm, we haveasetof candidatelectures-thesearethelectureswhoseprerequisiteshavealreadybeenscheduled.

If the candidatesetis lessthansize lvI , we scheduleall the lectures;otherwise, we choosethe lvI most 」 イ ゥ エ ゥ 」 。 ャ ャ ・ 」 エ オ イ ・ ウ ャ 、 schedulethose-theideais thattheyshouldbescheduledsoonersincetheyareat thestartof longerdependencychains.

The criterion is heuristicandmaynot leadto optimumschedules-this is to beexpectedsincetheproblemis NP-complete.Otherheuristicsmaybeemployed, e.g., we mayusethenumberof lecturesthatdependon lecture L as the criticality of lecture L or somecombinationof thecriterion.

Solution6.7: Thisproblemis verysimilarto anotherverypopularprob-lem that is askedin interviews.You are ァ ゥ カ ・ ョ Q ᅲ ュ 。 エ イ ゥ ク in whichboth rows andcolumnsaresortedin ascendingorderandyou aresup-posedto find a givennumberin thematrix.

In this case, we areessentiallylooking for animplicit matrix A suchthat A(i , j) ] ェ S N In our case, the matrix will haven1/3 rows andcolumns.Thereareseveralalgorithmsfor searchingfor anumberin suchamatrix thatarelinearin thenumberof rows.

Oneapproachis to startby comparingx to a Q N If x = An ,l , stop.Otherwise, therearetwo cases:

- x > An ,l , in whichcasex is greaterthanall elementsin Column1.- x < An ,l , in whichcasex is lessthanall elementsin Row n.

In eithercase, wehaveamatrixwith ヲ ・ キ ・ イ elementsto search.In eachiteration, we removea row or a column, which meanswe inspectR M Qelements.

セ I bo?l. i ウ s オ ュ o ヲ 」 ・ ウ H ゥ ョ エ n) {セ I セョセ ] ceil (pow(n, 1/3));セ I ゥ =m; int j = 0;セ I while ( j < ュ ゥ >= 0):J I セエ[ エNセ = hi*i +j*j*j;6 I if (k ] ] I {7 I return true;8 I } else if (k < n) {';J I ++J;

10 I } elseQ Q Q M ゥ [

121314

For a tight lowerbound, let x beanyinput. DefineA to be:

x-Iコ Q

x-Iz Q

Z • Iz Q

x-I

whre ailentri?snot showI1areOWe claim that my algorithm thatsolvesthe matnx searchproblemwill haveto compareZ with eachoftheh-l elementsshowI1(i.e-Fthe diagonaldemeI1tsmd theelemmtsゥ ュ ・ 、 ゥ 。 エ ・ カ 「 ・ ャ ッ キ ttml).calltheseelemmtsthe A ・、ャ・ュ・ ョ

cッュ ー。 イゥャャ QGゥャャ ゥャャ QY x with other ョ ・ ョ エ ウ does ・ ャ ャ ゥ ュ ゥ i i

・ ャ ・ ュ ャ ・ i ャ エ s . supposean algorithm did ROt compare Z with OIIe of the A・ャ・ュ・ョエウN ャ・ キ・ couldmakethatdemeI1tZ (iI1steadof Z-lor コ K ャandthe 。 ャ ァ ッ イ ゥ エ ゥ エ ィ ュ キ ッ オ Q ャ 、 「 ・ ス

i Q Y result. Thereforeat least R M 1 comparesare necessarywhichmeanstht thealgorithmwe desigledis optimum--Notethat for this problemFif thehputrlumberisnr the sizeof thinput is log bits. s ゥ ゥ ョ Q

algorithmin thesizeof the ゥ ョ ー オ エ N

Solution6.8: Ofteninterview ア オ ・ ウ エ ゥ ッ ウ 。 イ ・ ッ ー ・ ョ 、 ・ 、 with nodefinitegoodSOIlItiOII-all youcmdo is providesomegoodhellristiesandcodeit well. For theCollatz ィ ケ ー ッ ・ ウ ゥ ウ the generalideais to startwith eachnumbermditeratetill you reachOIIe.Hereare someof the ideasthatyou cantry to acceleratethecheck:

1.ReusecomputatiORbystorhgall the I1umbersyou havealreadyprovento convergetouthatway}assoOI1asyoureachsuchaI111II1"

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

164 CHAPTER6. INTRACTABILITY165

We candevelopheuristicsby observingthe following: at any time,thereis asetof lectureswhoseーイ ・ 」 ・ 、 ・ 」 ・ constraintshavebeensatisfied.If this setis smallerthanM , we canscheduleall of them;otherwise, weneedto selecta subsetto schedule.

ThesubsetselectioncanbebasedP ウ ・ カ ・ イ 。 ャ metrics:- Rankorderlecturesbasedonthelengthof thelongestdependency

chainthattheyareat thestartof.- RankorderlecturesbasedP エ ィ ・ numberof lecturesthat they are

immediateprerequisitesfor.- Rankorderlecturesbasedonthetotalnumberof lecturesthatthey

aredirector indirectprerequisitesfor.We canalsousecombinationsof thesecriteria to orderthe lecturesthatarecurrentlyschedulable.

For example, for eachvertex, we defineits criticality to be the lengthof a ャッ ァ・ウエ pathfrom it to a sink. We schedulelecturesby processingverticesin topologicalorder. At any ー ッ ャ エ in our algorithm, we haveasetof candidatelectures-thesearethelectureswhoseprerequisiteshavealreadybeenscheduled.

If the candidatesetis lessthansize lvI , we scheduleall the lectures;otherwise, we choosethe lvI most 」 イ ゥ エ ゥ 」 。 ャ ャ ・ 」 エ オ イ ・ ウ ャ 、 schedulethose-theideais thattheyshouldbescheduledsoonersincetheyareat thestartof longerdependencychains.

The criterion is heuristicandmaynot leadto optimumschedules-this is to beexpectedsincetheproblemis NP-complete.Otherheuristicsmaybeemployed, e.g., we mayusethenumberof lecturesthatdependon lecture L as the criticality of lecture L or somecombinationof thecriterion.

Solution6.7: Thisproblemis verysimilarto anotherverypopularprob-lem that is askedin interviews.You are ァ ゥ カ ・ ョ Q ᅲ ュ 。 エ イ ゥ ク in whichboth rows andcolumnsaresortedin ascendingorderandyou aresup-posedto find a givennumberin thematrix.

In this case, we areessentiallylooking for animplicit matrix A suchthat A(i , j) ] ェ S N In our case, the matrix will haven1/3 rows andcolumns.Thereareseveralalgorithmsfor searchingfor anumberin suchamatrix thatarelinearin thenumberof rows.

Oneapproachis to startby comparingx to a Q N If x = An ,l , stop.Otherwise, therearetwo cases:

- x > An ,l , in whichcasex is greaterthanall elementsin Column1.- x < An ,l , in whichcasex is lessthanall elementsin Row n.

In eithercase, wehaveamatrixwith ヲ ・ キ ・ イ elementsto search.In eachiteration, we removea row or a column, which meanswe inspectR M Qelements.

セ I bo?l. i ウ s オ ュ o ヲ 」 ・ ウ H ゥ ョ エ n) {セ I セョセ ] ceil (pow(n, 1/3));セ I ゥ =m; int j = 0;セ I while ( j < ュ ゥ >= 0):J I セエ[ エNセ = hi*i +j*j*j;6 I if (k ] ] I {7 I return true;8 I } else if (k < n) {';J I ++J;

10 I } elseQ Q Q M ゥ [

121314

For a tight lowerbound, let x beanyinput. DefineA to be:

x-Iコ Q

x-Iz Q

Z • Iz Q

x-I

whre ailentri?snot showI1areOWe claim that my algorithm thatsolvesthe matnx searchproblemwill haveto compareZ with eachoftheh-l elementsshowI1(i.e-Fthe diagonaldemeI1tsmd theelemmtsゥ ュ ・ 、 ゥ 。 エ ・ カ 「 ・ ャ ッ キ ttml).calltheseelemmtsthe A ・、ャ・ュ・ ョ

cッュ ー。 イゥャャ QGゥャャ ゥャャ QY x with other ョ ・ ョ エ ウ does ・ ャ ャ ゥ ュ ゥ i i

・ ャ ・ ュ ャ ・ i ャ エ s . supposean algorithm did ROt compare Z with OIIe of the A・ャ・ュ・ョエウN ャ・ キ・ couldmakethatdemeI1tZ (iI1steadof Z-lor コ K ャandthe 。 ャ ァ ッ イ ゥ エ ゥ エ ィ ュ キ ッ オ Q ャ 、 「 ・ ス

i Q Y result. Thereforeat least R M 1 comparesare necessarywhichmeanstht thealgorithmwe desigledis optimum--Notethat for this problemFif thehputrlumberisnr the sizeof thinput is log bits. s ゥ ゥ ョ Q

algorithmin thesizeof the ゥ ョ ー オ エ N

Solution6.8: Ofteninterview ア オ ・ ウ エ ゥ ッ ウ 。 イ ・ ッ ー ・ ョ 、 ・ 、 with nodefinitegoodSOIlItiOII-all youcmdo is providesomegoodhellristiesandcodeit well. For theCollatz ィ ケ ー ッ ・ ウ ゥ ウ the generalideais to startwith eachnumbermditeratetill you reachOIIe.Hereare someof the ideasthatyou cantry to acceleratethecheck:

1.ReusecomputatiORbystorhgall the I1umbersyou havealreadyprovento convergetouthatway}assoOI1asyoureachsuchaI111II1"

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter7

ParallelComputing

INTRACTABILITY

ber, youcanassumeit would reach1.2. In orderto savehashtablespace, you cankeeponly oddnumbers

in thehashtable.3. If youhavetestedeverynumberup to k, youcanstopthechainas

soonas you reacha numberthat is lessthanor equalto k. Also,you do notneedto storethenumbersbelow k in thehashtable, soyou cankeepdeletingthesenumbersfrom the hashtable as youprogress.

4. If multiplication and division are expensive, usebit shifting andaddition.

5. Since the numbersin a sequencemay grow beyond32 bits, youshoulduse64bit longsandkeeptestingfor overflow.

CHAPTER6.166

Solution7.1: ThenaIvesolutionwouldbe:

publk class SIimplementsservlet{SUIng wLast=null:1String [] c1osestToLastWord= null;

Tアma

GS

Q [ _ ゥ _ s s ・ オ ・ ウ エ s ・ イ r ・ ウ ー ッ ョ ウ ・

イ ・ ッ イ エ [ Z R R イ 」 Z R [ [ _ ッ ュ r エ H イ ・ ア I ;

ス[_[[、ヲiiQ ・ウーッョウ・ ( resp, ・ウ l [

wLast = w;

}closestTOLastWordzclosestIRDictionary(W);

678901234

MaQTQTQa

21iS SOIlltiOI1has a race cor1ditiOR-ThreadA might havewritteI1WLast md thenThreadB readswLast arld closestTOLastwordbe"fore ThreadA hasa chmceto updateclosestTOLastkford-Theail!o closestTOLastwordcould take ア オ ゥ エ ・ i o ァ ッ イ 「 ・ カ ・ ヲ 。 e sュ ァ ッ エ ィ ・ i ・ i エ ィ of 」 ォ e CkWOr d Hence it lsqlut possible tkatbetweeI1

RZ[Z[ [[Z[[ _ュ ofTfmdAJfmd b Z ・ 。 、 ウ m w l 。 ウ エ 、a ュ s s ッ ャ オ エzed; h is case, only ィ イ ・ 。 、 」 o オ Q ャ 、 be e・ク・」オ ・ュ cオ Q エ ゥォィji Q

QjZZZZZzZ[ ZエZ[ [エZ ZRw ョ Q エ ・エ o ウエ 。 ョ、s N wッ tッイ 、N Thisatime.e• ody OReservletthreadcmbe executingat

[ Z Z e ゥ Z [ Q R R R エ z z z j u z z e

Solution6.9: Thebrute-forcesolutionis to considerall pairsof points:ウ yieldsan0 H 。ャァッイゥエィュN

A reasonableapproachis to split thepointsinto two equal-sizedsetsusinga line x = P parallel to the Y-axis. Sucha line canbe found bycomputingthe medianof the valuesfor the x co-ordinates-thiscalcu-lation canbeperformedusingrandomizationin a manneranalogoustoQuicksort.

We canthencomputetheclosestpair of pointsrecursivelyonthetwosets;let theclosestpairof pointsontheleft of P bedz 。ー。イエ ャ、 theclosestpair of pointsto theright of P bedr apart. Let d = min(dz,dr ).

Now, all we needto look at is points which are in the band [P -d,P + d]. In degeneratesituations, all pointsmaybe within this band.s ッ キ ・ compareall thepairs, thecomplexitybecomesquadraticagain.Howeverwe cansort thepointsin thebandon their ケ ・ ッ M ッ イ 、 ゥ ョ 。 エ ・ ウ ャ 、

scanthe sortedlist, looking for pointsd or lessdistancefrom the ー ッ ャ エ

「 ・ Q Y processed.Intuitively, therecannotbe a largenumberof such ー ッ ャ エ s sinceoth-

・ イ キ ゥ ウ ・ ャ ・ closestpair in the left andright partitionswould haveto beless than d apart. ャゥウ intuition canbe analytically ェ オ ウ エ ゥ ・ 、 M s ィ ッ ウ

andHoey'sfamous1975paper"Closest-pointproblems"showsthatnomorethan6 pointscanbewithin d distanceof anypointwhich leadstoan0 (n log n) algorithm-thetime is dominatedby theneedto sort.

The recursioncanbe spedup by switching to brute-forcewhen asmallnumberof pointsremain.

Solution6.10: Hereareacoupleof simpleheuristicsthatyoucanusetospeedup primality tests:

1. It is ウ オ ヲ 、 ・ ョ エ to testfor ヲ 。 」 エ ッ イ ゥ コ ゥ ッ ョ オ ー to rv QM2. You canlimit yourselfto primenumbersonly. You maynot know

all theprimenumbersbetween2 andvn, howeveryoucanusethefact thatall primenumbersッ エ ィ ・ イ 。 ョ 2 and3 areof theform V ォ ャ

ッ イ Q N This would speedup your computationby a factor of 3.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter7

ParallelComputing

INTRACTABILITY

ber, youcanassumeit would reach1.2. In orderto savehashtablespace, you cankeeponly oddnumbers

in thehashtable.3. If youhavetestedeverynumberup to k, youcanstopthechainas

soonas you reacha numberthat is lessthanor equalto k. Also,you do notneedto storethenumbersbelow k in thehashtable, soyou cankeepdeletingthesenumbersfrom the hashtable as youprogress.

4. If multiplication and division are expensive, usebit shifting andaddition.

5. Since the numbersin a sequencemay grow beyond32 bits, youshoulduse64bit longsandkeeptestingfor overflow.

CHAPTER6.166

Solution7.1: ThenaIvesolutionwouldbe:

publk class SIimplementsservlet{SUIng wLast=null:1String [] c1osestToLastWord= null;

Tアma

GS

Q [ _ ゥ _ s s ・ オ ・ ウ エ s ・ イ r ・ ウ ー ッ ョ ウ ・

イ ・ ッ イ エ [ Z R R イ 」 Z R [ [ _ ッ ュ r エ H イ ・ ア I ;

ス[_[[、ヲiiQ ・ウーッョウ・ ( resp, ・ウ l [

wLast = w;

}closestTOLastWordzclosestIRDictionary(W);

678901234

MaQTQTQa

21iS SOIlltiOI1has a race cor1ditiOR-ThreadA might havewritteI1WLast md thenThreadB readswLast arld closestTOLastwordbe"fore ThreadA hasa chmceto updateclosestTOLastkford-Theail!o closestTOLastwordcould take ア オ ゥ エ ・ i o ァ ッ イ 「 ・ カ ・ ヲ 。 e sュ ァ ッ エ ィ ・ i ・ i エ ィ of 」 ォ e CkWOr d Hence it lsqlut possible tkatbetweeI1

RZ[Z[ [[Z[[ _ュ ofTfmdAJfmd b Z ・ 。 、 ウ m w l 。 ウ エ 、a ュ s s ッ ャ オ エzed; h is case, only ィ イ ・ 。 、 」 o オ Q ャ 、 be e・ク・」オ ・ュ cオ Q エ ゥォィji Q

QjZZZZZzZ[ ZエZ[ [エZ ZRw ョ Q エ ・エ o ウエ 。 ョ、s N wッ tッイ 、N Thisatime.e• ody OReservletthreadcmbe executingat

[ Z Z e ゥ Z [ Q R R R エ z z z j u z z e

Solution6.9: Thebrute-forcesolutionis to considerall pairsof points:ウ yieldsan0 H 。ャァッイゥエィュN

A reasonableapproachis to split thepointsinto two equal-sizedsetsusinga line x = P parallel to the Y-axis. Sucha line canbe found bycomputingthe medianof the valuesfor the x co-ordinates-thiscalcu-lation canbeperformedusingrandomizationin a manneranalogoustoQuicksort.

We canthencomputetheclosestpair of pointsrecursivelyonthetwosets;let theclosestpairof pointsontheleft of P bedz 。ー。イエ ャ、 theclosestpair of pointsto theright of P bedr apart. Let d = min(dz,dr ).

Now, all we needto look at is points which are in the band [P -d,P + d]. In degeneratesituations, all pointsmaybe within this band.s ッ キ ・ compareall thepairs, thecomplexitybecomesquadraticagain.Howeverwe cansort thepointsin thebandon their ケ ・ ッ M ッ イ 、 ゥ ョ 。 エ ・ ウ ャ 、

scanthe sortedlist, looking for pointsd or lessdistancefrom the ー ッ ャ エ

「 ・ Q Y processed.Intuitively, therecannotbe a largenumberof such ー ッ ャ エ s sinceoth-

・ イ キ ゥ ウ ・ ャ ・ closestpair in the left andright partitionswould haveto beless than d apart. ャゥウ intuition canbe analytically ェ オ ウ エ ゥ ・ 、 M s ィ ッ ウ

andHoey'sfamous1975paper"Closest-pointproblems"showsthatnomorethan6 pointscanbewithin d distanceof anypointwhich leadstoan0 (n log n) algorithm-thetime is dominatedby theneedto sort.

The recursioncanbe spedup by switching to brute-forcewhen asmallnumberof pointsremain.

Solution6.10: Hereareacoupleof simpleheuristicsthatyoucanusetospeedup primality tests:

1. It is ウ オ ヲ 、 ・ ョ エ to testfor ヲ 。 」 エ ッ イ ゥ コ ゥ ッ ョ オ ー to rv QM2. You canlimit yourselfto primenumbersonly. You maynot know

all theprimenumbersbetween2 andvn, howeveryoucanusethefact thatall primenumbersッ エ ィ ・ イ 。 ョ 2 and3 areof theform V ォ ャ

ッ イ Q N This would speedup your computationby a factor of 3.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution7.3: Our strategyis to launcha threadT per Requestorob-ject. ThreadT in turn launchesanotherthread, S, which calls executeand p イ ッ 」 ・ ウ ウ r ・ ウ ー ッ ウ ・ N The call to executein S is wrappedin a try-catchInterruptedExceptionloop; if executecompletessuccessfully,ProcessResponseis calledon the result.

After launchingS, T sleepsfor the timeoutinterval-whenit wakesup, it interruptsS. If S hascompleted, nothinghappens;otherwise, thetry-catch i ョ エ ・ イ イ オ ー エ ・ 、 e ク 」 ・ ー エ ゥ ッ 」 。 ャ ャ ウ error.

Codefor this is givenbelow:

The problemwith this approachis that we do not control the num-ber of threadslaunched.A threadconsumesa nontrivial amountof re-sourcesby itself-thereis the overheadof startingandendingdownthethreadandtheresourcesusedby thethread.For a lightly-loaded ウ ・ イ カ ・this maynot be an issuebut underload, it canresult in exceptionsthatarechallenging, if not impossible, to handle.

Theright trade-offis to usea threadpool. As thenameimplies, this is acollectionof threads, thesizeof which is bounded.JavaprovidesthreadpoolsthroughtheExecutorframework.

169

public static void main(String[] args) throws IOException {ServerSocketsocket = new ServerSocket(80) ;while (true) {

final Socket connection = socket.accept() ;Runnable task = new Runnable() {

public void run () {handleRequest(connection);

public String execute(String req I long delay) {try {

Thread.sleep(delay);} catch (InterruptedExceptione) {

public String error(String req) {return "response:"+ req + BZB Be jutB ;

class TaskExecutionWebServer{private static final int n i ャ e a N ヲ ヲ ゥ = 100;private static final Executor exec

= Executors.ョ ・ キ f ゥ ク ・ 、 ィ イ ・ 。 、 p ッ ッ ャ H n i ャ e a N ヲ ヲ ゥ I ;

class Requestor {public String execute(String req) {

return "response:"+ req;

.,ヲ

kcatl

eeエGELexeFL

It'e

-FXウjol

ebej

})

Qゥ

Tアオa

fSau

uqjョオゥ

Ma

123456789012345678

QT

ゥQTQTQaQF

eaQaTゥ

Solution7.2:Thefirst attemptto solvethis problemmight be to havemaialauncha new threadper requestratherthmprocessthe request

itself:

h the above codey multiple servlets can be h their call toClosestIEDictioaazywhich is goodbecausethecallmaytakea lOI1gtime. Becausewe lock P エ ィ ゥ ウ イ the read-assigIImentOR a hit and

ass?meI1ton completiOI1areatomic.Notethat we haveイ ゥ Z l s z l m ッ イ 、 when 。 ウ ウ ゥ ァ エ ッ ・ エ 」 ・ P ᆳwiser closestTOLastwordmight chmgebeforewe encodeit into the

response.

PARALLELCOMPUTING

class ThreadperTaskvvebServer{public static void main(Stringuargs)throwsIOExceptIOI1

I S… owhile ( true ) {

final Socket cOI1nection=socket.accept();RunRable task =new Runnable(){

public void イ ュ o サ NhandleRequest( connectlon)

public void service(ServletRequestreq I ServletResponse

esp) {String w=extractFromRequest(req);String result = null;synchronized(this){

if (w.equals(wLast)) {イセGセセャH = cl'osestToLastWord.clone() ;

ゥlヲH 」ャッウ・ウ l。ウ エ イ 、 == null) {result =closestInDictionary(i);synchronized (this) {

c1osestlnDictionary= result;wLast = w;

l i c ッ ャ エ ッ r ・ ウ ー ッ ョ ウ ・ H ャ エ I ;

public class S2 implements Servlet {String wLast = null;ウ セ [ ゥ セ ァ [] closestToLastWord= null;

CHAPTER7.

;ew Thrmd(tMK)start();

STUVWXY

オョオ

12

VWXY

オョオ

オュuu

ュョョ

168

ST

fd

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution7.3: Our strategyis to launcha threadT per Requestorob-ject. ThreadT in turn launchesanotherthread, S, which calls executeand p イ ッ 」 ・ ウ ウ r ・ ウ ー ッ ウ ・ N The call to executein S is wrappedin a try-catchInterruptedExceptionloop; if executecompletessuccessfully,ProcessResponseis calledon the result.

After launchingS, T sleepsfor the timeoutinterval-whenit wakesup, it interruptsS. If S hascompleted, nothinghappens;otherwise, thetry-catch i ョ エ ・ イ イ オ ー エ ・ 、 e ク 」 ・ ー エ ゥ ッ 」 。 ャ ャ ウ error.

Codefor this is givenbelow:

The problemwith this approachis that we do not control the num-ber of threadslaunched.A threadconsumesa nontrivial amountof re-sourcesby itself-thereis the overheadof startingandendingdownthethreadandtheresourcesusedby thethread.For a lightly-loaded ウ ・ イ カ ・this maynot be an issuebut underload, it canresult in exceptionsthatarechallenging, if not impossible, to handle.

Theright trade-offis to usea threadpool. As thenameimplies, this is acollectionof threads, thesizeof which is bounded.JavaprovidesthreadpoolsthroughtheExecutorframework.

169

public static void main(String[] args) throws IOException {ServerSocketsocket = new ServerSocket(80) ;while (true) {

final Socket connection = socket.accept() ;Runnable task = new Runnable() {

public void run () {handleRequest(connection);

public String execute(String req I long delay) {try {

Thread.sleep(delay);} catch (InterruptedExceptione) {

public String error(String req) {return "response:"+ req + BZB Be jutB ;

class TaskExecutionWebServer{private static final int n i ャ e a N ヲ ヲ ゥ = 100;private static final Executor exec

= Executors.ョ ・ キ f ゥ ク ・ 、 ィ イ ・ 。 、 p ッ ッ ャ H n i ャ e a N ヲ ヲ ゥ I ;

class Requestor {public String execute(String req) {

return "response:"+ req;

.,ヲ

kcatl

eeエGELexeFL

It'e

-FXウjol

ebej

})

Qゥ

Tアオa

fSau

uqjョオゥ

Ma

123456789012345678

QT

ゥQTQTQaQF

eaQaTゥ

Solution7.2:Thefirst attemptto solvethis problemmight be to havemaialauncha new threadper requestratherthmprocessthe request

itself:

h the above codey multiple servlets can be h their call toClosestIEDictioaazywhich is goodbecausethecallmaytakea lOI1gtime. Becausewe lock P エ ィ ゥ ウ イ the read-assigIImentOR a hit and

ass?meI1ton completiOI1areatomic.Notethat we haveイ ゥ Z l s z l m ッ イ 、 when 。 ウ ウ ゥ ァ エ ッ ・ エ 」 ・ P ᆳwiser closestTOLastwordmight chmgebeforewe encodeit into the

response.

PARALLELCOMPUTING

class ThreadperTaskvvebServer{public static void main(Stringuargs)throwsIOExceptIOI1

I S… owhile ( true ) {

final Socket cOI1nection=socket.accept();RunRable task =new Runnable(){

public void イ ュ o サ NhandleRequest( connectlon)

public void service(ServletRequestreq I ServletResponse

esp) {String w=extractFromRequest(req);String result = null;synchronized(this){

if (w.equals(wLast)) {イセGセセャH = cl'osestToLastWord.clone() ;

ゥlヲH 」ャッウ・ウ l。ウ エ イ 、 == null) {result =closestInDictionary(i);synchronized (this) {

c1osestlnDictionary= result;wLast = w;

l i c ッ ャ エ ッ r ・ ウ ー ッ ョ ウ ・ H ャ エ I ;

public class S2 implements Servlet {String wLast = null;ウ セ [ ゥ セ ァ [] closestToLastWord= null;

CHAPTER7.

;ew Thrmd(tMK)start();

STUVWXY

オョオ

12

VWXY

オョオ

オュuu

ュョョ

168

ST

fd

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

170 CHAPTER7. PARALLELCOMPUTING

Tアua

G「ヲッ

GG

uqjョオMMa

OmアオaエfSヲo

uqjョオMMWB

Sa

fd

uqjョオMMF

Sa

GS

uqjョオ

ィアmaエ

1111111122222222223333333333444444444455555return error(req) ;

return execute(req);

public void ProcessResponse(String response) {System.out. println H B p イ ッ 」 ・ ウ ウ r ・ ウ ー ッ ョ ウ ・ Z B イ ・ ウ ー ッ ョ ウ ・ I ;return;

public class AsyncThread {public static final long b o u t = 500L;public stati c void main(String [] args) {

Dispatch(new Requestor() I "tl" I 1000L);Dispatch(new Requestor() I "t2" I 100L);Dispatch (new Requestor() I "t3" I 10L);Dispatch(new Requestor() I "t4" IlL) ;Dispatch(new Requestor() I "t5" I 200L);

public static void Dispatch(final Requestor r I final String requestIfinal long delay) {

Runnable task = new Runnable() {public void run() {

Runnable actuaITask= new Runnable() {public void run () {

String response= r. execute(requestI delay);r. ProcessResponse( response);

Thread innerThread= new Thread(actuaITask);innerThread.start() ;try {

Thread.sleepH b e o u t I ;innerThread.interrupt() ;

} catch(InterruptedExceptione)e. printStackTrace() ;

new Thread(task). start() ;

Solution 7.4: There are two aspectsto the 、 ・ ウ ゥ ァ ョ M ヲ ゥ イ ウ エ ィ ・ data-structuresandsecond, thelockingmechanism.

Onesolutionis to usetwo data-structures.Thefirst is aheapin whichwe insertkey-valuepairs: the keys areruntimesandthevaluesarethethreadto runat thattime. A dispatchthreadrunsthesethreads;it sleepsfrom call to call andmaybewokenup if a threadis addedto or deletedfrom the pool. If wokenup, it advancesor retardsits remainingsleeptimebasedon the top of theheap.Onwaking オ ー ャ ッ ッ ォ ウ for the thread

171

at thetop of theheap-ifits launchエ ョ ・ is thecurrenttime, thedispatchthread 、 ・ ャ ・ エ ・ ウ ヲ イ ッ ュ the heapmd executesit It then sleepstill thelaURchtime for themxt thyeadh theheap-(Becauseof deletiom+itmavhappenthatthedispatchthreadキ 。 ォ ・ ウ 。 m ヲ ゥ i i 、 ウ ュ エ ィ ゥ ョ ァ エ ッ 、 ッ ェ J

U1esecOI1ddata-structureis a hashtablewith threadids askeysandmtrieshthehe?pasvalues IfwemedtocamelathreadFWego totheheapmddelete1t Eachtheathreadis addedrwehsertit htothehmif theinsertionis to the top of the ィ ・ 。 ー t ・ interruptthe 、 ゥ ウ ー 。 j jsothatit can 。 、 ェ エ Z ゥ エ ウ wakeup time.

shcethe heapis sharedby the updatemethodsmd the dispatchthreadywemedto lock it The simplestsolutioz1isto havea singlelockthatis usedfor all readmdwriteshtotheheapmdtEMhashtable.

Solution75:WewaIIt to kableto iRdicatewhetherthestringis behgad aswell aswhetherthe ウ ィ ァ is beingwritteI1to.weachievethis

with a pair of locks-LRmdLw mda readComterlockedby LRA readerproceedsas follows:it locks LRr hcremeRtsthe count

。 ョ 、 ャ ・ 。 ウ ・ ウ LR Afteritperfomsits mdsritlocks LR deementstjComterraMreleasesLR A writer locks LWr hm itedtively performithe followhg:it locks LR7checksto seeif the readCOIlIIter-is O;if so, itperformsits write, releasesLW, andthenreleasesLR. In code: I --- ---,1 I import ェ 。 カ 。 N ゥ ャ N d 。 エ ・ [

2 I import java.uti I .Random;3 I

4 I class Reader extends Thread56 I public void run() {7 I while ( true ) {8 synchronized H l r I

Y I イ・。、cッオョエKK[1011121314151617181920 I class Writer extends Thread212223242526272829

System.out. println H v N data );synchronized H v N l r I {

tN イ・。、cッオョエ [

doSomeThingElse() ;

public void run () {while ( true ) {

synchronized H Q N l w I {synchronized (RW. LR ) {

if H v N readCount== 0) {ス vN data = new Date() . toString() ;

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

170 CHAPTER7. PARALLELCOMPUTING

Tアua

G「ヲッ

GG

uqjョオMMa

OmアオaエfSヲo

uqjョオMMWB

Sa

fd

uqjョオMMF

Sa

GS

uqjョオ

ィアmaエ

1111111122222222223333333333444444444455555return error(req) ;

return execute(req);

public void ProcessResponse(String response) {System.out. println H B p イ ッ 」 ・ ウ ウ r ・ ウ ー ッ ョ ウ ・ Z B イ ・ ウ ー ッ ョ ウ ・ I ;return;

public class AsyncThread {public static final long b o u t = 500L;public stati c void main(String [] args) {

Dispatch(new Requestor() I "tl" I 1000L);Dispatch(new Requestor() I "t2" I 100L);Dispatch (new Requestor() I "t3" I 10L);Dispatch(new Requestor() I "t4" IlL) ;Dispatch(new Requestor() I "t5" I 200L);

public static void Dispatch(final Requestor r I final String requestIfinal long delay) {

Runnable task = new Runnable() {public void run() {

Runnable actuaITask= new Runnable() {public void run () {

String response= r. execute(requestI delay);r. ProcessResponse( response);

Thread innerThread= new Thread(actuaITask);innerThread.start() ;try {

Thread.sleepH b e o u t I ;innerThread.interrupt() ;

} catch(InterruptedExceptione)e. printStackTrace() ;

new Thread(task). start() ;

Solution 7.4: There are two aspectsto the 、 ・ ウ ゥ ァ ョ M ヲ ゥ イ ウ エ ィ ・ data-structuresandsecond, thelockingmechanism.

Onesolutionis to usetwo data-structures.Thefirst is aheapin whichwe insertkey-valuepairs: the keys areruntimesandthevaluesarethethreadto runat thattime. A dispatchthreadrunsthesethreads;it sleepsfrom call to call andmaybewokenup if a threadis addedto or deletedfrom the pool. If wokenup, it advancesor retardsits remainingsleeptimebasedon the top of theheap.Onwaking オ ー ャ ッ ッ ォ ウ for the thread

171

at thetop of theheap-ifits launchエ ョ ・ is thecurrenttime, thedispatchthread 、 ・ ャ ・ エ ・ ウ ヲ イ ッ ュ the heapmd executesit It then sleepstill thelaURchtime for themxt thyeadh theheap-(Becauseof deletiom+itmavhappenthatthedispatchthreadキ 。 ォ ・ ウ 。 m ヲ ゥ i i 、 ウ ュ エ ィ ゥ ョ ァ エ ッ 、 ッ ェ J

U1esecOI1ddata-structureis a hashtablewith threadids askeysandmtrieshthehe?pasvalues IfwemedtocamelathreadFWego totheheapmddelete1t Eachtheathreadis addedrwehsertit htothehmif theinsertionis to the top of the ィ ・ 。 ー t ・ interruptthe 、 ゥ ウ ー 。 j jsothatit can 。 、 ェ エ Z ゥ エ ウ wakeup time.

shcethe heapis sharedby the updatemethodsmd the dispatchthreadywemedto lock it The simplestsolutioz1isto havea singlelockthatis usedfor all readmdwriteshtotheheapmdtEMhashtable.

Solution75:WewaIIt to kableto iRdicatewhetherthestringis behgad aswell aswhetherthe ウ ィ ァ is beingwritteI1to.weachievethis

with a pair of locks-LRmdLw mda readComterlockedby LRA readerproceedsas follows:it locks LRr hcremeRtsthe count

。 ョ 、 ャ ・ 。 ウ ・ ウ LR Afteritperfomsits mdsritlocks LR deementstjComterraMreleasesLR A writer locks LWr hm itedtively performithe followhg:it locks LR7checksto seeif the readCOIlIIter-is O;if so, itperformsits write, releasesLW, andthenreleasesLR. In code: I --- ---,1 I import ェ 。 カ 。 N ゥ ャ N d 。 エ ・ [

2 I import java.uti I .Random;3 I

4 I class Reader extends Thread56 I public void run() {7 I while ( true ) {8 synchronized H l r I

Y I イ・。、cッオョエKK[1011121314151617181920 I class Writer extends Thread212223242526272829

System.out. println H v N data );synchronized H v N l r I {

tN イ・。、cッオョエ [

doSomeThingElse() ;

public void run () {while ( true ) {

synchronized H Q N l w I {synchronized (RW. LR ) {

if H v N readCount== 0) {ス vN data = new Date() . toString() ;

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

172 CHAPTER7. PARALLELCOMPUTING173

doSomeThingElse() ;}

static String data = new Date() . toString() ;stati c Random random = new Random() ;

static Object LR = new Object();static int readCount= 0;static Object LW = new Object();

Solution7.9:A casualimplemeI1tationis susceptibleto races.Forex-mPIera IIew customerseesthebarbercuttiIIg hair andgoesto thewaitw

ing room; 「 ・ ヲ ッ ォ ・ getsto the chairr thebaybercompletesthe ィ 。 ゥ イ 」 オ エchecksthe キ 。 ゥ エ ゥ ョ Q Y r‘ o ッュ Q 。ョ ャ、 ァ ッ・ ウ backto his chair エ ッ ウ ャ ・ ・ ー N This iform of livelock-thebarbermdthecustomerarebothidler waithgforeachother. As another・ ク ョ ー ャ ・ in the absenceof appropriatelockingtwo customersmay arrive ウ ゥ ュ エ _ ュ ッ オ ウ Q S U ウ ・ ・ the barbercutting ha?nda siIIglevacmtseath thewaitmgroomyaz1dgo to thewaithgroom

occupythesinglechair.ORewayto achievecorrectoperatioI1isto havea singlemutexwhich

llows mly ompersmtocharIgestateata time.TKhyber-mustacrmirpthe mutexbeforecheckhgfor customers;hemust releaseit wrJ;Lieitherbeghsto sleepor beginsto cuthair.A customermustacquirethemutexbeforeeI1terhgtke shop;hemustreleaseitwhmhesitsh eithera waiting roomchairor thebarberchair.

For a completesolution, in 。 、 、 ゥ エ ゥ ッ エ ッ the mutex, we needeventsemaphoresto recordthenumberof customersin エZ ィ ・ 。 ゥ エ ゥ ョ ァ イ ッ ッ ュ 。 ョ Qエ ィ ・ イ ョ オ オ イ ョ ュ ャ 「 ・ イ ‘ ッヲ ー・ッー i・ gettingtheir ィ 。 。 ゥ イ ‘ cut. Theeventsemaphorerecord-hgtheI1umberof customersh thewaitingroomis usedto wakeup thebarberwhemcustomerente?s;theeveI1tsemaphorerecordhgthemm-beyof customersgettingaka1rcutis usedto wakeupwaithgcustomers.

Solution7.10:TheI1aturalSOIl1tiORisfor eachresourceto havea lock.TheproblemariseswheReachthreadi requestslock i mdthm エ Q modN Sincealllockshavealreadybeenacquired, the threaddeadlocks

une approachisphavea mtral 」 o i Q エ イ ッ ャ ャ ・ キ ィ ゥ 」 ィ knows exactlywhich resourcesare in use and arbitrates 」 ッ ヲ ャ ゥ 」 エ ゥ ョ ァ requests. If イ ・

cesarem t ayailaple for a thready the c ッ イ Q エ イ ッ ャ ャ ・ イ 」 。 ・ ェ ・ 」 エ ィ ゥ ウ イ ・ ア オ ャ ・ s エ Nb ai ィ ・ イウo ャオエゥッ is エ P P イ 、 ・ i

e 。 」ア オゥ イ・、 in ゥ ョ 」 イ ・ 。 ウ ゥ ョ ァ order 。 ョ ャ 、 releasedin 、 ・ 」 イ ・ ・ 。 ウ ゥ ョ Q Y order. ForpIe, if all threadsrequestsimultaneously, resourceM 1will beleft

unrequested(sinceThread M 1 will request0 first, 。 i ャ、 then Qt Q ャ イ ・ 。

Q will block ッ イ Q ResourceO.

ャゥウ solutionis not ウ エ 。 イ イ 。 エ ゥ ッ ョ M ヲ イ ・ ・ e.g., T2 canwait foreverwhile T1and T3 alternate. t ッ 。 イ 。 ョ エ ・ ・ that no threadstarves, one could keeptrackof theI1umberof timesa threadcaIUIOtexecutewhenhisIIeighb

leasetheir locks. If this numberex

public static void main( String [] args ) {Thread rO = new Reader(); Thread rl = new Reader() ;Thread wo = new Writer (); Thread wi = new Writer () ;rO. start(); rl. start() ;wO. start (); w1. start () ;while ( true );

Solution7.6: Wewantto givewritersthepreference.Weachievethisbymodifying the solutionaboveto havea readerstartby locking LW andthenimmediatelyreleasingLW. In this way, a writer who acquirestheLW lock is guaranteedto 「 ・ ャ ・ 。 、 of thesubsequentreaders.

Solution7.7: We canachievefairnessbetweenreadersandwriters byhavingabit which indicateswhethera reador a write wasthelastoper-ationperformed.If thelastoperationperformedwasa read, a readeronacquiringa lock mustreleasethe lock andretry-thisgiveswriters pri-ority in acquiringthelock; a similar ッ ー ・ イ 。 エ ゥ ゥ ウ performedby writers.

Solution7.8: This problem canbe solvedfor a singleproducerand asingleconsumerwith apairof semaphores• llCount is incrementedandemptyCountis decrementedwheneveranitem is addedto thebuffer. Ifthe producerwants to decrementemptyCountwhen its count is 0, theproducersleeps.The next time an item is consumed, emptyCountis in-crementedandtheproduceris wokenup. Theconsumeroperatesanalo-ァ ッ オ ウ ャ t ィ ・ Javamethods, wait andnotify, canbeusedto implementthedesiredfunctionality.

If there are multiple producersand consumers, the solution abovehastwo races-twoproducerscantry writing to the sameslot andtwoconsumerscanreadfrom the sameslot. Theseracescanberemovedbyaddingmutexesaroundthe putItemlntoBufferand イ ・ ュ ッ カ ・ i エ ・ ュ f イ ッ ュ b オ Z G

calls.

30313233343536 Ipublic 」 ャ 。 ウ ウ サ37383940414243444546474849505152

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

172 CHAPTER7. PARALLELCOMPUTING173

doSomeThingElse() ;}

static String data = new Date() . toString() ;stati c Random random = new Random() ;

static Object LR = new Object();static int readCount= 0;static Object LW = new Object();

Solution7.9:A casualimplemeI1tationis susceptibleto races.Forex-mPIera IIew customerseesthebarbercuttiIIg hair andgoesto thewaitw

ing room; 「 ・ ヲ ッ ォ ・ getsto the chairr thebaybercompletesthe ィ 。 ゥ イ 」 オ エchecksthe キ 。 ゥ エ ゥ ョ Q Y r‘ o ッュ Q 。ョ ャ、 ァ ッ・ ウ backto his chair エ ッ ウ ャ ・ ・ ー N This iform of livelock-thebarbermdthecustomerarebothidler waithgforeachother. As another・ ク ョ ー ャ ・ in the absenceof appropriatelockingtwo customersmay arrive ウ ゥ ュ エ _ ュ ッ オ ウ Q S U ウ ・ ・ the barbercutting ha?nda siIIglevacmtseath thewaitmgroomyaz1dgo to thewaithgroom

occupythesinglechair.ORewayto achievecorrectoperatioI1isto havea singlemutexwhich

llows mly ompersmtocharIgestateata time.TKhyber-mustacrmirpthe mutexbeforecheckhgfor customers;hemust releaseit wrJ;Lieitherbeghsto sleepor beginsto cuthair.A customermustacquirethemutexbeforeeI1terhgtke shop;hemustreleaseitwhmhesitsh eithera waiting roomchairor thebarberchair.

For a completesolution, in 。 、 、 ゥ エ ゥ ッ エ ッ the mutex, we needeventsemaphoresto recordthenumberof customersin エZ ィ ・ 。 ゥ エ ゥ ョ ァ イ ッ ッ ュ 。 ョ Qエ ィ ・ イ ョ オ オ イ ョ ュ ャ 「 ・ イ ‘ ッヲ ー・ッー i・ gettingtheir ィ 。 。 ゥ イ ‘ cut. Theeventsemaphorerecord-hgtheI1umberof customersh thewaitingroomis usedto wakeup thebarberwhemcustomerente?s;theeveI1tsemaphorerecordhgthemm-beyof customersgettingaka1rcutis usedto wakeupwaithgcustomers.

Solution7.10:TheI1aturalSOIl1tiORisfor eachresourceto havea lock.TheproblemariseswheReachthreadi requestslock i mdthm エ Q modN Sincealllockshavealreadybeenacquired, the threaddeadlocks

une approachisphavea mtral 」 o i Q エ イ ッ ャ ャ ・ キ ィ ゥ 」 ィ knows exactlywhich resourcesare in use and arbitrates 」 ッ ヲ ャ ゥ 」 エ ゥ ョ ァ requests. If イ ・

cesarem t ayailaple for a thready the c ッ イ Q エ イ ッ ャ ャ ・ イ 」 。 ・ ェ ・ 」 エ ィ ゥ ウ イ ・ ア オ ャ ・ s エ Nb ai ィ ・ イウo ャオエゥッ is エ P P イ 、 ・ i

e 。 」ア オゥ イ・、 in ゥ ョ 」 イ ・ 。 ウ ゥ ョ ァ order 。 ョ ャ 、 releasedin 、 ・ 」 イ ・ ・ 。 ウ ゥ ョ Q Y order. ForpIe, if all threadsrequestsimultaneously, resourceM 1will beleft

unrequested(sinceThread M 1 will request0 first, 。 i ャ、 then Qt Q ャ イ ・ 。

Q will block ッ イ Q ResourceO.

ャゥウ solutionis not ウ エ 。 イ イ 。 エ ゥ ッ ョ M ヲ イ ・ ・ e.g., T2 canwait foreverwhile T1and T3 alternate. t ッ 。 イ 。 ョ エ ・ ・ that no threadstarves, one could keeptrackof theI1umberof timesa threadcaIUIOtexecutewhenhisIIeighb

leasetheir locks. If this numberex

public static void main( String [] args ) {Thread rO = new Reader(); Thread rl = new Reader() ;Thread wo = new Writer (); Thread wi = new Writer () ;rO. start(); rl. start() ;wO. start (); w1. start () ;while ( true );

Solution7.6: Wewantto givewritersthepreference.Weachievethisbymodifying the solutionaboveto havea readerstartby locking LW andthenimmediatelyreleasingLW. In this way, a writer who acquirestheLW lock is guaranteedto 「 ・ ャ ・ 。 、 of thesubsequentreaders.

Solution7.7: We canachievefairnessbetweenreadersandwriters byhavingabit which indicateswhethera reador a write wasthelastoper-ationperformed.If thelastoperationperformedwasa read, a readeronacquiringa lock mustreleasethe lock andretry-thisgiveswriters pri-ority in acquiringthelock; a similar ッ ー ・ イ 。 エ ゥ ゥ ウ performedby writers.

Solution7.8: This problem canbe solvedfor a singleproducerand asingleconsumerwith apairof semaphores• llCount is incrementedandemptyCountis decrementedwheneveranitem is addedto thebuffer. Ifthe producerwants to decrementemptyCountwhen its count is 0, theproducersleeps.The next time an item is consumed, emptyCountis in-crementedandtheproduceris wokenup. Theconsumeroperatesanalo-ァ ッ オ ウ ャ t ィ ・ Javamethods, wait andnotify, canbeusedto implementthedesiredfunctionality.

If there are multiple producersand consumers, the solution abovehastwo races-twoproducerscantry writing to the sameslot andtwoconsumerscanreadfrom the sameslot. Theseracescanberemovedbyaddingmutexesaroundthe putItemlntoBufferand イ ・ ュ ッ カ ・ i エ ・ ュ f イ ッ ュ b オ Z G

calls.

30313233343536 Ipublic 」 ャ 。 ウ ウ サ37383940414243444546474849505152

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter8

DesignProbletns

Many of the problemshthis chaptercmbethebasisfor phDmlevelre-search.AcomprehemivediscussiOI1onthe s0111tiOI1savailablefor suchproblemsisouisidethescopeofthisbooki m u i カ ゥ ・ キ エ ゥ ョ ァ キ ィ ・ ョ

ne askssucha questiOILyou shouldhavea discussiOI1hwhichyoudemonstratem ability to think creatively}understmddesip1trade-

ffslandattackunfamiliarproblems.Theanswersin this chapterarep

sentedin this 」 ッ ョ エ ・ ク エ M ・ ケ aremeantto beexamplesof goodresponsesin an ゥ ョ エ ・ イ カ ゥ ・ キ ャ 、 arenot definitive state-of-the-artsolutions.

Solution8.1: As mentionedin theprologueto thisbook, oneapproachis to do a coarsepixelizationof the tiles andfor eachpotentialtile posi-tiOIIfiM the tileJhtheimagethat is closestエ ッ ゥ ョ termsof a normde-ヲ ゥ ョ ・ ッ カ ・ イ eachpixel color. If the imagecollectionis ャ ゥ ュ ゥ ケ ッ オ wouldften endup キ ィ ウ ゥ ァ ョ ゥ ヲ ゥ 」 。 ・ イ イ ッ イ ウ N Sincethehumaneyeperceivesthe

azecolor of a regiOIL ithashenobservedthat if you 。 オ ウ エ the av-・ j 。 イ ァ ・ エ color of atilebasedommmmadebyitsmighborhgtiles,it improvesthe P イ ・ イ 。 ャ ャ quality.

Oftenthe エ 。 イ ァ ・ エ ュ ァ ・ mayhaveverysimilaycolorfor alargeI1umber-of tiles h thebackzround-Ifwe pick the sameimageovermd over fora contiguousrego-rit standsouth themosaicaIId 、 ッ ・ ウ ッ エ createverygoodaesthetics.Hencethe mosaictools would usuallyallow the usersLspecifyconstraints ィ ッ キ oftena tile canbe イ ・ ー ・ ・ 、 or a minimumseparationbetweeI1theWO Copiesof m image.

-ena rectmgleh thetargetimagerhdhgtkebestimagethatcan。 ー ー イ ッ ク Q 。 エ ・ it esser1tidlyboils downto searchhgfor theI1earestr1eighmbor in somebdiIIlensiOI1alspace(wherek is theRUmber-ofcolor pixelsusedto 。 ー ー イ ッ ク 。 エ ・ エ ィ ・ Q 。 ァ ・ I ᄋ s ゥ r 」 ・ we cm do somepreprocessmgomthelibrary of imagesyit makessenseto do somespatialhdexhg.A

ry ウ ー ャ ・ indexhgschemefor relatively low valueof k would be tojust form a bdimeI1Sionalgrid mdplacethe imagesto theclosestpoht

on the grid. A moreindexing.

Findingtheoverallbestヲagecanberepeatedis NP-hard.ウ ッ 。 「 ャ ケ キ ・ ャ ャ N

Solution8.2: The predominantway of doing this is toindices. In aninvertedindex, for eachword, we storea listwherethe word occurs.Herelocationis definedto be the pair ofmentid andtheoffsetin the document. The list is storedin sortedorderof locations(first orderedby documentid, thenby offset). Whenwe arelooking for the documentsthat containa setof words, whatwe needtodois ヲ ゥ ョ 、 ャ ・ intersectionof lists for eachword. Sincethelists arealreadysorted, theintersectioncanbedonein linear-time(linearin thetotal sizeof thelists). Therearevariousoptimizationsthatcanbedoneto thisbasicinfrastructure.We list a few thoughtsbelow.

- Compression-compressingtheinvertedindexhelpsbothwith theability to indexmoredocumentsaswell asmemorylocality (fewercachemisses). Sincewe are storingsortedlists, oneway of com-pressingis to usedelta 」 ッ ュ ー イ ・ ウ ウ ゥ ッ キ ィ ・ イ ・ we only storethediffer-encebetweenthe successiveentries.Thedeltascanberepresentedin fewerbits.

- Caching-thedistribution queries is often fairly skewedand ithelpsa greatdealto cachethe resultsof someof themostfrequentquenes.

- Frequency-basedoptimization-sincesearchresultsoften do notneedto returneverydocumentthat matches(only top ten or so),only a ヲイ。」エゥッ ッヲ highestquality documentscanbeusedto answermostof thequeries.This meansthatwe canmaketwo invertedin-dices, onewith thehigh quality documentsthatstaysin the mem-ory andonewith the remainingdocumentsthatstayson the disk.

ャゥウ way if we cankeepthenumberof queriesthatrequirethesec-ondaryindexto a smallenoughnumber, thenwe canstill maintaina reasonablethroughputandlatency.

- Intersectionorder-sincethetotal intersectiontime dependsP エ ィ ・

total sizeof lists, it would makesenseto intersectthe wordswithsmallersetsfirs t. For example, if we are looking for IIUSA GDP2009 11, it would makesenseto intersectthe lists for GDP and2009beforetrying to intersectthelist for USA.

We could also build a multilevel index to improve accuracyon doc-uments. For high priority web pages, we can recursivelyfrom IIdoc-umentll abstractionintroducea notion of Ilparagraphlland then '/sen-tencell to index further down. Thatway the intersectionsfor the wordsmightbewithin the samecontext. We canpick resultswith closerindex

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter8

DesignProbletns

Many of the problemshthis chaptercmbethebasisfor phDmlevelre-search.AcomprehemivediscussiOI1onthe s0111tiOI1savailablefor suchproblemsisouisidethescopeofthisbooki m u i カ ゥ ・ キ エ ゥ ョ ァ キ ィ ・ ョ

ne askssucha questiOILyou shouldhavea discussiOI1hwhichyoudemonstratem ability to think creatively}understmddesip1trade-

ffslandattackunfamiliarproblems.Theanswersin this chapterarep

sentedin this 」 ッ ョ エ ・ ク エ M ・ ケ aremeantto beexamplesof goodresponsesin an ゥ ョ エ ・ イ カ ゥ ・ キ ャ 、 arenot definitive state-of-the-artsolutions.

Solution8.1: As mentionedin theprologueto thisbook, oneapproachis to do a coarsepixelizationof the tiles andfor eachpotentialtile posi-tiOIIfiM the tileJhtheimagethat is closestエ ッ ゥ ョ termsof a normde-ヲ ゥ ョ ・ ッ カ ・ イ eachpixel color. If the imagecollectionis ャ ゥ ュ ゥ ケ ッ オ wouldften endup キ ィ ウ ゥ ァ ョ ゥ ヲ ゥ 」 。 ・ イ イ ッ イ ウ N Sincethehumaneyeperceivesthe

azecolor of a regiOIL ithashenobservedthat if you 。 オ ウ エ the av-・ j 。 イ ァ ・ エ color of atilebasedommmmadebyitsmighborhgtiles,it improvesthe P イ ・ イ 。 ャ ャ quality.

Oftenthe エ 。 イ ァ ・ エ ュ ァ ・ mayhaveverysimilaycolorfor alargeI1umber-of tiles h thebackzround-Ifwe pick the sameimageovermd over fora contiguousrego-rit standsouth themosaicaIId 、 ッ ・ ウ ッ エ createverygoodaesthetics.Hencethe mosaictools would usuallyallow the usersLspecifyconstraints ィ ッ キ oftena tile canbe イ ・ ー ・ ・ 、 or a minimumseparationbetweeI1theWO Copiesof m image.

-ena rectmgleh thetargetimagerhdhgtkebestimagethatcan。 ー ー イ ッ ク Q 。 エ ・ it esser1tidlyboils downto searchhgfor theI1earestr1eighmbor in somebdiIIlensiOI1alspace(wherek is theRUmber-ofcolor pixelsusedto 。 ー ー イ ッ ク 。 エ ・ エ ィ ・ Q 。 ァ ・ I ᄋ s ゥ r 」 ・ we cm do somepreprocessmgomthelibrary of imagesyit makessenseto do somespatialhdexhg.A

ry ウ ー ャ ・ indexhgschemefor relatively low valueof k would be tojust form a bdimeI1Sionalgrid mdplacethe imagesto theclosestpoht

on the grid. A moreindexing.

Findingtheoverallbestヲagecanberepeatedis NP-hard.ウ ッ 。 「 ャ ケ キ ・ ャ ャ N

Solution8.2: The predominantway of doing this is toindices. In aninvertedindex, for eachword, we storea listwherethe word occurs.Herelocationis definedto be the pair ofmentid andtheoffsetin the document. The list is storedin sortedorderof locations(first orderedby documentid, thenby offset). Whenwe arelooking for the documentsthat containa setof words, whatwe needtodois ヲ ゥ ョ 、 ャ ・ intersectionof lists for eachword. Sincethelists arealreadysorted, theintersectioncanbedonein linear-time(linearin thetotal sizeof thelists). Therearevariousoptimizationsthatcanbedoneto thisbasicinfrastructure.We list a few thoughtsbelow.

- Compression-compressingtheinvertedindexhelpsbothwith theability to indexmoredocumentsaswell asmemorylocality (fewercachemisses). Sincewe are storingsortedlists, oneway of com-pressingis to usedelta 」 ッ ュ ー イ ・ ウ ウ ゥ ッ キ ィ ・ イ ・ we only storethediffer-encebetweenthe successiveentries.Thedeltascanberepresentedin fewerbits.

- Caching-thedistribution queries is often fairly skewedand ithelpsa greatdealto cachethe resultsof someof themostfrequentquenes.

- Frequency-basedoptimization-sincesearchresultsoften do notneedto returneverydocumentthat matches(only top ten or so),only a ヲイ。」エゥッ ッヲ highestquality documentscanbeusedto answermostof thequeries.This meansthatwe canmaketwo invertedin-dices, onewith thehigh quality documentsthatstaysin the mem-ory andonewith the remainingdocumentsthatstayson the disk.

ャゥウ way if we cankeepthenumberof queriesthatrequirethesec-ondaryindexto a smallenoughnumber, thenwe canstill maintaina reasonablethroughputandlatency.

- Intersectionorder-sincethetotal intersectiontime dependsP エ ィ ・

total sizeof lists, it would makesenseto intersectthe wordswithsmallersetsfirs t. For example, if we are looking for IIUSA GDP2009 11, it would makesenseto intersectthe lists for GDP and2009beforetrying to intersectthelist for USA.

We could also build a multilevel index to improve accuracyon doc-uments. For high priority web pages, we can recursivelyfrom IIdoc-umentll abstractionintroducea notion of Ilparagraphlland then '/sen-tencell to index further down. Thatway the intersectionsfor the wordsmightbewithin the samecontext. We canpick resultswith closerindex

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

valuesfrom theselists.

Solution8.3: This is a well studiedproblembecauseof its implicationsfor building a high speedInternetbackbone.Therearea numberof ap-proachesthat havebeenproposedandusedin IP routers. Onesimpleapproachis to build a trie data-structuresuchthatwe cantraversetheエ [ ゥ セ for anIP addresstill we hit a nodethathasa label. This essentiallyrequiresonepointerindirectionperbit of input. The lookup speedcanbe-improved-a little at thecostof memoryby makingfatternodesin thetrie thatconsumemultiplebits at a time.

There are a numberof approachesthat havebeentried in softwareandhardwareto speedthelookupprocess:

- Binarysearchonhashtables-wecanhaveonehashtablefor eachpossiblelengthof prefix andthendoasearchfor thelongestmatch-ing prefiX by looking throughall the hashtables. However thiscouid take32 hashtable lookups. Oneway of reducingthis is todo abinarysearchfor the longestmatchingprefix. In orderfor bi-narysearchto work, wewouldhaveto insertadditionalprefixesinthe-hashtablesto ensurethatif a longerprefix exists, binarysearchdoesnot terminateearly. This canbe doneby performingabinarysearchfor eachprefix andinsertadditionaldummyentrieswher-everthebinarysearchterminatesearly. This could inflate the sizeof hashtablesby atmostlog2 32 (in practice, it is muchsmaller).

- TernaryContentAddressableMemory(TCAM)-a TCAM is aspe-cial pieceof ィ。イ、キ。イ キィ・イ・ insteadof storing o ウ ャ 、 1s, a singleunit of memorycanalsostorea third statecalledthe "don't care"state. Also, the contentsof memorycanbe addressedby partialcontentsof the memory. TCAMs with 32 addressbits areusedtostoreprdxes.Eachprefix is paddedwitb Hdor1FtcafeFFbits to makeit 32 bits. This way, whenwe use an IP addressto addresstheTCAM, we getall thematchingprefixes.A priority logic gatethenselectsthelongestmatchingprefix.

Solution8.4: Thebasicideabehindmostspellingcorrectionsystemsisthatthemisspelledword'seditdistancefrom theintendedword tendstobeverysmall(oneor two edits).Henceif we keepahashtablefor all thewordsin the 、 ゥ 」 エ ゥ ッ ョ 。 イ ケ ャ 、 lookfor all thewordsthatarewithin two editdistancesof thetext, mostlikely, theintendedwordwill befoundin thisset. If thealphabethasm charactersand エ ィ セ searchtexthas 」 ィ 。 イ 。 」 エ ・ イ ウwe would ;eedto performroughly n . m2 hashtable lookups. Whenwe intersectall the stringswithin two edit di

176 CHAPTER8. DESIGNPROBLEMS 177

probabilisticmodels.Therearevariousinterestingideasthatcanbeusedto improvethespellingcorrectionsystem:

- Typingerrorsmodel-oftenspellingmistakesarea resultof typingerrors.Typingerrorsareeasyto modelbasedonkeyboardlayouts.

- Phoneticmodeling-abig class of spelling errors happenwhenthe ー ・ イ ウ ッ ウ ー ・ ャ ャ ゥ ョ ァ it knowshow the wordssoundsbut 、 ッ ・ ウ ッ エ

know the exact ウ ー ・ ャ ャ ゥ N In suchcases, it helpsto mapthe text tophonemesandthen ヲ ャ 、 all the wordsthatmap to the samepho-neticsequence.

- History of refinements-oftenusers themselvesprovide a greatamountof dataaboutthe most likely misspellingsby first enter-ing a misspelledword andthencorrectingi t. This kind of historicdatais oftenimmenselyvaluablefor spellingcorrection.

- Stemming-oftenthe size of dictionary can be reducedby onlykeepingthe stemmedカ ・ イ ウ ゥ ッ ヲ thewordsin it andstemmingthequerytext aswell.

Solution 8.5: Stemmingis a fairly large topic and different systemshaveadopteddifferentapproaches.Porterstemmerdevelopedby Mar-tin Porteris consideredoneof themostauthoritativealgorithmsfor stem-ming in theEnglishlanguage.Herewe mentionsomebasicideasrelatedto stemming, howeverthis is in no way a comprehensivediscussionPstemmingapproaches.

Thebasicideain moststemmingsystemsworksbasedon somesim-pIe rewrite rules, suchas, if the word endswith "es" or "s" or "ation",thenwe removethem. Sometimes, a simpleterminationmaynot キ ッ イfor example, wolves セ wolf. In orderto coverthis case, we mayhavea rule to replacea suffix "ves" to "f". In the end, mostrules amounttomatchinga set of suffixes and dependingオ ー ッ キ ィ ゥ 」 ィ one we endupwith, we may apply a certaintransformationto the string. Oneway ofefficiently doingthis couldbeto build a finite statemachinebasedP 。 ャ ャ

the rules.A more sophisticatedsystemmight haveseveralexceptionsto the

broadrule basedP エ ィ ・ stemmatchingsomepatterns.For example, thePorterstemmerdefinesseveralrulesbasedon a patternof vowels andconsonants.

Otherapproachesincludeuseof stochasticmethodto learnrewriterules andN-grambasedapproacheswherewe look at the surroundingwordsto determinethecorrectstemmingfor aword.

Solution8.6: This problemasposed, hassomeambiguity:- Sinceweusuallydownloadonefile in onerequest, if afile is greater

than b bytes, thereis no way we canmeetthe constraintof serv-ing fewer thanbbyteseverym

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

valuesfrom theselists.

Solution8.3: This is a well studiedproblembecauseof its implicationsfor building a high speedInternetbackbone.Therearea numberof ap-proachesthat havebeenproposedandusedin IP routers. Onesimpleapproachis to build a trie data-structuresuchthatwe cantraversetheエ [ ゥ セ for anIP addresstill we hit a nodethathasa label. This essentiallyrequiresonepointerindirectionperbit of input. The lookup speedcanbe-improved-a little at thecostof memoryby makingfatternodesin thetrie thatconsumemultiplebits at a time.

There are a numberof approachesthat havebeentried in softwareandhardwareto speedthelookupprocess:

- Binarysearchonhashtables-wecanhaveonehashtablefor eachpossiblelengthof prefix andthendoasearchfor thelongestmatch-ing prefiX by looking throughall the hashtables. However thiscouid take32 hashtable lookups. Oneway of reducingthis is todo abinarysearchfor the longestmatchingprefix. In orderfor bi-narysearchto work, wewouldhaveto insertadditionalprefixesinthe-hashtablesto ensurethatif a longerprefix exists, binarysearchdoesnot terminateearly. This canbe doneby performingabinarysearchfor eachprefix andinsertadditionaldummyentrieswher-everthebinarysearchterminatesearly. This could inflate the sizeof hashtablesby atmostlog2 32 (in practice, it is muchsmaller).

- TernaryContentAddressableMemory(TCAM)-a TCAM is aspe-cial pieceof ィ。イ、キ。イ キィ・イ・ insteadof storing o ウ ャ 、 1s, a singleunit of memorycanalsostorea third statecalledthe "don't care"state. Also, the contentsof memorycanbe addressedby partialcontentsof the memory. TCAMs with 32 addressbits areusedtostoreprdxes.Eachprefix is paddedwitb Hdor1FtcafeFFbits to makeit 32 bits. This way, whenwe use an IP addressto addresstheTCAM, we getall thematchingprefixes.A priority logic gatethenselectsthelongestmatchingprefix.

Solution8.4: Thebasicideabehindmostspellingcorrectionsystemsisthatthemisspelledword'seditdistancefrom theintendedword tendstobeverysmall(oneor two edits).Henceif we keepahashtablefor all thewordsin the 、 ゥ 」 エ ゥ ッ ョ 。 イ ケ ャ 、 lookfor all thewordsthatarewithin two editdistancesof thetext, mostlikely, theintendedwordwill befoundin thisset. If thealphabethasm charactersand エ ィ セ searchtexthas 」 ィ 。 イ 。 」 エ ・ イ ウwe would ;eedto performroughly n . m2 hashtable lookups. Whenwe intersectall the stringswithin two edit di

176 CHAPTER8. DESIGNPROBLEMS 177

probabilisticmodels.Therearevariousinterestingideasthatcanbeusedto improvethespellingcorrectionsystem:

- Typingerrorsmodel-oftenspellingmistakesarea resultof typingerrors.Typingerrorsareeasyto modelbasedonkeyboardlayouts.

- Phoneticmodeling-abig class of spelling errors happenwhenthe ー ・ イ ウ ッ ウ ー ・ ャ ャ ゥ ョ ァ it knowshow the wordssoundsbut 、 ッ ・ ウ ッ エ

know the exact ウ ー ・ ャ ャ ゥ N In suchcases, it helpsto mapthe text tophonemesandthen ヲ ャ 、 all the wordsthatmap to the samepho-neticsequence.

- History of refinements-oftenusers themselvesprovide a greatamountof dataaboutthe most likely misspellingsby first enter-ing a misspelledword andthencorrectingi t. This kind of historicdatais oftenimmenselyvaluablefor spellingcorrection.

- Stemming-oftenthe size of dictionary can be reducedby onlykeepingthe stemmedカ ・ イ ウ ゥ ッ ヲ thewordsin it andstemmingthequerytext aswell.

Solution 8.5: Stemmingis a fairly large topic and different systemshaveadopteddifferentapproaches.Porterstemmerdevelopedby Mar-tin Porteris consideredoneof themostauthoritativealgorithmsfor stem-ming in theEnglishlanguage.Herewe mentionsomebasicideasrelatedto stemming, howeverthis is in no way a comprehensivediscussionPstemmingapproaches.

Thebasicideain moststemmingsystemsworksbasedon somesim-pIe rewrite rules, suchas, if the word endswith "es" or "s" or "ation",thenwe removethem. Sometimes, a simpleterminationmaynot キ ッ イfor example, wolves セ wolf. In orderto coverthis case, we mayhavea rule to replacea suffix "ves" to "f". In the end, mostrules amounttomatchinga set of suffixes and dependingオ ー ッ キ ィ ゥ 」 ィ one we endupwith, we may apply a certaintransformationto the string. Oneway ofefficiently doingthis couldbeto build a finite statemachinebasedP 。 ャ ャ

the rules.A more sophisticatedsystemmight haveseveralexceptionsto the

broadrule basedP エ ィ ・ stemmatchingsomepatterns.For example, thePorterstemmerdefinesseveralrulesbasedon a patternof vowels andconsonants.

Otherapproachesincludeuseof stochasticmethodto learnrewriterules andN-grambasedapproacheswherewe look at the surroundingwordsto determinethecorrectstemmingfor aword.

Solution8.6: This problemasposed, hassomeambiguity:- Sinceweusuallydownloadonefile in onerequest, if afile is greater

than b bytes, thereis no way we canmeetthe constraintof serv-ing fewer thanbbyteseverym

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

lower layersof networkingstacksuchasthetransportlayeror thenetwork layer. Often the systemdesignercould look at the dis-tribution of file sizesand concludethat this problemhappenssoinfrequentlythat we do not care. Alternately, we may choosetoserveno morethanthefirst bbytesof anyfile.

- Giventhatthehost'sbandwidthis aresourcefor whichtherecouldbe contention, oneimportantdesignchoiceto be madeis how toresolvea contention. Do we let requestsget servedin first-comefirst-servedorderor is therea notion of priority? Often crawlershaveabuilt-in notionof priority basedonhow importantthe doc-umentis to theusersor howfreshthecurrentcopyis.

Oneway of doingthis couldbeto maintaina serverwith which eachcrawlerchecksto seeif it is okayto hit a particularhost. The servercankeep an 。 」 」 ッ オ ッ ヲ how manybyteshavebeendownloadedfrom theserverin the lastminute andnot permit anycrawlerto hit the serverifwe arealreadycloseto the quota. If we do not careaboutpriority, thenwe cankeepthe interfacesynchronouswherea serverrequestsfor per-missionto downloada file andit immediatelygetsapprovedor denied.If we careaboutpriorities, thentheservermayenqueuethe イ ・ ア オ ・ ウ エ ャ 、

inform thecrawlerwhenit is alright to downloadthefile. Thequeuesatthepermissionservermaybebasedonpriorities.

In casethesingle ー ・ イ ュ ゥ ウ ウ ゥ ッ ウ ・ イ カ ・ イ becomesabottleneckfor thesys-tem, we canusemultiple serverssuchthat the responsibilityof a givenhostis decidedbyhashingthehostnameandassigningit to a particularserverbasedonthehashrange.

Solution8.7: Sincethe web graphcanhavebillions of nodesandit ismostly a sparseァ イ ィ it is bestto イ ・ ー イ ・ ウ ・ エ ィ ・ graphasan adjacencylist. Building the adjacencylist representationof the graphitself mayrequiresignificantamountof 」ッューオエ。エゥッ 、・ー・ョ、ゥョァ uponhow thein-formationis collected.Usually, thegraphis constructedby downloadingthepageson theweb andextractingthehyperlinkinformationfrom thepages.SincetheURL of apagecanbearbitrarily long andvariesa lot insize, it is oftena goodideato イ ・ ー イ ・ ウ ・ エ theURL by ahashvalue.

The mostexpensivepartof PageRankalgorithmis the repeatedma-trix multiplication. u ウ オ 。 ャ ャ ケ ゥ ウ notpossibleto keeptheentiregraphin-formationin a singlemachine's RAM. Thereareusuallytwo approachesto solvingthis problem:

- Disk-basedsorting-inthis approach, we keepthe columnvectorXinmem

178 CHAPTER8. DESIGNPROBLEMS 179

cando the entirecomputationon a ウ QYャ・ machine.Howeverthisapproachcanbe fairly slow because、 ゥ セ ォ M 「 。 ウ ・ 、 sortingis usuallySlOW.

- Partitionedgraph-inthis approach, we use ュ 。 」 ィ ゥ ョ ・ ウ andparti-tion the vertices(web pages)into n sets. u s Q Z ャ ケ the ー 。 イ エ ゥ エ ゥ 「 o i Q ゥ ョ Qゥ ウ 、 ッ ョ ・ ・ 「 ケ ー 。 イ エ エ ゥ エ ゥ o i Q ゥ ョ ァ thehashウ ー 。 」 ・ suchエ ィ 。 エ ゥ エ ゥ ウ ・ 。 ウ ケ エ ッ 、 ・ エ ・ イ ᆳ -ュ ャゥョ ャ・ which vertex ュ 。 ー ウ エ ッ which ュ 。 」 ィ ゥ ョ ャ ・ ッ g ゥ カ ・ ョ Q エ ィ ゥ ウ ー 。 イ ゥ o i Q E

ゥ i QYf eachIIlackhe loadsits verticesandtheir outgohgedgesintoRAM-Eachmachinealso loadsthe partsof the pageRmkvectorthat correspOIIdsto its vertices.TheIIeachmachimdoesa localmatrix multiplication. Sincesomeof the edgesP ・ 。 」 ィ machinewould correspor1dto thenodesthatareOWIledby othermachhesrtheresultvectoris goingto containnonzeroentriesfor verticesthataremtOWI1edby thelocalmachim-Sorattkeendof localmultiplimcation, weneedto sendupdatesto otherhostsso 。 エ thesevalues」 。 「 ・ correctlyaddedup. The advantageof this approachis thatwecanprocessarbitrarilylargegraphsaslongaswehavesufficientnumberof machines.

Solution8.8: If we havesufficientRAM on a singlemachine, the mostsimplesolutionwouldbeto maintaina ュゥョ ィ・。ー wherewemaintainallthe eventsby their priority. Sincewe are interestedin a scalablesolu-tion to this problem, we needto partition the problemacrossュ オ ャ エ i ・ュ ・ ョ ャ ・ ウ N

Onewayof doingthis couldbeto hashtheeventsand ー 。 イ エ ゥ エ ゥ エ ィ ・ ュ

into rangessothatonehashrange」 ッ イ イ ・ ウ ー ッ エ ッ onemachine.Thisway,theimertmddeleteoperationscmbedonebyjustcommuMeathgwithoneof theservers.Howeverin orderto do theextract-minoperation, weneedto senda find-min messageto all themachines, infer themin fromall their responses, andthentry to deleteit.

Sinceat a given time, all the clientswould be interestedin the sameevent(the highestpriority event)Fit is hard to distribute this problemwell. If a largenumberof clients are trying to do this ッ ー ・ イ 。 エ ゥ ッ 。 エ thesame エ ゥ イ ・ we may run into a situation where most clients will findthat the absolutemin eventtheywere エ イ ケ ゥ ョ Q Y to extracthasalreadybeenア ・ ャ ・ i ヲ エ ィ ・ エ ィ イ ャ イ ッ ャ

c ィ ゥ ョ ・ イ w ・ 」 。 ョ ォ ・ ・ ー omsemrthatisイ ・ ウ ー ッ ュ ゥ 「 ャ ・ ヲ ッ イ ュ ー ッ エ ッ all themachiRes-Thlsservercmprefetchtop h1mdredor so eventsfrom eachof themachinesandkeepthemin a heap.

In manyapplications, we donotneedstrongconsistencyguarantees.Whatwemedis thatoverallrwespendmostof ourresourcestakhgcareof the highestprioyity jobs-hsuchcasesra clieIIt cm pick om of thehashrmgesrmdomlyaM just requestthehighestpmrityjob from thecorrespOIIdhgmachhe.Thiswould work greatfor distributedcrawler

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

lower layersof networkingstacksuchasthetransportlayeror thenetwork layer. Often the systemdesignercould look at the dis-tribution of file sizesand concludethat this problemhappenssoinfrequentlythat we do not care. Alternately, we may choosetoserveno morethanthefirst bbytesof anyfile.

- Giventhatthehost'sbandwidthis aresourcefor whichtherecouldbe contention, oneimportantdesignchoiceto be madeis how toresolvea contention. Do we let requestsget servedin first-comefirst-servedorderor is therea notion of priority? Often crawlershaveabuilt-in notionof priority basedonhow importantthe doc-umentis to theusersor howfreshthecurrentcopyis.

Oneway of doingthis couldbeto maintaina serverwith which eachcrawlerchecksto seeif it is okayto hit a particularhost. The servercankeep an 。 」 」 ッ オ ッ ヲ how manybyteshavebeendownloadedfrom theserverin the lastminute andnot permit anycrawlerto hit the serverifwe arealreadycloseto the quota. If we do not careaboutpriority, thenwe cankeepthe interfacesynchronouswherea serverrequestsfor per-missionto downloada file andit immediatelygetsapprovedor denied.If we careaboutpriorities, thentheservermayenqueuethe イ ・ ア オ ・ ウ エ ャ 、

inform thecrawlerwhenit is alright to downloadthefile. Thequeuesatthepermissionservermaybebasedonpriorities.

In casethesingle ー ・ イ ュ ゥ ウ ウ ゥ ッ ウ ・ イ カ ・ イ becomesabottleneckfor thesys-tem, we canusemultiple serverssuchthat the responsibilityof a givenhostis decidedbyhashingthehostnameandassigningit to a particularserverbasedonthehashrange.

Solution8.7: Sincethe web graphcanhavebillions of nodesandit ismostly a sparseァ イ ィ it is bestto イ ・ ー イ ・ ウ ・ エ ィ ・ graphasan adjacencylist. Building the adjacencylist representationof the graphitself mayrequiresignificantamountof 」ッューオエ。エゥッ 、・ー・ョ、ゥョァ uponhow thein-formationis collected.Usually, thegraphis constructedby downloadingthepageson theweb andextractingthehyperlinkinformationfrom thepages.SincetheURL of apagecanbearbitrarily long andvariesa lot insize, it is oftena goodideato イ ・ ー イ ・ ウ ・ エ theURL by ahashvalue.

The mostexpensivepartof PageRankalgorithmis the repeatedma-trix multiplication. u ウ オ 。 ャ ャ ケ ゥ ウ notpossibleto keeptheentiregraphin-formationin a singlemachine's RAM. Thereareusuallytwo approachesto solvingthis problem:

- Disk-basedsorting-inthis approach, we keepthe columnvectorXinmem

178 CHAPTER8. DESIGNPROBLEMS 179

cando the entirecomputationon a ウ QYャ・ machine.Howeverthisapproachcanbe fairly slow because、 ゥ セ ォ M 「 。 ウ ・ 、 sortingis usuallySlOW.

- Partitionedgraph-inthis approach, we use ュ 。 」 ィ ゥ ョ ・ ウ andparti-tion the vertices(web pages)into n sets. u s Q Z ャ ケ the ー 。 イ エ ゥ エ ゥ 「 o i Q ゥ ョ Qゥ ウ 、 ッ ョ ・ ・ 「 ケ ー 。 イ エ エ ゥ エ ゥ o i Q ゥ ョ ァ thehashウ ー 。 」 ・ suchエ ィ 。 エ ゥ エ ゥ ウ ・ 。 ウ ケ エ ッ 、 ・ エ ・ イ ᆳ -ュ ャゥョ ャ・ which vertex ュ 。 ー ウ エ ッ which ュ 。 」 ィ ゥ ョ ャ ・ ッ g ゥ カ ・ ョ Q エ ィ ゥ ウ ー 。 イ ゥ o i Q E

ゥ i QYf eachIIlackhe loadsits verticesandtheir outgohgedgesintoRAM-Eachmachinealso loadsthe partsof the pageRmkvectorthat correspOIIdsto its vertices.TheIIeachmachimdoesa localmatrix multiplication. Sincesomeof the edgesP ・ 。 」 ィ machinewould correspor1dto thenodesthatareOWIledby othermachhesrtheresultvectoris goingto containnonzeroentriesfor verticesthataremtOWI1edby thelocalmachim-Sorattkeendof localmultiplimcation, weneedto sendupdatesto otherhostsso 。 エ thesevalues」 。 「 ・ correctlyaddedup. The advantageof this approachis thatwecanprocessarbitrarilylargegraphsaslongaswehavesufficientnumberof machines.

Solution8.8: If we havesufficientRAM on a singlemachine, the mostsimplesolutionwouldbeto maintaina ュゥョ ィ・。ー wherewemaintainallthe eventsby their priority. Sincewe are interestedin a scalablesolu-tion to this problem, we needto partition the problemacrossュ オ ャ エ i ・ュ ・ ョ ャ ・ ウ N

Onewayof doingthis couldbeto hashtheeventsand ー 。 イ エ ゥ エ ゥ エ ィ ・ ュ

into rangessothatonehashrange」 ッ イ イ ・ ウ ー ッ エ ッ onemachine.Thisway,theimertmddeleteoperationscmbedonebyjustcommuMeathgwithoneof theservers.Howeverin orderto do theextract-minoperation, weneedto senda find-min messageto all themachines, infer themin fromall their responses, andthentry to deleteit.

Sinceat a given time, all the clientswould be interestedin the sameevent(the highestpriority event)Fit is hard to distribute this problemwell. If a largenumberof clients are trying to do this ッ ー ・ イ 。 エ ゥ ッ 。 エ thesame エ ゥ イ ・ we may run into a situation where most clients will findthat the absolutemin eventtheywere エ イ ケ ゥ ョ Q Y to extracthasalreadybeenア ・ ャ ・ i ヲ エ ィ ・ エ ィ イ ャ イ ッ ャ

c ィ ゥ ョ ・ イ w ・ 」 。 ョ ォ ・ ・ ー omsemrthatisイ ・ ウ ー ッ ュ ゥ 「 ャ ・ ヲ ッ イ ュ ー ッ エ ッ all themachiRes-Thlsservercmprefetchtop h1mdredor so eventsfrom eachof themachinesandkeepthemin a heap.

In manyapplications, we donotneedstrongconsistencyguarantees.Whatwemedis thatoverallrwespendmostof ourresourcestakhgcareof the highestprioyity jobs-hsuchcasesra clieIIt cm pick om of thehashrmgesrmdomlyaM just requestthehighestpmrityjob from thecorrespOIIdhgmachhe.Thiswould work greatfor distributedcrawler

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

。ーーャゥ」。エゥ 「オエ it wouldbeabadideafor eventdrivensimulation.

Solution8.9: Oftenclientsof a servicecaremoreaboutthe 99-thor the95-thnerceI1tiklateRcyfor theserverratherthmthememMeneysinceィ ・ ケ 。 ョ エ ュ ッ ウ エ ofthe イ ・ ウ ・ カ ゥ 」 ・ 、 in a reasonableamountof

ccasionalrequesttakesvery long. If our architectureissuchthat at a time only a fixed I1umberof requestscm get servedmdother ー ・ ョ 、 ァ requestsmustwait for a slot to openup beforegetthgervedrit is importmtto desig1ourqueuingsystemh sucha way that

therequeststhattakeavery lOI1g time to servedoRotblockmmysmalljobsbehindthem.

Considerthe casewherethe time it takesfor the serverto processaequestis afunctiOI1oftherequest-GiventhedistributiORofrequestsytheウ イ カ ゥ 」 ・ time follows a Paretodistribution.hsuchcases, it greatlyhelpsLLavetwoqueuesaMpickagoodthresholdsuchthattherequeststhattake longer thanthe thresholdエ ゥ エ ァ ッ to one queueand the requeststhattaKJlessthanor equaltothet}msholdtimergo to theotherqueue-wepickthetkredo1dsuchthatthemajorityofjobsgoto thefasterqueuemdthejobsh this queuearenever-blockedbehiI1dabigjob-UIelargeriobsdohaveto wait morebehindthelargerjobsbutoverallthis strategy…1greatlyreducethe99-th ー ・ イ 」 ・ エ ゥ ャ ・ ャ 。 エ ・ 」 ケ N

Oftenthesystemdesigner、 ッ ・ ウ ッ エ knowhowlonga givenrequestisgoingto takehadvmceh orderto maketheright queuhgdecisiOIl-Ithasbeenshownthatevenin ウ オ 」 ィ ウ ・ ゥ エ is advantageousto keeptwoqueues.When a requestcomesin, it is put in the fast queue, howeverwhenittakes1ongerthan 。 」 ・ ゥ ョ thresholdtime, we canceltherequestandput it at thebackof theslow queue.

Solution8.10: Reasonablegoalsfor sucha systemcould include:-providingmerswiththemostrdevaIItds-provideadvertiserswith the bestPOSSIblereturnOI1heir hvest-

ment_ minimizing thecostof runningsuchan ッ ー ・ イ 。 エ ゥ ッ

Thereare tMKey compomntsto building sucha system:(1.)thefront-facing 」 ッ ュ ー ッ ・ by which advertiserscm add their advertise-mmts, COI1trdwhentheir adsget displayed, how muchaM how エ ィiiamt io mndtheir 。 、 カ ・ イ エ ゥ ァ ュ ッ ュ 」 。 m review the pedOmanceoftheiradsind(2.)the。 、 ュ ウ ・ イ カ ゥ ョ ァ ウ ケ ウ エ ・ ュ ャ ゥ 」 ィ selectswhichadsto showonthesearches.

The fro facing systemcanbe a fairly 」 ッ ョ カ ・ エ ゥ ッ iュ al キ ・ 「 ウ ゥ エ ・ 、 ・ ウ ゥ ァ イ ョ

Usersinteractwith the ウ ケ ウ エ ・ ュ オ ウ ゥ ョ Q Y 。 「 イ ッ キ ウ ・ イ 。 ョ ャ 、 ッ ー ・ ョ ャ ゥ ョ ァ 。 」 ッ ュ ャ ・ 」 エ ャ o イ 1

エッエィ・キ・ 「ウ ゥエ ・N You will needto

180 CHAPTER8. DESIGNPROBLEMS 181

uウ・イ state-asetof forms to let advertisersspecifythingslike theiradvertisingmaterials, their advertisingbudgetetc. Also a way tostorethis informationー ・ イ ウ ゥ ウ エ ・ ョ エ ャ

- Performancereports-awayto generatereportsonhowandwheretheadvertiser'smoneyis 「・ QY spent.

- Humaninteractions-eventhebestof automatedsystemsrequireoccasionalhumaninteractionandaway to interferewith thealgo-rithms. This may requirean interfacefor advertisersto be abletocontactcustomerservicerepresentativesandaninterfacefor thoseイ ・ ー イ ・ ウ ・ 。 エ ゥ カ ・ ウ to interactwith thesystem.ャ ・ whole front-endsystemcanbebuilt using, for example, HTML

and JavaScript, with a LAMP stack (Linux as the operatingsystem,Apacheasthe HTTP ウ ・ m ケ s q l asthe databasesoftware, andPHPfor theapplicationlogic) respondingto theuserinput.

ャ・ ad-servingsystemwould probablybe a lessconventionalwebservice. It needsto chooseadsbasedon their "relevance"to the search,perhapssomeknowledgeof theuser'ssearchhistory, andhowmuchtheadvertiseris willing to pay. A numberof strategiescouldbeenvisionedherefor estimatingrelevance, suchas, usinginformationretrievalor ma-chinelearningtechniquesthatlearnfrom pastuserinteractions.

The adscanbe addedto the searchresultsby embeddingJavaScriptin the resultsthat pulls in the adsfrom the ad-servingsystemdirectly.Thishelpsisolatethelatencyof servingsearchresultsfrom thelatencyofservingadresults.

Solution8.11: Thekeytechnicalchallengein this problemis to comeupwith the list of articles-theHTML codefor addingtheseto a sidebaristrivial.

Onesuggestionmightbe to addarticlesthathave ー イ ッ カ ・ エ ッ bepop-ular recently. Anotheris to havelinks to recentnewsarticles. A humanreaderatJinglecould tagarticleswhichhebelievesto besignificant. Hecould also add tagssuchas finance, politics, etc. to the articles. Thesetagscouldalsocomefrom theHTML meta-tagsor thepagetitle.

We could also sometimesprovide articles at randomand seehowpopularthey proveto be; the populararticlescanthenbe shownmorefrequently.

On a more sophisticatedlevel, Jingle could use automatictextualanalysis, whereasimilarity is definedbetweenpairsof articles-thissim-ilarity is a realnumberandmeasureshow manywords arecommontothe two. Severalissuescomeup, suchas the fact that frequentlyoccur-ring wordssuchas"for" and "the" shoul

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

。ーーャゥ」。エゥ 「オエ it wouldbeabadideafor eventdrivensimulation.

Solution8.9: Oftenclientsof a servicecaremoreaboutthe 99-thor the95-thnerceI1tiklateRcyfor theserverratherthmthememMeneysinceィ ・ ケ 。 ョ エ ュ ッ ウ エ ofthe イ ・ ウ ・ カ ゥ 」 ・ 、 in a reasonableamountof

ccasionalrequesttakesvery long. If our architectureissuchthat at a time only a fixed I1umberof requestscm get servedmdother ー ・ ョ 、 ァ requestsmustwait for a slot to openup beforegetthgervedrit is importmtto desig1ourqueuingsystemh sucha way that

therequeststhattakeavery lOI1g time to servedoRotblockmmysmalljobsbehindthem.

Considerthe casewherethe time it takesfor the serverto processaequestis afunctiOI1oftherequest-GiventhedistributiORofrequestsytheウ イ カ ゥ 」 ・ time follows a Paretodistribution.hsuchcases, it greatlyhelpsLLavetwoqueuesaMpickagoodthresholdsuchthattherequeststhattake longer thanthe thresholdエ ゥ エ ァ ッ to one queueand the requeststhattaKJlessthanor equaltothet}msholdtimergo to theotherqueue-wepickthetkredo1dsuchthatthemajorityofjobsgoto thefasterqueuemdthejobsh this queuearenever-blockedbehiI1dabigjob-UIelargeriobsdohaveto wait morebehindthelargerjobsbutoverallthis strategy…1greatlyreducethe99-th ー ・ イ 」 ・ エ ゥ ャ ・ ャ 。 エ ・ 」 ケ N

Oftenthesystemdesigner、 ッ ・ ウ ッ エ knowhowlonga givenrequestisgoingto takehadvmceh orderto maketheright queuhgdecisiOIl-Ithasbeenshownthatevenin ウ オ 」 ィ ウ ・ ゥ エ is advantageousto keeptwoqueues.When a requestcomesin, it is put in the fast queue, howeverwhenittakes1ongerthan 。 」 ・ ゥ ョ thresholdtime, we canceltherequestandput it at thebackof theslow queue.

Solution8.10: Reasonablegoalsfor sucha systemcould include:-providingmerswiththemostrdevaIItds-provideadvertiserswith the bestPOSSIblereturnOI1heir hvest-

ment_ minimizing thecostof runningsuchan ッ ー ・ イ 。 エ ゥ ッ

Thereare tMKey compomntsto building sucha system:(1.)thefront-facing 」 ッ ュ ー ッ ・ by which advertiserscm add their advertise-mmts, COI1trdwhentheir adsget displayed, how muchaM how エ ィiiamt io mndtheir 。 、 カ ・ イ エ ゥ ァ ュ ッ ュ 」 。 m review the pedOmanceoftheiradsind(2.)the。 、 ュ ウ ・ イ カ ゥ ョ ァ ウ ケ ウ エ ・ ュ ャ ゥ 」 ィ selectswhichadsto showonthesearches.

The fro facing systemcanbe a fairly 」 ッ ョ カ ・ エ ゥ ッ iュ al キ ・ 「 ウ ゥ エ ・ 、 ・ ウ ゥ ァ イ ョ

Usersinteractwith the ウ ケ ウ エ ・ ュ オ ウ ゥ ョ Q Y 。 「 イ ッ キ ウ ・ イ 。 ョ ャ 、 ッ ー ・ ョ ャ ゥ ョ ァ 。 」 ッ ュ ャ ・ 」 エ ャ o イ 1

エッエィ・キ・ 「ウ ゥエ ・N You will needto

180 CHAPTER8. DESIGNPROBLEMS 181

uウ・イ state-asetof forms to let advertisersspecifythingslike theiradvertisingmaterials, their advertisingbudgetetc. Also a way tostorethis informationー ・ イ ウ ゥ ウ エ ・ ョ エ ャ

- Performancereports-awayto generatereportsonhowandwheretheadvertiser'smoneyis 「・ QY spent.

- Humaninteractions-eventhebestof automatedsystemsrequireoccasionalhumaninteractionandaway to interferewith thealgo-rithms. This may requirean interfacefor advertisersto be abletocontactcustomerservicerepresentativesandaninterfacefor thoseイ ・ ー イ ・ ウ ・ 。 エ ゥ カ ・ ウ to interactwith thesystem.ャ ・ whole front-endsystemcanbebuilt using, for example, HTML

and JavaScript, with a LAMP stack (Linux as the operatingsystem,Apacheasthe HTTP ウ ・ m ケ s q l asthe databasesoftware, andPHPfor theapplicationlogic) respondingto theuserinput.

ャ・ ad-servingsystemwould probablybe a lessconventionalwebservice. It needsto chooseadsbasedon their "relevance"to the search,perhapssomeknowledgeof theuser'ssearchhistory, andhowmuchtheadvertiseris willing to pay. A numberof strategiescouldbeenvisionedherefor estimatingrelevance, suchas, usinginformationretrievalor ma-chinelearningtechniquesthatlearnfrom pastuserinteractions.

The adscanbe addedto the searchresultsby embeddingJavaScriptin the resultsthat pulls in the adsfrom the ad-servingsystemdirectly.Thishelpsisolatethelatencyof servingsearchresultsfrom thelatencyofservingadresults.

Solution8.11: Thekeytechnicalchallengein this problemis to comeupwith the list of articles-theHTML codefor addingtheseto a sidebaristrivial.

Onesuggestionmightbe to addarticlesthathave ー イ ッ カ ・ エ ッ bepop-ular recently. Anotheris to havelinks to recentnewsarticles. A humanreaderatJinglecould tagarticleswhichhebelievesto besignificant. Hecould also add tagssuchas finance, politics, etc. to the articles. Thesetagscouldalsocomefrom theHTML meta-tagsor thepagetitle.

We could also sometimesprovide articles at randomand seehowpopularthey proveto be; the populararticlescanthenbe shownmorefrequently.

On a more sophisticatedlevel, Jingle could use automatictextualanalysis, whereasimilarity is definedbetweenpairsof articles-thissim-ilarity is a realnumberandmeasureshow manywords arecommontothe two. Severalissuescomeup, suchas the fact that frequentlyoccur-ring wordssuchas"for" and "the" shoul

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

have the samespellhgbut completeIydiffererlt meanhgs(mtimVIrusmemsdifferez1tthhgsh the contextof articleson AIDS md computer

Z l e ゥ z e e Z ェ h s h ゥ ヲ ゥエ ・ 。 キ ィ 。 エ l エ l c ヲ ・ ウ ゥ r 、 ゥ カ ゥ 、 オ 。 ャ Q ュ m 。 カ ・ ュ 、 N e キ ・ ウ ・ ・ ュ オ ウ ・ 、 b

adbothA andB in a singlesession, キ・ ゥァィエ want to recomto 。 ョ ケ ッ ・ readiIIgA.Forcollaborative filtermgto workr weReedto have

a substantialnumberof users.

solution812:Anonlhepokerplayingservicew0111dhavea fror1tmendsystemwhichusersiIItemct with andaback-mdsystemwhichmmtheg r gesmomulooksヲ ッ イ ォ 。 、 etc.

The fror1tmendsystemwould mta1i a UI for 。 」 」 P Q Q エ management-this would cover fir st-timeregistratiORFloggingmhy mm 。 ァゥョァッ iョャ ャゥョ ャ・ー・ イM

s ・ョョ、 ィ ァ o イイ ・ュ c ・ゥ ゥ v ィ ァ ュ ・

ァ。ュ・ー ャ。 ケゥョァ vi エ ィゥ ウ couldbe as ウ p i ・ assomethe ・ of the game(cardsin ィ 」 。 イ 、 ウ on エ ャ 。 「 ャ ・ bets)and 。 ュto enterabet. A more ウ ッ ー ィ ゥ ウ エ ゥ 」 。 エ ・ 、 VI might useJ。 カ。s」イゥー エエッ。ョ ュ ᆳcards「 ・ ゥ ョ Q Y 、 ・ 。 ャ エ 」 ィ 。 ョ ァ ・ the ・ ク ー イ ・ ウ s ゥ o ョ ッ p ャ 。 ケ ・ イ G s iョ 。 。 ァ ・ ウ ウ エ 。 エ オ ウ ュ ・ ウ

-

ァj イ[ R lヲエ ZRZzZエ Z[ ell・Bエッ ヲッイュ 。

S Z ゥZ ZZゥzゥ ゥャ ゥQRRーイッァZエコZRR[ zゥjzヲヲココゥZWRQRZエヲzl」・ Onh

s ・エ イ S エ [ Q Z コZ s [ ゥ コイ [ Z c ゥ Z コ コ エ ] [ D D j Z Z [ [ コ コ Z s ゥ S S R R j z1orsituatior1andclaimthathis htemetcomectx

ZZZZ m 「 ケ ィ 。 rule that the s…will 「 ゥ 、 エ ィ ・ー ャ 。 ケ ・ エ ィ ・ 。 ュ 、 ッ ・ ウ m ュ ー ッ 、 ア Q Q S Z ィ N a ョ ッ

Z ゥ ゥ ゥ・ イ G ウ 」 ッ ョ ョ ・ 」 s m カ 。 ケ エ m ウ ヲ 。 カ ッ イ 。 「 ャ ・ t z a ァ 。 ゥ i i ・

CollusionbetweeI1playersis motheyseriouspDger logs cmbe mit d for examplesof playersworking togetherto

shareknowledge ッ ヲ イ ッ イ ・ ・ コ ・ P ー ャ 。 ケ ・ i エ Ihnf!:;エゥッ ョ ー ャ。 ケ・ イウ clQ エィィ・ュュ ・ャカ ・ ウヲャ 。ァ ウオウー ゥ オウー ャ。 ケ and 」 オ s ョ ・ イ s …イ・ーコZQQR _[RSZZヲZエィエュウ・ャケ SMedproblmyisstmeasyto getwrOI1g.A fairly frequentproblemis ushgpro S Qg u

182CHAPTER8. DESIGNPROBLEMS 183

a seedfor a randomnumbergenerator, which meansthat thereare P ャ ケ

roughly20,000possibleウ ・ ア オ ・ 」 ・ ウ of イ 。 ョ 、 ッ ュ オ ュ 「 ・ イ ウ N This meansthat,ッョ。 。カ・イ。ァ・ knowingthefirst_4 cardsis enoughto predicttheorderofthe restof thecardssincelog2 H QXNPT > log2 20000 ] QTNRXN

Solution8.13: At its core, a driving directionsserviceneedsto storethemap as a Graph, whereeachintersectionandstreetaddressis a vertexandthe roadsconnectingthemareedges.Whena userentersa startingaddressandan endingaddress, it finds the correspondingverticesandfinds the shortestpathconnectingthe two vertices(for somedefinitionof shortest).Thereareseveralissuesthatcomeup:

- Addressョ ッ イ ュ 。 ャ ゥ コ 。 エ ゥ ッ ョ M 。 givenaddressmaybeexpressedby theuserin different ways, for example, QQ Gウ エイ ・・エB may 「 ・ ウ ィ ッ イ ョ ・ ・ 、 エ O

h ウエ h エィ・イ・ ュ。ケ o エ be a city 。 ョ ャ 、 state, just zip codeor vice versa.We needa way to normalizethe addressesto a standardformat.Sometimesan underspecifiedaddressmayneedto be mappedtosomeconcreteaddress, for example, a city nameto thecity 」 ・ エ ・ イ N

d ・ ヲ ゥ ョ ゥ エ ゥ ッ ョ of shortest-differentusersmay have different prefer-encesfor routing, for example, shortestdistanceor fastestpathH 」 ッ ウ ゥ 、 ・ イ ゥ ョ ァ averagespeedontheroad), avoidinguseof freeways,etc. Eachof thesepreferencescanbe capturedby somenotion ofedgelength.

- Approximateshortestdistance-giventheenormityof a graphrep-resentingall the roadsin a large country, it would be fairly dif-ficult for a singleserverto computethe shortestpathusingstan-dardshortestpathalgorithmsand returnin a reasonableamountof time. Howeverusing the knowledgethat most long pathsgothrougha standardsystemof highwaysandthefact thatthenodesandedgesin thegraphイ ・ ー イ ・ ウ ・ エ pointsin euclideanspace, we candevisesomeclever 。 ー ー イ ッ ク 。 エ ゥ ッ 。 ャ ァ ッ イ ゥ エ ィ ュ ウ thatrunmuchfaster.

Solution 8.14: To quickly lookup an ISBN ョオュ「・ キ・ would want ahashtabledata-structure.Howeverit would take o H I time to find theleast-recently-useditem in a hashtableto discard.Oneway to improvethe ー ・ イ ヲ ッ イ ュ 。 」 ・ wouldbe to be lazy aboutgarbagecollectionsuchthatthe costof removalof least-recently-usedISBNs canbe amortizedoverseverallookups.To beconcrete, let'ssaywe wantthecacheto beof size

thenwedonotdeleteanyentriesfrom thehashtabletill it growsto thesizeof 2n. At this point, we go over theentirehashtable, looking at thenumberof t

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

have the samespellhgbut completeIydiffererlt meanhgs(mtimVIrusmemsdifferez1tthhgsh the contextof articleson AIDS md computer

Z l e ゥ z e e Z ェ h s h ゥ ヲ ゥエ ・ 。 キ ィ 。 エ l エ l c ヲ ・ ウ ゥ r 、 ゥ カ ゥ 、 オ 。 ャ Q ュ m 。 カ ・ ュ 、 N e キ ・ ウ ・ ・ ュ オ ウ ・ 、 b

adbothA andB in a singlesession, キ・ ゥァィエ want to recomto 。 ョ ケ ッ ・ readiIIgA.Forcollaborative filtermgto workr weReedto have

a substantialnumberof users.

solution812:Anonlhepokerplayingservicew0111dhavea fror1tmendsystemwhichusersiIItemct with andaback-mdsystemwhichmmtheg r gesmomulooksヲ ッ イ ォ 。 、 etc.

The fror1tmendsystemwould mta1i a UI for 。 」 」 P Q Q エ management-this would cover fir st-timeregistratiORFloggingmhy mm 。 ァゥョァッ iョャ ャゥョ ャ・ー・ イM

s ・ョョ、 ィ ァ o イイ ・ュ c ・ゥ ゥ v ィ ァ ュ ・

ァ。ュ・ー ャ。 ケゥョァ vi エ ィゥ ウ couldbe as ウ p i ・ assomethe ・ of the game(cardsin ィ 」 。 イ 、 ウ on エ ャ 。 「 ャ ・ bets)and 。 ュto enterabet. A more ウ ッ ー ィ ゥ ウ エ ゥ 」 。 エ ・ 、 VI might useJ。 カ。s」イゥー エエッ。ョ ュ ᆳcards「 ・ ゥ ョ Q Y 、 ・ 。 ャ エ 」 ィ 。 ョ ァ ・ the ・ ク ー イ ・ ウ s ゥ o ョ ッ p ャ 。 ケ ・ イ G s iョ 。 。 ァ ・ ウ ウ エ 。 エ オ ウ ュ ・ ウ

-

ァj イ[ R lヲエ ZRZzZエ Z[ ell・Bエッ ヲッイュ 。

S Z ゥZ ZZゥzゥ ゥャ ゥQRRーイッァZエコZRR[ zゥjzヲヲココゥZWRQRZエヲzl」・ Onh

s ・エ イ S エ [ Q Z コZ s [ ゥ コイ [ Z c ゥ Z コ コ エ ] [ D D j Z Z [ [ コ コ Z s ゥ S S R R j z1orsituatior1andclaimthathis htemetcomectx

ZZZZ m 「 ケ ィ 。 rule that the s…will 「 ゥ 、 エ ィ ・ー ャ 。 ケ ・ エ ィ ・ 。 ュ 、 ッ ・ ウ m ュ ー ッ 、 ア Q Q S Z ィ N a ョ ッ

Z ゥ ゥ ゥ・ イ G ウ 」 ッ ョ ョ ・ 」 s m カ 。 ケ エ m ウ ヲ 。 カ ッ イ 。 「 ャ ・ t z a ァ 。 ゥ i i ・

CollusionbetweeI1playersis motheyseriouspDger logs cmbe mit d for examplesof playersworking togetherto

shareknowledge ッ ヲ イ ッ イ ・ ・ コ ・ P ー ャ 。 ケ ・ i エ Ihnf!:;エゥッ ョ ー ャ。 ケ・ イウ clQ エィィ・ュュ ・ャカ ・ ウヲャ 。ァ ウオウー ゥ オウー ャ。 ケ and 」 オ s ョ ・ イ s …イ・ーコZQQR _[RSZZヲZエィエュウ・ャケ SMedproblmyisstmeasyto getwrOI1g.A fairly frequentproblemis ushgpro S Qg u

182CHAPTER8. DESIGNPROBLEMS 183

a seedfor a randomnumbergenerator, which meansthat thereare P ャ ケ

roughly20,000possibleウ ・ ア オ ・ 」 ・ ウ of イ 。 ョ 、 ッ ュ オ ュ 「 ・ イ ウ N This meansthat,ッョ。 。カ・イ。ァ・ knowingthefirst_4 cardsis enoughto predicttheorderofthe restof thecardssincelog2 H QXNPT > log2 20000 ] QTNRXN

Solution8.13: At its core, a driving directionsserviceneedsto storethemap as a Graph, whereeachintersectionandstreetaddressis a vertexandthe roadsconnectingthemareedges.Whena userentersa startingaddressandan endingaddress, it finds the correspondingverticesandfinds the shortestpathconnectingthe two vertices(for somedefinitionof shortest).Thereareseveralissuesthatcomeup:

- Addressョ ッ イ ュ 。 ャ ゥ コ 。 エ ゥ ッ ョ M 。 givenaddressmaybeexpressedby theuserin different ways, for example, QQ Gウ エイ ・・エB may 「 ・ ウ ィ ッ イ ョ ・ ・ 、 エ O

h ウエ h エィ・イ・ ュ。ケ o エ be a city 。 ョ ャ 、 state, just zip codeor vice versa.We needa way to normalizethe addressesto a standardformat.Sometimesan underspecifiedaddressmayneedto be mappedtosomeconcreteaddress, for example, a city nameto thecity 」 ・ エ ・ イ N

d ・ ヲ ゥ ョ ゥ エ ゥ ッ ョ of shortest-differentusersmay have different prefer-encesfor routing, for example, shortestdistanceor fastestpathH 」 ッ ウ ゥ 、 ・ イ ゥ ョ ァ averagespeedontheroad), avoidinguseof freeways,etc. Eachof thesepreferencescanbe capturedby somenotion ofedgelength.

- Approximateshortestdistance-giventheenormityof a graphrep-resentingall the roadsin a large country, it would be fairly dif-ficult for a singleserverto computethe shortestpathusingstan-dardshortestpathalgorithmsand returnin a reasonableamountof time. Howeverusing the knowledgethat most long pathsgothrougha standardsystemof highwaysandthefact thatthenodesandedgesin thegraphイ ・ ー イ ・ ウ ・ エ pointsin euclideanspace, we candevisesomeclever 。 ー ー イ ッ ク 。 エ ゥ ッ 。 ャ ァ ッ イ ゥ エ ィ ュ ウ thatrunmuchfaster.

Solution 8.14: To quickly lookup an ISBN ョオュ「・ キ・ would want ahashtabledata-structure.Howeverit would take o H I time to find theleast-recently-useditem in a hashtableto discard.Oneway to improvethe ー ・ イ ヲ ッ イ ュ 。 」 ・ wouldbe to be lazy aboutgarbagecollectionsuchthatthe costof removalof least-recently-usedISBNs canbe amortizedoverseverallookups.To beconcrete, let'ssaywe wantthecacheto beof size

thenwedonotdeleteanyentriesfrom thehashtabletill it growsto thesizeof 2n. At this point, we go over theentirehashtable, looking at thenumberof t

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution8.15: Assumethat the bandwidthfrom the lab machineis alimiting factor. It is reasonableto first performtrivial optimizations, suchascombiningthearticlesinto a singlefile andcompressingthis file.

Opening1000connections・ 。 」 ィ カ ・ minutesfrom the lab servertothe1000machinesin thedatacenterandtransferringthelatestnewsarti-clesis not feasiblesincethe total datatransferredwill beapproximately5 terabytes(withoutcompression)everyfive minutes.

Sincethebandwidthbetweenmachinesin a datacenteris very high,we can copy the file from the lab machineto a single machinein thedatacenterandhavethe machinesin the datacentercompletethe copy.Insteadof havingjust one machine ウ ・ イ カ ・ ・ file to the イ ・ ュ 。 ゥ ョ ゥ Y Y Y

machines, we canhaveeachmachinethat hasreceivedthe file initiatecopiesto the machinesthathavenot yet receivedthe file. In theory, thisleadsto anexponentialreductionin the time takento performthecopy.

Thereare severalissueswhich haveto be dealtwith: shoulda ma-chineinitiate further copiesbeforeit hasreceivedtheentirefile? H ャゥウ istricky becauseof link or serverfailures.) How shouldtheknowledgeofmachineswhich do not yethavecopiesof the file beshared?(Therecanbea centralrepositoryor serverscansimplycheckothersby randomse-lection.) If thebandwidthbetweenmachinesin a datacenteris not a con-stant, how shouldthe selectionsbemade?(Serverscloseto eachother,e.g., in thesamerack, shouldprefercommunicatingwith eachother.)

Finally, it shouldbe mentionedthat thereare opensourcesolutionsto this problem, e.g., Unison, whichwouldbea goodplaceto start.

Solution X N Q V Z ィ ゥ ョ ォ of thehostsasbeingverticesin a directedgraphwith anedgefrom A to B, if A initially know B's IP address.

We will studyvariantsof this problem-synchronizedor unsynchro-nizedhostsandknown or unknownboundson the network; comparethemwith respectto convergencetime, messagesize, and the numberof messages.We assumethegraphis stronglyconnectedH ・ イ キ ゥ ウ ・ theproblemis unsolvable).

First, assumethat thehostsareall synchronizedto a commonclock(therearestandardprotocolswhich canallow computersto synchronizewithin a few tensof milliseconds;alternately, GPSsignalscanbeusedtoachieveeventightersynchronization).

We will considerthe casewherethe numberof hostsN and the di-ameterD of the networkis known to all the hosts. Our algorithmwille

184 CHAPTER8. DESIGNPROBLEMS185

ZZ RSS イィゥァィ・ウエip。、、イ・ウウ 。、 ィィッウ ャo s s エォョ ャo ws pw B v W Sュオュ 「[ZZZS[s j・Z Z 。Zエjュ ーイッカ・ュィゥ ・ s エip 。 ・ s エ Z W[ Z R[ ZZZヲヲ オ エ。ョ オ ー、 ・ Q w ィ ャ・

It takesD romds.tOCORvergeand lEl·D messagesare communi-cated W e ィ ・ ュ i Q 「 ・ イ ッ ヲ 。 エ ゥ ッ ョ ウ c ッ ョ カ ・ イ ァ M ・ cp v ィ ッ ・ s ・ ィ ッ s エ j ィ _ ・ イ コ エ ゥ [エ Q o i Q エ _ ュ 、 ィ ッ ウ エ ゥ エ 「 ャ ッ キ ウ 。 「 ッ オ エ t ィ ゥ ウ ャ ・ [ [ ヲ j j イ ョ カ ー

:zfz::triemfrdi…ed ィ ッ ウ エ ウ ・ j エ Z ・ コinvolvesN ィッウエウ ゥ カュ・ウウイ・ c ッュ オ iエ Z[pZ_ ヲゥ ゥョ。ャhosts. f オ イ エ エ

。 Z Z Z Z Z ァ ・ ウ エ ッ ー o _ エ ケ 。 カ ・ イ ウ ・ s ・ longerroutes(in エィ・ ゥョ ァo

[RZエーャsZ[QZ[ ヲコエエコゥ [SSWhenlv md D arecompletelyuIIKZIOWIL leaderelectiomcmbepera

Z Z ュ イ ッ 。 、 ゥ ウ e 。 」 ィ ィ ッ 。 ケ ゥ ョ ァ out aュ」・ゥjゥイウウ ・jッ。mQエウッオエァッ ィ「ッイウN In any round, if a ィ

。イ」ョ r…ssagerit choosesORe of the hostsfrom which it re-zztczZJS 。イ・ t 。 ョヲッイュウ ゥエウ ウー 。 。 「 o Qits ctmbackrz22fw ac Cュ Q 、ゥイ・ ・ 」エャ ュュオ i

Z[ j ゥ U ゥゥ [_Z ゥ[ [ ZQゥZゥ[ ゥ [ 4f:[ _Q[R エZR RW_ AエZ [・Z j [コ[ Rイ [R Z__ィィッウ s

e Qjj[[[ [[ j[イoZR[ZイZエZZ_AA_ the 。 ュ c 、 、 ィ ャ イ ッ イ ャ o u s c t ィ ・ ヲ ャ ッ ィ ァ 。 ャhostsbecauseth N _ c 。 i i t Q o エ be directly generalizedto 。 s ケ i ャ 」 ィ イ ッ イ Q P u s

:nereIS no notion of a イッオ 、N However'W

ココ zj。カュァエュエィ・ゥイ messagesキ ゥ エ ィ ・ イ ッ j Zー ・ イ ヲ ッ イ ュ ィ ァ ェ [ Z [ R Z コ ヲ t ュ ・ ッ ュ all ゥ エ ウ s ィ 「 ッ イ ウ 「 ・ ヲ ッ イ ・

R Q U T U D W Q U U Z USolution847:DiscoveryaMleader、 ・ ョ are ゥ 、 ・ イ 。 エ ウ ッ the solu-honto Problem8.16worksheretoo.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution8.15: Assumethat the bandwidthfrom the lab machineis alimiting factor. It is reasonableto first performtrivial optimizations, suchascombiningthearticlesinto a singlefile andcompressingthis file.

Opening1000connections・ 。 」 ィ カ ・ minutesfrom the lab servertothe1000machinesin thedatacenterandtransferringthelatestnewsarti-clesis not feasiblesincethe total datatransferredwill beapproximately5 terabytes(withoutcompression)everyfive minutes.

Sincethebandwidthbetweenmachinesin a datacenteris very high,we can copy the file from the lab machineto a single machinein thedatacenterandhavethe machinesin the datacentercompletethe copy.Insteadof havingjust one machine ウ ・ イ カ ・ ・ file to the イ ・ ュ 。 ゥ ョ ゥ Y Y Y

machines, we canhaveeachmachinethat hasreceivedthe file initiatecopiesto the machinesthathavenot yet receivedthe file. In theory, thisleadsto anexponentialreductionin the time takento performthecopy.

Thereare severalissueswhich haveto be dealtwith: shoulda ma-chineinitiate further copiesbeforeit hasreceivedtheentirefile? H ャゥウ istricky becauseof link or serverfailures.) How shouldtheknowledgeofmachineswhich do not yethavecopiesof the file beshared?(Therecanbea centralrepositoryor serverscansimplycheckothersby randomse-lection.) If thebandwidthbetweenmachinesin a datacenteris not a con-stant, how shouldthe selectionsbemade?(Serverscloseto eachother,e.g., in thesamerack, shouldprefercommunicatingwith eachother.)

Finally, it shouldbe mentionedthat thereare opensourcesolutionsto this problem, e.g., Unison, whichwouldbea goodplaceto start.

Solution X N Q V Z ィ ゥ ョ ォ of thehostsasbeingverticesin a directedgraphwith anedgefrom A to B, if A initially know B's IP address.

We will studyvariantsof this problem-synchronizedor unsynchro-nizedhostsandknown or unknownboundson the network; comparethemwith respectto convergencetime, messagesize, and the numberof messages.We assumethegraphis stronglyconnectedH ・ イ キ ゥ ウ ・ theproblemis unsolvable).

First, assumethat thehostsareall synchronizedto a commonclock(therearestandardprotocolswhich canallow computersto synchronizewithin a few tensof milliseconds;alternately, GPSsignalscanbeusedtoachieveeventightersynchronization).

We will considerthe casewherethe numberof hostsN and the di-ameterD of the networkis known to all the hosts. Our algorithmwille

184 CHAPTER8. DESIGNPROBLEMS185

ZZ RSS イィゥァィ・ウエip。、、イ・ウウ 。、 ィィッウ ャo s s エォョ ャo ws pw B v W Sュオュ 「[ZZZS[s j・Z Z 。Zエjュ ーイッカ・ュィゥ ・ s エip 。 ・ s エ Z W[ Z R[ ZZZヲヲ オ エ。ョ オ ー、 ・ Q w ィ ャ・

It takesD romds.tOCORvergeand lEl·D messagesare communi-cated W e ィ ・ ュ i Q 「 ・ イ ッ ヲ 。 エ ゥ ッ ョ ウ c ッ ョ カ ・ イ ァ M ・ cp v ィ ッ ・ s ・ ィ ッ s エ j ィ _ ・ イ コ エ ゥ [エ Q o i Q エ _ ュ 、 ィ ッ ウ エ ゥ エ 「 ャ ッ キ ウ 。 「 ッ オ エ t ィ ゥ ウ ャ ・ [ [ ヲ j j イ ョ カ ー

:zfz::triemfrdi…ed ィ ッ ウ エ ウ ・ j エ Z ・ コinvolvesN ィッウエウ ゥ カュ・ウウイ・ c ッュ オ iエ Z[pZ_ ヲゥ ゥョ。ャhosts. f オ イ エ エ

。 Z Z Z Z Z ァ ・ ウ エ ッ ー o _ エ ケ 。 カ ・ イ ウ ・ s ・ longerroutes(in エィ・ ゥョ ァo

[RZエーャsZ[QZ[ ヲコエエコゥ [SSWhenlv md D arecompletelyuIIKZIOWIL leaderelectiomcmbepera

Z Z ュ イ ッ 。 、 ゥ ウ e 。 」 ィ ィ ッ 。 ケ ゥ ョ ァ out aュ」・ゥjゥイウウ ・jッ。mQエウッオエァッ ィ「ッイウN In any round, if a ィ

。イ」ョ r…ssagerit choosesORe of the hostsfrom which it re-zztczZJS 。イ・ t 。 ョヲッイュウ ゥエウ ウー 。 。 「 o Qits ctmbackrz22fw ac Cュ Q 、ゥイ・ ・ 」エャ ュュオ i

Z[ j ゥ U ゥゥ [_Z ゥ[ [ ZQゥZゥ[ ゥ [ 4f:[ _Q[R エZR RW_ AエZ [・Z j [コ[ Rイ [R Z__ィィッウ s

e Qjj[[[ [[ j[イoZR[ZイZエZZ_AA_ the 。 ュ c 、 、 ィ ャ イ ッ イ ャ o u s c t ィ ・ ヲ ャ ッ ィ ァ 。 ャhostsbecauseth N _ c 。 i i t Q o エ be directly generalizedto 。 s ケ i ャ 」 ィ イ ッ イ Q P u s

:nereIS no notion of a イッオ 、N However'W

ココ zj。カュァエュエィ・ゥイ messagesキ ゥ エ ィ ・ イ ッ j Zー ・ イ ヲ ッ イ ュ ィ ァ ェ [ Z [ R Z コ ヲ t ュ ・ ッ ュ all ゥ エ ウ s ィ 「 ッ イ ウ 「 ・ ヲ ッ イ ・

R Q U T U D W Q U U Z USolution847:DiscoveryaMleader、 ・ ョ are ゥ 、 ・ イ 。 エ ウ ッ the solu-honto Problem8.16worksheretoo.

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter9

DiscreteMathem.atics

Solution9.1:It is tempthgto try md pair up termsh the I111meratorand denominatorfor the ・ ク ー イ ・ ウ ウ ゥ ヲ ッ イ H セ I that have 」 ッ ュ ュ ヲ 。 イ ウ[ 、 trv to somehow」 。 ョ 」 ・ ャ ョ out. This approachis unsatisfactory

becauseof theneedto havefactorizations.Thebimmial coefficierltssatisfyseveraliderltitiesr the mostbasicof

which is theadditionformula:\1112//

\111l/11--kn/tall\|

ャャェO

ォ/IlI-\

N ッオウー イッ ッヲウ of エ ィ ゥ ウ ゥ 、 ・ ョ Q エ ゥ エ ケ セ イ 。 ョ Q Y ゥ ョ Q Y from thecombinatorialゥ ョ Q エ ・ イ ー イ ・ エ 。 エ ゥ o エ ッ ゥ ョ 、 オ 」 エ ゥ ゥ ッ i Q andfinally, direct ュ 。 ョ Q ゥ ー ャ Q ャ 。 エ ゥ ッ i Q of the ・ ク ー イ ・ ウ M

ゥ ウ ゥ 、 ・ュ ョe (;)and (;), bothof which areL meidividualresultsfrom

the subcali;are ゥ ョ エ セ ァ ・ イ ウ and if H セ I canbe イ ・ ー イ ・ ウ ・ ・ 、 by an int, theycantoo; so, overflow is not a concern.

The イ イ ウ ゥ ッ ョ canleadto repeatedsubcallsand 」 ッ ョ オ ・ ョ エ ャ ケ expo-ョ m ャ ュ m ・ ウ N t ィ ・ イ ・ is measyfix--mheintermediateresultsash

、 ケi Q。ュゥ」 ー イッ ァ イ。イ ュ」。ョ「 ・ combinedゥ i Q P H Q I エ ゥ ュ ・ ケ ゥ ・ ャ 、 ゥ イ 。 ョ o H W z R I c ッ ュ Q ー ャ ・ ク ゥ エ ケ 「 ッ オ ョ 、 N

Solution 9.2: Let f H 「 ・ the numberof ways of climbing n stairsthrougha combimtionof OReor WO Steps-We エ ・ that F(I) = 1 andF(0)=1.Now, allpathsthat leadusto crossウ エ ・ ー ウ eitherstartwith a

QZ[RR_[ZRRQRZZRSQQZ・ゥコケエZ ZRSUQ

187

waysof completingthepath.Hence

f H ] f H M 1) + fHM 2).

This leadsto a simple dynamicprogrammingalgorithmthat cancom-puteF(n) in O(n) time. An interestingthing to notehereis thatF(n) hasthe sameイ ・ 」 イ ・ ョ 」 ・ relationshipas the FibonaccinumbersandF(n) isactuallythe H K 1)-th f ゥ 「 ッ ョ 。 」 」 ゥ ョ 「 ・ イ N

Solution 9.3: This problemcanbe modeledusing undirectedgraphswhereverticescorrespondto guests. Thereis a pair of edgesbetweeneverypair of guests.Color anedgebetweenapairof guest"blue" if theyarefriends, otherwise, color it "red".

Thenthe theoremis ・アオゥカ。ャ・ エッ the claim that in anyclique on sixvertices, wheretheedgesareeitherblueor red, thereis a subsetof threevertices, all connectedby edgesof thesamecolor.

Chooseanyvertexv. Examinethefive edgeswith anendpointin Q

Theremustbe at leastthreeedgeswhich are of the samecolor c (thisfollows from the ー ゥ ァ ・ ッ ィ ッ ャ ・ principle). Let H I H セ I H I be threesuchedges.Now, either thereis an edgecoloredc betweenoneof Q , セ。ョ、 in which case。 ョ 、 the verticesin セ 。 ョ 、 。 イ ・ connectedbyedgescoloredc or there is no suchedge, in which 」 。 ウ ・ セ 。 ョ 、 。 イ ・

themselvesconnectedby edgesthatareof thesamecolor.

Solution9.4: Numberthe doors from 1 to 500. Let's startwith some・ ク ー i ・ ウ M 、 ッ ッ イ 12 is toggledon daysI , 2, 3, 4, 6, 12; door 3 is toggledo 、 。 ケ ウ 1 and3; door1 is toggledon day1; door500 is toggledon days1, 2, 4, 5, 10, 20, 25, 50, 100, 125, 250, 500.

Q・ ー。エエ・イ エィ。エ emergesis the following: a door is toggledasmanytimesasits id hasdivisors.Divisorscomein pairs: 12 = 1 x 12 = 2 x 6 =3 x 4. So, thetotalnumberof divisorsis even, exceptwhenthenumberisaperfectsquare.For theperfectsquarecase, thetotalnumberof divisorsis odd. Thereforethe doorsthat are openat the endof the processarethosewith ids 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256,289, 324, 361, 400, 441, 484-theseare22 doorsaltogether.In the generalcase, it wouldbe l yi j where ゥ ウ thenumberof doors.

Solution Y l ・ エ F(k , l) be the maximumnumberof floors that canbetestedby k identicalballs andat most l drops.We know thatF(I , l) = l.If we are given an additionalball to drop, we candrop the first ball atF(k, ャ QI ッイN If it breaks, thenwecanusetheremainingballsandl - 1dropsto determinethe floor exactly; if it doesnot break, thenwe coulddrop the first ball at F(k, l - 1) + F(k, l - 2) floor. If it breaks, we canusetheremainingk 「。ャャウ Q、 l - 2 dropsto narrowdowntheexactfloorbetweenF(k ,l

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter9

DiscreteMathem.atics

Solution9.1:It is tempthgto try md pair up termsh the I111meratorand denominatorfor the ・ ク ー イ ・ ウ ウ ゥ ヲ ッ イ H セ I that have 」 ッ ュ ュ ヲ 。 イ ウ[ 、 trv to somehow」 。 ョ 」 ・ ャ ョ out. This approachis unsatisfactory

becauseof theneedto havefactorizations.Thebimmial coefficierltssatisfyseveraliderltitiesr the mostbasicof

which is theadditionformula:\1112//

\111l/11--kn/tall\|

ャャェO

ォ/IlI-\

N ッオウー イッ ッヲウ of エ ィ ゥ ウ ゥ 、 ・ ョ Q エ ゥ エ ケ セ イ 。 ョ Q Y ゥ ョ Q Y from thecombinatorialゥ ョ Q エ ・ イ ー イ ・ エ 。 エ ゥ o エ ッ ゥ ョ 、 オ 」 エ ゥ ゥ ッ i Q andfinally, direct ュ 。 ョ Q ゥ ー ャ Q ャ 。 エ ゥ ッ i Q of the ・ ク ー イ ・ ウ M

ゥ ウ ゥ 、 ・ュ ョe (;)and (;), bothof which areL meidividualresultsfrom

the subcali;are ゥ ョ エ セ ァ ・ イ ウ and if H セ I canbe イ ・ ー イ ・ ウ ・ ・ 、 by an int, theycantoo; so, overflow is not a concern.

The イ イ ウ ゥ ッ ョ canleadto repeatedsubcallsand 」 ッ ョ オ ・ ョ エ ャ ケ expo-ョ m ャ ュ m ・ ウ N t ィ ・ イ ・ is measyfix--mheintermediateresultsash

、 ケi Q。ュゥ」 ー イッ ァ イ。イ ュ」。ョ「 ・ combinedゥ i Q P H Q I エ ゥ ュ ・ ケ ゥ ・ ャ 、 ゥ イ 。 ョ o H W z R I c ッ ュ Q ー ャ ・ ク ゥ エ ケ 「 ッ オ ョ 、 N

Solution 9.2: Let f H 「 ・ the numberof ways of climbing n stairsthrougha combimtionof OReor WO Steps-We エ ・ that F(I) = 1 andF(0)=1.Now, allpathsthat leadusto crossウ エ ・ ー ウ eitherstartwith a

QZ[RR_[ZRRQRZZRSQQZ・ゥコケエZ ZRSUQ

187

waysof completingthepath.Hence

f H ] f H M 1) + fHM 2).

This leadsto a simple dynamicprogrammingalgorithmthat cancom-puteF(n) in O(n) time. An interestingthing to notehereis thatF(n) hasthe sameイ ・ 」 イ ・ ョ 」 ・ relationshipas the FibonaccinumbersandF(n) isactuallythe H K 1)-th f ゥ 「 ッ ョ 。 」 」 ゥ ョ 「 ・ イ N

Solution 9.3: This problemcanbe modeledusing undirectedgraphswhereverticescorrespondto guests. Thereis a pair of edgesbetweeneverypair of guests.Color anedgebetweenapairof guest"blue" if theyarefriends, otherwise, color it "red".

Thenthe theoremis ・アオゥカ。ャ・ エッ the claim that in anyclique on sixvertices, wheretheedgesareeitherblueor red, thereis a subsetof threevertices, all connectedby edgesof thesamecolor.

Chooseanyvertexv. Examinethefive edgeswith anendpointin Q

Theremustbe at leastthreeedgeswhich are of the samecolor c (thisfollows from the ー ゥ ァ ・ ッ ィ ッ ャ ・ principle). Let H I H セ I H I be threesuchedges.Now, either thereis an edgecoloredc betweenoneof Q , セ。ョ、 in which case。 ョ 、 the verticesin セ 。 ョ 、 。 イ ・ connectedbyedgescoloredc or there is no suchedge, in which 」 。 ウ ・ セ 。 ョ 、 。 イ ・

themselvesconnectedby edgesthatareof thesamecolor.

Solution9.4: Numberthe doors from 1 to 500. Let's startwith some・ ク ー i ・ ウ M 、 ッ ッ イ 12 is toggledon daysI , 2, 3, 4, 6, 12; door 3 is toggledo 、 。 ケ ウ 1 and3; door1 is toggledon day1; door500 is toggledon days1, 2, 4, 5, 10, 20, 25, 50, 100, 125, 250, 500.

Q・ ー。エエ・イ エィ。エ emergesis the following: a door is toggledasmanytimesasits id hasdivisors.Divisorscomein pairs: 12 = 1 x 12 = 2 x 6 =3 x 4. So, thetotalnumberof divisorsis even, exceptwhenthenumberisaperfectsquare.For theperfectsquarecase, thetotalnumberof divisorsis odd. Thereforethe doorsthat are openat the endof the processarethosewith ids 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256,289, 324, 361, 400, 441, 484-theseare22 doorsaltogether.In the generalcase, it wouldbe l yi j where ゥ ウ thenumberof doors.

Solution Y l ・ エ F(k , l) be the maximumnumberof floors that canbetestedby k identicalballs andat most l drops.We know thatF(I , l) = l.If we are given an additionalball to drop, we candrop the first ball atF(k, ャ QI ッイN If it breaks, thenwecanusetheremainingballsandl - 1dropsto determinethe floor exactly; if it doesnot break, thenwe coulddrop the first ball at F(k, l - 1) + F(k, l - 2) floor. If it breaks, we canusetheremainingk 「。ャャウ Q、 l - 2 dropsto narrowdowntheexactfloorbetweenF(k ,l

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

ャ・ maximumwe canmakebybettingonblackcardsis

fHォK fHォ l - i)

QB(C,r ,t) = ,_ イ p ス セ x , min (Q(c+ 「 エMQI qH」M b,r-1 ,t)).「 サ o Q R N N N 」 ス

double blackLowerBound= Math.min(computeBestPayoff(c - b I r - 11 t - 1) I

computeBestPayoff(c + b I r I t - 1) );

double betterMove =

if (( r == t) I I ( r ==0) )return c * Math.pow(2It);

double best;if ((best = cacheLookup(cl r

lt)) != -1.0) {

return best;else {for (int b = 0 ; b <= c; b++) {

double redLowerBound = Math.min(computeBestPayoff(c + b I r - 11 t - 1) I

computeBestPayoff(c - b I r I t - 1) );

189

h ィ o w ・ カ ・ イ ゥ ヲ キ ・ 、 ゥ イ

イ オ ョ ウ ヲ ッ イ an u i Q 。 」 」 ・ ー エ 。 「 ャ y ャ ッ イ ァ time. This is becausewewill be ・ ク ー ャ ッ イ ゥ ァ

pathsfor which C growsvery large.shcewe are ァ ゥ カ ・ エ ィ ・ maxipayoff P 。 dollar whenfractionalamountscanbebet is lessthan9.09.we canprune 」 ッ ュ ー ャ エ ゥ ッ ョ ウ for Q(c,r ,t) when 」 Y P Y N ィ ・ ヲ ッ ャ ャ ッ キ ゥ ョ Q

_ ッ 、 ・ ゥ ュ ー ャ ・ ュ ・ ョ エ ウ the dynamic ー イ ッ ァ イ 。 ュ ュ ュ i Q ゥ ァ algorithmwith ゥ ウ ー イ u ャ :mg; Q エ 」 ッ ュ Q ー ャ Q エ ・ ウ themaximumー。 ケ ッ ヲ ヲ X P X in two minutes.A

1 limport java.lang.Math;; , import java.util N h 。 ー [

4 I public class CardSelect {5 I6 I private static int numCards= 52:7 I p ri vatestati c in t ョオイ 、 = 26:8 I private static int ー ・ イ b ッ オ ョ 、 Y P Y ;9 private static HashMap<IntegerI 、ッオ「ャ・ ^ cache;

10 ,li public static void ュ 。 ゥ ョ H s エ ァ [] args ) {セZ I cache = セ・キ _HashMap<IntegerI セ 、セセ「ャ・ '-' [] [] ; () ;13 I computeBestPayoff(100);14 I }15

[セ I ーイゥカ ウ t ati c double 」 。 」 ィ ・ l ッ ッ ォ H ゥ ョ エ cl

int rl

int t) {17|returncache mMmkey(c)?cacheァ オ H 」 I { イ } { エ } Z Q N P [18 I }19

:? I ー 。エ・ stati c void computeBestPayoff(int cash) {Zセ System.out. セ イ ゥ ョ エ ャ ョ ("Optimumwpay;fCisw" +RZ I 」ッイイ ョーオ ャーu オエ・b・ ・ s エp。ケッヲ ヲH hI I ョオ Qュ ュ ョ23 I24

25 public static double 」 ッ ュ ー b ・ ウ エ p 。 ケ ッ ヲ ヲ (26 int c l int r

lint エ セ I {

27 I if (c >= upperBound)28 I return c;293031323334353637383940414243444546

CHAPTER9. DISCRETEMATHEMATICS

HenceQ(c,r ,t) = max(QR(c,r.t) ,QB(c,r ,t)) which yields a dynamicprogrammingalgorithm for computing the maximum payoff-basecasesareof theform Q(c,0,t) andQ(c,t ,t) ,bothof which arec x 2t .

Given the aboverecurrenceイ ・ ャ 。 エ ゥ ッ ウ ィ ゥ ー it is straightforwardto ob-カ ・ that Q l) = (k+k-1

) sinceit follows ・ ク 。 」 エ エ ィ ・ samerecur-rencerelationship.(Oneeasyway to noticethis is to tabulatesomecon-cretevaluesfor F(k,l).) Now, sinceF(k,l) monotonicallyincreasesin kand l, we caneasilyinvert it to determinethenumberof dropsneeded,giventhenumberof ballsandthenumberof drops.

Solution9.6: A goodwaytobeginthisproblemis to comeupwith somestrategythatguaranteesapositivereturn.It is possibleto guaranteea 2xreturnby waiting till the lastcardandbettingthe ・ ョ エ ゥ イ ・ ョ ッ オ ョ エ on thelast cardwhosecolor is オョゥアオ・ 、・エ・イュゥョ・、 by the the 51 cardsthathavealreadybeenseen.

To do betterthana 2x return, considerthe caseof a deckof 4 cardswith 2 red cardsand 2 black cards. If we do not bet on the first card,therewill be threeremainingcards.Assume, without lossof generality,that two cardsareblack and one is red. If we bet エ ィ ・ next cardbeingblackandare ウ オ 」 」 ・ thenwehaveD セ whichwe candoubleonthelastcardfor a i > 2 return. If we Q P エ ィ ・ ョ エ ャ キ ッ remainingcardsareblack, in which casewe candoubleour remainingmoneytwice, i.e.,achievea セ x 2 x 2 = i > 2 イ・エオイ nッエ・ that this 。 ョ 。 ャ ケ 。 ウ ウ オ ュ ・ ウ wecanbetarbitraryfractionsof themoneywe possess.

Now, we considerthe casewherewe can P ャ ケ bet in pennyincre-ments.Let Q( 」 I bethemostwecanguarantee,whenwehaveccentsto ァ 「 ャ ・ キ ゥ エ ィ ャ 、 there are r red cardsremainingout of a total of tcards. We canbet b cents, 0 セ b セ c on the next card. Sincewe haveto designastrategythatmaximizestheworst-casepayoff, themaximum

ッ オ ョ エ we canmakeonbettingonredcardsis givenby

QR(C,r ,t) セQQ_M セク ョ HqH」 「 r - 1,t - 1),Q(c - b,r ,t)).bE{O,1,2,...,c}

。 ャュ・ョエ till k-1 dropsof エャ ゥイウエ ball,wecanエ ・ ウ エ エ P R [ ゥ F(k,l-i)floors. Hence

188

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

ャ・ maximumwe canmakebybettingonblackcardsis

fHォK fHォ l - i)

QB(C,r ,t) = ,_ イ p ス セ x , min (Q(c+ 「 エMQI qH」M b,r-1 ,t)).「 サ o Q R N N N 」 ス

double blackLowerBound= Math.min(computeBestPayoff(c - b I r - 11 t - 1) I

computeBestPayoff(c + b I r I t - 1) );

double betterMove =

if (( r == t) I I ( r ==0) )return c * Math.pow(2It);

double best;if ((best = cacheLookup(cl r

lt)) != -1.0) {

return best;else {for (int b = 0 ; b <= c; b++) {

double redLowerBound = Math.min(computeBestPayoff(c + b I r - 11 t - 1) I

computeBestPayoff(c - b I r I t - 1) );

189

h ィ o w ・ カ ・ イ ゥ ヲ キ ・ 、 ゥ イ

イ オ ョ ウ ヲ ッ イ an u i Q 。 」 」 ・ ー エ 。 「 ャ y ャ ッ イ ァ time. This is becausewewill be ・ ク ー ャ ッ イ ゥ ァ

pathsfor which C growsvery large.shcewe are ァ ゥ カ ・ エ ィ ・ maxipayoff P 。 dollar whenfractionalamountscanbebet is lessthan9.09.we canprune 」 ッ ュ ー ャ エ ゥ ッ ョ ウ for Q(c,r ,t) when 」 Y P Y N ィ ・ ヲ ッ ャ ャ ッ キ ゥ ョ Q

_ ッ 、 ・ ゥ ュ ー ャ ・ ュ ・ ョ エ ウ the dynamic ー イ ッ ァ イ 。 ュ ュ ュ i Q ゥ ァ algorithmwith ゥ ウ ー イ u ャ :mg; Q エ 」 ッ ュ Q ー ャ Q エ ・ ウ themaximumー。 ケ ッ ヲ ヲ X P X in two minutes.A

1 limport java.lang.Math;; , import java.util N h 。 ー [

4 I public class CardSelect {5 I6 I private static int numCards= 52:7 I p ri vatestati c in t ョオイ 、 = 26:8 I private static int ー ・ イ b ッ オ ョ 、 Y P Y ;9 private static HashMap<IntegerI 、ッオ「ャ・ ^ cache;

10 ,li public static void ュ 。 ゥ ョ H s エ ァ [] args ) {セZ I cache = セ・キ _HashMap<IntegerI セ 、セセ「ャ・ '-' [] [] ; () ;13 I computeBestPayoff(100);14 I }15

[セ I ーイゥカ ウ t ati c double 」 。 」 ィ ・ l ッ ッ ォ H ゥ ョ エ cl

int rl

int t) {17|returncache mMmkey(c)?cacheァ オ H 」 I { イ } { エ } Z Q N P [18 I }19

:? I ー 。エ・ stati c void computeBestPayoff(int cash) {Zセ System.out. セ イ ゥ ョ エ ャ ョ ("Optimumwpay;fCisw" +RZ I 」ッイイ ョーオ ャーu オエ・b・ ・ s エp。ケッヲ ヲH hI I ョオ Qュ ュ ョ23 I24

25 public static double 」 ッ ュ ー b ・ ウ エ p 。 ケ ッ ヲ ヲ (26 int c l int r

lint エ セ I {

27 I if (c >= upperBound)28 I return c;293031323334353637383940414243444546

CHAPTER9. DISCRETEMATHEMATICS

HenceQ(c,r ,t) = max(QR(c,r.t) ,QB(c,r ,t)) which yields a dynamicprogrammingalgorithm for computing the maximum payoff-basecasesareof theform Q(c,0,t) andQ(c,t ,t) ,bothof which arec x 2t .

Given the aboverecurrenceイ ・ ャ 。 エ ゥ ッ ウ ィ ゥ ー it is straightforwardto ob-カ ・ that Q l) = (k+k-1

) sinceit follows ・ ク 。 」 エ エ ィ ・ samerecur-rencerelationship.(Oneeasyway to noticethis is to tabulatesomecon-cretevaluesfor F(k,l).) Now, sinceF(k,l) monotonicallyincreasesin kand l, we caneasilyinvert it to determinethenumberof dropsneeded,giventhenumberof ballsandthenumberof drops.

Solution9.6: A goodwaytobeginthisproblemis to comeupwith somestrategythatguaranteesapositivereturn.It is possibleto guaranteea 2xreturnby waiting till the lastcardandbettingthe ・ ョ エ ゥ イ ・ ョ ッ オ ョ エ on thelast cardwhosecolor is オョゥアオ・ 、・エ・イュゥョ・、 by the the 51 cardsthathavealreadybeenseen.

To do betterthana 2x return, considerthe caseof a deckof 4 cardswith 2 red cardsand 2 black cards. If we do not bet on the first card,therewill be threeremainingcards.Assume, without lossof generality,that two cardsareblack and one is red. If we bet エ ィ ・ next cardbeingblackandare ウ オ 」 」 ・ thenwehaveD セ whichwe candoubleonthelastcardfor a i > 2 return. If we Q P エ ィ ・ ョ エ ャ キ ッ remainingcardsareblack, in which casewe candoubleour remainingmoneytwice, i.e.,achievea セ x 2 x 2 = i > 2 イ・エオイ nッエ・ that this 。 ョ 。 ャ ケ 。 ウ ウ オ ュ ・ ウ wecanbetarbitraryfractionsof themoneywe possess.

Now, we considerthe casewherewe can P ャ ケ bet in pennyincre-ments.Let Q( 」 I bethemostwecanguarantee,whenwehaveccentsto ァ 「 ャ ・ キ ゥ エ ィ ャ 、 there are r red cardsremainingout of a total of tcards. We canbet b cents, 0 セ b セ c on the next card. Sincewe haveto designastrategythatmaximizestheworst-casepayoff, themaximum

ッ オ ョ エ we canmakeonbettingonredcardsis givenby

QR(C,r ,t) セQQ_M セク ョ HqH」 「 r - 1,t - 1),Q(c - b,r ,t)).bE{O,1,2,...,c}

。 ャュ・ョエ till k-1 dropsof エャ ゥイウエ ball,wecanエ ・ ウ エ エ P R [ ゥ F(k,l-i)floors. Hence

188

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

190 CHAPTER9. DISCRETEMATHEMATICS

エGG

uqjョオQT

GGMアオa

ヲPWGG

uqjョオ

4445555555555666Math.max(blackLowerBound/ redLowerBound);best = Math.max(best/ betterMove);

double [] [] tmp;if (! cache.containsKey( c)) {

tmp = new double{ ョ オ ュ r ・ 、 K Q } { ョ オ イ c 。 イ 、 ウ K Q } [

for (int i = 0 ; i <= ョュ r・、[ i++ )for (int j = 0 ; j <= numCards; j++ )

ー{ゥ}{ェ} = -1. 0;cache.put(c O ー I ;

cache.get(c)[r][t] = best;return best;

Solution9.7: Theprocesswill convergesinceateachウ エ ・ ー イ ・ 、 オ 」 ・ エ ィ ・

numberof integersin A by one. The numberof odd integersremovedin eachstepis evensincewe either removetwo ッ 、 、 ャ エ ・ ァ ・ イ ウ or none.Thereforeif therewereanevennumberof oddintegerstobeginwith, thelast integermustbe ・ カ ・ ゥ ヲ therewereanoddnumberof oddnumbersto beginwith, it mustbeodd.

Solution 9.8: Considerthe thoughtexperimentof startingat an arbi-trary 」 ゥ エ ケ n ゥ エ ィ sufficiently largeamountof gasso thatwe cancompletethe loop. In this experiment, we note エ ィ ・ ッ オ エ of gasin the tank asthevehiclegoesthroughtheloop ateachcity beforeloadiI1gthegaskeptk1thatcity for the vehicle.Ifwe startat a different city with a differ-entamountof gas, theamountof gasin the tankat eachcity shouldstillvaryh thesamefashioz1witha constmtoffset.Ifwepick thecity wherethe amountof gasin the tank is minimum as the startingpoint/ clearlywe will neverrun out of gas. This computationcanbe easily doneinlinear-timewith a singlepassoverall thecities.

Solution 9.9: Considerthe casewhere exactly one personhas green・ ケ ・ ウ N ィ ・ statementfrom the explorerwould makeit clear to the per-sonwith ァ イ ・ ・ ・ ケ ・ ウ thathehasgreeneyessincenobodyelsethatheseeshasgreeneyes.

Now, ウセーーッウ・ thereare two inhabitantswith greeneyes. The firstdav, eachof thesetwo inhabitantswould seeexactlyone otherpersonwith greeneyes Eachwould seethe otherpersonOI1theseconddaytoor from which theycouldhfer thattheremustbewohhabitmtswithァ イ ・ ・ ・ ケ ・ ウ thesecondonebeingエ ィ ・ ュ ウ ・ ャ イ ・ ウ N Hencebothof themwouldleavethesecondday.

UsiI1g iIIductiozu we candemoIIStratethat if thereare k inhabitmtswith greeneyes, all thegreen-eyedinhabitantswould leaveafterthek-th

191

assembly.We alreadysawthebasecase, ォ QN Supposethe inductionィ ケ ー ッ ・ ウ ゥ ウ holds for k - 1. If thereare k inhabitantswith greeneyes,・。」ィ 。「ゥエ ャエ with greeneyeswould seek - 1 otherinhabitantswithァ イ ・ ・ ・ ケ ・ ウ N If at the k-th assembly, they seethat nobodyhasdeparted,it would indicatethattheythemselveshavegreeneyesandhenceall thegreen-eyedinhabitantswould leaveonthe k-th day.

As for the secondpart of the ア オ ・ ウ エ ゥ ッ O for k = 1/ it is fairly obviousthattheexplorergavenewknowledgeto thepersonwith greeneyes.Forothercases, thenewinformationis abit subtle.For k = 2/ thegreen-eyedinhabitantswould be able to infer the color of their eyeson the secondday basedon the information that ・ t ・ イ ケ ッ ョ ・ on the island knows thattherearegreen-eyedinhabitantsandyet no oneleft. For k = 3/ theyareableto inferbecauseeveryoneknowsthateveryoneknowsthattherearegreen-eyedinhabitantsandyet エ ィ ・ seconddayno oneleft.

Supposex is somefact and E (x) representsthe fact エ ィ 。 エ ・ カ カ ・ イ ケ ッ

ォ ョ ッ キ ウ x to be true. In this case, let g representthe fact that therearesomegreen-eyedinhabitantson the island. Thenon the k-th day, all theァ イ ・ ・ ・ ケ ・ 、 inhabitantswould usethe fact EK (g) to infer that theyhavegreen-eyes.Essentially, what theexplorerdid by announcingthe fact intheassemblyis thatit became"commonknowledge", i.e., e HァI becametrue.

Solution9.10: If the 。 ウ ウ オ ュ ー エ ゥ ッ ゥ ウ that onceyou havebrokenthe barinto two pieces, theybecomeseparateproblems, thenit doesnot matterwhatorderyou do it-you will require15 totalbreaksin anyscenario.

If, on the otherhand, the assumptionis that the wholebar staysto-gether H 。 ウ キ ッ オ ャ 、 if you werebreakingit inside its wrapper, for in-stance), thenyou cando a little better. You could simply breakit alongall axes (say, first the vertical and then the horizontal) for a total of 6breaks.

Solution 9.11: Player1 can alwayswin. The key ッ 「 ウ ・ 。 エ ゥ ッ ョ in thisgameis that we want to force the play to be symmetricalaroundthediagonal, i.e., (0,0) , (1 ,1),…, H _ I with our opponentforced to movefirst in termsof breakingthesymmetry.If thatis thecase, we canfolloweachof his movesby a matchingmovereflectedin this diagonalwhichwill eventuallyforcehim to selectthe (0,0) space.

ャ ・ way to force this type of play is to be the first ー ・ イ ウ ッ エ ッ select(1 , I)-this causestheplay areato bejust the 」 ッ ャ オ ュ Q (0, {P I andtherow H { P } P I (i.e., an "L" shape). At that ー ッ ゥ ョ キ ・ cansuccessfullymirror anymovethatPlayer2 makes, forcing him to eventuallychoose(0/0).

2l Choose(1J)i1 ivvゥャャ ャ ョーHI :

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

190 CHAPTER9. DISCRETEMATHEMATICS

エGG

uqjョオQT

GGMアオa

ヲPWGG

uqjョオ

4445555555555666Math.max(blackLowerBound/ redLowerBound);best = Math.max(best/ betterMove);

double [] [] tmp;if (! cache.containsKey( c)) {

tmp = new double{ ョ オ ュ r ・ 、 K Q } { ョ オ イ c 。 イ 、 ウ K Q } [

for (int i = 0 ; i <= ョュ r・、[ i++ )for (int j = 0 ; j <= numCards; j++ )

ー{ゥ}{ェ} = -1. 0;cache.put(c O ー I ;

cache.get(c)[r][t] = best;return best;

Solution9.7: Theprocesswill convergesinceateachウ エ ・ ー イ ・ 、 オ 」 ・ エ ィ ・

numberof integersin A by one. The numberof odd integersremovedin eachstepis evensincewe either removetwo ッ 、 、 ャ エ ・ ァ ・ イ ウ or none.Thereforeif therewereanevennumberof oddintegerstobeginwith, thelast integermustbe ・ カ ・ ゥ ヲ therewereanoddnumberof oddnumbersto beginwith, it mustbeodd.

Solution 9.8: Considerthe thoughtexperimentof startingat an arbi-trary 」 ゥ エ ケ n ゥ エ ィ sufficiently largeamountof gasso thatwe cancompletethe loop. In this experiment, we note エ ィ ・ ッ オ エ of gasin the tank asthevehiclegoesthroughtheloop ateachcity beforeloadiI1gthegaskeptk1thatcity for the vehicle.Ifwe startat a different city with a differ-entamountof gas, theamountof gasin the tankat eachcity shouldstillvaryh thesamefashioz1witha constmtoffset.Ifwepick thecity wherethe amountof gasin the tank is minimum as the startingpoint/ clearlywe will neverrun out of gas. This computationcanbe easily doneinlinear-timewith a singlepassoverall thecities.

Solution 9.9: Considerthe casewhere exactly one personhas green・ ケ ・ ウ N ィ ・ statementfrom the explorerwould makeit clear to the per-sonwith ァ イ ・ ・ ・ ケ ・ ウ thathehasgreeneyessincenobodyelsethatheseeshasgreeneyes.

Now, ウセーーッウ・ thereare two inhabitantswith greeneyes. The firstdav, eachof thesetwo inhabitantswould seeexactlyone otherpersonwith greeneyes Eachwould seethe otherpersonOI1theseconddaytoor from which theycouldhfer thattheremustbewohhabitmtswithァ イ ・ ・ ・ ケ ・ ウ thesecondonebeingエ ィ ・ ュ ウ ・ ャ イ ・ ウ N Hencebothof themwouldleavethesecondday.

UsiI1g iIIductiozu we candemoIIStratethat if thereare k inhabitmtswith greeneyes, all thegreen-eyedinhabitantswould leaveafterthek-th

191

assembly.We alreadysawthebasecase, ォ QN Supposethe inductionィ ケ ー ッ ・ ウ ゥ ウ holds for k - 1. If thereare k inhabitantswith greeneyes,・。」ィ 。「ゥエ ャエ with greeneyeswould seek - 1 otherinhabitantswithァ イ ・ ・ ・ ケ ・ ウ N If at the k-th assembly, they seethat nobodyhasdeparted,it would indicatethattheythemselveshavegreeneyesandhenceall thegreen-eyedinhabitantswould leaveonthe k-th day.

As for the secondpart of the ア オ ・ ウ エ ゥ ッ O for k = 1/ it is fairly obviousthattheexplorergavenewknowledgeto thepersonwith greeneyes.Forothercases, thenewinformationis abit subtle.For k = 2/ thegreen-eyedinhabitantswould be able to infer the color of their eyeson the secondday basedon the information that ・ t ・ イ ケ ッ ョ ・ on the island knows thattherearegreen-eyedinhabitantsandyet no oneleft. For k = 3/ theyareableto inferbecauseeveryoneknowsthateveryoneknowsthattherearegreen-eyedinhabitantsandyet エ ィ ・ seconddayno oneleft.

Supposex is somefact and E (x) representsthe fact エ ィ 。 エ ・ カ カ ・ イ ケ ッ

ォ ョ ッ キ ウ x to be true. In this case, let g representthe fact that therearesomegreen-eyedinhabitantson the island. Thenon the k-th day, all theァ イ ・ ・ ・ ケ ・ 、 inhabitantswould usethe fact EK (g) to infer that theyhavegreen-eyes.Essentially, what theexplorerdid by announcingthe fact intheassemblyis thatit became"commonknowledge", i.e., e HァI becametrue.

Solution9.10: If the 。 ウ ウ オ ュ ー エ ゥ ッ ゥ ウ that onceyou havebrokenthe barinto two pieces, theybecomeseparateproblems, thenit doesnot matterwhatorderyou do it-you will require15 totalbreaksin anyscenario.

If, on the otherhand, the assumptionis that the wholebar staysto-gether H 。 ウ キ ッ オ ャ 、 if you werebreakingit inside its wrapper, for in-stance), thenyou cando a little better. You could simply breakit alongall axes (say, first the vertical and then the horizontal) for a total of 6breaks.

Solution 9.11: Player1 can alwayswin. The key ッ 「 ウ ・ 。 エ ゥ ッ ョ in thisgameis that we want to force the play to be symmetricalaroundthediagonal, i.e., (0,0) , (1 ,1),…, H _ I with our opponentforced to movefirst in termsof breakingthesymmetry.If thatis thecase, we canfolloweachof his movesby a matchingmovereflectedin this diagonalwhichwill eventuallyforcehim to selectthe (0,0) space.

ャ ・ way to force this type of play is to be the first ー ・ イ ウ ッ エ ッ select(1 , I)-this causestheplay areato bejust the 」 ッ ャ オ ュ Q (0, {P I andtherow H { P } P I (i.e., an "L" shape). At that ー ッ ゥ ョ キ ・ cansuccessfullymirror anymovethatPlayer2 makes, forcing him to eventuallychoose(0/0).

2l Choose(1J)i1 ivvゥャャ ャ ョーHI :

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

3 I Until you win:4 I Wait for Player 2 to choose square (i I j)5 I Choose square (j Ii)

Solution9.15: Theproblemcanbesolvedusingdynamicーイ ッ ァ イ ゥ ョ ァ N

Let P(m,n) bethe largestmarginof victory thataplayercanachievewhenthecoinsremainingareindexedby m エッ ゥョ」ャオウゥカ・N

Thefunction P satisfiesthefollowing:

Solution9.12: Supposethe setof remainingsquaresareof the form ofa rectangleandoneadditionalsquare(whichmustbe P エ ィ ・ lower row)andPlayer2 is to move.Theremainingsetof squareswill beof theformof arectangle(if Player2playsthelower row) or arectanglewith a setofadditionalsquareson thelower row. In eithercase, Player1 canrecreatethe stateto be a rectangleand one additionalsquare, i.e., Player1 canforce a win. By playing (1,n - 1) ashis initial ュ ッ p ャ 。 ケ ・ イ 1 cancreatethis situationandthereforeforce amove.

In the generalcase, we can compute P for 」 ッ ゥ ョ ウ by dynamicー イ ッ ァ イ 。 ュ ュ ゥ エ ィ ・ イ ・ are ョ H K 1)/2 possibleargumentsfor- P セ、 thework requiredto computep from previouslycomputedvaluesis COIl-stant. HenceP canbecomputedin o H R I time.

Solution9.16:Theeasiestway to provethis is to ゥ ュ 。 ァ ゥ i ャ 。 エ ィ ・ イ(call him Bob)descendiRgthemountainORSaturday>h exactlythesamefashiOI1asAdam did OR SUI1day-WhenaseerldingOIISaturday> Adamwill passBob at sometime mdplace-tkisis the time md placewhichAdamwill be at onSunday.

193CHAPTER9. DISCRETEMATHEMATICS

ュ Hc{ョ} - pH K c{ュ} MpH ゥヲョ > m

C[m] ifn = m.

pH I

P(m ,m)

Solution9.14: Numberthecoinsfrom 1-50.PlayerF canchooseall theeven-numberedcoinsby first picking Coin 50 andthenalwayspickingthe oddnumbercoin at oneof the two ends. Forexample, if PlayerGchoosesCoinI , thenin thenextturn, PlayerF choosesCoin2. If PlayerGchoosesCoin49, thenF choosesCoin48 in thenextturn. In エ ィ ゥ ウ ᆪ G 。 ウ ィ ゥ ッ ョ 1,yF canalwaysleavean 。 イ NG ヲ 。 ョ ァ ・ ュ ャ ・ i Q エ キ カ ィ ・ イ ・ G can P ョ ャ ケ 」 ィ ッ ッ ウ ・ ヲ イ ッ ュ ャ ッ 、 、 ᆳ

i ョ ャ オ ュ ャ 「 ・ イ ・ 、 」 ッ ゥ ョ ャ s N

If the valueof the coinsat evenindicesis larger that of the coinsatodd indices, F canwin by selectingthe evenindicesandvice versa. Ifthevaluesarethesame, hecansimplychooseeitherandin eachcase, hecannotlose.

Solution 9.13: SupposePlayer 2 has a winning strategy. SupposePlayer1 choseH M 1,m - 1) ashis initial choiceandPlayer2 counteredwith position (i , j) , leaving the set S of squares.Now, it is Player Qtumandfrom this set, by hypothesis, Player2 canforce awin. HoweverPlayer1 couldhavechosen(i , j) ashis initial moveandthesetof remain-ing squareswouldbe S (sincethesquareH M 1,m - 1) is aboveandtotheright of all othersquares)with Player2'sturn.

This contradictsthehypothesisthatPlayer2 hasa winning strategy;thereforePlayer1 musthaveawinningstrategy.

Note that this doesnot give an explicit strategyaswe did for Prob-lem9.11andProblem9.12.

192

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

3 I Until you win:4 I Wait for Player 2 to choose square (i I j)5 I Choose square (j Ii)

Solution9.15: Theproblemcanbesolvedusingdynamicーイ ッ ァ イ ゥ ョ ァ N

Let P(m,n) bethe largestmarginof victory thataplayercanachievewhenthecoinsremainingareindexedby m エッ ゥョ」ャオウゥカ・N

Thefunction P satisfiesthefollowing:

Solution9.12: Supposethe setof remainingsquaresareof the form ofa rectangleandoneadditionalsquare(whichmustbe P エ ィ ・ lower row)andPlayer2 is to move.Theremainingsetof squareswill beof theformof arectangle(if Player2playsthelower row) or arectanglewith a setofadditionalsquareson thelower row. In eithercase, Player1 canrecreatethe stateto be a rectangleand one additionalsquare, i.e., Player1 canforce a win. By playing (1,n - 1) ashis initial ュ ッ p ャ 。 ケ ・ イ 1 cancreatethis situationandthereforeforce amove.

In the generalcase, we can compute P for 」 ッ ゥ ョ ウ by dynamicー イ ッ ァ イ 。 ュ ュ ゥ エ ィ ・ イ ・ are ョ H K 1)/2 possibleargumentsfor- P セ、 thework requiredto computep from previouslycomputedvaluesis COIl-stant. HenceP canbecomputedin o H R I time.

Solution9.16:Theeasiestway to provethis is to ゥ ュ 。 ァ ゥ i ャ 。 エ ィ ・ イ(call him Bob)descendiRgthemountainORSaturday>h exactlythesamefashiOI1asAdam did OR SUI1day-WhenaseerldingOIISaturday> Adamwill passBob at sometime mdplace-tkisis the time md placewhichAdamwill be at onSunday.

193CHAPTER9. DISCRETEMATHEMATICS

ュ Hc{ョ} - pH K c{ュ} MpH ゥヲョ > m

C[m] ifn = m.

pH I

P(m ,m)

Solution9.14: Numberthecoinsfrom 1-50.PlayerF canchooseall theeven-numberedcoinsby first picking Coin 50 andthenalwayspickingthe oddnumbercoin at oneof the two ends. Forexample, if PlayerGchoosesCoinI , thenin thenextturn, PlayerF choosesCoin2. If PlayerGchoosesCoin49, thenF choosesCoin48 in thenextturn. In エ ィ ゥ ウ ᆪ G 。 ウ ィ ゥ ッ ョ 1,yF canalwaysleavean 。 イ NG ヲ 。 ョ ァ ・ ュ ャ ・ i Q エ キ カ ィ ・ イ ・ G can P ョ ャ ケ 」 ィ ッ ッ ウ ・ ヲ イ ッ ュ ャ ッ 、 、 ᆳ

i ョ ャ オ ュ ャ 「 ・ イ ・ 、 」 ッ ゥ ョ ャ s N

If the valueof the coinsat evenindicesis larger that of the coinsatodd indices, F canwin by selectingthe evenindicesandvice versa. Ifthevaluesarethesame, hecansimplychooseeitherandin eachcase, hecannotlose.

Solution 9.13: SupposePlayer 2 has a winning strategy. SupposePlayer1 choseH M 1,m - 1) ashis initial choiceandPlayer2 counteredwith position (i , j) , leaving the set S of squares.Now, it is Player Qtumandfrom this set, by hypothesis, Player2 canforce awin. HoweverPlayer1 couldhavechosen(i , j) ashis initial moveandthesetof remain-ing squareswouldbe S (sincethesquareH M 1,m - 1) is aboveandtotheright of all othersquares)with Player2'sturn.

This contradictsthehypothesisthatPlayer2 hasa winning strategy;thereforePlayer1 musthaveawinningstrategy.

Note that this doesnot give an explicit strategyaswe did for Prob-lem9.11andProblem9.12.

192

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter10

Probability

Solution10.1: It is easyto solvethis problemwhenk = I-we simplymakeonecall to therandomnumbergenerator, takethereturnedr valuemod N w ・ 」 。 ョ ウ キ a { ョ - 1] with A[r].

For k > 1/ we startby choosingoneelementat randomas 。 「 ッ カ ・ ャ 、

we now イ ・ ー ・ エ ィ ・ sameprocesswith the M ャ ・ ャ ・ ュ ・ ョ エ subarrayAP:⦅ 2]. Eventually, therandomsubsetoccupiestheslotsA [n -1-k ZMQ}

andtheremainingelements。 イ ・ エ ィ ・ first n - k slots.The 。ャァッイゥエセ clearly is in-place. To show that all the subsetsare

equally ャ ゥ ォ キ ・ prove somethingstronger, namelythat all permuta-tionsof sizek areequallylikely.

Formally}m mmpermutationof a setS of cardimlitynisa ウ・アオ・ 」・

of m elementsof S with no イ ・ ー ・ エ ゥ ッ ョ ウ N Note that thereare H ョ セ Q

permutations.The inductionhypothesisnow is that after iterationm/ the subarray

a { ョ M ュ M ォ Z M Q } containseachpossiblem-permutationwith probability

ョ ッ イ m = 1/ 。ョケ ョ・ョエ ゥウ ャゥォ ・ ウ 」エ・、 so エ ャ 。 ウ ・caseholds.

supposethehductivehypothesisholdsfor m =l.Considerm =l +1. cッセウゥ、・イ aparticular(l + 1)-permutation, say H 「 W z K ャ I N Thiscon-sistsof a ウゥ i・ ・ャ・ュ・ョエQ followedby the l-permutationH R a l+1)'Let E1 be the event エ ィ 。 エ Q is selectedin iteration ャ Q ャ 、 E2 be theeventthatthe ウ エ l iterationsproducedH R 。 コ Q I G Theprobabilityof H Q 。 コ I イ ・ ウ オ ャ エ ゥ ョ ァ after iteratioIIJ+l is simply PT(E1nE2)zPr(E1IE2) . p e R IN By theinductivehypothesis, theprobabilityof per-ュ 。 エ ゥ ッ ョ H N N ャ Q I ゥ ウ R Q N TheprobabilityPr(E1IE2) = ョセz sincethealgorithmselectsfrom elementsin 0 Z M l - 1with equalprobability.G ィ ・ イ ・ ヲ ッ イ ・ Z

195

1 H MャIA 1Pr(E1 n e R I pイHe R ie Q I . p He Q I = n -l N A M ᄋ

n!

Thealgorithmgeneratesall randomk-permutationswith equalprob-ability, from which it follows thatall subsetsof sizek areequallylikely.

We makek calls to the randomnumbergenerator.When k is biggerエ ィ 。 O we can optimizeby computinga subsetof M k elementstoremovefrom the set. When ォ M 1/ this replacesn - 2 calls to therandomnumbergeneratorwith a singlecall.

Solution10.2: We storethe first k packets.Consequently, we selecttheエ ィ packetto addto our subsetwith probability セ N If we do chooseitt

we selectanelementuniformly at randomto ejectfrom thesubset.To provecorrectness, we useinductiononthenumberof packetsthat

havebeenread. Specifically, the inductivehypothesisis that all k-sizedsubsetsareequallylikely after セ k.

The numberof k-size subsetis HセI / so the ー イ ッ 「 。 「 ゥ ャ ゥ ッ ヲ any k-sizesubsetゥ ウ

For thebasecasen = k, thereis exactlyonesubsetof sizek which iswhatthealgorithmhascomputed.

Assumethe inductivehypothesisholdsfor n > k. Supposewe haveprocessedthe ョ Q M エ ィ packet. Theprobabilityof a k-sizesubsetthatdoesnot include the K 1-th packetis the probability that we had selectedthatsubsetafterreadingthe M エ ィ ゥ エ ・ イ 。 エ ゥ ッ 。 ョ 、 did not selectthen + 1-thpacketwhich is

1 H ォ | ォAH M ォIA O MォKャ | ォA . H M k)! . H M k + 1)HセI|T QI ョA |K 1 IA . H QI

t tィゥ ウ・ア オ。 Q 。ャウ tK コK_ INU PO m 、オ ・ィ Qャケーo エエ ャ・ウゥウ ゥ ウィッャ、 ウヲ

Q ァエィ・ i・ャ・ュ・A QエN .Gィ ・ probabilityof a ウゥコ・ subsetH thatincludesthe K 1-thpacket

p ョ 」 。 ョ be computedas follows: let G be a k-sizesubsetof the first npackets.Theonlywaywecangetfrom G to H is if G 」ッイ ゥョウh サpョKQスG

Let G* besucha subset;let {q} = h サpョK、ᄋ

ャ・ probability of going from G to H is the probability of selectingPn+1anddroppingア 。 ャ エ ッ N t. Thereare ョ Q - k candidatesubsetsヲ

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Chapter10

Probability

Solution10.1: It is easyto solvethis problemwhenk = I-we simplymakeonecall to therandomnumbergenerator, takethereturnedr valuemod N w ・ 」 。 ョ ウ キ a { ョ - 1] with A[r].

For k > 1/ we startby choosingoneelementat randomas 。 「 ッ カ ・ ャ 、

we now イ ・ ー ・ エ ィ ・ sameprocesswith the M ャ ・ ャ ・ ュ ・ ョ エ subarrayAP:⦅ 2]. Eventually, therandomsubsetoccupiestheslotsA [n -1-k ZMQ}

andtheremainingelements。 イ ・ エ ィ ・ first n - k slots.The 。ャァッイゥエセ clearly is in-place. To show that all the subsetsare

equally ャ ゥ ォ キ ・ prove somethingstronger, namelythat all permuta-tionsof sizek areequallylikely.

Formally}m mmpermutationof a setS of cardimlitynisa ウ・アオ・ 」・

of m elementsof S with no イ ・ ー ・ エ ゥ ッ ョ ウ N Note that thereare H ョ セ Q

permutations.The inductionhypothesisnow is that after iterationm/ the subarray

a { ョ M ュ M ォ Z M Q } containseachpossiblem-permutationwith probability

ョ ッ イ m = 1/ 。ョケ ョ・ョエ ゥウ ャゥォ ・ ウ 」エ・、 so エ ャ 。 ウ ・caseholds.

supposethehductivehypothesisholdsfor m =l.Considerm =l +1. cッセウゥ、・イ aparticular(l + 1)-permutation, say H 「 W z K ャ I N Thiscon-sistsof a ウゥ i・ ・ャ・ュ・ョエQ followedby the l-permutationH R a l+1)'Let E1 be the event エ ィ 。 エ Q is selectedin iteration ャ Q ャ 、 E2 be theeventthatthe ウ エ l iterationsproducedH R 。 コ Q I G Theprobabilityof H Q 。 コ I イ ・ ウ オ ャ エ ゥ ョ ァ after iteratioIIJ+l is simply PT(E1nE2)zPr(E1IE2) . p e R IN By theinductivehypothesis, theprobabilityof per-ュ 。 エ ゥ ッ ョ H N N ャ Q I ゥ ウ R Q N TheprobabilityPr(E1IE2) = ョセz sincethealgorithmselectsfrom elementsin 0 Z M l - 1with equalprobability.G ィ ・ イ ・ ヲ ッ イ ・ Z

195

1 H MャIA 1Pr(E1 n e R I pイHe R ie Q I . p He Q I = n -l N A M ᄋ

n!

Thealgorithmgeneratesall randomk-permutationswith equalprob-ability, from which it follows thatall subsetsof sizek areequallylikely.

We makek calls to the randomnumbergenerator.When k is biggerエ ィ 。 O we can optimizeby computinga subsetof M k elementstoremovefrom the set. When ォ M 1/ this replacesn - 2 calls to therandomnumbergeneratorwith a singlecall.

Solution10.2: We storethe first k packets.Consequently, we selecttheエ ィ packetto addto our subsetwith probability セ N If we do chooseitt

we selectanelementuniformly at randomto ejectfrom thesubset.To provecorrectness, we useinductiononthenumberof packetsthat

havebeenread. Specifically, the inductivehypothesisis that all k-sizedsubsetsareequallylikely after セ k.

The numberof k-size subsetis HセI / so the ー イ ッ 「 。 「 ゥ ャ ゥ ッ ヲ any k-sizesubsetゥ ウ

For thebasecasen = k, thereis exactlyonesubsetof sizek which iswhatthealgorithmhascomputed.

Assumethe inductivehypothesisholdsfor n > k. Supposewe haveprocessedthe ョ Q M エ ィ packet. Theprobabilityof a k-sizesubsetthatdoesnot include the K 1-th packetis the probability that we had selectedthatsubsetafterreadingthe M エ ィ ゥ エ ・ イ 。 エ ゥ ッ 。 ョ 、 did not selectthen + 1-thpacketwhich is

1 H ォ | ォAH M ォIA O MォKャ | ォA . H M k)! . H M k + 1)HセI|T QI ョA |K 1 IA . H QI

t tィゥ ウ・ア オ。 Q 。ャウ tK コK_ INU PO m 、オ ・ィ Qャケーo エエ ャ・ウゥウ ゥ ウィッャ、 ウヲ

Q ァエィ・ i・ャ・ュ・A QエN .Gィ ・ probabilityof a ウゥコ・ subsetH thatincludesthe K 1-thpacket

p ョ 」 。 ョ be computedas follows: let G be a k-sizesubsetof the first npackets.Theonlywaywecangetfrom G to H is if G 」ッイ ゥョウh サpョKQスG

Let G* besucha subset;let {q} = h サpョK、ᄋ

ャ・ probability of going from G to H is the probability of selectingPn+1anddroppingア 。 ャ エ ッ N t. Thereare ョ Q - k candidatesubsetsヲ

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

whichmeansthattheprobabilityof H is givenby

k 1 , _, 1 (n + 1- ォ I H M ォ I A ォ A O K Q |ᄋ N H K Q M ォ I ᄋ ヲ セ | = H .U Wセ - )n + 1 k \'U I - 'UJ G) H ャIョA | ォ )'

soinductiongoesthroughfor subsetsincludingthe K l-th element.

Solution10.3: We canmakeuseof the 。 ャ ァ ッ イ ゥ ャ i d for problem10.1withthe arrayA initializedby A[i] ] w ・ do not actuallyneedto storetheelementsin A, all we needto do is storetheelementsaswe selectthem,sothestoragerequirementis met.

Solution10.4: The processdoesnot yield all permutationswith equalprobability. Oneway to seethis is to considerthecase] 3. Thereare3! = 6permutationspossible.Therearea total of 33 = 27 waysin whichwecanchoosetheelementsto swapandtheyareall equallylikely. Since27 is not divisible by 6, somepermutationscorrespondto more waysthanothers, ergonot all permutationsareequallylikely.

ィ・ processcanbefixed byselectingelementsat randomandmovingthemto theend, similar to howwe proceededin Problems10.1and10.3.

Solution10.5: Our solutionto Problem10.1 canbe usedwith k = n.Although the subsetthat is returnedis unique(it will be サ P Q W ᆳ

I}) , all n! possibleorderingsof the elementsin the setoccurwith equalprobability. (Note thatwe cannotusethe trick to reducethe numberofcalls to therandomnumbergeneratorat theendof Solution10.1.)

Solution10.6: The first thing to noteis that threesegmentscanmakeatriangleiff no onesegmentis longerthanthe sumof the othertwo: the"only if" follows from the triangle inequalityandthe "if" follows froma construction-takea segmentanddraw circles at the endpointswithradiusequalto the lengthsof theothercircles.

Sincethethreesegmentlengthsaddup to I , thereis asegmentthatislongerthanthesumof the othertwo iff thereis a segmentthat is longertkm i

Let ャ ュ ゥ ョ H オ ャ オ R I m = max(ul,u2) - min(ul,u2), and u -1 - max(ul ,u2); thesearethe i ・ ァ エ ィ ウ of thefirst, second, andthird seg-ments, from left to right. If onesegmentis longerthan0.5, thennoneofthe otherscanbe longer than0.5; so, the eventsl > 0.5, m > 0.5, andu> 0.5 are 、 ゥ ッ ゥ ョ エ N

Observethat l ^ 「ッエィ ul andu2 aregreaterthan0.5; theproba-bility of this eventゥ ウ ᅲ 「 ・ 」 。 オ ウ ・ ul andu2 arechosenindependently.s ゥ ュ ゥ ャ 。 ュ > 0.5 iffbothul andu2 arelessthan0.5, which is セ x セN

To computetheprobabilityof m > 0.5, first we considerthecasethatul < u2. For m > 0.5, we needul to bebetweenP ャ 、 Q ャ 、 u2 to be

197

between0.5+ ul and1. This probabilitycanbeexpressedby theintegral

public static void uniform ()int overHalf = 0;for (int i = 0 ; i < numTrials; i++)

double u1 = Math.random() ;double u2 = Math.random();double min = Math.min(u1, u2);

public class triangle {static final int numTrials = 1000000;ー オ 「 ャ セ セ static void main( String [] args ) {

uniform () ;inOrder() ;

QWoilオャ PUQ R

coilui HQ QI Q

whichevaluatesto セ N

By symmetry, the probability of m > 0.5 whenul > u2 is also i.h ュ ・ ・ エ ィ ・ ー イ ッ 「 。 「 ゥ ャ ゥ エ ケ ッ ヲ 。 ウ ・ ァ ュ ・ i Q エ 「 ・ ィ ァ ャ P ・ イ エ ィ ュ j is S K [ ゥ ] セ

?OF エ_ e pZ o 「。 「ゥャ ャ

- T TG

For thesecondcase, we fail to be ableto makea trianglein caseul >0.5, u2 - ul > oN ッイ 1 - u2 > 0.5. Thefirst probabilityis simply セ

Thesecondprobabilityis givenby theintegral セ . '"

Notethattheprobability、 ュ ヲ オ ョ 」 エ ゥ ヲ ッ イ u2 is differentfrom thepreV lOUS 」。ウ ・ sュc・ u2 ゥ ウオョ ャゥ ヲッ イ イュ ュ ャゥョ Q { オャ I] , not [0,1]. This integralevaluates

エ ッ t ィ ・ エ ィ ゥ イ イ ッ 「 。 「 ゥ ャ ゥ エ ケ canalsobecomputedオ ウ ゥ ァ 。 ョ ゥ ァbutby symmetry> it mustbe the sameas the secOI1dprobability.Hmcethefinal probabilityis セ + 2 N PNXPW

iョエオゥエゥ イ・ャケ the secondformufationleadsto a higherprobabilityof aQP ャゥョ・ ウ・ァュ・ エ becausethereis lessdiversity in the ー ッ ゥ ョ N For thefirst case, the pointsarespreadrandomly; for the ウ ・ 」 ッ 、 thereis a 0.5chmcethatthearstpohtitself precludesusfrom buildhgthe trimgle-AI1other-wayto tMIlk of it is tAat if weputdOWI1alot of pohtsrthefirstmethodwill lead to shortsegmmtswith littlevariatiOIIh ieI19hsbutthe secondmethodwill give us a skeweddistributionandthe first fewsegmentswill beconsiderablylonger.

ャ ・ ウ ・ computationscanbeverified by a numericalsimulation.Hereis anexamplecodeto performthis:

23456789

1011121314

CHAPTER10. PROBABILITY196

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

whichmeansthattheprobabilityof H is givenby

k 1 , _, 1 (n + 1- ォ I H M ォ I A ォ A O K Q |ᄋ N H K Q M ォ I ᄋ ヲ セ | = H .U Wセ - )n + 1 k \'U I - 'UJ G) H ャIョA | ォ )'

soinductiongoesthroughfor subsetsincludingthe K l-th element.

Solution10.3: We canmakeuseof the 。 ャ ァ ッ イ ゥ ャ i d for problem10.1withthe arrayA initializedby A[i] ] w ・ do not actuallyneedto storetheelementsin A, all we needto do is storetheelementsaswe selectthem,sothestoragerequirementis met.

Solution10.4: The processdoesnot yield all permutationswith equalprobability. Oneway to seethis is to considerthecase] 3. Thereare3! = 6permutationspossible.Therearea total of 33 = 27 waysin whichwecanchoosetheelementsto swapandtheyareall equallylikely. Since27 is not divisible by 6, somepermutationscorrespondto more waysthanothers, ergonot all permutationsareequallylikely.

ィ・ processcanbefixed byselectingelementsat randomandmovingthemto theend, similar to howwe proceededin Problems10.1and10.3.

Solution10.5: Our solutionto Problem10.1 canbe usedwith k = n.Although the subsetthat is returnedis unique(it will be サ P Q W ᆳ

I}) , all n! possibleorderingsof the elementsin the setoccurwith equalprobability. (Note thatwe cannotusethe trick to reducethe numberofcalls to therandomnumbergeneratorat theendof Solution10.1.)

Solution10.6: The first thing to noteis that threesegmentscanmakeatriangleiff no onesegmentis longerthanthe sumof the othertwo: the"only if" follows from the triangle inequalityandthe "if" follows froma construction-takea segmentanddraw circles at the endpointswithradiusequalto the lengthsof theothercircles.

Sincethethreesegmentlengthsaddup to I , thereis asegmentthatislongerthanthesumof the othertwo iff thereis a segmentthat is longertkm i

Let ャ ュ ゥ ョ H オ ャ オ R I m = max(ul,u2) - min(ul,u2), and u -1 - max(ul ,u2); thesearethe i ・ ァ エ ィ ウ of thefirst, second, andthird seg-ments, from left to right. If onesegmentis longerthan0.5, thennoneofthe otherscanbe longer than0.5; so, the eventsl > 0.5, m > 0.5, andu> 0.5 are 、 ゥ ッ ゥ ョ エ N

Observethat l ^ 「ッエィ ul andu2 aregreaterthan0.5; theproba-bility of this eventゥ ウ ᅲ 「 ・ 」 。 オ ウ ・ ul andu2 arechosenindependently.s ゥ ュ ゥ ャ 。 ュ > 0.5 iffbothul andu2 arelessthan0.5, which is セ x セN

To computetheprobabilityof m > 0.5, first we considerthecasethatul < u2. For m > 0.5, we needul to bebetweenP ャ 、 Q ャ 、 u2 to be

197

between0.5+ ul and1. This probabilitycanbeexpressedby theintegral

public static void uniform ()int overHalf = 0;for (int i = 0 ; i < numTrials; i++)

double u1 = Math.random() ;double u2 = Math.random();double min = Math.min(u1, u2);

public class triangle {static final int numTrials = 1000000;ー オ 「 ャ セ セ static void main( String [] args ) {

uniform () ;inOrder() ;

QWoilオャ PUQ R

coilui HQ QI Q

whichevaluatesto セ N

By symmetry, the probability of m > 0.5 whenul > u2 is also i.h ュ ・ ・ エ ィ ・ ー イ ッ 「 。 「 ゥ ャ ゥ エ ケ ッ ヲ 。 ウ ・ ァ ュ ・ i Q エ 「 ・ ィ ァ ャ P ・ イ エ ィ ュ j is S K [ ゥ ] セ

?OF エ_ e pZ o 「。 「ゥャ ャ

- T TG

For thesecondcase, we fail to be ableto makea trianglein caseul >0.5, u2 - ul > oN ッイ 1 - u2 > 0.5. Thefirst probabilityis simply セ

Thesecondprobabilityis givenby theintegral セ . '"

Notethattheprobability、 ュ ヲ オ ョ 」 エ ゥ ヲ ッ イ u2 is differentfrom thepreV lOUS 」。ウ ・ sュc・ u2 ゥ ウオョ ャゥ ヲッ イ イュ ュ ャゥョ Q { オャ I] , not [0,1]. This integralevaluates

エ ッ t ィ ・ エ ィ ゥ イ イ ッ 「 。 「 ゥ ャ ゥ エ ケ canalsobecomputedオ ウ ゥ ァ 。 ョ ゥ ァbutby symmetry> it mustbe the sameas the secOI1dprobability.Hmcethefinal probabilityis セ + 2 N PNXPW

iョエオゥエゥ イ・ャケ the secondformufationleadsto a higherprobabilityof aQP ャゥョ・ ウ・ァュ・ エ becausethereis lessdiversity in the ー ッ ゥ ョ N For thefirst case, the pointsarespreadrandomly; for the ウ ・ 」 ッ 、 thereis a 0.5chmcethatthearstpohtitself precludesusfrom buildhgthe trimgle-AI1other-wayto tMIlk of it is tAat if weputdOWI1alot of pohtsrthefirstmethodwill lead to shortsegmmtswith littlevariatiOIIh ieI19hsbutthe secondmethodwill give us a skeweddistributionandthe first fewsegmentswill beconsiderablylonger.

ャ ・ ウ ・ computationscanbeverified by a numericalsimulation.Hereis anexamplecodeto performthis:

23456789

1011121314

CHAPTER10. PROBABILITY196If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

198

カュ

ョョmae

ュァョュオュ

mァ

CHAPTER10. PROBABILITY

double max = Math.max(ul, オRI[if(miE1 > O.5!lmax < 0.5i!max-min>0.5)

overHalf++;

; vstem out 1 B Bオョゥ Gオョゥ ュ Q e ョゥヲ ゥヲッ ヲ o イ Gイ i ij ッカ・イh。 Qヲ KB + numTrials );

public static void inOrder() {in t overHalf = 0;iLr(int i=0;i < numTrials;i++)

double x , Y I z;if «x = Math.random()) > 0.5)

overHalf++;lse {

if((yz(Math.random()*(1.o-x)))>0.5)ッ カ ・ イ h 。 ャ ヲ K [

else {if «z = HQN P HKケIIᄏPNUI

overHalf++;

; vstem out p iョ QH H " ij ッカ・イh。 Qヲ + ":" + numTrials );

Solution10.7:Theprobability that a givenball doesnot laIId up iRagiven「 ゥ ョ セ ウ M o n ᄋ t ィ ・ ー イ ッ 「 。 「 ゥ ャ ゥ エ ケ エ ィ 。 エ ュ ュ ッ ヲ エ ィ ・ 「 。 ャ ャ ウ ャ 。 m ゥ ョ エ ィ ・

is HI Mh・ョ」・エィ・・クー・」エ・、iQQQiiQ「・イッヲ・ューエケ「ゥュ」。 ・ givenasH I ュ N Note エ ィ ウ ュ 「 ・ 」 ャ ッ ウ ・ ャ ケ 。 ー ー イ ッ ク ゥ ュ 。 エ ・ 、 「 S W W コ ・ ュ O ョ N h ・ ョ 」 ・as Q P ァ asonan 。 カ ・ イ ・ eachserveris handlingsignificantlymorethanoneclient, thereshouldbevery few idle servers.

Solution10.8: Let Xi bethe イ 。 ョ ャ 、 。 ュ カ 。 イ ゥ 。 「 ャ ・ キ ィ ゥ 」 ィ ゥ ウ 1 ゥ ヲ H ゥゥI = i 。ョ Q0otherwise.(Sucha rmdomvariableis ofteRreferredto asm uhdiCatorrandomvariableHjTKeI1umberof fixed pohtsis equalto x Q K x R N N N KXn-EyneddoI1islinearri-ev the expectedvalue of a sumof random

。 「 ャ T ゥ ウ 。 ャ to the sum of the expectedvalues.of エ ィ ・ 、 ゥ カ ゥ 、 オ 。 ャbles. The expectedvalueof Xi is Qᄋ H ・。ョ

elementis equallylikely to bemappedエ ッ ケ ッ エ ィ ・ イ ・ ャ ・ ュ ・ イ t ィ ・ イ ・ ヲ ッ イ ・theexpectednumberof fixed pointsis n N ]QN

mputethe expectedlengthof 「ケ defining ゥ ョ セ ゥ 」 。 エ ッ イ ran-amvariablesN N ス キ ィ ・ イ ・ ス ] ャ ゥ ヲ ヲ y ェ \ ゥ H H ェ I \ H ゥ I I N o 「 ウ ・ イ カ セ

that the lengthof ゥ ウ simply the sumof the ᆬ ゥ ウ N ャ ・ ・ ク ー ・ 」 ィ セ 、 valueof¥i is t , sinceYj < i H HェI \ H i)) iff thelargestof エ Q ゥ イ ウ エ i elementsis at

199

p ッウゥエ エゥッ キィゥ」ィィ。 ウー イッ 「。 「ゥ ャ ゥエ ケエ 」・ all エZ ィ ・ ー ・ イ イ イ ュ

likely. tィ・ 。イ・ the expectedvaluefor the i ・ エ ィ ッ ヲ ゥ ウ Q K ゥ K ゥ ᄋ ゥ イ

which tendsto logeNote that for bothpartsof the problem, we usedthe linearity of ex-

pectationwhich doesnot requirethe individual randomvariablesto beindependent. This is crucialsincetheXiS and1js arenot independent-for example, if the ヲ ゥ イ ウ エ Q elementsgetmappedto themselves, thenthe M エ ィ elementmustalsomapto itself.

Solution10.9: Basically, wewantto producearandomnumberbetweenoand 「 inclusive.

We canproducea イ。ョ、ッュ オュ「・イ from 0 to l - 1 asfollows: let j betheleastintegersuchthat ャ R j N

If l is a powerof 2, say l = 2j , thenall we needare j calls to the 0-1valuedrandomnumbergenerator-thej bits from the calls encodea jbit integerfrom 0 to l - I , inclusive and all suchnumbersare equallylikely; so, we canusethis integer.

If l is not a powerof 2, the j callsmayormaynot encodeanintegerin therange0 to l - 1. If thenumberis in therange, we returnit; sinceallthenumbersareequallylikely, theresultis correct.

If the numberis not in the range, we try again. The probability ofhavingto try againis lessthan セ sincel > 2j -1. The ー 。 「 ゥ ャ ゥ エ ケ thatwe

take ・ ク 。 」 エ ォ stepsbeforesucceedingis at least(1 - セ )k-1 . セ ヲ meexpectednumberof trials beforewe convergeto a solution is bounded「 ケ ャ N セ K R N H セ I R H セ I S キ ィ ッ ウ ・ limit is 2.

Solution10.10: Let Fx(x) be the cumulative 、 ゥ ウ エ イ ゥ 「 ゥ ッ ョ function forX , i.e., Fx(x) = probabilitythatX セ

To generateX , weperformthefollowing operation:we selectanum-berr uniformly atrandomin theunit interval. We thenprojectbackfromFx to obtaina valuefor X , i.e., we returns = Fx1(r).

By construction, the probability that the valuewe returnis lessthanor equal エ ッ ゥ ウ f ク H I so the cumulativedistribution function of therandomvariablewe createdis exactlythatof X.

Solution 10.11: First we prove that if (X1,X 2 ,…) is a ウ・アオ・ 」・ ofBernoulli IID randomvariables, with p(Xi = QI ーI thenthe ・ ク ー ・ 」 エ ・ 、

エ ゥ ュ ・ エ ッ the ヲ ゥ イ ウ エ 1 ゥ ウ セN Ther ッョゥョァゥウ 。 ウヲ 。ャャッキウ Z、 ・ヲゥ ヲゥョ・f f ゥエエ ッ「 ・

・ カ・ャ Q エエ ィ 。 エ thefirst 1 comeson the i-th trial. ThenPr(Fi ) = (1 - p)• 1. p.Hencethe expectedtime is S ] ]Q i . (1 - p)i-l .p. This sumsimplifiesto セ (multiply both ウ ゥ 、 ケ p, s QZ 。」エ and ウ オ イ ョ Q エ ィ ・ ・ ゥ ョ ョ ヲ ヲ ゥ ゥ ョ ゥ エ ・ ・ ァ ・ ッ ュ ・

seriesッ イ Q theri‘·ight).Now, we considerthe problemof dice rolls. Thekey is to determine

the expectedtime

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

198

カュ

ョョmae

ュァョュオュ

mァ

CHAPTER10. PROBABILITY

double max = Math.max(ul, オRI[if(miE1 > O.5!lmax < 0.5i!max-min>0.5)

overHalf++;

; vstem out 1 B Bオョゥ Gオョゥ ュ Q e ョゥヲ ゥヲッ ヲ o イ Gイ i ij ッカ・イh。 Qヲ KB + numTrials );

public static void inOrder() {in t overHalf = 0;iLr(int i=0;i < numTrials;i++)

double x , Y I z;if «x = Math.random()) > 0.5)

overHalf++;lse {

if((yz(Math.random()*(1.o-x)))>0.5)ッ カ ・ イ h 。 ャ ヲ K [

else {if «z = HQN P HKケIIᄏPNUI

overHalf++;

; vstem out p iョ QH H " ij ッカ・イh。 Qヲ + ":" + numTrials );

Solution10.7:Theprobability that a givenball doesnot laIId up iRagiven「 ゥ ョ セ ウ M o n ᄋ t ィ ・ ー イ ッ 「 。 「 ゥ ャ ゥ エ ケ エ ィ 。 エ ュ ュ ッ ヲ エ ィ ・ 「 。 ャ ャ ウ ャ 。 m ゥ ョ エ ィ ・

is HI Mh・ョ」・エィ・・クー・」エ・、iQQQiiQ「・イッヲ・ューエケ「ゥュ」。 ・ givenasH I ュ N Note エ ィ ウ ュ 「 ・ 」 ャ ッ ウ ・ ャ ケ 。 ー ー イ ッ ク ゥ ュ 。 エ ・ 、 「 S W W コ ・ ュ O ョ N h ・ ョ 」 ・as Q P ァ asonan 。 カ ・ イ ・ eachserveris handlingsignificantlymorethanoneclient, thereshouldbevery few idle servers.

Solution10.8: Let Xi bethe イ 。 ョ ャ 、 。 ュ カ 。 イ ゥ 。 「 ャ ・ キ ィ ゥ 」 ィ ゥ ウ 1 ゥ ヲ H ゥゥI = i 。ョ Q0otherwise.(Sucha rmdomvariableis ofteRreferredto asm uhdiCatorrandomvariableHjTKeI1umberof fixed pohtsis equalto x Q K x R N N N KXn-EyneddoI1islinearri-ev the expectedvalue of a sumof random

。 「 ャ T ゥ ウ 。 ャ to the sum of the expectedvalues.of エ ィ ・ 、 ゥ カ ゥ 、 オ 。 ャbles. The expectedvalueof Xi is Qᄋ H ・。ョ

elementis equallylikely to bemappedエ ッ ケ ッ エ ィ ・ イ ・ ャ ・ ュ ・ イ t ィ ・ イ ・ ヲ ッ イ ・theexpectednumberof fixed pointsis n N ]QN

mputethe expectedlengthof 「ケ defining ゥ ョ セ ゥ 」 。 エ ッ イ ran-amvariablesN N ス キ ィ ・ イ ・ ス ] ャ ゥ ヲ ヲ y ェ \ ゥ H H ェ I \ H ゥ I I N o 「 ウ ・ イ カ セ

that the lengthof ゥ ウ simply the sumof the ᆬ ゥ ウ N ャ ・ ・ ク ー ・ 」 ィ セ 、 valueof¥i is t , sinceYj < i H HェI \ H i)) iff thelargestof エ Q ゥ イ ウ エ i elementsis at

199

p ッウゥエ エゥッ キィゥ」ィィ。 ウー イッ 「。 「ゥ ャ ゥエ ケエ 」・ all エZ ィ ・ ー ・ イ イ イ ュ

likely. tィ・ 。イ・ the expectedvaluefor the i ・ エ ィ ッ ヲ ゥ ウ Q K ゥ K ゥ ᄋ ゥ イ

which tendsto logeNote that for bothpartsof the problem, we usedthe linearity of ex-

pectationwhich doesnot requirethe individual randomvariablesto beindependent. This is crucialsincetheXiS and1js arenot independent-for example, if the ヲ ゥ イ ウ エ Q elementsgetmappedto themselves, thenthe M エ ィ elementmustalsomapto itself.

Solution10.9: Basically, wewantto producearandomnumberbetweenoand 「 inclusive.

We canproducea イ。ョ、ッュ オュ「・イ from 0 to l - 1 asfollows: let j betheleastintegersuchthat ャ R j N

If l is a powerof 2, say l = 2j , thenall we needare j calls to the 0-1valuedrandomnumbergenerator-thej bits from the calls encodea jbit integerfrom 0 to l - I , inclusive and all suchnumbersare equallylikely; so, we canusethis integer.

If l is not a powerof 2, the j callsmayormaynot encodeanintegerin therange0 to l - 1. If thenumberis in therange, we returnit; sinceallthenumbersareequallylikely, theresultis correct.

If the numberis not in the range, we try again. The probability ofhavingto try againis lessthan セ sincel > 2j -1. The ー 。 「 ゥ ャ ゥ エ ケ thatwe

take ・ ク 。 」 エ ォ stepsbeforesucceedingis at least(1 - セ )k-1 . セ ヲ meexpectednumberof trials beforewe convergeto a solution is bounded「 ケ ャ N セ K R N H セ I R H セ I S キ ィ ッ ウ ・ limit is 2.

Solution10.10: Let Fx(x) be the cumulative 、 ゥ ウ エ イ ゥ 「 ゥ ッ ョ function forX , i.e., Fx(x) = probabilitythatX セ

To generateX , weperformthefollowing operation:we selectanum-berr uniformly atrandomin theunit interval. We thenprojectbackfromFx to obtaina valuefor X , i.e., we returns = Fx1(r).

By construction, the probability that the valuewe returnis lessthanor equal エ ッ ゥ ウ f ク H I so the cumulativedistribution function of therandomvariablewe createdis exactlythatof X.

Solution 10.11: First we prove that if (X1,X 2 ,…) is a ウ・アオ・ 」・ ofBernoulli IID randomvariables, with p(Xi = QI ーI thenthe ・ ク ー ・ 」 エ ・ 、

エ ゥ ュ ・ エ ッ the ヲ ゥ イ ウ エ 1 ゥ ウ セN Ther ッョゥョァゥウ 。 ウヲ 。ャャッキウ Z、 ・ヲゥ ヲゥョ・f f ゥエエ ッ「 ・

・ カ・ャ Q エエ ィ 。 エ thefirst 1 comeson the i-th trial. ThenPr(Fi ) = (1 - p)• 1. p.Hencethe expectedtime is S ] ]Q i . (1 - p)i-l .p. This sumsimplifiesto セ (multiply both ウ ゥ 、 ケ p, s QZ 。」エ and ウ オ イ ョ Q エ ィ ・ ・ ゥ ョ ョ ヲ ヲ ゥ ゥ ョ ゥ エ ・ ・ ァ ・ ッ ュ ・

seriesッ イ Q theri‘·ight).Now, we considerthe problemof dice rolls. Thekey is to determine

the expectedtime

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

x ᄋQRP ケN 20 セ 0

x·70 > 0

x . 100+ y . f < 0

A fair price for theoptionis onein whichno arbitrageexists.If f is lessthan0, anarbitrageexists-wearepaidtobuyoptions, lose

nothingif thepricegoesdown, andmake$20peroptionif thepricegoesup. Thereforef セ 0, sowe canwrite the third ゥ ョ ・ ア ゥ エ ケ as ケ

The first ・ ア オ 。 エ ゥ ッ 」 。 ョ berewrittenasy MVᄋクN

Combiningthesetwo ゥ ョ ・ ア オ 。 ャ we seethat an arbitragedoesnotウ エ ゥ ヲ M V i.e., ヲ oオエウゥ、・ of the ゥ イ カ 。 ャ { P } we do

haveanarbitrage.For example, if f = 19 ^ thenthe option is overpricedandwe

shouldsell H i v イ ゥ エ ・ B I options. If we write b optionsandbuy oneshare,we will startwith a portfolio that is worth Q P P Q Y . b. If the stockgoesdown, the optionsareworthless;so, our portfolio is worth $70. If thestockgoesup, we lose$20oneachoptionwewrotebutseea gainonthestockwebought. We wantthenetgainto be ョ ッ ョ ョ ・ ァ 。 エ ゥ カ ・ Q 、 the initial

エ 。 seethe ヲ ゥ イ ウ エ newvalue ゥ ウ ゥ オ ウ エ 1. The time to seethe ウ ・ 」 ッ イ Q 、 ョ ・ キ カ 。 ャ オ ・

ヲ イ ッ イ イ ゥ イ ョ ・ キ カ 。 。 ャ オ ・ ゥ ウ s U _ ウ ゥ ョ 」 ・ エ ィ ・ ー イ ッ 「 」 ゥ 「 ゥ ャ ゥ エ ケ ッ ヲ ウ ・ ・ ゥゥ ョ Q Y 。 i v カ 。 ャ オ ・ ・

ァ ゥ ケ ・ ョ エ ィ 。 エ onevaluehasalreadybeenseen, is 5/6. In this way, the timetakento seethe third new value, given that two valueshave alreadybeenseen, ゥウ N Generalizingthis ゥ 、 ・ ・ ・ ョ エ ッ エ ィ ・ k-th

newvalue, giventhatk - 1valueshavealreadybeenseen, is tV HklIIOVᄋ

Hencetheexpectedtime to seeエ ィ ・ ィ ュ キ カ 。 ャ オ ・ ゥ ウ S K S K R K S

S K _ Q T N W

Solution10.12: Let f be the price for the option. A fair price is deter-minedby the no-arbitrageイ ・ ア オ ゥ イ ・ ュ ・ N Supposewe startwith a port-folio of x sharesand y optionsin S-xand y maybe negative(whichindicatesthatwe sell stocksor selloptions).

The initial valueof our ー 。 イ ッ ャ ゥ ッ is x . QPP ケ . f. On Day 100, twothingsmayhavehappened:

- The stockwentup andtheportfolio is worth x . 120+ y . 20.- Thestockwentdownandtheportfolio is worth x . 70.

If we could choosex 。ョ Q、 y in sucha way エ ィ 。 エ our initial ー ッ イ ッ ャ ゥ ッ hasa negativeカ 。 ャ オ ・ ᄋ M M M

ァ 。 イ 、 i ・ ウ ウ of エ ィ・ュ ャo vカ・ュ・イ Q エゥョ ャエ ィ・ ウ エ。」ォ ourportfolio takesanonnegativevalue, thenwe will havecreatedanarbitrage.

Thereforetheconditionsfor anarbitrageto existare:

QRP RPᄋ b > 0

100+ 19·b < 0

Z ZeエZZ[ZZ[ZZRZR_ Zココ[eゥZRZ[イjjヲ ュ Q ・ ア

ZR[RZZ Z Z_R SS エZ 「ZRZコZZヲ ウ・ our Q portfolio consistsッ ヲ z o o 」 Q ォp イ ッ 」 ・ ・ 、 ゥ イ Q Y as 。「ッ カ・ we seethe 」 ッ ョ 、 ゥ エ ゥ ッ イ Q for 。 ョ arbitrageto exist ゥ ウ Z

100. Xo + f . xャ xR < 0120· xッ RP . Xl + 1.02 . X2 セ P

70· x ッ Q N P R . X2 セ

w イ ゥ エ ゥ エ ィ ・ linear-terms?S AZr we seethat if 、 ・ エ H a I P thenwe canalwaysfiI1d m arbItrageSIReewecaI1solvea R 「 N Wewill denoterow iof A by Ai.

e エ コ ェ ャ エ ュ ャ W Q [ Q Z [ T Q Z [ R R T Texistsif theoptIOIIPI-ICeis ROt equalto f\

COI1versely}if theoptiOI1ispricedat ヲ 、 ・ エ H a I ッ andin particulAo = PNVRWU QKPSUXSaRᄋウゥョ」・aッゥウ。ャゥョ・。ュュ「ゥョ、orッヲaャ andA2 with ー ッ ウ ゥ エ キ ・ ゥ ァ ィ エ ウ thenif A 1x セ 0 and a R ク P Aoxmust 。 ャ ウ ッ

P sono arbitragecan ・ ク ゥ ウ エ N

Solution10.14:LetZbethepriceof thestockOI1day100.Theッ ー エ ゥ P ゥ ウworthlessif Z < 300.If theprice iS z SPoイ theoptiOI1·ydollars.Theexpectedvalueof Z is givenby thehtegralIsworth Z-300

portfolio to haveanegativevalue, i.e.,

ヲ。 オ 、ケ

ヲ ョI (x - 300) N「 、クNjSPP OR HRP IR

l ・jZ uZ Qゥ[ Z ゥョ Qs エ・ 。、 of 20. The ・ ク ー イ ・ ウ ウ ゥ ッ 。 「 ッ カ ・ simplifies to

The ゥ ョ 、 ・ ヲ ゥ ョ ゥ エ ・ ・ ゥ ョ エ ・ ァ イ 。 i ヲ ヲ G オ ェ

so the 、 ・ b ゥ エ ・ ゥ ョ エ 。 ャ equals O N Therefore エ ィ ・ ・ 、payoHon theoptionon day100 ゥセ PセGSY . 20 = $7.8.

CHAPTER10. PROBABILITY200

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

x ᄋQRP ケN 20 セ 0

x·70 > 0

x . 100+ y . f < 0

A fair price for theoptionis onein whichno arbitrageexists.If f is lessthan0, anarbitrageexists-wearepaidtobuyoptions, lose

nothingif thepricegoesdown, andmake$20peroptionif thepricegoesup. Thereforef セ 0, sowe canwrite the third ゥ ョ ・ ア ゥ エ ケ as ケ

The first ・ ア オ 。 エ ゥ ッ 」 。 ョ berewrittenasy MVᄋクN

Combiningthesetwo ゥ ョ ・ ア オ 。 ャ we seethat an arbitragedoesnotウ エ ゥ ヲ M V i.e., ヲ oオエウゥ、・ of the ゥ イ カ 。 ャ { P } we do

haveanarbitrage.For example, if f = 19 ^ thenthe option is overpricedandwe

shouldsell H i v イ ゥ エ ・ B I options. If we write b optionsandbuy oneshare,we will startwith a portfolio that is worth Q P P Q Y . b. If the stockgoesdown, the optionsareworthless;so, our portfolio is worth $70. If thestockgoesup, we lose$20oneachoptionwewrotebutseea gainonthestockwebought. We wantthenetgainto be ョ ッ ョ ョ ・ ァ 。 エ ゥ カ ・ Q 、 the initial

エ 。 seethe ヲ ゥ イ ウ エ newvalue ゥ ウ ゥ オ ウ エ 1. The time to seethe ウ ・ 」 ッ イ Q 、 ョ ・ キ カ 。 ャ オ ・

ヲ イ ッ イ イ ゥ イ ョ ・ キ カ 。 。 ャ オ ・ ゥ ウ s U _ ウ ゥ ョ 」 ・ エ ィ ・ ー イ ッ 「 」 ゥ 「 ゥ ャ ゥ エ ケ ッ ヲ ウ ・ ・ ゥゥ ョ Q Y 。 i v カ 。 ャ オ ・ ・

ァ ゥ ケ ・ ョ エ ィ 。 エ onevaluehasalreadybeenseen, is 5/6. In this way, the timetakento seethe third new value, given that two valueshave alreadybeenseen, ゥウ N Generalizingthis ゥ 、 ・ ・ ・ ョ エ ッ エ ィ ・ k-th

newvalue, giventhatk - 1valueshavealreadybeenseen, is tV HklIIOVᄋ

Hencetheexpectedtime to seeエ ィ ・ ィ ュ キ カ 。 ャ オ ・ ゥ ウ S K S K R K S

S K _ Q T N W

Solution10.12: Let f be the price for the option. A fair price is deter-minedby the no-arbitrageイ ・ ア オ ゥ イ ・ ュ ・ N Supposewe startwith a port-folio of x sharesand y optionsin S-xand y maybe negative(whichindicatesthatwe sell stocksor selloptions).

The initial valueof our ー 。 イ ッ ャ ゥ ッ is x . QPP ケ . f. On Day 100, twothingsmayhavehappened:

- The stockwentup andtheportfolio is worth x . 120+ y . 20.- Thestockwentdownandtheportfolio is worth x . 70.

If we could choosex 。ョ Q、 y in sucha way エ ィ 。 エ our initial ー ッ イ ッ ャ ゥ ッ hasa negativeカ 。 ャ オ ・ ᄋ M M M

ァ 。 イ 、 i ・ ウ ウ of エ ィ・ュ ャo vカ・ュ・イ Q エゥョ ャエ ィ・ ウ エ。」ォ ourportfolio takesanonnegativevalue, thenwe will havecreatedanarbitrage.

Thereforetheconditionsfor anarbitrageto existare:

QRP RPᄋ b > 0

100+ 19·b < 0

Z ZeエZZ[ZZ[ZZRZR_ Zココ[eゥZRZ[イjjヲ ュ Q ・ ア

ZR[RZZ Z Z_R SS エZ 「ZRZコZZヲ ウ・ our Q portfolio consistsッ ヲ z o o 」 Q ォp イ ッ 」 ・ ・ 、 ゥ イ Q Y as 。「ッ カ・ we seethe 」 ッ ョ 、 ゥ エ ゥ ッ イ Q for 。 ョ arbitrageto exist ゥ ウ Z

100. Xo + f . xャ xR < 0120· xッ RP . Xl + 1.02 . X2 セ P

70· x ッ Q N P R . X2 セ

w イ ゥ エ ゥ エ ィ ・ linear-terms?S AZr we seethat if 、 ・ エ H a I P thenwe canalwaysfiI1d m arbItrageSIReewecaI1solvea R 「 N Wewill denoterow iof A by Ai.

e エ コ ェ ャ エ ュ ャ W Q [ Q Z [ T Q Z [ R R T Texistsif theoptIOIIPI-ICeis ROt equalto f\

COI1versely}if theoptiOI1ispricedat ヲ 、 ・ エ H a I ッ andin particulAo = PNVRWU QKPSUXSaRᄋウゥョ」・aッゥウ。ャゥョ・。ュュ「ゥョ、orッヲaャ andA2 with ー ッ ウ ゥ エ キ ・ ゥ ァ ィ エ ウ thenif A 1x セ 0 and a R ク P Aoxmust 。 ャ ウ ッ

P sono arbitragecan ・ ク ゥ ウ エ N

Solution10.14:LetZbethepriceof thestockOI1day100.Theッ ー エ ゥ P ゥ ウworthlessif Z < 300.If theprice iS z SPoイ theoptiOI1·ydollars.Theexpectedvalueof Z is givenby thehtegralIsworth Z-300

portfolio to haveanegativevalue, i.e.,

ヲ。 オ 、ケ

ヲ ョI (x - 300) N「 、クNjSPP OR HRP IR

l ・jZ uZ Qゥ[ Z ゥョ Qs エ・ 。、 of 20. The ・ ク ー イ ・ ウ ウ ゥ ッ 。 「 ッ カ ・ simplifies to

The ゥ ョ 、 ・ ヲ ゥ ョ ゥ エ ・ ・ ゥ ョ エ ・ ァ イ 。 i ヲ ヲ G オ ェ

so the 、 ・ b ゥ エ ・ ゥ ョ エ 。 ャ equals O N Therefore エ ィ ・ ・ 、payoHon theoptionon day100 ゥセ PセGSY . 20 = $7.8.

CHAPTER10. PROBABILITY200If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution10.15: Thefirst thingto askis whatareyou trying to ッ ー ャ ゥ コ ・ _

Therearevarious ・ 」 カ ・ ウ all of which arereasonable-maximizeex-pectedーイ ッ エ minimize loss, maximizeratio of expectedーイ ッ ヲ エ ッ vari-ance, etc.

Let's say we want to ュ 。 ク ャ ゥ コ ・ profit. The expected ー イ ッ ヲ isjZZセPPH QN Xx - B) . 460dB. This simplifies to P N Y N T P P

M b K P N i bR

Thederivativeis 0.2B - 400.

Theexpectedprofit hasanegativederivativein therangeof interest-B E [0,400]. ThismeansthatasweincreaseB , we getlessandlessーイ ッ ヲ イ

sowe shouldkeepB = O.In retrospect, this resultmakessensesinceif we win the auction, we

arepayingtwice of X in ・ ク ー ・ 」 エ 。 エ ゥ 。 ョ 、 getting ャ ケ 1.8X in return.

Solution10.16: If theprobabilityof winning is P, thentheexpectedgainis M Q ー G N Hencefor a fair ァ 。 ュ ・ ] l/p.

The facevalueof the cardcanbeanynumberbetween1 and13. Forthe dealer, all valuesareequallylikely. Henceif the player'scardhasafacevaluei, thenthe ー イ ッ 「 。 「 ゥ ャ ゥ ッ ヲ winning for the playeris (i - 1)/13.If theplayeralwaystakesonly onerandomcard, his probabilityof win-ning is (1/13)2::i:l (i - 1)/13 = 6/13. Henceit makessenseto askforthe next card ャ ケ if the first card yields a probability less than 6/13,i.e., the face value of the first card is 7. If we are given that the facevalue of the first card is 7 or more, then the chancesof winning are(1/7) 2::;:7 (i - 1)/13 = 9/13; otherwise, it is 6/13. Hencethe overallprobabilityof キ ゥ ョ ョ ゥ ゥ ウ ᄋ ᄋ ] 99/169. Thusthe fair valuewouldbe Q V Y O Y Y Q N 707.

Solution10.17: We 」。 ゥ。ャャケ achievea probabilityof successof セ byalwayschoosingthefirst card.

A naturalway to proceedis to considertheprobabilityPk(f) of win-ning for the ッ ー エ ャ オ ュ strategyafter k cardsremain, of which! are redcards.Thenpk(!) = max(i ,i .Pk-l(! -1) + (1- i) .Pk-l(!))'

The basecasesfor the recurrencearePI (1) = 1 andPI (0) = O. Ap-plying the イ ・ 」 イ ・ ョ 」 ・ we obtainP2(2) = 1,P2(1) = セ pRHPI = 0, andP3(3) = 1,P3(2) = セ pSHQI = !, p3(0) = O. This suggeststhatPk(!) = i ,whichcandirectlybeverified from therecurrence.Thereforethebestwe」 。 、 ッ P52(26)= セセ = セ ゥウ nobetterthansimply selectingエ ャ ゥ イ ウ エ card.

An alternateview of this is that sincethe cardsin the deckare ran-domly ordered, the oddsof the top cardwe selectbeingred is the sameasthe cardat the 「 ッ ョ of the deck「 ・ ゥ ョ ァ キ ィ ゥ 」 ィ hasa i chanceofbeingredwhenthereare! redcardsandk cardsin total.

Solution 10.18: If we always select エ ィ ・ ウ ・ 」 。 イ ケ we have 。

chanceof selectingthebestsecretary.

202 CHAPTER10. PROBABILITY 203

oョ・B t to dobetteris to skip the ヲ ゥ イ ウ エ エ 。 イ ゥ ・ ウ andthenchoosf1the first one in ・ remainingset at is SUperior エ o ・ 「 ・ ウ エ j [ 。 zZ エエZZZ[ZRZ 、 ゥョエャ

ウゥョ」・ the ー イ ッ 「 。 ィ ゥ ャ ゥ エ ケ エ ィ 。 エ the ウ ・ 」 ッ イ ャ 、 bestsecretaryQ ゥ ・ ウ in theヲ ゥ イ ウ エ half andthe bestseer?taryis h the secondhalf is at least j Notethat theprobabilityof thism actuallymorethaZIi siRcethe s ・ c ッ ョ 、 「 ・ ウ t

[ZZ[ZZ ZZ[エイイZコZ Z [[S[エ [[ャ[ェ …ahigherエ ィ 。 。 ii エ is ォイ ャッキイ Q エ ィ。 エ ゥ ヲ we follow a strategyof ウ ォ ゥ ー ー ゥ ョ Q Y the ヲ ゥ イ ウ エ s s

andselectiI!gthefirst secretarywhois superiorto all othfar, the ー イ ッ 「 。 「 ゥ ャ i s 。 ク ゥ ュ ゥ コ ・ 、 for s closestエ ッ O e andthe maximumprobabilitytendsto 1/e.

Solution1019:LetL betheeventthattheselectedcohis tail-UiasedrUbe theever1tthat the selectedcohis head-biasedrmd3H5betheeveI1tthatMOhchosemtrmdomfrOIMhebagcomesupheads3timesoutof5 tOSSf1S

We want to compute p H l i S h U I N By Bayes'rule, this is Pr(Ln3H5)

ApplyhgBayedruleagaiIUthis pyobabilityequals PT(3H5)·

Pr(3H5IL) . Pr(L)

Pr(3H5 n (L U U))

Pr(3H5IL) . Pr(L)Pr(3H5 n lI pイHShU nU)

Pr(3H5IL) . p HlI

Pr(3H5IL) . pイHlI pイH ShU ihI . Pr(H)

H セ I . 0.43 . 0.62 . 0.5

HセI . 0.43. 0.62 • 0.5 + HセI .0.42 . 0.63 . 0.5

0.4

For the ウ ・ 」 ッ ー 。 イ キ ・ canusetheChebyshevゥ ョ 。 ャ ゥ エ ケ to computethenumberof tnalswe needfor a majority of エ ッ ウ ウ ・ ウ of the tail-biasedcoin tobeheadswith probability ziELet Lbe the ・ w i ャ エ 。 エ the i-thtossof the tail-biasedcohcomesup heads.Itwill be cOI1venieI1ttonpna b・イョ oャ 。イゥ。「ャ・ Xi to encodethis ・ カ ・ キ ゥ ィ Q m c ア 。 ィィ ・ 。 、 ウ 。 エ ャ 、 0 ゥョ、ゥ」。 エゥョァ エ。 ゥャウN

tィ・ュ・。ョ エィ・ sumX of n Bernoulli randomvariableswhichareIID with probabilityP ゥウ N P; thestandard、 ・ カ ゥ 。 エ ゥ ゥ ウ | O H Q N i ョour 」 ッ ョ エ ・ ク エ P N T 。 ョ 、 H j G ] O R U N v

re 」 ィ t ゥ ョ ・ ア オ 。 ァ ゥ カ ・ ウ us a 「ッ ッョ the ー イ ッ 「 ャ[ Q R _ 「 ヲ イ ッ ュ

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Solution10.15: Thefirst thingto askis whatareyou trying to ッ ー ャ ゥ コ ・ _

Therearevarious ・ 」 カ ・ ウ all of which arereasonable-maximizeex-pectedーイ ッ エ minimize loss, maximizeratio of expectedーイ ッ ヲ エ ッ vari-ance, etc.

Let's say we want to ュ 。 ク ャ ゥ コ ・ profit. The expected ー イ ッ ヲ isjZZセPPH QN Xx - B) . 460dB. This simplifies to P N Y N T P P

M b K P N i bR

Thederivativeis 0.2B - 400.

Theexpectedprofit hasanegativederivativein therangeof interest-B E [0,400]. ThismeansthatasweincreaseB , we getlessandlessーイ ッ ヲ イ

sowe shouldkeepB = O.In retrospect, this resultmakessensesinceif we win the auction, we

arepayingtwice of X in ・ ク ー ・ 」 エ 。 エ ゥ 。 ョ 、 getting ャ ケ 1.8X in return.

Solution10.16: If theprobabilityof winning is P, thentheexpectedgainis M Q ー G N Hencefor a fair ァ 。 ュ ・ ] l/p.

The facevalueof the cardcanbeanynumberbetween1 and13. Forthe dealer, all valuesareequallylikely. Henceif the player'scardhasafacevaluei, thenthe ー イ ッ 「 。 「 ゥ ャ ゥ ッ ヲ winning for the playeris (i - 1)/13.If theplayeralwaystakesonly onerandomcard, his probabilityof win-ning is (1/13)2::i:l (i - 1)/13 = 6/13. Henceit makessenseto askforthe next card ャ ケ if the first card yields a probability less than 6/13,i.e., the face value of the first card is 7. If we are given that the facevalue of the first card is 7 or more, then the chancesof winning are(1/7) 2::;:7 (i - 1)/13 = 9/13; otherwise, it is 6/13. Hencethe overallprobabilityof キ ゥ ョ ョ ゥ ゥ ウ ᄋ ᄋ ] 99/169. Thusthe fair valuewouldbe Q V Y O Y Y Q N 707.

Solution10.17: We 」。 ゥ。ャャケ achievea probabilityof successof セ byalwayschoosingthefirst card.

A naturalway to proceedis to considertheprobabilityPk(f) of win-ning for the ッ ー エ ャ オ ュ strategyafter k cardsremain, of which! are redcards.Thenpk(!) = max(i ,i .Pk-l(! -1) + (1- i) .Pk-l(!))'

The basecasesfor the recurrencearePI (1) = 1 andPI (0) = O. Ap-plying the イ ・ 」 イ ・ ョ 」 ・ we obtainP2(2) = 1,P2(1) = セ pRHPI = 0, andP3(3) = 1,P3(2) = セ pSHQI = !, p3(0) = O. This suggeststhatPk(!) = i ,whichcandirectlybeverified from therecurrence.Thereforethebestwe」 。 、 ッ P52(26)= セセ = セ ゥウ nobetterthansimply selectingエ ャ ゥ イ ウ エ card.

An alternateview of this is that sincethe cardsin the deckare ran-domly ordered, the oddsof the top cardwe selectbeingred is the sameasthe cardat the 「 ッ ョ of the deck「 ・ ゥ ョ ァ キ ィ ゥ 」 ィ hasa i chanceofbeingredwhenthereare! redcardsandk cardsin total.

Solution 10.18: If we always select エ ィ ・ ウ ・ 」 。 イ ケ we have 。

chanceof selectingthebestsecretary.

202 CHAPTER10. PROBABILITY 203

oョ・B t to dobetteris to skip the ヲ ゥ イ ウ エ エ 。 イ ゥ ・ ウ andthenchoosf1the first one in ・ remainingset at is SUperior エ o ・ 「 ・ ウ エ j [ 。 zZ エエZZZ[ZRZ 、 ゥョエャ

ウゥョ」・ the ー イ ッ 「 。 ィ ゥ ャ ゥ エ ケ エ ィ 。 エ the ウ ・ 」 ッ イ ャ 、 bestsecretaryQ ゥ ・ ウ in theヲ ゥ イ ウ エ half andthe bestseer?taryis h the secondhalf is at least j Notethat theprobabilityof thism actuallymorethaZIi siRcethe s ・ c ッ ョ 、 「 ・ ウ t

[ZZ[ZZ ZZ[エイイZコZ Z [[S[エ [[ャ[ェ …ahigherエ ィ 。 。 ii エ is ォイ ャッキイ Q エ ィ。 エ ゥ ヲ we follow a strategyof ウ ォ ゥ ー ー ゥ ョ Q Y the ヲ ゥ イ ウ エ s s

andselectiI!gthefirst secretarywhois superiorto all othfar, the ー イ ッ 「 。 「 ゥ ャ i s 。 ク ゥ ュ ゥ コ ・ 、 for s closestエ ッ O e andthe maximumprobabilitytendsto 1/e.

Solution1019:LetL betheeventthattheselectedcohis tail-UiasedrUbe theever1tthat the selectedcohis head-biasedrmd3H5betheeveI1tthatMOhchosemtrmdomfrOIMhebagcomesupheads3timesoutof5 tOSSf1S

We want to compute p H l i S h U I N By Bayes'rule, this is Pr(Ln3H5)

ApplyhgBayedruleagaiIUthis pyobabilityequals PT(3H5)·

Pr(3H5IL) . Pr(L)

Pr(3H5 n (L U U))

Pr(3H5IL) . Pr(L)Pr(3H5 n lI pイHShU nU)

Pr(3H5IL) . p HlI

Pr(3H5IL) . pイHlI pイH ShU ihI . Pr(H)

H セ I . 0.43 . 0.62 . 0.5

HセI . 0.43. 0.62 • 0.5 + HセI .0.42 . 0.63 . 0.5

0.4

For the ウ ・ 」 ッ ー 。 イ キ ・ canusetheChebyshevゥ ョ 。 ャ ゥ エ ケ to computethenumberof tnalswe needfor a majority of エ ッ ウ ウ ・ ウ of the tail-biasedcoin tobeheadswith probability ziELet Lbe the ・ w i ャ エ 。 エ the i-thtossof the tail-biasedcohcomesup heads.Itwill be cOI1venieI1ttonpna b・イョ oャ 。イゥ。「ャ・ Xi to encodethis ・ カ ・ キ ゥ ィ Q m c ア 。 ィィ ・ 。 、 ウ 。 エ ャ 、 0 ゥョ、ゥ」。 エゥョァ エ。 ゥャウN

tィ・ュ・。ョ エィ・ sumX of n Bernoulli randomvariableswhichareIID with probabilityP ゥウ N P; thestandard、 ・ カ ゥ 。 エ ゥ ゥ ウ | O H Q N i ョour 」 ッ ョ エ ・ ク エ P N T 。 ョ 、 H j G ] O R U N v

re 」 ィ t ゥ ョ ・ ア オ 。 ァ ゥ カ ・ ウ us a 「ッ ッョ the ー イ ッ 「 ャ[ Q R _ 「 ヲ イ ッ ュ

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

205

weretumtrue;whmY is queriedrwereturnfalse In thisway>thevalueッ ヲ x y is determinedP t afterboththe カ 。 イ ゥ 。 「 ャ ウ 。 イ ・ ッ

t t ィ ゥ ウ ァ ・ イ ャ ・ イ 。 ャ ゥ コ コ ・ ウ キ ゥ エ ィ ゥ エ ョ ャ 、 オ 」 エ エ

L k ・クーイ・ウウゥッイ Q イ・アオャ イ・ウ all thevariablesto have「 ・ ・ イ Q readbeforeits valueゥ ウ determined。 ョ ャ 、 its final valueis thevalueof the ャ 。 ウ エ カ 。 イ ゥ 。 「 S i ・ read.Fora クーイ・ウウゥッ ッヲ the ヲ ッ イ ュ イ ュ 「 are l セ イ ・ ウ ウ ゥ ・ _ Q

ェ ヲ j Q Q Zs ー ッ ウ ・ we evaluateanexpressionby 」 ィ ッ ッ ウ ゥ ッ ョ ・ of its two ウ ・ ク ᆳ

イ エ j i z z z ・ カ 。 ャ ・ evaluateエ ィ ・ ・ イ ウwe evaluatedfirst-ORFsvalue ゥ ウ ッ エ forcedby thesubexpressionthat

、 エ ヲ エ I Z イ z e z ウ [ Z エ イ イ エ z tZ z z z t j [ R W ケ chooseoneof (cPo /\ 」p ャI HPO|QI to ・ カ カ

'st ・ クー イ G・ウ ウウ ゥッイ Q evaluatedis true, we can ゥ ァ イ ャ o イ ・ thesec-o 、 [ P キ ゥ ウ ・ we evaluatethe ウ ・ 」 If エ ィ ・ ・ ク ッ イ ・ ウ ウ [ on ゥ • A

we reducedエ ャ Q

ー イ ・ ウ ウ ウ ゥ ッ ゥ ウ ヲ 。 ャ 。 エ i ・ 。 s エ ッ ョ ・ ッ ヲ エ Z ィ ・

・ j [ Q [ [ エ Z コ Z Z R [ エ Z e Z W s R ・ [ Z [ [ [ Z [ Z Z Z コ ヲ ァ イ イ エ エ Z Z Q o r ュ 、 ヲ tpectto avoid oneof the four ウ オ 「 G ・ ウ ウ ゥ ッ ョ ウ cPo,cP l' Q O j j e t ュ ェ [

Q(k + QI SᄋqHォIN

1:ZJhisr q H ォ I ] S k m ッ イ キ 。 イ 、 to use ゥ 」 エ ゥ ッ ョ to showQ ッ オ ヲ Z Q Q ヲ エ ッ ッ ヲ T

ォカ 。 イ ゥ ャ ・ ウ in an l ォ イ ・ ウ ウ ゥ ッ ョ so Q(k) =

For themajority of エ ッ ウ ウ ・ ウ to notbetails, it is ョ ・ 」 ・ ウ ウ 。 イ ィ 。 エ エ ィ ・ sumof then cointossesis greaterthanor equalto 0.5n. Wewanttoboundthisー 。 「 ゥ ャ ゥ 「 ケ 160' sowe take k = 10. We want to solvefor n suchthatP N U ョ Q P ᄋ R U i.e., oNャョ TNY Nェョ which is satisfiedfor ョ2400.Notethattheanalysisis nottight-theChebyshevinequalityrefersto the ー 。 「 ゥ ャ ゥ エ ケ of i x A 「 オ エ キ ・ 。 イ ・ ャ ケ looking at x N

TheChebyshevinequalityholdsfor all randomvariablesif theyhaveavariance.We canobtaina tighterboundby applyinga Chernoffbound,which is specificto thesumsof Bernoulli randomvariables.Specifically,

Chernoffboundstell us that p H x (1 + XII ・ ee We wmt tobound p イ H x P N U ョ = HQ PNRUIHPNTョII so 8 = 0.25. Thus we want

ゥ P T ョ ェ P \ 0.01; エ。ォゥ ョ。エオイ。ャャッァウ we ッ 「 エ 。 ゥ ョ H [ R U I R \ Q Q P P =-4.6, whichholdsfor ^ 552.

cィ・イョッヲヲ「ッオョ、 is alsopessimistic-throughsimulationcodeat-tachedbelow, we determinedthat キ ィ ・ ョ ] 553, only 17 times in 107

trials did we seea majority of tails; キ ィ ・ ョ ] 148, tails wasnot a major-ity in 0.88%of the trials.

PROBABILITYCHAPTER10.204

public class TailCoin {public static void maine String [] args ) {

int numFails = 0;int numTrials =new Integer( args[O] );

import java.util. Random;ゥ

Sa

G「イッ

double bias = 0.4;in t N = new Integer ( args[1] );Random r = new Random() ;for ( int i = 0 ; i < numTrials; i++ ) {

int sum = 0;for ( int j = 0 ; j < N; j++ ) {

sui ] ( r. nextDouble() < bias ) ? 1

if H ウ ^ ] N/2) numFai1s++;

jquqjョオQゥ

Saエ

ゥTゥTゥ

System.out. println (" fails: trials\t=\t 11+ numFai1s

QQ : 11 + numTrials+ 11\n\tratio\t=\t 11H (double) numFai1s / (double) numTrials) );

Oo

uqjョオゥ

アm

ゥエゥQゥ

GGOO

Solution10.20: First, we showthat any deterministicalgorithmmustexamineall Booleanvariables. The ideais that an adversarycanforcethe value of any subexpressionto be unknowntill all the variablesinthe subexpressionhavebeenread. For ・ ク ョ ー ャ ・ supposevariableX isANDedwith variableY. If the algorithmreadsthevalueof X beforeY ,

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

205

weretumtrue;whmY is queriedrwereturnfalse In thisway>thevalueッ ヲ x y is determinedP t afterboththe カ 。 イ ゥ 。 「 ャ ウ 。 イ ・ ッ

t t ィ ゥ ウ ァ ・ イ ャ ・ イ 。 ャ ゥ コ コ ・ ウ キ ゥ エ ィ ゥ エ ョ ャ 、 オ 」 エ エ

L k ・クーイ・ウウゥッイ Q イ・アオャ イ・ウ all thevariablesto have「 ・ ・ イ Q readbeforeits valueゥ ウ determined。 ョ ャ 、 its final valueis thevalueof the ャ 。 ウ エ カ 。 イ ゥ 。 「 S i ・ read.Fora クーイ・ウウゥッ ッヲ the ヲ ッ イ ュ イ ュ 「 are l セ イ ・ ウ ウ ゥ ・ _ Q

ェ ヲ j Q Q Zs ー ッ ウ ・ we evaluateanexpressionby 」 ィ ッ ッ ウ ゥ ッ ョ ・ of its two ウ ・ ク ᆳ

イ エ j i z z z ・ カ 。 ャ ・ evaluateエ ィ ・ ・ イ ウwe evaluatedfirst-ORFsvalue ゥ ウ ッ エ forcedby thesubexpressionthat

、 エ ヲ エ I Z イ z e z ウ [ Z エ イ イ エ z tZ z z z t j [ R W ケ chooseoneof (cPo /\ 」p ャI HPO|QI to ・ カ カ

'st ・ クー イ G・ウ ウウ ゥッイ Q evaluatedis true, we can ゥ ァ イ ャ o イ ・ thesec-o 、 [ P キ ゥ ウ ・ we evaluatethe ウ ・ 」 If エ ィ ・ ・ ク ッ イ ・ ウ ウ [ on ゥ • A

we reducedエ ャ Q

ー イ ・ ウ ウ ウ ゥ ッ ゥ ウ ヲ 。 ャ 。 エ i ・ 。 s エ ッ ョ ・ ッ ヲ エ Z ィ ・

・ j [ Q [ [ エ Z コ Z Z R [ エ Z e Z W s R ・ [ Z [ [ [ Z [ Z Z Z コ ヲ ァ イ イ エ エ Z Z Q o r ュ 、 ヲ tpectto avoid oneof the four ウ オ 「 G ・ ウ ウ ゥ ッ ョ ウ cPo,cP l' Q O j j e t ュ ェ [

Q(k + QI SᄋqHォIN

1:ZJhisr q H ォ I ] S k m ッ イ キ 。 イ 、 to use ゥ 」 エ ゥ ッ ョ to showQ ッ オ ヲ Z Q Q ヲ エ ッ ッ ヲ T

ォカ 。 イ ゥ ャ ・ ウ in an l ォ イ ・ ウ ウ ゥ ッ ョ so Q(k) =

For themajority of エ ッ ウ ウ ・ ウ to notbetails, it is ョ ・ 」 ・ ウ ウ 。 イ ィ 。 エ エ ィ ・ sumof then cointossesis greaterthanor equalto 0.5n. Wewanttoboundthisー 。 「 ゥ ャ ゥ 「 ケ 160' sowe take k = 10. We want to solvefor n suchthatP N U ョ Q P ᄋ R U i.e., oNャョ TNY Nェョ which is satisfiedfor ョ2400.Notethattheanalysisis nottight-theChebyshevinequalityrefersto the ー 。 「 ゥ ャ ゥ エ ケ of i x A 「 オ エ キ ・ 。 イ ・ ャ ケ looking at x N

TheChebyshevinequalityholdsfor all randomvariablesif theyhaveavariance.We canobtaina tighterboundby applyinga Chernoffbound,which is specificto thesumsof Bernoulli randomvariables.Specifically,

Chernoffboundstell us that p H x (1 + XII ・ ee We wmt tobound p イ H x P N U ョ = HQ PNRUIHPNTョII so 8 = 0.25. Thus we want

ゥ P T ョ ェ P \ 0.01; エ。ォゥ ョ。エオイ。ャャッァウ we ッ 「 エ 。 ゥ ョ H [ R U I R \ Q Q P P =-4.6, whichholdsfor ^ 552.

cィ・イョッヲヲ「ッオョ、 is alsopessimistic-throughsimulationcodeat-tachedbelow, we determinedthat キ ィ ・ ョ ] 553, only 17 times in 107

trials did we seea majority of tails; キ ィ ・ ョ ] 148, tails wasnot a major-ity in 0.88%of the trials.

PROBABILITYCHAPTER10.204

public class TailCoin {public static void maine String [] args ) {

int numFails = 0;int numTrials =new Integer( args[O] );

import java.util. Random;ゥ

Sa

G「イッ

double bias = 0.4;in t N = new Integer ( args[1] );Random r = new Random() ;for ( int i = 0 ; i < numTrials; i++ ) {

int sum = 0;for ( int j = 0 ; j < N; j++ ) {

sui ] ( r. nextDouble() < bias ) ? 1

if H ウ ^ ] N/2) numFai1s++;

jquqjョオQゥ

Saエ

ゥTゥTゥ

System.out. println (" fails: trials\t=\t 11+ numFai1s

QQ : 11 + numTrials+ 11\n\tratio\t=\t 11H (double) numFai1s / (double) numTrials) );

Oo

uqjョオゥ

アm

ゥエゥQゥ

GGOO

Solution10.20: First, we showthat any deterministicalgorithmmustexamineall Booleanvariables. The ideais that an adversarycanforcethe value of any subexpressionto be unknowntill all the variablesinthe subexpressionhavebeenread. For ・ ク ョ ー ャ ・ supposevariableX isANDedwith variableY. If the algorithmreadsthevalueof X beforeY ,

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

207

p 」ッューオエ・、⦅ー ゥエケ [i). This arraycaneitherbe 」 ッ ョ ウ エ イ エ ・ 、 、 ゥ ョ ァ staticinitialization or dynamically-aflag bit canbeusedto indicateif theen-try at a locationis uninitialized. Onceyou havethis array, you canim-pIementtheparity functionasfollows:

1 'short parity3(long a) {2 I short result = precomputed_parity[a»16];3 I result /\= precomputed_parity[a& OxFFFF];4 I return result;5 I}

Solution 11.2: Similar to computing parity (d. Problem 11.1)/ thefastest way to reverse bits would be to build a precomputedar-ray precomputed_reversesuch that for every 16-bit number i,precomputed…reverse[i] holds the bit-reversedi. Then you candosomethinglike this:

long reverse_bits(long l) {return (precomputed_reverse[l & OxFFFF] « 16)

precomputed_reverse[l » 16]

Solution11.3: Again, hereprecomputedarrayscanspeedthingssignif-icantly. Forall possible256valuesof abyte, we canstorethecorrespond-ゥ ョ ァ イ オ ャ ・ ョ ァ エ ィ encodedvalues.Onetricky thinghereis thata particularsequenceof identicalconsecutivebits maycrossthebyteboundaryandyou may needto combinethe resultsacrossthe byte boundaries.Thisjust requiressome 。 、 、 ゥ エ ゥ ッ イ ャ ッ ァ ゥ 」 to seeif the lastbit of the previousbyte matchesthe first bit of the currentbyte or not andaccordinglyei-ther simply concatenatethe encodedsequenceor addthe first numberfor thecurrentbyteto thelastnumberfor thepreviousbyte.

Solution11.4: We canusethe fact that everypermutationcanbe ex-pressedasacompositionof 、 ゥ ッ ゥ ョ エ cycles, with thedecompositionbeingオ ゥ ア オ ・ up to ordering.

For example, the permutation (3,1,2,4) can be イ ・ ー イ ・ ウ ・ エ ・ 、 as(1,3,2)(4)/ i.e., we canachievethe permutation(3,1,2,4) by thesetwomoves:1 •• 3,3 •• 2,2 •• 1 and4•• 4.

If the ー ・ イ ュ オ エ 。 エ ゥ キ 。 ウ givento usasa setof disjoint cycles, wecouldeasilyapply the ー ・ イ ュ オ エ 。 エ ゥ ッ ゥ ョ constantamountof additionalstoragesincewe justneedto performrotationby one ・ ャ ・ ュ ・ イ Qエ N s P O what イ ・ ュ 。 ゥ ョ Q

ゥ ウ away to identify thedisjoint cycles エ ィ 。 エ constitutethe ー ・ イ ュ Q u Q エ 。 エ ゥ ッ i Q N

Again, it is fairly easyto identify thesetof cyclesif youhaveanaddi-tional N bits: you startfrom any ー ッ ウ ゥ エ ゥ ッ 。 ョ 、 keepgoingforward (fromito A[i]) till youhit theinitial index, atwhich point you havefound P

of the 」 ケ 」 ャ ・ ウ N ィ ・ ョ you cango to anotherーッ ウ ゥ エ ゥ ッ エ ィ 。 エ is not alreadya

Chapter11

ua

Program.m.ing

Solution 11.1: The fastestalgorithm for manipulatingbits can varybasedontheunderlyinghardware.

The time takento directly computethe parity of a singlenumberisproportionalto thenumberof bits:

short parity(long a) {short result = 0;for (; a != 0; a = a » 1) {

result = result /\ (a & 1);

return result;

1234567

A neattrick thaterasestheleastsignificantbit of anumberin a singleッ ー ・ イ 。 エ ゥ ッ 」 。 ョ be usedエ ッ ー イ ッ カ ・ performancein thebestandaveragecases:

short parity2(long a) {short result = 0;while (a) {

resu lt /\= 1;a = a & (a - 1);

return result;

12345678

But whm you haveto pefform a lafge I1umberof parity operationsmd more generally;, my khd of bit fiddling operationythe bestwayto do this is to precomputethe answermd store it in m array.DeapendingupORhowmuchmemoryis at your disposal(md how muchfits efficieI1tly h cache)ryou cmvary the size of the lookup table.Be-low is an ・ ク 。 ュ ー ャ ・ ー ャ ・ ュ ・ エ 。 エ ゥ ッ ョ where you build a lookup table"precomputed_parity"that storesthe ー 。 イ ゥ ッ ヲ any 16-bit numberi as

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

207

p 」ッューオエ・、⦅ー ゥエケ [i). This arraycaneitherbe 」 ッ ョ ウ エ イ エ ・ 、 、 ゥ ョ ァ staticinitialization or dynamically-aflag bit canbeusedto indicateif theen-try at a locationis uninitialized. Onceyou havethis array, you canim-pIementtheparity functionasfollows:

1 'short parity3(long a) {2 I short result = precomputed_parity[a»16];3 I result /\= precomputed_parity[a& OxFFFF];4 I return result;5 I}

Solution 11.2: Similar to computing parity (d. Problem 11.1)/ thefastest way to reverse bits would be to build a precomputedar-ray precomputed_reversesuch that for every 16-bit number i,precomputed…reverse[i] holds the bit-reversedi. Then you candosomethinglike this:

long reverse_bits(long l) {return (precomputed_reverse[l & OxFFFF] « 16)

precomputed_reverse[l » 16]

Solution11.3: Again, hereprecomputedarrayscanspeedthingssignif-icantly. Forall possible256valuesof abyte, we canstorethecorrespond-ゥ ョ ァ イ オ ャ ・ ョ ァ エ ィ encodedvalues.Onetricky thinghereis thata particularsequenceof identicalconsecutivebits maycrossthebyteboundaryandyou may needto combinethe resultsacrossthe byte boundaries.Thisjust requiressome 。 、 、 ゥ エ ゥ ッ イ ャ ッ ァ ゥ 」 to seeif the lastbit of the previousbyte matchesthe first bit of the currentbyte or not andaccordinglyei-ther simply concatenatethe encodedsequenceor addthe first numberfor thecurrentbyteto thelastnumberfor thepreviousbyte.

Solution11.4: We canusethe fact that everypermutationcanbe ex-pressedasacompositionof 、 ゥ ッ ゥ ョ エ cycles, with thedecompositionbeingオ ゥ ア オ ・ up to ordering.

For example, the permutation (3,1,2,4) can be イ ・ ー イ ・ ウ ・ エ ・ 、 as(1,3,2)(4)/ i.e., we canachievethe permutation(3,1,2,4) by thesetwomoves:1 •• 3,3 •• 2,2 •• 1 and4•• 4.

If the ー ・ イ ュ オ エ 。 エ ゥ キ 。 ウ givento usasa setof disjoint cycles, wecouldeasilyapply the ー ・ イ ュ オ エ 。 エ ゥ ッ ゥ ョ constantamountof additionalstoragesincewe justneedto performrotationby one ・ ャ ・ ュ ・ イ Qエ N s P O what イ ・ ュ 。 ゥ ョ Q

ゥ ウ away to identify thedisjoint cycles エ ィ 。 エ constitutethe ー ・ イ ュ Q u Q エ 。 エ ゥ ッ i Q N

Again, it is fairly easyto identify thesetof cyclesif youhaveanaddi-tional N bits: you startfrom any ー ッ ウ ゥ エ ゥ ッ 。 ョ 、 keepgoingforward (fromito A[i]) till youhit theinitial index, atwhich point you havefound P

of the 」 ケ 」 ャ ・ ウ N ィ ・ ョ you cango to anotherーッ ウ ゥ エ ゥ ッ エ ィ 。 エ is not alreadya

Chapter11

ua

Program.m.ing

Solution 11.1: The fastestalgorithm for manipulatingbits can varybasedontheunderlyinghardware.

The time takento directly computethe parity of a singlenumberisproportionalto thenumberof bits:

short parity(long a) {short result = 0;for (; a != 0; a = a » 1) {

result = result /\ (a & 1);

return result;

1234567

A neattrick thaterasestheleastsignificantbit of anumberin a singleッ ー ・ イ 。 エ ゥ ッ 」 。 ョ be usedエ ッ ー イ ッ カ ・ performancein thebestandaveragecases:

short parity2(long a) {short result = 0;while (a) {

resu lt /\= 1;a = a & (a - 1);

return result;

12345678

But whm you haveto pefform a lafge I1umberof parity operationsmd more generally;, my khd of bit fiddling operationythe bestwayto do this is to precomputethe answermd store it in m array.DeapendingupORhowmuchmemoryis at your disposal(md how muchfits efficieI1tly h cache)ryou cmvary the size of the lookup table.Be-low is an ・ ク 。 ュ ー ャ ・ ー ャ ・ ュ ・ エ 。 エ ゥ ッ ョ where you build a lookup table"precomputed_parity"that storesthe ー 。 イ ゥ ッ ヲ any 16-bit numberi as

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

209

in t j = i;int tmp = A[ i ];do {

in t k = permutation[int swap_var = A[k];A[k] ] ー [

ョ ー = swap_var;j = k;

} while (j .,bᄋゥ

456789012345ゥ

ゥョl

OM

jィ

OM

TW

Solution115:Theso111tiORisverysimilar to thepreviousproblem.Allyou medto do is decomposethe permutatiORhtoa setof cyclesmdゥ ョ カ エ eachcycle onestepback. For example, the ー ・ イ ャ オ エ 。 エ ゥ ッ ョ 3,1,2,4canbe イ イ・ウ・ エ・、 as (1,3,2)(4). Hencethe inversecanbe representedas (2,3,1)(4) which amountsto 2,3,1,4.

h orderto saveadditionalspacerwe cmuseexactlythesamesetoftricks asin theaboveproblem.

Solution 11.6: If you try to figure out the position for each 」 ィ 。 イ 。 」ter h a siI1gle passrit becomesfairly complex-If you do this h twostages, it becomesfairly ・ 。 ウ i ョ the first step, invert the entire stringmdiMKsecoMsteprinmtmchword-FormmPIermmtsmtJUJyltsoc si ュ 」 ッ ウ エ ャ ケ is ュ N Hereis an examplecodethat achievesthis:

void InvertString(chau input , ウゥコ・ エ length) {for (int i = 0; i < leng-th O R [ K ゥ I {}swap(1nput+ir ゥrーオエKゥ・ョァエィMゥ QI ;

void ReverseWords(char* input) {size_t length = strlen(input) ;InvertStringH ゥ ー オ エ length);int start = 0;while( start < length) {

int end - start;キ ィ ゥ ャ セ (end < ャ・ョァエィ ゥョーオエ [end]

end.++;',-"') {

Solution11.7: Here is an examplecodethat reversesa linked list andイ ・ エ ョ ウ theheadpointerfor the reversedlist. The only importantthin

InvertString(input+ start , end-start);start = end + 1;

1234567890123456789

QTTS

TSaアゥQTQaゥQ

prog Qing

partof anycycle. Findinga ーッウゥエゥ エィ。エ is not alreadyapartof a cycleiseasyif youhaveabit-vectorthatcouldindicateキ ィ ・ ・ イ wehavealreadyincludeda givenpositionin a cycleor not.

Oneway to do this without usingadditionalO(N) storagecouldbeto usethesignbit in the integersthatconstitutethepermutation:

CHAPTERll.208

void ApplyPermutation(int * permutation, inh A , int n) {for (int i = 0; i < n; ++i) {

if (permutation[i] > 0) {/ / Star t sear ching for a cycl e from i.in t j = i;int tmp = A [ i ] ;do {

int k = permutation[j];int swap_var = A[k];A[k] = tmp;tmp = swap_var;/ I Mark j as visit ed .permutation[j] *= -1; I I sets the signj = k;

} while (j != i);

bit

ゥアGMアオa

fSヲo

uqjョオQa

Sa

MaQ

QTゥTゥ

I I Restore the sign for permutation.for (i n t i = 0; i < n; ++ i) {

ー・イュオエ。エゥッ {ゥ} *= -1;

ヲo

uqjョオMMF

MeaMMTQゥQ

The abovecodewill applythepermutationin O(N) timebut implic-itly we are オ ウ ゥ 。 、 、 ゥ エ ゥ ッ ョ 。 ャ O(N) storage(evenif we areborrowingitfrom the signbit of permutationmatrix). We needO(N) storageto re-memberall thevisitedcycles.

We canavoid this by just going from left to right andapplying thecycle ャ ケ if the 」 イ ・ ョ エ positionis the leftmostpositionin thecycle. Inorderto testwhetherthecurrentpositionis theleftmostpositionor not,youwill haveto traversethecycleoncemore.Thisbooststheruntimeto8(N2 ).

void ApplyPermutation2(int * permutation, inh A , int n) {for (int i = 0; i \[ KゥI {

II Traverse the cycle to see if i is the min elementbool min_element= true;int j = permutation[i];while( j != i) {

if (j < i) {min_element= false;break;

...J

teeMィ

n

ヲMMMゥエtᄋᄋaeumMTP

fl

コゥ

ree-pmzm

GGQ

rTA

ウN

1234567890123

ゥTゥ

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

209

in t j = i;int tmp = A[ i ];do {

in t k = permutation[int swap_var = A[k];A[k] ] ー [

ョ ー = swap_var;j = k;

} while (j .,bᄋゥ

456789012345ゥ

ゥョl

OM

jィ

OM

TW

Solution115:Theso111tiORisverysimilar to thepreviousproblem.Allyou medto do is decomposethe permutatiORhtoa setof cyclesmdゥ ョ カ エ eachcycle onestepback. For example, the ー ・ イ ャ オ エ 。 エ ゥ ッ ョ 3,1,2,4canbe イ イ・ウ・ エ・、 as (1,3,2)(4). Hencethe inversecanbe representedas (2,3,1)(4) which amountsto 2,3,1,4.

h orderto saveadditionalspacerwe cmuseexactlythesamesetoftricks asin theaboveproblem.

Solution 11.6: If you try to figure out the position for each 」 ィ 。 イ 。 」ter h a siI1gle passrit becomesfairly complex-If you do this h twostages, it becomesfairly ・ 。 ウ i ョ the first step, invert the entire stringmdiMKsecoMsteprinmtmchword-FormmPIermmtsmtJUJyltsoc si ュ 」 ッ ウ エ ャ ケ is ュ N Hereis an examplecodethat achievesthis:

void InvertString(chau input , ウゥコ・ エ length) {for (int i = 0; i < leng-th O R [ K ゥ I {}swap(1nput+ir ゥrーオエKゥ・ョァエィMゥ QI ;

void ReverseWords(char* input) {size_t length = strlen(input) ;InvertStringH ゥ ー オ エ length);int start = 0;while( start < length) {

int end - start;キ ィ ゥ ャ セ (end < ャ・ョァエィ ゥョーオエ [end]

end.++;',-"') {

Solution11.7: Here is an examplecodethat reversesa linked list andイ ・ エ ョ ウ theheadpointerfor the reversedlist. The only importantthin

InvertString(input+ start , end-start);start = end + 1;

1234567890123456789

QTTS

TSaアゥQTQaゥQ

prog Qing

partof anycycle. Findinga ーッウゥエゥ エィ。エ is not alreadyapartof a cycleiseasyif youhaveabit-vectorthatcouldindicateキ ィ ・ ・ イ wehavealreadyincludeda givenpositionin a cycleor not.

Oneway to do this without usingadditionalO(N) storagecouldbeto usethesignbit in the integersthatconstitutethepermutation:

CHAPTERll.208

void ApplyPermutation(int * permutation, inh A , int n) {for (int i = 0; i < n; ++i) {

if (permutation[i] > 0) {/ / Star t sear ching for a cycl e from i.in t j = i;int tmp = A [ i ] ;do {

int k = permutation[j];int swap_var = A[k];A[k] = tmp;tmp = swap_var;/ I Mark j as visit ed .permutation[j] *= -1; I I sets the signj = k;

} while (j != i);

bit

ゥアGMアオa

fSヲo

uqjョオQa

Sa

MaQ

QTゥTゥ

I I Restore the sign for permutation.for (i n t i = 0; i < n; ++ i) {

ー・イュオエ。エゥッ {ゥ} *= -1;

ヲo

uqjョオMMF

MeaMMTQゥQ

The abovecodewill applythepermutationin O(N) timebut implic-itly we are オ ウ ゥ 。 、 、 ゥ エ ゥ ッ ョ 。 ャ O(N) storage(evenif we areborrowingitfrom the signbit of permutationmatrix). We needO(N) storageto re-memberall thevisitedcycles.

We canavoid this by just going from left to right andapplying thecycle ャ ケ if the 」 イ ・ ョ エ positionis the leftmostpositionin thecycle. Inorderto testwhetherthecurrentpositionis theleftmostpositionor not,youwill haveto traversethecycleoncemore.Thisbooststheruntimeto8(N2 ).

void ApplyPermutation2(int * permutation, inh A , int n) {for (int i = 0; i \[ KゥI {

II Traverse the cycle to see if i is the min elementbool min_element= true;int j = permutation[i];while( j != i) {

if (j < i) {min_element= false;break;

...J

teeMィ

n

ヲMMMゥエtᄋᄋaeumMTP

fl

コゥ

ree-pmzm

GGQ

rTA

ウN

1234567890123

ゥTゥ

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

hereis thatyou savethepointerto thenextnodebefore ッ カ ・ イ キ イ ゥ エ ゥ ョ ァ ᆳ

1 INode* ReverseLinkedList (Node* head) {2 I Node* prey = nl [

3 I Node* current = head;4 I while ( current != NULL) {5 I nッ、・J ー = 」オ ・ョエ ^ョ・クエ[

6 I 」オイイ・ョエ ^ョ・クエ = prev;7 I prey = current;8 I current = tmp;9 I }

10 I return prev;11

Solution11.9: This is more of a trick ア オ ・ ウ エ ゥ ッ エ ィ 。 ョ a conceptualone.Given the pointer to a node, it is impossibleto deleteit from the listwithout modifying its predecessor'snext pointer and the only way togetto thepredecessoris to traversethelist from head.Howeverit is easyto deletethe nextnodesinceit just requiresmodifying the next pointerof the currentnode. Now if we copy the valuepart of thenextnodetothecurrentnode, this wouldbe ・ ア オ ゥ カ 。 ャ ・ エ ッ deletingthe 」 オ イ イ ・ ョ エ ッ 、 ・ N

(This アオ・ウエゥッ オウ・、 to becommonlyaskedbut it wouldbepoorprac-tice to usethis solutionin reallife-for example, a referenceto the suc-cessorof thenodethatwasjustdeletedis now corrupted.)

Solution11.10: At first glance, it would appearthatthesearchfunctiondoesa constantamountof work andthenrecurses 。 subarraythat islessthanhalf asbig asthearraypassedin-aclassicO(logn) algorithm.

Solution Q Q N X Z ィ ・ イ ・ are two elegantsolutionsto this problem. Onesolutionis thatyoutry to reversethelinked ャ ゥ ウ エ ャ 、 oneof thetwo thingscanhappen:

1. You reachthenull pointerat theendof the list-this indicatesthatthis wasa correctlyconstructedlinked list.

2. You reachtheheadpointerof thelist whichindicatesthatthelinkedlist hasa loop.

Of coursethis operationis destructive, i.e., you ュ ッ 、 ゥ t your inputbutyoucanrestoreエ ィ ・ ャ ー オ エ by reversingit again.

Another interestingapproachis to have two pointers traversethelinked list andin everystep, you advancethepointers.The first pointeris 。、 ャ。ョ」・、 by one ー ッ ウ ゥ エ ゥ ッ 。 ョ 、 the secondone is advancedby twopositions. If you havea correctlyconstructedlinked list, thenboth thepointerswill endup at thetail of thelist. Howeverif youhavea circularlinked list thenyouwouldbein aninfinite loop. Sincethesecondpointeris traversingthe loop twice asfast asthe ヲ ゥ イ ゥ エ will oftenintersectwiththe first pointerin the loop. If you find the two ー エ ・ イ ウ intersect, thiswould indicatethelist is circular.

Howeverthe arrayslicing-theCOIlstruetiORofthe subarray-ispo-tential1y expensive, dependingonhow it ゥ ウ ー ャ ・ ュ ・ エ ・ 、 N d ゥ ヲ ヲ ・ ・ ョ エ lan-guagesimplementarrayslicing in differentways: the elementsmaybealiasedto elementsh theoriginalarrayor theymaybecopied-Ifa copyゥ ウ 「 ・ ゥ ュ 。 、 ・ this copytakes8(l) time to compute, wherel is the i ・ エ ィ

of thearrayslide. Thereforethe イ ・ 」 イ ・ ョ 」 ・ is T(n) = XH IKtH I whichsolvesto t H I = XHI .

The right way to perform binary search, which avoids the copy,passesintegerindexesdenotingthe rangeto performsearchon (a1ter-nately, awhile loop canbeusedto avoidrecursion).SeeProblem1.2 formoredetails.

211CHAPTER11. PROGRAMMING210

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

hereis thatyou savethepointerto thenextnodebefore ッ カ ・ イ キ イ ゥ エ ゥ ョ ァ ᆳ

1 INode* ReverseLinkedList (Node* head) {2 I Node* prey = nl [

3 I Node* current = head;4 I while ( current != NULL) {5 I nッ、・J ー = 」オ ・ョエ ^ョ・クエ[

6 I 」オイイ・ョエ ^ョ・クエ = prev;7 I prey = current;8 I current = tmp;9 I }

10 I return prev;11

Solution11.9: This is more of a trick ア オ ・ ウ エ ゥ ッ エ ィ 。 ョ a conceptualone.Given the pointer to a node, it is impossibleto deleteit from the listwithout modifying its predecessor'snext pointer and the only way togetto thepredecessoris to traversethelist from head.Howeverit is easyto deletethe nextnodesinceit just requiresmodifying the next pointerof the currentnode. Now if we copy the valuepart of thenextnodetothecurrentnode, this wouldbe ・ ア オ ゥ カ 。 ャ ・ エ ッ deletingthe 」 オ イ イ ・ ョ エ ッ 、 ・ N

(This アオ・ウエゥッ オウ・、 to becommonlyaskedbut it wouldbepoorprac-tice to usethis solutionin reallife-for example, a referenceto the suc-cessorof thenodethatwasjustdeletedis now corrupted.)

Solution11.10: At first glance, it would appearthatthesearchfunctiondoesa constantamountof work andthenrecurses 。 subarraythat islessthanhalf asbig asthearraypassedin-aclassicO(logn) algorithm.

Solution Q Q N X Z ィ ・ イ ・ are two elegantsolutionsto this problem. Onesolutionis thatyoutry to reversethelinked ャ ゥ ウ エ ャ 、 oneof thetwo thingscanhappen:

1. You reachthenull pointerat theendof the list-this indicatesthatthis wasa correctlyconstructedlinked list.

2. You reachtheheadpointerof thelist whichindicatesthatthelinkedlist hasa loop.

Of coursethis operationis destructive, i.e., you ュ ッ 、 ゥ t your inputbutyoucanrestoreエ ィ ・ ャ ー オ エ by reversingit again.

Another interestingapproachis to have two pointers traversethelinked list andin everystep, you advancethepointers.The first pointeris 。、 ャ。ョ」・、 by one ー ッ ウ ゥ エ ゥ ッ 。 ョ 、 the secondone is advancedby twopositions. If you havea correctlyconstructedlinked list, thenboth thepointerswill endup at thetail of thelist. Howeverif youhavea circularlinked list thenyouwouldbein aninfinite loop. Sincethesecondpointeris traversingthe loop twice asfast asthe ヲ ゥ イ ゥ エ will oftenintersectwiththe first pointerin the loop. If you find the two ー エ ・ イ ウ intersect, thiswould indicatethelist is circular.

Howeverthe arrayslicing-theCOIlstruetiORofthe subarray-ispo-tential1y expensive, dependingonhow it ゥ ウ ー ャ ・ ュ ・ エ ・ 、 N d ゥ ヲ ヲ ・ ・ ョ エ lan-guagesimplementarrayslicing in differentways: the elementsmaybealiasedto elementsh theoriginalarrayor theymaybecopied-Ifa copyゥ ウ 「 ・ ゥ ュ 。 、 ・ this copytakes8(l) time to compute, wherel is the i ・ エ ィ

of thearrayslide. Thereforethe イ ・ 」 イ ・ ョ 」 ・ is T(n) = XH IKtH I whichsolvesto t H I = XHI .

The right way to perform binary search, which avoids the copy,passesintegerindexesdenotingthe rangeto performsearchon (a1ter-nately, awhile loop canbeusedto avoidrecursion).SeeProblem1.2 formoredetails.

211CHAPTER11. PROGRAMMING210If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Indexof Problem.s

k-clustering, 38ュクcィッュー WT

ク 2 Chomp, 74ᅲ cィッュー WT

PMQ ャ。ーウ。」ォ UU

2-SAT, 49500doors, 71

Anagrams, 17Anonymousletter, 18Approximatesort, 27Arbitrage, 47Assigningradiofrequencies, 44Asynchronouscallbacks, 61

Balls andbins, 78Barbershop, 63Bettingoncardcolors, 71Binarysearch, 88Birkhoff-von Neumanndecom-

position, 47

Channelcapacity, 48Checkingfor cyclicity, 87Checkingsimplicity, 22Circuit simulation, 28Climbing ウ エ 。 ゥ イ W Q

CNF-SAT, 57Collatzconjecture, 58Commonknowledge, 73Completionsearch, 21Computingxn , 57Computingsquareroots, 15

Computingthe binomial coeffi-cients, 70

Computingtheparity of a long,85

Connectedness, 42cッョエ。ゥョ・、 エ・イカ。ャウ RQ

Countingshortestpaths, 46Cuttingpaper, 32

Dancingwith thestars, 48Deletion from a singly linked

list, 87Differentiatingbiases, 82Dining philosophers, 63Distributedthrottling, 66Distributinglargefiles, 68Driving directions, 68

Edit distances, 52Efficient trials, 24Efficient userinterface, 37Ephemeralstatein a finite state

machine, 43Euler tour, 43Evenor odd, 73Expectednumberof dice rolls,

80Extendedcontacts, 42

Facility locationproblem, 56Find all occurrencesof a sub-

string, 50Findingthemin andmaxsimul-

taneously, 24

INDEXOFPROBLEMS

Findingthewinner and イ オ ョ ャ ・ イ ᆳ

up, 24Formingatrianglefrom random

lengths, 78Frogcrossing, 31

Gassingup, 73Goodsortingalgorithms, 23

h。 Mr。ュ。ョオェ。ョ number, 57Heightdetermination, 71Hersheybar, 74Host 、 ゥ ウ 」 ッ カ ・ イ V Y

Huffmancoding, 36

ImplementPageRank, 66Intersecttwo sortedarrays, 16Intersectinglines, 20Invert a ー・イュオエ。エゥッ XV

IP forwarding, 65ISBN cache, 68

Latencyイ ・ 、 オ 」 エ ゥ ッ V W

Leader・ ャ ・ 」 エ ゥ ッ V X

Leastdistancesorting, 25Loadbalancing, 33Longest nondecreasingsubse-

ア オ ・ 」 ・ S P

Longest palindrome ウ オ 「 ウ ・

ア オ ・ 」 ・ U R

Matrix search, 21Maximizingexpressions, 34Mergingsortedarrays, 27Minimize waiting time, 36Missingelement, 18Missing element, limited re-

sources, 16Mosaic, 64

Nearestpointsin the ー i ャ ・ U X

Nonuniform random numbergeneration, 79

NormalizeURLs, 51

213

Offline sampling, 77P 」・ or twice, 82Online advertisingsystem, 67Onlinepoker, 67Onlinesampling, 78oーエゥイ オュ bidding, 81Optimumbuffer insertion, 34Optionpricingwith interest, 81Option ー イ ゥ 」 ゥ ョ ァ M 」 ッ ョ エ ゥ ョ オ ッ オ ウ

case, 81oーエゥッ ーイゥ」ゥョァM、ゥウ」イ・エ・ case,

81Ordernodesin a binary treeby

depth, 42

Packingfor USPSpriority mail,37

Pairingusersby attributes, 18Partyplanning, 39PCBwiring, 42Permutingthe elementsof anar-

ray, 86Pickingup coins-I, 75Pickingup coins-II, 75Pointscoveringintervals, 37Prettyprinting, 52Primality checking, 58Privacyandanonymization, 25Producer-consumerqueue, 62

Ramseytheory, 71Randomdirectedacyclic graph,

46Randompermutations, 79Randompermutations-l, 78Randomー・ イ ュ オ エ 。 エ ゥ ッ ウ M R W X

Rayscoveringarcs, 38Readers-writers, 62Readers-writerswith fairness,

62Readers-writerswith write pref-

erence, 62Recommendationsystem, 67Redor bluehousemajority, 33

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

Indexof Problem.s

k-clustering, 38ュクcィッュー WT

ク 2 Chomp, 74ᅲ cィッュー WT

PMQ ャ。ーウ。」ォ UU

2-SAT, 49500doors, 71

Anagrams, 17Anonymousletter, 18Approximatesort, 27Arbitrage, 47Assigningradiofrequencies, 44Asynchronouscallbacks, 61

Balls andbins, 78Barbershop, 63Bettingoncardcolors, 71Binarysearch, 88Birkhoff-von Neumanndecom-

position, 47

Channelcapacity, 48Checkingfor cyclicity, 87Checkingsimplicity, 22Circuit simulation, 28Climbing ウ エ 。 ゥ イ W Q

CNF-SAT, 57Collatzconjecture, 58Commonknowledge, 73Completionsearch, 21Computingxn , 57Computingsquareroots, 15

Computingthe binomial coeffi-cients, 70

Computingtheparity of a long,85

Connectedness, 42cッョエ。ゥョ・、 エ・イカ。ャウ RQ

Countingshortestpaths, 46Cuttingpaper, 32

Dancingwith thestars, 48Deletion from a singly linked

list, 87Differentiatingbiases, 82Dining philosophers, 63Distributedthrottling, 66Distributinglargefiles, 68Driving directions, 68

Edit distances, 52Efficient trials, 24Efficient userinterface, 37Ephemeralstatein a finite state

machine, 43Euler tour, 43Evenor odd, 73Expectednumberof dice rolls,

80Extendedcontacts, 42

Facility locationproblem, 56Find all occurrencesof a sub-

string, 50Findingthemin andmaxsimul-

taneously, 24

INDEXOFPROBLEMS

Findingthewinner and イ オ ョ ャ ・ イ ᆳ

up, 24Formingatrianglefrom random

lengths, 78Frogcrossing, 31

Gassingup, 73Goodsortingalgorithms, 23

h。 Mr。ュ。ョオェ。ョ number, 57Heightdetermination, 71Hersheybar, 74Host 、 ゥ ウ 」 ッ カ ・ イ V Y

Huffmancoding, 36

ImplementPageRank, 66Intersecttwo sortedarrays, 16Intersectinglines, 20Invert a ー・イュオエ。エゥッ XV

IP forwarding, 65ISBN cache, 68

Latencyイ ・ 、 オ 」 エ ゥ ッ V W

Leader・ ャ ・ 」 エ ゥ ッ V X

Leastdistancesorting, 25Loadbalancing, 33Longest nondecreasingsubse-

ア オ ・ 」 ・ S P

Longest palindrome ウ オ 「 ウ ・

ア オ ・ 」 ・ U R

Matrix search, 21Maximizingexpressions, 34Mergingsortedarrays, 27Minimize waiting time, 36Missingelement, 18Missing element, limited re-

sources, 16Mosaic, 64

Nearestpointsin the ー i ャ ・ U X

Nonuniform random numbergeneration, 79

NormalizeURLs, 51

213

Offline sampling, 77P 」・ or twice, 82Online advertisingsystem, 67Onlinepoker, 67Onlinesampling, 78oーエゥイ オュ bidding, 81Optimumbuffer insertion, 34Optionpricingwith interest, 81Option ー イ ゥ 」 ゥ ョ ァ M 」 ッ ョ エ ゥ ョ オ ッ オ ウ

case, 81oーエゥッ ーイゥ」ゥョァM、ゥウ」イ・エ・ case,

81Ordernodesin a binary treeby

depth, 42

Packingfor USPSpriority mail,37

Pairingusersby attributes, 18Partyplanning, 39PCBwiring, 42Permutingthe elementsof anar-

ray, 86Pickingup coins-I, 75Pickingup coins-II, 75Pointscoveringintervals, 37Prettyprinting, 52Primality checking, 58Privacyandanonymization, 25Producer-consumerqueue, 62

Ramseytheory, 71Randomdirectedacyclic graph,

46Randompermutations, 79Randompermutations-l, 78Randomー・ イ ュ オ エ 。 エ ゥ ッ ウ M R W X

Rayscoveringarcs, 38Readers-writers, 62Readers-writerswith fairness,

62Readers-writerswith write pref-

erence, 62Recommendationsystem, 67Redor bluehousemajority, 33

If y

ou f

ind t

he b

ook h

elp

ful, p

lea

se p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

214

r・ ャ。イ expression matching,53

Reservoirsampling, 77Reverseall the words in a sen-

tence, 86Reversinga singly linked list, 87Reversingthebits in a long, 85Roadnetwork, 47Robotbatterycapacity, 18Rotatea string, 51Run-lengthencoding, 85Runningaverages, 27

Scalablepriority system, 66Scheduling, 57Schedulingtutors, 35Searcha sortedarrayfor a ]

i, 16Searcha sortedarrayfor k, 16Searchasortedarrayfor thefirst

elementlarger than k,16

Search an array of unknownlength, 16

SearchBST for x > k, 20SearchBST for a key, 20Searchengine, 65Searchfor a pair which sumsto

8, 17Searchfor frequentitems, 19Searchfor majority, 19Searchinga maze, 41Searchingtwo sortedarrays, 20Selectinga redcard, 82Selectingthebestsecretary, 82Servlet with caching, 60Shortestpathwith fewestedges,

45Shortestpathsin thepresenceof

randomization, 46Space-timeintersections, 75Spellchecker, 65Stableassignment, 47sエ・ュイ QY VU

INDEXOFPROBLEMS

String matching with uniquecharacters, 51

t・ Q photoday-I, 44・ ーィッエッ 、。ケ R TX

TeraSort, 24Testrotation, 51Gィ ・ complexityof AND-OR for-

mulas, 83Gィ ・ッイケ of equality, 49オ・。、 pools, 61

Tiesin a ーイ・ウゥ、・ ゥ。ャ election, 32Timer, 61Timing analysis, 44Travelingsalesmanin theplane,

56Traveling salesman with a

choice, 46Treediameter, 43Triangulation, 34

Uniform randomnumbergen-eration, 79

Unique ・ ャ ・ ュ ・ ョ R V

Variablelengthsort, 26View from thetop, 21Voltageselection, 34

Wordbreaking, 32If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!

214

r・ ャ。イ expression matching,53

Reservoirsampling, 77Reverseall the words in a sen-

tence, 86Reversinga singly linked list, 87Reversingthebits in a long, 85Roadnetwork, 47Robotbatterycapacity, 18Rotatea string, 51Run-lengthencoding, 85Runningaverages, 27

Scalablepriority system, 66Scheduling, 57Schedulingtutors, 35Searcha sortedarrayfor a ]

i, 16Searcha sortedarrayfor k, 16Searchasortedarrayfor thefirst

elementlarger than k,16

Search an array of unknownlength, 16

SearchBST for x > k, 20SearchBST for a key, 20Searchengine, 65Searchfor a pair which sumsto

8, 17Searchfor frequentitems, 19Searchfor majority, 19Searchinga maze, 41Searchingtwo sortedarrays, 20Selectinga redcard, 82Selectingthebestsecretary, 82Servlet with caching, 60Shortestpathwith fewestedges,

45Shortestpathsin thepresenceof

randomization, 46Space-timeintersections, 75Spellchecker, 65Stableassignment, 47sエ・ュイ QY VU

INDEXOFPROBLEMS

String matching with uniquecharacters, 51

t・ Q photoday-I, 44・ ーィッエッ 、。ケ R TX

TeraSort, 24Testrotation, 51Gィ ・ complexityof AND-OR for-

mulas, 83Gィ ・ッイケ of equality, 49オ・。、 pools, 61

Tiesin a ーイ・ウゥ、・ ゥ。ャ election, 32Timer, 61Timing analysis, 44Travelingsalesmanin theplane,

56Traveling salesman with a

choice, 46Treediameter, 43Triangulation, 34

Uniform randomnumbergen-eration, 79

Unique ・ ャ ・ ュ ・ ョ R V

Variablelengthsort, 26View from thetop, 21Voltageselection, 34

Wordbreaking, 32

If y

ou f

ind t

he b

ook h

elp

ful, p

lease p

urc

hase a

copy t

o s

upport

the a

uth

ors

!