840 likes | 1.04k Views
HEAPS & PRIORITY QUEUES. Array and Tree implementations. Priority queue. A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element is the first one removed.
E N D
HEAPS & PRIORITY QUEUES Array and Tree implementations
Priority queue • A stack is first in, last out • A queue is first in, first out • A priority queue is least-first-out • The “smallest” element is the first one removed
The definition of “smallest” is up to the programmer (for example, • you might define it by implementing Comparator or Comparable)
Priority queue • If there are several “smallest” elements, • the implementer must decide which to remove first • Remove any “smallest” element (don’t care which) • Remove the first one added
A priority queue is an: • ADT where items are removed based on their priority • (highest-to-lowest), regardless of the order they ARE inserted in the structure
A priority queue ADT • PriorityQueue(): Methods • void add(Comparable o): inserts o into the priority queue • Comparable removeLeast(): removes and returns the least element • Comparable getLeast(): returns (but does not remove) the least element • booleanisEmpty():returns true iff empty • int size(): returns the number of elements • void clear():discards all elements
Evaluating implementations • When we choose a data structure, • it is important to look at usage patterns • If we load an array once and do thousands of searches on it, • we want to make searching fast— • so we would probably sort the array
When we choose a data structure, • it is important to look at usage patterns • If we load a huge array and expect to do only a few searches, • we probably don’t want to spend time sorting the array
Evaluating implementations • For almost all uses of a queue (including a priority queue), • we eventually remove everything that we add
TIMING • Hence, • when we analyze a priority queue, • neither “add” nor “remove” is more important— • we need to look at the timing for (“add + remove”)
Array implementations • A priority queue could be implemented as an unsorted array (with a count of elements) • Adding an element would take O(1) time (why?) • Removing an element would take O(n) time (why?) • Hence, (adding and removing) an element takes O(n) time • This is an inefficient representation – a BigO(N)
Array implementations • A priority queue could be implemented as a sortedarray (again, with a counter of elements) • Adding an element would take O(n) time (why?) • Removing an element would take O(1) time (why?) • So adding and removing) an element takes • O(n) time • Again, this is very inefficient
Linked list implementations A priority queue could be implemented as an • unsorted linked list • Adding an element would take O(1)time (why?) • Removing an element would take (N) time (why?) • Again, inefficient!
SORTED LINKED LIST • A priority queue could be implemented as a sorted linked list • Adding an element would take O(n) time (why?) • Removing an element would take O(1)time (why?) • Again, an inefficient algorithm
Binary tree implementations • A priority queue could be represented as a • balanced binary search tree • Insertion and removal could destroy the balance • We would need an algorithm to rebalance the binary tree • Good rebalancing algorithms require O(log n) time, but are complicated
Heap Implementaton • The concepts of heaps is used to refer to memory in a computer, • E.g. the part of the memory that provides dynamic memory allocation ( get it as you need it) • “Heap” is also used as a name for a special kind of binary tree
Heap is a complete binary tree • A heap is a complete binary tree with values stored in its nodes such that no child has a value bigger than the value of its parent – (though it can be equal)
Therefore we would like to find a better algorithm. We want to: • Insert and Remove with a Big O of one
HEAPS & Priority Queues • A binary tree represented as a heap facilitates the operation to find the maximum element: “it is always in the root of the tree “ • Because finding the node with the maximum value in a heap is a BigO(I) • A HEAP is the most efficient structure for implementing priority queues
Heaps A heap is a certain kind of complete binary tree. Root When a complete binary tree is built, its first node must be the root.
Heaps Complete binary tree. Right child Left child The Second and third Nodes are always the left and right child of the root.
Heaps Complete binary tree. The next nodes always fill the next level from left-to-right.
Heaps Complete binary tree.
Heaps A heap is a certain kind of complete binary tree. 45 35 23 27 21 22 4 19 Each node in a heap contains a key that can be compared to other nodes' keys.
A node has the heap property if it is Equal to or greater than as its children OR if it is smaller than or equal to its children (since smaller numbers represent higher priorities) 3 12 8 12 8 3 Priority queue: Yellow node has the heap property - less than its children Heap: Yellow node has the heap property To implement a heap as a priority queue
Heaps A heap is a certain kind of complete binary tree. This example is a max heap. 45 35 23 27 21 22 4 19 The "heap property" requires that each node's key is >= to the keys of its children
Priority Queues • A heap can easily implement a priority queue • but what happens with we remove the element of highest priority? • How do we re-arrange the tree?
Removing from a heap • There are two things we need to consider when rearranging: • We want to end up with a heap which means that • The tree has to be complete – • ALL HEAPS ARE COMPLETE TREES!
To remove an element at top of tree: • Remove the element at location 0 • Move the element in the lastIndexto location 0, decrement lastIndex (lastIndex is at the end of the heap) • Reheap the new root node (the one now at location 0) • This is called down-heap bubbling or percolating down • Down-heap bubblingrequires O(log n) time to remove an element
Removing the Top of a Heap(max heap) • Move the last node onto the root. 45 42 23 35 21 22 4 19 27
Removing the Top of a Heap(max heap) • Move the value in the last node into the root. 27 42 23 35 21 22 4 19
Removing the Top of a Heap • Move the last node onto the root. • Push the out-of-place node downward, • swapping with its larger child • until the new node reaches an acceptable location. 27 42 23 35 21 22 4 19
Removing the Top of a Heap • Move the last node onto the root. • Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 42 27 23 35 21 22 4 19
Removing the Top of a Heap • Move the last node onto the root. • Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. • Note that we discard half of the tree with each move downward 42 35 23 27 21 22 4 19
reheapificationdownward. • We stop when: • The children all have keys <= to the out-of-place node, or • The node reaches the leaf. • The process of pushing the node downward is called: reheapificationdownward. 42 35 23 27 21 22 4 19
Priority Queues • Another Example : • How to delete a node from a Priority Queue • Since it is queue - first in first out, • we remove the first one The Root
23 12 16 7 9 13 5 3 2 1 An Example of a MAX HeapThe root has largest value
12 16 7 9 13 5 3 2 1 Deleting a node from the heap • We use a heap to implement a priority queue • the "next" element ( the root ) in the queue was removed (pop operation). • We end up with a root with no value Root has no value
Deleting a node from the heap The obvious question is: which node can we use to replace this one? 12 16 7 9 13 5 3 2 1
Deleting a node from the heap 12 16 7 9 13 5 If we want this tree to stay complete the rightmost element in the bottommost level is the obvious choice 3 2 1
Deleting a node from the heap We now have a problem: We havea complete tree but this is not a heap! WHY??? This can be solved byapplying systematicswap of nodes. 2 12 16 7 9 13 5 3 1
Deleting a node from the heap 2 12 16 7 9 13 5 We systematically swap the root with the larger of the children nodes until no more swaps can be done 3 1
Deleting a node from the heap 16 12 2 7 9 13 5 3 1
Deleting a node from the heap 16 12 13 7 9 2 5 3 1
Deleting a node from the heap 16 12 13 7 9 2 5 3 1 The tree has restored its heap property!
Insert operation • Another example - the heap below • Recall that while delete causes pairwise swaps from the root to the bottom, • insert causes pairwise swaps from the bottom to the top. • Suppose that we want to insert an element with value 20 22 17 12 11 3 13 5 4 1 6
insert (20) 22 12 17 11 3 13 5 4 1 20 6
insert (20) 22 12 17 11 20 13 5 4 1 3 6
insert (20) 22 20 17 12 11 13 5 4 1 3 6
insert (20) 22 20 17 11 12 13 5 4 1 3 6