- Alan Perlis Topic 24 Heaps "You think you know when you can learn,

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
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 Binary Heaps: enqueue dequeue deleting an.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Dr. Andrew Wallace PhD BEng(hons) EurIng
1 Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
COMP 103 Priority Queues, Partially Ordered Trees and Heaps.
Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Introduction.
data ordered along paths from root to leaf
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Topic 24 Heaps "You think you know when you can learn, are more sure when you can write even more when you can teach, but certain when you can program."
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Advanced Data Structure By Kayman 21 Jan Outline Review of some data structures Array Linked List Sorted Array New stuff 3 of the most important.
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
"Teachers open the door, but you must enter by yourself. "
Partially Ordered Data ,Heap,Binary Heap
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CS 201 Data Structures and Algorithms
Priority Queues and Heaps
Heaps And Priority Queues
Programming Abstractions
Week 11 - Friday CS221.
Hashing Exercises.
Source: Muangsin / Weiss
Binary Heaps What is a Binary Heap?
March 31 – Priority Queues and the heap
Heapsort.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Cse 373 April 26th – Exam Review.
Heaps.
Binary Heaps What is a Binary Heap?
Binary Heaps What is a Binary Heap?
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Heaps & Priority Queues
Topic 19 Binary Search Trees
structures and their relationships." - Linus Torvalds
- Alan Perlis Heaps "You think you know when you can learn,
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 8 – Binary Search Tree
structures and their relationships." - Linus Torvalds
CSE 373: Data Structures and Algorithms
ITEC 2620M Introduction to Data Structures
Heaps and the Heapsort Heaps and priority queues
"Teachers open the door, but you must enter by yourself. "
CE 221 Data Structures and Algorithms
Data Structures and Analysis (COMP 410)
Ch. 12 Tables and Priority Queues
Priority Queues & Heaps
Heapsort.
Chapter 9 The Priority Queue ADT
structures and their relationships." - Linus Torvalds
Heaps.
Presentation transcript:

- Alan Perlis Topic 24 Heaps "You think you know when you can learn, are more sure when you can write even more when you can teach, but certain when you can program." - Alan Perlis

Priority Queue Recall priority queue Options? elements enqueued based on priority dequeue removes the highest priority item Options? List? Binary Search Tree? Linked List enqueue BST enqueue O(N) O(1) O(N) O(logN) O(N) O(N) O(logN) O(logN) O(1) O(logN) CS314 Heaps

Another Option A heap A complete binary tree not to be confused with the runtime heap (portion of memory for dynamically allocated variables) A complete binary tree all levels have maximum number of nodes except deepest where nodes are filled in from left to right Maintains the heap order property in a min heap the value in the root of any subtree is less than or equal to all other values in the subtree CS314 Heaps

Clicker Question 2 In a max heap with no duplicates where is the largest value? the root of the tree in the left-most node in the right-most node a node in the lowest level None of these CS314 Heaps

Example Min Heap 12 17 16 19 52 37 25 21 45 CS314 Heaps

Enqueue Operation Add new element to next open spot in array Swap with parent if new value is less than parent Continue back up the tree as long as the new value is less than new parent node CS314 Heaps

Enqueue Example Add 15 to heap (initially next left most node) 12 17 16 19 52 37 25 21 45 15 CS314 Heaps

Enqueue Example Swap 15 and 52 12 17 16 19 15 37 25 21 45 52 CS314 Heaps

Enqueue Example Swap 15 and 17, then stop 12 15 16 19 17 37 25 21 45 52 CS314 Heaps

Internal Storage Interestingly heaps are often implemented with an array instead of nodes 12 17 16 19 52 37 25 21 45 for element at position i: parent index: i / 2 left child index: i * 2 right child index: i * 2 + 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 12 17 16 19 52 37 25 21 45 CS314 Heaps

PriorityQueue Class public class PriorityQueue<E extends Comparable<E>> { private int size; private E[] con; public PriorityQueue() { heap = getArray(2); } private E[] getArray(int size) { return (E[]) (new Comparable[size]); CS314 Heaps

PriorityQueue enqueue public void enqueue(E val) { if ( size >= con.length - 1 ) enlargeArray( con.length * 2 + 1 ); size++; int indexToPlace = size; while ( indexToPlace > 1 && val.compareTo( con[indexToPlace / 2] ) < 0 ) { con[indexToPlace] = con[indexToPlace / 2]; // swap indexToPlace /= 2; // change indexToPlace to parent } con[indexToPlace] = val; private void enlargeArray(int newSize) { E[] temp = getArray(newSize); System.arraycopy(con, 1, temp, 1, size); con = temp;

Dequeue min value / front of queue is in root of tree swap value from last node to root and move down swapping with smaller child unless values is smaller than both children CS314 Heaps

Dequeue Example Swap 35 into root (save 12 to return) 12 15 16 17 23 37 25 21 45 35 CS314 Heaps

Dequeue Example Swap 35 into root (save 12 to return) 35 15 16 17 23 37 25 21 45 CS314 Heaps

Dequeue Example Swap 35 with smaller child (15) 15 35 16 17 23 37 25 21 45 CS314 Heaps

Dequeue Example Swap 35 with smaller child (17) 15 17 16 35 23 37 25 21 45 CS314 Heaps

Dequeue Example Swap 35 with smaller child (21) 15 17 16 21 23 37 25 45 CS314 Heaps

Dequeue Code public E dequeue( ) { E top = con[1]; int hole = 1; boolean done = false; while ( hole * 2 < size && ! done ) { int child = hole * 2; // see which child is smaller if ( con[child].compareTo( con[child + 1] ) > 0 ) child++; // child now points to smaller // is replacement value bigger than child? if (con[size].compareTo( con[child] ) > 0 ) { con[hole] = con[child]; hole = child; } else done = true; con[hole] = con[size]; size--; return top;

PriorityQueue Comparison Run a Stress test of PQ implemented with Heap and PQ implemented with BinarySearchTree What will result be? Heap takes half the time or less of BST Heap faster, but not twice as fast About the same BST faster, but not twice as fast BST takes half the time or less of Heap CS314 Heaps

Data Structures Data structures we have studied arrays, array based lists, linked lists, maps, sets, stacks, queue, trees, binary search trees, graphs, hash tables, red-black trees, priority queues, heaps Most program languages have some built in data structures, native or library Must be familiar with performance of data structures best learned by implementing them yourself CS314 Heaps

Data Structures We have not covered every data structure Heaps http://en.wikipedia.org/wiki/List_of_data_structures

Data Structures deque, b-trees, quad-trees, binary space partition trees, skip list, sparse list, sparse matrix, union-find data structure, Bloom filters, AVL trees, trie, 2-3-4 trees, and more! Must be able to learn new and apply new data structures CS314 Heaps