如果您从 R 中使用 igraph,请使用此选项
realize_degseq {igraph} | R 文档 |
创建一个具有给定顶点度的图通常很有用。此函数以确定性的方式创建这样一个图。
realize_degseq(
out.deg,
in.deg = NULL,
allowed.edge.types = c("simple", "loops", "multi", "all"),
method = c("smallest", "largest", "index")
)
out.deg |
数值向量,度的序列(对于无向图)或出度(对于有向图)。对于无向图,其总和应为偶数。对于有向图,其总和应与 |
in.deg |
对于有向图,入度序列。默认值为 |
allowed.edge.types |
字符,指定允许的边的类型。“simple”仅允许简单图(无环,无多重边)。“multiple”允许有多重边,但不允许环。“loops”允许环边,但不允许多重边(当前未实现)。“all”允许所有类型的边。默认值为“simple”。 |
method |
字符,生成图的方法;见上文。 |
简单的无向图使用 Havel-Hakimi 算法(无向情况)或类似的 Kleitman-Wang 算法(有向情况)构造。这些算法的工作原理是选择一个任意顶点,并将其所有存根连接到其他顶点。重复此步骤,直到所有度都已连接。
“method”参数控制在算法过程中选择顶点的顺序。
“smallest”方法选择剩余度最小的顶点。结果通常是一个具有高负度同配性的图。在无向情况下,只要存在连通实现,无论是否允许使用多重边,此方法都保证生成连通图。在有向情况下,它倾向于生成弱连通图,但这不能保证。这是默认方法。
“largest”方法选择剩余度最大的顶点。结果通常是一个具有高正度同配性的图,并且经常是不连通的。
“index”方法按索引顺序选择顶点。
新的图对象。
sample_degseq
对于一个随机变体,它从具有给定度序列的图中采样。
g <- realize_degseq(rep(2,100))
degree(g)
is_simple(g)
## Exponential degree distribution, with high positive assortativity.
## Loop and multiple edges are explicitly allowed.
## Note that we correct the degree sequence if its sum is odd.
degs <- sample(1:100, 100, replace=TRUE, prob=exp(-0.5*(1:100)))
if (sum(degs) %% 2 != 0) { degs[1] <- degs[1] + 1 }
g4 <- realize_degseq(degs, method="largest", allowed.edge.types="all")
all(degree(g4) == degs)
## Power-law degree distribution, no loops allowed but multiple edges
## are okay.
## Note that we correct the degree sequence if its sum is odd.
degs <- sample(1:100, 100, replace=TRUE, prob=(1:100)^-2)
if (sum(degs) %% 2 != 0) { degs[1] <- degs[1] + 1 }
g5 <- realize_degseq(degs, allowed.edge.types="multi")
all(degree(g5) == degs)