CS S-08 Priority Queues Heaps 1

Similar documents
Data Structures and Algorithms

Data Structures and Algorithms

Properties of a heap (represented by an array A)

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

CMSC 341 Lecture 14: Priority Queues, Heaps

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

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

Binary Trees, Binary Search Trees

Maintain binary search tree property nodes to the left are less than the current node, nodes to the right are greater

Sorting and Searching

9. Heap : Priority Queue

8. Binary Search Tree

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

Sorting and Searching

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

Module 2: Priority Queues

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

Module 2: Priority Queues

Data Structures and Algorithms

Section 4 SOLUTION: AVL Trees & B-Trees

Lecture 6: Analysis of Algorithms (CS )

Heapsort. Heap data structure

Trees. (Trees) Data Structures and Programming Spring / 28

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

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

The Heap Data Structure

Balanced Binary Search Trees. Victor Gao

CS350: Data Structures Heaps and Priority Queues

Priority Queues, Heaps, and Heapsort

Recall: Properties of B-Trees

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Priority Queues (Heaps)

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

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

Heaps, Heapsort, Priority Queues

Heapsort. Algorithms.

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

Priority Queues and Binary Heaps

CS 240 Data Structures and Data Management. Module 2: Priority Queues

CS 240 Data Structures and Data Management. Module 2: Priority Queues

CS 380 ALGORITHM DESIGN AND ANALYSIS

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

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

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

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

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

BM267 - Introduction to Data Structures

Binary Search Trees Treesort

CS165: Priority Queues, Heaps

Algorithms and Data Structures

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

CS 171: Introduction to Computer Science II. Binary Search Trees

CSE 214 Computer Science II Heaps and Priority Queues

The beautiful binary heap.

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

Lecture: Analysis of Algorithms (CS )

A red-black tree is a balanced binary search tree with the following properties:

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

Data Structures Question Bank Multiple Choice

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

CSC 421: Algorithm Design Analysis. Spring 2013

CS61B Lecture #23. Today: Priority queues (Data Structures 6.4, 6.5) Range queries ( 6.2) Java utilities: SortedSet, Map, etc.

Heaps. 2/13/2006 Heaps 1

Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods

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

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

CS2 Algorithms and Data Structures Note 6

CS61B Lecture #23. Today: Priority queues (Data Structures 6.4, 6.5) Range queries ( 6.2) Java utilities: SortedSet, Map, etc.

CMSC 341. Binary Heaps. Priority Queues

Heaps Goodrich, Tamassia. Heaps 1

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

Module 2: Priority Queues

Priority Queues, Binary Heaps, and Heapsort

A set of nodes (or vertices) with a single starting point

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Binary Heaps. CSE 373 Data Structures Lecture 11

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

Stacks, Queues, and Priority Queues. Inf 2B: Heaps and Priority Queues. The PriorityQueue ADT

Priority queues. Priority queues. Priority queue operations

Lecture 5: Sorting Part A

Priority Queues Heaps Heapsort

Heaps. A heap is a certain kind of complete binary tree.

Binary Heaps in Dynamic Arrays

Heaps. (our first advanced data structure)

Algorithms and Data Structures

Section 1: True / False (1 point each, 15 pts total)

CS2 Algorithms and Data Structures Note 6

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

Programming II (CS300)

Binary heaps (chapters ) Leftist heaps

Arrays aren t going to work. What can we do? Use pointers Copy a large section of a heap, with a single pointer assignment

Heapsort. Why study Heapsort?

CS301 - Data Structures Glossary By

Topic: Heaps and priority queues

CSCI2100B Data Structures Heaps

Heap: A binary heap is a complete binary tree in which each, node other than root is smaller than its parent. Heap example: Fig 1. NPTEL IIT Guwahati

Transcription:

CS245-2015S-08 Priority Queues Heaps 1 08-0: Priority Queue ADT Keys are priorities, with smaller keys having a better priority 08-1: Priority Queue ADT Sorted Array Add Element Remove Smallest Key 08-2: Priority Queue ADT Sorted Array Add Element Remove Smallest Key O(n) O(1) (using circular array) 08-3: Priority Queue ADT Binary Search Tree Add Element Remove Smallest Key 08-4: Priority Queue ADT

CS245-2015S-08 Priority Queues Heaps 2 Binary Search Tree Add Element O(lgn) Remove Smallest Key O(lg n) If the tree is balanced 08-5: Priority Queue ADT Binary Search Tree Add Element Remove Smallest Key O(n) O(n) Computer Scientists are Pessimists 08-6: Heap Definition (Murphy was right) Complete Binary Tree Heap Property For every subtree in a tree, each value in the subtree is value stored at the root of the subtree 08-7: Heap Examples 1 2 4 7 3 14 15 9 8 5 4 Valid Heap 08-8: Heap Examples

