Vous êtes sur la page 1sur 29

Divide and Conquer Strategy

General Concept of Divide &


Conquer
Given a function to compute on n inputs, the divide-and-

conquer strategy consists of:


splitting the inputs into k distinct subsets, 1kn, yielding k
subproblems.
solving these subproblems
combining the subsolutions into solution of the whole.
if the subproblems are relatively large, then divide_Conquer
is applied again.
if the subproblems are small, the are solved without
splitting.
2

The Divide and Conquer Algorithm


Divide_Conquer(problem P)
{
if Small(P) return S(P);
else {
divide P into smaller instances P1, P2, , Pk, k1;
Apply Divide_Conquer to each of these subproblems;
return Combine(Divide_Conque(P1), Divide_Conque(P2),,
Divide_Conque(Pk));
}
}

Three Steps of The Divide and Conquer Approach

The most well known algorithm design strategy:


1. Divide the problem into two or more smaller
subproblems.
2. Conquer the subproblems by solving them
recursively.
3. Combine the solutions to the subproblems into the
solutions for the original problem.

A Typical Divide and Conquer Case

a problem of size n

subproblem 1
of size n/2

subproblem 2
of size n/2

a solution to
subproblem 1

a solution to
subproblem 2

a solution to
the original problem

Divde_Conquer recurrence
relation
The computing time of Divde_Conquer is

n small
g ( n)
T ( n)
T ( n1 ) T ( n2 ) ... T ( nk ) f ( n) otherwise

T(n) is the time for Divde_Conquer on any input size n.


g(n) is the time to compute the answer directly (for small
inputs)
f(n) is the time for dividing P and combining the solutions.

Binary search
Find an element in a sorted array:
Divide: Check middle element.
Conquer: Recursively search 1 sub
array.
Combine: Trivial.

Example: Find 9
3

12

15

Binary search
Find an element in a sorted array:
Divide: Check middle element.
Conquer: Recursively search 1 sub
array.
Combine: Trivial.

Example: Find 9

Binary search
Find an element in a sorted array:
Divide: Check middle element.
Conquer: Recursively search 1 sub
array.
Combine: Trivial.

Example: Find 9

Binary search
Find an element in a sorted array:
Divide: Check middle element.
Conquer: Recursively search 1 sub
array.
Combine: Trivial.

Example: Find 9

Binary search
Find an element in a sorted array:
Divide: Check middle element.
Conquer: Recursively search 1 sub
array.
Combine: Trivial.

Example: Find 9

Binary search
Find an element in a sorted array:
Divide: Check middle element.
Conquer: Recursively search 1 sub
array.
Combine: Trivial.

Example: Find 9

Binary search
Find an element in a sorted array:
Divide: Check middle element.
Conquer: Recursively search 1 sub
array.
Combine: Trivial.

Example: Find 9

Recursive Method for Binary Search

2006 Pearson Addison-Wesley.


All rights reserved

Recurrence for binary search

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


# subproblems
subproblem
size

b=2, a=1
Case 2 (k=0)

cost of dividing
and combining

QuickSort
We use Divide-and-Conquer:
1. Divide: partition A[p..r] into two subarrays A[p..q-1] and A[q+1..r] such
that each element of A[p..q-1] is A[q], and each element of A[q+1..r] is
A[q]. Compute q as part of this partitioning.
1. Conquer: sort the subarrays A[p..q-1] and A[q+1..r] by recursive calls to
QUICKSORT.
1. Combine: the partitioning and recursive sorting leave us with a sorted
A[p..r] no work needed here.
An obvious difference is that we do most of the work in the divide stage, with
no work at the combine one.

Quicksort Algorithm
Quicksort example
2

Current pivots

Previous pivots

Quicksort
2

Hi, I am nothing

Nothing Jr.

Nothing 3rd

QuickSort
The Pseudo-Code

QuickSort

QuickSort
Proof of Correctness: PARTITION
We look for a loop invariant and we observe that at the
beginning of each iteration of the loop (l.3-6) for any
array index k:

1. If pki, then A[k]x;


2. If i+1kj1, then A[k]>x;
3. If k=r, then A[k]=x.
4. Ifjkr1, then we dont know anything about A[k].

QuickSort
The Invariant

Initialization. Before the first iteration: i=p1, j=p. No values


between p and i; no values between i+1 and j1. The first two
conditions are trivially satisfied; the initial assignment satisfies 3.
Maintenance. Two cases
1.A[j]>x.

2. A[j]x.

Complexity of Quick Sort


Average-case O(NlogN)
Worst Case: O(N2)
This happens when the pivot is the smallest (or the largest) element
means when the partitioning routine produces one subproblem with n-1
elements and one with 0 elements.
Then one of the partitions is empty, and we repeat recursively the
procedure for N-1 elements. Assume this unbalanced partitioning arises
in each recursive call.
Best-case O(NlogN)
The pivot is the median of the array, the left and the right parts
have same size.
There are logN partitions, and to obtain each partitions we do N
comparisons (and not more than N/2 swaps). Hence the
complexity is O(NlogN)
22

Analysis
T(N) = T(i) + T(N - i -1) + cN
The time to sort the file is equal to
the time to sort the left partition with i elements, plus
the time to sort the right partition with N-i-1 elements, plus
the time to build the partitions.

23

Worst-Case Analysis
The pivot is the smallest (or the largest) element
T(N) = T(N-1) + cN, N > 1

24

Best-Case Analysis

The pivot is in the middle


T(N) = 2T(N/2) + cN

Divide by N:
T(N) / N = T(N/2) / (N/2) + c

25

Advantages and Disadvantages


Advantages:
One of the fastest algorithms on average
Does not need additional memory (the sorting takes
place in the array - this is called in-place processing )
Disadvantages:
The worst-case complexity is O(N2)

26

Worst-Case Analysis
What will be the worst case?
The pivot is the smallest element, all the time
Partition is always unbalanced

Best-case Analysis
What will be the best case?
Partition is perfectly balanced.
Pivot is always in the middle (median of the array)

Vous aimerez peut-être aussi