Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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:

1 CSC 172 DATA STRUCTURES

2 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

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

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

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 9 63 4132 1

7 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

8 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

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

10 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

11 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

12 Insert in Heap Array 1. Put new element in A[n+1] 2. Add 1 to n 3. Bubble up

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

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

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

16 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

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

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

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

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

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

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

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

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

25 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


Ads by Google