CSCI2100B Data Structures Heaps

Similar documents
Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

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

Algorithms and Data Structures

Data Structures Lesson 9

CE 221 Data Structures and Algorithms

Heap Model. specialized queue required heap (priority queue) provides at least

Priority Queues (Heaps)

CS350: Data Structures Heaps and Priority Queues

Priority Queues (Heaps)

Binary Heaps. CSE 373 Data Structures Lecture 11

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

Sorting and Searching

Priority Queues Heaps Heapsort

Sorting and Searching

Priority Queues Heaps Heapsort

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

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

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

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

Recall: Properties of B-Trees

Administration CSE 326: Data Structures

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

Priority Queues. Binary Heaps

Heaps, Heap Sort, and Priority Queues.

Data Structures and Algorithms

Figure 1: A complete binary tree.

CMSC 341 Lecture 14: Priority Queues, Heaps

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

Figure 1: A complete binary tree.

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

CS 315 April 1. Goals: Heap (Chapter 6) continued review of Algorithms for Insert DeleteMin. algoritms for decrasekey increasekey Build-heap

9. Heap : Priority Queue

UNIT III BALANCED SEARCH TREES AND INDEXING

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

CSCI2100B Data Structures Trees

COMP : Trees. COMP20012 Trees 219

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

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

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

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

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

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

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

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

Binary Heaps in Dynamic Arrays

Today s Outline. The One Page Cheat Sheet. Simplifying Recurrences. Set Notation. Set Notation. Priority Queues. O( 2 n ) O( n 3 )

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

CSE 373: Data Structures and Algorithms

Priority Queues. 04/10/03 Lecture 22 1

Binary Trees, Binary Search Trees

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

Programming II (CS300)

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

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

1 Tree Sort LECTURE 4. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

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

CH 8. HEAPS AND PRIORITY QUEUES

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

Binary heaps (chapters ) Leftist heaps

CH. 8 PRIORITY QUEUES AND HEAPS

Properties of a heap (represented by an array A)

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

Programming II (CS300)

Data Structures Week #8. Heaps (Priority Queues)

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

CMSC 341 Leftist Heaps

CS165: Priority Queues, Heaps

Chapter 6 Heapsort 1

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Nicki Dell Spring 2014

The beautiful binary heap.

Chapter 22 Splay Trees

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Priority Queues and Binary Heaps

CMSC 341 Lecture 15 Leftist Heaps

CMSC 341. Binary Heaps. Priority Queues

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Binary Trees

8. Binary Search Tree

Heaps Goodrich, Tamassia. Heaps 1

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

CMSC 341 Lecture 15 Leftist Heaps

Balanced Search Trees. CS 3110 Fall 2010

Priority Queues and Heaps

Stores a collection of elements each with an associated key value

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Topic: Heaps and priority queues

Section 4 SOLUTION: AVL Trees & B-Trees

CS711008Z Algorithm Design and Analysis

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

CS711008Z Algorithm Design and Analysis

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues. Aaron Bauer Winter 2014

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?

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

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

Heapsort. Heap data structure

CSE332: Data Abstractions Lecture 4: Priority Queues; Heaps. James Fogarty Winter 2012

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

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

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

Transcription:

CSCI2100B Data Structures Heaps Irwin King king@cse.cuhk.edu.hk http://www.cse.cuhk.edu.hk/~king Department of Computer Science & Engineering The Chinese University of Hong Kong

Introduction In some applications, a simple queue may not be the best strategy to complete jobs. Printer queue Multiprocessing queue Problems Sometimes it seems that small jobs take longer Important jobs can t be done first

Priority Queues (Heaps) Different from a simple queue where one adds an entry at the end and takes an entry at the front, A priority queue takes an entry that satisfies some special properties among all the entries and place it at the front so to be taken out first.

Example In a job queue, there are many algorithms that can be implemented to accomplish tasks. first-come-first-serve shortest-job-first longest-job-first priority-first combination of the above

Priority Queue A priority queue consists of entries, each of which contains a key called the priority of the entry. A priority queue has only two operations other than the usual creation, size, full, and empty operations: Insert--inserts an entry. Delete_Min--finds, passes back, and removes the entry having the highest priority. If entries have equal priorities, then the first entry inserted is removed first.

