Divide and Conquer Algorithms Explained
Divide and Conquer Algorithms Explained
GTU # 3150703
Unit-3:
Divide and Conquer
Algorithms
Outline
Looping
▪ Introduction to Recurrence Equation
▪ Different methods to solve recurrence
▪ Divide and Conquer Technique
▪ Multiplying large Integers Problem
▪ Problem Solving using divide and conquer
algorithm –
✓ Binary Search
✓ Sorting (Merge Sort, Quick Sort)
✓ Matrix Multiplication
✓ Exponential
Recurrence Equation
Introduction
Many algorithms (divide and conquer) are recursive in nature.
When we analyze them, we get a recurrence relation for time complexity.
We get running time as a function of 𝒏 (input size) and we get the running time on inputs of
smaller sizes.
A recurrence is a recursive description of a function, or a description of a function in terms of
itself.
A recurrence relation recursively defines a sequence where the next term is a function of the
previous terms.
𝑻(𝒏) = 𝑻(𝒏 − 𝟏) + 𝒏 1
Replacing 𝑛Time
by 𝑛to − 1 and
solve the 𝑛 − 2, we can write following equations.
instance of size 𝑛
𝑻 𝒏−𝟏 =𝑻 𝒏−𝟐 +𝒏−𝟏 2
𝑻(𝒏) = 𝑻(𝒏 − 𝟑) + 𝒏 − 𝟐 + 𝒏 − 𝟏 + 𝒏 4
𝑻 𝒏 = 𝑻 𝒏 − 𝒏 + (𝒏 − 𝒏 + 𝟏) + (𝒏 − 𝒏 + 𝟐) + … + 𝒏
𝑻 𝒏 = 𝟎 +𝟏 + 𝟐 + …+𝒏
𝒏 𝒏+𝟏
𝑻 𝒏 = = 𝑶 𝒏𝟐
𝟐
#3150703 (ADA) Unit 3 – Divide & Conquer Algorithms 7
Substitution Method – Example 2
𝑐1 𝑖𝑓 𝑛 = 0
𝑡 𝑛 =ቊ
𝑐2 + 𝑡 𝑛 − 1 𝑜/𝑤
1 if n = 0 or 1
1. T n = ቊ
T n − 1 + n − 1 o/w
𝑝 𝑥 = ෑ 𝑥 − 𝑟𝑖
𝑖=1
The solution of recurrence is given as,
𝒌
𝒕𝒏 = 𝒄𝒊 𝒓𝒏𝒊
𝒊=𝟏
Function fibiter(n)
i ← 1; j ← 0;
for k ← 1 to n do
j ← i + j;
i ← j – i;
return j
Analysis of Iterative Algorithm: If we count all arithmetic operations at unit cost; the
instructions inside for loop take constant time 𝑐. The time taken by the for loop is bounded
above by 𝑛, 𝑖. 𝑒., 𝒏𝒄 = 𝛉(𝒏)
Case 1
#3150703 (ADA) Unit 3 – Divide & Conquer Algorithms 11
Homogeneous Recurrence – Example 1 : Fibonacci Series
If the value of 𝒏 is large, then time needed to execute addition operation increases linearly with
the length of operand.
At the end of 𝑘𝑡ℎ iteration, the value of 𝒊 and 𝒋 will be 𝒇𝒌−𝟏 and 𝒇𝒌.
As per De Moivre’s formula the size of 𝒇𝒌 is in 𝜽(𝒌).
So, 𝒌𝒕𝒉 iteration takes time in 𝜽(𝒌). let 𝒄 be some constant such that this time is bounded
above by 𝒄𝒌 for all 𝒌 ≥ 𝟏.
The time taken by fibiter algorithm is bounded above by,
𝑛 𝑛
𝑛 𝑛+1
𝑐. 𝑘 = 𝑐. 𝑘 = 𝑐.
2
𝑘=1 𝑘=1
𝑻 𝒏 = 𝜽 𝒏𝟐
Case 2
Function fibrec(n)
if n < 2 then return n
else return fibrec (n – 1) + fibrec (n – 2)
𝒌
The general solution is therefore of the form, 𝒏
𝒏 𝒏
𝑻𝒏 = 𝒄𝟏 𝒓𝟏 + 𝒄𝟐 𝒓𝟐 𝑻 𝒏 = 𝒄 𝒊 𝒓𝒊
𝒊=𝟏
Substituting initial values 𝑛 = 0 and 𝑛 = 1
𝑇0 = 𝑐1 + 𝑐2 = 0 1
𝑇1 = 𝑐1 𝑟1 + 𝑐2 𝑟2 = 1 (2)
Solving these equations, we obtain
1 1
𝑐1 = and 𝑐2 = −
5 5
𝑻𝒏 = 𝒄𝟏 𝒓𝒏𝟏 + 𝒄𝟐 𝒓𝒏𝟐
𝑛 𝑛
1 1+ 5 1− 5
𝑇𝑛 = − … … … de Moivre′ s formula
5 2 2
𝒏
𝑻𝒏 ∈ 𝑶 ∅
𝒕 𝒎 = 𝟐𝒎 − 𝟏 = 𝑶 𝟐𝒎
#3150703 (ADA) Unit 3 – Divide & Conquer Algorithms 18
Homogeneous Recurrence Exercises
Solve the following recurrences
𝑛 𝑖𝑓 𝑛 = 0 𝑜𝑟 1
1. 𝑡𝑛 = ቊ5𝑡
𝑛−1 − 6𝑡𝑛−2 𝑂/𝑊
𝑛 𝑖𝑓 𝑛 = 0, 1 𝑜𝑟 2
2. 𝑡𝑛 = ቊ
5𝑡𝑛−1 − 8𝑡𝑛−2 + 4𝑡𝑛−3 𝑜/𝑤
𝒏Τ𝒃 𝒏Τ𝒃
1 1 1 1 𝑻 𝒏 = 𝒏 𝒍𝒐𝒈 𝒏 + 𝒏
𝑻 𝒏 = 𝑶(𝒏 log 𝒏)
#3150703 (ADA) Unit 3 – Divide & Conquer Algorithms 26
Example 2: 𝑻(𝒏) = 𝑻(𝒏/𝟑) + 𝑻(𝟐𝒏/𝟑) + 𝒏
Recurrence Tree Method
The recursion tree for this recurrence is ▪ When we add the values across the levels of
the recursion tree, we get a value of 𝑛 for
every level.
log3/2 𝑛−1
𝑛
𝑇(𝑛) = 𝑛 + 𝑛log3/2 2 𝑇(1)
𝑖=0
𝑛Τ3 2𝑛Τ3 𝒏
𝒍𝒐𝒈𝟑 𝒏 𝒍𝒐𝒈𝟑/𝟐 𝒏 𝑻(𝒏) ∈ 𝒏 log 𝟑/𝟐 𝒏
1𝑛 2𝑛 1 2𝑛 2 2𝑛 𝒏
33 33 3 3 3 3
∞
𝑛Τ2 2
𝑛 Τ2 2 1Τ2 𝑛2 𝟏
𝒊
𝟐
𝑻 𝒏 ≤𝒏
𝟐
𝒊=𝟎
𝑛 Τ4 2 𝑛 Τ4 2 𝑛 Τ4 2 𝑛 Τ4 2 1Τ4 𝑛2 𝑻 𝒏 ≤ 𝟐𝒏𝟐
𝑻 𝒏 = 𝑶 𝒏𝟐
𝑂 𝑛2
#3150703 (ADA) Unit 3 – Divide & Conquer Algorithms 28
Recurrence Tree Method - Exercises
Example 1: 𝑇(𝑛) = 𝑇(𝑛/4) + 𝑇(3𝑛/4) + 𝑐. 𝑛
Example 2: 𝑇(𝑛) = 3𝑇(𝑛/4) + 𝑐. 𝑛2
Example 3: 𝑇(𝑛) = 𝑇(𝑛/4) + 𝑇(𝑛/2) + 𝑛2
Example 4: 𝑇(𝑛) = 𝑇(𝑛/3) + 𝑇(2𝑛/3) + 𝑛
1 3 7 9 11 32 52 74 90
Step 1:
1 2 3 4 5 6 7 8 9
1 3 7 9 11 32 52 74 90
1 3 7 9 11 32 52 74 90
IsIs𝟕𝟕< =midpoint
midpointvalue?
value?YES.
No.
Step 3:
1 2 3 4 5 6 7 8 9
1 3 7 9 11 32 52 74 90
1 3 7 9 11 32 52 74 90
1 3 7 9 11 32 52 74 90
1 3 7 9 11 32 52 74 90
Step 7:
1 2 3 4 5 6 7 8 9
1 3 7 9 11 32 52 74 90
2. Explain binary search algorithm and find the element 𝒙 = 𝟑𝟏 in the following array. [7]
10, 15, 18, 26, 27, 31, 38, 45, 59
3. Let 𝑻[𝟏. . 𝒏] be a sorted array of distinct integers. Give an algorithm that can find an index 𝒊
such that 𝟏 ≤ 𝒊 ≤ 𝒏 and 𝑻[𝒊] = 𝒊, provided such an index exists. Prove that your
algorithm takes time in 𝑂(𝑙𝑜𝑔𝑛) in the worst case.
𝟎𝟗𝟖𝟏 𝟏𝟐𝟑𝟒
𝒘 = 𝟎𝟗 𝒙 = 𝟖𝟏 𝒚 = 𝟏𝟐 𝒛 = 𝟑𝟒
2. We can write as,
Step 1: 𝒘 = 𝟖𝟏 𝒙 = 𝟏𝟒 𝒚 = 𝟕𝟔 𝒛 = 𝟐𝟐
𝑝 = 𝑤 ∙ 𝑦 = 81 ∙ 76 = 6156
𝑞 = 𝑥 ∙ 𝑧 = 14 ∙ 22 = 308
𝑟 = (𝑤 + 𝑥) ∙ (𝑦 + 𝑧) = 95 ∙ 98 = 9310
8114 × 7622 = 𝟏𝟎𝟒𝒑 + 𝟏𝟎𝟐 (𝒓 − 𝒑 − 𝒒) + 𝒒
= 61560000 + 284600 + 308
= 61844908
Unsorted Array
724 521 2 98 529 31 189 451
1 2 3 4 5 6 7 8
1 2 1 2 Split 1 2 1 2
724 521 2 98 529 31 189 451
1 1 1 1 1 1 1 1
724 521 2 98 529 31 189 451
1×6+3×4 1×8+3×2
𝑎𝑛𝑠𝑤𝑒𝑟 =
7×6+5×4 7×8+5×2
𝜽 𝒏𝒌 𝒊𝒇 𝒍 < 𝒃𝒌
𝒕 𝒏 = 𝜽 𝒏𝒌 𝒍𝒐𝒈𝒏 𝒊𝒇 𝒍 = 𝒃𝒌
𝜽 𝒏𝒍𝒐𝒈𝒃 𝒍 𝒊𝒇 𝒍 > 𝒃𝒌
#3150703 (ADA) Unit 3 – Divide & Conquer Algorithms 60
Quick Sort
Introduction
Quick sort chooses the first element as a pivot element, a lower bound is the first index and an
upper bound is the last index.
The array is then partitioned on either side of the pivot.
Elements are moved so that, those greater than the pivot are shifted to its right whereas the
others are shifted to its left.
Each Partition is internally sorted recursively.
Pivot
Element
0 1 2 3 4 5 6 7 8 9
42 23 74 11 65 58 94 36 99 87
LB UB
p ← T[i] 94 74 99
87 87
99
k ← i; l ← j+1 k l
Repeat
Swap
k ← k+1 until T[k] > p or k ≥ j
Repeat
87
94 74 94
87 99
l ← l-1 until T[l] ≤ p k l
While k < l do LB UB
Swap T[k] and T[l] Swap
Repeat k ← k+1 until 74 87
87 74 94 99
T[k] > p
Repeat l ← l-1 until k l
T[l] ≤ p 11 23 36 42 58 65 74 87 94 99
Swap T[i] and T[l]
function exposeq(a, n)
r ← a
for i ← 1 to n - 1 do
r ← a * r
return r
This algorithm takes a time in 𝜽(𝒏) since the instruction 𝒓 = 𝒂 ∗ 𝒓 is executed exactly 𝒏 −
𝟏 times, provided the multiplications are counted as elementary operations.
𝑀 𝑚, 𝑖𝑚 − 1 + 1 ≤ 𝑇 𝑚, 𝑛 ≤ 𝑀 𝑚, 𝑖𝑚
𝑖=1 𝑖=1
𝑛−1 𝑛−1
𝑇 𝑚, 𝑛 ≤ 𝑀 𝑚, 𝑖𝑚 ≤ 𝑐𝑚 𝑖𝑚
𝑖=1 𝑖=1
𝑛−1
𝑐𝑚2 𝑖 ≤ 𝑐𝑚2 𝑛2 = 𝜽 𝒎𝟐 𝒏𝟐
𝑖=1
function expoDC(a, n)
if n = 1 then return a
if n is even then return [expoDC(a, n/2)]2
return a * expoDC(a, n - 1)
#3150703 (ADA) Unit 3 – Divide & Conquer Algorithms 75
Exponentiation – D & C
0 𝑖𝑓 𝑛 = 1
Number of operations performed by 𝑁 𝑛 = ቐ𝑁 𝑛Τ2 + 1 𝑖𝑓 𝑛 𝑖𝑠 𝑒𝑣𝑒𝑛
the algorithm is given by, 𝑁 𝑛 − 1 + 1 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
0 𝑖𝑓 𝑛 = 1
Time taken by the 𝑇 𝑚, 𝑛 = ቐ𝑇 𝑚, 𝑛Τ2 + 𝑀 𝑚 𝑛Τ2, 𝑚 𝑛Τ2 𝑖𝑓 𝑛 𝑖𝑠 𝑒𝑣𝑒𝑛
𝑇 𝑚, 𝑛 − 1 + 𝑀 𝑚, 𝑛 − 1 𝑚 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
algorithm is given by,
Solving it gives, 𝑻(𝒎, 𝒏) ∈ 𝛉 (𝒎𝒍𝒈𝟑 𝒏𝒍𝒈𝟑 )
function expoDC(a, n)
if n = 1 then return a
if n is even then return [expoDC(a, n/2)]2
return a * expoDC(a, n - 1)
#3150703 (ADA) Unit 3 – Divide & Conquer Algorithms 76
Exponentiation – Summary
Multiplication
Classic D&C
exposeq 𝜽 𝒎𝟐 𝒏 𝟐 𝜽 𝒎𝒍𝒈𝟑 𝒏𝟐
expoDC 𝜽 𝒎𝟐 𝒏 𝟐 𝜽 𝒎𝒍𝒈𝟑 𝒏𝒍𝒈𝟑