Graph TheoryD2Phase 2minimum-spanning-treepriority-queuegreedy

Prim MST

Vertex growth process for MST formation emphasizing candidate edge relaxation.

Easy Explanation

Prim grows a minimum spanning tree from one node by repeatedly taking the cheapest edge to a new node.

Visualizer
O(1)Prim MST
Deterministic frontier growth from a start node, locking the cheapest edge to each new node.

Nodes / Edges

0 / 0

Visited

0

Total Weight

0

Result

Processing

Ready

Press Play or Step to start execution.

Progress

candidates 0, locked 0, components 0

Current Node

n/a

Selected Edges

n/a

No nodes available for visualization.
Prim MST Implementation
Pseudocode + TypeScript
Reference implementation examples are intentionally abstracted from the playback engine so learners can map concepts to code.
pseudocode
function primMst(nodeCount, edges, start):
  inTree <- set with start
  mst <- []
  totalWeight <- 0

  while inTree has not all nodes:
    bestEdge <- null

    for each edge (u, v, w):
      if u inTree and v not inTree:
        consider (u, v, w)
      else if v inTree and u not inTree:
        consider (v, u, w)

      keep the minimum candidate edge by weight

    if bestEdge is null:
      break  // disconnected component finished

    add bestEdge.to to inTree
    append bestEdge to mst
    totalWeight <- totalWeight + bestEdge.weight

  return { mst, totalWeight }
1.00xNo Run