Model of a Priority Queue

Implementation of a Priority Queue Several possible implementations are possible. Simple linked list A sorted contiguous list An unsorted list Binary search tree

Binary Heap (or just Heap) Heaps have two properties Structure property Heap order property As with AVL trees, an operation on a heap can destroy one of the properties, so a heap operation must not terminate until all heap properties are in order.

Structure Property A heap is a binary tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right. Such a tree is known as a complete binary tree.

Example

Observation A complete binary tree of height h has between 2 h and 2 h+1-1 nodes. 2 h This implies that the height of a complete binary tree is log n, which is clearly O(log n). Because a complete binary tree is so regular, it can be represented in an array and no pointers are necessary.

Example of an Implementation For any element in array position i, the left child is in position 2i, the right child is in the cell after the left child (2i + 1), and the parent is in position i/2. Thus not only are pointers not required, but the operations required to traverse the tree are extremely simple. Problem is the estimation of the maximum heap size is required in advance.

Heap Order Property The property that allows operations to be performed quickly is the heap order property. For a heap, the smallest element should be at the root so that the operation to remove will be quick. By the heap order property, the minimum element can always be found at the root. Thus, we get the extra operation, find_min, in constant time, O(1).

Heap Order Property Since we want to be able to find the minimum quickly, it makes sense that the smallest element should be at the root. If we consider that any subtree should also be a heap, then any node should be smaller than all of its descendants. Applying this logic, we arrive at the heap order property. In a heap, for every node X, the key in the parent of X is smaller than (or equal to) the key in X, with the obvious exception of the root (which has no parent).

Example Two complete trees (only the left tree is a heap).

Heap Operations - Insert We create a hole in the next available location. If x can be placed in the hole without violating the heap order, then we do so and are done. Otherwise we slide the element that is in the hole's parent node into the hole, thus bubbling the hole up toward the root. We continue this process until x can be placed in the hole. This strategy is known as a percolate up.

Example-Insert 14

Example

Observation The time to do the insertion could be as much as O(log n) if the element to be inserted is the new minimum and is percolated all the way to the root. It has been shown that 2.607 comparisons are required on average to perform an insert. The average insert moves an element up 1.607 levels.

Heap Operations - Delete Deletions are handled in a similar manner as insertions. Finding the minimum is easy; the hard part is removing it. When the root is removed, a hole is created. We then need to slide the smaller of the hole's children into the hole, thus pushing the hole down one level.

Deletion We repeat this step until x can be placed in the hole. Thus, our action is to place x in its correct spot along a path from the root containing minimum children. The rearranging will typically take less than O(log n).

Example

Example

Example

Other Heap Operations Finding the minimum can be performed in constant time. No help in finding the maximum. There is no ordering information. Decrease_Key (P,Δ) Increase_Key(P,Δ) Remove(I) Build_Heap

Observation on Build_Heap Takes n keys and places them into an empty heap. We could perform n successive Inserts. This will take O(n) average but O(n log n) worstcase. One other way is to place the n keys into the tree in any order. Then perform Percolate_Down on half of the nodes.

Example-Initial, Percolate_Down(7)

Example-Percolate_Down(6), Percolate_Down(5)

Example-Percolate_Down(4), Percolate_Down(3)

Example-Percolate_Down(2), Percolate_Down(1)

Back to the k Selection Problem First Algorithm We now could use what we just learned and apply it to find out the k-th smallest or largest element in a set. To build a heap, it takes O(n) average and O(n log n) for worst case scenario. To delete a heap, it take O(log n). Hence, the total running time is O(n + k log n).

More For small k then the running time dominated by the heap building operation and is O(n). For larger values of k, the running time is O(k log n) time.

Second Algorithm We could also build a smaller heap tree of k elements. It then compares the remaining entries against the heap. If the new element is larger, then it replaces the root or else it is being discarded. To build a k element heap, the time will be O(k).

More The time to process each of the remaining elements is O(1), to test if the element goes into the heap, plus O(log k), to delete the root and insert the new element if this is necessary. Thus, the total time is O(k + (n-k) log k) = O(n log k). This algorithm also gives a bound of n log n for finding the median.