天天看点

算法导论 第六章:优先级队列

        虽然堆排序的时间复杂度为O(nlgn),但在实际中,快速排序(下一章将介绍)往往要优于堆排序。尽管如此,堆数据结构在其他方面有着很大的用处,如用于优先级队列的实现。因为堆可让优先级队列的所有操作的时间复杂度为O(lgn)。最大优先级队列常用于共享主机上的作业调度,最小优先级队列常用于事件驱动的模拟中。

以最大优先级队列为例,其支持的操作如下:

1)INSERT(S,x): 向集合中插入元素x

算法导论 第六章:优先级队列

2)MAXIMUM(S): 返回集合S的最大值

算法导论 第六章:优先级队列

3)EXTRACT-MAX(S):删除集合S的最大值

算法导论 第六章:优先级队列

4)INCREASE-KEY(S,x,k):将集合S中的元素x的值增加为k

算法导论 第六章:优先级队列

最大优先级队列实现的完整代码如下:

#include<iostream>
#include<cstdlib>
#include<climits>
#define PARENT(i) (i/2)
#define LEFT(i)   (2*i)
#define RIGHT(i)  (2*i+1)
using namespace std;

void Print(int *a)
{
	int n=a[0];
	for(int i=1;i<=n;i++)
		cout<<a[i]<<"   ";
	cout<<endl;
	}
int *Transform(int *a,int n)
{
  int * A=new int[n+1];
   A[0]=n;
   for(int i=0;i<n;i++)
	   A[i+1]=a[i];
   return A;	
	}
void swap(int &a,int &b)
{
	int temp;
	temp=a;
	a=b;
	b=temp;
	}
void Max_Heapify(int *A,int i)
{//adjust the heap to maintain the heap property
	int heap_size=A[0];
	int l=LEFT(i) ;   //index of i's left child
	int r=RIGHT(i);
	int largest;     //At each step,the largest of the element A[i],A[LEFT(i)]
					 //A[RIGHT(i)] is determined,and its index is stored in largest. 
	if(l<=heap_size && A[l]>A[i])
		largest	= l;
	else
		largest = i;

	if(r<=heap_size && A[r]>A[largest])
		largest = r;
	
	if(largest!=i)     //the subtree rooted at i violate the max-heap property
	{ 
		swap(A[i],A[largest]);
		Max_Heapify(A,largest);  //after exchanging ,the subtree rooted at largest might violate the max-heap property
 		}
	}
void Build_MaxHeap(int *A)
{//Transform array A into max-heap A,where A[0]=heap-size
	int A_length=A[0];
	for(int i=A_length/2;i>=1;i--)
		Max_Heapify(A,i);	
	}
int GetHeapMaximum(int *A)
{//return the elements of S with the largest key
	Build_MaxHeap(A);
	return A[1];
	}
int Heap_ExtractMax(int *A)
{
	int max;
	int heap_size;
	heap_size=A[0];
	if(heap_size<1)
	{ 
		cout<<"heap underflow"<<endl;
		return -1;
		}
	Build_MaxHeap(A);
	max=A[1];
	A[1]=A[heap_size];
	heap_size--;
	A[0]=heap_size;
	Max_Heapify(A,1);
	
	return max;
	}
void Heap_IncreaseKey(int *A,int i,int key)
{
	Build_MaxHeap(A);
	   if(key<A[i]){
		cout<<"new key is smaller than the current key."<<endl;
		return;
		}
	A[i]=key;
	while(i>1 && A[PARENT(i)]<A[i])
	 {
		swap(A[PARENT(i)],A[i]);
		i=PARENT(i);	
	 	}
	}
void Heap_InsertKey(int *A,int key)
{
	int heap_size=A[0];
	heap_size=heap_size+1;
	A[0]=heap_size;
	A[heap_size]=INT_MIN;
	Heap_IncreaseKey(A,heap_size,key);
	}
int main(){
	
	int a[]={4,1,3,2,16,9,10,14,8,7};
	int n=sizeof(a)/sizeof(int);
	int *A=new int[n+1];

	cout<<"---------------Init the set--------------------"<<endl;
	cout<<"After initing,S[1..n] is:"<<endl;
	A=Transform(a,n);   //Transform a[0...n-1] into a[1...n];a[0]=a.length
	Print(A);
	
	cout<<"---------------Excute MAXIMUM------------------"<<endl;
	int hMaxN;
	hMaxN=GetHeapMaximum(A);
	cout<<"The largest key is:"<<hMaxN<<endl;
	
	cout<<"------------Excute EXTRACT-MAX-----------------"<<endl;
	hMaxN=Heap_ExtractMax(A);
	cout<<"The largest key is:"<<hMaxN<<endl;
	cout<<"After removing the largest,the heap is:"<<endl;
	Print(A);
	
	cout<<"------------Excute INCREASE-KEY----------------"<<endl;
	int key=11;
	int index=6;
	Heap_IncreaseKey(A,index,key);
	cout<<"After increasing ,the heap is:"<<endl;
	Print(A);
	
	cout<<"--------------Excute HEAP-INSERT---------------"<<endl;
	int ikey=13;
	Heap_InsertKey(A,ikey);
	Print(A);
	cout<<"-----------------------------------------------"<<endl;
	return 0;
	}
           

运行结果如下:

算法导论 第六章:优先级队列

若有错误,请指正~~~

继续阅读