SlideShare a Scribd company logo
1 of 21
Rank 
Rank of an element is its position in ascending key 
order. 
[2,6,7,8,10,15,18,20,25,30,35,40] 
rank(2) = 0 
rank(15) = 5 
rank(20) = 7
Selection Problem 
• Given n unsorted elements, determine the 
k’th smallest element. That is, determine the 
element whose rank is k-1. 
• Applications 
 Median score on a test. 
• k = ceil(n/2). 
 Median salary of Computer Scientists. 
 Identify people whose salary is in the bottom 
10%. First find salary at the 10% rank.
Selection By Sorting 
• Sort the n elements. 
• Pick up the element with desired rank. 
• O(n log n) time.
D&C Selection Example 
Find kth element of: 
a 3 2 8 0 11 10 1 2 9 7 1 
Use 3 as the pivot and partition. 
a 1 2 1 0 2 34 10 11 9 7 8 
rank(pivot) = 5. So pivot is the 6’th 
smallest element.
D&C Selection Example 
a 1 2 1 0 2 34 10 11 9 7 8 
• If k = 6 (k-1 = rank(pivot)), pivot is the 
element we seek. 
• If k < 6 (k-1 < rank(pivot)), find k’th 
smallest element in left partition. 
• If k > 6 (k-1 > rank(pivot)), find (k-rank( 
pivot)-1)’th smallest element in right 
partition.
Time Complexity 
• Worst case arises when the partition to be 
searched always has all but the pivot. 
 O(n2) 
• Expected performance is O(n). 
• Worst case becomes O(n) when the pivot is 
chosen carefully. 
 Partition into n/9 groups with 9 elements each 
(last group may have a few more) 
 Find the median element in each group. 
 pivot is the median of the group medians. 
 This median is found using select recursively.
Closest Pair Of Points 
• Given n points in 2D, find the pair that are 
closest.
Applications 
• We plan to drill holes in a metal sheet. 
• If the holes are too close, the sheet will tear 
during drilling. 
• Verify that no two holes are closer than a 
threshold distance (e.g., holes are at least 1 inch 
apart).
Air Traffic Control 
• 3D -- Locations of airplanes flying in the 
neighborhood of a busy airport are known. 
• Want to be sure that no two planes get closer than 
a given threshold distance.
Simple Solution 
• For each of the n(n-1)/2 pairs of points, 
determine the distance between the points 
in the pair. 
• Determine the pair with the minimum 
distance. 
• O(n2) time.
Divide-And-Conquer Solution 
• When n is small, use simple solution. 
• When n is large 
 Divide the point set into two roughly equal parts A 
and B. 
 Determine the closest pair of points in A. 
 Determine the closest pair of points in B. 
 Determine the closest pair of points such that one 
point is in A and the other in B. 
 From the three closest pairs computed, select the one 
with least distance.
Example 
A B 
• Divide so that points in A have x-coordinate 
<= that of points in B.
Example 
d1 
A B 
• Find closest pair in A. 
• Let d1 be the distance between the points in 
this pair.
Example 
A B 
• Find closest pair in B. 
d1 
d2 
• Let d2 be the distance between the points in 
this pair.
Example 
A B 
• Let d = min{d1, d2}. 
d1 
d2 
• Is there a pair with one point in A, the other in 
B and distance < d?
Example 
A RRA B 
B 
• Candidates lie within d of the dividing line. 
• Call these regions RA and RB, respectively.
Example 
A R B A RB 
• Let q be a point in RA. 
q 
• q need be paired only with those points in RB that are 
within d of q.y.
Example 
q 
d 
2d 
A R B A RB 
• Points that are to be paired with q are in a d x 2d 
rectangle of RB (comparing region of q). 
• Points in this rectangle are at least d apart.
Example 
q 
d 
2d 
A R B A RB 
• So the comparing region of q has at most 6 points. 
• So number of pairs to check is <= 6| RA | = O(n).
Time Complexity 
• Create a sorted by x-coordinate list of points. 
 O(n log n) time. 
• Create a sorted by y-coordinate list of points. 
 O(n log n) time. 
