File size: 1,100 Bytes
cc651f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# `@rtsao/scc`

Find strongly connected components of a directed graph using [Tarjan's algorithm](https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm).

This algorithm efficiently yields both a topological order and list of any cycles.

## Installation

```
yarn add @rtsao/scc
```

```
npm install @rtsao/scc
```

## Usage

```js
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 β”‚ β”€β”˜
          β””β”€β”€β”€β”˜
```