Chapter 21 Heaps and Priority Queues

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 11: Priority Queues and Heaps Java Software Structures: Designing.
Advertisements

Priority Queues CSC 172 SPRING 2002 LECTURE 16. Priority Queues Model Set with priorities associate with elements Priorites are comparable by a < operator.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
1 Heaps and Priority Queues Heap Definition and Operations Using Heaps –Heap sort –Priority queues Implementing heaps –With links –With arrays –Analysis.
Priority Queues. 2 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.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on MinHeap: Insert Delete Converting an array.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 10: Binary Search Trees Java Software Structures: Designing.
Trees CSCI Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Chapter 20 Binary Search Trees
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
Linked Structures - Review Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
Heaps and Heap Sorting Heaps implemented in an Array Heap Sorting.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Quotes “From each according to his ability, to each according to his needs” -- Karl Marx/Queue ADT “In America, first you get the sugar, then you get the.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
1 Binary Search Trees What are the disadvantages of using a linked list? What are the disadvantages of using an array- based list? Binary search trees.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Chapter 11 Heap.
Priority Queues and Heaps
Binary Heaps What is a Binary Heap?
Chapter 21 Heaps and Priority Queues
Bohyung Han CSE, POSTECH
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Heaps What is a Binary Heap?
Binary Heaps What is a Binary Heap?
ADT Heap data structure
Heaps and Priority Queue
Java Software Structures: John Lewis & Joseph Chase
Chapter 8 – Binary Search Tree
Binary Heaps Text Binary Heap Building a Binary Heap
Priority Queues.
Heaps and Heap Sorting Heaps implemented in an Array Heap Sorting.
Binary Tree Application Operations in Heaps
Priority Queues.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Priority Queues & Heaps
CHAPTER 11: Priority Queues and Heaps
Heaps and Priority Queues
CSE 12 – Basic Data Structures
1 Lecture 10 CS2013.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Priority Queues & Heaps
Chapter 9 The Priority Queue ADT
Priority Queue and Heap
Heaps.
Chapter 20 Binary Search Trees
CHAPTER 3: Collections—Stacks
Presentation transcript:

Chapter 21 Heaps and Priority Queues

Chapter Scope Heaps, conceptually Using heaps to solve problems Heap implementations Using heaps to implement priority queues Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Heaps A heap is a complete binary tree in which each element is less than or equal to both of its children So a heap has both structural and ordering constraints As with binary search trees, there are many possible heap configurations for a given set of elements Our definition above is really a minheap A similar definition could be made for a maxheap Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Heaps Operations on a heap: Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Java Foundations, 4th Edition, Lewis/DePasquale/Chase package jsjf; /** * HeapADT defines the interface to a Heap. * * @author Java Foundations * @version 4.0 */ public interface HeapADT<T> extends BinaryTreeADT<T> { * Adds the specified object to this heap. * @param obj the element to be added to the heap public void addElement(T obj); * Removes element with the lowest value from this heap. * @return the element with the lowest value from the heap public T removeMin(); * Returns a reference to the element with the lowest value in * this heap. * @return a reference to the element with the lowest value in the heap public T findMin(); } Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Heaps Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Heaps Two minheaps containing the same data: Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Adding a New Element To add an element to the heap, add the element as a leaf, keeping the tree complete Then, move the element up toward the root, exchanging positions with its parent, until the relationship among the elements is appropriate This will guarantee that the resulting tree will conform to the heap criteria Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Adding a New Element The initial insertion point for a new element in a heap: Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Adding a New Element Inserting an element and moving it up the tree as far as appropriate: Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Removing the Min Element Remove the root (min) and reconstruct the heap First, move the last leaf of the tree to be the new root of the tree Then, move it down the tree as needed until the relationships among the elements is appropriate In particular, compare the element to the smaller of its children and swap them if the child is smaller Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Removing the Min Element The element to replace the root is the "last leaf" in the tree: Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Removing the Min Element Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Priority Qeueus Recall that a FIFO queue removes elements in the order in which they were added A priority queue removes elements in priority order, independent of the order in which they were added Priority queues are helpful in many scheduling situations A heap is a classic mechanism for implementing priority queues Java Foundations, 4th Edition, Lewis/DePasquale/Chase

