Advanced maths

Minimum Steiner forest calculator for undirected terminal pairs

Find a minimum-cost undirected forest connecting every requested terminal pair.

Your numbers

Labels are 0 through n−1; include isolated vertices.
One row: u v integer-cost. No loops or duplicate unordered pairs.
One row: s t, distinct zero-based endpoints.1–4 different pairs.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

Find a minimum-cost undirected forest connecting every requested terminal pair. Different demands may share edges or remain in separate components.

THE FORMULAMinimize Σe∈F cost(e), subject to every requested pair being connected in the forest F. Ties prefer fewer retained edges.

A worked example

The default requests 0–2 and 3–5 on a six-vertex chain. Keeping 0–1–2 and 3–4–5 costs 4; the intervening 2–3 edge is unnecessary.

Good to know

1–8 vertices; at most 16 edges; 1–4 distinct terminal pairs. Integer costs 0–1,000,000; exact subset enumeration with a search cap.

Connecting demands without connecting everything

A Steiner forest connects each requested source–destination pair using available graph edges. It does not demand that every input vertex, or even all terminals, occupy one common component. Vertices that are not demand endpoints can still serve as useful intermediate junctions when their incident edges reduce total connection cost.

The objective counts each retained edge once, even if several demand paths use it. Sharing can therefore make a joint solution cheaper than separately buying each pair's shortest path. The returned result is a minimum-cost forest for the supplied finite graph, not a geometric construction that introduces new junctions outside its vertex set.

Two demand paths need no bridge0123451 + 11 + 1Dashed bridge is not neededMinimum retained cost = 4
Scroll across to see the full diagram.
Demand 0–2 and demand 3–5 use four unit edges, costing 4.

Compare this with the Directed Steiner tree calculator from a root and terminal vertices. Connect several required terminal pairs in an undirected minimum-cost forest; a directed root-to-terminals arborescence imposes different paths and connectivity.

Entering costs and terminal pairs

Enter each undirected edge as two zero-based vertex labels followed by a nonnegative integer cost. The graph may contain isolated vertices, but every demanded pair must be connectable. Reverse duplicates describe the same undirected edge and are rejected, as are loops, negative costs and indices outside the declared vertex count.

Enter terminal pairs separately as two distinct endpoint labels per row. Repeating a pair in the opposite order does not create another demand. Several pairs may share an endpoint, in which case their connectivity requirements naturally force those terminals into a common component even though no all-terminal requirement is imposed explicitly.

Why the default forest costs four

The example offers a six-vertex unit-cost chain and an additional expensive edge from zero to five. Demand zero–two uses edges zero–one and one–two. Demand three–five uses three–four and four–five. Those four unit edges satisfy both requirements in two separate components at a total cost of four.

Keeping the chain's middle edge would join those components but add one unit without satisfying any new demand. The five-unit shortcut also cannot improve the solution. The example illustrates the defining difference from a spanning tree: a valid minimum forest deliberately leaves some available connections unused and need not connect all six vertices together.

Exact subset search and cycle removal

The implementation examines edge subsets, computes their total costs and uses a disjoint-set connectivity structure to check the terminal requirements. A subset containing a cycle is skipped. With nonnegative costs, deleting a cycle edge preserves connectivity without increasing cost, so an optimum always has a forest representative.

Among feasible forests with equal total cost, the calculator prefers one using fewer edges. This matters particularly when zero-cost edges are present: adding an irrelevant zero-cost branch does not change the primary objective but makes the witness less concise. A completed exhaustive search certifies the minimum under this stated tie rule.

Inspecting the connection certificate

The retained edge list is the purchased network. A separate path is reconstructed for each requested pair, allowing the connectivity claim to be checked without guessing how components join. Every consecutive pair of vertices in a displayed path must correspond to a retained edge, and the path endpoints must match the demand.

Add the costs of the retained edges once to reproduce the result. Do not add the costs of all demand paths independently, because shared edges would then be charged repeatedly. A forest provides a unique route within each connected component, so the displayed paths are consistent with the returned network rather than independent alternative purchases.

Steiner forest versus related objectives

A minimum spanning forest spans every component of the input graph, while this problem only connects specified demands. A Steiner tree typically joins a single terminal set into one component. Either related objective may retain unnecessary edges or enforce stronger connectivity than required for the pair list supplied here.

Selecting shortest paths independently is also a different optimization strategy. It can miss savings available when paths are allowed to share a common backbone. This calculator optimizes the total union of purchased edges directly, but it does not impose capacities, route lengths, edge-disjointness, reliability requirements or separate charges for each demand using an edge.

Infeasibility and bounded exact computation

If a demanded pair lies in different input components, no feasible forest exists; the result reports that status instead of inventing a connection. A search-resource error is different: it means no final optimum or infeasibility conclusion was certified. The small graph and edge limits keep the exact enumeration bounded.

Useful checks include a single demand, where the optimum equals a shortest-path cost, and a graph already composed of demand-satisfying disjoint paths. Adding an optional edge cannot worsen the optimum. Multiplying every integer cost by the same positive integer scales the minimum by that factor, provided the new values remain supported.

A few questions, answered

Must all terminals be in one tree?

No. Only each requested pair must be connected; different pairs may occupy separate components.

Are shared edges charged repeatedly?

No. A retained edge is paid for once regardless of how many demand paths use it.

Can zero-cost edges be entered?

Yes. Costs are nonnegative integers, and equal-cost solutions prefer fewer edges.

Does infeasible mean the search timed out?

No. An infeasible result follows completed search; a resource-cap error does not certify infeasibility.

Find your calculator

Start typing to explore the library.