js-shortest-path

1.1.0 • Public • Published

js-shortest-path-strategies

Algorithms strategies for finding graph shortest paths

Usage

npm install js-shortest-path

GreedyStrategy

const {GraphBuilder, GreedStrategy} = require('js-shortest-path');
 
//Creates a graph
const graph = GraphBuilder()
    .edge("S", "A", 2)
    .edge("S", "B", 4)
    .edge("A", "B", 1)
    .edge("A", "C", 2)
    .edge("B", "C", 3)
    .build();
 
const greedy = GreedStrategy(graph);
 
//Prints all paths from S to C
console.log(greedy.paths('S','C'));
 
//Prints the shortest path from S to C
console.log(greedy.shortest('S','C'));
 

DijkstraStrategy

const {GraphBuilder, DijkstraStrategy} = require('js-shortest-path');
 
//Creates a graph
const graph = GraphBuilder()
    .edge("S", "A", 2)
    .edge("S", "B", 4)
    .edge("A", "B", 1)
    .edge("A", "C", 2)
    .edge("B", "C", 3)
    .build();
 
const dijkstra = DijkstraStrategy(graph);
 
//Prints the shortest path from S to C
console.log(dijkstra.shortest('S','C'));
 

Dependencies (0)

    Dev Dependencies (3)

    Package Sidebar

    Install

    npm i js-shortest-path

    Weekly Downloads

    16

    Version

    1.1.0

    License

    ISC

    Unpacked Size

    18.1 kB

    Total Files

    12

    Last publish

    Collaborators

    • hdvianna