Search for a command to run...
Grow a minimum spanning tree from a chosen start node
Graph playground
Edit undirected edges and replay the algorithm from a new start node.
Start the growing tree
Begin at node 0; only edges leaving the visited tree are candidates.
Prim's Algorithm
1function prim(graph, start) {2 const visited = new Set([start]);3 const frontier = edgesLeaving(visited);//start: 0candidates: 2selected: 0totalWeight: 04 const tree = [];5 6 while (frontier.length > 0) {7 const edge = popLightest(frontier);8 if (!visited.has(edge.to)) {9 visited.add(edge.to);10 tree.push(edge);11 frontier.push(...edgesLeaving(visited));12 }13 }14 return tree;15}