Theory of the Minimum Spanning Tree Problem [Part 1]
📅December 30, 2020 | ☕️25 minutes read
In this blog post, I will discuss the inner structure and some theoretical results behind the minimum spanning tree problem in the context of undirected graphs. Firstly, we define a spanning subgraph and a minimum spanning subgraph. After proving some results regarding the structure of the minimum spanning tree problem, we develop two famous algorithms - Prim’s algorithm and Kruskal’s algorithm using the inner structure of the problem discussed in results proved before. The point of this approach which firstly develops the theory and then develops the algorithm is to shed some light on the key ideas behind those algorithms. After having discussed the structure of this problem, both algorithms seem completely natural and their correctness suddenly becomes evident. This is particularly important since both of these algorithms belong to the category of greedy algorithms, which gradually build the optimal solution to the original problem by making only the locally optimal choices. Clearly, it is common that the correctness proof of such algorithms becomes quite involved.
Definition 1: a spanning subset and a spanning tree
Let be an undirected connected graph having the vertex set and edge set . Then a subgraph where is a spanning subgraph of the graph G if the graph is connected. If is a tree, we say is a spanning tree of the graph G.
Now, we state and prove two important theorems which guarantee existence of minimum spanning tree for every connected graph.
Theorem 1: Every connected graph has a spanning tree.
Claim: Let be an undirected connected graph having the vertex set and edge set . Then has a spanning tree and hence a spanning subgraph.
Proof: We will prove by induction on the number of vertices .
Basis: Suppose = 1. Trivially, is connected. The graph is trivially a spanning tree. This establishes the basis.
Induction Hypothesis: Suppose that for all graphs , with , the claim holds.
Induction Step: Now consider an undirected connected graph with the vertex set such that . Since , let . Since the graph is connected, , there exists a path . Now, by the previous statement, there exists an edge for some . Consider the subgraph where:
Observe that is a subgraph of . Since was connected, so is because otherwise cannot be connected. By Induction Hypothesis, has a spanning tree, say for some . Now consider where . We claim that is a spanning tree of the graph . Since , does not induce a cycle in and connects . Since was a tree which is a subgraph of , all vertices in are also connected in and the edge connected to all of vertices in . Therefore, since is a connected graph without cycles, we deduce it is a tree. Since vertices of are , is a spanning tree of .
This completes the proof.
Remark: Does every undirected graph have a spanning tree? The answer is no, because any graph without edges and more than one vertex does not have a spanning tree. Therefore, the connectedness assumption of the graph is a crucial assumption which cannot be dropped.
Definition 2: a minimum spanning subset and a minimum spanning tree
Let be a positive edge cost function. Define . We say that a spanning subset of is minimal if is minimal amongst all possible spanning subgraphs of .
Corollary 1: Every connected graph has a minimum spanning tree.
Proof: By Theorem 1, every connected graph has a spanning tree. Since the number of spanning trees of any connected graph is finite, we deduce that for every connected graph there exists at least one minimum spanning tree.
Theorem 2: Assuming the strictly positive edge cost function, every minimal spanning subgraph is a tree.
Claim: Let be an undirected connected graph having the vertex set and edge set . Let be a positive edge cost function. Then every minimal spanning subgraph of G is a tree.
Proof: (By contradiction)
Let be a minimal spanning subgraph of the graph . Assume, for the sake of contradiction, that is not a tree. By definition of , is connected. Since is connected and it is not a tree, it must be the case has a cycle. Suppose is a cycle in the graph . Now select any edge . Consider and the graph . Since is a cycle, vertices still remain connected in along some path in . Moreover, we claim that is connected. Select . Since is a spanning subset of , there exists a path . We split in cases depending on whether .
Suppose . Then and remain connected in by existence of the path in . By construction of , is in .
Suppose . Then and remain connected in by existence of the path in . By construction of , is in .
Since was arbitrary, we deduce is connected so must be a spanning subgraph of . Observe that . Since is a positive edge cost function, so . This contradicts the minimality of . The assumption is not a tree was wrong. Hence must be a a tree. This completes the proof.
Definitions stated above ensure the (minimum) spanning trees or subgraphs are precisely defined while above proved theorems guarantee the existence of spanning tree in connected graphs. Although these results may be basic, they outline a general setup for more interesting results, such as the following one.
Theorem 3: Uniqueness of a minimum spanning tree
Claim: Let be an undirected connected graph having the vertex set and edge set . Let be an injective edge cost function. Then the minimum spanning tree of the graph must be unique.
Proof: (By contradiction)
Suppose, for the sake of contradiction, that there exist two distinct minimum spanning trees and . Since and are distinct, let be the minimum cost edge that is not present in both and . By the fact is injective, the edge is the unique edge that is not present in both and of the minimum cost. Assume, without loss of generality, that is present in and hence not present in . Since is a spanning tree, there exists the unique path . We claim there exists an edge such that where is a path in and .
By construction of , the path can be decomposed as where is possibly and . If the path is entirely included in , we obtain a cycle which contradicts the fact was a tree. Hence it must be the case there exists such that where is a path in and . Observe that .
Now consider the graph where . We claim that is as spanning tree of . By the fact is the unique path from to in the tree , the graph is split into two disjoint connected components, one containing and the other one containing , say and respectively. Observe that and are actually subtrees of . Since reside in disjoint connected components of , and , addition of an edge to connects and without induction of cycle so the graph must be connected. Moreover, since and were subtrees and are joined by a single edge without cycle, we deduce is a tree. Since the vertex set of is and is a tree, we deduce is spanning so is a spanning tree of the graph .
Now consider .
Since and is a spanning tree, cannot be a minimum spanning tree. This is a contradiction. The assumption has distinct minimum spanning trees was wrong, so we deduce has the unique minimum spanning tree, as claimed.
It turns out that in case of connected graphs, there always exists a minimum spanning tree containing the minimum cost edge.
Theorem 4: There exists a minimum spanning tree containing minimum cost edge.
Claim: Let be an undirected connected graph having the vertex set and edge set . Let be an edge cost function. Let be a not necessarily unique edge with the minimum cost amonst all edges in . Then there exists a minimum spanning tree of the graph containing .
Proof: (By contradiction)
Let be a minimum spanning tree of the graph which does not include an edge , that is, . Since is a tree, there exists an unique path . Since , we decompose the path above as where possibly equals and . Now consider . We will show that is a tree and that is spanning. Since the path is unique path from to in , removing an edge from disconnects into two disjoint components (trees), one containing and the other one containing . Say and respectively. Now since and reside in distinct disjoint components , , addition of the edge connects and into a singe component , since cannot induce a cycle. Since and are subtrees of , connecting them via produces a tree . Since is a tree with the vertex set , is a spanning tree of the graph . We also have:
Since is a minimum spanning tree, . Combining this result with the previous one, we obtain . Hence is a minimum spanning tree of containing an edge of the minimum cost. This completes the proof. .
Theorem 5: Elementary cycle properties of a minimum spanning tree
Claim: Let be an undirected connected graph having the vertex set and edge set . Let be an edge cost function. Suppose is a cycle in . The following holds:
- Suppose an edge is the edge of the maximum cost in the cycle above. If has strictly larger cost then any other edge in the cycle above, cannot be included in a minimum spanning tree of .
- Suppose is the edge of the minimum cost from the cycle above. Then may not be included in some minimum spanning tree of .
Proof of 1: (By contradiction)
Let be a minimum spanning tree of the graph which does include an edge . Consider the graph . Since was a tree, removing an edge disconnects into two disjoint connected components and such that contains and contains . Now, since is a cycle, there exists some edge on the cycle such that and . By assumption regarding , we have . Now set . We claim is a spanning tree of . Since and are disjoint, adding an edge connects and into a single connected component. Since and were subtrees of , we deduce is also a tree. Since it includes all vertices from , we deduce is a spanning tree. We also have
Since is a spanning tree, we deduce cannot be a minimum spanning tree. This is a contradiction to the fact was a minimum spanning tree. The assumption includes was wrong, so we deduce cannot contain . Since was arbitrary, no minimum spanning tree of contains . This completes the proof.
Counterexample for 2:
In the graph above, consider the cycle . An edge of minimum cost is .
However, the minimum spanning tree of the graph above is:
. Observe that does not include .
After having shown the previous results, to prove possibly the most important result regarding the structure of the minimum spanning tree problem, we need to define a concept of the graph cut.
Definition 3: a graph cut, crossing edge
Let be a graph having the vertex set and edge set . A cut is a partition of . An edge crosses the cut if .
Now we state and prove the fundamental result regarding the structure of the minimum spanning tree problem. Consider again the Theorem 4. Theorem 4 indicates that it is always ‘safe’ to include a minimum cost edge in a ‘potential’ partial minimum spanning tree, because it must be a part of some minimum spanning tree. However, the step/s after this one are not immediatelly clear and we need a bit more insight into the structure of the minimum spanning tree problem to discover and justify what could be the next possible steps in the process of finding some minimum spanning tree.
Theorem 6: The fundamental cut property of the minimum spanning tree problem
Claim: Let be an undirected connected graph having the vertex set and edge set . Let be an edge cost function. Let be a cut of the graph . Let be the set of edges which belong to some minimum spanning tree and assume no edge in crosses the cut . Let be the edge crossing the cut whose cost is minimal amongst edges crossing the cut . Then an edge belongs to some minimum spanning tree of .
Remark: The intuitive explanation of this claim is the following assertion: Suppose we somehow have a subset of edges of some minimum spanning tree . If we can find a suitable cut of the graph and the minimum cost edge that crosses the cut, we can safely extend to . By ‘safely’ extending we mean that is also a subset of some minimum spanning tree. This means that by iteratively finding suitable cuts and minimum weight crossing edges, we eventually construct a minimum spanning tree!
Proof: (By construction)
Let be a minimum spanning tree whose edge set contains , . It suffices to show there exists a minimum spanning tree containing edges . If then and the claim holds.
Thus, suppose . We will construct a minimum spanning tree containing . Since is a spanning tree, there exists the unique path between vertices and in . Since , and since and that path decomposes into where , and possibly or but not both and . Consider . We will show is a tree which is also a spanning tree such that .
We will firstly show that is indeed a tree.
Since is a tree, the path is unique so disconnects . More precisely, the graph consists of precisely two disjoint connected components, one containing , say , and the other one containing , say .
Since both and are in fact subgraphs of which is a tree, both of them are actually subtrees of . Now consider an edge . Since is a minimum weight edge crossing the cut and since is crossing the cut, we have . Clearly, since is crossing the cut, . Also, since is connecting two disjoint connected components, the addition of to cannot induce a cycle and must connect and . The resulting graph is therefore a tree since it is connected and has no cycles. Moreover, since was spanning, so is since it is connected and has the same vertex set as . We deduce is also a spanning tree of .
Now, we examine the weight of the spanning tree :
Since was a minimum spanning tree . By combining two above results we obtain . Hence is a minimum spanning tree containing . Since and , we have that the set
, as desired.
This completes the proof.
Prim’s Algorithm
Intuitive Minimum Spanning Tree Algorithm
Now, using the Theorem 6, we can think of an algorithm which gradually builds a minimum spanning tree. We start from some vertex and consider the cut . Since must be present as a subset of any minimum spanning tree edge set, by selecting the least cost edge from , say we can safely consider as a subset of some minimum spanning tree which is justified by Theorem 6. We repeat the process until we do not obtain a minimum spanning tree. We have just described the hand-wavy Prim’s algorithm. A careful reader may question the convergence of the process discussed and that will be addressed in the proof of the correctness of Prim’s algorithm.
Prim’s Algorithm implementation
Assumptions
Assume that the variable holds the instance of a priority queue abstract data type. In the context of this algorithm, we assume that the priority queue is implemented as a binary heap.
Theorem 7: Proof of Correctness of Prim’s algorithm
Claim: Procedure Prim correctly computes the cost and the edge set of some minimum spanning tree.
Proof: (By induction)
We will consider the following invariant and prove it by induction:
Prior to the -th iteration of the while loop, where , the set contains the vertices of the ‘partial’ minimum spanning tree defining the cut , the set contains the edges of the ‘partial’ minimum spanning tree which are subset of edges of some actual minimum spanning tree and no edge in the set crosses the cut . The priority queue stores vertices in whose key is the cost of the minimum cost edge crossing the cut entering each vertex.
Basis: Prior to the first iteration of the while loop, corresponding to , , . Trivially, is a cut while is a subset of the edge set of any minimum spanning tree. The for loop in the line 2 ensures is initialised correctly. Since there are no edges crossing the graph cut the initialisation of is indeed correct, so the basis is established.
Induction Assumption: Assume that the stated invariant holds for all iterations upto the -th iteration of the while loop.
Induction Step:
By the invariant, the set contains the vertices of the ‘partial’ minimum spanning tree defining the cut and the set contains the edges of the ‘partial’ minimum spanning tree which are subset of edges of some actual minimum spanning tree and no edge in the set crosses the cut .
In the while loop body, the vertex is extracted from the priority queue.
The cut is updated to .
Suppose .
Then is the start vertex and there was no incoming edge. Observe that since is the start vertex, the graph is a tree and since is empty, it remains the subset of some minimum spanning tree while it does not cross the cut . We conclude that the graph is a tree.
Suppose .
By the invariant, is the minimum cost edge crossing the cut entering the vertex . By Theorem 6, is a subset of edges of some minimum spanning tree. Lines 14,15 correctly update the set to and correctly increment the cost of the ‘partial’ minimum spanning tree built so far. Since was a tree and is a crossing edge, does not induce any cycle so the graph remains a tree.
Now the for loop in the line 16 considers all possible outgoing edges of . Observe that all those edges , where is a vertex adjacent to are actually crossing edges. The conditional in line 17 correctly updates values of the minimum cost edge entering any vertex in the set if the update is necessary. Observe that those updates maintain the statement regarding in the invariant. It is possible that not all crossing edges affecting are considered. This is fine since the invariant for -th iteration guarantees that crossing edges which are not updated are still the least cost crossing edges. Therefore, the invariant is established for the next iteration.
Now consider what happens at the termination of the while loop. The while loop terminates precisely when . But then . By the invariant proved before, the ‘partial’ minimum spanning tree includes all vertices in . By the invariant, is the set of edges of the ‘partial’ minimum spanning tree which is also subset of edges of some minimum spanning tree. By invariant, is a tree. Since , we deduce is a spanning tree. Since is the subset of edges of some minimum spanning tree, we deduce is a minimum spanning tree. By invariant, the cost of was computed correctly. This completes the proof.
Some observations
- According to Theorem 4, there exists a minimum spanning tree containing the least cost edge. Observe that this will be the top edge in the priority queue, which will be the first edge used to connect source vertex in the Prim’s algorithm
- It is possible to adapt Prim’s algorithm to count minimum spanning trees. Observe that Prim’s algorithm implicitely resolves ties with respect to the edge cost by taking the first discovered edge of the particular cost. By performing the process of considering all edges of particular cost in growing a ‘partial’ minimum spanning tree, by using any complete graph search algorithm (BFS, DFS, …) we can enumerate all minimum spanning trees.
Running time analysis of Prim’s Algorithm
Claim: Procedure Prim executes operations.
Proof: Let be the number of operations executed in the procedure Prim.
- The for loop in the line 2 executes precisely times and its body performs only constant time operations. Hence, there are operations performed in this loop
- Line 5 is a constant time operation
- Line 6 is a min heap heapify operation which takes operations
- Lines 7 to 9 are constant time operations
Now we analyse the main while loop. Each vertex gets extracted once and only once from the heap . Each extraction operation takes operations. Observe that once that regardless of whether or not the if in the line 13 passes, there are at most constant number of operations in the while loop body except the inner for loop. Observe that for loop executes once and only once for each edge in across the whole execution of the algorithm. There are operations spent in each heap update. Since each edge gets considered from both endpoints, there can be at most heap operations throughout the whole execution of the algorithm coming from processing edges. Each such operation itself causes rebalancing the heap and hence takes operations. Hence the inner for loop contributes with operations throughout the whole execution of the algorithm. Recall, since the graph is connected, .
By summing up all contributions, we get:
This completes the analysis.
Kruskal’s Algorithm
Unlike Prim’s algorithm, which constructs a minimum spanning tree by ‘growing’ a single ‘partial’ minimum spanning tree, Kruskal’s algorithm takes a different approach. Kruskal’s algorithm relies on cleverly merging subtrees of a ‘forest’ until the ‘forest’ becomes a tree. Main idea is to start from a forest of leaves representing each vertex and in each step connect two disjoint forest components corresponding to the least cost ‘crossing’ edge. Theorem 6 gives conditions which guarantee the components merged in the process remain subtrees of some minimum spanning tree.
Here is a bit deeper explanation. Clearly, every vertex must be included in some minimum spanning tree. Hence every component in the initial forest of singleton vertex trees is a part of some minimum spanning tree. We can think of this as a ‘base’ case. Now, suppose that we have a forest of components (trees) for some . Assume all are parts of some (not necessarily the same) minimum spanning tree. Suppose is the set of edges of the forest. Now let be the edge connecting two distinct components, where and . Observe that is crossing the cut . By Theorem 6, is a subset of some minimum spanning tree. Observe that the number of components decreases by one every time a merge is performed. Hence the process completes after exactly iterations. One challenge is quickly determining whether and belong to distinct components. It turns out there is a data structure solving exactly this problem - disjoint sets. This process is the main intuition behind Kruskal’s algorithm:
Theorem 8: Proof of Correctness of Prim’s algorithm
Claim: Procedure Kruskal correctly computes the cost and the edge set of some minimum spanning tree.
Proof: (By induction)
We will consider the following invariant and prove it by induction:
Prior to the -th considered edge in the for loop in line 10, where , the set contains the edges of the forest . is a subset of edges of some minimum spanning tree. Furthermore, contains all and only the edges which were minimum cost crossing edges connecting disjoint components discoverable in the first edges of in the sorted order. Edges in are the first discovered such edges.
Basis: Consider . By the for loop in the line 5, there are precisely disjoint singleton trees in the forest . Since the edge set is , and is a subset of any minimum spanning tree edge set, the invariant holds.
Induction Assumption: Assume the claim holds for .
Induction Step: Consider -th iteration.
Suppose .
Firstly, we prove that causes the union operation is a minimum cost edge crossing the cut where no edge in crosses .
Suppose causes the union operation. Then the correctness of disjoint set means so and belong to disjoint components and . Now consider the cut . Since contains all and only the edges in current forest and , it must be the case and are not connected in the current forest. But then, contains no edge crossing the cut because otherwise and would be connected. We claim that is the minimum cost edge to connect and directly or indirectly. By the sorted order of , if there existed an edge to connect connect and directly or indirectly, that edge had to have the strictly smaller cost than and had to appear before . But then, by invariant, that edge had to cause the union operation in some previous iteration. This means that now belong to the same connected component. This contradicts the fact . Hence is indeed a minimum cost edge to connect and directly or indirectly.
Suppose is an edge crossing the cut where no edge in crosses . Then, by the correctness of disjoint set , so the union occurs.
We condition on whether causes the union operation or it does not.
Suppose causes the union operation.
By the proof of the previous claim, is a minimum cost edge crossing the cut where no edge in crosses . Since is a subset of edges of some minimum spanning tree, by Theorem 6, is also a subset of edges of some minimum spanning tree. The algorithm correctly updates and in lines 13,14 while correctly merges and in line 12. This establishes the invariant for the next iteration.
Suppose does not cause the union operation. Then . Then and belong to the same component, so contains some edge crossing . By the invariant, that edge had smaller or equal cost then so cannot improve the current forest and can only induce a cycle. Hence must not contain . Components remain untouched so the invariant for the next iteration holds by the previous invariant and the fact should not be included in the set .
This proves the induction step.
After the termination of the main for loop, is a subset of edges of some minimum spanning tree. We claim is a minimum spanning tree. There are precisely edges in any minimum spanning tree and all of them are connecting some disjoint components since removal of each edge disconnects that minimum spanning tree. Furthermore, all those edges are the minimum cost edges crossing some cut, because otherwise we can contradict the optimality of the minimum spanning tree by replacing them with a better alternative. We will show contains edges. Assume is a minimum cost edge crossing some cut . By the invariant, if is the first such an edge in the sorted order of it will be included in . Otherwise, there existed some other edge crossing the same cut which was discoverable before which was included in by the invariant. Since was arbitrary, . Now, since any minimum spanning tree contains precisely edges and is a subset of edges of some minimum spanning tree with , we deduce is a minimum spanning tree. This implies the correctness of the Kruskal’s algorithm.
Some observations
- According to Theorem 4, there exists a minimum spanning tree containing the least cost edge. Observe that this will be the first edge in sorted edge set, which will be the first edge used to connect two leaves in the Kruskal’s algorithm.
- An efficient implementation of Kruskal’s algorithm relies on disjoint sets data structure, which is a very specific and interesting data structure that can be implemented in many ways (as linked lists, as trees …). There are several obstacles in the implementation of disjoint sets which are solved by using path compression and weighted union heuristics.
- Kruskal’s algorithm relies on the fast implementation of some sorting algorithm, which is one of the fundamental problems in computer science. If the distribution of edge costs is known, a faster linear time sorting algorithm can be employed.
- Handling ties in Kruskal’s algorithm is very transparent and its execution is entirely deterministic after the sorting completes. Different sorting sorting algorithms used to sort edges may result in different resulting minimum spanning trees.
Running time analysis
Claim: Procedure Kruskal executes operations.
Proof: Let be the number of operations executed in the procedure Kruskal. Now:
- Lines 2..4 are constant time operations
- The for loop in the line 5 contributes to disjoint set make operations
- The for loop in the line 7 executes times and its body is a constant time operation.
- Assuming the usage of the optimal comparison based sorting algorithm, the operation in line 9 takes operations.
Now we apply the aggregate analysis to the main for loop in the line 10. Clearly, the main for loop executes precisely times and each operation takes constant number of operations (by the properties of disjoint set data structure). Hence each execution of the main for loop takes at least number of operations. Now we will count the number of executions of the if body throughout entire execution of the main for loop. By Correctness of Kruskal’s algorithm, there will be exactly edges in . But that means there are exactly disjoint set union operations corresponding to executions of if body throughout entire execution of the main for loop. Using the properties of disjoint set data structure with weighted union, throughout entire execution of the algorithm, all disjoint set operations take atomic operations. Assuming the graph is connected, we have so .
By summing up all contributions, we get:
This completes the analysis.