Skip to content

Add graham scan algorithm #4205

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 7 commits into from Mar 2, 2021
Merged

Add graham scan algorithm #4205

merged 7 commits into from Mar 2, 2021

Conversation

ghost
Copy link

@ghost ghost commented Feb 13, 2021

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@ghost ghost added require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required awaiting reviews This PR is ready to be reviewed labels Feb 13, 2021
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

# remove the lowest and the most left point from points for preparing for sort
points.pop(minidx)

def angle_comparer(p: list[int, int]) -> float:
Copy link

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file other/graham_scan.py, please provide doctest for the function angle_comparer

Please provide descriptive name for the parameter: p

Copy link
Author

Choose a reason for hiding this comment

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

To reviewers:
This is an inner function.

argument name is fixed here fdaacde

angle = degrees(atan2(y - miny, x - minx))
return angle

sorted_points = sorted(points, key=lambda p: angle_comparer(p))
Copy link

Choose a reason for hiding this comment

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

Please provide descriptive name for the parameter: p

Copy link
Author

Choose a reason for hiding this comment

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

argument name is fixed here fdaacde

straight = 2
right = 3

def check_direction(
Copy link

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file other/graham_scan.py, please provide doctest for the function check_direction

Copy link
Author

Choose a reason for hiding this comment

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

To reviewers:
This is an inner function

@ghost ghost added the tests are failing Do not merge until tests pass label Feb 13, 2021
@ghost ghost removed the require descriptive names This PR needs descriptive function and/or variable names label Feb 13, 2021
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

# remove the lowest and the most left point from points for preparing for sort
points.pop(minidx)

def angle_comparer(point: list[int, int]) -> float:
Copy link

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file other/graham_scan.py, please provide doctest for the function angle_comparer

straight = 2
right = 3

def check_direction(
Copy link

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file other/graham_scan.py, please provide doctest for the function check_direction

@ghost
Copy link
Author

ghost commented Feb 13, 2021

To reviewers:

The test is failed at pre-commit because CI executes black for whole files in this repository, and it changes 814 files.

I did black with its newest version, and so CI does. It should be updated, or ignore this fail.

@ghost ghost removed the require tests Tests [doctest/unittest/pytest] are required label Feb 22, 2021
@ghost ghost removed the tests are failing Do not merge until tests pass label Mar 2, 2021
@cclauss
Copy link
Member

cclauss commented Mar 2, 2021

@ghost
Copy link
Author

ghost commented Mar 2, 2021

@cclauss
Hi. I'm not sure if this algorithm should belong to sorts, because this aims to find the convex hull of a finite set of points. It's kind of coordinate things rather than sorting.

@cclauss
Copy link
Member

cclauss commented Mar 2, 2021

Putting algorithms in other makes them very hard for visitors to find. Is there another category that makes sense?

@ghost ghost closed this Mar 2, 2021
@ghost ghost reopened this Mar 2, 2021
@cclauss cclauss closed this Mar 2, 2021
@cclauss cclauss reopened this Mar 2, 2021
@ghost ghost removed the awaiting reviews This PR is ready to be reviewed label Mar 2, 2021
@cclauss cclauss merged commit a796ccf into TheAlgorithms:master Mar 2, 2021
@ghost
Copy link
Author

ghost commented Mar 2, 2021

@cclauss

I see. searches must be fitting. I will move this.

How does this algorithm compare with master/divide_and_conquer/convex_hull.py

divide_and_conquer algorithm is also known well to solve convex hull problem only by O(n * logn) time complexity. However, it's approach is totally different from graham scan.

Graham scan doesn't use divide_and_conquer approach, but it focuses on sorting points by angle.
We'll look for all points at same time and trace the periphery with checking if the angle is towards to inner or outer.

@ghost
Copy link
Author

ghost commented Mar 2, 2021

Oh. Thank you for merging.

I wonder if I should create other PR to move this to searches, or shouldn't....

@cclauss
Copy link
Member

cclauss commented Mar 2, 2021

https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/convex_hull.py has three different convex_hull algorithms and your file has a fourth. What would be really cool would be to benchmark the four algorithms on one or more common datasets to see the relative performance.

peRFectBeliever pushed a commit to peRFectBeliever/Python that referenced this pull request Apr 1, 2021
* Add graham scan algorithm

* Fix argument name p with point

* Add tests in inner function

* updating DIRECTORY.md

* Fix graham scan for isort --profile=black

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Panquesito7 pushed a commit to Panquesito7/Python that referenced this pull request May 13, 2021
* Add graham scan algorithm

* Fix argument name p with point

* Add tests in inner function

* updating DIRECTORY.md

* Fix graham scan for isort --profile=black

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
shermanhui pushed a commit to shermanhui/Python that referenced this pull request Oct 22, 2021
* Add graham scan algorithm

* Fix argument name p with point

* Add tests in inner function

* updating DIRECTORY.md

* Fix graham scan for isort --profile=black

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
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.

1 participant