depth-first
Version:
Depth first search directed graphs
65 lines (48 loc) • 1.46 kB
Markdown
[]: https://img.shields.io/travis/laat/depth-first.svg?branch=master
[]: https://travis-ci.org/laat/depth-first
[]: https://img.shields.io/npm/v/depth-first.svg?style=flat
[]: https://npmjs.org/package/depth-first
> Depth first search directed graphs
```
$ npm install --save depth-first
```
We want to traverse the following graph.

```js test
import dfs from "depth-first";
// First, we define our edges.
const edges = [
["put on your shoes", "tie your shoes"],
["put on your shirt", "put on your jacket"],
["put on your shorts", "put on your jacket"],
["put on your shorts", "put on your shoes"]
];
// List the vertices that can be reached starting at 'put on your shirt'
dfs(edges, "put on your shirt");
/* =>
[
'put on your shirt',
'put on your jacket',
]
*/
```
```js test
// List the vertices that can be reached starting at 'put on your jacket' when
// the edges are reversed
dfs(edges, "put on your jacket", { reverse: true });
/* =>
[
'put on your jacket',
'put on your shirt',
'put on your shorts',
]
*/
```
This package uses the same data structure as [toposort](https://github.com/marcelklehr/toposort)
MIT © [Sigurd Fosseng](https://github.com/laat)