# Divide and Conquer Divide

Shared by:
Categories
-
Stats
views:
74
posted:
3/26/2011
language:
English
pages:
155
Document Sample

```							Divide and Conquer

Chapter 5

CSci 3110 • Divide and Conquer • 1/3
Overview

Design principle:
Divide and conquer

Proof technique:
Induction, induction, induction

Analysis technique:
Recurrence relations

Problems:
Merge sort
Selection
Counting inversions
Integer multiplication

CSci 3110 • Divide and Conquer • 2/3
Merge Sort

M ERGE -S ORT(A, p, r)
1 if p < r
2     then q ← ⌊ p+r ⌋
2
3          M ERGE -S ORT(A, p, q)
4          M ERGE -S ORT(A, q + 1, r)
5          M ERGE(A, p, q, r)

CSci 3110 • Divide and Conquer • 3/3
Merge Sort

M ERGE -S ORT(A, p, r)                     M ERGE(A, p, q, r)
1 if p < r                                 1 n1 ← q − p + 1
2     then q ← ⌊ p+r ⌋                     2 n2 ← r − q
2
3          M ERGE -S ORT(A, p, q)          3 for i ← 1 to n1
4          M ERGE -S ORT(A, q + 1, r)      4    do L[i] ← A[p + i − 1]
5          M ERGE(A, p, q, r)              5 for i ← 1 to n2
6    do R[i] ← A[q + i]
7 L[n1 + 1] ← ∞
8 R[n2 + 1] ← ∞
9 i←1
10 j ← 1
11 for k ← p to r
12    do if L[i] < R[j]
13           then A[k] ← L[i]
14                i←i+1
15           else A[k] ← R[j]
16                j ←j+1

CSci 3110 • Divide and Conquer • 3/3
Merge Sort: The Micro-View

87       4    17 11      9       13       7       5

Divide

87           4       17 11           9    13          7       5

Divide

87        4           17 11        9      13           7        5

Divide

87        4           17    11    9        13          7            5

Do nothing

87        4           17    11    9        13          7            5

Merge

4       87           11 17        9      13           5        7

Merge

4       11 17 87                 5        7       9       13

Merge

4        5     7     9   11 13 17 87

CSci 3110 • Divide and Conquer • 4/3
Merge Sort: The Macro-View

87       4    17 11      9       13       7       5

Divide

87       4       17 11           9    13          7       5

Recurse

4        11 17 87                5        7       9       13

Merge

4       5     7     9   11 13 17 87

CSci 3110 • Divide and Conquer • 5/3

Divide the input instance into one or
more smaller instances.

Recursively solve these smaller input
instances.

Combine the solutions produced by the
recursive calls into a solution to the
original instance.

CSci 3110 • Divide and Conquer • 6/3

Divide the input instance into one or
more smaller instances.

Recursively solve these smaller input
instances.

Combine the solutions produced by the
recursive calls into a solution to the
original instance.

In most algorithms, either the divide or the combine step is trivial:
The divide step in Merge Sort is trivial
The combine step in Quick Sort is trivial

CSci 3110 • Divide and Conquer • 6/3
Correctness of Merge Sort

Lemma: Merge Sort correctly sorts any input array.

CSci 3110 • Divide and Conquer • 7/3
Correctness of Merge Sort

Lemma: Merge Sort correctly sorts any input array.

Proof by induction:

Base case: (n = 1)

CSci 3110 • Divide and Conquer • 7/3
Correctness of Merge Sort

Lemma: Merge Sort correctly sorts any input array.

Proof by induction:

Base case: (n = 1)

A one-element array is already sorted.

CSci 3110 • Divide and Conquer • 7/3
Correctness of Merge Sort

Lemma: Merge Sort correctly sorts any input array.

Proof by induction:

Base case: (n = 1)

A one-element array is already sorted.

Inductive step: (n > 1)

CSci 3110 • Divide and Conquer • 7/3
Correctness of Merge Sort

Lemma: Merge Sort correctly sorts any input array.

Proof by induction:

Base case: (n = 1)

A one-element array is already sorted.

Inductive step: (n > 1)

The left and right half each have size less than n.
By the inductive hypothesis, the recursive calls sort
them correctly.
Merge correctly merges two sorted sequences.

CSci 3110 • Divide and Conquer • 7/3
Correctness of D&C Algorithms

Divide-and-conquer algorithms are the algorithmic incarnation of
induction:

Base case: Whenever we don’t recurse, but produce the answer directly
(often trivially).

Inductive step: Reduce the solution of a given instance to the solution of
smaller instances, by recursing.

CSci 3110 • Divide and Conquer • 8/3
Correctness of D&C Algorithms

Divide-and-conquer algorithms are the algorithmic incarnation of
induction:

Base case: Whenever we don’t recurse, but produce the answer directly
(often trivially).

Inductive step: Reduce the solution of a given instance to the solution of
smaller instances, by recursing.

Induction is the natural proof method for divide-and-conquer
algorithms.

CSci 3110 • Divide and Conquer • 8/3
Recurrence Relations

A recurrence relation deﬁnes the value of a function f in terms of its
values for smaller arguments.

CSci 3110 • Divide and Conquer • 9/3
Recurrence Relations

A recurrence relation deﬁnes the value of a function f in terms of its
values for smaller arguments.

Examples:

Fibonacci numbers:

1                       if n ≤ 2
f (n) =
f (n − 1) + f (n − 2)   if n > 2

CSci 3110 • Divide and Conquer • 9/3
Recurrence Relations

A recurrence relation deﬁnes the value of a function f in terms of its
values for smaller arguments.

Examples:

Fibonacci numbers:

1                       if n ≤ 2
f (n) =
f (n − 1) + f (n − 2)   if n > 2

Binomial coefﬁcients:

1                             if k = 1 or k = n
B(n, k) =
B(n − 1, k − 1) + B(n − 1, k) otherwise

CSci 3110 • Divide and Conquer • 9/3
A Recurrence for Merge Sort

Analysis:

Recurrence:

T (n) =

CSci 3110 • Divide and Conquer • 10/3
A Recurrence for Merge Sort

Analysis:
If n = 0 or n = 1, we spend constant time to ﬁgure out that there is
nothing to do and then exit.

Recurrence:

Θ(1)                          if n ≤ 1
T (n) =

CSci 3110 • Divide and Conquer • 10/3
A Recurrence for Merge Sort

Analysis:
If n = 0 or n = 1, we spend constant time to ﬁgure out that there is
nothing to do and then exit.
If n > 1, we

Recurrence:

Θ(1)                          if n ≤ 1
T (n) =
if n > 1

CSci 3110 • Divide and Conquer • 10/3
A Recurrence for Merge Sort

Analysis:
If n = 0 or n = 1, we spend constant time to ﬁgure out that there is
nothing to do and then exit.
If n > 1, we
Spend constant time to compute the middle index,

Recurrence:

Θ(1)                          if n ≤ 1
T (n) =
Θ(1)   if n > 1

CSci 3110 • Divide and Conquer • 10/3
A Recurrence for Merge Sort

Analysis:
If n = 0 or n = 1, we spend constant time to ﬁgure out that there is
nothing to do and then exit.
If n > 1, we
Spend constant time to compute the middle index,
Make one recursive call on the left half, whose size is ⌈n/2⌉,

Recurrence:

Θ(1)                           if n ≤ 1
T (n) =
T (⌈n/2⌉)            + Θ(1)    if n > 1

CSci 3110 • Divide and Conquer • 10/3
A Recurrence for Merge Sort

Analysis:
If n = 0 or n = 1, we spend constant time to ﬁgure out that there is
nothing to do and then exit.
If n > 1, we
Spend constant time to compute the middle index,
Make one recursive call on the left half, whose size is ⌈n/2⌉,
Make one recursive call on the right half, whose size is ⌊n/2⌋,

Recurrence:

Θ(1)                           if n ≤ 1
T (n) =
T (⌈n/2⌉) + T (⌊n/2⌋) + Θ(1)   if n > 1

CSci 3110 • Divide and Conquer • 10/3
A Recurrence for Merge Sort

Analysis:
If n = 0 or n = 1, we spend constant time to ﬁgure out that there is
nothing to do and then exit.
If n > 1, we
Spend constant time to compute the middle index,
Make one recursive call on the left half, whose size is ⌈n/2⌉,
Make one recursive call on the right half, whose size is ⌊n/2⌋,
Spend linear time to merge the two sorted sequences.

Recurrence:

Θ(1)                           if n ≤ 1
T (n) =
T (⌈n/2⌉) + T (⌊n/2⌋) + Θ(1)   if n > 1

CSci 3110 • Divide and Conquer • 10/3
A Recurrence for Merge Sort

Analysis:
If n = 0 or n = 1, we spend constant time to ﬁgure out that there is
nothing to do and then exit.
If n > 1, we
Spend constant time to compute the middle index,
Make one recursive call on the left half, whose size is ⌈n/2⌉,
Make one recursive call on the right half, whose size is ⌊n/2⌋,
Spend linear time to merge the two sorted sequences.

Recurrence:

Θ(1)                         if n ≤ 1
T (n) =
T (⌈n/2⌉) + T (⌊n/2⌋) + Θ(n) if n > 1

CSci 3110 • Divide and Conquer • 10/3
A Recurrence for Binary Search

Analysis:

Recurrence:

T (n) =

CSci 3110 • Divide and Conquer • 11/3
A Recurrence for Binary Search

Analysis:
If n = 0 or n = 1, we spend constant time to test whether we have found
the desired element.

Recurrence:

Θ(1)             if n ≤ 1
T (n) =

CSci 3110 • Divide and Conquer • 11/3
A Recurrence for Binary Search

Analysis:
If n = 0 or n = 1, we spend constant time to test whether we have found
the desired element.
If n > 1, we

Recurrence:

Θ(1)             if n ≤ 1
T (n) =
if n > 1

CSci 3110 • Divide and Conquer • 11/3
A Recurrence for Binary Search

Analysis:
If n = 0 or n = 1, we spend constant time to test whether we have found
the desired element.
If n > 1, we
Spend constant time to ﬁnd the middle element of the array and
compare it to the search key,

Recurrence:

Θ(1)            if n ≤ 1
T (n) =
Θ(1) if n > 1

CSci 3110 • Divide and Conquer • 11/3
A Recurrence for Binary Search

Analysis:
If n = 0 or n = 1, we spend constant time to test whether we have found
the desired element.
If n > 1, we
Spend constant time to ﬁnd the middle element of the array and
compare it to the search key,
Make one recursive call on one of the two halves.

Recurrence:

Θ(1)             if n ≤ 1
T (n) =
T (⌈n/2⌉) + Θ(1) if n > 1

CSci 3110 • Divide and Conquer • 11/3
A Recurrence for Binary Search

Analysis:
If n = 0 or n = 1, we spend constant time to test whether we have found
the desired element.
If n > 1, we
Spend constant time to ﬁnd the middle element of the array and
compare it to the search key,
Make one recursive call on one of the two halves.

Recurrence:

Θ(1)             if n ≤ 1
T (n) =
T (⌈n/2⌉) + Θ(1) if n > 1

Observe how we use an inductive description of the running time of an
algorithm that operates inductively. This deserves to be called natural.

CSci 3110 • Divide and Conquer • 11/3
Simpliﬁed Recurrence Notation

The recurrences we use to analyze algorithms will have a base case of

T (n) ≤ c ∀ n ≤ n0 .

The exact constants only affect the constant factors in the running time.

CSci 3110 • Divide and Conquer • 12/3
Simpliﬁed Recurrence Notation

The recurrences we use to analyze algorithms will have a base case of

T (n) ≤ c ∀ n ≤ n0 .

The exact constants only affect the constant factors in the running time.
Floors and ceilings only affect constant factors.

CSci 3110 • Divide and Conquer • 12/3
Simpliﬁed Recurrence Notation

The recurrences we use to analyze algorithms will have a base case of

T (n) ≤ c ∀ n ≤ n0 .

The exact constants only affect the constant factors in the running time.
Floors and ceilings only affect constant factors.

So we are lazy and write:
Merge sort: T (n) = 2 T (n/2) + Θ(n)
Binary search: T (n) = T (n/2) + Θ(1)

CSci 3110 • Divide and Conquer • 12/3
Solving Recurrences

Given two algorithms A and B for the same problem with running times

TA (n) = 2 T (n/2) + Θ(n)
TB (n) = 3 T (n/2) + Θ(log n)

Which one is faster?

CSci 3110 • Divide and Conquer • 13/3
Solving Recurrences

Given two algorithms A and B for the same problem with running times

TA (n) = 2 T (n/2) + Θ(n)
TB (n) = 3 T (n/2) + Θ(log n)

Which one is faster?

A recurrence as such tells us very little about the running time of the
algorithm?

CSci 3110 • Divide and Conquer • 13/3
Solving Recurrences

Given two algorithms A and B for the same problem with running times

TA (n) = 2 T (n/2) + Θ(n)
TB (n) = 3 T (n/2) + Θ(log n)

Which one is faster?

A recurrence as such tells us very little about the running time of the
algorithm?

We want to “solve” the recurrence, that is, obtain an expression of the form
T (n) = Θ(n2 ), T (n) = Θ(n lg n) or similar.

CSci 3110 • Divide and Conquer • 13/3
Solving Recurrences

Given two algorithms A and B for the same problem with running times

TA (n) = 2 T (n/2) + Θ(n)
TB (n) = 3 T (n/2) + Θ(log n)

Which one is faster?

A recurrence as such tells us very little about the running time of the
algorithm?

We want to “solve” the recurrence, that is, obtain an expression of the form
T (n) = Θ(n2 ), T (n) = Θ(n lg n) or similar.

Formally, we want an expression T (n) = Θ(f (n)), where f (n) does
not depend on T (n).

CSci 3110 • Divide and Conquer • 13/3
Methods to Solve Recurrences

Substitution
Guess what the right answer is.
(Intuition, experience, black magic)
Use induction to prove that the guess is right.

CSci 3110 • Divide and Conquer • 14/3
Methods to Solve Recurrences

Substitution
Guess what the right answer is.
(Intuition, experience, black magic)
Use induction to prove that the guess is right.

Recursion trees
Visualize how the recurrence unfolds.
Use the tree to
Obtain a guess, which is then veriﬁed using substitution, or
Obtain an exact answer if analysis is done sufﬁciently rigorously.

CSci 3110 • Divide and Conquer • 14/3
Methods to Solve Recurrences

Substitution
Guess what the right answer is.
(Intuition, experience, black magic)
Use induction to prove that the guess is right.

Recursion trees
Visualize how the recurrence unfolds.
Use the tree to
Obtain a guess, which is then veriﬁed using substitution, or
Obtain an exact answer if analysis is done sufﬁciently rigorously.

Master theorem
Cook book recipe for solving common recurrences.

CSci 3110 • Divide and Conquer • 14/3
Substitution Example: Merge Sort

Lemma: The running time of Merge Sort is O(n lg n).

CSci 3110 • Divide and Conquer • 15/3
Substitution Example: Merge Sort

Lemma: The running time of Merge Sort is O(n lg n).

Recurrence:

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

CSci 3110 • Divide and Conquer • 15/3
Substitution Example: Merge Sort

Lemma: The running time of Merge Sort is O(n lg n).

Recurrence:

T (n) = 2 T (n/2) + O(n), that is,
T (n) ≤ 2 T (n/2) + an, for some a > 0 and n ≥ n0 .

CSci 3110 • Divide and Conquer • 15/3
Substitution Example: Merge Sort

Lemma: The running time of Merge Sort is O(n lg n).

Recurrence:

T (n) = 2 T (n/2) + O(n), that is,
T (n) ≤ 2 T (n/2) + an, for some a > 0 and n ≥ n0 .

Guess:
T (n) ≤ cn lg n, for some c > 0 and n ≥ n1 .

CSci 3110 • Divide and Conquer • 15/3
Substitution Example: Merge Sort

Lemma: The running time of Merge Sort is O(n lg n).

Recurrence:

T (n) = 2 T (n/2) + O(n), that is,
T (n) ≤ 2 T (n/2) + an, for some a > 0 and n ≥ n0 .

Guess:
T (n) ≤ cn lg n, for some c > 0 and n ≥ n1 .

Base case:
For 2 ≤ n < 4, T (n) ≤ c′ ≤ c′ n ≤ c′ n lg n, for some c′ > 0.
Hence, for c ≥ c′ , T (n) ≤ cn lg n.

CSci 3110 • Divide and Conquer • 15/3
Inductive step: (n ≥ 4)

CSci 3110 • Divide and Conquer • 16/3
Inductive step: (n ≥ 4)

T (n) ≤ 2 T (n/2) + an

CSci 3110 • Divide and Conquer • 16/3
Inductive step: (n ≥ 4)

T (n) ≤ 2 T (n/2) + an
cn n
≤2·      lg + an
2   2

CSci 3110 • Divide and Conquer • 16/3
Inductive step: (n ≥ 4)

T (n) ≤ 2 T (n/2) + an
cn n
≤2·      lg + an
2   2
= cn(lg n − 1) + an

CSci 3110 • Divide and Conquer • 16/3
Inductive step: (n ≥ 4)

T (n) ≤ 2 T (n/2) + an
cn n
≤2·      lg + an
2   2
= cn(lg n − 1) + an
= cn lg n + (a − c)n

CSci 3110 • Divide and Conquer • 16/3
Inductive step: (n ≥ 4)

T (n) ≤ 2 T (n/2) + an
cn n
≤2·      lg + an
2   2
= cn(lg n − 1) + an
= cn lg n + (a − c)n
≤ cn lg n, for all c ≥ a.

CSci 3110 • Divide and Conquer • 16/3
Inductive step: (n ≥ 4)

T (n) ≤ 2 T (n/2) + an
cn n
≤2·      lg + an
2   2
= cn(lg n − 1) + an
= cn lg n + (a − c)n
≤ cn lg n, for all c ≥ a.

Notes:
Since the base case is valid only for n ≥ 2, we can apply the induction
hypothesis only to n ≥ 2. This is why the inductive step starts at n ≥ 4.

CSci 3110 • Divide and Conquer • 16/3
Inductive step: (n ≥ 4)

T (n) ≤ 2 T (n/2) + an
cn n
≤2·      lg + an
2   2
= cn(lg n − 1) + an
= cn lg n + (a − c)n
≤ cn lg n, for all c ≥ a.

Notes:
Since the base case is valid only for n ≥ 2, we can apply the induction
hypothesis only to n ≥ 2. This is why the inductive step starts at n ≥ 4.
We only proved the upper bound. The lower bound can be proved
similarly, but usually needs to be done separately.

CSci 3110 • Divide and Conquer • 16/3
Substitution Example: Binary Search

Lemma: The running time of Binary Search is O(lg n).

CSci 3110 • Divide and Conquer • 17/3
Substitution Example: Binary Search

Lemma: The running time of Binary Search is O(lg n).

Recurrence:

T (n) = T (n/2) + O(1), that is,
T (n) ≤ T (n/2) + a, for some a > 0 and n ≥ n0 .

CSci 3110 • Divide and Conquer • 17/3
Substitution Example: Binary Search

Lemma: The running time of Binary Search is O(lg n).

Recurrence:

T (n) = T (n/2) + O(1), that is,
T (n) ≤ T (n/2) + a, for some a > 0 and n ≥ n0 .

Guess:
T (n) ≤ c lg n, for some c > 0 and n ≥ n1 .

CSci 3110 • Divide and Conquer • 17/3
Substitution Example: Binary Search

Lemma: The running time of Binary Search is O(lg n).

Recurrence:

T (n) = T (n/2) + O(1), that is,
T (n) ≤ T (n/2) + a, for some a > 0 and n ≥ n0 .

Guess:
T (n) ≤ c lg n, for some c > 0 and n ≥ n1 .

Base case:
For 2 ≤ n < 4, T (n) ≤ c′ ≤ c′ lg n, for some c′ > 0.
Hence, for c ≥ c′ , T (n) ≤ c lg n.

CSci 3110 • Divide and Conquer • 17/3
Inductive step: (n ≥ 4)

T (n) ≤ T (n/2) + a

CSci 3110 • Divide and Conquer • 18/3
Inductive step: (n ≥ 4)

T (n) ≤ T (n/2) + a
n
≤ c lg + a
2

CSci 3110 • Divide and Conquer • 18/3
Inductive step: (n ≥ 4)

T (n) ≤ T (n/2) + a
n
≤ c lg + a
2
= c(lg n − 1) + a

CSci 3110 • Divide and Conquer • 18/3
Inductive step: (n ≥ 4)

T (n) ≤ T (n/2) + a
n
≤ c lg + a
2
= c(lg n − 1) + a
= c lg n + (a − c)

CSci 3110 • Divide and Conquer • 18/3
Inductive step: (n ≥ 4)

T (n) ≤ T (n/2) + a
n
≤ c lg + a
2
= c(lg n − 1) + a
= c lg n + (a − c)
≤ c lg n, for all c ≥ a.

CSci 3110 • Divide and Conquer • 18/3
A Recursion Tree for Merge Sort

Strategy: Expand the recurrence    T (n) = 2 T (n/2) + Θ(n)

CSci 3110 • Divide and Conquer • 19/3
A Recursion Tree for Merge Sort

Strategy: Expand the recurrence           T (n) = 2 T (n/2) + Θ(n)

T (n)

CSci 3110 • Divide and Conquer • 19/3
A Recursion Tree for Merge Sort

Strategy: Expand the recurrence         T (n) = 2 T (n/2) + Θ(n)

an

T(n)
2             T(n)
2

CSci 3110 • Divide and Conquer • 19/3
A Recursion Tree for Merge Sort

Strategy: Expand the recurrence                 T (n) = 2 T (n/2) + Θ(n)

an

an                      an
2                       2

T(n)
4         T(n)
4         T(n)
4         T(n)
4

CSci 3110 • Divide and Conquer • 19/3
A Recursion Tree for Merge Sort

Strategy: Expand the recurrence              T (n) = 2 T (n/2) + Θ(n)

an

an                   an
2                    2

an          an        an        an
4           4         4         4

T(n) T(n) T(n) T(n) T(n) T(n) T(n) T(n)
8    8    8    8    8    8    8    8

CSci 3110 • Divide and Conquer • 19/3
A Recursion Tree for Merge Sort

Strategy: Expand the recurrence                                        T (n) = 2 T (n/2) + Θ(n)

an

an                                      an
2                                       2

an                  an                  an                  an
4                   4                   4                   4

an        an        an        an        an        an        an        an
8         8         8         8         8         8         8         8

b b b b b b b                    b b b b b b b b b

CSci 3110 • Divide and Conquer • 19/3
A Recursion Tree for Merge Sort

Strategy: Expand the recurrence                                        T (n) = 2 T (n/2) + Θ(n)

an                                               an

an                                      an
2                                       2

an                  an                  an                  an
4                   4                   4                   4

an        an        an        an        an        an        an        an
8         8         8         8         8         8         8         8

b b b b b b b                    b b b b b b b b b

CSci 3110 • Divide and Conquer • 19/3
A Recursion Tree for Merge Sort

Strategy: Expand the recurrence                                        T (n) = 2 T (n/2) + Θ(n)

an                                               an

an                                      an
2                                       2                           an

an                  an                  an                  an
4                   4                   4                   4

an        an        an        an        an        an        an        an
8         8         8         8         8         8         8         8

b b b b b b b                    b b b b b b b b b

CSci 3110 • Divide and Conquer • 19/3
A Recursion Tree for Merge Sort

Strategy: Expand the recurrence                                        T (n) = 2 T (n/2) + Θ(n)

an                                               an

an                                      an
2                                       2                           an

an                  an                  an                  an
4                   4                   4                   4                 an

an        an        an        an        an        an        an        an
8         8         8         8         8         8         8         8

b b b b b b b                    b b b b b b b b b

CSci 3110 • Divide and Conquer • 19/3
A Recursion Tree for Merge Sort

Strategy: Expand the recurrence                                        T (n) = 2 T (n/2) + Θ(n)

an                                               an

an                                      an
2                                       2                           an

an                  an                  an                  an
4                   4                   4                   4                 an

an        an        an        an        an        an        an        an
8         8         8         8         8         8         8         8            an

b b b b b b b                    b b b b b b b b b

CSci 3110 • Divide and Conquer • 19/3
A Recursion Tree for Merge Sort

Strategy: Expand the recurrence                                        T (n) = 2 T (n/2) + Θ(n)

an                                               an

an                                      an
2                                       2                           an

an                  an                  an                  an
4                   4                   4                   4                 an

an        an        an        an        an        an        an        an
8         8         8         8         8         8         8         8            an

b b b b b b b                    b b b b b b b b b                                    bn

CSci 3110 • Divide and Conquer • 19/3
A Recursion Tree for Merge Sort

Strategy: Expand the recurrence                                        T (n) = 2 T (n/2) + Θ(n)

an                                               an

an                                      an
2                                       2                           an

an                  an                  an                  an
4                   4                   4                   4                 an

an        an        an        an        an        an        an        an
8         8         8         8         8         8         8         8            an

b b b b b b b                    b b b b b b b b b                                    bn

CSci 3110 • Divide and Conquer • 19/3
A Recursion Tree for Merge Sort

Strategy: Expand the recurrence                                        T (n) = 2 T (n/2) + Θ(n)

an                                               an

an                                      an
2                                       2                           an

an                  an                  an                  an
4                   4                   4                   4                 an
lg n
an        an        an        an        an        an        an        an
8         8         8         8         8         8         8         8            an

b b b b b b b                    b b b b b b b b b                                    bn

CSci 3110 • Divide and Conquer • 19/3
A Recursion Tree for Merge Sort

Strategy: Expand the recurrence                                         T (n) = 2 T (n/2) + Θ(n)

an                                               an

an                                      an
2                                       2                           an

an                  an                  an                  an
4                   4                   4                   4                 an
lg n
an        an        an        an        an        an        an        an
8         8         8         8         8         8         8         8            an

b b b b b b b                    b b b b b b b b b                                    bn

Solution: T (n) = Θ(n lg n)

CSci 3110 • Divide and Conquer • 19/3
A Recursion Tree for Binary Search

Recurrence: T (n) = T (n/2) + Θ(1)

CSci 3110 • Divide and Conquer • 20/3
A Recursion Tree for Binary Search

Recurrence: T (n) = T (n/2) + Θ(1)

a

a

a
lg n
a

b

CSci 3110 • Divide and Conquer • 20/3
A Recursion Tree for Binary Search

Recurrence: T (n) = T (n/2) + Θ(1)

a

a

a
lg n
a

b

Solution: T (n) = Θ(lg n)

CSci 3110 • Divide and Conquer • 20/3
A Less Obvious Recursion Tree

Recurrence: T (n) = T (n/3) + T (2n/3) + Θ(n)

n

2n                   n
3
an           3

4n                            n
9
an               9
log3/2 n                                                                       log3 n
8n                                       n
27
an                     27

1                     bn                          1

CSci 3110 • Divide and Conquer • 21/3
A Less Obvious Recursion Tree

Recurrence: T (n) = T (n/3) + T (2n/3) + Θ(n)

Θ(n)

log3 n
log3/2 n
Θ(n)

CSci 3110 • Divide and Conquer • 21/3
A Less Obvious Recursion Tree

Recurrence: T (n) = T (n/3) + T (2n/3) + Θ(n)

Θ(n)

log3 n
log3/2 n
Θ(n)

Solution: T (n) = Θ(n lg n)

CSci 3110 • Divide and Conquer • 21/3
Sometimes Only Substitution Will Do
Recurrence: T (n) = 2 T (n/3) + T (n/2) + Θ(n)

CSci 3110 • Divide and Conquer • 22/3
Sometimes Only Substitution Will Do
Recurrence: T (n) = 2 T (n/3) + T (n/2) + Θ(n)

log3 n
log2 n                     i-th level: Θ(n · (7/6)i )

CSci 3110 • Divide and Conquer • 22/3
Sometimes Only Substitution Will Do
Recurrence: T (n) = 2 T (n/3) + T (n/2) + Θ(n)

log3 n
log2 n                       i-th level: Θ(n · (7/6)i )

Lower bound: T (n) = Ω(n1+log3 (7/6) ) ≈ Ω(n1.14 )

CSci 3110 • Divide and Conquer • 22/3
Sometimes Only Substitution Will Do
Recurrence: T (n) = 2 T (n/3) + T (n/2) + Θ(n)

log3 n
log2 n                       i-th level: Θ(n · (7/6)i )

Lower bound: T (n) = Ω(n1+log3 (7/6) ) ≈ Ω(n1.14 )
Upper bound: T (n) = O(n1+log2 (7/6) ) ≈ O(n1.22 )

CSci 3110 • Divide and Conquer • 22/3
Master Theorem

Theorem: (Master Theorem)
Let a ≥ 1 and b > 1, let f (n) be a function over the positive integers, and
let T (n) be given by the following recurrence:

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

(i) If f (n) = O(nlogb a−ǫ ), for some ǫ > 0, then T (n) = Θ(nlogb a ).

(ii) If f (n) = Θ(nlogb a ), then T (n) = Θ(nlogb a lg n).

(iii) If f (n) = Ω(nlogb a+ǫ ), for some ǫ > 0, and af (n/b) ≤ cf (n), for
some c < 1 and all n ≥ n0 , then T (n) = Θ(f (n)).

CSci 3110 • Divide and Conquer • 23/3
Selection

Given:
An array storing n numbers, x1 ≤ x2 ≤ · · · ≤ xn , in any order
A parameter 1 ≤ k ≤ n

To compute: xk

Element xk is also referred to as the k-th order statistic of the set
{x1 , x2 , . . . , xn }.

Example:

16 3    5 21 8 10 7 17

The 4-th order statistic is 8 because

3   5   7   8 10 16 17 21

CSci 3110 • Divide and Conquer • 24/3
Two Simple Solutions

Repeated Minimum:
Find and remove the minimum, repeat k times.

CSci 3110 • Divide and Conquer • 25/3
Two Simple Solutions

Repeated Minimum:
Find and remove the minimum, repeat k times.
Running time:

CSci 3110 • Divide and Conquer • 25/3
Two Simple Solutions

Repeated Minimum:
Find and remove the minimum, repeat k times.
Running time: Θ(kn)

CSci 3110 • Divide and Conquer • 25/3
Two Simple Solutions

Repeated Minimum:
Find and remove the minimum, repeat k times.
Running time: Θ(kn)

Sort and select:
Sort the sequence
Report the k-th item in the sorted sequence

CSci 3110 • Divide and Conquer • 25/3
Two Simple Solutions

Repeated Minimum:
Find and remove the minimum, repeat k times.
Running time: Θ(kn)

Sort and select:
Sort the sequence
Report the k-th item in the sorted sequence
Running time:

CSci 3110 • Divide and Conquer • 25/3
Two Simple Solutions

Repeated Minimum:
Find and remove the minimum, repeat k times.
Running time: Θ(kn)

Sort and select:
Sort the sequence
Report the k-th item in the sorted sequence
Running time: Θ(n lg n)

CSci 3110 • Divide and Conquer • 25/3
The Hunt for Intuition: Quicksort

Q UICKSORT(A)
1 if |A| ≤ 1
2    then return A
3    else p ← median of A
4         Partition A into three pieces:
L = {x ∈ A | x < p}
{p}
R = {x ∈ A \ {p} | x ≥ p}
5         L′ ← Q UICKSORT (L)
6         R′ ← Q UICKSORT (R)
7         return L′ ◦ {p} ◦ R′

CSci 3110 • Divide and Conquer • 26/3
The Hunt for Intuition: Quicksort

Q UICKSORT(A)
1 if |A| ≤ 1
2    then return A
3    else p ← median of A
4         Partition A into three pieces:
L = {x ∈ A | x < p}
{p}
R = {x ∈ A \ {p} | x ≥ p}
5         L′ ← Q UICKSORT (L)
6         R′ ← Q UICKSORT (R)
7         return L′ ◦ {p} ◦ R′

Assumption: We know how to ﬁnd the median in Θ(n) time.

Running time: T (n) =

CSci 3110 • Divide and Conquer • 26/3
The Hunt for Intuition: Quicksort

Q UICKSORT(A)
1 if |A| ≤ 1
2    then return A
3    else p ← median of A
4         Partition A into three pieces:
L = {x ∈ A | x < p}
{p}
R = {x ∈ A \ {p} | x ≥ p}
5         L′ ← Q UICKSORT (L)
6         R′ ← Q UICKSORT (R)
7         return L′ ◦ {p} ◦ R′

Assumption: We know how to ﬁnd the median in Θ(n) time.

Running time: T (n) = 2 T (n/2) + Θ(n)

CSci 3110 • Divide and Conquer • 26/3
The Hunt for Intuition: Quicksort

Q UICKSORT(A)
1 if |A| ≤ 1
2    then return A
3    else p ← median of A
4         Partition A into three pieces:
L = {x ∈ A | x < p}
{p}
R = {x ∈ A \ {p} | x ≥ p}
5         L′ ← Q UICKSORT (L)
6         R′ ← Q UICKSORT (R)
7         return L′ ◦ {p} ◦ R′

Assumption: We know how to ﬁnd the median in Θ(n) time.

Running time: T (n) = 2 T (n/2) + Θ(n) = Θ(n lg n)

CSci 3110 • Divide and Conquer • 26/3
Partition & Recurse

Start by partitioning A around the median:

L       p        R

CSci 3110 • Divide and Conquer • 27/3
Partition & Recurse

Start by partitioning A around the median:

L    p        R

If k = |L| + 1, then p = xk .

CSci 3110 • Divide and Conquer • 27/3
Partition & Recurse

Start by partitioning A around the median:

L    p        R

If k = |L| + 1, then p = xk .
Return p

CSci 3110 • Divide and Conquer • 27/3
Partition & Recurse

Start by partitioning A around the median:

L      p        R

If k = |L| + 1, then p = xk .
Return p

If k < |L| + 1, then xk ∈ L and y > xk , for all y ∈ R ∪ {p}.

CSci 3110 • Divide and Conquer • 27/3
Partition & Recurse

Start by partitioning A around the median:

L      p        R

If k = |L| + 1, then p = xk .
Return p

If k < |L| + 1, then xk ∈ L and y > xk , for all y ∈ R ∪ {p}.
Recursively ﬁnd the k-th order statistic in L and return it.

CSci 3110 • Divide and Conquer • 27/3
Partition & Recurse

Start by partitioning A around the median:

L      p        R

If k = |L| + 1, then p = xk .
Return p

If k < |L| + 1, then xk ∈ L and y > xk , for all y ∈ R ∪ {p}.
Recursively ﬁnd the k-th order statistic in L and return it.

If k > |L| + 1, then xk ∈ R and y ≤ xk , for all y ∈ L ∪ {p}.

CSci 3110 • Divide and Conquer • 27/3
Partition & Recurse

Start by partitioning A around the median:

L      p        R

If k = |L| + 1, then p = xk .
Return p

If k < |L| + 1, then xk ∈ L and y > xk , for all y ∈ R ∪ {p}.
Recursively ﬁnd the k-th order statistic in L and return it.

If k > |L| + 1, then xk ∈ R and y ≤ xk , for all y ∈ L ∪ {p}.
Recursively ﬁnd the (k − |L| − 1)-st order statistic in R and return it.

CSci 3110 • Divide and Conquer • 27/3
Partition & Recurse

Start by partitioning A around the median:

L      p        R

If k = |L| + 1, then p = xk .
Return p

If k < |L| + 1, then xk ∈ L and y > xk , for all y ∈ R ∪ {p}.
Recursively ﬁnd the k-th order statistic in L and return it.

If k > |L| + 1, then xk ∈ R and y ≤ xk , for all y ∈ L ∪ {p}.
Recursively ﬁnd the (k − |L| − 1)-st order statistic in R and return it.

Running time: T (n) ≤

CSci 3110 • Divide and Conquer • 27/3
Partition & Recurse

Start by partitioning A around the median:

L      p        R

If k = |L| + 1, then p = xk .
Return p

If k < |L| + 1, then xk ∈ L and y > xk , for all y ∈ R ∪ {p}.
Recursively ﬁnd the k-th order statistic in L and return it.

If k > |L| + 1, then xk ∈ R and y ≤ xk , for all y ∈ L ∪ {p}.
Recursively ﬁnd the (k − |L| − 1)-st order statistic in R and return it.

Running time: T (n) ≤ T (n/2) + O(n)

CSci 3110 • Divide and Conquer • 27/3
Partition & Recurse

Start by partitioning A around the median:

L      p        R

If k = |L| + 1, then p = xk .
Return p

If k < |L| + 1, then xk ∈ L and y > xk , for all y ∈ R ∪ {p}.
Recursively ﬁnd the k-th order statistic in L and return it.

If k > |L| + 1, then xk ∈ R and y ≤ xk , for all y ∈ L ∪ {p}.
Recursively ﬁnd the (k − |L| − 1)-st order statistic in R and return it.

Running time: T (n) ≤ T (n/2) + O(n) = O(n)

CSci 3110 • Divide and Conquer • 27/3
Relaxing the Partition

Problem: Finding the median of A is selection.

CSci 3110 • Divide and Conquer • 28/3
Relaxing the Partition

Problem: Finding the median of A is selection.

Have we walked in a circle?

CSci 3110 • Divide and Conquer • 28/3
Relaxing the Partition

Problem: Finding the median of A is selection.

Have we walked in a circle?

Observation: An “approximate” median does the job:
If |L| ≤ cn and |R| ≤ cn, for some c < 1, then

T (n) ≤ T (cn) + O(n) = O(n).

CSci 3110 • Divide and Conquer • 28/3
Finding an Approximate Median

Partition input into groups of 5 elements.
Sort each group and add its 3rd element to an array A′ .
Find the median of A′ (by calling the selection algorithm recursively!)
and return as approximate median.

CSci 3110 • Divide and Conquer • 29/3
The Procedure Finds an Approximate Median

Lemma: There are at least 3n − 6 elements on either side of the
10
computed approximate median p.

CSci 3110 • Divide and Conquer • 30/3
The Procedure Finds an Approximate Median

Lemma: There are at least 3n − 6 elements on either side of the
10
computed approximate median p.

Proof: (for elements greater than p)

⌈n/5⌉             n
At least       2     −1≥       10   − 1 groups to the right of p

At most one is not full
Every full group contains at least 3 elements > p

n            3n
Total: 3   10   −2 =    10   −6

CSci 3110 • Divide and Conquer • 30/3
The Final Running Time

Summary of selection algorithm:
Find approximate median: linear work + recurse on ⌈n/5⌉ elements
Partition: linear work
Recurse on piece of size at most 7n/10 + 6

CSci 3110 • Divide and Conquer • 31/3
The Final Running Time

Summary of selection algorithm:
Find approximate median: linear work + recurse on ⌈n/5⌉ elements
Partition: linear work
Recurse on piece of size at most 7n/10 + 6

Recurrence:

O(1)                           if n ≤ 140
T (n) =
O(n) + T (⌈ n ⌉) + T ( 7n + 6) if n > 140
5          10

CSci 3110 • Divide and Conquer • 31/3
The Final Running Time

Summary of selection algorithm:
Find approximate median: linear work + recurse on ⌈n/5⌉ elements
Partition: linear work
Recurse on piece of size at most 7n/10 + 6

Recurrence:

O(1)                           if n ≤ 140
T (n) =
O(n) + T (⌈ n ⌉) + T ( 7n + 6) if n > 140
5          10

= O(n)

CSci 3110 • Divide and Conquer • 31/3
The Final Running Time

Summary of selection algorithm:
Find approximate median: linear work + recurse on ⌈n/5⌉ elements
Partition: linear work
Recurse on piece of size at most 7n/10 + 6

Recurrence:

O(1)                           if n ≤ 140
T (n) =
O(n) + T (⌈ n ⌉) + T ( 7n + 6) if n > 140
5          10

= O(n)

Theorem: The k-th order statistic of a set of n elements can be found in
O(n) time.

CSci 3110 • Divide and Conquer • 31/3
Counting Inversions

Given a sequence S = (x1 , x2 , . . . , xn ) of n numbers, an inversion is a pair
(xi , xj ) such that
i < j and
xi > xj .

Example:

5   3   7   8 21 10 17 16

Inversions: (5, 3), (21, 10), (21, 17), (21, 16), (17, 16)

CSci 3110 • Divide and Conquer • 32/3
Counting Inversions

Given a sequence S = (x1 , x2 , . . . , xn ) of n numbers, an inversion is a pair
(xi , xj ) such that
i < j and
xi > xj .

Example:

5   3   7   8 21 10 17 16

Inversions: (5, 3), (21, 10), (21, 17), (21, 16), (17, 16)

Problem: Count all inversions in S.

CSci 3110 • Divide and Conquer • 32/3
Classifying Inversions

As in Merge Sort, partition array into left half, L, and right half, R:

L              R

short   long     short

An inversion (xi , xj ) is short if {xi , xj } ⊆ L or {xi , xj } ⊆ R

An inversion (xi , xj ) is long if xi ∈ L and xj ∈ R

CSci 3110 • Divide and Conquer • 33/3
Classifying Inversions

As in Merge Sort, partition array into left half, L, and right half, R:

L              R

short   long     short

An inversion (xi , xj ) is short if {xi , xj } ⊆ L or {xi , xj } ⊆ R

An inversion (xi , xj ) is long if xi ∈ L and xj ∈ R

Since we are talking about divide and conquer:
Find short recursions recursively.

CSci 3110 • Divide and Conquer • 33/3
Counting Long Inversions
Observation: Sorting L and R does not affect the number of long
inversions.

CSci 3110 • Divide and Conquer • 34/3
Counting Long Inversions
Observation: Sorting L and R does not affect the number of long
inversions.

Procedure:
Sort L and R.
Count long inversions by merging L and R:
x
L   U   R                         L
y
R

CSci 3110 • Divide and Conquer • 34/3
Counting Long Inversions
Observation: Sorting L and R does not affect the number of long
inversions.

Procedure:
Sort L and R.
Count long inversions by merging L and R:
x
L   U   R                         L
y
R

When y < x, then y forms an inversion with exactly the elements
remaining in L.

CSci 3110 • Divide and Conquer • 34/3
Counting Long Inversions
Observation: Sorting L and R does not affect the number of long
inversions.

Procedure:
Sort L and R.
Count long inversions by merging L and R:
x
L   U   R                         L
y
R

When y < x, then y forms an inversion with exactly the elements
remaining in L.
∴ Increase inversion count by |L|.
CSci 3110 • Divide and Conquer • 34/3
Analysis

T (n) =

CSci 3110 • Divide and Conquer • 35/3
Analysis

T (n) = 2 T (n/2) + Θ(n lg n)

CSci 3110 • Divide and Conquer • 35/3
Analysis

T (n) = 2 T (n/2) + Θ(n lg n) = Θ(n lg2 n).

CSci 3110 • Divide and Conquer • 35/3
Analysis

T (n) = 2 T (n/2) + Θ(n lg n) = Θ(n lg2 n).

Observation:
Counting long inversions produces L ∪ R in sorted order, as a
by-product.

CSci 3110 • Divide and Conquer • 35/3
Analysis

T (n) = 2 T (n/2) + Θ(n lg n) = Θ(n lg2 n).

Observation:
Counting long inversions produces L ∪ R in sorted order, as a
by-product.
In particular, the recursive calls on L and R return L and R in sorted
order.

CSci 3110 • Divide and Conquer • 35/3
Analysis

T (n) = 2 T (n/2) + Θ(n lg n) = Θ(n lg2 n).

Observation:
Counting long inversions produces L ∪ R in sorted order, as a
by-product.
In particular, the recursive calls on L and R return L and R in sorted
order.
∴ We can save the sorting step.

CSci 3110 • Divide and Conquer • 35/3
Analysis

T (n) = 2 T (n/2) + Θ(n lg n) = Θ(n lg2 n).

Observation:
Counting long inversions produces L ∪ R in sorted order, as a
by-product.
In particular, the recursive calls on L and R return L and R in sorted
order.
∴ We can save the sorting step.

∴ T (n) =

CSci 3110 • Divide and Conquer • 35/3
Analysis

T (n) = 2 T (n/2) + Θ(n lg n) = Θ(n lg2 n).

Observation:
Counting long inversions produces L ∪ R in sorted order, as a
by-product.
In particular, the recursive calls on L and R return L and R in sorted
order.
∴ We can save the sorting step.

∴ T (n) = 2 T (n/2) + Θ(n)

CSci 3110 • Divide and Conquer • 35/3
Analysis

T (n) = 2 T (n/2) + Θ(n lg n) = Θ(n lg2 n).

Observation:
Counting long inversions produces L ∪ R in sorted order, as a
by-product.
In particular, the recursive calls on L and R return L and R in sorted
order.
∴ We can save the sorting step.

∴ T (n) = 2 T (n/2) + Θ(n) = Θ(n lg n).

CSci 3110 • Divide and Conquer • 35/3
Multiplying Large Integers

Problem: Given two n-digit numbers x = xn−1 xn−1 . . . x0 and
y = yn−1 yn−2 . . . y0 , we want to compute z = x · y using only digit-wise
operations.

CSci 3110 • Divide and Conquer • 36/3
Multiplying Large Integers

Problem: Given two n-digit numbers x = xn−1 xn−1 . . . x0 and
y = yn−1 yn−2 . . . y0 , we want to compute z = x · y using only digit-wise
operations.

54163 * 63021
324978
162489
0
108326
54163
3413406423

CSci 3110 • Divide and Conquer • 36/3
Multiplying Large Integers

Problem: Given two n-digit numbers x = xn−1 xn−1 . . . x0 and
y = yn−1 yn−2 . . . y0 , we want to compute z = x · y using only digit-wise
operations.

54163 * 63021
324978
162489
0
108326
54163
3413406423

Cost:

CSci 3110 • Divide and Conquer • 36/3
Multiplying Large Integers

Problem: Given two n-digit numbers x = xn−1 xn−1 . . . x0 and
y = yn−1 yn−2 . . . y0 , we want to compute z = x · y using only digit-wise
operations.

54163 * 63021
324978
162489
0
108326
54163
3413406423

Cost: Θ(n2 )

CSci 3110 • Divide and Conquer • 36/3
Divide-and-Conquer Multiplication

Assumption: n = 2k

A recursive method:

x'             x"                  y'             y"

x" · y"

x" · y' + x' · y"

x' · y'

x·y

CSci 3110 • Divide and Conquer • 37/3
Divide-and-Conquer Multiplication

Assumption: n = 2k

A recursive method:

x'            x"                  y'             y"

x" · y"

x" · y' + x' · y"

x' · y'

x·y

Recurrence: T (n) =

CSci 3110 • Divide and Conquer • 37/3
Divide-and-Conquer Multiplication

Assumption: n = 2k

A recursive method:

x'           x"                  y'             y"

x" · y"

x" · y' + x' · y"

x' · y'

x·y

Recurrence: T (n) = 4 T (n/2) + Θ(n)

CSci 3110 • Divide and Conquer • 37/3
Divide-and-Conquer Multiplication

Assumption: n = 2k

A recursive method:

x'           x"                  y'             y"

x" · y"

x" · y' + x' · y"

x' · y'

x·y

Recurrence: T (n) = 4 T (n/2) + Θ(n) = Θ(n2 )                               Bummer!

CSci 3110 • Divide and Conquer • 37/3
One Less Recursive Call

Compute recursively:
A = x′ · y ′
B = x′′ · y ′′
C = (x′ + x′′ ) · (y ′ + y ′′ )

CSci 3110 • Divide and Conquer • 38/3
One Less Recursive Call

Compute recursively:
A = x′ · y ′
B = x′′ · y ′′
C = (x′ + x′′ ) · (y ′ + y ′′ )

Combine results:

CSci 3110 • Divide and Conquer • 38/3
One Less Recursive Call

Compute recursively:
A = x′ · y ′
B = x′′ · y ′′
C = (x′ + x′′ ) · (y ′ + y ′′ )

Combine results:
x′ · y ′ = A
x′′ · y ′′ = B

CSci 3110 • Divide and Conquer • 38/3
One Less Recursive Call

Compute recursively:
A = x′ · y ′
B = x′′ · y ′′
C = (x′ + x′′ ) · (y ′ + y ′′ )

Combine results:
x′ · y ′ = A
x′′ · y ′′ = B
x′ · y ′′ + x′′ · y ′ = C − A − B

CSci 3110 • Divide and Conquer • 38/3
One Less Recursive Call

Compute recursively:
A = x′ · y ′
B = x′′ · y ′′
C = (x′ + x′′ ) · (y ′ + y ′′ )

Combine results:
x′ · y ′ = A
x′′ · y ′′ = B
x′ · y ′′ + x′′ · y ′ = C − A − B

Recurrence: T (n) =

CSci 3110 • Divide and Conquer • 38/3
One Less Recursive Call

Compute recursively:
A = x′ · y ′
B = x′′ · y ′′
C = (x′ + x′′ ) · (y ′ + y ′′ )

Combine results:
x′ · y ′ = A
x′′ · y ′′ = B
x′ · y ′′ + x′′ · y ′ = C − A − B

Recurrence: T (n) = 3 T (n/2) + Θ(n)

CSci 3110 • Divide and Conquer • 38/3
One Less Recursive Call

Compute recursively:
A = x′ · y ′
B = x′′ · y ′′
C = (x′ + x′′ ) · (y ′ + y ′′ )

Combine results:
x′ · y ′ = A
x′′ · y ′′ = B
x′ · y ′′ + x′′ · y ′ = C − A − B

Recurrence: T (n) = 3 T (n/2) + Θ(n) = Θ(n1+lg(3/2) )

CSci 3110 • Divide and Conquer • 38/3
One Less Recursive Call

Compute recursively:
A = x′ · y ′
B = x′′ · y ′′
C = (x′ + x′′ ) · (y ′ + y ′′ )

Combine results:
x′ · y ′ = A
x′′ · y ′′ = B
x′ · y ′′ + x′′ · y ′ = C − A − B

Recurrence: T (n) = 3 T (n/2) + Θ(n) = Θ(n1+lg(3/2) ) ≈ Θ(n1.58 )

CSci 3110 • Divide and Conquer • 38/3
One Less Recursive Call

Compute recursively:
A = x′ · y ′
B = x′′ · y ′′
C = (x′ + x′′ ) · (y ′ + y ′′ )

Combine results:
x′ · y ′ = A
x′′ · y ′′ = B
x′ · y ′′ + x′′ · y ′ = C − A − B

Recurrence: T (n) = 3 T (n/2) + Θ(n) = Θ(n1+lg(3/2) ) ≈ Θ(n1.58 )

Note: This works only because addition has an inverse operation; that is, it
does not work over a semi-ring.

CSci 3110 • Divide and Conquer • 38/3
Summary
Divide an conquer:
Divide the problem instance into two or more smaller instances of the
same problem.
Recursively solve (conquer) these smaller problem instances.
Combine the solutions obtained for the smaller instances to construct a
solution for the original problem instance.

Divide-and-conquer algorithms are by deﬁnition recursive.
∴ Natural expression of running time using recurrence relations
∴ Natural correctness proofs using induction

Solving recurrence relations:
Substitution
Recursion trees
Master theorem

CSci 3110 • Divide and Conquer • 39/3

```
Related docs
Other docs by gjjur4356
Chapter 82011455721