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 select the next edge to add?
We select 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 (or min-heap) is used to quickly find and extract the edge with the smallest weight connecting to the MST.
Click to reveal answer
intermediate
What is the time complexity of Prim's Algorithm using a priority queue?
The time complexity is O(E log V), where E is the number of edges and V is the number of vertices, because each edge is processed and the priority queue operations take logarithmic time.
Click to reveal answer
What does Prim's Algorithm start with?
AAll edges
BThe vertex with the highest degree
CAny single vertex
DThe edge with the largest weight
✗ Incorrect
Prim's Algorithm starts from any single vertex and grows the MST from there.
Which data structure helps Prim's Algorithm pick the smallest edge efficiently?
AStack
BQueue
CLinked List
DPriority Queue
✗ Incorrect
A priority queue (min-heap) helps select the smallest edge quickly.
What condition must an edge satisfy to be added to the MST in Prim's Algorithm?
AConnects a vertex in MST to a vertex outside MST
BConnects two vertices already in MST
CHas the largest weight
DIs the first edge in the graph
✗ Incorrect
Edges must connect the MST to a new vertex outside it.
What happens if the graph is not connected when running Prim's Algorithm?
AAlgorithm finds MST for all components
BAlgorithm fails to include all vertices
CAlgorithm runs infinitely
DAlgorithm ignores disconnected vertices
✗ Incorrect
Prim's Algorithm only works on connected graphs; otherwise, some vertices remain unreachable.
What is the main goal of Prim's Algorithm?
AFind the minimum spanning tree
BFind the shortest path between two vertices
CFind the maximum spanning tree
DSort all edges by weight
✗ Incorrect
Prim's Algorithm finds the minimum spanning tree of a graph.
Explain step-by-step how Prim's Algorithm builds a Minimum Spanning Tree.
Think about growing the tree one edge at a time.
You got /4 concepts.
Describe why a priority queue is useful in Prim's Algorithm and how it affects performance.
Consider how to quickly find the next best edge.
You got /4 concepts.
Practice
(1/5)
1. What is the main goal of Prim's algorithm in graph theory?
easy
A. To find the minimum spanning tree connecting all vertices with the least total edge weight
B. To find the shortest path between two vertices
C. To detect cycles in a graph
D. To sort vertices based on their degree
Solution
Step 1: Understand Prim's algorithm purpose
Prim's algorithm builds a tree connecting all vertices with minimum total edge weight.
Step 2: Compare with other graph algorithms
Shortest path and cycle detection are different problems; Prim's focuses on spanning tree.
Final Answer:
To find the minimum spanning tree connecting all vertices with the least total edge weight -> Option A
Hint: Add chosen edge weights stepwise to get MST total [OK]
Common Mistakes:
Forgetting to add the first vertex key (0) to total
Adding all edges instead of MST edges
Mixing up vertex indices
4. The following TypeScript code for Prim's algorithm has a bug. What is the error?
function primMST(graph: number[][]): number {
const n = graph.length;
const key = new Array(n).fill(Infinity);
const mstSet = new Array(n).fill(false);
key[0] = 0;
let totalWeight = 0;
for (let count = 0; count <= n - 1; count++) {
let min = Infinity;
let u = -1;
for (let v = 0; v < n; v++) {
if (!mstSet[v] && key[v] < min) {
min = key[v];
u = v;
}
}
mstSet[u] = true;
totalWeight += min;
for (let v = 0; v < n; v++) {
if (graph[u][v] && !mstSet[v] && graph[u][v] < key[v]) {
key[v] = graph[u][v];
}
}
}
return totalWeight;
}
medium
A. The inner loop condition should be v <= n
B. The key array is not initialized correctly
C. The mstSet array should be initialized with true values
D. The outer loop runs one extra time causing an invalid vertex selection
Solution
Step 1: Analyze outer loop range
The loop runs from 0 to n inclusive (<= n - 1), which means n iterations instead of n-1.
Step 2: Understand impact of extra iteration
Extra iteration causes selection of u = -1 (no valid vertex), leading to errors or invalid updates.
Final Answer:
The outer loop runs one extra time causing an invalid vertex selection -> Option D
Quick Check:
Loop count must be n-1, not n [OK]
Hint: Outer loop should run n-1 times, not n [OK]
Common Mistakes:
Using <= instead of < in loop condition
Initializing mstSet incorrectly
Changing inner loop bounds wrongly
5. Given a weighted undirected graph represented by the adjacency matrix below, apply Prim's algorithm starting from vertex 0. Which edges are included in the MST?