Guest
Sign in to keep your graphs and progress.
Select an algorithm and generate steps to begin visualization
Two equal halves, as few ties between them as possible
Splits a graph into two equal halves with few edges between them by swapping pairs of vertices, keeping the best run of swaps in each pass
Select an algorithm and generate steps to begin visualization
Graph bisection splits the vertices into two halves of equal size so that the total weight of the edges between the halves, the cut, is as small as possible. Without the size condition the answer would be a minimum cut, which is easy to find but often slices off a single vertex. With it, the problem is NP-hard. Brian Kernighan and Shen Lin published their heuristic in 1970 to place the parts of electronic circuits on boards with few wires between them, and it is still the model for local improvement in partitioning.
For every vertex, D is its external weight, to the other half, minus its internal weight, to its own half. Swapping a from one half with b from the other lowers the cut by D(a) + D(b) − 2w(a, b). A pass swaps the best pair, locks both, updates the D values as if the swap had happened, and repeats until every vertex is locked, even when the gain turns negative, since a later swap may more than make up for it. Then it keeps only the prefix of swaps with the largest total gain, undoes the rest and starts a new pass, and it stops when no prefix lowers the cut. A pass takes O(n³) as written here, or O(n² log n) with sorted D values. The run starts from a deliberately poor alternating split, shows D on each vertex, enlarges the pair being swapped, draws cut edges in magenta and lists the gains and running total. The result is a local optimum, not always the best bisection.
Partitioning decides how circuits are split across chips, how a simulation mesh is divided among processors so that each gets equal work with little communication, and how a social network splits into two groups. The Fiduccia–Mattheyses variant (1982) moves one vertex at a time with bucket lists and runs in linear time per pass, and multilevel partitioners such as METIS shrink the graph, split the small version and refine the split with KL or FM passes on the way back up. A spectral split is a common starting point for those passes.
One pass tries a whole sequence of swaps, then keeps the best beginning of it.
KernighanLin(G, A, B): // |A| and |B| differ by at most one
repeat:
D(v) = external(v) - internal(v) for every vertex
unlock all vertices, gains = []
while unlocked pairs remain:
pick unlocked a in A, b in B maximising
g = D(a) + D(b) - 2 w(a, b)
lock a and b, append g to gains
update D as if a and b had swapped
k = the prefix length with the largest sum G of gains
if G > 0: swap the first k pairs for real
until G <= 0
return A, BAfter a and b are locked, every other vertex x changes its D by twice its edge weights to a and b: D(x) grows by 2w(x, a) − 2w(x, b) for x on the side of a, and by 2w(x, b) − 2w(x, a) for x on the side of b. Allowing negative gains inside a pass is what lets the method leave shallow local minima where single improving swaps would get stuck.
The ten-vertex example: two dense groups of five joined by two edges.
Example graph: A, B, C, D, E form a five-cycle with the chords A–C and B–D, and F, G, H, I, J form the same shape with the chords F–H and G–I. B–F and C–J join the two groups. All weights are 1.
Final split {F, G, H, I, J} and {A, B, C, D, E} with cut 2: only B–F and C–J cross. That is optimal here, because each group stays connected after removing any one edge, so any balanced split that divides a group cuts at least 2 of its edges.
Time: O(n³) per pass · Space: O(n²)
A pass makes n/2 choices, and each choice compares up to (n/2)² pairs, so O(n³) per pass as written. Sorting the D values on each side and scanning them in order brings this down to O(n² log n). Every pass that is kept lowers the cut, so with integer weights there are at most as many passes as the starting cut, and in practice a few suffice. The weight matrix takes O(n²).
Other ways to split a graph.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Fiduccia–Mattheyses | Large sparse graphs and hypergraphs. It moves one vertex at a time within a balance tolerance and keeps the gains in buckets. | O(m) per pass |
| Spectral bisection | A global view first. The Fiedler vector gives a good starting split that KL or FM can then polish. | A few sparse matrix products per iteration |
| Stoer–Wagner minimum cut | The halves need not be equal. It finds the true minimum cut, which may cut off a single vertex. | O(n·m + n² log n) |
Related algorithms: Spectral Partitioning (Fiedler Vector), Stoer-Wagner Min Cut, Minimum Cut, Louvain Community Detection, Girvan-Newman Communities