计算二分 R igraph 中的 4 和 6 周期

发布于 2025-01-19 12:52:49 字数 1021 浏览 3 评论 0原文

我想计算四循环的数量和二分部分中的六循环的数量。 - 全循环“> rigraph找到所有周期,我想出了这个解决方案:

> set.seed(1)
> library(igraph)
> G <- bipartite.random.game(10,10,p=.5)  #A random bipartite
> 
> cycles <- NULL  #List all cycles up to length 6 (2-cycles, 4-cycles, and 6-cycles)
> for(v1 in V(G)) {for(v2 in neighbors(G, v1)) {cycles <- c(cycles, lapply(all_simple_paths(G, v2, v1, cutoff = 5), function(p) c(v1,p)))}}
> fourcycles <- cycles[which(sapply(cycles, length) == 5)]  #Just four-cycles
> fourcycles <- fourcycles[sapply(fourcycles, min) == sapply(fourcycles, `[`, 1)]  #Unique four-cycles
> length(fourcycles)  #Number of four-cycles
[1] 406
> 
> sixcycles <- cycles[which(sapply(cycles, length) == 7)]  #Just six-cycles
> sixcycles <- sixcycles[sapply(sixcycles, min) == sapply(sixcycles, `[`, 1)]  #Unique six-cycles
> length(sixcycles)  #Number of six-cycles
[1] 5490

它有效,但是对于稍大的图,它是不切实际的,因为可能有很多数倍的循环来枚举。有没有一种方法可以更有效地进行此操作,也许可以利用该图是两部分的事实?谢谢!

I want to count the number of four-cycles and the number of six-cycles in a bipartite igraph in R. Adapting the code in r igraph find all cycles, I've come up with this solution:

> set.seed(1)
> library(igraph)
> G <- bipartite.random.game(10,10,p=.5)  #A random bipartite
> 
> cycles <- NULL  #List all cycles up to length 6 (2-cycles, 4-cycles, and 6-cycles)
> for(v1 in V(G)) {for(v2 in neighbors(G, v1)) {cycles <- c(cycles, lapply(all_simple_paths(G, v2, v1, cutoff = 5), function(p) c(v1,p)))}}
> fourcycles <- cycles[which(sapply(cycles, length) == 5)]  #Just four-cycles
> fourcycles <- fourcycles[sapply(fourcycles, min) == sapply(fourcycles, `[`, 1)]  #Unique four-cycles
> length(fourcycles)  #Number of four-cycles
[1] 406
> 
> sixcycles <- cycles[which(sapply(cycles, length) == 7)]  #Just six-cycles
> sixcycles <- sixcycles[sapply(sixcycles, min) == sapply(sixcycles, `[`, 1)]  #Unique six-cycles
> length(sixcycles)  #Number of six-cycles
[1] 5490

It works, but is impractical for even slightly larger graphs because there are potentially exponentially many cycles to enumerate. Is there a way to do this more efficiently, maybe exploiting the fact that the graph is bipartite? Thanks!

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

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

发布评论

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

评论(1

以歌曲疗慰 2025-01-26 12:52:49

您可以使用 igraph 的亚同构函数来计算周期。例如,计算 5 个周期:

set.seed(123)
g <- sample_gnm(10,30)
> count_subgraph_isomorphisms(make_ring(5), g)
[1] 3500

这会将 5 个周期多计数 10,因为每个 5 个周期有 10 个自同构。一般来说,n 圈有 2n 个自同构。因此真正的计数是 350。

> automorphisms(make_ring(5))$group_size
[1] "10"

它也会很慢。


在新发布的 igraph 1.3.0 中,我们可以使用主题查找器功能,该功能现已扩展为可处理 5 和 6 顶点无向图。

由于基序是诱导子图,但我们正在寻找所有循环,而不仅仅是诱导循环,因此我们首先需要检查每个 5-基序有多少个 5-循环。请注意,5 个顶点上有 34 个非同构图形,您可以在 OEIS 中查找。另请注意像以前一样除以 10,以避免计数过多。

cycle5_per_motif <- sapply(0:33, function (c) count_subgraph_isomorphisms(make_ring(5), graph_from_isomorphism_class(5, c, directed=F)) / 10)

有了这些信息,我们就可以运行主题查找器并计算最终的循环计数:

> sum(motifs(g, 5) * cycle5_per_motif, na.rm=T)
[1] 350

这比使用 count_subgraph_isomorphisms 快得多,但目前最多只能处理 6 个循环。

You can count cycles using igraph's subisomorphism functions. For example, counting 5-cycles:

set.seed(123)
g <- sample_gnm(10,30)
> count_subgraph_isomorphisms(make_ring(5), g)
[1] 3500

This overcounts 5-cycles by 10 as each 5-cycle has 10 automorphisms. Generally, n-cycles have 2n automorphisms. Thus the true count is 350.

> automorphisms(make_ring(5))$group_size
[1] "10"

It will also be quite slow.


With the newly released igraph 1.3.0, we can use the motif finder functionality, which has now been extended to work with 5 and 6 vertex undirected graphs.

Since motifs are induced subgraphs, but we are looking for all cycles, not just induced ones, we first need to check how many 5-cycles each 5-motif has. Note that there are 34 non-isomorphic graphics on 5 vertices, as you can look up e.g. in OEIS. Notice also the division by 10, as before, to avoid overcounting.

cycle5_per_motif <- sapply(0:33, function (c) count_subgraph_isomorphisms(make_ring(5), graph_from_isomorphism_class(5, c, directed=F)) / 10)

With this information, we can run the motif finder and compute the final cycle count:

> sum(motifs(g, 5) * cycle5_per_motif, na.rm=T)
[1] 350

This is much faster than using count_subgraph_isomorphisms, but it only works up to 6-cycles at this moment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文