Searches for a cycle in a directed graph, and tells you the nodes in the
first cycle it finds. Should work on your existing data structures
without conversion, because it operates on
Iterables and a
getConnectedNodes adapter function that you provide.
The implementation is a depth-first search using a stack instead of recursion, so it's not limited by the maximum call stack size.
Your environment must support
natively or via a polyfill.
npm install --save find-cycle
const findDirectedCycle =
The nodes to start the search from. Your nodes may be of any primitive
or object type besides
getConnectedNodes: (node: Node) => ?(Iterator<Node> | Iterable<Node>)
Given a node in your directed graph, return the nodes connected to it as
Iterable. You may return
there are no connected nodes.
An array of nodes in the first cycle found, if any, including each node in the cycle only once.
const findCycle =const edges =1: 22: 33: 44: 2 55: 37: 8 98: 19: 10 1110: 1111: 9 8const startNodes = 1const getConnectedNodes = edgesnodetodeep
const findCycle =