Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ export(hrg.predict)
export(hrg_tree)
export(hub.score)
export(hub_score)
export(hypercube)
export(identical_graphs)
export(igraph.console)
export(igraph.drl.coarsen)
Expand Down Expand Up @@ -658,6 +659,7 @@ export(make_full_bipartite_graph)
export(make_full_citation_graph)
export(make_full_graph)
export(make_graph)
export(make_hypercube)
export(make_kautz_graph)
export(make_lattice)
export(make_line_graph)
Expand Down
39 changes: 39 additions & 0 deletions R/make.R
Original file line number Diff line number Diff line change
Expand Up @@ -2001,6 +2001,45 @@ lattice <- function(...) constructor_spec(make_lattice, ...)

## -----------------------------------------------------------------

#' Create a hypercube graph
#'
#' The n-dimensional hypercube graph has \eqn{2^n} vertices and
#' \eqn{2^{n-1} n} edges.
#' Two vertices are connected if the binary representations of their vertex
#' IDs (minus one, to make them zero-based) differ in precisely one bit.
#'
#' @param n The dimension of the hypercube graph. Must be non-negative and
#' not greater than 57.
#' @param directed Logical scalar, whether the graph should be directed.
#' If `TRUE`, edges point from vertices with lower IDs toward vertices
#' with higher IDs.
#' @return An igraph graph.
#'
#' @family deterministic constructors
#' @export
#' @examples
#' # 3-dimensional hypercube (cube)
#' print_all(make_hypercube(3))
#' # 4-dimensional hypercube (tesseract)
#' print_all(make_hypercube(4))
#' @cdocs igraph_hypercube
make_hypercube <- function(n, directed = FALSE) {
on.exit(.Call(R_igraph_finalizer))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
on.exit(.Call(R_igraph_finalizer))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the on.exit(.Call(R_igraph_finalizer)) line in cd7ad26. This is now handled by the autogenerated hypercube_impl() function, consistent with other recently added functions like make_circulant().

res <- hypercube_impl(n, directed)
if (igraph_opt("add.params")) {
res$name <- "Hypercube graph"
res$n <- n
}
res
}

#' @rdname make_hypercube
#' @param ... Passed to `make_hypercube()`.
#' @export
hypercube <- function(...) constructor_spec(make_hypercube, ...)

## -----------------------------------------------------------------

#' Create a ring graph
#'
#' A ring is a one-dimensional lattice and this function is a special case
Expand Down
1 change: 1 addition & 0 deletions man/graph_from_atlas.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/graph_from_edgelist.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/graph_from_literal.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_.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_chordal_ring.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_empty_graph.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_citation_graph.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_graph.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_graph.Rd

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

55 changes: 55 additions & 0 deletions man/make_hypercube.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_lattice.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_ring.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_star.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_tree.Rd

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

53 changes: 53 additions & 0 deletions tests/testthat/test-make.R
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,59 @@ test_that("make_lattice prints a warning for fractional length)", {
expect_identical_graphs(lattice_rounded, lattice_integer)
})

test_that("make_hypercube works", {
# 0-dimensional hypercube has 1 vertex and 0 edges
hc0 <- make_hypercube(0)
expect_vcount(hc0, 1)
expect_ecount(hc0, 0)

# 1-dimensional hypercube (line segment) has 2 vertices and 1 edge
hc1 <- make_hypercube(1)
expect_vcount(hc1, 2)
expect_ecount(hc1, 1)

# 2-dimensional hypercube (square) has 4 vertices and 4 edges
hc2 <- make_hypercube(2)
expect_vcount(hc2, 4)
expect_ecount(hc2, 4)

# 3-dimensional hypercube (cube) has 8 vertices and 12 edges
hc3 <- make_hypercube(3)
expect_vcount(hc3, 8)
expect_ecount(hc3, 12)

# Verify edges for 3D hypercube
# Vertices should be connected if their IDs (minus 1) differ in exactly one bit
# IDs: 1(000), 2(001), 3(010), 4(011), 5(100), 6(101), 7(110), 8(111)
expected_edges <- make_empty_graph(n = 8) +
edges(c(
1, 2, # 000 - 001
1, 3, # 000 - 010
1, 5, # 000 - 100
2, 4, # 001 - 011
2, 6, # 001 - 101
3, 4, # 010 - 011
3, 7, # 010 - 110
4, 8, # 011 - 111
5, 6, # 100 - 101
5, 7, # 100 - 110
6, 8, # 101 - 111
7, 8 # 110 - 111
))
expect_equal(as_edgelist(hc3), as_edgelist(expected_edges))

# Test directed hypercube
hc3_directed <- make_hypercube(3, directed = TRUE)
expect_true(is_directed(hc3_directed))
expect_ecount(hc3_directed, 12)
})

test_that("make_hypercube can be used with make_()", {
hc1 <- make_hypercube(3)
hc2 <- make_(hypercube(3))
expect_identical_graphs(hc1, hc2)
})

test_that("make_graph works", {
graph_make <- make_graph(1:10)
graph_elist <- make_empty_graph(n = 10) + edges(1:10)
Expand Down
Binary file removed tests/testthat/testthat-problems.rds
Binary file not shown.
Loading