Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
graph-data-structure
Advanced tools
The graph-data-structure npm package provides a simple and efficient way to create and manipulate graph data structures in JavaScript. It supports various graph operations such as adding nodes and edges, finding paths, and detecting cycles.
Add Nodes and Edges
This feature allows you to add nodes and edges to the graph. The code sample demonstrates how to create a graph, add nodes 'A' and 'B', and then add an edge from 'A' to 'B'.
const Graph = require('graph-data-structure');
const graph = Graph();
graph.addNode('A');
graph.addNode('B');
graph.addEdge('A', 'B');
console.log(graph.serialize());
Find Paths
This feature allows you to find paths between nodes in the graph. The code sample demonstrates how to find a path from node 'A' to node 'C' through node 'B'.
const Graph = require('graph-data-structure');
const graph = Graph();
graph.addNode('A');
graph.addNode('B');
graph.addNode('C');
graph.addEdge('A', 'B');
graph.addEdge('B', 'C');
const path = graph.path('A', 'C');
console.log(path);
Detect Cycles
This feature allows you to detect cycles in the graph. The code sample demonstrates how to detect a cycle in a graph where nodes 'A', 'B', and 'C' form a cycle.
const Graph = require('graph-data-structure');
const graph = Graph();
graph.addNode('A');
graph.addNode('B');
graph.addNode('C');
graph.addEdge('A', 'B');
graph.addEdge('B', 'C');
graph.addEdge('C', 'A');
const hasCycle = graph.hasCycle();
console.log(hasCycle);
Graphlib is a library for creating and manipulating directed graphs in JavaScript. It offers a rich set of features including graph serialization, pathfinding, and cycle detection. Compared to graph-data-structure, graphlib provides more advanced functionalities and is suitable for more complex graph operations.
Cytoscape is a graph theory library for visualizing and analyzing graphs. It supports a wide range of graph operations and provides extensive visualization capabilities. While graph-data-structure focuses on basic graph operations, Cytoscape is more geared towards visualization and complex graph analysis.
d3-graphviz is a library that integrates Graphviz with D3.js to create interactive graph visualizations. It is particularly useful for rendering graphs and visualizing their structure. Unlike graph-data-structure, which is more about graph manipulation, d3-graphviz excels in graph visualization.
A graph data structure with topological sort algorithm.
If you are using NPM, install the library by running
npm install graph-data-structure
Require it in your code like this.
var Graph = require("graph-data-structure");
Create a graph instance.
var graph = Graph();
Add some nodes and edges.
graph.addNode("a");
graph.addNode("b");
graph.addEdge("a", "b");
Nodes are added implicitly when edges are added.
graph.addEdge("b", "c");
Topological sort can be invoked like this.
graph.topologicalSort(); // Returns ["a", "b", "c"]
Here's an example of topological sort with getting dressed (from Cormen et al. "Introduction to Algorithms" page 550).
var graph = Graph();
// Shoes depend on socks.
// Socks need to be put on before shoes.
graph.addEdge("socks", "shoes");
graph.addEdge("shirt", "belt");
graph.addEdge("shirt", "tie");
graph.addEdge("tie", "jacket");
graph.addEdge("belt", "jacket");
graph.addEdge("pants", "shoes");
graph.addEdge("underpants", "pants");
graph.addEdge("pants", "belt");
console.log(graph.topologicalSort()); // prints [ "underpants", "pants", "shirt", "tie", "belt", "jacket", "socks", "shoes" ]
For more detailed example code that shows more methods, have a look at the tests.
Methods on graphs include:
addNode(node)
Adds a node to the graph, accepts a string node identifier. If node was already added, this function does nothing.removeNode(node)
Removes a node from the graph. Also removes incoming and outgoing edges.nodes()
List all nodes in the graph.adjacent(node)
Gets the adjacent node list for the given node. This is the set of nodes for which there is an incoming edge from the given node.addEdge(u, v)
Adds an edge from node u to node v. Implicitly adds the nodes if they were not already added.removeEdge(u, v)
Removes the edge from node u to node v. Does not remove the nodes. Does nothing if the edge does not exist.depthFirstSearch(sourceNodes, includeSourceNodes)
Depth First Search algorithm, inspired by Cormen et al. "Introduction to Algorithms" 3rd Ed. p. 604. This variant includes an additional option includeSourceNodes
to specify whether to include or exclude the source nodes from the result (true by default). If sourceNodes
is not specified, all nodes in the graph are used as source nodes.topologicalSort(sourceNodes, includeSourceNodes)
The topological sort algorithm yields a list of visited nodes such that for each visited edge (u, v), u comes before v in the list. Amazingly, this comes from just reversing the result from depth first search. Inspired by Cormen et al. "Introduction to Algorithms" 3rd Ed. p. 613. This variant includes an additional option includeSourceNodes
to specify whether to include or exclude the source nodes from the result (true by default). If sourceNodes
is not specified, all nodes in the graph are used as source nodes.FAQs
A graph data structure with topological sort.
The npm package graph-data-structure receives a total of 184,403 weekly downloads. As such, graph-data-structure popularity was classified as popular.
We found that graph-data-structure demonstrated a healthy version release cadence and project activity because the last version was released less than 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’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.