Implementation and operation of "algorithm design-priority queue" priority queue

Source: Internet
Author: User

Priority queues are a concrete application of heap sequencing.

The priority queue is divided into the following actions:

1.INSERT (S,X) inserts the element x into the priority queue.

2.MAXIMUM (s): Returns the element with the largest keyword in S.

3.extract_max (s): Remove the element of the largest keyword in s

4.increase_key (s,x,k): Increments the key value of element x to K,k is not less than the element of X.

Application of Priority queue:

1. Job scheduling for shared computer systems. The maximum priority queue to I records the individual jobs to be executed and the relative priority between them. When a job is completed or interrupted, the scheduler invokes Extract_max from all waiting jobs and selects the job with the highest priority to execute. At any time the scheduler can call insert to put a new job into the queue.

2. Can be used in minimum spanning tree and cell shortest Path first algorithm, need to apply Decrease_key application.

Code:

/*
Author:bai
Name: Priority queue
Use for:1. Job scheduling in a shared computer system
2. Event-driven simulator
3. Use in minimum spanning tree and cell shortest path
Date:2015/4/30
*/
#include <iostream>
using namespace Std;
typedef struct HEAP
{
int heap_size;
int *key;
}heap;
void Initialize (heap *a)
{
a->key=new int;
a->heap_size=0;
}
void Max_heapify (Heap *a,int i)//in order to keep the heap Dagen in nature (I use the method of cyclic operation here, and the approach of recursive invocation is applied in the introduction of algorithm)
{
Non-recursive method
int largest;
int l=2*i;//l is A's left child
int R=2*I+1;//R is A's right child
if (L<=a->heap_size&&a->key[l]>a->key[i])//Here the greater than sign change to less than the number is small Gan sort
Largest=l;
Else
largest=i;
if (R<=a->heap_size&&a->key[r]>a->key[largest])
Largest=r;
while (largest!=i)
{
int temp=a->key[i];
a->key[i]=a->key[largest];
a->key[largest]=temp;
I=largest;
L=2*i;
r=2*i+1;
if (L<=a->heap_size&&a->key[l]>a->key[i])
Largest=l;
Else
largest=i;
if (R<=a->heap_size&&a->key[r]>a->key[largest])
Largest=r;
}
for (int k=1;k<=a->heap_size;k++)
printf ("%d,", a->key[k]);
printf ("\ n");
}
void Build_max_heap (HEAP *a)
{
int m= (a->heap_size)/2;
while (m>=1)
{
Max_heapify (A,M);
m--;
}
}
void Heap_sort (HEAP *a)
{
Build_max_heap (A);
printf ("The array after the first build of the big root heap is:");
for (int k=1;k<=a->heap_size;k++)
printf ("%d,", a->key[k]);
printf ("\ n");
int length=a->heap_size;
printf ("The big root heap is sorted after the array is:");
printf ("%d,", a->key[1]);
for (int i=length;i>=2;i--)
{
a->key[1]=a->key[i];
a->heap_size--;
Max_heapify (a,1);//Now only the first node of the heap is wrong, and the other subtrees are large piles
printf ("%d", a->key[1]);
}
printf ("\ n");
printf ("done\n");
}
Maximum value for priority queue
int Heap_maximum (HEAP *a)
{
printf ("%d\n", a->key[1]);
return 0;
}
Remove the maximum value from the priority queue
void Heap_extract_max (HEAP *a)
{
if (a->heap_size<1)
printf ("Can't continue to delete anymore!") \ n ");
a->key[1]=a->key[a->heap_size];
a->heap_size--;
Max_heapify (a,1);
}
Increase a number m in the priority queue to Key,key>m
void Heap_increase_key (HEAP *a,int i,int KEY)
{
a->key[i]=key;
int PARENT=I/2;
while (I>1&&a->key[i]>a->key[parent])//This point increases, to determine whether it is larger than the parent node, and then loop to modify.
{
int temp=a->key[i];
a->key[i]=a->key[parent];
a->key[parent]=temp;
I=parent;
PARENT=I/2;
}


}
Insert the value of key into the a priority queue
First create a new node, then assign it a negative infinity, and then use Heap_increase_key to increase it to key.
void Heap_insert (HEAP *a,int key)
{
a->heap_size=a->heap_size+1;
a->key[a->heap_size]=int_min;
Heap_increase_key (A,a->heap_size,key);
}
void Show_heap (HEAP *a)
{
printf ("The current priority queue is: \ n");
for (int i=1;i<=a->heap_size;i++)
printf ("%d", a->key[i]);
printf ("\ n");
}
int main (void)
{
int ch;
Heap *a=new Heap;
Initialize (A);
printf ("Please enter a set of values for the heap to end with #?") \ n ");
int key;
int i=1;
while (scanf ("%d", &key) ==1)
{
Heap_insert (A,key);//The inserted process is automatically created as a priority queue
}
Fflush (stdin);
while (1)
{
printf ("*************** Please select the following action option ***************\n");
printf ("1. Take the maximum value of the priority queue \ n");
printf ("2. Remove the current maximum value of the priority queue and still automatically change to the priority queue structure \ n" after removal);
printf ("3. Insert a value, which is still automatically transformed into a priority queue structure \ n");
printf ("4. Increase the value of an I point to key\n");
printf ("5. Exit system \ n");
scanf ("%d", &ch);
Switch (CH)
{
Case 1:heap_maximum (A);
Case 2:

Heap_extract_max (A);
Break
Case 3:
{
int key;
printf ("Please enter the key value you want to insert:");
scanf ("%d", &key);
Heap_insert (A,key);
}
Show_heap (A);
Break
Case 4:
{
printf ("How big are you going to turn the number?" :");
int I,key;
scanf ("%d%d", &i,&key);
Heap_increase_key (A,i,key);
}
Show_heap (A);
Break
Case 5:
return 0;
Default:break;
}
}
}

Results show:






Implementation and operation of "algorithm design-priority queue" priority queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.