Skip to content
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

Merged
merged 3 commits into from
Jul 4, 2019
Merged

Conversation

Dharni0607
Copy link
Contributor

added closest pair of points algorithm under divide and conquer.
Please review.
;)

@Dharni0607 Dharni0607 changed the title Issue#817 closest pair of points algo Jul 3, 2019
@Erfaniaa Erfaniaa self-assigned this Jul 3, 2019
@Erfaniaa Erfaniaa self-requested a review July 3, 2019 09:28
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)
Copy link
Member

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))
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 OK but actually sqrt is unnecessary. For the optimization, you can use sqrt only once after all calculations have been done.

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.

cross_strip.append(point)

cross_strip = column_based_sort(cross_strip, 1)
closest_in_strip = dis_btw_closest_pair(cross_strip,
Copy link
Member

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

@Dharni0607 Dharni0607 force-pushed the issue#817 branch 2 times, most recently from 10ec5b4 to 3bf3564 Compare July 4, 2019 06:23
@Erfaniaa
Copy link
Member

Erfaniaa commented Jul 4, 2019

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

@Dharni0607
Copy link
Contributor Author

Ok.

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.

The algorithm works correctly.

return min_dis



Copy link
Member

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

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

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

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

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?

@Erfaniaa Erfaniaa merged commit 035457f into TheAlgorithms:master Jul 4, 2019
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.

None yet

2 participants