namastey-adjacency-list
v1.0.0
Published
A JavaScript package that implements the Adjacency List data structure for efficient graph representation and traversal.
Downloads
17
Maintainers
Readme
namastey-adjacency-list
Brief Description
namastey-adjacency-list
is a JavaScript package that provides an implementation of the Adjacency List data structure. It allows you to manage and manipulate a graph's vertices and edges efficiently.
Features
- addVertex(vertex): Adds a vertex to the graph. If the vertex already exists, it does nothing.
- addEdge(vertex1, vertex2): Adds an edge between two vertices. If either vertex does not exist, it is added automatically.
- removeVertex(vertex): Removes a vertex from the graph along with all its edges.
- removeEdge(vertex1, vertex2): Removes the edge between two vertices. If either vertex does not exist, it does nothing.
- getAdjacencyList(): Returns the adjacency list representation of the graph.
- hasEdge(vertex1, vertex2): Checks if there is an edge between two vertices.
Installation
To install the package, run the following command:
npm install namastey-adjacency-list
Examples
const AdjacencyList = require('namastey-adjacency-list');
const graph = new AdjacencyList();
// Add vertices
graph.addVertex('A');
graph.addVertex('B');
graph.addVertex('C');
// Add edges
graph.addEdge('A', 'B');
graph.addEdge('B', 'C');
// Get adjacency list
console.log(graph.getAdjacencyList());
// Output: { A: [ 'B' ], B: [ 'A', 'C' ], C: [ 'B' ] }
// Check if edge exists
console.log(graph.hasEdge('A', 'B')); // Output: true
console.log(graph.hasEdge('A', 'C')); // Output: false
// Remove an edge
graph.removeEdge('A', 'B');
console.log(graph.getAdjacencyList());
// Output: { A: [], B: [ 'C' ], C: [ 'B' ] }
// Remove a vertex
graph.removeVertex('B');
console.log(graph.getAdjacencyList());
// Output: { A: [], C: [] }