1 / 16

Overview of Oh, Omega, Theta in Algorithmic Analysis

This recap discusses the concepts of Oh, Omega, and Theta notation in asymptotic analysis, their properties, and how they are used to compare functions. It also introduces the divide and conquer approach and provides examples of its application in solving recurrence relations in algorithms like merge sort and the max subarray problem.

rjosue
Download Presentation

Overview of Oh, Omega, Theta in Algorithmic Analysis

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. Recap: Oh, Omega, Theta • Oh (like ≤) • Omega (like ≥) • Theta(like =) O(n) is asymptotic upper bound 0 ≤ f(n) ≤ cg(n) Ω(n) is asymptotic lower bound 0 ≤ cg(n) ≤ f(n) Θ(n) is asymptotic tight bound 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) CSC317

  2. More on Oh, Omega, Theta Theorem: f(n) = Θ(n) if and only if (iff) f(n) = O(n)and f(n) = Ω(n) Question: Is f(n) = n2 + 5 Ω(n3) ? Answer: NO (why?)! CSC317

  3. Answer: NO! Proof by contradiction Question: Is f(n) = n2 + 5 Ω(n3) ? Definition:Ω (g(n)) = f(n): There exist positive constants c, such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 Therefore: If f(n) = Ω(n3), then there exists n0; c such that for all n ≥ n0 n2 + 5 ≥ cn3 Remember from before: n2 + 5n2 ≥ n2+ 5 n2 + 5n2 ≥ cn3 6 ≥ cn Can’t be true for all n ≥ n0 CSC317

  4. Some properties of Oh, Omega, Theta Transitivity: f(n) = Θ(g(n)) and g(n) = Θ(h(n)) then f(n) = Θ(h(n)) (same for O and Ω?) Reflexivity: f(n) = Θ(f(n)) (same for O and Ω?) f(n) = Θ(g(n)) iff g(n) = Θ(f(n)) Symmetry: (same for O and Ω?) CSC317

  5. Final thoughts: Oh, Omega, Theta • Useful comparison formula of functions in book (section3.2) CSC317

  6. What kind of recurrences arise in algorithms and how do we solve more generally (than what we saw for merge sort)? • More recurrence examples • Run time not always intuitive, so need tools Divide and Conquer approach Max Subarray Problem: Can buy stock once, sell stock once. Want to maximize profit; allowed to look into the future CSC317

  7. Max Subarray Problem: Can buy stock once, sell stock once. Want to maximize profit; allowed to look into the future Sell Buy Buy low, sell high … CSC317

  8. Brute force: Try every possible pair of buy and sell dates Hmm, can we do better? CSC317

  9. Let’s reframe the problem as greatest sum of any contiguous array Efficiency? Still brute force Divide and conquer anybody? CSC317

  10. Where could max subarray be? low low high high middle middle i j i j i j CSC317

  11. Where could max subarray be? low high middle DIVIDE chop subarray in two equal subarrays A[low,…,middle] and A[middle+1,..., high] 2. CONQUERfind max of subarrays A[low,…,middle] and A[middle+1,..., high] 3. COMBINE find the best solution of: a. the two solutions found in conquer step b. solution of subarray crossing the midpoint CSC317

  12. Keep recursing until low=high (one element left) DIVIDE chop subarray in two equal subarrays A[low,…,middle] and A[middle+1,..., high] 2. CONQUER find max of subarrays A[low,…,middle] and A[middle+1,..., high] 3. COMBINE find the best solution of: a. the two solutions found in conquer step b. solution of subarray crossing the midpoint low high middle i j CSC317

  13. i j low high middle • Start from the middle • Go to the left until hit max sum. • Go to the right until hit max sum. • Return total left and right sum. • COMPLEXITY? Θ(n) CSC317

  14. float recmax(int l, int u) if (l > u) /* zero elements */ return 0; if (l == u) /* one element */ return max(0, A[l]); m = (l+u) / 2; /* find max crossing to left */ lmax = sum = 0; for (i = m; i ≥ l; i--) { sum += A[i]; if (sum > lmax) lmax = sum; } /* find maxcrossingtoright */ rmax = sum = 0; for (i = m+1; i ≤ u; i++) { sum += A[i]; if (sum > rmax) rmax = sum; } returnmax(max(recmax(l, m), recmax(m+1, u)), lmax + rmax); CSC317

  15. COSTS: DIVIDE CONQUER COMBINE T = Θ(1) T = 2T(n/2) T = Θ(n)+Θ(1) comparison Subarray crossing TOTAL : T(n) = 2T(n/2) + Θ(n) = Θ(n log n) Merge sort anyone? CSC317

  16. CLASSICAL EXAMPLE: MATRIX MULTIPLICATION Run time: O(n3) in the naïve implementation 1. n = A.rows 2. Let C be a new n by n matrix 3.for i=1 to n 4.forj=1 to n 5. cij = 0 6. fork=1 to n 7. cij= cij+ aikbkj 8. returnC Can we do better? CSC317

More Related