1 / 25

CSC 172 DATA STRUCTURES

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.

cshain
Télécharger la présentation

CSC 172 DATA STRUCTURES

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 6 3 4 1 3 2 1

  7. 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

  8. 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

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

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

  11. 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

  12. Insert in Heap Array Put new element in A[n+1] Add 1 to n 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 Take A[1] Replace it by A[n] Subtract 1 from n 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 Insert {1,8,2,7,3,6,4} Deletemax Deletemax Insert {5,9} Deletemax

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

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

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

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

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

  22. 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

  23. Big-Oh of Heapify … 3 2 1 0 … n/16 n/8 n/4 n/2

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

  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)

More Related