Lecture 03
Lecture 03
a problem of size n
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
a solution to
the original problem
3
Divide-and-Conquer
Examples
• Sorting: mergesort and quicksort
• Binary tree traversals
• Multiplication of large integers
• Matrix multiplication: Strassen’s
algorithm
• Closest-pair and convex-hull
algorithms.
• Binary search: decrease-by-half (or
degenerate divide&conq.)
• T(n) = aT(n/b) + f (n) where f(n) (nd),
d0
Divide-and- •
a)
If a > bd, T(n) (nlog b
8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
Merge sort 8 3 2 9 7 1 5 4
Example 3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
Merge sort
Split array A[0..n-1] in two about Sort arrays B and C recursively Merge sorted arrays B and C into
equal halves and make copies of array A as follows:
each half in arrays B and C
Repeat the following until no elements
remain in one of the arrays:
•compare the first elements in the
remaining unprocessed portions of the
arrays
•copy the smaller of the two into A, while
incrementing the index indicating the
unprocessed portion of that array
Once all elements in one of the arrays are
processed, copy the remaining
unprocessed elements from the other
array into A.
Pseudocode
of Merg esort
• Thinking about recursive
programs: Assume recursive
calls work
• and ask does the
whole program then work?
Pseudocode
of Merge
Analysis of Mergesort
• Number of key comparisons – recurrence:
• C(n) = …
Space requirement:
• Book’s algorithm?
Analysis of Number of comparisons in
the worst case is close to
Space requirement:
•Book’s algorithm: n + n
Sum(1+1/2+1/4+…) = n + n Θ(2)=
Θ(3n)
•Can be done Θ(2n) : Don’t copy Can be implemented
in sort; just specify bounds, copy without recursion
into extra array on merge, then (bottom-up)
copy back
•Any other space required?
•Can be implemented in place.
How …
8 3 2 9 7 1 5 4
Merge sort
Example 8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
Quicksort
A[i]p A[i]p
Select a pivot (partitioning element) – Rearrange the list so that all the Exchange the pivot with the last element Sort the two subarrays recursively
here, the first element elements in the first s positions are in the first (i.e., ) subarray — the pivot is
smaller than or equal to the pivot and all now in its final position
the elements in the remaining n-s
positions are larger than or equal to the
pivot (see next slide for an algorithm)
Quicksort Algorithm
QS (A, l, r) if l < r then Ask later: What if we skip final use simpler bounds for first
swap and recursive call?
s = partition (A, l, r)
QS (A, l , s-1)
QS (A, s+1, r )
Quicksort
Example
5 3 1 9 8 2 4 7
Quicksort
Recursion Tree
0 1 2 3 4 5 6 7
5 3 1 9 8 2 4 7
0 .. 7 / s=4
0 .. 3 / s=x
0 .. s-1 / s=?
s+1 .. 3
5 .. 7 / s=y
5 .. y-1 / s=?
y+1 .. 7 / s=?
Analysis of Quicksort
Recurrences:
• Best case:
• Worst case:
• Average case:
Performance:
• Best case:
• Worst case:
• Average case:
Binary Tree Algorithms
Binary tree is a divide-and-conquer ready structure!
Efficiency: Θ(n)
A. Levitin “Introduction to the Design & Analysis of
Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. 23
Upper Saddle River, NJ. All Rights Reserved.
Binary Tree Algorithms
Binary tree is a divide-and-conquer ready structure!
Efficiency: Θ(n)
24
Binary Tree Algorithms (cont.)
TL TR
h(T) = ??
Efficiency: Θ(n)
25
Ex. 2: Computing the height of a binary tree
Binary Tree TL TR
Algorithms
(cont.) h(T) = max{h(TL), h(TR)} + 1 if T and h() = -1
Efficiency: Θ(n)
26