Skew Heaps

35
Priority Queues (Skew Heaps) Application

Transcript of Skew Heaps

Page 1: Skew Heaps

Priority Queues (Skew Heaps) Application

Page 2: Skew Heaps

A queue is a pile in which items are added an one end and removed from the other. In this respect, a queue is like the line of customers waiting to be served by a bank teller. As customers arrive, they join the end of the queue while the teller serves the customer at the head of the queue. As a result, a queue is used when a sequence of activities must be done on a first-come, first-served basis.

Basic Concepts

Page 3: Skew Heaps

For example, consider the software which manages a printer. In general, it is possible for users to submit documents for printing much more quickly than it is possible to print them. A simple solution is to place the documents in a FIFO queue. In a sense this is fair, because the documents are printed on a first-come, first-served basis.

Basic Concepts

Page 4: Skew Heaps

Basic Concepts

However, a user who has submitted a short document for printing will experience a long delay when much longer documents are already in the queue. An alternative solution is to use a priority queue in which the shorter a document, the higher its priority. By printing the shortest documents first, we reduce the level of frustration experienced by the users. In fact, it can be shown that printing documents in order of their length minimizes the average time a user waits for her document.

Page 5: Skew Heaps

Basic Concepts

A priority queue is essentially a list of items in which each item has associated with it a priority. In general, different items may have different priorities and we speak of one item having a higher priority than another. Given such a list we can determine which is the highest (or the lowest) priority item in the list. Items are inserted into a priority queue in any, arbitrary order. However, items are withdrawn from a priority queue in order of their priorities starting with the highest priority item first.

Page 6: Skew Heaps

Priority queues are often used in the implementation of algorithms. Typically the problem to be solved consists of a number of subtasks and the solution strategy involves prioritizing the subtasks and then performing those subtasks in the order of their priorities.

Basic Concepts

Page 7: Skew Heaps

Applications of Priority QueuesCPU Scheduling

Page 8: Skew Heaps

The SJF algorithm can be either preemptive or non-preemptive. The choice arises when a new process arrives at the ready queue while a previous process is still executing. The next CPU burst of the newly arrived process may be shorter than what is left of the currently executing process. A preemptive SJF algorithm will preempt the currently executing process, whereas a non-preemptive SJFalgorithm will allow the currently running process to finish its CPU burst. Preemptive SJF scheduling is sometimes called shortest-remaining-time-first scheduling.

Applications of Priority QueuesCPU Scheduling

Page 9: Skew Heaps

Shortest-Job-First (SJF) Scheduling Associate with each process the length of its next CPU

burst. Use these lengths to schedule the process with the shortest time

Two schemes: nonpreemptive – once CPU given to the process it

cannot be preempted until completes its CPU burst preemptive – if a new process arrives with CPU burst

length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF)

SJF is optimal – gives minimum average waiting time for a given set of processes

Applications of Priority Queues CPU Scheduling

Page 10: Skew Heaps

Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

SJF (non-preemptive)

Average waiting time = (0 + 6 + 3 + 7)/4 = 4

Example of Non-Preemptive SJF

P1 P3 P2

73 160

P4

8 12

Applications of Priority QueuesCPU Scheduling

Page 11: Skew Heaps

Example of Preemptive SJFProcess Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

SJF (preemptive)

Average waiting time = (9 + 1 + 0 +2)/4 = 3

P1 P3P2

42 110

P4

5 7

P2 P1

16

Applications of Priority QueuesCPU Scheduling

Page 12: Skew Heaps

The SJF algorithm is a special case of the general priority scheduling algorithm. A priority is associated with each process, and the CPU is allocated to the process with the highest priority. Equal-priority processes are scheduled in FCFS order. An SJF algorithm is simply a priority algorithm where the priority (p) is the inverse of the (predicted) next CPU burst. The larger the CPU burst, the lower the priority, and vice versa.

Applications of Priority Queues CPU Scheduling

Page 13: Skew Heaps

Priority Scheduling A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority

(smallest integer highest priority) Preemptive nonpreemptive

SJF is a priority scheduling where priority is the predicted next CPU burst time

Problem Starvation – low priority processes may never execute Solution Aging – as time progresses increase the priority of the

process

Applications of Priority Queues CPU Scheduling

Page 14: Skew Heaps

Basic Concepts

Process Arrival Time (s) Burst Time (s) Priority

P1 0.0 4 2

P2 1.0 1 1

P3 1.0 2 3

P4 2.0 8 4

P5 2.0 5 1

Priority (preemptive)

Average waiting time = 0 + (1-1) + (10-1) + (12 -2) + (2-2) /5 = 3.8 s

Priority Scheduling

P1 P5P2

21 120

P3

7 10

P1 P4

20

Page 15: Skew Heaps

