Graphology Shortest Path
Shortest path functions for graphology
.
Installation
npm install graphology-shortest-path
Usage
Unweighted
bidirectional
Returns the shortest path in the graph between source & target or null
if such a path does not exist.
import {bidirectional} from 'graphology-shortest-path';
import {bidirectional} from 'graphology-shortest-path/unweighted';
const path = bidirectional(graph, source, target);
Arguments
- graph Graph: a
graphology
instance. - source any: source node.
- target any: target node.
singleSource
Return a map of every shortest path between the given source & all the nodes of the graph.
import {singleSource} from 'graphology-shortest-path';
import {singleSource} from 'graphology-shortest-path/unweighted';
const paths = singleSource(graph, source);
Arguments
- graph Graph: a
graphology
instance. - source any: source node.
singleSourceLength
Return a map of every shortest path length between the given source & all the nodes of the graph.
import {singleSourceLength} from 'graphology-shortest-path';
import {singleSourceLength} from 'graphology-shortest-path/unweighted';
const paths = singleSourceLength(graph, source);
Arguments
- graph Graph: a
graphology
instance. - source any: source node.
undirectedSingleSourceLength
Return a map of every shortest path length between the given source & all the nodes of the graph. This is basically the same as singleSourceLength except that it will consider any given graph as undirected when traversing.
import {undirectedSingleSourceLength} from 'graphology-shortest-path';
import {undirectedSingleSourceLength} from 'graphology-shortest-path/unweighted';
const paths = undirectedSingleSourceLength(graph, source);
Arguments
- graph Graph: a
graphology
instance. - source any: source node.
Dijkstra
bidirectional
Returns the shortest path in the weighted graph between source & target or null
if such a path does not exist.
import {dijkstra} from 'graphology-shortest-path';
import dijkstra from 'graphology-shortest-path/dijkstra';
const path = dijkstra.bidirectional(graph, source, target);
const path = dijkstra.bidirectional(graph, source, target, 'customWeight');
const path = dijkstra.bidirectional(
graph,
source,
target,
(_, attr) => attr.importance
);
Arguments
- graph Graph: a
graphology
instance. - source any: source node.
- target any: target node.
- getEdgeWeight ?string|function [
weight
]: name of the weight attribute or getter function.
singleSource
Return a map of every shortest path between the given source & all the nodes of the weighted graph.
import {dijkstra} from 'graphology-shortest-path';
import dijkstra from 'graphology-shortest-path/dijkstra';
const paths = dijkstra.singleSource(graph, source);
const paths = dijkstra.singleSource(graph, source, 'customWeight');
const path = dijkstra.singleSource(graph, source, (_, attr) => attr.importance);
Arguments
- graph Graph: a
graphology
instance. - source any: source node.
- getEdgeWeight ?string|function [
weight
]: name of the weight attribute or getter function.
Utilities
edgePathFromNodePath
Helper function that can convert a node path to an edge path.
import {edgePathFromNodePath} from 'graphology-shortest-path';
import {edgePathFromNodePath} from 'graphology-shortest-path/utils';
const edgePath = edgePathFromNodePath(graph, nodePath);
Arguments
- graph Graph: a
graphology
instance. - nodePath Array: node path to convert.