So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

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

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we

Hi everyone. Starting this week I'm going to make a couple tweaks to how section is run. The first thing is that I'm going to go over all the slides

Sorting and Searching

Data structures. Priority queues, binary heaps. Dr. Alex Gerdes DIT961 - VT 2018

Sorting and Searching

Heap sort. Carlos Moreno uwaterloo.ca EIT

MITOCW watch?v=w_-sx4vr53m

Binary heaps (chapters ) Leftist heaps

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

Heap sort. Carlos Moreno uwaterloo.ca EIT

Priority Queues Heaps Heapsort

Priority Queues Heaps Heapsort

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

Binary Trees

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

Priority Queues and Binary Heaps

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

CS302 Midterm Exam Answers & Grading James S. Plank September 30, 2010

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

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

Lecture Summary CSC 263H. August 5, 2016

Binary Heaps in Dynamic Arrays

Created by Julie Zelenski, with edits by Jerry Cain, Keith Schwarz, Marty Stepp, Chris Piech and others.

CS 106B Lecture 26: Esoteric Data Structures: Skip Lists and Bloom Filters

Direct Variations DIRECT AND INVERSE VARIATIONS 19. Name

Lecture Notes on Priority Queues

The Stack, Free Store, and Global Namespace

Post Experiment Interview Questions

(Refer Slide Time: 00:51)

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

(the bubble footer is automatically inserted into this space)

6.001 Notes: Section 8.1

Instructor: Craig Duckett. Lecture 04: Thursday, April 5, Relationships

Section 4 SOLUTION: AVL Trees & B-Trees

DATA STRUCTURES AND ALGORITHMS

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

1 Heaps. 1.1 What is a heap? 1.2 Representing a Heap. CS 124 Section #2 Heaps and Graphs 2/5/18

6.001 Notes: Section 15.1

Programming and Data Structures Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras

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

CMPSC 250 Analysis of Algorithms Spring 2018 Dr. Aravind Mohan Shortest Paths April 16, 2018

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

Binary Search Trees. Carlos Moreno uwaterloo.ca EIT

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

CSE143 Notes for Monday, 4/25/11

Exam Data structures DIT960/DAT036

MITOCW watch?v=flgjisf3l78

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

(Refer Slide Time: 06:01)

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

The following content is provided under a Creative Commons license. Your support

MITOCW watch?v=tkwnms5irbu

MITOCW watch?v=0jljzrnhwoi

Blitz2D Newbies: Definitive Guide to Types by MutteringGoblin

Skill 1: Multiplying Polynomials

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

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

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

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

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

The heap is essentially an array-based binary tree with either the biggest or smallest element at the root.

Chapter 6 Heapsort 1

MITOCW watch?v=zm5mw5nkzjg

PRIORITY QUEUES AND HEAPS

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

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

Priority queues. Priority queues. Priority queue operations

MITOCW watch?v=r6-lqbquci0

PRIORITY QUEUES AND HEAPS

Summer Final Exam Review Session August 5, 2009

SIMPLE PROGRAMMING. The 10 Minute Guide to Bitwise Operators

Balanced Search Trees

Java Programming. Computer Science 112

Data Structures Lesson 9

UNIT III BALANCED SEARCH TREES AND INDEXING

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

Heaps. Data Structures and Other Objects Using C++

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

PRIORITY QUEUES AND HEAPS

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

6.001 Notes: Section 6.1

by Marina Cholakyan, Hyduke Noshadi, Sepehr Sahba and Young Cha

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

Lecture 13: AVL Trees and Binary Heaps

CS02b Project 2 String compression with Huffman trees

AVL Trees Heaps And Complexity

(Refer Slide Time: 01.26)

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Lecture 23: Priority Queues 10:00 AM, Mar 19, 2018

COS 226 Midterm Exam, Spring 2009

Discussion 2C Notes (Week 9, March 4) TA: Brian Choi Section Webpage:

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

Week - 01 Lecture - 03 Euclid's Algorithm for gcd. Let us continue with our running example of gcd to explore more issues involved with program.

Section 05: Solutions

CS 106 Introduction to Computer Science I

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

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

CSE 373: Data Structures and Algorithms

What did we talk about last time? Examples switch statements

Transcription:

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees. According to the 161 schedule, heaps were last week, hashing is this week, and binary search trees are next week. However, balanced binary search trees are actually kind of a pain to implement, especially if we try to make it fit just in this class's time, so what we're gonna do instead is work on heaps this week to do something that balanced binary search trees are good for. Next week we'll work on a hashing problem. 1

First let's start with a regular heap, like the one discussed in 161. Well, not exactly the one in 161; those slides are all for MAX heaps, but the first problem for today is to implement a MIN heap. You can still use the 161 slides to help you out; all you need to do is remember to switch the direction of all the comparisons of heap elements. Now on the survey someone asked whether we could have more consistency between this class and the main 161 class. In general, I'd like to present the material in basically the same way, but I do want to flip a convention if it is better suited for the purposes of this class. So why are we swapping min and max for this assignment? Well, it turns out that the classical algorithms that you'll learn in this course that need a priority queue, such as Prim's algorithm or Dijkstra's algorithm, need a min priority queue, not a max priority queue, in order to work. The convention of lower priority numbers coming first is also fairly common in industry; for example, if you've ever worked with a bug tracking or feature request system, you might have noticed that P0 things are more important than P1 things, which are more important than P2 things. So in general, you're going to find that min priority queues are more common than max priority queues, so we're going to code up min heaps. 2

