






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Class: DSGN & ANALYSIS OF ALGORITHMS; Subject: Computer Science; University: Eastern Washington University; Term: Spring 2010;
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!
Algorithm usually proceeds from the inside out First determine the time required by individual instructions Second, combine the times according to the control structures that combine the instructions in the program Some control structures sequencing are easy to evaluate Others such as while loops are more difficult
Let P 1 and P 2 be two fragments of an algorithm Let t 1 and t 2 be the times taken by P 1 and P 2 respectively Sequencing Rule The time required to compute " P 1 : P 2 ", is simply t 1 + t 2. By the maximum rule, this time is in (^) (max( t 1 , t 2 )) It could happen that one of the parameters that control t2 depend on the result of the computation performed by P Thus analysis of " P 1 : P 2 " cannot always be performed by considering P 1 and P 2 independently
function Fibiter(n) i ^ 1; j ^0 for k ^1 to n do j ^ i + j i ^ j – i return j Analyzing the loop, the instructions inside the loop take constant time Let the time taken by these instructions be bounded above by some constant c The time taken by the for loop is bounded above by n times the constant c : nc The algorithm therefore takes a time in O(n) ^ ^ (n) ^ ^ (n)
function Fibrec(n) if n < 2 then return n else return Fibrec (n - 1) + Fibrec (n - 2) Let T(n) be the time taken by a call on Fibrec (n) If n < 2, the algorithm simply returns n , which takes some time constant time a Most of the work is spent in the two recursive calls which take time T(n - 1) and T(n - 2) One addition involving fn -1 and fn -2 (values returned by the recursive calls) Control of the recursion Test " if n < 2"
Usually harder to analyze than for loops - there is no a priori way to determine the amount of iterations through the loop Need to better understand how the value of the function decreases An alternative is to treat the loops like recursive algorithms — recursion naturally leads to a descriptive recurrence, which then can be handled by techniques to be described later. NOTE: p. 103 misprint (regarding Binary_Search ) j
Analyze algorithms by using a test instruction Barometers executed at least as many times as any other instruction in the algorithm allows us to neglect exact times taken by each instruction no need for time constants (bounds) taken by elementary constants Warning: insure that the barometer instruction indeed executed at least as many times as any other instruction.
Average Case Analysis Example: QuickSort is known to have worst-case O( n 2 ), but average case scales as n log n Text example: Insertion Sort procedure insert ( T [1.. n ]) for i 2 to n do x T [ i ]; j i – 1 while j > 0 and x < T [ j ] do T [ j +1] T [ j ] j j – 1 T [ j +1] x Worst-case: inner while loop iterates i –1 times, so that there are i comparisons for j > 0 and (thanks to short-cut evaluation) i –1 comparisons for x < T [ j ] Average case: all permutations are equally likely, so that the average will be based on each x being positioned in the i possible positions. Result is O( n 2 ), but noting that the hidden constant implicit in O notation is half that of the worst-case. NOTE: Skip 4.6 (Amortized analysis)
Recurrences Intelligent guess-work: note that spreadsheets work very well in computing recurrences, and also can plot the results. For instance, the disk movements for the Towers of Hanoi: Hanoi(0) = 0 Hanoi( k ) = Hanoi( k –1) + 1 + Hanoi( k –1) A 2 : 0 B 2 : 0 A k : =1+A k –1 for a range of k > 2 B k : =1+2*B k –1 for a range of k > 2 n H(n) 0 0 1 1 2 3 3 7 4 15 5 31 6 63 7 127 Trial function: Hanoi(n) = 2 n