r""" This function contracts the clusters of the graph, under the assumption that it was given a full clustering, i.e., all vertices have exactly one cluster in clustering Returns: a graph in which: - each vertex is a cluster from the clustering, - each origi
(edges: pw.Table[Edge], clustering: pw.Table[Clustering])
| 11 | |
| 12 | |
| 13 | def _contract(edges: pw.Table[Edge], clustering: pw.Table[Clustering]) -> Graph: |
| 14 | r""" |
| 15 | This function contracts the clusters of the graph, |
| 16 | under the assumption that it was given a full clustering, |
| 17 | i.e., all vertices have exactly one cluster in clustering |
| 18 | |
| 19 | Returns: |
| 20 | a graph in which: |
| 21 | - each vertex is a cluster from the clustering, |
| 22 | - each original edge now points to clusters containing the original endpoints |
| 23 | |
| 24 | """ |
| 25 | |
| 26 | new_vertices = ( |
| 27 | clustering.groupby(clustering.c) |
| 28 | .reduce(v=clustering.c) |
| 29 | .with_id(pw.this.v) |
| 30 | .select() |
| 31 | ) |
| 32 | new_edges = edges.select(u=clustering.ix(edges.u).c, v=clustering.ix(edges.v).c) |
| 33 | return Graph(new_vertices, new_edges) |
| 34 | |
| 35 | |
| 36 | def _contract_weighted( |