From 3f10cb94d44115281486d25aa07b9a82d6c6f344 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 28 Oct 2025 21:43:57 +0000 Subject: [PATCH 1/3] Initial plan From 5a8eef610520efdb5dc64a120a83dd378cc37352 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 28 Oct 2025 22:00:55 +0000 Subject: [PATCH 2/3] fix: Return `parent` as numeric vector in `bfs()` and `dfs()` instead of `igraph.vs` The `parent` result from `bfs()` and `dfs()` now returns a plain numeric vector instead of an `igraph.vs` object. This fixes issues with NA values causing errors when using `str()` or indexing operations. Changes: - Root vertices now have NA as parent (instead of creating invalid igraph.vs) - Unreachable vertices have -1 as parent value - Reachable vertices have their parent's vertex ID (1-based) - Updated snapshot tests to reflect numeric vector output Fixes #1576 Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com> --- R/structural-properties.R | 8 ++-- .../testthat/_snaps/structural-properties.md | 10 ++-- tests/testthat/test-structural-properties.R | 46 +++++++++++++++++++ 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/R/structural-properties.R b/R/structural-properties.R index 71f8bc54bbe..e506457f407 100644 --- a/R/structural-properties.R +++ b/R/structural-properties.R @@ -2800,6 +2800,8 @@ bfs <- function( } if (parent) { res$parent <- res$parent + 1 + # Convert 0 (no parent) to NA + res$parent[res$parent == 0] <- NA } if (pred) { res$pred <- res$pred + 1 @@ -2812,9 +2814,6 @@ bfs <- function( if (order) { res$order <- V(graph)[.env$res$order, na_ok = TRUE] } - if (parent) { - res$parent <- create_vs(graph, res$parent, na_ok = TRUE) - } if (pred) { res$pred <- create_vs(graph, res$pred, na_ok = TRUE) } @@ -3053,6 +3052,8 @@ dfs <- function( } if (parent) { res$parent <- res$parent + 1 + # Convert 0 (no parent) to NA + res$parent[res$parent == 0] <- NA } if (igraph_opt("return.vs.es")) { @@ -3062,7 +3063,6 @@ dfs <- function( if (order.out) { res$order.out <- V(graph)[.env$res$order.out, na_ok = TRUE] } - if (parent) res$parent <- create_vs(graph, res$parent, na_ok = TRUE) } else { if (order) { res$order <- res$order[res$order != 0] diff --git a/tests/testthat/_snaps/structural-properties.md b/tests/testthat/_snaps/structural-properties.md index e006e10afcb..89e93366bc5 100644 --- a/tests/testthat/_snaps/structural-properties.md +++ b/tests/testthat/_snaps/structural-properties.md @@ -29,9 +29,8 @@ 0 1 2 0 0 $parent - + 5/5 vertices, named: - a b c z d - b + a b c z d + -1 NA 2 -1 -1 $pred + 5/5 vertices, named: @@ -51,9 +50,8 @@ [1] "out" $father - + 5/5 vertices, named: - a b c z d - b + a b c z d + -1 NA 2 -1 -1 # bfs() deprecated arguments diff --git a/tests/testthat/test-structural-properties.R b/tests/testthat/test-structural-properties.R index 422e615f969..6709157e8db 100644 --- a/tests/testthat/test-structural-properties.R +++ b/tests/testthat/test-structural-properties.R @@ -259,6 +259,52 @@ test_that("bfs() does not pad order", { expect_equal(as.numeric(bfs(g, root = 2, unreachable = FALSE)$order), c(2, 1)) }) +test_that("bfs() parent is numeric vector with NA, not igraph.vs", { + # Verify fix for https://github.com/igraph/rigraph/issues/1576 + g <- sample_gnm(10, 20) + res <- bfs(g, 1, parent = TRUE)$parent + + # parent should be a numeric vector, not igraph.vs + expect_true(is.numeric(res)) + expect_false(inherits(res, "igraph.vs")) + + # str() should work without error + expect_no_error(str(res)) + + # Indexing should work without error + expect_no_error(res[1]) + + # Root vertex should have NA parent + expect_true(is.na(res[1])) + + # Other vertices should have numeric parents (or -1 for unreachable) + non_root <- res[-1] + expect_true(all(is.na(non_root) | non_root > 0 | non_root == -1)) +}) + +test_that("dfs() parent is numeric vector with NA, not igraph.vs", { + # Verify fix for https://github.com/igraph/rigraph/issues/1576 + g <- sample_gnm(10, 20) + res <- dfs(g, 1, parent = TRUE)$parent + + # parent should be a numeric vector, not igraph.vs + expect_true(is.numeric(res)) + expect_false(inherits(res, "igraph.vs")) + + # str() should work without error + expect_no_error(str(res)) + + # Indexing should work without error + expect_no_error(res[1]) + + # Root vertex should have NA parent + expect_true(is.na(res[1])) + + # Other vertices should have numeric parents (or -1 for unreachable) + non_root <- res[-1] + expect_true(all(is.na(non_root) | non_root > 0 | non_root == -1)) +}) + test_that("diameter() works -- undirected", { g <- largest_component(sample_gnp(30, 3 / 30)) sp <- distances(g) From 5eb4ae6633f0270eb58ada5b39593722b340ce0a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 28 Oct 2025 22:05:06 +0000 Subject: [PATCH 3/3] docs: Update cross-references in man pages Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com> --- man/make_circulant.Rd | 4 +++- man/make_full_multipartite.Rd | 1 + man/make_turan.Rd | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/man/make_circulant.Rd b/man/make_circulant.Rd index 50928495616..f3d3afcb949 100644 --- a/man/make_circulant.Rd +++ b/man/make_circulant.Rd @@ -49,10 +49,12 @@ Other deterministic constructors: \code{\link{make_empty_graph}()}, \code{\link{make_full_citation_graph}()}, \code{\link{make_full_graph}()}, +\code{\link{make_full_multipartite}()}, \code{\link{make_graph}()}, \code{\link{make_lattice}()}, \code{\link{make_ring}()}, \code{\link{make_star}()}, -\code{\link{make_tree}()} +\code{\link{make_tree}()}, +\code{\link{make_turan}()} } \concept{deterministic constructors} diff --git a/man/make_full_multipartite.Rd b/man/make_full_multipartite.Rd index 04c9a1ea840..e0cedcb7102 100644 --- a/man/make_full_multipartite.Rd +++ b/man/make_full_multipartite.Rd @@ -48,6 +48,7 @@ Other deterministic constructors: \code{\link{graph_from_literal}()}, \code{\link{make_}()}, \code{\link{make_chordal_ring}()}, +\code{\link{make_circulant}()}, \code{\link{make_empty_graph}()}, \code{\link{make_full_citation_graph}()}, \code{\link{make_full_graph}()}, diff --git a/man/make_turan.Rd b/man/make_turan.Rd index 3eec18e4487..6431e0f76ba 100644 --- a/man/make_turan.Rd +++ b/man/make_turan.Rd @@ -47,6 +47,7 @@ Other deterministic constructors: \code{\link{graph_from_literal}()}, \code{\link{make_}()}, \code{\link{make_chordal_ring}()}, +\code{\link{make_circulant}()}, \code{\link{make_empty_graph}()}, \code{\link{make_full_citation_graph}()}, \code{\link{make_full_graph}()},