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 in the MST.
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 MST to a new vertex, it ensures no cycles and minimal total weight, following a greedy approach that leads to an optimal MST.
Click to reveal answer
What does Prim's Algorithm start with?
AAny single vertex
BThe edge with the smallest weight
CAll vertices at once
DThe vertex with the highest degree
✗ Incorrect
Prim's Algorithm begins with any single vertex and grows the MST from there.
Which data structure helps Prim's Algorithm pick the next edge efficiently?
AStack
BPriority Queue
CLinked List
DQueue
✗ Incorrect
A priority queue (min-heap) is used to quickly select the smallest edge.
Prim's Algorithm is an example of which type of approach?
ABacktracking
BDynamic Programming
CDivide and Conquer
DGreedy
✗ Incorrect
Prim's Algorithm uses a greedy approach by always choosing the smallest edge available.
What condition must the graph satisfy for Prim's Algorithm to work correctly?
AGraph must be connected
BGraph must be directed
CGraph must have no cycles
DGraph must be unweighted
✗ Incorrect
The graph must be connected so that all vertices can be reached and included in the MST.
What happens if Prim's Algorithm picks an edge that creates a cycle?
AIt continues anyway
BIt removes a vertex
CIt skips that edge
DIt restarts the algorithm
✗ Incorrect
Prim's Algorithm never adds edges that create cycles; it skips them.
Explain step-by-step how Prim's Algorithm builds a Minimum Spanning Tree.
Think about growing the tree one edge at a time by choosing the smallest connecting edge.
You got /5 concepts.
Describe why Prim's Algorithm guarantees the minimum total weight for the spanning tree.
Consider how always picking the smallest edge leads to the best overall solution.
You got /5 concepts.
Practice
(1/5)
1. What is the main goal of Prim's Algorithm in graph theory?
easy
A. To find the shortest path between two nodes
B. To detect cycles in a graph
C. To find the minimum spanning tree of a connected weighted graph
D. To sort the vertices of a graph
Solution
Step 1: Understand Prim's Algorithm purpose
Prim's Algorithm is designed to find a tree that connects all vertices with the minimum total edge weight.
Step 2: Differentiate from other graph algorithms
It is not used for shortest path, cycle detection, or sorting vertices, which are different problems.
Final Answer:
To find the minimum spanning tree of a connected weighted graph -> Option C
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 nodes
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]; for(int i=0; i
D. int key[V]; for(int i=0; i
Solution
Step 1: Understand key array role
The key array stores minimum edge weights to include each vertex; initially, all are set to a very large value (infinity).
Step 2: Check initialization options
Setting key[i] = INT_MAX correctly represents infinity; other options set to 0 or -1 are incorrect for this purpose.
Final Answer:
int key[V]; for(int i=0; i -> Option A
Quick Check:
Initialize keys with INT_MAX for minimum edge tracking [OK]
Hint: Initialize keys with INT_MAX to represent infinity [OK]
Common Mistakes:
Initializing keys with 0 instead of INT_MAX
Using negative values incorrectly
Not initializing keys at all
3. Consider the following graph with 4 vertices and edges with weights:
What is the total weight of the minimum spanning tree found by Prim's Algorithm starting from vertex 0?
medium
A. 21
B. 19
C. 20
D. 25
Solution
Step 1: Start from vertex 0 and pick minimum edges
Edges from 0: (0-3)=5, (0-2)=6, (0-1)=10. Pick (0-3)=5 first.
Step 2: Add next minimum edge connecting new vertices
From vertices {0,3}, edges are (3-2)=4, (3-1)=15, (0-2)=6, (0-1)=10. Pick (3-2)=4 next.
Step 3: Add last vertex with minimum edge
From {0,3,2}, edges to 1 are (0-1)=10 and (3-1)=15. Pick (0-1)=10.
Step 4: Calculate total weight
Total = 5 + 4 + 10 = 19.
Final Answer:
19 -> Option B
Quick Check:
Sum of chosen edges = 19 [OK]
Hint: Pick smallest edge connecting new vertex each step [OK]
Common Mistakes:
Choosing edges with larger weights first
Not updating the set of included vertices
Adding edges that form cycles
4. The following C++ code snippet is intended to implement Prim's Algorithm but has a bug. What is the error?
const int V = 4;
int key[V] = {INT_MAX, INT_MAX, INT_MAX, INT_MAX};
bool mstSet[V] = {false, false, false, false};
key[0] = 0;
for (int count = 0; count < V - 1; count++) {
int u = -1;
for (int v = 0; v < V; v++) {
if (!mstSet[v] && (u == -1 || key[v] < key[u]))
u = v;
}
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 condition 'graph[u][v]' should check for non-zero edges
B. The loop should run from 0 to V instead of V - 1
C. The key array is not initialized with INT_MAX
D. The variable 'u' is initialized incorrectly inside the loop
Solution
Step 1: Analyze the neighbor update condition
The condition if(graph[u][v] && !mstSet[v] && graph[u][v] < key[v]) uses graph[u][v] as a boolean check (true if non-zero).
Step 2: Identify the bug
This implicit check assumes positive weights and treats 0 as no edge. Standard implementations explicitly use graph[u][v] != 0 to clearly indicate checking for edge existence (non-zero weight).
Step 3: Rule out other options
A: 'u' is correctly initialized to -1 inside each iteration. B: V-1 iterations build the tree correctly. C: key array is initialized with INT_MAX.
Final Answer:
The condition 'graph[u][v]' should check for non-zero edges -> Option A
Quick Check:
Edge check: graph[u][v] != 0 [OK]
Hint: Check edge existence condition carefully for zero weights [OK]