Docsity

Lecture Notes on Algorithm Analysis | CS 3653, Study notes of Computer Science

Toggle sidebar
bg1
CS 3653 Fall 2002 Lecture 11-1 T. Menhaj
Lecture 11
Algorithms (cont’d)
Algorithm Analysis
From the last example we learned that we have to be
able to analyze the properties of an algorithm.
Main questions:
1) How to measure its performance?
how fast does it perform? (Number of iterations; in
each interation, number of instructions required to
execute the algorithm. a.k.a. Time complexity)
2) How much storage (space) does the algorithm
require (Space complexity)?
Analysis of an algorithm is a mechanism to derive
estimates for the computer time and storage needed to run
the algorithm. The actual amount of time and storage is
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Lecture Notes on Algorithm Analysis | CS 3653 and more Study notes Computer Science in PDF only on Docsity!

Lecture 11

Algorithms (cont’d)

Algorithm Analysis

From the last example we learned that we have to be able to analyze the properties of an algorithm.

Main questions:

  1. How to measure its performance? how fast does it perform? (Number of iterations; in each interation, number of instructions required to execute the algorithm. a.k.a. Time complexity)

  2. How much storage (space) does the algorithm require (Space complexity)?

Analysis of an algorithm is a mechanism to derive estimates for the computer time and storage needed to run the algorithm. The actual amount of time and storage is

referred to the complexity of an algorithm.

Back to the previous example:

Algorithm Poly:

Assume that T (^) a =time for execution of an addition T (^) m =time for execution of a multiplication.

Total time required to execute the polynomial evaluation for an input of size n becomes:

T (^) poly=(n+1)T (^) a+(n(n+1)/2)Tm

T (^) poly1=n*T (^) a+n*T (^) m = n(T (^) a+T (^) m )

Now let n=

T (^) poly=(101)T (^) a+50(101)T (^) m =101(T (^) a +50T (^) m )

T (^) poly(n)=n+1+.5n^2+.5n

T (^) poly(10)=11+50+5=

T (^) poly(100)=101+5000+50=

T (^) poly(500)~=.5(500)^

T (^) poly(n) ~=.5n^2 as n increases

It is said that T (^) poly(n) is of order n^

Clarification:

( ) (^ )

2 2 2 2 2 (^22)

0.5 1.5 1 0.5 1.5 3 , 1 poly^3 lim n poly^3.

n n n n n n n T n n T^ n const →∞ n

    • ≤ + + ≤ ∀ ≥ ⇒ ≤ ⇒ = =

You can easily show that T (^) poly1(n) is of order n.

Definition: Let g and h functions on `^ +. We call g(n) is of

order at most h(n) or g^ (^ n^ )^ =^ ϑ(^ h n (^ )), if ther are a positive

constant C and M^ ∈^ `^ + so that

g n ( ) ≤ C h n ( ) , ∀ n ≥ M

i.e., the above inequlity does not hold for finitely many positive integers n.

It is further said that g(n) is of order at least h(n) or

g ( n ) = Ω ( h n ( )) if there are a positive constant D and N ∈ `+

so that g n (^^ )^ ≥^ D h n (^^ )^ , ∀ n^^ ≥^ N.

Note: g(n) is of order h(n) or g^ (^ n )^^ = Θ^ (^ h n (^ )), if g^ (^ n^ )^ =^ ϑ(^ h n (^ ))

and at the same time g^ (^ n^ )^ = Ω^ (^ h n (^ )).

Bach to polynomial problem: Based on the above definitions and knowing that

Tpoly ( n ) ≤ 3 n^2 , ∀ n ∈ `+ ⇔ Tpoly ( n ) = ϑ( n^2 )

Obviously,

Tpoly ( n ) ≥ 0.5 n^2 , ∀ n ∈ `+ ⇔ Tpoly ( n ) = Ω( n^2 )

Example: Consider the following algorithm.

Input n Function m=fpt(n) m= s=’true’ while s p=n/ if p==floor(p), then m=m+ n=p else s=’false’ endif endwhile endfunction

What is this algorithm doing? We can easily see that for n=3, 9, 27, and 54 the output m

become 1, 2, 3 and 3. So we may conclude that the algorithm finds the the maximum power of 3 that divides the input n.

Example: Assume that n is randomly picked up from the interval [0, 19683] and the only time consuming step is division, we may find :

The best case is 1 if 3 does not divides n.

The worst case is 10, since n=19683=3^9.

The average case is

9 2 9 1

19683- 19683 2 19683 19683 1 3 3 3 19683 19683 19683 (^3) 1. 19683

i^ i

i i

= i^ i

=

  (^) +   (^) +   + −      

  • ^  = ^ =

Best-Case: 1 comparision if v<=w 1

Worst-Case: n comparisions; v>wn-

Average-case: First assume that v is on the list. Because the chance of v equals any of wi ’s is the same, the number of computations

=

( ) 1

n i

i n n^ n
n = n
= +^ = +

∑ ⋅

In general you may assume that the probability (see lecture

5) that v equals wi , prob v (^^ =^ wi^ )=^ pi and prob v (^^ ≠^ wi^ )=^ q , from

lecture 5 we know that 1 1.

n

∑ i = pi^ +^ q =

Obviously : v < w 1 (^) , w 1 (^) ≤ v < w 2 (^) , w 2 (^) ≤ v < w 3 (^) , …, wn (^) − 1 ≤ v < wn , wn ≤ v

Using the “total ignorance interpretation of probabity”,

they each should have the same probability (^) n q + 1.

The number of computations =

1 1 1 (^ )

3 1 1 2 1

n n n

∑ i = p ii +^ ∑ i = n q +^ i^ +^ n q + n^ =^ ∑ i = p ii^ + qn nn ++

With the assumption of each v=wi is equally likely, then

1

n i (^1) i (^1). i

p q p q = n ∑ +^ =^ ⇒^ = −

and the average becomes

( 1 −^ q^ ) n^ 2 + 1 +^ q^ n n 2 ((^ n^ ++ (^31) ) ) =^ n^ 2 + 1 + q (^2) ( n n −+^1 1 ).

Check: if v is on the list, q=0 and the above average becomes (n+1)/2 as we obtained before.