SLIDE 6 CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer
Min-Max- Problem
Tutorial
Unfolding the recursion for Min-Max
We have T(n) = if n = 1 1 if n = 2 T( n
2
n
2
else .
1 T(1) = 0 2 T(2) = 1 3 T(3) = T(2) + T(1) + 2 = 1 + 0 + 2 = 3 4 T(4) = T(2) + T(2) + 2 = 1 + 1 + 2 = 4 5 T(5) = T(3) + T(2) + 2 = 3 + 1 + 2 = 6 6 T(6) = T(3) + T(3) + 2 = 3 + 3 + 2 = 8 7 T(7) = T(4) + T(3) + 2 = 4 + 3 + 2 = 9 8 T(8) = T(4) + T(4) + 2 = 4 + 4 + 2 = 10 9 T(9) = T(5) + T(4) + 2 = 6 + 4 + 2 = 12 10 T(10) = T(5) + T(5) + 2 = 6 + 6 + 2 = 14.
We count 4 steps +1 and 5 steps +2 — we guess T(n) ≈ 3
2n.
CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer
Min-Max- Problem
Tutorial
Finding the best min-max algorithm
1 As you can see in the section on the min-max problem, for
some input sizes we can validate the guess T(n) ≈ 3
2n.
2 One can now try to find a precise general formula for T(n), 3 However we see that we have T(6) = 8, while we can
handle this case with 7 comparisons. So perhaps we can find a better algorithm?
4 And that is the case: 1
If n is even, find the min-max for the first two elements using 1 comparison; if n is odd, find the min-max for the first element using 0 comparisons.
2
Now iteratively find the min-max of the next two elements using 1 comparison, and compute the new current min-max using 2 further comparisons. And so on ....
This yields an algorithm using precisely 3
2n
- − 2
- comparisons. And this is precisely optimal for all n.
We learn: Here divide-and-conqueor provided a good stepping stone to find a really good algorithm.
CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer
Min-Max- Problem
Tutorial
Designing an algorithm: Median
The median of a sequence of numbers is the “middle value” — the value in the middle position of the list after sorting. Can we do better than the obvious algorithm (by sorting), using divide-and-conquer? (Here some judgement is needed, that the precise details about what actually is the “middle value” won’t make a fundamental difference, and so it’s best to ignore them for the initial phase, where developing the ideas is of utmost importance.)
CS 270 Algorithms Oliver Kullmann Growth of Functions Divide-and- Conquer
Min-Max- Problem
Tutorial
First strategy: divide in half
1 Divide the array A of numbers into two parts, B and C, of
equal size (more precisely, nearly equal size, but such details are best ignored in the beginning).
2 In principle B and C could be anything, but easiest is to let
them be the first half and the second half of A.
3 Compute (that is now the conquer-phase) the medians
mB, mC of the arrays B, C.
4 If mC < mB, then swap B and C. 5 Re-order B and C internally, so that the elements smaller
than the median are on the left, and the larger elements on the right.
6 Now consider the array of elements from mB to mC (note
that this is again half of the size of A): The median of this array (computed again in the conquer-phase, recursively) is the median of A.