Tải bản đầy đủ (.pdf) (1,064 trang)

Algorithms and data structures the science of computing do

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.41 MB, 1,064 trang )

Algorithmsand
DataStructures:
TheScienceof
Computing
byDouglasBaldwin
andGregW.Scragg
CharlesRiverMedia©
2004(640pages)
ISBN:1584502509

Byfocusingonthe
architectureof
algorithms,
mathematical
modelingand
analysis,and
experimental
confirmationof
theoreticalresults,
thisbookhelps
studentsseecomputer
scienceisabout
problemsolving,not
simplymemorizing
andreciting
languages.
TableofContents
AlgorithmsandDataStructures—TheScienceof
Computing
Preface



PartI-TheScienceofComputing'sThreeMethodsof
Inquiry

Chapter1
Chapter2
Chapter3
Chapter4

- WhatistheScienceofComputing?
- Abstraction:AnIntroductiontoDesign
- Proof:AnIntroductiontoTheory
- Experimentation:AnIntroductiontothe
ScientificMethod

PartII-ProgramDesign

Chapter5 - Conditionals
Chapter6 - DesigningwithRecursion
Chapter7 - AnalysisofRecursion
Chapter8 - CreatingCorrectIterativeAlgorithms
Chapter9 - IterationandEfficiency
Chapter10 - ACaseStudyinDesignandAnalysis:
EfficientSorting
PartIII-IntroductiontoDataStructures

Chapter11 - Lists
Chapter12 - QueuesandStacks
Chapter13 - BinaryTrees
Chapter14 - CaseStudiesinDesign:Abstracting

Indirection
PartIV-TheLimitsofComputerScience

Chapter15 - ExponentialGrowth
Chapter16 - LimitstoPerformance
Chapter17 - TheHaltingProblem
AppendixA - Object-orientedProgramminginJava
AppendixB - AbouttheWebSite
Index
ListofFigures
ListofTables


ListofListings,TheoremsandLemmas
ListofSidebars


AlgorithmsandDataStructures:The
ScienceofComputing
byDouglasBaldwinandGregW.Scragg
CharlesRiverMedia©2004(640pages)
ISBN:1584502509

Byfocusingonthearchitectureofalgorithms,
mathematicalmodelingandanalysis,and
experimentalconfirmationoftheoretical
results,thisbookhelpsstudentsseecomputer
scienceisaboutproblemsolving,notsimply
memorizingandrecitinglanguages.


BackCover
Whilemanycomputersciencetextbooksareconfined
toteachingprogrammingcodeandlanguages,
AlgorithmsandDataStructures:TheScienceof
Computingtakesastepbacktointroduceandexplore
algorithms--thecontentofthecode.Focusingon
threecoretopics:design(thearchitectureof
algorithms),theory(mathematicalmodelingand
analysis),andthescientificmethod(experimental
confirmationoftheoreticalresults),thebookhelps
studentsseethatcomputerscienceisaboutproblem
solving,notsimplythememorizationandrecitationof
languages.Unlikemanyothertexts,themethodsof
inquiryareexplainedinanintegratedmannerso
studentscanseeexplicitlyhowtheyinteract.Recursion
andobjectorientedprogrammingareemphasizedas
themaincontrolstructureandabstractionmechanism,


respectively,inalgorithmdesign.
Features:
Reflectstheprinciplethatcomputerscienceisnot
solelyaboutlearninghowtospeakina
programminglanguages
Coversrecursion,binarytrees,stacks,queues,
hashtables,andobject-orientedalgorithms
WrittenespeciallyforCS2students
AbouttheAuthors
DouglasBaldwinisanAssociateProfessorofComputer
ScienceatSUNYGeneseo.AgraduateofYale

University,hehastaughtcoursesfromCS1to
CompilerConstruction,andfromNetworkingtoTheory
ofProgrammingLanguages.Hehasauthoredmany
journalarticlesandconferencepaperswithinthefield.
GregW.ScraggisProfessorEmeritusfromSUNY
Geneseowithoverthirtyyearsexperienceincomputer
science.SincehisgraduationfromtheUniversityof
California,hehasreceivedseveralgrantsrelatedto
computerscienceeducationandhaswrittenover60
articlesforcomputersciencejournals.



AlgorithmsandDataStructures—TheScienceof
Computing
DouglasBaldwin
GregW.Scragg

