Data Structures and Algorithms: K Closest Points to Origin

beginner
23 min

Data Structures and Algorithms: K Closest Points to Origin

Welcome to our in-depth lesson on finding the K closest points to the origin in Python! This lesson is designed for both beginners and intermediates, so let's dive right in. šŸŽÆ

Understanding the Concept

In this lesson, we will learn how to find the K closest points to the origin (0, 0) in a list of 2D points using Python. This concept is essential for machine learning algorithms, image processing, and data analysis. šŸ“

The Algorithm

The algorithm we will use is called k-nearest neighbors (KNN). It's a simple and popular algorithm in machine learning for classification and regression problems. In our case, we will use it to find the closest points to the origin. šŸ’”

Data Structure: Priority Queue

To efficiently find the K closest points, we will use a data structure called a priority queue. It maintains elements in a way that the highest or lowest element is easily accessible. In our case, we will use a min-heap, where the smallest element is always at the root. šŸ“

Implementing the Solution

Let's implement the KNN algorithm with a min-heap as a priority queue.

python
import heapq def k_closest_points(points, k): # Create an empty min-heap min_heap = [] distances = {} # Iterate through each point for point in points: # Calculate the distance between the point and the origin (0, 0) distance = math.sqrt(point[0]**2 + point[1]**2) # If the point is not in the min-heap or it's farther away than K points, remove it from the min-heap if len(min_heap) < k or distances[tuple(point)] > distance: # Add the point and distance to the min-heap and the distances dictionary heapq.heappush(min_heap, (distance, point)) distances[tuple(point)] = distance # If the min-heap has more than K points, remove the farthest point if len(min_heap) > k: heapq.heappop(min_heap) # Extract and return the K closest points result = [point for _, point in min_heap] return result

Let's take a look at this code:

  1. We initialize an empty min-heap and a dictionary to store the distances.
  2. We iterate through each point in the given list.
  3. For each point, we calculate the distance to the origin and check if it's closer than the Kth point in the min-heap.
  4. If the point is closer, we add it to the min-heap and the distances dictionary.
  5. If the min-heap already has K points or more, we remove the farthest point.
  6. Finally, we extract and return the K closest points.

Practical Example

Let's test our function with a list of 2D points:

python
points = [(1, 3), (2, 4), (5, 1), (3, 2), (6, 8), (9, 0)] k = 3 result = k_closest_points(points, k) print(result)

The output should be: [(1, 3), (2, 4), (3, 2)] - the three closest points to the origin in the given list.

Quiz

Quick Quiz
Question 1 of 1

What is the algorithm used to find the K closest points to the origin in this lesson?

By the end of this lesson, you should have a solid understanding of the k-nearest neighbors algorithm, priority queues, and how to find the K closest points to the origin in Python. Happy coding! šŸ’”šŸŽÆ