def combine_edges(graph, property_aggregators): """Combine edges with the same endpoints. Replaces the edges in graph with new edge objects, where any set of edges between the same two nodes is replaced by a single edge. Each new edge has a property 'subedges' (edge.properties['subedges']) which contains the original edge objects. Arguments: graph -- a Graph object. It is destructively modified. """ if property_aggregators is None: property_aggregators = {} edges_by_node = dict([(node_id, set()) for node_id in graph.nodes]) for edge in graph.edges.values(): edges_by_node[edge.source.id].add(edge) edges_by_node[edge.target.id].add(edge) edge_sets = {} for edge in graph.edges.values(): if edge.id in edge_sets: continue eset = list(edges_by_node[edge.source.id] & edges_by_node[edge.target.id]) for edge_set in eset: edge_sets[edge_set] = eset edge_sets = map_dict(equalize_edge_orientation, edge_sets) edges = [create_edge(x, property_aggregators) for x in edge_sets.values()] graph.edges = dict([(e.id, e) for e in edges])