# Priority Queues CSC 172 SPRING 2002 LECTURE 16. Priority Queues Model Set with priorities associate with elements Priorites are comparable by a < operator.

## Presentation on theme: "Priority Queues CSC 172 SPRING 2002 LECTURE 16. Priority Queues Model Set with priorities associate with elements Priorites are comparable by a < operator."— Presentation transcript:

Priority Queues CSC 172 SPRING 2002 LECTURE 16

Priority Queues Model Set with priorities associate with elements Priorites are comparable by a < operator Operations Insert and element with a given priority Deletemax Find and remove from the set the element with highest priority

PriorityQueue Interface public Interface PriorityQueue { int size(); boolean isEmpty(); void add(Object element); Object getMin(); Object removeMin(); }

Sorted Linked List implementation Deletemax is O(1) – take first element Insert if O(n) – on average, go halfway down the list

Partially Ordered Tree A binary tree with elements and priorities at nodes Satisfying the partially ordered tree (POT) property The priority at any node >= the priority of its children

Balanced POT A POT in which all nodes exist, down to a certain level, and for one level below that, “extra” nodes may exist, as far left as possible 9 63 4132 1

Insertion on a Balanced POT 1. Place new element in the leftmost possible place 2. Bubbleup new node by repeatedly exchanging it with its parent if it has higher priority than its parent  Restores the POT property  Maintains balanced property  O(log n) because an n-node balanced tree has no more than 1 + log 2 n levels

Deletemax on Balance POT 1. Select element at root 2. Replace root element by rightmost element at lowest level 3. Bubbledown root by repeatedly exchanging it with the larger of its children, as long as it it smaller than either  Restores POT property  Preserves balance  O(log n) because O(1) at each step along a path of length at most log 2 n

Heap Data Structure Represent a balance POT by an array A n nodes represented by A[1] through A[n] A[0] not used Array holds elements level-by-level 9 63 4132 1  96341321 A node represented by A[k] has parent A[k/2]

Alternate Heap Data Structure Represent a balance POT by an array A n nodes represented by A[0] through A[n-1] A[0] is used Array holds elements level-by-level 9 63 4132 1 96341321 A node represented by A[k] has parent A[(k-1)/2] Children of A[k] are: A[2*k+1] A[2*k+2]

Bubbleup on Heap At position k: 1. If k == 1, then done 2. Otherwise, compare A[k] with A[k/2].  If A[k] is smaller, then done  Otherwise swap and repeat at k/2

Bubbledown on Heap At positition k 1. If 2k > n, then done 2. Otherwise, see if A[k] is smaller than A[2k] or A[2k + 1] (if 2k+1 <= n)  If not, done  If so, swap with larger and repeat

Insert on heap 1. Put new element in A[n+1] 2. Add 1 to n 3. Bubbleup

Insert on heap – O(log n) 1. Put new element in A[n+1] O(1) 2. Add 1 to n O(1) 3. Bubbleup O(log n)

Deletemax on heap 1. Take A[1] 2. Replace it by A[n] 3. Subtract 1 from n 4. Bubbledown

Deletemax on heap – O(log n) 1. Take A[1] O(1) 2. Replace it by A[n] O(1) 3. Subtract 1 from n O(1) 4. Bubbledown O(log n)

Heapsort 1. Heapify array A by bubbling down A[k] for: k = n/2,n/2-1,n/2-2,…,1 in order How long does this take? Might seems like O(n log n) but really O(n) 2. Repeatedly deletemax until all are selected Priority is reverse of sorted order n deletemaxes @ O(log n) each == O(n log n)

Big-Oh of Heapify Bubble down n/2,n/2-1,n/2-2,…,1 in order 9 76 5 42 4 32 3 12 6 35  976435632124235

Big-Oh of Heapify Bubble down n/2,n/2-1,n/2-2,…,1 in order 9 76 5 42 4 32 3 12 6 35  976435632124235

Big-Oh of Heapify Bubble down n/2,n/2-1,n/2-2,…,1 in order 9 76 5 42 4 32 3 12 6 35  976435632124235

Big-Oh of Heapify Bubble down n/2,n/2-1,n/2-2,…,1 in order 9 76 5 42 4 32 3 12 6 35  976435632124235

Big-Oh of Heapify Number of swaps bound at each level  976435632124235 No swaps At most one At most two

Big-Oh of Heapify …3210 n/2 n/4n/8n/16… (n/2)(1/2+2/4+3/8+4/16…..)

Big-Oh of Heapify 1/2+2/4+3/8+4/16….. 1/2+(1/4+1/4)+(1/8+1/8+1/8) +….. 1/2+ 1/4 + 1/8 + … = 1 1/4 + 1/8 +….. = 1/2 1/8 +….. = 1/4 ….. = …  So, O(2*n/2) = O(n)

Comparators class Decreasing implements Comparator{ public int compare(Object o1, Object o2){ return ((Integer)o2).compareTo((Integer)o1); } // compare method }// class Decreasing

Comparators class ByLength implements Comparator{ public int compare(Object o1, Object o2){ // positive when o1 longer than o2 return ((String)o1).length() – ((String)o2).length(); } // compare method }// class Decreasing

Java Implementation public class Heap implements PriorityQueue{ protected Object[] heap; protected int size; protected Comparator comparator; public Heap(); // for Comparable objects public Heap(Comparator comp);// user supply

Using the Comparators Heap myHeap = new Heap(new Decreasing()); Heap heap2 = new Heap(new ByLength());

Compare method private int compare (Object e1, Object e2) { return (comparator == null ? ((Comparable)e1).compareTo(e2) : comparator.compare(e1,e2); }//method compare

Constructors public Heap() { final int DEFAULT_INITIAL_CAPACITY = 11; heap = new Object[DEFAULT_INITIAL_CAPACITY]; }// default constructor public Heap (Comparator comp) { this(); comparator = comp; }// constructor with Comparator

Add Method public void add(Object element){ if (++size == heap.length) { Object[] newHeap = new Object[2*heap.length]; System.arraycopy(heap,0,newHeap,0,size); heap = newHeap; } // if heap[size-1] = element; bubbleUp(); }// method add

bubbleUp method protected void bubbleUp() { int child = size –1, parent; Object temp; while (child >0) { parent = (child-1)/2; if (compare(heap[parent],heap[child]) <= 0) break; temp = heap[parent]; heap[parent] = heap[child]; heap[child] = temp; child = parent; }//while }method bubbleUp

getMin method public Object getMin() { if (size == 0) throw new NoSuchElementException( “Priority Queue Empty”); return heap[0]; }//method getMin

removeMin method public Object removeMin() { if (size == 0) throw new NoSuchElementException( “Priority Queue Empty”); Object minElement = heap[0]; heap[0] = heap[size –1]; heap[--size] = minElement;// helps for heapsort bubbleDown(0); return minElement; }//method removeMin

bubbleDown method public bubbleDown(int start) { int parent = start, child = 2 * parent +1; Object temp; while (child < size) { if (child < size-1 && compare(heap[child],heap[child+1])>0) child++; if (compare(heap[parent],heap[child] <= 0) break; temp = heap[child]; heap[child] = heap[parent]; heap[parent] = temp; parent = child; child = 2 * parent + 1; }// while }//method getMin

Heapsort 1. Heapify array A by bubbling down A[k] for: k = n/2,n/2-1,n/2-2,…,1 in order How long does this take? Might seems like O(n log n) but really O(n) 1. Repeatedly deletemax until all are selected Priority is reverse of sorted order n deletemaxes @ O(log n) each == O(n log n)

Heapsort public void heapSort(Object[] a) { Object temp; int length = a.length, i; heap = a; size = length; for ( i = length/2; i>=0;i--) bubbleDown(i); while (size > 0) removeMin(); for (i = 0 ; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4035052/slides/slide_37.jpg", "name": "Heapsort public void heapSort(Object[] a) { Object temp; int length = a.length, i; heap = a; size = length; for ( i = length/2; i>=0;i--) bubbleDown(i); while (size > 0) removeMin(); for (i = 0 ; i=0;i--) bubbleDown(i); while (size > 0) removeMin(); for (i = 0 ; i

Download ppt "Priority Queues CSC 172 SPRING 2002 LECTURE 16. Priority Queues Model Set with priorities associate with elements Priorites are comparable by a < operator."

Similar presentations