Skip to content
Closed
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
43 changes: 27 additions & 16 deletions src/_igraph/graphobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -13204,15 +13204,16 @@ PyObject *igraphmodule_Graph_modularity_matrix(igraphmodule_GraphObject *self,
* Newman's edge betweenness method
*/
PyObject *igraphmodule_Graph_community_edge_betweenness(igraphmodule_GraphObject *self, PyObject *args, PyObject *kwds) {
static char *kwlist[] = { "directed", "weights", NULL };
static char *kwlist[] = { "directed", "weights", "lengths", NULL };
int directed = 1;
PyObject *weights_o = Py_None;
PyObject *weights_o = Py_None, *lengths_o = Py_None;
PyObject *res, *qs, *ms;
igraph_matrix_int_t merges;
igraph_vector_t q;
igraph_vector_t *weights = NULL;
igraph_vector_t *weights = NULL, *lengths = NULL;
igraph_error_t err;

if (!PyArg_ParseTupleAndKeywords(args, kwds, "|pO", kwlist, &directed, &weights_o)) {
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|pOO", kwlist, &directed, &weights_o, &lengths_o)) {
return NULL;
}

Expand All @@ -13235,7 +13236,16 @@ PyObject *igraphmodule_Graph_community_edge_betweenness(igraphmodule_GraphObject
return igraphmodule_handle_igraph_error();
}

if (igraph_community_edge_betweenness(&self->g,
if (igraphmodule_attrib_to_vector_t(lengths_o, self, &lengths, ATTRIBUTE_TYPE_EDGE)) {
igraph_vector_destroy(&q);
igraph_matrix_int_destroy(&merges);
if (weights) {
igraph_vector_destroy(weights); free(weights);
}
return NULL;
}

err = igraph_community_edge_betweenness(&self->g,
/* removed_edges = */ NULL,
/* edge_betweenness = */ NULL,
/* merges = */ &merges,
Expand All @@ -13244,20 +13254,19 @@ PyObject *igraphmodule_Graph_community_edge_betweenness(igraphmodule_GraphObject
/* membership = */ NULL,
directed,
weights,
/* lengths = */ NULL)) {

igraph_vector_destroy(&q);
igraph_matrix_int_destroy(&merges);
if (weights) {
igraph_vector_destroy(weights); free(weights);
}

return igraphmodule_handle_igraph_error();;
}
lengths);

if (weights) {
igraph_vector_destroy(weights); free(weights);
}
if (lengths) {
igraph_vector_destroy(lengths); free(lengths);
}
if (err) {
igraph_vector_destroy(&q);
igraph_matrix_int_destroy(&merges);
return igraphmodule_handle_igraph_error();
}

qs = igraphmodule_vector_t_to_PyList(&q, IGRAPHMODULE_TYPE_FLOAT);
igraph_vector_destroy(&q);
Expand Down Expand Up @@ -18989,7 +18998,7 @@ struct PyMethodDef igraphmodule_Graph_methods[] = {
{"community_edge_betweenness",
(PyCFunction)igraphmodule_Graph_community_edge_betweenness,
METH_VARARGS | METH_KEYWORDS,
"community_edge_betweenness(directed=True, weights=None)\n--\n\n"
"community_edge_betweenness(directed=True, weights=None, lengths=None)\n--\n\n"
"Community structure detection based on the betweenness of the edges in\n"
"the network. This algorithm was invented by M Girvan and MEJ Newman,\n"
"see: M Girvan and MEJ Newman: Community structure in social and biological\n"
Expand All @@ -19010,6 +19019,8 @@ struct PyMethodDef igraphmodule_Graph_methods[] = {
" when we calculate the betweenness values.\n"
"@param weights: name of an edge attribute or a list containing\n"
" edge weights. Higher weights indicate stronger connections.\n\n"
"@param lengths: name of an edge attribute or a list of positive edge lengths\n"
" used to compute shortest paths for betweenness.\n"
"@return: a tuple with the merge matrix that describes the dendrogram\n"
" and the modularity scores before each merge. The modularity scores\n"
" use the weights if the original graph was weighted.\n"
Expand Down
14 changes: 8 additions & 6 deletions src/igraph/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ def _community_optimal_modularity(graph, *args, **kwds):
return VertexClustering(graph, membership, modularity)


def _community_edge_betweenness(graph, clusters=None, directed=True, weights=None):
def _community_edge_betweenness(
graph, clusters=None, directed=True, weights=None, lengths=None
):
"""Community structure based on the betweenness of the edges in the
network.

Expand All @@ -245,18 +247,18 @@ def _community_edge_betweenness(graph, clusters=None, directed=True, weights=Non
@param clusters: the number of clusters we would like to see. This
practically defines the "level" where we "cut" the dendrogram to
get the membership vector of the vertices. If C{None}, the dendrogram
is cut at the level that maximizes the modularity when the graph is
unweighted; otherwise the dendrogram is cut at a single cluster
(because cluster count selection based on modularities does not make
sense for this method if not all the weights are equal).
is cut at the level that maximizes modularity.
@param directed: whether the directionality of the edges should be
taken into account or not.
@param weights: name of an edge attribute or a list containing
edge weights. Higher weights indicate stronger connections.
@param lengths: name of an edge attribute or a list of positive edge
lengths used to compute shortest paths for betweenness. If omitted,
each edge has unit length.
@return: a L{VertexDendrogram} object, initially cut at the maximum
modularity or at the desired number of clusters.
"""
merges, qs = GraphBase.community_edge_betweenness(graph, directed, weights)
merges, qs = GraphBase.community_edge_betweenness(graph, directed, weights, lengths)
if clusters is None:
if qs is not None:
clusters = _optimal_cluster_count_from_merges_and_modularity(
Expand Down
17 changes: 17 additions & 0 deletions tests/test_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,23 @@ def testEdgeBetweenness(self):
self.assertMembershipsEqual(cl, [0, 0, 0, 1, 1])
self.assertAlmostEqual(cl.q, 0.2750, places=3)

def testEdgeBetweennessLengths(self):
g = Graph([(0, 1), (1, 2), (2, 3), (3, 0)])
g.es["length"] = [1, 10, 1, 1]
g.es["strength"] = [4, 1, 4, 1]

# Shortest paths first avoid the long edge (1, 2). The algorithm
# removes (3, 0), then (1, 2), splitting the cycle into two pairs.
cl = g.community_edge_betweenness(2, lengths=g.es["length"]).as_clustering()
self.assertMembershipsEqual(cl, [0, 0, 1, 1])
cl = g.community_edge_betweenness(
weights="strength", lengths="length"
).as_clustering()
self.assertMembershipsEqual(cl, [0, 0, 1, 1])
self.assertAlmostEqual(cl.q, 0.3)
with self.assertRaises(InternalError):
g.community_edge_betweenness(lengths=[0, 1, 1, 1])

def testEigenvector(self):
g = Graph.Full(5) + Graph.Full(5)
g.add_edges([(0, 5)])
Expand Down
Loading