Skip to content

Commit

Permalink
feat: Graph.Hypercube()
Browse files Browse the repository at this point in the history
  • Loading branch information
szhorvat committed Jun 28, 2024
1 parent 47e7515 commit dc009cf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/_igraph/graphobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2735,6 +2735,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 @@ -14177,6 +14212,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

0 comments on commit dc009cf

Please sign in to comment.