Process Arrival Time (s) Burst Time (s) Priority

P1 0.0 4 2

P2 1.0 1 1

P3 1.0 2 3

P4 2.0 8 2

P5 2.0 5 1

Priority (preemptive)

Average waiting time = 0 + (1-1) + (18-1) + (10 -2) + (2-2) /5 = 5 s

P1 P5P2

21 180

P4

7 10

P1 P3

20

Page 16: Skew Heaps

This time we'll describe another structure that can be used to implement a priority queue: the heap. A heap is a kind of tree. It offers both insertion and deletion in O(logN) time. Thus it's not quite as fast for deletion, but much faster for insertion. It's the method of choice for implementing priority queues where speed is important and there will be many insertions.

Page 17: Skew Heaps

Skew Heaps

A skew heap is a self-adjusting version of a leftist heap that is incredibly simple to implement. The relationship of skew heaps to leftist heaps is analogous to the relation between splay trees and AVL trees. Skew heaps are binary trees with heap order, but there is no structural constraint on these trees. Unlike leftist heaps, no information is maintained about the null path length of any node. The right path of a skew heap can be arbitrarily long at any time, so the worst-case running time of all operations is O(n).

Page 18: Skew Heaps

As with leftist heaps, the fundamental operation on skew heaps is merging. The merge routine is once again recursive, and we perform the exact same operations as before, with one exception. The difference is that for leftist heaps, we check to see whether the left and right children satisfy the leftist heap order property and swap them if they do not. In Skew Heaps unconditionally swapping all nodes in the merge path Skew Heaps attempt to maintain a short right path from the root.

Skew Heaps

Page 19: Skew Heaps

Skew HeapsThe following diagram is a graphical representation of

a Skew Heap.

Page 20: Skew Heaps

Skew HeapsFrom the above diagram it can be seen that a Skew Heap can be “heavy” at times on the right side of the tree. Depending on the order of operations Skew Heaps can have long or short, right hand side path lengths. As the following diagram shows by inserting the element ‘45’ into the above heap a short right hand side is achieved:

Page 21: Skew Heaps

Skew Heaps

Skew Heaps are of interest as they do not maintain any balancing information but still can achieve amortized log n time in the Union Operation.The following operations can be executed on Skew Heaps:

1. MakeHeap (Element e)2. FindMin (Heap h)3. DeleteMin (Heap h)4. Insert (Heap h, Element e)5. Union (Heap h1, Heap h2)

Algorithm

Page 22: Skew Heaps

Skew Heaps

The only difference between a skew heap and a leftist heap is the merging operation is treated differently in skew heaps. The swapping of the children of a visited node on the right path is performed unconditionally; the dist value is not maintained.

The purpose of the swapping is to keep the length of the right path bounded, even though the length of the right path can grow to Omega (n), it is quite effective.

Page 23: Skew Heaps

Skew Heaps

The reasoning behind this is that insertions are made on the right side and therefore creating a “heavy” right side. Then by swapping everything unconditionally a relatively “light” right side is created. So, the good behavior of skew heaps is due to always inserting to the right and unconditionally swapping all nodes.

Page 24: Skew Heaps

MakeHeap (Element e)return new Heap(e);

FindMin(Heap h)if (h == null)return null;elsereturn h.key

Skew Heaps

Page 25: Skew Heaps

Skew Heaps

DeleteMin(Heap h)Element e = h.key;h = Union (h.left, h.right);return e;

Insert (Heap h, Element e)z = MakeHeap(e);h = Union (h, z);

Page 26: Skew Heaps

Union (Heap h1, heap h2)Heap dummy;if (h1 == null)return h2;12else if (h2 == null)return h1;else{// Assure that the key of h1 is smallestif (h1.key > h2.key){Node dummy = h1;h1 = h2;h2 = dummy;

Skew Heaps

Page 27: Skew Heaps

}}if (h1.right == null) // Hook h2 directly to h1h1.right = h2;else // Union recursivelyh1.right = Union (h1.right, h2);// Swap children of h1dummy = h1.right;h1.right = h1.left;h1.left = dummy;return h1;

Skew Heaps

Page 28: Skew Heaps

· Skew heaps have heap order but no structuralconstraint. Self-adjusting and no need to maintain the path lengths, thus simple to implement.

· O(N), but for M consecutive operations, O(M lgN)

· recursively merge the heap with larger root with theright subheap of the smaller root, and always swap

· slight loss of balance information is compensated bythe lack of testing

· a leftist heap is a skew heap, but not vice versa

Skew Heaps

Page 29: Skew Heaps

Skew Heaps

Page 30: Skew Heaps
Page 31: Skew Heaps
Page 32: Skew Heaps
Page 33: Skew Heaps
Page 34: Skew Heaps
Page 35: Skew Heaps