Spanning Trees

Consider a graph . A spanning tree of is a subgraph (with and ) that is a tree (i.e., contains no cycles) that covers every vertex of . That is . For example, consider the following graph:

A graph of six nodes

A spanning tree for the graph is given below:

A spanning tree of the previous graph

Spanning trees are not necessarily unique. For example, here is a different spanning tree for the graph.

An alternative spanning tree of the previous graph

Constructing a spanning tree for a graph is useful in many situations, for example:

  • Consider an electrical network for a neighborhood where nodes represents houses and edges represent potential electrical connections between houses. A spanning tree in this context represents a minimal set of electrical connections used to connect all of the houses to the power grid.

  • Consider a collection of networked computers where nodes represent computers and edges represent physical connections between computers. A spanning tree in this context represents a minimal collection of connections that allow one machine to communicate with another without the fear of encountering a routing loop where a message is relayed between a collection of machines in perpetuity.

Exercise (Sizes of Spanning Trees)

Say that we have a graph . What is the number of edges of any spanning tree of ? Prove your claim.

Constructing Spanning Trees

To construct a spanning tree, we can employ either traversal algorithm we have discussed for graphs---breadth-first or depth-first search---to reach every vertex from some arbitrary starting vertex. When processing a vertex , we add to our working set each vertex connected to (i.e., ) that we haven't already seen. If we are performing breadth-first search, we treat the working set like a queue, processing the oldest entry in the working set first. If we are performing depth-first search, we treat the working set like a stack, processing the newest entry in the working set first.

For example, the first spanning tree we discussed previously is the result of a breadth-first traversal, namely:

The second spanning tree is the result of a depth-first traversal, namely:

Exercise (Runtime of Spanning Tree Construction)

Prove that for a graph , we must process exactly vertices when constructing the spanning tree, irrespective of the traversal used to generate the tree.

Minimum Spanning Trees

Now, let's consider extending our graph edges with weights. Each weight represents a "cost" associated with that edge, for example:

  • Distances if the edges represent connections between physical places.
  • Monetary amounts if the edges represent a transformation from one kind of object to another.

Let:

Be the sum of the weights of all the edges in .

With a weighted graph, we can refine our notion of spanning tree. We can now consider minimum spanning trees (MST), a spanning tree with minimal cost. Formally defined:

Definition (Minimum Spanning Tree)

Let be a weighted graph. Then a minimum spanning tree, , be a spanning tree of such that for any other spanning tree of , it is the case that .

In our above examples, we could attach an appropriate cost to each graph:

  • Weights in the electrical network represent physical distance between houses.
  • Weights in the computer network represent the average time it takes for two computers to communicate with each other.

Minimum spanning trees for each of these examples then also minimize the values of these trees---physical distances and average communication time, respectively---in addition to "spanning" the graphs.

Algorithms for Minimum Spanning Trees

Note that our current methods for constructing minimum spanning trees are agnostic to the weights of the edges. Even though such algorithms choose a minimal number of edges, this doesn't guarantee that the weight of the resulting spanning tree is minimized. For example, consider the following weighted graph:

An example weighted graph

A BFS traversal of the graph starting at produces the following weighted graph:

A spanning tree generated from a BFS of the previous graph

Its weight is but it is not minimal. The following minimum spanning tree is minimal for our graph:

A minimum spanning tree of the previous graph

The weight of this MST is . We clearly need another method of calculating a MST that takes into account (a) the vertices we have yet to explore and (b) the weights of the chosen edges.

There are several algorithms that we could consider. Here, we will consider Prim's Algorithm which we will present as a modification of breadth-first search for this setting. To gain some intuition about how to proceed, let's see how naive breadth-first search failed to produce the MST and then modify the algorithm to obtain the desired result.

BST producing a spanning tree of the previous graph

Initially, we begin our BFS at and then add . We can think of and and the edge as part of our MST. Our goal is to figure out which edge to add to the MST next. Note that we must pick an edge that does not create a loop in the MST, thus we must only consider edges that connect the MST to a vertex not already in the MST.

At this point, BFS would consider next. However, we see that the edge is not in the MST. This is because it turns out it is more profitable to instead include vertex by way of edge instead. How can we avoid making this choice? Note that we have three edges to choose from at this point---, , and ---and the last two edges have a lower weight (1) than (5). It seems like we should consider one of these edges first since its weight is smaller.

