---
title: Run Graph Algorithms API
slug: run-graph-algorithms
url: https://apyhub.com/modernsoftware-pro/service/run-graph-algorithms
provider: ModernSoftware.pro
categories: [Model Kit]
tags: [graph-traversal, shortest-path, topological-sort, minimum-spanning-tree, distance-matrix, graph-analysis, graphs, algorithms]
auth: api_key
version: 1.0
service_type: sync
endpoints: 6
atoms: 30-100
mcp: true
---

# Run Graph Algorithms API

Run BFS, DFS, shortest path, topological sort, minimum spanning tree, and distance matrix calculations on a graph described by vertices and edges.

## Endpoints

| Method | URL | Description | Atoms |
| --- | --- | --- | --- |
| POST | `https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/breadth-first-search` | What it does Performs a breadth-first search traversal on the provided graph starting from the spec… | 30 |
| POST | `https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/depth-first-search` | What it does Performs a depth-first search traversal on the provided graph, starting from the verte… | 30 |
| POST | `https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/topological-sort` | What it does Returns a topological ordering of the vertices in a directed graph. You send the graph… | 50 |
| POST | `https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/shortest-path` | What it does Finds the shortest path in a directed graph from the start vertex, using the graph pro… | 100 |
| POST | `https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/minimum-spanning-tree` | What it does Finds the minimum spanning tree for the graph you provide and returns the selected tre… | 50 |
| POST | `https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/distance-matrix` | What it does Computes the distance matrix for the graph provided in the request body. The response… | 100 |

## Endpoint reference

### Breadth-first search traversal through all vertices in a graph.

`POST https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/breadth-first-search` · 30 atoms · accepts `application/json` · returns `application/json`

| Parameter | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `graph.type` | body | string | yes | Graph type: `directed` - edges have an direction from vertex A to vertex B that the algorithm must follow. `undirected` - edges do not have a direction. One of: directed, undirected. |
| `graph.edges` | body | array of object | yes | List of weighted edges between graph vertices, without duplicates. For directed graphs the order of vertices matters, A->B and B->A count as separate edges. |
| `graph.edges[].f` | body | string | yes | One of the two vertex ID-s connected with an edge. For directed graphs: the starting vertex 'from'. |
| `graph.edges[].t` | body | string | yes | One of the two vertex ID-s connected with an edge. For directed graphs: the ending vertex 'to'. |
| `graph.edges[].weight` | body | integer | no | Edge weight (unused in this algorithm) Default: `1`. |
| `graph.vertices` | body | array of object | yes | List of up to 1000 vertices comprising the graph. |
| `graph.vertices[].id` | body | string | yes | Unique ID (1-40 characters, letters, digits, underscores, colons, hyphens) |
| `start` | query | string | yes | Start vertex ID |

#### Quickstart

Run a breadth-first search from the `start` vertex on the graph you send in the JSON body.

```bash
curl -X POST "https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/breadth-first-search?start=home" \
  -H "apy-token: $APY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "graph": {
      "type": "undirected",
      "vertices": [
        { "id": "home" },
        { "id": "about" },
        { "id": "contact" }
      ],
      "edges": [
        { "f": "home", "t": "about" },
        { "f": "about", "t": "contact" }
      ]
    }
  }'
```

#### What you'll get back

Returns a JSON object with a `start` string and a `visited` array of strings. `start` is the starting vertex you supplied, and `visited` lists the vertices visited by the breadth-first search.

```json
{
  "start": "home",
  "visited": ["home", "about", "contact"]
}
```

### Depth-first search traversal through all vertices in a graph.

`POST https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/depth-first-search` · 30 atoms · accepts `application/json` · returns `application/json`

| Parameter | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `graph.type` | body | string | yes | Graph type: `directed` - edges have an direction from vertex A to vertex B that the algorithm must follow. `undirected` - edges do not have a direction. One of: directed, undirected. |
| `graph.edges` | body | array of object | yes | List of weighted edges between graph vertices, without duplicates. For directed graphs, the order of vertices matters, A->B and B->A count as separate edges. |
| `graph.edges[].f` | body | string | yes | One of the two vertex ID-s connected with an edge. For directed graphs: the starting vertex 'from'. |
| `graph.edges[].t` | body | string | yes | One of the two vertex ID-s connected with an edge. For directed graphs: the ending vertex 'to'. |
| `graph.edges[].weight` | body | integer | no | Edge weight (unused in this algorithm) Default: `1`. |
| `graph.vertices` | body | array of object | yes | List of up to 1000 vertices comprising the graph. |
| `graph.vertices[].id` | body | string | yes | Unique ID (1-40 characters, letters, digits, underscores, colons, hyphens) |
| `start` | query | string | yes | Start vertex ID |

#### Quickstart

Run a depth-first search starting from the `start` vertex on the graph you send in the JSON body.

```bash
curl -X POST "https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/depth-first-search?start=A" \
  -H "apy-token: $APY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"graph":{"type":"directed","vertices":[{"id":"A"},{"id":"B"},{"id":"C"}],"edges":[{"f":"A","t":"B"},{"f":"B","t":"C"}]}}'
```

