Heaps, stacks, queues

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

Programming II (CS300)

Priority Queues and Binary Heaps

Data Structures and Algorithms

Sorting and Searching

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

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

Sorting and Searching

Comp215: Trees. Mack Joyner and Dan S. Wallach (Rice University) Copyright 2016, Dan S. Wallach. All rights reserved.

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

Programming II (CS300)

HEAPS & PRIORITY QUEUES

Announcements HEAPS & PRIORITY QUEUES. Abstract vs concrete data structures. Concrete data structures. Abstract data structures

CSE 2123: Collections: Priority Queues. Jeremy Morris

Priority Queues Heaps Heapsort

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

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

Binary Heaps in Dynamic Arrays

- Alan Perlis. Topic 24 Heaps

You must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.

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

Course Review. Cpt S 223 Fall 2009

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

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

CompSci 201 Tree Traversals & Heaps

CE 221 Data Structures and Algorithms

Comp215: Trees. Dan S. Wallach (Rice University) Copyright 2015, Dan S. Wallach. All rights reserved.

Course Review for Finals. Cpt S 223 Fall 2008

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

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

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

PRIORITY QUEUES AND HEAPS

9. Heap : Priority Queue

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

and 6.855J February 6, Data Structures

Priority queues. Priority queues. Priority queue operations

Trees. A tree is a directed graph with the property

Priority queues. Priority queues. Priority queue operations

PRIORITY QUEUES AND HEAPS

PRIORITY QUEUES AND HEAPS

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

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

CSE 214 Computer Science II Heaps and Priority Queues

ME/CS 132: Advanced Robotics: Navigation and Vision

CS350: Data Structures Heaps and Priority Queues

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

Programming Abstractions

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

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

Summer Final Exam Review Session August 5, 2009

3. Fundamental Data Structures

CMSC 341 Lecture 14: Priority Queues, Heaps

Algorithms and Data Structures

Topic: Heaps and priority queues

COMP : Trees. COMP20012 Trees 219

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

Basic Data Structures and Heaps

Data Structures Question Bank Multiple Choice

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

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

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

Binary Heaps. CSE 373 Data Structures Lecture 11

Heaps with merging. We can implement the other priority queue operations in terms of merging!

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

Basic Data Structures (Version 7) Name:

Standard ADTs. Lecture 19 CS2110 Summer 2009

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

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

CS 261 Data Structures. Priority Queue ADT & Heaps

CMSC 341 Lecture 15 Leftist Heaps

Red-Black-Trees and Heaps in Timestamp-Adjusting Sweepline Based Algorithms

Binary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017

Lecture 13: AVL Trees and Binary Heaps

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

Figure 1: A complete binary tree.

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

AP Computer Science 4325

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

CMSC 341 Lecture 15 Leftist Heaps

Linked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.

Data Structures and Algorithms for Engineers

Comp2402: Review Questions

Figure 1: A complete binary tree.

CMSC 341 Leftist Heaps

Priority Queues and Heaps

The beautiful binary heap.

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

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

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

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;

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

Algorithms and Data Structures

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.

Chapter 9. Priority Queue

Heapsort. Heap data structure

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

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Priority Queues and Huffman Trees

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Linked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.

Transcription:

Heaps, stacks, queues Dan S. Wallach and Mack Joyner, Rice University Copyright 216 Dan Wallach, All Rights Reserved

Where was Prof. Wallach on Tuesday?

Two hours of scintillating Congressional testimony: https://www.youtube.com/watch?v=iru9zuff9a

At the end of the semester, if we have time, we ll have a lecture on computer security & electronic voting. Two hours of scintillating Congressional testimony: https://www.youtube.com/watch?v=iru9zuff9a

Preliminaries: queues vs. stacks Stack: last-in-first-out (LIFO) Our IList classes work like this. Queue: first-in-first-out (FIFO) You implemented this last week. Functional vs. mutating? Straightforward to do it either way.

How do you implement a mutating queue? Option 1: doubly-linked list (java.util.linkedlist) Every node has pointers to next and previous nodes Sentinel node to deal with empty-list case O(1) insert and removal Possible to add and remove from either end (= Deque or Dequeue) Option 2: array (java.util.arraydeque) Tricky: requires growing the array, tracking start/end, wraparound O(1) insert and removal (amortized: sometimes requires an O(n) copy)

How about a functional queue? Option 1: use two lists ( inbox and outbox ) You built this last week. Constant amortized time. Option 2: use a tree (not recommended) You ll notice that our trees have a getmin method. All operations are O(log n)

So what s a priority queue? Priority queues are typically implemented via mutation. getmin() returns the smallest value and removes it at the same time. public interface IPriorityQueue<T> { T getmin(); void insert(t val); int size(); boolean empty(); }

