Skip to content

Add multigraph argument to edgelist, igraph and tidygraph samplers (#30) - #49

Merged
alexpghayes merged 5 commits into
mainfrom
feature/multigraph-arg-30
Nov 17, 2026
Merged

alexpghayes merged 5 commits into
mainfrom
feature/multigraph-arg-30

Conversation

@alexpghayes

Copy link
Copy Markdown
Collaborator

Closes #30

Summary

Adds a multigraph argument to sample_edgelist(), sample_igraph() and sample_tidygraph() (generics and all methods, including sample_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 integer weight, an edgelist column or an igraph edge attribute (tidygraph inherits it as an edge column). The weight equals the corresponding sample_sparse() entry on every code path:
    • undirected: edgelist + graph_from_data_frame()
    • directed square: edgelist + graph_from_data_frame()
    • directed rectangular (bipartite): sample_sparse() + graph_from_biadjacency_matrix(weighted = TRUE), with weights coerced to integer

Design decisions

  • poisson_edges = FALSE: multigraph only changes anything for Poisson graphs, but with multigraph = FALSE the weight attribute is always present (all 1s for Bernoulli graphs). This keeps the output shape predictable.
  • Undirected symmetry: undirected edgelists already store every edge with 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 symmetric sparseMatrix() in sample_sparse() does.
  • Self-loops: a self-loop (i, i) sampled w times becomes one loop with weight w, which equals A[i, i] from sample_sparse(). When allow_self_loops = FALSE, loops are removed before collapsing.
  • Ordering: collapsing happens last, after the undirected self-loop top-up, deduplication and self-loop removal. It uses dplyr::count(). The RNG stream is untouched, so with a common seed every sampler draws the same graph.
  • Validation: multigraph must be a single TRUE/FALSE. It is checked in each generic with rlang::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 trip rlang::check_dots_unnamed(). sample_sparse() does not take it.

Tests

New file tests/testthat/test-multigraph.R (plus one error snapshot) covers:

  • the default is unchanged
  • collapsed weights match 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) collapsing
  • poisson_edges = FALSE
  • zero-edge graphs
  • sample_sparse() rejecting multigraph
  • bad input
testthat::test_local()
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 293 ]

R CMD check --no-manual --ignore-vignettes --no-tests reports no new issues. It shows only the existing from/to global-variable NOTE in sample_edgelist.undirected_factor_model(), which comes from #47.

Notes

  • Man pages were regenerated with roxygen2 7.3.2 (the version in RoxygenNote) to avoid unrelated churn.
  • Existing bug, not fixed here: sample_sparse() on a directed rectangular model (n != d) errors when zero edges are sampled, because sparseMatrix(1:n, 1:d, x = 0, ...) gets index vectors of different lengths. This affects sample_igraph() on the bipartite path with either multigraph value.

🤖 Generated with Claude Code

alexpghayes and others added 3 commits November 17, 2026 13:51
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>
alexpghayes and others added 2 commits November 17, 2026 14:39
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
alexpghayes marked this pull request as ready for review November 17, 2026 22:44
@alexpghayes
alexpghayes merged commit cd684fc into main Nov 17, 2026
8 checks passed
alexpghayes added a commit that referenced this pull request Nov 17, 2026
main now has the directed stack, the overlapping SBM changes, #49, #51
and #53. Resolve the NEWS.md conflict by keeping both sides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Be consistent with Poisson graphs about using weighted graphs versus multigraphs in igraph and tidygraph samplers

1 participant