






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







From the last example we learned that we have to be able to analyze the properties of an algorithm.
Main questions:
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)
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
constant C and M^ ∈^ `^ + so that
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
Bach to polynomial problem: Based on the above definitions and knowing that
Obviously,
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
∑ ⋅
In general you may assume that the probability (see lecture
lecture 5 we know that 1 1.
n
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 =
3 1 1 2 1
n n n
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.