View
225
Download
0
Category
Preview:
Citation preview
8/10/2019 Lecture 37 Closest Pair
http://slidepdf.com/reader/full/lecture-37-closest-pair 2/18
Closest-Pair AlgorithmClosest-Pair Algorithm
Given: A set of points in 2-D
8/10/2019 Lecture 37 Closest Pair
http://slidepdf.com/reader/full/lecture-37-closest-pair 3/18
Closest-Pair AlgorithmClosest-Pair Algorithm
Step 1: Sort the points in one D
8/10/2019 Lecture 37 Closest Pair
http://slidepdf.com/reader/full/lecture-37-closest-pair 4/18
Lets sort based on the X-axis
O(n log n)
1
2
3
4
!
"
#
$
1%
11
12
13 1
4
Closest-Pair AlgorithmClosest-Pair Algorithm
8/10/2019 Lecture 37 Closest Pair
http://slidepdf.com/reader/full/lecture-37-closest-pair 5/18
Step 2: Split the points& i'e'&Dra a line at the id-point beteen " and#
1
2
3
4
!
"
#
$
1%
11
12
13 1
4
Closest-Pair AlgorithmClosest-Pair Algorithm
S*b-+roble
1
S*b-+roble
2
8/10/2019 Lecture 37 Closest Pair
http://slidepdf.com/reader/full/lecture-37-closest-pair 6/18
Advantage: ,orall& e.d ha/e to0opare ea0h of the 14 points ith e/erother point'
(n-1)n2 13142 91 comparisons
1
2
3
4
!
"
#
$
1%
11
12
13 1
4
Closest-Pair AlgorithmClosest-Pair Algorithm
S*b-+roble
1
S*b-+roble
2
8/10/2019 Lecture 37 Closest Pair
http://slidepdf.com/reader/full/lecture-37-closest-pair 7/18
Advantage: ,o& e ha/e to s*b-
probles of half the sie' 5h*s& e ha/e todo !"2 0oparisons ti0e& hi0h is 420oparisons
1
2
3
4
!
"
#
$
1%
11
12
13 1
4
Closest-Pair AlgorithmClosest-Pair Algorithm
d1d2
S*b-+roble
1
S*b-+roble
2
sol*tion d in(d1& d2)
8/10/2019 Lecture 37 Closest Pair
http://slidepdf.com/reader/full/lecture-37-closest-pair 8/18
Advantage: 6ith 7*st one split e 0*t the
n*ber of 0oparisons in half' Ob/io*sl&e gain an e/en greater ad/antage if esplit the s*b-probles'
1
2
3
4
!
"
#
$
1%
11
12
13 1
4
Closest-Pair AlgorithmClosest-Pair Algorithm
d1d2
S*b-+roble
1
S*b-+roble
2
d in(d1& d2)
8/10/2019 Lecture 37 Closest Pair
http://slidepdf.com/reader/full/lecture-37-closest-pair 9/18
Problem: 8oe/er& hat if the 0losest to
points are ea0h fro di9erent s*b-probles
1
2
3
4
!
"
#
$
1%
11
12
13 1
4
Closest-Pair AlgorithmClosest-Pair Algorithm
d1d2
S*b-+roble
1
S*b-+roble
2
8/10/2019 Lecture 37 Closest Pair
http://slidepdf.com/reader/full/lecture-37-closest-pair 10/18
8ere is an exaple here e ha/e to
0opare points fro s*b-proble 1 to thepoints in s*b-proble 2'
1
2
3
4
!
"#
$
1%
11
12
13 1
4
Closest-Pair AlgorithmClosest-Pair Algorithm
d1d2
S*b-+roble
1
S*b-+roble
2
8/10/2019 Lecture 37 Closest Pair
http://slidepdf.com/reader/full/lecture-37-closest-pair 11/18
8oe/er& e onl ha/e to 0opare points
inside the folloing ;strip'<
1
2
3
4
!
"
#
$
1%
11
12
13 1
4
Closest-Pair AlgorithmClosest-Pair Algorithm
d1d2
S*b-+roble
1
S*b-+roble
2
dd
d in(d1& d2)
8/10/2019 Lecture 37 Closest Pair
http://slidepdf.com/reader/full/lecture-37-closest-pair 13/18
Closest Pair D & CClosest Pair D & C
AlgorithmAlgorithm 1) 6e sort all points a00ording to x 0oordinates'1) 6e sort all points a00ording to x 0oordinates'
2) Di/ide all points in to hal/es'2) Di/ide all points in to hal/es'
3) >e0*rsi/el ?nd the sallest distan0es in both s*barras'3) >e0*rsi/el ?nd the sallest distan0es in both s*barras'
4) 5a@e the ini* of to sallest distan0es' Let the4) 5a@e the ini* of to sallest distan0es' Let the
ini* be d'ini* be d'
) reate an arra stripBC that stores all points hi0h are at) reate an arra stripBC that stores all points hi0h are at
ost d distan0e aa fro the iddle line di/iding the toost d distan0e aa fro the iddle line di/iding the to
sets'sets' !) ind the sallest distan0e in stripBC'!) ind the sallest distan0e in stripBC'
") >et*rn the ini* of d and the sallest distan0e") >et*rn the ini* of d and the sallest distan0e
0al0*lated in abo/e step !0al0*lated in abo/e step !
8/10/2019 Lecture 37 Closest Pair
http://slidepdf.com/reader/full/lecture-37-closest-pair 15/18
Step 3: E*t& e 0an 0ontin*e the
ad/antage b splitting the s*b-probles'
1
2
3
4
!
"
#
$
1%
11
12
13 1
4
Closest-Pair AlgorithmClosest-Pair Algorithm
8/10/2019 Lecture 37 Closest Pair
http://slidepdf.com/reader/full/lecture-37-closest-pair 16/18
Step 3: Fn fa0t e 0an 0ontin*e to split *ntil
ea0h s*b-proble is tri/ial& i'e'& ta@es one0oparison'
1
2
3
4
!
"
#
$
1%
11
12
13 1
4
Closest-Pair AlgorithmClosest-Pair Algorithm
8/10/2019 Lecture 37 Closest Pair
http://slidepdf.com/reader/full/lecture-37-closest-pair 17/18
inall: 5he sol*tion to ea0h s*b-proble is
0obined *ntil the ?nal sol*tion isobtained
1
2
3
4
!
"
#
$
1%
11
12
13 1
4
Closest-Pair AlgorithmClosest-Pair Algorithm
Recommended