[Algorithm design-priority queue] implementation and operation of priority queue, algorithm queue
Priority queue is a specific application of heap sorting.
Priority Queues are divided into the following operations:
1. INSERT (S, x) inserts element x into the priority queue.
2. MAXIMUM (S): returns the element with the MAXIMUM keyword in s.
3. EXTRACT_MAX (S): removes the element of the maximum keyword in S.
4. INCREASE_KEY (S, x, k): increases the key value of element x to k, and k is an element not less than x.
Application of priority queue:
1. Job Scheduling in the shared computer system. The maximum priority queue requires I to record the jobs to be executed and their relative priority. After a job is completed or interrupted, the scheduler calls EXTRACT_MAX to select the job with the highest priority from all the waiting jobs for execution. At any time, the scheduler can call INSERT to present a new job to the queue.
2. DECREASE_KEY can be used in the Minimum Spanning Tree and cell Shortest Path priority algorithms.
Code:
/*
Author: Bai
Name: priority queue
Use for: 1. Share Job Scheduling in computer systems
2. event-driven Simulators
3. Used in the Minimum Spanning Tree and the minimum cell path
Date:
*/
# Include <iostream>
Using namespace std;
Typedef struct heap
{
Int heap_size;
Int * key;
} Heap;
Void initialize (heap *)
{
A-> key = new int;
A-> heap_size = 0;
}
Void MAX_HEAPIFY (heap * A, int I) // to keep the heap as A large root heap (here I use the cyclic operation method, the recursive call method is applied in the introduction to algorithms)
{
// Non-Recursive Method
Int largest;
Int l = 2 * I; // l is the left child of.
Int r = 2 * I + 1; // r is the right child of.
If (l <= A-> heap_size & A-> key [l]> A-> key [I]) // The greater than the number here is changed to smaller than the number, that is, the smaller root heap sorting.
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 *)
{
Int m = (A-> heap_size)/2;
While (m> = 1)
{
MAX_HEAPIFY (A, m );
M --;
}
}
Void HEAP_SORT (heap *)
{
BUILD_MAX_HEAP ();
Printf ("the array after the first creation of a large 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 array after sorting the big root heap 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); // currently, only the first node of the heap is incorrect, and all the other subtree is A large root heap.
Printf ("% d", A-> key [1]);
}
Printf ("\ n ");
Printf ("done \ n ");
}
// Calculate the maximum value of the priority queue
Int HEAP_MAXIMUM (heap *)
{
Printf ("% d \ n", A-> key [1]);
Return 0;
}
// Remove the maximum value from the priority queue
Void HEAP_EXTRACT_MAX (heap *)
{
If (A-> heap_size <1)
Printf ("You cannot delete it again! \ N ");
A-> key [1] = A-> key [A-> heap_size];
A-> heap_size --;
MAX_HEAPIFY (A, 1 );
}
// Increase the number of 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]) // after this point is increased, You need to judge whether it is bigger than the parent node, then, it is cyclically modified.
{
Int temp = A-> key [I];
A-> key [I] = A-> key [parent];
A-> key [parent] = temp;
I = parent;
Parent = I/2;
}
}
// Insert the key value into the priority queue
// Create a new node, assign it a negative infinity value, and then use HEAP_INCREASE_KEY to increase it to the 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 *)
{
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 ();
Printf ("enter a set of heap values to end? \ N ");
Int key;
Int I = 1;
While (scanf ("% d", & key) = 1)
{
HEAP_INSERT (A, key); // The insert process automatically creates A priority queue.
}
Fflush (stdin );
While (1)
{
Printf ("**************** select the following operation options **************** \ n ");
Printf ("1. obtain the maximum value of the priority queue \ n ");
Printf ("2. Remove the current maximum value of the priority queue. After the removal, it is still automatically changed to the priority queue structure \ n ");
Printf ("3. Insert a value. After insertion, it is still automatically converted to the 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); break;
Case 2:
HEAP_EXTRACT_MAX ();
Break;
Case 3:
{
Int key;
Printf ("Enter the Key value you want to insert :");
Scanf ("% d", & key );
HEAP_INSERT (A, key );
}
SHOW_HEAP ();
Break;
Case 4:
{
Printf ("How much do you want to change the number? :");
Int I, key;
Scanf ("% d", & I, & key );
HEAP_INCREASE_KEY (A, I, key );
}
SHOW_HEAP ();
Break;
Case 5:
Return 0;
Default: break;
}
}
}
Result Display: