1 / 101

ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΡΧΕΙΩΝ

ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΡΧΕΙΩΝ. Priority Queues ( ουρές προτεραιότητας) HEAPS ( σωροί) – binary heaps HeapSort (ταξινόμηση σωρού). Priority Queues. A priority queue is an ADT where: Each element has an associated priority Efficient extraction of the highest-priority element is supported.

bsclafani
Télécharger la présentation

ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΡΧΕΙΩΝ

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. ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΡΧΕΙΩΝ Priority Queues (ουρές προτεραιότητας) HEAPS (σωροί) – binary heaps HeapSort (ταξινόμηση σωρού)

  2. Priority Queues • A priority queue is an ADT where: • Each element has an associated priority • Efficient extraction of the highest-priority element is supported. • Typical operations of a priority queue are: • Enqueue (insert an element) • Dequeue (remove the highest-priority element) • Max (return the highest-priority element) • Meld (join two priority queues into one) • Implementation : Binary Heap

  3. Priority Queues: Applications • Scheduling Examples: • Printer scheduling. • Jobs sent to a printer by faculty have a higher priority than jobs sent by students • Process scheduling. • Several processes require CPU time. Priority may be based on various factors: • Shortest jobs have higher priority • Jobs with a shorter remaining time have higher priority • Jobs that have waited the longest have higher priority • etc.

  4. Priority Queues: Applications • Discrete-event simulations • Simulations of systems modeled as collections of entities that interact with one another through events that occur at discrete times. • The priority assigned to an event is its start time. • Examples: • Truck fleet model • Consider the event "Truck leaves city A" with priority 10:00am. Its execution (dequeue) triggers events "Truck arrives at depot" with priority 6:00pm, and "Truck has unloaded" with priority 7:00pm, which are then enqueued, based on their priorities. • Circuit validation

  5. Example • How many aircraft are there in the airat peak traffic? • How would you represent all theaircraft? – Arrays – Linked Lists – Trees

  6. Characteristics of Flights • Created and removed dynamically – Need to add and remove aircraft in realtime • Different aircraft have differentpriorities – Different weights associated with differentaircraft • Length of time aircraft has been flying • Emergencies • Special aircraft (military/dignitaries)

  7. Array Based Representation • Removing Aircraft • Removing takes 1 step, but copying takes n • •Sorting Aircraft • – Insertion Sort-> n2-> 25 * 106operations • – Merge Sort-> n log n -> 20 * 103operations

  8. A heap is a data structure used to implement an efficient priority queue. The idea is to make it efficient to extract the element with the highest priority ­ the next item in the queue to be processed. We could use a sorted linked list, with O(1) operations to remove the highest priority node and O(N) to insert a node. Using a tree structure will involve both operations being O(log2N) which is faster. Heaps and priority queues

  9. Tree Based Representation

  10. Perfect Binary Tree • For binary tree with height h • All nodes at levels h–1 or less have 2 children (full) h = 1 h = 2 h = 3 h = 4

  11. Complete Binary Trees • For binary tree with height h • All nodes at levels h–2 or less have 2 children (full) • All leaves on level h are as far left as possible h = 1 h = 3 h = 2

  12. Complete Binary Trees h = 4

  13. Complete Trees • Nodes can be numbered in level-by-level order: The left child of the i-th node is the node 2*i and the right child is the node 2*i + 1 The parent of the i-th node is the node i / 2

  14. Complete Trees (cont’d) • It is convenient to store a complete binary tree in an array (or an ArrayList) in the order of nodes

  15. Heaps • A complete binary tree is a full binary tree that has all leaves at the same depth • Is the tree below complete? • Heaps are nearlycomplete binary trees

  16. Heaps A heap is defined only on “nearly complete binary tress. What is a “nearly complete binary tree”? A complete binary tree is one where all the internalnodes have exactly 2 children, and all the leaves are on the same level. An internal node comprises all the nodes except the leaf nodes. Leaf nodes are nodes without children.  Interior node Edge   Leaf node

  17. Heaps Height of a node: The number of edges starting at that node, and going down to the furthest leaf. Height of the heap: The maximum number of edges from the root to a leaf.  Root Height of root = 3 Height of blue node = 1

  18. 1 1 2 2 3 3 4 5 4 5 6 7 6 7 8 9 10 11 12 13 8 9 10 11 12 Heaps Nearly complete – if not complete, then the only unfilled level is filled in from left to right. Parent(i) return i/2 Parent(i) return 2i + 1 Left(i) return 2i

  19. Heaps • Two key properties • Complete binary tree • Value at node • Smaller than or equal to values in subtrees (min-heaps) • Example heap • X  Y • X  Z X Y Z

  20. Heap & Non-heap Examples 5 5 8 5 45 6 45 5 2 2 6 22 6 22 6 22 25 8 45 25 8 45 25 Heaps Non-heaps

  21. Heap Properties • Key operations • Insert ( X ) • getSmallest ( ) • Key applications • Heapsort • Priority queue

  22. Heap Operations – Insert( X ) • Algorithm • Add X to end of tree • While (X < parent) Swap X with parent // Xbubbles up tree • Complexity • # of swaps proportional to height of tree • O( log(n) )

  23. Heap Insert Example • Insert ( 20 ) 10 10 10 30 25 20 25 30 25 37 37 37 20 30 1) Insert to end of tree 2) Compare to parent, swap if parent key larger 3) Insert complete

  24. Heap Insert Example • Insert ( 8 ) 10 8 10 10 30 25 10 25 30 25 8 25 37 37 37 8 37 30 30 1) Insert to end of tree 2) Compare to parent, swap if parent key larger 3) Insert complete

  25. Heap Operation – getSmallest() • Algorithm • Get (remove) smallest node at root • Replace root with X at end of tree • While ( X > child ) Swap X with smallest child // X drops down tree • Return smallest node • Complexity • # swaps proportional to height of tree • O( log(n) )

  26. Heap GetSmallest Example • getSmallest () 30 10 8 25 10 25 30 25 10 37 37 37 30 1) Replace root with end of tree 2) Compare node to children, if larger swap with smallest child 3) Repeat swap if needed

  27. Heap GetSmallest Example • getSmallest () 8 37 10 10 25 10 10 25 37 25 30 25 30 30 30 37 37 1) Replace root with end of tree 2) Compare node to children, if larger swap with smallest child 3) Repeat swap if needed

  28. Binary Heaps • Binary heap = • a binary tree that is • complete • every level except possibly the bottom one is completely filled and the leaves in the bottom level are as far left as possible • satisfies the heap property: • the key stored in every node is greater than or equal to the keys stored in its children • this is called a max-heap. If the key at each node is smaller than or equal to the keys of its children, then we have a min-heap.

  29. Heaps • The heap property of a tree is a condition that must be true for the tree to be considered a heap. • Min-heap property: for min-heaps, requires A[parent(i)]  A[i] So, the root of any subtree holds the least value in that subtree. • Max-heap property: for max-heaps, requires A[parent(i)]  A[i] The root of any subtree holds the greatest value in the subtree.

  30. 1 9 2 8 3 8 4 8 5 7 6 8 7 1 8 6 9 7 10 5 11 8 12 9 Heaps - example • Looks like a max heap, but max-heap property is violated at indices 11 and 12. Why? because those nodes’ parents have larger smaller values.

  31. Heaps • The algorithm for removeMin uses the reheap-down procedure • The algorithm for add uses the reheap-up procedure • Both adding and removing an item takes time proportional to the height of the heap (e.g., 20 for 1,000,000 nodes) Remove the root and place the last leaf at the root. Starting at the root, swap the node with its smaller child, as many times as needed to repair the heap. Add a leaf. Starting at the last leaf, swap the node with its parent as many times as needed to repair the heap.

  32. The removeMin Algorithm Step 1: the root is removed Step 2: the last leaf replaces the root Step 3: “reheap down”: the new root value moves down, swapping places with the smaller child

  33. The add Algorithm Step 1: the new value is added as the last leaf Step 2: “reheap up”: the new value moves up, swapping places with its parent

  34. Binary Tree -> Array representation • Use Breadth-First-Search • – The root node is A(1) • – Node i is A(i)

  35. Heaps Assume we are given a tree represented by matrix A, and such that i  length[A], and the trees rooted at Left(i) and Right(i) are heaps. Then, to create a heap rooted at A[i], use procedure Max-Heapify(A,i): Max-Heapify(A,i) 1. l  Left(i) ) * l=left* 2. r  Right(i) 3. if l  heap-size[A] and A[l] > A[i] 4. then largest  l 5. else largest  i 6. if r  heap-size[A] and A[r] > A[largest] 7. then largest  r 8. if largest  i 9. then swap( A[i], A[largest]) 10. Max-Heapify(A, largest)

  36. Heaps • So, what must we do to build a heap? We call Max-Heapify(A,i) for every i starting at last node and going to the root. • Why top-down? • Because Max-Heapify() moves the larger node upward into the root. If we start at the top and go to larger node indices, the highest a node can move is to the root of that subtree. But what if there is a violation between the subtree’s root and its parent? • So, we must start from the bottom and go towards the root to fix the problems caused by moving larger nodes into the root of subtrees.

  37. 20 20 20 20 10 20 19 19 20 10 19 10 9 9 9 9 9 9 19 17 10 19 19 10 18 18 18 18 18 18 6 6 6 6 6 6 7 7 7 7 7 7 17 17 17 10 17 17 16 16 16 16 16 16 17 17 17 17 17 17 16 16 16 16 16 16 1 1 1 1 1 1 Max-Heapify(A,1)

  38. 16 14 10 8 7 9 3 2 4 1 Heaps • In practice, heaps are usually implemented as arrays: A = 16 14 10 8 7 9 3 2 4 1 =

  39. 16 14 10 8 7 9 3 2 4 1 Heaps • To represent a complete binary tree as an array: • The root node is A[1] • Node i is A[i] • The parent of node i is A[i/2] (note: integer divide) • The left child of node i is A[2i] • The right child of node i is A[2i + 1] A = 16 14 10 8 7 9 3 2 4 1 =

  40. Referencing Heap Elements • So… Parent(i) { return i/2; } Left(i) { return 2*i; } right(i) { return 2*i + 1; }

  41. The Heap Property • Heaps also satisfy the heap property: A[Parent( i )]  A[ i] for all nodes i > 1 • In other words, the value of a node is at most the value of its parent • Where is the largest element in a heap stored?

  42. Heap Operations: Heapify() • Heapify(): maintain the heap property • Given: a node i in the heap with children l and r • Given: two subtrees rooted at l and r, assumed to be heaps • Problem: The subtree rooted at i may violate the heap property • Action: let the value of the parent node “float down” so subtree at i satisfies the heap property

  43. Heapify() Example 16 4 10 14 7 9 3 2 8 1 A = 16 4 10 14 7 9 3 2 8 1

  44. Heapify() Example 16 4 10 14 7 9 3 2 8 1 A = 16 4 10 14 7 9 3 2 8 1

  45. Heapify() Example 16 4 10 14 7 9 3 2 8 1 A = 16 4 10 14 7 9 3 2 8 1

  46. Heapify() Example 16 14 10 4 7 9 3 2 8 1 A = 16 14 10 4 7 9 3 2 8 1

  47. Heapify() Example 16 14 10 4 7 9 3 2 8 1 A = 16 14 10 4 7 9 3 2 8 1

  48. Heapify() Example 16 14 10 4 7 9 3 2 8 1 A = 16 14 10 4 7 9 3 2 8 1

  49. Heapify() Example 16 14 10 8 7 9 3 2 4 1 A = 16 14 10 8 7 9 3 2 4 1

  50. Heapify() Example 16 14 10 8 7 9 3 2 4 1 A = 16 14 10 8 7 9 3 2 4 1

More Related