简体   繁体   English

使用igraph在R中创建循环图或邻接矩阵?

[英]Creating a cycle graph or adjacency matrix in R, with igraph?

I am currently trying to (ideally) use igraph to generate a cycle graph adjacency matrix. 我目前正在尝试(理想情况下)使用igraph生成循环图邻接矩阵。 I would like each unit to be neighbors with k people. 我希望每个单位与k人成为邻居。

For k = 2, I am hoping to get: 对于k = 2,我希望得到:

library(igraph)
as_adj(make_graph(c(1, 2, 1, 10, 2, 3, 3, 4, 4, 5 ,5,6, 6,7, 7,8, 8,9, 9, 10), directed = FALSE))

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    1    0    0    0    0    0    0    0     1
 [2,]    1    0    1    0    0    0    0    0    0     0
 [3,]    0    1    0    1    0    0    0    0    0     0
 [4,]    0    0    1    0    1    0    0    0    0     0
 [5,]    0    0    0    1    0    1    0    0    0     0
 [6,]    0    0    0    0    1    0    1    0    0     0
 [7,]    0    0    0    0    0    1    0    1    0     0
 [8,]    0    0    0    0    0    0    1    0    1     0
 [9,]    0    0    0    0    0    0    0    1    0     1
[10,]    1    0    0    0    0    0    0    0    1     0

Is there a way to use existing functions in igraph to create graphs such as the one above, but for a generic k? 有没有一种方法可以使用igraph现有函数来创建诸如上述的图,但是要使用通用k? Thanks. 谢谢。

You can do this with sample_degseq : 您可以使用sample_degseq做到这一点:

"It is often useful to create a graph with given vertex degrees. This is exactly what sample_degseq does." “创建具有给定顶点度的图形通常很有用。这正是sample_degseq所做的。”

k=3 # degree for each node
n=10 # number of nodes
g = sample_degseq(rep(k,n),method = "simple.no.multiple")

Adjacency: 邻接:

as_adj(g)

[1,] . . 1 . 1
[2,] . . 1 1 .
[3,] 1 1 . . .
[4,] . 1 . . 1
[5,] 1 . . 1 .

Plot: 情节:

plot(g)

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM