CS200: Tables and Priority Queues

Similar documents
4/3/13. Outline. Part 7. Tables and Priority Queues. Value Oriented Data Structures. Example: Table of Student Points. Table ADT.

Outline. Part 7. Tables and Priority Queues. Example: Table of Student Points. Value Oriented Data Structures. Table ADT. Search Keys 4/2/12

ADT Priority Queue. Heaps. A Heap Implementation of the ADT Priority Queue. Heapsort

Tables and Priority Queues

Tables and Priority Queues!

CS200: Balanced Search Trees

CS165: Priority Queues, Heaps

Tables, Priority Queues, Heaps

CS200 Midterm 2 Fall 2007

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

PROGRAMMING II ABSTRACT DATA TYPES Unassessed Coursework 4: Binary Search Trees and Heaps

Tables and Priority Queues

Heaps & Priority Queues. (Walls & Mirrors - Remainder of Chapter 11)

Heaps. Heaps Priority Queue Revisit HeapSort

Data Structures and Algorithms

CS302 - Data Structures using C++

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Algorithms and Data Structures

CMSC 341 Lecture 14: Priority Queues, Heaps

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

CS350: Data Structures Heaps and Priority Queues

CSE 214 Computer Science II Heaps and Priority Queues

Programming II (CS300)

FINAL EXAM REVIEW CS 200 RECITATIOIN 14

Priority Queues Heaps Heapsort

Priority Queue. How to implement a Priority Queue? queue. priority queue

9. Heap : Priority Queue

" Which data Structure to extend to create a heap? " Two binary tree extensions are needed in a heap. n it is a binary tree (a complete one)

COMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first

Heaps. Heaps. A heap is a complete binary tree.

Binary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Programming II (CS300)

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

Data Structure Lecture#12: Binary Trees 3 (Chapter 5) U Kang Seoul National University

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

Priority Queues Heaps Heapsort

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

Trees 1: introduction to Binary Trees & Heaps. trees 1

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Appendix A and B are also worth reading.

Heaps and Priority Queues

CH 8. HEAPS AND PRIORITY QUEUES

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

CH. 8 PRIORITY QUEUES AND HEAPS

Heaps. 2/13/2006 Heaps 1

Binary Trees, Binary Search Trees

CS200: Trees. Rosen Ch & 11.3 Prichard Ch. 11. CS200 - Trees

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

CS200 Final Exam Fall 2007

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Binary Heaps. CSE 373 Data Structures Lecture 11

Chapter 9. Priority Queue

- Alan Perlis. Topic 24 Heaps

CSC 321: Data Structures. Fall 2016

CSC 321: Data Structures. Fall 2017

CS102 Binary Search Trees

Programming II (CS300)

Heaps Goodrich, Tamassia. Heaps 1

PRIORITY QUEUES AND HEAPS

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CS 10: Problem solving via Object Oriented Programming. Priori9zing 2

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

CSE 373: Data Structures and Algorithms

CS 2412 Data Structures. Chapter 7 Heaps

Module 2: Priority Queues

Sorting and Searching

Algorithms and Data Structures

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

Stores a collection of elements each with an associated key value

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

CSCI 104 Binary Trees / Priority Queues / Heaps. Mark Redekopp Michael Crowley

Sorting and Searching

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

COMP : Trees. COMP20012 Trees 219

Chapter 5. Binary Trees

Priority Queues and Heaps. Heaps of fun, for everyone!

Lecture Notes on Priority Queues

Lecture 7. Transform-and-Conquer

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

CS 310: Priority Queues and Binary Heaps

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Transcription:

Value Oriented Data Structures CS200: Tables and Priority Queues Value-oriented operations are very common: q Find John Smith s facebook q Retrieve the student transcript of Ruth Mui q Register Jack Smith for an Amazon Cloud account q Add a user to a database (e.g., Netflix database). Prichard Ch. 12 To support such uses: Arrange the data to facilitate search/insertion/deletion of an item given its search key CS200 - Tables and Priority Queues 1 CS200 - Tables and Priority Queues 2 Example: Table of Student Points Student ID Student First Name Student Last Name Exam 1 Exam 2 00124 Jake Glen 7 89 001247 Parastoo Mahgreb 87 78 0012 Wayne Dwyer 90 9 01234 Bob Harding 4 0 0223 Mary Laing 9 97 Search Keys In many applications the search key must be unique to a single record Records should be organized to facilitate search for an item by search key The search key of a record must not change while it is in the table. Why? Each row is a record Each column is a field Student ID is the search key field CS200 - Tables and Priority Queues 3 CS200 - Tables and Priority Queues 4 1

Table Records public abstract class KeyedItem<KT extends!!comparable <? super KT>>! //KT is constrained to be a type that implements comparable or is a! //subclass of a type which does so! {!!private KT searchkey;!!!public KeyedItem(KT key) {!!!searchkey = key; } //constructor!!!public KT getkey() {!!!return searchkey; } //accessor! }! There is no method to set the search key. Why? Table Record Example public class User extends KeyedItem<String> {!!private String StudentID; // search key!!private String firstname;!!private String lastname;!!public User(String userid, String _firstname, ) {!!!super(userid);//why is super used here?!!!firstname = _firstname;!!}//constructor! CS200 - Tables and Priority Queues CS200 - Tables and Priority Queues Table Interface public interface TableInterface<T extends KeyedItem<KT>,! KT extends Comparable <? super KT>> {!!// Precondition for all operations:!!// No two items of the table have the same search key.!!// The table's items are sorted by search key! // (actually not required)!!public boolean tableisempty();!!// Determines whether a table is empty.!!// Postcondition: Returns true if the table is empty;!!// false otherwise!!public int tablelength();!!// Determines the length of a table.!!// Postcondition: Returns number of items in the table.! Table Interface (cont.) public void tableinsert(t newitem) throws TableException;!!// Inserts a record into a table in its proper sorted!!// order according to the item's search key.!!// Precondition: The record s (newitem s) search key!!// must be unique in the table.!!// Postcondition: If successful, newitem is in its!!// proper order in table.!!// Otherwise, throw TableException.!! public boolean tabledelete(kt searchkey);!!// Deletes a record with search key KT from table.!!// Precondition: searchkey is the search key of item!!// Postcondition: If there is a record with KT in the!!// table, the item was deleted and method returns!!// true. Otherwise, table is unchanged; return false.! CS200 - Tables and Priority Queues 7 CS200 - Tables and Priority Queues 8 2

Table Interface (cont.) Reusing Data Structures public KeyedItem tableretrieve(kt searchkey);!!// Retrieves a record with a search key KT from table.!!// Precondition: searchkey is search key for record to!!// be retrieved.!!// Postcondition: If the retrieval was successful,!!// table record with search key matching KT is returned.!!// If no such record exists, return null.!! } // end TableInterface! CS200 - Tables and Priority Queues 9 Performance of Table Implementations Possible Implementations n Array sorted by search key n Linked List sorted by search key n Binary search tree CS200 - Tables and Priority Queues Search Add Remove array- O(log n) based O(n) O(n) Unsorted array-based O(n) O(1) O(n) BST* O(log n) [O(n)] O(log n) [O(n)] O(log n) [O(n)] * Worst case behavior in [] CS200 - Tables and Priority Queues 11 CS200 - Tables and Priority Queues 12 3

Priority Queues Characteristics q Items are associated with a value: priority q Provide access to one element at a time - the one with the highest priority Uses q Operating systems q Network management Real time traffic usually gets highest priority when bandwidth is limited Priority Queue ADT Operations 1. Create an empty priority queue createpqueue()! 2. Determine whether empty pqisempty():boolean! 3. Insert new item pqinsert(in newitem:pqitemtype) throws PQueueException! 4. Retrieve and delete the item with the highest priority pqdelete():pqitemtype! CS200 - Tables and Priority Queues 13 CS200 - Tables and Priority Queues 14 PQ Array Implementation 30 size 3 20 9 9.8 9 99.2 0 1 29 MAX_QUEUE-1 ArrayList ordered by priority q Find the correct position for the insertion q Shift the array elements to make room for the new item PQ Reference-based Implementation pqhead Reference-based implementation q in descending order 99.2 9 9.8 3 Highest priority value is at the beginning of the linked list pqdelete returns the item that pshead references and changes pqhead to reference the next item. pqinsert must traverse the list to find the correct position for insertion. CS200 - Tables and Priority Queues 1 CS200 - Tables and Priority Queues 1 4

