Algorithm Search⌘K

Algorithmadvanced

Prim-Dijkstra tradeoff

Also called PD tree, Alpert Prim-Dijkstra.

Alpert and coauthors' tree builder with a parameter c in [0, 1] that blends Prim's minimum spanning tree and Dijkstra's shortest path tree to trade wire length for radius.

Time
Θ(n^2) worst
Space
Θ(n) extra
Where
n = the number of pins
Properties
deterministicnot exactiterative
Origin
Charles J. Alpert, T. C. Hu, J. H. Huang, Andrew B. Kahng, David Karger, 1993

The Prim-Dijkstra tradeoff of Charles Alpert, T. C. Hu, J. H. Huang, Andrew Kahng and David Karger builds a routing tree for a net in electronic design automation (EDA) that is short and also has short paths from the driver. It grows a tree from the source pin like Prim's algorithm and Dijkstra's algorithm, but it adds the pin vv and tree point uu that minimize c⋅d(u)+dist(u,v)c \cdot d(u) + \mathrm{dist}(u, v), where d(u)d(u) is the path length from the source to uu in the tree and c∈[0,1]c \in [0, 1] is a parameter. With c=0c = 0 this is Prim's algorithm and gives a minimum spanning tree. With c=1c = 1 it is Dijkstra's algorithm and gives a shortest path tree. Values in between trade total wire, which sets the capacitance, against the radius, the longest source-to-sink path, which sets the delay to the far sinks.

In plain words

A net has one driver and several sinks. The minimum spanning tree uses the least wire, but a sink can sit at the end of a long detour, and its signal arrives late. Wiring each sink straight to the driver gives the shortest paths, but uses a lot of wire.

Build the tree one sink at a time from the driver. Prim's rule adds the sink closest to the tree. Dijkstra's rule adds the sink whose path from the driver would be shortest.

The Prim-Dijkstra rule mixes the two. It adds the sink with the smallest value of "the wire to reach it" plus cc times "the path from the driver to where it joins". A small cc cares mostly about wire, a large cc mostly about path length. A designer tries a few values of cc and keeps the best tree.

The problem

The input is a set of nn points p0,…,pn−1p_0, \ldots, p_{n-1} with a source s=p0s = p_0, and a metric dist\mathrm{dist}, in routing the L1 distance ∣xu−xv∣+∣yu−yv∣|x_u - x_v| + |y_u - y_v|. The output is a spanning tree rooted at ss. Its cost is the sum of its edge lengths, and its radius is the largest path length d(v)d(v) from ss to a point vv in the tree. Let R=max⁡vdist(s,v)R = \max_v \mathrm{dist}(s, v). No tree has radius less than RR, and no tree has cost less than the minimum spanning tree MST\mathrm{MST}. In general no single tree reaches both, so the goal is a good tradeoff, as in the bounded-radius bounded-cost tree.

The algorithm

  1. PRIM-DIJKSTRA(points, s, c)
  2. 1d(s) ← 0; T ← {s}; for each v ≠ s: key(v) ← dist(s, v), best(v) ← s
  3. 2repeat n − 1 times
  4. 3v ← the point not in T with the least key(v), ties to the smaller index
  5. 4u ← best(v); add v to T with parent u; d(v) ← d(u) + dist(u, v)
  6. 5for each point w not in T do
  7. 6if c · d(v) + dist(v, w) < key(w) then key(w) ← c · d(v) + dist(v, w); best(w) ← v
  8. 7return the parents and the path lengths d

The keys are those of Prim's algorithm with an array, so each step takes O(n)O(n) time. The key of ww is min⁡u∈Tc⋅d(u)+dist(u,w)\min_{u \in T} c \cdot d(u) + \mathrm{dist}(u, w) at all times, because d(u)d(u) does not change once uu is in the tree.

Example

Take nine points that run up, across and down, with the source at the bottom left:

p0=(0,0)p_0 = (0, 0), p1=(1,3)p_1 = (1, 3), p2=(2,6)p_2 = (2, 6), p3=(3,9)p_3 = (3, 9), p4=(6,9)p_4 = (6, 9), p5=(9,9)p_5 = (9, 9), p6=(9,6)p_6 = (9, 6), p7=(9,3)p_7 = (9, 3), p8=(9,0)p_8 = (9, 0).

The minimum spanning tree is the chain p0,p1,…,p8p_0, p_1, \ldots, p_8, of cost 27. In it the last point p8p_8 is 27 away from the source along the tree, though it is only 9 away in the plane. The largest distance from the source is R=18R = 18, to p5p_5.

cc parents of p1,…,p8p_1, \ldots, p_8 cost radius
0 p0,p1,p2,p3,p4,p5,p6,p7p_0, p_1, p_2, p_3, p_4, p_5, p_6, p_7 27 27
1/4 p0,p1,p2,p3,p4,p5,p6,p0p_0, p_1, p_2, p_3, p_4, p_5, p_6, p_0 33 24
1/2 p0,p1,p2,p3,p4,p7,p8,p0p_0, p_1, p_2, p_3, p_4, p_7, p_8, p_0 33 18
3/4 the same as for 1/2 33 18
1 p0p_0 for all 93 18

With c=1/4c = 1/4, the point p8p_8 is joined straight to the source. With c=1/2c = 1/2, the right side of the net hangs from p8p_8, so both sides are reached by short paths. This tree has the optimal radius 18 at cost 33, while the shortest path tree of c=1c = 1 has the same radius at cost 93: in the L1 metric many shortest path trees exist, and Dijkstra's rule with its ties picks the star.

Correctness

The proof shows the following.

  • A spanning tree. Each step adds one new point with a parent already in the tree, so the result is a spanning tree rooted at ss, and d(v)d(v) is the length of its tree path.
  • The keys are right. Line L6 keeps key(w)=min⁡u∈Tc⋅d(u)+dist(u,w)\mathrm{key}(w) = \min_{u \in T} c \cdot d(u) + \mathrm{dist}(u, w).
  • The two ends. With c=0c = 0 the key is the distance to the tree, so the result is Prim's minimum spanning tree. With c=1c = 1 the key is d(u)+dist(u,w)d(u) + \mathrm{dist}(u, w), so the result is Dijkstra's shortest path tree of the complete graph, and d(v)=dist(s,v)d(v) = \mathrm{dist}(s, v) for every vv by the triangle inequality.
  • A radius bound. For every c∈(0,1]c \in (0, 1] and every point, c⋅d(v)≤dist(s,v)c \cdot d(v) \le \mathrm{dist}(s, v), so the radius is at most R/cR / c.
Proof: The Prim-Dijkstra tree is a spanning tree, it is Prim's tree for c = 0 and a shortest path tree for c = 1, and its radius is at most R / cfull

Let p0,…,pn−1p_0, \ldots, p_{n-1} be points with source ss, and let dist\mathrm{dist} be a metric, so it satisfies the triangle inequality. Fix c∈[0,1]c \in [0, 1]. Write TkT_k for the set of points in the tree after kk steps of line L2, so T0={s}T_0 = \{s\}.

Lemma 1 (spanning tree). After the loop, the parents form a spanning tree rooted at ss, and d(v)d(v) is the length of the tree path from ss to vv.

Proof. Each step adds one point v∉Tv \notin T with parent u=best(v)∈Tu = \mathrm{best}(v) \in T, by Lemma 2 below. So the parent edges never close a cycle, and after n−1n - 1 steps all nn points are in TT. By induction on the steps, d(v)=d(u)+dist(u,v)d(v) = d(u) + \mathrm{dist}(u, v) is the tree path length, since d(u)d(u) already is. □\square

