Skip to content

Commit

Permalink
feat: mean_degree()
Browse files Browse the repository at this point in the history
  • Loading branch information
szhorvat committed Jun 28, 2024
1 parent 47e7515 commit a5ed7b3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/_igraph/graphobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,29 @@ PyObject *igraphmodule_Graph_density(igraphmodule_GraphObject * self,
return igraphmodule_real_t_to_PyObject(res, IGRAPHMODULE_TYPE_FLOAT);
}

/** \ingroup python_interface_graph
* \brief Calculates the mean degree
* \return the mean degree
* \sa igraph_mean_degree
*/
PyObject *igraphmodule_Graph_mean_degree(igraphmodule_GraphObject * self,
PyObject * args, PyObject * kwds)
{
char *kwlist[] = { "loops", NULL };
igraph_real_t res;
PyObject *loops = Py_True;

if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &loops))
return NULL;

if (igraph_mean_degree(&self->g, &res, PyObject_IsTrue(loops))) {
igraphmodule_handle_igraph_error();
return NULL;
}

return igraphmodule_real_t_to_PyObject(res, IGRAPHMODULE_TYPE_FLOAT);
}

/** \ingroup python_interface_graph
* \brief The maximum degree of some vertices in an \c igraph.Graph
* \return the maxium degree as a Python object
Expand Down Expand Up @@ -15075,6 +15098,14 @@ struct PyMethodDef igraphmodule_Graph_methods[] = {
" assumes that there can't be any loops.\n"
"@return: the density of the graph."},

/* interface to igraph_mean_degree */
{"mean_degree", (PyCFunction) igraphmodule_Graph_mean_degree,
METH_VARARGS | METH_KEYWORDS,
"mean_degree(loops=True)\n--\n\n"
"Calculates the mean degree of the graph.\n\n"
"@param loops: whether to consider self-loops during the calculation\n"
"@return: the mean degree of the graph."},

/* interfaces to igraph_diameter */
{"diameter", (PyCFunction) igraphmodule_Graph_diameter,
METH_VARARGS | METH_KEYWORDS,
Expand Down
7 changes: 7 additions & 0 deletions tests/test_structural.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ def testDensity(self):
self.assertAlmostEqual(7 / 16, self.gdir.density(True), places=5)
self.assertAlmostEqual(1 / 7, self.tree.density(), places=5)

def testMeanDegree(self):
self.assertEqual(9.0, self.gfull.mean_degree())
self.assertEqual(0.0, self.gempty.mean_degree())
self.assertEqual(2.5, self.g.mean_degree())
self.assertEqual(7 / 4, self.gdir.mean_degree())
self.assertAlmostEqual(13 / 7, self.tree.mean_degree(), places=5)

def testDiameter(self):
self.assertTrue(self.gfull.diameter() == 1)
self.assertTrue(self.gempty.diameter(unconn=False) == inf)
Expand Down

0 comments on commit a5ed7b3

Please sign in to comment.