Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf ·...

37
Analysis of Algorithms Analysis of Algorithms 1. Asymptotic Notations 2 Analysis of simple algorithms 2. Analysis of simple algorithms

Transcript of Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf ·...

Page 1: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of AlgorithmsAnalysis of Algorithms

1. Asymptotic Notations2 Analysis of simple algorithms2. Analysis of simple algorithms

Page 2: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 2

Learning outcomesLearning outcomesYou should be able to:You should be able to:

Describe asymptotic notations: O Ω and ΘDescribe asymptotic notations: O, Ω , and Θ

Analyze the time complexity of algorithms

Page 3: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 3

IntroductionIntroductionWhat is Algorithm?What is Algorithm?

a clearly specified set of simple instructions to be followed to solve a problem

Takes a set of values as input andTakes a set of values, as input and produces a value, or set of values, as output

May be specified I E li hIn EnglishAs a computer programAs a pseudo-code

Data structuresMethods of organizing data

Program = algorithms + data structuresProgram = algorithms + data structures

Page 4: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 4

IntroductionIntroductionWhy need algorithm analysis ?Why need algorithm analysis ?

writing a working program is not good enoughThe program may be inefficient!The program may be inefficient!If the program is run on a large data set, then the running time becomes an issue

Page 5: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 5

Example: Selection ProblemExample: Selection ProblemGiven a list of N numbers determine the kthGiven a list of N numbers, determine the kth largest, where k ≤ N.Algorithm 1:Algorithm 1:(1) Read N numbers into an array(2) Sort the array in decreasing order by some ( ) y g y

simple algorithm(3) Return the element in position k

Page 6: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 6

Example: Selection ProblemExample: Selection Problem…Algorithm 2:Algorithm 2:(1) Read the first k elements into an array and sort

them in decreasing orderg(2) Each remaining element is read one by one

If smaller than the kth element, then it is ignoredOth i it i l d i it t t i thOtherwise, it is placed in its correct spot in the array, bumping one element out of the array.

(3) The element in the kth position is returned as the answer.

Page 7: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 7

Example: Selection ProblemExample: Selection Problem…Which algorithm is better whenWhich algorithm is better when

N =100 and k = 100?N =100 and k = 1?N 100 and k 1?

What happens when N = 1,000,000 and k = 500,000?,There exist better algorithms

Page 8: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 8

Algorithm AnalysisAlgorithm AnalysisWe only analyze correct algorithmsWe only analyze correct algorithmsAn algorithm is correct

If, for every input instance, it halts with the correct output

Incorrect algorithmsMight not halt at all on some input instancesMight halt with other than the desired answerMight halt with other than the desired answer

Analyzing an algorithmPredicting the resources that the algorithm requiresResources include

MemoryCommunication bandwidthComputational time (usually most important)

Page 9: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 9

Algorithm AnalysisAlgorithm Analysis…Factors affecting the running timeFactors affecting the running time

computer compileralgorithm usedalgorithm usedinput to the algorithm

The content of the input affects the running timetypically the input size (number of items in the input) is the maintypically, the input size (number of items in the input) is the main consideration

E.g. sorting problem ⇒ the number of items to be sortedE.g. multiply two matrices together ⇒ the total number of

l t i th t t ielements in the two matrices

Machine model assumedInstructions are executed one after another, with no

t ti N t ll l tconcurrent operations ⇒ Not parallel computers

Page 10: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 10

ExampleExampleCalculate ∑

N

i3Calculate ∑=i

i1

1 1

2

3

4

2N+2

4N

1

Lines 1 and 4 count for one unit eachLine 3: executed N times, each time four unitsLine 2: (1 for initialization, N+1 for all the tests, N for all the increments) total 2N + 2all the increments) total 2N + 2total cost: 6N + 4 ⇒ O(N)

Page 11: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 11

Worst / average / best caseWorst- / average- / best-caseWorst-case running time of an algorithmWorst case running time of an algorithm

The longest running time for any input of size nAn upper bound on the running time for any input⇒ guarantee that the algorithm will never take longer⇒ guarantee that the algorithm will never take longerExample: Sort a set of numbers in increasing order; and the data is in decreasing orderThe worst case can occur fairly oftenThe worst case can occur fairly often

E.g. in searching a database for a particular piece of information

Best-case running timesort a set of numbers in increasing order; and the data issort a set of numbers in increasing order; and the data is already in increasing order

Average-case running timeMay be difficult to define what “average” meansMay be difficult to define what average means

Page 12: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 12

Running time of algorithmsRunning-time of algorithmsBounds are for the algorithms rather thanBounds are for the algorithms, rather than programs

programs are just implementations of an algorithm,programs are just implementations of an algorithm, and almost always the details of the program do not affect the bounds

