advanced algorithmics (6eap) - ut · advanced algorithmics (6eap) mtat.03.238 order of growth…...

79
Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall 1 Jaak Vilo

Upload: hoangkhuong

Post on 29-Aug-2018

248 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

AdvancedAlgorithmics(6EAP)

MTAT.03.238Orderofgrowth…maths

JaakVilo2016fall

1JaakVilo

Page 2: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Programexecutiononinputofsizen

• Howmanysteps/cyclesaprocessorwouldneedtodo

• Howtorelatealgorithmexecutiontonrofstepsoninputofsizen?f(n)

• e.g.f(n)=n+n*(n-1)/2+17n+n*log(n)

Page 3: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Whathappensininfinity?

• Fastercomputer,largerinput?

• Badalgorithmonfastcomputerwillbeoutcompetedbygoodalgorithmonslow…

Page 4: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Big-Ohnotationclasses

Class Informal Intuition Analogy

f(n)∈ ο (g(n)) fisdominatedbyg Strictlybelow <f(n)∈O(g(n)) Boundedfromabove Upperbound ≤f(n)∈Θ(g(n)) Boundedfrom

aboveand below“equalto” =

f(n)∈Ω(g(n)) Boundedfrombelow Lowerbound ≥f(n)∈ω(g(n)) fdominatesg Strictlyabove >

Page 5: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

MathematicalBackgroundJustification

• Asengineers,youwillnotbepaidtosay:MethodAisbetter thanMethodB

orAlgorithmAisfaster thanAlgorithmB

• Suchdescriptionsaresaidtobequalitative innature;fromtheOED:

qualitative,a. a Relatingto,connectedorconcernedwith,qualityorqualities.Nowusuallyinimpliedorexpressedoppositiontoquantitative.

Page 6: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

MathematicalBackgroundJustification

• Businessdecisionscannotbebasedonqualitativestatements:– AlgorithmAcouldbebetter thanAlgorithmB,butAlgorithmAwouldrequirethreepersonweekstoimplement,test,andintegratewhileAlgorithmBhasalreadybeenimplementedandhasbeenusedforthepastyear

– therearecircumstanceswhereitmaybeneficialtouseAlgorithmA,butnotbasedonthewordbetter

Page 7: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

MathematicalBackgroundJustification

• Thus,wewilllookataquantitativemeansofdescribingdatastructuresandalgorithms

• FromtheOED:

quantitative,a. Relatingtoorconcernedwithquantityoritsmeasurement;thatassessesorexpressesquantity.Inlaterusefreq.contrastedwithqualitative.

Page 8: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Programtimeandspacecomplexity

• Time:countnrofelementarycalculations/operationsduringprogramexecution

• Space:countamountofmemory(RAM,disk,tape,flashmemory,…)– usuallywedonotdifferentiatebetweencache,RAM,…

– Inpractice,forexample,randomaccessontapeimpossible

Page 9: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

• Program1.17floatSum(float*a,constintn)

floats=0;for(inti=0;i<n;i++)

s+=a[i];returns;

– Theinstancecharacteristicisn.– Sincea isactuallytheaddressofthefirstelementofa[],andn ispassedbyvalue,thespaceneededbySum()isconstant(Ssum(n)=1).

Page 10: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

• Program1.18floatRSum(float*a,constintn)

if(n<=0)return0;elsereturn(RSum(a,n-1)+a[n-1]);

– Eachcallrequiresatleast4words• Thevaluesofn,a,returnvalueandreturnaddress.

– Thedepthoftherecursionisn+1.• Thestackspaceneededis4(n+1).

n=997

n=1000

n=999

n=998

Page 11: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Inputsize=n

• Inputsizeusuallydenotedbyn• Timecomplexityfunction=f(n)

– array[1..n]– e.g.4*n+3

• Graph:nvertices,medgesf(n,m)

Page 12: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 13: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 14: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 15: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 16: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 17: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 18: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 19: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 20: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

• So,wemaybeabletocountanrofoperationsneeded

• Whatdowedowiththisknowledge?

Page 21: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 22: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 23: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

n2

100 n log n

Page 24: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

n2

100 n log n

n = 1000

Page 25: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

0.00001*n2

100 n log n

n = 10,000,000

Page 26: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

0.00001*n2

100 n log n

n = 2,000,000,000

Page 27: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Logscale y

Page 28: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

logscale xlogscale y

Page 29: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

• plot[1:10]0.01*x*x,5*log(x),x*log(x)/3

Page 30: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 31: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 32: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 33: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Algorithmanalysisgoal

• Whathappensinthe“longrun”,increasingn

• Compare f(n)toreferenceg(n)(orfandg)

• Atsomen0 >0,ifn>n0,alwaysf(n)< g(n)

Page 34: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 35: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 36: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 37: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 38: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Θ ,O,andΩ

Figure 2.1 Graphic examples of the Θ , O, and Ω notations. In each part, the value of n0 shown is the minimum possible value; any greater value would also work.

Page 39: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 40: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 41: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 42: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Dominanttermsonly…

• Essentially,weareinterestedinthelargest(dominant)termonly…

• Whenthisgrowslargeenough,itwill“overshadow” allsmallerterms

Page 43: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Theorem1.2

).()( Thes, .for ,)(

have we,1 ,let Therefore,

.1for ,

)(

:Proof).()( then ,...)( If

0

00

0

0

00

01

mm

m

ii

m

ii

m

m

i

mii

m

m

i

ii

m

i

ii

mmm

nOnfnncnnf

nac

nan

nan

nananf

nOnfanananf

=³£

==

³£

£

£=

=+++=

å

å

å

åå

=

=

=

-

==

Page 44: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

AsymptoticAnalysis

• Givenanytwofunctionsf(n) andg(n),wewillrestrictourselvesto:– polynomialswithpositiveleadingcoefficient– exponentialandlogarithmicfunctions

• Thesefunctions as• Wewillconsiderthelimitoftheratio:

)g()f(limnn

n ¥®

¥®n¥®

Page 45: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

AsymptoticAnalysis

• Ifthetwofunctionf(n) andg(n) describetheruntimesoftwoalgorithms,and

thatis,thelimitisaconstant,thenwecanalwaysrunthesloweralgorithmonafastercomputertogetsimilarresults

¥<<¥® )g(

)f(lim0nn

n

Page 46: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

AsymptoticAnalysis

• Toformallydescribeequivalentruntimes,wewillsaythatf(n) = Q(g(n)) if

• Note:thisisnotequality– itwouldhavebeenbetterifitsaidf(n) ∈ Q(g(n)) however,someonepicked=

¥<<¥® )g(

)f(lim0nn

n

Page 47: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

AsymptoticAnalysis

• Wearealsointerestedifonealgorithmrunseitherasymptoticallyslowerorfasterthananother

• Ifthisistrue,wewillsaythatf(n) = O(g(n))

¥<¥® )g(

)f(limnn

n

Page 48: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

AsymptoticAnalysis

• Ifthelimitiszero,i.e.,

thenwewillsaythatf(n) = o(g(n)) t• Thisisthecaseiff(n) andg(n) arepolynomialswheref hasa

lowerdegree

0)g()f(lim =

¥® nn

n

Page 49: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

AsymptoticAnalysis

• Tosummarize:

0)g()f(lim >

¥® nn

n

))(g()f( nn O=

))(g()f( nn Θ=

))(g()f( nn Ω=

¥<¥® )g(

)f(limnn

n

¥<<¥® )g(

)f(lim0nn

n

Page 50: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

AsymptoticAnalysis

• Wehaveonefinalcase:

¥=¥® )g(

)f(limnn

n

))(g()f( nn o=

))(g()f( nn Θ=

))(g()f( nn ω=

0)g()f(lim =

¥® nn

n

¥<<¥® )g(

)f(lim0nn

n

Page 51: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

AsymptoticAnalysis

• Graphically,wecansummarizetheseasfollows:

Wesay

if

Page 52: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

AsymptoticAnalysis

• Allofn2 100000 n2 – 4 n + 19 n2 + 1000000323 n2 – 4 n ln(n) + 43 n + 10 42n2 + 32

n2 + 61 n ln2(n) + 7n + 14 ln3(n) + ln(n)arebig-Q ofeachother

• E.g.,42n2 + 32 = Q( 323 n2 – 4 n ln(n) + 43 n + 10 )

Page 53: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

AsymptoticAnalysis• Wewillfocusonthese

Q(1) constantQ(ln(n)) logarithmicQ(n) linearQ(n ln(n)) “n–log–n”Q(n2) quadraticQ(n3) cubic2n, en, 4n, ... exponential

O(1)<O(logn)<O(n)<O(nlogn)<O(n2)<O(n3)<O(2n)<O(n!)

Page 54: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Growthoffunctions

• SeeChapter“GrowthofFunctions” (CLRS)

Page 55: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 56: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 57: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 58: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 59: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 60: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Logarithms

• Algebracheatsheet:http://tutorial.math.lamar.edu/pdf/Algebra_Cheat_Sheet.pdf

Page 61: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Changeofbasea→b

xb

x aa

b loglog1log =

)(loglog xx ab Q=

Page 62: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Big-Ohnotationclasses

Class Informal Intuition Analogy

f(n)∈ ο (g(n)) fisdominatedbyg Strictlybelow <f(n)∈O(g(n)) Boundedfromabove Upperbound ≤f(n)∈Θ(g(n)) Boundedfrom

aboveand below“equalto” =

f(n)∈Ω(g(n)) Boundedfrombelow Lowerbound ≥f(n)∈ω(g(n)) fdominatesg Strictlyabove >

Page 63: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

FamilyofBachmann–Landaunotations

!

Notation Name Intuition As , eventually... Definition

Big Omicron; Big O; Big Oh

f is bounded above by g(up to constant factor) asymptotically

for some k

or

(Note that, since the beginning of the 20th century, papers in number theory have been increasingly and widely using this notation in the weaker sense that f = o(g) is false)

Big Omega

f is bounded below by g(up to constant factor) asymptotically

for some positivek

Big Theta f is bounded both above and below bygasymptotically for some positive k1, k2

Small Omicron; Small O; Small Oh

f is dominated by gasymptotically for every ε

Small Omega f dominates gasymptotically for every k

on the order of; "twiddles"

f is equal to gasymptotically

Page 64: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 65: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 66: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 67: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

googol

Page 68: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Howmuchtimedoessortingtake?

• Comparison-basedsort:A[i]<=A[j]

– Upperbound– currentbest-knownalgorithm– Lowerbound – theoretical“atleast”estimate– Iftheyareequal,wehavetheoreticallyoptimalsolution

Page 69: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Simplesort

for i=2..nfor j=i ;j>1;j--

if A[j]<A[j-1]then swap(A[j],A[j-1])else nexti

Page 70: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Thedivide-and-conquerdesignparadigm

1. Divide theproblem(instance)intosubproblems.

2. Conquer thesubproblems bysolvingthemrecursively.

3. Combine subproblem solutions.

Page 71: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Mergesort

Merge-Sort(A,p,r)if p<r then q=(p+r)/2

Merge-Sort( A,p,q)Merge-Sort( A,q+1,r)Merge( A,p,q,r)

It was invented by John von Neumann in 1945.

Page 72: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Example

• Applyingthemergesortalgorithm:

Page 73: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Wikipedia/viz.Va

lue

Pos in array

Page 74: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 75: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Quicksort

Page 76: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations
Page 77: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Wikipedia/“video”

Page 78: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

Conclusions

• Algorithmcomplexitydealswiththebehaviorinthelong-term– worstcase -- typical– averagecase -- quitehard– bestcase -- bogus,“cheating”

• Inpractice,long-termsometimesnotnecessary– E.g.forsorting20elements,youdon’tneedfancyalgorithms…

Page 79: Advanced Algorithmics (6EAP) - ut · Advanced Algorithmics (6EAP) MTAT.03.238 Order of growth… maths Jaak Vilo 2016 fall ... • Time: count nr of elementary calculations/operations

n2.3727

n2.376