• Using these two lists, the required pairs of points 
from RA and RB can be constructed in O(n) time. 
• Let n < 4 define a small instance.
Time Complexity 
• Let t(n) be the time to find the closest pair (excluding the 
time to create the two sorted lists). 
• t(n) = c, n < 4, where c is a constant. 
• When n >= 4, 
t(n) = t(ceil(n/2)) + t(floor(n/2)) + an, 
where a is a constant. 
• To solve the recurrence, assume n is a power of 2 
and use repeated substitution. 
• t(n) = O(n log n).

More Related Content

What's hot

Module 2 Design Analysis and Algorithms
Module 2 Design Analysis and AlgorithmsModule 2 Design Analysis and Algorithms
Module 2 Design Analysis and AlgorithmsCool Guy
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesjayavignesh86
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
10 merge sort
10 merge sort10 merge sort
10 merge sortirdginfo
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statisticsRajendran
 
07 Analysis of Algorithms: Order Statistics
07 Analysis of Algorithms: Order Statistics07 Analysis of Algorithms: Order Statistics
07 Analysis of Algorithms: Order StatisticsAndres Mendez-Vazquez
 
PCA (Principal component analysis) Theory and Toolkits
PCA (Principal component analysis) Theory and ToolkitsPCA (Principal component analysis) Theory and Toolkits
PCA (Principal component analysis) Theory and ToolkitsHopeBay Technologies, Inc.
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer Swati Kulkarni Jaipurkar
 
Recurrence relationclass 5
Recurrence relationclass 5Recurrence relationclass 5
Recurrence relationclass 5Kumar
 
Metodos jacobi y gauss seidel
Metodos jacobi y gauss seidelMetodos jacobi y gauss seidel
Metodos jacobi y gauss seidelCesar Mendoza
 

What's hot (20)

Quicksort
QuicksortQuicksort
Quicksort
 
Module 2 Design Analysis and Algorithms
Module 2 Design Analysis and AlgorithmsModule 2 Design Analysis and Algorithms
Module 2 Design Analysis and Algorithms
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
勾配法
勾配法勾配法
勾配法
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Relaxation method
Relaxation methodRelaxation method
Relaxation method
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
8. Hash table
8. Hash table8. Hash table
8. Hash table
 
Sorting
SortingSorting
Sorting
 
10 merge sort
10 merge sort10 merge sort
10 merge sort
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statistics
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
07 Analysis of Algorithms: Order Statistics
07 Analysis of Algorithms: Order Statistics07 Analysis of Algorithms: Order Statistics
07 Analysis of Algorithms: Order Statistics
 
Term paper
Term paperTerm paper
Term paper
 
PCA (Principal component analysis) Theory and Toolkits
PCA (Principal component analysis) Theory and ToolkitsPCA (Principal component analysis) Theory and Toolkits
PCA (Principal component analysis) Theory and Toolkits
 
Greedy Algorithms with examples' b-18298
Greedy Algorithms with examples'  b-18298Greedy Algorithms with examples'  b-18298
Greedy Algorithms with examples' b-18298
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer
 
Recurrence relationclass 5
Recurrence relationclass 5Recurrence relationclass 5
Recurrence relationclass 5
 
Metodos jacobi y gauss seidel
Metodos jacobi y gauss seidelMetodos jacobi y gauss seidel
Metodos jacobi y gauss seidel
 

Viewers also liked (20)

Lec39
Lec39Lec39
Lec39
 
Lec20
Lec20Lec20
Lec20
 
Lec26
Lec26Lec26
Lec26
 
Lec38
Lec38Lec38
Lec38
 
Lec24
Lec24Lec24
Lec24
 
Lec37
Lec37Lec37
Lec37
 
Lec25
Lec25Lec25
Lec25
 
Lec19
Lec19Lec19
Lec19
 
Lec21
Lec21Lec21
Lec21
 
Week7
Week7Week7
Week7
 
Lec22
Lec22Lec22
Lec22
 
Lec23
Lec23Lec23
Lec23
 
Lec27
Lec27Lec27
Lec27
 
Lec29
Lec29Lec29
Lec29
 
I 7
I 7I 7
I 7
 
Lec6
Lec6Lec6
Lec6
 
Lec23
Lec23Lec23
Lec23
 
Lec1
Lec1Lec1
Lec1
 
Lec30
Lec30Lec30
Lec30
 
10 Ways to Win at SlideShare SEO & Presentation Optimization
10 Ways to Win at SlideShare SEO & Presentation Optimization10 Ways to Win at SlideShare SEO & Presentation Optimization
10 Ways to Win at SlideShare SEO & Presentation Optimization
 

