namastey-undirected-graph
v1.0.0
Published
A JavaScript package that implements the Undirected Graph data structure with various important methods.
Downloads
10
Maintainers
Readme
namastey-undirected-graph
Brief Description
namastey-undirected-graph
is a JavaScript package that provides an implementation of an undirected graph data structure. It supports various methods for manipulating and querying the graph.
Features
- addVertex(vertex): Adds a new vertex to the graph. If the vertex already exists, it does nothing.
- addEdge(vertex1, vertex2): Adds an undirected edge between
vertex1
andvertex2
. Creates vertices if they do not exist. - removeVertex(vertex): Removes a vertex and all its edges from the graph.
- removeEdge(vertex1, vertex2): Removes the undirected edge between
vertex1
andvertex2
. - hasEdge(vertex1, vertex2): Checks if an edge exists between
vertex1
andvertex2
. - printGraph(): Prints the adjacency list representation of the graph.
Installations
You can install namastey-undirected-graph
globally using npm:
npm install -g namastey-undirected-graph
Examples
const Graph = require('namastey-undirected-graph');
const graph = new Graph();
graph.addVertex('A');
graph.addVertex('B');
graph.addEdge('A', 'B');
graph.printGraph(); // Output: A -> B\nB -> A
console.log(graph.hasEdge('A', 'B')); // Output: true
graph.removeEdge('A', 'B');
console.log(graph.hasEdge('A', 'B')); // Output: false
graph.removeVertex('A');
graph.printGraph(); // Output: B -> null