Lemma 2 (key invariant). After each step, for every w∉Tw \notin T, key(w)=min⁡u∈T(c⋅d(u)+dist(u,w))\mathrm{key}(w) = \min_{u \in T} \big(c \cdot d(u) + \mathrm{dist}(u, w)\big) and best(w)\mathrm{best}(w) is a point of TT that attains it.

Proof. At the start T={s}T = \{s\} and d(s)=0d(s) = 0, so key(w)=dist(s,w)\mathrm{key}(w) = \mathrm{dist}(s, w) is right. When vv joins TT, the value d(v)d(v) is fixed and never changes, and the only new term in the minimum is c⋅d(v)+dist(v,w)c \cdot d(v) + \mathrm{dist}(v, w). Line L6 takes the minimum of the old key and this term, and updates best(w)\mathrm{best}(w) when the new term is smaller. □\square

Theorem 1 (the Prim end). For c=0c = 0 the tree is a minimum spanning tree.

Proof. With c=0c = 0 the key of ww is min⁡u∈Tdist(u,w)\min_{u \in T} \mathrm{dist}(u, w), the length of the lightest edge from TT to ww. So each step adds a lightest edge that leaves TT, which is Prim's rule. By the cut property, every such edge belongs to a minimum spanning tree that contains the edges chosen before, so the final tree is a minimum spanning tree. □\square

Theorem 2 (the Dijkstra end). For c=1c = 1, d(v)=dist(s,v)d(v) = \mathrm{dist}(s, v) for every vv, so the tree is a shortest path tree.

Proof. With c=1c = 1 the rule is Dijkstra's algorithm on the complete graph with lengths dist\mathrm{dist}, which are not negative, so d(v)d(v) is the shortest path distance from ss in this graph. The direct edge svsv is a path of length dist(s,v)\mathrm{dist}(s, v), and every path is at least this long by the triangle inequality, so the shortest path distance is dist(s,v)\mathrm{dist}(s, v). □\square

Theorem 3 (radius bound). For every c∈[0,1]c \in [0, 1] and every point vv, c⋅d(v)≤dist(s,v)c \cdot d(v) \le \mathrm{dist}(s, v). So for c>0c > 0 the radius is at most R/cR / c, where R=max⁡vdist(s,v)R = \max_v \mathrm{dist}(s, v).

Proof. Let vv be added with parent uu. The source is in TT when vv is added and d(s)=0d(s) = 0, so by Lemma 2, key(v)≤c⋅0+dist(s,v)\mathrm{key}(v) \le c \cdot 0 + \mathrm{dist}(s, v). Also key(v)=c⋅d(u)+dist(u,v)≥c⋅d(u)+c⋅dist(u,v)=c⋅d(v)\mathrm{key}(v) = c \cdot d(u) + \mathrm{dist}(u, v) \ge c \cdot d(u) + c \cdot \mathrm{dist}(u, v) = c \cdot d(v), using c≤1c \le 1. Together, c⋅d(v)≤dist(s,v)≤Rc \cdot d(v) \le \mathrm{dist}(s, v) \le R. For v=sv = s the claim is 0≤00 \le 0. □\square

Lemma 3 (the cost at c = 1 is not bounded). For c=1c = 1 there are inputs where the cost is Θ(n)\Theta(n) times the minimum spanning tree.

Proof. Let k≥1k \ge 1, s=(0,0)s = (0, 0) and qi=(i,k−i)q_i = (i, k - i) for i=0,…,ki = 0, \ldots, k, with the L1 metric. Every qiq_i has dist(s,qi)=k\mathrm{dist}(s, q_i) = k, and dist(qi,qj)=2∣i−j∣\mathrm{dist}(q_i, q_j) = 2|i - j|. In the tree of Theorem 2, d(qi)=kd(q_i) = k. If the parent of qiq_i were some qjq_j, then d(qi)=k+2∣i−j∣>kd(q_i) = k + 2|i - j| > k, so every parent is ss, and the cost is (k+1)k(k + 1) k. The path s,q0,q1,…,qks, q_0, q_1, \ldots, q_k is a spanning tree of cost k+2k=3kk + 2k = 3k. So the ratio is at least (k+1)/3(k + 1)/3, with n=k+2n = k + 2 points. □\square

