Algorithm Search⌘K

Algorithmcore

Min-cut placement

Also called Breuer min-cut placement.

Breuer's placement that bisects the netlist and the chip region again and again with a min-cut partitioner, until each region holds a few cells.

Solves
Placement
Time
O(p (n + m + P) log S) worst · averageO((n + m + P) log S) best
Space
O(n + m + P) extra
Where
P = the number of pins; S = the number of sites; m = the number of nets; n = the number of cells; p = the largest number of FM passes in one region
Properties
randomizednot exactrecursive
Origin
Melvin A. Breuer, 1977

Min-cut placement solves the placement problem by divide and conquer on the chip. It cuts the placement region into two halves by a line, splits the cells into two groups of the right sizes with a hypergraph partitioner so that few nets cross, and does the same in each half, until each region holds one site or a few cells. Breuer introduced it in 1977. The idea is that every net that crosses a cut line adds to the wire length, so keeping the cuts small keeps the HPWL small. It is a heuristic: each cut is only locally good, and the method has no approximation guarantee.

In plain words

You must seat many guests at a long table so that friends sit close. Split the guests into two groups with few friendships across, and give each group one half of the table. Then split each group again for its half, and so on, until each guest has a chair.

Each split looks only at the guests of one group. A guest who has a friend in the other half of the room would like to sit near that side, but a plain split cannot see this. Terminal propagation fixes that: it adds a dummy guest who stands for the friend outside and is fixed on the right side.

The problem

The input is a netlist of nn unit cells and mm nets with PP pins, fixed pads with integer positions, and a rectangle of W×HW \times H unit sites at the points (x,y)(x, y) with 0≤x<W0 \le x < W and 0≤y<H0 \le y < H, with n≤S=WHn \le S = W H. The output is a legal placement: an injective map of the cells to the sites. The goal, which the method approaches but does not reach in general, is the least total HPWL of the nets. The implementation puts every pin at the position of its cell or pad, with no pin offsets. Rows of cells of different widths change only the capacity of a region; the method stays the same.

The algorithm

The regions are processed in breadth-first order with a queue, so that all regions of one level are cut before the next level starts. Each cell has an estimated position, the center of its current region, which terminal propagation uses for cells outside the region being cut.

  1. MIN-CUT-PLACE(cells, nets, pads, W, H)
  2. 1Q ← a queue with (the whole region, all cells)
  3. 2while Q is not empty
  4. 3(B, C) ← the front of Q, removed
  5. 4if B is one site then place the cell of C there (if any) and continue
  6. 5cut B across its longer side through the middle into B0 and B1, with c0 and c1 sites
  7. 6build the netlist of C: each net restricted to C; with terminal propagation, add a fixed terminal on side s if the other pins of the net lie on side s of the line, and drop the net if they lie on both sides
  8. 7t ← round(|C| c0 / (c0 + c1)); start from a random split with t cells on side 0
  9. 8(C0, C1) ← FM with the fixed terminals and |C0| in [max(t − 1, |C| − c1), min(t + 1, c0)]
  10. 9for s ∈ {0, 1}: set the estimate of each cell of C_s to the center of B_s, and add (B_s, C_s) to Q
  11. 10return the sites of the cells

Line L6 looks at each net with a pin in CC. The pins in CC stay. A pin outside CC is a pad at its position or a cell at its estimate. If its coordinate across the cut lies exactly on the cut line, it is ignored, since it pulls to neither side. If all other outside pins are on side ss, the net gets the fixed terminal of side ss; if they are on both sides, the net is cut whatever the split is, so it is left out. Without terminal propagation, line L6 keeps only the pins in CC.

Line L8 uses Fiduccia-Mattheyses passes with the two terminals fixed. A tentative move may leave the window by one cell, but only a prefix of the pass that ends inside the window is kept. So FM can still swap cells when the window has one value, as for a region of 4 cells and 4 sites. The window is never empty, and it keeps ∣C0∣≤c0|C_0| \le c_0 and ∣C1∣≤c1|C_1| \le c_1.

Example

Place 8 cells on a 4×24 \times 2 grid of sites. A pad LL at (−1,0)(-1, 0) and a pad RR at (4,1)(4, 1). The 13 nets, all 2-pin, are

L0,  01,  02,  12,  13,  23,  34,  45,  46,  56,  57,  67,  7R.L0, \; 01, \; 02, \; 12, \; 13, \; 23, \; 34, \; 45, \; 46, \; 56, \; 57, \; 67, \; 7R.

So cells 0 to 3 form one cluster with pad LL, cells 4 to 7 another with pad RR, and the net 3434 joins them. The trace is the run of the implementation with terminal propagation and random.Random(0).

