使用 igraph:由 decompose.graph() 构建的组件的社区成员资格

发布于 2025-01-04 12:29:48 字数 934 浏览 1 评论 0原文

我非常感谢使用 decompose.graph、来自 igraphlapply 的社区检测功能的帮助。

我有一个 igraph 对象 G,其顶点属性“标签”和边缘属性“权重”。我想使用 igraph 中的不同函数来计算社区成员资格,为简单起见,将其设为 walktrap.community

该图没有连接,这就是为什么我决定分解它 进入连接的组件并在每个组件上运行 walktrap.community ,然后将社区成员资格顶点属性添加到原始图 G 中。

我当前正在执行以下操作

comps <- decompose.graph(G,min.vertices=2)
communities <- lapply(comps,walktrap.community)

此时我陷入困境,因为我得到了列表具有我无法弄清楚的结构的对象。 decompose.graph 上的文档只告诉它返回列表对象,当我对结果使用 lapply 时,我完全感到困惑。此外,每个组件中的社区从 0 开始编号,我不知道如何将 weights 参数提供给 walktrap.community 函数。

如果不是这些组件,我会执行以下操作:

wt <- walktrap.community(G, modularity=TRUE, weights=E(G)$weight)
wmemb <- community.to.membership(G, wt$merges,steps=which.max(wt$modularity)-1)
V(G)$"walktrap" <- wmemb$membership

有人可以帮我解决这个问题吗?或者提供一些 有帮助的信息/链接吗?

I would appreciate help with using decompose.graph, community detection functions from igraph and lapply.

I have an igraph object G with vertex attribute "label" and edge attribute "weight". I want to calculate community memberships using different functions from igraph, for simplicity let it be walktrap.community.

This graph is not connected, that is why I decided to decompose it
into connected components and run walktrap.community on each component, and afterwards add a community membership vertex attribute to the original graph G.

I am doing currently the following

comps <- decompose.graph(G,min.vertices=2)
communities <- lapply(comps,walktrap.community)

At this point I get stuck since I get the list object with the structure I cannot figure out. The documentation on decompose.graph tells only that it returns list object, and when I use lapply on the result I get completely confused. Moreover, the communities are numbered from 0 in each component, and I don't know how to supply weights parameter into walktrap.community function.

If it were not for the components, I would have done the following:

wt <- walktrap.community(G, modularity=TRUE, weights=E(G)$weight)
wmemb <- community.to.membership(G, wt$merges,steps=which.max(wt$modularity)-1)
V(G)$"walktrap" <- wmemb$membership

Could anyone please help me solve this issue? Or provide some
information/links which could help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷不放纵 2025-01-11 12:29:48

您可以使用循环:

library(igraph)
set.seed(2)
G <- erdos.renyi.game(100, 1/50)
comps <- decompose.graph(G,min.vertices=2)
length(comps)  # 2 components, in this example
for(i in seq_along(comps)) { # For each subgraph comps[[i]]
  wt <- walktrap.community(comps[[i]], modularity=TRUE, weights=E(comps[[i]])$weight)
  wmemb <- community.to.membership(comps[[i]], wt$merges,steps=which.max(wt$modularity)-1)
  V(comps[[i]])$"walktrap" <- wmemb$membership
}

可以使用 lapplymapply 来实现,但可读性较差。

comps <- decompose.graph(G,min.vertices=2)
wt <- lapply( comps, function(u)
  walktrap.community(u, modularity=TRUE, weights=E(u)$weight)
)
wmemb <- mapply( 
  function(u,v) community.to.membership(u, v$merges,steps=which.max(v$modularity)-1),
  comps, wt,
  SIMPLIFY=FALSE
)
comps <- mapply( 
  function(u,v) { V(u)$"walktrap" <- v$membership; u },
  comps, wmemb,
  SIMPLIFY=FALSE
)

You could use a loop:

library(igraph)
set.seed(2)
G <- erdos.renyi.game(100, 1/50)
comps <- decompose.graph(G,min.vertices=2)
length(comps)  # 2 components, in this example
for(i in seq_along(comps)) { # For each subgraph comps[[i]]
  wt <- walktrap.community(comps[[i]], modularity=TRUE, weights=E(comps[[i]])$weight)
  wmemb <- community.to.membership(comps[[i]], wt$merges,steps=which.max(wt$modularity)-1)
  V(comps[[i]])$"walktrap" <- wmemb$membership
}

It is possible to do it with lapply and mapply, but it is less readable.

comps <- decompose.graph(G,min.vertices=2)
wt <- lapply( comps, function(u)
  walktrap.community(u, modularity=TRUE, weights=E(u)$weight)
)
wmemb <- mapply( 
  function(u,v) community.to.membership(u, v$merges,steps=which.max(v$modularity)-1),
  comps, wt,
  SIMPLIFY=FALSE
)
comps <- mapply( 
  function(u,v) { V(u)$"walktrap" <- v$membership; u },
  comps, wmemb,
  SIMPLIFY=FALSE
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文