depth-first
TypeScript icon, indicating that this package has built-in type declarations

4.0.0 • Public • Published

depth-first travis npm

Depth first search directed graphs

Install

$ npm install --save depth-first

Usage

We want to traverse the following graph.

demo graph

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',
]
*/

Reverse edges

// 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',
]
*/

Inspired by

This package uses the same data structure as toposort

License

MIT © Sigurd Fosseng

Dependencies (0)

    Dev Dependencies (9)

    Package Sidebar

    Install

    npm i depth-first

    Weekly Downloads

    242

    Version

    4.0.0

    License

    MIT

    Unpacked Size

    4.68 kB

    Total Files

    7

    Last publish

    Collaborators

    • laat