CS245-2015S-08 Priority Queues Heaps 3 1 8 5 2 9 4 14 5 7 10 13 08-9: Heap Insert Invalid Heap What is the only place we can insert an element in a heap, and maintain the complete binary tree property? 08-10: Heap Insert What is the only place we can insert an element in a heap, and maintain the complete binary tree property? End of the tree as a child of the shallowest leaf that is farthest to the left Will the resulting tree still be a heap? 08-11: Heap Insert What is the only place we can insert an element in a heap, and maintain the complete binary tree property? End of the tree as a child of the shallowest leaf that is farthest to the left Inserting an element at the end of the heap may break the heap property Swap the value up the tree (examples) 08-12: Heap Insert Running time for Insert? 08-13: Heap Insert Running time for Insert? Place element at end of tree: O(1) (We ll see a clever way to find the end of the tree in a bit) Swap element up the tree: O(height of tree) (Worst case, swap all the way up to the root) Height of a Complete Binary Tree withnnodes? 08-14: Heap Insert Running time for Insert? Place element at end of tree: O(1) (We ll see a clever way to find the end of the tree in a bit Swap element up the tree: O(height of tree) (Worst case, swap all the way up to the root)

CS245-2015S-08 Priority Queues Heaps 4 Height of a Complete Binary Tree withnnodes = Θ(lgn) Total running time: Θ(lgn) in the worst case 08-15: Heap Remove Smallest Finding the smallest element is easy at the root of the tree Removing the Root of the heap is hard What element is easy to remove? How could this help us? 08-16: Heap Remove Smallest Finding the smallest element is easy at the root of the tree Removing the Root of the heap is hard Removing the element at the end of the heap is easy Copy last element of heap into root Remove the last element Problem? 08-17: Heap Remove Smallest Finding the smallest element is easy at the root of the tree Removing the Root of the heap is hard Removing the element at the end of the heap is easy Copy last element of heap into root Remove the last element May break the heap property 08-18: Heap Remove Smallest Finding the smallest element is easy at the root of the tree Removing the Root of the heap is hard Removing the element at the end of the heap is easy Copy last element of heap into root Remove the last element Push the root down, until heap property is satisfied 08-19: Heap Remove Smallest Running time for remove smallest? 08-20: Heap Remove Smallest Running time for remove smallest?

CS245-2015S-08 Priority Queues Heaps 5 Copy last element into root, remove last element: O(1), given a O(1) time method to find the last element Push the root down: O(height of the tree) (Worst case, push element all the way down) As before, Complete Binary Tree with n elements has heightθ(lgn) Total time: Θ(lgn) in the worst case 08-21: Representing Heaps Represent heaps using pointers, much like BSTs Need to add parent pointers for insert to work correctly Need to maintain a pointer to the location to insert the next element (this could be hard to update & maintain) Space needed to store pointers 3 per node could be greater than the space need to store the data in the heap! Memory allocation and deallocation is slow There is a better way! 08-22: Representing Heaps A Complete Binary Tree can be stored in an array: 1 08-23: CBTs as Arrays The root is stored at index 1 For the node stored at index i: 2 14 5 3 16 15 7 6 8 9 1 2 14 5 3 16 15 7 6 8 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Left child is stored at index2 i Right child is stored at index 2 i + 1 Parent is stored at index i/2 08-24: CBTs as Arrays Finding the parent of a node int parent(int n) { return (n / 2); }

CS245-2015S-08 Priority Queues Heaps 6 Finding the left child of a node int leftchild(int n) { return 2 * n; } Finding the right child of a node int rightchild(int n) { return 2 * n + 1; } 08-25: Building a Heap Build a heap out ofnelements 08-26: Building a Heap Build a heap out ofnelements Start with an empty heap Doninsertions into the heap MinHeap H = new MinHeap(); for(i=0 < i<a.size(); i++) H.insert(A[i]); Running time? 08-27: Building a Heap Build a heap out ofnelements Start with an empty heap Doninsertions into the heap MinHeap H = new MinHeap(); for(i=0 < i<a.size(); i++) H.insert(A[i]); Running time? O(n lg n) is this bound tight? 08-28: Building a Heap Total time: c 1 + n i=1 c 2lgi Running Time: Θ(nlgn) 08-29: Building a Heap Build a heap from the bottom up c 1 + n c 2 lgi i=1 n i=n/2 n i=n/2 c 2 lgi c 2 lg(n/2) = (n/2)c 2 lg(n/2) = (n/2)c 2 ((lgn) 1) Ω(nlgn)

CS245-2015S-08 Priority Queues Heaps 7 Place elements into a heap array Each leaf is a legal heap First potential problem is at location i/2 08-30: Building a Heap Build a heap from the bottom up Place elements into a heap array Each leaf is a legal heap First potential problem is at location i/2 for(i=n/2; i>=0; i--) pushdown(i); 08-31: Building a Heap How many swaps, worst case? If everypushdown has to swap all the way to a leaf: n/4 elements 1 swap n/8 elements 2 swaps n/16 elements 3 swaps n/32 elements 4 swaps... Total # of swaps: 08-32: Decreasing a Key n/4+2n/8+3n/16+4n/32+...+(lgn)n/n Given a specific element in a heap, how can we decrease the key of that element, and maintain the heap property? 08-33: Decreasing a Key Given a specific element in a heap, how can we decrease the key of that element, and maintain the heap property? Push the element up the tree, just like after an insert 08-34: Decreasing a Key Decrease the key of a specific element in a heap: Decrease the key value Push the element up the tree, just like after an insert Time required? 08-35: Decreasing a Key Decrease the key of a specific element in a heap:

CS245-2015S-08 Priority Queues Heaps 8 Decrease the key value Push the element up the tree, just like after an insert Time required: Θ(lgn), in the worst case. 08-36: Removing an Element Given a specific element in a heap, how can we remove that element, and maintain the heap property? 08-37: Removing an Element Given a specific element in a heap, how can we remove that element, and maintain the heap property? Decrease key to a value<root Remove smallest element 08-38: Removing an Element Given a specific element in a heap, how can we remove that element, and maintain the heap property? Decrease key to a value<root. TimeΘ(lgn) worst case Remove smallest element. TimeΘ(lgn) worst case 08-39: Java Specifics When inserting an element, push value up until it reaches the root, or it s its parent Our while statement will have two tests We can insert a sentinel value at index 0, guaranteed to be any element in the heap Now our while loop only requires a single test