Skip to content

Return type-stable empty graphs from all samplers - #53

Merged
alexpghayes merged 3 commits into
mainfrom
fix/rectangular-empty-sample-sparse
Nov 17, 2026
Merged

alexpghayes merged 3 commits into
mainfrom
fix/rectangular-empty-sample-sparse

Conversation

@alexpghayes

Copy link
Copy Markdown
Collaborator

Problem

  • sample_sparse() built an empty directed adjacency matrix from explicit zeros at 1:n and 1:d. When n != d those index vectors have different lengths, so sparseMatrix() errors. That broke sample_sparse(), sample_igraph() and sample_tidygraph() on rectangular directed models whenever no edges are sampled.
  • Empty graphs also came back with different types than non-empty ones:
    • sample_edgelist() returned a 0-row base matrix of doubles, not a tibble of integers.
    • Undirected sample_sparse() returned a dgCMatrix, not a symmetric dsCMatrix.

Fix

  • Build empty matrices from zero-length index vectors, with symmetric = TRUE for undirected models.
  • Return tibble(from = integer(0), to = integer(0)) from the empty branch of sample_edgelist.matrix().

For undirected, directed square and directed rectangular models, with Poisson and Bernoulli edges, sample_edgelist(), sample_sparse() and sample_igraph() now return the same class, column types and attributes for empty and non-empty graphs.

Tests

tests/testthat/test-empty-graphs.R covers empty rectangular graphs in all three samplers and checks that classes and column types match between empty and non-empty graphs. Full suite: 199 passed, 0 failed.

Merge note

#49 (multigraph argument) also edits the empty branch of sample_edgelist.matrix(). When resolving the conflict, keep the integer tibble from this PR and apply collapse_multiedges() to it when multigraph = FALSE.

🤖 Generated with Claude Code

alexpghayes and others added 3 commits November 17, 2026 13:56
sample_sparse() built an empty directed matrix from explicit zeros at
1:n and 1:d, which errors whenever n != d, breaking sample_sparse(),
sample_igraph() and sample_tidygraph() on rectangular models with no
sampled edges. Build empty matrices from zero-length index vectors
instead, and make the empty undirected case symmetric like the
non-empty one.

sample_edgelist() returned a 0-row base matrix of doubles when no edges
were sampled; it now returns a tibble with integer from/to columns,
matching non-empty edgelists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Resolve the NEWS.md conflict with #57's bullets by keeping both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Resolve the NEWS.md conflict by keeping both sides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alexpghayes
alexpghayes marked this pull request as ready for review November 17, 2026 22:43
@alexpghayes
alexpghayes merged commit cb0c48a into main Nov 17, 2026
8 checks passed
alexpghayes added a commit that referenced this pull request Nov 17, 2026
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 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.

1 participant