Program to group a set of points into k different groups in Python

Suppose we have a list of points and a number k. The points are in the form (x, y) representing Cartesian coordinates. We can group any two points p1 and p2 if the Euclidean distance between them is ≤ k. We have to find the total number of disjoint groups.

So, if the input is like points = [[2, 2],[3, 3],[4, 4],[11, 11],[12, 12]], k = 2, then the output will be 2, as it can make two groups: ([2,2],[3,3],[4,4]) and ([11,11],[12,12]).

Algorithm

To solve this problem, we'll use a graph-based approach with Depth-First Search (DFS):

  1. Build adjacency graph: Connect points within distance k
  2. Use DFS: Find all connected components
  3. Count components: Each component represents a group

Implementation

from collections import defaultdict

class Solution:
    def solve(self, points, k):
        adj = defaultdict(list)
        
        n = len(points)
        # Build adjacency list by checking all pairs
        for j in range(n):
            for i in range(j):
                x1, y1 = points[i]
                x2, y2 = points[j]
                # Check if Euclidean distance <= k
                if (x1 - x2) ** 2 + (y1 - y2) ** 2 <= k ** 2:
                    adj[i].append(j)
                    adj[j].append(i)
        
        seen = set()
        
        def dfs(i):
            if i in seen:
                return
            seen.add(i)
            for neighbor in adj[i]:
                dfs(neighbor)
        
        # Count connected components
        groups = 0
        for i in range(n):
            if i not in seen:
                groups += 1
                dfs(i)
        
        return groups

# Test the solution
ob = Solution()
points = [
    [2, 2],
    [3, 3],
    [4, 4],
    [11, 11],
    [12, 12]
]
k = 2
print(ob.solve(points, k))
2

How It Works

The algorithm works in three main steps:

  1. Graph Construction: For each pair of points, calculate the Euclidean distance. If distance ≤ k, add an edge between them in the adjacency list.
  2. DFS Traversal: Use depth-first search to explore all connected points starting from unvisited points. Each DFS call discovers one complete group.
  3. Group Counting: The number of times we initiate DFS equals the number of disjoint groups.

Example Walkthrough

For points = [[2,2], [3,3], [4,4], [11,11], [12,12]] and k = 2:

  • Distance from [2,2] to [3,3] = ?2 ? 1.41 ≤ 2 ?
  • Distance from [3,3] to [4,4] = ?2 ? 1.41 ≤ 2 ?
  • Distance from [11,11] to [12,12] = ?2 ? 1.41 ≤ 2 ?
  • Distance from [4,4] to [11,11] = ?98 ? 9.9 > 2 ?

This creates two connected components: {0,1,2} and {3,4}, resulting in 2 groups.

Conclusion

This solution uses graph theory to group points based on distance constraints. The DFS approach efficiently finds connected components, giving us the number of disjoint groups in O(n²) time complexity.

Updated on: 2026-03-25T11:10:51+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements