Professional Documents
Culture Documents
Algorithms-II : CS345A
Lecture 3
• Algorithm for Joint Seat Allocation contd.
• Divide and Conquer Paradigm - I
1
Homework from last class
2
Multiple Merit Lists
19 IITs
31 NITs
18 IIITs
33989 seats 1304127 candidates
18 OtherGFTIs
3
Fairness
Choice list of Mohan
p q p
Mohan
q
Ram
Ram Mohan
4
How to tackle Multiple Merit Lists using
“Single Merit list algorithm”
5
A common man’s algorithm
Allocation Allocation No
IITs NITs
Should we
remove from
NITs for future ?
Repeat
or ?
I prefer to .
𝒑 𝒓 𝒒 6
Choice list for IITs Choice list for NITs
A common man’s algorithm
Allocation_IIT();
Allocation_NIT();
; Round
While ( ) and Round )
{ For each do
{ is asked to choose between the two choices;
The preference list of is trimmed accordingly;
}
Allocation_IIT();
Allocation_NIT(); Round Round +
;
}
7
Homework
8
PROBLEM 1
Closest Pair Distance
9
Distance between 2 points
(𝑥 𝑗 , 𝑦 𝑗 )
O() time
( 𝑥𝑖 , 𝑦𝑖)
𝑥
10
The Closest Pair Distance Problem
𝑥
11
The Closest Pair Distance Problem
Problem Definition:
Given a set of points in plane,
compute the minimum Euclidean distance among all pairs of points.
Algorithms:
• O() : Trivial algorithm
• O() : Divide and Conquer based algorithm
12
Question 1
Tool 1 A fact from high school geometry
Fact :
Let and be any two points.
• If , distance()
• If , distance()
13
Question 2
Tool 2 A fundamental question about data structure ?
Question:
While solving an algorithmic problem, when do we feel the need of an
efficient data structure ?
Answer:
when the algorithm involves “many” operations of same type on a given data.
14
A DIVIDE AND CONQUER ALGORITHM
FOR
Closest Pair Distance
15
The divide step
𝑥
16
Solving the problem in 1-dimension
𝑥
Left half set points points 17
Right half set
Solving the problem in 1-dimension
𝜹𝑳 𝜹𝑹
𝑥
Left half set points points 18
Right half set
How to extend to 2-dimensions
𝑥
19
The divide step
𝑥
20
Solving the 2 smaller instances
𝜹𝑳
𝜹𝑹
Tool 1
¿𝜹
𝜹𝑳
𝜹𝑹
𝜹𝜹
min() 22
The combine step
Potentially points in each strip
𝜹𝑳
𝜹𝑹
𝜹
𝜹
min() 23
The combine step
𝜹𝑳
𝒑
𝜹𝑹
𝜹
𝜹
min() 24
The combine step
Tool 1
𝜹𝑳
𝜹
𝒑 𝜹
𝜹𝑹
𝜹
𝜹
min() 25
A discrete math exercise
Tool 3
Exercise:
What is the maximum number of points that can be placed in a𝜹unit − square
such that each pair of them is separated by distance at least 1𝜹 ?
Answer: 4.
1
√2
𝜹
1
26
The combine step
𝜹𝑳
Remember:
28
Break here
29
The combine step
Tool 2 𝜹
𝜹
𝜹
𝜹
Sort all points of the right strip in
𝜹𝑳 increasing order of -coordinate.
𝜹
′ ′
𝜹
𝜹
(𝑥 , 𝑦 ) Binary search for ()
𝒑 𝜹
𝜹Binary
𝑹
search for ()
𝜹
𝜹
min() 30
Divide and Conquer based algorithm
CP-Distance()
{ If (| |=1 ) return infinity;
{ Compute -median of ;
Split-by--median(); + 2 T(||/2) time
CP-Distance() ; Divide step
CP-Distance() ;
min(, );
strip of ;
strip of ;
Sorted array of ;
For each ,
time
y-coordinate of ;
Search for points with y-coordinate within ; Combine/conquer step
Compute distance from to each of these points;
Update accordingly;
return ;
}
31
Running time of the algorithm
What is the recurrence for running time?
c
Theorem:
There exists an O( ) time algorithm to compute the closest pair distance of
points in plane.
32
The conquer step in O() time
If the two strips are already sorted
𝜹𝑳
𝜹𝑹
𝜹𝜹
min() 33
The conquer step in O() time
If the two strips are already sorted
𝜹
𝒑
𝜹𝑳
𝜹𝑹
𝜹𝜹
min() 34
The conquer step in O() time
Tool 3 ?
𝒑
Fact : At most 4 consecutive points need to be considered.
𝜹𝑳
𝜹𝑹
𝜹𝜹
min() 36
The conquer step in O() time
If the two strips are already sorted
𝜹𝑳
𝜹𝑹
𝜹𝜹
min() 37
Inspiration from Merge sort
Merge
38
The conquer step in O() time
𝜹𝑳
𝜹𝑹
𝜹 𝑳𝜹 𝑳
39
Homework
• Make sincere attempts to write a neat pseudocode of the
algorithm.
40
Divide and Conquer based algorithm
CP-Distance()
{ If (| |=1 ) return (, );
Compute -median of ;
Split-by--median();
(, ) CP-Distance() ;
(, ) CP-Distance() ; + 2 T(||/2) time
min(, ); -Merge(, ); Divide step
strip of ; strip of ;
While( and )
{ first() ; first();
If( y() y() )
{ Compute distance from to the first 4 points in ;
Update accordingly;
time
Remove from ;
} conquer step
else … //similar processing for point ;
}
return (, )
}
41
Conclusion
Theorem:
There exists an O( ) time algorithm to compute the closest pair distance of
points in plane.
42
Homework
1. Complete the pseudocode of the algorithm.
3. We assumed that the median line passes through only one point. What if
the median line passes through multiple points. How will you ensure that the
left half as well as right half has points.
43