You are on page 1of 10

DS & AA UNIT-V

Priority Queues A priority queue is a queue in which the sequence is created in such a way that the element with the highest (or smallest) priority is at the front, and the rest of the elements are arranged in priority sequence. A priority queue is a BIFO container: The best one in comes out first. This means that each element is assigned a priority number, and the element with the highest priority comes out first. Entries with equal priorities are processed in FIFO sequence. The operations performed on a priority queue are 1) Find an element 2) Insert a new element 3) Remove an element. In a min priority queue, the find operation finds the element with minimum priority and the remove operation removes this element. In a max priority queue, the find operation finds the element with maximum priority, and the remove operation removes this element. Priority queues are widely used in computer systems. Priority queues may be used for task scheduling in computers, where some programs and activities should be executed sooner than others and are therefore given a higher priority.

Priority Queue ADT AbstractDataType maxPriorityQueue { Instances Finite collection of elements, each has a priority Operations empty( ) : Return true iff the queue is empty size( ) : Return number of elements in the queue top( ) : Return element with maximum priority pop( ) : Remove the element with largest priority from the queue push( x ) : Insert the element x into the queue. }

DS & AA UNIT-V

Example Operations on MinPriorityQueue:

Representation of Priority queue using Heap: We can use Binary Heap data structure to represent the priority queue. A heap or max heap is a complete binary tree in which each parent node u labeled by a element e(u) and its respective child nodes v, w labeled e(v), e(w) respectively are such that e(u) >= e(v) and also e(u) >= e(w). Since the parent node keys are greater than or equal to their respective child node keys at each level, the key at the root node would turn out to be the largest amongst all the keys represented as a heap. Properties of Binary Heap: There are mainly two properties that must be satisfied by a heap data structure. 1. Structure Property 2. Heap Order Property 1. Structural Property: A heap is a binary tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right. Such a tree is known as a complete binary tree. 2. Heap Order Property: The property that allows operations to be performed quickly is the heap order property. With this property the largest element should be at the root. If we consider that any sub tree should also be a heap, then any node should be greater than all of its descendants.

DS & AA UNIT-V
Representation of Binary Heap: We can use one-dimensional array to represent the elements of heap data structure which is formed in complete binary tree. The elements of the heap may be mapped with the locations of the array in such a way that the parent of the element at index i is at index i/2, and the children are at indexes 2i and 2i+1. Storing a Heap in an Array For example the given heap is

The elements in the above heap mapped in to the locations of the array as

For example, in the above tree the element 60 is at index i = 5, its parent is element 66 at index i/2 = 2, and its children are elements 58 and 25 at indexes 2i = 10 and 2i + 1=11. The natural mapping between a complete binary tree and an array is a two- way correspondence. To map the array elements back into a complete binary tree, simply number the tree nodes consecutively in a level-order traversal beginning with number 1 at the root. Then copy the array element at index i into the tree node numbered i.

DS & AA UNIT-V
Construction of Heap: Given an Unordered list of elements L = { k1,k2,k3,..kn} , the construction of the heap proceeds by inserting keys from L one by one into an existing heap. First k1 is inserted into the initially empty heap as its root. K2 is inserted as the left child of k1. if the heap order property is violated then k1 and k2 swap positions to construct a heap out of themselves. Next k3 is inserted as the right child of node k1. if k3 violates the heap order property then k3 swaps position with its parent k1 and so on. In general, a key ki is inserted into the heap as the child of node i/2. If the heap orders property is violated then swap between ki and ki/2 which in turn may trigger further adjustments between ki/2 and its parent and so on.

DS & AA UNIT-V
Algorithm buildheap( a,n) { for i:=1 to n do call insert ( a, i); } Insertion into Heap: To insert an element x into the heap, we create a hole in the next available location. If x can be placed in the hole without violating heap order, then we do so and are done. Otherwise we slide the element that is in the hole's parent node into the hole, thus bubbling the hole up toward the root. We continue this process until x can be placed in the hole where it is no longer greater than its parent..

