{"id":22382457,"url":"https://github.com/jcoreio/find-cycle","last_synced_at":"2025-07-31T03:31:51.969Z","repository":{"id":28770763,"uuid":"119282865","full_name":"jcoreio/find-cycle","owner":"jcoreio","description":"find and identify a cycle in a directed graph","archived":false,"fork":false,"pushed_at":"2023-11-07T22:18:09.000Z","size":986,"stargazers_count":15,"open_issues_count":21,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-11-07T01:06:42.788Z","etag":null,"topics":["cycle","cycles","cyclic","detect","detection","detector","directed","directed-graph","discord","find","finder","graph","graphs","javascript","js","search"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jcoreio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-28T17:50:09.000Z","updated_at":"2023-11-07T21:09:06.000Z","dependencies_parsed_at":"2024-06-19T02:57:28.111Z","dependency_job_id":null,"html_url":"https://github.com/jcoreio/find-cycle","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Ffind-cycle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Ffind-cycle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Ffind-cycle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Ffind-cycle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jcoreio","download_url":"https://codeload.github.com/jcoreio/find-cycle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228209939,"owners_count":17885595,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cycle","cycles","cyclic","detect","detection","detector","directed","directed-graph","discord","find","finder","graph","graphs","javascript","js","search"],"created_at":"2024-12-05T00:13:04.713Z","updated_at":"2024-12-05T00:13:05.227Z","avatar_url":"https://github.com/jcoreio.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# find-cycle\n\n[![Build Status](https://travis-ci.org/jcoreio/find-cycle.svg?branch=master)](https://travis-ci.org/jcoreio/find-cycle)\n[![Coverage Status](https://codecov.io/gh/jcoreio/find-cycle/branch/master/graph/badge.svg)](https://codecov.io/gh/jcoreio/find-cycle)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n\nSearches for a cycle in a directed graph, and tells you the nodes in the\nfirst cycle it finds. Should work on your existing data structures\nwithout conversion, because it operates on `Iterables` and a\n`getConnectedNodes` adapter function that you provide.\n\nThe implementation is a depth-first search using a stack instead of\nrecursion, so it's not limited by the maximum call stack size.\n\n# Compatibility\n\nYour environment must support `Set`, `Map`, and `Symbol.iterator`\nnatively or via a polyfill.\n\n**Node**: 4+\n\n# Installation\n\n```sh\nnpm install --save find-cycle\n```\n\n# API\n\n## `findDirectedCycle(startNodes, getConnectedNodes)`\n\n```js\nconst findDirectedCycle = require('find-cycle/directed')\n```\n\n### Arguments\n\n#### `startNodes: Iterable\u003cNode\u003e`\n\nThe nodes to start the search from. Your nodes may be of any primitive\nor object type besides `null` or `undefined`.\n\n#### `getConnectedNodes: (node: Node) =\u003e ?(Iterator\u003cNode\u003e | Iterable\u003cNode\u003e)`\n\nGiven a node in your directed graph, return the nodes connected to it as\nan `Iterator` or `Iterable`. You may return `null` or `undefined` if\nthere are no connected nodes.\n\n### Returns: `?Array\u003cNode\u003e`\n\nAn array of nodes in the first cycle found, if any, including each node\nin the cycle only once.\n\n## Examples\n\n### With Arrays\n\n```js\nconst findCycle = require('find-cycle/directed')\n\nconst edges = {\n  1: [2],\n  2: [3],\n  3: [4],\n  4: [2, 5],\n  5: [3],\n  7: [8, 9],\n  8: [1],\n  9: [10, 11],\n  10: [11],\n  11: [9, 8],\n}\n\nconst startNodes = [1]\nconst getConnectedNodes = (node) =\u003e edges[node]\n\nexpect(findCycle(startNodes, getConnectedNodes)).to.deep.equal([2, 3, 4])\n```\n\n### With Sets/Maps\n\n```js\nconst findCycle = require('find-cycle/directed'\nconst edges = new Map([\n  [1, new Set([2])],\n  [2, new Set([3])],\n  [3, new Set([4])],\n  [4, new Set([2, 5])],\n  [5, new Set([3])],\n  [7, new Set([8, 9])],\n  [8, new Set([1])],\n  [9, new Set([10, 11])],\n  [10, new Set([11])],\n  [11, new Set([9, 8])],\n])\n\nconst startNodes = new Set([1])\nconst getConnectedNodes = node =\u003e edges.get(node)\n\nexpect(findCycle(startNodes, getConnectedNodes)).to.deep.equal([2, 3, 4])\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Ffind-cycle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjcoreio%2Ffind-cycle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Ffind-cycle/lists"}