Search for a command to run...
Connect every node with the lightest possible edge set
Graph playground
Edit undirected edges and replay the algorithm from a new start node.
Sort edges by weight
Kruskal starts with the cheapest edge and grows a forest without creating cycles.
Kruskal's Algorithm
1function kruskal(edges, nodeCount) {2 edges.sort((a, b) => a.weight - b.weight);3 const dsu = new UnionFind(nodeCount);//candidates: 9selected: 0totalWeight: 04 const tree = [];5 6 for (const edge of edges) {7 if (dsu.find(edge.from) !== dsu.find(edge.to)) {8 dsu.union(edge.from, edge.to);9 tree.push(edge);10 }11 }12 return tree;13}