Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
The graphlib package is a JavaScript library for creating and manipulating directed and undirected graphs. It provides a variety of methods for graph creation, traversal, and analysis, making it a versatile tool for working with graph data structures.
Graph Creation
This feature allows you to create a new graph, add nodes, and create edges between nodes. The code sample demonstrates how to create a graph, add two nodes 'a' and 'b', and create an edge from 'a' to 'b'.
const graphlib = require('graphlib');
const g = new graphlib.Graph();
g.setNode('a');
g.setNode('b');
g.setEdge('a', 'b');
console.log(g.nodes()); // ['a', 'b']
console.log(g.edges()); // [{ v: 'a', w: 'b' }]
Graph Traversal
This feature allows you to traverse the graph using depth-first search (DFS). The code sample demonstrates how to perform a DFS starting from node 'a' and prints the traversal order.
const graphlib = require('graphlib');
const g = new graphlib.Graph();
g.setNode('a');
g.setNode('b');
g.setNode('c');
g.setEdge('a', 'b');
g.setEdge('b', 'c');
const dfs = graphlib.alg.dfs(g, ['a']);
console.log(dfs); // [{ v: 'a' }, { v: 'b' }, { v: 'c' }]
Graph Analysis
This feature allows you to analyze the graph for properties such as acyclicity. The code sample demonstrates how to check if the graph contains cycles and prints the result.
const graphlib = require('graphlib');
const g = new graphlib.Graph();
g.setNode('a');
g.setNode('b');
g.setNode('c');
g.setEdge('a', 'b');
g.setEdge('b', 'c');
g.setEdge('c', 'a');
const isAcyclic = graphlib.alg.isAcyclic(g);
console.log(isAcyclic); // false
Cytoscape is a graph theory library for visualization and analysis. It provides a rich set of features for graph manipulation and visualization, making it more suitable for applications that require interactive graph displays. Compared to graphlib, Cytoscape offers more advanced visualization capabilities but may be more complex to use for simple graph operations.
d3-graphviz is a library for rendering graphs described in the DOT language using D3. It is particularly useful for visualizing complex graphs and integrates well with the D3 ecosystem. While graphlib focuses on graph manipulation and analysis, d3-graphviz excels in graph rendering and visualization.
Dagre is a JavaScript library for laying out directed graphs on the client-side. It is often used in conjunction with graphlib for layout purposes. Dagre provides advanced layout algorithms, making it a good complement to graphlib's graph manipulation capabilities.
Graphlib is a JavaScript library that provides data structures for undirected and directed multi-graphs along with algorithms that can be used with them.
Note that graphlib is current a pre-1.0.0 library. We will do our best to maintain backwards compatibility for patch level increases (e.g. 0.0.1 to 0.0.2) but make no claim to backwards compatibility across minor releases (e.g. 0.0.1 to 0.1.0). Watch our CHANGELOG for details on changes.
Before installing this library you need to install the npm package manager.
To get graphlib from npm, use:
$ npm install graphlib
You can get the latest browser-ready scripts:
Before building this library you need to install the npm package manager.
Check out this project and run this command from the root of the project:
$ make
This will generate graphlib.js
and graphlib.min.js
in the out/dist
directory
of the project.
var Digraph = require("graphlib").Digraph;
// Create a new empty graph
var g = new Digraph();
// Add node "A" to the graph with no value
g.addNode("A");
// This returns true
g.hasNode("A");
// Add node "B" to the graph with a String value
g.addNode("B", "B's value");
// Prints `B's value`
console.log(g.node("B"));
// Add node "C" to the graph with an Object value
g.addNode("C", { k: 123 });
g.addNode("D");
// Prints `[ 'A', 'B', 'C', 'D' ]`
console.log(g.nodes());
// Add a directed edge with the ID "AB" from "A" to "B", but assign no value
g.addEdge("AB", "A", "B");
// Add a directed edge with no ID (Diraph will assign one) from "B" to "C"
g.addEdge(null, "B", "C");
// Add a directed edge from "C" to "D" with an Object value
g.addEdge("CD", "C", "D", { k: 456 });
// Since Digraph is a multi-graph, we can have multiple edges incident on the
// same source and target nodes.
g.addEdge("AB2", "A", "B");
// Prints `[ 'AB', '_ANON-1', 'CD', 'AB2' ]`. `_ANON-1` is the edge from "B" to "C"
console.log(g.edges());
// Which edges go from "A" to "B"? This prints `[ 'AB', 'AB2' ]`
console.log(g.outEdges("A", "B"));
// Which edges are incident on "D"? This prints `[ 'CD' ]`
console.log(g.incidentEdges("D"));
// How about a subgraph?
var g2 = g.subgraph(["A", "B", "C"]);
// Prints `[ 'A', 'B', 'C' ]`
console.log(g2.nodes());
// Prints `[ 'AB', '_ANON-1', 'AB2' ]`. Note that edges that have both their
// source and target nodes in the graph are also included in the subgraph.
console.log(g2.edges());
We welcome contributions under the MIT license! Here are a few ways you can help:
If your change involves change to the core Graph API, we recommend discussing the idea via a GitHub issue first.
Graphlib is licensed under the terms of the MIT License. See the LICENSE file for details.
FAQs
A directed and undirected multi-graph library
The npm package graphlib receives a total of 1,785,822 weekly downloads. As such, graphlib popularity was classified as popular.
We found that graphlib demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.