CHARLESRIVERMEDIA,INC.
Hingham,Massachusetts
Copyright2004byCHARLESRIVERMEDIA,INC.
Allrightsreserved.
Nopartofthispublicationmaybereproducedinanyway,storedina
retrievalsystemofanytype,ortransmittedbyanymeansormedia,
electronicormechanical,including,butnotlimitedto,photocopy,
recording,orscanning,withoutpriorpermissioninwritingfromthe
publisher.
Publisher:DavidPallai
Production:EricLengyel
CoverDesign:ThePrintedImage

CHARLESRIVERMEDIA,INC.
10DownerAvenue
Hingham,Massachusetts02043
781-740-0400
781-740-8816(FAX)

www.charlesriver.com


Thisbookisprintedonacid-freepaper.
DouglasBaldwinandGregScragg.AlgorithmsandDataStructures:The
ScienceofComputing.
ISBN:1-58450-250-9
Allbrandnamesandproductnamesmentionedinthisbookare
trademarksorservicemarksoftheirrespectivecompanies.Anyomission
ormisuse(ofanykind)ofservicemarksortrademarksshouldnotbe
regardedasintenttoinfringeonthepropertyofothers.Thepublisher
recognizesandrespectsallmarksusedbycompanies,manufacturers,
anddevelopersasameanstodistinguishtheirproducts.
LibraryofCongressCataloging-in-PublicationData

Baldwin,Douglas(DouglasL.),1958Algorithmsanddatastructures:thescienceofcomputing/DouglasBaldwinandGre
p.cm.
Includesbibliographicalreferencesandindex.
ISBN1-58450-250-9
1.Computeralgorithms.2.Datastructures(Computerscience)I.Scragg,
GregW.II.Title.
QA76.9.A43B352004
005.1—dc22
2004008100

PrintedintheUnitedStatesofAmerica
04765432FirstEdition
CHARLESRIVERMEDIAtitlesareavailableforsitelicenseorbulk
purchasebyinstitutions,usergroups,corporations,etc.Foradditional
information,pleasecontacttheSpecialSalesDepartmentat781-7400400.


ACKNOWLEDGMENTS
TheScienceofComputingrepresentstheculminationofaprojectthat
hasbeenindevelopmentforaverylongtime.Inthecourseofthe
project,agreatmanypeopleandorganizationshavecontributedinmany
ways.Whileitisimpossibletolistthemall,wedowishtomentionsome
whosecontributionshavebeenespeciallyimportant.Theresearchinto
themethodologywassupportedbyboththeNationalScienceFoundation
andtheU.S.DepartmentofEducation,andwearegratefulfortheir
support.Duringthefirstseveralyearsoftheproject,HansKoomenwasa
co-investigatorwhoplayedacentralroleinthedevelopmentalwork.We
receivedvaluablefeedbackintheformofreviewsfrommanyincluding
JohnHamer,PeterHenderson,LewHitchner,KrisPowers,OritHazzan,
MarkLeBlanc,AllenTucker,TonyRalston,DanielHyde,StuartHirshfield,
TimGegg-Harrison,NicholasHowe,CatherineMcGeoch,andKen
Slonneger.G.MichaelSchneiderandJimLeisywerealsoparticularly
encouragingofourefforts.HommaFarian,InduTalwar,andNancyJones
alluseddraftsofthetextintheircourses,helpingwiththatcrucialfirst
exposure.WeheldaseriesofworkshopsatSUNYGeneseoatwhich
someoftheideaswerefleshedout.Facultyfromotherinstitutionswho
attendedandcontributedtheirideasincludeElizabethAdams,HansPeterAppelt,LoisBrady,MarcusBrown,JohnCross,NiraHerrmann,
MargaretIwobi,MargaretReek,EthelSchuster,JamesSlack,and
FengmanZhang.Almost1500studentsservedasthefrontlinesoldiers
—theoneswhocontributedastheguineapigsofourefforts—butwe

especiallywishtothankSuzanneSelib,JimDurbin,BruceCowley,Ernie
Johnson,CoralieAshworth,KevinKosieracki,GregArnold,Steve
Batovsky,WendyAbbott,LisaCiferri,NandiniMehta,SteveBender,Mary
Johansen,PeterDenecke,JasonKapusta,MichaelStringer,Jesse
Smith,GarrettBriggs,ElenaKornienko,andGenevieveHerres,allof
whomworkeddirectlywithusonstagesoftheproject.Finally,wecould
nothavecompletedthisprojectwithoutthestaffofCharlesRiverMedia,
especiallyStephenMossberg,DavidPallai,andBryanDavidson.





Preface
AlgorithmsandDataStructures:TheScienceofComputing(whichwe
usuallyrefertosimplyasTheScienceofComputing)isabout
understandingcomputation.Weseeitasadistinctdeparturefrom
previoussecond-coursecomputersciencetexts,whichemphasize
buildingcomputations.TheScienceofComputingdevelops
understandingbycouplingalgorithmdesigntomathematicaland
experimentaltechniquesformodelingandobservingalgorithms'
behavior.Itsattentiontorigorousscientificexperimentationparticularly
distinguishesitfromothercomputingtexts.TheScienceofComputing
introducesstudentstocomputerscience'sthreecoremethodsofinquiry:
design,mathematicaltheory,andthescientificmethod.Itintroduces
thesemethodsearlyinthecurriculum,sothatstudentscanusethem
throughouttheirstudies.Thebookusesastronglyhands-onapproachto
demonstratetheimportanceof,andinteractionsbetween,allthree
methods.



THETARGETAUDIENCE
ThetargetcourseforTheScienceofComputingisthesecondcoursein
acomputersciencecurriculum(CS2).Forbetterorworse,thatcourse
hasbecomemorevariedinrecentyears.TheScienceofComputingis
appropriateformany—butnotall—implementationsofCS2.

TheTargetStudent
TheScienceofComputingisaimedatstudentswhoaremajoringin,or
independentlystudying,computerscience.Itisalsosuitableforstudents
whowanttocombineafirmbackgroundincomputersciencewith
anothermajor.
Theprogramminglanguageforexamplesandexercisesinthisbookis
Java.Weassumethatstudentshavehadanintroductoryprogramming
courseusinganobject-orientedlanguage,althoughnotnecessarilyJava.
Thebookshouldalsobeaccessiblewithjustalittleextraworktothose
whostartedwithaprocedurallanguage.Anappendixhelpsstudents
whosepreviousexperienceiswithalanguageotherthanJavamakethe
transitiontoJava.
ThereisquiteabitofmathinTheScienceofComputing.Weteachallof
theessentialmathematicswithinthetext,assumingonlythatreaders
haveagoodprecollegemathbackground.However,readerswhohave
completedoneormorecollege-levelmathcourses,particularlyin
discretemath,willinevitablyhaveaneasiertimewiththemathinthis
bookthanreaderswithoutsuchabackground.

TheTargetSchoolandDepartment
EverycomputersciencedepartmenthasaCS2course,andmostcould
useTheScienceofComputing.However,thisbookismostsuitedto
thosedepartmentsthat:

Wanttogivestudentsanearlyandfirmfoundationinallthe


methodsofinquirythattheywillneedinlaterstudies,or
Wanttoincreasetheiremphasisonthenonprogramming
aspectsofcomputerscience,or
Wanttocloselyaligntheirprogramswithothermathand/or
scienceprograms.





WHYTHESCIENCEOFCOMPUTING?
Webelievethatanintroductiontocomputerscienceshouldbeanindepthstudyofthebasicfoundationsofthefield.Theappropriate
foundationslienotinwhatcomputersciencestudies,butinhowit
studies.

ThreeMethodsofInquiry
TheScienceofComputingisbasedonthreemethodsofinquirycentral
tocomputerscience(essentially,thethree"paradigms"ofcomputer
sciencedescribedbyDenningetal.in"ComputingasaDiscipline,"
CommunicationsoftheACM,January1989).Inparticular,thebook's
missionistoteach:
Design-thecreationofalgorithms,programs,architectures,etc.
TheScienceofComputingemphasizes:
Abstractionasawayoftreatingcomplexoperationsas
"primitives,"sothatonecanwritealgorithmsintermsappropriate
totheproblemtheysolve.
Recursionasatoolforcontrollingalgorithmsanddefining

problems.
Theory-themathematicalmodelingandanalysisofalgorithms,
programs,problems,etc.
TheScienceofComputingemphasizes:
Theuseofmathematicstopredicttheexecutiontimeof
algorithms.
Theuseofmathematicstoverifythecorrectnessofalgorithms.
EmpiricalAnalysis-theuseofthescientificmethodtostudy
algorithms,programs,etc.


TheScienceofComputingemphasizes:
Therigorousnotionof"experiment"usedinthesciences
Techniquesforcollectingandanalyzingdataontheexecution
timeofprogramsorpartsofprograms.
Advancesincomputersciencedependonallthreeofthesemethodsof
inquiry;therefore,awell-educatedcomputerscientistmustbecome
familiarwitheach—startingearlyinhiseducation.





WHATTHESCIENCEOFCOMPUTINGISNOT
TheScienceofComputingisnotrightforeveryCS2course.In
particular,TheScienceofComputingisnot...
PureTraditional
TheScienceofComputingisnota"standard"CS2withextramaterial.
Tofitasoundintroductiontomethodsofinquiryintoasinglecourse,we
necessarilyreducesomematerialthatistraditionalinCS2.Forinstance,

