Experi Men 12
Experi Men 12
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
Output:
For i = 1 to V-1:
Print (parent[i], i) with cost = key[i]
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