# Calculate probability that nodes between two communities link to each other

**URL:** https://igraph.discourse.group/t/calculate-probability-that-nodes-between-two-communities-link-to-each-other/1202
**Category:** Usage
**Tags:** R
**Created:** [27 April 2022 20:02 UTC](https://igraph.discourse.group/t/calculate-probability-that-nodes-between-two-communities-link-to-each-other/1202 "2022-04-27T20:02:29Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Alexey\_Didorenko](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/alexey_didorenko/32/483_2.png) [@Alexey\_Didorenko](https://igraph.discourse.group/u/Alexey_Didorenko)
#### Post date: [27 April 2022 20:02 UTC](https://igraph.discourse.group/t/calculate-probability-that-nodes-between-two-communities-link-to-each-other/1202/1 "2022-04-27T20:02:29Z")

</div>

I am new in Network Science.  
I have graph with community structure.  
How can I calculate probability that nodes from community i are connected with nodes from community j?  
Using igraph, of course.

---

<div class="post-metadata">

### Author: ![bagginstyrone](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/bagginstyrone/32/477_2.png) [@bagginstyrone](https://igraph.discourse.group/u/bagginstyrone)
#### Post date: [17 May 2022 08:05 UTC](https://igraph.discourse.group/t/calculate-probability-that-nodes-between-two-communities-link-to-each-other/1202/2 "2022-05-17T08:05:26Z")

</div>

> [@Alexey\_Didorenko](#):
>
> How can I calculate probability that nodes from community i are connected with nodes from community j?

Have you found the solution to your problem? Your problem is old and maybe you have figured out something beneficial for the rest of us.

---

<div class="post-metadata">

### Author: ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)
#### Post date: [23 May 2022 07:57 UTC](https://igraph.discourse.group/t/calculate-probability-that-nodes-between-two-communities-link-to-each-other/1202/3 "2022-05-23T07:57:07Z")

</div>

> [@Alexey\_Didorenko](#):
>
> How can I calculate probability that nodes from community i are connected with nodes from community j?

It is not entirely clear what you are looking for here. Possibly, you are simply interested in the proportion of links of community i that connect to community j? That is, if in total community i has K\_i links and A\_{ij} links between community i and j, you are looking for \frac{A\_{ij}}{K\_i}?

One you have the result of one of the `cluster_xxx` functions, you get a result of a `communities` structure. You can then contract the graph, so that each cluster becomes a new node, and use that to count things.

Suppose that the clustering is available from `cl`. You can then contract the graph as

```R
G_contracted <- contract(G, cl$membership)

```

In the contracted graph, there will only be as many nodes as there are clusters in `cl`, that is,`length(cl)`. In this contracted graph you can get the adjacency matrix by doing

```R
A <- as_adjacency_matrix(G_contracted)

```

Doing this on the `graph.famous('Zachary')` graph, with some clustering result, you obtain for instance this result

```R
> A
3 x 3 sparse Matrix of class "dgCMatrix"
            
[1,] 24 4 10
[2,] 4 6 .
[3,] 10 . 34

```

Now it is simply a matter of dividing this matrix by the row sums.

```R
> A / Matrix::rowSums(A)
3 x 3 sparse Matrix of class "dgCMatrix"
                                  
[1,] 0.6315789 0.1052632 0.2631579
[2,] 0.4000000 0.6000000 .        
[3,] 0.2272727 . 0.7727273

```

---

<div class="post-metadata">

### Author: ![bagginstyrone](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/bagginstyrone/32/477_2.png) [@bagginstyrone](https://igraph.discourse.group/u/bagginstyrone)
#### Post date: [26 May 2022 11:06 UTC](https://igraph.discourse.group/t/calculate-probability-that-nodes-between-two-communities-link-to-each-other/1202/4 "2022-05-26T11:06:02Z")

</div>

@vtraag Pretty useful answer. Let me see what results I get after following this method.
