0% found this document useful (0 votes)
29 views52 pages

3-Chapter Three - Divide and Conquer

Chapter 3 discusses the Divide and Conquer design paradigm, which involves dividing a problem into subproblems, solving them recursively, and combining their solutions. Key algorithms highlighted include Merge Sort, Binary Search, Powering a Number, Fibonacci Numbers, and Matrix Multiplication, including Strassen's Algorithm for improved efficiency. The chapter also revisits the Master Theorem for analyzing the time complexity of these algorithms.

Uploaded by

mersha abdisa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views52 pages

3-Chapter Three - Divide and Conquer

Chapter 3 discusses the Divide and Conquer design paradigm, which involves dividing a problem into subproblems, solving them recursively, and combining their solutions. Key algorithms highlighted include Merge Sort, Binary Search, Powering a Number, Fibonacci Numbers, and Matrix Multiplication, including Strassen's Algorithm for improved efficiency. The chapter also revisits the Master Theorem for analyzing the time complexity of these algorithms.

Uploaded by

mersha abdisa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Chapter 3 – Divide and

Conquer
Outline

Divide and Conquer:


Binary Search,
Powering a Number,
Fibonacci Numbers,
Matrix Multiplication,
Strassen’s Algorithm.

08/26/25 2
Divide-and-Conquer Design
Paradigm

 The divide-and-conquer method is a powerful strategy for


designing asymptotically efficient algorithms.
 The divide-and-conquer design paradigm:
 Divide the problem (instance) into one or more subproblems.
 Conquer the subproblems by solving them recursively.
 Combine subproblem solutions to form a solution to the original
problem.
Merge Sort

 Divide the subarray A[p:r] to be sorted into two adjacent


subarrays, each of half the size.
 To do so, compute the midpoint q of A[p:r] (taking the average of
p and r), and divide A[p: r] into subarrays A[p:q] and A[q+1: r].

 Conquer by sorting each of the two subarrays A[p:q] and


A[q+1:r] recursively using merge sort.
 Combine by merging the two sorted subarrays A[p:q] and
A[q+1:r] back into A[p:r], producing the sorted answer.
Merge Sort
Merge Sort

 Divide: Trivial. (takes constant time Θ(1))


 Conquer: Recursively sort 2 subarrays (each of size n/2 ).
 Combine: Linear-time merge. (Takes Θ(n))

T(n) = 2 T(n/2) + (n)

# subproblems work dividing


subproblem size and combining
Master Theorem (reprise)

T(n) = a T(n/b) + f (n)


CASE 1: f (n) = O(nlogba – ), constant  > 0
 T(n) = (nlogba) .[watershed function grows asymptotically faster than the
driving function]

CASE 2: f (n) = (nlogba lgkn), constant k  0


 T(n) = (nlogba lgk+1n) .[if the two functions grow at nearly the same
asymptotic rate]

CASE 3: f (n) = (nlogba +  ), constant  > 0, and


regularity condition
 T(n) = (f(n)) .[the driving function f(n) grows asymptotically faster
Master Theorem
(reprise)
T(n) = a T(n/b) + f (n)
CASE 1: f (n) = O(nlogba – ), constant  > 0
 T(n) = (nlogba) .
CASE 2: f (n) = (nlogba lgkn), constant k  0
 T(n) = (nlogba lgk+1n) .
CASE 3: f (n) = (nlogba +  ), constant  > 0, and regularity condition
 T(n) = ( f (n)) .
Merge sort: a = 2, b = 2  nlogba = nlog22 = n
 CASE 2 (k = 0)  T(n) = (n lg n) .
Binary Search

 Find an element in a sorted array:


 Divide: Check middle element.
 Conquer: Recursively search 1 subarray.
 Combine: Trivial.
Binary Search

 Find an element in a sorted array:


 Divide: Check middle element.
 Conquer: Recursively search 1 subarray.
 Combine: Trivial.
 Example: Find 9

3 5 7 8 9 12
15
Binary Search

 Find an element in a sorted array:


 Divide: Check middle element.
 Conquer: Recursively search 1 subarray.
 Combine: Trivial.
 Example: Find 9

3 5 7 8 9 12
15
Binary Search

 Find an element in a sorted array:


 Divide: Check middle element.
 Conquer: Recursively search 1 subarray.
 Combine: Trivial.
 Example: Find 9

9 12
3 5 7 8
15
Binary Search

 Find an element in a sorted array:


 Divide: Check middle element.
 Conquer: Recursively search 1 subarray.
 Combine: Trivial.
 Example: Find 9

9 12
3 5 7 8
15
Binary Search

 Find an element in a sorted array:


 Divide: Check middle element.
 Conquer: Recursively search 1 subarray.
 Combine: Trivial.
 Example: Find 9

9
3 5 7 8 12 15
Binary Search

 Find an element in a sorted array:


 Divide: Check middle element.
 Conquer: Recursively search 1 subarray.
 Combine: Trivial.
 Example: Find 9

9
3 5 7 8 12 15
Recurrence for Binary
Search

T(n) = 1 T(n/2) + (1)

# subproblems work dividing


and combining
subproblem size
Recurrence for Binary
Search

T(n) = 1 T(n/2) + (1)

# subproblems work dividing


and combining
subproblem size

nlogba = nlog21 = n0 = 1  CASE 2 (k = 0)


 T(n) = (lg n)
.
Powering a Number

 Problem: Compute an, where n  N. (2 5 = 22 * 22 * 2)

 Naive algorithm: (n).


Powering a Number

 Problem: Compute an, where n  N.

 Naive algorithm: (n).

 Divide-and-conquer algorithm:

a n/2  a n/2
if n is even;
a =
n
if n is odd.
a (n–1)/2  a (n–1)/2 
Powering a Number

 Problem: Compute an, where n  N.

 Naive algorithm: (n).

 Divide-and-conquer algorithm:

a n/2  a n/2
if n is even;
a =
n
if n is odd.
a (n–1)/2  a (n–1)/2 
T(n) = T(n/2) + (1)  T(n) = (lg n) .

a
Fibonacci Numbers

 Recursive definition:

0
Fn = if n = 0;
1
if n = 1;
Fn–1 + Fn–2
if n  2.

0
1
Fibonacci Numbers

 Recursive definition:

0 if
Fn = n = 0;
1 if
n = 1;
Fn–1 + Fn–2 if
n  2.
 Naive recursive algorithm: ( n) (exponential time),
where  = (15)/02 = 1.618 is the golden ratio 1(for n > =
12).
1 2
Computing Fibonacci
Numbers

 Bottom-up:
 Compute F0, F1, F2, …, Fn in order, forming each number
by summing the two previous.
 Running time: (n).
Computing Fibonacci
Numbers

 Bottom-up:
 Compute F0, F1, F2, …, Fn in order, forming each number
by summing the two previous.
 Running time: (n).
 Naive recursive squaring:
 Fn =  n/ 5 rounded to the nearest integer.
 Recursive squaring: (lg n) time.
 This method is unreliable, since floating-point arithmetic is
prone to round-off errors.
Recursive Squaring

 Theorem:
Recursive Squaring

 Theorem:

 Algorithm: Recursive squaring.


Time = (lg n) .
Recursive Squaring

 Theorem:

 Algorithm: Recursive squaring.


Time = (lg n) .
 Proof of theorem. (Induction on n.)

 Base (n = 1):
Recursive Squaring

 Inductive step (n  2):


Matrix Multiplication

Input: A = [aij], B = [bij].


i, j = 1, 2,… , n.
Output: C = [cij] = A B.

n
cij  aik  bkj
k 1
Standard Algorithm

for i  1 to n
do for j  1 to n
do cij  0
for k  1 to n
do cij  cij + aik bkj
Standard Algorithm

for i  1 to n
do for j  1 to n
do cij  0
for k  1 to n
do cij  cij + aik bkj

Running time = (n3)


Divide-and-Conquer
Algorithm
IDEA:
nn matrix = 22 matrix of (n/2)n/2) submatrices:

C = A B
r = ae + bg
s = af + bh 8 mults of (n/2)n/2) submatrices
t = ce + dg 4 adds of (n/2)n/2) submatrices
u = cf + dh
Divide-and-Conquer
Algorithm
IDEA:
nn matrix = 22 matrix of (n/2)n/2) submatrices:

C = A B
r = ae + bg recursive
s = af + bh 8 mults of (n/2)n/2) submatrices
t = ce + dg ^
4 adds of (n/2)n/2) submatrices
u = cf + dh
Analysis of D&C Algorithm

T(n) = 8 T(n/2) +
(n2)
# submatrices work adding
submatrix size
submatrices
Analysis of D&C Algorithm

T(n) = 8 T(n/2) +
(n2)
# submatrices work adding
submatrix size
submatrices
 CASE 1  T(n) = (n3).
nlogba = nlog28 = n3
Analysis of D&C Algorithm

T(n) = 8 T(n/2) +
(n2)
# submatrices work adding
submatrix size
submatrices
 CASE 1  T(n) = (n3).
nlogba = nlog28 = n3
No better than the ordinary algorithm.
Strassen’s Idea
Improved Matrix Multiplication:
 Multiply 22 matrices with only 7 recursive mults.