step region cut line cells terminals result cut
1 x∈[0,3]x \in [0, 3] x=1.5x = 1.5 0 to 7 LL on side 0, RR on side 1 {0,1,2,3}∣{4,5,6,7}\{0,1,2,3\} \mid \{4,5,6,7\} 1
2 x∈[0,1]x \in [0, 1] x=0.5x = 0.5 0 to 3 LL for 0 on side 0, cell 4 at (2.5,0.5)(2.5, 0.5) for 3 on side 1 {0,2}∣{1,3}\{0,2\} \mid \{1,3\} 3
3 x∈[2,3]x \in [2, 3] x=2.5x = 2.5 4 to 7 cell 3 at (1,0.5)(1, 0.5) for 4 on side 0, RR for 7 on side 1 {4,5}∣{6,7}\{4,5\} \mid \{6,7\} 3
4 x=0x = 0 y=0.5y = 0.5 0, 2 LL for 0 on side 0 0 at y=0y = 0, 2 at y=1y = 1 1
5 x=1x = 1 y=0.5y = 0.5 1, 3 cell 0 for 1 on side 0, cell 2 for 1 and 3 on side 1 1 at y=0y = 0, 3 at y=1y = 1 2
6 x=2x = 2 y=0.5y = 0.5 4, 5 cell 3 at (1,1)(1, 1) for 4 on side 1 5 at y=0y = 0, 4 at y=1y = 1 1
7 x=3x = 3 y=0.5y = 0.5 6, 7 cell 5 for 6 and 7 on side 0, cell 4 for 6 and RR for 7 on side 1 7 at y=0y = 0, 6 at y=1y = 1 3

Step 1 has c0=c1=4c_0 = c_1 = 4, so the window is exactly 4; FM goes from the random start, with cut 7, to cut 1. In step 2, the split {0,1}∣{2,3}\{0, 1\} \mid \{2, 3\} also has cut 3, and the random start decides. In step 5 the net 3434 is dropped, because cell 4 has the estimate (2,0.5)(2, 0.5), exactly on the line y=0.5y = 0.5. In step 7 both splits cut 3 nets.

The placement is

x=0x = 0 x=1x = 1 x=2x = 2 x=3x = 3
y=1y = 1 2 3 4 6
y=0y = 0 0 1 5 7

with HPWL 16. The lines x=−0.5,0.5,1.5,2.5,3.5x = -0.5, 0.5, 1.5, 2.5, 3.5 are crossed by 1,3,1,3,11, 3, 1, 3, 1 nets, and the line y=0.5y = 0.5 by 7 nets, so 9+7=169 + 7 = 16, as the identity below says. A brute force over all 8!8! placements gives the optimum 16 for this instance. Without terminal propagation, seeds 0 to 3 give 24, 20, 18 and 24; with it, all four give 16.

Correctness

The proof shows the following.

  • The window. For ∣C∣≤c0+c1|C| \le c_0 + c_1, the window of line L8 contains tt, so the random start lies in it, and every kept split satisfies ∣C0∣≤c0|C_0| \le c_0 and ∣C1∣≤c1|C_1| \le c_1.
  • Legality. By induction on the queue, each region holds at most as many cells as it has sites. Regions of the same level are disjoint, and each cell is in exactly one of them. A region of one site holds at most one cell. So the output is an injective map to sites inside the rectangle.
  • Termination. Each cut is through the middle of a side of length at least 2, so both parts are smaller. The queue becomes empty after at most 2S−12S - 1 regions.
  • HPWL as a sum of cuts. With integer positions, the HPWL of a placement equals the sum, over the lines x=j+12x = j + \frac12 and y=j+12y = j + \frac12, of the number of nets with pins on both sides of the line. Each cut of line L8 tries to make one term of this sum small.
  • Terminal propagation counts exactly. If all outside pins of every net were at their final positions, the cut of line L8 with the terminals would be the number of nets of the region that cross the line, less the number of dropped nets, which does not depend on the split. So both have the same best splits. The terminal propagation page discusses this. The estimates are only centers of regions, so in the algorithm this is a model, not a guarantee.
Proof: Min-cut placement returns a legal placement, stops after at most 2S - 1 regions, and its HPWL is a sum of cut countsfull

The sites are the integer points (x,y)(x, y) with 0≤x<W0 \le x < W and 0≤y<H0 \le y < H, and S=WHS = W H. A region is a rectangle of sites [x0,x1)×[y0,y1)[x_0, x_1) \times [y_0, y_1). For a region BB with the cells CC, line L5 cuts the longer side through the middle into B0B_0 and B1B_1, with c0c_0 and c1c_1 sites, and line L7 sets t=round(kc0/(c0+c1))t = \mathrm{round}(k c_0 / (c_0 + c_1)) with k=∣C∣k = |C|. The window of line L8 is [lo,hi][lo, hi] with lo=max⁡(k−c1,t−1,0)lo = \max(k - c_1, t - 1, 0) and hi=min⁡(c0,t+1,k)hi = \min(c_0, t + 1, k).

Lemma 1 (window). If k≤c0+c1k \le c_0 + c_1, then lo≤t≤hilo \le t \le hi.

Proof. Let q=kc0/(c0+c1)q = k c_0 / (c_0 + c_1). Rounding gives ∣t−q∣≤12|t - q| \le \frac12. Since k≤c0+c1k \le c_0 + c_1, we have q≤c0q \le c_0, so t≤c0+12t \le c_0 + \frac12, and t≤c0t \le c_0 because tt is an integer. In the same way k−q=kc1/(c0+c1)≤c1k - q = k c_1 / (c_0 + c_1) \le c_1, so k−t≤k−q+12≤c1+12k - t \le k - q + \frac12 \le c_1 + \frac12, and k−t≤c1k - t \le c_1. Also 0≤q≤k0 \le q \le k gives 0≤t≤k0 \le t \le k. The terms t−1t - 1 and t+1t + 1 hold trivially. So every term of the maximum is at most tt, and every term of the minimum is at least tt. □\square

Lemma 2 (FM keeps the window). If the start has ∣C0∣∈[lo,hi]|C_0| \in [lo, hi], the split returned by line L8 has ∣C0∣∈[lo,hi]|C_0| \in [lo, hi].

Proof. A pass keeps the prefix of its tentative moves with the largest total gain among the prefixes that end with ∣C0∣∈[lo,hi]|C_0| \in [lo, hi], and the empty prefix is one of them because the pass starts inside the window. So each pass ends inside the window, and the next pass starts there. The fixed terminals never move and are not counted in ∣C0∣|C_0|. FM stops because a new pass starts only after a pass with positive total gain, and the integer cut falls by at least 1 each time. □\square

The start of line L7 has exactly tt cells on side 0, which is in the window by Lemma 1. So with Lemma 2 every split satisfies ∣C0∣≤c0|C_0| \le c_0 and ∣C1∣=k−∣C0∣≤c1|C_1| = k - |C_0| \le c_1.

Theorem 1 (legality). If n≤Sn \le S, the output places every cell on a site of the rectangle, and no two cells on the same site.

Proof. Call a pair (B,C)(B, C) in the queue good if ∣C∣|C| is at most the number of sites of BB. The first pair is good because n≤Sn \le S. If (B,C)(B, C) is good and is cut, Lemmas 1 and 2 show that both pairs (Bs,Cs)(B_s, C_s) are good. Consider the tree of all regions put in the queue, where the children of a cut region are B0B_0 and B1B_1. The two children are disjoint and their union is the parent, so two regions of the tree are either nested or disjoint. The cells of a cut region go to exactly one child, so at each time every cell is in exactly one region of the queue, or has a site. A cell leaves the queue only through line L4, and line L4 handles only regions of one site. A good pair with one site has at most one cell, so line L4 never puts two cells of one region on its site. Two different one-site regions are leaves of the tree, so they are disjoint and are different sites. By Theorem 2 the queue becomes empty, so every cell gets a site. ■\blacksquare

Theorem 2 (termination). The loop of lines L2 to L9 runs at most 2S−12S - 1 times, and every root to leaf path of the region tree has at most ⌈log⁡2W⌉+⌈log⁡2H⌉\lceil \log_2 W \rceil + \lceil \log_2 H \rceil cuts.

Proof. A region that is cut is not one site, so its longer side has length L≥2L \ge 2, and the cut makes parts with sides ⌊L/2⌋≥1\lfloor L / 2 \rfloor \ge 1 and ⌈L/2⌉<L\lceil L / 2 \rceil < L. So both children are nonempty rectangles with fewer sites. The region tree is a binary tree in which each inner node has exactly two children. Its leaves are pairwise disjoint nonempty sets of sites, so there are at most SS leaves, and at most 2S−12S - 1 nodes. Each node is removed from the queue once. On a path from the root, each vertical cut replaces a width w≥2w \ge 2 by at most ⌈w/2⌉\lceil w / 2 \rceil, so after ⌈log⁡2W⌉\lceil \log_2 W \rceil vertical cuts the width is 1 and no more vertical cut happens (line L5 cuts vertically only when the width is the longer side, and a region of width 1 and height 1 is not cut). The same holds for the height. □\square

Lemma 3 (HPWL as a sum of cut counts). Let all pins have integer positions. For an integer jj, let XjX_j be the number of nets with pins on both sides of the line x=j+12x = j + \frac12, and define YjY_j in the same way. Then the HPWL is ∑jXj+∑jYj\sum_j X_j + \sum_j Y_j.

Proof. Take one net with smallest and largest xx coordinates a≤ba \le b. It has pins on both sides of x=j+12x = j + \frac12 exactly when a<j+12<ba < j + \frac12 < b, that is a≤j≤b−1a \le j \le b - 1. There are b−ab - a such jj, which is the xx part of its bounding box. The same argument holds for yy. Add over all nets and change the order of the sums. □\square

Every cell of CsC_s is later placed inside BsB_s by Theorem 1, so it ends on side ss of the cut line. So the cut that line L8 makes small is a part of one term XjX_j or YjY_j: the nets of CC that will cross that line.

Lemma 4 (terminal propagation). Suppose that every pin outside CC is at a fixed position that is not on the cut line. For a split (C0,C1)(C_0, C_1), let XX be the number of nets with a pin in CC that have pins on both sides of the line when the cells of CsC_s are on side ss, and let ZZ be the cut of the netlist built in line L6. Then X=Z+DX = Z + D, where DD is the number of nets left out because they have outside pins on both sides. DD does not depend on the split.

Proof. Take a net ee with a pin in CC. If it has no outside pins, ee crosses the line exactly when its pins in CC are split, and the restricted net is cut exactly then (a net with one pin is never split and is left out). If all its outside pins are on side ss, ee crosses the line exactly when some pin in CC is on side 1−s1 - s, and that is exactly when the restricted net with the terminal of side ss is cut. If it has outside pins on both sides, it crosses the line for every split, and it is left out of the netlist. Add over the nets. □\square

So under the assumption of Lemma 4, the splits of least cut in line L8 are exactly the splits with the fewest crossing nets. In the algorithm the outside cells are at the centers of their regions, not at their final sites, so the assumption is false in general, and the lemma explains the method but gives no bound.

Running time. By Theorem 2 there are O(log⁡S)O(\log S) levels. The regions of one level have disjoint cell sets. Line L6 visits each pin of each net with a pin in CC, and adds at most one terminal per net. With a mark per net, a net is handled once per region it touches, and a net with ∣e∣|e| pins touches at most ∣e∣|e| regions of a level, so the netlists of a level have O(n+m+P)O(n + m + P) pins in all. An FM pass with gain buckets costs time linear in the size of its netlist (see the Fiduccia-Mattheyses page), so a level costs O(p(n+m+P))O(p (n + m + P)) if each region uses at most pp passes, and the total is O(p(n+m+P)log⁡S)O(p (n + m + P) \log S). The queue holds the regions of at most two levels, with disjoint cell sets, so the space is O(n+m+P)O(n + m + P). The implementation picks each move by a scan of the free cells and rebuilds each restricted net from its full pin list, so a region with kk cells costs O(k2+∑e∣e∣2)O(k^2 + \sum_e |e|^2) per pass over the nets ee of the region.

What is not proved. Nothing above compares the HPWL of the output with the minimum. Each cut is only a local optimum of FM, and no approximation ratio of min-cut placement for HPWL is known.

Complexity

  • Levels. A cut halves the longer side, so after at most ⌈log⁡2W⌉+⌈log⁡2H⌉\lceil \log_2 W \rceil + \lceil \log_2 H \rceil levels every region is one site. This is O(log⁡S)O(\log S).
  • One level. The regions of a level hold disjoint cells. Building their netlists in line L6 visits each pin of each net with a pin in the region, and each net gets at most one terminal, so a level has O(n+m+P)O(n + m + P) pins in total with marking of the nets. With gain buckets, an FM pass costs time linear in the size of its netlist, so a level costs O(p(n+m+P))O(p (n + m + P)) for at most pp passes per region.
  • Total. O(p(n+m+P)log⁡S)O(p (n + m + P) \log S) time and O(n+m+P)O(n + m + P) space, since the regions of a level can be processed one by one.
  • The implementation finds the move of largest gain by a scan, and line L6 recomputes each restricted net from the full pin list, so a region with kk cells costs O(k2+∑e∣e∣2)O(k^2 + \sum_e |e|^2) per FM pass. This is fine for the toy inputs of the tests.

Properties

Min-cut placement is a heuristic for an NP-hard problem. What is proved and what is measured are kept apart.

