# jkstra

> Small JavaScript graph routing library

Latest version **0.1.1** (published 2021-11-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install jkstra
pnpm add jkstra
yarn add jkstra
bun add jkstra
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2021-11-28 |
| First published | 2016-04-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 144.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 49 |
| Author | Benjamin Becquet |
| Maintainers | bbecquet |
| Keywords | graph, routing, pathfinding, Dijkstra |

## Links

- npm: https://www.npmjs.com/package/jkstra
- Repository: https://github.com/bbecquet/jkstra
- Homepage: https://github.com/bbecquet/jkstra#readme
- Issues: https://github.com/bbecquet/jkstra/issues
- npm.io page: https://npm.io/package/jkstra

## Dependencies (1)

- [updatable-priority-queue](https://npm.io/package/updatable-priority-queue.md) 0.0.1

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 0.1.1 (latest) — 2021-11-28
- 0.1.0 — 2021-11-28
- 0.0.6 — 2016-04-28
- 0.0.5 — 2016-04-25
- 0.0.4 — 2016-04-22
- 0.0.3 — 2016-04-11

## README

# jkstra - A simple graph and routing library

**jkstra** is a JavaScript library to work with **graphs** - as in "data structures", with edges and vertices, not as in "graphics".

[See an interactive demo](http://bbecquet.github.io/jkstra/demo/).

It was made originally as a base model for a webapp that needed to run offline shortest path computations on a public transportation network.

For now it supports simple directed graphs and provides implementations Dijkstra and A\* algorithms to find shortest paths in it.

```bash
npm install jkstra
```

## Simple example

```javascript
var jkstra = require("<path_to_jkstra>");

var graph = new jkstra.Graph();

var n = []; // to easily keep references to the node objects

n.push(graph.addVertex(0));
n.push(graph.addVertex(1));
n.push(graph.addVertex(2));
n.push(graph.addVertex(3));
n.push(graph.addVertex(4)); // the parameter is arbitrary data assigned to the node
n.push(graph.addVertex({ id: 666, label: "A node holding complex data" }));

console.log(n[3].data); // => 3
console.log(n[5].data); // => {id: 666, label: 'A node holding complex data'}

graph.addEdge(n[0], n[1], 7); // The edges are directed. Here, only the edge from 0 to 1 is created.
graph.addEdgePair(n[0], n[2], 9); // But two opposite edges sharing the same data can be easily created
graph.addEdge(n[0], n[5], 14);
graph.addEdge(n[1], n[2], 10);
graph.addEdge(n[1], n[3], 15);
graph.addEdge(n[2], n[5], 2);
graph.addEdge(n[2], n[3], 12); // As for the nodes, you can assign any data to the edge.
graph.addEdge(n[3], n[4], 6); // Here we use it to store a single value which will be used as a cost.
graph.addEdge(n[5], n[4], 10);

// you can access edges from nodes with the outEdges/inEdges function
console.log(
  graph
    .outEdges(n[5])
    .map(function (e) {
      return e.data;
    })
    .join()
);
// => [10]

var dijkstra = new jkstra.algos.Dijkstra(graph);

// computes the shortestPath between nodes 0 and 4,
// using the single number stored in each as its cost
var path = dijkstra.shortestPath(n[0], n[4], {
  edgeCost: function (e) {
    return e.data;
  },
});

// the result is an array of the edge objects that make the path
console.log(
  path
    .map(function (e) {
      return e.data;
    })
    .join()
);
// => [9, 2, 10]
```

## [API](doc/API.md)

## License

MIT.

## Author

[Benjamin Becquet](http://bbecquet.net/)

---
_Source: https://npm.io/package/jkstra · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
