---
title: "Total Neighbors"
component: "graph-ml"
version: "3.10"
module: "link-prediction"
html_url: "/graph-ml/3.10/link-prediction/total-neighbors"
canonical_url: "https://www.tigergraph.com/docs/graph-ml/3.10/link-prediction/total-neighbors"
markdown_url: "https://www.tigergraph.com/docs/graph-ml/3.10/link-prediction/total-neighbors.md"
---

[View as HTML](https://www.tigergraph.com/docs/graph-ml/3.10/link-prediction/total-neighbors)

# Total Neighbors

Supported Graph Characteristics

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

Algorithm link: [Total Neighbors](https://github.com/tigergraph/gsql-graph-algorithms/tree/master/algorithms/Topological%20Link%20Prediction/total%5Fneighbors)

The algorithm counts the total number of neighbors, or vertices connected by one hop, of two vertices.

## Notes

This algorithm ignores edge weights.

## Specifications

```gsql
CREATE QUERY tg_total_neighbors(VERTEX v_source VERTEX v_target,
    SET<STRING> e_type)
```

### Parameters

| Name | Description | Default value |
| --- | --- | --- |
| `VERTEX v_source` | The first vertex to compare. Provide the vertex ID and type as a tuple: `("id","type")` | N/A |
| `VERTEX v_target` | The second vertex to compare with the first. Provide the vertex ID and type as a tuple: `("id","type")` | N/A |
| `SET<STRING> e_type_set` | Edge types to traverse. | (an empty set of strings) |

### Output

The total number of neighbors of two vertices as a `closeness` value.

### Time complexity

The algorithm has a time complexity of \\$O(D1 + D2)\\$, where \\$D1\\$ and \\$D2\\$ are the degrees of the two vertices.

## Example

Suppose we have the following graph.

![preferential attachment ex](https://www.tigergraph.com/docs/graph-ml/3.10/link-prediction/_images/preferential-attachment-ex.png) 

Dan and Jenny together have 6 neighbors in total.

Dan's neighbors are:

* Nancy
* Kevin
* Tom
* Jenny

and Jenny's neighbors are:

* Dan
* Tom
* Amily

Running the algorithm with input vertices of `Dan` and `Jenny` would give us a result of `6` because the connection Dan - Jenny and Jenny - Dan is not double-counted.

* Query
* Result

```gsql
RUN QUERY tg_total_neighbors (("Jenny", "person"), ("Dan", "person"), ["friendship"])
```

```json
{
  "error": false,
  "message": "",
  "version": {
    "schema": 2,
    "edition": "enterprise",
    "api": "v2"
  },
  "results": [{"closeness": 6}]
}
```
