-
-
Notifications
You must be signed in to change notification settings - Fork 44.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
closest pair of points algo #943
Conversation
Edge case: closest points lie on different sides of partition | ||
This case handled by forming a strip of points | ||
which are at a distance (< closest_pair_dis) from mid-point. | ||
(It is a proven that strip contains at most 6 points) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's wrong. Please review the theoretical part of this part more.
|
||
|
||
def euclidean_distance(point1, point2): | ||
return math.sqrt(pow(point1[0] - point2[0], 2) + pow(point1[1] - point2[1], 2)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's OK but actually sqrt is unnecessary. For the optimization, you can use sqrt only once after all calculations have been done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok.
cross_strip.append(point) | ||
|
||
cross_strip = column_based_sort(cross_strip, 1) | ||
closest_in_strip = dis_btw_closest_pair(cross_strip, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use brute force for the points inside the strip. You have to calculate this part in O(n).
10ec5b4
to
3bf3564
Compare
The codes are now fine. I'm going to merge it but consider that we should reduce the closest pair of points time complexity to O(n log n), in future (now it's O(n * log n * log n)). |
Ok. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The algorithm works correctly.
return min_dis | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the new line.
|
||
points = [[2, 3], [12, 30], [40, 50], [5, 1], [12, 10]] | ||
points = ((2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (0, 2), (5, 6), (1, 2)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use a tuple of tuples. Use a list of tuples: [(1, 2), (3, 4), (5, 6)]
""" divide and conquer approach | ||
|
||
Parameters : | ||
points, points_count (tuple(tuple(int, int)), int) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use a tuple of tuples. Use a list of tuples.
""" closest pair of points in strip | ||
|
||
Parameters : | ||
points, points_count, min_dis (tuple(tuple(int, int)), int, int) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use a tuple of tuples. Use a list of tuples.
if current_dis < min_dis: | ||
min_dis = current_dis | ||
return min_dis | ||
|
||
|
||
#divide and conquer approach | ||
def closest_pair_of_points(points, no_of_points): | ||
def dis_btw_closest_in_strip(points, points_counts, min_dis = float("inf")): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not important but if you have a better name for this function, please update it. Long function names are also fine. Using "between" is better than "btw". Also you can use "brute force" word in its name.
These were only some suggestions. It's up to you.
Time complexity: O(n * (logn) ^ 2) | ||
min(closest_pair_dis, closest_in_strip) would be the final answer. | ||
|
||
Time complexity: O(n * (logn)^2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want you can put a reference to it. Are there any references in CLRS book?
added closest pair of points algorithm under divide and conquer.
Please review.
;)