Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the key array with maximum values.
DSA C#
int[] key = new int[V]; for (int i = 0; i < V; i++) { key[i] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of int.MaxValue causes incorrect minimum edge selection.
Using negative values can cause logic errors.
✗ Incorrect
We initialize the key array with int.MaxValue to represent infinite distance before updating with actual edge weights.
2fill in blank
mediumComplete the code to select the vertex with the minimum key value not yet included in MST.
DSA C#
int minKey = int.MaxValue, minIndex = -1; for (int v = 0; v < V; v++) { if (!mstSet[v] && key[v] < [1]) { minKey = key[v]; minIndex = v; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with key[v] instead of minKey causes wrong vertex selection.
Using minIndex in comparison leads to errors.
✗ Incorrect
We compare key[v] with minKey to find the smallest key value among vertices not yet in MST.
3fill in blank
hardFix the error in updating the key and parent arrays for adjacent vertices.
DSA C#
for (int v = 0; v < V; v++) { if (graph[u, v] != 0 && !mstSet[v] && graph[u, v] < [1]) { parent[v] = u; key[v] = graph[u, v]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with parent[v] instead of key[v] causes logic errors.
Comparing with graph[u, v] itself is incorrect.
✗ Incorrect
We compare the edge weight graph[u, v] with the current key[v] to update the minimum edge weight.
4fill in blank
hardFill both blanks to complete the loop that builds the MST by repeating for all vertices.
DSA C#
for (int count = 0; count < [1] - 1; count++) { int u = [2](key, mstSet); mstSet[u] = true; // Update key and parent arrays }
Attempts:
3 left
💡 Hint
Common Mistakes
Using V - 1 in loop condition causes off-by-one error.
Using MinKey instead of MinKeyIndex causes type errors.
✗ Incorrect
We loop V-1 times to include all vertices except the first, and select the vertex with minimum key using MinKeyIndex function.
5fill in blank
hardFill all three blanks to print the constructed MST edges and their weights.
DSA C#
Console.WriteLine("Edge \tWeight"); for (int i = 1; i < [1]; i++) { Console.WriteLine($"{parent[i]} - [2] \t[3]", graph[i, parent[i]]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Looping from 0 causes printing invalid edges.
Using parent[i] in place of i in string interpolation causes wrong output.
✗ Incorrect
We loop from 1 to V-1 to print edges from parent to i with their weights from graph matrix.