What is proved. Legality, termination and the cost above; the identity between HPWL and the sum of crossing counts; and the exact count of terminal propagation, up to a constant, when the outside pins are where they are assumed to be. No approximation ratio for HPWL is known. Each cut is a local optimum of FM, not a minimum cut, and a minimum cut at one level can force a bad cut at the next.

What is measured. The numbers below are from the implementation of this page, on toy inputs, and are not benchmark results. Published placers based on min-cut partitioning were compared on the ISPD 2005 and 2006 placement contest benchmarks; see those contests for the numbers.

  • Tiny grids. On 100 random instances with 6 cells on a 3×23 \times 2 grid, 8 nets and 0 to 3 pads outside the grid (generator random.Random(2026), run tt seeded with random.Random(t)), compared with a brute force over all placements: with terminal propagation the result was optimal in 53 instances, with mean ratio 1.036 to the optimum; without it, optimal in 7, with mean ratio 1.171.
  • A hidden grid. 20 netlists (generator random.Random(2027)), each the 8×88 \times 8 grid graph on 64 cells in a random order, with 112 two-pin nets and 16 pad nets to pads just outside the edge. Each net has length at least 1, and the hidden layout reaches 128, so the optimum is exactly 128. With terminal propagation the mean HPWL was 146.3, and two of the first three runs reached 128. Without it the mean was 392.4. Terminal propagation won in all 20.

Variants and improvements

  • Cut sequences. Breuer compared quadrature (alternate vertical and horizontal cuts through the middle, as here), bisection (cut the region into rows first, then cut each row), and slice/bisection. The order of the cut lines changes the result.
  • Multilevel partitioners. Modern min-cut placers such as Capo use multilevel hypergraph partitioning for each cut, with fixed terminals, and a tolerance on the sizes.
  • Unequal cuts and whitespace. A cut line need not be in the middle; it can follow the sizes of the two cell groups, so that the free space spreads evenly.
  • End cases. Stop at regions of a few cells and place them by exhaustive search or by a small branch and bound, then legalize and run detailed placement.
  • k-way cuts. Cut a region into four parts at once (quadrisection), which sees both directions at the same time.
  • Hybrids. Use min-cut placement to spread cells in an analytical or quadratic placer, or use quadratic placement to estimate the positions for terminal propagation.

When to use it

Use min-cut placement when you have a good hypergraph partitioner and want a placement that is legal by construction, that scales to large netlists, and that tends to keep the routing demand across each cut line small. In the ISPD 2005 and 2006 placement contests, analytical placers had the best results, so min-cut is now used mostly inside other flows, for floorplans and for the end cases. The recursive cuts also build a slicing floorplan of the chip.

Pitfalls

  • No terminal propagation. Without it, each cut ignores the cells outside the region, and the two halves of a cluster can end up far apart. The toy measurements above show the size of the effect.
  • Balance too tight or too loose. An exact window gives FM no room to move; too much slack lets one side take more cells than its sites. Bound the window by the capacities, as line L8 does.
  • Pins on the cut line. A pin exactly on the line pulls to neither side. Counting it on one side biases the cut.
  • Nets with outside pins on both sides. Such a net is cut in every split. Giving it two terminals adds a constant to every cut, which is harmless, but giving it one terminal is wrong.
  • Depth first order. If the regions are cut depth first, the estimates of the cells in regions not yet cut are coarse, and terminal propagation sees less. Breadth first order makes all estimates of one level equally fine.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

min_cut_placement.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/min-cut-placement/. The authors are listed in prov/S0NJTB.jsonl.

import random
from collections import deque
from fractions import Fraction
from typing import Sequence

Point = tuple[int, int]


def hpwl(pos: Sequence[Point], nets: Sequence[Sequence[int]]) -> int:
    """Total half-perimeter wirelength; pins sit at the cell or pad position (no pin offsets)."""
    total = 0
    for e in nets:
        xs = [pos[p][0] for p in e]
        ys = [pos[p][1] for p in e]
        total += max(xs) - min(xs) + max(ys) - min(ys)
    return total


