From 463a8011f379d3dc78bd24438531eff6577f8d32 Mon Sep 17 00:00:00 2001 From: sarthaxtic <128150513+sarthaxtic@users.noreply.github.com> Date: Fri, 1 Nov 2024 14:07:37 +0530 Subject: [PATCH 1/6] Create Readme.md --- Priority Queue/Readme.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Priority Queue/Readme.md diff --git a/Priority Queue/Readme.md b/Priority Queue/Readme.md new file mode 100644 index 00000000..1138c5a5 --- /dev/null +++ b/Priority Queue/Readme.md @@ -0,0 +1,24 @@ +#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: + 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. + Min-Heap: In a min-heap, the element with the lowest priority (minimum value) is at the root. + +#Operations in a Priority Queue + Insert (Enqueue): Insert an element into the priority queue while maintaining the heap property. + Remove (Dequeue): Remove the element with the highest priority (root element in a max-heap or min-heap). + Peek: Access the highest-priority element without removing it. + +#Explanation of Code + enqueue: Inserts a new element at the end of the heap array and then "bubbles up" to restore the min-heap property. + dequeue: Removes the root (smallest) element and replaces it with the last element in the array, then "bubbles down" to maintain the min-heap. + peek: Returns the highest-priority element without removing it. + +#Complexity Analysis + Insertion (enqueue): O(log⁡n)O(logn) because it may require "bubbling up." + Deletion (dequeue): O(log⁡n)O(logn) due to "bubbling down." + Peek: O(1)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. From 492095c2dfe84b12d95bfca80c7b271dffa20eae Mon Sep 17 00:00:00 2001 From: sarthaxtic <128150513+sarthaxtic@users.noreply.github.com> Date: Fri, 1 Nov 2024 14:11:24 +0530 Subject: [PATCH 2/6] Update Readme.md --- Priority Queue/Readme.md | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/Priority Queue/Readme.md b/Priority Queue/Readme.md index 1138c5a5..8e3891d9 100644 --- a/Priority Queue/Readme.md +++ b/Priority Queue/Readme.md @@ -1,24 +1,26 @@ -#Definition +# 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: - 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. - Min-Heap: In a min-heap, the element with the lowest priority (minimum value) is at the root. +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 - Insert (Enqueue): Insert an element into the priority queue while maintaining the heap property. - Remove (Dequeue): Remove the element with the highest priority (root element in a max-heap or min-heap). - Peek: Access the highest-priority element without removing it. +# 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 - enqueue: Inserts a new element at the end of the heap array and then "bubbles up" to restore the min-heap property. - dequeue: Removes the root (smallest) element and replaces it with the last element in the array, then "bubbles down" to maintain the min-heap. - peek: Returns 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 - Insertion (enqueue): O(log⁡n)O(logn) because it may require "bubbling up." - Deletion (dequeue): O(log⁡n)O(logn) due to "bubbling down." - Peek: O(1)O(1) since the root is always the highest-priority element. +# 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. From 5db126d8d8f35226c4a17a0c6d892a3cc641f106 Mon Sep 17 00:00:00 2001 From: sarthaxtic <128150513+sarthaxtic@users.noreply.github.com> Date: Fri, 1 Nov 2024 14:12:05 +0530 Subject: [PATCH 3/6] Create PriorityQueue.c --- Priority Queue/PriorityQueue.c | 96 ++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Priority Queue/PriorityQueue.c diff --git a/Priority Queue/PriorityQueue.c b/Priority Queue/PriorityQueue.c new file mode 100644 index 00000000..b8d4e19c --- /dev/null +++ b/Priority Queue/PriorityQueue.c @@ -0,0 +1,96 @@ +#include +#include + +#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; +} From dd1836069b0b7f18b0b9113e5d3852043c907864 Mon Sep 17 00:00:00 2001 From: sarthaxtic <128150513+sarthaxtic@users.noreply.github.com> Date: Sat, 2 Nov 2024 18:48:14 +0530 Subject: [PATCH 4/6] Create Priority Queue.c --- Queue/Priority Queue/Priority Queue.c | 96 +++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Queue/Priority Queue/Priority Queue.c diff --git a/Queue/Priority Queue/Priority Queue.c b/Queue/Priority Queue/Priority Queue.c new file mode 100644 index 00000000..b8d4e19c --- /dev/null +++ b/Queue/Priority Queue/Priority Queue.c @@ -0,0 +1,96 @@ +#include +#include + +#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; +} From 26fa4514333f03b7976d3531d1fc0b821605d14d Mon Sep 17 00:00:00 2001 From: sarthaxtic <128150513+sarthaxtic@users.noreply.github.com> Date: Sat, 2 Nov 2024 18:48:43 +0530 Subject: [PATCH 5/6] Create Readme.md --- Queue/Priority Queue/Readme.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Queue/Priority Queue/Readme.md diff --git a/Queue/Priority Queue/Readme.md b/Queue/Priority Queue/Readme.md new file mode 100644 index 00000000..8e3891d9 --- /dev/null +++ b/Queue/Priority Queue/Readme.md @@ -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. From fc49ec5ece1aa361fd119f00a00e9e2f2b7b7199 Mon Sep 17 00:00:00 2001 From: sarthaxtic <128150513+sarthaxtic@users.noreply.github.com> Date: Sat, 2 Nov 2024 18:49:20 +0530 Subject: [PATCH 6/6] Delete Priority Queue directory --- Priority Queue/PriorityQueue.c | 96 ---------------------------------- Priority Queue/Readme.md | 26 --------- 2 files changed, 122 deletions(-) delete mode 100644 Priority Queue/PriorityQueue.c delete mode 100644 Priority Queue/Readme.md diff --git a/Priority Queue/PriorityQueue.c b/Priority Queue/PriorityQueue.c deleted file mode 100644 index b8d4e19c..00000000 --- a/Priority Queue/PriorityQueue.c +++ /dev/null @@ -1,96 +0,0 @@ -#include -#include - -#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; -} diff --git a/Priority Queue/Readme.md b/Priority Queue/Readme.md deleted file mode 100644 index 8e3891d9..00000000 --- a/Priority Queue/Readme.md +++ /dev/null @@ -1,26 +0,0 @@ -# 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.