Skip to content

fix: stop GrahamScan dropping hull vertices on horizontal collinear runs - #7785

Merged
DenizAltunkapan merged 5 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-graham-scan-collinear
Nov 29, 2026
Merged

DenizAltunkapan merged 5 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-graham-scan-collinear

Conversation

@Aditya2375

Copy link
Copy Markdown
Contributor

Describe your change:

Fixed GrahamScan dropping hull vertices when several input points are collinear with the anchor point on a horizontal ray.

The constructor sorts the points by polar angle around the lowest point and then assumes the last point of the first collinear run (points[firstNonCollinearIndex - 1]) is the farthest one from the anchor - only that point can be a hull vertex, the rest are interior to the edge. That assumption holds for the reference implementation (Princeton algs4), whose PolarOrder compares horizontal collinear ties as equal so the stable sort keeps them in ascending distance order. Here PolarOrder breaks horizontal ties by descending distance (Integer.compare(dx2, dx1), pinned by PointTest), so the point at firstNonCollinearIndex - 1 is the nearest point of the run and the actual hull vertex is never looked at again.

Examples with the current implementation:

new GrahamScan(new Point[] {(1,3), (6,3), (8,3), (5,3)}).hull()
// returns [(1,3), (5,3)] - (8,3) is a hull vertex but missing, (5,3) is interior

new GrahamScan(new Point[] {(3,3),(11,3),(5,3),(0,3),(2,3),(2,3),(4,3),(0,3),(5,3)}).hull()
// returns [(0,3), (0,3)] - the anchor point duplicated, (11,3) missing

The fix selects the farthest point of the collinear run explicitly (by squared distance) instead of relying on the sort order, so it is correct regardless of how PolarOrder breaks ties. No changes to Point/PolarOrder were needed and all existing tests pass unmodified.

I verified the fix against an independent monotone-chain oracle on 5000 randomized point sets (including horizontal/diagonal collinear and duplicate-heavy inputs): all hulls match. Added regression tests for the cases above; they fail on the current code and pass with the fix.

  • 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 Java files are placed inside an existing directory.
  • All filenames are in all uppercase characters with no spaces or dashes.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@codecov-commenter

codecov-commenter commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.52%. Comparing base (a7b6674) to head (a9a39d0).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7785      +/-   ##
============================================
+ Coverage     82.50%   82.52%   +0.01%     
- Complexity     8550     8556       +6     
============================================
  Files           847      847              
  Lines         26349    26356       +7     
  Branches       5150     5152       +2     
============================================
+ Hits          21740    21749       +9     
  Misses         3823     3823              
+ Partials        786      784       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Aditya2375
Aditya2375 marked this pull request as ready for review November 25, 2026 06:54
@DenizAltunkapan
DenizAltunkapan enabled auto-merge (squash) November 29, 2026 16:39
@DenizAltunkapan
DenizAltunkapan merged commit b3f2381 into TheAlgorithms:master Nov 29, 2026
13 checks passed
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