Professional Documents
Culture Documents
cse.iitkgp.ac.in
Algorithms – I (CS21203)
Merge Sort
8 3 2 9 5 1 7 4
8 3 2 9 5 1 7 4
8 3 2 9 5 1 7 4
8 3 2 9 5 1 7 4
3 8 2 9 1 5 4 7
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
Aug 10,11,16,17,18, 2023 4
CS21203 / Algorithms - I | Divide and Conquer
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 3 4 8
8 3 2 9 5 1 7 4
8 3 2 9 5 1 7 4
8 3 2 9 5 1 7 4
3 8 2 9 1 5 4 7
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
Aug 10,11,16,17,18, 2023 7
CS21203 / Algorithms - I | Divide and Conquer
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
[Constant time]
Θ ( 𝑛1 )
Θ ( 𝑛2 )
Θ ( 𝑛1 +𝑛 2)
Θ ( 1) 𝑝 𝑛
2 𝑟𝑝 𝑛
2 𝑟
𝑛
2∗
2
Θ ( 1)
𝑝𝑛
4𝑟𝑝𝑛
4𝑟
𝑝𝑛
4𝑟𝑝𝑛
4𝑟
𝑛
4∗
4
Θ ( 𝑛)
𝑝=𝑟
𝑝=𝑟 𝑝=𝑟𝑝=𝑟
𝑝 =𝑟 𝑝=𝑟
𝑝 =𝑟
𝑝=𝑟
𝑛𝑛 𝑛𝑛 𝑛 𝑛 𝑛𝑛
44 4 4 4 4 4 4
Recurrence Relation
• Let be the running time of the
algorithm when the input size is
• Base case takes constant time
Base case
Combine time =
𝑇𝑐𝑛
(𝑛)
𝑇 (𝑛)
𝑇 (
𝑛
2
) 𝑇 (
𝑛
2
) 𝑇 (𝑐𝑛
𝑛
2
)
𝑛 𝑛 𝑐𝑛 𝑛 𝑛 𝑐𝑛
𝑇 ( ) 𝑇 ( ) 𝑇 ( )𝑇 ( )
4 4 2 4 4 2
𝑛 𝑛 𝑛 𝑛
𝑇 ( )𝑇 ( 𝑇) ( ) 𝑇 ( )
How many ? 4 4 4 4
Base 𝑑 𝑑 𝑑 𝑑
{
𝑑 𝑛=1
𝑇 ( 𝑛 )=
2𝑇
𝑛
2 ( )
+𝑐𝑛 𝑛>1 𝑐𝑛
4
𝑐𝑛 𝑐𝑛
4 4
𝑐𝑛
4
Base 𝑑 𝑑 𝑑 𝑑
• Some terminologies
A Root
B C Internal
D E F G Leaf
• In a Binary Tree, a node can have at most two children
Some Terminologies
A Parent
D E F G
Root
A
Left subtree Right subtree
B C
D E F G
I J K L M N O 3
P Level 3 8=2
• Number of nodes at level
• Number of leaves Number of nodes at level [ is height]
• Naturally height
• Note: Number of Levels = Height (H) + 1
• Total number of nodes,
Some Terminologies
A
B C
I J K L M N O P
𝑐𝑛 𝑐𝑛
2 2 Level 1
{
𝑑 𝑛=1
𝑇 ( 𝑛 )=
2𝑇
𝑛
2 ( )
+𝑐𝑛 𝑛>1 𝑐𝑛
4
𝑐𝑛 𝑐𝑛
4 4
𝑐𝑛
4
Level
Base 𝑑 𝑑 𝑑 𝑑 Level
• To get the total runtime, first we need to get the height of the tree
• Number of leaves
• Base cost =
𝑐𝑛 𝑐𝑛
2∗
𝑐𝑛
2
= 𝑐𝑛
2 2 Level 1
𝑐𝑛 𝑐𝑛 𝑐𝑛 𝑐𝑛 𝑐𝑛
4∗ = 𝑐𝑛
4 4 4 4 4
Level
Base 𝑑 𝑑 𝑑 𝑑 Level
• What about the internal costs?
• 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 =
• Total cost =
Aug 10,11,16,17,18, 2023 21
CS21203 / Algorithms - I | Divide and Conquer
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
{
𝑑 𝑛=1
𝑇 ( 𝑛 )=
4𝑇
𝑛
2
+𝑐𝑛 𝑛>1 ( )
• Does it make sense?
• The general form of the recurrence can be
𝑐𝑛 𝑐𝑛 𝑐𝑛 𝑐𝑛
4 4 4 4
Base 𝑑 𝑑 𝑑 𝑑
• What will be the number of leaves? More than or less than previous?
• What about the height? More than or less than previous?
• Height
• Number of leaves
𝑐𝑛 𝑐𝑛 𝑐𝑛 𝑐𝑛 𝑐𝑛
4 4 4 4
Level 2 16
4
=4 𝑐𝑛
Level
Base 𝑑 𝑑 𝑑 𝑑 Level
• So base cost,
• Now the internal cost
• So, number of operations at level is
• Internal cost =
𝑐𝑛 𝑐𝑛 𝑐𝑛 𝑐𝑛 𝑐𝑛
4 4 4 4
Level 2 16
4
=4 𝑐𝑛
Level
Base 𝑑 𝑑 𝑑 𝑑 Level
• So base cost,
• Internal cost =
• Total cost =
Regularity Condition
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
4 7 9 5 15 11 17
4 7 5 9 15 11 12
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)
Quicksort
Θ ( 1)
Θ ( 𝑛)
Θ ( 1)
Θ ( 𝑛)
• Same as mergesort –
• However, this is the best case scenario
Θ ( 𝑛)
2c • Number of levels =
• Recurrence relation
Base case 𝐶𝑜𝑛𝑠𝑡 .
𝑖
𝑖 𝑛 −𝑖 −1
• The recurrence relation for this is
• 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.)
• and so on
𝑖 𝑖
𝑖 𝑛 −𝑖 −1 𝑛 −𝑖 −1 𝑖
• Divide everything by
𝑘=1
𝑘= 2
𝑘= 3
• All the bracketed sums are 1
• So, as
• Again, for or ,
• … (2)
• Combining (1) and (2), 1
Quicksort
• Quicksort was proposed by Sir Charles Antony Richard (Tony) Hoare
in 1959/60 at age 26
source: http://curation.cs.manchester.ac.uk/Turing100/www.turing100.manchester.ac.uk/speakers/invited-list/11-speakers/39-hoare.html
• He was trying to sort words for a machine translation project from
Russian to English
• In his own words – “I have been very lucky. What a wonderful way to start a
career in Computing, by discovering a new sorting algorithm! ”
• Emeritus Professor, Oxford University
• Turing award winner, 1980.
A Quick Animation
Click to
play
https://tin
yurl.com/3
k8pt3a3
Median Finding
• Given a set of numbers, a median is the “halfway point” of the set
• More formally, given a set of numbers, medians occur at (lower
median) and at (upper median)
• Following the book, by “median”, we will mean the lower median
Median Finding
• The problem is called the selection problem
• Input: Set of (distinct) numbers and an integer , with
• Output: The element that is larger than exactly other elements of
6 2 9 1 12 5 10 3
-1 3 4 -5 9 -2
• The problem will be trivial if the numbers are all positive
• By inspection the solution is the subarray from 3 to 9
• What can be a brute-force strategy to find this?
• As we are looking for start and end positions of the subarray, we can
try for all possible start indices and all possible end indices
• This means n-choose-2 i.e., algorithm
• Can we use divide-and-conquer paradigm to improve the runtime?
-1 3 4 -5 9 -2
• Then recursively solve the same problem on the divided subarrays
• Next we need to combine. While combining, the maximum sum can
come from the two subarrays as well from an array that spans across
the two subarrays
• We need to find the maximum sum corresponding to the subarray
crossing the centerline. And we need to find it efficiently.
• If we can find the maximum crossing subarray in time, then we have
a chance to get recursion leading to overall runtime
• To do so, we will cleverly use the fact that we are trying to find a
subarray whose one end lies on the right of the centerline and the
other end lies on the left
• The maximum subarray crossing the centerline will be a
concatenation of 1) a subarray whose one end is the centerline and
goes to the left 2) a subarray whose one end is the centerline and
goes to the right
• So, start at the centerline, keep going to the left, keep doing the
sum and keep track of the maximum of the sum. Similarly, for the
subarray going to the right from the centerline
Aug 10,11,16,17,18, 2023 54
CS21203 / Algorithms - I | Divide and Conquer
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
• Now we need to compare three sums – 1) one from entirely the left
subarray 2) one from entirely the right subarray 3) one from the
maximum crossing subarray
• Maximum of these three we will return
• Runtime of finding the maximum crossing subarray?
Θ ( 1)
Θ ( 𝑛)
Θ ( 1)
Aug 10,11,16,17,18, 2023 57
CS21203 / Algorithms - I | Divide and Conquer
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
Θ ( 1)
Θ ( 1)
𝑛
𝑇( )
2 𝑛
𝑇( )
2
Θ ( 𝑛)
Θ ( 1)
• All the diagonals of a convex polygon lie entirely inside the polygon
• Imagine that the points are represented by nails sticking out from a
board. Convex hull is the shape formed by a tight rubber band that
surrounds all the nails
Aug 10,11,16,17,18, 2023 60
CS21203 / Algorithms - I | Divide and Conquer
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
𝑎0 𝑏0
𝑎2 𝑎1
𝑏2
Aug 10,11,16,17,18, 2023 65
CS21203 / Algorithms - I | Divide and Conquer
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
𝑎0 𝑏0
𝑦 41
𝑦 00
𝑎2 𝑎1 𝑏2
Aug 10,11,16,17,18, 2023 66
CS21203 / Algorithms - I | Divide and Conquer
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
𝑏1
𝑎3 𝑎4
𝑎0 𝑏0
𝑎2 𝑎1 𝑏2
Aug 10,11,16,17,18, 2023 67
CS21203 / Algorithms - I | Divide and Conquer
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
𝑦 00
𝑎3
𝑎2 𝑏3
𝑎0
Start with connecting two
closest points to the
𝑎1 midline on either side
𝑏2
𝑦 00
𝑎3 𝑦 01
𝑎2 𝑏3
𝑎0
Move clockwise
from to
𝑎1
𝑏2
𝑦 30
𝑦 00
𝑎3 𝑦 01
𝑎2 𝑏3
𝑎0
As intercept decreased, we
will go back to on R.H.S. and
𝑎1 move counterclockwise on
L.H.S to
Aug 10,11,16,17,18, 2023 70
CS21203 / Algorithms - I | Divide and Conquer
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
𝑏2
𝑦 30
𝑦 31
𝑦 00
𝑎3 𝑦 01
𝑎2 𝑏3
𝑎0
As intercept increased, we
will move clockwise on R.H.S
𝑎1 to
𝑦 20 𝑏2
𝑦 30
𝑦 31
𝑦 00
𝑎3 𝑦 01
𝑎2 𝑏3
𝑎0
As intercept decreased, we
will go back to on R.H.S. and
𝑎1 move counterclockwise on
L.H.S to
Aug 10,11,16,17,18, 2023 72
CS21203 / Algorithms - I | Divide and Conquer
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
𝑦 20 𝑏2
𝑦 30
𝑦 31 𝑦 21
𝑦 00
𝑎3 𝑦 01
𝑎2 𝑏3
𝑎0
As intercept increased, we
will move clockwise on R.H.S
𝑎1 to
𝑎0
As intercept decreased, we
will go back to on R.H.S. and
𝑎1 move counterclockwise on
L.H.S to
Aug 10,11,16,17,18, 2023 74
CS21203 / Algorithms - I | Divide and Conquer
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
PseudoCode
// Initialize counters
i=j=0;
Return
𝑎3
𝑏3
𝑎2
𝑎0
𝑎1
𝑎0
𝑎1 • So the hull is
Aug 10,11,16,17,18, 2023 77
CS21203 / Algorithms - I | Divide and Conquer
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
𝛿 𝛿
𝛿 𝛿
𝛿 𝛿
𝒬𝑦 ℛ𝑦
𝒬𝑥 ℛ𝑥
Aug 10,11,16,17,18, 2023 86
CS21203 / Algorithms - I | Divide and Conquer
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
𝒫
𝒬 ℛ
𝒬𝑦 ℛ𝑦
𝒬𝑥 ℛ𝑥
Aug 10,11,16,17,18, 2023 87
CS21203 / Algorithms - I | Divide and Conquer
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
𝒬𝑦 ℛ𝑦
𝒬𝑥 ℛ𝑥
Aug 10,11,16,17,18, 2023 88
CS21203 / Algorithms - I | Divide and Conquer
Computer Science and Engineering| Indian Institute of Technology Kharagpur
cse.iitkgp.ac.in
• So, runtime
Thank You!!