Permalink
Browse files

a few aesthetic changes

  • Loading branch information...
1 parent 2ff4385 commit 55c45095c0abd8629c6e22c5a893151db2d06615 Marcos Sainz committed Jun 13, 2011
Showing with 12 additions and 12 deletions.
  1. +7 −7 kdtree.rb
  2. +1 −1 kdtree_benchmark.rb
  3. +4 −4 kdtree_spec.rb
View
@@ -15,8 +15,8 @@ def initialize(points, depth = 0)
@k = points.first.length # we assume all points have same dimension k
@root = build(points, depth)
end
- def nearest_neighbors(point, nnearest = 1)
- search_nearest_neighbors(@root, point, nil, nnearest)
+ def nnearest(point, nnearest = 1)
+ nnearest!(@root, point, nil, nnearest)
end
private
def build(points, depth = 0)
@@ -29,19 +29,19 @@ def build(points, depth = 0)
build(points[pivot+1..-1], depth + 1))
end
- # TODO: provide the k-Nearest Neighbours to a point by maintaining k current bests instead of just one.
- # Branches are only eliminated when they can't have points closer than any of the k current bests.
- def search_nearest_neighbors(here, point, best, nnearest = 1)
+ # TODO: provide the N-Nearest Neighbours to a point by maintaining N current bests instead of just one.
+ # Branches are only eliminated when they can't have points closer than any of the N current bests.
+ def nnearest!(here, point, best, nnearest = 1)
return best if here.nil?
# if the current node is better than the current best, then it becomes the current best
best = here if best.nil? || distance(here.location, point) < distance(best.location, point)
# determine which branch contains the point along the split dimension
nearer, farther = point[here.split] <= here.location[here.split] ?
[here.left, here.right] : [here.right, here.left]
# search the nearer branch
- best = search_nearest_neighbors(nearer, point, best)
+ best = nnearest!(nearer, point, best)
# search the farther branch if the distance to the hyperplane is less than the best so far
- best = search_nearest_neighbors(farther, point, best) if
+ best = nnearest!(farther, point, best) if
distance([here.location[here.split]], [point[here.split]]) <= distance(best.location, point)
# else no need to search the entire farther branch i.e. prune!
best
View
@@ -24,7 +24,7 @@ def euclidean_distance m, n
x.report LABELS[1] do
kdtree = KDTree.new(@@neighbors)
@@newcomers.each do |newcomer|
- @@result2 << kdtree.nearest_neighbors(newcomer).location.inspect
+ @@result2 << kdtree.nnearest(newcomer).location.inspect
end
end
end
View
@@ -37,16 +37,16 @@
@kdtree.to_a.should eql [[2, 3], [5, 4], [4, 7], [7, 2], [8, 1], [9, 6]]
end
end
- describe '#nearest_neighbors' do
+ describe '#nnearest' do
it 'correctly finds the nearest neighbor' do
newcomers = [[0,0], [7,2], [(4+9)/2.0,(6+7)/2.0]] # the last newcomer is midway between 2 nearest neighbors
- newcomers.map {|newcomer| @kdtree.nearest_neighbors(newcomer).location }.should eql [[2,3], [7,2], [4,7]]
+ newcomers.map {|newcomer| @kdtree.nnearest(newcomer).location }.should eql [[2,3], [7,2], [4,7]]
neighbors = [[6, 33], [9, 37], [12, 10], [15, 14], [20, 31], [22, 1], [25, 96], [31, 41], [31, 65], [36, 30],
[46, 36], [56, 51], [57, 80], [62, 55], [78, 12], [81, 97], [86, 79], [91, 3]]
kdtree = KDTree.new neighbors
- #puts ''; kdtree.print; puts ''
- kdtree.nearest_neighbors([10, 34]).location.should eql [9, 37]
+ puts ''; kdtree.print; puts ''
+ kdtree.nnearest([10, 34]).location.should eql [9, 37]
end
end
context 'private methods' do

0 comments on commit 55c4509

Please sign in to comment.