Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions R/structural-properties.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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")) {
Expand All @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion man/make_circulant.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/make_full_multipartite.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/make_turan.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions tests/testthat/_snaps/structural-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
0 1 2 0 0

$parent
+ 5/5 vertices, named:
a b c z d
<NA> <NA> b <NA> <NA>
a b c z d
-1 NA 2 -1 -1

$pred
+ 5/5 vertices, named:
Expand All @@ -51,9 +50,8 @@
[1] "out"

$father
+ 5/5 vertices, named:
a b c z d
<NA> <NA> b <NA> <NA>
a b c z d
-1 NA 2 -1 -1


# bfs() deprecated arguments
Expand Down
46 changes: 46 additions & 0 deletions tests/testthat/test-structural-properties.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down