Strassen’s Idea
Improved Matrix Multiplication:
 Multiply 22 matrices with only 7 recursive mults.
P1 = a  ( f – h)
P2 = (a + b)  h
P3 = (c + d)  e
P4 = d  (g – e)
P5 = (a + d)  (e + h)
P6 = (b – d)  (g + h)
P7 = (a – c)  (e + f )
Strassen’s Idea
Improved Matrix Multiplication:
 Multiply 22 matrices with only 7 recursive mults.
P1 = a  ( f – h) r = P5 + P4 – P2 + P6
P2 = (a + b)  h s = P1 + P2
P3 = (c + d)  e t = P3 + P4
P4 = d  (g – e) u = P5 + P1 – P3 – P7
P5 = (a + d)  (e + h)
P6 = (b – d)  (g + h)
P7 = (a – c)  (e + f )
Strassen’s Idea
Improved Matrix Multiplication:
 Multiply 22 matrices with only 7 recursive mults.
P1 = a  ( f – h) r = P5 + P4 – P2 + P6
P2 = (a + b)  h s = P1 + P2
P3 = (c + d)  e t = P3 + P4
P4 = d  (g – e) u = P5 + P1 – P3 – P7
P5 = (a + d)  (e + h) 7 mults, 18 adds/subs.
P6 = (b – d)  (g + h) Note: No reliance on
P7 = (a – c)  (e + f ) commutativity of mult!
Strassen’s Idea
Improved Matrix Multiplication:
 Multiply 22 matrices with only 7 recursive mults.

P1 = a  ( f – h) r = P5 + P4 – P2 + P6
P2 = (a + b)  h = (a + d) (e + h)
P3 = (c + d)  e + d (g – e) – (a + b) h
P4 = d  (g – e) + (b – d) (g + h)
P5 = (a + d)  (e + h) = ae + ah + de + dh
+ dg –de – ah – bh
P6 = (b – d)  (g + h)
P7 = (a – c)  (e + f ) + bg + bh – dg – dh
= ae + bg
Strassen’s Algorithm

1. Divide: Partition A and B into (n/2)(n/2)


submatrices. Form terms to be multiplied using +
and – .
2. Conquer: Perform 7 multiplications of (n/2)(n/2)
submatrices recursively.
3. Combine: Form C using + and – on (n/2)(n/2)
submatrices.
Strassen’s Algorithm

1. Divide: Partition A and B into (n/2)(n/2)


submatrices. Form terms to be multiplied using +
and – .
2. Conquer: Perform 7 multiplications of (n/2)(n/2)
submatrices recursively.
3. Combine: Form C using + and – on (n/2)(n/2)
submatrices.
T(n) = 7 T(n/2) + (n2)
Analysis of Strassen

T(n) = 7 T(n/2) + (n2)


Analysis of Strassen

T(n) = 7 T(n/2) + (n2)

 CASE 1  T(n) = (nlg 7).


nlogba = nlog27  n2.81
Analysis of Strassen

T(n) = 7 T(n/2) + (n2)

 CASE 1  T(n) = (nlg 7).


nlogba = nlog27  n2.81
 The number 2.81 may not seem much smaller than 3,
but because the difference is in the exponent, the
impact on running time is significant.
 In fact, Strassen’s algorithm beats the ordinary
algorithm on today’s machines for n  32 or so.
Analysis of Strassen

T(n) = 7 T(n/2) + (n2)

 CASE 1  T(n) = (nlg 7).


nlogba = nlog27  n2.81
 The number 2.81 may not seem much smaller than 3,
but because the difference is in the exponent, the
impact on running time is significant.
 In fact, Strassen’s algorithm beats the ordinary
algorithm on today’s machines for n  32 or so.
 Best to date (of theoretical interest only): (n2.376L).
Conclusion

 Divide and conquer is just one of several powerful


techniques for algorithm design.
 Divide-and-conquer algorithms can be analyzed
using recurrences and the master method (so
practice this math).
 The divide-and-conquer strategy often leads to
efficient algorithms.
Question & Answer

08/26/25 49
Thank You !!!

08/26/25 50
Exercises - 3

08/26/25 51
Exercises - 3

 For each of the following recurrences, sketch its recursion


tree, and guess a good asymptotic upper bound on its
solution.
T(n) = T(n/2) + n3.
T(n) = 3T(n/2) + n.
Use the master method to give tight asymptotic bounds for
the following recurrences.
 T(n) = 2T(n/4) + 1.
T(n) = 2T(n/4) + n.

08/26/25 52

You might also like