algorithmic speed - hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · algorithmic...
TRANSCRIPT
![Page 1: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/1.jpg)
AlgorithmicSpeed
BBM101- Introduction toProgramming I
Hacettepe UniversityFall2015
FuatAkal,AykutErdem,Erkut Erdem,Vahid Garousi
1Slides basedonmaterialpreparedbyE.Grimson, J.Guttag andC.Terman inMITx 6.00.1x
![Page 2: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/2.jpg)
Measuringcomplexity
• Goalsindesigningprograms1. Itreturnsthecorrectansweronalllegalinputs2. Itperformsthecomputationefficiently
• Typically(1)ismostimportant,butsometimes(2)isalsocritical,e.g.,programsforcollisiondetection
• Evenwhen(1)ismostimportant,itisvaluabletounderstandandoptimize(2)
2
![Page 3: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/3.jpg)
Computationalcomplexity
• Howmuchtimewillittakeaprogramtorun?• Howmuchmemorywillitneedtorun?
• Needtobalanceminimizingcomputationalcomplexitywithconceptualcomplexity– Keepcodesimpleandeasytounderstand,butwherepossibleoptimizeperformance
3
![Page 4: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/4.jpg)
Howdowemeasurecomplexity?
• Givenafunction,wouldliketoanswer:“Howlongwillthistaketorun?”
• Couldjustrunonsomeinputandtimeit.• Problemisthatthisdependson:
1. Speedofcomputer2. SpecificsofPythonimplementation3. Valueofinput
• Avoid(1)and(2)bymeasuringtimeintermsofnumberofbasicstepsexecuted
4
![Page 5: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/5.jpg)
Measuringbasicsteps
• Usearandomaccessmachine(RAM)asmodelofcomputation– Stepsareexecutedsequentially– Stepisanoperationthattakesconstanttime• Assignment• Comparison• Arithmeticoperation• Accessingobjectinmemory
• Forpoint(3),measuretimeintermsofsizeofinput
5
![Page 6: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/6.jpg)
Butcomplexitymightdependonvalueofinput?
def linearSearch(L, x): for e in L:
if e==x: return True
return False
• IfxhappenstobenearfrontofL,thenreturnsTruealmostimmediately
• IfxnotinL,thencodewillhavetoexamineallelementsofL
• Needageneralwayofmeasuring
6
![Page 7: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/7.jpg)
Casesformeasuringcomplexity• Bestcase:minimumrunningtimeoverallpossibleinputsofagivensize– ForlinearSearch – constant,i.e.independentofsizeofinputs
• Worstcase:maximumrunningtimeoverallpossibleinputsofagivensize– ForlinearSearch – linearinsizeoflist
• Average(orexpected)case:averagerunningtimeoverallpossibleinputsofagivensize
• Wewillfocusonworstcase– akindofupperboundonrunningtime
7
![Page 8: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/8.jpg)
Example• Numberofsteps– 1 (forassignment)– 5*n (1fortest,plus2forfirstassignment,plus2forsecondassignmentinwhile;repeatedn<mesthroughwhile)
– 1 (forreturn)• 5*n+2steps• Butasngetslarge,2isirrelevant,sobasically5*nsteps
8
def fact(n): answer = 1 while n > 1:
answer *= n n -= 1
return answer
![Page 9: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/9.jpg)
Example
• Whataboutthemultiplicativeconstant(5inthiscase)?
• Wearguethatingeneral,multiplicativeconstantsarenotrelevantwhencomparingalgorithms
9
![Page 10: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/10.jpg)
Exampledef sqrtExhaust(x, eps):
step = eps**2 ans = 0.0 while abs(ans**2 - x) >= eps and ans <= max(x, 1):
ans += step
return ans
• Ifwecallthison100and0.0001,willtakeonebillioniterationsoftheloop– Haveroughly8stepswithineachiteration
10
![Page 11: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/11.jpg)
Exampledef sqrtBi(x, eps):
low = 0.0high = max(1, x) ans = (high + low)/2.0 while abs(ans**2 - x) >= eps:
if ans**2 < x: low = ans
else: high = ans
ans = (high + low)/2.0 return ans
• Ifwecallthison100and0.0001,willtakethirtyiterationsoftheloop– Haveroughly10stepswithineachiteration
• 1billionor8billionversus30or300– itissizeofproblemthatmatters
11
![Page 12: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/12.jpg)
Measuringcomplexity
• Giventhisdifferenceiniterationsthroughloop,multiplicativefactor(numberofstepswithinloop)probablyirrelevant
• Thus,wewillfocusonmeasuringthecomplexityasafunctionofinputsize–Willfocusonthelargestfactorinthisexpression–Willbemostlyconcernedwiththeworstcasescenario
12
![Page 13: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/13.jpg)
Asymptoticnotation
• Needaformalwaytotalkaboutrelationshipbetweenrunningtimeandsizeofinputs
• Mostlyinterestedinwhathappensassizeofinputsgetsverylarge,i.e.approachesinfinity
13
![Page 14: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/14.jpg)
Exampledef f(x):
for i in range(1000): ans = i
for i in range(x): ans += 1
for i in range(x): for j in range(x):
ans += 1
Complexityis1000+2x+2x2,ifeachlinetakesonestep
14
![Page 15: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/15.jpg)
Example
• 1000+2x+2x2
• Ifxissmall,constanttermdominates– E.g.,x=10then1000of1220stepsareinfirstloop
• Ifxislarge,quadratictermdominates– E.g.x=1,000,000,thenfirstlooptakes0.000000005%oftime,secondlooptakes0.0001%oftime(outof2,000,002,001,000 steps)!
15
![Page 16: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/16.jpg)
Example• Soreallyonlyneedtoconsiderthenestedloops(quadraticcomponent)
• Doesitmatterthatthisparttakes2x2 steps,asopposedtosayx2 steps?– Forourexample,ifourcomputerexecutes100millionstepspersecond,differenceis5.5hoursversus2.25hours
– Ontheotherhandifwecanfindalinearalgorithm,thiswouldruninafractionofasecond
– Somultiplicativefactorsprobablynotcrucial,butorderofgrowthiscrucial
16
![Page 17: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/17.jpg)
Rulesofthumbforcomplexity
• Asymptoticcomplexity– Describerunningtimeintermsofnumberofbasicsteps
– Ifrunningtimeissumofmultipleterms,keeponewiththelargestgrowthrate
– Ifremainingtermisaproduct,dropanymultiplicativeconstants
• Use“BigO”notation(akaOmicron)– Givesanupperboundonasymptoticgrowthofafunction
17
![Page 18: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/18.jpg)
Complexityclasses
• O(1)denotesconstantrunningtime• O(logn)denoteslogarithmicrunningtime• O(n)denoteslinearrunningtime• O(nlogn)denoteslog-linearrunningtime• O(nc)denotespolynomialrunningtime(cisaconstant)
• O(cn)denotesexponentialrunningtime(cisaconstantbeingraisedtoapowerbasedonsizeofinput)
18
![Page 19: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/19.jpg)
Constantcomplexity
• Complexityindependentofinputs• Veryfewinterestingalgorithmsinthisclass,butcanoftenhavepiecesthatfitthisclass
• Canhaveloopsorrecursivecalls,butnumberofiterationsorcallsindependentofsizeofinput
19
![Page 20: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/20.jpg)
Logarithmiccomplexity
• Complexitygrowsaslogofsizeofoneofitsinputs
• Example:– Bisectionsearch– Binarysearchofalist
20
![Page 21: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/21.jpg)
Logarithmiccomplexity
21
def intToStr(i): digits = '0123456789' if i == 0:
return '0' result = '' while i > 0:
result = digits[i%10] + result i = i/10
return result
![Page 22: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/22.jpg)
Logarithmiccomplexity• Onlyhavetolookatloopasnofunctioncalls
• Withinwhileloopconstantnumberofsteps
• Howmanytimesthroughloop?– Howmanytimescanonedividei by10?
– O(log(i))
22
def intToStr(i): digits = '0123456789' if i == 0:
return '0' result = '' while i > 0:
result = digits[i%10] + result
i = i/10 return result
![Page 23: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/23.jpg)
Linearcomplexity• Searchingalistinordertoseeifanelementispresent
• Addcharactersofastring,assumedtobecomposedofdecimaldigits
def addDigits(s): val = 0 for c in s:
val += int(c)
return val
• O(len(s))23
![Page 24: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/24.jpg)
Linearcomplexity• Complexitycandependonnumberofrecursivecalls
def fact(n): if n == 1:
return 1 else:
return n*fact(n-1)
• Numberofrecursivecalls?– Fact(n),thenfact(n-1),etc.untilgettofact(1)– Complexityofeachcallisconstant– O(n) 24
![Page 25: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/25.jpg)
Log-linearcomplexity
• Manypracticalalgorithmsarelog-linear• Verycommonlyusedlog-linearalgorithmismergesort
• Willreturntothis
25
![Page 26: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/26.jpg)
Polynomialcomplexity
• Mostcommonpolynomialalgorithmsarequadratic,i.e.,complexitygrowswithsquareofsizeofinput
• Commonlyoccurswhenwehavenestedloopsorrecursivefunctioncalls
26
![Page 27: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/27.jpg)
Quadraticcomplexity
27
def isSubset(L1, L2): for e1 in L1:
matched = False for e2 in L2:
if e1 == e2: matched = True break
if not matched: return False
return True
![Page 28: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/28.jpg)
Quadraticcomplexity
• Outerloopexecutedlen(L1)times
• Eachiterationwillexecuteinnerloopuptolen(L2)times
• O(len(L1)*len(L2))• WorstcasewhenL1
andL2samelength,noneofelementsofL1inL2
• O(len(L1)2)28
def isSubset(L1, L2): for e1 in L1:
matched = False for e2 in L2:
if e1 == e2: matched = True break
if not matched: return False
return True
![Page 29: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/29.jpg)
Quadraticcomplexity
Findintersectionoftwolists,returnalistwitheachelementappearingonlyonce
29
def intersect(L1, L2): tmp = [] for e1 in L1:
for e2 in L2: if e1 == e2:
tmp.append(e1) res = [] for e in tmp:
if not(e in res): res.append(e)
return res
![Page 30: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/30.jpg)
Quadraticcomplexity
• Firstnestedlooptakeslen(L1)*len(L2)steps
• Secondlooptakesatmostlen(L1)steps
• Lattertermoverwhelmedbyformerterm
• O(len(L1)*len(L2))
30
def intersect(L1, L2): tmp = [] for e1 in L1:
for e2 in L2: if e1 == e2:
tmp.append(e1) res = [] for e in tmp:
if not(e in res): res.append(e)
return res
![Page 31: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/31.jpg)
Exponentialcomplexity
• Recursivefunctionswheremorethanonerecursivecallforeachsizeofproblem– TowersofHanoi
• Manyimportantproblemsareinherentlyexponential– Unfortunate,ascostcanbehigh–Willleadustoconsiderapproximatesolutionsmorequickly
31
![Page 32: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/32.jpg)
Exponentialcomplexity
32
def genSubsets(L): res = [] if len(L) == 0:
return [[]] #list of empty list smaller = genSubsets(L[:-1]) # get all subsets without last element extra = L[-1:] # create a list of just last element new = [] for small in smaller:
new.append(small+extra) # for all smaller solutions, add one with last element return smaller+new# combine those with last element and those without
![Page 33: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/33.jpg)
Exponentialcomplexity
• Assumingappendisconstanttime
• Timeincludestimetosolvesmallerproblem,plustimeneededtomakeacopyofallelementsinsmallerproblem
33
def genSubsets(L): res = [] if len(L) == 0:
return [[]] smaller = genSubsets(L[:-1]) extra = L[-1:] new = [] for small in smaller:
new.append(small+extra) return smaller+new
![Page 34: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/34.jpg)
Exponentialcomplexity
• Butimportanttothinkaboutsizeofsmaller
• Knowthatforasetofsizekthereare2kcases
• Sotosolveneed2n-1 +2n-2 +...+20 steps
• MathtellsusthisisO(2n)
34
def genSubsets(L): res = [] if len(L) == 0:
return [[]] smaller = genSubsets(L[:-1]) extra = L[-1:] new = [] for small in smaller:
new.append(small+extra) return smaller+new
![Page 35: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/35.jpg)
Complexityclasses
• O(1)denotesconstantrunningtime• O(logn)denoteslogarithmicrunningtime• O(n)denoteslinearrunningtime• O(nlogn)denoteslog-linearrunningtime• O(nc)denotespolynomialrunningtime(cisaconstant)
• O(cn)denotesexponentialrunningtime(cisaconstantbeingraisedtoapowerbasedonsizeofinput)
35
![Page 36: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/36.jpg)
Comparingcomplexities
• Sodoesitreallymatterifourcodeisofaparticularclassofcomplexity?
• Dependsonsizeofproblem,butforlargescaleproblems,complexityofworstcasemakesadifference
36
![Page 37: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/37.jpg)
ConstantversusLogarithmic
37
Constant*versus*logarithmic*
![Page 38: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/38.jpg)
Observations
• Alogarithmicalgorithmisoftenalmostasgoodasaconstanttimealgorithm
• Logarithmiccostsgrowveryslowly
38
![Page 39: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/39.jpg)
LogarithmicversusLinear
39
Logarithmic*versus*Linear*
![Page 40: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/40.jpg)
Observations
• Logarithmicclearlybetterforlargescaleproblemsthanlinear
• Doesnotimplylinearisbad,however
40
![Page 41: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/41.jpg)
LinearversusLog-linear
41
Linear*versus*LogFlinear*
![Page 42: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/42.jpg)
Observations
• Whilelog(n)maygrowslowly,whenmultipliedbyalinearfactor,growthismuchmorerapidthanpurelinear
• O(nlogn)algorithmsarestillveryvaluable
42
![Page 43: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/43.jpg)
Log-linearversusQuadratic
43
LogFlinear*versus*Quadra/c*
![Page 44: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/44.jpg)
Observations
• Quadraticisoftenaproblem,however.• Someproblemsinherentlyquadraticbutifpossiblealwaysbettertolookformoreefficientsolutions
44
![Page 45: Algorithmic Speed - Hacettepe Üniversitesibbm101/fall15/lectures/... · 2015-11-11 · Algorithmic Speed BBM 101 -Introduction to Programming I Hacettepe University Fall 2015 Fuat](https://reader033.vdocument.in/reader033/viewer/2022050111/5f48871077253049e13486a2/html5/thumbnails/45.jpg)
QuadraticversusExponential• Exponentialalgorithmsveryexpensive– Rightplotisonalogscale,sinceleftplotalmostinvisiblegivenhowrapidlyexponentialgrows
• Exponentialgenerallynotofuseexceptforsmallproblems
45
Quadra/c*versus*Exponen/al*
• Exponen/al*algorithms*very*expensive*– Right*plot*is*on*a*log*scale,*since*leD*plot*almost*invisible*given*how*rapidly*exponen/al*grows*
• Exponen/al*generally*not*of*use*except*for*small*problems**