L 14 Divide&Conquer
L 14 Divide&Conquer
Introduction
• General techniques that often yield effective algorithms for solving large classes
of problems.
• Some of the more important techniques are (i) Greedy (ii) Dynamic Programming
and (iii) Divide & Conquer
• Before using any technique, better ask about the quality of the solution obtained.
• Remember that for NP-Complete problems, these or any other known techniques
will NOT yield efficient solutions.
Hard Problem
• Each player must play one match per day for n – 1 days.
• The top right corner is obtained by pitting the high order players
against low order players, by cyclically permuting the orders.
• The bottom right corner is similarly obtained by pitting the low order
players against high order players, by cyclically permuting the
orders.
1 2 4 5 6
1 23 3 5 76
2 7
4 8
3
1 4 6 7
4 8
Player 37
5 6 1 54
6 4 1 7 38
8 52
7 28
5 2 61
8 3 2 8 45
63
Will7 play player
National University1Of Computer
7 & Emerging Sciences
Divide & Conquer
1 1 2 1 2 4 5 6
1 2 1 23 3 1 23 3 5 76
7
2 1 2 1 4 2
4 8
3 4 1 3
42 1 4 6 7
4 3 3 4 8
5 6 37 1 54
2
6 4 1 7 38
1 8 52
7 28
5 2 61
8 3 2 8 45
7 63
National University Of Computer & Emerging Sciences
Divide & Conquer
Min-Max Cont’d
• Divide-and-Conquer:
– For n ≤ 2, make 1 comparison
– For large n, divide set into two smaller sets and determine
largest/smallest element for each set
– Compare largest/smallest from two subsets to determine
smallest/largest of combined sets
– Do recursively
• Let n =8
• How many comparisons?
Min-Max Cont’d
Misc
• Divide-and-Conquer methodology naturally leads to
recursive algorithms
• In general, implement algorithm as recursive
program
– Few instances where non-recursive is better
XY = (A2n/2 + B) (C2n/2 + D)
= AC2n + (AD + BC) 2n/2 + BD ............ (*)
• The above computation of XY reduces to four multiplications of (n/
2)-bit numbers plus some additions and shifts (multiplications by p
owers of 2).
A better Algorithm
• So, the multiplication of two n-bit numbers remains O
(n2) if the number of half-size subproblems remains 4
• In order to reduce the time complexity, we attempt to
reduce the number of subproblems (i.e., multiplication
s ) by a trick which uses more additions and shifts, spe
cifically, by reducing one multiplication with many addit
ions and shifts.
• This trick pays off asymptotically, i.e., when n is
large.
Analysis
Example
2. The constant ignored is very large i.e. 500. Useful for large
multiplications.