Connect four villages at the lowest total cost. Step through Kruskal’s algorithm, see the path that makes an edge redundant, then change a weight and try again.
Network
4 vertices · 4 candidate edges
Start with the lightest edge
Each vertex starts in its own group. Which two groups can the cheapest edge connect?
Build a minimum spanning tree for four villages
Each vertex is a village. Each edge weight is the cost of laying fiber between two villages. A village can reach another through an intermediate station. A minimum spanning tree (MST) connects every vertex of a connected, weighted, undirected graph with the smallest possible sum of edge weights.
Start with Four villages and use Next step four times. You can also select a candidate edge in the list to revisit its decision.
| Candidate | What changes on the graph | Total cost |
|---|---|---|
| A–B, weight 1 | A and B join the same component | 1 |
| B–C, weight 2 | C joins the component containing A and B | 3 |
| A–C, weight 3 | The existing route through B would close a cycle, so skip A–C | 3 |
| C–D, weight 4 | D joins and all four villages are connected | 7 |
Pause at step three. The highlighted A → B → C route already connects the endpoints of the dashed A–C edge. Step back and forward once. Both the total cost and the component count stay the same.
Change a weight and predict which edge gets replaced
Open Edit graph and change A C 3 to A C 0. Keep the other three lines and choose Apply and restart. A–C now comes first, followed by A–B. When B–C comes up, the existing route is B → A → C. The tree uses A–C, A–B and C–D, lowering its total cost from 7 to 5. Negative weights also work and are considered in ascending numerical order.
Why is the greedy choice safe?
Kruskal sorts the edges by weight. It accepts an edge when its endpoints belong to different components. Think of the boundary around one of those components. The current edge is among the lightest that cross the boundary. If an optimal tree omits it, adding it forms a cycle. Another edge on that cycle crosses the same boundary and can be removed without increasing the total. Each accepted edge therefore keeps an optimal completion possible. Princeton Algorithms on minimum spanning trees
The visualizer uses union-find to track components and follows the selected edges to show the existing route when it skips a cycle. For a connected graph with V vertices, the tree is complete after V − 1 edges have been selected. Any remaining candidates are marked Not needed.
What happens with equal weights or disconnected vertices?
- Choose Equal weights. Each side of the square costs 2. Any three sides connect all four corners for a total of 6. The visualizer breaks ties by endpoint letters so replays are consistent. Remove the selected A–B edge and run it again to find a different tree with the same cost.
- Choose Disconnected islands. A, B and C form one group, D and E form another, and F stays isolated. After every edge has been checked, the three components each have their own minimum spanning tree. Together these form a minimum spanning forest.
The fiber network minimizes the sum of all installation costs. To explore the distance from one starting village to each destination, try the shortest-path demo. Dijkstra’s algorithm visualizer