Similar to Lec36

lecture 10
lecture 10lecture 10
lecture 10sajinsc
 
ClosestPairClosestPairClosestPairClosestPair
ClosestPairClosestPairClosestPairClosestPairClosestPairClosestPairClosestPairClosestPair
ClosestPairClosestPairClosestPairClosestPairShanmuganathan C
 
Jurnal informatika
Jurnal informatika Jurnal informatika
Jurnal informatika MamaMa28
 
A Tutorial on Computational Geometry
A Tutorial on Computational GeometryA Tutorial on Computational Geometry
A Tutorial on Computational GeometryMinh-Tri Pham
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqPradeep Bisht
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesSreedhar Chowdam
 
High-dimensional polytopes defined by oracles: algorithms, computations and a...
High-dimensional polytopes defined by oracles: algorithms, computations and a...High-dimensional polytopes defined by oracles: algorithms, computations and a...
High-dimensional polytopes defined by oracles: algorithms, computations and a...Vissarion Fisikopoulos
 
Mba admission in india
Mba admission in indiaMba admission in india
Mba admission in indiaEdhole.com
 
Achieving Spatial Adaptivity while Searching for Approximate Nearest Neighbors
Achieving Spatial Adaptivity while Searching for Approximate Nearest NeighborsAchieving Spatial Adaptivity while Searching for Approximate Nearest Neighbors
Achieving Spatial Adaptivity while Searching for Approximate Nearest NeighborsDon Sheehy
 

Similar to Lec36 (20)

Lecture24
Lecture24Lecture24
Lecture24
 
lecture 10
lecture 10lecture 10
lecture 10
 
factoring
factoringfactoring
factoring
 
sorting
sortingsorting
sorting
 
ClosestPairClosestPairClosestPairClosestPair
ClosestPairClosestPairClosestPairClosestPairClosestPairClosestPairClosestPairClosestPair
ClosestPairClosestPairClosestPairClosestPair
 
Knn solution
Knn solutionKnn solution
Knn solution
 
Jurnal informatika
Jurnal informatika Jurnal informatika
Jurnal informatika
 
Knn 160904075605-converted
Knn 160904075605-convertedKnn 160904075605-converted
Knn 160904075605-converted
 
Linear sorting
Linear sortingLinear sorting
Linear sorting
 
Sorting2
Sorting2Sorting2
Sorting2
 
A Tutorial on Computational Geometry
A Tutorial on Computational GeometryA Tutorial on Computational Geometry
A Tutorial on Computational Geometry
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
 
lecture14.ppt
lecture14.pptlecture14.ppt
lecture14.ppt
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
convex hull
convex hullconvex hull
convex hull
 
Lec08 fitting
Lec08 fittingLec08 fitting
Lec08 fitting
 
High-dimensional polytopes defined by oracles: algorithms, computations and a...
High-dimensional polytopes defined by oracles: algorithms, computations and a...High-dimensional polytopes defined by oracles: algorithms, computations and a...
High-dimensional polytopes defined by oracles: algorithms, computations and a...
 
Mba admission in india
Mba admission in indiaMba admission in india
Mba admission in india
 
Unit 2
Unit 2Unit 2
Unit 2
 
Achieving Spatial Adaptivity while Searching for Approximate Nearest Neighbors
Achieving Spatial Adaptivity while Searching for Approximate Nearest NeighborsAchieving Spatial Adaptivity while Searching for Approximate Nearest Neighbors
Achieving Spatial Adaptivity while Searching for Approximate Nearest Neighbors
 

More from Nikhil Chilwant (11)

The joyless economy
The joyless economyThe joyless economy
The joyless economy
 
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
 
Lec18
Lec18Lec18
Lec18
 
Lec17
Lec17Lec17
Lec17
 
Lec16
Lec16Lec16
Lec16
 
Lec15
Lec15Lec15
Lec15
 
Lec14
Lec14Lec14
Lec14
 
Lec13
Lec13Lec13
Lec13
 
Lec12
Lec12Lec12
Lec12
 
Lec11
Lec11Lec11
Lec11
 
Lec10
Lec10Lec10
Lec10
 

