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
- Model
- Real RAM
- Properties
- deterministicnot exactiterative
- Evidence
- Proof sketchCode in 1 languages
- 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 and tree point that minimize , where is the path length from the source to in the tree and is a parameter. With this is Prim's algorithm and gives a minimum spanning tree. With 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 times "the path from the driver to where it joins". A small cares mostly about wire, a large mostly about path length. A designer tries a few values of and keeps the best tree.
The problem
The input is a set of points with a source , and a metric , in routing the L1 distance . The output is a spanning tree rooted at . Its cost is the sum of its edge lengths, and its radius is the largest path length from to a point in the tree. Let . No tree has radius less than , and no tree has cost less than the minimum spanning tree . In general no single tree reaches both, so the goal is a good tradeoff, as in the bounded-radius bounded-cost tree.
The algorithm
PRIM-DIJKSTRA(points, s, c)- 1
d(s) ← 0; T ← {s}; for each v ≠ s: key(v) ← dist(s, v), best(v) ← s - 2
repeat n − 1 times - 3
v ← the point not in T with the least key(v), ties to the smaller index - 4
u ← best(v); add v to T with parent u; d(v) ← d(u) + dist(u, v) - 5
for each point w not in T do - 6
if c · d(v) + dist(v, w) < key(w) then key(w) ← c · d(v) + dist(v, w); best(w) ← v - 7
return the parents and the path lengths d
The keys are those of Prim's algorithm with an array, so each step takes time. The key of is at all times, because does not change once is in the tree.
Example
Take nine points that run up, across and down, with the source at the bottom left:
, , , , , , , , .
The minimum spanning tree is the chain , of cost 27. In it the last point 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 , to .
| parents of | cost | radius | |
|---|---|---|---|
| 0 | 27 | 27 | |
| 1/4 | 33 | 24 | |
| 1/2 | 33 | 18 | |
| 3/4 | the same as for 1/2 | 33 | 18 |
| 1 | for all | 93 | 18 |
With , the point is joined straight to the source. With , the right side of the net hangs from , so both sides are reached by short paths. This tree has the optimal radius 18 at cost 33, while the shortest path tree of 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 , and is the length of its tree path.
- The keys are right. Line L6 keeps .
- The two ends. With the key is the distance to the tree, so the result is Prim's minimum spanning tree. With the key is , so the result is Dijkstra's shortest path tree of the complete graph, and for every by the triangle inequality.
- A radius bound. For every and every point, , so the radius is at most .
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 be points with source , and let be a metric, so it satisfies the triangle inequality. Fix . Write for the set of points in the tree after steps of line L2, so .
Lemma 1 (spanning tree). After the loop, the parents form a spanning tree rooted at , and is the length of the tree path from to .
Proof. Each step adds one point with parent , by Lemma 2 below. So the parent edges never close a cycle, and after steps all points are in . By induction on the steps, is the tree path length, since already is.
Lemma 2 (key invariant). After each step, for every , and is a point of that attains it.
Proof. At the start and , so is right. When joins , the value is fixed and never changes, and the only new term in the minimum is . Line L6 takes the minimum of the old key and this term, and updates when the new term is smaller.
Theorem 1 (the Prim end). For the tree is a minimum spanning tree.
Proof. With the key of is , the length of the lightest edge from to . So each step adds a lightest edge that leaves , 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.
Theorem 2 (the Dijkstra end). For , for every , so the tree is a shortest path tree.
Proof. With the rule is Dijkstra's algorithm on the complete graph with lengths , which are not negative, so is the shortest path distance from in this graph. The direct edge is a path of length , and every path is at least this long by the triangle inequality, so the shortest path distance is .
Theorem 3 (radius bound). For every and every point , . So for the radius is at most , where .
Proof. Let be added with parent . The source is in when is added and , so by Lemma 2, . Also , using . Together, . For the claim is .
Lemma 3 (the cost at c = 1 is not bounded). For there are inputs where the cost is times the minimum spanning tree.
Proof. Let , and for , with the L1 metric. Every has , and . In the tree of Theorem 2, . If the parent of were some , then , so every parent is , and the cost is . The path is a spanning tree of cost . So the ratio is at least , with points.
Running time. Each of the steps scans at most keys in line L3 and updates at most keys in lines L5 and L6, each in . The total is time and space.
Complexity
- Time. steps, each with a scan of keys and updates, so , as for Prim's algorithm with an array. This is optimal for the complete graph, which has edges.
- Space. The keys, the best tree points, the parents and the path lengths take .
- Faster forms. For and the L1 metric an method exists, the rectilinear minimum spanning tree. For the keys depend on the tree, and the simple loop is the usual method for nets.
Properties
- What is proved. The result is a spanning tree. It is a minimum spanning tree for and a shortest path tree for . For every the radius is at most . For no bound on the cost relative to the MST is proved on this page, and none is claimed. For the cost can be times the MST: with the source at and the points for , every point is at distance , the shortest path tree is the star of cost , and the minimum spanning tree costs . For 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:
| mean cost / MST | worst cost / MST | mean radius / | worst radius / | |
|---|---|---|---|---|
| 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 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 .
- 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 of the tree point, not the distance from the source in the plane. With 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 the radius bound and the link to Dijkstra's algorithm are lost, since the key is no longer at least 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 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.
# 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 93CM0J Edit this page History Report an error Markdown