What is graphlib?
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.
What are graphlib's main functionalities?
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
Other packages similar to graphlib
cytoscape
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
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
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
Graphlib is a JavaScript library that provides an implementation of a directed
multi-graph. This library is used as part of the
dagre library, but is available here in a
light-weight, standalone form.
Build / Install
Before building this library you need to install the npm package manager.
To get graphlib from npm, use:
$ npm install graphlib
Use
Graphlib currently exports a single Graph object. The graphlib.js
file includes
documentation for each of the functions on this Object.
Here we'll show a running example of the Graph API.
var Graph = require("graphlib").Graph;
var g = new Graph();
g.addNode("A");
g.hasNode("A");
g.addNode("B", "B's value");
console.log(g.node("B"));
g.addNode("C", { k: 123 });
g.addNode("D");
console.log(g.nodes());
g.addEdge("AB", "A", "B");
g.addEdge(null, "B", "C");
g.addEdge("CD", "C", "D", { k: 456 });
g.addEdge("AB2", "A", "B");
console.log(g.edges());
console.log(g.edges("A", "B"));
console.log(g.edges("D"));
var g2 = g.subgraph(["A", "B", "C"]);
console.log(g2.nodes());
console.log(g2.edges());
There are a number of other functions for doing queries on Graph
objects.
Please see graphlib.js
which includes documentation for each of the
functions.
License
Graphlib is licensed under the terms of the MIT License. See the LICENSE file
for details.