#### What you'll get back

Returns a JSON object with a `start` string and a `visited` array of strings. `start` echoes the starting vertex, and `visited` lists the vertices reached by the depth-first search.

```json
{
  "start": "A",
  "visited": ["A", "B", "C"]
}
```

### Topological sort of the vertices in a directed, acyclic graph.

`POST https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/topological-sort` · 50 atoms · accepts `application/json` · returns `application/json`

| Parameter | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `graph.type` | body | string | yes | This algorithm works only on `directed` graphs, where edges have a direction from one vertex to another. One of: directed. |
| `graph.edges` | body | array of object | yes | List of weighted edges between graph vertices, without duplicates. The order of vertices matters, A->B and B->A count as separate edges. |
| `graph.edges[].f` | body | string | yes | One of the two vertex ID-s connected with an edge. For directed graphs: the starting vertex 'from'. |
| `graph.edges[].t` | body | string | yes | One of the two vertex ID-s connected with an edge. For directed graphs: the ending vertex 'to'. |
| `graph.edges[].weight` | body | integer | no | Edge weight (unused in this algorithm) Default: `1`. |
| `graph.vertices` | body | array of object | yes | List of up to 1000 vertices comprising the graph. |
| `graph.vertices[].id` | body | string | yes | Unique ID (1-40 characters, letters, digits, underscores, colons, hyphens) |

#### Quickstart

This example sorts a small directed graph topologically.

```bash
curl -X POST "https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/topological-sort" \
  -H "apy-token: $APY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"graph":{"type":"directed","vertices":[{"id":"A"},{"id":"B"},{"id":"C"}],"edges":[{"f":"A","t":"B"},{"f":"B","t":"C"}]}}'
```

#### What you'll get back

Returns a JSON object with an `order` array of strings — the topological ordering of the graph's vertex IDs.

```json
{
  "order": ["A", "B", "C"]
}
```

### Finds the shortest path between vertices in a directed graph.

`POST https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/shortest-path` · 100 atoms · accepts `application/json` · returns `application/json`

| Parameter | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `end` | query | string | no | End vertex ID (if provided, the algorithm finds the shortest path only to that vertex) |
| `graph.type` | body | string | yes | This algorithm works only on `directed` graphs, where edges have a direction from one vertex to another. One of: directed. |
| `graph.edges` | body | array of object | yes | List of weighted edges between graph vertices, without duplicates. For directed graphs, the order of vertices matters, A->B and B->A count as separate edges. |
| `graph.edges[].f` | body | string | yes | One of the two vertex ID-s connected with an edge. For directed graphs: the starting vertex 'from'. |
| `graph.edges[].t` | body | string | yes | One of the two vertex ID-s connected with an edge. For directed graphs: the ending vertex 'to'. |
| `graph.edges[].weight` | body | integer | no | Edge weight - the cost of following the given edge. Default: `1`. |
| `graph.vertices` | body | array of object | yes | List of up to 1000 vertices comprising the graph. |
| `graph.vertices[].id` | body | string | yes | Unique ID (1-40 characters, letters, digits, underscores, colons, hyphens) |
| `start` | query | string | yes | Start vertex ID |

#### Quickstart

Find the shortest path from one start vertex to another in a directed graph.

```bash
curl -X POST "https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/shortest-path?start=vertex_a&end=vertex_b" \
  -H "apy-token: $APY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "graph": {
      "type": "directed",
      "vertices": [
        { "id": "vertex_a" },
        { "id": "vertex_b" },
        { "id": "vertex_c" }
      ],
      "edges": [
        { "f": "vertex_a", "t": "vertex_c", "weight": 2 },
        { "f": "vertex_c", "t": "vertex_b", "weight": 3 }
      ]
    }
  }'
```

#### What you'll get back

Returns a JSON object in one of two shapes. For a single destination, it includes `start`, `end`, `reachable` as a boolean, `distance` as an integer or `null`, and `path` as an array of vertex IDs or `null`.

```json
{
  "start": "vertex_a",
  "end": "vertex_b",
  "reachable": true,
  "distance": 5,
  "path": ["vertex_a", "vertex_c", "vertex_b"]
}
```

If no `end` is provided, it returns a JSON object with `start` and a `paths` object mapping each reachable vertex ID to an object containing `distance` and `path`.

### Finds the minimum spanning tree covering all vertices in a graph.

`POST https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/minimum-spanning-tree` · 50 atoms · accepts `application/json` · returns `application/json`

| Parameter | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `graph.type` | body | string | yes | Graph type: `directed` - edges have an direction from vertex A to vertex B that the algorithm must follow. `undirected` - edges do not have a direction. One of: directed, undirected. |
| `graph.edges` | body | array of object | yes | List of weighted edges between graph vertices, without duplicates. For directed graphs, the order of vertices matters, A->B and B->A count as separate edges. |
| `graph.edges[].f` | body | string | yes | One of the two vertex ID-s connected with an edge. For directed graphs: the starting vertex 'from'. |
| `graph.edges[].t` | body | string | yes | One of the two vertex ID-s connected with an edge. For directed graphs: the ending vertex 'to'. |
| `graph.edges[].weight` | body | integer | no | Edge weight - the 'cost' of following the given edge. Default: `1`. |
| `graph.vertices` | body | array of object | yes | List of up to 1000 vertices comprising the graph. |
| `graph.vertices[].id` | body | string | yes | Unique ID (1-40 characters, letters, digits, underscores, colons, hyphens) |

#### Quickstart

Find a minimum spanning tree for a simple weighted graph.

```bash
curl -X POST "https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/minimum-spanning-tree" \
  -H "apy-token: $APY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "graph": {
      "type": "undirected",
      "vertices": [
        { "id": "A" },
        { "id": "B" },
        { "id": "C" }
      ],
      "edges": [
        { "f": "A", "t": "B", "weight": 1 },
        { "f": "C", "t": "B", "weight": 2 },
        { "f": "A", "t": "C", "weight": 3 }
      ]
    }
  }'
```

#### What you'll get back

Returns a JSON object with an `edges` array and a `totalWeight` integer. `edges` contains the selected tree edges, each with `f`, `t`, and optionally `weight`; `totalWeight` is the sum of the chosen edge weights.

```json
{
  "edges": [
    { "f": "A", "t": "B", "weight": 1 },
    { "f": "C", "t": "B", "weight": 2 }
  ],
  "totalWeight": 3
}
```

### Computes the distance matrix between all pairs of vertices in a graph.

`POST https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/distance-matrix` · 100 atoms · accepts `application/json` · returns `application/json`

| Parameter | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `graph.type` | body | string | yes | Graph type: `directed` - edges have an direction from vertex A to vertex B that the algorithm must follow. `undirected` - edges do not have a direction. One of: directed, undirected. |
| `graph.edges` | body | array of object | yes | List of weighted edges between graph vertices, without duplicates. For directed graphs, the order of vertices matters, A->B and B->A count as separate edges. |
| `graph.edges[].f` | body | string | yes | One of the two vertex ID-s connected with an edge. For directed graphs: the starting vertex 'from'. |
| `graph.edges[].t` | body | string | yes | One of the two vertex ID-s connected with an edge. For directed graphs: the ending vertex 'to'. |
| `graph.edges[].weight` | body | integer | no | Edge weight - the 'cost' of following the given edge Default: `1`. |
| `graph.vertices` | body | array of object | yes | List of up to 384 vertices (for directed graphs) or 512 vertices (for undirected graphs) that comprise the graph. |
| `graph.vertices[].id` | body | string | yes | Unique ID (1-40 characters, letters, digits, underscores, colons, hyphens) |

#### Quickstart

Send a graph in the request body to calculate its distance matrix.

```bash
curl -X POST "https://api.eu.apyhub.com/modernsoftware-pro/run-graph-algorithms/distance-matrix" \
  -H "apy-token: $APY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"graph":{"type":"directed","vertices":[{"id":"A"},{"id":"B"}],"edges":[{"f":"A","t":"B","weight":1}]}}'
```

#### What you'll get back

Returns a JSON object with either a `distance-matrix-asymmetric` object or a `distance-matrix-symmetric` object. Each of those is an object whose keys are vertex IDs and whose values are arrays of integers representing distances.

```json
{
  "distance-matrix-asymmetric": {
    "A": [0, 1],
    "B": [-1, 0]
  }
}
```

## About

## What it does
Graph Algorithms gives you traversal, path, and graph-structure utilities for directed and undirected graphs. Send a graph with `vertices` and `edges`, then use the specific algorithm endpoint to get the result back in a compact JSON shape.

Use breadth-first search or depth-first search when you need the visit order from a chosen `start` vertex. `Breadth-first search` returns `{ start, visited }`, while `Depth-first search` returns the same response shape with a different traversal strategy. For directed graphs, both follow edge direction; for undirected graphs, edges are treated as bidirectional.

Use `Topological sort` on a directed graph when you need an ordering of vertices in a DAG. It returns `{ order }`, which is useful for dependency resolution, build pipelines, task scheduling, and other workflows where prerequisites must come before dependents.

For weighted directed graphs, `Shortest path` returns either a single `{ start, end, reachable, distance, path }` result when you supply `end`, or a `{ start, paths }` map of distances and paths to all reachable vertices. `Minimum spanning tree` returns the selected `edges` and `totalWeight` for a spanning tree covering the graph, and `Distance matrix` returns a symmetric or asymmetric matrix depending on whether the graph is undirected or directed. These endpoints fit graph analysis, routing logic, and operations planning where you need explicit paths or pairwise distances.

## Usage

Base URL: `https://api.eu.apyhub.com` (default region — see
`GET https://apyhub.com/api/public/regions` for the rest).

Authenticate with an ApyHub API key in the `apy-token` header.
Full docs and a live playground: https://apyhub.com/modernsoftware-pro/service/run-graph-algorithms
