Graph generator with the given weighted edge sequence.


I need a graph generator similar to the:

igraph_degree_sequence_game — Generates a random graph with a given degree sequence

But returns a random graph with the given weighted edge sequence. I have the weight of edges that enter and of edges that leave each node, and I need to generate random links.

Can I generate?

Thank you,


There is no such built-in function at the moment.

This does not seem like a trivial problem, and I can see multiple interpretations of what you describe. Can you clarify through a concrete example? Show a concrete example input, and a possible valid output from the function.

P.S. The reason why the system held your post for moderation is that it detected that it was pasted (not typed at a typical rate). Did you cross-post it to somewhere else? This is the typical reason why people end up copy/pasting. If you cross-posted, please link to the other instances of this post.

@szhorvat This is a question about moderation, not sample_degseq.
“P.S. The reason why the system held your post for moderation is that it detected that it was pasted (not typed at a typical rate). Did you cross-post it to somewhere else? This is the typical reason why people end up copy/pasting.”
Actually I typically edit in a text editor to avoid the frustration when a web page is inadvertently refreshed and one’s work is lost. So if I compose in a text editor and paste it will always be held for moderation?

I don’t know, but I don’t think so. We are still learning to use Discourse, and adapting as we go. This was a default setting. It is meant to prevent spam messages, so I assume it only applies to first-time posters. If you find any problems with this, do let me know.

The reason I commented on this (for the first time) is because there is a chance that @Alvaro_Sechinel has cross-posted the same question somewhere else. In the past, this auto-moderation action was triggered when people have written a message to the old igraph mailing list, then later posted it here as well. We don’t have a policy against cross-posting, but it is useful to cross-link cross-posts: if someone gives an answer on one forum only, people reading the question on a different forum should have a link to that answer.


It is the first time that I post on the forum.

Input example:

1 out= 75.43 (sum of edges coming out of node A)
1 in = 77.18 (sum of edges entering node A)

2 out = 213.122
2 in = 73.805

3 out = 14.921
3 in = 28.564

4 out = 71.056
4 in = 210.864

5 out = 22.642
5 in = 111.778

6 out = 4.263
6 in = 0.897

7 out = 105.917
7 in = 4.263

Output example:


In the output example:

A= 73.805
B= 68.120
C= 3.883
D= 209.239
E= 9.060
F= 21.745
G= 1.625
H= 2.936
I= 5.861
J= 0.897
K= 4.263



So you have no constraints on how many outgoing and incoming connections a vertex can have, or on which other vertices it may connect to. Your only constraints are on the total incoming and outgoing edge weights, for each vertex. Edge weights are non-negative real numbers.

I believe this is the matrix scaling problem. There’s an overview here:

Perhaps this is a useful starting point.


Is there any way to use the generator igraph_degree_sequence_game with positive rational numbers in the arguments in_deg and out_deg?
My idea is to use igraph_degree_sequence_game generator with the sum of edges that enter the node in the argument in_deg and the sum of edges that exit the node in the argument out_deg. I think the output would be equivalent.



No, there is not. The methods used by this function construct unweighted graphs.

You can use the IGRAPH_DEGSEQ_SIMPLE method (i.e. the configuration model) with large positive integers to construct a multigraph, then interpret edge multiplicities as weights. However:

  • the sampling will not be uniform (see documentation)
  • there will be self-loops


I have a question regarding this issue: What if we use the “strength” function instead of “centr_degree” (to give an example) to perform a configurational model with weights? I conducted a toy test, and one problem I encountered is that it creates self-loops, and there seems to be a slight mismatch between the row sums (when I convert it to an adjacency matrix).

Example to how i performed (based on other help):

for an unipartite, undirect, and weighted graph

degr_cent ← strength(your_graph, mode = ‘all’)
config ← sample_degseq(degr_cent, method = “simple”)
matrix_of_your_config_graph<-as_adjacency_matrix(config, type = “both”, sparse = FALSE)

in sample_degseq if i try to use the method = “”, it doesn’t work, is it because this method restrict loops, and the size of the graph is smaller than the weights to assigned for each node?

Any insights or suggestions on how to address this would be greatly appreciated. Thank you in advance for your help!


As I said above, there is currently no function in igraph to construct a weighted graph with given strengths. The input to sample_degseq() must be integer degrees, not strengths.

Aside: If you need the degrees, use degree(), not centr_degree().

I suggest you create a single graph using realize_degseq(), then rewire it using rewire(graph, with=keeping_degseq(niter = 10*ecount(graph)). This will sample graphs with the given degree sequence uniformly.

Sampling with given degrees and the associated documentation will be improved in the next major version. See the C library documentation for a preview: igraph Reference Manual

thank you very much for the response!
as you suggested i tried the functions: rewire(graph, with=keeping_degseq(niter = 10*ecount(graph)), its good but the results is also an unweighted graph, no? and then it’s similar as implementing the sample_degseq function with the degree distribution?

In other hand, I am close to a solution (with the help of other post), perhaps similar to the configurational model, for bipartite spp. abundance matrix in the way (spp. x communities, in this case, unlike my previous example, it is now a bipartite matrix with abundance data that are integer, relative big values),
then you tell me what do you think about it:

degr_cent ← strength(your_graph, mode = ‘all’)
deg1 ← degr_cent[1:nrow(m_sppxcomm)]
deg2 ← degr_cent[(nrow(m_sppxcomm)+1):length(degr_cent)]
edgelist ← cbind(spp=sample(rep(rownames(m_sppxcomm), deg1)), comm=sample(rep(colnames(m_sppxch$“2005”), deg2))) # m_sppxcomm is my community matrix from which i get my graph
m_random<-as.matrix(xtabs(~spp+comm, edgelist, sparse = F))

It is not clear to me what you are looking for. You touched on a mixture of different problems including generating weighted graphs (with what precise constraint?), the bad time performance of “” and sampling bipartite graphs with given degrees.

If you can give a mathematically precise description of what you are looking for, ideally with a concrete example, then we can tell you whether this is already available in igraph, or if not, whether it is easy to implement with the current functionality.