Skip to content

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

Merged
merged 5 commits into from
Jul 9, 2019

Conversation

Dharni0607
Copy link
Contributor

n*logn algorithm for closest pair of points.



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)]
Copy link
Member

@cclauss cclauss Jul 9, 2019

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok.

@Erfaniaa Erfaniaa self-assigned this Jul 9, 2019
& 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)
Copy link
Member

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)
Copy link
Member

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.
Copy link
Member

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)
Copy link
Member

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.
Copy link
Member

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".

Copy link
Member

@Erfaniaa Erfaniaa left a 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)
Copy link
Member

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)
Copy link
Member

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):
Copy link
Member

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,
Copy link
Member

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.
Copy link
Member

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.

Copy link
Member

@Erfaniaa Erfaniaa left a 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.

@Erfaniaa Erfaniaa merged commit c85312d into TheAlgorithms:master Jul 9, 2019
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* updated closest pair of points (n*(logn)^2) to (n*logn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants