AlgoGenesis / C

AlgoGenesis is a centralized open-source platform dedicated to providing optimized and well-documented algorithm implementations in C. Perfect for both beginners and advanced users, this repository serves as a comprehensive learning resource for solving algorithmic challenges.
MIT License
88 stars 306 forks source link

[NEW ALGORITHM] priority queue #341

Closed ABHI-SHEK-001 closed 1 month ago

ABHI-SHEK-001 commented 1 month ago

A priority queue is an abstract data type that operates similarly to a regular queue but with an added feature: each element in a priority queue has a priority associated with it. Elements are dequeued not in the order they were enqueued but rather based on their priority. This makes priority queues particularly useful in scenarios where certain tasks need to be prioritized over others.

Basic Concepts

In a standard queue, elements are processed in a First-In-First-Out (FIFO) manner, meaning the first element added is the first one to be removed. In contrast, a priority queue processes elements based on their priority levels. The element with the highest priority is served before others, regardless of their order of entry. In some implementations, higher numerical values may indicate higher priority, while in others, it may be the opposite, where lower numerical values denote higher priority.

Implementations

Priority queues can be implemented in several ways, each with its own advantages and drawbacks:

  1. Array-Based Implementation: In an unsorted array, elements can be added in constant time, O(1), but removing the element with the highest priority requires O(n) time to find it. In a sorted array, insertion takes O(n) time due to the need to maintain order, but removal is O(1) since the highest priority element is always at one end.

  2. Linked List Implementation: A priority queue can also be implemented using a linked list. In an unsorted linked list, insertion is O(1), while removal is O(n). In a sorted linked list, both insertion and removal can be made O(n).

  3. Heap-Based Implementation: The most efficient and commonly used method is the binary heap. A binary heap is a complete binary tree that satisfies the heap property, where each parent node is prioritized over its child nodes. In a min-heap, the parent node has a value less than or equal to that of its children, while in a max-heap, the opposite is true. This structure allows for efficient insertion and deletion operations, both of which can be performed in O(log n) time, making heaps the preferred choice for implementing priority queues.

Applications

Priority queues are widely used in various applications, including:

Conclusion

In summary, a priority queue is a versatile and efficient data structure that extends the basic queue concept by associating priorities with its elements. Its various implementations, particularly the heap-based approach, offer optimized performance for both insertion and deletion operations. With numerous practical applications across computing and data management, priority queues are an essential tool in both theoretical and applied computer science.

Checklist:

Contributor in GSSoC-ext Want to work on it

github-actions[bot] commented 1 month ago

👋 Thank you for raising an issue! We appreciate your effort in helping us improve. Our team will review it shortly. Stay tuned!