A spanning tree is a subgraph that connects all vertices of a connected, undirected graph without forming any cycle.
A minimum spanning tree, or MST, is a spanning tree with the minimum possible total edge weight.
This page focuses on spanning tree and MST concepts. Prim's and Kruskal's algorithms have their own dedicated detailed pages.
Add one worked example that compares the normal path with the boundary case for spanning_tree.
Spanning Tree and Minimum Spanning Tree Detailed Notes Examples and FAQs should be studied as a practical algorithm analysis lesson, not as a label. Start by naming the input, the rule that changes the input, and the result a learner should be able to predict after reading the page.
Think of a spanning tree as the minimum set of connections needed to keep every place reachable. If the graph has extra edges, those extra edges create alternate routes or cycles. A spanning tree removes those extras while keeping the whole graph connected.
A spanning tree of a connected, undirected graph is a subgraph that includes every vertex of the original graph and is also a tree. Since it is a tree, it must be connected and must not contain any cycle.
The word spanning means all vertices are covered. The word tree means the selected edges connect those vertices without cycles. So a spanning tree is the simplest connected version of the original graph.
Suppose a graph has vertices A, B, C, and D with these edges:
| Edge | Weight |
|---|---|
| A - B | 4 |
| A - C | 3 |
| B - C | 2 |
| B - D | 5 |
| C - D | 7 |
The graph has 4 vertices, so every spanning tree must contain exactly 3 edges. Different valid choices are possible.
| Tree | Selected Edges | Total Weight | Valid? |
|---|---|---|---|
| T1 | A-B, B-C, B-D | 4 + 2 + 5 = 11 | Yes, all vertices connected and no cycle |
| T2 | A-C, B-C, B-D | 3 + 2 + 5 = 10 | Yes, all vertices connected and no cycle |
| T3 | A-B, A-C, C-D | 4 + 3 + 7 = 14 | Yes, all vertices connected and no cycle |
| Not a tree | A-B, A-C, B-C | 9 | No, vertex D is missing and A-B-C forms a cycle |
| Property | Explanation |
|---|---|
| Contains all vertices | No original vertex is excluded from the spanning tree |
| Has V - 1 edges | A tree with V vertices always contains V - 1 edges |
| No cycles | Adding any extra edge to a spanning tree creates a cycle |
| Connected | Removing any selected edge disconnects the tree |
| Can be multiple | One graph can have many different spanning trees |
| Uses original edges only | A spanning tree cannot invent a new edge that was not in the graph |
| Concept | Meaning | Can Have Cycles? | Must Include All Original Vertices? |
|---|---|---|---|
| Graph | General structure of vertices and edges | Yes | It is the original structure |
| Tree | Connected graph with no cycle | No | Not necessarily tied to another graph |
| Spanning Tree | Tree formed from a graph that covers all vertices | No | Yes |
When a connected, undirected graph has edge weights, every spanning tree has a total weight. A minimum spanning tree is the spanning tree whose total edge weight is as small as possible.
Every MST is a spanning tree, but not every spanning tree is an MST. The MST is the cheapest valid way to connect all vertices.
From the example graph, compare the valid spanning trees by total weight.
| Point | Spanning Tree | Minimum Spanning Tree |
|---|---|---|
| Meaning | Any tree that connects all vertices | The spanning tree with minimum total weight |
| Weights required? | No | Yes, weights are needed for comparison |
| Number of edges | V - 1 | V - 1 |
| Cycle allowed? | No | No |
| Can be many? | Yes | Yes, if equal weights allow ties |
| Main concern | Connectivity without cycles | Connectivity with least total cost |
The cut property is the main idea behind MST algorithms. If we divide the vertices into two non-empty groups, the lightest edge crossing between those groups is safe to include in some MST.
The cycle property says that if a cycle exists in a weighted graph, the heaviest edge in that cycle is not needed for an MST when it is strictly heavier than the alternatives.
| Graph Type | Spanning Tree Exists? | Reason |
|---|---|---|
| Connected undirected graph | Yes | All vertices can be connected |
| Disconnected graph | No single spanning tree | At least one vertex group is unreachable from another |
| Unweighted connected graph | Yes | Weights are not required for a normal spanning tree |
| Weighted connected graph | Yes | Also allows MST comparison |
| Directed graph | Not in the classical MST sense | Directed graphs use different structures such as arborescences |
For any spanning tree:
Number of edges in a spanning tree = V - 1
If V = 6:
Required spanning tree edges = 6 - 1 = 5
Spanning trees are useful whenever we need connectivity with no redundant cycles. MSTs are useful when each connection has a cost.
| Application | How It Helps |
|---|---|
| Network design | Connect all routers or computers with minimum cable cost |
| Road planning | Connect cities while minimizing construction cost |
| Electrical grids | Connect stations with minimum wiring |
| Clustering | Build an MST and remove heavy edges to form groups |
| Broadcasting | Send data to all nodes without repeated loops |
| Approximation algorithms | MSTs provide useful structure for harder optimization problems |
This page explains the concept. Use the dedicated pages below for full algorithm steps, dry runs, and code.
| Algorithm | Basic Idea | Dedicated Page |
|---|---|---|
| Kruskal's Algorithm | Sort edges and add the cheapest safe edge | daa/kruskals-minimum-spanning-tree |
| Prim's Algorithm | Grow one tree using the cheapest outgoing edge | daa/prims-minimal-spanning-tree |
1. Try a small boundary input.
2. Record where the algorithm changes path.
3. Compare the result with the normal case.
4. State the complexity in one line.
1. Try empty, missing, duplicate, or invalid data.
2. Identify where Spanning Tree and Minimum Spanning Tree Detailed Notes Examples and FAQs changes behavior.
3. Explain the safest correction.
4. Retest the normal path.
Choose edges with small weights but leave one vertex disconnected.
A spanning tree must include and connect every vertex.
Accept an edge set with a cycle as a spanning tree.
Remove cycle-forming edges until exactly V - 1 edges remain.
Use MST when the question asks for shortest path from a source.
Use shortest path algorithms for source-to-destination distance problems.
Memorizing Spanning Tree and Minimum Spanning Tree Detailed Notes Examples and FAQs without the situation where it is useful.
Connect Spanning Tree and Minimum Spanning Tree Detailed Notes Examples and FAQs to a concrete algorithm analysis task.
No. Every MST is a spanning tree, but only the spanning tree with the smallest total weight is a minimum spanning tree.
A spanning tree with V vertices always has exactly V - 1 edges.
Yes. If edge weights are repeated, multiple spanning trees may have the same minimum total weight.
Yes. MST algorithms can handle negative, zero, and positive edge weights because they compare edge weights directly.
Study Kruskal's algorithm and Prim's algorithm on their dedicated pages to learn how MSTs are constructed efficiently.
Explore 500+ free tutorials across 20+ languages and frameworks.