Binary heaps: efficient priority queues Storage happens in an array (or java.util.arraylist) Map a tree into the array. No pointers, just math on array indices. leftchild(i) = 2i + 1 rightchild(i) = 2i + 2 parent(i) = (i-1)/2 4 2 Note: integer arithmetic 9 A tree like this is called complete. Heap values Array index 2 4 9

Binary heaps: data definition The heap property : A parent s value is less than (or equal to) its children s values. Ergo: The minimum value is in the root. And that s the zeroth element. 4 2 9 A tree like this is called complete. Heap values Array index 2 4 9

Binary heaps: inserting a new value First, go to the next available slot (here, #8) Work upwards, swapping to create the heap property 2 insert() 4 9

Binary heaps: inserting a new value First, go to the next available slot (here, #8) Work upwards, swapping to create the heap property 2 insert() 4 9 8

Binary heaps: inserting a new value First, go to the next available slot (here, #8) Work upwards, swapping to create the heap property 2 insert() 4 9 8

Binary heaps: inserting a new value First, go to the next available slot (here, #8) Work upwards, swapping to create the heap property 2 insert() 4 9 8

Binary heaps: inserting a new value First, go to the next available slot (here, #8) Work upwards, swapping to create the heap property 2 insert() 4 9 8

Binary heaps: inserting a new value First, go to the next available slot (here, #8) Work upwards, swapping to create the heap property 2 insert() 4 9 8

Binary heaps: inserting a new value First, go to the next available slot (here, #8) Work upwards, swapping to create the heap property 2 insert() 4 9 8

Binary heaps: inserting a new value First, go to the next available slot (here, #8) Work upwards, swapping to create the heap property 2 insert() Insertion cost: O(log n) Stable, predictable. 4 9 8

Binary heaps: removing the min value The answer is right on top, so that s easy But what about the children? 2 First, move the last child to the top. 4 9 8

Binary heaps: removing the min value The answer is right on top, so that s easy But what about the children? 2 First, move the last child to the top. 4 9 8

Binary heaps: removing the min value The answer is right on top, so that s easy But what about the children? Now recursively work down Reestablish the heap property. Use the smallest value. If equal, either is fine. 4 9

Binary heaps: removing the min value The answer is right on top, so that s easy But what about the children? Now recursively work down Reestablish the heap property. Use the smallest value. If equal, either is fine. 4 9

Binary heaps: removing the min value The answer is right on top, so that s easy But what about the children? Now recursively work down Reestablish the heap property. Use the smallest value. If equal, either is fine. 4 9

Binary heaps: removing the min value The answer is right on top, so that s easy But what about the children? Now recursively work down Reestablish the heap property. Use the smallest value. If equal, either is fine. 4 9

Binary heaps: removing the min value The answer is right on top, so that s easy But what about the children? Now recursively work down Reestablish the heap property. 4 Use the smallest value. If equal, either is fine. 9

Binary heaps: removing the min value The answer is right on top, so that s easy But what about the children? Now recursively work down Reestablish the heap property. 4 Use the smallest value. If equal, either is fine. 9

Binary heaps: removing the min value The answer is right on top, so that s easy But what about the children? Now recursively work down Reestablish the heap property. 4 Use the smallest value. If equal, either is fine. 9 Total cost: O(log n)

Using arrays is a serious win Compact memory usage (no left/right pointers) Navigating up or down is easy O(log n) operations aren t just expected case A heap is always a complete tree, so it s always O(log n) ArrayList vs. arrays ArrayList will automatically allocate bigger arrays (internally) and copy.

Performance numbers FIFO queues (functional) ListQueue : 1 inserts, 1 fetches:.1 μs per insert TreapQueue : 1 inserts, 1 fetches:.8 μs per insert FIFO queues (mutating) LinkedList : 1 inserts, 1 fetches:.2 μs per insert ArrayDeque : 1 inserts, 1 fetches:.18 μs per insert Priority queues (mutating) BinaryHeap : 1 inserts, 1 fetches:.99 μs per insert PriorityQueue: 1 inserts, 1 fetches:.61 μs per insert

Performance numbers FIFO queues (functional) ListQueue : 1 inserts, 1 fetches:.1 μs per insert TreapQueue : 1 inserts, 1 fetches:.8 μs per insert FIFO queues (mutating) LinkedList : 1 inserts, 1 fetches:.2 μs per insert ArrayDeque : 1 inserts, 1 fetches:.18 μs per insert Priority queues (mutating) BinaryHeap : 1 inserts, 1 fetches:.99 μs per insert PriorityQueue: 1 inserts, 1 fetches:.61 μs per insert Mutation is winning big, this time...

Live coding Q&A for project 4 Advice for debugging Build your valid() method first, then use it to test. Take advantage of printing your treaps. Use tostringhelper().