A minimum spanning tree connects every node using the cheapest possible total edge weight. Two greedy algorithms find it: Kruskal (sort all edges, add each one that does not create a cycle) and Prim (grow one tree, always adding the cheapest edge leaving it).
Ticket kiosks stand at the integer grid points in points (each [x, y]) in a town where cable can only run along the streets, so a cable between two kiosks costs their Manhattan distance |x1 - x2| + |y1 - y2|. Write min_cost_connect_points(points) that returns the cheapest total cost of cables joining every kiosk into one network with exactly one route between any two kiosks.
Examples
Input: points = [[0, 0], [4, 1], [1, 3], [6, 5], [2, -2]]
Output: 19
Input: points = [[-3, 2], [5, 4], [1, -6]]
Output: 22
Explanation: cables of cost 10 and 12 from [-3, 2]; the direct [5, 4] to [1, -6] link would cost 14
Constraints
- Target: O(n^2 log n) time (there are O(n^2) candidate edges)
Goals
- Recognise a minimum spanning tree problem from the phrase 'connect everything as cheaply as possible'
- Implement Kruskal's algorithm (sort edges, union-find) or Prim's algorithm (heap of frontier edges)
- Generate the implicit complete graph from a list of points