250 likes | 275 Views
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.
E N D
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 6 3 4 1 3 2 1
Insertion on a Balanced POT Place new element in the leftmost possible place “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 + log2n levels
Deletemax on Balanced POT Select element at root Replace root element by rightmost element at lowest level 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 log2n
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 6 3 4 1 3 2 1 9 6 3 4 1 3 2 1 Children of A[k] are: A[2*k] A[2*k+1] A node represented by A[k] has parent A[k/2]
Bubble up in Heap Array At position k: If k == 1, then done 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 If 2k > n, then done 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 Put new element in A[n+1] Add 1 to n 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 Take A[1] Replace it by A[n] Subtract 1 from n 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 Insert {1,8,2,7,3,6,4} Deletemax Deletemax Insert {5,9} Deletemax
Make a Heap Simplest idea: insert N items: O(n) average, O(nlogn) worst case. 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) 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 7 6 3 6 5 4 1 2 3 5 4 2 3 2 9 7 6 4 3 5 6 3 2 1 2 4 2 3 5
Big-Oh of Heapify Bubble down n/2, n/2-1, n/2-2,…,1 in order 9 7 6 3 6 5 4 1 2 3 5 4 2 3 2 9 7 6 4 3 5 6 3 2 1 2 4 2 3 5
Big-Oh of Heapify Bubble down n/2, n/2-1, n/2-2,…,1 in order 9 7 6 3 6 5 4 1 2 3 5 4 2 3 2 9 7 6 4 3 5 6 3 2 1 2 4 2 3 5
Big-Oh of Heapify Bubble down n/2, n/2-1, n/2-2,…,1 in order 9 7 6 3 6 5 4 1 2 3 5 4 2 3 2 9 7 6 4 3 5 6 3 2 1 2 4 2 3 5
Big-Oh of Heapify Max. number of swaps at each level At most two each No swaps At most one each 9 7 6 4 3 5 6 3 2 1 2 4 2 3 5
Big-Oh of Heapify … 3 2 1 0 … n/16 n/8 n/4 n/2
Big-Oh of Heapify … 3 2 1 0 … n/16 n/8 n/4 n/2 < (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)