J.E.D.I
Figure 1.93 Example of Breadth First Search
6.6 Minimum Cost Spanning Tree for Undirected Graphs
A minimum cost spanning tree MST, as defined previously, is a subgraph of a given graph G, in which all the vertices are connected and has the lowest cost. This is
particularly useful in finding the cheapest way to connect computers in a network, and in similar applications.
Finding the MST for a given undirected graph using brute-force approach is not advisable since the number of spanning trees for n distinct vertices is n
n-2
. It is therefore imperative to use another approach in finding the minimum cost spanning tree and in
Data Structures 109
J.E.D.I
this lesson, we will cover algorithms that use the greedy approach. In this approach, a sequence of opportunistic choices succeeds in finding a global optimum. To solve the
MST problem, we shall use Prims and Kruskals algorithms, which are both greedy algorithms.
6.6.1.1 MST Theorem
Let G = V,E be a connected, weighted, undirected graph. Let U be some proper subset of V and u, v be an edge of least cost such that u
∈ U and v
∈ V – U. There exists a
minimum cost spanning tree T such that u, v is an edge in T.
6.6.1.2 Prim’s Algorithm
This algorithm finds the edge of least cost connecting some vertex u in U to some vertex v in V – U at each step of the algorithm:
Let G = V,E be a connected, weighted, undirected graph. Let U denote the set of vertices chosen and T denote the set of edges already taken in at any
instance of the algorithm.
1. Choose initial vertex from V and place it in U. 2. From among the vertices in V - U choose that vertex, say v, which is
connected to some vertex, say u, in U by an edge of least cost. Add vertex v to U and edge u, v to T.
3. Repeat 2 until U = V, in which case, T is a minimum cost spanning
tree for G. For example,
Figure 1.94 A Weighted Undirected Graph
having a as the start vertex, the following shows the execution of Prims algorithm to solve the MST problem:
Data Structures 110
J.E.D.I
Figure 1.95 Result of Applying Prims Algorithm on Graph in Figure 6.16
6.6.1.3 Kruskals Algorithm
The other greedy algorithm that is used to find the MST was developed by Kruskal. In this algorithm, the vertices are listed in non-decreasing order of the weights. The first
edge to be added to T, which is the MST, is the one that has the lowest cost. An edge is considered if at least one of the vertices is not in the tree found so far.
Now the algorithm: Let G = V, E be a connected, weighted, undirected graph on n vertices. The
minimum cost spanning tree, T, is built edge by edge, with the edges considered in non-decreasing order of their cost.
1. Choose the edge with the least cost as the initial edge. 2. The edge of least cost among the remaining edges in E is considered for
inclusion in T. If cycle will be created, the edge in T is rejected. For example,
Data Structures 111
J.E.D.I
The following shows the execution of Kruskals algorithm in solving the MST problem of the graph above:
Data Structures 112
J.E.D.I
Edges MST
U V-U
Remarks
c, e – 1 c, d – 4
a, e – 5 a, c – 6
d, e – 8 a, d – 10
a, b – 11 d, f – 12
b, c – 13 e, f – 20
- a, b, c,
d, e, f List of edges in
non-decreasing order of weight
c, e – 1 accept c, d – 4