Programming II (CS300)

Similar documents
Programming II (CS300)

Programming II (CS300)

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Heaps Goodrich, Tamassia. Heaps 1

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

CH 8. HEAPS AND PRIORITY QUEUES

Stores a collection of elements each with an associated key value

CH. 8 PRIORITY QUEUES AND HEAPS

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

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

Priority Queues and Binary Heaps

Heaps. 2/13/2006 Heaps 1

Sorting and Searching

Sorting and Searching

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

Data Structures and Algorithms

CS350: Data Structures Heaps and Priority Queues

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

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

CSE 2123: Collections: Priority Queues. Jeremy Morris

Binary Trees

Priority queues. Priority queues. Priority queue operations

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

Data Structures and Algorithms

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

Priority queues. Priority queues. Priority queue operations

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

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

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

Trees. A tree is a directed graph with the property

Algorithms and Data Structures

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

9. Heap : Priority Queue

Binary Trees, Binary Search Trees

Heaps and Priority Queues

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

Name CPTR246 Spring '17 (100 total points) Exam 3

Priority Queues and Heaps. Heaps and Priority Queues 1

COMP : Trees. COMP20012 Trees 219

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

Priority Queues Heaps Heapsort

Elementary Data Structures 2

Algorithms and Data Structures

The priority is indicated by a number, the lower the number - the higher the priority.

CE 221 Data Structures and Algorithms

CSCI2100B Data Structures Heaps

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

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

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Data Structures Lecture 7

Priority Queues Heaps Heapsort

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Priority Queues & Heaps. CS16: Introduction to Data Structures & Algorithms Spring 2019

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

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Data Structures Lesson 9

a graph is a data structure made up of nodes in graph theory the links are normally called edges

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

Using a Heap to Implement a Priority Queue

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

CSE 230 Intermediate Programming in C and C++ Binary Tree

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

CS165: Priority Queues, Heaps

Height of a Heap. Heaps. 1. Insertion into a Heap. Heaps and Priority Queues. The insertion algorithm consists of three steps

Priority Queues & Heaps. Chapter 9

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

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Heaps and Priority Queues

CS2 Algorithms and Data Structures Note 6

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

Binary Heaps in Dynamic Arrays

Chapter 20: Binary Trees

Recall: Properties of B-Trees

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

Associate Professor Dr. Raed Ibraheem Hamed

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

CS 10: Problem solving via Object Oriented Programming Winter 2017

Properties of a heap (represented by an array A)

CSE 214 Computer Science II Heaps and Priority Queues

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

Balanced Binary Search Trees. Victor Gao

Sorting and Selection

Chapter 2: Basic Data Structures

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

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

INF2220: algorithms and data structures Series 1

Priority Queues. Outline. COMP9024: Data Structures and Algorithms. Priority Queue ADT. Total Order Relations. Entry ADT

Data Structures and Algorithms for Engineers

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

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

Binary Search Trees Treesort

Priority Queues, Binary Heaps, and Heapsort

Analysis of Algorithms

Section 4 SOLUTION: AVL Trees & B-Trees

CMSC 341 Lecture 14: Priority Queues, Heaps

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Round 3: Trees. Tommi Junttila. Aalto University School of Science Department of Computer Science. CS-A1140 Data Structures and Algorithms Autumn 2017

PRIORITY QUEUES AND HEAPS

Transcription:

1 Programming II (CS300) Chapter 12: Heaps and Priority Queues MOUNA KACEM mouna@cs.wisc.edu Fall 2018

Heaps and Priority Queues 2 Priority Queues Heaps

Priority Queue 3 QueueADT Objects are added and removed according to the First-In, First-Out (FIFO) principle PriorityQueueADT A collection of prioritized elements that allows Arbitrary element insertion of a new element, and Removal of the element having highest priority The priority of an element added to a priority-queue is designated by a key The element with minimal value of key will have the highest priority

Priority-Queue Examples 4

Priority Queue 5 public interface PriorityQueueADT<T extends Comparable<T>>{ public void insert(t newobject); public T removebest(); // removes and returns the element with the highest priority } public T peekbest(); // returns without removing the element with the highest priority public boolean isempty();

Java.lang.Comparable Interface 6 Interface providing a mean for defining comparison between two objects Comparable interface Includes a single method: compareto a.compareto(b) must return an integer i such that i < 0 designated that a < b i = 0 designated that a = b i > 0 designated that a > b for more details about the interface Comparable, please visit https://docs.oracle.com/javase/8/docs/api/java/lang/comparable.html

Implementation Strategies for a Priority-Queue 7 Implementing a priorityqueue with an Unsorted LinkedList head tail (18, 3) (15, 1) (45, 0) (10, 2) Method insert removebest peekbest isempty Running Time O(1) O(n) O(n) O(1)

Implementation Strategies for a Priority-Queue 8 Implementing a priorityqueue with a Sorted LinkedList head tail (45, 0) (15, 1) (10, 2) (18, 3) Method insert removebest peekbest isempty Running Time O(n) O(1) O(1) O(1)

Implementation Strategies for a Priority-Queue 9 Using Unsorted LinkedList Using Sorted LinkedList Method Running Time Method Running Time insert O(1) insert O(n) removebest O(n) removebest O(1) peekbest O(n) peekbest O(1) isempty O(1) isempty O(1)

Heaps and Priority Queues 10 Priority Queues Heaps

Heaps 11 Binary Heap is a binary tree that should satisfy these two constraints Structural constraint A Heap is a complete binary tree All levels of a complete binary tree are completely filled except possibly the last level and the last level has all keys as left as possible

Heaps 12 Structural Constraint (complete binary tree) Non-Heap Heap Non-Heap Heap Heap Non-Heap

Heaps 13 Binary Heap is a binary tree that should satisfy these two constraints Structural constraint A Heap is a complete binary tree Value-relationship constraint A Heap must satisfy the heap ordering property. There are two ordering properties: min-heap property: the value of each node is greater than or equal to the value of its parent, with the minimum-value element at the root. max-heap property: the value of each node is less than or equal to the value of its parent, with the maximum-value element at the root.

Heaps 14 Value-Relationship Constraint Max-Heap Min-Heap Max-Heap Max-Heap Min-Heap

Non-Heaps 15

Binary Tree Traversal Algorithms 16 Binary tree traversals: breadth-first traversal or level-order traversal Visit nodes by levels from top to bottom and from left to right depth-first traversal PreOrder traversal visit the parent first and then left and right children InOrder traversal visit the left child, then the parent and the right child PostOrder traversal visit left child, then the right child and then the parent

Binary-Tree Level-order Traversal 17 Level 0 Level 1 Level 2 Level 3 Level-order traversal: F, B, G, A, D, I, C, E, H

Array-Representation of a Heap 18

Implementing a Priority-Queue using a min-heap 19 public interface PriorityQueueADT<T extends Comparable<T>>{ public void insert(t newobject); // insert an element to the priority Queue public T peekbest(); // returns without removing the element with the highest priority public boolean isempty(); // checks whether the priority Queue is empty or not } public T removebest(); // removes and returns the element with the highest priority

Implementing a Priority-Queue using a min-heap 20 Adding an element to the Heap (insert operation) Michael T. Goofrich, et al., "Data Structures & Algorithms", Wiley, six edition, 2014, pp. 360-384.

Implementing a Priority-Queue using a min-heap 21 Adding an element to the Heap (insert operation) 1. First Step: Maintain the complete binary tree property If the heap is empty or the bottom level is already full Place the new node at the leftmost position of a new level

Implementing a Priority-Queue using a min-heap 22 Adding an element to the Heap (insert operation) 1. First Step: Maintain the complete binary tree property If the heap is not empty and the bottom level is not already full Place the new node just beyond the rightmost node at the bottom level of the heap

Implementing a Priority-Queue using a min-heap 23 Adding an element to the Heap (insert operation) 1. First Step: Maintain the complete binary tree property 2. Second Step: Restore the heap-order property (Up-Heap Bubbling) After the first step, the tree representing the heap is complete. But, it may violate the heap-order property

Implementing a Priority-Queue using a min-heap 24 Adding an element to the Heap (insert operation) 1. Second Step: Restore the heap-order property (Up-Heap Bubbling) After the first step, the tree representing the heap is complete. But, it may violate the heap-order property if the priority queue was empty before the insertion Nothing to do

Implementing a Priority-Queue using a min-heap 25 Adding an element to the Heap (insert operation) 1. Second Step: Restore the heap-order property (Up-Heap Bubbling) if the priority queue was not empty before the insertion Suppose that The new node is inserted at the position p of the priority queue. (p cannot be the position of the root as the priority queue was not empty before the insertion operation) The new inserted node s parent is located at the position q of the priority queue We compare the key (aka priority) at position p to that of p s parent at position q

Implementing a Priority-Queue using a min-heap 26 Adding an element to the Heap (insert operation) 1. Second Step: Restore the heap-order property (Up-Heap Bubbling) if the priority queue was not empty before the insertion We compare the key (aka priority) at position p to that of p s parent at position q if key k p k q, the heap-order property is satisfied and the algorithm terminates if key k p < k q, then restore the heap-order property swap the entries stored at positions p and q Again, the heap-order property may be violated, so repeat the process: Going up in the binary tree until no violation of the heap-order property occurs

Implementing a Priority-Queue using a min-heap 27 Adding an element to the Heap (insert operation) (a) (b)

Implementing a Priority-Queue using a min-heap 28 Adding an element to the Heap (insert operation) (c) (d)

Implementing a Priority-Queue using a min-heap 29 Adding an element to the Heap (insert operation) (e) (f)

Implementing a Priority-Queue using a min-heap 30 Adding an element to the Heap (insert operation) (g)

Implementing a Priority-Queue using a min-heap 31 Adding an element to the Heap (insert operation) The upward movement of the newly inserted entry by means of swaps is conventionally called up-heap bubbling or percolate up A swap either resolves the violation of the heap-order property or propagates it one level up in the heap. In the worst case, upheap bubbling causes the new entry to move all the way up to the root of heap. In the worst case, the number of swaps performed in the execution of method insert is equal to the height of the heap (O(log(n))) The insert operation using a heap is of time complexity: O(log(n)) where n represents the number of elements in the heap (i.e. size of the heap)

Implementation Strategies for a Priority-Queue 32 Using Unsorted LinkedList Method Running Time insert O(1) removebest O(n) peekbest O(n) isempty O(1) Using Sorted LinkedList Method Running Time insert O(n) removebest O(1) peekbest O(1) isempty O(1) Using a Heap Method Running Time insert O(log(n)) removebest peekbest isempty Recall that the height h of a complete binary tree with n nodes is at most O(log(n)).

Implementing a Priority-Queue using a min-heap 33 Removing an element from the Heap (removebest operation) Using a min-heap, the entry with the highest priority (with the smallest key) is stored at the root r of the binary tree representing the heap Deleting the root node would leave two disconnected subtrees

Implementing a Priority-Queue using a min-heap 34 Removing an element to the Heap (removebest operation) Using a min-heap, the entry with the highest priority (with the smallest key) is stored at the root r of the binary tree representing the heap Deleting the root node would leave two disconnected subtrees

Implementing a Priority-Queue using a min-heap 35 Removing an element to the Heap (removebest operation) Using a min-heap, the entry with the highest priority (with the smallest key) is stored at the root r of the binary tree representing the heap Deleting the root node would leave two disconnected subtrees Solution: Delete the leaf at the last position p of the tree and copy its value to the root r Then, perform a Heap-Down Bubbling or percolate down process after the removal

Implementing a Priority-Queue using a min-heap 36 Removing an element to the Heap (removebest operation)

Implementing a Priority-Queue using a min-heap 37 Removing an element to the Heap (removebest operation)

Implementing a Priority-Queue using a min-heap 38 Removing an element to the Heap (removebest operation)

Implementing a Priority-Queue using a min-heap 39 Removing an element to the Heap (removebest operation)

Implementation Strategies for a Priority-Queue 40 Using Unsorted LinkedList Method Running Time insert O(1) removebest O(n) peekbest O(n) isempty O(1) Using Sorted LinkedList Method Running Time insert O(n) removebest O(1) peekbest O(1) isempty O(1) Using a Heap Method Running Time insert O(log(n)) removebest O(log(n)) peekbest isempty

Implementation Strategies for a Priority-Queue 41 Using Unsorted LinkedList Method Running Time insert O(1) removebest O(n) peekbest O(n) isempty O(1) Using Sorted LinkedList Method Running Time insert O(n) removebest O(1) peekbest O(1) isempty O(1) Using a Heap Method Running Time insert O(log(n)) removebest O(log(n)) peekbest O(1) isempty O(1)

Array-Based implementation of a Heap 42

Array-Based implementation of a Heap 43 The array-based representation of a binary tree is suitable for a complete binary tree. The elements of the tree are stored in an array T such that the element at position p is stored in T with index equal to the level number f (p) of p, defined as follows: If p is the root, then f (p) = 0. If p is the left child of position q, then f (p) = 2 f (q)+1. If p is the right child of position q, then f (p) = 2 f (q)+2. For a tree with of size n, the elements have contiguous indices in the range [0,n 1] The last position of is always at index n 1.