0
0
DSA Typescriptprogramming~10 mins

Minimum Spanning Tree Prim's Algorithm in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the key array with infinite values.

DSA Typescript
const key: number[] = new Array(V).fill([1]);
Drag options to blanks, or click blank then click option'
A0
B-1
CInfinity
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of Infinity causes incorrect minimum edge selection.
Using null or -1 can cause runtime errors when comparing weights.
2fill in blank
medium

Complete the code to mark the starting vertex as included in MST.

DSA Typescript
mstSet[[1]] = true;
Drag options to blanks, or click blank then click option'
AV - 1
B1
C-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from vertex 1 or -1 causes index errors or skips the first vertex.
Using V - 1 starts from the last vertex, which is not standard.
3fill in blank
hard

Fix the error in the loop condition to find the vertex with minimum key value not yet included in MST.

DSA Typescript
for (let v = 0; v < [1]; v++) {
Drag options to blanks, or click blank then click option'
Akey.length
BV
CmstSet.length
Dgraph.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using mstSet.length or key.length may work but is less clear.
Using graph.length may cause errors if graph is adjacency matrix with different dimensions.
4fill in blank
hard

Fill both blanks to update key and parent arrays when a better edge is found.

DSA Typescript
if (graph[u][v] && mstSet[v] === false && graph[u][v] < [1][v]) {
  [2][v] = u;
}
Drag options to blanks, or click blank then click option'
Akey
Bparent
CmstSet
Dgraph
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing key and parent arrays causes wrong MST construction.
Updating mstSet or graph instead causes logic errors.
5fill in blank
hard

Fill all three blanks to complete the key update inside the loop.

DSA Typescript
key[v] = graph[[1]][[2]];
mstSet[[3]] = true;
Drag options to blanks, or click blank then click option'
Au
Bv
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping u and v causes wrong edge weight assignment.
Marking mstSet[v] instead of mstSet[u] causes incorrect MST inclusion.