/** * PrioritizedObject represents a node in a priority queue containing a * comparable object, arrival order, and a priority value. * * @author Java Foundations * @version 4.0 */ public class PrioritizedObject<T> implements Comparable<PrioritizedObject> { private static int nextOrder = 0; private int priority; private int arrivalOrder; private T element; * Creates a new PrioritizedObject with the specified data. * @param element the element of the new priority queue node * @param priority the priority of the new queue node public PrioritizedObject(T element, int priority) this.element = element; this.priority = priority; arrivalOrder = nextOrder; nextOrder++; } Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Java Foundations, 4th Edition, Lewis/DePasquale/Chase /** * Returns the element in this node. * * @return the element contained within the node */ public T getElement() { return element; } * Returns the priority value for this node. * @return the integer priority for this node public int getPriority() return priority; * Returns the arrival order for this node. * @return the integer arrival order for this node public int getArrivalOrder() return arrivalOrder; Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Java Foundations, 4th Edition, Lewis/DePasquale/Chase /** * Returns a string representation for this node. * */ public String toString() { return (element + " " + priority + " " + arrivalOrder); } * Returns 1 if the this object has higher priority than * the given object and -1 otherwise. * @param obj the object to compare to this node * @return the result of the comparison of the given object and * this one public int compareTo(PrioritizedObject obj) int result; if (priority > obj.getPriority()) result = 1; else if (priority < obj.getPriority()) result = -1; else if (arrivalOrder > obj.getArrivalOrder()) else return result; Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Java Foundations, 4th Edition, Lewis/DePasquale/Chase import jsjf.*; /** * PriorityQueue implements a priority queue using a heap. * * @author Java Foundations * @version 4.0 */ public class PriorityQueue<T> extends ArrayHeap<PrioritizedObject<T>> { * Creates an empty priority queue. public PriorityQueue() super(); } * Adds the given element to this PriorityQueue. * @param object the element to be added to the priority queue * @param priority the integer priority of the element to be added public void addElement(T object, int priority) PrioritizedObject<T> obj = new PrioritizedObject<T>(object, priority); super.addElement(obj); Java Foundations, 4th Edition, Lewis/DePasquale/Chase

/. Removes the next highest priority element from this priority /** * Removes the next highest priority element from this priority * queue and returns a reference to it. * * @return a reference to the next highest priority element in this queue */ public T removeNext() { PrioritizedObject<T> obj = (PrioritizedObject<T>)super.removeMin(); return obj.getElement(); } Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Implementing Heaps with Links The operations on a heap require moving up the heap as well as down So we'll add a parent pointer to the HeapNode class, which is itself based on the node for a binary tree In the heap itself, we'll keep track of a pointer so that we always know where the last leaf is Java Foundations, 4th Edition, Lewis/DePasquale/Chase

package jsjf; /** * HeapNode represents a binary tree node with a parent pointer for use * in heaps. * * @author Java Foundations * @version 4.0 */ public class HeapNode<T> extends BinaryTreeNode<T> { protected HeapNode<T> parent; * Creates a new heap node with the specified data. * @param obj the data to be contained within the new heap node public HeapNode(T obj) super(obj); parent = null; } Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Java Foundations, 4th Edition, Lewis/DePasquale/Chase /** * Return the parent of this node. * * @return the parent of the node */ public HeapNode<T> getParent() { return parent; } * Sets the element stored at this node. * @param the element to be stored public void setElement(T obj) element = obj; * Sets the parent of this node. * @param node the parent of the node public void setParent(HeapNode<T> node) parent = node; Java Foundations, 4th Edition, Lewis/DePasquale/Chase

package jsjf; import jsjf. exceptions. ; / package jsjf; import jsjf.exceptions.*; /** * LinkedHeap implements a heap. * * @author Java Foundations * @version 4.0 */ public class LinkedHeap<T> extends LinkedBinaryTree<T> implements HeapADT<T> { public HeapNode<T> lastNode; public LinkedHeap() super(); } Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Java Foundations, 4th Edition, Lewis/DePasquale/Chase /** * Adds the specified element to this heap in the appropriate * position according to its key value. * * @param obj the element to be added to the heap */ public void addElement(T obj) { HeapNode<T> node = new HeapNode<T>(obj); if (root == null) root=node; else HeapNode<T> nextParent = getNextParentAdd(); if (nextParent.getLeft() == null) nextParent.setLeft(node); nextParent.setRight(node); node.setParent(nextParent); } lastNode = node; modCount++; if (size() > 1) heapifyAdd(); Java Foundations, 4th Edition, Lewis/DePasquale/Chase

/. Returns the node that will be the parent of the new node /** * Returns the node that will be the parent of the new node * * @return the node that will be the parent of the new node */ private HeapNode<T> getNextParentAdd() { HeapNode<T> result = lastNode; while ((result != root) && (result.getParent().getLeft() != result)) result = result.getParent(); if (result != root) if (result.getParent().getRight() == null) else result = (HeapNode<T>)result.getParent().getRight(); while (result.getLeft() != null) result = (HeapNode<T>)result.getLeft(); } return result; Java Foundations, 4th Edition, Lewis/DePasquale/Chase

/. Reorders this heap after adding a node /** * Reorders this heap after adding a node. */ private void heapifyAdd() { T temp; HeapNode<T> next = lastNode; temp = next.getElement(); while ((next != root) && (((Comparable)temp).compareTo(next.getParent().getElement()) < 0)) next.setElement(next.getParent().getElement()); next = next.parent; } next.setElement(temp); Java Foundations, 4th Edition, Lewis/DePasquale/Chase

/. Remove the element with the lowest value in this heap and /** * Remove the element with the lowest value in this heap and * returns a reference to it. Throws an EmptyCollectionException * if the heap is empty. * * @return the element with the lowest value in this heap * @throws EmptyCollectionException if the heap is empty */ public T removeMin() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("LinkedHeap"); T minElement = root.getElement(); if (size() == 1) root = null; lastNode = null; } Java Foundations, 4th Edition, Lewis/DePasquale/Chase

