Repository navigation
Add multigraph argument to edgelist, igraph and tidygraph samplers (#30) - #49
Merged
Merged
Conversation
multigraph = TRUE (the default) keeps the current behavior of returning Poisson graphs as multigraphs. multigraph = FALSE collapses repeated edges into a single edge with an integer weight equal to the corresponding sample_sparse() entry, on all three igraph code paths. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 17, 2026
Resolve the NEWS.md conflict with #57's bullets by keeping both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main now has #53, which returns an integer tibble for empty edgelists. Keep that tibble and collapse it with collapse_multiedges() when multigraph = FALSE, so empty graphs are type-stable either way. Keep both sides of the R/utils.R helpers and NEWS.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
alexpghayes
marked this pull request as ready for review
November 17, 2026 22:44
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #30
Summary
Adds a
multigraphargument tosample_edgelist(),sample_igraph()andsample_tidygraph()(generics and all methods, includingsample_edgelist.matrix()), so Poisson graphs can come back as weighted graphs instead of multigraphs.multigraph = TRUE(default): current behavior, unchanged. Poisson graphs are multigraphs with repeated rows/edges and no weight attribute. Not a breaking change.multigraph = FALSE: repeated edges are collapsed into one edge with an integerweight, an edgelist column or an igraph edge attribute (tidygraph inherits it as an edge column). The weight equals the correspondingsample_sparse()entry on every code path:graph_from_data_frame()graph_from_data_frame()sample_sparse()+graph_from_biadjacency_matrix(weighted = TRUE), with weights coerced to integerDesign decisions
poisson_edges = FALSE:multigraphonly changes anything for Poisson graphs, but withmultigraph = FALSEtheweightattribute is always present (all 1s for Bernoulli graphs). This keeps the output shape predictable.from <= to, and that includes the extra self-loops added in Sample self-loops at the full rate in undirected models #47. So collapsing by(from, to)merges(i, j)with(j, i), just as the symmetricsparseMatrix()insample_sparse()does.(i, i)sampledwtimes becomes one loop with weightw, which equalsA[i, i]fromsample_sparse(). Whenallow_self_loops = FALSE, loops are removed before collapsing.dplyr::count(). The RNG stream is untouched, so with a common seed every sampler draws the same graph.multigraphmust be a singleTRUE/FALSE. It is checked in each generic withrlang::is_bool()and raises a clear error such as! `multigraph` must be a single `TRUE` or `FALSE`, not `NA`.It is a named argument after..., so it does not triprlang::check_dots_unnamed().sample_sparse()does not take it.Tests
New file
tests/testthat/test-multigraph.R(plus one error snapshot) covers:sample_sparse()entry by entry under a common seed, for undirected models (with and without self-loops), directed square models and directed rectangular/bipartite models(i, j)/(j, i)collapsingpoisson_edges = FALSEsample_sparse()rejectingmultigraphR CMD check --no-manual --ignore-vignettes --no-testsreports no new issues. It shows only the existingfrom/toglobal-variable NOTE insample_edgelist.undirected_factor_model(), which comes from #47.Notes
RoxygenNote) to avoid unrelated churn.sample_sparse()on a directed rectangular model (n != d) errors when zero edges are sampled, becausesparseMatrix(1:n, 1:d, x = 0, ...)gets index vectors of different lengths. This affectssample_igraph()on the bipartite path with eithermultigraphvalue.🤖 Generated with Claude Code