Did you know that a spanning tree of an undirected graph is just a connected subgraph covering all the vertices with the minimum possible edges?
In fact, a graph may have more than one spanning tree, as a rule for producing a spanning tree with n vertices and m edges is to remove (m – n + 1 ) edges.
For example, suppose we are given the following undirected graph containing three edges and three vertices. How do we find its spanning trees?
Well, we simply start by removing one edge and see if the resulting graph is a tree.
And what we quickly notice, is that there are three possible spanning trees that we can generate from our undirected graph, depending on which edge we choose to remove.
Now, depending on the number of edges and vertices contained in closed, undirected graph will determine the number of edges that will need to be removed to produce a spanning tree. In fact, did you know that the maximum number of spanning trees for an undirected graph is
This means we can take an undirected graph with a finite set of vertices and create a tree!
Okay, but what if we want to make a minimum spanning tree, where the weight is the smallest possible spanning tree?
Recall that the weight of an edge corresponds to its length or cost, and therefore the weight of a spanning tree is the sum of all the weights of its edges. Consequently, if we are asked to find a minimum spanning tree, or MST, we want to find the smallest sum of weights or lengths of the tree’s edges.
There are two primary algorithms for finding the minimum spanning tree:
- Prim’s Algorithm.
- Kruskal’s Algorithm.
Prim Algorithm
Prim’s algorithm finds the minimum spanning tree by starting with one node and then keeps adding new nodes from its nearest neighbor of minimum weight until the number of edges is one less than the number of vertices, as noted by Simon Fraser University.
So, in summary, Prim’s algorithm starts with a single node and grows a spanning tree by adding edges until every vertex is selected and the minimum spanning tree is built with a Minimal Total Cost = 5 + 3 + 3 + 4 + 4 = 19.
Kruskal Algorithm
Now let’s look at Kruskal’s algorithm and see how it differs in finding the MST.
So, Kruskal’s algorithm keeps adding edges of minimum weight until a minimum spanning tree is created — thus, giving us a Minimal Total Cost of 3 + 3 + 4 + 4 + 5 = 19
It is important to note that while Prim’s algorithm and Kruskal’s algorithm might produce different spanning trees, but they will always provide the same minimal cost. That means you can choose the algorithm you like the best to find the minimum spanning tree!
And lastly, we want to note that Prim’s Algorithm and Kruskal’s Algorithm are greedy algorithms, as they will always produce optimal results. A greedy algorithm generates a solution one piece at a time and offers the optimal result based on the available data. Simply put, a greedy algorithm makes the “best” choice in the current moment. Note that while it’s fast and effective for making decisions on a local level, it’s not always a good fit for global situations.
Video Tutorial w/ Full Lesson & Detailed Examples (Video)
Get access to all the courses and over 450 HD videos with your subscription
Monthly and Yearly Plans Available
Still wondering if CalcWorkshop is right for you?
Take a Tour and find out how a membership can take the struggle out of learning math.