+ All Categories
Home > Documents > Analysis of Algorithm

Analysis of Algorithm

Date post: 15-Feb-2016
Category:
Upload: sahkyo
View: 83 times
Download: 1 times
Share this document with a friend
Description:
Analysis of Algorithm. Why Analysis?. We need to know the “behavior” of algorithms How much resource (time/space) does it use So tha t we know when to use which algorithm So that two algorithm can be compared whether which one is better, with respect to the situtation. Can we?. Sure! - PowerPoint PPT Presentation
60
Analysis of Algorithm
Transcript
Page 1: Analysis of Algorithm

Analysis of Algorithm

Page 2: Analysis of Algorithm

Why Analysis?

• We need to know the “behavior” of algorithms– How much resource (time/space) does it use

• So that we know when to use which algorithm– So that two algorithm can be compared whether

which one is better, with respect to the situtation

Page 3: Analysis of Algorithm

Can we?

• Sure!• If we have the algorithm– We can implement it– We can test it with input

• But… – Is that what we really want?– If you wish to know whether falling from floor 20 of

Eng. 4 building would kill you• Will you try?

Page 4: Analysis of Algorithm

Prediction

• We wish to know the “Behavior” of the algorithm– Without actually trying it

• Back to the suicidal example– Can you guess whether you survive jumping from 20th

floor of Eng. 4 building?– What about 15th floor?– What about 10th floor?– What about 5th floor?– What about 2nd floor?

– Why?

Page 5: Analysis of Algorithm

Modeling

• If floor_number > 3 then– Die

• Else– Survive (maybe?)

Describe behavior using some kind of model, rule, etc.

Page 6: Analysis of Algorithm

Generalization

• What about jumping from Central World’s 20th floor?

• What about jumping from Empire State’s 20th floor?

• What about jumping from Bai Yok’s 20th floor?

• Can our knowledge (our analysis of the situation) be applicable on the above questions?

Page 7: Analysis of Algorithm

Generalization

• Knowledge from some particular instances might be applicable to another instance

Page 8: Analysis of Algorithm

Analysis

• We need something that can tell us the behavior of the algorithm that is…– Useful (give us knowledge without actually doing it)– Applicable (give us knowledge for similar kind of

situation)

GeneralizationModeling

Page 9: Analysis of Algorithm

Analysis (Measurement)

• What we really care?

RESOURCE

Time(CPU power)

Space(amount of RAM)

Page 10: Analysis of Algorithm

Model

• How to describe performance of an algorithm?

Usage of Resource how well

does an algo use

resource?

Page 11: Analysis of Algorithm

Model

• Resource Function

Time Function of algorithm A

SpaceFunction of algorithm A

Input ?

Input ?

Time used

Space used

Size of input

Page 12: Analysis of Algorithm

Example

• Inserting a value into a sorted array• Input: – a sorted array A[1..N]– A number X

• Output– A sorted array A[1..N+1] which includes X

Page 13: Analysis of Algorithm

Algorithm

• Element Insertion

• Assume that X = 20– What if A = [1,2,3]? How much time?– What if A = [101,102,103]?

idx = N;while (idx >= 1 && A[idx] > X) { A[idx + 1] = A[idx]; idx--;} A[idx] = X;

Usually, resource varies according to size of input

Page 14: Analysis of Algorithm

Using the Model

Time

Size of Input

best

worst

average

Page 15: Analysis of Algorithm

Resource Function

• Give us resource by “size of input”

– Why?• Easy to compute• Applicable (usually give meaningful, fairly accurate

result without much requirement)

Page 16: Analysis of Algorithm

Conclusion

• Measurement for algorithm– By modeling and generalization– For prediction of behavior

• Measurement is functions on the size of input– With some simplification– Best, avg, worst case

Page 17: Analysis of Algorithm

ASYMPTOTIC NOTATION

Page 18: Analysis of Algorithm

Comparing two algorithms

• We have established that a “resource function” is a good choice of measurement

• The next step, answering which function is “better”

Page 19: Analysis of Algorithm

What is “better” in our sense?

• Takes less resource• Consider this which one is better?

f(x)

g(x)

Page 20: Analysis of Algorithm

Slice

f(x)

g(x)

Page 21: Analysis of Algorithm

What is “better” in our sense?

• which one is better?– Performance is now a function, not a single value– Which slice to use?

• Can we say “better” based on only one slice?• Use the slice where it’s really matter– i.e., when N is large– What is large N?

• Infinity?

– Implication?

Page 22: Analysis of Algorithm

Comparison by infinite N

• There is some problem

– Usually,

