Skip to content

feat: mean_degree() #785

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

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 @@ -4,6 +4,7 @@

- 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.

### Changed

Expand Down
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
Loading