A Minimum Spanning Tree is a subset of edges in a connected, weighted graph that connects all vertices together without any cycles and with the minimum possible total edge weight.
Click to reveal answer
beginner
What is the main idea behind Prim's Algorithm?
Prim's Algorithm builds the MST by starting from any vertex and repeatedly adding the smallest edge that connects a vertex in the MST to a vertex outside it until all vertices are included.
Click to reveal answer
beginner
In Prim's Algorithm, how do we choose the next edge to add?
We choose the edge with the smallest weight that connects a vertex already in the MST to a vertex not yet included.
Click to reveal answer
intermediate
What data structure is commonly used to efficiently select the next edge in Prim's Algorithm?
A priority queue (often implemented as a min-heap) is used to quickly find and extract the edge with the smallest weight.
Click to reveal answer
intermediate
Why does Prim's Algorithm always produce a Minimum Spanning Tree?
Because it always picks the smallest edge connecting the growing MST to a new vertex, ensuring no cycles and minimal total weight by the greedy choice property.
Click to reveal answer
What does Prim's Algorithm start with?
AAny single vertex
BAll edges sorted by weight
CAll vertices at once
DThe edge with the largest weight
✗ Incorrect
Prim's Algorithm starts from any single vertex and grows the MST by adding edges.
Which data structure helps Prim's Algorithm pick the next smallest edge efficiently?
AStack
BQueue
CPriority Queue
DLinked List
✗ Incorrect
A priority queue (min-heap) is used to efficiently select the smallest edge.
Prim's Algorithm is an example of which type of algorithm?
ADynamic Programming
BGreedy
CDivide and Conquer
DBacktracking
✗ Incorrect
Prim's Algorithm uses a greedy approach by always choosing the smallest edge.
What condition must the graph satisfy for Prim's Algorithm to work correctly?
AGraph must be connected
BGraph must have negative weights
CGraph must be directed
DGraph must be disconnected
✗ Incorrect
Prim's Algorithm requires the graph to be connected to reach all vertices.
What does Prim's Algorithm add to the MST at each step?
AA vertex with the largest degree
BA random edge
CAll edges connected to the current vertex
DAn edge with the smallest weight connecting MST to a new vertex
✗ Incorrect
At each step, Prim's Algorithm adds the smallest edge connecting the MST to a new vertex.
Explain how Prim's Algorithm builds a Minimum Spanning Tree step-by-step.
Think about growing the tree one edge at a time by choosing the smallest connecting edge.
You got /4 concepts.
Describe why Prim's Algorithm guarantees the minimum total weight for the spanning tree.
Focus on how choosing the smallest edge at each step leads to overall minimum weight.
You got /4 concepts.
Practice
(1/5)
1. What is the main goal of Prim's algorithm in graph theory?
easy
A. To sort the vertices of a graph
B. To find the minimum spanning tree of a connected weighted graph
C. To detect cycles in a graph
D. To find the shortest path between two nodes
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 B
Quick Check:
Prim's algorithm = Minimum Spanning Tree [OK]
Hint: Prim's always builds a tree with minimum total edge weight [OK]
Common Mistakes:
Confusing Prim's with shortest path algorithms
Thinking it detects cycles
Assuming it sorts vertices
2. Which of the following is the correct way to initialize the key array in Prim's algorithm in C?
easy
A. int key[V]; for(int i=0; i
B. int key[V] = {0};
C. int key[V] = {INT_MAX};
D. int key[V] = NULL;
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; i
Final Answer:
int key[V]; for(int i=0; i -> Option A
Quick Check:
Initialize keys to INT_MAX using loop [OK]
Hint: Use a loop to set all keys to INT_MAX, not zero [OK]
Common Mistakes:
Initializing key array with zeros
Using incorrect array initialization syntax
Assigning NULL to an int array
3. Consider the following adjacency matrix for a graph with 4 vertices:
If Prim's algorithm starts from vertex 0, what is the total weight of the minimum spanning tree?
medium
A. 14
B. 9
C. 11
D. 10
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 = 11
Step 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 C
Quick Check:
Sum edges 2+3+6 = 11 [OK]
Hint: Add smallest edges connecting new vertices stepwise [OK]
Common Mistakes:
Including edges that form cycles
Adding edges with zero weight incorrectly
Starting from wrong vertex
4. The following code snippet is part of Prim's algorithm implementation. Identify the error:
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];
}
}
medium
A. The loop should run from 1 to V instead of 0 to V-1
B. mstSet[u] should be set to false instead of true
C. The key array is not updated correctly inside the inner loop
D. The condition 'graph[u][v]' should be 'graph[u][v] != 0'
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 D
Quick Check:
Explicit non-zero check improves clarity [OK]
Hint: Use explicit '!= 0' for clarity in conditions [OK]
Common Mistakes:
Misunderstanding loop ranges
Incorrectly toggling mstSet flags
Not updating key array properly
5. Given a graph with vertices and edges as below: