BINARY HEAPS 2 cs2420 Introduction to Algorithms and Data Structures Spring 2015

Similar documents
Data Structures and Algorithms

Course Content Concepts

CS 101 Computer Science I Fall Instructor Muller. Syllabus

GACE Computer Science Assessment Test at a Glance

WSU Five-Year Program Review Self-Study Cover Page

Notetaking Directions

CS 1103 Computer Science I Honors. Fall Instructor Muller. Syllabus

(Sub)Gradient Descent

CS 100: Principles of Computing

The Ohio State University Library System Improvement Request,

RANKING AND UNRANKING LEFT SZILARD LANGUAGES. Erkki Mäkinen DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A ER E P S I M S

Massachusetts Department of Elementary and Secondary Education. Title I Comparability

Task Types. Duration, Work and Units Prepared by

OCR for Arabic using SIFT Descriptors With Online Failure Prediction

Learning goal-oriented strategies in problem solving

Mathematics Success Grade 7

Multimedia Application Effective Support of Education

ASTR 102: Introduction to Astronomy: Stars, Galaxies, and Cosmology

preassessment was administered)

Software Maintenance

Stacks Teacher notes. Activity description. Suitability. Time. AMP resources. Equipment. Key mathematical language. Key processes

Exploration. CS : Deep Reinforcement Learning Sergey Levine

Chemistry 106 Chemistry for Health Professions Online Fall 2015


A Decision Tree Analysis of the Transfer Student Emma Gunu, MS Research Analyst Robert M Roe, PhD Executive Director of Institutional Research and

CSCI 104 Sorting Algorithms. Mark Redekopp David Kempe

Computer Science 1015F ~ 2016 ~ Notes to Students

Teaching Algorithm Development Skills

Common Core State Standards

MATH 1A: Calculus I Sec 01 Winter 2017 Room E31 MTWThF 8:30-9:20AM

School of Innovative Technologies and Engineering

Cognitive Modeling. Tower of Hanoi: Description. Tower of Hanoi: The Task. Lecture 5: Models of Problem Solving. Frank Keller.

On-Line Data Analytics

White Paper. The Art of Learning

Proof Theory for Syntacticians

Critical Thinking in Everyday Life: 9 Strategies

PowerTeacher Gradebook User Guide PowerSchool Student Information System

Detailed Instructions to Create a Screen Name, Create a Group, and Join a Group

Chapter 4 - Fractions

Many instructors use a weighted total to calculate their grades. This lesson explains how to set up a weighted total using categories.

The Indices Investigations Teacher s Notes

FINANCE 3320 Financial Management Syllabus May-Term 2016 *

International Business BADM 455, Section 2 Spring 2008

Algebra 2- Semester 2 Review

A Version Space Approach to Learning Context-free Grammars

Welcome to the Purdue OWL. Where do I begin? General Strategies. Personalizing Proofreading

BIOS 104 Biology for Non-Science Majors Spring 2016 CRN Course Syllabus

CALCULUS III MATH

Radius STEM Readiness TM

Chinese Language Parsing with Maximum-Entropy-Inspired Parser

Accounting 312: Fundamentals of Managerial Accounting Syllabus Spring Brown

"f TOPIC =T COMP COMP... OBJ

THE PENNSYLVANIA STATE UNIVERSITY SCHREYER HONORS COLLEGE DEPARTMENT OF MATHEMATICS ASSESSING THE EFFECTIVENESS OF MULTIPLE CHOICE MATH TESTS

Biology 1 General Biology, Lecture Sections: 47231, and Fall 2017

STUDENT MOODLE ORIENTATION

PROMOTION MANAGEMENT. Business 1585 TTh - 2:00 p.m. 3:20 p.m., 108 Biddle Hall. Fall Semester 2012

Computer Science is more important than Calculus: The challenge of living up to our potential

A Neural Network GUI Tested on Text-To-Phoneme Mapping

An Effective Framework for Fast Expert Mining in Collaboration Networks: A Group-Oriented and Cost-Based Method

Cal s Dinner Card Deals

The Good Judgment Project: A large scale test of different methods of combining expert predictions

Using Blackboard.com Software to Reach Beyond the Classroom: Intermediate

Planning for Preassessment. Kathy Paul Johnston CSD Johnston, Iowa

What is the ielts test fee. Where does the domestic cat come from..

Schoology Getting Started Guide for Teachers

Access Center Assessment Report

ASTRONOMY 2801A: Stars, Galaxies & Cosmology : Fall term

Machine Learning and Data Mining. Ensembles of Learners. Prof. Alexander Ihler

Unit Plan: Meter, Beat, and Time Signatures Music Theory Jenny Knabb The Pennsylvania State University Spring 2015

How to make an A in Physics 101/102. Submitted by students who earned an A in PHYS 101 and PHYS 102.

Computer Science 141: Computing Hardware Course Information Fall 2012

CLASS EXPECTATIONS Respect yourself, the teacher & others 2. Put forth your best effort at all times Be prepared for class each day

Mathematics process categories

Advanced Multiprocessor Programming

MOODLE 2.0 GLOSSARY TUTORIALS

CLASSROOM USE AND UTILIZATION by Ira Fink, Ph.D., FAIA

LEGAL RESEARCH & WRITING FOR NON-LAWYERS LAW 499B Spring Instructor: Professor Jennifer Camero LLM Teaching Fellow: Trygve Meade

Course Syllabus for Calculus I (Summer 2017)

Ricochet Robots - A Case Study for Human Complex Problem Solving

Grammars & Parsing, Part 1:

Measures of the Location of the Data

Unit 7 Data analysis and design

This scope and sequence assumes 160 days for instruction, divided among 15 units.

Outreach Connect User Manual

Implementing a tool to Support KAOS-Beta Process Model Using EPF

Course Syllabus for Math

Defragmenting Textual Data by Leveraging the Syntactic Structure of the English Language

Building People. Building Nations. GUIDELINES for the interpretation of Kenyan school reports

Grade 2: Using a Number Line to Order and Compare Numbers Place Value Horizontal Content Strand

SOUTHERN MAINE COMMUNITY COLLEGE South Portland, Maine 04106

Syllabus: CS 377 Communication and Ethical Issues in Computing 3 Credit Hours Prerequisite: CS 251, Data Structures Fall 2015

PSCH 312: Social Psychology

National Longitudinal Study of Adolescent Health. Wave III Education Data

Spring 2015 Natural Science I: Quarks to Cosmos CORE-UA 209. SYLLABUS and COURSE INFORMATION.

CS Machine Learning

AGENDA. Truths, misconceptions and comparisons. Strategies and sample problems. How The Princeton Review can help

Automatic Discretization of Actions and States in Monte-Carlo Tree Search

T Seminar on Internetworking

Some Principles of Automated Natural Language Information Extraction

Classify: by elimination Road signs

Transcription:

BINARY HEAPS 2 cs2420 Introduction to Algorithms and Data Structures Spring 2015 1

adistrivia 2

-assignment 10 is due tonight -assignment 11 is up, due next Thursday 3

assignment 7 scores number of students score 4

let s chat about ethics 5

// djb2 hash function unsigned long hash(unsigned char *str) { unsigned long hash = 5381; int c; while (c = *str++) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ } return hash; 6

last time 7

complete trees 8

-a complete binary tree has its levels completely filled, with the possible exception of the bottom level -bottom level is filled from left to right -each level has twice as many nodes as the previous level 9

-a N-node complete tree has at most (logn) height -operations are thus at most O(logN) -each level has twice as many nodes as the previous one -how do we use this knowledge to simplify the implementation? 10

complete trees as an array -if we are guaranteed that tree is complete, we can implement it as an array instead of a linked structure -the root goes at index 0, its left child at index 1, its right child at index 2 -for any node at index i, it two children are at index (i*2) + 1 and (i*2) + 2 11

a b c d e f g h i j a b c d e f g h i j index: 0 1 2 3 4 5 6 7 8 9 10 -for example, d s children start at (3*2) + 1 -how do we that f has no children? 12

a b c d e f g h i j a b c d e f g h i j index: 0 1 2 3 4 5 6 7 8 9 10 -any node s parent is at index (i-1) / 2 13

remember the priority queue? 14

-a priority queue is a data structure in which access is limited to the imum item in the set -add -findmin -deletemin -add location is unspecified, so long as the the above is always enforced -what are our options for implementing this? 15

binary heap 16

-a binary heap is a binary tree with two special properties -structure: it is a complete tree -order: the data in any node is less than or equal to the data of its children -this is also called a -heap -a -heap would have the opposite property 17

3 22 16 31 43 17 92 100 56 45 -where is the smallest item? 18

adding to a heap -we must be careful to maintain the two properties when adding to a heap -structure and order -deal with the structure property first where can the new item go to maintain a complete tree? -then, percolate the item upward until the order property is restored -swap upwards until > parent 19

adding 14 put it at the end of the tree 3 22 16 31 43 17 92 100 56 45 20

adding 14 put it at the end of the tree 3 22 16 31 43 17 92 100 56 45 14 21

adding 14 put it at the end of the tree percolate up the tree to fix the order 3 22 16 31 43 17 92 100 56 45 14 22

adding 14 put it at the end of the tree percolate up the tree to fix the order 3 22 16 31 43 17 92 100 56 45 14 23

adding 14 put it at the end of the tree percolate up the tree to fix the order 3 22 16 31 14 17 92 100 56 45 43 24

adding 14 put it at the end of the tree percolate up the tree to fix the order 3 22 16 31 14 17 92 100 56 45 43 25

