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 (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
Why does Prim's Algorithm always produce a Minimum Spanning Tree?
Because it always picks the smallest edge that connects the growing MST to a new vertex, it ensures no cycles and minimal total weight, satisfying MST properties.
Click to reveal answer
What does Prim's Algorithm start with?
AAny single vertex
BAll edges
CThe largest edge
DAn empty graph
✗ Incorrect
Prim's Algorithm starts from any single vertex and grows the MST from there.
Which data structure helps Prim's Algorithm pick the next smallest edge efficiently?
APriority queue (min-heap)
BQueue
CStack
DLinked list
✗ Incorrect
A priority queue or min-heap allows quick access to the smallest edge.
Prim's Algorithm is used on which type of graph?
ADirected graph with negative weights
BUnweighted graph
CConnected weighted undirected graph
DDisconnected graph
✗ Incorrect
Prim's Algorithm requires a connected weighted undirected graph.
What happens if Prim's Algorithm picks an edge that creates a cycle?
AIt continues anyway
BIt skips that edge
CIt removes a vertex
DIt restarts
✗ Incorrect
Prim's Algorithm never adds edges that create cycles; it skips them.
What is the time complexity of Prim's Algorithm using a priority queue?
AO(log V)
BO(V^2)
CO(V + E)
DO(E log V)
✗ Incorrect
Using a priority queue, Prim's Algorithm runs in O(E log V) time.
Explain how Prim's Algorithm builds a Minimum Spanning Tree step-by-step.
Think about growing a tree by adding the cheapest connection each time.
You got /5 concepts.
Describe the role of the priority queue in Prim's Algorithm and why it is important.
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 detect cycles in a graph
B. To find the shortest path between two vertices
C. To find the minimum spanning tree connecting all vertices with the least total edge weight
D. To sort the vertices in topological order
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, cycle detection, and topological sort serve different purposes than Prim's.
Final Answer:
To find the minimum spanning tree connecting all vertices with the least total edge weight -> Option C
Quick Check:
Prim's algorithm = Minimum Spanning Tree [OK]
Hint: Prim's finds minimum spanning tree, not shortest path [OK]
Common Mistakes:
Confusing Prim's with Dijkstra's shortest path
Thinking it detects cycles
Mixing it with topological sorting
2. Which of the following Python data structures is most suitable to efficiently select the next edge with the smallest weight in Prim's algorithm?
easy
A. List
B. Priority Queue (heapq)
C. Dictionary
D. Queue
Solution
Step 1: Identify the need for selecting minimum edge
Prim's algorithm repeatedly selects the edge with the smallest weight.
Step 2: Choose data structure for efficient minimum extraction
Priority Queue (heapq) allows efficient extraction of the smallest element.
Final Answer:
Priority Queue (heapq) -> Option B
Quick Check:
Minimum edge selection = Priority Queue [OK]
Hint: Use heapq for fast minimum edge extraction [OK]
Common Mistakes:
Using list and scanning all edges each time
Using queue which is FIFO, not minimum-based
Using dictionary which doesn't order by weight
3. What is the output of the following Python code implementing Prim's algorithm on the given graph?
graph = {
0: [(1, 2), (3, 6)],
1: [(0, 2), (2, 3), (3, 8), (4, 5)],
2: [(1, 3), (4, 7)],
3: [(0, 6), (1, 8), (4, 9)],
4: [(1, 5), (2, 7), (3, 9)]
}
import heapq
def prim(graph, start=0):
mst = []
visited = set([start])
edges = [(weight, start, to) for to, weight in graph[start]]
heapq.heapify(edges)
while edges:
weight, frm, to = heapq.heappop(edges)
if to not in visited:
visited.add(to)
mst.append((frm, to, weight))
for to_next, w in graph[to]:
if to_next not in visited:
heapq.heappush(edges, (w, to, to_next))
return mst
print(prim(graph))
medium
A. [(0, 1, 2), (1, 2, 3), (1, 4, 5), (0, 3, 6)]
B. [(0, 3, 6), (3, 4, 9), (1, 4, 5), (1, 2, 3)]
C. [(0, 1, 2), (0, 3, 6), (1, 4, 5), (2, 4, 7)]
D. [(1, 2, 3), (2, 4, 7), (0, 3, 6), (1, 4, 5)]
Solution
Step 1: Start from vertex 0 and add edges
Edges from 0: (0-1,2), (0-3,6). Choose smallest: (0-1,2).
Step 2: Add edges from vertex 1 and pick next smallest
From 1: (1-2,3), (1-3,8), (1-4,5). Next smallest edge overall is (1-2,3).
Step 3: Continue adding edges and selecting minimum
Next edges: (1-4,5), (0-3,6). Choose (1-4,5) then (0-3,6).
Edges chosen in ascending weight order form MST [OK]
Hint: Pick edges by smallest weight connecting new vertices [OK]
Common Mistakes:
Choosing edges with higher weight first
Adding edges that create cycles
Not updating visited vertices correctly
4. The following code snippet for Prim's algorithm has a bug. What is the error?
def prim_bug(graph, start=0):
mst = []
visited = set()
edges = [(0, start, start)]
while edges:
weight, frm, to = edges.pop()
if to not in visited:
visited.add(to)
if frm != to:
mst.append((frm, to, weight))
for to_next, w in graph[to]:
if to_next not in visited:
edges.append((w, to, to_next))
return mst
medium
A. The start vertex is never added to visited
B. Visited set is not initialized properly
C. Edges are not added to the list
D. Using list pop() instead of priority queue causes wrong edge selection order
Solution
Step 1: Check how edges are selected
Edges are popped from a list using pop(), which removes the last element, not the smallest weight.
Step 2: Understand Prim's requirement for minimum edge selection
Prim's requires selecting the edge with the smallest weight each time, which needs a priority queue (heap).
Final Answer:
Using list pop() instead of priority queue causes wrong edge selection order -> Option D
Quick Check:
Edge selection order must be by minimum weight [OK]
Hint: Use heapq, not list pop(), to pick minimum edge [OK]
Common Mistakes:
Using list pop() instead of heapq.heappop()
Not initializing visited with start vertex
Appending edges incorrectly
5. Given a weighted undirected graph with vertices 0 to 5 and edges: