learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Algorithm Selection

Select an algorithm and generate steps to begin visualization

Kernighan-Lin Graph Partitioning Visualizer

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

Time: O(n³) per pass
Space: O(n²)
Use Case: Circuit layout (VLSI), parallel computing, community detection, load balancing
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Kernighan–Lin Bisection

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.

How it works

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.

Applications

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.

Pseudocode

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, B

After 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.

Worked example, step by step

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.

  1. A poor start. Alternating the vertices gives {A, C, E, G, I} and {B, D, F, H, J}, with 9 edges cut.
  2. Pass 1, first swaps. Swapping C and H gains 3, then A and F gains 1, then E and J gains 3, and the running total reaches 7.
  3. Pass 1, the rest. The last two pairs are still swapped and locked: G and B loses 5, I and D loses 2, and the total ends at 0, as it always does once every vertex has changed sides.
  4. The best prefix. The first three swaps give the largest total, 7. They are kept, the last two are undone, and the cut drops from 9 to 2.
  5. Pass 2. From {F, G, H, I, J} against {A, B, C, D, E}, the best first swap, J with B, already loses 3, and the running total never rises above 0. No prefix helps, so the run stops.

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.

Complexity, and where it comes from

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²).

When to use Kernighan–Lin Bisection, and when not to

Other ways to split a graph.

AlternativePrefer it whenCost
Fiduccia–MattheysesLarge 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 bisectionA 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 cutThe halves need not be equal. It finds the true minimum cut, which may cut off a single vertex.O(n·m + n² log n)

Common pitfalls

  • Stopping at the first negative gain. A pass must go on through negative gains and only then pick the best prefix. A swap that costs 2 can open the way for one that gains 5, and stopping early never sees it.
  • Forgetting to update D. After each tentative swap the D values of all unlocked vertices change. Using the values from the start of the pass gives wrong gains for every later pair.
  • Trusting one run. The result depends on the starting split and is only a local optimum. Run from several random starts, or from a spectral split, and keep the best.

Frequently asked questions

What is D in Kernighan–Lin?
For a vertex v, D(v) is the weight of its edges to the other half minus the weight of its edges to its own half. Moving v alone would lower the cut by D(v), and swapping a and b lowers it by D(a) + D(b) − 2w(a, b), since an edge between them stays cut.
Can Kernighan–Lin split into more than two parts?
Yes, by recursive bisection: split in two, then split each half, and so on. Multilevel tools such as METIS do this and refine every level with KL-style passes.
Is Kernighan–Lin a community detection method?
It can serve as one, with the cut or modularity as the target. It needs the number and sizes of the groups in advance, which methods such as Louvain do not.

Related algorithms: Spectral Partitioning (Fiedler Vector), Stoer-Wagner Min Cut, Minimum Cut, Louvain Community Detection, Girvan-Newman Communities

An example graph: you can change it, or start blank.

Zoom Controls

30%
Nodes: 10
Edges: 16