Advanced maths

Degree constrained minimum spanning tree calculator

Find a minimum-cost spanning tree subject to a separate degree upper bound at each vertex.

Your numbers

Vertices are labeled 0 through n−1, including isolated vertices.
One edge per line: u,v,cost. No loops or duplicate pairs. Blank means no edges.
One bound per vertex in zero-based label order.

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 spanning tree subject to a separate degree upper bound at each vertex. Return the exact constrained tree, total cost and degrees, or a completed-search infeasibility result.

THE FORMULAmin Σ_{e∈T}w_e subject to T spanning tree of G and deg_T(v)≤b_v for every vertex v.

A worked example

With all degree bounds 2, the default cheap star of cost 3 is forbidden because its center has degree 3. A path using edges 0–1 (1), 0–2 (1), 2–3 (2) has cost 4 and degrees 2,1,2,1. Every cost-3 tree would use all three unit edges, so 4 is optimal.

Good to know

Connected simple undirected graphs with 1–9 vertices and at most 20 edges; finite nonnegative edge costs ≤1000000 and integer degree bounds 0–n−1. Exact enumeration with degree/cycle pruning; 5000000-state cap throws unfinished error, distinct from completed infeasibility.

Optimize cost while respecting degrees

A degree-constrained spanning tree connects every declared vertex with original edges, contains no cycle and respects an upper bound on the number of selected edges incident to each vertex. Among all trees meeting those requirements, the calculator minimizes the sum of entered edge costs.

Degree limits can represent available ports, connection capacity or a desired path-like structure. They are upper bounds, not required exact degrees. A vertex allowed degree three may receive degree one in the optimum. The objective is total edge cost, so a more balanced-looking tree is not preferred unless its constraints or cost require it.

A four-cost path respects all degree limitsDegree-feasible tree costs 4 instead of the forbidden 30123112Upper bounds: 2,2,2,2Tree degrees: 2,1,2,1Cost: 1 + 1 + 2 = 4Dashed edges are available but not selected
Scroll across to see the full diagram.
With all degree bounds 2, the default cheap star of cost 3 is forbidden because its center has degree 3. A path using edges 0–1 (1), 0–2 (1), 2–3 (2) has cost 4 and degrees 2,1,2,1. Every cost-3 tree would use all three unit edges, so 4 is optimal.

Compare this with the Minimum cost k factor calculator with prescribed vertex degrees. Find a connected acyclic spanning subgraph respecting per-vertex degree bounds and minimizing cost; a k-factor can contain cycles and need not be a tree.

Enter costs and one bound per vertex

Each undirected edge row contains endpoint,endpoint,cost. Use zero-based integer endpoint labels and a finite nonnegative cost no greater than one million. Provide exactly one integer degree upper bound per vertex in label order, with each bound between zero and n−1.

The graph must be connected before degree constraints are imposed. Loops, reversed duplicates, missing costs, invalid labels and mismatched bound counts are rejected. A connected graph can still have no degree-feasible spanning tree. That is a mathematical infeasibility result, unlike malformed input, which should be corrected and recalculated.

Check the constrained star example

The default unit-cost edges form a star centered at vertex 0. Its cost is three, but its center has degree three. With degree bounds [2,2,2,2], that cheapest unconstrained tree is forbidden. One feasible replacement uses edges 0–1, 0–2 and 2–3.

Those costs are one, one and two, totaling four. Their degrees in label order are two, one, two and one, all within bounds. A tree costing less than four would have to use the three unit-cost edges, since every other available edge costs two. That unique cheaper structure violates the center’s bound.

Enumerate only extendable forests

The search considers inclusion and exclusion of each edge. Including an edge is allowed only if its endpoints currently lie in different components and neither endpoint has exhausted its degree capacity. The chosen edges therefore remain an acyclic forest with valid partial degrees throughout the search.

When n−1 edges have been chosen without a cycle, the forest spans and is a tree. Its cost is compared with the best completed candidate. Branches with too few remaining edges to reach n−1 are discarded. Every feasible spanning tree still appears in the completed search, so the minimum retained cost is exact.

Verify the returned tree

For a feasible result, check that all selected edges occur in the original weighted list and that the tree has n−1 edges. Sum their costs to recover the reported objective. The degree array should count the incident selected edges at every vertex and remain componentwise below the input bounds.

Connectivity and acyclicity are both required. A cheap collection of disjoint paths with valid degrees is not a spanning tree. The calculator preserves the selected edge costs in its witness, allowing cost and structure to be audited together. Equal-cost optimal trees may exist, so specific selected edges are not necessarily unique.

Distinguish infeasibility from interruption

A completed search with no feasible tree returns feasible false and a null cost, together with empty tree and degree arrays. For example, a three-vertex path with bounds one at every vertex is impossible, because its middle vertex must have degree two in its only spanning tree.

The recursive search has a five-million-state cap. If reached, it throws an unfinished-search error rather than returning infeasible or certifying the best tree encountered. Resource exhaustion leaves mathematical feasibility or optimality unresolved. Reducing a graph for a new attempt changes the instance and should not erase that distinction in recorded results.

Boundary cases and model limits

For any tree on n vertices, the degree sum is 2n−2. Bounds whose sum is smaller therefore cannot support a spanning tree, although satisfying this inequality alone is insufficient. A degree-zero bound at a vertex of a graph with at least two vertices also makes a spanning tree impossible.

A singleton has an empty spanning tree with cost zero and degree zero. The calculator allows nonnegative costs but no parallel edges, directed arcs, penalties for unused capacity or requirements on exact degree. Nine vertices and twenty edges keep this exhaustive implementation bounded; the cited reference studies the broader degree-constrained problem family.

A few questions, answered

Are the degree bounds exact requirements?

No. They are upper bounds; smaller selected degrees are allowed.

Can a connected input still be infeasible?

Yes. The degree limits may exclude every spanning tree.

Does a null cost mean a capped search?

No. Null cost accompanies completed infeasibility; a resource cap throws an explicit unfinished-search error.

What is the singleton convention?

Its empty spanning tree has cost 0 and degree 0, with a bound of 0 allowed.

Find your calculator

Start typing to explore the library.