namastey-directed-graph
v1.0.1
Published
A JavaScript package for implementing the Directed Graph data structure with various important methods.
Downloads
3
Maintainers
Readme
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 fromvertex1
tovertex2
. 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 fromvertex1
tovertex2
.hasEdge(vertex1, vertex2):
Checks if there is an edge fromvertex1
tovertex2
.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()); // Output: Vertices: [ 'A', 'B', 'C' ]
console.log('Edges:', graph.getEdges()); // Output: Edges: [ [ 'A', 'B' ], [ 'B', 'C' ] ]
graph.printGraph();
// Output:
// A -> B
// B -> C
console.log('Has edge from A to B:', graph.hasEdge('A', 'B')); // Output: Has edge from A to B: true
graph.removeEdge('A', 'B');
console.log('Has edge from A to B:', graph.hasEdge('A', 'B')); // Output: Has edge from A to B: false
graph.removeVertex('C');
console.log('Vertices after removing C:', graph.getVertices()); // Output: Vertices after removing C: [ 'A', 'B' ]