---
title: "Same Community"
component: "graph-ml"
version: "3.10"
module: "link-prediction"
html_url: "/graph-ml/3.10/link-prediction/same-community"
canonical_url: "https://www.tigergraph.com/docs/graph-ml/3.10/link-prediction/same-community"
markdown_url: "https://www.tigergraph.com/docs/graph-ml/3.10/link-prediction/same-community.md"
---

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

# Same Community

Supported Graph Characteristics

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

Algorithm link: [Same Community](https://github.com/tigergraph/gsql-graph-algorithms/tree/master/algorithms/Topological%20Link%20Prediction/same%5Fcommunity)

The Same Community algorithm is a simple boolean algorithm to check whether two vertices share the same community labeling.

This algorithm takes two vertices as input, returning 1 if the two vertices are in the same community, and returning 0 if they are not in the same community.

## Notes

The algorithm assumes that a community ID is stored in an integer attribute on each vertex. For this to happen, usually a [Community Detection](https://www.tigergraph.com/docs/graph-ml/3.10/community-algorithms/index.md) algorithm is run to create such community IDs.

This algorithm ignores edge weights.

## Specifications

```gsql
CREATE QUERY tg_same_community(VERTEX v_source VERTEX v_target, STRING
    communityAttribute)
```

### 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 |
| `STRING communityAttribute` | The vertex attribute used to store a vertex's community ID. | (A blank string) |

### Output

* Returns `1` if the two vertices are in the same community.
* Returns `0` if the two vertices are not in the same community.

### Time complexity

The algorithm has a time complexity of \\$O(1)\\$.

## Example

Suppose we have the following vertices:

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

Their community IDs were generated by running the [Weakly Connected Components](https://www.tigergraph.com/docs/graph-ml/3.10/community-algorithms/connected-components.md) algorithm on the graph. If we run the algorithm between Kevin and Jenny, we get 1 because the two vertices are in the same community as indicated by their `community` attribute:

* Query
* Result

```gsql
RUN QUERY tg_same_community (("Jenny", "person"), ("Kevin", "person"), "community")
```

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