Analysis of Algorithms: Minimum Cost Spanning Tree
Analysis of Algorithms: Minimum Cost Spanning Tree
Mohammed Hussein
Analysis of Algorithms
1
2
outlines
E = No. of edges
C= edges we need
V= No. of nodes
Start from 0
1 2
3 8
• 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.
https://www.geeksforgeeks.org/prims-minimum-spanning-tree-mst-gre
edy-algo-5/
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.
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.
https://onlinegdb.com/BXg4N43lF
Dr. Mohammed Hussein
29
How to find MST using Kruskal’s
algorithm?
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
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 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.
Prim’s algorithm prefer list data structures. Kruskal’s algorithm prefer heap data structures.
33
DR : Mohammed Al-Hubaishi