Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/seregpie/nearestneighborchain
Builds a hierarchy of the clusters.
https://github.com/seregpie/nearestneighborchain
algorithm array cluster clustering collection distance hierarchical hierarchy javascript method object technique
Last synced: 20 days ago
JSON representation
Builds a hierarchy of the clusters.
- Host: GitHub
- URL: https://github.com/seregpie/nearestneighborchain
- Owner: SeregPie
- License: mit
- Created: 2016-12-02T13:47:56.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-02-28T14:39:51.000Z (almost 5 years ago)
- Last Synced: 2024-09-25T09:30:26.292Z (3 months ago)
- Topics: algorithm, array, cluster, clustering, collection, distance, hierarchical, hierarchy, javascript, method, object, technique
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NearestNeighborChain
`NearestNeighborChain(values, distance)`
Builds a hierarchy of the clusters.
| argument | description |
| ---: | :--- |
| `values` | An iterable of the values to build the hierarchy of the clusters from. |
| `distance` | A function to calculate the distance between two values. The value pairs with the lowest distance build a cluster. |Returns the clustered values as a nested array.
## dependencies
- [BronKerbosch](https://github.com/SeregPie/BronKerbosch)
## setup
### npm
```shell
npm install @seregpie/nearest-neighbor-chain
```### ES module
```javascript
import NearestNeighborChain from '@seregpie/nearest-neighbor-chain';
```### Node
```javascript
let NearestNeighborChain = require('@seregpie/nearest-neighbor-chain');
```### browser
```html
```
The module is globally available as `NearestNeighborChain`.
## usage
```javascript
let array = [4, 90, 12, 61, 29];
let clusters = NearestNeighborChain(array, (a, b) => Math.abs(a - b));
// => [[29, [4, 12]], [90, 61]]
```---
Overlapping clusters are merged together.
```javascript
let intersection = function(a, b) {
a = new Set(a);
b = new Set(b);
return [...a].filter(v => b.has(v));
};
let array = ['ac', 'ab', 'baab', 'aba', 'bc'];
let clusters = NearestNeighborChain(array, (a, b) => -intersection(a, b).length);
// => ['ac', 'bc', ['ab', 'baab', 'aba']]
```## see also
- [KMeans](https://github.com/SeregPie/KMeans)