As mentioned in the main lecture, while it's easier to think of heaps as tree structures with pointers, when we actually implement them, we just use a single array, with the parent and child relationships being implied from the array indices. We'll see this trick in a couple different forms before the end of the class, so keep an eye out for that. Anyway, heaps are an odd beast in that they really do depend on one-indexed arrays, even though the languages we usually code them in have zero-indexed arrays. What this means is when you make a heap with a capacity of n, you're going to need to allocate an array of size n+1 and leave the first element blank. 3

As a reminder, to access your children, you multiply your current index by 2 and add 0 for your left child, or 1 for your right child. A side note: As was pointed out on homework 2 problem 1, multiplication is actually a somewhat expensive operation, and multiplying by 2 is the same as performing a left bit shift of 1, which is a cheap operation (on par with addition). However, bit shifts make for difficult code to read, especially since for some reason they're really low in the order of operations, which would be a tradeoff of sorts EXCEPT for the fact that modern compilers can recognize when you're just multiplying by 2 and turn it into a bit shift operation at compile time anyway. So keep the multiplication for readability's sake, since you're not actually taking a performance hit at all. 4

In the other direction, to find your parent, you divide your index by 2 and round down. Notice that you don't actually need to use a floor function; instead, you can simply rely on integer division to automatically round down for you. Again, this is also possible with a bit shift, but again, the compiler will take care of that for you. Now, there are extensive slides from the main course that explain what you need to do to insert a number into a heap, and how to extract the minimum element from the heap, so I'll refer you all to those slides. 5

Let's move on to the main problem for today. A min heap lets you insert a number, and it lets you find and take out the smallest number you have, both in log time. A max heap lets you insert a number, and it lets you find and take out the LARGEST number you have, both in log time. Is it possible to do both, that is, to choose to take out either the smallest number OR the largest number, both in log time? Well, one way you could do this is with a balanced binary search tree, since finding the first and last elements in the tree each take log time. But can we do this with heaps, and avoid the huge constant factors associated with balanced binary search trees? What if we kept BOTH a min-heap and a max-heap? Insertion is easy; we just have to insert the number into both heaps. Extraction is harder; when we extract the min, it's easy to figure out what to do with the min-heap, but we need to also delete it from the max-heap, which means we have to find it in the max-heap first. Same goes for the other direction. 6

In order to make sure that we can quickly find the element in the other heap, we need to maintain a pair of pointers that links the two copies of the same element in the two heaps to each other. Now I'm drawing the heaps and these links as arrows to make them easier to visualize, but later on we'll see that we're still going to be using arrays, not pointers. This is important, since we want to take advantage of the performance boost we get from using flat arrays. 7

So how does this work? Well, let's walk through extract-min. The first part of the process works the same as for a single heap; 8

we swap the root of the min-heap with the last element in the min-heap, 9

and then we fix the min-heap up. If we had just one heap, we'd cut off and report back the last element now. In a min-max heap, though, we also need to remove this 1 from the max-heap. To do that, 10

we follow the pointer to find it in the max-heap, and then swap it with the last element in the max-heap. Now we're going to need to fix the max-heap by swapping that red element into the right place. Now remember that for the min-heap, the red element started at the top of the heap and bubbled down. To fix the max-heap, though, we're going to start at the bottom and bubble up. Why is it that we don't have to worry about bubbling down at all? (Because we swapped the red element with the smallest element in the max-heap, which can't have any descendants that are larger than it, so the red element, which is no smaller, also can't have any descendants that are larger than it.) 11

Once that's fixed, we can just chop the last element off of both heaps, and then we're done. So to summarize, to extract the minimum element, we do a regular extraction on the min-heap, which involves bubbling down to fix the heap, and then we follow the pointer to do a special extraction on the max-heap, which involves bubbling up to fix the heap. Similarly, to extract the maximum element, we extract from the maxheap, bubbling down, and then follow the pointer to extract from the min-heap, bubbling up. 12

So all this time I've been talking about pointers, but this is a Java class, and Java doesn't have actual pointers you can manipulate. But I also said earlier that we wanted to use arrays anyway. For the individual heaps themselves, you already know how to treat them as an array. For the links between the heaps, I'm going to recommend storing an array that holds the indices of the elements in the other array. For example, if you have an array of pointers from the min heap to the max heap called minptr, then minptr of i being j means that the element at position i in the min heap matches the element at position j in the max heap. To make this easier to code, you should make sure your swap function correctly updates all of the relevant pointers in both heaps, even if you're just swapping elements in one heap. For this week only, I've provided you with starter code that handles the I/O, and also suggests a way of decomposing such a swap function. If you follow this suggestion, then your extraction code should look really similar to the extraction code you write for the regular min-heap. 13