A spanning tree includes every vertex in a graph while connecting them with no cycles. With exactly n−1 edges for n vertices, it offers a simple way to keep a graph connected. See why extra edges can create cycles and why a spanning cycle is different.

Multiple Choice

What is a spanning tree?

A spanning tree is a subset of the graph’s edges that keeps every vertex present, but connects them all without forming any cycle. In a graph with n vertices, such a subgraph has exactly n−1 edges, making it a minimal way to stay connected. That description matches the statement: a connected acyclic subgraph that includes all vertices. It includes all vertices while remaining connected and without cycles, which is exactly what a spanning tree is. The other ideas fall short: taking all edges can create cycles and isn’t necessarily minimal; a disconnected subgraph isn’t connected; a cycle that spans all vertices is connected and spans all vertices but contains a cycle, so it isn’t a tree.

A network can be connected in many different ways. A city’s road map might link every neighborhood through dozens of routes, while a computer network may have several paths between the same pair of machines. But sometimes the goal is simpler: keep every point reachable while using as few connections as possible. In graph theory, a spanning tree captures that idea.

It’s a small structure with a useful balance. It includes every vertex in the original graph, keeps the vertices connected, and contains no cycles. That last condition means there’s no closed route that starts and ends at the same vertex without retracing an edge. Remove unnecessary connections, and a spanning tree is what remains.

First, what do “spanning” and “tree” mean?

A graph consists of vertices (also called nodes) and edges that connect them. Think of vertices as towns and edges as roads, or as computers and network links. A graph may contain many vertices and a tangle of edges, with some locations connected by several possible routes.

“Spanning” means the subgraph includes all the vertices of the original graph. It doesn’t have to include all the original edges. In fact, it usually leaves many of them out.

“Tree” refers to a connected graph with no cycles. A tree can branch in all sorts of ways, but it has no loop. Put the two ideas together and you get a spanning tree: a connected, cycle-free subgraph that reaches every vertex.

That definition has three parts, and each one matters:

  • Every original vertex is included.

  • Every pair of vertices can be connected by a path.

  • No cycle appears in the selected edges.

Leave out a vertex, and the subgraph no longer spans the original graph. Leave the graph disconnected, and some vertices can’t reach others. Add an edge that creates a cycle, and the subgraph is no longer a tree. The conditions work as a set.

Why does it have exactly (n-1) edges?

If a spanning tree has (n) vertices, it has exactly (n-1) edges. This is one of the most useful facts about trees—and it’s more than a number to memorize. It follows from how a tree grows.

Start with one vertex. It’s connected to itself in the sense that there’s no other vertex to reach, and it needs no edges. Now add a new vertex. To keep the growing structure connected without making a cycle, attach that vertex with one edge. Each added vertex needs one new edge.

So, starting with one vertex, adding the remaining (n-1) vertices adds (n-1) edges. The same idea works in reverse: remove any edge from a tree, and it splits into two components. That tells us each edge is doing necessary work. There’s no spare connection that can be removed while keeping the whole structure connected.

For instance, a graph with six vertices needs five edges in any spanning tree. The original graph might have twenty edges, or just enough to remain connected. Either way, each spanning tree on those six vertices contains five edges.

A small example makes it clear

Imagine four towns: A, B, C, and D. Suppose roads link A to B, B to C, C to D, and also A to C. The original graph is connected, but it contains more roads than a spanning tree needs.

Choose the roads A–B, B–C, and C–D. Every town is reachable from every other town, and no loop is formed. This is a spanning tree.

Now add the road A–C. The towns are still connected, but a cycle appears: A–B–C–A. The result is no longer a tree. Or remove B–C from the original selection. Towns A and B are then cut off from C and D, so the subgraph is disconnected.

The distinction can feel subtle at first. A cycle that visits every vertex may look like it “spans” the graph, and it does include every vertex. But a cycle is not a tree. A spanning tree must connect everything without looping back around.

How many spanning trees can one graph have?

A connected graph can have more than one spanning tree. In the town example, we could choose different roads and still keep every town connected without creating a cycle. Each valid selection is a separate spanning tree.

Some graphs have only one. If a graph is already a tree, all of its edges are needed to connect its vertices, so the graph itself is its only spanning tree. Other graphs contain extra edges, which may allow several different spanning trees.

A complete graph, where every pair of distinct vertices has an edge between them, can have a great many. For a complete graph with (n) vertices, the number of spanning trees is (n^{n-2}), a result known as Cayley’s formula. You don’t need that formula to understand the basic idea, but it hints at how quickly the choices can grow.

Finding one: follow the structure

For a connected graph, a spanning tree can be built by exploring the vertices and keeping only the edges that first reach new vertices. Two common methods are breadth-first search and depth-first search.

Breadth-first search visits nearby vertices first. Starting from one vertex, it looks at its neighbors, then the neighbors of those neighbors, and so on. The edges used to discover new vertices form a spanning tree.

Depth-first search follows one path as far as it can before turning back and exploring another. It also produces a spanning tree when the original graph is connected. The shapes may differ: one tree can be broad and shallow, while another is narrow and deep. Both can span the same graph.

The key rule is simple: when an edge would connect two vertices already joined by a path in the growing structure, adding it would make a cycle. Leave it out. That’s the moment when a graph search turns into a tree-building process.

What if the graph is disconnected?

A disconnected graph has groups of vertices that no path can link. Since a spanning tree must be connected, such a graph has no spanning tree.

It can, however, have a spanning forest. A forest is a collection of trees, and a spanning forest includes every vertex while keeping each connected component cycle-free. If the original graph has several separate components, each component can have its own tree.

This distinction is handy in real systems. A map may contain isolated regions with no road between them, or a network may have separate clusters. A spanning forest describes the connections within each cluster without pretending that one cluster can reach another.

Why spanning trees matter beyond definitions

Spanning trees show up whenever a system needs to remain connected with fewer links. In network design, for example, a company may want to connect offices while reducing the number of cables or wireless links. A tree offers a basic connected layout: every office can communicate through the network, and there are no redundant links.

The absence of cycles can also make some tasks easier. In a tree, there is exactly one path between any two vertices. That makes routing and traversal straightforward. But there’s a trade-off: if one edge fails, the tree may split into two disconnected parts. A design that needs resilience may keep extra edges, even though those edges create cycles. Minimal isn’t always better; it depends on what the system needs.

When edges have different costs, the problem becomes more specific. A minimum spanning tree is a spanning tree whose total edge weight is as small as possible. The weights might represent cable cost, distance, time, or energy use. Algorithms such as Kruskal’s and Prim’s find a minimum spanning tree in a weighted connected graph. They still aim to include every vertex and avoid cycles, but they also compare the costs of edges.

The simple idea at the heart of it

A spanning tree isn’t a subgraph containing every edge, and it isn’t a cycle that loops through every vertex. It’s a lean connected structure that keeps all vertices and removes every unnecessary loop. With (n) vertices, it has (n-1) edges.

That combination—complete reach, no cycles, just enough links—makes spanning trees one of graph theory’s most practical building blocks. They help explain how networks can stay connected, how searches can organize their results, and why a few carefully chosen connections can hold an entire system together.