# Betweenness and communities (cluster\_edge\_betweenness)

**URL:** <https://igraph.discourse.group/t/betweenness-and-communities-cluster-edge-betweenness/1840>\
**Category:** Usage\
**Tags:** R\
**Created:** [3 August 2024 11:26 UTC](https://igraph.discourse.group/t/betweenness-and-communities-cluster-edge-betweenness/1840 "2024-08-03T11:26:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edward](https://avatars.discourse-cdn.com/v4/letter/e/90db22/32.png) [@Edward](https://igraph.discourse.group/u/Edward)\
**Post date:** [3 August 2024 11:26 UTC](https://igraph.discourse.group/t/betweenness-and-communities-cluster-edge-betweenness/1840/1 "2024-08-03T11:26:20Z")

</div>

Could someone explain how communities are determined based on R’s cluster\_edge\_betweenness function? I would like to know why, in the attached graph, p14 and p15 are grouped with the g6 community and not with the other one (g3 + g5). And how can I force these two nodes to be clustered with g3/g5? I tried using ‘weights’ but no luck.

```auto
library(igraph)
library(dplyr)

mydf <- data.frame(id=LETTERS, locus=c(rep("alpha",14), rep("beta",12)), 
     group=c(rep(1,2), rep(2,6), rep(3,6), rep(4,4), rep(5,4), rep(6,4)),
                   pair=c(1:12,14,15,3,4,6,8,9:16))

mydf <- filter(mydf, group %in% c(3,5,6))
mydf

g <- mydf %>%
  dplyr::mutate(sg = paste0("g", group), sp = paste0("p", pair)) %>%
  dplyr::select(sg, sp, locus) %>%
  graph_from_data_frame(directed=FALSE) 

g
#IGRAPH f429061 UN-- 11 14 -- 
#+ attr: name (v/c), locus (e/c)
#+ edges from f429061 (vertex names):
# [1] g3--p9 g3--p10 g3--p11 g3--p12 g3--p14 g3--p15 g5--p9 g5--p10 g5--p11 g5--p12 g6--p13
#[12] g6--p14 g6--p15 g6--p16

ceb <- cluster_edge_betweenness(g)
  
plot(ceb, g, 
     vertex.shape=c(rep("square",3), rep("circle",8)),
     col="yellow", 
     edge.color = as.factor(E(ig)$locus), 
     edge.width=2 + (E(ig)$locus=="alpha") * 2)

```

 ![image](https://global.discourse-cdn.com/free1/uploads/igraph/original/2X/5/53adff7e667fa1b3958f75e7ef5bba898a691704.png)

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [3 August 2024 12:51 UTC](https://igraph.discourse.group/t/betweenness-and-communities-cluster-edge-betweenness/1840/2 "2024-08-03T12:51:26Z")

</div>

> [@Edward](#):
>
> Could someone explain how communities are determined based on R’s cluster\_edge\_betweenness function?

There is a reference in the documentation to the original paper by Girvan and Newman, which you can look at.

This is a well-known community detection algorithm that today is usually discussed only for educational purposes. It does not do very well, in general. There is a (somewhat mediocre) Wikipedia article about how it works which you can also look at: [Girvan–Newman algorithm - Wikipedia](https://en.wikipedia.org/wiki/Girvan%E2%80%93Newman_algorithm)

> [@Edward](#):
>
> I would like to know why, in the attached graph, p14 and p15 are grouped with the g6 community and not with the other one (g3 + g5).

Because `g3--p15` and `g3--p14` are the highest betweenness edges, thus they are removed first.

> [@Edward](#):
>
> And how can I force these two nodes to be clustered with g3/g5?

If you just want to plot manually specified groups, you can enter your own membership vector.

---

<div class="post-metadata">

**Author:** ![Edward](https://avatars.discourse-cdn.com/v4/letter/e/90db22/32.png) [@Edward](https://igraph.discourse.group/u/Edward)\
**Post date:** [3 August 2024 13:16 UTC](https://igraph.discourse.group/t/betweenness-and-communities-cluster-edge-betweenness/1840/3 "2024-08-03T13:16:37Z")

</div>

Thank you szhorvat! That’s very helpful.

I was hoping to find a method that groups g3–p15 and g3–p14 in the same community as {p9, p10, p11, p12}. Can I use “weights” to reduce the betweenness for those two particular edges, or any other way?

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [3 August 2024 13:20 UTC](https://igraph.discourse.group/t/betweenness-and-communities-cluster-edge-betweenness/1840/4 "2024-08-03T13:20:02Z")

</div>

If you want to use a certain partitioning, why are you looking for a method that returns it? You already have the partitioning.

---

<div class="post-metadata">

**Author:** ![Edward](https://avatars.discourse-cdn.com/v4/letter/e/90db22/32.png) [@Edward](https://igraph.discourse.group/u/Edward)\
**Post date:** [3 August 2024 13:35 UTC](https://igraph.discourse.group/t/betweenness-and-communities-cluster-edge-betweenness/1840/5 "2024-08-03T13:35:57Z")

</div>

I know the partitioning for only those two edges. I don’t have the partitioning for all cases, that’s why I need a method to do it in general. I thought there may be a way to use edge attributes (which I have - the locus variable) to make the algorithm choose one or the other community for certain edges that fall in-between clusters. 🙂
