Expand description
Hypercube constructor (ALGO-CN-007).
Counterpart of igraph_hypercube() in
references/igraph/src/constructors/regular.c:983-1003.
The n-dimensional hypercube graph Q_n has 2^n vertices and
2^(n-1) * n edges. Two vertices are connected when the binary
representations of their zero-based vertex IDs differ in exactly
one bit.
Small cases:
- n = 0 → singleton (one vertex, zero edges)
- n = 1 → K2 (two vertices, one edge:
(0, 1)) - n = 2 → 4-cycle (
(0,1), (0,2), (1,3), (2,3)) - n = 3 → 8-vertex cube (12 edges)
The graph is n-regular (every vertex has degree n) and bipartite
(vertices split into two classes by the parity of their popcount).
Algorithm: enumerate all 2^n vertices in id order; for each vertex
v and bit i ∈ [0, n) toggle that bit to get u = v ^ (1 << i),
and emit the canonical edge (v, u) only when v < u so each edge
is produced exactly once. Mirrors the upstream C loop.
Time complexity: O(2^n · n) = O(|V| · log |V|) = O(|E|).
§Upper bound
n is capped at 30 so that:
- vertex count
2^nfits comfortably inu32, - edge count
2^(n-1) · nfits inusizeon every supported platform (≤ ~1.6 × 10^10 onn = 30, well within 64-bitusize::MAX; this lets theedgesVecallocation succeed even on 32-bit targets whereusize::MAX ≈ 4 × 10^9— atn = 30it would exhaustusize, but real workloads stay far below it).
Constants§
- MAX_
HYPERCUBE_ DIMENSION - Maximum dimension accepted by
hypercube.
Functions§
- hypercube
- Build the n-dimensional hypercube graph
Q_n.