Bounds are for algorithms, rather thanblproblems

A problem can be solved with several algorithms, some are more efficient than otherssome are more efficient than others

Page 13: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 13

Growth RateGrowth Rate

The idea is to establish a relative order among functions for large nfor large n∃ c , n0 > 0 such that f(N) ≤ c g(N) when N ≥ n0f(N) grows no faster than g(N) for “large” N

Page 14: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 14

Asymptotic notation: Big OhAsymptotic notation: Big-Ohf(N) = O(g(N))f(N) = O(g(N))There are positive constants c and n0 such thatthat

f(N) ≤ c g(N) when N ≥ n0

The growth rate of f(N) is less than or equal tothe growth rate of g(N)g(N) is an upper bound on f(N)

Page 15: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 15

Big Oh: exampleBig-Oh: exampleLet f(N) = 2N2 ThenLet f(N) = 2N2. Then

f(N) = O(N4)f(N) = O(N3)f(N) O(N )f(N) = O(N2) (best answer, asymptotically tight)

O(N2): reads “order N-squared” or “Big-Oh N-squared”

Page 16: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 16

Big Oh: more examplesBig Oh: more examplesN2 / 2 – 3N = O(N2)N / 2 3N O(N )1 + 4N = O(N)7N2 + 10N + 3 = O(N2) = O(N3)log N log N / log 10 O(log N) O(log N)log10 N = log2 N / log2 10 = O(log2 N) = O(log N)sin N = O(1); 10 = O(1), 1010 = O(1)

)( 2NONNiN=⋅≤∑

)( 321

2 NONNiN

i=⋅≤∑ =

)(1

NONNii

=⋅≤∑ =

log N + N = O(N)logk N = O(N) for any constant kN = O(2N) but 2N is not O(N)N = O(2N), but 2N is not O(N)210N is not O(2N)

Page 17: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 17

Math Re ie : logarithmic f nctionsMath Review: logarithmic functionsabiffbxa log ==

bbaab

abiffbx x

loglogloglog

log+=

abb

bm

ma log

loglog =

naaba

an

b loglogloglog =

=

xdaaa bbb

1loglog)(loglog ≠=

xdxxd e 1log=

Page 18: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 18

Some rulesSome rulesWhen considering the growth rate of a function usingWhen considering the growth rate of a function using Big-OhIgnore the lower order terms and the coefficients of gthe highest-order termNo need to specify the base of logarithm

Ch i th b f t t t th h thChanging the base from one constant to another changes the value of the logarithm by only a constant factor

If T1(N) = O(f(N) and T2(N) = O(g(N)), thenT1(N) + T2(N) = max(O(f(N)), O(g(N))),T1(N) * T2(N) = O(f(N) * g(N))

Page 19: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 19

Big OmegaBig-Omega

∃ c , n0 > 0 such that f(N) ≥ c g(N) when N ≥ n0

f(N) grows no slower than g(N) for “large” N

Page 20: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 20

Big OmegaBig-Omega

f(N) = Ω(g(N))There are positive constants c and n suchThere are positive constants c and n0 such that

f(N) ≥ c g(N) when N ≥ nf(N) ≥ c g(N) when N ≥ n0

The growth rate of f(N) is greater than or g ( ) gequal to the growth rate of g(N).

Page 21: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 21

Big Omega: examplesBig-Omega: examplesLet f(N) = 2N2 ThenLet f(N) = 2N2. Then

f(N) = Ω(N)f(N) = Ω(N2) (best answer)f(N) Ω(N ) (best answer)

Page 22: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 22

f(N) = Θ(g(N))f(N) = Θ(g(N))

the growth rate of f(N) is the same as the growth rate f (N)of g(N)

Page 23: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 23

Big ThetaBig-Theta

f(N) = Θ(g(N)) iff f(N) = O(g(N)) and f(N) = Ω(g(N))f(N) O(g(N)) and f(N) Ω(g(N))The growth rate of f(N) equals the growth rate of g(N)Example: Let f(N)=N2 , g(N)=2N2

Since f(N) = O(g(N)) and f(N) = Ω(g(N)), th f(N) ( (N))thus f(N) = Θ(g(N)).

Big-Theta means the bound is the tightest possiblepossible.

Page 24: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 24

Some rulesSome rules

If T(N) is a polynomial of degree k, then T(N) = Θ(Nk)T(N) = Θ(N ).

For logarithmic functionsFor logarithmic functions,T(logm N) = Θ(log N).

Page 25: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 25

Typical Growth Rates

Page 26: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 26

Growth ratesGrowth rates …Doubling the input sizeDoubling the input size

f(N) = c ⇒ f(2N) = f(N) = cf(N) = log N ⇒ f(2N) = f(N) + log 2f(N) = N ⇒ f(2N) = 2 f(N) f(N) = N2 ⇒ f(2N) = 4 f(N) f(N) = N3 ⇒ f(2N) = 8 f(N)f(N) = N3 ⇒ f(2N) = 8 f(N) f(N) = 2N ⇒ f(2N) = f2(N)

Advantages of algorithm analysisAdvantages of algorithm analysis To eliminate bad algorithms earlypinpoints the bottlenecks, which are worth coding

f llcarefully

Page 27: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 27

Using L' Hopital's ruleUsing L Hopital s ruleL' Hopital's ruleL Hopital s rule

If and

then)(lim Nf )(lim Nf ′

∞=∞→

)(lim Nfn

∞=∞→

)(lim Ngn

then =

Determine the relative growth rates (using L' Hopital's rule if

)(lim

Ngn ∞→ )(lim

Ngn ′∞→

necessary)compute

)()(lim

NgNf

n ∞→

if 0: f(N) = O(g(N)) and f(N) is not Θ(g(N))if constant ≠ 0: f(N) = Θ(g(N))if ∞: f(N) = Ω(f(N)) and f(N) is not Θ(g(N))if ∞: f(N) = Ω(f(N)) and f(N) is not Θ(g(N))limit oscillates: no relation

Page 28: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 28

General RulesGeneral RulesFor loops

at most the running time of the statements inside the for-loop (including tests) times the number of iterations.iterations.

Nested for loops

the running time of the statement multiplied by the product of the sizes of all the for-loops.O(N2)

Page 29: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 29

General rules (cont’d)General rules (cont d)Consecutive statementsConsecutive statements

These just addO(N) + O(N2) = O(N2)O(N) + O(N ) O(N )

If S1Else S2

never more than the running time of the test plus the larger of the running times of S1 and S2.

Page 30: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 30

Another ExampleAnother ExampleMaximum Subsequence Sum ProblemMaximum Subsequence Sum ProblemGiven (possibly negative) integers A1, A2, ...., A find the maximum value of ∑

j

AAn, find the maximum value of

For convenience the maximum subsequence sum

∑=ik

kA

For convenience, the maximum subsequence sum is 0 if all the integers are negative

E.g. for input –2, 11, -4, 13, -5, -2Answer: 20 (A2 through A4)

Page 31: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 31

Algorithm 1: SimpleAlgorithm 1: SimpleExhaustively tries all possibilities (brute force)Exhaustively tries all possibilities (brute force)

O(N3)

Page 32: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 32

Algorithm 2: Divide and conquerAlgorithm 2: Divide-and-conquer Divide-and-conquerDivide and conquer

split the problem into two roughly equal subproblems, which are then solved recursivelypatch together the two solutions of the subproblems to arrivepatch together the two solutions of the subproblems to arrive at a solution for the whole problem

The maximum subsequence sum can be Entirely in the left half of the inputEntirely in the right half of the inputIt crosses the middle and is in both halves

Page 33: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 33

Algorithm 2 (cont’d)Algorithm 2 (cont d)

The first two cases can be solved recursivelyFor the last case:For the last case:

find the largest sum in the first half that includes the last element in the first halfthe largest sum in the second half that includes the firstthe largest sum in the second half that includes the first element in the second halfadd these two sums together

Page 34: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 34

Algorithm 2Algorithm 2 …

O(1)

T(m/2)

T(m/2)

O(m)

T(m/2)

O(1)

Page 35: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 35

Algorithm 2 (cont’d)Algorithm 2 (cont d)Recurrence equationRecurrence equation

T =1)1(

NNTNT += )2

(2)(

2 T(N/2): two subproblems, each of size N/2N: for “patching” two solutions to find solution toN: for patching two solutions to find solution to whole problem

Page 36: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 36

Algorithm 2 (cont’d)Algorithm 2 (cont d)Solving the recurrence: NNTNT += )

2(2)(Solving the recurrence:

NNT += 2)4

(4

)2

()(

NNT

=

+= 3)8

(8

L

With k=log N (i.e. 2k = N), we havekNNT k

k += )2

(2

Thus the running time is O(N log N)NNN

NNTNNT+=+=

loglog)1()(

Thus, the running time is O(N log N)faster than Algorithm 1 for large data sets

Page 37: Analysis of AlgorithmsAnalysis of Algorithmseinspem.upm.edu.my/wcaa/images/Algo_Analysis.pdf · Analysis of Algorithms / Slide 12 Running-time of algorithmstime of algorithms Bounds

Analysis of Algorithms / Slide 37

QuestionQuestion

??????