lukes_partition

lukes_partition#

graph_tool.inference.lukes_partition(g, max_size, vweight=None, eweight=None)[git][source]#

Find the optimal partition of a tree into connected parts of bounded size, with the algorithm of Lukes.

Parameters:
gGraph

Tree or forest to be partitioned (the edge directions are ignored).

max_sizeint

Largest total vertex weight of a part.

vweightVertexPropertyMap (optional, default: None)

Nonnegative integer vertex weights. If not given, every vertex has weight one.

eweightEdgePropertyMap (optional, default: None)

Edge weights. If not given, every edge has weight one.

Returns:
bVertexPropertyMap

The part of every vertex, labelled from zero.

See also

graph_tool.structure.minimum_spanning_tree

Minimum spanning tree

partition_quality

Coverage and performance of a partition

Notes

The algorithm of Lukes [lukes-efficient-1974] finds, among the partitions of the vertices of a tree into connected parts of total weight at most max_size, one with the smallest total weight of the edges between the parts, or, equivalently, the largest total weight of the edges inside the parts. For every vertex \(v\) and part size \(s\), dynamic programming computes the largest total weight \(f_v(s)\) of the edges inside the parts of the subtree of \(v\), when the part of \(v\) has total vertex weight \(s\). Starting from \(f_v(s) = 0\) for \(s = x_v\), the vertex weight of \(v\), and \(-\infty\) otherwise, the children \(c\) of \(v\) are merged one at a time, either cutting the edge to the child or joining the part of the child,

\[f_v(s) \leftarrow \max\left(f_v(s) + \max_{s'}f_c(s'), \max_{s_1 + s_2 = s}\left[f_v(s_1) + f_c(s_2) + w_{vc}\right]\right),\]

for \(s \le K\), where \(K\) is max_size and \(w_{vc}\) is the weight of the edge between \(v\) and \(c\). The optimal partition is then reconstructed from the root, starting with the size \(s\) that maximizes \(f_v(s)\) at the root and following the choices made in the merges. Every tree of a forest is partitioned separately. It runs in time \(O(VK^2)\) and memory \(O(VK)\). If several partitions are optimal, only one of them is returned.

References

[lukes-efficient-1974]

J. A. Lukes, “Efficient algorithm for the partitioning of trees”, IBM Journal of Research and Development 18, 217-224 (1974), DOI: 10.1147/rd.183.0217 [sci-hub, @tor]

Examples

>>> g = gt.collection.data["karate"]
>>> tree = gt.minimum_spanning_tree(g)
>>> t = gt.GraphView(g, efilt=tree)
>>> b = gt.lukes_partition(t, 15)
>>> print(np.bincount(b.a))
[12 15  3  4]

The parts, shown in the figure below, can be drawn on the tree, with the edges between the parts in gray, as follows:

>>> fill = np.array(["#9bbedc", "#f28c8e", "#a6d7a4", "#cba7d1"])
>>> line = np.array(["#377eb8", "#e41a1c", "#4daf4a", "#984ea3"])
>>> bs = gt.edge_endpoint_property(t, b, "source").fa
>>> bt = gt.edge_endpoint_property(t, b, "target").fa
>>> ecolor = t.new_ep("string", vals=np.where(bs == bt, line[bs],
...                                           "#999999"))
>>> gt.graph_draw(t, pos=gt.tree_layout(t, 33),
...               vertex_fill_color=t.new_vp("string", vals=fill[b.a]),
...               edge_color=ecolor, output_size=(600, 300),
...               output="lukes-partition-karate.pdf")
<...>
../_images/lukes-partition-karate.png

Partition of the minimum spanning tree of the karate club network into connected parts with at most 15 vertices, drawn with tree_layout() from vertex 33. The vertex colors indicate the parts, and the edges between the parts are gray.#