From 39ed0f3e85deb55d68897f41e20d998eb8963246 Mon Sep 17 00:00:00 2001 From: Mohammed Ahmed Majid <109688855+PilotAxis@users.noreply.github.com> Date: Sun, 19 May 2024 22:59:32 +0530 Subject: [PATCH 1/5] Create Queues.md Added Introduction, Real Life Examples, Operations. --- contrib/ds-algorithms/Queues.md | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 contrib/ds-algorithms/Queues.md diff --git a/contrib/ds-algorithms/Queues.md b/contrib/ds-algorithms/Queues.md new file mode 100644 index 00000000..918c9343 --- /dev/null +++ b/contrib/ds-algorithms/Queues.md @@ -0,0 +1,47 @@ +# Introduction to Queues + +A queue is a linear data structure where elements are added at the back (enqueue) and removed from the front (dequeue). Imagine a line at a coffee shop, the first in line (front) gets served first, and new customers join at the back. This FIFO approach ensures order and fairness in processing elements. + +Queues offer efficient implementations for various scenarios. They are often used in: +- Task Scheduling - Operating systems utilize queues to manage processes waiting for CPU time. +- Breadth-first search algorithms - Traversing a tree or graph involves exploring neighbouring nodes level by level, often achieved using a queue. +- Message passing - Communication protocols leverage queues to buffer messages between applications for reliable delivery. + +
+ +Screenshot 2024-05-18 at 10 16 30 PM + + +## Real Life Examples of Queues +- Customer Service - Consider how a customer service phone line works. Customers calling are put into a queue. The first customer to call is the first one to be served (FIFO). As more customers call, they are added to the end of the queue, and as customers are served, they are removed from the front. The entire process follows the queue data structure. + +- Printers - Printers operate using a queue to manage print jobs. When a user sends a document to the printer, the job is added to the queue (enqueue). Once a job completes printing, it's removed from the queue (dequeue), and the next job in line starts. This sequential order of handling tasks perfectly exhibits the queue data structure. + +- Computer Memory - Certain types of computer memory use a queue data structure to hold and process instructions. For example, in a computer's cache memory, the fetch-decode-execute cycle of an instruction follows a queue. The first instruction fetched is the first one to be decoded and executed, while new instructions fetched are added to the rear. + +
+ +# Important Terminologies in Queues + +Understanding these terms is crucial for working with queues: + +- Enqueue - Adding an element to the back of the queue. +- Dequeue - Removing the element at the front of the queue. +- Front - The first element in the queue, to be removed next. +- Rear/Back - The last element in the queue, where new elements are added. +- Empty Queue - A queue with no elements. +- Overflow - Attempting to enqueue an element when the queue is full. +- Underflow - Attempting to dequeue an element from an empty queue. + +
+ +# Operations on a Queue + +There are some key operations in a queue that include - + +- isFULL - This operation is used to check if a queue is full. +- isEMPTY - This operation is used to check if a queue is empty. +- Display - This operation is used to display the queue elements. +- Peek - This operation is the process of getting the front value of a queue, without removing it. (i.e., Value at the front). + +image From 6c50c6e498bc3308fdd3a29c926ff76062744531 Mon Sep 17 00:00:00 2001 From: Mohammed Ahmed Majid <109688855+PilotAxis@users.noreply.github.com> Date: Sun, 19 May 2024 23:01:28 +0530 Subject: [PATCH 2/5] Update Queues.md --- contrib/ds-algorithms/Queues.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/ds-algorithms/Queues.md b/contrib/ds-algorithms/Queues.md index 918c9343..ead6ce42 100644 --- a/contrib/ds-algorithms/Queues.md +++ b/contrib/ds-algorithms/Queues.md @@ -9,7 +9,7 @@ Queues offer efficient implementations for various scenarios. They are often use
-Screenshot 2024-05-18 at 10 16 30 PM +Screenshot 2024-05-18 at 10 16 30 PM ## Real Life Examples of Queues From cbf87b0b5d9dc768dfdd8a03878789514cf75d4a Mon Sep 17 00:00:00 2001 From: Mohammed Ahmed Majid <109688855+PilotAxis@users.noreply.github.com> Date: Sun, 19 May 2024 23:03:44 +0530 Subject: [PATCH 3/5] Update Queues.md --- contrib/ds-algorithms/Queues.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/ds-algorithms/Queues.md b/contrib/ds-algorithms/Queues.md index ead6ce42..8ba2ce32 100644 --- a/contrib/ds-algorithms/Queues.md +++ b/contrib/ds-algorithms/Queues.md @@ -9,7 +9,7 @@ Queues offer efficient implementations for various scenarios. They are often use
-Screenshot 2024-05-18 at 10 16 30 PM +Types of Data Structure ## Real Life Examples of Queues From a50c8c9a90a5f4c739a24fd752add4af8627afd0 Mon Sep 17 00:00:00 2001 From: Mohammed Ahmed Majid <109688855+PilotAxis@users.noreply.github.com> Date: Tue, 21 May 2024 19:13:40 +0530 Subject: [PATCH 4/5] Update Queues.md --- contrib/ds-algorithms/Queues.md | 131 ++++++++++++++++++++++++++------ 1 file changed, 107 insertions(+), 24 deletions(-) diff --git a/contrib/ds-algorithms/Queues.md b/contrib/ds-algorithms/Queues.md index 8ba2ce32..2c5c0f0f 100644 --- a/contrib/ds-algorithms/Queues.md +++ b/contrib/ds-algorithms/Queues.md @@ -1,23 +1,33 @@ -# Introduction to Queues +# Queues in Python A queue is a linear data structure where elements are added at the back (enqueue) and removed from the front (dequeue). Imagine a line at a coffee shop, the first in line (front) gets served first, and new customers join at the back. This FIFO approach ensures order and fairness in processing elements. Queues offer efficient implementations for various scenarios. They are often used in: -- Task Scheduling - Operating systems utilize queues to manage processes waiting for CPU time. -- Breadth-first search algorithms - Traversing a tree or graph involves exploring neighbouring nodes level by level, often achieved using a queue. -- Message passing - Communication protocols leverage queues to buffer messages between applications for reliable delivery. +- **Task Scheduling** - Operating systems utilize queues to manage processes waiting for CPU time. +- **Breadth-first search algorithms** - Traversing a tree or graph involves exploring neighbouring nodes level by level, often achieved using a queue. +- **Message passing** - Communication protocols leverage queues to buffer messages between applications for reliable delivery. -
+## Types of Queue -Types of Data Structure +A queue can be classified into 4 types - +- **Simple Queue** - A simple queue is a queue, where we can only insert an element at the back and remove the element from the front of the queue, this type of queue follows the FIFO principle. +- **Double-Ended Queue (Dequeue)** - In this type of queue, insertions and deletions of elements can be performed from both ends of the queue.
+Double-ended queues can be classified into 2 types -> + - **Input-Restricted Queue** + - **Output-Restricted Queue** +- **Circular Queue** - It is a special type of queue where the back is connected to the front, where the operations follow the FIFO principle. +- **Priority Queue** - In this type of queue, elements are accessed based on their priority in the queue.
+Priority queues are of 2 types -> + - **Ascending Priority Queue** + - **Descending Priority Queue** ## Real Life Examples of Queues -- Customer Service - Consider how a customer service phone line works. Customers calling are put into a queue. The first customer to call is the first one to be served (FIFO). As more customers call, they are added to the end of the queue, and as customers are served, they are removed from the front. The entire process follows the queue data structure. +- **Customer Service** - Consider how a customer service phone line works. Customers calling are put into a queue. The first customer to call is the first one to be served (FIFO). As more customers call, they are added to the end of the queue, and as customers are served, they are removed from the front. The entire process follows the queue data structure. -- Printers - Printers operate using a queue to manage print jobs. When a user sends a document to the printer, the job is added to the queue (enqueue). Once a job completes printing, it's removed from the queue (dequeue), and the next job in line starts. This sequential order of handling tasks perfectly exhibits the queue data structure. +- **Printers** - Printers operate using a queue to manage print jobs. When a user sends a document to the printer, the job is added to the queue (enqueue). Once a job completes printing, it's removed from the queue (dequeue), and the next job in line starts. This sequential order of handling tasks perfectly exhibits the queue data structure. -- Computer Memory - Certain types of computer memory use a queue data structure to hold and process instructions. For example, in a computer's cache memory, the fetch-decode-execute cycle of an instruction follows a queue. The first instruction fetched is the first one to be decoded and executed, while new instructions fetched are added to the rear. +- **Computer Memory** - Certain types of computer memory use a queue data structure to hold and process instructions. For example, in a computer's cache memory, the fetch-decode-execute cycle of an instruction follows a queue. The first instruction fetched is the first one to be decoded and executed, while new instructions fetched are added to the rear.
@@ -25,23 +35,96 @@ Queues offer efficient implementations for various scenarios. They are often use Understanding these terms is crucial for working with queues: -- Enqueue - Adding an element to the back of the queue. -- Dequeue - Removing the element at the front of the queue. -- Front - The first element in the queue, to be removed next. -- Rear/Back - The last element in the queue, where new elements are added. -- Empty Queue - A queue with no elements. -- Overflow - Attempting to enqueue an element when the queue is full. -- Underflow - Attempting to dequeue an element from an empty queue. - -
+- **Enqueue** - Adding an element to the back of the queue. +- **Dequeue** - Removing the element at the front of the queue. +- **Front** - The first element in the queue, to be removed next. +- **Rear/Back** - The last element in the queue, where new elements are added. +- **Empty Queue** - A queue with no elements. +- **Overflow** - Attempting to enqueue an element when the queue is full. +- **Underflow** - Attempting to dequeue an element from an empty queue. -# Operations on a Queue +## Operations on a Queue There are some key operations in a queue that include - -- isFULL - This operation is used to check if a queue is full. -- isEMPTY - This operation is used to check if a queue is empty. -- Display - This operation is used to display the queue elements. -- Peek - This operation is the process of getting the front value of a queue, without removing it. (i.e., Value at the front). +- **isFULL** - This operation checks if a queue is full. +- **isEMPTY** - This operation checks if a queue is empty. +- **Display** - This operation displays the queue elements. +- **Peek** - This operation is the process of getting the front value of a queue, without removing it. (i.e., Value at the front). + +
-image +# Implementation of Queue + +```python +def isEmpty(Qu): + if Qu == []: + return True + else: + return False + +def Enqueue(Qu, item) : + Qu.append(item) + if len(Qu) == 1: + front = rear = 0 + else: + rear = len(Qu) - 1 + print(item, "enqueued to queue") +def Dequeue(Qu): + if isEmpty(Qu): + print("Underflow") + else: + item = Qu.pop(0) + if len(Qu) == 0: #if it was single-element queue + front = rear = None + print(item, "dequeued from queue") + +def Peek(Qu): + if isEmpty(Qu): + print("Underflow") + else: + front = 0 + print("Frontmost item is :", Qu[front]) + +def Display(Qu): + if isEmpty(Qu): + print("Queue Empty!") + elif len(Qu) == 1: + print(Qu[0], "<== front, rear") + else: + front = 0 + rear = len(Qu) - 1 + print(Qu[front], "<-front") + for a in range(1, rear): + print(Qu[a]) + print(Qu[rear], "<-rear") + +queue = [] #initially queue is empty +front = None + +# Example Usage +Enqueue(queue, 1) +Enqueue(queue, 2) +Enqueue(queue, 3) +Dequeue(queue) +Peek(queue) +Display(queue) +``` + +## Output + +``` +1 enqueued to queue +2 enqueued to queue +3 enqueued to queue +1 dequeued from queue +Frontmost item is : 2 +2 <-front +3 <-rear +``` + +## Complexity Analysis + +- **Worst case**: `O(n^2)` This occurs when the code performs lots of display operations. +- **Best case**: `O(n)` If the code mostly performs enqueue, dequeue and peek operations. +- **Average case**: `O(n^2)` It occurs when the number of operations in display are more than the operations in enqueue, dequeue and peek. From 0f0a658a61f9c90eb503ec575b11f1656e3436e9 Mon Sep 17 00:00:00 2001 From: Mohammed Ahmed Majid <109688855+PilotAxis@users.noreply.github.com> Date: Tue, 21 May 2024 19:19:15 +0530 Subject: [PATCH 5/5] Update index.md Added reference to the file Queues.md --- contrib/ds-algorithms/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/ds-algorithms/index.md b/contrib/ds-algorithms/index.md index 5b521551..e88fab13 100644 --- a/contrib/ds-algorithms/index.md +++ b/contrib/ds-algorithms/index.md @@ -2,3 +2,4 @@ - [Section title](filename.md) - [Sorting Algorithms](sorting-algorithms.md) +- [Queues in Python](Queues.md)