Divide-and-Conquer Algorithms
• The divide-and-conquer paradigm
• Divide the problem into a number of subproblems.
• Conquer the subproblems (solve them).
• Combine the subproblem solutions to get the solution to the
original problem.
• Merge sort:
• Divide the n-element sequence to be sorted into two n/2-element
sequence.
• Conquer: sort the subproblems, recursively using merge sort.
• Combine: merge the resulting two sorted n/2-element sequences.
Merge Sort: A Divide-and-Conquer Algorithm
MergeSort(A, p, r) T(n)
1. If p < r then (1)
2. q (p+r)/2 (1)
3. MergeSort (A, p, q) T(n/2)
4. MergeSort (A, q +1, r) T(n/2)
5. Merge(A, p, q, r) (n)
Recurrence: Analyzing Divide-and-Conquer Algorithms
• Describes a function recursively in terms of itself.
• Describes performance of recursive algorithms.
• Recurrence for a divide-and-conquer algorithms
(1), if n c
T (n)
aT (n / b) D ( n ) C ( n ),otherwise
• a: # of subproblems
• n/b: size of the subproblems
• D(n): time to divide the problem of size n into subproblems
• C(n): time to combine the subproblem solutions to get the answer for the problem
of size n
• Merge sort:
(1), if n c
T (n)
2T ( n / 2) ( n ),otherwise
• a = 2: two subproblems
• n/b = n/2: each subproblem has size n/2
• D(n) = (1): compute midpoint of array
• C(n) = (n): merging by scanning sorted subarrays
Solving Recurrences
• Some general methods for solving recurrences
Recursion Tree Method
Iteration Method
• Two simplifications that won't affect asymptotic
analysis
• Ignore floors and ceilings.
• Assume base cases are constant, i.e., T(n) = (1) for small n.
Merge Sort Recurrence: Recursion Tree Method
(1) , if n=1
T (n)
2 T(n/2) + (n ) , if n >1
(n lg n) grows more slowly than (n2).
• Thus merge sort asymptotically beats insertion
sort in the worst case.
Merge Sort Recurrence: Iteration Method
T(n) = 2 T(n/2) + n
T(n/2) = 2(T(n/4)) + n/2
2T(n/2) = 2(2(T(n/4)) + n/2)
2T(n/2) = 4T(n/4) + n
T(n) = 4T (n/4) + 2n
T(n/4) = 2T(n/8) + (n/4)
4T(n/4) = 4(2T(n/8)) + (n/4)
4T(n/4) = 8T(n/8) + n
T(n) = 8T(n/8) + 3n
Continuing in this manner, we obtain
T(n) = 2lgn T(n/2lgn) + lgn.n
T(n) = nT(n/n) + nlgn 2lgn = n
T(n) = nT(1) + nlgn
T(n) = n + nlgn
T(n) = (nlgn)
Divide and Conquer
1. Divide the problem into a number of
subproblems
There must be base case (to stop recursion).
2. Conquer (solve) each subproblem recursively
3. Combine (merge) solutions to subproblems
into a solution to the original problem
Binary Search
• Very efficient algorithm for searching in sorted array:
K
vs
A[0] . . . A[m] . . . A[n-1]
• If K = A[m], stop (successful search); otherwise, continue
searching by the same method in A[0..m-1] if K < A[m]
and in A[m+1..n-1] if K > A[m]
l 0; r n-1
while l r do
m (l+r)/2
if K = A[m] return m
else if K < A[m] r m-1
else l m+1
return -1
Analysis of Binary Search
• Time efficiency
• worst-case recurrence: T (n) = T(n/2 ) + 1, T (1) = 1
solution: T (n) = logn
This is VERY fast: e.g., T(106) = 20