0% found this document useful (0 votes)
80 views

Priority Queue Implementation

This document describes implementations of a priority queue using an array and hash table data structures. It aims to compare the efficiency of insertion and execution of processes for each approach. The array implementation stores processes in an array from highest to lowest priority. Insertion takes O(N^2) time by shifting processes, while execution takes O(1) time by removing the last process. The hash table maps priorities to linked lists, allowing O(1) insertion and execution by adding/removing from list heads. Testing showed the array had longer average insertion and waiting times than the hash table approach.

Uploaded by

Hadi H.M
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Priority Queue Implementation

This document describes implementations of a priority queue using an array and hash table data structures. It aims to compare the efficiency of insertion and execution of processes for each approach. The array implementation stores processes in an array from highest to lowest priority. Insertion takes O(N^2) time by shifting processes, while execution takes O(1) time by removing the last process. The hash table maps priorities to linked lists, allowing O(1) insertion and execution by adding/removing from list heads. Testing showed the array had longer average insertion and waiting times than the hash table approach.

Uploaded by

Hadi H.M
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

P R I O R I T Y Q U E U E I M P L E M E N TAT I O N

I 2 2 0 6 P R O J E C T
AIM OF THE PROJECT
• This project aims to show different ways of managing priority queues by using Array and Hash Table data

structures
• And to show the methods used and the results for insertion and execution of a given processes
• And to compare the results ,showing the efficiency for each
• approach .
PRIORITY QUEUE
• Priority Queue is an extension of queue with following properties:
1.Every item has a priority associated with it.
2.An element with high priority is dequeued before an element with low priority.
3.If two elements have the same priority, they are served according to their order in the
queue(FIFO).
• A typical priority queue supports following operations:
• insert(item): Inserts an item with given priority.
• getHighestPriority(): Returns the highest priority item.
• deleteHighestPriority(): Removes the highest priority
item(Execute).
I - T H E A R R AY I M P L E M E N TAT I O N
• the priority queue is represented by an array having two indexes (front(head) and
rear(tail) ).where the processes having higher priority are set to the right side of the array (tail)
and lower priority to the left of the array.

This is the shape of an


empty queue
INSERTION
• For the first process (p1) insertion is done by enqueueing (inserting in the rear) and increasing the
rear index by 1.

Int enqueue(queue * q,process e) {


if( isFull( * q ) )
return 0;
q->rear = (q->rear + 1 ) % N;
q->data[q->rear]=e;
return 1;
}
• Then the following processes are checked where we search for the first process(pi) in the array of
same priority or higher and we shift all processes after the found one and pi to the right (rear) and
the process is placed in the position of the found process(p1) and the rear index is increased by 1:
• Ex: a process p3 of priority 3 needs to be inserted.
void check(queue *q,process p)
{
int i,j;
for(i=0; i<=q->rear; i++)
{

if(p.priority>=q>data[i].priority)//find the
first process of same priority
{
//shift all processes after the
found one to the right (rear)
for(j=q->rear+1; j>i; j--)
{
q->data[j]=q->data[j-1];
}
q->data[i]=p;//insert it to ith
position
return;
}
}
q->data[i]=p;
}
COMPLEXITY ON INSERTION
• The search for first process pi costs O(N).
• And the shifting process costs O(N) .
• So the order of this operation is done in O(N^2).
EXECUTION OF HIGHEST
PRIORITY PROCESS
• the highest priority process is found always at the rear side of the array,so we print the id of the
rear process and sleep its execution time then we shift the rear to the back in order to execute the
next high priority process and so on till we reach the front process(last process).
• Complexity:since we always remove the last process (at the rear )this operation is of order O(1).

void executeByPriority(queue *q)//executes the highest priority process


{
if(isEmpty1(*q))
{
printf("no elements to delete the queue is empty");
return;
}
{ double x=(double)q->data[q->rear].timeex/100;
printf("the process of id (%d) is executed successfully \n",q->data[q->rear].id);
q->rear--;//shift the rear back
Sleep(x);//simulate the execution time
if(q->rear==1)
q->front=0;
return;
}
}
D I S P L AY I N G T H E P R O C E S S E S
A C C O R D I N G T O A R R I VA L T I M E A N D
PRIORITY
• For displaying the list of waiting processes for each priority according to their arrival order we
start from the index 1 till the rear index, we print the processes of same priority and we set a
count to count the number of processes having the same priority(this is of order O(n)) and then
restart this operation starting from the new count (to avoid repetition)
TESTING OF
A R R AY
I M P L E M E N TAT I O N
• The insertion of process is done by reading each process
from the file and inserting it into an empty array.

• Then we display all the processes in the queue .


