1
votes

I am currently trying to (ideally) use igraph to generate a cycle graph adjacency matrix. I would like each unit to be neighbors with k people.

For k = 2, I am hoping to get:

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? Thanks.

1
You say that you want a "cycle graph" but it does not seem clear what that means for k>2. Do you just want a graph such that all nodes have degree =k?G5W
@G5W Yes, that is the idea, thanks!user321627

1 Answers

2
votes

You can do this with sample_degseq:

"It is often useful to create a graph with given vertex degrees. This is exactly what sample_degseq does."

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)

enter image description here