Skip to content

Commit

Permalink
Merge pull request #784 from igraph/feat/hypercube
Browse files Browse the repository at this point in the history
feat: Graph.Hypercube()
  • Loading branch information
ntamas authored Jul 3, 2024
2 parents 613d39a + 5bb38e0 commit 9dd5a58
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
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 @@ -14205,6 +14240,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 9dd5a58

Please sign in to comment.