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/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}()}, 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)