Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

namastey-weighted-graph

A JavaScript package for implementing and working with weighted graph data structures, including various important methods for graph manipulation and traversal.

graph, weighted graph, data structures, JavaScript, graph algorithms, graph traversal, graph manipulation

readme

namastey-weighted-graph

Brief Description: A JavaScript package implementing a Weighted Graph data structure with various important methods.

Features

  • addVertex(vertex): Adds a vertex to the graph.
  • addEdge(vertex1, vertex2, weight): Adds an edge with a weight between two vertices.
  • removeVertex(vertex): Removes a vertex and its associated edges.
  • removeEdge(vertex1, vertex2): Removes an edge between two vertices.
  • getAdjacencyList(): Returns the adjacency list of the graph.
  • shortestPath(start, end): Finds the shortest path between two vertices using Dijkstra's Algorithm.

Installation

To install this package, use the following command:

npm install -g namastey-weighted-graph

Examples

const WeightedGraph = require('namastey-weighted-graph');

const graph = new WeightedGraph();
graph.addVertex('A');
graph.addVertex('B');
graph.addVertex('C');
graph.addEdge('A', 'B', 5);
graph.addEdge('B', 'C', 10);

console.log(graph.getAdjacencyList());
// Output: Map { 'A' => [ { node: 'B', weight: 5 } ],
//                  'B' => [ { node: 'A', weight: 5 }, { node: 'C', weight: 10 } ],
//                  'C' => [ { node: 'B', weight: 10 } ] }

console.log(graph.shortestPath('A', 'C'));
// Output: [ 'A', 'B', 'C' ]