Experiment No. 4 Design and Analysis Spanning Tree: Solve Minimum Cost Spanning Tree Problem Using Greedy Method
Experiment No. 4 Design and Analysis Spanning Tree: Solve Minimum Cost Spanning Tree Problem Using Greedy Method
4
Solve minimum cost spanning tree problem using greedy method.
Moreover, if there exist any duplicate weighted edges, the graph may
have multiple minimum spanning tree.
In the above graph, we have shown a spanning tree though it’s not the
minimum spanning tree. The cost of this spanning tree is (5 + 7 + 3 + 3
+ 5 + 8 + 3 + 4) = 38.
We will use Prim’s algorithm to find the minimum spanning tree.
Prim’s Algorithm
It is a greedy algorithm. It starts with an empty spanning tree. The idea is to
maintain two sets of vertices:
Contain vertices already included in MST.
Contain vertices not yet included.
At every step, it considers all the edges and picks the minimum weight edge.
After picking the edge, it moves the other endpoint of edge to set containing
MST.
Steps for finding MST using Prim's Algorithm:
Create MST set that keeps track of vertices already included in MST.
Assign key values to all vertices in the input graph. Initialize all key values as
INFINITE (∞). Assign key values like 0 for the first vertex so that it is picked
first.
While MST set doesn't include all vertices.
Pick vertex u which is not is MST set and has minimum key value. Include 'u'to
MST set.
Update the key value of all adjacent vertices of u. To update, iterate through
all adjacent vertices. For every adjacent vertex v, if the weight of edge u.v less
than the previous key value of v, update key value as a weight of u.v.
MST-PRIM (G, w, r)
1. for each u ∈ V [G]
2. do key [u] ← ∞
3. π [u] ← NIL
4. key [r] ← 0
5. Q ← V [G]
6. While Q ? ∅
7. do u ← EXTRACT - MIN (Q)
8. for each v ∈ Adj [u]
9. do if v ∈ Q and w (u, v) < key [v]
10. then π [v] ← u
11. key [v] ← w (u, v)
Kruskal's Algorithm:
An algorithm to construct a Minimum Spanning Tree for a connected weighted
graph. It is a Greedy Algorithm. The Greedy Choice is to put the smallest
weight edge that does not because a cycle in the MST constructed so far.
Analysis:
Where E is the number of edges in the graph and V is the number of vertices,
Kruskal's Algorithm can be shown to run in O (E log E) time, or simply, O (E log
V) time, all with simple data structures. These running times are equivalent
because:
1. O (E log E) = O (E log V).