graph-alg
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

graph-alg

npm version Build Status Coverage Status

A Node.js module to manipulate Graph

Installation

npm install graph-alg --save

Usage

Javascript

const Dijkstra = require('graph-alg').Dijkstra;

TypeScript

import { Dijkstra } from 'graph-alg';

Test

npm run test

Graph class

Dijkstra

Example:

import { Dijkstra } from 'graph-alg';
 
/**
 * Oriented Graph with ponderation
 * +-+        +-+         +-+         +-+
 * |0+---5---->1+----+-4-->3+-----3--->5|
 * +++        +^+    |    +++         +^+
 *  |          |     |     |           |
 *  |          8     2     6           |
 *  |          |     |     |           |
 *  |         +++    |    +v+          |
 *  +----2---->2+--7-+---->4+-----1----+
 *            +-+         +-+
 */
const graph: IDirectedGraph = {
    "0": { "1": 5, "2": 2 },
    "1": { "3": 4, "4": 2 },
    "2": { "1": 8, "4": 7 },
    "3": { "5": 3, "4": 6 },
    "4": { "5": 1 },
    "5": {},
};
// Initialize Dijkstra alg with an oriented graph
const dijkstra = new Dijkstra(graph);
// Compute the smaller path from node 0 to node 5
const result = dijkstra.resolve("0", "5");
 
// result.distance equals 8
// result.path equals ["0", "1", "4", "5"]
 

Dependencies (0)

    Dev Dependencies (10)

    Package Sidebar

    Install

    npm i graph-alg

    Weekly Downloads

    0

    Version

    1.0.3

    License

    ISC

    Unpacked Size

    375 kB

    Total Files

    64

    Last publish

    Collaborators

    • mseiler