westudybinarytreesasexamplesofrecursivedefinition,the
constructionofrecursivealgorithms(e.g.,search,insertion,deletion,and
traversal),mathematicalanalysisofdatastructuresandtheiralgorithms,
andexperimentsthatdrivehomethemeaningofmathematicalresults(e.
g.,hownearlyindistinguishable"logarithmic"timeisfrom
"instantaneous");however,wedonottrytocovermultiwaytrees,AVL
trees,Btrees,redblacktrees,andothervariationsontreesthatappearin
manyCS2texts.
TheScienceofComputing'semphasisonmethodsofinquiryratherthan
programmingdoeshaveimplicationsforsubsequentcourses.Students
mayenterthosecourseswithaslightlynarrowerexposuretodata
structuresthanistraditional,andprogramsthatwantCS2toprovidea
foundationinsoftwareengineeringforlatercourseswillfindthatthereis
lessroomtodosoinTheScienceofComputingthaninamore
traditionalCS2.However,theseeffectswillbeoffsetbystudentsleaving
TheScienceofComputingwithstrongerthanusualabilitiesin
mathematicalandexperimentalanalysisofalgorithms.Thismeansthat
intermediatecoursescanquicklyfillinmaterialnotcoveredbyThe
ScienceofComputing.Forexample,intermediateanalysisofalgorithms
coursesshouldbeabletomovemuchfasterafterTheScienceof
ComputingthantheycanafteratraditionalCS2.Bottomline:ifrigid
adherencetoatraditionalmodelisessential,thenthismaynotbethe
righttextforyou.
SoftwareEngineering
SomenewversionsofCS2movethefocusfromdatastructuresto


softwareengineering.Thisalsoisdistinctfromtheapproachhere.Welay
asolidfoundationforlaterstudyofsoftwareengineering,butsoftware
engineeringperseisnotamajorfactorinthisbook.

DataStructures
InspiteofthecoverageinPartIII,TheScienceofComputingisnota
datastructuresbook.Atraditionaldatastructurescoursecouldeasilyuse
TheScienceofComputing,butyouwouldprobablywanttoaddamore
traditionaldatastructurestextorreferencebookasasupplementaltext.
InsteadofanyoftheseotherapproachestoCS2,theaimofThe
ScienceofComputingistopresentamorebalancedtreatmentofdesign,
mathematicalanalysis,andexperimentation,thusmakingitclearto
studentsthatallthreetrulyarefundamentalmethodsforcomputer
scientists.





THESCIENCEOFCOMPUTINGAND
COMPUTINGCURRICULA2001
Ourcentralphilosophyisthatthefoundationsofcomputerscience
extendbeyondprogramstoalgorithmsasabstractionsthatcanand
shouldbethoughtfullydesigned,mathematicallymodeled,and
experimentallyanalyzed.Whileprogrammingisessentialtoputting
algorithmsintoconcreteformforapplieduse,algorithmdesignis
essentialifthereistobeanythingtoprograminthefirstplace,
mathematicalanalysisisessentialtounderstandingwhichalgorithms
leadtocorrectandefficientprograms,andexperimentsareessentialfor
confirmingthepracticalrelevanceoftheoreticalanalyses.Althoughthis
philosophyappearstodifferfromtraditionalapproachestointroductory
computerscience,itisconsistentwiththedirectionsinwhichcomputer
sciencecurriculaareevolving.TheScienceofComputingmatches
nationalandinternationaltrendswell,andisappropriateformostCS2

courses.
OurcentralthemesaligncloselywithmanyofthegoalsintheACM/IEEE
ComputingCurricula2001report,forinstance:[1]
Anintroductorysequencethatexposesstudentstothe
"conceptualfoundations"ofcomputerscience,includingthe
"modesofthoughtandmentaldisciplines"computerscientists
usetosolveproblems.
Introducingdiscretemathearly,andapplyingitthroughoutthe
curriculum.
Anintroductorysequencethatincludesreasoningaboutand
experimentallymeasuringalgorithms'useoftimeandother
resources.
Acurriculuminwhichstudents"havedirecthands-onexperience
withhypothesisformulation,experimentaldesign,hypothesis
testing,anddataanalysis."
Anearlyintroductiontorecursion.


Anintroductorysequencethatincludesabstractionand
encapsulationastoolsfordesigningandunderstanding
programs.
ComputingCurricula2001stronglyrecommendsathree-semester
introductorysequence,andoutlinesseveralpossibleimplementations.
TheScienceofComputingprovidesanappropriateapproachtothe
secondorthirdcoursesinmostoftheseimplementations.

