chapter 6 heaps - william & marytadavis/cs303/f16/ch06.pdf · leftist heaps 31 −leftist heaps...

50
Chapter 6 Heaps

Upload: others

Post on 02-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Chapter 6

Heaps

Page 2: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Introduction

2

−some systems applications require that items be processed

in specialized ways

−printing

−may not be best to place on a queue

−some jobs may be more important

−small 1-page jobs should be printed before a 100-page

job

−operating system scheduler

−processes run only for a slice of time

−queue: FIFO

−some short jobs may take too long

−other jobs are more important and should not wait

Page 3: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Heap Model

3

−specialized queue required

−heap (priority queue)

−provides at least

− insert

−deleteMin: finds, returns, and removes min (or max)

−other operations common

−used in other applications

−external sorting

−greedy algorithms

−discrete event simulation

Page 4: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Heap Implementation

4

−heaps can be implemented with

− linked list, with insertions at head

− insert 𝑂 1

−deleteMin 𝑂 𝑁

−ordered linked list (worse due to number of insertions)

− insert 𝑂 𝑁

−deleteMin 𝑂 1

−binary search tree

− insert 𝑂 log 𝑁

−deleteMin 𝑂 log 𝑁

−since only min is deleted, tree will be unbalanced

−overkill since other included operations not required

Page 5: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binary Heap

5

−binary heap common

−simply termed heap

− two properties

−structure

−heap order

−operations can destroy one of the properties

−operation must continue until heap properties have been

restored, which is typically simple

Page 6: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binary Heap

6

−structure property

−completely filled

−except bottom level, which is filled from left to right

−complete binary tree has between 2ℎ and 2ℎ+1 − 1 nodes

−height: log 𝑁

−can be represented with an array (no links necessary)

−array position 𝑖

−left child in 2𝑖

−right child in 2𝑖 + 1

−parent in 𝑖/2

−operations simple

−maximum heap size must be known in advance

Page 7: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binary Heap

7

Page 8: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binary Heap

8

−heap-order property

−allows operations to be performed quickly

−want to be able to find minimum quickly

−smallest element at root

−any subtree should also be a heap

−any node should be smaller than all its descendants

Page 9: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Heap Operations

9

− two main heap operations

− insert

−deleteMin

− insert

−need to maintain structure property

−create hole in next available location (at bottom of tree)

−place new value there if possible

−otherwise, slide parent into hole and bubble up hole

−continue until new value can be inserted

−termed percolate up strategy

Page 10: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binary Heap

10

−example: insert 14

Page 11: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Heap Operations

11

− insert (cont.)

−could have used repeated swaps, but a swap requires

three assignments

− if element percolated up 𝑑 levels

−swap method: 3𝑑 assignments

−non-swap method: 𝑑 + 1 assignments

− if new element is smaller than all others in heap, hole will

percolate to the root

−hole will be at index 1 and we will break out of the loop

−if extra check for 1 in loop, adds unnecessary time

−could place a copy of new value in position 0

Page 12: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Heap Operations

12

− insert (cont.)

−could require as much as 𝑂 log 𝑁

−on average, percolation terminates early

−on average 2.607 comparisons are required

Page 13: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Heap Operations

13

−deleteMin

−finding minimum easy

−removing minimum more difficult

−hole is created at root

−last element in complete binary tree must move

−if last element can be placed in hole, done

−otherwise, slide hole’s smaller child into hole

−hole slides down one level

−repeat until last element can be placed in hole

−worst case: 𝑂 log 𝑁

−average case: 𝑂 log 𝑁

Page 14: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binary Heap

14

−example: deleteMin (13)

Page 15: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binary Heap

15

−example: deleteMin (13) (cont.)

Page 16: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Heap Operations

16

−other heap operations

−finding minimum fast

−finding maximum not possible without linear scan through

entire heap

−maximum in one of the leaves

−could use separate data structure, such as hash table

Page 17: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Heap Operations

17

−other heap operations (cont.)

−decreaseKey

−lowers value at position 𝑝 by given amount

−percolate up

−example: change process priority for more run time

− increaseKey

−increases value at position 𝑝 by given amount

−percolate down

−example: drop process priority if taking too much time

−remove

−decreaseKey to root, then deleteMin

−example: process is terminated early by user

Page 18: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Heap Operations

18

−other heap operations (cont.)

−buildHeap

−place 𝑁 items into the tree in any order

−maintains structure property

−use percolateDown(𝑖) from node 𝑖

Page 19: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binary Heap

19

−example: start with random placement

−start with percolateDown(7)

−each dashed line represents 2 comparisons

Page 20: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binary Heap

20

−example: (cont.)

Page 21: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Heap Applications

21

−selection problem

−from a list of 𝑁 elements, find 𝑘th largest

−original algorithm

−sort list and index 𝑘th element

−with simple sort, 𝑂 𝑁2

−alternative algorithm

−read 𝑘 elements into array and sort them

−smallest is in 𝑘th position

−other elements processed one by one, placing them

into the array

−running time 𝑂 𝑁 ∙ 𝑘

−if 𝑘 = 𝑁/2 , 𝑂 𝑁2

Page 22: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Heap Applications

22

−selection problem (cont.)

− if heap is used

−build heap of 𝑁 elements

−perform 𝑘 deleteMin operations

−last element extracted is 𝑘th smallest element

−if 𝑘 = 𝑁/2 , ϴ 𝑁 log 𝑁

−another algorithm using heap

−as in previous algorithm, but put 𝑘 element in heap

−smallest is in 𝑘th position

−other elements processed one by one, placing them

into the array

−running time ϴ 𝑁 log 𝑁

