Priority Queue Implementation
Priority Queue Implementation
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.
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).
• 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.
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)