[feature request] General reduce function on multiple edges
Here I am attaching the same content as from the mailing list.
The idea is to implement a general scheme for map-reduce operation on edges or vertices, in an efficient ways, and more generically than what is done with
I have a graph in graph-tool with parallel edges. All parallel edges are assigned an
EdgePropertyMap representing their weight with a double. I would like to return a graph where the edges are condensed into a single edge where a custom reduce operation on these weights is performed. For example:
H = gt.Graph(directed=True) H.add_edge(0,1) # edge 0 H.add_edge(0,1) # edge 1 H.add_edge(0,1) # edge 2 H.add_edge(1,0) # edge 3 H.add_edge(0,2) # edge 4 ew = H.new_edge_property("double") ew[list(H.edges())]=1.2 ew[list(H.edges())]=2.3 ew[list(H.edges())]=-4.2 ew[list(H.edges())]=5.8 ew[list(H.edges())]=1.0 H.ep['weights'] = ew
In this case the edge (0,1), with a reduce function "sum", should have total weight 1.2+2.3-4.2= -0.7 while the remaining edges should keep the same weight (i.e. edge 3 should keep weight 5.8 and edge 4 should maintain weight 1.0).
gt.condensation_graph seems to do the trick, but only sum operation can be performed. To do this we need to use
H.vertex_index as vertex property:
Hcond, prop, vcount, ecount, v_avg, edge_avg = gt.condensation_graph(H,prop=H.vertex_index, aeprops=[H.ep['weights']])
Unfortunately this function only allows sum operation to be performed. What about using a different "reduce" function, like for example max? What about operations on non-numeric types of edges? It would greatly help carrying complex operations directly in Graph-tool, even if the value type of edges property is different than numeric.
More generally, what about introducing a split-apply-reduce framework to vertices and edges, or is it already possible with some modifications to