0% found this document useful (0 votes)
16 views

Minimum spanning tree using Prims algorithm

Presentation on MST using Prims algorithm,DSA

Uploaded by

sabanoor0405
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Minimum spanning tree using Prims algorithm

Presentation on MST using Prims algorithm,DSA

Uploaded by

sabanoor0405
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

DATA STRUCTURES

PRESENTATION
MINIMUM SPANNING TREE USING
PRIMS ALGORITHM

GROUP MEMBERS
SABA NOOR 21
KINZA ALI 04
WISHAH ZUBAIR 34
MINIMUM SPANING TREE USING
PRIMS ALGORITHM
A Minimum Spanning Tree (MST) of a
connected, undirected graph is a subgraph
that connects all the vertices together with
the minimum possible total edge weight and
without forming any cycles.
KEY POINTS:
• Spanning:
An MST must include all the vertices in the
original graph.
• Tree:
There are no cycles.
The graph remains connected.
Minimum Total Weight: The total sum of the
edge weights in the MST is minimized.
Prim’s Algorithm: A Solution for Finding MST
Greedy Approach:
Prim's algorithm works by continuously adding the minimum-weight edge that
connects a vertex inside the MST to a vertex outside of the MST.
Efficiency: Prim’s algorithm is efficient and works well for dense graphs
(graphs with many edges).

It has a time complexity of O(ElogV), where 𝑉 is the number of vertices


Time complexity:

E is the number of edges, especially when implemented with a priority queue


(binary heap).

Steps in Prim's Algorithm


1) Start from any vertex: Choose an arbitrary vertex to begin the MST.
2) Select the minimum edge: From the current tree, select the edge with the
smallest weight that connects to a vertex outside the tree.
3) Add the edge: Add this edge and the corresponding vertex to the tree.
4) Repeat until all vertices are included in the tree.
APPLICATIONS OF MST

Telecommunications networks:
Minimizing the cost of laying cables or wires between
various locations.
Road network design:
Finding the least costly network of roads connecting cities.
Works for Dense Graphs:
Prim’s algorithm is especially useful for graphs with a large
number of edges (dense graphs).
Simple and Incremental:
It's easy to understand and implement, and it works
incrementally, growing the MST one edge at a time.
No need for global sorting:
Unlike Kruskal’s Algorithm, which requires sorting all edges
first, Prim's algorithm can avoid this by selecting edges on
the fly, making it efficient when implemented with the right
data structures.
Consider the following graph as an example for which we need to find the Minimum Spanning Tree (MST).
The final structure of the MST is as follows and the weight of the edges of the MST
is
(4 + 8 + 1 + 2 + 4 + 2 + 7 + 9) = 37.
WAYS TO IMPLEMENT PRIMS
ALGORITHM
1.The simplest approach uses an adjacency
matrix and an array, with a time complexity of
O(V²).

2. A more efficient method uses an adjacency


list with a priority queue , achieving O(E log
V) complexity.

3. For dense graphs, a Fibonacci heap can be


used for further optimization, reducing the
time complexity to O(E + V log V).
IMPLEMENTATION OF PRIMS ALGORITHM USING
ADJENCY MATRIX
BEGIN

FUNCTION minKey(key[], inMST[], V):


Initialize min = infinity, minIndex = -1
FOR each vertex v from 0 to V-1:
IF vertex v is not in MST AND key[v] < min:
Set min = key[v]
Set minIndex = v
RETURN minIndex
END FUNCTION
FUNCTION primMST(graph[][], V):
// Initialize arrays
Initialize key[] as an array of size V with values set to
infinity
Initialize parent[] as an array of size V with values set to -
1
Initialize inMST[] as an array of size V with values set to
false
// Starting from vertex 0
Set key[0] = 0
// Loop to construct the MST
FOR count from 0 to V-1:
// Pick the vertex u with the minimum key value from
vertices not yet in MST
Set u = minKey(key, inMST, V)

// Mark vertex u as included in MST


Set inMST[u] = true
// Update key values and parent indices for
adjacent vertices of u
FOR each vertex v from 0 to V-1: IF graph[u]
[v] is not 0 AND vertex v is not in MST AND graph[u]
[v] < key[v]:
Set key[v] = graph[u][v]
Set parent[v] = u

// Output the edges and weights of the MST


FOR i from 1 to V-1:
PRINT "Edge" parent[i] " - " i " Weight" graph[i]
[parent[i]]

END FUNCTION

You might also like