• Then the execution is performed till the last process .
R E S U LT S
• By subtracting the time before the insertion from the time after the time required for inserting all
processes from the file is obtained and found to be (10.592 seconds). So the average to insert the
all processes is (10.592 seconds/numberOfProcesses = 0.10592 milliseconds).
• And by subtracting the time before the insertion from the time after the time required for
execution all processes in the array is found to be (54168.8 seconds =15.0468 hours),so the
average time to execute a processes is (541.688 milliseconds).
• To calculate the average waiting time of all processes we have to sum the waiting time for all process and
divide it by total number of processes . Each process is inserted after the average number to insert a
process multiplied by its id since the insertion is in ascending order of id .And executed after the whole
insertion time added to the time required by the previously executed process to execute .so we start from
the highest priority processes in the array and we calculate its waiting time and add its execution time to
the whole execution time taken and then we continue in the same manner till we reach the last process.
Using this method the average waiting time was found to be (6.865699761416 hours) .
• In the same manner of waiting time except adding the burst time(execution time)for each process we get
the average turnaround time which is (6.865837174583 hours)
H A S H TA B L E I M P L E M E N TAT I O N
• We construct a data hash table of size equal to the highest priority +1 of the processes containing pointers
to a set of empty linked lists(of processes) in order to avoid collision .

• The hash key for each processes is set to its priority modulo (the highest

priority +1) .
Ex : a process of priority 1 has a hash key = 1%2000=1 .
INSERTION
• After calculating the hash key of the process, the process is inserted at the tail of the linked list
found at the hash key index .
• ex: a process p1 of priority 1 is inserted in the tail of the linked list found at the index 1 in the hash table ,
this operation is of complexity O(1).

- If another process of same priority is inserted the processes is inserted in the tail of the same linked list at
the same index , that is known as separate chaining collision Resolution Technique.
data* insert(data*hashArray1,process key
)
{
data* b=hashArray1;
int hashIndex = hashCode(key);//gets
the hash key
Priority hash table after inserting several
processes
insertTail(b[hashIndex].li,key);//insert
the process in the list at its hash key
index

return b;
}
EXECUTING HIGHEST PRIORITY
PROCESS
• the higher priority process is found on the head of the first non empty linked list .after its execution its easly
removed by removing the head of this linked list .
• since we have to search for the first non empty linked list its of order O(N) and in best case O(1).

Highest
priority
process
Remove tail
of the list
D I S P L AY I N G T H E P R O C E S S E S
• we iterate from the start till the end of the hash table(O(N)) if the linked list at a specific index is not empty
we print the components of the linked list from the head to the tail(O(N)) respecting their arrival order.
• these whole process is of order O(N^2).

void displayProcesses(data *a)//displays all the process from highest to lowest according to their arrival order
{
for(int i=1; i<SIZE; i++)
{
if(!isEmpty(*a[i].li))
{
printf("the waiting processes having priority %d are :\n",i-1);
displayLL(*a[i].li);//prints the components of a linked list from head to the tail
printf("\n");
}
else
printf("");
}
}
T E S T I N G T H E H A S H TA B L E
I M P L E M E N TAT I O N
• Insertion is done by reading the processes from the file and inserting them into an empty hash
table .
• And then displaying all the processes in the hash array .
• And By subtracting the time before the insertion from the time after the time required for
inserting all processes from the file is obtained and found to be 37 milliseconds. So the average to
insert the all processes is 37 milliseconds/numberOfProcesses = 0.000370 milliseconds.
Execution simulation.

• by subtracting the time before the insertion from


the time after) the time required for execution all
processes in the array is found to be (53261.7
seconds =14.794916 hours),so the average time to
execute a processes is 532.6170 milliseconds.
• In the same manner used in the array implementation the average Waiting time and average Turnarround
time is calculated ,we started from the highest priority process till the lowest one. And added their Waiting
time and divide it by the total number of processes so it turned up to be (23740867.732761 ms
=6.5946854813225 hours).
• And the average Turnarround calculated (23741352.741861 ms =6.5948202060725 hours).
Summary of the results

Type\test Average Complexity Average Complexity Average Average waiting time


Insertion of insertion execution of execution turnaround
time time
Array 0.10592 O(N^2) 541.688 O(1) 6.8658 6.8656
implementation
ms ms hours hours

Hash 0.000370 O(1) 532.6170 O(N) 6.5948 6.5946 hours


implementation
ms ms hours

Conclusion:
By comparing to the theoretical optimal execution time (13.741316 hours) the two approaches shows a
consisting results ,but still the hash array a
step ahead from the array implementation in both insertion (10.55 seconds faster) and execution time
(906.7824 seconds faster)

You might also like