If we choose , we obtain the following extended MST that includes , , and :

Choosing BD to extend the spanning tree

Now we can consider the following edges:

  • ---cost 5,
  • ---cost 1, and
  • ---cost 15.

By considering the lowest weight edge next, we then add to the MST:

Extending the spanning tree with BE

We continue in this manner, considering the lowest weight edge that expands the MST by one vertex. We would next add (cost 5):

Extending the spanning tree with EG

Next we would add (cost 3):

Extending the spanning tree with FG

And then finally (cost 4):

Extending the spanning tree with CG

At this point, we know we are done because there are no other vertices left to add to the graph. Alternatively, we know that any tree of the graph contains edges, so once we add this number of edges, we can safely stop.

To summarize, Prim's algorithm proceeds as follows on a graph :

  • Choose a start vertex as an initial consisting of just .
  • For times:
    • Choose the minimum weight edge that connects with a vertex not in and add to .
  • Afterwards is a MST for .

Prim's algorithm is an example of a greedy algorithm. A greedy algorithm is one that, for each iteration, makes its next choice by choosing the minimum or maximum option available. Here, that choice is the minimum weight edge that expands the current MST. However, how do we know this choice is correct?

To prove that our greedy choice is correct, we must first introduce the notion of a cut.

Definition (Cut)

be a graph. A cut of the graph is a partition of its vertices with . A non-trivial cut is one where and .

Since in the above definition uniquely identifies the cut, we will refer to the cut by for notational convenience.

Cuts allow us to talk about the state of Prim's algorithm precisely. At any iteration of the algorithm, we may view the MST as inducing a cut of . The algorithm then considers the minimum weight edge that flows between vertices of the cut, i.e., vertices of the form with and . We must show that this edge belongs to some MST of . In particular, we'll show that it can belong to the eventual MST of the graph grown from the current MST , that is, .

Claim (Cut Property)

Let be a graph and with and be a minimum spanning tree for . Consider the set of edges that connect a vertex in to a vertex not in , i.e.,

And let be an edge with minimum weight of . belongs to some MST of where .

Proof

We prove this fact by contradiction. Let the cut induced by be and the minimum weight edge under consideration be with and . Suppose for the sake of contradiction that does not belong to any MST of of which is a subtree. Now consider an arbitrary MST of , call it . Note that because must be connected in , if does not connect in , there must exist some other path between and in . Let the edge in this path that flows across the cut induced by be .

Now, consider the alternative tree of where we replace with , call it . is a spanning tree because any vertex that was reachable through is now reachable through . Furthermore, note that and are both edges across the cut induced by , but is assumed to have minimum weight among such edges. Therefore, . But only differs from in this edge, so we can conclude that which implies that is a MST for , a contradiction since belongs to it.

This argument is an example of an exchange argument for greedy algorithms. We justify the greedy choice by arguing that any "sub-optimal" choice could be substituted by the greedy choice to obtain a solution at least as good as the original one. Our argument accounts for the fact that since edge weights are not necessarily distinct that and could be both valid minimal choices. The resulting MSTs are, therefore, potentially different, but both have the same (minimal) weight. Note that if the edge weights of are distinct, then is the minimum weight edge of the cut and thus in the proof above is not a MST; . This implies that in the case where edge weights are distinct, the minimum weight edge must belong to any MST of , not just one of them.

We can use this lemma to prove the correctness of Prim's algorithm easily. The algorithm maintains a MST for the vertices it contains so far, extending the MST by one edge (and thus, one vertex) on each iteration. We therefore prove the correctness of Prim's algorithm by induction of the number of iterations of its loop, claiming that is a MST for the vertices it contains so far.

Claim (Correctness of Prim's Algorithm)

On the -th iteration of Prim's on a graph , with and is a MST for .*

Proof

By induction on .

  • . Initially contains a single vertex and is trivially a MST for that one vertex.
  • . Our induction hypothesis says that on iteration , is a MST for In the -st iteration, we extend with an edge across the cut induced by . By the Cut Property, we know that can belong some MST of with . Therefore, we know that extended with is a MST for its vertices.