# 03 Recursion DAC

```Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Algorithms – I (CS29003/203)
Autumn 2022, IIT Kharagpur
Divide and Conquer, Recursion
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Divide and Conquer
• General techniques
• Merge sort
• Recursion Tree Method
• Master Theorem
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and COnquer
2
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Divide and Conquer
• Divide and conquer involves three steps
• Divide a problem into pieces (smaller instances of same problem)
•
E.g., divide into halves
• Conquer the subproblems by solving them recursively
•
Can just call the same algorithm on the subproblems (recursively solving)
•
Base case: when n=1 (or is small)
• Combine solutions to pieces to get answer to original problem
•
Combining results from recursive calls.
•
Usually the hardest part in the algorithm design
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
3
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Merge Sort
8 3 2 9 5 1 7 4
8 3 2 9
8 3
8
5 1
2 9
3
3 8
5 1 7 4
2
9
2 9
2 3 8 9
7 4
1
5
1 5
7
4
4 7
1 4 5 7
1 2 3 4 5 7 8 9
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
4
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Merge Sort
• Dividing is easy. The key operation, though, is merge.
• How to do merging of two sorted arrays to produce a merged
sorted array?
2 5 9
Aug 10,11,12,17, 2022
3 4 8
CS21003/CS21203 / Algorithms - I | Divide and Conquer
5
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Merge Sort Pseudocode
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
6
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Merge Sort – In What Order?
8 3 2 9 5 1 7 4
8 3 2 9
8 3
8
5 1
2 9
3
3 8
5 1 7 4
2
9
2 9
2 3 8 9
7 4
1
5
1 5
7
4
4 7
1 4 5 7
1 2 3 4 5 7 8 9
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
7
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Merge Sort – Runtime Analysis
Total number of elements in two arrays is π1 + π2
Θ 1 [Constant time]
Θ π1
Θ π2
Θ 1
Overall runtime of “MERGE”
subroutine is Θ(total number
of elements after merging)
Θ π1 + π2
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
8
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Merge Sort – Runtime Analysis
π
Θ 1
Θ 1
π
π
πΰ΅
2
π
π
π
πΰ΅
2
π
2 ∗ πΰ΅2
4 ∗ πΰ΅4
π πΰ΅4 π π πΰ΅4 π
π πΰ΅4 π π πΰ΅4 π
π = π π = π π = ππ = π
π = ππ = π π = ππ = π
Θ π
8 ∗ πΰ΅8
πΰ΅ πΰ΅
4
4
πΰ΅ πΰ΅
4
4
πΰ΅ πΰ΅
4
4
πΰ΅ πΰ΅
4
4
• How do we count the recursive calls?
• Recursive calls belong to the next level
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
9
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Merge Sort – Runtime Analysis
• What are the number of levels in mergesort?
• log π
• There are log π levels and at each level the number of operations is
Θ(π)
• The runtime of mergesort is Θ(π log π)
• Next we will learn how to write an equation that represents the
running time of a divide and conquer algorithm.
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
10
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Recurrence Relation
• Let π π be the running time of the
algorithm when the input size is π
• Base case takes constant time
Base case
Divide time = const
π
• π π = ΰ΅π + Θ π + 2π
π
2
π=1
π&gt;1
π
Conquer time = π(2 ) each
Combine time = Θ(π)
π
• π π = ΰ΅2π
π
2
+Θ π
π=1
π&gt;1
• Recurrence relation: Represents the
running time of a recursive algorithm
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
11
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Solving Recurrences – Recursion Tree
π
• π π = ΰ΅2π
π
2
+Θ π
π=1
π&gt;1
• We can write Θ π as some const.*π
• Does the base case need to be always for π = 1?
• General form:
• π π = α
π
Aug 10,11,12,17, 2022
2π
π = π0
π
2
+ ππ
π &gt; π0
CS21003/CS21203 / Algorithms - I | Divide and Conquer
12
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Solving Recurrences – Recursion Tree
•
•
•
•
π
π=1
π
π π = α
2π
+ ππ π &gt; 1
2
Left hand side is π(π). This is a node
It expands to 2, π(πΤ2) nodes and one ππ node
The convention is to replace π(π) with these nodes
And continue
ππ
π(π)
π(π)
π(πΰ΅2)
π(πΰ΅4)
Aug 10,11,12,17, 2022
π(πΰ΅2)
π(πΰ΅4)
ππΰ΅
2
πΰ΅ )
π(ππ
2
π(πΰ΅4)
π(πΰ΅4)
CS21003/CS21203 / Algorithms - I | Divide and Conquer
ππΰ΅
2
13
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Solving Recurrences – Recursion Tree
π
π=1
π
π π = α
2π
+ ππ π &gt; 1
2
•
•
•
•
Left hand side is π(π). This is a node
It expands to 2, π(πΤ2) nodes and one ππ node
The convention is to replace π(π) with these nodes
And continue
ππ
The tree ends
when the base
case is reached
π(π)
ππΰ΅
2
π(πΰ΅4)
How many π ?
Base
Aug 10,11,12,17, 2022
π(πΰ΅4) π(πΰ΅4)
ππΰ΅
2
π(πΰ΅4)
π π π
CS21003/CS21203 / Algorithms - I | Divide and Conquer
π
14
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Solving Recurrences – Recursion Tree
ππ
ππΰ΅
2
ππΰ΅
2
π
π=1
π π = α2π π + ππ π &gt; 1
2
ππΰ΅
4
Base
ππΰ΅
4
ππΰ΅
4
ππΰ΅
4
π π π
π
• Total runtime comes from counting number of operations from the
internal nodes and counting them from base nodes
• Lets get introduced to trees and some related terminologies as we need
• More will come later
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
15
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Solving Recurrences – Recursion Tree
• Graphs are sets of nodes and edges
• Trees are a form of a graph with some specialties
• Trees have direction (parent/child relationships) and don't contain
cycles
• Some terminologies
Root
A
D
Internal
C
B
E
F
G
Leaf
• In a Binary Tree, a node can have at most two children
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
16
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Some Terminologies
Parent
A
Right child
Left child
B
D
C
Siblings
G
F
E
Root
A
Right subtree
Left subtree
C
B
D
Aug 10,11,12,17, 2022
E
F
G
CS21003/CS21203 / Algorithms - I | Divide and Conquer
17
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Some Terminologies
A
C
B
D
D
•
•
•
•
•
E
D
G
F
E
E
D
E
D
E
Level 0
Max # of
Nodes
1 = 20
Level 1
2 = 21
Level 2
4 = 22
Level 3
8 = 23
Number of nodes at level π = 2π
Number of leaves πΏ = Number of nodes at level π» ππ  2π» [π» is height]
Naturally height π» = log 2 πΏ
Note: Number of Levels = Height (H) + 1
π
π»+1
Total number of nodes, π = σπ»
−1
π=0 2 = 2
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
18
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Some Terminologies
A
C
B
D
D
E
D
E
D
For general `k’-array Tree
G
F
E
E
D
E
• Number of nodes at level π = π π
• Number of leaves πΏ = Number of nodes at level π» = π π» [π» is height]
• Naturally height π» = log π πΏ
π
• Total number of nodes, π = σπ»
π=0 π =
Aug 10,11,12,17, 2022
π π»+1 −1
π−1
CS21003/CS21203 / Algorithms - I | Divide and Conquer
19
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Solving Recurrences – Recursion Tree
Level 0
ππ
ππΰ΅
2
ππΰ΅
2
π
π=1
π π = α2π π + ππ π &gt; 1
2
ππΰ΅
4
ππΰ΅
4
ππΰ΅
4
Level 1
ππΰ΅
4
Level π» − 1
Base
π π π
π
Level π»
• To get the total runtime, first we need to get the height of the tree
π» = log 2 π
π»
log
• Number of leaves πΏ = 2 = 2 2 π = π
• Base cost = π &times; πΏ = ππ = Θ(π)
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
20
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Solving Recurrences – Recursion Tree
ππ
Level 0
ππ
2 ∗ ππΰ΅2 = ππ
ππΰ΅
2
ππΰ΅
2
4 ∗ ππΰ΅4 = ππ
ππΰ΅
4
ππΰ΅
4
ππΰ΅
4
Level 1
ππΰ΅
4
Level π» − 1
Base
•
•
•
•
•
π π π
π
Level π»
We need to sum the costs at each internal node
Since the costs at each level is same we can multiply by height
Internal cost = ππ ∗ π» = ππ log 2 π = Θ (π log2 π)
Total cost =Θ π + π π log2 π = π π log2 π
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
21
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Solving Recurrences – Recursion Tree
• Lets take another example
π
π=1
π
π π = α
4π
+ ππ π &gt; 1
2
• Does it make sense?
• The general form of the recurrence can be
T n = ππ
•
•
•
π
+ π(π)
π
Where, π is number of subproblems (branching factor)
π is the size of each subproblem (division ratio)
π(π) total time for divide and combine operations
• What are these values for binary search?
T n =π
• with base case time - constant
Aug 10,11,12,17, 2022
π
+π
2
CS21003/CS21203 / Algorithms - I | Divide and Conquer
22
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Solving Recurrences – Recursion Tree
π
π=1
π π = α4π π + ππ π &gt; 1
2
cπ
πππ
π(
ΰ΅2ΰ΅2)
ππΰ΅
4
ππΰ΅
4
ππΰ΅
4
Base
πππ
π(
ΰ΅2ΰ΅2)
πππΰ΅ΰ΅ )
π(
22
πΰ΅ΰ΅ )
π(ππ
22
ππΰ΅
4
π π π
π
• What will be the number of leaves? More than or less than previous?
• What about the height? More than or less than previous?
• Height π» = log π π = log 2 π
• Number of leaves πΏ = ππ» = 4log2 π = π2
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
23
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Solving Recurrences – Recursion Tree
π
π=1
π π = α4π π + ππ π &gt; 1
2
cπ
ππΰ΅
2
ππΰ΅
4
ππΰ΅
4
ππΰ΅
4
ππΰ΅
2
ππΰ΅
2
ππ
Level 0
ππΰ΅
2
ππΰ΅
4
ππ
= 2ππ
2
Level 1
4
Level 2
16
ππ
= 4ππ
4
Level π» − 1
Base
π π π
π
Level π»
• So base cost, = π &times; πΏ = ππ2 = π(π2 )
• Now the internal cost
• So, number of operations at level π is 2π ππ
π
π»
log2 π
• Internal cost = σπ»−1
− 1 = ππ π − 1 =
π=0 2 ππ = ππ 2 − 1 = ππ 2
2
ππ − ππ
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
24
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Solving Recurrences – Recursion Tree
π
π=1
π π = α4π π + ππ π &gt; 1
2
cπ
ππΰ΅
2
ππΰ΅
4
ππΰ΅
4
ππΰ΅
4
ππΰ΅
2
ππΰ΅
2
ππ
Level 0
ππΰ΅
2
ππΰ΅
4
ππ
= 2ππ
2
Level 1
4
Level 2
16
ππ
= 4ππ
4
Level π» − 1
Base
π π π
π
Level π»
• So base cost, = π &times; πΏ = ππ2 = π(π2 )
• Internal cost = ππ2 − ππ
• Total cost = ππ2 + ππ2 − ππ = π(π2 )
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
25
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Solving Recurrences – Master Theorem
• Review of the general procedure
π
• General form:
• π π = α
•
•
•
•
π
ππ
π
π( )
π
π ≤ π0
π
π
+ π(π)
π(π)
π
π( )
π
π
π( )
π
π
π( )
π
π &gt; π0
where π is base case runtime
π is number of subproblems (branching factor) Base π
π is the size of each subproblem (division ratio)
π(π) total time for divide and combine operations
π
π
• Compute Height π» = log π π
• Compute number of leaves πΏ = ππ» = πlogπ π = πlogπ π
• Compute base cost (Generally - πΏ &times; π)
π
• Compute internal cost π π + ππ
+ π2 π
π
• Sum base and internal cost
Aug 10,11,12,17, 2022
π
π2
+β―
CS21003/CS21203 / Algorithms - I | Divide and Conquer
26
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Solving Recurrences – Master Theorem
• Master theorem provides a consolidated set of rules
• It depends on the relation between the number of leaves (πΏ =
πlogπ π ) and the function π(π)
• Case I: If πΏ is polynomially larger i.e., π π = π πlogπ π−β , where β
is some constant &gt; 0, then π π = Θ(πlogπ π )
• β is the difference in the polynomial degree between πΏ and π(π)
• If πΏ = π2 and π π = π, then β=? ο  1
3
• Similarly, if πΏ = π2 and π π = π2 , then β=? ο  0.5
• Another way of thinking this is – the ratio between πΏ and π(π)
must be at least a polynomial
π2
πΏ
• Take πΏ = π2 and π π =
, Then
= log π and log π is less
log π
π(π)
than polynomial
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
27
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Solving Recurrences – Master Theorem
• Case I: If πΏ is polynomially larger i.e., π π = π πlogπ π−β , where β
is some constant &gt; 0, then π π = Θ(πlogπ π )
•
β is the difference in the polynomial degree between πΏ and π(π)
• Case II: If π π and πΏ are asymptotically same, i.e., π π = Θ πlogπ π
then π π = Θ πlogπ π log π = Θ(π(π) log π)
• Case III: If π(π) is polynomially larger i.e., π π = Ω πlogπ π+β ,
π
then π π = Θ(π(π)) if ππ( ) ≤ ππ(π) for π &lt; 1
Regularity Condition
π
π
+ ππ
2
π = 2, π = 2
πΏ = πlogπ π = π
π π = ππ
Case II: π π = Θ(π log π)
π π = 2π
Aug 10,11,12,17, 2022
π
+ ππ
2
π = 4, π = 2
πΏ = πlogπ π = π2
π π = ππ
π π = 4π
Case I: π π = Θ πlogπ π = Θ(π2 )
CS21003/CS21203 / Algorithms - I | Divide and Conquer
28
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Quicksort
• Quicksort is a sorting algorithm which puts elements in the right
places one by one
• What is a “right place”?
• Is there any element which is at the right place?
3
7
9
5
15 11
4
7
9
5
15 11 17
4
7
5
9
15 11 12
Aug 10,11,12,17, 2022
4
CS21003/CS21203 / Algorithms - I | Divide and Conquer
29
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Quicksort
• Lets walkthrough an example to understand how quicksort works
7
6
10
5
9
2
1
15
7
• The crucial step in quicksort is partioning (divide into two parts
such that left of pivot is less and right of pivot id more)
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
30
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Quicksort
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
31
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Quicksort – Runtime Analysis
Θ 1
Θ π
Overall runtime of “PARTITION”
subroutine is Θ(π)
Θ 1
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
32
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Quicksort – Runtime Analysis
Θ π
• Lets take two extreme cases
Partitions are perfectly balanced everytime
• Number of levels = log 2 π
• Recurrence relation
π
+ ππ
2
• Same as mergesort – Θ(π log π)
π π = 2π
•
Aug 10,11,12,17, 2022
However, this is the best case scenario
CS21003/CS21203 / Algorithms - I | Divide and Conquer
33
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Quicksort – Runtime Analysis
Θ π
• Lets take two extreme cases
Partitions are most unbalanced everytime
• Number of levels = Θ(π)
• Recurrence relation
πΆπππ π‘.
π π = π π − 1 + π(0) + ππ
• Lets try to solve this recusrsive eqn.
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
34
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Quicksort – Runtime Analysis
• Base case = ππππ π‘.
cπ
• Internal cost at level π is π(π − π)
C(π − 1)
• Total internal cost
π 2+ 3+ β―π
C(π − 2)
• Number of levels = Θ(π)
2c
• Recurrence relation
Base case
πΆπππ π‘.
π π = π π − 1 + π 0 + ππ
• Same as insertion sort – Θ π2
•
Aug 10,11,12,17, 2022
This is the worst case scenario
CS21003/CS21203 / Algorithms - I | Divide and Conquer
35
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Quicksort – Runtime Analysis
• What should the input look like for the best case?
•
The medians are chosen as pivots always
• What should the input look like for the worst case?
•
The input array is already sorted
• Probability of getting an already sorted array is
1
π!
• However, the problem can be tackled if instead of always taking
the first element as pivot, we take any element as pivot in random
• The average-case time complexity of the randomized version of
quicksort is Θ(π log π)
• The proof of this involves a more complicated recurrence than we
have seen so far.
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
36
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Quicksort – Runtime Analysis
• We will follow “Introduction to Algorithms and Data Structures” by
M. J. Dinneen, G. Gimelfarb, M. C. Wilson for the proof
• Let π(π) denote the average-case running time for size π list
• In the first step, the time taken to compare all the elements with
the pivot is ππ
• Let 0 ≤ π ≤ π − 1 is the final position of the pivot. So, the two
sublists are of size π and π − π − 1 respectively
π
π
π−π−1
• The recurrence relation for this π is π π = π π + π π − 1 − π + ππ
•
Small detail: Last term should be Θ π , as it depends on the exact implementaiton
(e.g., whether comparison starts from pivot or one element after, etc.)
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
37
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Quicksort – Runtime Analysis
π
•
•
•
•
•
π
π−π−1
The recurrence relation for this π is π π = π π + π π − 1 − π + ππ
The final pivot position π can be any of the π positions with equal
probability
So, the value of π(π) to be π 0 + π π − 1 + ππ (i.e., π = 0) has
1
probability
π
Similarly, the value of π(π) to be π 1 + π π − 2 + ππ (i.e., π = 1)
1
has probability and so on
π
So the expected value of π(π) is given by,
π−1
π−1
1
1
π π = ΰ· π π + π π − 1 − π + ππ = ΰ· π π + π π − 1 − π
π
π
π=0
Aug 10,11,12,17, 2022
+ ππ
π=0
CS21003/CS21203 / Algorithms - I | Divide and Conquer
38
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Quicksort – Runtime Analysis
1
• π π = σπ−1
(π π + π π − 1 − π ) + ππ
π π=0
• π 0 + π π − 1 = π π − 1 + π 0 πππ π = 0 πππ π = π − 1
• π 1 + π π − 2 = π π − 2 + π 1 πππ π = 1 πππ π = π − 2 and so
on
π
π
π
• π π =
π−π−1
1 π−1
σ (π
π π=0
π−π−1
π
2
π
π + π π − 1 − π ) + ππ = σπ−1
π=0 π π + ππ
2
• Let us rewrite this as ππ π = 2 σπ−1
π=0 π π + ππ
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
39
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Quicksort – Runtime Analysis
2
• ππ π = 2 σπ−1
π=0 π π + ππ
• Replace π by π − 1 and subtract from the above
•
ππ π = 2 π 0 + π 1 + β― π π − 2 + π π − 1 + ππ2
π − 1 π π − 1 = 2 π 0 + π 1 + β― + π π − 3 + π(π − 2) + π π − 1
• ππ π − π − 1 π π − 1 = 2π π − 1 + π π2 − π − 1
• ππ π = π + 1 π π − 1 + π(2π − 1)
• Divide everything by π(π + 1)
•
π(π)
π+1
=
π(π−1)
+
π
π
2π−1
π(π+1)
=
π(π−1)
+
π
π
2
2
3
1
−
π+1
π
• Last term uses partial fraction decomposition
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
40
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Quicksort – Runtime Analysis
π(π)
π+1
=
π(π−1)
+
π
π
•
π(1)
2
=
π(0)
3π
+
1
2
−
π
1
•
π(2)
3
=
π(1)
3π
+
2
3
−
π
2
•
π(3)
4
=
π(2)
3π
+
3
4
−
π
3
•
π(π)
π+1
=
π(π−1)
3π
π
+
−
π
π+1
π
•
π(π)
π+1
=
π(0)
+
1
•
2π−1
π(π+1)
=
• Lets put π = 1, 2,3, … π
Aug 10,11,12,17, 2022
3π
π(π−1)
+
π
π
1
1
1
1
+ + +β―+
2
3
4
π+1
3
1
−
π+1
π
−π
1
1
1
+ + +
1
2
3
CS21003/CS21203 / Algorithms - I | Divide and Conquer
β―+
1
π
41
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Quicksort – Runtime Analysis
•
π(π)
π+1
=
π(0)
+
1
•
π(π)
π+1
=
π(0)
−π
1
1
2
3π
+
1
3
1
1
1
1
+ + +β―+
2
3
4
π+1
3π
π+1
+ 2π
1
1
+
2
3
1
4
−π
+ + β―+
1
π
1
1
1
+ + +
1
2
3
=π−π+
β―+
3π
+
π+1
1
π
2π(π»π − 1)
1
π
• π»π = 1 + + + β― + ; π = π(0)
• π π = π − π π + 1 + 3π + 2π π + 1 π»π − 1
•
= π + π − 3π π + 2ππ»π + 2πππ»π
• Using a result π»π = Θ(log π), we can write
• π π = π + π − 3π π + 2πΘ log π + 2πΘ π log π = Θ(π log π)
• π»π = Θ log π -&gt; This will shown to be true for a special case, next
Aug 10,11,12,17, 2022
CS21003/CS21203 / Algorithms - I | Divide and Conquer
42
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
To be continued …
```