Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Graph.Hypercube() #784

Merged
merged 2 commits into from
Jul 3, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Added

- Added `Graph.Hypercube()` for creating n-dimensional hypercube graphs.
- Added `Graph.Chung_Lu()` for sampling from the Chung-Lu model as well as several related models.
- Added `Graph.is_complete()` to test if there is a connection between all distinct pairs of vertices.
- Added `Graph.mean_degree()` for a convenient way to compute the average degree of a graph.
Expand Down
47 changes: 47 additions & 0 deletions src/_igraph/graphobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2758,6 +2758,41 @@ PyObject *igraphmodule_Graph_Hexagonal_Lattice(PyTypeObject * type,
return (PyObject *) self;
}


/** \ingroup python_interface_graph
* \brief Generates hypercube graph
* \return a reference to the newly generated Python igraph object
* \sa igraph_hypercube
*/
PyObject *igraphmodule_Graph_Hypercube(PyTypeObject * type,
PyObject * args, PyObject * kwds)
{
Py_ssize_t n;
igraph_bool_t directed;
PyObject *o_directed = Py_False;
igraphmodule_GraphObject *self;
igraph_t g;

static char *kwlist[] = { "n", "directed", NULL };

if (!PyArg_ParseTupleAndKeywords(args, kwds, "n|O", kwlist, &n, &o_directed)) {
return NULL;
}

CHECK_SSIZE_T_RANGE(n, "vertex count");

directed = PyObject_IsTrue(o_directed);

if (igraph_hypercube(&g, n, directed)) {
igraphmodule_handle_igraph_error();
return NULL;
}

CREATE_GRAPH_FROM_TYPE(self, g, type);

return (PyObject *) self;
}

/** \ingroup python_interface_graph
* \brief Generates a bipartite graph from a bipartite adjacency matrix
* \return a reference to the newly generated Python igraph object
Expand Down Expand Up @@ -14200,6 +14235,18 @@ struct PyMethodDef igraphmodule_Graph_methods[] = {
"@param mutual: whether to create all connections as mutual\n"
" in case of a directed graph.\n"},

/* interface to igraph_hypercube */
{"Hypercube", (PyCFunction) igraphmodule_Graph_Hypercube,
METH_VARARGS | METH_CLASS | METH_KEYWORDS,
"Hypercube(n, directed=False)\n--\n\n"
"Generates an n-dimensional hypercube graph.\n\n"
"The hypercube graph M{Q_n} has M{2^n} vertices and M{2^{n-1} n} edges.\n"
"Two vertices are connected when the binary representations of their vertex\n"
"IDs differ in precisely one bit.\n"
"@param n: the dimension of the hypercube graph\n"
"@param directed: whether to create a directed graph; edges will point\n"
" from lower index vertices towards higher index ones."},

/* interface to igraph_biadjacency */
{"_Biadjacency", (PyCFunction) igraphmodule_Graph_Biadjacency,
METH_VARARGS | METH_CLASS | METH_KEYWORDS,
Expand Down
5 changes: 5 additions & 0 deletions tests/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ def testHexagonalLattice(self):
g = Graph.Hexagonal_Lattice([2, 2], directed=True, mutual=True)
self.assertEqual(sorted(g.get_edgelist()), sorted(el + [(y, x) for x, y in el]))

def testHypercube(self):
el = [(0, 1), (0, 2), (0, 4), (1, 3), (1, 5), (2, 3), (2, 6), (3, 7), (4, 5), (4, 6), (5, 7), (6, 7)]
g = Graph.Hypercube(3)
self.assertEqual(g.get_edgelist(), el)

def testLCF(self):
g1 = Graph.LCF(12, (5, -5), 6)
g2 = Graph.Famous("Franklin")
Expand Down
Loading