– The larger the problem, the more resource used

)(lim xfn

Page 23: Analysis of Algorithm

Separation between Abstraction and Implementation

• Rate of Growth– by changing the size of input, how does the

TIME and SPACE requirement change– Compare by how f(x) grows when x increase,

w.r.t. g(x)

)(/)( xgxf)()(lim

xgxf

n

Page 24: Analysis of Algorithm

Compare by RoG

)()(lim

xgxf

n

0 : f(x) grows “slowzer” than g(x)

∞ : f(x) grows “faster” than g(x)

else : f(x) grows “similar” to g(x)

Page 25: Analysis of Algorithm

Growth Rate Comparison

0)(/)(lim

ngnfn

• 0.5n • 1 • log n • log6 n • n0.5 • n3 • 2n • n!

Sometime it is simple

Some time it is not

Page 26: Analysis of Algorithm

l’Hôpital’s Rule

• Limit of ratio of two functions equal to limit of ratio of their derivative.

– Under specific condition

Page 27: Analysis of Algorithm

l’Hôpital’s Rule

• If

• then

Page 28: Analysis of Algorithm

The problem of this approach

• What if f(x) cannot be differentiated?

• Too complex to find derivative

Page 29: Analysis of Algorithm

Compare by Classing

• Coarse grain comparison– Another simplification

• Work (mostly) well in practice• Classing

Page 30: Analysis of Algorithm

Classing

• Simplification by classification• Grading Analogy

Score Grade

>= 80 A

70 <= x < 80 B

60 <= x < 70 C

50 <= x < 60 D

< 50 F

Page 31: Analysis of Algorithm

Compare by Classification

algo

Page 32: Analysis of Algorithm

Compare by Classification

algo

Group A

Group C

Group B

Group F

Group Dgrouping

Page 33: Analysis of Algorithm

Compare by Classification

algo

Group A

Group C

Group B

Group F

Group DDescribe “simplified” property

>= 8070 <= x < 80

60 <= x < 70

50 <= x < 60

x < 50

Page 34: Analysis of Algorithm

Compare by Classification• Group by some similar property– Select a representative of the group– Use the representative for comparison

• If we have the comparison of the representative– The rest is to do the classification

Page 35: Analysis of Algorithm

Complexity Class

• We define a set of complexity class– using rate of growth

• Here comes the so-called Asymptotic Notation– Q, O, W, o, w

• Classify by asymptotic bound

Page 36: Analysis of Algorithm

Asymptote

• Something that bounds curves

Asymptote

Curve

Page 37: Analysis of Algorithm

Asymptote

• Remember hyperbola?

Page 38: Analysis of Algorithm

O-notation

O(g(n)) = {f(n) : positive constants c and n0, such that n n0,

we have 0 f(n) cg(n) }

For function g(n), we define O(g(n)), big-O of n, as the set:

g(n) is an asymptotic upper bound for f(n).

Intuitively: Set of all functions whose rate of growth is the same as or lower than that of g(n).

n0

cg(x)

f(x)

f(x) O(g(x))

Page 39: Analysis of Algorithm

W -notation

g(n) is an asymptotic lower bound for f(n).

Intuitively: Set of all functions whose rate of growth is the same as or higher than that of g(n).

W(g(n)) = {f(n) : positive constants c and n0, such that n n0,

we have 0 cg(n) f(n)}

For function g(n), we define W(g(n)), big-Omega of n, as the set:

n0

cg(x)

f(x)

f(x) W(g(x))

Page 40: Analysis of Algorithm

Q-notation

Q(g(n)) = {f(n) : positive constants c1, c2, and n0, such that n n0,

we have 0 c1g(n) f(n) c2g(n)

}

For function g(n), we define Q(g(n)), big-Theta of n, as the set:

g(n) is an asymptotically tight bound for f(n).

Intuitively: Set of all functions thathave the same rate of growth as g(n).

n0

c1g(x)

f(x)

c2g(x)

f(x) Q(g(x))

Page 41: Analysis of Algorithm

Example• F(n) = 300n + 10 – is a member of Q(30n)– why?• let c1 = 9

• let c2 = 11

• let n = 1

Page 42: Analysis of Algorithm

Another Example• F(n) = 300n2 + 10n– is a member of Q(10n2)– why?• let c1 = 29

• let c2 = 31

• let n = 11

Page 43: Analysis of Algorithm

How to Compute?• Remove any constant– F(n) = n3+2n2 + 4n + 10• is a member of Q(n3+n2 + n)

• Remove any lower degrees– F(n) = n3+2n2 + 4n + 10• is a member of Q(n3)

Page 44: Analysis of Algorithm

Relations Between Q, W, O

• I.e., Q(g(n)) = O(g(n)) Ç W(g(n))

• In practice, asymptotically tight bounds are obtained from asymptotic upper and lower bounds.

For any two functions g(n) and f(n), f(n) = Q(g(n)) if and only if

f(n) = O(g(n)) and f(n) = W(g(n)).

Page 45: Analysis of Algorithm

Practical Usage

• We say that the program has a worst case running time of O(g(n))

• We say that the program has a best case running time of W(g(n))

• We say that the program has a tight-bound running time of Q(g(n))

Page 46: Analysis of Algorithm

Example

• Insertion sort takes O(n2) in the worst case

– Meaning: at worst, insertion sort, takes time that grows not more than quadratic of the size of the input

• Insertion sort takes W(n) in the best case

– Meaning: at best, insertion sort, takes time that grows not less than linear to the size of the input

Page 47: Analysis of Algorithm

o-notation

o(g(n)) = {f(n): c > 0, n0 > 0 such that n n0, we have 0 f(n) < cg(n)}.

For a given function g(n), the set little-oh:

Page 48: Analysis of Algorithm

ω(g(n)) = {f(n): c > 0, n0 > 0 such that n n0, we have 0 cg(n) < f(n)}.

w -notation

For a given function g(n), the set little-omega:

Page 49: Analysis of Algorithm

Remark on Notation

• An asymptotic group is a set– Hence f(n) is a member of an asymptotic group– E.g., f(n) O( n )

• Strictly speaking, f(n) = O( n ) is syntactically wrong• But we will see this a lot• It’s traditions

Page 50: Analysis of Algorithm

Comparison of Functions

f (n) O(g(n)) f (n) g(n)f (n) W(g(n)) f (n) g(n)f (n) Q(g(n)) f (n) = g(n)f (n) o(g(n)) f (n) < g(n)

f (n) w(g(n)) f (n) > g(n)

Where < , > , = means grows slower, faster, equally

Page 51: Analysis of Algorithm

Lost of Trichotomy

• Trichotomy– given two numbers a,b– Only one of the following must be true• a < b, a > b, a=b

• For our asymptotic notation– given f(n) and g(n)– it is possible that• f(n) != O(g(n)) and f(n) != W(g(n)) • e.g., n, n1+sin n

Page 52: Analysis of Algorithm

Properties• Transitivity

f(n) = Q(g(n)) & g(n) = Q(h(n)) f(n) = Q(h(n))f(n) = O(g(n)) & g(n) = O(h(n)) f(n) = O(h(n))f(n) = W(g(n)) & g(n) = W(h(n)) f(n) = W(h(n))f(n) = o (g(n)) & g(n) = o (h(n)) f(n) = o (h(n))f(n) = ω(g(n)) & g(n) = ω(h(n)) f(n) = ω(h(n))

• Reflexivityf(n) = Q(f(n))

f(n) = O(f(n)) f(n) = W(f(n))

Page 53: Analysis of Algorithm

Properties

• Symmetryf(n) = Q(g(n)) g(n) = Q(f(n))

• Complementarityf(n) = O(g(n)) g(n) = W(f(n)) f(n) = o(g(n)) g(n) = ω((f(n))

Page 54: Analysis of Algorithm

Complexity Graphs

log(n)

n

Page 55: Analysis of Algorithm

Complexity Graphs

log(n)

n

n

n log(n)

Page 56: Analysis of Algorithm

Complexity Graphs

n10

n log(n)

n3

n2

Page 57: Analysis of Algorithm

Complexity Graphs (log scale)

n10

n20nn

1.1n

2n

3n

Eventually, 1.1n will overcome n10

Page 58: Analysis of Algorithm

Common Class of Growth Rate

• constant : ( 1 )Θ• logarithmic : ( Θ log n )• polylogarithmic : ( Θ logc n ) , c ≥ 1• sublinear : ( nΘ a ) , 0 < a < 1• linear : ( Θ n )• quadratic : ( Θ n2 )• polynomial : ( nΘ c ) , c ≥ 1• exponential : ( Θ cn ) , c > 1

Page 59: Analysis of Algorithm

Logarithm

• Base of log is irrelevant– log b n = ( log c n ) / ( log c b )

– log 10 n = ( log 2 n ) / ( log 2 10 ) = ( log n )Θ• any polynomial function of n does not

matter– log n30 = 30 log n = ( log n )Θ

Page 60: Analysis of Algorithm

Conclusion

• Compare which one is better– By comparing their ratio when n approaches

infinity– By comparing their asymptotic notation of the

resource function

• Asymptotic notation– What is– Property


Recommended