Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including graph-alg with all npm packages installed. Try it out:

var graphAlg = require("graph-alg")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

graph-alg v1.0.3

A Node.js module to manipulate Graph

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"]

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free