+ All Categories
Home > Documents > Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to...

Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to...

Date post: 17-Jan-2016
Category:
Upload: carol-cain
View: 224 times
Download: 3 times
Share this document with a friend
60
Complexity Analysis: Asymptotic Analysis
Transcript
Page 1: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Complexity Analysis:Asymptotic Analysis

Page 2: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Recall

• What is the measurement of algorithm?• How to compare two algorithms?• Definition of Asymptotic Notation

Page 3: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Today Topic

• Finding the asymptotic upper bound of the algorithm

Page 4: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

WAIT…Did we miss something?

The resource function!

Page 5: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Resource Function

Time Function of an

algorithm A

SpaceFunction of an

algorithm A

Size of input

Size of input

Time used

Space used

We don’t know, yet

And this one

Page 6: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

From Experiment

• We count the number of instructions executed

• Only count some instruction– One that’s promising

Page 7: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Why instruction count?

• Does instruction count == time?– Probably– But not always

– But… • Usually, there is a strong relation between

instruction count and time if we count the most occurred instruction

Page 8: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Why count just some?

• Remember the findMaxCount()?– Why we count just the max assignment?– Why don’t we count everything?

• What if, each max assignment takes N instruction– That starts to make sense

Page 9: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Time Function = instruction count

• Time Function = instruction count• Time Function = instruction count• Time Function = instruction count

Page 10: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

COMPUTE O()

Page 11: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Interesting Topics of Upper Bound

• Rule of thumb!• We neglect– Lower order terms from addition• E.g. n3+n2 = O(n3)

– Constant• E.g. 3n3 = O(n3)

Remember that we use = instead of (more correctly)

Page 12: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Why Discard Constant?

• From the definition– We can scale by adjusting the constant c

• E.g. 3n = O(n)– Because• When we let c >= 3, the condition is satisfied

Page 13: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Why Discard Lower Order Term?

• Consider– f(n) = n3+n2

– g(n) = n3

• If f(n) = O(g(n))– Then, for some c and n0

• c * g(n)-f(n) > 0• Definitely, just use any c >1

Page 14: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Why Discard Lower Order Term?

• Try c = 1.1

• Does 0.1n3-n2 > 0 ? • It is when – 0.1n > 1– E.g., n > 10

1.1 * g(n)-f(n) = 0.1n3-n2

0.1n3-n2 > 00.1n3 > n2

0.1n3/n2 > 10.1n > 1

Page 15: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Lower Order only?

• In fact,– It’s only the dominant term that count

• Which one is dominating term?– The one that grows faster

• Why?– Eventually, it is g*(n)/f*(n)

• If g(n) grows faster, – g(n)/f*(n) > some constant– E.g, lim g(n)/f*(n) infinity

The non-dominant term

The dominant term

Page 16: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

What dominating what?

na nb (when a > b)

n log n n

n2 log n n log2 n

cn nc

Log n 1

n log n

Left side dominates

Page 17: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Putting into Practice

• What is the asymptotic class of

– 0.5n3+n4-5(n-3)(n-5)+n3log8n+25+n1.5

– (n-5)(n2+3)+log(n20)

– 20n5+58n4+15n3.2*3n2

O(n4)

O(n3)

O(n5.2)

Page 18: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Asymptotic Notation from Program Flow

• Sequence• Conditions• Loops• Recursive Call

Page 19: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Sequence

Block A

Block B

f (n)

g (n)

f(n) + g(n) =

O( max(f(n),g(n)) )

Page 20: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example

Block A

Block B

O(n)

O(n2)

O(n2)

f(n) + g(n) = O(max (f(n),g(n))

Page 21: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example

Block A

Block B

Θ(n)

O(n2)

O(n2)

f(n) + g(n) = O(max (f(n),g(n))

Page 22: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example

Block A

Block B

Θ(n)

Θ(n2)

Θ(n2)

f(n) + g(n) = O(max (f(n),g(n))

Page 23: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example

Block A

Block B

O(n2)

Θ(n2)

Θ(n2)

f(n) + g(n) = O(max (f(n),g(n))

Page 24: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Condition

Block A Block B

f (n) g (n)

O( max (f(n),g(n)) )

Page 25: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Loops

for (i = 1;i <= n;i++) { P(i)}

Let P(i) takes time ti

n

iit

1

Page 26: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example

for (i = 1;i <= n;i++) { sum += i;}

sum += i Θ(1)

)()1(1

nn

i

Page 27: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Why don’t we use max(ti)?

• Because the number of terms is not constant

for (i = 1;i <= n;i++) { sum += i;}

for (i = 1;i <= 100000;i++) { sum += i;}

Θ(n)

Θ(1)With large constant

Page 28: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example

for (j = 1;j <= n;j++) { for (i = 1;i <= n;i++) { sum += i; }}

sum += i Θ(1)

)(

)(...)()(

)()1(

2

11 1

n

nnn

nn

j

n

j

n

i

Page 29: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example

for (j = 1;j <= n;j++) { for (i = 1;i <= j;i++) { sum += i; }}

sum += i Θ(1)

n

j

n

j

n

j

n

j

j

i

jc

cj

j

1

1

11 1

)()1(

)(2

)1(

2n

nnc

Page 30: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example : Another way

for (j = 1;j <= n;j++) { for (i = 1;i <= j;i++) { sum += i; }}

sum += i Θ(1)

)(

)(

)()1(

21

11 1

nO

nO

j

n

j

n

j

n

j

j

i

Page 31: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example

for (j = 2;j <= n-1;j++) { for (i = 3;i <= j;i++) { sum += i; }}

sum += i Θ(1)

)(

)122

(

12

)1(

)2()1(

2

2

1

2

1

2

1

2 3

n

nn

nn

j

j

n

j

n

j

n

j

j

i

Your turn

Page 32: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example : While loops

While (n > 0) { n = n - 1;}

(n)Θ

Page 33: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example : While loops

While (n > 0) { n = n - 10;}

(n/10) = (n)Θ Θ

Page 34: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example : While loops

While (n > 0) { n = n / 2;}

(log n)Θ

Page 35: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example : Euclid’s GCD

function gcd(a, b) { while (b > 0) { tmp = b b = a mod b a = tmp } return a}

Page 36: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example : Euclid’s GCD

function gcd(a, b) { while (b > 0) { tmp = b b = a mod b a = tmp } return a}

How many iteration?

Compute mod and swap

Until the modding one is

zero

Page 37: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example : Euclid’s GCD

a

b

Page 38: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example : Euclid’s GCD

a

b

a mod b

If a > ba mod b < a / 2

Page 39: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Case 1: b > a / 2

a

b

a mod b

Page 40: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Case 1: b ≤ a / 2

a

b

a mod bWe can always put another b

Page 41: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example : Euclid’s GCD

function gcd(a, b) { while (b > 0) { tmp = b b = a mod b a = tmp } return a}

B always reduces at least half

O( log n)

Page 42: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

RECURSIVE PROGRAM

Page 43: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Code that calls itself

void recur(int i) { // checking termination

// do something

// call itself}

Page 44: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Find summation from 1 to i

int sum(int i) { //terminating condition if (i == 1) //return 1;

// recursive call int result; result = i + sum(i-1); return result;}

void main() { printf(“%d\n”,sum()); }

Page 45: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Order of Call: Printing Example

void seq(int i) { if (i == 0) return;

printf("%d ",i); seq(i - 1);

}

void seq(int i) { if (i == 0) return;

seq(i - 1); printf("%d ",i);

}

Page 46: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Draw Triangle

void drawtri(int start,int i,int n) { if (i <= n) { for (int j = start;j <= i+start-1;j++) { printf("%d ",j); } printf("\n"); drawtri(start,i + 1,n); }}

Page 47: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Programming Recursive Function

• First, define what will the function do– Usually, it is related to “smaller” instant of

problem

• Write the function to do that in the trivial case

• Call itself to do the defined things

Page 48: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Analyzing Recursive Programming

• Use the same method, count the most occurred instruction

• Needs to consider every calls to the function

Page 49: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example

void sum(int i) { if (i == 0) return 0;

int result; result = i + sum(i - 1); return result;}

Θ(n)

Page 50: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Example 2

void sum(int i) { if (i == 0) return 0;

int result; result = i + sum(i - 1); int count = 0; for (int j = 0;j < i;j++) { count++; } return result;}

Θ(n2)

Page 51: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Theorem

– T(n)= ΣT(ain) + O(N)

– If Σai <1 then • T(n) = O(n)

T(n) = T(0.7n) + T(0.2n) + T(0.01)n + 3n = O(n)

Page 52: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Recursion

void try( n ){

if ( n <= 0 ) return 0;

for ( j = 1; j <= n ; j++) sum += j;

try (n * 0.7) try (n * 0.2)}

Page 53: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Recursion

void try( n ){

if ( n <= 0 ) return 0;

for ( j = 1; j <= n ; j++) sum += j;

try (n * 0.7) try (n * 0.2)}

terminating

process

recursion

(1Θ)

(nΘ)

T(0.7n) + T(0.2n)

T(n)

T(n) = T(0.7n) + T(0.2n) + O(n)

Page 54: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Guessing and proof by induction

• T(n) = T(0.7n) + T(0.2n) + O(n)• Guess: T(n) = O(n), T(n) ≤ cn• Proof:• Basis: obvious• Induction: – Assume T(i < n) = O(i)– T(n) ≤ 0.7cn + 0.2cn + O(n)– = 0.9cn + O(n)– = O(n) <<< dominating rule

Page 55: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Using Recursion Tree

• T(n) = 2 T(n/2) + n

n

n/2

n/4 n/4

n/2

n/4 n/4

n

2 n/2

4 n/4

Lg n

T(n) = O(n lg n)

Page 56: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Master Method

• T(n) = aT(n/b) + f (n) a ≥ 1, b > 1

• Let c = logb(a)

• f (n) = ( Ο nc - ε ) → T(n) = ( Θ nc )• f (n) = ( Θ nc ) → T(n) = ( Θ nc log n )– f (n) = ( Θ nc logkn ) → T(n) = (Θ nc logk+1n )

• f (n) = ( Ω nc + ε ) → T(n) = ( Θ f (n) )

Page 57: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Master Method : Example

• T(n) = 9T(n/3) + n• a = 9, b = 3, c = log 3 9 = 2, nc = n2

• f (n) = n = ( nΟ 2 - 0.1 )• T(n) = (nΘ c) = (nΘ 2)

The case off (n) = ( Ο nc - ε )

Page 58: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Master Method : Example

• T(n) = T(n/3) + 1• a = 1, b = 3, c = log 3 1 = 0, nc = 1

• f (n) = 1 = ( nΘ c ) = ( 1 )Θ• T(n) = (nΘ c log n) = ( log n)Θ

The case off (n) = ( Θ nc )

Page 59: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Master Method : Example

• T(n) = 3T(n/4) + n log n• a = 3, b = 4, c = log 4 3 < 0.793, nc < n0.793

• f (n) = n log n = ( nΩ 0.793 )• a f (n/b) = 3 ((n/4) log (n/4) ) ≤ (3/4) n log n = d f (n)• T(n) = ( f (n) ) = ( n log n)Θ Θ

The case off (n) = ( Ω nc + ε )

Page 60: Complexity Analysis: Asymptotic Analysis. Recall What is the measurement of algorithm? How to compare two algorithms? Definition of Asymptotic Notation.

Conclusion

• Asymptotic Bound is, in fact, very simple– Use the rule of thumbs• Discard non dominant term• Discard constant

• For recursive– Make recurrent relation• Use master method• Guessing and proof• Recursion Tree


Recommended