Introduction to Computer Science CSCI 109 Andrew Goodney Fall 2017 China – Tianhe-2 Readings St. Amant, Ch. 4, Ch. 8 Lecture 5: Data Structures & Algorithms 9/25, 2017 “An algorithm (pronounced AL-go-rith- um) is a procedure or formula for solving a problem. The word derives from the name of the mathematician, Mohammed ibn-Musa al-Khwarizmi, who was part of the royal court in Baghdad and who lived from about 780 to 850.”
53
Embed
Introduction to Computer Sciencebytes.usc.edu/files/cs109/fa17/slides/CSCI109... · uCharacterize functions according to how fast they grow uThe growth rate of a function is called
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Introduction to Computer ScienceCSCI109
AndrewGoodneyFall2017
China– Tianhe-2
ReadingsSt.Amant,Ch.4,Ch.8
Lecture5:DataStructures&Algorithms9/25,2017
“An algorithm (pronounced AL-go-rith-um) is a procedure or formula forsolving a problem. The word derivesfrom the name of the mathematician,Mohammed ibn-Musa al-Khwarizmi,who was part of the royal court inBaghdad and who lived from about780 to 850.”
Reminders
u Quiz2today(coverslecturematerialfrom1/30and2/6)u Nolecturenextweek(Feb20)duetoPresidents’day
u Quiz3onFeb27(coverslecturematerialfromtoday(2/13))u HW2dueon2/27
u SoatreeisEitherasinglevertex,ora vertexthatistheparent
ofoneormoretrees
6
Eric
Emily Jane
Terry Bob
Drew Pam
Kim
Recursion: algorithms
u Definingalgorithmsintermsofthemselves(e.g.,quicksort)Checkwhetherthesequencehasjustoneelement.Ifitdoes,stopCheckwhetherthesequencehastwoelements.Ifitdoes,andtheyareintherightorder,stop.Iftheyareinthewrongorder,swapthem,stop.Chooseapivotelementandrearrangethesequencetoputlower-valuedelementsononesideofthepivot,higher-valuedelementsontheothersideQuicksortthelowerelementsQuicksortthehigherelements
u Initializeatreewithasinglevertex,chosenarbitrarilyfromthegraph
u Growthetreebyaddingonevertex.Dothisbyaddingtheminimum-weightedgechosenfromtheedgesthatconnectthetreetoverticesnotyetinthetree
u Repeatuntilallverticesareinthetree
u Howfastitgoesdependsonhowyoustoretheverticesofthegraph
u Ifyoudon’tkeeptheverticesofthegraphinsomereadilysortedorderthenthecomplexityisO(V2)wherethegraphhasVvertices
22
Kruskal’s algorithm
u Initializeatreewithasingleedgeoflowestweightu Addedgesinincreasingorderofweight
u Ifanedgecausesacycle,skipitandmoveontothenexthighestweightedge
u Repeatuntilalledgeshavebeenconsideredu Evenwithoutmuchthoughtonhowtheedgesarestored(aslongaswesortthemonceinthebeginning),thecomplexityisO(ElogE)wherethegraphhasEedges
23
Dijkstra’s algorithm
u Ateachiterationwerefinethedistanceestimatethroughanewvertexwe’recurrentlyconsidering
u (Binary)searchofasortedlist:O(log2n)u Selectionsort:O(n2)
u Quicksort:O(nlogn)u Breadthfirsttraversalofatree:O(V)
u Depthfirsttraversalofatree:O(V)
u Prim’salgorithmtofindtheMSTofagraph:O(V2)u Kruskal’s algorithmtofindtheMSTofagraph:O(ElogE)
u Dijkstra’s algorithmtofindtheshortestpathfromanodeinagraphtoallothernodes:O(V2)
36
Subset sum problem
u Givenasetofintegersandanintegers,doesanynon-emptysubsetsumtos?
u {1,4,67,-1,42,5,17}ands=24 Nou {4,3,17,12,10,20}ands =19 Yes {4,3,12}
u IfasethasN elements,ithas2N subsets.u CheckingthesumofeachsubsettakesamaximumofNoperations
u Tocheckallthesubsetstakes2NNoperationsu Someclevernesscanreducethisbyabit(2Nbecomes 2N/2,butallknownalgorithmsareexponential– i.e.O(2NN)
37
Travelling salesperson problem
u Givenalistofcitiesandthedistancesbetweeneachpairofcities,whatistheshortestpossibleroutethatvisitseachcityexactlyonceandreturnstotheorigincity?
u Givenagraphwhereedgesarelabeledwithdistancesbetweenvertices.Startataspecifiedvertex,visitallotherverticesexactlyonceandreturntothestartvertexinsuchawaythatsumoftheedgeweightsisminimized
u Therearen! routes(anumberontheorderofnn - muchbiggerthan2n)
u O(n!)
38
Enumerating permutations
u Listallpermutations(i.e.allpossibleorderings)ofnnumbers
uWhatistheorderofanalgorithmthatcandothis?
39
Enumerating permutations
u Listallpermutations(i.e.allpossibleorderings)ofnnumbers
uWhatistheorderofanalgorithmthatcandothis?uO(n!)
40
Analysis of problems
u Studyofalgorithmsilluminatesthestudyofclasses ofproblems
u Ifapolynomialtimealgorithmexiststosolveaproblemthentheproblemiscalledtractable
u Ifaproblemcannotbesolvedbyapolynomialtimealgorithmthenitiscalledintractable
u Thisdividesproblemsinto#?ree groups:known polynomialtimealgorithmbutnotyetproventobeintractable
41
Analysis of problems
u Studyofalgorithmsilluminatesthestudyofclasses ofproblems
u Ifapolynomialtimealgorithmexiststosolveaproblemthentheproblemiscalledtractable
u Ifaproblemcannotbesolvedbyapolynomialtimealgorithmthenitiscalledintractable
u Thisdividesproblemsintothreegroups:v Problemswithknownpolynomialtimealgorithmsv Problemsthatareproventohavenopolynomial-timealgorithmv Problemswithnoknownpolynomialtimealgorithmbutnotyetproventobeintractable
42
Tractable and Intractable
u Tractableproblems(P)v Sortingalistv Searchinganunorderedlistv Findingaminimumspanningtree
inagraph
43
u Intractablev Listingallpermutations(all
possibleorderings)ofnnumbers
u Mightbe(in)tractablev Subsetsum:givenasetof
numbers,isthereasubsetthataddsuptoagivennumber?
v Travellingsalesperson:ncities,n!routes,findtheshortestroute
u ‘Propertiesofproblems’(NOT ‘propertiesofalgorithms’)u Tractable:problemcanbesolvedbyapolynomialtimealgorithm(orsomethingmoreefficient)
u Intractable:problemcannotbesolvedbyapolynomialtimealgorithm(allsolutionsareproventobemoreinefficientthanpolynomialtime)
u Unknown:notknowniftheproblemistractableorintractable(noknownpolynomialtimesolution,noproofthatapolynomialtimesolutiondoesnotexist)
44
Tractability and Intractability
u ‘Propertiesofproblems’(NOT ‘propertiesofalgorithms’)u Tractable:problemcanbesolvedbyapolynomialtimealgorithm(orsomethingmoreefficient)
u Intractable:problemcannotbesolvedbyapolynomialtimealgorithm(allsolutionsareproventobemoreinefficientthanpolynomialtime)
u Unknown:notknowniftheproblemistractableorintractable(noknownpolynomialtimesolution,noproofthatapolynomialtimesolutiondoesnotexist)
45
Subset sum problem
u Givenasetofintegersandanintegers,doesanynon-emptysubsetsumtos?
u {1,4,67,-1,42,5,17}ands=24 Nou {4,3,17,12,10,20}ands =19 Yes {4,3,12}
u IfasethasN elements,ithas2N subsets.u CheckingthesumofeachsubsettakesamaximumofNoperations
u Tocheckallthesubsetstakes2NNoperationsu Someclevernesscanreducethisbyabit(2Nbecomes 2N/2,butallknownalgorithmsareexponential)
46
Take away
u Somesimpleproblemsseemtobeveryhardtosolvebecauseofexponentialorfactorialrun-time
u Notsosimpleinpractice:
47
Problem NaïveSolution(s) Best?
Knapsack 2N 2N,pseudopolynomial
Subset-sum N2N 2N/2,pseudopolynomial
TravellingSalesman N! N22N
P and NP
u P:setofproblemsthatcanbesolvedinpolynomialtime
u Considersubsetsumv Noknownpolynomialtimealgorithmv However,ifyougivemeasolutiontotheproblem,itiseasyformetocheckifthesolutioniscorrect– i.e.Icanwriteapolynomialtimealgorithmtocheckifagivensolutioniscorrect