Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
The Divide-and-Conquer Strategy
4 -1
Introduction to Divide and Conquer
Powering a number
Merge sort
Binary search
Matrix multiplication
Strassen’s algorithm
The divide-and-conquer Design Paradigm
Divide the problem into subproblems.
Conquer the subproblems by solving them
recursively.
Combine subproblem solutions.
Many algorithms use this paradigm.
Divide-and-Conquer Technique
(cont.)
a problem of size n
(instance)
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
It general leads
to a recursive
algorithm!
Divide & conquer
DANDC(p , q)
{ If small (p , q) then
Return (g(p , q))
Else
{
m Divide (p , q)
// A= p to m ,B= m+1 to q
Return (combine (DANDC(p , m),DANDC(m+1 , q)))
}
End DANDC }
4 -5
A simple example
finding the maximum of a set S of n numbers
4 -6
T(n)=
g(n)
if n is small
2T(n/2) + f(n)
otherwise
1.Divide:Trivial.
2.Conquer:Recursively sort subarrays.
3.Combine:Linear-time merge.
# subproblems
subproblem
size
cost of dividing
and combining
Powering a Number
Problem: Compute xn, where n∈N.
Power (x , n)
Power(float x, float n)
Local float a;
If n = 0 then
Return 1;
a power(x , n/2);
If n % 2 then
[Odd n ]
Return x* x * a;
Else
[even n ]
return a* a ;
end
10
Merge Sort
1.Divide:Trivial.
2.Conquer:Recursively sort subarrays.
3.Combine:Linear-time merge.
Merge Sort
1
Alg.: MERGE-SORT (A,low, high)
2
3
4
q
5
6
7
8
r
5 2 4 7 1 3 2 6
{ if low < high
Check for base case
then mid ← (low + high)/2
Divide
MERGE-SORT(A,low, mid)
left Conquer
MERGE-SORT(A,mid+ 1, high)
right Conquer
MERGE(A,low, mid , high) }
p
Combine
//Initial call: MERGE-SORT (A, 1, n)
12
Mergesort Example
8 3 2 9 7 1 5 4
8 3 2 9
8 3
8
7 1 5 4
2 9
3
2
3 8
71
9
2 9
7
5 4
1
5
1 7
2 3 8 9
4 5
1 4 5 7
1 2 3 4 5 7 8 9
4
Merge(low , mid , high)
{
h high , i low, j mid + 1,k0;
WHILE (i≤mid ) and (j ≤ high)
{
IF a[i] ≤ a[j] then
{b[k] = a[i]; i = i+1; }
Else {b[k] = a[j] ; j = j+1;}
k=k+1;}
while (i<mid)
{ b[k] = a[i] ; i = i+1; k=k+1 }
while (j<high)
{ b[k] = a[j] ; j = j+1; k=k+1 }
for k= low to high do a[k] = b[k] ;}
MERGE-SORT (A,low, high)
{ if low < high
then mid ← (low + high)/2
MERGE-SORT(A,low, mid)
MERGE-SORT(A,mid+ 1, high)
MERGE(A,low, mid , high) }
Merge(low , mid , high)
{
h high , i low, j mid + 1,k0;
WHILE (i≤mid ) and (j ≤ high)
{
IF a[i] ≤ a[j] then
{b[k] = a[i]; i = i+1; }
Else {b[k] = a[j] ; j = j+1;}
k=k+1;}
while (i<mid)
{ b[k] = a[i] ; i = i+1; k=k+1 }
while (j<high)
{ b[k] = a[j] ; j = j+1; k=k+1 }
for k= low to high do a[k] = b[k] ;}
Time complexity of the
general algorithm
Time complexity:
T(n)=
2T(n/2)+S(n)+M(n) , n c
b
,n<c
8 3 2 9 7 1 5 4
8 3 2 9
where S(n) : time for splitting
M(n) : time for merging
b : a constant
c : a constant
8 3
8
7 1 5 4
2 9
3
2
3 8
71
9
2 9
7
5 4
1
5
1 7
2 3 8 9
4
4 5
1 4 5 7
1 2 3 4 5 7 8 9
4 -16
T(n)=
a
n=1 , a constant
2T(n/2) + cn
n>1 ,c costant
When is power of 2 , n = 2k we can solve this
Equation
T(n)=
a
n=1 , a constant
2T(n/2) + cn
n>1 ,c costant
When is power of 2 , n = 2k we can solve this
Equation
T(n) = 2(2T(n/4) + cn /2 ) + cn
// k=2
= 4 T(n/4) + 2cn
= 4(2T(n/8) + cn /4 ) + 2cn
= 8 T(n/8)+ 3 cn
// k=3
.
.
.
= 2k T(1) + kcn
= an + cn log n
It is easy to see that if 2k < n < 2k+1 then T(n) <= T(2k+1)
Therefore T(n)= O (n log n)
Merge sort
Time complexity
8 3 2 9 7 1 5 4
Tmerge ( N ) O(7 N 3) O( N )
8 3 2 9
7 1 5 4
T ( N ) 2T ( N / 2) 1 Tmerge 2T ( N / 2) 7 N 4
8 3
8
2 9
3
2
3 8
71
9
2 9
7
5 4
1
5
1 7
2 3 8 9
4 5
1 4 5 7
1 2 3 4 5 7 8 9
Since we have b=c O(N log N)
4
Merge sort
Substitution method
T ( N ) 2T ( N / 2) 7 N 4
2(2T ( N 2 2 ) 7 N 2 4) 7 N 4
2 2T ( N 2 2 ) 7 N 7 N 4 4 2
2 2 (2T ( N 23 ) 7( N 2 2 ) 4) 7 N 7 N 4 4 2
23 T ( N 23 ) 7 N 7 N 7 N 4 4 2 4 2 2
23 T ( N 23 ) 3 7 N 4 (1 2 23 )
log N
2log2 N log 2 N 7 N 4 2i
i 0
7 N log N 9 N 4
O( N log N )