Divide and Conquer
Divide and Conquer
Intro
• Divide-and-conquer is probably the best-known general algorithm design
technique.
• Though its fame may have something to do with its catchy name, it is well
deserved: quite a few very efficient algorithms are specific
implementations of this general strategy.
• we will now scan the subarray from both ends, comparing the
subarray’s elements to the pivot.
• Many problems about binary trees can be solved by applying the divide-
• and-conquer technique.
• Recall that the height is defined as the length of the longest path from the
root to a leaf.
• Hence, it can be computed as the maximum of the heights of the root’s left
and right subtrees plus 1. The height of the empty tree as − 1.
Cont.,
Three Classic Tree Traversals
• The most important divide-and-conquer algorithms for
binary trees are the three classic traversals:
preorder, inorder, and postorder.
• All three traversals visit nodes of a binary tree
recursively, i.e., by visiting the tree’s root and its left and
right subtrees.
• They differ only by the timing of the root’s visit:
In the preorder traversal: the root is visited before the left and
right subtrees are visited (in that order).
In the inorder traversal: the root is visited after visiting its left
subtree but before visiting the right subtree.
In the postorder traversal: the root is visited after visiting the left
and right subtrees (in that order).
Cont.,
• Finally, we should note
that, obviously, not all
questions about binary
trees require traversals of
both left and right
subtrees.
• For example, the search
and insert operations for a
binary search tree require
processing only one of the
two subtrees.
Multiplication of Large
Integers and Strassen’s
Matrix Multiplication
Multiplication of Large Integers
2 ∗ 4 + 3 ∗ 1 = (2 + 3) ∗ (1 + 4) −
2∗1−3∗4
Cont.,
• Of course, there is nothing special about the numbers we just
multiplied. For any pair of two-digit numbers a = a1a0 and b =
b1b0, their product c can be computed by the formula
Cont.,
• How many digit multiplications does this algorithm
make? Since multiplication of n-digit numbers requires
three multiplications of n/2-digit numbers, the
recurrence for the number of multiplications M(n) is
Cont.,
Strassen’s Matrix Multiplication
• Now that we have seen that the divide-and-conquer
approach can reduce the number of one-digit
multiplications in multiplying two integers.
• we should not be surprised that a similar feat can be
accomplished for multiplying matrices.
• Such an algorithm was published by V. Strassen in 1969
[Str69].
• The principal insight of the algorithm lies in the
discovery that we can find the product C of two 2 × 2
matrices A and B with just seven multiplications as
opposed to the eight required by the brute-force
algorithm.
C11=m1+m4-m5+m7
C12=m3+m5
C21=m2+m4
C22=m1+m3-m2+m6