adding 14 put it at the end of the tree percolate up the tree to fix the order 3 14 16 31 22 17 92 100 56 45 43 26

adding 14 put it at the end of the tree percolate up the tree to fix the order 3 14 16 31 22 17 92 100 56 45 43 27

adding 14 put it at the end of the tree percolate up the tree to fix the order 3 14 16 31 22 17 92 100 56 45 43 28

cost of add -percolate up until smaller than all nodes below it -how many nodes are there on each level (in terms of N)? -about half on the lowest level -about 3/4 in the lowest two levels 29

-if the new item is the smallest in the set, cost is O(logN) -must percolate up every level to the root -complete trees have logn levels -is this the worst, average, or best case? -it has been shown that on average, 1.6 comparisons are needed for any N -thus, add terates early, and average cost is O(1) 30

-you may find conflicting information on the average cost value -some sources may say O(logN) -it depends on the level of the analysis and how tight of a bound we want -don t always believe what you read, test for yourself! 31

remove from a heap -priority queues only support removing the smallest item -in heap this is always what? -remove and return the root -we have a hole at the top, structure property is violated -fill the whole with another item in the tree -which one? -percolate down 32

let s remove the smallest item Take out 3 3 14 16 31 22 17 92 100 56 45 43 33

let s remove the smallest item Take out 3 14 16 31 22 17 92 100 56 45 43 34

let s remove the smallest item Take out 3 fill with last item on last level. why? 14 16 31 22 17 92 100 56 45 43 35

let s remove the smallest item Take out 3 fill with last item on last level. why? 14 16 31 22 17 92 100 56 45 43 36

let s remove the smallest item Take out 3 fill with last item on last level. why? 43 14 16 31 22 17 92 100 56 45 37

let s remove the smallest item Take out 3 fill with last item on last level. why? percolate down 43 14 16 31 22 17 92 100 56 45 38

let s remove the smallest item Take out 3 fill with last item on last level. why? percolate down 43 14 16 31 22 17 92 100 56 45 39

let s remove the smallest item Take out 3 fill with last item on last level. why? percolate down 14 43 16 31 22 17 92 100 56 45 40

let s remove the smallest item Take out 3 fill with last item on last level. why? percolate down 14 43 16 31 22 17 92 100 56 45 41

let s remove the smallest item Take out 3 fill with last item on last level. why? percolate down 14 22 16 31 43 17 92 100 56 45 42

let s remove the smallest item Take out 3 fill with last item on last level. why? percolate down 14 22 16 31 43 17 92 100 56 45 43

let s remove the smallest item Take out 3 fill with last item on last level. why? percolate down 14 22 16 31 43 17 92 100 56 45 44

cost of remove -worst case is O(logN) -percolating down to the bottom level -average case is also O(logN) -rarely terates more than 1-2 levels from the bottom why? 45

back to the priority queue 46

-option 1: a linked list -add: O(1) -findmin: O(N) -deletemin: O(N) (including finding) -option 2: a sorted linked list -add: O(N) -findmin: O(1) -deletemin: O(1) -option 3: a self-balancing BST -add: O(logN) -findmin: O(logN) -deletemin: O(logN) -option 4: a binary heap -add: O(1) (percolate up average of 1.6 swaps) -findmin: O(1) (just access the root) -deletemin: O(logN) (percolate down but rarely terates before bottom) 47

today 48

-- heaps -add -delete -delete -delete algorithm 49

- heaps 50

-a - heap further extends the heap order property -for any node E at even depth, E is the imum element in its subtree -for any node O at odd depth, O is the imum element in its subtree -the root is considered to be at even depth (zero) 51

1 11 14 2 4 3 6 5 8 9 52

1 11 14 2 4 3 6 5 8 9 where is the smallest item? where is the largest item? 53

add 54

-AGAIN, we must ensure the heap property structure -must be a complete tree -add an item to the next open leaf node -THEN, restore order with its parent -does it belong on a level or a level? -swap if necessary -the new location deteres if it is a or node -percolate up the appropriate levels -if new item is a node, percolate up levels -else, percolate up levels 55

want to add 13 1 11 14 2 4 3 6 5 8 9 56

want to add 13 add to first open space 1 11 14 2 4 3 6 5 8 9 13 57

parent on a level, and 13 is greater than parent, so no swap necessary 13 is now a node 1 11 14 2 4 3 6 5 8 9 13 58

percolate up the levels compare to grandparent 1 11 14 2 4 3 6 5 8 9 13 59

percolate up the levels compare to grandparent 1 13 14 2 4 3 6 5 8 9 11 60

percolate up the levels compare to grandparent does 13 have another grandparent? done! 1 13 14 2 4 3 6 5 8 9 11 61

-if the parent is on a level, new node must be greater than the parent -if the parent is on a level, new node must be less than the parent -percolate up like normal, except skip every other level 62