def fm_with_fixed(
    k: int,
    nets: Sequence[Sequence[int]],
    fixed: dict[int, int],
    side: Sequence[int],
    lo: int,
    hi: int,
) -> list[int]:
    """Fiduccia-Mattheyses passes on vertices 0 .. k-1 plus fixed vertices.

    fixed maps the extra vertex ids (k and up) to their side; they never move
    and do not count for the balance. A tentative move may take the number of
    free vertices on side 0 one step outside [lo, hi], but only a prefix that
    ends inside [lo, hi] is applied, so exact balance (lo = hi) still allows
    swaps. The vertex of largest gain is found by a scan.
    """
    total = k + len(fixed)
    side = list(side) + [0] * len(fixed)
    for v, s in fixed.items():
        side[v] = s
    vnets: list[list[int]] = [[] for _ in range(total)]
    for i, e in enumerate(nets):
        for v in e:
            vnets[v].append(i)
    while True:
        cnt = [[0, 0] for _ in nets]
        for i, e in enumerate(nets):
            for v in e:
                cnt[i][side[v]] += 1
        gain = [0] * total
        for v in range(k):
            s = side[v]
            for i in vnets[v]:
                gain[v] += (cnt[i][s] == 1) - (cnt[i][1 - s] == 0)
        locked = [False] * k + [True] * len(fixed)
        c0 = sum(1 for v in range(k) if side[v] == 0)
        moves: list[int] = []
        run, best_run, best_k = 0, 0, 0
        while True:
            pick = -1
            for v in range(k):
                if locked[v]:
                    continue
                nc0 = c0 - 1 if side[v] == 0 else c0 + 1
                if lo - 1 <= nc0 <= hi + 1 and (pick < 0 or gain[v] > gain[pick]):
                    pick = v
            if pick < 0:
                break
            v, f = pick, side[pick]
            t = 1 - f
            locked[v] = True
            for i in vnets[v]:
                e = nets[i]
                if cnt[i][t] == 0:
                    for u in e:
                        if not locked[u]:
                            gain[u] += 1
                elif cnt[i][t] == 1:
                    for u in e:
                        if side[u] == t and not locked[u]:
                            gain[u] -= 1
                cnt[i][f] -= 1
                cnt[i][t] += 1
                if cnt[i][f] == 0:
                    for u in e:
                        if not locked[u]:
                            gain[u] -= 1
                elif cnt[i][f] == 1:
                    for u in e:
                        if side[u] == f and u != v and not locked[u]:
                            gain[u] += 1
            run += gain[v]
            side[v] = t
            c0 += -1 if f == 0 else 1
            moves.append(v)
            if lo <= c0 <= hi and run > best_run:
                best_run, best_k = run, len(moves)
        for v in moves[best_k:]:
            side[v] = 1 - side[v]
        if best_run <= 0:
            return side[:k]


def min_cut_placement(
    n: int,
    nets: Sequence[Sequence[int]],
    pads: Sequence[Point],
    width: int,
    height: int,
    rng: random.Random,
    terminal_propagation: bool = True,
) -> list[Point]:
    """Place n unit cells on the sites (x, y), 0 <= x < width, 0 <= y < height.

    Pin ids 0 .. n-1 are cells and n + j is the fixed pad at pads[j]. Regions
    are cut in breadth-first order across their longer side, through the
    middle, and the cells of each region are bisected by FM in proportion to
    the site counts, within one cell. With terminal propagation, every net
    with pins outside the region gets a fixed terminal on the side of the cut
    where those pins are (pads at their position, other cells at the center
    of their current region); a net with outside pins on both sides is always
    cut and is left out, and pins exactly on the cut line are ignored.

    Returns the site of each cell. Cost: at most ceil(log2 width) +
    ceil(log2 height) levels of regions; building the subnetlists of a level
    costs O(sum of |e|^2 over the nets), and an FM pass in a region of k
    cells costs O(k^2 + sum of |e|^2 over its nets) with the scan.
    """
    if n > width * height:
        raise ValueError("more cells than sites")
    est: list[tuple[Fraction, Fraction]] = [(Fraction(width - 1, 2), Fraction(height - 1, 2))] * n
    pos: list[Point] = [(-1, -1)] * n
    pin_nets: list[list[int]] = [[] for _ in range(n)]
    for i, e in enumerate(nets):
        for p in e:
            if p < n:
                pin_nets[p].append(i)
    queue = deque([(0, width, 0, height, list(range(n)))])  # @line L1
    while queue:  # @line L2
        x0, x1, y0, y1, cells = queue.popleft()  # @line L3
        if not cells:
            continue
        if x1 - x0 == 1 and y1 - y0 == 1:  # @line L4
            pos[cells[0]] = (x0, y0)
            continue
        vertical = x1 - x0 >= y1 - y0  # @line L5
        if vertical:
            xm = x0 + (x1 - x0) // 2
            line = Fraction(2 * xm - 1, 2)
            sub = [(x0, xm, y0, y1), (xm, x1, y0, y1)]
        else:
            ym = y0 + (y1 - y0) // 2
            line = Fraction(2 * ym - 1, 2)
            sub = [(x0, x1, y0, ym), (x0, x1, ym, y1)]
        caps = [(b[1] - b[0]) * (b[3] - b[2]) for b in sub]
        local = {c: j for j, c in enumerate(cells)}
        k = len(cells)
        fixed: dict[int, int] = {}
        sub_nets: list[list[int]] = []
        seen: set[int] = set()
        for c in cells:  # @line L6
            for i in pin_nets[c]:
                if i in seen:
                    continue
                seen.add(i)
                inner = sorted({local[p] for p in nets[i] if p < n and p in local})
                if terminal_propagation:
                    sides = set()
                    for p in nets[i]:
                        if p < n and p in local:
                            continue
                        q = (Fraction(pads[p - n][0]), Fraction(pads[p - n][1])) if p >= n else est[p]
                        coord = q[0] if vertical else q[1]
                        if coord != line:
                            sides.add(0 if coord < line else 1)
                    if len(sides) == 2:
                        continue
                    if sides:
                        s = sides.pop()
                        term = k + s
                        fixed[term] = s
                        inner.append(term)
                if len(inner) >= 2:
                    sub_nets.append(inner)
        # The two terminal ids k and k + 1 must both exist for fm_with_fixed.
        fixed.setdefault(k, 0)
        fixed.setdefault(k + 1, 1)
        t = round(Fraction(k * caps[0], caps[0] + caps[1]))  # @line L7
        lo, hi = max(k - caps[1], t - 1, 0), min(caps[0], t + 1, k)
        start = [0] * t + [1] * (k - t)
        rng.shuffle(start)
        side = fm_with_fixed(k, sub_nets, fixed, start, lo, hi)  # @line L8
        for s in (0, 1):  # @line L9
            group = [cells[j] for j in range(k) if side[j] == s]
            bx0, bx1, by0, by1 = sub[s]
            for c in group:
                est[c] = (Fraction(bx0 + bx1 - 1, 2), Fraction(by0 + by1 - 1, 2))
            queue.append((bx0, bx1, by0, by1, group))
    return pos  # @line L10
test_min_cut_placement.py 140 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/min-cut-placement/. The authors are listed in prov/S0NJTB.jsonl.

import random
from itertools import permutations

from min_cut_placement import fm_with_fixed, hpwl, min_cut_placement


def direct_hpwl(pos, nets) -> int:
    """HPWL by a direct sum over pairs of pins: the largest x gap plus the largest y gap."""
    total = 0
    for e in nets:
        total += max(abs(pos[a][0] - pos[b][0]) for a in e for b in e)
        total += max(abs(pos[a][1] - pos[b][1]) for a in e for b in e)
    return total


def crossings(pos, nets) -> int:
    """Sum over the lines x = j + 1/2 and y = j + 1/2 of the number of nets that cross the line."""
    xs = [p[0] for p in pos]
    ys = [p[1] for p in pos]
    total = 0
    for j in range(min(xs) - 1, max(xs) + 1):
        total += sum(1 for e in nets if min(pos[p][0] for p in e) <= j < max(pos[p][0] for p in e))
    for j in range(min(ys) - 1, max(ys) + 1):
        total += sum(1 for e in nets if min(pos[p][1] for p in e) <= j < max(pos[p][1] for p in e))
    return total


def brute_force(n, nets, pads, width, height) -> int:
    sites = [(x, y) for x in range(width) for y in range(height)]
    best = None
    for perm in permutations(sites, n):
        h = hpwl(list(perm) + list(pads), nets)
        if best is None or h < best:
            best = h
    return best


def random_instance(rng, n, width, height, m):
    q = rng.randint(0, 3)
    pads = []
    for _ in range(q):
        if rng.random() < 0.5:
            pads.append((rng.choice([-1, width]), rng.randrange(height)))
        else:
            pads.append((rng.randrange(width), rng.choice([-1, height])))
    pins = n + q
    nets = []
    for _ in range(m):
        size = rng.choice([2, 2, 3, 3, 4])
        e = rng.sample(range(pins), min(size, pins))
        if any(p < n for p in e) and len(e) >= 2:
            nets.append(e)
    return pads, nets


def check_legal(pos, n, width, height):
    assert len(pos) == n
    assert len(set(pos)) == n
    for x, y in pos:
        assert 0 <= x < width and 0 <= y < height


def page_example():
    # 8 cells on a 4 x 2 grid, pad 8 at the left (x = -1) and pad 9 at the right (x = 4).
    nets = [[8, 0], [0, 1], [0, 2], [1, 2], [1, 3], [2, 3], [3, 4], [4, 5], [4, 6], [5, 6], [5, 7], [6, 7], [7, 9]]
    pads = [(-1, 0), (4, 1)]
    return 8, nets, pads, 4, 2


