max_cut

Contents

max_cut#

graph_tool.topology.max_cut(g, weight=None, part=None)[source][git]#

Return an approximate maximum cut.

Parameters:
gGraph

Graph to be used (the edge directions are ignored).

weightEdgePropertyMap (optional, default: None)

Nonnegative edge weights. If not given, every edge has unit weight.

partVertexPropertyMap (optional, default: None)

If provided, the side of every vertex will be stored in this property map.

Returns:
cutfloat

The total weight of the edges between the two sides.

partVertexPropertyMap

Boolean vertex property map with the side of every vertex.

See also

cut_size

Size of a given cut

graph_tool.flow.min_cut

Minimum cut

graph_tool.spectral.spectral_clustering

Partition based on the spectrum

Notes

A cut is a partition of the vertices into two sides \(S\) and \(\bar S = V\setminus S\), and its weight is

\[c(S) = \sum_{\{u,v\}\in E,\, u\in S,\, v\in\bar S} w_{uv},\]

the total weight of the edges with an endpoint on each side, ignoring the edge directions (self-loops are never cut). A maximum cut maximizes \(c(S)\). Finding it is NP-hard, and a local search is used instead: starting from a random partition, vertices are moved to the other side as long as this increases the weight of the cut. The result is a local optimum, whose weight is at least half of the total weight of the edges (and hence of the maximum). Each move increases the cut, and every round of moves takes time \(O(V + E)\).

Examples

>>> g = gt.collection.data["karate"]
>>> cut, part = gt.max_cut(g)
>>> print(cut, g.num_edges())
55.0 78

The two sides and the edges of the cut can be shown in a drawing of the network:

>>> vcolor = g.new_vp("string", vals=np.where(part.a, "#f28c8e",
...                                           "#9bbedc"))
>>> s = gt.edge_endpoint_property(g, part, "source")
>>> t = gt.edge_endpoint_property(g, part, "target")
>>> ecolor = g.new_ep("string", vals=np.where(s.a != t.a, "#e41a1c",
...                                           "#999999"))
>>> gt.graph_draw(g, g.vp.pos, vertex_fill_color=vcolor,
...               edge_color=ecolor, output="max_cut-karate.pdf")
<...>
../_images/max_cut-karate.png

Approximate maximum cut of Zachary’s karate club network. The two sides are shown in red and blue, and the edges of the cut in red.#