Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithm Analysis 2P03 © Dave Bockus Acknowledgments to Mark Allen Weiss 2014 Data Structures & Algorithm Analysis in Java.

Similar presentations


Presentation on theme: "Algorithm Analysis 2P03 © Dave Bockus Acknowledgments to Mark Allen Weiss 2014 Data Structures & Algorithm Analysis in Java."— Presentation transcript:

1 Algorithm Analysis 2P03 © Dave Bockus Acknowledgments to Mark Allen Weiss 2014 Data Structures & Algorithm Analysis in Java

2 Strategies for Analyzing Code  Rule 1-- FOR LOOPS: The running time of a FOR loop is at most the running time of the statements inside the FOR loop (including tests) times the number of iterations.  Rule 2-- Nested Loops: The total running time of a statement inside a group of nested loops is the running time of the statements multiplied by the product of the sizes of all the loops. for (i=0; i < n; i++) for (j=0; j < n; j++) sum++; Running time n x n

3 Strategies for Analyzing Code Cont...  Rule 3-- Consecutive Statements: Statements simply add up, where the largest statement takes precedence. for (int i = 0; i < n; i++) sum++; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) sum++; Total running time is determined by statement set B Statements A Statements B

4 Strategies for Analyzing Code Cont...  Rule 4-- If/Else: Running time of an IF/ELSE is never more than the running time of the test plus the larger of the running times of S1 and S2.  Worst case is condition + S2 = O(n 2 ) If (condition) S1 Else S2 If (condition) for (int i = 0; i < n; i++) sum++; Else for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) sum++;

5 Strategies for Analyzing Code Cont... Loops where the problem N size is divided by some factor k is a log k N loop. for (int i = 0; i < n; i++){ n = n/3; sum++; } –Running time is log 3 N or –Just O(log N)

6 Strategies for Analyzing Code Cont...  Analyze loops from the inside out.  Recursion:  Tail recursion is nothing more then a LOOP.  Other types of recursion may require recurrence relations to be defined. PrintList (node ptr){ if (ptr != null) { print(ptr.data); PrintList(ptr.next); } } PrintList (node ptr){ while (ptr != null){ print(ptr.data); ptr = ptr.next; } }

7 Partitioning a List [ 3724615 ] Given a List: [ 2 1 ] 3 [ 4 6 7 5 ]  Take first # or choose a pivot element  Put everything less than this in a left sublist  Put everything greater than this in a right sublist  Keep doing this for each sublist  E.g. The above partitions into:

8 Partitioning a List Cont… [ 3724615 ] i j 1). While j th element > i th element, reduce j [ 3724615 ] i j 2). Swap the i th and j th elements [ 1724635 ] i j 3). While i th element <= j th element, increase i [ 1724635 ] i j 4). Swap the i th and j th elements [ 1324675 ] i j 5). Repeat 1 to 4 until j=i

9 Partitioning a List Cont… [ 3724615 ] i j [ 3724615 ] i j Step 1 [ 1724635 ] i j Step 2 [ 1324675 ] i j Step 1 [ 1324675 ] ij Step 1 [ 1324675 ] ij Step 1 [ 1234675 ] ij Step 2 [ 1234675 ] i=j Step 3 Finished [ 1324675 ] i j Step 4 [ 1724635 ] i j Step 3 ][

10 Best Case for Quick Sort Alternately we can use a recurrence relation –T(N) = T(i) + T(N – i – 1) + cN So –T(N) = 2T(N/2) + cN –T(N)/N = T(N/2)/(N/2) + c Multiply both sides by 1/N LHS: with i elements RHS: with N-i elements Number of comparison for pivot – constant time.

11 Best Case QS Continued... Cut list in ½ for each iteration until 1 element in partition –T(N)/N = T(N/2)/(N/2) + c –T(N/2)/(N/2)= T(N/4)/(N/4) + c –T(N/4)/(N/4)= T(N/8)/(N/8) + c –T(N/8)/(N/8)= T(N/16)/(N/16) + c at some point –T(2)/(2)= T(1)/(1) + c

12 Best Case QS Continued... If we add up all equations: –T(N) + T(N/2) … T(2) = T(N/2) … T(2) +T(1) + clgN N + N/2 + N/4 … 2 N/2 + N/4 …2+ 1 Cancel out terms: –T(N) = T(1) + clgN N 1 –T(N) = NT(1) + cNlgN T(1) is 1 so: –T(N) = N + cNlgN so order NlgN

13 Worst Case for QS –T(N) = T(N-1) +cNN>1 Reducing the list by choosing the smallest –T(N-1) = T(N-2) + c(N-1) –T(N-2) = T(N-3) + c(N-2) until –T(2) = T(1) +c(2) Pivot

14 Now add up all equations –T(N) +  T(N-1) - T(1) =  T(N-1) + c  N –T(N) = T(1) + c  N –T(N) = 1 + cN(N+1)/2 or –T(N) = O(N 2 ) Worst Case for QS

15 Exercise: Bubble Sort Determine the run time equation and complexity of bubble sort? for (i=1; i<=n; i++) for (j=1; j<n; j++) { {compare & swap} if (x[j] > x[j+1]) swap }  The basic BS compares every element with every other element.  Each time through the loop n elements are compared.  Since we do this n times there are n x n or n 2 comparisons.  Runtime equation is f(n) = n 2 with On 2 complexity

16 Exercise: Nexted Loops Determine the run time equation and complexity of the following code? What is the value of sum if n = 20. for (int i=1; i<=n; i++) for (int j=1; j<=i; j++) sum++;

17 Exercise: Nexted Loops Cont…. We must note that the inner loop will increase sum i times each time it executes. –So after 6 iterations of the inner loop, sum is 1 + 2 + 3 + 4 + 5 + 6, this happens to be the sum of n numbers:  n = [n(n+1)]/2 if n = 20 then = 20(21)/2 = 210

18 Running the code n = 1 -- sum is 1 n = 2 -- sum is 3 n = 3 -- sum is 6 n = 4 -- sum is 10 n = 5 -- sum is 15 n = 6 -- sum is 21 n = 7 -- sum is 28 n = 8 -- sum is 36 n = 9 -- sum is 45 n = 10 -- sum is 55 n = 11 -- sum is 66 n = 12 -- sum is 78 n = 13 -- sum is 91 n = 14 -- sum is 105 n = 15 -- sum is 120 n = 16 -- sum is 136 n = 17 -- sum is 153 n = 18 -- sum is 171 n = 19 -- sum is 190 n = 20 -- sum is 210


Download ppt "Algorithm Analysis 2P03 © Dave Bockus Acknowledgments to Mark Allen Weiss 2014 Data Structures & Algorithm Analysis in Java."

Similar presentations


Ads by Google