Question

For the following set of points, describe how the CLOSEST-PAIR algorithm finds a closest pair of points: (3) (3,2),(2,1)...

For the following set of points, describe how the CLOSEST-PAIR algorithm finds a closest pair
of points: (3)
(3,2),(2,1),(2,3),(1,2),(3,1),(2,2),(1,3),(3,−1),(5,−2)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

 

GCC 4.8.2] on linux Cormparing (3, 2) & (2. 1) Distance: 2 Updating rinimum distance as it is better. Conparing (3, 2) & (2,

Flow:
We keep a mindistance variable to hold the result for the minimum distance found till now. We start making pairs of each point and then find the distance between them. If the distance between them is lower than the minDistance found till now, then they become our result set.. Using this procedure, we keep improving our result set.

Flow output:
Comparing (3, 2) & (2, 1) Distance: 2
Updating minimum distance as it is better.
Comparing (3, 2) & (2, 3) Distance: 2
Comparing (3, 2) & (1, 2) Distance: 4
Comparing (3, 2) & (3, 1) Distance: 1
Updating minimum distance as it is better.
Comparing (3, 2) & (2, 2) Distance: 1
Comparing (3, 2) & (1, 3) Distance: 5
Comparing (3, 2) & (3, -1) Distance: 9
Comparing (3, 2) & (5, -2) Distance: 20
Comparing (2, 1) & (2, 3) Distance: 4
Comparing (2, 1) & (1, 2) Distance: 2
Comparing (2, 1) & (3, 1) Distance: 1
Comparing (2, 1) & (2, 2) Distance: 1
Comparing (2, 1) & (1, 3) Distance: 5
Comparing (2, 1) & (3, -1) Distance: 5
Comparing (2, 1) & (5, -2) Distance: 18
Comparing (2, 3) & (1, 2) Distance: 2
Comparing (2, 3) & (3, 1) Distance: 5
Comparing (2, 3) & (2, 2) Distance: 1
Comparing (2, 3) & (1, 3) Distance: 1
Comparing (2, 3) & (3, -1) Distance: 17
Comparing (2, 3) & (5, -2) Distance: 34
Comparing (1, 2) & (3, 1) Distance: 5
Comparing (1, 2) & (2, 2) Distance: 1
Comparing (1, 2) & (1, 3) Distance: 1
Comparing (1, 2) & (3, -1) Distance: 13
Comparing (1, 2) & (5, -2) Distance: 32
Comparing (3, 1) & (2, 2) Distance: 2
Comparing (3, 1) & (1, 3) Distance: 8
Comparing (3, 1) & (3, -1) Distance: 4
Comparing (3, 1) & (5, -2) Distance: 13
Comparing (2, 2) & (1, 3) Distance: 2
Comparing (2, 2) & (3, -1) Distance: 10
Comparing (2, 2) & (5, -2) Distance: 25
Comparing (1, 3) & (3, -1) Distance: 20
Comparing (1, 3) & (5, -2) Distance: 41
Comparing (3, -1) & (5, -2) Distance: 5
Result: Closest pair: (3, 2) (3, 1)

I have written a python code, just in case you want it:

# though the distance is the square root of the difference
# but because we just want to compare and find closest pair,
# it is fine and simple
def pairDistance(p1, p2):
        return (p1[0] - p2[0]) ** 2 +  (p1[1] - p2[1]) ** 2


pairs = [(3,2),(2,1),(2,3),(1,2),(3,1),(2,2),(1,3),(3,-1), (5,-2)]

minDistance = None
index1 = None
index2 = None

for i in range(len(pairs)):
        for j in range(i + 1, len(pairs)):

                # compare point i and j.
                dist = pairDistance(pairs[i], pairs[j])
                
                print('Comparing', pairs[i], '&', pairs[j], 'Distance:', dist)

                if minDistance is None or minDistance > dist:
                
                        print('Updating minimum distance as it is better.')
                        minDistance = dist
                        index1, index2 = i, j

print('Result: Closest pair: ', pairs[index1], pairs[index2])
Add a comment
Know the answer?
Add Answer to:
For the following set of points, describe how the CLOSEST-PAIR algorithm finds a closest pair of points: (3) (3,2),(2,1)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT