0
0
DSA Typescriptprogramming~20 mins

Minimum Spanning Tree Prim's Algorithm in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prim's MST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Prim's Algorithm on a Small Graph
What is the printed state of the minimum spanning tree edges after running Prim's algorithm starting from node 0 on this graph?
DSA Typescript
const graph = [
  [0, 2, 0, 6, 0],
  [2, 0, 3, 8, 5],
  [0, 3, 0, 0, 7],
  [6, 8, 0, 0, 9],
  [0, 5, 7, 9, 0]
];

function primMST(graph: number[][]): string {
  const V = graph.length;
  const key = Array(V).fill(Infinity);
  const parent = Array(V).fill(-1);
  const mstSet = Array(V).fill(false);

  key[0] = 0;

  for (let count = 0; count < V - 1; count++) {
    let min = Infinity;
    let u = -1;
    for (let v = 0; v < V; v++) {
      if (!mstSet[v] && key[v] < min) {
        min = key[v];
        u = v;
      }
    }

    mstSet[u] = true;

    for (let v = 0; v < V; v++) {
      if (graph[u][v] && !mstSet[v] && graph[u][v] < key[v]) {
        parent[v] = u;
        key[v] = graph[u][v];
      }
    }
  }

  let result = '';
  for (let i = 1; i < V; i++) {
    result += `${parent[i]} - ${i} | `;
  }
  return result.trim();
}

console.log(primMST(graph));
A0 - 1 | 1 - 2 | 0 - 3 | 1 - 4 |
B0 - 1 | 0 - 2 | 0 - 3 | 0 - 4 |
C0 - 1 | 1 - 2 | 3 - 4 | 0 - 3 |
D1 - 0 | 2 - 1 | 3 - 0 | 4 - 1 |
Attempts:
2 left
💡 Hint
Trace the algorithm step-by-step, always picking the smallest edge connecting the MST set to a new vertex.
🧠 Conceptual
intermediate
1:00remaining
Understanding Prim's Algorithm Data Structures
Which data structure is primarily used in Prim's algorithm to efficiently select the next edge with the smallest weight?
AStack
BHash Table
CQueue
DPriority Queue (Min-Heap)
Attempts:
2 left
💡 Hint
Think about how to always pick the smallest edge quickly.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Prim's Algorithm Implementation
What error will this code produce when running Prim's algorithm on a graph with disconnected components?
DSA Typescript
function primMST(graph: number[][]): number[] {
  const V = graph.length;
  const key = Array(V).fill(Infinity);
  const parent = Array(V).fill(-1);
  const mstSet = Array(V).fill(false);

  key[0] = 0;

  for (let count = 0; count < V - 1; count++) {
    let min = Infinity;
    let u = -1;
    for (let v = 0; v < V; v++) {
      if (!mstSet[v] && key[v] < min) {
        min = key[v];
        u = v;
      }
    }

    mstSet[u] = true;

    for (let v = 0; v < V; v++) {
      if (graph[u][v] && !mstSet[v] && graph[u][v] < key[v]) {
        parent[v] = u;
        key[v] = graph[u][v];
      }
    }
  }

  return parent;
}
AInfinite loop because u remains -1 when graph is disconnected
BTypeError when comparing Infinity with number
CRuntimeError due to accessing undefined index
DNo error, returns MST for disconnected graph
Attempts:
2 left
💡 Hint
Consider what happens if no vertex is reachable from the current MST set.
Predict Output
advanced
2:00remaining
Prim's Algorithm Output with Edge Weights and Parent Array
What is the final parent array after running Prim's algorithm on this graph starting from node 0?
DSA Typescript
const graph = [
  [0, 1, 4, 0, 0, 0],
  [1, 0, 4, 2, 7, 0],
  [4, 4, 0, 3, 5, 0],
  [0, 2, 3, 0, 4, 6],
  [0, 7, 5, 4, 0, 7],
  [0, 0, 0, 6, 7, 0]
];

function primMST(graph: number[][]): number[] {
  const V = graph.length;
  const key = Array(V).fill(Infinity);
  const parent = Array(V).fill(-1);
  const mstSet = Array(V).fill(false);

  key[0] = 0;

  for (let count = 0; count < V - 1; count++) {
    let min = Infinity;
    let u = -1;
    for (let v = 0; v < V; v++) {
      if (!mstSet[v] && key[v] < min) {
        min = key[v];
        u = v;
      }
    }

    mstSet[u] = true;

    for (let v = 0; v < V; v++) {
      if (graph[u][v] && !mstSet[v] && graph[u][v] < key[v]) {
        parent[v] = u;
        key[v] = graph[u][v];
      }
    }
  }

  return parent;
}

console.log(primMST(graph));
A[-1, 0, 1, 3, 1, 3]
B[-1, 0, 1, 1, 3, 3]
C[-1, 0, 0, 1, 2, 3]
D[-1, 0, 1, 2, 3, 4]
Attempts:
2 left
💡 Hint
Follow the selection of edges with minimum weights step-by-step.
🧠 Conceptual
expert
1:00remaining
Time Complexity of Prim's Algorithm with Different Data Structures
What is the time complexity of Prim's algorithm when implemented using an adjacency matrix and a simple array to find the minimum key?
AO(V + E)
BO(E log V)
CO(V^2)
DO(V log V + E)
Attempts:
2 left
💡 Hint
Consider how many times you scan all vertices to find the minimum key.