Skip to contents

It is often useful to create a graph with given vertex degrees. This function creates such a graph in a randomized manner.

Usage

sample_degseq(
  out.deg,
  in.deg = NULL,
  method = c("simple", "vl", "simple.no.multiple", "simple.no.multiple.uniform")
)

degseq(..., deterministic = FALSE)

Arguments

out.deg

Numeric vector, the sequence of degrees (for undirected graphs) or out-degrees (for directed graphs). For undirected graphs its sum should be even. For directed graphs its sum should be the same as the sum of in.deg.

in.deg

For directed graph, the in-degree sequence. By default this is NULL and an undirected graph is created.

method

Character, the method for generating the graph. Right now the “simple”, “simple.no.multiple” and “vl” methods are implemented.

...

Passed to realize_degseq() if ‘deterministic’ is true, or to sample_degseq() otherwise.

deterministic

Whether the construction should be deterministic

Value

The new graph object.

Details

The “simple” method connects the out-stubs of the edges (undirected graphs) or the out-stubs and in-stubs (directed graphs) together. This way loop edges and also multiple edges may be generated. This method is not adequate if one needs to generate simple graphs with a given degree sequence. The multiple and loop edges can be deleted, but then the degree sequence is distorted and there is nothing to ensure that the graphs are sampled uniformly.

The “simple.no.multiple” method is similar to “simple”, but tries to avoid multiple and loop edges and restarts the generation from scratch if it gets stuck. It is not guaranteed to sample uniformly from the space of all possible graphs with the given sequence, but it is relatively fast and it will eventually succeed if the provided degree sequence is graphical, but there is no upper bound on the number of iterations.

The “simple.no.multiple.uniform” method is a variant of “simple.no.multiple” with the added benefit of sampling uniformly from the set of all possible simple graphs with the given degree sequence. Ensuring uniformity has some performance implications, though.

The “vl” method is a more sophisticated generator. The algorithm and the implementation was done by Fabien Viger and Matthieu Latapy. This generator always generates undirected, connected simple graphs, it is an error to pass the in.deg argument to it. The algorithm relies on first creating an initial (possibly unconnected) simple undirected graph with the given degree sequence (if this is possible at all). Then some rewiring is done to make the graph connected. Finally a Monte-Carlo algorithm is used to randomize the graph. The “vl” samples from the undirected, connected simple graphs uniformly.

Author

Gabor Csardi csardi.gabor@gmail.com

Examples


## The simple generator
g <- sample_degseq(rep(2, 100))
degree(g)
#>   [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
#>  [38] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
#>  [75] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
is_simple(g) # sometimes TRUE, but can be FALSE
#> [1] TRUE
g2 <- sample_degseq(1:10, 10:1)
degree(g2, mode = "out")
#>  [1]  1  2  3  4  5  6  7  8  9 10
degree(g2, mode = "in")
#>  [1] 10  9  8  7  6  5  4  3  2  1

## The vl generator
g3 <- sample_degseq(rep(2, 100), method = "vl")
degree(g3)
#>   [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
#>  [38] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
#>  [75] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
is_simple(g3) # always TRUE
#> [1] TRUE

## Exponential degree distribution
## 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 <- sample_degseq(degs, method = "vl")
all(degree(g4) == degs)
#> [1] TRUE

## Power-law degree distribution
## 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 <- sample_degseq(degs, method = "vl")
#> Error in sample_degseq(degs, method = "vl"): At vendor/cigraph/src/games/degree_sequence_vl/gengraph_mr-connected.cpp:150 : Cannot realize the given degree sequence as an undirected, simple graph. Invalid value
all(degree(g5) == degs)
#> Error in eval(expr, envir, enclos): object 'g5' not found