1.0.0 • Published 10 months ago

namastey-weighted-graph v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

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