# CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.

## Presentation on theme: "CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert."— Presentation transcript:

CSC 172 DATA STRUCTURES

Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert an element with a given priority Deletemax or Deletemin 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 getMax(); Object deleteMax(); }

Sorted Linked List implementation Deletemax is O(1) – take first element Insert is 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 In Weiss, the VALUE of the key is <= values of children, interpretation is up to user.

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. “Bubble up” 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 Balanced POT 1. Select element at root 2. Replace root element by rightmost element at lowest level 3. Bubble down root by repeatedly exchanging it with the larger of its children, as long as it is 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 balanced 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 12314369  A node represented by A[k] has parent A[k/2] Children of A[k] are: A[2*k] A[2*k+1]

Bubble up in Heap Array 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

Bubble down in Heap Array 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 in Heap Array 1. Put new element in A[n+1] 2. Add 1 to n 3. Bubble up

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

Deletemax in Heap Array 1. Take A[1] 2. Replace it by A[n] 3. Subtract 1 from n 4. Bubble down

Deletemax in heap array – O(log n)  Take A[1] O(1)  Replace it by A[n] O(1)  Subtract 1 from n O(1)  Bubble down O(log n)

POP QUIZ  What heap do you get if you 1. Insert {1,8,2,7,3,6,4} 2. Deletemax 3. Deletemax 4. Insert {5,9} 5. Deletemax

Make a Heap 1. Simplest idea: insert N items: O(n) average, O(nlogn) worst case. 2. 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)

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 532421236534679 

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 532421236534679 

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 532421236534679 

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 532421236534679 

Big-Oh of Heapify Max. number of swaps at each level 532421236534679  No swaps At most one each At most two each

Big-Oh of Heapify 0123… n/2 n/4n/8n/16…

Big-Oh of Heapify 0123… 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)

Download ppt "CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert."

Similar presentations