Page 23: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Heap Applications

23

−event simulation

−bank where customers arrive and wait until one of 𝑘

tellers is available

−customer arrival and service time based on probability

distribution function

−compute statistics on the length of time a customer

must wait, or the length of the line

−need to consider event that will occur in the least

amount of time

−heap can be used to order events

Page 24: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

𝑑-Heaps

24

−𝑑-heap

−same as binary heap, but all nodes have 𝑑 children

− tends to be more shallow than binary heap

− running time reduced to 𝑂 log𝑑 𝑁

−deleteMin more expensive since more comparisons

required

−useful when heap is too large to fit entirely into main

memory

−4-heaps may outperform 2-heaps (binary heaps)

Page 25: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

𝑑-Heaps

25

−example: 𝑑-heap with 𝑑 = 3

Page 26: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Leftist Heaps

26

−one weakness of heaps so far is combining two heaps is

difficult

− three data structures that can help

− leftist heaps

−skew heaps

−binomial queues

Page 27: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Leftist Heaps

27

− leftist heaps

−can be difficult to design a data structure for merging that

uses an array, but runs efficiently

− linked data structure therefore required

− leftist heap

−structure and ordering properties of binary heaps

−difference is that heap is not perfectly balanced

−very unbalanced is desired

Page 28: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Leftist Heaps

28

− leftist heaps (cont.)

−null path length (npl): length of shortest path from current

node to a node without two children

−npl of a node with zero or one child is 0

−npl of a null pointer is -1

−npl of each node is 1 more than the minimum of the null

path lengths of its children

− leftist heap property

−npl of the left child is at least as large as that of the right

child

−biases tree to deeper left subtree

Page 29: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Leftist Heaps

29

−example: leftist heap

Page 30: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Leftist Heaps

30

− leftist heaps operations

−all work should be done on the right path, which is

guaranteed to be short

− inserts and merges may destroy the leftist heap property

−not difficult to fix

Page 31: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Leftist Heaps

31

− leftist heaps operations (cont.)

−merging

− insertion special case of merging a 1-node heap with a

larger heap

− if either of the two heaps is empty, return the other heap

−otherwise, compare roots

−recursively merge heap with the larger root with the right

subheap of the heap with the smaller root

Page 32: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Leftist Heaps

32

− leftist heaps operations (cont.)

−merging example

Page 33: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Leftist Heaps

33

− leftist heaps operations (cont.)

−merging example

Page 34: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Leftist Heaps

34

− leftist heaps operations (cont.)

−merging example

−result is not leftist: left npl = 1, right npl = 2

Page 35: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Leftist Heaps

35

− leftist heaps operations (cont.)

−merging example

−fix by swapping children

Page 36: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Skew Heaps

36

−self-adjusting version of leftist heap

− relationship of skew heap to leftist heap is analogous to splay

trees and AVL trees

−skew heaps

−binary trees with heap order

−but no structural constraint

−no information kept about null path length

−right path can be arbitrarily long

−worst case of all operations: 𝑂 𝑁

−for 𝑀 operations, total worst case: 𝑂 𝑀 log 𝑁 , or

𝑂 log 𝑁 amortized

Page 37: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Skew Heaps

37

−skew heaps (cont.)

−fundamental operation is merging

−after merging, for leftists heaps, check both children for

structure and swap children if needed

− in skew heaps, always swap children

−except largest nodes on right paths do not swap

children

−no extra space required to maintain path lengths

−no tests required to determine when to swap children

Page 38: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Skew Heaps

38

−skew heaps example

−merge two skew heaps

−tree with larger root will merge onto tree with smaller

root

Page 39: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Skew Heaps

39

−skew heaps example (cont.)

−recursively merge 𝐻2 with the subheap of 𝐻1 rooted at 8

−heap happens to be leftist

Page 40: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Skew Heaps

40

−skew heaps example (cont.)

−make this heap the new left child of 𝐻1 and the old left

child of 𝐻1 becomes the new right child

Page 41: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binomial Queues

41

−binomial queues

−keep a collection of heap-ordered trees, known as a

forest

−at most one binomial tree of every height

−heap order imposed on each binomial tree

−can represent any priority queue

−example: a priority queue of size 13 can be

represented by the forest 𝐵3, 𝐵2, 𝐵0 or 1101

−worst case of all operations:𝑂 log 𝑁

Page 42: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binomial Queues

42

−binomial queues example

Page 43: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binomial Queues

43

−binomial queue of size 6 example

Page 44: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binomial Queues

44

−binomial queue operations

−minimum element found by scanning roots of all trees

−found in 𝑂 log 𝑁

−can keep ongoing information to reduce to 𝑂 1

−merging two queues

−merge takes 𝑂 log 𝑁

Page 45: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binomial Queues

45

−merge example

−merge of two 𝐵1 trees

Page 46: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binomial Queues

46

−merge example (cont.)

−now we have three binomial trees of height 3; keep one

and merge the other two

Page 47: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binomial Queues

47

−merge values 1–7 example

− insert 1

− insert 2

− insert 3

− insert 4

Page 48: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binomial Queues

48

−merge values 1–7 example (cont.)

− insert 5

− insert 6

− insert 7

Page 49: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binomial Queues

49

−deleteMin example

−separate tree with minimum root from rest of tree

− remaining trees after removing min

Page 50: Chapter 6 Heaps - William & Marytadavis/cs303/f16/ch06.pdf · Leftist Heaps 31 −leftist heaps operations (cont.) −merging −insertion special case of merging a 1-node heap with

Binomial Queues

50

−deleteMin example (cont.)

−after merge