Binary Heaps in Dynamic Arrays

Similar documents
Priority Queues and Binary Heaps

Dictionaries. Priority Queues

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

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

13.4 Deletion in red-black trees

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

Chapter 6 Heapsort 1

Lecture Notes for Advanced Algorithms

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

Recall: Properties of B-Trees

quiz heapsort intuition overview Is an algorithm with a worst-case time complexity in O(n) data structures and algorithms lecture 3

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

13.4 Deletion in red-black trees

Binary Heaps. CSE 373 Data Structures Lecture 11

Algorithms and Data Structures

CSCI2100B Data Structures Heaps

Sorting and Searching

Heaps Goodrich, Tamassia. Heaps 1

Sorting and Searching

Partha Sarathi Manal

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Heapsort. Heap data structure

Algorithms. AVL Tree

The Heap Data Structure

Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

Binary Search and Worst-Case Analysis

CS711008Z Algorithm Design and Analysis

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Priority Queues. T. M. Murali. January 29, 2009

CS711008Z Algorithm Design and Analysis

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

CSE 548: Analysis of Algorithms. Lectures 14 & 15 ( Dijkstra s SSSP & Fibonacci Heaps )

Properties of red-black trees

CS2 Algorithms and Data Structures Note 6

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

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

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

Administration CSE 326: Data Structures

Recursion: The Beginning

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

The B-Tree. Yufei Tao. ITEE University of Queensland. INFS4205/7205, Uni of Queensland

9. Heap : Priority Queue

Priority queues. Priority queues. Priority queue operations

Stores a collection of elements each with an associated key value

Programming II (CS300)

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

Priority Queues, Binary Heaps, and Heapsort

BM267 - Introduction to Data Structures

Maps; Binary Search Trees

Priority Queues. T. M. Murali. January 26, T. M. Murali January 26, 2016 Priority Queues

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Algorithm Theory, Winter Term 2015/16 Problem Set 5 - Sample Solution

Algorithms. Red-Black Trees

CH 8. HEAPS AND PRIORITY QUEUES

Lecture Notes: External Interval Tree. 1 External Interval Tree The Static Version

Data Structures and Algorithms

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

CH. 8 PRIORITY QUEUES AND HEAPS

Programming II (CS300)

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Data Structures Lesson 9

Dynamic Arrays and Amortized Analysis

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

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?

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Non-context-Free Languages. CS215, Lecture 5 c

Properties of a heap (represented by an array A)

Fibonacci Heaps. Structure. Implementation. Implementation. Delete Min. Delete Min. Set of min-heap ordered trees min

SFU CMPT Lecture: Week 9

Module 2: Priority Queues

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

Search Trees. Undirected graph Directed graph Tree Binary search tree

Priority Queues. Priority Queues Trees and Heaps Representations of Heaps Algorithms on Heaps Building a Heap Heapsort.

Heaps. 2/13/2006 Heaps 1

CSE 5311 Notes 4a: Priority Queues

Heaps and Priority Queues

Priority Queues. T. M. Murali. January 23, T. M. Murali January 23, 2008 Priority Queues

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

Introduction to Algorithms 3 rd edition

High Dimensional Indexing by Clustering

Topic: Heaps and priority queues

Balanced search trees. DS 2017/2018

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

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

CS350: Data Structures Heaps and Priority Queues

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

CS2 Algorithms and Data Structures Note 6

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

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

W4231: Analysis of Algorithms

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

Finding Strongly Connected Components

CMSC 341 Lecture 15 Leftist Heaps

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

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

Priority Queues Heaps Heapsort

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

Organizing Spatial Data

Lecture Notes: Range Searching with Linear Space

Transcription:

Yufei Tao ITEE University of Queensland

We have already learned that the binary heap serves as an efficient implementation of a priority queue. Our previous discussion was based on pointers (for getting a parent node connected with its children). In this lecture, we will see a pointerless way to implement a binary heap, which in practice achieves much lower space consumption. We will also see a way to build a heap from n integers in just O(n) time, improving the obvious O(n log n) bound.

Recall: Priority Queue A priority queue stores a set S of n integers and supports the following operations: Insert(e): Adds a new integer to S. Delete-min: Removes the smallest integer in S, and returns it.

Recall: Binary Heap Let S be a set of n integers. A binary heap on S is a binary tree T satisfying: 1 T is complete. 2 Every node u in T corresponds to a distinct integer in S the integer is called the key of u (and is stored at u). 3 If u is an internal node, the key of u is smaller than those of its child nodes.

Storing a Complete Binary Tree Using an Array Let T be any complete binary tree with n nodes. Let us linearize the nodes in the following manner: Put nodes at a higher level before those at a lower level. Within the same level, order the nodes from left to right. Let us store the linearized sequence of nodes in an array A of length n.

