Professional Documents
Culture Documents
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
An Example: Calculating a0 + a1 + … + an-1
ALGORITHM RecursiveSum(A[0..n-1])
//Input: An array A[0..n-1] of orderable elements
//Output: the summation of the array elements
if n > 1
return ( RecursiveSum(A[0.. n/2 – 1]) + RecursiveSum(A[n/2 .. n– 1]) )
k
Efficiency: (for n = 2 )
A(1) = 0;
General Divide and Conquer recurrence
Tree traversals
Binary search
the arrays
copy the smaller of the two into A, while incrementing the index
Efficiency?
Worst case: the smaller element comes from alternating arrays.
The Divide, Conquer and
Combine Steps in Quicksort
Divide: Partition array A[l..r] into 2 subarrays, A[l..s-1]
and A[s+1..r] such that each element of the first array
is ≤A[s] and each element of the second array is ≥ A[s].
(computing the index of s is part of partition.)
Implication: A[s] will be in its final position in the sorted array.
Conquer: Sort the two subarrays A[l..s-1] and A[s+1..r] by
recursive calls to quicksort
Combine: No work is needed, because A[s] is already in its
correct place after the partition is done, and the two
subarrays have been sorted.
Quicksort
Select a pivot w.r.t. whose value we are going to divide the
sublist. (e.g., p = A[l])
Rearrange the list so that it starts with the pivot followed by a ≤
sublist (a sublist whose elements are all smaller than or equal to
the pivot) and a ≥ sublist (a sublist whose elements are all
greater than or equal to the pivot ) (See algorithm Partition in
section 4.2)
Exchange the pivot with the last element in the first sublist(i.e.,
≤ sublist) – the pivot is now in its final position
Sort the two sublists recursively using quicksort.
p
A[i]≤p A[i] ≥ p
The Quicksort Algorithm
ALGORITHM Quicksort(A[l..r])
//Sorts a subarray by quicksort
//Input: A subarray A[l..r] of A[0..n-1],defined by
its left and right indices l and r
//Output: The subarray A[l..r] sorted in
nondecreasing order
if l < r
s Partition (A[l..r]) // s is a split position
Quicksort(A[l..s-1])
Quicksort(A[s+1..r]
The Partition Algorithm
ALGORITHM Partition (A[l ..r])
//Partitions a subarray by using its first element as a pivot
//Input: A subarray A[l..r] of A[0..n-1], defined by its left and right indices l and r (l
< r)
//Output: A partition of A[l..r], with the split position returned as this function’s value
P A[l]
i l; j r + 1;
Repeat
repeat i i + 1 until A[i]>=p //left-right scan
repeat j j – 1 until A[j] <= p//right-left scan
if (i < j) //need to continue with the scan
swap(A[i], a[j])
until i >= j //no need to scan
swap(A[l], A[j])
return j
Quicksort Example
15 22 13 27 12 10 20 25
Efficiency of Quicksort
Based on whether the partitioning is balanced.
Best case: split in the middle — Θ( n log n)
C(n) = 2C(n/2) + Θ(n) //2 subproblems of size n/2 each
ALGORITHM BinarySearch(A[0..n-1], K)
//Implements nonrecursive binary search
//Input: An array A[0..n-1] sorted in ascending order and
// a search key K
//Output: An index of the array’s element that is equal to K
// or –1 if there is no such element
l 0, r n – 1
while l r do //l and r crosses over can’t find K.
m (l + r) / 2
if K = A[m] return m //the key is found
else
if K < A[m] r m – 1 //the key is on the left half of the array
else lm+1 // the key is on the right half of the array
return -1
Binary Search – a Recursive Algorithm
ALGORITHM BinarySearchRecur(A[0..n-1], l, r, K)
if l > r //base case 1: l and r cross over can’t find K
return –1
else
m (l + r) / 2
if K = A[m] //base case 2: K is found
return m
else //general case: divide the problem.
if K < A[m] //the key is on the left half of the array
return BinarySearchRecur(A[0..n-1], l, m-1, K)
Efficiency
C(n) Θ( log n)
Binary Tree Traversals
Definitions
A binary tree T is defined as a finite set of nodes
that is either empty or consists of a root and two
disjoint binary trees TL and TR called, respectively,
the left and right subtree of the root.
Examples.
The height of a tree is defined as the length of the
longest path from the root to a leaf.
Problem: find the height of a binary tree.
Find the Height of a Binary Tree
ALGORITHM Height(T)
//Computes recursively the height of a binary tree
//Input: A binary tree T
//Output: The height of T
if T =
return –1
else
return max{Height(TL), Height(TR)} + 1
Base case?
Efficiency? C(n) = n + x =2n + 1
Exercises
The following algorithm seeks to compute the
number of leaves in a binary tree. Is it correct?
ALGORITHM LeafCounter(T)
//Computes recursively the number of leaves in a binary tree
//Input: A binary tree T
//Output: The number of leaves in T
if T =
return 0
else
return LeafCounter(TL)+ LeafCounter(TR)
Binary Tree Traversals– preorder, inorder,
and postorder traversal
Binary tee traversal: visit all nodes of a binary tree recursively.
Write a recursive preorder binary tree traversal algorithm.
Algorithm Preorder(T)
//Implement the preorder traversal of a binary tree
//Input: Binary tree T (with labeled vertices)
//Output: Node labels listed in preorder
if T ‡
write label of T’s root
Preorder(TL)
Preorder(TR)
How many recursive calls ?
Multiplication of Large Integers (1)
Multiplication of two n-digit integers.
# of digit # of additions
multiplications
23 * 14 (n = 2) 4 3
123 * 456(n = 3) 9 8
… … …
Multiplication of 2 n2 n2-1
n-bit integers
Multiplication of a m-digit integer and a n-digit integer ( where n > m) can be
modeled as the multiplication of 2 n-digit integers (by padding n – m 0s before
the first digit of the m-digit integer)
Multiplication of Large Integers (2)
Do we have to make n2 digit multiplications?
n = 2: we only need 3 digit multiplications.
What about n > 2?
Assume n is an even number. (What about the situations where
n is an odd number?)
Recursively compute the product of two integers, with the
integer size (# of digits) reduced by half each time.
Efficiency of the recursive algorithm
Recurrence Relation
Strassen’s Matrix Multiplication
Brute-force algorithm
c00 c01 a00 a01 b00 b01
= *
c10 c11 a10 a11 b10 b11
m1 + m4 - m5 + m7 m 3 + m5
=
m2 + m 4 m1 + m3 - m2 + m6
m1 = (a00 + a11) * (b00 + b11)
m2 = (a10 + a11) * b00
m3 = a00 * (b01 - b11)
m4 = a11 * (b10 - b00)
m5 = (a00 + a01) * b11
7 multiplications
m6 = (a10 - a00) * (b00 + b01)
m7 = (a01 - a11) * (b10 + b11) 18 additions
Strassen’s Matrix Multiplication
Two nxn matrices, where n is a power of two (What
about the situations where n is not a power of two?)
M1 + M 4 - M5 + M 7 M 3 + M5
=
M2 + M4 M1 + M 3 - M 2 + M 6
Submatrices:
M1 = (A00 + A11) * (B00 + B11)
Number of additions