0% found this document useful (0 votes)
9 views40 pages

Analysis of Algorithms: Minimum Cost Spanning Tree

The document provides an overview of Minimum Cost Spanning Trees (MCST) and discusses two algorithms for finding them: Prim's and Kruskal's algorithms. It explains the concepts, applications, and implementation details of both algorithms, highlighting their advantages and disadvantages. The conclusion emphasizes the significance of the MCST problem in computer science and the comparative efficiency of the two algorithms based on graph density.

Uploaded by

jale.cavus
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)
9 views40 pages

Analysis of Algorithms: Minimum Cost Spanning Tree

The document provides an overview of Minimum Cost Spanning Trees (MCST) and discusses two algorithms for finding them: Prim's and Kruskal's algorithms. It explains the concepts, applications, and implementation details of both algorithms, highlighting their advantages and disadvantages. The conclusion emphasizes the significance of the MCST problem in computer science and the comparative efficiency of the two algorithms based on graph density.

Uploaded by

jale.cavus
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/ 40

Dr.

Mohammed Hussein
Analysis of Algorithms

Minimum Cost Spanning


Tree

DR. MOHAMMED AL-HUBAISHI

1
2
outlines

► Introduction to Minimum Cost Spanning Tree (MCST)


► Prim's Algorithm for Finding MCST
► Kruskal's Algorithm for Finding MCST
► Example of Prim's Algorithm
► Example of Kruskal's Algorithm
► Conclusion

Dr. Mohammed Hussein


3
A minimum cost spanning tree
(MCST) is a subset of edges in a
connected, weighted graph that
connects all the vertices together
without any cycles and has the
minimum possible total edge
Introduction to weight.
Minimum Cost
Spanning Tree The problem of finding the MCST of
a graph is a classic optimization
problem in computer science and
has many real-world applications
such as network design,
transportation planning, and
resource allocation.

Dr. Mohammed Hussein


4
MCST example

E = No. of edges
C= edges we need
V= No. of nodes

Dr. Mohammed Hussein


Total cost = sum of E
5
MCST example
network design
One example of minimum cost spanning tree network design is the
construction of a telecommunications network that connects several
locations.
The network design requires the creation of links (or edges)
between the different locations to ensure that they are all
connected.

Dr. Mohammed Hussein


Assuming there are six locations to be connected, the minimum cost 6
spanning tree will be selected by choosing the edges that connect the
locations with the lowest cost.

The process may involve the following steps:


• List all the possible edges that can be created between the
six locations.
• Assign a cost to each edge based on factors such as the
distance between the locations, cost of construction,
maintenance, etc.
• Sort the edges in ascending order of cost.
• Starting from the lowest cost edge, select the next lowest
cost edge that does not form a cycle with the previously
chosen edges.
• Continue selecting edges until all locations are connected.

The resulting minimum cost spanning tree will have the


minimum total cost of linking all the locations together.
https://onlinegdb.com/KhfW6SHUb

Dr. Mohammed Hussein


7
MCST example
network design

Start from 0

Dr. Mohammed Hussein


8
MCST example
transportation planning

► A minimum cost spanning tree (MCST) can be used for


transportation planning by modeling the transportation network as
a graph, where each node represents a location and each edge
represents a transport route between two locations. The weight of
each edge represents the cost of using that route.
► To find the MCST for transportation planning, the algorithm would
determine the subset of edges that connects all the nodes with the
minimum total cost. This subset of edges would represent the
optimal transport routes for minimizing the cost of transporting
goods or people between the locations.

Dr. Mohammed Hussein


9
MCST example
transportation planning
For example, consider a transportation network with five locations: A, B, C, D, and
E. The cost of each transport route is shown in the following table:

1 2

3 8

Dr. Mohammed Hussein


10
MCST example
resource allocation problem
Suppose you have a company with 5 branches located in different cities, and
you want to connect all the branches with minimum cost. The cost of
building a direct link between any two branches is given below:

the minimum cost of connecting all branches is 1+4+2+3==10 (units of


cost).
Dr. Mohammed Hussein
11
Example1

Dr. Mohammed Hussein


12
Example2

Dr. Mohammed Hussein


13
Example3

Dr. Mohammed Hussein


14
Prim's Algorithm for Finding
MCST

► Prim's algorithm is a greedy algorithm that finds the MCST of a


graph. It starts with an arbitrary vertex and repeatedly adds the
cheapest edge that connects an explored vertex to an unexplored
vertex until all vertices are explored.
► This algorithm has a time complexity of O(E log V) where E is the
number of edges and V is the number of vertices in the graph.

Dr. Mohammed Hussein


15
How to implement Prim’s Algorithm?

• Create a set mstSet that keeps track of vertices already included in MST.
• Assign a key value to all vertices in the input graph. Initialize all key values
as INFINITE. Assign the key value as 0 for the first vertex so that it is
picked first.
• While mstSet doesn’t include all vertices
• Pick a vertex u that is not there in mstSet and has a minimum key value.
• Include u in the mstSet.
• Update the key value of all adjacent vertices of u. To update the key values,
iterate through all adjacent vertices.
• For every adjacent vertex v, if the weight of edge u-v is less than the previous key
value of v, update the key value as the weight of u-v.

https://www.geeksforgeeks.org/prims-minimum-spanning-tree-mst-greedy-algo-5/
Dr. Mohammed Hussein
16
Pseudocode for Prim's algorithm:

1. Initialize a priority queue PQ with all vertices except the starting vertex,
and set their distances to infinity.
2. Set the distance of the starting vertex to 0 and add it to the MCST.
3. While PQ is not empty:
a) Remove the vertex with the smallest distance from PQ and add it to the
MCST.
b) For each neighboring vertex v of the newly added vertex, if v is still in the
PQ and the edge weight between the newly added vertex and v is smaller
than the current distance of v in the PQ, update the distance of v in the PQ
to the new edge weight.
4. Once all vertices have been added to the MCST, return it.

Dr. Mohammed Hussein


17
pseudocode for Prim's algorithm
18
The steps of Prim's algorithm:

1. Step 1: Determine an arbitrary vertex as the starting vertex of the MST.


2. Step 2: Follow steps 3 to 5 till there are vertices that are not included in the MST (known as fringe
vertex).
3. Step 3: Find edges connecting any tree vertex with the fringe vertices.
4. Step 4: Find the minimum among these edges.
5. Step 5: Add the chosen edge to the MST if it does not form any cycle.
6. Step 6: Return the MST and exit

https://www.geeksforgeeks.org/prims-minimum-spanning-tree-mst-gre
edy-algo-5/

Dr. Mohammed Hussein


19
Prim's Algorithm for Finding
MCST
► The idea of using key values is to pick the minimum weight edge from
the cut. The key values are used only for vertices that are not yet included
in MCST, the key value for these vertices indicates the minimum weight
edges connecting them to the set of vertices included in MCST.
► The basic idea behind Prim's algorithm is to start with an arbitrary vertex,
and add the closest vertex to the growing MCST until all vertices are
visited.

Dr. Mohammed Hussein


20
Prim's Algorithm for Finding MCST

The basic idea behind Prim's algorithm is to start with an arbitrary vertex,
and add the closest vertex to the growing MCST until all vertices are visited.

Dr. Mohammed Hussein


https://onlinegdb.com/CumioTNfQ
21
Example :Prim's Algorithm for
Finding MCST

Dr. Mohammed Hussein


22
Example :Prim's Algorithm for
Finding MCST

Dr. Mohammed Hussein


23
Advantages of Prim’s
algorithm

1. Prim’s algorithm is guaranteed to find the MST in a


connected, weighted graph.
2. It has a time complexity of O(E log V) using a binary heap or
Fibonacci heap, where E is the number of edges and V is the
number of vertices.
3. It is a relatively simple algorithm to understand and
implement compared to some other MST algorithms.

Dr. Mohammed Hussein