Lec36

  • 1. Rank Rank of an element is its position in ascending key order. [2,6,7,8,10,15,18,20,25,30,35,40] rank(2) = 0 rank(15) = 5 rank(20) = 7
  • 2. Selection Problem • Given n unsorted elements, determine the k’th smallest element. That is, determine the element whose rank is k-1. • Applications  Median score on a test. • k = ceil(n/2).  Median salary of Computer Scientists.  Identify people whose salary is in the bottom 10%. First find salary at the 10% rank.
  • 3. Selection By Sorting • Sort the n elements. • Pick up the element with desired rank. • O(n log n) time.
  • 4. D&C Selection Example Find kth element of: a 3 2 8 0 11 10 1 2 9 7 1 Use 3 as the pivot and partition. a 1 2 1 0 2 34 10 11 9 7 8 rank(pivot) = 5. So pivot is the 6’th smallest element.
  • 5. D&C Selection Example a 1 2 1 0 2 34 10 11 9 7 8 • If k = 6 (k-1 = rank(pivot)), pivot is the element we seek. • If k < 6 (k-1 < rank(pivot)), find k’th smallest element in left partition. • If k > 6 (k-1 > rank(pivot)), find (k-rank( pivot)-1)’th smallest element in right partition.
  • 6. Time Complexity • Worst case arises when the partition to be searched always has all but the pivot.  O(n2) • Expected performance is O(n). • Worst case becomes O(n) when the pivot is chosen carefully.  Partition into n/9 groups with 9 elements each (last group may have a few more)  Find the median element in each group.  pivot is the median of the group medians.  This median is found using select recursively.
  • 7. Closest Pair Of Points • Given n points in 2D, find the pair that are closest.
  • 8. Applications • We plan to drill holes in a metal sheet. • If the holes are too close, the sheet will tear during drilling. • Verify that no two holes are closer than a threshold distance (e.g., holes are at least 1 inch apart).
  • 9. Air Traffic Control • 3D -- Locations of airplanes flying in the neighborhood of a busy airport are known. • Want to be sure that no two planes get closer than a given threshold distance.
  • 10. Simple Solution • For each of the n(n-1)/2 pairs of points, determine the distance between the points in the pair. • Determine the pair with the minimum distance. • O(n2) time.
  • 11. Divide-And-Conquer Solution • When n is small, use simple solution. • When n is large  Divide the point set into two roughly equal parts A and B.  Determine the closest pair of points in A.  Determine the closest pair of points in B.  Determine the closest pair of points such that one point is in A and the other in B.  From the three closest pairs computed, select the one with least distance.
  • 12. Example A B • Divide so that points in A have x-coordinate <= that of points in B.
  • 13. Example d1 A B • Find closest pair in A. • Let d1 be the distance between the points in this pair.
  • 14. Example A B • Find closest pair in B. d1 d2 • Let d2 be the distance between the points in this pair.
  • 15. Example A B • Let d = min{d1, d2}. d1 d2 • Is there a pair with one point in A, the other in B and distance < d?
  • 16. Example A RRA B B • Candidates lie within d of the dividing line. • Call these regions RA and RB, respectively.
  • 17. Example A R B A RB • Let q be a point in RA. q • q need be paired only with those points in RB that are within d of q.y.
  • 18. Example q d 2d A R B A RB • Points that are to be paired with q are in a d x 2d rectangle of RB (comparing region of q). • Points in this rectangle are at least d apart.
  • 19. Example q d 2d A R B A RB • So the comparing region of q has at most 6 points. • So number of pairs to check is <= 6| RA | = O(n).
  • 20. Time Complexity • Create a sorted by x-coordinate list of points.  O(n log n) time. • Create a sorted by y-coordinate list of points.  O(n log n) time. • Using these two lists, the required pairs of points from RA and RB can be constructed in O(n) time. • Let n < 4 define a small instance.
  • 21. Time Complexity • Let t(n) be the time to find the closest pair (excluding the time to create the two sorted lists). • t(n) = c, n < 4, where c is a constant. • When n >= 4, t(n) = t(ceil(n/2)) + t(floor(n/2)) + an, where a is a constant. • To solve the recurrence, assume n is a power of 2 and use repeated substitution. • t(n) = O(n log n).

Editor's Notes

  1. Algorithm doesn’t actually find closest pair with 1 point in A and other in B. Rather, it does this only if such a pair is the closest overall pair!
  2. When you add in the initial sort time for the sort by x and sort by y lists, the overall time remains O(n log n).