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();
const dfsResult = graph.depthFirstSearch('A');
console.log('DFS:', dfsResult);
const bfsResult = graph.breadthFirstSearch('A');
console.log('BFS:', bfsResult);