From dc009cf32e287619222f6640e7925342430c330c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szabolcs=20Horva=CC=81t?= Date: Fri, 28 Jun 2024 16:31:59 +0000 Subject: [PATCH] feat: Graph.Hypercube() --- src/_igraph/graphobject.c | 47 +++++++++++++++++++++++++++++++++++++++ tests/test_generators.py | 5 +++++ 2 files changed, 52 insertions(+) diff --git a/src/_igraph/graphobject.c b/src/_igraph/graphobject.c index 31049e887..107cf0e6f 100644 --- a/src/_igraph/graphobject.c +++ b/src/_igraph/graphobject.c @@ -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 @@ -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, diff --git a/tests/test_generators.py b/tests/test_generators.py index 733a31c66..30cc58d23 100644 --- a/tests/test_generators.py +++ b/tests/test_generators.py @@ -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")