Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the MST set for Prim's algorithm.
DSA Javascript
const mstSet = new Array(V).fill([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using true initially, which means all vertices are already included.
Using 0 or null which do not represent boolean inclusion.
✗ Incorrect
The MST set keeps track of vertices included in MST. Initially, none are included, so all are false.
2fill in blank
mediumComplete the code to update the minimum key value for vertex v.
DSA Javascript
if (graph[u][v] && mstSet[v] === false && graph[u][v] < key[[1]]) { key[v] = graph[u][v]; parent[v] = u; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using u instead of v in key array comparison.
Comparing with parent[v] or key[u] which are incorrect.
✗ Incorrect
We compare the new edge weight with the current key value of vertex v to update it.
3fill in blank
hardFix the error in the function that finds the vertex with minimum key value not yet included in MST.
DSA Javascript
function minKey(key, mstSet) {
let min = Infinity;
let minIndex = -1;
for (let v = 0; v < key.length; 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 minIndex which is an index, not a value.
Comparing with Infinity which is always larger.
✗ Incorrect
We compare key[v] with the current minimum value stored in min to find the smallest key.
4fill in blank
hardFill both blanks to complete the loop that constructs the MST result string.
DSA Javascript
let result = ''; for (let i = 1; i < V; i++) { result += parent[i] + ' - ' + [1] + ': ' + [2] + '\n'; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using key[i] which may not represent the actual edge weight.
Using parent[i] twice instead of i for the destination vertex.
✗ Incorrect
We print edges from parent[i] to i with their weights from the graph adjacency matrix.
5fill in blank
hardFill all three blanks to complete the Prim's algorithm main loop updating MST and keys.
DSA Javascript
for (let count = 0; count < V - 1; count++) { let u = minKey(key, mstSet); mstSet[[1]] = true; for (let v = 0; v < V; v++) { if (graph[u][v] && mstSet[v] === false && graph[u][v] < [2]) { parent[v] = u; key[v] = graph[u][v]; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Marking mstSet[v] instead of mstSet[u].
Comparing with mstSet[v] instead of key[v].
✗ Incorrect
We mark vertex u as included in MST, then update key[v] if a smaller edge is found.
