Algorithms Department of Computer Science University of Illinois at Urbana-Champaign Instructor: Jeff Erickson Teaching Assistants: • Spring 1999: MitchHarrisandShripadThite • Summer 1999 (IMCS):MitchHarris • Summer 2000 (IMCS):MitchHarris • Fall 2000: ChrisNeihengen,EktaManaktala,andNickHurlburt • Spring 2001: BrianEnsink,ChrisNeihengen,andNickHurlburt • Summer 2001 (I2CS):AshaSeetharamandDanBullok • Fall 2002: Erin Wolf, Gio Kao, Kevin Small, Michael Bond, Rishi Talreja, Rob McCann, and YasutakaFurakawa • Spring 2004: DanCranston,JohnathonFischer,KevinMilans,andLanChen • Fall 2005: ErinChambers,IgorGammer,andAdityaRamani • Fall 2006: DanCranston,NitishKorula,andKevinMilans • Spring 2007: KevinMilans • Fall 2008: RezaZamani-Nasab • Spring 2009: AlinaEne,BenMoseley,andAmirNayyeri • Spring 2010: DavidMorrison,KyleFox,andRachitAgarwal • Fall 2010: AlinaEne (cid:13)c Copyright1999–2011JeffErickson.LastupdateJuly8,2011. Thisworkmaybefreelycopiedanddistributed,eitherelectronicallyoronpaper. Itmaynotbesoldformorethantheactualcostofreproduction,storage,ortransmittal. ThisworkislicensedunderaCreativeCommonsAttribution-NonCommercial-ShareAlike3.0UnitedStatesLicense. Forlicensedetails,seehttp://creativecommons.org/licenses/by-nc-sa/3.0/us/. Forthemostrecenteditionofthiswork,seehttp://www.cs.illinois.edu/~jeffe/teaching/algorithms/. ShallItellyou,myfriend,howyouwillcometounderstandit? Goandwriteabookonit. —HenryHome,LordKames(1696–1782),toSirGilbertElliot Youknow,Icouldwriteabook. Andthisbookwouldbethickenoughtostunanox. —LaurieAnderson,“LetX=X”,BigScience(1982) I’mwritingabook.I’vegotthepagenumbersdone, sonowIjusthavetofillintherest. —StephenWright About These Notes Thiscoursepacketincludeslecturenotes,homeworkquestions,andexamquestionsfromalgorithms courses I taught at the University of Illinois at Urbana-Champaign in Spring 1999, Fall 2000, Spring 2001,Fall2002,Spring2004,Fall2005,Fall2006,Spring2007,Fall2008,Spring2009,Spring2010, andFall2010. TheselecturenotesandmyvideotapedlectureswerealsoofferedoverthewebinSummer 1999, Summer 2000, Summer 2001, Fall 2002, and Fall 2005 as part of the UIUC computer science department’sonlinemaster’sprogram. Lecturenoteswerepostedtothecoursewebsiteafewdays(on average)aftereachlecture. Homeworks,exams,andsolutionswerealsodistributedovertheweb. Most (but not all) of the exercises at the end of each lecture note have been used at least once in ahomeworkassignment,discussionsection,orexam. Youcanalsofindanear-completecollectionof homeworks and exams from past semesters of my class online at http://www.cs.illinois.edu/~jeffe/ teaching/algorithms/. Alargefractionoftheseexerciseswerecontributedbysomeamazingteaching assistants: AdityaRamani,AlinaEne,AmirNayyeri,AshaSeetharam,BenMoseley,BrianEnsink,Chris Neihengen,DanBullok,DanCranston,DavidMorrison,JohnathonFischer,EktaManaktala, Erin Wolf Chambers, Igor Gammer, Gio Kao, Kevin Milans, Kevin Small, Kyle Fox, Lan Chen, Michael Bond, Mitch Harris, Nick Hurlburt, Nitish Korula, Rachit Agarwal, Reza Zamani-Nasab,RishiTalreja,RobMcCann,ShripadThite,andYasuFurakawa. (cid:63) Stars indicate more challenging problems; many of these appeared on qualifying exams for the (cid:198) algorithmsPhDstudentsatUIUC.Asmallnumberofreally hardproblemsaremarkedwitha larger star;oneortwoopenproblemsareindicatedby(cid:198)enormousstars. Pleasedonotaskmeforsolutionstotheexercises. Ifyou’reastudent,seeingthesolutionwillrob you of the experience of solving the problem yourself, which is the only way to learn the material. If you’reaninstructor,youshouldn’tassignproblemsthatyoucan’tsolveyourself! (Idonotalwaysfollow myownadvice;someoftheseproblemshaveseriousbugs.) Acknowledgments Thelecturenotesandexercisesdrawheavilyonthecreativity,wisdom,andexperienceofthousandsof algorithmsstudents,teachers,andresearchers. Inparticular,Iamimmenselygratefultothealmost1400 Illinoisstudentswhohaveusedthesenotesasaprimaryreference,offereduseful(ifsometimespainful) criticism,andsufferedthroughsometrulyawfulfirstdrafts. I’malsogratefulforthecontributionsand feedbackfromteachingassistants,alllistedabove. Naturally,thesenotesoweagreatdealtothepeoplewhotaughtmethisalgorithmsstuffinthefirst place: BobBixbyandMichaelPerlmanatRice;DavidEppstein,DanHirshberg,andGeorgeLuekeratUC Irvine;andAbhiramRanade,DickKarp,ManuelBlum,MikeLuby,andRaimundSeidelatUCBerkeley. I’vealsobeenhelpedtremendouslybymanydiscussionswithfacultycolleaguesatUIUC—EdgarRamos, HerbertEdelsbrunner,JasonZych,LennyPitt,MaheshViswanathan,MargaretFleck,Shang-HuaTeng, Steve LaValle, and especially Chandra Chekuri, Ed Reingold, and Sariel Har-Peled. I stole the first iterationoftheoverallcoursestructure,andtheideatowriteupmyownlecturenotes,fromHerbert Edelsbrunner. Finally,“Johnny’s”multi-coloredcrayonhomeworkwasfoundundertheTAofficedooramongthe otherFall2000Homework1submissions. ThesquareKufirenditionofthename“al-Khwa¯rizm¯ı”onthe backofthecoverpageismine. Additional References Istronglyencouragemystudents(andotherreaders)nottorestrictthemselvestoasingletextual reference. Authorsandreadersbringtheirownperspectivestothematerial;noinstructor‘clicks’with every student, or even every very strong student. Finding the author that most effectively gets their intuitionintoyourheadtakesomeeffort,butthateffortpaysoffhandsomelyinthelongrun. Thefollowingreferenceshavebeenparticularlyvaluablesourcesofinspiration,intuition,examples, andproblems. Thislistisincomplete! • Alfred V. Aho, John E. Hopcroft, and Jeffrey D. Ullman. The Design and Analysis of Computer Algorithms. Addison-Wesley,1974. (IusedthistextbookasanundergradatRice,andagainasa mastersstudentatUCIrvine.) • ThomasCormen,CharlesLeiserson,RonRivest,andCliffStein. IntroductiontoAlgorithms,third edition. MITPress/McGraw-Hill,2009. (Thesecondeditionwasmyrecommendedtextbookuntil 2005. IalsousedthefirsteditionasateachingassistantatBerkeley.) • Sanjoy Dasgupta, Christos H. Papadimitriou, and Umesh V. Vazirani. Algorithms. McGraw-Hill, 2006. (Thisisthecurrentrecommendedtextbookformyundergraduateclasses.) • JeffEdmonds. HowtoThinkaboutAlgorithms. CambridgeUniversityPress,2008. • Michael R. Garey and David S. Johnson. Computers and Intractability: A Guide to the Theory of NP-Completeness. W.H.Freeman,1979. • MichaelT.GoodrichandRobertoTamassia. AlgorithmDesign: Foundations,Analysis,andInternet Examples. JohnWiley&Sons,2002. • Jon Kleinberg and Éva Tardos. Algorithm Design. Addison-Wesley, 2005. (This is the current recommendedtextbookformygraduatealgorithmsclasses.) • DonaldKnuth. TheArtofComputerProgramming,volumes1–3. Addison-Wesley,1997. (Myparents gavemetheseforChristmaswhenIwas14. Ididn’tactuallyreadthemuntilmuchlater.) • UdiManber. IntroductiontoAlgorithms: ACreativeApproach. Addison-Wesley,1989. (Iusedthis textbookasateachingassistantatBerkeley.) • RajeevMotwaniandPrabhakarRaghavan. RandomizedAlgorithms. CambridgeUniversityPress, 1995. • IanParberry. ProblemsonAlgorithms. Prentice-Hall,1995. (Thisbookisoutofprint,butitcanbe downloadedfor‘free’fromhttp://www.eng.unt.edu/ian/books/free/license.html.) • AlexanderSchrijver. CombinatorialOptimization: PolyhedraandEfficiency. Springer,2003. • RobertSedgewick. Algorithms. Addison-Wesley,1988. (Thisbookanditssequelshavebyfarthe bestalgorithmillustrationsI’veseenanywhere.) • RobertEndreTarjan. DataStructuresandNetworkAlgorithms. SIAM,1983. • RobertJ.Vanderbei. LinearProgramming: FoundationsandExtensions. Springer,2001. • ClassnotesfrommyownalgorithmsclassesatBerkeley,especiallythosetaughtbyDickKarpand RaimundSeidel. • Lecturenotes,slides,homeworks,exams,andvideolecturespostedbyinnumerablecolleagues aroundtheworld. • TheSourceofAllKnowledge(Google)andTheSourceofAllLies(Wikipedia). Prerequisites For the most part, these notes assume the reader has mastered the material covered in the first twoyearsofastrongundergraduatecomputersciencecurriculum,andhastheintellectualmaturityto recognizeandrepairanyremaininggapsintheirmastery. (Masteryisnotthesamethingas‘exposure’or ‘agoodgrade’;thisiswhyIstarteverysemesterwithHomeworkZero.) Specificprerequisitesinclude: • Proof techniques: direct proof, indirect proof, proof by contradiction, combinatorial proof, and induction(includingits“strong”,“structural”,and“recursive”forms). Lecture0requiresinduction, andwheneverLecture n−1requiresinduction,sodoesLecture n. • Discretemathematics: High-schoolalgebra,naivesettheory,Booleanalgebra,first-orderpredicate logic,sets,functions,relations,modulararithmetic,recursivedefinitions,trees(asabstractobjects, notdatastructures),graphs. • Elementarydiscreteprobability: uniformvsnon-uniformprobabilitydistributions,expectation, linearityofexpectation,independence. • Iterativeprogrammingconcepts: variables,conditionals,iteration,subroutines,indirection(ad- dresses/pointers/references),recursion. Programmingexperienceinanylanguagethatsupports pointersandrecursionisaplus. • Fundamentaldatastructures: arrays,linkedlists,binarysearchtrees,atleastonebalancedsearch tree(suchasAVLtrees,red-blacktrees,B-trees,skiplists,splaytrees,ortreaps),binaryheaps. • Fundamental abstract data types: dictionaries, stacks, queues, priority queues; the difference betweenthislistandthepreviouslist. • Fundamentalalgorithms: elementaryarithmetic,linearsearch,binarysearch,sorting(selection, insertion,merge-,heap-,quick-,radix,anythingbutbubble-),pre-/post-/inordertreetraversal. • Basic algorithm analysis: Asymptotic notation (o, O, Θ, Ω, ω), translating loops into sums and recursivecallsintorecurrences,evaluatingsimplesumsandrecurrences. • Mathematical maturity: facility with abstraction, formal (especially recursive) definitions, and (especiallyinductive)proofs;followingmathematicalarguments;recognizingsyntactic,semantic, and/orlogicalnonsense;writingtheformerratherthanthelatter. Someofthisprerequisitematerialiscoveredbrieflyinthesenotes,butmoreasareminderthana goodintroduction. Caveat Lector! Withfewexceptions,eachofthesenotescontainsfartoomuchmaterialtocoverinasinglelecture. In a typical 75-minute lecture, I tend to cover 4 to 5 pages of material—a bit more if I’m lecturing to graduate students than to undergraduates. Your mileage may vary! (Arguably, that means that as I continuetoaddmaterial,thelabel“lecturenotes”becomeslessandlessaccurate.) Despite several rounds of revision, these notes still contain lots of mistakes, errors, bugs, gaffes, omissions, snafus, kludges, typos, mathos, grammaros, thinkos, brain farts, nonsense, garbage, cruft, junk,andoutrightlies,all of which are entirely Steve Skiena’s fault. Ireviseandupdatethesenotes everytimeIteachthecourse,sopleaseletmeknowifyoufindabug. (Steveisunlikelytocare.) Whenever I teach the algorithms class, I award extra credit points to the first student to post an explanation and correction of any error in the lecture notes to the course newsgroup. Obviously, the number of extra credit points depends on the severity of the error and the quality of the correction. If I’m not teaching the course, encourage your instructor to set up a similar extra-credit scheme, and forwardthebugreportstoSteveme! Ofcourse,anyotherfeedbackisalsowelcome! Enjoy! —Jeff It is traditional for the author to magnanimously accept the blame for whatever deficienciesremain. Idon’t. Anyerrors,deficiencies,orproblemsinthisbookare somebodyelse’sfault,butIwouldappreciateknowingaboutthemsoastodetermine whoistoblame. —StevenS.Skiena,TheAlgorithmDesignManual(1997) (cid:13)c Copyright2011JeffErickson. ReleasedunderaCreativeCommonsAttribution-NonCommercial-ShareAlike3.0License(http://creativecommons.org/licenses/by-nc-sa/3.0/). Freedistributionisstronglyencouraged;commercialdistributionisexpresslyforbidden. Seehttp://www.cs.uiuc.edu/~jeffe/teaching/algorithms/forthemostrecentrevision. Algorithms Lecture 0: Introduction [F10] Weshouldexplain,beforeproceeding,thatitisnotourobjecttoconsiderthisprogramwith referencetotheactualarrangementofthedataontheVariablesoftheengine,butsimply asanabstractquestionofthenatureandnumberoftheoperationsrequiredtobeperfomed duringitscompletesolution. —AdaAugustaByronKing,CountessofLovelace,translator’snotesforLuigiF.Menabrea, “SketchoftheAnalyticalEngineinventedbyCharlesBabbage,Esq.”(1843) Youarerighttodemandthatanartistengagehisworkconsciously, butyouconfusetwo differentthings:solvingtheproblemandcorrectlyposingthequestion. —AntonChekhov,inalettertoA.S.Suvorin(October27,1888) Themorewereduceourselvestomachinesinthelowerthings, themoreforceweshallsetfreetouseinthehigher. —AnnaC.Brackett,TheTechniqueofRest(1892) Themomentamanbeginstotalkabouttechnique that’sproofthatheisfreshoutofideas. —RaymondChandler 0 Introduction 0.1 What is an algorithm? An algorithm is an explicit, precise, unambiguous, mechanically-executable sequence of elementary instructions. Forexample,hereisanalgorithmforsingingthatannoyingsong‘99BottlesofBeeronthe Wall’,forarbitraryvaluesof99: BOTTLESOFBEER(n): Fori←ndownto1 Sing“i bottlesofbeeronthewall,i bottlesofbeer,” Sing“Takeonedown,passitaround,i−1bottlesofbeeronthewall.” Sing“Nobottlesofbeeronthewall,nobottlesofbeer,” Sing“Gotothestore,buysomemore,nbottlesofbeeronthewall.” Theword‘algorithm’doesnotderive,asalgorithmophobicclassicistsmightguess,fromtheGreek root algos (ἄλγος), meaning ‘pain’. Rather, it is a corruption of the name of the 9th century Persian mathematician Abu¯ ’Abd Alla¯h Muh.ammad ibn Mu¯sa¯ al-Khwa¯rizm¯ı.1 Al-Khwa¯rizm¯ı is perhaps best knownasthewriterofthetreatiseAl-Kita¯bal-mukhtas.arf¯ıh¯ısa¯bal-g˘abrwa’l-muqa¯bala2,fromwhichthe modernwordalgebraderives. Inanothertreatise,al-Khwa¯rizm¯ıpopularizedthemoderndecimalsystem forwritingandmanipulatingnumbers—inparticular,theuseofasmallcircleor.sifrtorepresentamissing quantity—which had originated in India several centuries earlier. This system later became known in Europe as algorism. Thanks to the efforts of the medieval Italian mathematician Leonardo of Pisa, betterknownasFibonacci,algorismbegantoreplacetheabacusasthepreferredsystemofcommercial calculation3 inEuropeinthelate12thcentury,althoughcyphersbecametrulyubiquitousinWestern Europe only after the French revolution 600 years later. The more modern word algorithm is a false 1‘Mohammad,fatherofAdbdulla,sonofMoses,theKwa¯rizmian’. Kwa¯rizmisanancientcity,nowcalledKhiva,inthe KhorezmProvinceofUzbekistan. 2‘TheCompendiousBookonCalculationbyCompletionandBalancing’. 3fromtheLatinwordcalculus,meaningliterally‘smallrock’,referringtothestonesonacountingboard,orabacus ©Copyright2010JeffErickson. ReleasedunderaCreativeCommonsAttribution-NonCommercial-ShareAlike3.0License(http://creativecommons.org/licenses/by-nc-sa/3.0/). Freedistributionisstronglyencouraged;commercialdistributionisexpresslyforbidden. Seehttp://www.cs.uiuc.edu/~jeffe/teaching/algorithms/forthemostrecentrevision. 1 Algorithms Lecture 0: Introduction [F10] cognatewiththeGreekwordarithmos(ἀριθμός),meaning‘number’(andperhapstheaforementioned άλγος). Thus,untilveryrecently,thewordalgorithmreferredexclusivelytopencil-and-papermethods fornumericalcalculations. Peopletrainedinthereliableexecutionofthesemethodswerecalled—you guessedit—computers. 0.2 A Few Simple Examples Multiplication by compass and straightedge Althoughtheyhaveonlybeenanobjectofformalstudyforafewdecades,algorithmshavebeenwithus sincethedawnofcivilization,forcenturiesbeforeAl-Khwa¯rizm¯ıandFibonaccipopularizedthecypher. Here is an algorithm, popularized (but almost certainly not discovered) by Euclid about 2500 years ago,formultiplyingordividingnumbersusingarulerandcompass. TheGreekgeometersrepresented numbersusinglinesegmentsoftheappropriatelength. Inthepseudo-codebelow,CIRCLE(p,q)represents thecirclecenteredatapoint p andpassingthroughanotherpointq. Hopefullytheotherinstructions areobvious.4 Z 〈〈Constructthelineperpendicularto(cid:96)andpassingthrough P.〉〉 RIGHTANGLE((cid:96),P): ChooseapointA∈(cid:96) A,B←INTERSECT(CIRCLE(P,A),(cid:96)) C,D←INTERSECT(CIRCLE(A,B),CIRCLE(B,A)) returnLINE(C,D) 〈〈Constructapoint Z suchthat|AZ|=|AC||AD|/|AB|.〉〉 C MULTIPLYORDIVIDE(A,B,C,D): α←RIGHTANGLE(LINE(A,C),A) D E←INTERSECT(CIRCLE(A,B),α) F ←INTERSECT(CIRCLE(A,D),α) β ←RIGHTANGLE(LINE(E,C),F) B γ←RIGHTANGLE(β,F) returnINTERSECT(γ,LINE(A,C)) b(cid:13) F a(cid:13) A E g(cid:13) Multiplyingordividingusingacompassandstraightedge. This algorithm breaks down the difficult task of multiplication into a series of simple primitive operations: drawingalinebetweentwopoints,drawingacirclewithagivencenterandboundarypoint, and so on. These primitive steps are quite non-trivial to execute on a modern digital computer, but thisalgorithmwasn’tdesignedforadigitalcomputer;itwasdesignedforthePlatonicIdealClassical Greek Mathematician, wielding the Platonic Ideal Compass and the Platonic Ideal Straightedge. In thisexample,Euclidfirstdefinesanewprimitiveoperation,constructingarightangle,by(asmodern programmerswouldputit)writingasubroutine. 4Euclidandhisstudentsalmostcertainlydrewtheirconstructionsonanἄβαξ,atablecoveredindustorsand(orperhaps verysmallrocks).Overthenextseveralcenturies,theGreekabaxevolvedintothemedievalEuropeanabacus. 2 Algorithms Lecture 0: Introduction [F10] Multiplication by duplation and mediation Here is an even older algorithm for multiplying large numbers, sometimes called (Russian) peasant multiplication. AvariantofthismethodwascopiedintotheRhindpapyrusbytheEgyptianscribeAhmes around 1650 BC, from a document he claimed was (then) about 350 years old. This was the most commonmethodofcalculationbyEuropeansbeforeFibonacci’sintroductionofArabicnumerals;itwas stilltaughtinelementaryschoolsinEasternEuropeinthelate20thcentury. Thisalgorithmwasalso commonly used by early digital computers that did not implement integer multiplication directly in hardware. x y prod PEASANTMULTIPLY(x,y): 0 prod←0 123 +456 =456 while x >0 61 +912 =1368 if x isodd 30 1824 prod←prod+y 15 +3648 =5016 x ←(cid:98)x/2(cid:99) 7 +7296 =12312 y ← y+y 3 +14592 =26904 return p 1 +29184 =56088 The peasant multiplication algorithm breaks the difficult task of general multiplication into four simpler operations: (1) determining parity (even or odd), (2) addition, (3) duplation (doubling a number),and(4)mediation(halvinganumber,roundingdown).5 Ofcourseafullspecificationofthis algorithmrequiresdescribinghowtoperformthosefour‘primitive’operations. Peasantmultiplication requires(aconstantfactor!)morepaperworktoexecutebyhand,butthenecessaryoperationsareeasier (forhumans)torememberthanthe10×10multiplicationtablerequiredbytheAmericangradeschool algorithm.6 Thecorrectnessofpeasantmultiplicationfollowsfromthefollowingrecursiveidentity,whichholds foranynon-negativeintegers x and y: 0 if x =0 x· y = (cid:98)x/2(cid:99)·(y+ y) if x iseven (cid:98)x/2(cid:99)·(y+ y)+ y if x isodd Congressional Apportionment Hereisanothergoodexampleofanalgorithmthatcomesfromoutsidetheworldofcomputing. ArticleI, Section2oftheUnitedStatesConstitutionrequiresthat RepresentativesanddirectTaxesshallbeapportionedamongtheseveralStateswhichmaybeincluded withinthisUnion,accordingtotheirrespectiveNumbers.... TheNumberofRepresentativesshall notexceedoneforeverythirtyThousand,buteachStateshallhaveatLeastoneRepresentative.... SincetherearealimitednumberofseatsavailableintheHouseofRepresentatives,exactproportional representation is impossible without either shared or fractional representatives, neither of which are 5TheversionofthisalgorithmactuallyusedinancientEgyptdoesnotusemediationorparity,butitdoesusecomparisons. To avoid halving, the algorithm pre-computes two tables by repeated doubling: one containing all the powers of 2 not exceeding x,theothercontainingthesamepowersof2multipliedby y. Thepowersof2thatsumto x arethenfoundby greedysubtraction,andthecorrespondingentriesintheothertableareaddedtogethertoformtheproduct. 6AmericanschoolkidslearnavariantofthelatticemultiplicationalgorithmdevelopedbyIndianmathematiciansand describedbyFibonacciinLiberAbaci.Thetwoalgorithmsareequivalentiftheinputnumbersarerepresentedinbinary. 3 Algorithms Lecture 0: Introduction [F10] legal. Asaresult,severaldifferentapportionmentalgorithmshavebeenproposedandusedtoround thefractionalsolution fairly. The algorithmactuallyusedtoday, calledtheHuntington-Hillmethodor themethodofequalproportions,wasfirstsuggestedbyCensusBureaustatisticianJosephHillin1911, refinedbyHarvardmathematicianEdwardHuntingtonin1920,adoptedintoFederallaw(2U.S.C.§§2a and2b)in1941,andsurvivedaSupremeCourtchallengein1992.7 TheinputarrayPop[1..n]stores the populations of the n states, and R is the total number of representatives. Currently, n = 50 and R=435. TheoutputarrayRep[1..n]storesthenumberofrepresentativesassignedtoeachstate. APPORTIONCONGRESS(Pop[1..n],R): PQ←NEWPRIORITYQUEUE fori←1ton Rep[i]←1 (cid:112) INSERT(cid:128)PQ,i,Pop[i]/ 2(cid:138) R←R−1 whileR>0 s←EXTRACTMAX(PQ) Rep[s]←Rep[s]+1 (cid:16) (cid:46)(cid:112) (cid:17) INSERT PQ,s,Pop[s] Rep[s](Rep[s]+1) R←R−1 returnRep[1..n] Thispseudocodedescriptionassumesthatyouknowhowtoimplementapriorityqueuethatsupports theoperationsNEWPRIORITYQUEUE,INSERT,andEXTRACTMAX. (Theactuallawdoesn’tassumethat,of course.) The output of the algorithm, and therefore its correctness, does not depend at all on how the priority queue is implemented. The Census Bureau uses an unsorted array, stored in a column of an Excel spreadsheet; you should have learned a more efficient solution in your undergraduate data structuresclass. A bad example Consider“Martin’salgorithm”:8 BECOMEAMILLIONAIREANDNEVERPAYTAXES: Getamilliondollars. Don’tpaytaxes. Ifyougetcaught, Say“Iforgot.” Prettysimple,exceptforthatfirststep;it’sadoozy. AgroupofbillionaireCEOsmightconsiderthis analgorithm,sinceforthemthefirststepisbothunambiguousandtrivial,butfortherestofuspoor slobs,Martin’sprocedureistoovaguetobeconsideredanactualalgorithm. Ontheotherhand,thisisa perfectexampleofareduction—itreducestheproblemofbeingamillionaireandneverpayingtaxesto the‘easier’problemofacquiringamilliondollars. We’llseereductionsoverandoveragaininthisclass. 7Overrulinganearlierrulingbyafederaldistrictcourt,theSupremeCourtunanimouslyheldthatanyapportionment method adopted in good faith by Congress is constitutional (United States Department of Commerce v. Montana). The current congressional apportionment algorithm is described in gruesome detail at the U.S. Census Department web site http://www.census.gov/population/www/censusdata/apportionment/computing.html. Agoodhistoryoftheapportionment problemcanbefoundathttp://www.thirty-thousand.org/pages/Apportionment.htm. AreportbytheCongressionalResearch Servicedescribingvariousapportionmentmethodsisavailableathttp://www.rules.house.gov/archives/RL31074.pdf. 8S.Martin,“YouCanBeAMillionaire”,SaturdayNightLive,January21,1978. AppearsonComedyIsNotPretty,Warner Bros.Records,1979. 4
Description: