Operations supported on Max-Priority Queues are:
1) Max : Returns maximum element currently in queue in O(1) time
2) ExtractMax : Returns and removes maximum element currently
in queue in O(lg(n)) time
3) Insert: Inserts given element in queue in O(lg(n)) time
4) IncreasePriority : Increases priority of element at given index in
O(lg(n)) time
5) IncreasePriority : Decrease priority of element at given index in
O(lg(n)) time
All order statistics mentioned above is in worst case
#include <stdio.h>
#include <stdlib.h>
#define LEFT(i) ((i<<1)+1)
#define RIGHT(i) ((i<<1)+2)
#define PARENT(i) (i>>1)
void Swap(int *i,int *j);
void MaintainMaxHeap(int a[],int index,int size);
int Max(int a[],int numValidElements,int size)
{
if(numValidElements>0)
return a[0];
else
fprintf(stderr,"Error\n");
}
/*
Number of numValidElements count should be decremented
by caller itself.
*/
int ExtractMax(int a[],int numValidElements,int size)
{
if(numValidElements>0)
{
Swap(&a[0],&a[--numValidElements]);
MaintainMaxHeap(a,0,numValidElements);
return a[numValidElements];
}
else
fprintf(stderr,"Error\n");
}
void IncreasePriority(int a[],int index,int newPriority,int numValidElements)
{
if(a[index]<newPriority)
{
a[index]=newPriority;
while(index>0 && a[index]>a[PARENT(index)])
{
Swap(&a[index],&a[PARENT(index)]);
index=PARENT(index);
}
}
else
{
fprintf(stderr,"Error in IncreasePriority\n");
}
}
void DecreasePriority(int a[],int index,int newPriority,int numValidElements)
{
if(a[index]>newPriority)
{
a[index]=newPriority;
MaintainMaxHeap(a,index,numValidElements);
}
else
{
fprintf(stderr,"Error in DecreasePriority\n");
}
}
/*
Its upto caller to increase numValidElements after
*/
void Insert(int a[],int priority,int numValidElements,int size)
{
if(numValidElements<size)
{
a[numValidElements]=INFINITE;
IncreasePriority(a,numValidElements,priority,size);
}
else
fprintf(stderr,"Queue Overflow\n");
}
/*
Pre: Subtrees of node at position index are already both max_heaps
and size is number of elements in an array
Post: Element at position index is inserted properly so that tree rooted
at index is max_heap
*/
void MaintainMaxHeap(int a[],int index,int size)
{
int left=LEFT(index);
int right=RIGHT(index);
int max=index;
if(left < size && a[left]>a[max])
max=left;
if(right < size && a[right]>a[max])
max=right;
if(max!=index)
{
Swap(&a[max],&a[index]);
MaintainMaxHeap(a,max,size);
}
}
/*
Post: Elements of array a from index [0,size-1] are
arranged so as to form max heap
*/
void BuildMaxHeap(int a[],int size)
{
int i;
// Elements from (size/2) to (size-1) are all leaves
for(i=size/2-1;i>=0;i--)
MaintainMaxHeap(a,i,size);
}
void HeapSort(int a[],int size)
{
int i;
BuildMaxHeap(a,size);
for(i=size-1;i>0;i--)
{
// Max element in array is at index=0
Swap(&a[0],&a[i]);
MaintainMaxHeap(a,0,--size);
}
}
void Swap(int *i,int *j)
{
int temp=*i;
*i=*j;
*j=temp;
}
NOTES:
1)Above implementation was for implementing max priority queues, similarly minimum priority queues could also be implemented using min heaps.
2)Error Handling is done simply by printing message.In any practical application it should be thoroughly done. Thank you.
1) Max : Returns maximum element currently in queue in O(1) time
2) ExtractMax : Returns and removes maximum element currently
in queue in O(lg(n)) time
3) Insert: Inserts given element in queue in O(lg(n)) time
4) IncreasePriority : Increases priority of element at given index in
O(lg(n)) time
5) IncreasePriority : Decrease priority of element at given index in
O(lg(n)) time
#include <stdio.h>
#include <stdlib.h>
#define LEFT(i) ((i<<1)+1)
#define RIGHT(i) ((i<<1)+2)
#define PARENT(i) (i>>1)
void Swap(int *i,int *j);
void MaintainMaxHeap(int a[],int index,int size);
void BuildMaxHeap(int a[],int size);
void HeapSort(int a[],int size);
int Max(int a[],int numValidElements,int size)
{
if(numValidElements>0)
return a[0];
else
fprintf(stderr,"Error\n");
}
/*
Number of numValidElements count should be decremented
by caller itself.
*/
int ExtractMax(int a[],int numValidElements,int size)
{
if(numValidElements>0)
{
Swap(&a[0],&a[--numValidElements]);
MaintainMaxHeap(a,0,numValidElements);
return a[numValidElements];
}
else
fprintf(stderr,"Error\n");
}
void IncreasePriority(int a[],int index,int newPriority,int numValidElements)
{
if(a[index]<newPriority)
{
a[index]=newPriority;
while(index>0 && a[index]>a[PARENT(index)])
{
Swap(&a[index],&a[PARENT(index)]);
index=PARENT(index);
}
}
else
{
fprintf(stderr,"Error in IncreasePriority\n");
}
}
void DecreasePriority(int a[],int index,int newPriority,int numValidElements)
{
if(a[index]>newPriority)
{
a[index]=newPriority;
MaintainMaxHeap(a,index,numValidElements);
}
else
{
fprintf(stderr,"Error in DecreasePriority\n");
}
}
/*
Its upto caller to increase numValidElements after
*/
void Insert(int a[],int priority,int numValidElements,int size)
{
if(numValidElements<size)
{
a[numValidElements]=INFINITE;
IncreasePriority(a,numValidElements,priority,size);
}
else
fprintf(stderr,"Queue Overflow\n");
}
/*
Pre: Subtrees of node at position index are already both max_heaps
and size is number of elements in an array
Post: Element at position index is inserted properly so that tree rooted
at index is max_heap
*/
void MaintainMaxHeap(int a[],int index,int size)
{
int left=LEFT(index);
int right=RIGHT(index);
int max=index;
if(left < size && a[left]>a[max])
max=left;
if(right < size && a[right]>a[max])
max=right;
if(max!=index)
{
Swap(&a[max],&a[index]);
MaintainMaxHeap(a,max,size);
}
}
/*
Post: Elements of array a from index [0,size-1] are
arranged so as to form max heap
*/
void BuildMaxHeap(int a[],int size)
{
int i;
// Elements from (size/2) to (size-1) are all leaves
for(i=size/2-1;i>=0;i--)
MaintainMaxHeap(a,i,size);
}
void HeapSort(int a[],int size)
{
int i;
BuildMaxHeap(a,size);
for(i=size-1;i>0;i--)
{
// Max element in array is at index=0
Swap(&a[0],&a[i]);
MaintainMaxHeap(a,0,--size);
}
}
void Swap(int *i,int *j)
{
int temp=*i;
*i=*j;
*j=temp;
}
NOTES:
1)Above implementation was for implementing max priority queues, similarly minimum priority queues could also be implemented using min heaps.
2)Error Handling is done simply by printing message.In any practical application it should be thoroughly done. Thank you.
No comments:
Post a Comment