Running time. Each of the n−1n - 1 steps scans at most nn keys in line L3 and updates at most nn keys in lines L5 and L6, each in O(1)O(1). The total is Θ(n2)\Theta(n^2) time and Θ(n)\Theta(n) space. □\square

Complexity

  • Time. n−1n - 1 steps, each with a scan of nn keys and nn updates, so Θ(n2)\Theta(n^2), as for Prim's algorithm with an array. This is optimal for the complete graph, which has Θ(n2)\Theta(n^2) edges.
  • Space. The keys, the best tree points, the parents and the path lengths take Θ(n)\Theta(n).
  • Faster forms. For c=0c = 0 and the L1 metric an O(nlog⁡n)O(n \log n) method exists, the rectilinear minimum spanning tree. For 0<c<10 < c < 1 the keys depend on the tree, and the simple Θ(n2)\Theta(n^2) loop is the usual method for nets.

Properties

  • What is proved. The result is a spanning tree. It is a minimum spanning tree for c=0c = 0 and a shortest path tree for c=1c = 1. For every c∈(0,1]c \in (0, 1] the radius is at most R/cR / c. For 0<c<10 < c < 1 no bound on the cost relative to the MST is proved on this page, and none is claimed. For c=1c = 1 the cost can be Θ(n)\Theta(n) times the MST: with the source at (0,0)(0, 0) and the points (i,k−i)(i, k - i) for i=0,…,ki = 0, \ldots, k, every point is at distance kk, the shortest path tree is the star of cost (k+1)k(k + 1) k, and the minimum spanning tree costs 3k3k. For k=10k = 10 these are 110 and 30.
  • What is measured. On 100 random nets of 30 points with coordinates from 0 to 999, the source being the first point (Python random.Random(2027)), the mean and worst ratios were:
cc mean cost / MST worst cost / MST mean radius / RR worst radius / RR
0 1.000 1.000 1.744 3.321
1/4 1.045 1.130 1.189 1.655
1/2 1.150 1.278 1.054 1.257
3/4 1.310 1.587 1.012 1.087
1 4.209 6.654 1.000 1.000

These are toy inputs, not benchmark results. The middle values of cc give most of the radius gain for a small part of the cost.

  • Deterministic. Ties go to the smaller index of the new point and then to the first tree point that reached the key.

Variants and improvements

  • Steiner trees. The result is a spanning tree. Routers turn it into a rectilinear Steiner tree by merging overlapping L shapes of edges that leave the same point, which lowers the cost without making paths longer.
  • PD-II. Alpert and coauthors revisited the method in 2018 and added steps that remove detours and improve the tree after it is built, for the timing-driven routing of modern chips.
  • Bounded radius. The bounded-radius bounded-cost tree gives a tree with proved bounds on both the radius and the cost, at the price of a looser practical tradeoff.
  • Monotone paths. If every path from the source must be shortest, the tree is a rectilinear Steiner arborescence, the Steiner form of the case c=1c = 1.
  • Delay models. The path length is a stand-in for delay. The Elmore delay of the tree is a better model, and timing-driven routers check the tree against it.

When to use it

  • To build the initial tree of a net in timing-driven global routing, when both wire length and the delay to critical sinks matter.
  • When a single knob that moves smoothly from the minimum spanning tree to the shortest path tree is wanted.

When a proved guarantee on both cost and radius is needed, use the bounded-radius bounded-cost tree. When only the wire length matters, use the rectilinear minimum spanning tree or a Steiner tree.

