Skip to content

Commit

Permalink
Merge pull request #1551 from sarthaxtic/main
Browse files Browse the repository at this point in the history
Added Priority Queue
  • Loading branch information
pankaj-bind authored Nov 4, 2024
2 parents 8a58aae + fc49ec5 commit 71d1e24
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
96 changes: 96 additions & 0 deletions Queue/Priority Queue/Priority Queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <stdio.h>
#include <stdlib.h>

#define MAX 100 // Maximum number of elements in the priority queue

typedef struct {
int data[MAX];
int size;
} PriorityQueue;

// Function to swap elements in the heap
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

// Heapify function for maintaining the min-heap property
void minHeapify(PriorityQueue *pq, int index) {
int smallest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;

if (left < pq->size && pq->data[left] < pq->data[smallest])
smallest = left;

if (right < pq->size && pq->data[right] < pq->data[smallest])
smallest = right;

if (smallest != index) {
swap(&pq->data[index], &pq->data[smallest]);
minHeapify(pq, smallest);
}
}

// Insert element into the priority queue
void enqueue(PriorityQueue *pq, int value) {
if (pq->size >= MAX) {
printf("Priority queue overflow\n");
return;
}

// Insert the new element at the end
int i = pq->size++;
pq->data[i] = value;

// Fix the min-heap property if it's violated
while (i != 0 && pq->data[(i - 1) / 2] > pq->data[i]) {
swap(&pq->data[i], &pq->data[(i - 1) / 2]);
i = (i - 1) / 2;
}
}

// Remove element with highest priority (smallest element in min-heap)
int dequeue(PriorityQueue *pq) {
if (pq->size <= 0) {
printf("Priority queue underflow\n");
return -1;
}
if (pq->size == 1) {
return pq->data[--pq->size];
}

// Store the minimum value, remove it, and fix the heap
int root = pq->data[0];
pq->data[0] = pq->data[--pq->size];
minHeapify(pq, 0);

return root;
}

// Peek at the highest-priority element
int peek(PriorityQueue *pq) {
if (pq->size <= 0) {
printf("Priority queue is empty\n");
return -1;
}
return pq->data[0];
}

// Main function to demonstrate priority queue
int main() {
PriorityQueue pq;
pq.size = 0;

enqueue(&pq, 10);
enqueue(&pq, 20);
enqueue(&pq, 5);
enqueue(&pq, 7);

printf("Highest priority element: %d\n", peek(&pq));
printf("Dequeued element: %d\n", dequeue(&pq));
printf("Highest priority element after dequeue: %d\n", peek(&pq));

return 0;
}
26 changes: 26 additions & 0 deletions Queue/Priority Queue/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Definition

In C, a priority queue is a special type of queue where each element has a priority associated with it. Elements with higher priority are served before elements with lower priority. If two elements have the same priority, they are served based on their order in the queue (depending on whether it's a min-heap or max-heap).

A priority queue can be implemented in various ways, but one of the most efficient implementations is using a binary heap:
1) Max-Heap: In a max-heap, the element with the highest priority (maximum value) is at the root of the heap, making it easy to access and remove.
2) Min-Heap: In a min-heap, the element with the lowest priority (minimum value) is at the root.

# Operations in a Priority Queue
1) Insert (Enqueue): Insert an element into the priority queue while maintaining the heap property.
2) Remove (Dequeue): Remove the element with the highest priority (root element in a max-heap or min-heap).
3) Peek: Access the highest-priority element without removing it.



# Explanation of Code
1) enqueue: Inserts a new element at the end of the heap array and then "bubbles up" to restore the min-heap property.
2) dequeue: Removes the root (smallest) element and replaces it with the last element in the array, then "bubbles down" to maintain the min-heap.
3) peek: Returns the highest-priority element without removing it.

# Complexity Analysis
1) Insertion (enqueue): O(logn) because it may require "bubbling up."
2) Deletion (dequeue): O(logn) due to "bubbling down."
3) Peek: O(1) since the root is always the highest-priority element.

This implementation offers a simple, efficient way to manage a priority queue in C using a min-heap.

0 comments on commit 71d1e24

Please sign in to comment.