Kruskal’s algorithm

Choose which bridges to build between islands so you can reach every island for the lowest total building cost.

Consider bridges from cheapest to most expensive. Build one if it joins two different groups. A disconnected map produces one tree per component.

Sorting takes O(E log E). Here finding a group can take up to V parent links. The result lists the endpoint pairs of the chosen edges.