ANALYSIS OF ALGORITHMS Average Case Consider Dn and an input I Dn . Let t(I) = # of basic operations performed by the algorithm on I, and p(I)= probability that input I occurs (i.e., the probability that I is created randomly). p(I) is sometimes very difficult to determine analytically so in general is determined by experience and by making simple assumptions. The average case is then A(n) p( I ).t ( I ) I Dn Example: Find x in a list of integers (A5). Again fix x and consider Dn be the set of all inputs of size n, where all entries are distinct. In this case let Dn I 1 I 2 I n I n1 Where I1= are all inputs where x appear in the first position I2= are all inputs where x appear in the second position In = are all inputs where x appear in the last position In+1= are all inputs where x does not appear x_ _ _ _ _ _ …_ _ _x_ _ _ _ _ …_ _ _ _ _ _ _ _ _ …_ x _ _ _ _ _ _ _ …_ _ The problem is that we do not know what is the probability that x appears in an input. Suppose that q is the probability that x appears (to be determined). Thus the probability p(I1)= p(I2)= p(I3)=……= p(In)=q/n, and p(In+1)=1-q. To determine t(I), we first note from looking at A5, that if x appears at the first position, we execute on basic operation, if x appears in the second position we execute 2 basic operations, if x appears on the last positions we execute n basic operations, and x does not appear we also execute n basic operations. Thus t(Ii)=i, 1 i n, and t(In+1)=n; Thus, we obtain A(n) p(I ).t ( I ) p(I )t (I ) p( I 1 1 2 )t ( I 2 ) p( I n )t ( I n ) p( I n 1 )t ( I n 1 ) I Dn n q/n i (1 q)n i 1 q n(n 1) q(n 1) q qn . (1 q)n n qn n n 2 2 2 2 Thus the average-case depends on the probability q that we will randomly obtain an input where x appears. Suppose that q=1/2 (we have the same prob. that an input where x appears than an input where x does not appear), then substituting A( n) n q qn 3 1 n 1/ 4 n / 4 n 2 2 4 4 __________________________________ __________________________________ n 1 n 3/4.n If q=1 (we will always get an input where x appears) A( n) n q qn 1 1 n 1/ 2 n / 2 n 2 2 2 2 __________________________________ 1 n 1/2.n If q=0 then (will be never obtain an input where x appears), then A(n) n, which is the worst-case scenario. Classifying functions by theirs growth rates. Consider two algorithms A and B, to solve the same problem (e.g. sorting) and we want to compare them: WA(n)=2n, and WB(n)=9n, which one is better? As we are considering the number of basic operations, we know that to obtain the total number of operations, we should multiply WA(n) by a constant c, and WB(n) by a constant c'. Since we do not know these constant, we don't know which one is more efficient. Next suppose WA(n)=n3 /2 and WB(n)=5n2 , which one is better, we know that for small inputs A is better, but for large inputs B is more efficient. Thus form the previous examples we conclude that in order to classify functions, we can ignore constants and small inputs. We can classify algorithms using "asymptotic growth rate", where small inputs and constant do not play a role. Let f(n) be a real function on the natural numbers. Consider the universe of all real functions on the natural numbers (infinite many), and we will group these functions with respect to f(n). (f) Functions that grow at the same rate as f(n) Functions that grow faster or equal to f(n) (f) (f) Functions that grow slower or equal to f(n) As an example suppose that f(n)=n2, one function that belongs to (f(n)) is g(n)= n3 (as n3 grows faster than n2 ), and one function that belongs to (f(n)) is h(n)=n (as n grows slower than n2).