Introduction Algorithm Analysis How can we say that one algorithm performs better than another Quantify the resources required to execute Slides by Christopher M Bourke Instructor Berthe Y Choueiry I Time I Memory I I O I circuits power etc Spring 2006 Computer Science Engineering 235 Section 2 3 of Rosen cse235 cse unl edu Input Size Time is not merely CPU clock cycles we want to study algorithms independent or implementations platforms and hardware We need an objective point of reference For that we measure time by the number of operations as a function of an algorithm s input size Orders of Growth For a given problem we characterize the input size n appropriately I Sorting The number of items to be sorted I Graphs The number of vertices and or edges I Numerical The number of bits needed to represent a number The choice of an input size greatly depends on the elementary operation the most relevant or important operation of an algorithm I Comparisons I Additions I Multiplications Intractability Problems that we can solve today only with exponential or super exponential time algorithms are said to be likely intractable That is though they may be solved in a reasonable amount of time for small n for large n there is likely no hope of efficient execution It may take millions or billions of years Tractable problems are problems that have efficient read polynomial algorithms to solve them Polynomial order of magnitude usually means there exists a polynomial p n nk for some constant k that always bounds the order of growth More on asymptotics in the next slide set Intractable problems may need to be solved using approximation or randomized algorithms Small input sizes can usually be computed instantaneously thus we are most interested in how an algorithm performs as n Indeed for small values of n most such functions will be very similar in running time Only for sufficiently large n do differences in running time become apparent As n the differences become more and more stark Worst Best and Average Case Some algorithms perform differently on various inputs of similar size It is sometimes helpful to consider the Worst Case Best Case and Average Case efficiencies of algorithms For example say we want to search an array A of size n for a given value K I Worst Case K 6 A then we must search every item n comparisons I Best Case K is the first item that we check so only one comparison Average Case Average Case Since any worth while algorithm will be used quite extensively the average running time is arguably the best measure of its performance if the worst case is not frequently encountered For searching an array and assuming that p is the probability of a successful search we have Cavg n p 2p ip np n 1 p n p 1 2 i n n 1 p n p n n 1 n 1 p n 2 Average Case analysis of algorithms is important in a practical sense Often Cavg and Cworst have the same order of magnitude and thus from a theoretical point of view are no different from each other Practical implementations however require a real world examination If p 1 search succeeds Cavg n n 1 2 5n If p 0 search fails Cavg n n A more intuitive interpretation is that the algorithm must examine on average half of all elements in A Mathematical Analysis of Algorithms Analysis Examples Example I After developing pseudo code for an algorithm we wish to analyze its efficiency as a function of the size of the input n in terms of how many times the elementary operation is performed Here is a general strategy Consider the following code Algorithm UniqueElements 1 Decide on a parameter s for the input n 2 Identify the basic operation 3 Evaluate if the elementary operation depends only on n otherwise evaluate best worst and average case separately 4 Set up a summation corresponding to the number of elementary operations 5 Simplify the equation to get as simple of a function f n as possible 1 2 3 4 5 6 Input Integer array A of size n Output true if all elements a A are distinct for i 1 n 1 do for j i 1 n do if ai aj then return false end end 7 end 8 return true Analysis Example Analysis Example Example I Analysis Example I Analysis The inner for loop depends on the outer for loop so it contributes For this algorithm what is n X I The elementary operation I Input Size I Does the elementary operation depend only on n j i 1 The outer for loop is run n 2 times More formally it contributes We observe that the elementary operation is executed once in each iteration thus we have n 1 X i 1 Cworst n n 1 X n X i 1 j i 1 1 n n 1 2 Analysis Example Analysis Example Example II Example II Analysis The parity of a bit string determines whether or not the number of 1s appearing in it is even or odd It is used as a simple form of error correction over communication networks Algorithm Parity 1 2 3 4 5 Input An integer n in binary b Output 0 if the parity of n is even 1 otherwise parity 0 while n 0 do if b 0 1 then parity parity 1 mod 2 right shift n 6 For this algorithm what is I The elementary operation I Input Size I Does the elementary operation depend only on n The while loop will be executed as many times as there are 1 bits in its binary representation In the worst case we ll have a bit string of all ones The number of bits required to represent an integer n is end 7 end 8 return parity dlog ne so the running time is simply log n Analysis Example Analysis Example Example III Example III Analysis Algorithm MyFunction n m p 1 2 3 4 5 6 7 Input Integers n m p such that n m p Output Some function f n m p x 1 for i 0 10 do for j 0 n do for k m 2 m do x x p end end 8 end 9 return x Summation Tools I I Outer Loop executed 11 times I 2nd Loop executed n 1 times I Inner Loop executed about I Thus we have m 2 times C n m p 11 n 1 m 2 I But do we really need to consider p Summation Tools II Example Section 3 2 p229 has more summation rules You can always use Maple to evaluate and simplify complex expressions but know how to do them by hand To invoke maple on cse you can use the command line interface by typing maple Under unix gnome or KDE or via any xwindows interface you can use the graphical version via xmaple Will be demonstrated during recitation simplify sum i i 0 n 1 2 1 n n 2 2 Sum …
View Full Document
Unlocking...