# Graph find cycle

`graph-find-cycle` · version 1.0.0 · Graphs & scheduling · free, no key needed

Find one directed cycle by 3-color DFS, or report that the graph is acyclic.

**Use when you need to: graph find cycle · find a directed cycle · report one directed cycle.**

## Supported

- graph find cycle
- find a directed cycle
- report one directed cycle

## Not supported

- listing every cycle
- enumerating all simple cycles
- undirected cycles
- cycle basis
- topological sort

## Behavior

- Input is a directed simple graph: unique node ids and unique {from,to} edges whose endpoints are in nodes.
- DFS iterates candidate roots in declared node index order 0..n-1 and skips already finished (black) nodes.
- From a node, outgoing neighbors are walked in declared edge order.
- Nodes are 3-colored white, gray, and black. The first back-edge to a gray node defines the reported cycle.
- The cycle lists node ids starting at that back-edge target, continuing along the gray stack, then repeating the start.
- The cycle is not rotated to the minimum declared index. Discovery order is the contract.
- A self-loop is a cycle of the form [id, id]. Every reported cycle has length at least 2.
- An empty graph is not cyclic. A DAG is not cyclic: cyclic is false and cycle is [].

## Input

- `nodes` (array of string, required): max items 2000; each min length 1; each max length 256
- `edges` (array of object, required): max items 10000

## Output

- `cyclic` (boolean, required)
- `cycle` (array of string, required)

## Limits

- max nodes: 2000
- max edges: 10000
- max node id bytes: 256

## Example

Request input:

```json
{
  "nodes": [
    "a",
    "b",
    "c"
  ],
  "edges": [
    {
      "from": "a",
      "to": "b"
    },
    {
      "from": "b",
      "to": "c"
    },
    {
      "from": "c",
      "to": "a"
    }
  ]
}
```

Response:

```json
{
  "result": {
    "cyclic": true,
    "cycle": [
      "a",
      "b",
      "c",
      "a"
    ]
  }
}
```

## How to call it

### MCP

Connect `https://computefirst.net/mcp` ([setup](/docs#connect)), then call `execute` with:

```json
{
  "id": "graph-find-cycle",
  "version": "1.0.0",
  "input": {
    "nodes": [
      "a",
      "b",
      "c"
    ],
    "edges": [
      {
        "from": "a",
        "to": "b"
      },
      {
        "from": "b",
        "to": "c"
      },
      {
        "from": "c",
        "to": "a"
      }
    ]
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/graph-find-cycle/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"nodes":["a","b","c"],"edges":[{"from":"a","to":"b"},{"from":"b","to":"c"},{"from":"c","to":"a"}]}'
```

The machine-readable contract is at [/v1/tools/graph-find-cycle/versions/1.0.0](/v1/tools/graph-find-cycle/versions/1.0.0).

### CLI

```sh
node cli.mjs run graph-find-cycle 1.0.0 --input input.json --base-url https://computefirst.net
```

Get the client at [/clients/cli/](/clients/cli/).

## Related tools

- [Graph reverse](/tools/graph-reverse): Reverse every directed edge of a simple graph, keeping node order and original edge order.
- [Graph DAG longest path](/tools/graph-dag-longest-path): Return a longest node-weighted path in a DAG, where path weight is the sum of node durations.
- [Graph induced subgraph](/tools/graph-induced-subgraph): Return the vertex-induced subgraph on a listed node subset, preserving declared node and edge order.
- [Graph reachable from](/tools/graph-reachable-from): List nodes reachable from given sources by directed BFS, in discovery order.
- [Graph shortest unweighted path](/tools/graph-shortest-unweighted-path): Find a shortest directed path by fewest edges from source to target.
- [Graph strongly connected components](/tools/graph-strongly-connected-components): Partition a directed simple graph into strongly connected components, listed by declared node index.
