-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Implemented KD Tree Data Structure #11532
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
cclauss
merged 32 commits into
TheAlgorithms:master
from
Ramy-Badr-Ahmed:feature/kd-tree-implementation
Sep 3, 2024
Merged
Changes from 7 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
0d6985c
Implemented KD-Tree Data Structure
Ramy-Badr-Ahmed 6665d23
Implemented KD-Tree Data Structure. updated DIRECTORY.md.
Ramy-Badr-Ahmed 6b3d47e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4203cda
Create __init__.py
Ramy-Badr-Ahmed 3222bd3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a41ae5b
Replaced legacy `np.random.rand` call with `np.random.Generator` in k…
Ramy-Badr-Ahmed 1668d73
Replaced legacy `np.random.rand` call with `np.random.Generator` in k…
Ramy-Badr-Ahmed 81d6917
added typehints and docstrings
Ramy-Badr-Ahmed 6cddcbd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8b238d1
docstring for search()
Ramy-Badr-Ahmed cd1dd9f
Merge remote-tracking branch 'origin/feature/kd-tree-implementation' …
Ramy-Badr-Ahmed ead2838
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 543584c
Added tests. Updated docstrings/typehints
Ramy-Badr-Ahmed ad31f83
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1322921
updated tests and used | for type annotations
Ramy-Badr-Ahmed 4608a9f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7c1aa7e
E501 for build_kdtree.py, hypercube_points.py, nearest_neighbour_sear…
Ramy-Badr-Ahmed ba24e75
I001 for example_usage.py and test_kdtree.py
Ramy-Badr-Ahmed 05975a3
I001 for example_usage.py and test_kdtree.py
Ramy-Badr-Ahmed 31782d1
Update data_structures/kd_tree/build_kdtree.py
Ramy-Badr-Ahmed 6a9b3e1
Update data_structures/kd_tree/example/hypercube_points.py
Ramy-Badr-Ahmed 2fd24d4
Update data_structures/kd_tree/example/hypercube_points.py
Ramy-Badr-Ahmed 2cf9d92
Added new test cases requested in Review. Refactored the test_build_k…
Ramy-Badr-Ahmed a3803ee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f1f5862
Considered ruff errors
Ramy-Badr-Ahmed ec6559d
Merge remote-tracking branch 'origin/feature/kd-tree-implementation' …
Ramy-Badr-Ahmed 5c07a1a
Considered ruff errors
Ramy-Badr-Ahmed 3c09ac1
Apply suggestions from code review
cclauss bab43e7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a10ff15
Update kd_node.py
cclauss d77a285
imported annotations from __future__
Ramy-Badr-Ahmed 0426806
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from .kd_node import KDNode | ||
|
||
|
||
def build_kdtree(points, depth=0): | ||
if not points: | ||
return None | ||
|
||
k = len(points[0]) # dimensionality of the points | ||
axis = depth % k | ||
|
||
# Sort point list and choose median as pivot element | ||
points.sort(key=lambda x: x[axis]) | ||
Ramy-Badr-Ahmed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
median_idx = len(points) // 2 | ||
|
||
# Create node and construct subtrees | ||
return KDNode( | ||
point=points[median_idx], | ||
left=build_kdtree(points[:median_idx], depth + 1), | ||
right=build_kdtree(points[median_idx + 1 :], depth + 1), | ||
) |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import numpy as np | ||
|
||
from hypercube_points import hypercube_points | ||
from data_structures.kd_tree.build_kdtree import build_kdtree | ||
from data_structures.kd_tree.nearest_neighbour_search import nearest_neighbour_search | ||
|
||
|
||
num_points = 5000 | ||
cube_size = 10 | ||
num_dimensions = 10 | ||
|
||
points = hypercube_points(num_points, cube_size, num_dimensions) | ||
hypercube_kdtree = build_kdtree(points.tolist()) | ||
|
||
rng = np.random.default_rng() | ||
query_point = rng.random(num_dimensions).tolist() | ||
|
||
nearest_point, nearest_dist, nodes_visited = nearest_neighbour_search( | ||
hypercube_kdtree, query_point | ||
) | ||
|
||
print(f"Query point: {query_point}") | ||
print(f"Nearest point: {nearest_point}") | ||
print(f"Distance: {nearest_dist:.4f}") | ||
print(f"Nodes visited: {nodes_visited}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import numpy as np | ||
|
||
|
||
def hypercube_points(num_points, hypercube_size, num_dimensions): | ||
Ramy-Badr-Ahmed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rng = np.random.default_rng() | ||
return hypercube_size * rng.random((num_points, num_dimensions)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class KDNode: | ||
def __init__(self, point, left=None, right=None): | ||
Ramy-Badr-Ahmed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.point = point | ||
self.left = left | ||
self.right = right |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
def nearest_neighbour_search(root, query_point): | ||
Ramy-Badr-Ahmed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
nearest_point = None | ||
nearest_dist = float("inf") | ||
nodes_visited = 0 | ||
|
||
def search(node, depth=0): | ||
Ramy-Badr-Ahmed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
nonlocal nearest_point, nearest_dist, nodes_visited | ||
if node is None: | ||
return | ||
|
||
nodes_visited += 1 | ||
|
||
# Calculate the current distance (squared distance) | ||
current_point = node.point | ||
current_dist = sum((qp - cp) ** 2 for qp, cp in zip(query_point, current_point)) | ||
|
||
# Update nearest point if the current node is closer | ||
if nearest_point is None or current_dist < nearest_dist: | ||
nearest_point = current_point | ||
nearest_dist = current_dist | ||
|
||
# Determine which subtree to search first (based on axis and query point) | ||
k = len(query_point) # dimensionality of points | ||
axis = depth % k | ||
|
||
if query_point[axis] <= current_point[axis]: | ||
nearer_subtree = node.left | ||
further_subtree = node.right | ||
else: | ||
nearer_subtree = node.right | ||
further_subtree = node.left | ||
|
||
# Search the nearer subtree first | ||
search(nearer_subtree, depth + 1) | ||
|
||
# If the further subtree has a closer point | ||
if (query_point[axis] - current_point[axis]) ** 2 < nearest_dist: | ||
search(further_subtree, depth + 1) | ||
|
||
search(root, 0) | ||
return nearest_point, nearest_dist, nodes_visited |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.