简体   繁体   English

使用igraph,网络或其他R包计算有向无环图的所有生成树

[英]Compute ALL spanning trees of a directed acyclic graph using igraph, network, or other R package

I want to compute the complete set of spanning trees for a graph. 我想为图形计算完整的生成树集。 The graphs I'm working with are small (usually less than 10 nodes). 我正在使用的图形很小(通常少于10个节点)。

I see functionality for computing the minimum spanning tree with igraph : 我看到了使用igraph计算最小生成树的功能:

library(igraph)
g <- sample_gnp(100, 3/100)
g_mst <- mst(g)

and I see a previous StackOverflow post that described how to compute a spanning tree using a breadth-first search . 并且我看到了之前的StackOverflow帖子 ,该帖子描述了如何使用广度优先搜索来计算生成树。 The code below is adapted from the accepted answer: 以下代码改编自公认的答案:

r <- graph.bfs(g, root=1, neimode='all', order = TRUE, father = TRUE)
h <- graph(rbind(r$order, r$father[r$order, na_ok = TRUE])[,-1], directed = FALSE)

However, I don't know how to adapt this to compute multiple spanning trees. 但是,我不知道如何使它适应计算多个生成树。 How would one adapt this code to compute all spanning trees? 如何修改此代码以计算所有生成树? I'm thinking that one piece of this would be to loop through each node to use as the "root" of each tree, but I don't think that takes me all the way there (since there could still be multiple spanning trees associated with a given root node). 我认为这其中的一部分将是遍历每个节点以用作每棵树的“根”,但是我不认为这会一直困扰着我(因为仍然可能存在多个关联的生成树)与给定的根节点)。

EDIT 编辑

The end-goal is to compute the distortion of a graph, which is defined as follows ( link, see page 5 ): 最终目标是计算图的失真 ,其定义如下( 链接,请参阅第5页 ):

Consider any spanning tree T on a graph G , and compute the average distance t = E[H T ] on T between any two nodes that share a link in G . 考虑在图G任何生成树T,并计算平均距离t = E [H T]T共享G中的链路的任何两个节点之间。 The distortion measures how T distorts links in G , ie it measures how many extra hops are required to go from one side of a link in G to the other, if we are restricted to using T . 失真度度量T扭曲G中的链接的方式,即,如果我们限制使用T ,则它度量从G链接的一侧到另一侧需要多少跳。 The distortion is defined [13] to be the smallest such average over all possible T s. 畸变定义为[13],在所有可能的T s上,最小的平均值。 Intuitively distortion measures how tree-like a graph is. 直观地,失真度衡量的是树状图。

[13] RGH Tagmunarunkit and S. Jamin, “Network topology generators: degree-based vs. structural,” in SIGMCOMM, 2002. [13] RGH Tagmunarunkit和S. Jamin,“网络拓扑生成器:基于程度与结构的关系”,在SIGMCOMM中,2002年。

I don't think you will find a function to do that on an R package. 我认为您不会在R包上找到执行此操作的函数。

There are n^{n-2} spanning trees on a graph (according to the Cayley's formula ). 图上有n ^ {n-2}个生成树(根据Cayley公式 )。 Even on your graph with 10 nodes, there may exist 1,000,000,000 different spanning trees, which is a big number. 即使在具有10个节点的图上,也可能存在1,000,000,000个不同的生成树,这是一个很大的数目。

Furthermore, the problem of counting or enumerating all spanning trees of a given graph is #P-Complete , which is as harder as NP-Complete problems. 此外,对给定图的所有生成树进行计数或枚举的问题是#P-Complete ,这与NP-Complete问题一样困难。

If you are really willing to do that, I recommend dropping R and start using C or C++, which can compute your problem much faster than any R code can do. 如果您真的愿意这样做,我建议您删除R并开始使用C或C ++,它们可以比任何R代码都能更快地计算您的问题。
Have a look on this paper for a survey on algorithms for computing all spanning trees of a connected graph (which I think is your case). 请看一下本文 ,以了解计算连接图的所有生成树的算法的调查(我认为这是您的情况)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 r中的igraph包:如何标记有向无环图的边 - igraph package in r: How to label edges for a directed-acyclic-graph 如何用R(通过iGraph,network或其他软件包)在R的边缘两边绘制文本的有向网络图? - How to draw a directed network graph with texts on both sides of edges in R (via iGraph, network or some other package)? igraph R中从根到叶的有向树图中的所有路径 - All paths in directed tree graph from root to leaves in igraph R 如何在R中绘制有向无环格子图 - How to plot directed acyclic lattice graph in R 在R中生成随机有向无环图显示了环和双向 - Generating a random directed acyclic graph in R is showing loops and bidirections 使用R包“ igraph”清洁图形 - Cleaning a graph with R package “igraph” 使用R中的igraph仅显示网络图上的特定标签 - Show only specific labels on network graph using igraph in R 在 igraph(R 包)中反转有向图(转置图)中的边 - Invert edges in a directed graph (transpose graph) in igraph (R packages) 如何在R中展开使用igraph包制作的社区图 - How to spread out community graph made by using igraph package in R 使用R中的igraph来计算出有向图的特征向量中心性时,外联节点应该排成行还是列? - Should out-linking nodes be in rows or columns for calculating eigenvector centrality of a directed graph using igraph in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM