You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
5 months ago | |
---|---|---|
.. | ||
LICENSE | 5 months ago | |
README.md | 5 months ago | |
index.d.ts | 5 months ago | |
index.js | 5 months ago | |
index.js.flow | 5 months ago | |
package.json | 5 months ago |
README.md
@rtsao/scc
Find strongly connected components of a directed graph using Tarjan's algorithm.
This algorithm efficiently yields both a topological order and list of any cycles.
Installation
yarn add @rtsao/scc
npm install @rtsao/scc
Usage
const scc = require("@rtsao/scc");
const digraph = new Map([
["a", new Set(["c", "d"])],
["b", new Set(["a"])],
["c", new Set(["b"])],
["d", new Set(["e"])],
["e", new Set()]
]);
const components = scc(digraph);
// [ Set { 'e' }, Set { 'd' }, Set { 'b', 'c', 'a' } ]
Illustration of example input digraph
┌───┐ ┌───┐
│ d │ ◀── │ a │ ◀┐
└───┘ └───┘ │
│ │ │
▼ ▼ │
┌───┐ ┌───┐ │
│ e │ │ c │ │
└───┘ └───┘ │
│ │
▼ │
┌───┐ │
│ b │ ─┘
└───┘