diff --git a/docs/Queue/Blocked-queue.png b/docs/Queue/Blocked-queue.png new file mode 100644 index 000000000..e82a642f2 Binary files /dev/null and b/docs/Queue/Blocked-queue.png differ diff --git a/docs/Queue/blocked-queue.md b/docs/Queue/blocked-queue.md new file mode 100644 index 000000000..e58583639 --- /dev/null +++ b/docs/Queue/blocked-queue.md @@ -0,0 +1,343 @@ +--- +id: blocked-queue-in-dsa +title: Blocked Queue Data Structure +sidebar_label: Blocked Queue +sidebar_position: 1 +description: "A blocked queue is a linear data structure that operates on the First In First Out (FIFO) principle but includes mechanisms to block and unblock threads when the queue is empty or full. This is particularly useful in concurrent programming." +tags: [dsa, data-structures, BlockedQueue] +--- + +### Introduction to Blocked Queue + +A **blocked queue** is a linear data structure that follows the First In First Out (FIFO) principle, similar to a regular queue. However, it includes mechanisms to block and unblock threads when the queue is empty or full. This is particularly useful in concurrent programming where multiple threads may need to access the queue simultaneously. + +![alt text](Blocked-queue.png) + +### Blocked Queue Operations + +1. **Enqueue**: Add an element to the back of the queue. +2. **Dequeue**: Remove the element from the front of the queue. +3. **Peek**: Retrieve the element at the front of the queue without removing it. +4. **isEmpty**: Check if the queue is empty. +5. **isFull**: Check if the queue is full. +6. **Size**: Get the number of elements in the queue. + +### Pseudocode + +#### Basic Operations + +1. **Enqueue**: + + ```text + function enqueue(blockedQueue, element): + if isFull(blockedQueue): + blockThread() // Block the thread until space is available + blockedQueue.rear = (blockedQueue.rear + 1) % blockedQueue.size + blockedQueue.elements[blockedQueue.rear] = element + unblockThread() // Unblock any waiting threads + ``` + +2. **Dequeue**: + + ```text + function dequeue(blockedQueue): + if isEmpty(blockedQueue): + blockThread() // Block the thread until an element is available + frontElement = blockedQueue.elements[blockedQueue.front] + blockedQueue.front = (blockedQueue.front + 1) % blockedQueue.size + unblockThread() // Unblock any waiting threads + return frontElement + ``` + +3. **Peek**: + + ```text + function peek(blockedQueue): + if isEmpty(blockedQueue): + return "Queue is empty" + return blockedQueue.elements[blockedQueue.front] + ``` + +4. **isEmpty**: + + ```text + function isEmpty(blockedQueue): + return blockedQueue.front == blockedQueue.rear + ``` + +5. **isFull**: + + ```text + function isFull(blockedQueue): + return (blockedQueue.rear + 1) % blockedQueue.size == blockedQueue.front + ``` + +6. **Size**: + + ```text + function size(blockedQueue): + return (blockedQueue.rear - blockedQueue.front + blockedQueue.size) % blockedQueue.size + ``` + +### Implementation in Python, C++, and Java + +#### Python Implementation + +```python +import threading + +class BlockedQueue: + def __init__(self, size): + self.size = size + self.elements = [None] * size + self.front = 0 + self.rear = 0 + self.lock = threading.Lock() + self.not_empty = threading.Condition(self.lock) + self.not_full = threading.Condition(self.lock) + + def enqueue(self, element): + with self.not_full: + while self.is_full(): + self.not_full.wait() + self.rear = (self.rear + 1) % self.size + self.elements[self.rear] = element + self.not_empty.notify() + + def dequeue(self): + with self.not_empty: + while self.is_empty(): + self.not_empty.wait() + frontElement = self.elements[self.front] + self.front = (self.front + 1) % self.size + self.not_full.notify() + return frontElement + + def peek(self): + with self.lock: + if self.is_empty(): + return "Queue is empty" + return self.elements[self.front] + + def is_empty(self): + return self.front == self.rear + + def is_full(self): + return (self.rear + 1) % self.size == self.front + + def size(self): + return (self.rear - self.front + self.size) % self.size + +# Example usage +bq = BlockedQueue(5) +bq.enqueue(10) +bq.enqueue(20) +print(bq.dequeue()) # Output: 10 +print(bq.peek()) # Output: 20 +print(bq.is_empty()) # Output: False +print(bq.size()) # Output: 1 +``` + +#### C++ Implementation + +```cpp +#include +#include +#include +using namespace std; + +class BlockedQueue { +private: + int *elements; + int front, rear, size; + mutex mtx; + condition_variable not_empty, not_full; + +public: + BlockedQueue(int size) { + this->size = size; + elements = new int[size]; + front = rear = 0; + } + + void enqueue(int element) { + unique_lock lock(mtx); + not_full.wait(lock, [this] { return !is_full(); }); + rear = (rear + 1) % size; + elements[rear] = element; + not_empty.notify_one(); + } + + int dequeue() { + unique_lock lock(mtx); + not_empty.wait(lock, [this] { return !is_empty(); }); + int frontElement = elements[front]; + front = (front + 1) % size; + not_full.notify_one(); + return frontElement; + } + + int peek() { + lock_guard lock(mtx); + if (is_empty()) { + cout << "Queue is empty" << endl; + return -1; // Indicating empty + } + return elements[front]; + } + + bool is_empty() { + return front == rear; + } + + bool is_full() { + return (rear + 1) % size == front; + } + + int size_of_queue() { + return (rear - front + size) % size; + } + + ~BlockedQueue() { + delete[] elements; + } +}; + +// Example usage +int main() { + BlockedQueue bq(5); + bq.enqueue(10); + bq.enqueue(20); + cout << bq.dequeue() << endl; // Output: 10 + cout << bq.peek() << endl; // Output: 20 + cout << boolalpha << bq.is_empty() << endl; // Output: false + cout << bq.size_of_queue() << endl; // Output: 1 + return 0; +} +``` + +#### Java Implementation + +```java +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +public class BlockedQueue { + private int[] elements; + private int front, rear, size; + private final Lock lock = new ReentrantLock(); + private final Condition notEmpty = lock.newCondition(); + private final Condition notFull = lock.newCondition(); + + public BlockedQueue(int size) { + this.size = size; + elements = new int[size]; + front = rear = 0; + } + + public void enqueue(int element) { + lock.lock(); + try { + while (is_full()) { + notFull.await(); + } + rear = (rear + 1) % size; + elements[rear] = element; + notEmpty.signal(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } finally { + lock.unlock(); + } + } + + public int dequeue() { + lock.lock(); + try { + while (is_empty()) { + notEmpty.await(); + } + int frontElement = elements[front]; + front = (front + 1) % size; + notFull.signal(); + return frontElement; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return -1; // Indicating underflow + } finally { + lock.unlock(); + } + } + + public int peek() { + lock.lock(); + try { + if (is_empty()) { + System.out.println("Queue is empty"); + return -1; // Indicating empty + } + return elements[front]; + } finally { + lock.unlock(); + } + } + + public boolean is_empty() { + return front == rear; + } + + public boolean is_full() { + return (rear + 1) % size == front; + } + + public int size_of_queue() { + return (rear - front + size) % size; + } + + public static void main(String[] args) { + BlockedQueue bq = new BlockedQueue(5); + bq.enqueue(10); + bq.enqueue(20); + System.out.println(bq.dequeue()); // Output: 10 + System.out.println(bq.peek()); // Output: 20 + System.out.println(bq.is_empty()); // Output: false + System.out.println(bq.size_of_queue()); // Output: 1 + } +} +``` + +### Complexity +-**Time Complexity**: + + - Enqueue: $O(1)$ + - Dequeue: $O(1)$ + - Peek: $O(1)$ + - isEmpty: $O(1)$ + - isFull: $O(1)$ + - Size: $O(1)$ +-**Space Complexity**: $O(n)$, where $n$ is the number of elements that can be stored in the blocked queue. + +### Example + +Consider a blocked queue with the following operations: + +1. Enqueue 10 +2. Enqueue 20 +3. Dequeue +4. Peek +5. Check if empty +6. Get size + +**Operations**: + +- Enqueue 10: Queue becomes [10, _, _, _, _] +- Enqueue 20: Queue becomes [10, 20, _, _, _] +- Dequeue: Removes 10, Queue becomes [_, 20, _, _, _] +- Peek: Returns 20, Queue remains [_, 20, _, _, _] +- isEmpty: Returns false +- Size: Returns 1 + +### Conclusion + +A blocked queue is an efficient data structure that improves the utilization of space and provides thread safety in concurrent programming scenarios. It is widely used in applications such as producer-consumer problems, task scheduling, and resource management. Understanding and implementing a blocked queue can significantly enhance performance and synchronization in multi-threaded environments. \ No newline at end of file diff --git a/docs/Trees/k-d tree algorithm.md b/docs/Trees/k-d tree algorithm.md new file mode 100644 index 000000000..0d0168bed --- /dev/null +++ b/docs/Trees/k-d tree algorithm.md @@ -0,0 +1,193 @@ +--- +id: kd-tree +title: K-D Tree +sidebar_label: K-D Tree +sidebar_position: 3 +description: "A K-D Tree is a space-partitioning data structure for organizing points in a k-dimensional space." +tags: [Data Structures, K-D Tree, Algorithms] +--- + +# K-D Tree + +## Overview +A **K-D Tree** (short for k-dimensional tree) is a space-partitioning data structure used for organizing points in a k-dimensional space. K-D Trees are useful for various applications such as range searches and nearest neighbor searches. They are a generalization of binary search trees to multiple dimensions. + +## Features +- **Space Partitioning**: Divides the space into nested half-spaces. +- **Efficient Searches**: Supports efficient range and nearest neighbor searches. +- **Balanced Structure**: Can be balanced to ensure efficient operations. + +## Table of Contents +- [How It Works](#how-it-works) +- [Operations](#operations) + - [Insertion](#insertion) + - [Deletion](#deletion) + - [Search](#search) + - [Nearest Neighbor Search](#nearest-neighbor-search) +- [Code Example](#code-example) +- [Applications](#applications) +- [Time Complexity](#time-complexity) + +## How It Works +In a K-D Tree: +- Each node represents a k-dimensional point. +- The tree alternates between the k dimensions at each level of the tree. +- The root node splits the space into two half-spaces based on the first dimension, the children of the root split based on the second dimension, and so on. + +### Construction +1. **Choose Axis**: Select the axis based on the depth of the node modulo k. +2. **Sort Points**: Sort the points based on the chosen axis. +3. **Median Point**: Choose the median point to ensure balanced partitions. +4. **Recursively Construct**: Recursively construct the left and right subtrees using the points before and after the median. + +## Operations + +### Insertion +To insert a new point: +1. Start at the root and choose the axis based on the depth. +2. Compare the point with the current node's point based on the chosen axis. +3. Recursively insert the point into the left or right subtree based on the comparison. + +### Deletion +To delete a point: +1. Find the node containing the point. +2. Replace the node with a suitable candidate from its subtrees to maintain the K-D Tree properties. +3. Recursively adjust the tree to ensure balance. + +### Search +To search for a point: +1. Start at the root and choose the axis based on the depth. +2. Compare the point with the current node's point based on the chosen axis. +3. Recursively search the left or right subtree based on the comparison. + +### Nearest Neighbor Search +To find the nearest neighbor: +1. Start at the root and traverse the tree to find the leaf node closest to the target point. +2. Backtrack and check if there are closer points in the other half-spaces. +3. Use a priority queue to keep track of the closest points found. + +## Code Example + +### Python Example (K-D Tree): + +```python +class Node: + def __init__(self, point, axis, left=None, right=None): + self.point = point + self.axis = axis + self.left = left + self.right = right + +class KDTree: + def __init__(self, points): + self.k = len(points[0]) if points else 0 + self.root = self.build_tree(points, depth=0) + + def build_tree(self, points, depth): + if not points: + return None + + axis = depth % self.k + points.sort(key=lambda x: x[axis]) + median = len(points) // 2 + + return Node( + point=points[median], + axis=axis, + left=self.build_tree(points[:median], depth + 1), + right=self.build_tree(points[median + 1:], depth + 1) + ) + + def insert(self, root, point, depth=0): + if root is None: + return Node(point, depth % self.k) + + axis = root.axis + if point[axis] < root.point[axis]: + root.left = self.insert(root.left, point, depth + 1) + else: + root.right = self.insert(root.right, point, depth + 1) + + return root + + def search(self, root, point, depth=0): + if root is None or root.point == point: + return root + + axis = root.axis + if point[axis] < root.point[axis]: + return self.search(root.left, point, depth + 1) + else: + return self.search(root.right, point, depth + 1) + + def nearest_neighbor(self, root, point, depth=0, best=None): + if root is None: + return best + + axis = root.axis + next_best = None + next_branch = None + + if best is None or self.distance(point, root.point) < self.distance(point, best.point): + next_best = root + else: + next_best = best + + if point[axis] < root.point[axis]: + next_branch = root.left + other_branch = root.right + else: + next_branch = root.right + other_branch = root.left + + next_best = self.nearest_neighbor(next_branch, point, depth + 1, next_best) + + if self.distance(point, next_best.point) > abs(point[axis] - root.point[axis]): + next_best = self.nearest_neighbor(other_branch, point, depth + 1, next_best) + + return next_best + + def distance(self, point1, point2): + return sum((x - y) ** 2 for x, y in zip(point1, point2)) ** 0.5 + +# Example usage: +points = [(2, 3), (5, 4), (9, 6), (4, 7), (8, 1), (7, 2)] +kd_tree = KDTree(points) + +# Insert a new point +kd_tree.insert(kd_tree.root, (3, 6)) + +# Search for a point +result = kd_tree.search(kd_tree.root, (5, 4)) +print("Found:", result.point if result else "Not found") + +# Find the nearest neighbor +nearest = kd_tree.nearest_neighbor(kd_tree.root, (9, 2)) +print("Nearest neighbor:", nearest.point if nearest else "None") +``` + +### Output: +``` +Found: (5, 4) +Nearest neighbor: (8, 1) +``` + +## Applications +- **Databases**: Used for maintaining sorted data in databases where frequent insertions and deletions are required. +- **Memory Management**: Efficiently manage free memory blocks in systems. +- **Auto-completion**: Used in applications that require predictive text suggestions. + +## Time Complexity + +| Operation | Average Time | Worst Case Time | +|----------------------|--------------|-----------------| +| **Search** | O(log n) | O(n) | +| **Insertion** | O(log n) | O(n) | +| **Deletion** | O(log n) | O(n) | +| **Nearest Neighbor** | O(log n) | O(n) | + +> **Note**: The worst-case time complexity arises in unbalanced cases; balanced K-D Trees maintain an average of O(log n) for most operations. + +## Conclusion +K-D Trees are powerful data structures for organizing and searching points in multi-dimensional spaces. They are essential for applications that require efficient spatial searches and nearest neighbor queries. +--- \ No newline at end of file