0% found this document useful (0 votes)
10 views4 pages

Experi Men 12

The document outlines a C program to determine the Minimum Cost Spanning Tree using Prim's Algorithm. It includes the algorithm steps, complexity analysis indicating both time and space complexity as Θ(V²), and mentions the use of an adjacency matrix for graph representation. The program initializes key values, tracks included vertices, and extracts the minimum key vertex to construct the MST.

Uploaded by

ankurmondal039
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)
10 views4 pages

Experi Men 12

The document outlines a C program to determine the Minimum Cost Spanning Tree using Prim's Algorithm. It includes the algorithm steps, complexity analysis indicating both time and space complexity as Θ(V²), and mentions the use of an adjacency matrix for graph representation. The program initializes key values, tracks included vertices, and extracts the minimum key vertex to construct the MST.

Uploaded by

ankurmondal039
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/ 4

Experiment – 12

Write a C program to determine Minimum Cost Spanning Tree by


implementing Prim's Algorithm
Sol: -
12.1 Algorithm
Input:
A weighted connected graph G(V, E) represented by an adjacency matrix or list
Output:
Minimum Cost Spanning Tree (MST)

Algorithm PrimMST(G):
Initialize:
V = number of vertices in G
key[0…V-1] = ∞ // Minimum cost to connect to MST
parent[0…V-1] = NIL // Stores MST structure
mstSet[0…V-1] = false // Track vertices included in MST

key[0] = 0 // Start from the first vertex

For count from 0 to V-1 do:


u = Extract_Min_Key_Vertex(key, mstSet)
mstSet[u] = true // Include u in MST

For each vertex v adjacent to u do:


if weight(u, v) < key[v] and mstSet[v] == false:
key[v] = weight(u, v)
parent[v] = u

Output:
For i = 1 to V-1:
Print (parent[i], i) with cost = key[i]

Function Extract_Min_Key_Vertex(key, mstSet):


min = ∞
min_index = -1

For v = 0 to V-1:
if mstSet[v] == false and key[v] < min:
min = key[v]
min_index = v

return min_index
12.2 Algorithmic paradigm: - Greedy algorithm
12.3 Complexity analysis: -
In this implementation, the graph is represented using an adjacency matrix, and at every step,
the algorithm selects the vertex with the minimum key value using a linear search. The
number of vertices is denoted by V, and the number of edges by E.
Now, for each of the V vertices, the algorithm calls the minKey() function once, which scans
all V vertices to find the one with the minimum key not yet included in the MST. This results
in Θ(V²) time for selecting minimum key vertices across all iterations.
Additionally, for each selected vertex, the algorithm goes through all V vertices again to
check for adjacent vertices and update their keys if a better edge is found. Since this inner
update loop is nested within the outer loop that runs V times, it also leads to Θ(V²) time
overall.
So, combining both the selection of the minimum key vertex and the key updates for adjacent
vertices, the overall time complexity is Θ(V²).
As for space complexity, the adjacency matrix itself consumes Θ(V²) space because it stores
weights for all possible pairs of vertices, regardless of whether an edge exists. The key,
parent, and mstSet arrays each use Θ(V) space. Therefore, the total space complexity is
dominated by the adjacency matrix and is Θ(V²).
Hence, both time and space complexity of this implementation of Prim's Algorithm are
Θ(V²).
12.4 Source Code: -
12.5 Output

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy