【问题标题】:Generating undirected network with pre-specified degree distribution without any self loops生成具有预先指定的度分布的无向网络,没有任何自环
【发布时间】:2014-02-10 04:33:38
【问题描述】:

我想生成一个有 100 个节点的无向​​网络,其中一半节点的度数为 10,另一半的度数为 3。这样的网络可以在没有自环的情况下构建吗?

使用下面指定的代码:

library(graph)
degrees=c(rep(3,50),rep(10,50))
names(degrees)=paste("node",seq_along(degrees)) #nodes must be names
x=randomNodeGraph(degrees)

我可以得到这样的图表,但其中包含自环。

有什么方法可以得到没有自循环的图表?

【问题讨论】:

    标签: r


    【解决方案1】:

    使用来自 Bioconductor (see here) 的图形包很容易

    #install graph from Bioconductor
    source("http://bioconductor.org/biocLite.R")
    biocLite("graph")
    
    #load graph and make the specified graph
    library(graph)
    degrees=c(rep(3,50),rep(10,50))
    names(degrees)=paste("node",seq_along(degrees)) #nodes must be names
    x=randomNodeGraph(degrees)
    
    #verify graph
    edges=edgeMatrix(x)
    edgecount=table(as.vector(edges))
    table(edgecount)
    #edgecount
    # 3 10 
    #50 50
    

    【讨论】:

    • 是否也可以用 2 列格式而不是邻接矩阵来表示这个图?
    • t(edges) 能得到你想要的吗?
    • 所以你提出的图是有向的,总度数由入度和出度组成。我想要一个图表,如果节点 1 连接到节点 5,那么从节点 5 到节点 1 的连接相同。这可能吗?
    • randomNodeGraph 帮助说:允许自循环。结果图是有向的(但总是可以强制为无向的)。但我找不到如何强制它是无方向的。你能帮忙吗?
    • ugraph(x) 是底层的无向图
    【解决方案2】:

    Erdős–Gallai theorem 回答了如果这样的图是否可以构建的问题。

    它基于你的情况下你的图表的非递减度数序列

    ds <- c( rep( 10, 50 ), rep(3,50) )
    

    你可以计算不等式的右边

    rhs <- (1:100 * 0:99) + c( rev(cumsum(rev(apply( data.frame(ds, 1:100) , 1, min ))))[-1], 0 )
    

    还有左边的

    lhs <- cumsum( ds )
    

    最后:

    all( lhs <= rhs )
    [1] TRUE
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-14
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-16
      • 1970-01-01
      相关资源
      最近更新 更多