0% found this document useful (0 votes)
5 views3 pages

MST Writeup4 A

The document discusses implementing a Minimum Spanning Tree (MST) using Prim's and Kruskal's algorithms to connect multiple business offices with minimal cost. It outlines the problem of connecting cities with varying costs and explains the greedy approach of both algorithms for finding the MST. The learning objectives include understanding MST concepts and selecting appropriate data structures for implementation.

Uploaded by

poonamd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

MST Writeup4 A

The document discusses implementing a Minimum Spanning Tree (MST) using Prim's and Kruskal's algorithms to connect multiple business offices with minimal cost. It outlines the problem of connecting cities with varying costs and explains the greedy approach of both algorithms for finding the MST. The learning objectives include understanding MST concepts and selecting appropriate data structures for implementation.

Uploaded by

poonamd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Title: Implement MST using appropriate data structures (Prims/Kruskal).

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.

Minimum Spanning Trees

In a weighted graph, the weight of a sub -graph


graph is the sum of the weights of the edges in the sub-
sub
graph. A minimum spanning tree (MST) for a weighted undirected graph is a spanning tree with
minimum weight. To derive an MST, Prim’s algorithm or Kruskal’s algorithm can be used.

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.

Let us understand it with an example:

Algorithm:

Time Complexity O(|E| log |V|)

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:

*Write difference between Prims and kruskal

Conclusion:

Input:

Output:

Conclusion:

You might also like