---
title: "All Paths (Single-Pair)"
description: "Overview of TigerGraph's implementation of the All Paths algorithm."
component: "graph-ml"
version: "3.10"
module: "pathfinding-algorithms"
html_url: "/graph-ml/3.10/pathfinding-algorithms/single-pair-all-paths"
canonical_url: "https://www.tigergraph.com/docs/graph-ml/3.10/pathfinding-algorithms/single-pair-all-paths"
markdown_url: "https://www.tigergraph.com/docs/graph-ml/3.10/pathfinding-algorithms/single-pair-all-paths.md"
---

[View as HTML](https://www.tigergraph.com/docs/graph-ml/3.10/pathfinding-algorithms/single-pair-all-paths)

# All Paths (Single-Pair)

Supported Graph Characteristics

| Unweighted edges |
| --- |
| Directed edges |
| Undirected edges |
| Homogeneous vertex types |
| Heterogeneous vertex types |

Algorithm links: [All Paths (Single-Pair)](https://github.com/tigergraph/gsql-graph-algorithms/tree/master/algorithms/Path/path%5Fbetween%5Ftwo%5Fvertices/bidirection)

This algorithm finds and prints all paths between two input vertices. This is similar to the all-paths functionality in the standard GraphStudio viewer, except that it supports output of all vertices in all paths to a file or to the console. If you are using GraphStudio and only want a fast visual display of all paths, use the GraphStudio implementation instead of this algorithm.

There are directional and bidirectional versions of this algorithm.

## Specifications

Directional

```gsql
CREATE QUERY tg_all_path(VERTEX source_v, VERTEX target_v, INT maxDepth = 10,
  BOOL print_accum = TRUE, STRING file_path = "")
```

Bidirectional

```gsql
CREATE QUERY tg_all_path_bidirection(VERTEX source_v, VERTEX target_v, SET<STRING> e_type, INT maxDepth = 3,
  BOOL print_accum = TRUE, STRING file_path = "")
```

### Parameters

| Parameter | Description | Default value |
| --- | --- | --- |
| `VERTEX source_v` | Source vertex where the paths start. Provide the vertex ID and type as a tuple: `("id","type")` | N/A |
| `VERTEX target_v` | Target vertex where the paths end. Provide the vertex ID and type as a tuple: `("id","type")` | N/A |
| `SET<STRING> e_type` | Used for the bidirectional version of the algorithm. A set of strings referring to the edge types to use. | (empty set of strings) |
| `INT maxDepth` | The maximum length of the path returned. | 10 |
| `BOOL print_accum` | If `true`, write output to the console in JSON format. | True |
| `STRING file_path` | If not empty, write output to this file. | (empty string) |

### Output

In both JSON and CSV outputs, the result is a list of vertices visited on each path.

### Time complexity

\\(O(1)-O(VE)\\), where \\(V\\) is the number of vertices and \\(E\\) is the number of edges.

### Space complexity

\\(O(1)-O(V^2E)\\)

## Example

Suppose we have a large network of consumers, products, and the contexts in which the products are used. We want to find the contexts in which products `P001` and `P002` are both used.

![single pair all paths](https://www.tigergraph.com/docs/graph-ml/3.10/pathfinding-algorithms/_images/single-pair-all-paths.png)

This image was generated from GraphStudio using the Find Paths option in the Explore Graph window and limiting the maximum depth, or number of vertices in the path, to 3.

The paths can also be output in JSON format to the console or CSV format to a file using the all-paths algorithm.

Query

```gsql
RUN QUERY tg_all_path("P001", "P002", 3, TRUE, "/home/tigergraph/output.csv")
```

* JSON
* CSV

```json
[
  {
    "path": [
      [
        "P001",
        "GW",
        "P002"
      ],
      [
        "P001",
        "T3",
        "P002"
      ],
      [
        "P001",
        "T4",
        "P002"
      ]
    ]
  }
]
```

```csv
P001,GW,P002
P001,T3,P002
P001,T4,P002
```
