Skip to content

Commit

Permalink
Merge pull request #776 from igraph/feat/is_complete
Browse files Browse the repository at this point in the history
feat: add is_complete()
  • Loading branch information
ntamas authored May 19, 2024
2 parents ccb057a + 79c4c60 commit 1a5dac3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# igraph Python interface changelog

### Added

- Added `Graph.is_complete()` to test if there is a connection between all distinct pair of vertices.

### Changed

- Error messages issued when an attribute is not found now mention the name and type of that attribute.
Expand Down
29 changes: 29 additions & 0 deletions src/_igraph/graphobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,25 @@ PyObject *igraphmodule_Graph_is_simple(igraphmodule_GraphObject* self, PyObject*
}


/** \ingroup python_interface_graph
* \brief Checks whether an \c igraph.Graph object is a complete graph.
* \return \c True if the graph is complete, \c False otherwise.
* \sa igraph_is_complete
*/
PyObject *igraphmodule_Graph_is_complete(igraphmodule_GraphObject* self, PyObject* Py_UNUSED(_null)) {
igraph_bool_t res;

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

if (res)
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}


/** \ingroup python_interface_graph
* \brief Determines whether a graph is a (directed or undirected) tree
* \sa igraph_is_tree
Expand Down Expand Up @@ -13625,6 +13644,16 @@ struct PyMethodDef igraphmodule_Graph_methods[] = {
"@return: C{True} if it is simple, C{False} otherwise.\n"
"@rtype: boolean"},

/* interface to igraph_is_complete */
{"is_complete", (PyCFunction) igraphmodule_Graph_is_complete,
METH_NOARGS,
"is_complete()\n--\n\n"
"Checks whether the graph is complete, i.e. whether there is at least one\n"
"connection between all distinct pairs of vertices. In directed graphs,\n"
"ordered pairs are considered.\n\n"
"@return: C{True} if it is complete, C{False} otherwise.\n"
"@rtype: boolean"},

/* interface to igraph_is_tree */
{"is_tree", (PyCFunction) igraphmodule_Graph_is_tree,
METH_VARARGS | METH_KEYWORDS,
Expand Down
2 changes: 1 addition & 1 deletion src/igraph/adjacency.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _get_biadjacency(graph, types="type", *args, **kwds):
bipartite adjacency matrix is an M{n} times M{m} matrix, where M{n} and
M{m} are the number of vertices in the two vertex classes.
@param types: an igraph vector containing the vertex types, or an
@param types: a vector containing the vertex types, or an
attribute name. Anything that evalulates to C{False} corresponds to
vertices of the first kind, everything else to the second kind.
@return: the bipartite adjacency matrix and two lists in a triplet. The
Expand Down
6 changes: 6 additions & 0 deletions tests/test_bipartite.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ def testBipartiteProjection(self):
g = Graph.Full_Bipartite(10, 5)

g1, g2 = g.bipartite_projection()
self.assertTrue(g1.is_complete())
self.assertTrue(g1.isomorphic(Graph.Full(10)))
self.assertTrue(g2.is_complete())
self.assertTrue(g2.isomorphic(Graph.Full(5)))
self.assertTrue(g.bipartite_projection(which=0).isomorphic(g1))
self.assertTrue(g.bipartite_projection(which=1).isomorphic(g2))
Expand All @@ -183,15 +185,19 @@ def testBipartiteProjection(self):
self.assertTrue(g.bipartite_projection_size() == (10, 45, 5, 10))

g1, g2 = g.bipartite_projection(probe1=10)
self.assertTrue(g1.is_complete())
self.assertTrue(g1.isomorphic(Graph.Full(5)))
self.assertTrue(g2.is_complete())
self.assertTrue(g2.isomorphic(Graph.Full(10)))
self.assertTrue(g.bipartite_projection(which=0).isomorphic(g2))
self.assertTrue(g.bipartite_projection(which=1).isomorphic(g1))
self.assertTrue(g.bipartite_projection(which=False).isomorphic(g2))
self.assertTrue(g.bipartite_projection(which=True).isomorphic(g1))

g1, g2 = g.bipartite_projection(multiplicity=False)
self.assertTrue(g1.is_complete())
self.assertTrue(g1.isomorphic(Graph.Full(10)))
self.assertTrue(g2.is_complete())
self.assertTrue(g2.isomorphic(Graph.Full(5)))
self.assertTrue(g.bipartite_projection(which=0).isomorphic(g1))
self.assertTrue(g.bipartite_projection(which=1).isomorphic(g2))
Expand Down
1 change: 1 addition & 0 deletions tests/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def testFull(self):
g = Graph.Full(20, directed=True)
el = g.get_edgelist()
el.sort()
self.assertTrue(g.is_complete())
self.assertTrue(
g.get_edgelist() == [(x, y) for x in range(20) for y in range(20) if x != y]
)
Expand Down

0 comments on commit 1a5dac3

Please sign in to comment.