Minimum Spanning Tree Prim's Algorithm in DSA C - Time & Space Complexity
We want to understand how the time needed to find a minimum spanning tree using Prim's algorithm changes as the graph grows.
Specifically, how does the number of steps grow when the graph has more nodes and edges?
Analyze the time complexity of the following Prim's algorithm code snippet.
void primMST(int graph[V][V]) {
int parent[V];
int key[V];
bool mstSet[V];
for (int i = 0; i < V; i++) {
key[i] = INT_MAX;
mstSet[i] = false;
}
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
mstSet[u] = true;
for (int v = 0; v < V; v++) {
if (graph[u][v] != 0 && mstSet[v] == false && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}
}
}
This code finds the minimum spanning tree of a graph using Prim's algorithm with an adjacency matrix.
Look at the loops and repeated steps:
- Primary operation: The outer loop runs once for each vertex (V times).
- Nested operation: Inside, the inner loop checks all vertices to update keys (V times per outer loop).
- Dominant operation: The nested inner loop checking all vertices dominates the time.
As the number of vertices (n) grows, the algorithm does more work:
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 checks (10 * 10) |
| 100 | About 10,000 checks (100 * 100) |
| 1000 | About 1,000,000 checks (1000 * 1000) |
Pattern observation: The number of operations grows roughly with the square of the number of vertices.
Time Complexity: O(V2)
This means if the number of vertices doubles, the time to run the algorithm roughly quadruples.
[X] Wrong: "Prim's algorithm always runs in linear time because it just picks edges one by one."
[OK] Correct: The algorithm checks all vertices repeatedly to find the minimum key, which takes more time as the graph grows, especially with an adjacency matrix.
Understanding how Prim's algorithm scales helps you explain your choices clearly and shows you know how to handle graphs efficiently in real problems.
"What if we used a priority queue (min-heap) instead of scanning all vertices to find the minimum key? How would the time complexity change?"
Practice
Solution
Step 1: Understand Prim's algorithm purpose
Prim's algorithm is designed to find a tree that connects all vertices with the smallest total edge weight.Step 2: Compare with other graph problems
Shortest path and cycle detection are different problems; Prim's focuses on minimum spanning tree.Final Answer:
To find the minimum spanning tree of a connected weighted graph -> Option BQuick Check:
Prim's algorithm = Minimum Spanning Tree [OK]
- Confusing Prim's with shortest path algorithms
- Thinking it detects cycles
- Assuming it sorts vertices
Solution
Step 1: Understand key array purpose
The key array stores minimum edge weights to include vertices; initially set to a very large value (infinity).Step 2: Check initialization methods
int key[V]; for(int i=0; iFinal Answer:
int key[V]; for(int i=0; i -> Option AQuick Check:
Initialize keys to INT_MAX using loop [OK]
- Initializing key array with zeros
- Using incorrect array initialization syntax
- Assigning NULL to an int array
int graph[4][4] = {
{0, 2, 0, 6},
{2, 0, 3, 8},
{0, 3, 0, 0},
{6, 8, 0, 0}
};
If Prim's algorithm starts from vertex 0, what is the total weight of the minimum spanning tree?Solution
Step 1: Start from vertex 0 and pick minimum edges
Edges chosen: 0-1 (2), 1-2 (3), 0-3 (6)Step 2: Sum the weights of chosen edges
Total weight = 2 + 3 + 6 = 11Step 3: Re-evaluate chosen edges for MST
Actually, the MST edges are 0-1 (2), 1-2 (3), and 1-3 (8) or 0-3 (6) but 0-3 (6) is better than 1-3 (8). So edges are 0-1 (2), 1-2 (3), 0-3 (6) totaling 11.Step 4: Check if a smaller total weight is possible
Check edges: 0-1 (2), 1-2 (3), 1-3 (8) sum to 13; 0-1 (2), 1-2 (3), 3-4 (not present); so 11 is correct.Final Answer:
11 -> Option CQuick Check:
Sum edges 2+3+6 = 11 [OK]
- Including edges that form cycles
- Adding edges with zero weight incorrectly
- Starting from wrong vertex
for (int count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
mstSet[u] = true;
for (int v = 0; v < V; v++) {
if (graph[u][v] && !mstSet[v] && graph[u][v] < key[v])
key[v] = graph[u][v];
}
}Solution
Step 1: Analyze the condition 'graph[u][v]'
In C, non-zero values are true, zero is false. Using 'graph[u][v]' works but is less clear; better to explicitly check 'graph[u][v] != 0'.Step 2: Check other parts for correctness
Loop range and key update logic are correct; mstSet[u] should be true to mark visited.Final Answer:
The condition 'graph[u][v]' should be 'graph[u][v] != 0' -> Option DQuick Check:
Explicit non-zero check improves clarity [OK]
- Misunderstanding loop ranges
- Incorrectly toggling mstSet flags
- Not updating key array properly
Vertices: 5 Edges: 0 - 1 (2) 0 - 3 (6) 1 - 2 (3) 1 - 3 (8) 1 - 4 (5) 2 - 4 (7) 3 - 4 (9)If Prim's algorithm starts from vertex 0, which edges will be included in the minimum spanning tree?
Solution
Step 1: Start from vertex 0 and pick smallest edges stepwise
Choose 0-1 (2), then from {0,1} choose 1-2 (3), then 1-4 (5), finally 0-3 (6).Step 2: Confirm no cycles and all vertices included
All vertices 0,1,2,3,4 are connected with minimum total weight edges without cycles.Final Answer:
Edges: 0-1, 1-2, 1-4, 0-3 -> Option AQuick Check:
Pick smallest edges connecting new vertices [OK]
- Choosing edges with larger weights first
- Including edges that create cycles
- Missing to connect all vertices
Ready to prove it?
Pass the test and this pattern moves unknown
