Complete the code to initialize the key array with infinite values.
const key: number[] = new Array(V).fill([1]);We use Infinity to represent that initially all vertices have an infinite key value before being updated.
Complete the code to mark the starting vertex as included in MST.
mstSet[[1]] = true;The algorithm starts from vertex 0, so we mark mstSet[0] as true to include it first.
Fix the error in the loop condition to find the vertex with minimum key value not yet included in MST.
for (let v = 0; v < [1]; v++) {
mstSet.length or key.length may work but is less clear.graph.length may cause errors if graph is adjacency matrix with different dimensions.The loop must run from 0 to V (number of vertices) to check all vertices.
Fill both blanks to update key and parent arrays when a better edge is found.
if (graph[u][v] && mstSet[v] === false && graph[u][v] < [1][v]) { [2][v] = u; }
We update key[v] with the new smaller weight and set parent[v] to u to record the MST edge.
Fill all three blanks to complete the key update inside the loop.
key[v] = graph[[1]][[2]]; mstSet[[3]] = true;
We update key[v] with the weight from u to v, and mark mstSet[u] as true to include vertex u in MST.