24
Disadvantages of Prim’s
algorithm

1. Like Kruskal’s algorithm, Prim’s algorithm can be slow on dense


graphs with many edges, as it requires iterating over all edges at
least once.
2. Prim’s algorithm relies on a priority queue, which can take up extra
memory and slow down the algorithm on very large graphs.
3. The choice of starting node can affect the MST output, which may
not be desirable in some applications.

Dr. Mohammed Hussein


25
Kruskal's Algorithm for Finding
MCST

► Kruskal's algorithm is another greedy algorithm that finds the


MCST of a graph. It starts with the edges sorted by weight and
repeatedly adds the next cheapest edge that does not create
a cycle until all vertices are connected.
► This algorithm has a time complexity of O(E log E) where E is the
number of edges in the graph.

Dr. Mohammed Hussein


26
Example of Kruskal's Algorithm

Using Kruskal's algorithm, we sort the edges by weight and add the
next cheapest edge that does not create a cycle until all vertices are
connected.

Dr. Mohammed Hussein


27
Example of Kruskal's Algorithm

Dr. Mohammed Hussein


28
Example of Kruskal's Algorithm

https://onlinegdb.com/BXg4N43lF
Dr. Mohammed Hussein
29
How to find MST using Kruskal’s
algorithm?

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.

Dr. Mohammed Hussein


30
Union-Find algorithm to detect cycles

https://www.geeksforgeeks.org/introduction-to-disjoint-set-data-structure-or-union-find-algorithm/
31
Step 2 uses the Union-Find
algorithm to detect cycles.

1. UNION
2. FIND
A set is divided into subsets. Each subset contains
related elements. If we come to know that the two
element ai and aj are related, then we can do the
followings:
3. Find the subset : Si containing ai
4. Find the subset : Sj containing aj
5. If Si and Sj are two independent subsets
then we create a new subset by taking union of Si and Sj
► New subset = Si ∪ Sj

Dr. Mohammed Hussein


Difference between Prim’s and
Kruskal’s algorithm for MST 32
Prim’s Algorithm Kruskal’s Algorithm

It starts to build the Minimum Spanning Tree from any vertex in It starts to build the Minimum Spanning Tree from the vertex
the graph. carrying minimum weight in the graph.

It traverses one node more than one time to get the minimum
It traverses one node only once.
distance.

Prim’s algorithm has a time complexity of O(V 2), V being the


Kruskal’s algorithm’s time complexity is O(E log V), V being the
number of vertices and can be improved up to O(E log V) using
number of vertices.
Fibonacci heaps.

Kruskal’s algorithm can generate forest(disconnected


Prim’s algorithm gives connected component as well as it works
components) at any instant as well as it can work on
only on connected graph.
disconnected components

Prim’s algorithm runs faster in dense graphs. Kruskal’s algorithm runs faster in sparse graphs.

It generates the minimum spanning tree starting from the root It generates the minimum spanning tree starting from the least
vertex. weighted edge.

Applications of prim’s algorithm are Travelling Salesman


Applications of Kruskal algorithm are LAN connection, TV
Problem, Network for roads and Rail tracks connecting all the
Network etc.
cities etc.

Prim’s algorithm prefer list data structures. Kruskal’s algorithm prefer heap data structures.
33

Dr. Mohammed Hussein


34
Start from 2

Dr. Mohammed Hussein


35
ex

Dr. Mohammed Hussein


36
Where is the start node ?

Dr. Mohammed Hussein


37
ex4

Dr. Mohammed Hussein


38
Where is the start node ?

Dr. Mohammed Hussein


39
Conclusion

► In conclusion, the MCST problem is an important optimization


problem in computer science with many real-world applications.
► Both Prim's and Kruskal's algorithms are effective ways of finding the
MCST of a graph, with Prim's having a better time complexity for
sparse graphs and Kruskal's having a better time complexity for
dense graphs.

Dr. Mohammed Hussein


40

…questions, comments, etc. are welcome…

DR : Mohammed Al-Hubaishi

You might also like