Nearest Neighbor
NearestNeighbor
Brute forcing the nearest neighbor problem.
Author: Ignatios Drakoulas
License: MIT
-
NearestNeighbor
- ~Point
-
~sortBruteForce(neighbors, [distanceFn]) ⇒
Array.<Point>
|Array.<any>
-
~getNearestIndex(home, neighbors, [distanceFn]) ⇒
number
-
~getNearest(home, neighbors, [distanceFn]) ⇒
Point
|any
NearestNeighbor~Point
Kind: inner class of NearestNeighbor
new Point(x, y)
Represents a point in two dimensions.
Param | Type | Description |
---|---|---|
x | Number |
abscissa |
y | Number |
ordinate |
Array.<Point>
| Array.<any>
NearestNeighbor~sortBruteForce(neighbors, [distanceFn]) ⇒ Sorts all the neighbors by their respective distance starting from the first
Kind: inner method of NearestNeighbor
Param | Type | Description |
---|---|---|
neighbors |
Array.<Point> | Array.<any>
|
The array of Point elements |
[distanceFn] | function |
Optional, distSquared will be used if none provided |
Example
var nn = require("NearestNeighbor.js");
var neighbors = [
{x: 5, y: 5},
{x: 3, y: 3},
{x: 4, y: 4},
{x: 1, y: 1},
{x: 2, y: 2},
//other points
];
var ordered_neighbors = nn.sortBruteForce(neighbors);
// ordered_neighbors now contain the neighbors, in the order they ought to be visited.
number
NearestNeighbor~getNearestIndex(home, neighbors, [distanceFn]) ⇒ Returns the nearest, relative to the provided home point, neighbor's index from the array of neighbors
Kind: inner method of NearestNeighbor
Param | Type | Description |
---|---|---|
home |
Point | any
|
The reference Point |
neighbors |
Array.<Point> | Array.<any>
|
The array of Point elements |
[distanceFn] | function |
Optional, distSquared will be used if none provided |
Example
var nn = require("NearestNeighbor.js");
var home = {x: 6, y: 6};
var neighbors = [
{x: 4, y: 4},
{x: 3, y: 3},
{x: 5, y: 5},
{x: 1, y: 1},
{x: 2, y: 2},
//other points
];
var index = nn.getNearestIndex(home, neighbors);
// index will equal 2, because {x: 5, y: 5} is
// the nearest neighbor for {x: 6, y: 6} which is the home point
Point
| any
NearestNeighbor~getNearest(home, neighbors, [distanceFn]) ⇒ Returns the nearest neighbor, relative to the provided home point, from the given array of neighbors
Kind: inner method of NearestNeighbor
Param | Type | Description |
---|---|---|
home |
Point | any
|
The reference Point |
neighbors |
Array.<Point> | Array.<any>
|
The array of Point elements |
[distanceFn] | function |
Optional, distSquared will be used if none provided |
Example
var nn = require("NearestNeighbor.js");
var home = {x: 6, y: 6};
var neighbors = [
{x: 5, y: 5},
{x: 3, y: 3},
{x: 4, y: 4},
{x: 1, y: 1},
{x: 2, y: 2},
//other points
];
var neighbor = nn.getNearest(home, neighbors);
// neighbor will equal {x: 5, y: 5}, because {x: 5, y: 5} is
// the nearest neighbor for {x: 6, y: 6} which is the home point
Copyright © 2021 Ignatios Drakoulas