
Sponsored
Sponsored
This approach involves treating the points as nodes of a graph and the Manhattan distance between them as the edge weights. The problem translates to finding a Minimum Spanning Tree (MST), which connects all nodes with the minimum edge weight.
Kruskal's algorithm is a popular choice for this kind of problem. It works as follows:
This approach efficiently computes the minimum cost of connecting all points.
The time complexity is dominated by the sorting step and the union-find operations. Sorting takes O(E log E), where E is the number of edges. Union-Find operations are nearly constant time, resulting in an overall complexity of O(E log E). The space complexity is O(n) for the Union-Find structure and edge storage, where n is the number of points.
1#include <stdio.h>
2#include <stdlib.h>
3#include <stdbool.h>
4
5int find(int
This implementation first constructs all the edges with their distances, using a nested loop to calculate the Manhattan distance between each pair of points. Then, it sorts the edges by distance. The Union-Find structure is initialized and used to progressively add edges to the Minimum Spanning Tree as long as they connect previously disconnected components. The function returns the total cost of the spanning tree.
Another approach uses Prim's algorithm to construct the Minimum Spanning Tree (MST). Rather than sorting edges, Prim's method grows the MST one vertex at a time, starting from an arbitrary vertex and adding the minimum cost edge that expands the MST.
The steps involved are:
This approach offers a clear alternative to Kruskal's, particularly well-suited to densely connected systems.
This approach has a time complexity of O(n^2) due to nested loops over nodes, suitable for problems where n is moderately sized. Space complexity is O(n) for extra arrays managing node state and weights.
1import java.util.*;
2
3class Solution {
4 public int minCostConnectPoints(int[][] points) {
5 int n = points.length;
6 Queue<int[]> pq = new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0]));
7 boolean[] inMST = new boolean[n];
8 int[] minDist = new int[n];
9 Arrays.fill(minDist, Integer.MAX_VALUE);
10 minDist[0] = 0;
11 pq.offer(new int[]{0, 0}); // {cost, index}
12
13 int totalCost = 0;
14
15 while (!pq.isEmpty()) {
16 int[] edge = pq.poll();
17 int weight = edge[0], u = edge[1];
18 if (inMST[u]) continue;
19
20 inMST[u] = true;
21 totalCost += weight;
22
23 for (int v = 0; v < n; v++) {
24 if (!inMST[v]) {
25 int dist = Math.abs(points[u][0] - points[v][0]) + Math.abs(points[u][1] - points[v][1]);
26 if (dist < minDist[v]) {
27 minDist[v] = dist;
28 pq.offer(new int[]{dist, v});
29 }
30 }
31 }
32 }
33
34 return totalCost;
35 }
36}The Java version adopts Prim's algorithm using a priority queue, comparable to the C++ implementation. It steadily incorporates nodes with the least effort and tracks vertices through an MST completion status array. Upon successfully integrating a node, connecting edges are reconsidered, ensuring that expansions into the MST remain as cost-effective as possible, with an increasing total cost accumulating.