Pitfalls

  • Recomputing d inside the key. The key uses the path length d(u)d(u) of the tree point, not the distance from the source in the plane. With dist(s,u)\mathrm{dist}(s, u) instead, the method is a different heuristic, and the proof of the radius bound does not apply to it.
  • Values of c outside [0, 1]. For c>1c > 1 the radius bound and the link to Dijkstra's algorithm are lost, since the key is no longer at least c⋅d(v)c \cdot d(v) of the added point.
  • Treating c = 1 as cheap. In the L1 metric the shortest path tree is far from unique, and the choice made by the tie rule can be very costly. A rectilinear Steiner arborescence gives the same path lengths with much less wire.
  • Floating point keys. With cc given as a float, equal keys can compare unequal. Use exact fractions, or a tolerance, to keep the tie rule.

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.

prim_dijkstra_tradeoff.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/prim-dijkstra-tradeoff/. The authors are listed in prov/93CM0J.jsonl.

from fractions import Fraction
from typing import Sequence, Union

Point = tuple[int, int]
Number = Union[int, Fraction]


def l1(p: Point, q: Point) -> int:
    return abs(p[0] - q[0]) + abs(p[1] - q[1])


def prim_dijkstra_tradeoff(points: Sequence[Point], c: Number, source: int = 0) -> tuple[list[int], list[int]]:
    """Alpert, Hu, Huang, Kahng and Karger's Prim-Dijkstra tree with parameter 0 <= c <= 1.

    Grows a spanning tree from the source. Each step adds the point v and tree
    point u that minimize c * d(u) + dist(u, v), where d(u) is the length of the
    tree path from the source to u. c = 0 gives Prim's minimum spanning tree and
    c = 1 gives a shortest path tree. Returns (parent, d); parent[source] = -1.
    Ties go to the smaller index of v, then of u. Runs in O(n^2) time.
    """
    if not 0 <= c <= 1:
        raise ValueError("c must lie in [0, 1]")
    n = len(points)
    parent = [-1] * n
    d = [0] * n
    if n == 0:
        return parent, d
    in_tree = [False] * n
    in_tree[source] = True
    key: list[Number] = [l1(points[source], p) for p in points]  # @line L1
    best_u = [source] * n
    for _ in range(n - 1):  # @line L2
        v = min((i for i in range(n) if not in_tree[i]), key=lambda i: (key[i], i))  # @line L3
        u = best_u[v]
        in_tree[v] = True
        parent[v] = u
        d[v] = d[u] + l1(points[u], points[v])  # @line L4
        for w in range(n):  # @line L5
            if not in_tree[w]:
                k = c * d[v] + l1(points[v], points[w])
                if k < key[w]:  # @line L6
                    key[w] = k
                    best_u[w] = v
    return parent, d  # @line L7


def tree_cost(points: Sequence[Point], parent: Sequence[int]) -> int:
    return sum(l1(points[v], points[u]) for v, u in enumerate(parent) if u >= 0)
test_prim_dijkstra_tradeoff.py 81 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/prim-dijkstra-tradeoff/. The authors are listed in prov/93CM0J.jsonl.

import random
from fractions import Fraction

from prim_dijkstra_tradeoff import l1, prim_dijkstra_tradeoff, tree_cost


def mst_length(pts: list[tuple[int, int]]) -> int:
    """Kruskal on all pairs, written apart from the tree builder."""
    n = len(pts)
    parent = list(range(n))

    def find(a: int) -> int:
        while parent[a] != a:
            a = parent[a]
        return a

    total = 0
    for d, i, j in sorted((l1(pts[i], pts[j]), i, j) for i in range(n) for j in range(i + 1, n)):
        if find(i) != find(j):
            parent[find(i)] = find(j)
            total += d
    return total


def is_tree_from(parent: list[int], source: int) -> bool:
    n = len(parent)
    if parent[source] != -1:
        return False
    for v in range(n):
        seen = set()
        while v != source:
            if v in seen or parent[v] < 0:
                return False
            seen.add(v)
            v = parent[v]
    return True