EffectiveThinking
Mostcomputersciencedepartmentsseetheirprimarymissionas
developingstudents'abilitytothinkeffectivelyaboutcomputation.
BecauseTheScienceofComputingisfirstandforemostabouteffective

thinkingincomputerscience,itisanidealCS2bookforsuchschools,
whetherwithinaCC2001-compatiblecurriculumornot.
[1]QuotationsinthislistarefromChapters7and9oftheComputing
Curricula2001ComputerSciencevolume.





DISTINCTIVEFEATURESOFTHISBOOK
Thisbookhasanumberofotherfeaturesthatthestudentandinstructor
shouldconsider.

Abstractvs.Concrete
Abstractionasaproblem-solvinganddesigntechniqueisanimportant
conceptinTheScienceofComputing.Object-orientedprogrammingisa
nearlyidealforminwhichtodiscusssuchabstraction.Earlyinthebook,
studentsuseobject-orientedabstractionbydesigningandanalyzing
algorithmswhoseprimitivesarereallymessagestoobjects.This
abstractionenablesshortalgorithmsthatembodyoneimportantidea
apiecetononethelesssolveinterestingproblems.Classlibrarieslet
studentscodethealgorithmsinworkingprograms,demonstratingthat
theobjectsare"real"evenifstudentsdon'tknowhowtheyare
implemented.Forinstance,manyoftheearlyexamplesofalgorithmsuse
messagestoahypotheticalrobottoperformcertaintasks;studentscan
codeandrunthesealgorithms"forreal"usingasoftwarelibrarythat
providesananimatedsimulationoftherobot.Later,studentslearnto
createtheirownobject-orientedabstractionsastheydesignnewclasses
whosemethodsencapsulatevariousalgorithms.


AlgorithmsandPrograms
Themethodsofinquiry,andthealgorithmsanddatastructurestowhich
weapplythem,arefundamentaltocomputing,regardlessofone's
programminglanguage.However,studentsmustultimatelyapply
fundamentalideasintheformofconcreteprograms.TheScienceof
Computingbalancesthesecompetingrequirementsbydevotingmostof
thetexttoalgorithmsasthingsthataremorethanjustprograms.For
example,wedon'tjustpresentanalgorithmasapieceofcode;we
explainthethinkingthatleadstothatcodeandillustratehow
mathematicalanalysesfocusattentiononpropertiesthatcanbe
observednomatterhowonecodesanalgorithm,abstractingaway
language-specificdetails.Ontheotherhand,theconcreteexamplesin
TheScienceofComputingarewritteninarealprogramminglanguage


(Java).Exercisesandprojectsrequirethatstudentsfollowthealgorithm
throughtothecodedlanguage.Thepresentationhelpsseparate
fundamentalmethodsfromlanguagedetails,helpingstudents
understandthatthefundamentalsarealwaysrelevant,andindependent
oflanguage.Studentsrealizethatthereismuchtolearnaboutthe
fundamentalsthemselves,apartfromsimplyhowtowritesomethingina
particularlanguage.

EarlyCompetence
Design,theory,andempiricalanalysisallrequirelongpracticetomaster.
Wefeelthatstudentsshouldbeginusingeachearlyintheirstudies,and
shouldcontinueusingeachthroughoutthosestudies.TheScienceof
Computinggivesstudentsrudimentarybutrealabilitytouseallthree
methodsofinquiryearlyinthecurriculum.Thiscontrastssharplywith
sometraditionalcurricula,inwhichtheoreticalanalysisisdeferreduntil

intermediateorevenadvancedcourses,andexperimentationmaynever
beexplicitlyaddressedatall.

Integration
Design,theory,andempiricalanalysisarenotindependentmethods,but
rathermutuallysupportingideas.Studentsshouldthereforelearnabout
theminanintegratedmanner,seeingexplicitlyhowthemethodsinteract.
Thisapproachhelpsstudentsunderstandhowallthreemethodsare
relevanttotheirparticularinterestsincomputerscience.Unfortunately,
thetraditionalintroductorysequenceartificiallycompartmentalizes
methodsbyplacingtheminseparatecourses(e.g.,programdesignin
CS1and2,butcorrectnessandperformanceanalysisinananalysisof
algorithmscourse).

ActiveLearning
Webelievethatstudentsshouldactivelyengagecomputersasthey
learn.Readingisonlyapreludetopersonallysolvingproblems,writing
programs,derivingandsolvingequations,conductingexperiments,etc.
Activeengagementisparticularlyvaluableinmakingacoursesuchas


×