minimum connected subgraph containing a given set of nodes
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Given a graph and a required set of vertices, the goal is to find the smallest connected subgraph that contains all of those required vertices. In general graphs this is closely related to the Steiner tree problem, which is computationally hard, but several special cases and practical approximations are very useful.
The Problem Behind the Name
Suppose you have a graph and a subset of vertices called terminals. You want a connected subgraph that includes every terminal while using as little structure as possible. "As little as possible" might mean:
- fewest edges
- lowest total edge weight
- fewest added non-terminal vertices
In an unweighted graph, minimizing the number of edges is the natural interpretation. In a weighted graph, the usual objective is minimum total weight.
This is not the same as a minimum spanning tree. A minimum spanning tree connects all vertices in the graph. Here, only the terminal set must be connected, and the solution may add a few extra non-terminal vertices if they help connect the terminals cheaply.
Those extra helper vertices are called Steiner vertices.
Relation to the Steiner Tree Problem
For a general graph, the minimum connected subgraph containing a given set of nodes is the Steiner tree problem. That matters because the Steiner tree problem is NP-hard, so there is no known polynomial-time algorithm for all cases.
Still, the problem is not hopeless. There are three common ways to deal with it:
- solve special graph families exactly
- use dynamic programming when the terminal count is small
- use approximation or heuristics for larger graphs
If your graph is a tree, the problem becomes much easier. The minimal connected subgraph is simply the union of the unique paths between the terminals.
Exact Solution on a Tree
Trees are the cleanest case because there is exactly one simple path between any two vertices. A practical way to compute the answer is:
- start with the full tree
- repeatedly prune leaves that are not terminals
- stop when every leaf is a terminal
The remaining edges form the minimum connected subgraph containing the required nodes.
Here is a runnable Python example:
For this tree, the result is the smallest subtree that still connects vertices 2, 4, and 5.
What Changes in General Graphs
In a general graph, there may be many candidate paths between terminals. Sometimes the best solution includes a non-terminal vertex because it lets several terminals share part of the route. That sharing is exactly why the problem is harder than just joining terminals with independent shortest paths.
A common approximation strategy is:
- compute shortest-path distances between every pair of terminals
- build a complete graph on the terminals using those distances
- compute a minimum spanning tree in that terminal graph
- expand each terminal edge back into the original shortest path
This does not always produce the optimal Steiner tree, but it is a useful and understandable baseline.
When This Appears in Practice
This problem shows up in network design, VLSI routing, road planning, multicast distribution, and biological pathway analysis. The theme is always the same: connect a required set cheaply without forcing the rest of the graph into the solution.
Because exact optimization is expensive in large graphs, engineering solutions often combine:
- preprocessing to remove obviously irrelevant vertices
- heuristics based on shortest paths
- local improvement after building an initial tree
That is usually a better practical strategy than insisting on exact optimality at all costs.
Common Pitfalls
The first mistake is confusing the problem with a minimum spanning tree. If the graph has many irrelevant vertices, an MST solves a different problem.
Another common mistake is assuming the union of pairwise shortest paths is always optimal. It can work as a heuristic, but the combined structure may contain unnecessary edges or miss a cheaper shared connection through a different Steiner vertex.
On trees, developers sometimes overcomplicate the solution. The prune-non-terminal-leaves method is simple, exact, and efficient.
Finally, always clarify the objective. Minimum by edge count and minimum by total weight can produce different answers.
Summary
- The minimum connected subgraph for a required node set is the Steiner tree problem in general graphs.
- The problem is NP-hard in general, so approximations are often necessary.
- On a tree, the exact answer is easy: prune non-terminal leaves until only the required subtree remains.
- A minimum spanning tree is not the same problem unless all vertices are required.
- Clear definition of the cost function is essential before choosing an algorithm.