def test_page_example():
    n, nets, pads, w, h = page_example()
    pos = min_cut_placement(n, nets, pads, w, h, random.Random(1))
    check_legal(pos, n, w, h)
    full = pos + pads
    assert hpwl(full, nets) == direct_hpwl(full, nets) == crossings(full, nets)
    assert hpwl(full, nets) >= brute_force(n, nets, pads, w, h)


def test_hpwl_identity_and_direct_sum():
    rng = random.Random(3)
    for _ in range(300):
        n = rng.randint(1, 8)
        w, h = rng.randint(1, 4), rng.randint(1, 4)
        if n > w * h:
            continue
        pads, nets = random_instance(rng, n, w, h, rng.randint(0, 10))
        sites = rng.sample([(x, y) for x in range(w) for y in range(h)], n)
        full = sites + pads
        assert hpwl(full, nets) == direct_hpwl(full, nets) == crossings(full, nets)


def test_fm_with_fixed_keeps_balance_and_never_worse():
    rng = random.Random(9)
    for _ in range(300):
        k = rng.randint(1, 9)
        nets = []
        for _ in range(rng.randint(0, 12)):
            e = sorted(set(rng.sample(range(k + 2), min(k + 2, rng.randint(2, 4)))))
            if any(v < k for v in e):
                nets.append(e)
        t = rng.randint(0, k)
        side = [0] * t + [1] * (k - t)
        rng.shuffle(side)
        fixed = {k: 0, k + 1: 1}

        def cut(s):
            full = list(s) + [0, 1]
            return sum(1 for e in nets if len({full[v] for v in e}) == 2)

        out = fm_with_fixed(k, nets, fixed, side, t, t)
        assert out.count(0) == t
        assert cut(out) <= cut(side)


def test_against_brute_force_on_small_grids():
    rng = random.Random(2026)
    for trial in range(120):
        w, h = rng.choice([(1, 3), (2, 2), (3, 1), (2, 3), (3, 2), (1, 5), (4, 1)])
        n = rng.randint(1, w * h)
        pads, nets = random_instance(rng, n, w, h, rng.randint(1, 8))
        for tp in (True, False):
            pos = min_cut_placement(n, nets, pads, w, h, random.Random(trial), terminal_propagation=tp)
            check_legal(pos, n, w, h)
            full = pos + pads
            got = hpwl(full, nets)
            assert got == direct_hpwl(full, nets) == crossings(full, nets)
            assert got >= brute_force(n, nets, pads, w, h)


def test_larger_grid_is_legal():
    rng = random.Random(4)
    for _ in range(10):
        w, h = rng.randint(3, 6), rng.randint(3, 6)
        n = rng.randint(1, w * h)
        pads, nets = random_instance(rng, n, w, h, 2 * n)
        pos = min_cut_placement(n, nets, pads, w, h, rng)
        check_legal(pos, n, w, h)

History

Breuer (1977) proposed placing a circuit by repeated min-cut partitioning, with several orders of cut lines, and related the cut values to the wire length. Lauther (1979) used min-cut placement for building blocks. Dunlop and Kernighan (1985) added terminal propagation and applied the method to standard cells, with a Kernighan-Lin style partitioner. In the late 1990s, multilevel partitioners such as hMETIS made min-cut placement fast on large designs, and Capo (Caldwell, Kahng and Markov, 2000) was a widely used min-cut placer.

References

  1. M. A. Breuer, Min-cut placement, Journal of Design Automation and Fault-Tolerant Computing 1(4), 343 to 362, 1977, the placement by repeated min-cut partitioning, the cut line sequences (quadrature, bisection and slice/bisection), and the relation between the cut values and the wire length
  2. M. A. Breuer, A class of min-cut placement algorithms, Proceedings of the 14th Design Automation Conference, 284 to 290, 1977, the conference version of the min-cut placement procedures
  3. Alfred E. Dunlop and Brian W. Kernighan, A procedure for placement of standard-cell VLSI circuits, IEEE Transactions on Computer-Aided Design of Integrated Circuits and Systems 4(1), 92 to 98, 1985, terminal propagation, and min-cut placement of standard cells with Kernighan-Lin style partitioning
  4. C. M. Fiduccia and R. M. Mattheyses, A linear-time heuristic for improving network partitions, 19th Design Automation Conference, 175 to 181, 1982, the partitioner used at each cut, with the balance condition
  5. Andrew B. Kahng, Jens Lienig, Igor L. Markov and Jin Hu, VLSI Physical Design, From Graph Partitioning to Timing Closure, second edition, Springer, 2022, Chapter 4, Global and Detailed Placement (min-cut placement; general locator)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.average
  • a full proof of time.best
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example