eko-graph

implemented of weighted and directed graph

Usage no npm install needed!

<script type="module">
  import ekoGraph from 'https://cdn.skypack.dev/eko-graph';
</script>

README

Graph

Table of Content

  1. Features
  2. Installation
  3. Basic usage
  4. Example
  5. API
  6. Tests
  7. License

Features

  • Make on the weighted graph and the directed graph concept.
  • Calculate the cost from given path
  • Calculate the cheapest cost from the point to the another point

Installation

Using NPM or YARN

# NPM
$ npm install eko-graph --save

# YARN
$ yarn add eko-graph

Basic usage

'use strict';

const { Graph } = require('eko-graph');

const graph = new Graph();

// to add vertex and its neighbor and weight on the edge
graph.addAdjacency('AB1');
graph.addAdjacency('AC4');

console.log(graph.adjacency);
// { A: [{ val: 'B', weight: 1 }, { val: 'C', weight: 4 }] }

Example

To view the example, clone this repo and install dependencies

$ git clone git@github.com:JPooban/eko-graph.git

$ cd eko-graph

$ npm install

Then run the example

$ node ./example/get-delivery-cost.js

API

Get delivery cost

Calculate the cost from given route.

// graph.getDelivertCost(route);

const cost = graph.getDelivertCost([ 'A', 'B', 'E' ]); // -> 4

In the case of non existing route, it will throw an error with No Such Route message.

Get possible delivery route number

Calculate​ ​the​ ​number​ ​of​ ​possible​ ​delivery​ ​route​ ​that​ ​can​ ​be​ ​construct​ ​by​ ​the​ ​given conditions.

// graph.getPossibleDeliveryRoute(start, end, options);

const possibleNumber = graph.getPossibleDeliveryRoute('E', 'D', { maxStop: 4 }); // -> 4

Properties

Property Default Description
maxStop undefined The maximum stop of each path.
(count the path which is length is less than or equal maxStop + 1)

Get cheapest delivery cost

Calculate​ ​the​ ​cheapest​ ​delivery​ ​route​ ​between​ ​two​ vertex.

// graph.getCheapestCost(start, end);

const cheapestCost = graph.getCheapestCost('E', 'D'); // -> 9

Tests

To run unit test suite, must install the dependencies. Use Jest as the test framework.

# install dependencies
$ npm install

# run the unit test
$ npm run test

License

MIT