Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC317 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.

Similar presentations


Presentation on theme: "CSC317 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."— Presentation transcript:

1 CSC317 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 ≤ c 1 g(n) ≤ f(n) ≤ c 2 g(n)

2 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) = n 2 + 5 Ω(n 3 ) ? Answer: NO (why?)!

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

4 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 Ω?) Symmetry: f(n) = Θ(g(n)) iff g(n) = Θ(f(n)) (same for O and Ω?)

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

6 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

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

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

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

10 CSC317 10 Where could max subarray be? middle lowhigh middle lowhigh iiijjj

11 CSC317 11 Where could max subarray be? middle lowhigh 1.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

12 CSC317 12 1.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 Keep recursing until low=high (one element left) middle lowhigh ij

13 CSC317 13 middle lowhigh ij 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)

14 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 max crossing to right */ rmax = sum = 0; for (i = m+1; i ≤ u; i++) { sum += A[i]; if (sum > rmax) rmax = sum; } return max(max(recmax(l, m), recmax(m+1, u)), lmax + rmax);

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

16 CSC317 16 CLASSICAL EXAMPLE: MATRIX MULTIPLICATION Run time: O(n 3 ) 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. for j=1 to n 5. cij = 0 6. for k=1 to n 7. cij = cij+ aik bkj 8. return C Can we do better?


Download ppt "CSC317 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."

Similar presentations


Ads by Google