- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 point p1 and p2 if the Euclidean distance between them is <= k, we have to find 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])

To solve this, we will follow these steps −

Define a function dfs() . This will take i

if i is in seen, then

return

insert i into seen

for each nb in adj[i], do

dfs(nb)

From the main method, do the following−

adj := a map

n := size of points

for j in range 0 to n, do

for i in range 0 to j, do

p1 := points[i]

p2 := points[j]

if Euclidean distance between p1 and p2 < k, then

insert j at the end of adj[i]

insert i at the end of adj[j]

seen := a new set

ans := 0

for i in range 0 to n, do

if i not seen, then

ans := ans + 1

dfs(i)

return ans

Let us see the following implementation to get better understanding −

## Example

from collections import defaultdict class Solution: def solve(self, points, k): adj = defaultdict(list) n = len(points) for j in range(n): for i in range(j): x1, y1 = points[i] x2, y2 = points[j] 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 nb in adj[i]: dfs(nb) ans = 0 for i in range(n): if i not in seen: ans += 1 dfs(i) return ans ob = Solution() points = [ [2, 2], [3, 3], [4, 4], [11, 11], [12, 12] ] k = 2 print(ob.solve(points, k))

## Input

[[2, 2],[3, 3],[4, 4],[11, 11],[12, 12]],2

## Output

2

- Related Articles
- Program to reverse linked list by groups of size k in Python
- Python Program to get K length groups with given summation
- Program to find number of friend groups in a set of friends connections in Python
- Python program to convert Set into Tuple and Tuple into Set
- Python program to find the group sum till each K in a list
- Python Program to Group Strings by K length Using Suffix
- Split tuple into groups of n in Python
- Python program to split string into k distinct partitions
- Program to find maximum number of K-sized groups with distinct type items are possible in Python
- Count number of ways to partition a set into k subsets in C++
- Program to find number of sublists that contains exactly k different words in Python
- Program to sort given set of Cartesian points based on polar angles in Python
- Program to find length of longest set of 1s by flipping k bits in Python
- Program to find minimum cost to paint fences with k different colors in Python
- Program to split lists into strictly increasing sublists of size greater than k in Python