Divide & Conquer
Divide & Conquer
Decide on : algorithm
design techniques etc.
Design an algorithm
Prove correctness
Read Chapter 5
Divide and Conquer
Subproblem 1 Subproblem 2
of size n/2 of size n/2
Don’t assume
always breaks up
into 2, could be > 2
Solution to subproblems Solution to
subproblem 1 subproblem 2
Solution to the
original probelm
10
Logic/Idea
a0 + a1 + …… + an-1
2 10 3 5 7 1 6 10 1 3
0 1 2 3 4 0 1 2 3 4
2 10 3 5 7 1 6 10 1 3
# of additions
is same as in
2 10 3 5 7 brute force, 1 6 10 1 3
needs stack
for recursion…
12 3 5 7 7 10 1 3
Bad!
12 not all divide 4
and conquer
15 works!! 14
27 21
Could be efficient
48 for parallel processors though….
Department of Computer Science
Department of Computer Science
Example-2: Maximum Number
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
11 11 8 9
22 17
39 Divide:
Merge:
Department of Computer Science
Designing Algorithm for
maximum from N NUMBERS
problem using
DIVIDE AND CONQUER
Logic/Idea
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 9 7 5
9 7
9 Divide:
Merge:
Department of Computer Science
Example-2:
Brute Maximum Number
Force Example
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=0 j=0
tmp:
k=0
Department of Computer Science 25
Merge Algorithm
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=0 j=0
tmp: 3
k=0
Department of Computer Science 26
Merge Algorithm
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=1 j=0
tmp: 3 5
k=1
Department of Computer Science 27
Merge Algorithm
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=2 j=0
tmp: 3 5 6
k=2
Department of Computer Science 28
Merge Algorithm
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=2 j=1
tmp: 3 5 6 10
k=3
Department of Computer Science 29
Merge Algorithm
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=2 j=2
tmp: 3 5 6 10 14
k=4
Department of Computer Science 30
Merge Algorithm
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=2 j=3
tmp: 3 5 6 10 14 15
k=5
Department of Computer Science 31
Merge Algorithm
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=3 j=3
tmp: 3 5 6 10 14 15 22
k=6
Department of Computer Science 32
Merge Algorithm
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=3 j=4
tmp: 3 5 6 10 14 15 22 28
k=7
Department of Computer Science 33
Merge Algorithm
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=4 j=4
tmp: 3 5 6 10 14 15 22 28 30
k=8
Department of Computer Science 34
Merge Algorithm
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=5 j=4
Done.
tmp: 3 5 6 10 14 15 22 28 30 43 50
k=9
Department of Computer Science 35
Analysis of Merge Algorithm
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
Step 2 :Conquer
Step 3: Merge
How can we take advantage of a Merge
algorithm to perform step 2 and step 3
simultaneously?
That is how we utilize Merge algorithm to solve sorting
problem?
8 3 2 9 7 1 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
8 3 2 9 7 1 5 4
8 3 2 9 7 1 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
Department of Computer Science
Sorting Problem
MergeSort (A, p, r)
// This algorithm sort A[p..r] by divide & conquer
// Input : a sequence of n numbers stored in array A
//OUTPUT: an ordered sequence of n numbers
1 if p < r
2 then q (p+r)/2
3 MergeSort (A, p, q)
4 MergeSort (A, q+1, r)
5 Merge (A, p, q, r) // merges A[p..q] with A[q+1..r]
18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43
18 26 32 6 43 15 9 1 18 26 6 32 15 43 1 9
18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
T(n/2)
T(n/2)
Θ(n)
n n n
T ( k 1 ) 2.T ( k ) k 1
2 2 2
Department of Computer Science
Analysis of Merge-sort Algorithm
n n
T (n) 2.T ( ) (n) 2 .T ( 2 ) n n
2
2 2
n
T (n) 2 .T ( 2 ) n n
2
2
n
T (n) 2 .T ( 3 ) n n n
3
2
...
n
T (n) 2 .T ( k ) n
k
n .
n
. .
2 k times
Department of Computer Science
Analysis of Merge-sort Algorithm
n
T (n) 2 .T ( k ) n
k
n .
n
. .
2 k times
n
T (n) 2 .T ( k ) k .n
k
2
Let us suppose that : n 2 k log 2 n k
T(n) = 2T(n/2)+n
Here, a = ?, b = ?, d = ? a = 2, b = 2, d = 1
Which of the 3 cases holds ? a = bd
2 = 21, case 2
Department of Computer Science
Θ(nlgn)
Analysis of Merge-sort: Substitution Method
T(n) = 2T(n/2) + n
Guess: T(n) = O(nlgn)
Induction goal: T(n) ≤ cn lgn, for some c and n ≥ n0
Induction hypothesis: T(n/2) ≤ cn/2 lg(n/2)
N/2
Merge(A[1-2], A[3-4]) Merge(A[5-6], A[7-8])
N/4
Merge(A[1-4], A[5-8])
N/N
N=8
N/2=4
N/4= 2
N/8= 1
Department of Computer Science
In the first iteration, there are n/2
comparisons. In the second
iteration, n/2 sorted sequences of
two elements each are merged in
k = logn
pairs. The number of comparisons
needed to merge each pair is either
2 or 3. In the third iteration, n/4
sorted sequences of four elements
each are merged in pairs. The
number of comparisons needed to
merge each pair is between 4 and
N=8 7. In general, in the jth iteration of
the while loop, there are n/2j
merge operations on two subarrays
N/2=4 of size 2j−1 and it follows, that
N/4= 2 the number of comparisons needed
in the jth iteration is between
N/8= 1 (n/2j)2j−1 and (n/2j)(2j − 1).
Department of Computer Science
Case 1: when (n/2j)2j−1 comparison needed
CONCLUSION
Decide on : algorithm
design techniques etc.
Design an algorithm
Prove correctness
Read Chapter 5
Divide and Conquer
Lecture No 20
Subproblem 1 Subproblem 2
of size n/2 of size n/2
Don’t assume
always breaks up
into 2, could be > 2
Solution to subproblems Solution to
subproblem 1 subproblem 2
Solution to the
original probelm
OUTPUT
j <- i-1 17 29 | 34 45 68 89 90
while j ≥ 0 and A[j] > v do
17 29 34 | 45 68 89 90
A[j+1] <- A[j]
j <- j-1 17 29 34 45 | 68 89 90
A[j+1] <- v
17 29 34 45 68 | 89 90
17 29 34 45 68 89 | 90
17 29 34 45 68 89 90
17 | 29 34 45 68 89 90
17 | 29 34 45 68 89 90
17 29 | 34 45 68 89 90
17 29 34 45 68 89 | 90
17 29 34 45 68 89 90
(n2)
Department of Computer Science
Comparison of Merge sort and Quick Sort
How we do
that?
3 1 8 2 6 7 5
Recursive
c2 = a1*b1 c0 = a0*b0
c1 = (a1+a0)*(b1+b0)-(c2+c0)
We can write,
a = a110n/2 + a0
Why?
b = b110n/2 + b0
c2 = a1*b1 c0 = a0*b0
c1 = (a1+a0)*(b1+b0)-(c2+c0)