Dsa Ass 1 Ruba
Dsa Ass 1 Ruba
Abbottabad Campus
ASSIGNMENT NO 1
Priority Queue
A priority queue is a special type of queue in which each element is associated
with a priority value. And, elements are served on the basis of their priority. That
is, higher priority elements are served first.
However, if elements with the same priority occur, they are served according to
their order in the queue.
Assigning Priority Value
Generally, the value of the element itself is considered for assigning the priority.
For example,
The element with the highest value is considered the highest priority element.
However, in other cases, we can assume the element with the lowest value as the
highest priority element.
We can also set priorities according
Difference between Priority Queue and Normal Queue
In a queue, the first-in-first-out rule is implemented whereas, in a
priority queue, the values are removed on the basis of priority. The
element with the highest priority is removed first.
By using these three parts, we can create a priority queue that can efficiently store
and retrieve data based on its importance.
[This algorithm is used to insert an element into the Priority Queue, where
PQ = Array
N = Final Limit
Write (“Overflow”)
Goto step-5
End if
I ← Rear
I←I-1
End while
If (I
For J ← Rear to I + 1 do
PQ[J + 1] ← PQ[J]
End for
End if
Rear ← Rear + 1
Step-5 [Finished]
Exit
Insert the new element at the end of the tree.
[This algorithm is used to delete an element from the Priority Queue, where
PQ = Array
N = Final Limit
Write (“Underflow”)
Goto step-4
End if
Item ← PQ [Front].item
Priority ← PQ [Front].priority
PQ [I] ← PQ[I + 1]
End for
Step-4 [Finished]
Exit
Select the element to be deleted.
Select the element to be deleted
Types of Queues:
Simple Queue:
The basic form of a queue that follows the FIFO principle. Elements are
added to the rear and removed from the front.
Circular Queue:
A queue where the last element is connected to the first element, creating
a circular structure. This type of queue efficiently utilizes memory and
allows for constant-time enqueue and dequeue operations.
Priority Queue:
A queue where elements are served based on their priority levels. Higher-
priority elements are processed before lower-priority ones, regardless of
their arrival order .
Double-ended Queue (Deque):
A queue that allows insertion and deletion of elements at both the front
and the back. This provides more flexibility in managing the queue.
Blocking Queue:
A queue that blocks or waits when attempting to enqueue an element into
a full queue or dequeue from an empty queue. This queue type is often
used in concurrent programming to coordinate between producer and
consumer threads .
Applications of Queues:
Operating Systems:
Queues are used in operating systems for job scheduling, process
scheduling, and managing interrupts .
Networking:
In network routers, queues are used to manage packets waiting for
transmission, ensuring efficient data flow.
Breadth-First Search (BFS):
Queues are essential in graph algorithms like BFS for traversing graphs
in a level-by-level manner.
Printer Queue:
Managing documents waiting to be printed, ensuring fair and orderly
printing.
Banks and Supermarkets:
Queues are utilized to manage customer queues at service points,
ensuring fairness and efficiency in serving customers.
Call Center Systems:
Managing incoming calls waiting to be serviced by agents, ensuring
timely and orderly handling of customer inquiries.
Simulation:
Queues are used in simulations to model various real-world scenarios
such as traffic flow, customer service systems, and manufacturing
processes.
Buffering :
In computing and networking, queues are used for buffering data between
different processes or systems, ensuring smooth data transmission and
processing. Priority Queue Implementations in, Java.
Advantages of Priority Queue:
It helps to access the elements in a faster way. This is because elements in a
priority queue are ordered by priority, one can easily retrieve the highest
priority element without having to search through the entire queue.
The ordering of elements in a Priority Queue is done dynamically. Elements in
a priority queue can have their priority values updated, which allows the queue
to dynamically reorder itself as priorities change.
Efficient algorithms can be implemented. Priority queues are used in many
algorithms to improve their efficiency, such as Dijkstra’s algorithm for finding
the shortest path in a graph and the A* search algorithm for path finding.
Disadvantages of Priority Queue:
High complexity. Priority queues are more complex than simple data
structures like arrays and linked lists, and may be more difficult to implement
and maintain.
High consumption of memory. Storing the priority value for each element in a
priority queue can take up additional memory, which may be a concern in
systems with limited resources.
It is not always the most efficient data structure. In some cases, other data
structures like heaps or binary search trees may be more efficient for certain
operations, such as finding the minimum or maximum element in the queue.
At times it is less predictable. This is because the order of elements in a
priority queue is determined by their priority values, the order in which
elements are retrieved may be less predictable than with other data structures
like stacks or queues, which follow a first-in, first-out (FIFO) or last-in, first-
out (LIFO) order.
----------PROGRAM-------
size = 0; }
System.out.println("Queue is full!");
return;
array[size] = item;
int i = size;
swap(array, i, i - 1);
i--; }
size++; }
if (size == 0) {
System.out.println("Queue is empty!");
return -1; }
size--;
heapifyDown(0);
return max; }
if (size == 0)
System.out.println("Queue is empty!");
return -1; }
size--;
heapifyUp(0);
return min; }
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
largest = left; }
largest = right; }
if (largest != i) {
swap(array, i, largest);
heapifyDown(largest);
}}
int smallest = i;
int parent = (i - 1) / 2;
smallest = parent; }
if (smallest != i) {
swap(array, i, smallest);
heapifyUp(smallest);
}}
array[i] = array[j];
array[j] = temp;
}}
____________________________________________________________________________