Divide and Conquer
Divide and Conquer
I. General Method
• Given a function to compute on inputs the divide-and-
conquer strategy, splits the inputs into distinct subsets,
1 < ≤ , yielding subproblems.
• These sub problems must be solved, and then a method
must be found to combine sub solutions into a solution of
the whole.
• If the sub problems are still relatively large, then the divide-
and-conquer strategy can possibly be reapplied.
• Often the sub problems resulting from a divide-and-conquer
design are of the same type as the original problem. Hence,
reapplication of the divide-and-conquer principle is naturally
expressed by a recursive algorithm.
• Now smaller and smaller sub problems of the same kind are
generated until eventually sub problems that are small
enough to be solved without splitting are produced.
• The control abstraction for the general method of divide-
and-conquer strategy is given in Algorithm 3.1.
• is a Boolean-valued function that determines whether
the input size is small enough that the answer can be
computed without splitting. If this is so, the function is
invoked. Otherwise the problem is divided into smaller sub
problems. These sub problems , ,…, are solved by
recursive applications of . is a function that
determines the solution to using the solutions to the sub
problems.
• If the size of is and the sizes of the sub problems are
, , … , , respectively, then the computing time of is
described by the recurrence relation:
= (
+ + ⋯+ " + # $ℎ &'
where is the time for on any input of size and
is the time to compute the answer directly for small
inputs. The function # is the time for dividing and
combining the solutions to the sub problems.
• For divide-and-conquer based algorithms that produce sub
problems of the same type as the original problem, we describe
such algorithms using recursion.
• The complexity of many divide-and-conquer algorithms is
given by recurrences of the form
1 =1
=) (
* ++# >1
• The first comparison is . with 576. If . < 576, then the next comparison
is with 536; similarly, if . > 576, then the next comparison is with 5116.
• Each path through the tree represents a sequence of comparisons in the
binary search method.
• If . is the present, then the algorithm will end at one of the circular
nodes that lists the index into the array where . was found.
• If . is not present, the algorithm will terminate at one of the square
nodes.
• Circular nodes are called internal nodes and square nodes are called
external nodes.
• If 2 9 ≤ < 2 , then all circular nodes are at levels 1,2, … , whereas all
square nodes are at levels and + 1.
• The number of element comparisons needed to terminate at a circular
node on level is whereas the number of element comparisons needed
to terminate at a square node at level is − 1.
In conclusion, the time complexity of binary search for successful
searches is ? log , and the time complexity of binary search for
unsuccessful searches is 2 log .
MERGE SORT
=? log
QUICKSORT
• In merge sort, the file 51: 6 was divided at its midpoint into
sub arrays which were independently sorted and later merged.
• In quicksort, the division into two sub arrays is made so that
the sorted sub arrays do not need to be merged later.
• This is accomplished by rearranging the elements in
51: 6 such that 5 6 ≤ 5/6 for all between 1 and and all
/ between + 1 and for some , 1 ≤ ≤ . Thus, the
elements in 51: 6 and 5 + 1: 6 can be independently sorted.
No merge is needed.
• The rearrangement of the elements is accomplished by picking
some element of 5 6, say $ = 5 6, and then reordering the other
elements so that all elements appearing before $ in 51: 6 are
less than or equal to $ and all elements appearing after $ are
greater than or equal to $. This rearranging is referred to as
partitioning.
In analyzing, Quicksort, we count only the number of element
comparisons .
Y = +1 + ∑ [ [E Y −1 + Y −
E
(1)
Y = + 1 + 25 Y 0 + Y 1 + ⋯+ Y −1 6 (2)
−1 Y −1 = − 1 + 25 Y 0 + Y 1 + ⋯+ Y −2 6 (3)
Y − −1 Y −1 =2 +2 Y −1 (4)
or
]^ E ]^ E9
= +
E8 E E8
(5)
_^ ` _ `db e
c ^ 8
`ab ` `ab
_^ `de e e
c 8 8
`db ` `ab
_^ `df e e e
c 8 8 8 (6)
`de `db ` `ab
⋮
_ b b
c ^ 8 ∑fhgh`ab
e g
b
c ∑fhgh`ab
g
Since
E8
2 ∑i[ [E8 ≤j .= l +1 − l2
k
Finally, Y ≤2 +1 5 l +2 − l 26 = m S nop S
NAÏVE MATRIX MULTIPLICATION
,/ = s , q ,/
[ [E
Imagine that A and B are each partitioned into four square sub matrices, each
E E
sub matrix having dimensions × .
≤2
=) (
8 * ++L >2
2