Download Divide and conquer

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Recap: Oh, Omega, Theta
• Oh (like ≤)
O(n) is asymptotic upper bound 0 ≤ f(n) ≤ cg(n)
• Omega (like ≥)
Ω(n) is asymptotic lower bound 0 ≤ cg(n) ≤ f(n)
• Theta (like =)
Θ(n) is asymptotic tight bound 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n)
CSC317
1
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) ?
CSC317
Answer: NO (why?)!
2
Question: Is f(n) = n2 + 5 Ω(n3) ?
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 ≥ 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
3
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 Ω?)
CSC317
4
Final thoughts: Oh, Omega, Theta
• Useful comparison formula of functions in book (section3.2)
CSC317
5
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
6
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
7
Brute force: Try every possible pair of buy and
sell dates
æn æ
n!
n(n-1)(n- 2)! n(n-1)
=
=
= Q(n2 )
æ æ=
(n- 2)!2!
2
æ 2 æ (n- 2)!2!
Hmm, can we do better?
CSC317
8
Let’s reframe the problem as greatest sum of any contiguous array
Efficiency? Still brute force
Divide and conquer anybody?
CSC317
9
Where could max subarray be?
low
high
middle
low
i
j
i
j
i
j
high
middle
CSC317
10
Where could max subarray be?
low
high
middle
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
CSC317
11
Keep recursing until low=high (one element left)
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
low
i
j
high
middle
CSC317
12
i
low
j
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
13
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);
CSC317
14
COSTS:
1. DIVIDE T = Θ(1)
2. CONQUER T = 2T(n/2)
3. COMBINE T = Θ(n)+Θ(1)
comparison
Subarray
crossing
TOTAL :
T(n) = 2T(n/2) + Θ(n) = Θ(n log n)
Merge sort anyone?
CSC317
15
CLASSICAL EXAMPLE: MATRIX MULTIPLICATION
N
cij = å aik bkj
k=1
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. for j=1 to n
5.
cij = 0
6.
for k=1 to n
7.
cij = cij+ aik bkj
8. return C
CSC317
Can we do better?
16