EXAMPLE = [(0, 0), (1, 3), (2, 6), (3, 9), (6, 9), (9, 9), (9, 6), (9, 3), (9, 0)]


def test_example() -> None:
    parent, d = prim_dijkstra_tradeoff(EXAMPLE, 0)
    assert tree_cost(EXAMPLE, parent) == 27 and max(d) == 27
    parent, d = prim_dijkstra_tradeoff(EXAMPLE, Fraction(1, 4))
    assert parent == [-1, 0, 1, 2, 3, 4, 5, 6, 0]
    assert tree_cost(EXAMPLE, parent) == 33 and max(d) == 24
    parent, d = prim_dijkstra_tradeoff(EXAMPLE, Fraction(1, 2))
    assert parent == [-1, 0, 1, 2, 3, 4, 7, 8, 0]
    assert tree_cost(EXAMPLE, parent) == 33 and max(d) == 18
    parent, d = prim_dijkstra_tradeoff(EXAMPLE, 1)
    assert d == [l1(EXAMPLE[0], p) for p in EXAMPLE]
    assert tree_cost(EXAMPLE, parent) == 93


def test_edges() -> None:
    assert prim_dijkstra_tradeoff([], 0) == ([], [])
    assert prim_dijkstra_tradeoff([(1, 1)], 1) == ([-1], [0])
    assert prim_dijkstra_tradeoff([(0, 0), (2, 2)], Fraction(1, 3)) == ([-1, 0], [0, 4])


def test_bounds(seed: int = 31) -> None:
    rng = random.Random(seed)
    for _ in range(300):
        n = rng.randint(1, 14)
        pts = [(rng.randint(0, 30), rng.randint(0, 30)) for _ in range(n)]
        s = rng.randrange(n)
        parent, d = prim_dijkstra_tradeoff(pts, 0, s)
        assert is_tree_from(parent, s)
        assert tree_cost(pts, parent) == mst_length(pts)
        parent, d = prim_dijkstra_tradeoff(pts, 1, s)
        assert is_tree_from(parent, s)
        assert all(d[v] == l1(pts[s], pts[v]) for v in range(n))
        c = Fraction(rng.randint(1, 9), 10)
        parent, d = prim_dijkstra_tradeoff(pts, c, s)
        assert is_tree_from(parent, s)
        assert all(c * d[v] <= l1(pts[s], pts[v]) for v in range(n))
        assert all(d[v] == d[parent[v]] + l1(pts[v], pts[parent[v]]) for v in range(n) if v != s)

History

Routing trees with short source-to-sink paths became important in the early 1990s, when wire delay grew in deep submicron chips. Jason Cong, Andrew Kahng, Gabriel Robins, Majid Sarrafzadeh and C. K. Wong gave the bounded-radius bounded-cost trees in 1992, with proved bounds. Charles Alpert, T. C. Hu, J. H. Huang, Andrew Kahng and David Karger presented the direct blend of Prim's and Dijkstra's algorithms in 1993, and published the journal version in 1995, where they compared it with the bounded-radius trees. In 2018 Alpert, Kahng and coauthors revisited the method as PD-II for timing-driven routing.

References

  1. C. J. Alpert, T. C. Hu, J. H. Huang, A. B. Kahng and D. Karger, Prim-Dijkstra tradeoffs for improved performance-driven routing tree design, IEEE Transactions on Computer-Aided Design 14(7), 890 to 896, 1995, the construction with the key c d(u) + dist(u, v) and its experiments
  2. J. Cong, A. B. Kahng, G. Robins, M. Sarrafzadeh and C. K. Wong, Provably good performance-driven global routing, IEEE Transactions on Computer-Aided Design 11(6), 739 to 752, 1992, the bounded-radius bounded-cost trees that the Prim-Dijkstra trees are compared with

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.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
  • at least three sources
  • a scene preset for the example