Procedure to insert into a binary heap: Algorithm Insert (a, n) { //Inserts a[n] into heap which is stored in a [1 : n-1] i : = n; item : = a[n]; while( ( i>1) and (a[ i/2] < item )) do { a[i] := a[ i/2]; i:= i/2; } a[i]: item; } 5

DS & AA UNIT-V
Deleting from Heap: This operation deletes the largest node from the heap. Because the largest node always found at the top of the heap, we know its location. The heap removal algorithm always removes the root element from the tree. This is done by moving the last leaf element into the root element and then restoring the heap property by penetrating the new root element down the tree until it is no longer less than its children. On each iteration, the parent is swapped with the greater of its two children.

In this example 88 which is the root will be removed and the root is replaced with the last leaf (key 44). Then, to restore the heap property, the element 44 is swapped with the larger of its two children (77). That step is repeated until the element 44 is no longer smaller than any of its children. In this case, the result is that 44 end up as a leaf again. Algorithm DelMAX( a, n, x) // Delete the maximum from heap a[1:n] and store it in x { if( n == 0) then { Write heap is empty; } x: = a[1]; a[1]:= a[n]; call Adjust( a,1, n-1); }

DS & AA UNIT-V
Algorithm Adjust ( a, i , n) { // The complete binary tree with roots 2i and 2i +1 are combined with node i to form a heap rooted at i i:= 2*i; item := a[i]; while ( i<=n) do { // compare left and right child and let j be larger child if( j< n) and ( a[ j] < a[j+1])) then j: =j+1; if( item >= a[j]) then break; // A position for item is found. a[ j/2] : = a[j]; j: = 2j; } a[ j/2]:= item; } External Sorting The external sorting methods are the exclusive strategies to sort huge files residing in external storage devices like magnetic tapes, magnetic disks, hard disks. Model for External Sorting The wide variety of mass storage devices makes external sorting much more deviceindependent than internal sorting. Since access to an element on tape is done by winding the tape to the correct location , tapes can be efficiently accessed only in sequential order in either direction. We need to have at least three tape drives to perform sorting. We need 2 drives to do an efficient sort, the third drive simplifies the procedure. 2-Way Merging The basic external sorting algorithm uses merging algorithm from merge sort. Suppose we have four tapes Ta1,Ta2, Tb1,Tb2 which are two input and two output tapes. Depending on the point in the algorithm , the a and b tapes are either input tapes or output tapes. Suppose the data are initially on Ta1. Further that the internal memory can hold and sort M records at a time. A natural first step is to read M records at a time from the input tape, sort the records internally , and then write the sorted records alternatively to Tb1 and Tb2, which are output tapes We will call each set of sorted records a run. When this is done we rewind all the tapes. Now Tb1 and Tb2 contain a group of runs. We take the first run from each tape and merge them, writing the result, which is a run twice as long on to Ta1. 7

DS & AA UNIT-V
Recalling the merging operation on two sorted lists is simple; we need almost no memory, since the merge is performed as Tb1 and Tb2 advance. Then we take the next run from each tape, merge these and write the result to Ta2. We continue this process, alternating between Ta1 and Ta2 until either Tb1 or Tb2 is empty. At this point either both are empty or there is one run left. In the latter case, we copy this run to the appropriate tape. We rewind all four tapes, and repeat the same steps, this time using the a tapes as input and the b tapes as output. This will give runs of 4 M. We continue the process until we get one run of length N. This algorithm will require [log (N/4)] passes, plus the initial run-constructing pass. For N=13, M=3 it requires [lg 13/3]=3 more passes and initial run-constructing pass A common principle behind most popular external sorting methods is 1) Internally sort batches of records from the source files to generate runs. Write out the runs as and when they are generated on to external storage devices 2) Merge the runs generated in the earlier phase, to obtain large but fewer runs and write them onto the external storage devices. 3) Repeat the run generation and merge until the final phase. Only one run gets generated on which the sorting of files is done. For example N = 13 records and M = 3 records at a time internal memory can hold and sort. Ta1 Ta2 Tb1 Tb2 81 94 11 96 12 35 17 99 28 58 41 75 15

Step 1: Ta1 Ta2 Tb1 Tb2

11 12

81 35

94 96

17 41

28 58

99 75

15

Step 2: Ta1 11 Ta2 17 Tb1 Tb2

12 28

35 41

81 58

94 75

96 99

15

DS & AA UNIT-V
Step 3: Ta1 Ta2 Tb1 Tb2

11 15

12

17

28

35

51

58

75

81

94

96

99

Step 4: Ta1 Ta2 Tb1 Tb2 11 12 15 17 28 35 41 58 75 81 94 96 99

Multi Way Merging Basic 2-way Merge can be enhanced to a k-way Merge by taking extra tapes to reduce the number of passes required to sort the input data. Merging two runs is done by winding each input tape to a beginning of each run. Then the smaller element is found ,placed on an output tape, and the appropriate input tape is advanced. If there are k input tapes ,this strategy works the same way but the only difference being that it is more complicated to find the smallest of the k elements. We can find the smallest of these elements by using a priority queue. To obtain the next element to write on the output tape ,we perform deleteMin( ) operation in priority queue. The appropriate input tape is advanced ,and if the run on the input tape is not yet completed, we insert the new element into the priority queue. After the initial run construction phase , the number of passes required using k-way merging is logk(N/M) because the runs get k times as large in each pass. For example N=13 and M=3 and k=3 tapes used then the total passes required are log3(13/3)=2 passes after initial construction of runs in tapes. Ta1 Ta2 Ta3 Tb1 Tb2 Tb3 Ta1 Ta2 Ta3 Tb1 Tb2 Tb3 81 94 11 96 12 35 17 99 28 58 41 75 15

Step1

11 12 17

81 35 28

94 96 99

41 15

58

75

DS & AA UNIT-V
Ta1 Ta2 Ta3 Tb1 Tb2 Tb3 Ta1 Ta2 Ta3 Tb1 Tb2 Tb3 Step 2 11 12 15 41 17 58 28 75 35 81 94 96 66

Step 3

11

12

15

17

28

35

41

58

75

81

94

96

99

Polyphase Merge The k-way Merging strategy requires the use of 2k tapes. This could be prohibitive for some applications. It is possible to get by with only k+1 tapes. That means 2-way merging can be performed with only 3 tapes. In 2-way merging suppose we have 3 tapes T1,T2,T3 and an input file on T1 that will produce 34 runs. One option is to put 17 runs on each of T2 and T3. We could then merge this result this result on to T1 obtaining one tape with 17 runs. The problem is that since all the runs are on one tape, we must now put some of these runs on T2 to perform another merge. The logical way to do this is to copy the first eight runs from T1 onto T2 and then perform the merge. This has the effect of adding an extra half pass for every pas we do. An alternative method is to split the original 34 runs unevenly. For example out of 34 runs ,we put 21 runs on T2 and 13 runs on T3. We could then merge 13 runs onto T1 before T3 was empty. At this point we could rewind T1 and T3 and merge T1 with 13 runs, and T2 which has 8 runs on to T3. We could then merge 8 runs until T2 was empty which would leave 5 runs left on T1 and 8 runs on T3. We could then merge T1 and T3 and so on. This type process will be continued until a tape contains all N elements in sorted Order. We can extend this process to a k-way merge in which case we need k+1 tapes. 2-way merge with 3 tapes and total 34 runs in polyphase merge. Run Construction 0 21 13 After T3+T2 13 8 0 After T1+T2 5 0 8 After T1+T3 0 5 3 After T2+T3 3 2 0 After T1+T2 1 0 2 After T1+T3 0 1 1 After T2+T3 1 0 0

T1 T2 T3

10

You might also like