learngraphtheory.org

Guest User

Using app without sign in

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

300 Exercises with Hints, Worked Algorithms and Complete Solutions

By Mohammed Islam Hadjoudj

Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Steiner Tree Calculator

Connect only what must be connected, as cheaply as possible

Lightest tree connecting chosen terminals, possibly through extra junctions (Kou-Markowsky-Berman, within 2 × the optimum)

Time: O(|T|·(V + E) log V)
Space: O(V²)
Use Case: Cable and fibre layout, pipelines, multicast, chip wiring
Showing the example graph for this algorithm.
Terminals4 marked

Click the vertices the tree must connect. The others may be used as junctions.

Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Steiner Tree

The Steiner tree problem asks for the lightest tree that connects a chosen set of vertices, the terminals. Unlike a minimum spanning tree, it does not have to reach every vertex, and it may pass through other vertices, called Steiner points, when they make the tree cheaper. It is NP-hard, one of Karp's 21 problems of 1972. With all vertices as terminals it becomes the minimum spanning tree, and with two terminals a shortest path.

How it works

The Kou-Markowsky-Berman heuristic of 1981 first computes the shortest distances between all pairs of terminals and builds a minimum spanning tree of that complete graph, which says which terminals to link directly. Each of its edges is then replaced by the corresponding shortest path in the network; these paths may share edges and pass through Steiner points. A minimum spanning tree of their union removes any cycles, and finally leaves that are not terminals are pruned. The result is at most twice the weight of the lightest Steiner tree. For up to ten terminals this page also computes the exact optimum with the Dreyfus-Wagner dynamic programme, to show how close the heuristic came.

Applications

Steiner trees design networks that only need to reach certain places: fibre-optic and cable layouts, power and water distribution, oil and gas pipelines, and multicast routing that delivers one stream to a group of receivers. In chip design they route the wires that connect the pins of a net, and in biology they reconstruct phylogenetic trees. They are a standard example of approximation algorithms in courses.

Pseudocode

The Steiner tree problem asks for the lightest tree that connects a given set of terminal vertices, possibly through other vertices. The visualizer uses the Kou, Markowsky and Berman heuristic, which builds the tree from shortest paths between the terminals.

SteinerTree(graph, terminals T):
    d = shortest path distances between the terminals
    M = minimum spanning tree of the complete graph on T, weights d
    H = the union of the shortest paths for the edges of M
    S = minimum spanning tree of H
    while S has a leaf that is not a terminal:
        remove that leaf
    return S

An optimal Steiner tree, doubled and walked around, gives a closed walk through all terminals of weight at most twice the optimum, so the distance graph has a spanning tree that cheap, and M is no heavier. Expanding M into paths cannot add weight, and the overlaps that appear when two paths share edges only remove weight. The result is at most 2(1 - 1/l) times the optimum, where l is the number of leaves of the optimal tree.

Worked example, step by step

Connect the four terminal towns of the example this page loads as cheaply as possible. Two road junctions, S1 and S2, are available as Steiner points, and a third junction E is not worth using.

Example graph: Terminals A, B, C, D. Direct roads A-B 13, A-C 9, B-D 9 and C-D 13. Through the junctions: A-S1 4, C-S1 4, S1-S2 4, S2-B 4 and S2-D 4. A detour A-E 7 and E-B 7.

  1. Terminal distances. Shortest paths between terminals: A to C is 8 through S1, B to D is 8 through S2, and A to B, A to D, C to B and C to D are all 12, through both junctions. Every one of them beats the direct road.
  2. Spanning tree on the terminals. A minimum spanning tree on these distances uses A-C (8), B-D (8) and one link of 12, here A-B. This tree M on the terminals weighs 28.
  3. Expand into paths. A-C becomes A-S1-C, B-D becomes B-S2-D, and A-B becomes A-S1-S2-B. The edge A-S1 appears in two of these paths but is needed only once, so the union has five edges: A-S1, S1-C, S1-S2, S2-B and S2-D.
  4. Clean up. The union is already a tree, and all its leaves, A, B, C and D, are terminals, so nothing is pruned.

