Download presentation

Presentation is loading. Please wait.

Published byRemington Blye Modified about 1 year ago

1
Priority Queues CSC 172 SPRING 2002 LECTURE 16

2
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

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

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

5
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

6
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

7
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

8
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

9
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 A node represented by A[k] has parent A[k/2]

10
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 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]

11
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

12
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

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

14
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)

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

16
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)

17
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 O(log n) each == O(n log n)

18
Big-Oh of Heapify Bubble down n/2,n/2-1,n/2-2,…,1 in order

19
Big-Oh of Heapify Bubble down n/2,n/2-1,n/2-2,…,1 in order

20
Big-Oh of Heapify Bubble down n/2,n/2-1,n/2-2,…,1 in order

21
Big-Oh of Heapify Bubble down n/2,n/2-1,n/2-2,…,1 in order

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

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

24
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)

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

26
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

27
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

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

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

30
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

31
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

32
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

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

34
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

35
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

36
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 O(log n) each == O(n log n)

37
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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google