Beruflich Dokumente
Kultur Dokumente
Week2
AnalysisofAlgorithms
Efficiency:
Runningtime Spaceused
Efficiencyisdefinedasafunctionofthe inputsize:
DSA09
TheRAMmodel
Itisimportanttochoosethelevelofdetail. TheRAMmodel:
Datatypesintegers,characters,andfloats
M.Bhlen 3
DSA09
AnalysisofInsertionSort/1
Timetocomputetherunningtimeasa functionoftheinputsize(exactanalysis).
cost
for j := 2 to n do key := A[j] // Insert A[j] into A[1..j-1] i := j-1 while i>0 and A[i]>key do A[i+1] := A[i] i-A[i+1]:= key
DSA09 M.Bhlen
c1 c2 0 c3 c4 c5 c6 c7
times
j= 2 t j n j= 2 t j1 n j= 2 t j1
n-1
4
AnalysisofInsertionSort/2
t T(n)=c1*n+c2(n1)+c3(n1)+c4 t 1 t 1 c5+c6+c7(n1)
j
DSA09
M.Bhlen
AnalysisofInsertionSort/3
DSA09
M.Bhlen
PerformanceAnalysis
Oftenitissufficienttocountthenumberof iterationsofthecore(innermost)part.
Insomecasesthecostofselectedoperations dominatesallothercosts.
DiskI/OversusRAMoperations. Databasesystems.
M.Bhlen 7
DSA09
Best/Worst/AverageCase/1
Analyzinginsertionsorts
n j=2
t j 1
DSA09
Best/Worst/AverageCase/2
Foraspecificsizeofinputsizen,investigate runningtimesfordifferentinputinstances:
6n
running time
5n 4n 3n 2n 1n
}
A B C D E F input instance G
M.Bhlen
worst case
=4321
best case
DSA09
Best/Worst/AverageCase/3
Forinputsofallsizes:
worst-case
6n
average-case best-case
Running time
5n 4n 3n 2n 1n 1 2 3 4 5 6 7 8 9 10 11 12 ..
10
Best/Worst/AverageCase/4
Worstcaseisusuallyused:
DSA09
AnalysisofLinearSearch
INPUT: A[1..n] a sorted array of integers, q an integer. OUTPUT: j s.t. A[j]=q. NIL if j(1jn): A[j]q
10
j := 1 while j <= n and A[j] != q do j++ if j <= n then return j else return NIL
Worst case running time: n Average case running time: n/2 Best case running time: 0
M.Bhlen 12
DSA09
BinarySearch
411
DSA09
l := 1; r := n do m := (l+r)/2 if A[m] = q then return m else if A[m] > q then r := m-1 else l := m+1 while l <= r return NIL
M.Bhlen
13
AnalysisofBinarySearch
Howmanytimesistheloopexecuted?
Witheachexecutionthedifference betweenlandriscutinhalf.
DSA09
LinearvsBinarySearch
DSA09
CorrectnessofAlgorithms
DSA09
PartialandTotalCorrectness
Partialcorrectness
IFthispointisreached, THENthisisthedesiredoutput
Anylegalinput
Algorithm
Output
Totalcorrectness
INDEEDthispointisreached, ANDthisisthedesiredoutput
Anylegalinput
Algorithm
Output
DSA09
M.Bhlen
17
Assertions
E.g.,A[1],,A[j]formanincreasingsequence
DSA09
Pre/postconditions
Example:
INPUT:anarrayofintegersA[1..n],n>0 OUTPUT:(m1,m2)suchthateither
DSA09
LoopInvariants
DSA09
Example:BinarySearch/1
Wewanttoshowthatqis l := 1; r := n; notinAifNILisreturned. do m := (l+r)/2 Invariant: if A[m]=q then return m i[1..l1]:A[i]<q(Ia) else if A[m]>q then r := else l := m+1 i[r+1..n]:A[i]>q(Ib) while l <= r Initialization:l=1,r=n return NIL theinvariantholdsbecause therearenoelementstotheleftoflortotherightofr. l=1yieldsj,i[1..0]:A[i]<q thisholdsbecause[1..0]isempty r=nyieldsj,i[n+1..n]:A[i]>q thisholdsbecause[n+1..n]isempty
DSA09 M.Bhlen
m-1
21
Example:BinarySearch/2
l := 1; r := n; do m := (l+r)/2 if A[m]=q then return m else if A[m]>q then r := m-1 else l := m+1 while l <= r return NIL
DSA09
Example:BinarySearch/3
116
l := 1; r := n; do m := (l+r)/2 if A[m]=q then return m else if A[m]>q then r := m-1 else l := m+1 while l <= r return NIL
l:=m+1weget(l+r)/2+1>l r:=m1weget(l+r)/21<r
Therangegetssmallerduringeachiterationandtheloop willterminatewhenl<=rnolongerholds.
M.Bhlen 23
DSA09
Example:InsertionSort/1
Invariant: outsidewhileloop
A[1...j1]issorted A[1...j1]Aorig
insidewhileloop:
for j := 2 to n do key := A[j] i := j-1 while i>0 and A[i]>key do A[i+1] := A[i] i-A[i+1] := key
DSA09
Example:InsertionSort/2
outsidewhileloop
insidewhileloop:
for j := 2 to n do key := A[j] i := j-1 while i>0 and A[i]>key do A[i+1] := A[i] i-A[i+1] := key
Initialization:
DSA09
Example:InsertionSort/3
outsidewhileloop
insidewhileloop:
for j := 2 to n do key := A[j] i := j-1 while i>0 and A[i]>key do A[i+1] := A[i] i-A[i+1] := key
Maintenance:
DSA09
Example:InsertionSort/4
outsidewhileloop
insidewhileloop:
for j := 2 to n do key := A[j] i := j-1 while i>0 and A[i]>key do A[i+1] := A[i] i-A[i+1] := key
Termination:
DSA09
AsymptoticAnalysis
Asymptoticallymoreefficientalgorithmsarebest forallbutsmallinputs
M.Bhlen 28
DSA09
AsymptoticNotation/1
ThebigOh ONotation
Usedforworstcase analysis
c g ( n)
Running Time
f n
n0
Input Size
DSA09
M.Bhlen
29
AsymptoticNotation/2
ThebigOmega Notation
Running Time
f n
c g ( n)
n0
Input Size
DSA09
M.Bhlen
30
AsymptoticNotation/3
SimpleRule:Droplowerorderterms andconstantfactors.
DSA09
AsymptoticNotation/4
ThebigTheta Notation
c 2g n
Running Time
f n
c1 n g
n0
Input Size
DSA09
32
AsymptoticNotation/5
Twomoreasymptoticnotations
"LittleOh"notationf(n)=o(g(n)) nontightanalogueofBigOh
DSA09
AsymptoticNotation/6
10
Analogywithrealnumbers
f f f= f< f>
g g g g g
DSA09
ComparisonofRunningTimes
Determiningthemaximalproblemsize.
RunningTime 1second T(n)ins 400n 20nlogn 2n2 n4 2n 2500 4096 707 31 19 1minute 150000 166666 5477 88 25
M.Bhlen
DSA09
SpecialCaseAnalysis
DSA09
SpecialCases
DSA09
Sortedness/1
Thefollowingalgorithmcheckswhether anarrayissorted.
INPUT: A[1..n] an array integers. OUTPUT: TRUE if A is sorted; FALSE otherwise for i := 1 to n if A[i] A[i+1] then return FALSE return TRUE
Analyzethealgorithmbyconsidering specialcases.
M.Bhlen 38
DSA09
Sortedness/2
INPUT: A[1..n] an array integers. OUTPUT: TRUE if A is sorted; FALSE otherwise
12
for i := 1 to n-1 if A[i] > A[i+1] then return FALSE return TRUE
DSA09
BinarySearch,Variant1
Analyzethefollowingalgorithmbyconsidering specialcases.
l := 1; r := n do m := (l+r)/2 if A[m] = q then return m else if A[m] > q then r := m-1 else l := m+1 while l < r return NIL
DSA09
M.Bhlen
40
BinarySearch,Variant2
Analyzethefollowingalgorithmbyconsidering specialcases.
l := 1; r := n while l < r do { m := (l+r)/2 if A[m] <= q then l := m+1 else r := m } if A[l-1] = q then return q else return NIL
DSA09
M.Bhlen
41
BinarySearch,Variant3
Analyzethefollowingalgorithmbyconsidering specialcases.
l := 1; r := n while l <= r do m := (l+r)/2 if A[m] <= q then l := m+1 else r := m if A[l-1] = q then return q else return NIL
DSA09
M.Bhlen
42
AQuickMathRefresher
Arithmeticprogression
n
n(1 + n) i = 1 + 2 + 3 + ... + n = 2 i= 0
Geometricprogression
givenanintegern0andarealnumber0<a 1
1 an +1 ai = 1 + a + a 2 + ... + a n = 1 a i= 0
geometricprogressionsexhibitexponential growth
M.Bhlen 43
DSA09
Miscellaneous/1
Manipulatingsummations:
c a j=c a j j j
j a j b j = j a j j b j
DSA09
M.Bhlen
44
Miscellaneous/2
Manipulatinglogarithmsandpowers:
a
logb=logb/loga
mn
m n
aman=am+n
DSA09
Summations
Therunningtimeofinsertionsortis determinedbyanestedloop.
for j := 2 to n key := A[j] i := j-1 while i>0 and A[i]>key A[i+1] := A[i] i := i-1 A[i+1] := key
DSA09
ProofbyInduction/1
Basis
1(1 + 1) S (1) = i = 2 i= 0
1
DSA09
M.Bhlen
47
ProofbyInduction/2
InductiveStep
S ( k ) = i =
i =0 n k
k ( k +1) for 1 k n 1 2
n 1
S ( n) = i =i + n = (n 1) + n = S
i =0 i =0
( n 1 +1) ( n 2 n + 2 n) = (n 1) +n = = 2 2 n( n +1) = 2
DSA09 M.Bhlen 48
Summary
Algorithmiccomplexity Correctnessofalgorithms
Asymptoticanalysis
DSA09
Specialcaseanalysis
NextWeek
DSA09
M.Bhlen
50