delete 63

- node is one of the two children of the root -replace node with the last leaf node in the tree -preserve structure property! -restore order with the new node s children -if any child is larger, swap -percolate swapped child down the levels -if no child was larger, percolate the new node down the levels -if the node reaches the second to last level of tree, may require one more swap with direct children 64

want to delete the compare children of root 1 22 14 2 4 3 6 5 10 9 7 65

want to delete the compare children of root 22 is 1 22 14 2 4 3 6 5 10 9 7 66

want to delete the compare children of root 22 is 1 14 2 4 3 6 5 10 9 7 67

replace with last leaf node 1 14 2 4 3 6 5 10 9 7 68

replace with last leaf node 1 14 2 4 3 6 5 10 9 7 69

replace with last leaf node 1 7 14 2 4 3 6 5 10 9 70

restore order with children 7 is on a level, so must be > than children 1 7 14 2 4 3 6 5 10 9 71

restore order with children 7 is on a level, so must be > than children no swap required 1 7 14 2 4 3 6 5 10 9 72

7 is now a node percolate down levels 1 7 14 2 4 3 6 5 10 9 73

compare to all 4 grandchildren (or, 3 in this case) is 7 greater than all? 1 7 14 2 4 3 6 5 10 9 74

compare to all 4 grandchildren (or, 3 in this case) is 7 greater than all? swap with largest 1 7 14 2 4 3 6 5 10 9 75

compare to all 4 grandchildren (or, 3 in this case) is 7 greater than all? swap with largest 1 10 14 2 4 3 6 5 7 9 76

does 7 have more grandchildren? 1 10 14 2 4 3 6 5 7 9 77

done! 1 10 14 2 4 3 6 5 7 9 78

-if 7 had been less than one of its DIRECT children we would have swapped them -we would have then percolated that child down the levels instead of 7 -very similar to a regular -heap, except we skip every other level when percolating 79

deleting the 80

-the node is always the root -replace it with last leaf node -restore order with direct children -then, percolate new root down the levels 81

want to delete the 1 22 6 2 4 3 5 5 10 9 7 82

want to delete the 22 6 2 4 3 5 5 10 9 7 83

want to delete the replace with the last leaf node 22 6 2 4 3 5 5 10 9 7 84

want to delete the replace with the last leaf node 22 6 2 4 3 5 5 10 9 7 85

want to delete the replace with the last leaf node 7 22 6 2 4 3 5 5 10 9 86

restore order with direct children 7 22 6 2 4 3 5 5 10 9 87

restore order with direct children is 7 < both children? 7 22 6 2 4 3 5 5 10 9 88

restore order with direct children is 7 < both children? swap with smallest 7 22 6 2 4 3 5 5 10 9 89

restore order with direct children is 7 < both children? swap with smallest 6 22 7 2 4 3 5 5 10 9 90

percolate 6 down levels 6 22 7 2 4 3 5 5 10 9 91

percolate 6 down levels compare to all grandchildren is 6 < all the grandchildren? 6 22 7 2 4 3 5 5 10 9 92

percolate 6 down levels compare to all grandchildren is 6 < all the grandchildren? swap with smallest 6 22 7 2 4 3 5 5 10 9 93

percolate 6 down levels compare to all grandchildren is 6 < all the grandchildren? swap with smallest 6 22 7 2 4 3 5 5 10 9 94

percolate 6 down levels compare to all grandchildren is 6 < all the grandchildren? swap with smallest 2 22 7 6 4 3 5 5 10 9 95

does 6 have grandchildren? 2 22 7 6 4 3 5 5 10 9 96

6 has no grandchildren, but is not a leaf node compare with direct children to ensure order property 2 22 7 6 4 3 5 5 10 9 97

6 is on a level, so it must be smaller than children swap with smallest 2 22 7 6 4 3 5 5 10 9 98

6 is on a level, so it must be smaller than children swap with smallest 2 22 7 6 4 3 5 5 10 9 99

6 is on a level, so it must be smaller than children swap with smallest 2 22 7 5 4 3 5 6 10 9 100

6 is on a level, so it must be smaller than children swap with smallest done! 2 22 7 5 4 3 5 6 10 9 101

delete algorithm 102

delete ( is analogous) 1.locate node X (node containing item) 2.replace X with last node in tree (last index in array!) 3.detere if new X is violating order property with direct children - if so, swap contents of X with the largest child 4.percolate new item X down levels 5.if lowest level reached, restore order with lowest level (if applicable) 103

doubly-ended priority queue 104

-add -findmin -findmax -deletemin -deletemax - AND items can be found in constant time with a - heap -BUT lots of special cases -tutorial on website with diagrams and step-by-step explanations 105

next time 106

-reading -chapter 12 in book -homework -assignment 10 due tonight -assignment 11 is out 107