Escolar Documentos
Profissional Documentos
Cultura Documentos
Agenda:
Lectures1012
Introduction
CountingOperations
BigODefinition
PropertiesofBigO
CalculatingBigO
GrowthRateExamples
BigOPerformanceofPythonLists
BigOPerformanceofPythonDictionaries
AlgorithmAnalysis/Complexity
Reading:
Textbook:Chapter2
COMPSCI105
WhatIsAlgorithmAnalysis?
DataStructures&Algorithm
Howtocompare programswithoneanother?
Whentwoprogramssolvethesameproblembutlookdifferent,
isoneprogrambetter thantheother?
Whatcriteria areweusingtocomparethem?
DataStructures:
Asystematicwayoforganizing andaccessing data.
NosingledatastructureworkswellforALL purposes.
Readability?
Efficient?
Whydoweneedalgorithmanalysis/complexity?
Writingaworkingprogramisnotgoodenough
Theprogrammaybeinefficient!
Iftheprogramisrunonalargedataset,thentherunningtimebecomes
anissue
Algorithm
Input
Algorithm
Output
Analgorithmisastepbystepprocedureforsolvingaprobleminafinite
amountoftime.
Program
isanalgorithmthathasbeenencodedintosomeprogramminglanguage.
Program=datastructures+algorithms
COMPSCI105
COMPSCI105
AlgorithmAnalysis/Complexity
EfficiencyofAlgorithms
Whenweanalyzetheperformanceofanalgorithm,weare
interestedinhowmuch ofagivenresource thealgorithmuses
tosolveaproblem.
Themostcommonresourcesaretime (howmanystepsittakes
tosolveaproblem)andspace (howmuchmemoryittakes).
Wearegoingtobemainlyinterestedinhow long ourprograms
taketorun,astimeisgenerallyamorepreciousresourcethan
space.
Forexample,thefollowinggraphsshowtheexecutiontime,in
milliseconds,againstsamplesize,nofagivenproblemin
differentcomputers
More
powerful
computer
Theactualrunningtimeofaprogramdependsnotonlyonthe
efficiencyofthealgorithm,butonmanyothervariables:
Processorspeed&type
Operatingsystem
etc
COMPSCI105
COMPSCI105
RunningtimeofAlgorithms
Example1
Inordertocomparealgorithmspeedsexperimentally
Task:
Completethesum_of_n() functionwhichcalculatesthesumofthefirst
nnaturalnumbers.
Allothervariablesmustbekeptconstant,i.e.
independentofspecificimplementations,
independentofcomputers used,and,
independentofthedata onwhichtheprogramruns
Arguments:aninteger
Returns:thesumofthefirstnnaturalnumbers
Cases:
Involvedalotofwork(bettertohavesometheoreticalmeansof
predictingalgorithmspeed)
sum_of_n(5)
15
sum_of_n(100000)
5000050000
COMPSCI105
COMPSCI105
Algorithm1
Algorithm2
sum_of_n
sum_of_n_2
Setthe_sum =0
Setthe_sum =0
Addeachvaluetothe_sum using
aforloop
Usetheequation(n(n+1))/2,to
calculatethetotal
Returnthe_sum
Returnthe_sum
time_start = time.time()
time_start = time.clock()
Thetimingcalls
embeddedbeforeand
afterthesummation
tocalculatethetime
requiredforthe
calculation
the_sum = 0
for i in range(1,n+1):
the_sum = the_sum + i
time_end = time.time()
time_taken = time_end - time_start
the_sum = 0
the_sum = (n * (n+1) ) / 2
time_end = time.clock()
time_taken = time_end - time_start)
9
COMPSCI105
ExperimentalResult
AdvantagesofLearningAnalysis
Using4differentvaluesforn:[10000,100000,1000000,
10000000]
Time
Consuming
Process!
sum_of_n
(for loop)
sum_of_n_2
(equation)
10000
0.0033
0.00000181
100000
0.0291
0.00000131
1000000
0.3045
0.00000107
10000000
2.7145
0.00000123
Timeincreaseaswe
increasethevalueofn.
10
COMPSCI105
Predicttherunningtimeduringthedesignphase
Therunningtimeshouldbeindependent ofthetypeofinput
Therunningtimeshouldbeindependent ofthehardwareandsoftware
environment
Saveyourtimeandeffort
Thealgorithmdoesnotneedtobecoded anddebugged
Helpyoutowritemoreefficientcode
NOTimpactedby
numberofintegers
beingadded.
Weshallcount thenumberofbasicoperationsofanalgorithm,
andgeneralise thecount.
COMPSCI105
11
COMPSCI105
12
BasicOperations
Example2A
Weneedtoestimate therunningtimeasafunctionofproblem
sizen.
AprimitiveOperationtakesaunitoftime.Theactuallength
oftimewilldependonexternalfactorssuchasthehardwareand
softwareenvironment
Eachofthesekindsofoperationwouldtakethesameamountoftimeona
givenhardwareandsoftwareenvironment
Assigningavaluetoavariable
Callingamethod.
Performinganarithmeticoperation.
Comparingtwonumbers.
Indexingalistelement.
Returningfromafunction
1 assignment ->
1 assignment ->
11 comparisons ->
10 plus/assignments ->
10 plus/assignments ->
1 return ->
Problemsize
Example:Calculatingthesumofelementsinthelist.
Performanceisusuallymeasuredbytherate atwhichthe
runningtimeincreasesastheproblemsizegetsbigger,
ie.weareinterestedintherelationshipbetweentherunningtimeand
theproblemsize.
Itisveryimportantthatweidentifywhattheproblemsizeis.
def count2(numbers):
n = len(numbers)
the_sum = 0
index = 0
while index < n:
the_sum = the_sum + numbers[index]
index += 1
return the_sum
Forexample,ifweareanalyzinganalgorithmthatprocessesalist,the
problemsizeisthesize ofthelist.
Inmanycases,theproblemsizewillbethevalue ofavariable,
wheretherunningtimeoftheprogramdependsonhowbigthat
valueis.
Total=3n+5operations
Weneedtomeasureanalgorithmstimerequirementasa
functionoftheproblemsize,e.g.intheexampleabovethe
problemsizeisthenumberofelementsinthelist.
COMPSCI105
14
COMPSCI105
Example2B
1 assignment ->
1 assignment ->
1 assignment ->
n +1 comparisons ->
n plus/assignments ->
n plus/assignments ->
1 return
def count1(numbers):
the_sum = 0
index = 0
while index < 10:
the_sum = the_sum + numbers[index]
index += 1
return the_sum
Total=34operations
13
COMPSCI105
Example:Calculatingasumoffirst10elementsinthelist
15
COMPSCI105
16
Exercise1
Example3
Howmanyoperationsarerequiredtodothefollowingtasks?
Considerthefollowingtwoalgorithms:
AlgorithmA:
a) Addinganelementtotheendofalist
OuterLoop:noperations
InnerLoop: operations
b) Printingeachelementofalistcontainingnelements
Total=
= operations
AlgorithmB:
OuterLoop:noperations
InnerLoop:5operations
Total=n*5=5*noperations
for i in range(0, n):
for j in range(0, 5):
print (i,j)
17
COMPSCI105
18
COMPSCI105
GrowthRateFunction AorB?
GrowthRateFunction AorB?
Forsmallervaluesofn,thedifferencesbetweenalgorithmA
(n2/5)andalgorithmB(5n)arenotverybig.Butthedifferences
areveryevidentforlargerproblemsizessuchasforn>1,000,000
2*1011
Ifnis106 ,
AlgorithmAstimerequirementis
Vs5*106
=2*1011
large problemsizes
Algorithmefficiencyisaconcernfor
AlgorithmBstimerequirementis
5*n=5*106
Whatdoesthegrowthratetellus
abouttherunningtimeofthe
program?
COMPSCI105
Aisfaster
whennisa
smallnumber
19
COMPSCI105
20
BigODefinition
BigONotation
Let
and
befunctionsthatmapnonnegative
integerstorealnumbers.Wesaythat
isO(
)if
thereisarealconstant,c,wherec>0andanintegerconstant
c
foreveryintegern
n0,wheren0 1suchthat
n0.
describetheactualtimeoftheprogram
isamuchsimplerfunctionthan
Withassumptionsandapproximations,wecanuse
thecomplexityi.e.O(
WeuseBigOnotation(capitalletterO)tospecifytheorder
ofcomplexityofanalgorithm
todescribe
BigONotationisa
mathematicalformulathatbest
describesanalgorithms
performance
21
COMPSCI105
22
COMPSCI105
BigOhNotation(FormalDefinition)
BigOExamples
Supposeanalgorithmrequires
Givenfunctions
and
, wesaythat
isO(
therearepositiveconstants,candn0,suchthat
c
foreveryintegern n0.
7n2operationstosolveaproblemofsizen
)if
c
for
everyintegern>=n0.
O(n)
10,000
Example:2n+10isO(n)
2n+10 cn
(c 2)n 10
n 10/(c 2)
Pickc=3andn0 =10
n2 3*n+10operationstosolveaproblemofsizen
3n
1,000
2n+10
O(n2)
100
1
1
10
100
O(n3)
1,000
COMPSCI105
23
COMPSCI105
24
PropertiesofBigO
Ignoreloworderterms
TherearethreepropertiesofBigO
Ignorelowordertermsinthefunction(smallerterms)
Considerthefunction:
O(f(n))+O(g(n))=O(maxoff(n)andg(n))
Ignoreanyconstants inthehighordertermofthefunction
C*O(f(n))=O(f(n))
Combine growthratefunctions
Forsmallvaluesofnthelastterm,1000,dominates.
Whennisaround10,theterms100n+1000dominate.
Whennisaround100,thetermsn2 and100ndominate.
Whenngetsmuchlargerthan100,then2 dominatesallothers.
SoitwouldbesafetosaythatthisfunctionisO(n2)forvaluesofn>
100
O(f(n))*O(g(n))=O(f(n)*g(n))
O(f(n))+O(g(n))=O(f(n)+g(n))
Consideranotherfunction:
f(n) = n3 + n2 + n + 5000
BigOisO(n3)
Andconsideranotherfunction:
f(n) = n + n2 + 5000
BigOisO(n2)
COMPSCI105
25
26
COMPSCI105
IgnoreanyConstantMultiplications
Combinegrowthratefunctions
Considerthefunction:
Considerthefunction:
f(n) = n * log n
f(n) = 254 * n2 + n
BigOisO(n2)
Consideranotherfunction:
BigOisO(nlogn)
Consideranotherfunction:
f(n) = n / 30
f(n) = n2 * n
BigOisO(n)
Andconsideranotherfunction:
BigOisO(n3)
f(n) = 3n + 1000
BigOisO(n)
COMPSCI105
27
COMPSCI105
28
Exercise2
Best,average&worstcasecomplexity
Insomecases,itmayneedtoconsiderthebest,worstand/or
averageperformanceofanalgorithm
Forexample,ifwearerequiredtosortalistofnumbersan
ascendingorder
WhatistheBigOperformanceofthefollowinggrowth
functions?
Worstcase:
ifitisinreverseorder
T(n)=n+log(n)
T(n)=n4 +n*log(n)+300n3
T(n)=300n+60*n*log(n)+342
Bestcase:
ifitisalreadyinorder
Averagecase
Determinetheaverageamountoftimethatanalgorithmrequirestosolve
problemsofsizen
Moredifficulttoperformtheanalysis
Difficulttodeterminetherelativeprobabilitiesofencounteringvariousproblems
ofagivensize
Difficulttodeterminethedistributionofvariousdatavalues
29
COMPSCI105
CalculatingBigO
Rules
Rule1:Straightlinecode
Rulesforfindingoutthetimecomplexityofapieceofcode
30
COMPSCI105
BigO=ConstanttimeO(1)
Doesnotvarywiththesizeoftheinput
Example:
Straightlinecode
Loops
NestedLoops
Consecutivestatements
Ifthenelsestatements
Logarithmiccomplexity
Assigningavaluetoavariable
Performinganarithmeticoperation.
Indexingalistelement.
x = a + b
i = y[2]
Rule2:Loops
Therunningtimeofthestatementsinsidetheloop
(includingtests)timesthenumberofiterations
Example:
Constanttime*n
=c*n=O(n)
Executed
n times
for i in range(n):
print(i)
Constant
time
COMPSCI105
31
COMPSCI105
32
Rules(cont)
Rules(cont)
Rule3:NestedLoop
Rule5:ifelsestatement
Worstcaserunningtime:thetest,pluseithertheif part
ortheelse part(whicheveristhelarger).
Example:
Analyzeinsideout.Totalrunningtimeistheproductof
thesizesofalltheloops.
Outer loop:
Executed n times
Example:
constant*(innerloop:n)*(outerloop:n)
Totaltime=c*n*n=c*n2 =O(n2)
for i in range(n):
for j in range(n):
k = i + j
c0 +Max(c1,(n*(c2 +c3)))
Totaltime=c0 *n(c2 +c3)=O(n)
Assumption:
Inner loop:
Executed n times
Rule4:Consecutivestatements
Theconditioncanbeevaluatedinconstanttime.Ifitisnot,weneedto
addthetimetoevaluatetheexpression.
Addthetimecomplexitiesofeachstatement
Example:
Constanttime+ntimes*constanttime
c0 +c1n
BigO=O(f(n)+g(n))
=O(max(f(n)+g(n)))
Executed
n times
=O(n)
Test:
Constant time c0
Constant
time
x = x + 1
for i in range(n):
m = m + 2;
33
COMPSCI105
True case:
if len(a) != len(b):
Constant c1
return False
else:
for index in range(len(a)):
False case:
if a[index] != b[index]:
Executed n times
return False
Another if:
constant c2 + constant c3
34
COMPSCI105
Rules(cont)
HypotheticalRunningTime
Rule6:Logarithmic
AnalgorithmisO(logn)ifittakesaconstanttimetocuttheproblemsize
byafraction(usuallyby)
Example:
Findingawordinadictionaryofnpages
Lookatthecentre pointinthedictionary
Iswordtoleftorrightofcentre?
Repeatprocesswithleftorrightpartofdictionaryuntilthewordisfound
Example:
size = n
while size > 0:
// O(1) stuff
size = size / 2
Size:n,n/2,n/4,n/8,n/16,...2,1
Ifn=2K,itwouldbeapproximatelyksteps.Theloopwillexecutelogkin
theworstcase(log2n=k).BigO=O(logn)
Note:wedontneedtoindicatethebase.Thelogarithmstodifferentbases
differonlybyaconstantfactor.
COMPSCI105
35
Therunningtimeonahypotheticalcomputerthatcomputes106
operationspersecondforvariesproblemsizes
Notation
n
10
102
103
104
105
106
1 sec
1 sec
1 sec
1 sec
1 sec
O(1)
Constant
1 sec
O(log(n))
Logarithmic
3 sec
7 sec
10 sec
13 sec
17 sec
20 sec
O(n)
Linear
10
sec
100 sec
1 msec
10 msec
100 msec
1 sec
O(nlog(n))
N log N
33 sec
664 sec
10 msec
13.3 msec
1.6 sec
20 sec
O(n2)
Quadratic
100 sec
10 msec
1 sec
1.7 min
16.7 min
11.6 days
O(n3)
Cubic
1 msec
1 sec
16.7 min
11.6 days
31.7 years
31709
years
O(2n)
Exponential
10 msec
3e17 years
COMPSCI105
36
ComparisonofGrowthRate
ConstantGrowthRate O(1)
Timerequirementisconstantand,therefore,independentof
theproblemssizen.
def rate1(n):
s = "SWEAR"
for i in range(25):
print("I must not ", s)
101
102
103
104
105
106
O(1)
COMPSCI105
38
COMPSCI105
LogarithmicGrowthRate O(logn)
LinearGrowthRate O(n)
Increaseslowlyastheproblemsizeincreases
Ifyousquaretheproblemsize,youonlydoubleitstime
requirement
Thebaseofthelogdoesnotaffectaloggrowthrate,soyou
canomitit.
Thetimeincreasesdirectlywiththesizesoftheproblem.
Ifyousquaretheproblemsize,youalsosquareitstime
requirement
def rate3(n):
s = "FIGHT"
for i in range(n):
print("I must not ", s)
def rate2(n):
s = "YELL"
i = 1
while i < n:
print("I must not ", s)
i = i * 2
n
101
102
103
104
105
106
101
102
103
104
105
106
O(log2 n)
13
16
19
O(n)
10
102
103
104
105
106
COMPSCI105
39
COMPSCI105
40
10
QuadraticGrowthRate O(n2)
n*lognGrowthRate O(nlog(n))
Thetimerequirementincreasesmorerapidlythanalinear
algorithm.
Suchalgorithmsusuallydivideaproblemintosmaller
problemthatareeachsolvedseparately.
Thetimerequirementincreasesrapidlywiththesizeofthe
problem.
Algorithmsthatusetwonestedloopsareoftenquadratic.
def rate5(n):
s = "LIE"
for i in range(n):
for j in range(n):
print("I must not ", s)
def rate4(n):
s = "HIT"
for i in range(n):
j = n
while j > 0:
print("I must not ", s)
j = j // 2
101
102
103
104
105
106
101
102
103
104
105
106
O(nlog(n))
30
664
9965
105
106
107
O(n2)
102
104
106
108
1010
1012
41
COMPSCI105
42
COMPSCI105
ExponentialGrowthRate O(2n)
CubicGrowthRate O(n3)
Thetimerequirementincreasesmorerapidlywiththesizeof
theproblemthanthetimerequirementforaquadratic
algorithm
Algorithmsthatusethreenestedloopsareoftenquadratic
andarepracticalonlyforsmallproblems.
Asthesizeofaproblemincreases,thetimerequirement
usuallyincreasestoorapidlytobepractical.
def rate7(n):
s = "POKE OUT MY TONGUE"
for i in range(2 ** n):
print("I must not ", s)
def rate6(n):
s = "SULK"
for i in range(n):
for j in range(n):
for k in range(n):
print("I must not ", s)
101
102
103
104
105
106
101
102
103
104
105
106
O(n3)
103
106
109
1012
1015
1018
O(2n)
103
1030
10301
103010
1030103
10301030
COMPSCI105
43
COMPSCI105
44
11
Exercise3
Exercise3
WhatistheBigOofthefollowingstatements?
WhatistheBigOofthefollowingstatements?
Executed
n times
for i in range(n):
for j in range(i+1, n):
print(i,j)
Executed
10 times
for i in range(n):
for j in range(10):
print (i,j)
Constant time
Wheniis0,theinnerloopexecutes(n1)times.Wheniis1,theinner
loopexecutesn2times.Wheniisn2,theinnerloopexecuteonce.
Thenumberoftimestheinnerloopstatementsexecute:
Runningtime=n*10*1=10n,BigO=
WhatistheBigOofthefollowingstatements?
Executed
n times
Executed
n times
for i in range(n):
for j in range(n):
print(i,j)
for k in range(n):
print(k)
(n1)+(n2)...+2+1
Runningtime=n*(n1)/2,
BigO=
?
Executed
n times
ThefirstsetofnestedloopsisO(n2)andthesecondloopisO(n).Thisis
O(max(n2,n))BigO=
?
45
COMPSCI105
46
COMPSCI105
Exercise3
GivetheBigOcomplexityofthebigO()functionbelow:
def bigO(my_list1):
def bigO(my_list1):
n = len(my_list1)
my_list2 = []
i = 5
n = len(my_list1)
while i < n:
for i in range(n):
helperO1(my_list1, i)
j = i
i += 5
while j > 0:
return my_list1
my_list2.append(my_list1[j])
j = j // 2
return my_list2
value = my_list1[position2]
i = position2 3
while i > -1 and i < position2:
average = int((value + my_list1[i]) / 2)
my_list1[i] = average
i += 1
COMPSCI105
47
COMPSCI105
48
12
PerformanceofPythonDataStructures
Review
Wehaveageneralideaof
BigOnotationand
thedifferencesbetweenthedifferentfunctions,
Now,wewilllookattheBigOperformancefortheoperations
onPythonlists anddictionaries.
Itisimportanttounderstand theefficiency ofthesePython
datastructures
Inlaterchapterswewillseesomepossibleimplementations of
bothlistsanddictionariesandhowtheperformance depends
ontheimplementation.
49
COMPSCI105
Pythonlistsareorderedsequencesofitems.
Specificvaluesinthesequencecanbereferencedusing
subscripts.
Pythonlistsare:
dynamic.Theycangrowandshrinkondemand.
heterogeneous,asinglelistcanholdarbitrarydatatypes.
mutable sequencesofarbitraryobjects.
ListOperations
ListOperations
Usingoperators:
my_list = [1,2,3,4]
print (2 in my_list)
zeroes = [0] * 20
print (zeroes)
COMPSCI105
50
COMPSCI105
UsingMethods:
True
[0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0]
51
COMPSCI105
52
13
Examples
ListOperations
Examples:
Thedel statement
print (my_list.index(4))
my_list.insert(4, "Hello")
print (my_list)
Indexofthefirst
occurrenceofthe
parameter
>>>
>>>
>>>
[1,
>>>
>>>
[1,
>>>
>>>
[]
[9,5,4,3,'Hello',2,1,1]
print (my_list.count(1))
my_list.remove(1)
print (my_list)
[9,5,4,3,'Hello',2,1]
print(my_list.pop(3))
print (my_list)
Removeanitemfromalistgivenitsindexinsteadofitsvalue
Usedtoremoveslicesfromalistorcleartheentirelist
[3,1,4,1,5,9,2]
[1,1,2,3,4,5,9]
[9,5,4,3,2,1,1]
my_list = [3, 1, 4, 1, 5, 9]
my_list.append(2)
my_list.sort()
my_list.reverse()
Thenumberof
occurrenceofthe
parameter
3
[9,5,4,'Hello',2,1]
53
COMPSCI105
54
COMPSCI105
BigOEfficiencyofListOperators
O(1) Constant
Operationsforindexing andassigning toanindexposition
BigO=O(1)
Ittakesthesameamountoftimenomatterhowlargethelistbecomes.
i.e.independentofthesizeofthelist
COMPSCI105
55
COMPSCI105
56
14
InsertingelementstoaList
4Experiments
Therearetwowaystocreatealongerlist.
Fourdifferentwaystogeneratealistofnnumbersstartingwith
0.
for i in range(n):
Example1:
Usingaforloopandcreatethelistbyconcatenation
BigOfortheappendmethodis (1).
BigOfortheconcatenationoperatoris ( )where isthesize
ofthelistthatisbeingconcatenated.
Example2:
Usingaforloopandtheappendmethod
for i in range(n):
my_list.append(i)
Example3:
Usinglistcomprehension
Example4:
Usingtherangefunctionwrappedbyacalltothelistconstructor.
my_list = list(range(n))
57
COMPSCI105
58
COMPSCI105
TheResult
Pop()vsPop(0)
Fromtheresultsofourexperiment:
Fromtheresultsofourexperiment:
Append:BigOisO(1)
Concatenation:BigOisO(k)
1)Usingforloop
Theappendoperationismuchfasterthanconcatenation
2)Twoadditionalmethodsforcreatingalist
Usingthelistconstructorwithacalltorange ismuchfaster thanalist
comprehension
Asthelistgetslongerandlongerthetimeittakestopop(0)alsoincreases
thetimeforpopstaysveryflat.
pop(0):BigOisO(n)
pop():BigOisO(1)
Why?
Itisinterestingtonotethatthelistcomprehensionistwice asfastasafor
loopwithanappendoperation.
pop(0)
for i in range(n):
my_list = my_list + [i]
for i in range(n):
my_list.append(i)
COMPSCI105
pop()
my_list = list(range(n))
59
COMPSCI105
60
15
Pop()vsPop(0)
Exercise4
pop():
Whichofthefollowinglistoperationsisnot (1)?
Removeselementfromtheendofthelist
pop(0)
1.
2.
3.
4.
Removesfromthebeginningofthelist.
BigOisO(n)aswewillneedtoshiftallelementsfromspacetothe
beginningofthelist
list.pop(0)
list.pop()
list.append()
list[10]
?
12
44
100
44
100
18
18
61
COMPSCI105
62
COMPSCI105
IntroductionPythonDictionaries
PerformanceofPythonDictionaries
Dictionariesstoreamappingbetweenasetofkeys andasetof
values
Keyscanbeanyimmutable type.
Valuescanbeany type
Asingledictionarycanstorevaluesofdifferenttypes
capitals = {'Iowa':'DesMoines','Wisconsin':'Madison'}
print(capitals['Iowa'])
capitals['Utah']='SaltLakeCity'
print(capitals)
capitals['California']='Sacramento'
print(len(capitals))
for k in capitals:
print(capitals[k]," is the capital of ", k)
Youcandefine,modify,view,lookupordeletethekeyvalue
pairsinthedictionary
Dictionariesareunordered
Note:
Dictionariesdifferfromlistsinthatyoucanaccessitemsinadictionary
byakey ratherthanaposition.
COMPSCI105
DesMoines
{'Wisconsin':'Madison','Iowa':'DesMoines',
'Utah':'SaltLakeCity'}
4
SacramentoisthecapitalofCalifornia
MadisonisthecapitalofWisconsin
DesMoines isthecapitalofIowa
SaltLakeCity isthecapitalofUtah
63
COMPSCI105
64
16
BigOEfficiencyofOperators
Containsbetweenlistsanddictionaries
Table2.3
Operation
Copy
getitem
setitem
deleteitem
contains(in)
iteration
Fromtheresults
Thetimeittakesforthecontains operatoronthelistgrows linearlywith
thesizeofthelist.
Thetimeforthecontains operatoronadictionaryisconstant evenasthe
dictionarysizegrows
BigOEfficiency
( )
(1)
(1)
(1)
(1)
( )
Lists,bigOisO(n)
Dictionaries,bigOisO(1)
Lists
Dictionaries
65
COMPSCI105
66
COMPSCI105
Exercise5
Summary
CompletetheBigOperformanceofthefollowingdictionary
operations
ComplexityAnalysismeasureanalgorithmstimerequirementas
afunctionoftheproblemsizebyusingagrowthratefunction.
Itisanimplementationindependent wayofmeasuringanalgorithm
1.
inmy_dict
2.
delmy_dict[ ]
3.
my_dict[ ]==10
4.
Complexityanalysisfocusesonlarge problems
Worstcaseanalysisconsidersthemaximum amountofworkan
algorithmwillrequireonaproblemofagivensize
Averagecaseanalysisconsiderstheexpected amountofworkthatitwill
require.
Generallywewanttoknowtheworstcaserunningtime.
Itprovidestheupperboundontimerequirements
Wemayneedaverageorthebestcase
Normallyweassumeworstcaseanalysis,unlesstoldotherwise.
COMPSCI105
67
COMPSCI105
68
17