---
title: "Preferential Attachment"
component: "graph-ml"
version: "3.10"
module: "link-prediction"
html_url: "/graph-ml/3.10/link-prediction/preferential-attachment"
canonical_url: "https://www.tigergraph.com/docs/graph-ml/3.10/link-prediction/preferential-attachment"
markdown_url: "https://www.tigergraph.com/docs/graph-ml/3.10/link-prediction/preferential-attachment.md"
---

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

# Preferential Attachment

Supported Graph Characteristics

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

Algorithm Link: [Preferential Attachment](https://github.com/tigergraph/gsql-graph-algorithms/tree/master/algorithms/Topological%20Link%20Prediction/preferential%5Fattachment)

Preferential Attachment is a measure to compute the closeness of vertices based on the number of their neighbors. The algorithm returns the product of the number of neighbors of the first vertex and the number of neighbors of the second vertex.

For more information, see [Preferential Attachment](https://en.wikipedia.org/wiki/Preferential%5Fattachment).

## Notes

Preferential attachment as a metric works best on graphs that have an "unequal" structure where a majority of vertices have few connections and a minority of vertices have many connections.

This algorithm ignores edge weights.

## Specifications

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

### Parameters

| Name | Description | Data type |
| --- | --- | --- |
| `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. | (A blank set of strings) |

### Time complexity

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

## Return value

The product of the number of neighbors 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) 

Since Dan has four neighbors, while Jenny has three, the return value of the algorithm is \\(3\*4=12\\).

* Query
* Result

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

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