else { HeapNode<T> nextLast = getNewLastNode(); if (lastNode else { HeapNode<T> nextLast = getNewLastNode(); if (lastNode.getParent().getLeft() == lastNode) lastNode.getParent().setLeft(null); lastNode.getParent().setRight(null); ((HeapNode<T>)root).setElement(lastNode.getElement()); lastNode = nextLast; heapifyRemove(); } modCount++; return minElement; Java Foundations, 4th Edition, Lewis/DePasquale/Chase

/. Returns the node that will be the new last node after a remove /** * Returns the node that will be the new last node after a remove. * * @return the node that willbe the new last node after a remove */ private HeapNode<T> getNewLastNode() { HeapNode<T> result = lastNode; while ((result != root) && (result.getParent().getLeft() == result)) result = result.getParent(); if (result != root) result = (HeapNode<T>)result.getParent().getLeft(); while (result.getRight() != null) result = (HeapNode<T>)result.getRight(); return result; } Java Foundations, 4th Edition, Lewis/DePasquale/Chase

/. Reorders this heap after removing the root element /** * Reorders this heap after removing the root element. */ private void heapifyRemove() { T temp; HeapNode<T> node = (HeapNode<T>)root; HeapNode<T> left = (HeapNode<T>)node.getLeft(); HeapNode<T> right = (HeapNode<T>)node.getRight(); HeapNode<T> next; if ((left == null) && (right == null)) next = null; else if (right == null) next = left; else if (((Comparable)left.getElement()).compareTo(right.getElement()) < 0) else next = right; temp = node.getElement(); Java Foundations, 4th Edition, Lewis/DePasquale/Chase

while ((next. = null) && (((Comparable)next. getElement()) while ((next != null) && (((Comparable)next.getElement()).compareTo(temp) < 0)) { node.setElement(next.getElement()); node = next; left = (HeapNode<T>)node.getLeft(); right = (HeapNode<T>)node.getRight(); if ((left == null) && (right == null)) next = null; else if (right == null) next = left; else if (((Comparable)left.getElement()).compareTo(right.getElement()) < 0) else next = right; } node.setElement(temp); Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Implementing Heaps with Arrays Since a heap is a complete tree, an array-based implementation is reasonable As previously discussed, a parent element at index n will have children stored at index 2n+1 and 2n+2 of the array Conversely, for any node other than the root, the parent of the node is found at index (n-1)/2 Java Foundations, 4th Edition, Lewis/DePasquale/Chase

package jsjf; import java. util. ; import jsjf. exceptions. ; / package jsjf; import java.util.*; import jsjf.exceptions.*; /** * ArrayBinaryTree implements the BinaryTreeADT interface using an array * * @author Java Foundations * @version 4.0 */ public class ArrayBinaryTree<T> implements BinaryTreeADT<T>, Iterable<T> { private static final int DEFAULT_CAPACITY = 50; protected int count; protected T[] tree; protected int modCount; * Creates an empty binary tree. public ArrayBinaryTree() count = 0; tree = (T[]) new Object[DEFAULT_CAPACITY]; } Java Foundations, 4th Edition, Lewis/DePasquale/Chase

/. Creates a binary tree with the specified element as its root /** * Creates a binary tree with the specified element as its root. * * @param element the element which will become the root of the new tree */ public ArrayBinaryTree(T element) { count = 1; tree = (T[]) new Object[DEFAULT_CAPACITY]; tree[0] = element; } Java Foundations, 4th Edition, Lewis/DePasquale/Chase

package jsjf; import jsjf. exceptions. ; / package jsjf; import jsjf.exceptions.*; /** * ArrayHeap provides an array implementation of a minheap. * * @author Java Foundations * @version 4.0 */ public class ArrayHeap<T> extends ArrayBinaryTree<T> implements HeapADT<T> { * Creates an empty heap. public ArrayHeap() super(); } Java Foundations, 4th Edition, Lewis/DePasquale/Chase

/. Adds the specified element to this heap in the appropriate /** * Adds the specified element to this heap in the appropriate * position according to its key value. * * @param obj the element to be added to the heap */ public void addElement(T obj) { if (count == tree.length) expandCapacity(); tree[count] = obj; count++; modCount++; if (count > 1) heapifyAdd(); } Java Foundations, 4th Edition, Lewis/DePasquale/Chase

/. Reorders this heap to maintain the ordering property after /** * Reorders this heap to maintain the ordering property after * adding a node. */ private void heapifyAdd() { T temp; int next = count - 1; temp = tree[next]; while ((next != 0) && (((Comparable)temp).compareTo(tree[(next-1)/2]) < 0)) tree[next] = tree[(next-1)/2]; next = (next-1)/2; } tree[next] = temp; Java Foundations, 4th Edition, Lewis/DePasquale/Chase

/. Remove the element with the lowest value in this heap and /** * Remove the element with the lowest value in this heap and * returns a reference to it. Throws an EmptyCollectionException if * the heap is empty. * * @return a reference to the element with the lowest value in this heap * @throws EmptyCollectionException if the heap is empty */ public T removeMin() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("ArrayHeap"); T minElement = tree[0]; tree[0] = tree[count-1]; heapifyRemove(); count--; modCount--; return minElement; } Java Foundations, 4th Edition, Lewis/DePasquale/Chase

/. Reorders this heap to maintain the ordering property /** * Reorders this heap to maintain the ordering property * after the minimum element has been removed. */ private void heapifyRemove() { T temp; int node = 0; int left = 1; int right = 2; int next; if ((tree[left] == null) && (tree[right] == null)) next = count; else if (tree[right] == null) next = left; else if (((Comparable)tree[left]).compareTo(tree[right]) < 0) else next = right; temp = tree[node]; Java Foundations, 4th Edition, Lewis/DePasquale/Chase

while ((next < count) && (((Comparable)tree[next]) while ((next < count) && (((Comparable)tree[next]).compareTo(temp) < 0)) { tree[node] = tree[next]; node = next; left = 2 * node + 1; right = 2 * (node + 1); if ((tree[left] == null) && (tree[right] == null)) next = count; else if (tree[right] == null) next = left; else if (((Comparable)tree[left]).compareTo(tree[right]) < 0) else next = right; } tree[node] = temp; Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Heap Sort Given the ordering property of a heap, it is natural to think of using a heap to sort a list of numbers A heap sort sorts a set of elements by adding each one to a heap, then removing them one at a time The smallest element comes off the heap first, so the sequence will be in ascending order Java Foundations, 4th Edition, Lewis/DePasquale/Chase

Java Foundations, 4th Edition, Lewis/DePasquale/Chase package jsjf; /** * HeapSort sorts a given array of Comparable objects using a heap. * * @author Java Foundations * @version 4.0 */ public class HeapSort<T> { * Sorts the specified array using a Heap * @param data the data to be added to the heapsort public void HeapSort(T[] data) ArrayHeap<T> temp = new ArrayHeap<T>(); // copy the array into a heap for (int i = 0; i < data.length; i++) temp.addElement(data[i]); // place the sorted elements back into the array int count = 0; while (!(temp.isEmpty())) data[count] = temp.removeMin(); count++; } Java Foundations, 4th Edition, Lewis/DePasquale/Chase