What is @dagrejs/graphlib?
@dagrejs/graphlib is a JavaScript library for creating and manipulating directed graphs. It provides a flexible and efficient way to work with graph data structures, including adding and removing nodes and edges, traversing the graph, and performing various graph algorithms.
What are @dagrejs/graphlib's main functionalities?
Creating a Graph
This feature allows you to create a new graph, add nodes, and add edges between nodes. The code sample demonstrates creating a graph, adding two nodes 'a' and 'b', and creating an edge from 'a' to 'b'.
const graphlib = require('@dagrejs/graphlib');
const Graph = graphlib.Graph;
const g = new 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 perform depth-first search (DFS) traversal on the graph. The code sample demonstrates creating a graph with three nodes and two edges, and then performing DFS starting from node 'a'.
const graphlib = require('@dagrejs/graphlib');
const Graph = graphlib.Graph;
const g = new 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' }]
Finding Shortest Path
This feature allows you to find the shortest path in the graph using Dijkstra's algorithm. The code sample demonstrates creating a graph with three nodes and three weighted edges, and then finding the shortest path from node 'a' to all other nodes.
const graphlib = require('@dagrejs/graphlib');
const Graph = graphlib.Graph;
const g = new Graph();
g.setNode('a');
g.setNode('b');
g.setNode('c');
g.setEdge('a', 'b', { weight: 1 });
g.setEdge('b', 'c', { weight: 2 });
g.setEdge('a', 'c', { weight: 4 });
const dijkstra = graphlib.alg.dijkstra(g, 'a');
console.log(dijkstra); // { a: { distance: 0 }, b: { distance: 1 }, c: { distance: 3 } }
Other packages similar to @dagrejs/graphlib
graphlib
graphlib is a library for creating and manipulating directed graphs in JavaScript. It provides similar functionalities to @dagrejs/graphlib, including adding and removing nodes and edges, graph traversal, and graph algorithms. It is part of the dagre project, which includes tools for graph layout and rendering.
cytoscape
cytoscape is a graph theory library for analysis and visualization. It provides a rich set of features for graph manipulation, layout, and rendering. Compared to @dagrejs/graphlib, cytoscape offers more advanced visualization capabilities and is suitable for creating interactive graph-based applications.
vis-network
vis-network is a library for creating, manipulating, and visualizing networks. It provides a wide range of features for graph visualization, including different layout algorithms and interaction capabilities. While @dagrejs/graphlib focuses on graph manipulation and algorithms, vis-network emphasizes visualization and user interaction.
Graphlib
Graphlib is a JavaScript library that provides data structures for undirected
and directed multi-graphs along with algorithms that can be used with them.
To learn more see our Wiki.
There are 2 versions on NPM, but only the one in the DagreJs org is receiving updates right now.
License
Graphlib is licensed under the terms of the MIT License. See the
LICENSE file
for details.