namastey-directed-graph
Brief Description:
namastey-directed-graph
is a JavaScript package that implements a directed graph data structure. It provides various methods to manage and interact with directed graphs.
Features
-
addVertex(vertex):
Adds a new vertex to the graph. If the vertex already exists, it is not added again.
-
addEdge(vertex1, vertex2):
Adds a directed edge from vertex1
to vertex2
. Both vertices must exist in the graph.
-
removeVertex(vertex):
Removes the specified vertex and all edges associated with it. Also removes any edges leading to this vertex.
-
removeEdge(vertex1, vertex2):
Removes the directed edge from vertex1
to vertex2
.
-
hasEdge(vertex1, vertex2):
Checks if there is an edge from vertex1
to vertex2
.
-
getVertices():
Returns an array of all vertices in the graph.
-
getEdges():
Returns an array of all edges in the graph as pairs of vertices.
-
printGraph():
Prints the graph to the console in a human-readable format.
Installation
To install the namastey-directed-graph
package, use npm:
npm install namastey-directed-graph
Examples
const DirectedGraph = require('namastey-directed-graph');
const graph = new DirectedGraph();
graph.addVertex('A');
graph.addVertex('B');
graph.addVertex('C');
graph.addEdge('A', 'B');
graph.addEdge('B', 'C');
console.log('Vertices:', graph.getVertices());
console.log('Edges:', graph.getEdges());
graph.printGraph();
console.log('Has edge from A to B:', graph.hasEdge('A', 'B'));
graph.removeEdge('A', 'B');
console.log('Has edge from A to B:', graph.hasEdge('A', 'B'));
graph.removeVertex('C');
console.log('Vertices after removing C:', graph.getVertices());