org.openimaj.knn

## Class ShortNearestNeighboursExact

• All Implemented Interfaces:
NearestNeighbours<short[],float[],IntFloatPair>

```public class ShortNearestNeighboursExact
extends ShortNearestNeighbours```
Exact (brute-force) k-nearest-neighbour implementation.
Author:
Jonathon Hare (jsh2@ecs.soton.ac.uk), Sina Samangooei (ss@ecs.soton.ac.uk)
• ### Nested Class Summary

Nested Classes
Modifier and Type Class and Description
`static class ` `ShortNearestNeighboursExact.Factory`
• ### Field Summary

Fields
Modifier and Type Field and Description
`protected ShortFVComparator` `distance`
`protected short[][]` `pnts`
• ### Constructor Summary

Constructors
Constructor and Description
`ShortNearestNeighboursExact(short[][] pnts)`
Construct the ShortNearestNeighboursExact over the provided dataset and using Euclidean distance.
```ShortNearestNeighboursExact(short[][] pnts, ShortFVComparator distance)```
Construct the ShortNearestNeighboursExact over the provided dataset with the given distance function.
• ### Method Summary

All Methods
Modifier and Type Method and Description
`float` ```computeDistance(short[] a, short[] b)```
Compute the distance between two vectors using the underlying distance comparison used by this class.
`ShortFVComparator` `distanceComparator()`
Get the distance comparator
`short[][]` `getPoints()`
Get the underlying data points.
`int` `numDimensions()`
Get the number of dimensions of each vector in the dataset
`void` ```searchKNN(List<short[]> qus, int K, int[][] indices, float[][] distances)```
Search for the K nearest neighbours to each of the N queries, and return the indices of each nearest neighbour and their respective distances.
`void` ```searchKNN(short[][] qus, int K, int[][] indices, float[][] distances)```
Search for the K nearest neighbours to each of the N queries, and return the indices of each nearest neighbour and their respective distances.
`List<IntFloatPair>` ```searchKNN(short[] query, int K)```
Search for the K nearest neighbours to the given query and return an ordered list of pairs containing the distance and index of each neighbour.
`void` ```searchNN(List<short[]> qus, int[] indices, float[] distances)```
Search for the nearest neighbour to each of the N queries, and return the index of each nearest neighbour and the respective distance.
`IntFloatPair` `searchNN(short[] query)`
Search for the nearest neighbour to the given query and return a pair containing the distance and index of that neighbour.
`void` ```searchNN(short[][] qus, int[] indices, float[] distances)```
Search for the nearest neighbour to each of the N queries, and return the index of each nearest neighbour and the respective distance.
`int` `size()`
Get the size of the dataset
• ### Methods inherited from class org.openimaj.knn.ShortNearestNeighbours

`distanceFunc, distanceFunc, distanceFunc, distanceFunc`
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Field Detail

• #### pnts

`protected final short[][] pnts`
• #### distance

`protected final ShortFVComparator distance`
• ### Constructor Detail

• #### ShortNearestNeighboursExact

`public ShortNearestNeighboursExact(short[][] pnts)`
Construct the ShortNearestNeighboursExact over the provided dataset and using Euclidean distance.
Parameters:
`pnts` - the dataset
• #### ShortNearestNeighboursExact

```public ShortNearestNeighboursExact(short[][] pnts,
ShortFVComparator distance)```
Construct the ShortNearestNeighboursExact over the provided dataset with the given distance function.

Note: If the distance function provides similarities rather than distances they are automatically inverted.

Parameters:
`pnts` - the dataset
`distance` - the distance function
• ### Method Detail

• #### searchNN

```public void searchNN(short[][] qus,
int[] indices,
float[] distances)```
Description copied from interface: `NearestNeighbours`
Search for the nearest neighbour to each of the N queries, and return the index of each nearest neighbour and the respective distance.

For efficiency, to use this method, you need to pre-construct the arrays for storing the results outside of the method and pass them in as arguments.

If a nearest-neighbour cannot be determined, it will have an index value of -1

