# Graph induced subgraph

`graph-induced-subgraph` · version 1.0.0 · Graphs & scheduling · free, no key needed

Return the vertex-induced subgraph on a listed node subset, preserving declared node and edge order.

**Use when you need to: graph induced subgraph · induced subgraph of listed nodes · restrict graph to a node subset.**

## Supported

- graph induced subgraph
- induced subgraph of listed nodes
- restrict graph to a node subset

## Not supported

- edge-induced subgraph
- graph contraction
- identify or merge vertices
- undirected subgraph mode

## Behavior

- Input is a directed simple graph plus keep, an array of unique node ids that already appear in nodes; empty keep is allowed.
- Output nodes are the members of keep listed in original declared nodes order, not keep-array order and not lexicographic id order.
- Output edges are the original edges whose from and to are both in keep, in original relative edges order.
- Self-loops remain when the looped node is kept. Cycles are allowed.
- Unknown input fields, duplicate keep ids, and keep ids missing from nodes are rejected.

## 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
- `keep` (array of string, required): max items 2000; each min length 1; each max length 256

## Output

- `nodes` (array of string, required)
- `edges` (array of object, required)

## Limits

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

## Example

Request input:

```json
{
  "nodes": [
    "compile",
    "test",
    "lint",
    "package",
    "deploy"
  ],
  "edges": [
    {
      "from": "compile",
      "to": "test"
    },
    {
      "from": "compile",
      "to": "lint"
    },
    {
      "from": "test",
      "to": "package"
    },
    {
      "from": "lint",
      "to": "package"
    },
    {
      "from": "package",
      "to": "deploy"
    }
  ],
  "keep": [
    "package",
    "compile",
    "test"
  ]
}
```

Response:

```json
{
  "result": {
    "nodes": [
      "compile",
      "test",
      "package"
    ],
    "edges": [
      {
        "from": "compile",
        "to": "test"
      },
      {
        "from": "test",
        "to": "package"
      }
    ]
  }
}
```

## How to call it

### MCP

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

```json
{
  "id": "graph-induced-subgraph",
  "version": "1.0.0",
  "input": {
    "nodes": [
      "compile",
      "test",
      "lint",
      "package",
      "deploy"
    ],
    "edges": [
      {
        "from": "compile",
        "to": "test"
      },
      {
        "from": "compile",
        "to": "lint"
      },
      {
        "from": "test",
        "to": "package"
      },
      {
        "from": "lint",
        "to": "package"
      },
      {
        "from": "package",
        "to": "deploy"
      }
    ],
    "keep": [
      "package",
      "compile",
      "test"
    ]
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/graph-induced-subgraph/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"nodes":["compile","test","lint","package","deploy"],"edges":[{"from":"compile","to":"test"},{"from":"compile","to":"lint"},{"from":"test","to":"package"},{"from":"lint","to":"package"},{"from":"package","to":"deploy"}],"keep":["package","compile","test"]}'
```

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

### CLI

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

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

## Related tools

- [Graph ancestors](/tools/graph-ancestors): List every ancestor of given targets, including the targets, in reverse-graph BFS discovery 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 dependency impact](/tools/graph-dependency-impact): List nodes downstream of a removed set: not themselves removed, reachable by a directed path of length at least 1.
- [Graph shortest unweighted path](/tools/graph-shortest-unweighted-path): Find a shortest directed path by fewest edges from source to target.
- [Graph to adjacency](/tools/graph-to-adjacency): Convert a directed simple graph into per-node outgoing adjacency lists.
- [Graph find cycle](/tools/graph-find-cycle): Find one directed cycle by 3-color DFS, or report that the graph is acyclic.
