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 (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 binary heap and adjacency list?
The time complexity is O(E log V), where E is the number of edges and V is the number of vertices.
Click to reveal answer
What does Prim's Algorithm start with?
AThe vertex with the highest degree
BAny single vertex
CAll vertices disconnected
DAll edges sorted by 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
CPriority queue (min-heap)
DLinked list
✗ Incorrect
A priority queue (min-heap) allows quick access to the smallest edge.
Prim's Algorithm is an example of which type of algorithm?
AGreedy
BDivide and conquer
CDynamic programming
DBacktracking
✗ Incorrect
Prim's Algorithm is greedy because it picks the smallest edge at each step.
What condition must the graph satisfy for Prim's Algorithm to work correctly?
AGraph must be weighted with negative edges
BGraph must be directed
CGraph must have no cycles
DGraph must be connected
✗ Incorrect
The graph must be connected so all vertices can be reached.
What happens if Prim's Algorithm is run on a disconnected graph?
AIt fails to include all vertices
BIt finds MST for the whole graph
CIt finds MST for each connected component separately
DIt creates cycles
✗ Incorrect
Prim's Algorithm cannot reach all vertices in a disconnected graph, so it fails to include them all.
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 cheapest connection.
You got /4 concepts.
Describe the role of the priority queue in Prim's Algorithm and why it improves efficiency.
Consider how to quickly find the minimum edge at each step.
You got /4 concepts.
Practice
(1/5)
1. What is the main goal of Prim's Algorithm in graph theory?
easy
A. To detect cycles in a graph
B. To find the minimum spanning tree connecting all vertices with minimum total edge weight
C. To find the shortest path between two vertices
D. To sort vertices in topological order
Solution
Step 1: Understand Prim's Algorithm purpose
Prim's Algorithm builds a tree that connects all vertices with the smallest possible sum of edge weights.
Step 2: Compare with other graph algorithms
Shortest path algorithms find paths, cycle detection finds loops, and topological sort orders vertices, which are different goals.
Final Answer:
To find the minimum spanning tree connecting all vertices with minimum total edge weight -> Option B
Quick Check:
Prim's Algorithm = Minimum Spanning Tree [OK]
Hint: Prim's builds minimum spanning tree by adding cheapest edges [OK]
Common Mistakes:
Confusing Prim's with shortest path algorithms
Thinking it detects cycles
Mixing it with topological sorting
2. Which Rust data structure is best suited to efficiently pick the next minimum edge in Prim's Algorithm?
easy
A. Vec (Vector)
B. LinkedList
C. HashMap
D. BinaryHeap (Min-Heap)
Solution
Step 1: Identify operation needed in Prim's Algorithm
Prim's needs to repeatedly select the edge with the smallest weight efficiently.
Step 2: Choose data structure for minimum extraction
A BinaryHeap (min-heap) allows quick access to the smallest element, unlike Vec or LinkedList which require scanning.
Final Answer:
BinaryHeap (Min-Heap) -> Option D
Quick Check:
Min-Heap = Efficient minimum edge selection [OK]
Hint: Use min-heap to get smallest edge fast in Prim's [OK]
Common Mistakes:
Using Vec which is slow for minimum extraction
Choosing HashMap which is for key-value, not min extraction
Using LinkedList which is inefficient for this
3. What is the output of the following Rust code snippet implementing Prim's Algorithm on a graph with edges [(0,1,2), (1,2,3), (0,2,1)] starting from vertex 0?
let edges = vec![(0,1,2), (1,2,3), (0,2,1)];
// Prim's MST total weight calculation
// Output the total weight of MST
medium
A. 3
B. 5
C. 4
D. 6
Solution
Step 1: List edges and weights
Edges: (0-1)=2, (1-2)=3, (0-2)=1. Start from vertex 0.
Step 2: Build MST using Prim's Algorithm
Start at 0, pick smallest edge (0-2)=1, then pick next smallest connecting edge (0-1)=2. Total weight = 1 + 2 = 3.
Step 3: Check if all vertices connected
Vertices 0,1,2 connected with edges (0-2) and (0-1). Edge (1-2) with weight 3 is not needed.
Final Answer:
3 -> Option A
Quick Check:
MST weight = 1 + 2 = 3 [OK]
Hint: Pick smallest edges connecting new vertices stepwise [OK]
Common Mistakes:
Adding all edges instead of MST edges
Choosing heavier edges first
Not starting from given start vertex
4. Identify the bug in this Rust snippet implementing Prim's Algorithm:
let mut visited = vec![false; n];
let mut heap = BinaryHeap::new();
heap.push(Reverse((0, 0)));
while let Some(Reverse((cost, u))) = heap.pop() {
if visited[u] { continue; }
visited[u] = true;
for &(v, w) in &graph[u] {
if !visited[v] {
heap.push(Reverse((w, v)));
}
}
}
medium
A. The heap should be a max-heap, not min-heap
B. The heap should store (cost, vertex) as (u, cost) instead
C. The visited check is missing before pushing neighbors
D. The graph adjacency list should store (vertex, weight) not (weight, vertex)
Solution
Step 1: Analyze visited check placement
Currently, neighbors are pushed without checking if visited, causing duplicates in heap.
Step 2: Fix by checking visited before pushing neighbors
Adding a visited check before pushing neighbors avoids unnecessary heap entries and improves efficiency.
Final Answer:
The visited check is missing before pushing neighbors -> Option C
Quick Check:
Check visited before pushing neighbors to heap [OK]
Hint: Check visited before pushing neighbors to avoid duplicates [OK]
Common Mistakes:
Ignoring visited check before pushing neighbors
Confusing min-heap with max-heap
Swapping vertex and weight in tuples
5. Given a weighted undirected graph with vertices 0 to 4 and edges: