feat: error when adding named vertices to non-empty unnamed graphs
Adding named vertices to unnamed graphs creates confusing output where existing vertices have NA names and edges display as NA--NA.
Changes
-
Error on name mismatch: Added error (abort) in
+.igraphwhen:- Using
vertex()/vertices()with unnamed arguments (interpreted as names) on non-empty unnamed graphs (graphs with vertices) - Using character vectors (e.g.,
g + c("a", "b")) on non-empty unnamed graphs
- Using
-
Allow empty graphs: Adding named vertices to empty graphs (graphs with 0 vertices, created by
make_empty_graph()) is allowed, enabling graph construction from scratch -
Test coverage: Added snapshot tests for error messages in
tests/testthat/_snaps/operators.md - Test updates: Updated tests to work with the new error behavior
The implementation checks vcount(e1) > 0 to determine if a graph is non-empty. Empty graphs are defined as having zero vertices, consistent with standard graph theory terminology.
Example
# Error when adding to non-empty unnamed graphs
> make_ring(10) + vertex(1)
Error: Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have `NA` names.
> make_empty_graph(1) + vertex("a")
Error: Cannot add named vertices to a non-empty unnamed graph. Existing vertices will have `NA` names.
# Works when adding to empty graphs (0 vertices)
> make_empty_graph() + vertex("a")
IGRAPH DN-- 1 0 --
+ attr: name (v/c)
> make_empty_graph() + c("a", "b")
IGRAPH UN-- 2 0 --
+ attr: name (v/c)
# Works when adding to already-named graphs
> g <- make_ring(10); V(g)$name <- letters[1:10]; g + vertex("k")
IGRAPH UN-- 11 10 -- Ring graph
+ edges from ... (vertex names):
[1] a--b b--c c--d ...
# Works when adding unnamed vertices (attributes only)
> make_ring(10) + vertex(foo = 5)
IGRAPH U--- 11 10 -- Ring graph
+ attr: name (g/c), mutual (g/l), circular (g/l), foo (v/n)
- Fixes igraph/rigraph#60
Original prompt
This section details on the original issue you should resolve
<issue_title>Warn when adding named vertices to unnamed graphs</issue_title> <issue_description>Or add vertex names automatically? Or both? E.g.
make_ring(10) + vertex(1) #> IGRAPH UN-- 11 10 -- Ring graph #> + attr: name (g/c), mutual (g/l), circular (g/l), name (v/n) #> + edges (vertex names): #> [1] NA--NA NA--NA NA--NA NA--NA NA--NA NA--NA NA--NA NA--NA NA--NA NA--NA</issue_description>
Comments on the Issue (you are @copilot in this section)
- Fixes igraph/rigraph#60
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.
@copilot: Abort instead of warning. Adding to an empty graph should work. Remember to add a snapshot test for the error message.
Changed to abort instead of warn in commit 8723eee. The implementation now:
- Errors when adding named vertices to non-empty unnamed graphs (graphs with edges)
- Allows adding named vertices to empty graphs (no edges), like
make_empty_graph()ormake_empty_graph(n) - Added snapshot tests for the error messages in
tests/testthat/_snaps/operators.md
The key insight is checking ecount(e1) > 0 rather than vertex count, which prevents the confusing NA--NA edge display issue while still allowing graph construction from scratch.
@copilot: Empty graphs have zero vertices, not edges. Review the error message.
Corrected in commit a4aa43d. Changed the check from ecount(e1) > 0 to vcount(e1) > 0. Empty graphs are now properly defined as having 0 vertices. The error message is now consistent with the check - "non-empty" refers to vertex count, not edge count.