Skip to content

Commit a796ccf

Browse files
ulwlugithub-actions
ulwlu
and
github-actions
authored
Add graham scan algorithm (#4205)
* 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>
1 parent 0435128 commit a796ccf

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

Diff for: DIRECTORY.md

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
* [Binary Count Trailing Zeros](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_count_trailing_zeros.py)
3131
* [Binary Or Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_or_operator.py)
3232
* [Binary Xor Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_xor_operator.py)
33+
* [Count Number Of One Bits](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/count_number_of_one_bits.py)
34+
* [Reverse Bits](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/reverse_bits.py)
3335
* [Single Bit Manipulation Operations](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/single_bit_manipulation_operations.py)
3436

3537
## Blockchain
@@ -122,6 +124,7 @@
122124
* [Fenwick Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/fenwick_tree.py)
123125
* [Lazy Segment Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/lazy_segment_tree.py)
124126
* [Lowest Common Ancestor](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/lowest_common_ancestor.py)
127+
* [Merge Two Binary Trees](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/merge_two_binary_trees.py)
125128
* [Non Recursive Segment Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/non_recursive_segment_tree.py)
126129
* [Number Of Possible Binary Trees](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/number_of_possible_binary_trees.py)
127130
* [Red Black Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/red_black_tree.py)
@@ -274,6 +277,7 @@
274277

275278
## Graphics
276279
* [Bezier Curve](https://github.com/TheAlgorithms/Python/blob/master/graphics/bezier_curve.py)
280+
* [Koch Snowflake](https://github.com/TheAlgorithms/Python/blob/master/graphics/koch_snowflake.py)
277281
* [Vector3 For 2D Rendering](https://github.com/TheAlgorithms/Python/blob/master/graphics/vector3_for_2d_rendering.py)
278282

279283
## Graphs
@@ -520,6 +524,7 @@
520524
* [Frequency Finder](https://github.com/TheAlgorithms/Python/blob/master/other/frequency_finder.py)
521525
* [Game Of Life](https://github.com/TheAlgorithms/Python/blob/master/other/game_of_life.py)
522526
* [Gauss Easter](https://github.com/TheAlgorithms/Python/blob/master/other/gauss_easter.py)
527+
* [Graham Scan](https://github.com/TheAlgorithms/Python/blob/master/other/graham_scan.py)
523528
* [Greedy](https://github.com/TheAlgorithms/Python/blob/master/other/greedy.py)
524529
* [Integeration By Simpson Approx](https://github.com/TheAlgorithms/Python/blob/master/other/integeration_by_simpson_approx.py)
525530
* [Largest Subarray Sum](https://github.com/TheAlgorithms/Python/blob/master/other/largest_subarray_sum.py)
@@ -840,6 +845,7 @@
840845
* [Merge Insertion Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_insertion_sort.py)
841846
* [Merge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_sort.py)
842847
* [Natural Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/natural_sort.py)
848+
* [Odd Even Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_sort.py)
843849
* [Odd Even Transposition Parallel](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_parallel.py)
844850
* [Odd Even Transposition Single Threaded](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_single_threaded.py)
845851
* [Pancake Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/pancake_sort.py)
@@ -856,6 +862,7 @@
856862
* [Recursive Quick Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/recursive_quick_sort.py)
857863
* [Selection Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/selection_sort.py)
858864
* [Shell Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/shell_sort.py)
865+
* [Slowsort](https://github.com/TheAlgorithms/Python/blob/master/sorts/slowsort.py)
859866
* [Stooge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/stooge_sort.py)
860867
* [Strand Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/strand_sort.py)
861868
* [Tim Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/tim_sort.py)

Diff for: other/graham_scan.py

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
"""
2+
This is a pure Python implementation of the merge-insertion sort algorithm
3+
Source: https://en.wikipedia.org/wiki/Graham_scan
4+
5+
For doctests run following command:
6+
python3 -m doctest -v graham_scan.py
7+
"""
8+
9+
from __future__ import annotations
10+
11+
from collections import deque
12+
from enum import Enum
13+
from math import atan2, degrees
14+
from sys import maxsize
15+
16+
17+
def graham_scan(points: list[list[int, int]]) -> list[list[int, int]]:
18+
"""Pure implementation of graham scan algorithm in Python
19+
20+
:param points: The unique points on coordinates.
21+
:return: The points on convex hell.
22+
23+
Examples:
24+
>>> graham_scan([(9, 6), (3, 1), (0, 0), (5, 5), (5, 2), (7, 0), (3, 3), (1, 4)])
25+
[(0, 0), (7, 0), (9, 6), (5, 5), (1, 4)]
26+
27+
>>> graham_scan([(0, 0), (1, 0), (1, 1), (0, 1)])
28+
[(0, 0), (1, 0), (1, 1), (0, 1)]
29+
30+
>>> graham_scan([(0, 0), (1, 1), (2, 2), (3, 3), (-1, 2)])
31+
[(0, 0), (1, 1), (2, 2), (3, 3), (-1, 2)]
32+
33+
>>> graham_scan([(-100, 20), (99, 3), (1, 10000001), (5133186, -25), (-66, -4)])
34+
[(5133186, -25), (1, 10000001), (-100, 20), (-66, -4)]
35+
"""
36+
37+
if len(points) <= 2:
38+
# There is no convex hull
39+
raise ValueError("graham_scan: argument must contain more than 3 points.")
40+
if len(points) == 3:
41+
return points
42+
# find the lowest and the most left point
43+
minidx = 0
44+
miny, minx = maxsize, maxsize
45+
for i, point in enumerate(points):
46+
x = point[0]
47+
y = point[1]
48+
if y < miny:
49+
miny = y
50+
minx = x
51+
minidx = i
52+
if y == miny:
53+
if x < minx:
54+
minx = x
55+
minidx = i
56+
57+
# remove the lowest and the most left point from points for preparing for sort
58+
points.pop(minidx)
59+
60+
def angle_comparer(point: list[int, int], minx: int, miny: int) -> float:
61+
"""Return the angle toward to point from (minx, miny)
62+
63+
:param point: The target point
64+
minx: The starting point's x
65+
miny: The starting point's y
66+
:return: the angle
67+
68+
Examples:
69+
>>> angle_comparer([1,1], 0, 0)
70+
45.0
71+
72+
>>> angle_comparer([100,1], 10, 10)
73+
-5.710593137499642
74+
75+
>>> angle_comparer([5,5], 2, 3)
76+
33.690067525979785
77+
"""
78+
# sort the points accorgind to the angle from the lowest and the most left point
79+
x = point[0]
80+
y = point[1]
81+
angle = degrees(atan2(y - miny, x - minx))
82+
return angle
83+
84+
sorted_points = sorted(points, key=lambda point: angle_comparer(point, minx, miny))
85+
# This insert actually costs complexity,
86+
# and you should insteadly add (minx, miny) into stack later.
87+
# I'm using insert just for easy understanding.
88+
sorted_points.insert(0, (minx, miny))
89+
90+
# traversal from the lowest and the most left point in anti-clockwise direction
91+
# if direction gets right, the previous point is not the convex hull.
92+
class Direction(Enum):
93+
left = 1
94+
straight = 2
95+
right = 3
96+
97+
def check_direction(
98+
starting: list[int, int], via: list[int, int], target: list[int, int]
99+
) -> Direction:
100+
"""Return the direction toward to the line from via to target from starting
101+
102+
:param starting: The starting point
103+
via: The via point
104+
target: The target point
105+
:return: the Direction
106+
107+
Examples:
108+
>>> check_direction([1,1], [2,2], [3,3])
109+
Direction.straight
110+
111+
>>> check_direction([60,1], [-50,199], [30,2])
112+
Direction.left
113+
114+
>>> check_direction([0,0], [5,5], [10,0])
115+
Direction.right
116+
"""
117+
x0, y0 = starting
118+
x1, y1 = via
119+
x2, y2 = target
120+
via_angle = degrees(atan2(y1 - y0, x1 - x0))
121+
if via_angle < 0:
122+
via_angle += 360
123+
target_angle = degrees(atan2(y2 - y0, x2 - x0))
124+
if target_angle < 0:
125+
target_angle += 360
126+
# t-
127+
# \ \
128+
# \ v
129+
# \|
130+
# s
131+
# via_angle is always lower than target_angle, if direction is left.
132+
# If they are same, it means they are on a same line of convex hull.
133+
if target_angle > via_angle:
134+
return Direction.left
135+
if target_angle == via_angle:
136+
return Direction.straight
137+
if target_angle < via_angle:
138+
return Direction.right
139+
140+
stack = deque()
141+
stack.append(sorted_points[0])
142+
stack.append(sorted_points[1])
143+
stack.append(sorted_points[2])
144+
# In any ways, the first 3 points line are towards left.
145+
# Because we sort them the angle from minx, miny.
146+
current_direction = Direction.left
147+
148+
for i in range(3, len(sorted_points)):
149+
while True:
150+
starting = stack[-2]
151+
via = stack[-1]
152+
target = sorted_points[i]
153+
next_direction = check_direction(starting, via, target)
154+
155+
if next_direction == Direction.left:
156+
current_direction = Direction.left
157+
break
158+
if next_direction == Direction.straight:
159+
if current_direction == Direction.left:
160+
# We keep current_direction as left.
161+
# Because if the straight line keeps as straight,
162+
# we want to know if this straight line is towards left.
163+
break
164+
elif current_direction == Direction.right:
165+
# If the straight line is towards right,
166+
# every previous points on those straigh line is not convex hull.
167+
stack.pop()
168+
if next_direction == Direction.right:
169+
stack.pop()
170+
stack.append(sorted_points[i])
171+
return list(stack)

0 commit comments

Comments
 (0)