Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the key values for all vertices in Prim's algorithm.
DSA Go
for i := 0; i < V; i++ { key[i] = [1] mstSet[i] = false }
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing key values to 0 will cause incorrect minimum edge selection.
Using -1 can cause logic errors since weights are positive.
✗ Incorrect
We initialize key values to the maximum integer value to represent infinity before starting Prim's algorithm.
2fill in blank
mediumComplete the code to pick the vertex with the minimum key value not yet included in MST.
DSA Go
func minKey(key []int, mstSet []bool) int {
min := int(^uint(0) >> 1)
minIndex := -1
for v := 0; v < V; v++ {
if mstSet[v] == false && key[v] < [1] {
min = key[v]
minIndex = v
}
}
return minIndex
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing key[v] with key[v] itself is always false.
Using minIndex instead of min causes logic errors.
✗ Incorrect
We compare key[v] with the current minimum value stored in min to find the smallest key.
3fill in blank
hardFix the error in the condition to update key values for adjacent vertices in Prim's algorithm.
DSA Go
for v := 0; v < V; v++ { if graph[u][v] != 0 && mstSet[v] == false && graph[u][v] < [1] { key[v] = graph[u][v] parent[v] = u } }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with key[u] instead of key[v] causes wrong updates.
Using mstSet[v] in comparison is logically incorrect.
✗ Incorrect
We compare the edge weight graph[u][v] with the current key[v] to update it if smaller.
4fill in blank
hardFill both blanks to complete the loop that constructs the MST result printing parent-child edges.
DSA Go
for i := 1; i < V; i++ { fmt.Printf("%d - %d\n", [1], [2]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Printing key[i] instead of vertex index causes confusion.
Using mstSet[i] in print statement is incorrect.
✗ Incorrect
We print edges from parent[i] to i to show MST connections.
5fill in blank
hardFill all three blanks to complete the main loop of Prim's algorithm updating MST and keys.
DSA Go
for count := 0; count < V-1; count++ { u := [1](key, mstSet) mstSet[u] = [2] for v := 0; v < V; v++ { if graph[u][v] != 0 && mstSet[v] == false && graph[u][v] < [3] { key[v] = graph[u][v] parent[v] = u } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Marking mstSet[u] as false instead of true.
Comparing graph[u][v] with key[u] instead of key[v].
✗ Incorrect
We pick the minimum key vertex u, mark it true in mstSet, and update keys for adjacent vertices.
