Restrict the Max degree of the graph

Hi, I am trying to generate a graph which the maximum degree of each nodes is four. Is there any function in ‘igraph R package’ to do this? My problem is generating a random graph which the max degree of each nodes is 4 [as in appendix image] ,and then, interpret the graph to the adjacency matrix.
Thanks you all!

Do you want to want some random graph with a maximum degree of four, or do you want the process given in the image? The image is way more specific.

For example sample_k_regular will give you a specific degree, which means it can also give you a maximum degree, but that’s only a solution if you don’t have other restrictions on the distribution of the edges in your graph.

1 Like

Thank’s. I want the process given in the image, but the max degree of each vertex must be 4 (not the max degree of whole graph).
I try the R codes as follow:

library(MASS)
library(Matrix)
library(philentropy)
library(igraph)
d=10
tmp = matrix(runif(2d, 0, 0.5), d, 2)
tmp = 2 * tmp
L = distance(tmp,method=“euclidean”)
s=0.125
prob = (1/sqrt(2
pi))exp(-(L^2)/2s)
g=graph.adjacency(prob,mode=“undirected” , diag = FALSE)
theta=as_adjacency_matrix(g,type=“both”)
for(j in 1:d){
for(i in 1:d){
if(theta[i,j]==0)
omega[i,j]=0
else
omega[i,j]=0.245
}
}
diag(omega) = 1

The problem is the graph ‘g’ will be empty and the adjacency matrix ‘theta’ is always 0.

I don’t know if I can answer your question, but can you fix your formatting? That makes it easier for others to read. Now your code has no indentation and there are some <em> tags that I think shouldn’t be there. That’s why some parts are cursive. It’s probably because *'s are translated to <em>'s or something.

I’m a new user of this website, however, I’ve tried to.
Best,
code

Well, if you hadn’t included the image I’d say it is simple: generate a random sequence from 1:4 (or 0:4) of the desired number of vertices, and call sample_degseq, for example:

degs <- sample(1:4, 100, replace=TRUE)
head(degs)
[1] 1 3 4 3 1 3
g <- sample_degseq(degs)
summary(g)
IGRAPH 5267986 U--- 100 119 -- Degree sequence random graph
+ attr: name (g/c), method (g/c)
degree_distribution(g)
[1] 0.00 0.31 0.21 0.27 0.21

or

degs2 <- sample(0:4, 100, replace=TRUE)
g2 <- sample_degseq(degs2)
summary(g2)
IGRAPH 5249ae5 U--- 100 116 -- Degree sequence random graph
+ attr: name (g/c), method (g/c)
degree_distribution(g2)
[1] 0.17 0.13 0.22 0.17 0.31

but I’m sorry I can’t delve into the formula in the image tonight …