PQ BST Implementation 3 20 9 9.8 9 99.2 Binary search tree q What is the highest value of the nodes? q Removing is easy It has at most one child q You can use a balanced variation of the binary search tree. Other options? Heap - Definition A maximum heap (maxheap) is a complete binary tree that satisfies the following: q It is an empty tree or it has the heap property: Its root contains a key greater or equal to the keys of its children Its left and right subtrees are also heaps Implications of the heap property: q The root holds the maximum value (global property) q Values in descending order on every path from root to leaf Heap is NOT a binary search tree. CS200 - Tables and Priority Queues 17 CS200 - Tables and Priority Queues 18 Examples 20 0 1 2 Satisfies heap property Complete 30 2 20 1 Satisfies heap property Not complete 1 30 20 2 Does not satisfy heap property Not complete CS200 - Tables and Priority Queues 19 Heap ADT createheap() // create empty heap! heapisempty():boolean! // determines if empty! heapinsert(in newitem:heapitemtype) throws HeapException! // inserts newitem based on its search key.! // Throws exception if heap is full! heapdelete():heapitemtype! // retrieves and then deletes heap s root! // item which has largest search key!! CS200 - Tables and Priority Queues 20

ArrayList Implementation ArrayList Implementation 2 4 0 20 1 1 3 2 1 2 3 4 0 20 2 1 2 1 Root: position 0 0 3 Left child of root: Right child of root: position 4 120 position 2 2 1 1 2 3 4 0 20 2 1 CS200 - Tables and Priority Queues 21 CS200 - Tables and Priority Queues 22 ArrayList Implementation Traversal items: q Root at position 0 q Left child of position i at position 2i+1 q Right child of position i at position 2(i+1) q Parent of position i at position (i-1)/2 Heap Operations - heapinsert! Step 1: put a new value into first open position (maintaining completeness) Step 2: percolate values up q Re-enforcing the heap property q Swap with parent until in the right place CS200 - Tables and Priority Queues 23 CS200 - Tables and Priority Queues 24

Insertion into a heap (Insert 1) Insertion into a heap (Insert 1) 9 1 9 3 2 1 Insert 1 Trickle up Trickle up 3 2 CS200 - Tables and Priority Queues 2 CS200 - Tables and Priority Queues 2 Heap Insert Pseudocode // insert newitem into bottom of tree! items[size] = newitem! // percolate new item up to appropriate spot! place = size! parent = (place-1)/2! while (parent >= 0 and items[place] > items[parent]) {!!swap items[place] and items[parent]!!place = parent!!parent = (place-1)/2! }! increment size! Part of the insert operation is often called siftup Heap operations heapdelete! Step 1: always remove value at root (Why?) Step 2: substitute with rightmost leaf of bottom level (Why?) Step 3: percolate/sift down q Swap with maximum child as necessary CS200 - Tables and Priority Queues 27 CS200 - Tables and Priority Queues 28 7

Deletion from a heap Deletion from a heap 9 9 3 2 3 2 Delete Place last node in root Trickle down CS200 - Tables and Priority Queues 29 CS200 - Tables and Priority Queues 30 heapdelete Pseudocode // return the item in root! rootitem = items[0]! //copy item from last node into root! items[0] = items[size-1]! size--! // restore the heap property! heaprebuild(items, 0, size)!! return rootitem! heaprebuild Pseudocode heaprebuild(inout items:arraytype, in root:integer, in size:integer)!!if (root is not a leaf) {!!!child = 2 * root + 1 // left child!!!if (root has right child) {!!!!rightchild = child + 1!!!!if (items[rightchild].getkey() > items[child].getkey()) {!!!!!child = rightchild! }! } // larger child!!!if (items[root].getkey() < items[child].getkey()) {!!!!swap items[root] and items[child]!!!!heaprebuild(items, child, size)!!}! }! heaprebuild is also called siftdown CS200 - Tables and Priority Queues 31 CS200 - Tables and Priority Queues 32 8

