namastey-graph
v1.0.0
Published
A package implementing Graph data structure
Downloads
11
Maintainers
Readme
namastey-graph
namastey-graph is a JavaScript package that implements the Graph data structure along with various important methods like adding/removing vertices and edges, depth-first search (DFS), and breadth-first search (BFS).
Features
- addVertex(vertex): Adds a vertex to the graph.
- addEdge(vertex1, vertex2): Adds an edge between two vertices.
- removeEdge(vertex1, vertex2): Removes the edge between two vertices.
- removeVertex(vertex): Removes a vertex and all associated edges.
- depthFirstSearch(start): Performs depth-first search starting from a given vertex.
- breadthFirstSearch(start): Performs breadth-first search starting from a given vertex.
- printGraph(): Prints the graph's adjacency list.
Installation
To install the package globally, run the following command:
npm install -g namastey-graph
Examples
const Graph = require('namastey-graph');
const graph = new Graph();
graph.addVertex('A');
graph.addVertex('B');
graph.addVertex('C');
graph.addEdge('A', 'B');
graph.addEdge('A', 'C');
graph.addEdge('B', 'C');
graph.printGraph();
// Output:
// A -> B, C
// B -> A, C
// C -> A, B
const dfsResult = graph.depthFirstSearch('A');
console.log('DFS:', dfsResult);
// Output: DFS: [ 'A', 'B', 'C' ]
const bfsResult = graph.breadthFirstSearch('A');
console.log('BFS:', bfsResult);
// Output: BFS: [ 'A', 'B', 'C' ]