Parameters:
`qus` - An array of N query vectors
`indices` - The return N-dimensional array for holding the indices of the nearest neighbour of each respective query.
`distances` - The return N-dimensional array for holding the distances of the nearest neighbour to each respective query.
• #### searchKNN

```public void searchKNN(short[][] qus,
int K,
int[][] indices,
float[][] distances)```
Description copied from interface: `NearestNeighbours`
Search for the K nearest neighbours to each of the N queries, and return the indices of each nearest neighbour and their respective distances.

For efficiency, to use this method, you need to pre-construct the arrays for storing the results outside of the method and pass them in as arguments.

If a k-th nearest-neighbour cannot be determined, it will have an index value of -1

Parameters:
`qus` - An array of N query vectors
`K` - the number of neighbours to find
`indices` - The return N*K-dimensional array for holding the indices of the K nearest neighbours of each respective query.
`distances` - The return N*K-dimensional array for holding the distances of the nearest neighbours of each respective query.
• #### searchNN

```public void searchNN(List<short[]> qus,
int[] indices,
float[] distances)```
Description copied from interface: `NearestNeighbours`
Search for the nearest neighbour to each of the N queries, and return the index of each nearest neighbour and the respective distance.

For efficiency, to use this method, you need to pre-construct the arrays for storing the results outside of the method and pass them in as arguments.

If a nearest-neighbour cannot be determined, it will have an index value of -1

Parameters:
`qus` - An array of N query vectors
`indices` - The return N-dimensional array for holding the indices of the nearest neighbour of each respective query.
`distances` - The return N-dimensional array for holding the distances of the nearest neighbour to each respective query.
• #### searchKNN

```public void searchKNN(List<short[]> qus,
int K,
int[][] indices,
float[][] distances)```
Description copied from interface: `NearestNeighbours`
Search for the K nearest neighbours to each of the N queries, and return the indices of each nearest neighbour and their respective distances.

For efficiency, to use this method, you need to pre-construct the arrays for storing the results outside of the method and pass them in as arguments.

If a k-th nearest-neighbour cannot be determined, it will have an index value of -1

Parameters:
`qus` - An array of N query vectors
`K` - the number of neighbours to find
`indices` - The return N*K-dimensional array for holding the indices of the K nearest neighbours of each respective query.
`distances` - The return N*K-dimensional array for holding the distances of the nearest neighbours of each respective query.
• #### searchKNN

```public List<IntFloatPair> searchKNN(short[] query,
int K)```
Description copied from interface: `NearestNeighbours`
Search for the K nearest neighbours to the given query and return an ordered list of pairs containing the distance and index of each neighbour.

If k neighbours cannot be determined, then the resultant list might have fewer than k elements.

Parameters:
`query` - the query vector
`K` - the number of neighbours to search for
Returns:
the top K nearest neighbours ordered by increasing distance
• #### searchNN

`public IntFloatPair searchNN(short[] query)`
Description copied from interface: `NearestNeighbours`
Search for the nearest neighbour to the given query and return a pair containing the distance and index of that neighbour.

If the nearest-neighbour cannot be determined `null` will be returned.

Parameters:
`query` - the query vector
Returns:
the distance and index of the nearest neighbour
• #### numDimensions

`public int numDimensions()`
Description copied from class: `ShortNearestNeighbours`
Get the number of dimensions of each vector in the dataset
Specified by:
`numDimensions` in class `ShortNearestNeighbours`
Returns:
the number of dimensions
• #### size

`public int size()`
Description copied from interface: `NearestNeighbours`
Get the size of the dataset
Returns:
the dataset size
• #### getPoints

`public short[][] getPoints()`
Get the underlying data points.
Returns:
the data points
• #### computeDistance

```public float computeDistance(short[] a,
short[] b)```
Compute the distance between two vectors using the underlying distance comparison used by this class.
Parameters:
`a` - the first vector
`b` - the second vector
Returns:
the distance between the two vectors
• #### distanceComparator

`public ShortFVComparator distanceComparator()`
Get the distance comparator
Returns:
the distance comparator