Complete the code to initialize the key values for all vertices in Prim's algorithm.
for (int i = 0; i < V; i++) { key[i] = [1]; mstSet[i] = false; }
We initialize all key values to a very large number (infinity) to ensure any edge weight will be smaller and can update the key.
Complete the code to find the vertex with the minimum key value not yet included in MST.
int minKey(int key[], bool mstSet[]) {
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++) {
if (mstSet[v] == false && key[v] < [1]) {
min = key[v];
min_index = v;
}
}
return min_index;
}The variable 'min' holds the smallest key found so far. We compare key[v] with min to find the minimum.
Fix the error in updating the key and parent arrays after including a vertex in MST.
for (int v = 0; v < V; v++) { if (graph[u][v] && mstSet[v] == false && graph[u][v] < [1]) { parent[v] = u; key[v] = graph[u][v]; } }
We compare the edge weight graph[u][v] with the current key[v] to update the minimum edge weight for vertex v.
Fill both blanks to complete the loop that constructs the MST and updates arrays.
for (int count = 0; count < V - 1; count++) { int u = [1](key, mstSet); mstSet[u] = true; for (int v = 0; v < V; v++) { if (graph[u][v] && mstSet[v] == false && graph[u][v] < [2]) { parent[v] = u; key[v] = graph[u][v]; } } }
The function minKey finds the vertex with the minimum key not in MST. The comparison uses key[v] to update minimum edge weights.
Fill all three blanks to complete the function that prints the MST edges and their weights.
void printMST(int parent[], int graph[V][V]) {
printf("Edge \tWeight\n");
for (int i = 1; i < V; i++) {
printf("%d - %d \t%d \n", [1], [2], [3]);
}
}The edge is from parent[i] to i. The weight is graph[parent[i]][i]. The printf arguments must match this order.