Array-based Heaps: Complexity Heap versus BST for PriorityQueue Average Worst Case BST can also be used to implement a priority queue insert delete O(log n) O(log n) O(log n) O(log n) How does worst case complexity compare? How does average case complexity compare? What if you know the maximum needed size for the PriorityQueue? CS200 - Tables and Priority Queues 33 CS200 - Tables and Priority Queues 34 Small number of priorities A heap of queues with a queue for each priority value. HeapSort Algorithm q Insert all elements (one at a time) to a heap q Iteratively delete them Removes minimum/maximum value at each step Computational complexity? CS200 - Tables and Priority Queues 3 CS200 - Tables and Priority Queues 3 9

Repeat n times of insert operation? 3 HeapSort Alternative method (in-place): q Create a heap out of the input array: Consider the input array as a complete binary tree Create a heap by iteratively expanding the portion of the tree that is a heap q Start from the leaves, which are semi-heaps q Move up to next level calling heaprebuild with each parent 9 2 q Iteratively swap the root item with last item in unsorted portion and rebuild Question : Can we reduce the number of operations? CS200 - Tables and Priority Queues 37 CS200 - Tables and Priority Queues 38 Build initial tree Begin with the root Left to right down this tree 3 9 2 1 0 Transform tree into a heap for (index = n -1 down to 0)! //Assertion: the tree rooted at index is a semiheap! heaprebuild(anarray, index x)! //Assertion: the tree rooted at index is a heap! 3 9 2 Question : To build this initial heap, how many operations are needed? A. 3 CS200 - Tables and Priority Queues B. 4 C. D. 39 Call heaprebuild() on the leaves from right to left Move up the tree CS200 - Tables and Priority Queues 40

3 3 9 2 9 2 3 9 2 3 9 2 CS200 - Tables and Priority Queues 41 CS200 - Tables and Priority Queues 42 3 9 9 2 3 2 9 3 2 9 3 2 CS200 - Tables and Priority Queues 43 CS200 - Tables and Priority Queues 44 11

Do we need n steps? 9 3 3 2 9 2 9 3 2 No Child No Child No Child CS200 - Tables and Priority Queues 4 CS200 - Tables and Priority Queues 4 After transforming the array into a heap : n-1 (~) Heapsort partitions the array into two regions q Heap region 9 3 2 q sorted region HEAP (Largest elements in array) HEAP 9 3 2 0 1 2 3 last last+1 n - 1 HEAP 9 3 2 HEAP 9 3 2 CS200 - Tables and Priority Queues 47 CS200 - Tables and Priority Queues 48 12

: n-2 (4~) : n-3 (3~) 9 3 2 9 3 2 9 3 2 2 3 9 HEAP 9 3 2 HEAP 2 3 9 2 3 9 HEAP 3 2 9 CS200 - Tables and Priority Queues 49 CS200 - Tables and Priority Queues 0 : n-4 (2~) : n- (1~) 9 3 2 9 3 2 HEAP 3 2 9 HEAP 3 2 9 HEAP 3 2 9 HEAP 3 2 9 HEAP 2 3 9 HEAP 2 3 9 CS200 - Tables and Priority Queues 1 CS200 - Tables and Priority Queues 2 13

:n- (0~) 9 3 2 HEAP 2 3 9 2 3 9 HeapSort Pseudocode heapsort(ouritems:arraylist, n:integer)!!// First step: build heap out of the input array!!for (index = n - 1 down to 0) {!!!// Invariant: the tree rooted at index is a!!!// semiheap!!!// semiheap: tree where the subtrees of the!!!// root are heaps!!!heaprebuild(ouritems, index, n)!!!// The tree rooted at index is a heap!!}!!! Final result 2 3 9 CS200 - Tables and Priority Queues 3 CS200 - Tables and Priority Queues 4 HeapSort Pseudocode heapsort(ouritems:arraylist, n:integer)!!for (index = n-1 down to 0) {!!!heaprebuild(ouritems, index, n)!!}!!last = n -1 // initialize the regions!!for (step = 1 through n) {!!!swap ouritems[0] and ouritems[last]!!!decrement last!!!heaprebuild(ouritems, 0, last) }! CS200 - Tables and Priority Queues 14