Chapter 2 Divide and Conquer Strategy
Chapter 2 Divide and Conquer Strategy
•Greedy strategy:
•Principle, control abstraction, time analysis of
control abstraction,
• knapsack problem,
• Job scheduling algorithm,
•Single Source Shortest Path Dijkstra’s,
• Bellman-Ford
Divide-and-Conquer
General idea:
• Worst-case: O( log n )
– If v does not exist
• Best-case: O( 1 )
– When the target element v happens to be in the
middle of the array
• Average-case: O( log n )
– Technically, some statistical analysis needed
here (beyond the scope of this course)
Binary Search (version 2)
• Can use recursion instead of iteration
• BinSearch(A,v):
return BinSearchHelper(A,0,n-1,v)
• BinSearchHelper(A,low,high,v):
if low > high then
return -1
mid (low+high)/2
if A[mid] = v then
return mid
else if A[mid] < v then
return BinSearchHelper(A,mid+1,high,v)
else
return BinSearchHelper(A,low,mid-1,v)
Mergesort
• Split array A[0..n-1] in two about equal halves and make
copies of each half in arrays B and C
• Sort arrays B and C recursively
• Merge sorted arrays B and C into array A as follows:
– Repeat the following until no elements remain in one of
the arrays:
• compare the first elements in the remaining
unprocessed portions of the arrays
• copy the smaller of the two into A, while incrementing
the index indicating the unprocessed portion of that
array
– Once all elements in one of the arrays are processed, copy
the remaining unprocessed elements from the other array
into A.
Pseudocode of Mergesort
Pseudocode of Merge
Mergesort Example
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
Analysis of Mergesort
• All cases have same efficiency: Θ(n log n)
idea: Split each part into 2 equal parts and sort each part using
Mergesort, then merge the tow sorted sub-lists into one sorted list.
The algorithm:
procedure MergeSort (low, high)
begin low high
if
low high
then mid
2
call MergeSort (low, mid);
call MergeSort (mid+1, high);
call Merge (low, mid, high);
end;
procedure Merge (low, mid, high)
else
move A(i) through A(mid) to U(k) through U(high);
end;
Analysis:
A[i]p A[i]p
• Exchange the pivot with the last element in the first (i.e.,
) subarray — the pivot is now in its final position
• Sort the two subarrays recursively
Analysis of Quicksort
• Best case: split in the middle — Θ(n log n)
• Worst case: sorted array! — Θ(n2)
• Average case: random arrays — Θ(n log n)
• Improvements:
– better pivot selection: median of three partitioning
– switch to insertion sort on small subfiles
– elimination of recursion
These combine to 20-25% improvement
The problem:
Same as Merge Sort
Compared with Merge Sort:
Eg: Sort array 65, 70, 75, 80, 85, 60, 55, 50, 45
65 70 75 80 85 60 55 50 45
Pivot item
60 55 50 45 70 75 80 85
65
55 50 45 60 65 70 75 80 85
50 45 55 60 65 70 80 85
75
45 55 60 65 70 75 80 85
The algorithm:
procedure QuickSort (p, q)
begin
if p < q,
then call Partition (p, q, pivotposition);
call QuickSort (p, pivotposition –1);
call QuickSort (pivotposition+1, q);
end;
Worst-Case:
Call Partition O(n) times, each time takes
O(n) steps. So O(n2), and it is worse than
Merge Sort in the Worst-Case.
Best-Case:
Split the list evenly each time.
O(nLogn)
Average-Case:
Assume pivot is selected randomly, so the probability of
pivot being the kth element is equal, k.
1
prob ( pivot k ) , k
n
n C A (n) (n 1)C A (n 1) 2 (n 1) 2 C A (n 1)
n C A (n) 2 (n 1) (n 1) C A (n 1)
• divide n(n + 1) for both sides
C A (n) C A (n 1) 2 (n 1)
n 1 n n(n 1)
C (n 2) 2 ( n 2) 2 (n 1)
A
n 1 (n 1) n n(n 1)
C (n 3) 2 ( n 3) 2 (n 2) 2 (n 1)
A
n2 (n 2) (n 1) ( n 1) n n(n 1)
Note :
C A (1) 0
C (1) 1 2 n 1
A 2( )
2 2 3 3 4 n(n 1)
n
k 1
0 2
k 2 k ( k 1)
n n
1 1
2 2 dk 2 ( Log e n Log e 1)
k 2 k 1
k
O( Logn)
C A (n) O (nLogn)
Best-Case = Average-Case
Integer Arithmetic
1. Find the maximum and minimum
else: mid ← i j ;
2
call Rmaxmin (i, mid, gmax, gmin);
call Rmaxmin (mid+1, j, hmax, hmin);
fmax ← MAX (gmax, hmax);
fmin ← MIN (gmin, hmin);
end
end;
Eg: find max and min in the array:
22, 13, -5, -8, 15, 60, 17, 31, 47 ( n = 9 )
Index: 1 2 3 4 5 6 7 8 9
Array: 22 13 -5 -8 15 60 17 31 47
(1)
Rmaxmin(1, 9, 60, -8)
(6)
1, 5, 22, -8 6, 9, 60, 17
(2) (5) (7) (8)
k 1 n k 1 k 2 1
2 T( k 1
) (2 2 2 )
2
k 1 n k
2 T (2) (2 2) 1 n 2
2
1.5n 2
They don’t have to be the same constant.
We make them the same here for simplicity.
It will not affect the overall result.
Theorem:
b n 1
where are a, b, c constants
T ( n) n
aT ( ) bn n 1
c
Claim:
O (n) ac
T (n) O (nLogcn) ac
Log c a
O (n ) ac
Proof: Assume n = ck
n
T (n) a T ( ) bn
c
n n n n
T (n) a (a T ( 2 ) b ) bn a 2 T ( 2 ) ab bn
c c c c
n n n
a 2 (a T ( 3 ) b 2 ) ab bn
c c c
3 n a2 a
a T ( 3 ) bn ( 2 1)
c c c
k n a k 1 a k 2 a0
a T ( k ) bn ( k 1 k 2 0 )
c c c c
a a a
a k b bn (( ) k 1 ( ) k 2 ( ) 0 )
c c c
n a a a
a k b k bn (( ) k 1 ( ) k 2 ( ) 0 )
c c c c
a a a a
bn ( ) k bn (( ) k1 ( ) k 2 ( ) 0 )
c c c c
k a
bn ( ( ) i )
i 0 c
k
a i
If a < c, ( c ) is a constant
i 0
T(n) = bn · a constant
T(n) = O(n)
k
a
if a = c, ( ) i k 1
i 0 c
k
T (n) bn1 bn (k 1)
i 0
bn ( Log c n 1)
O(nLogn)
a k 1
( ) 1
k
a i c
If a > c, (
c
)
a
i 0
( ) 1
c
a k 1
k ( ) 1
a i
T ( n) bn ( ) bn c
i 0 c
a
( ) 1
c
a k
bn ( ) b a k b a Log c n
c
b n Log c a
Log c a
O(n )
2. Integer multiplication
The problem:
Multiply two large integers (n digits)
The traditional way:
Use two for loops, it takes operations
The Divide-and-Conquer way:
Suppose large integers, , divide into two part a and
b, same as into c and d.
n
x: a b x a 10 b
2
y: c d y c 10 d
2
n n
x y (a 10 b)(c 10 d )
2 2
n
ac 10 n bd 10 (ad bc)
2
Therefore, we need to improve equation show before:
n
ac 10 n bd 10 2 (ad bc)
n
ac 10 n bd 10 2 ((a b)(c d ) ac bd )
Worst-Case is:
n
T (n) 3 T ( ) bn
2
O(n Log 2 3 ) O(n1.58 )
The algorithm by Divide-and-Conquer:
Large-int function multiplication (x,y)
begin
n = MAX ( # of digits in x, #of digits in y);
if (x = 0) or (y = 0), return 0;
else if (n = 1), return x*y in the usual way;
else n
m = 2 ;
a = x divide 10m;
b = x rem 10m;
c = y divide 10m;
d = y rem 10m;
p1= MULTIPLICATION (a, c);
p2= MULTIPLICATION (b, d);
p3 = MULTIPLICATION
2m m
(a+b, c+d);
p1 10 p 2 10 ( p3 p1 p 2 )
return ;
end;
example: x = 143, y = 256
• Thus:
T(n) = 2T(n/2) + O(n)
T(n) = O(n log n)
Divide and Conquer Strategy
•Problem subdivision – Divide and Conquer:
•Binary search,
•Quick sort,
•Merge sort,
•Integer Arithmetic,
• Maximum sub-array,
•Master’s theorem and its uses. Greedy Strategy
•Greedy strategy:
•Principle, control abstraction, time analysis of
control abstraction,
• knapsack problem,
• Job scheduling algorithm,
•Single Source Shortest Path Dijkstra’s,
• Bellman-Ford
Fractional Knapsack Problem
• Knapsack capacity: W
• There are n items: the i-th item has value Pi and weight wi
• Goal:
wixi W and
xiPi is maximum
Fractional Knapsack - Example
• E.g.: 20
---
$80
Item 3 30 +
Item 2 50 50
20 $100
Item 1 30
20 +
10 10 $60
• Pick the item with the maximum value per pound Pi/wi
• If the supply of that element is exhausted and the thief can carry
more: take as much as possible from the item with the next greatest
value per pound
• It is good to order items based on their value per pound
P1 P2 Pn
...
w1 w2 wn
Fractional Knapsack Problem
Alg.: Fractional-Knapsack (W, P[n], w[n])
5. w w – x iw i
67
JOB SEQUENCING WITH
DEADLINES (Contd..)
Sr.No. Feasible Processing Profit value
Solution Sequence
(i) (1,2) (2,1) 110
(ii) (1,3) (1,3) or (3,1) 115
(iii) (1,4) (4,1) 127 is the optimal one
(iv) (2,3) (2,3) 25
(v) (3,4) (4,3) 42
(vi) (1) (1) 100
(vii) (2) (2) 10
(viii) (3) (3) 15
(ix) (4) (4) 27
68
GREEDY ALGORITHM TO
OBTAIN AN OPTIMAL SOLUTION
• Consider the jobs in the non increasing
order of profits subject to the constraint that
the resulting job sequence J is a feasible
solution.
• In the example considered before, the non-
increasing profit vector is
(100 27 15 10) (2 1 2 1)
p1 p4 p3 p2 d1 d4 d3 d2
69
GREEDY ALGORITHM TO OBTAIN AN
OPTIMAL SOLUTION (Contd..)
J = { 1} is a feasible one
J = { 1, 4} is a feasible one with processing
sequence ( 4,1)
J = { 1, 3, 4} is not feasible
J = { 1, 2, 4} is not feasible
J = { 1, 4} is optimal
70