What is tarjan-graph?
The tarjan-graph npm package is a JavaScript library that implements Tarjan's algorithm to find strongly connected components in a directed graph. This is useful in various applications such as analyzing networks, optimizing paths, and detecting cycles in complex systems.
What are tarjan-graph's main functionalities?
Finding Strongly Connected Components
This feature allows you to find and list all strongly connected components in a directed graph. A strongly connected component (SCC) is a subset of the graph where every vertex is reachable from every other vertex in the same subset.
const TarjanGraph = require('tarjan-graph');
const graph = new TarjanGraph();
graph.add('A', ['B']);
graph.add('B', ['C']);
graph.add('C', ['A']);
const scc = graph.getStronglyConnectedComponents();
console.log(scc);
Cycle Detection
This feature checks if the directed graph contains any cycles. A cycle exists when there is a path from a node back to itself through directed edges.
const TarjanGraph = require('tarjan-graph');
const graph = new TarjanGraph();
graph.add('X', ['Y']);
graph.add('Y', ['Z']);
graph.add('Z', ['X']);
const hasCycle = graph.hasCycle();
console.log(hasCycle);
Other packages similar to tarjan-graph
strongly-connected-components
This package also finds strongly connected components in a directed graph. It is similar to tarjan-graph but might differ in implementation details and API design, offering users alternatives based on their specific needs or preferences.
graphology
Graphology is a comprehensive graph analysis library. While it includes functionality for finding strongly connected components, it also provides a wide range of other graph-related algorithms and utilities, making it more versatile than tarjan-graph if broader graph analysis capabilities are needed.
tarjan-graph
This is a simple directed graph lib, mostly just for checking if a
directed graph contains a cycle. It uses
Tarjan's algorithm
for checking if the graph contains a cycle.
This library also has some very basic Graphviz support
for visualizing graphs using the DOT language.
Installation
Install using npm
:
npm install tarjan-graph
Usage
JavaScript:
const Graph = require('tarjan-graph').default;
TypeScript:
import Graph from 'tarjan-graph';
All examples use the following graph:
node -e "
const Graph = require('tarjan-graph').default;
const graph = new Graph()
.add('a', ['b', 'c'])
.add('b', ['d', 'e'])
.add('c', ['b'])
.add('d', ['e'])
.add('e', ['c'])
.add('f', ['c', 'a', 'g'])
.add('g', ['h', 'i'])
.add('h', ['j'])
.add('i', ['j'])
.add('j', ['f', 'k'])
.add('k', ['k']);
console.log(graph.toDot());
" | dot -o docs/example-graph.png -Tpng`
Doing stuff with cycles:
console.log(graph.hasCycle());
console.log(graph.getCycles());
Doing stuff with SCCs:
console.log(graph.getStronglyConnectedComponents());
Searching:
graph.dfs('g', (v) => {
console.log(v.name + ': ' + v.successors.map(w => w.name).join(', '));
});
console.log(graph.getDescendants('a'));
And of course, dat dot:
console.log(graph.toDot());