Example 1 39 8 79 54 26 23 93 Stored as 1 39 8 79 54 26 23 93

Property 1 Let us refer to the i-th element of A as A[i]. Lemma: Suppose that node u of T is stored at A[i]. Then, the left child of u is stored at A[2i], and the right child at A[2i + 1]. Observe this from the example of the previous slide.

Property 1 Proof: Suppose that u is the j-th node at Level l. This level must be full because u has a child node (which must be at Level l + 1). In other words, there are 2 l nodes at level l. We will prove the lemma only for the left child (the right child is simply stored at the next position of the array). From the fact that u is the i-th node in the linearized order, we know: i = j + 2 0 + 2 1 +... + 2 l 1 = j + 2 l 1.

Property 1 1 j... u... level l... level l + 1 Next we will prove that there are precisely i 1 nodes in A after u but before its left child. These nodes include: Those at Level l behind u: there are 2 l j of them. Child nodes of the first j 1 nodes at Level l: there are 2(j 1) of them. Hence, in total, there are 2 l j + 2(j 1) = 2 l + j 2 = i 1 such nodes. This completes the proof.

Property 1 The following is an immediate corollary of the previous lemma: Corollary: Suppose that node u of T is stored at A[i]. Then, the parent of u is stored at A[ i/2 ].

Property 2 The following is a simple yet useful fact: Lemma: The rightmost leaf node at the bottom level is stored at A[n]. Proof: Obvious.

Now we have got everything we need to implement the insertion and delete-min algorithms (discussed in the previous lecture) on the array representation of a binary heap.

Example Inserting 15: 1 39 8 79 54 26 23 93 15 1 39 8 15 54 26 23 93 79 1 15 8 39 54 26 23 93 79 Performing a delete-min: 1 15 8 39 54 26 23 93 79 79 15 8 39 54 26 23 93 8 15 79 39 54 26 23 93 8 15 23 39 54 26 79 93

Performance Guarantees Combining our analysis on (i) binary heaps and (ii) dynamic arrays, we obtain the following guarantees on a binary heap implemented with a dynamic array: Space consumption O(n). Insertion: O(log n) time amortized. Delete-min: O(log n) time amortized.

Next, we consider the problem of creating a binary heap on a set S of n integers. Obviously, we can do so in O(n log n) time by doing n insertions. However, this is an overkill because the binary heap does not need to support any delete-min operations until all the n numbers have been inserted. This raises the question whether we can build the heap faster. The answer is positive: we will see an algorithm that does so in O(n) time.

Fixing a Messed-Up Root Let us first consider the following root-fix operation. We are given a complete binary tree T with root r. It is guaranteed that: The left subtree of r is a binary heap. The right subtree of r is a binary heap. However, the key of r may not be smaller than the keys of its children. The operation fixes the issue, and makes T a binary heap. This can be done in O(log n) time in the same manner as the delete-min algorithm (by descending a path).

Example 25 8 8 15 8 15 25 15 23 39 54 26 23 39 54 26 23 39 54 26 25 93 79 93 79 93 79

Building a Heap Given an array A that stores a set S of n integers, we can turn A into a binary heap on S using the following simple algorithm, which views A as a complete binary search tree T : For each i = n downto 1 - Perform root-fix on the subtree of T rooted at A[i] Think: Why are the conditions of root-fix always satisfied?

Example i 54 26 15 93 8 1 23 39 i 54 26 15 93 8 1 23 39 54 26 15 93 8 i 1 23 39 i 54 26 15 93 8 1 23 39 i 54 26 15 39 8 1 23 93 54 26 i 1 39 8 15 23 93 i 54 8 1 39 26 15 23 93 i 1 8 15 39 26 54 23 93

Running Time Now let us analyze the time of the building algorithm. Suppose that T has height h. Without loss of generality, assume that all the levels of T are full namely, n = 2 h 1 (why no generality is lost?). Observe: A node at Level h 1 incurs O(1) time in root-fix; 2 h 1 such nodes. A node at Level h 2 incurs O(2) time in root-fix; 2 h 2 such nodes. A node at Level h 3 incurs O(3) time in root-fix; 2 h 3 such nodes.... A node at Level h h incurs O(h) time in root-fix; 2 0 such nodes.

Running Time Hence, the total time is bounded by h O ( ( h ) i 2 h i) = O i 2 h i i=1 i=1 We will prove that the right hand side is O(n) in the next slide.

Running Time Suppose that x = 2 h 1 + 2 2 h 2 + 3 2 h 3 +... + h 2 0 (1) 2x = 2 h + 2 2 h 1 + 3 2 h 2 +... + h 2 1 (2) Subtracting (1) from (2) gives x = 2 h + 2 h 1 + 2 h 2 +... + 2 1 h 2 h+1 = 2(n + 1) = O(n).