MST Writeup4 A
MST Writeup4 A
Problem Statement: You have a business with several offices; you want to lease phone lines to
connect them up with each other; and the phone company charges different amounts of money to
connect different pairs of cities. You want a set of lines that connects all your offices
office with a
minimum total cost. Solve the problem by suggesting appropriate data structures.
Learning Objectives:
To understand the concept of minimum spanning tree
tree(MST).
Learning Outcome:
Students will able to finalize the appropriate data structures.
Studentss will be able to implement minimum cost spanning tree using Prims/Kruskal.
Theory:
According to given statement, as we have several business offices which are located at different
cities and we want a phone line to connect them with each other the phone company
co charges
different amounts of money to connect different pairs of cities. we want a set of lines that connect
all your offices with minimum total cost. So the appropriate data structure to connect to these cities
is minimum spanning tree where we use kru
kruskal's
skal's algorithm to get the minimum cost spanning tree.
The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that
graph as shown below:
Kruskal's algorithm
Kruskal’s algorithm to find the minimum cost spanning tree uses the greedy approach. The Greedy Choice is
to pick the smallest weight edge that does not cause a cycle in the MST constructed so far.
Steps:
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If the cycle is not
formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.
Algorithm:
Prim's Algorithm
Prim's algorithm doesn't pre-sort all edges. Instead, it uses a Priority Queue to maintain a running sorted
next-possile vertices.
Starting from one vertex, loop through all unvisited neighbours and enqueue a pair of values for each
neighbour - the vertex and the weight of edge connecting current vertex to the neighbour. Each time it
greedily select the top of the priority queue (the one with least weight value) and add the edge into the
final MST if the enqueued neighbour hasn't been already visited.
The following graph demonstrates the steps:
Algorithm:
Conclusion:
Input:
Output:
Conclusion: