1 / 10

CS 330: Algorithms

CS 330: Algorithms. Gene Itkis. Complexity: outline. Evaluating efficiency What to count Ignoring constants Big-O, and other letters “Direct” Make an educated guess Prove the guess (or adjust it and try again) Recurrences Same as above, or Master Method. Performance/Complexity.

gbonner
Download Presentation

CS 330: Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 330: Algorithms Gene Itkis

  2. Complexity: outline • Evaluating efficiency • What to count • Ignoring constants • Big-O, and other letters • “Direct” • Make an educated guess • Prove the guess (or adjust it and try again) • Recurrences • Same as above, or • Master Method Gene Itkis

  3. Performance/Complexity • Depends on input • Input size: • Express complexity as a function of input size • Examples: Selection Sort is O(n2), Merge – O(n lg n) • Specific instance: • Best Case • Worst Case • Average • Costs of specific operations • Choose the right operations • Ignore some details: big-O notation Gene Itkis

  4. Performance/Complexity for i= 1 to n do min_index <-- i for j= i+1 to n do if A[j] < A[min_index] then min_index <-- j swap( A[i], A[min_index] ) Depends on input size: n elements Best/Worst/Average Cases: here  same • Cost of specific operations: • Comparisons • Assignments/moves • Other (e.g. arithmetic)  typical  sorting crates Gene Itkis

  5. Cost of various operations • How to compare the costs of +, <, := ? • Usually they are “similar”? • For each <, we usually do a few of +, :=, etc. • The difference between different options is often a constant factor • Big-O allows to hide these details • As we saw, these details are often secondary to “n vs n2” and similar relations • Constant factors hidden in big-O are often similar between the various options • Discussion section! Gene Itkis

  6. Selection Sort 1 ? × O(1) 1? 3?  O(1) for i= 1 to n do min_index <-- i for j= i+1 to n do if A[j] < A[min_index] then min_index <-- j swap( A[i], A[min_index] ) n× • ? = • “average” n/2 iterations × O(1) • Total: n × n/2 × O(1) = O(n2) • Total:[(n-1)+(n-2)+(n-3)+…+1] × O(1) = n(n-1)/2=O(n2) • Recurrence: T(n)=T(n-1) + O(1) Gene Itkis

  7. Merge Sort Merge_Sort(A[], first, last): if (last < first) then return else middle (first+last)/2 Merge_Sort( A, first, middle ) Merge_Sort( A, middle+1, last ) Merge( A[first...middle], A[middle+1...last], A[first...last] ) return A[] Divide & Concur Reduction Gene Itkis

  8. Recurrence Relations • Selection Sort • T(n) = T(n-1) + O(n) • Merge Sort • T(n) = 2T(n/2) + O(n) • Reduction: • Sel_Sort(L): • xRem_Min(L); • Sel_Sort(L); • LIns_Head(x,L); • return L • M_S(A[], f, l): • … • M_S( A, f, mid ) • M_S( A, mid+1, l ) • Merge( A[f...mid], A[mid+1...l], A[f...l] ) Gene Itkis

  9. Recurrence Relations • Binary Search • Bin_Search( x, A[], f, l ): • if (l < f) then return • mid (f+l)/2 • case • A[m]=x:return ( m, A[m] ) • A[m]<x:returnBin_Search( x,A, f, mid-1 ) • A[m]>x:returnBin_Search( x,A, mid+1, l ) T(n) = T(n/2) + O(1) Gene Itkis

  10. Common Recurrences • T(n) = T(n-1) + O(n) • T(n) = 2T(n/2) + O(n) • T(n) = T(n/2) + O(1) • T(n) = T(n/2) + O(1) • T(n) = 2T(n-1) + O(n) O(n2) O(n lg n) O( lg n ) O( n ) O(2n) Gene Itkis

More Related