Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@datastructures-js/graph
Advanced tools
npm install --save @datastructures-js/graph
const { Graph, DirectedGraph } = require('@datastructures-js/graph');
import { Graph, DirectedGraph } from '@datastructures-js/graph';
creates an empty graph
const directedGraph = new DirectedGraph();
const graph = new Graph();
adds a vertex to the graph.
runtime | params |
---|---|
O(1) |
key: {number} or {string}
value: {object} |
directedGraph.addVertex('v1', 1);
directedGraph.addVertex('v1', 1);
directedGraph.addVertex('v2', 2);
directedGraph.addVertex('v3', 3);
directedGraph.addVertex('v4', 4);
directedGraph.addVertex('v5', 5);
graph.addVertex('v1', true);
graph.addVertex('v2', true);
graph.addVertex('v3', true);
graph.addVertex('v4', true);
graph.addVertex('v5', true);
checks if the graph has a vertex by its key.
runtime | params |
---|---|
O(1) | key: {number} or {string} |
console.log(directedGraph.hasVertex('v7')); // false
console.log(graph.hasVertex('v1')); // true
gets the number of vertices in the graph.
runtime | return |
---|---|
O(1) | {number} |
console.log(directedGraph.verticesCount()); // 5
console.log(graph.verticesCount()); // 5
adds an edge with a weight between two existings vertices. Default weight is 1 if not defined. The edge is directed when added in a directed graph, and two-ways when added in a graph.
runtime | params |
---|---|
O(1) |
srcKey: {number} or {string} the source vertex key
destKey: {number} or {string} the destination vertex key weight: {number} the weight of the edge |
directedGraph.addEdge('v1', 'v2', 2);
directedGraph.addEdge('v1', 'v3', 3);
directedGraph.addEdge('v1', 'v4', 1);
directedGraph.addEdge('v2', 'v4', 1);
directedGraph.addEdge('v3', 'v5', 2);
directedGraph.addEdge('v4', 'v3', 1);
directedGraph.addEdge('v4', 'v5', 4);
graph.addEdge('v1', 'v2', 2);
graph.addEdge('v2', 'v3', 3);
graph.addEdge('v1', 'v3', 6);
graph.addEdge('v2', 'v4', 1);
graph.addEdge('v4', 'v3', 1);
graph.addEdge('v4', 'v5', 4);
graph.addEdge('v3', 'v5', 2);
checks if the graph has an edge between two existing vertices. In directed graph, it returns true only if there is a direction from source to destination.
runtime | params |
---|---|
O(1) |
srcKey: {number} or {string} the source vertex key
destKey: {number} or {string} the destination vertex key |
console.log(directedGraph.hasEdge('v1', 'v2')); // true
console.log(directedGraph.hasEdge('v2', 'v1')); // false
console.log(graph.hasEdge('v1', 'v2')); // true
console.log(graph.hasEdge('v2', 'v1')); // true
gets the number of edges in the graph.
runtime | return |
---|---|
O(1) | {number} |
console.log(directedGraph.edgesCount()); // 7
console.log(graph.edgesCount()); // 7
gets the edge's weight between two vertices in the graph. If there is no direct edge between the two vertices, it returns null. It also returns 0 if the source key is equal to destination key.
runtime | return |
---|---|
O(1) | {number} or {null} |
console.log(directedGraph.getWeight('v1', 'v2')); // 2
console.log(directedGraph.getWeight('v2', 'v1')); // null
console.log(directedGraph.getWeight('v1', 'v1')); // 0
console.log(graph.getWeight('v1', 'v2')); // 2
console.log(graph.getWeight('v2', 'v1')); // 2
console.log(graph.getWeight('v1', 'v1')); // 0
console.log(graph.getWeight('v1', 'v4')); // null
removes a vertex and all its edges from the graph
runtime | params |
---|---|
Directed Graph: O(E) : E = number of edges in the graph | key: {number} or {string} the vertex key |
Graph: O(K) : K ∈ E = number of connected edges to key |
directedGraph.removeVertex('v5');
console.log(directedGraph.verticesCount()); // 4
console.log(directedGraph.edgesCount()); // 5
graph.removeVertex('v5');
console.log(graph.verticesCount()); // 4
console.log(graph.edgesCount()); // 5
removes an edge between two existing vertices
runtime | params |
---|---|
O(1) | key: {number} or {string} the vertex key |
directedGraph.removeEdge('v1', 'v3');
console.log(directedGraph.edgesCount()); // 4
graph.removeEdge('v2', 'v3');
console.log(graph.edgesCount()); // 4
traverses the graph using the depth-first recursive search.
runtime | params |
---|---|
O(V) : V = number of vertices in the graph |
srcKey: {number} or {string} the starting vertex key
cb: {function(Vertex)} the callback that is called with the traversed vertex object. |
directedGraph.traverseDfs('v1', (v) => console.log(`${v.getKey()}:${v.getValue()}`));
/*
v1:1
v2:2
v4:4
v3:3
*/
graph.traverseDfs('v1', (v) => console.log(v.serialize()));
/*
{ key: 'v1', value: true }
{ key: 'v2', value: true }
{ key: 'v4', value: true }
{ key: 'v3', value: true }
*/
traverses the graph using the breadth-first search with a queue.
runtime | params |
---|---|
O(V) : V = number of vertices in the graph |
srcKey: {number} or {string} the starting vertex key
cb: {function(Vertex)} the callback that is called with the traversed vertex object. |
directedGraph.traverseBfs('v1', (v) => console.log(`${v.getKey()}:${v.getValue()}`));
/*
v1:1
v2:2
v4:4
v3:3
*/
graph.traverseBfs('v1', (v) => console.log(v.serialize()));
/*
{ key: 'v1', value: true }
{ key: 'v2', value: true }
{ key: 'v3', value: true }
{ key: 'v4', value: true }
*/
clears all vertices and edges in the graph.
runtime |
---|
O(1) |
directedGraph.clear();
console.log(directedGraph.verticesCount()); // 0
console.log(directedGraph.edgesCount()); // 0
graph.clear();
console.log(graph.verticesCount()); // 0
console.log(graph.edgesCount()); // 0
grunt build
The MIT License. Full License is here
FAQs
graph & directed graph implementation in javascript
The npm package @datastructures-js/graph receives a total of 5,217 weekly downloads. As such, @datastructures-js/graph popularity was classified as popular.
We found that @datastructures-js/graph demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.