The Steiner tree weighs 20 and runs through both Steiner points, which here is optimal. Connecting the terminals with direct roads only, A-C, B-D and A-B, would cost 31, more than half as much again. The shared edge A-S1 is where the heuristic gains on its own estimate of 28: overlapping shortest paths are exactly what makes Steiner points worth using.

Complexity, and where it comes from

Time: NP-hard, O(t (E + V log V)) for the heuristic · Space: O(V + E + t^2)

The Steiner tree problem in graphs is NP-hard, and it was one of Karp's 21 problems of 1972. The heuristic needs one Dijkstra run per terminal, O(t (E + V log V)) for t terminals, then a spanning tree on t vertices, O(t^2), and a spanning tree of the union, which has at most V vertices. Mehlhorn showed how to get the same guarantee in a single O(E + V log V) computation using Voronoi regions around the terminals. The exact Dreyfus-Wagner dynamic program runs in about O(3^t V + 2^t V^2) after shortest paths, so it is practical when there are few terminals even if the graph is large.

When to use Steiner Tree, and when not to

The Steiner tree sits between two polynomial problems. Which one applies depends on how many vertices must be connected.

AlternativePrefer it whenCost
Minimum spanning treeEvery vertex is a terminal, so there is nothing to choose and the problem becomes polynomial.O(E log V)
Shortest pathThere are only two terminals. The Steiner tree is then simply the shortest path between them.O(E + V log V)
Dreyfus-WagnerYou need the exact optimum and there are at most about 15 to 20 terminals.O(3^t V + 2^t V^2)
Byrka et al. approximationTheoretical best guarantee, a factor of about 1.39, based on linear programming and randomised rounding.polynomial, heavy
Euclidean Steiner treePoints in the plane where junctions may be placed anywhere. Steiner points then meet at 120 degree angles.NP-hard

Common pitfalls

  • Using the spanning tree of the terminals alone. Restricting the tree to edges between terminals ignores the junctions that make Steiner trees cheaper. In the example it costs 31 instead of 20, and if terminals are not directly adjacent it may not even connect them.
  • Adding up the expanded paths. The weight of M overestimates the final tree whenever shortest paths share edges. Take the union of the paths and compute its spanning tree, as the algorithm does, rather than reporting the weight of M.
  • Leaving useless Steiner points. After the second spanning tree, a junction can remain as a leaf that connects nothing. Prune non-terminal leaves repeatedly until every leaf is a terminal.
  • Confusing the graph and Euclidean versions. In the graph version Steiner points must be existing vertices. In the Euclidean version they can be anywhere in the plane. The algorithms and the guarantees are different.

Frequently asked questions

What is a Steiner tree?
Given a weighted graph and a subset of vertices called terminals, a Steiner tree is a tree that connects all the terminals. It may pass through other vertices, called Steiner points. The Steiner tree problem asks for the lightest such tree.
What is the difference between a Steiner tree and a minimum spanning tree?
A minimum spanning tree must connect every vertex of the graph. A Steiner tree only has to connect the terminals and may use any other vertices when that makes it lighter. The spanning tree problem is polynomial, the Steiner tree problem is NP-hard.
Is the Steiner tree problem NP-hard?
Yes. It was one of the 21 NP-complete problems in Karp's 1972 paper. It becomes polynomial in the two extreme cases: two terminals, where it is a shortest path, and all vertices as terminals, where it is a minimum spanning tree.
What is the KMB approximation for Steiner trees?
The heuristic of Kou, Markowsky and Berman from 1981 computes shortest paths between the terminals, takes a minimum spanning tree on those distances, expands its edges into paths, and cleans up the result. Its tree is at most twice as heavy as the optimum.
Where are Steiner trees used?
In the design of telecommunication, power and pipeline networks that must link given sites, in VLSI routing to connect the pins of one net, in multicast routing, and in phylogenetics, where the tree that connects observed species may pass through hypothetical ancestors.

Related algorithms: Prim's MST Algorithm, Kruskal's MST Algorithm, Dijkstra's Algorithm, Christofides

Interactive Controls

Zoom Controls

44%
Nodes: 7
Edges: 11