-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
updates in closest pair of points algorithm #979
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
Conversation
|
||
|
||
points = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (0, 2), (5, 6), (1, 2)] | ||
points = column_based_sort(points) | ||
points = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] |
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.
Please consider adding if __name__ == "__main__": before this line and then indent this line and any lines below it. This will allow our automated testing to run doctests without executing your code.
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.
& by applying divide and conquer approach, | ||
minimum distance is obtained recursively. | ||
|
||
>> closest points lie on different sides of partition | ||
This case handled by forming a strip of points | ||
whose Xco-ords distance is less than closest_pair_dis | ||
from mid-point's Xco-ords. | ||
from mid-point's Xco-ords. points sorted based on Yco-ords | ||
are used in this step to reduce sorting time. | ||
Closest pair distance is found in the strip of points. (closest_in_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.
Put the period at the end of the line.
Closest pair distance is found in the strip of points. (closest_in_strip) | ||
|
||
min(closest_pair_dis, closest_in_strip) would be the final answer. | ||
|
||
Time complexity: O(n * (logn)^2) | ||
Time complexity: O(n * logn) |
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.
"log n"
put a space
@@ -1,25 +1,24 @@ | |||
""" | |||
The algorithm finds distance btw closest pair of points in the given n points. | |||
Approach used -> Divide and conquer | |||
The points are sorted based on Xco-ords | |||
The points are sorted based on Xco-ords and | |||
then based on Yco-ords separetely. |
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 a typo: "separately"
""" | ||
|
||
|
||
import math | ||
|
||
|
||
def euclidean_distance_sqr(point1, point2): | ||
return 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.
Use ** 2 instead of pow.
@@ -1,25 +1,24 @@ | |||
""" | |||
The algorithm finds distance btw closest pair of points in the given n 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.
Use "between" instead of "btw".
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.
Use lower case (and snake case) variable names.
closest_in_strip = dis_between_closest_in_strip(cross_strip, | ||
len(cross_strip), closest_pair_dis) | ||
return min(closest_pair_dis, closest_in_strip) | ||
|
||
|
||
def closest_pair_of_points(points, points_counts): | ||
return math.sqrt(closest_pair_of_points_sqr(points, points_counts)) | ||
points_sorted_on_X = column_based_sort(points, column = 0) |
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.
Make it lower case. Use "points_sorted_on_x" and "points_sorted_on_y" with small x and y in all parts of your code.
closest_in_strip = dis_between_closest_in_strip(cross_strip, | ||
len(cross_strip), closest_pair_dis) | ||
return min(closest_pair_dis, closest_in_strip) | ||
|
||
|
||
def closest_pair_of_points(points, points_counts): | ||
return math.sqrt(closest_pair_of_points_sqr(points, points_counts)) | ||
points_sorted_on_X = column_based_sort(points, column = 0) | ||
points_sorted_on_Y = column_based_sort(points, column = 1) |
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.
Make it lower case.
@@ -66,7 +65,7 @@ def dis_between_closest_in_strip(points, points_counts, min_dis = float("inf")): | |||
return min_dis | |||
|
|||
|
|||
def closest_pair_of_points_sqr(points, points_counts): | |||
def closest_pair_of_points_sqr(points_sorted_on_X, points_sorted_on_Y, points_counts): |
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.
Make them lower case.
@@ -1,25 +1,24 @@ | |||
""" | |||
The algorithm finds distance btw closest pair of points in the given n points. | |||
Approach used -> Divide and conquer | |||
The points are sorted based on Xco-ords | |||
The points are sorted based on Xco-ords and | |||
then based on Yco-ords separetely. | |||
& by applying divide and conquer approach, |
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.
I think here "And " is better than "& ".
& by applying divide and conquer approach, | ||
minimum distance is obtained recursively. | ||
|
||
>> closest points lie on different sides of partition | ||
>> closest points can lie on different sides of partition. |
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 better to make the first character of each sentence upper case. Please check other sentences too.
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.
I tested this code, and it's correct.
* updated closest pair of points (n*(logn)^2) to (n*logn)
n*logn algorithm for closest pair of points.