Priority queues Hash tables chaining Priority queue ADT Binary - - PowerPoint PPT Presentation

priority queues
SMART_READER_LITE
LIVE PREVIEW

Priority queues Hash tables chaining Priority queue ADT Binary - - PowerPoint PPT Presentation

Priority queues Hash tables chaining Priority queue ADT Binary heap March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1 Separate chaining Separate chaining takes a different approach to collisions Each entry in the hash table


slide-1
SLIDE 1

Priority queues

Hash tables – chaining Priority queue ADT Binary heap

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1

slide-2
SLIDE 2

Separate chaining

  • Separate chaining takes a different approach to collisions
  • Each entry in the hash table is a pointer to a linked list (or
  • ther dictionary-compatible data structure)

– If a collision occurs the new item is added to the end of the list at the appropriate location

  • Performance degrades less rapidly using separate chaining

– with uniform random distribution, separate chaining maintains good performance even at high load factors 𝜇 > 1

Cinda Heeren / Andy Roth / Geoffrey Tien 2 March 13, 2020

slide-3
SLIDE 3

Separate chaining example

  • ℎ 𝑦 = 𝑦 mod 23
  • Insert 81, ℎ(𝑦) = 12, add to back (or front) of list
  • Insert 35, ℎ(𝑦) = 12
  • Insert 60, ℎ(𝑦) = 14

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 29 32 58 21 81 35 60

slide-4
SLIDE 4

Hash table discussion

  • If 𝜇 is less than ½, open addressing and separate chaining give

similar performance

– As 𝜇 increases, separate chaining performs better than open addressing – However, separate chaining increases storage overhead for the linked list pointers

  • It is important to note that in the worst case hash table

performance can be poor

– That is, if the hash function does not evenly distribute data across the table

Cinda Heeren / Andy Roth / Geoffrey Tien 4 March 13, 2020

slide-5
SLIDE 5

Priority queue

Priority queue ADT Binary heap

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5

slide-6
SLIDE 6

Priority queues?

  • Suppose Geoff has made a to-do list (with priority values):

6 – HW2 regrades 2 – Vacuum home 8 – Renew car insurance 1 – Sleep 9 – Extinguish computer on fire 2 – Eat 8 – Lecture prep 1 – Bathe

  • We are interested in quickly finding the task with the highest

priority

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 6

slide-7
SLIDE 7

Priority queue ADT

  • A collection organised so as to allow fast access to and

removal of the largest (or smallest) element

– Prioritisation is a weaker condition than ordering – Order of insertion is irrelevant – Element with the highest priority (whatever that means) is the first element to be removed

  • Not really a queue: not FIFO!

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7

slide-8
SLIDE 8

Priority queue ADT

  • Priority queue operations

– create – destroy – insert – removeMin (or removeMax) – isEmpty

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 8

  • Priority queue property

– For two elements 𝑦 and 𝑧 in the queue, if 𝑦 has a higher priority value than 𝑧, 𝑦 will be removed before 𝑧 – Note that in most definitions, a single priority queue structure supports

  • nly removeMin, or only removeMax, and not both

G(9) D(100) G(9) C(3) F(7) E(5) A(4) B(6) insert D(100) removeMax

slide-9
SLIDE 9

Priority queue properties

  • A priority queue is an ADT that maintains a multiset of items

– vs set: a multiset allows duplicate entries

  • Two or more distinct items in a priority queue may have the

same priority

  • If all items have the same priority, will it behave FIFO like a

queue?

– not necessarily! Due to implementation details

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 9

slide-10
SLIDE 10

Priority queue applications

  • Hold jobs for a printer in order of size
  • Manage limited resources such as bandwidth on a transmission

line from a network router

  • Sort numbers
  • Anything greedy: an algorithm that makes the "locally best

choice" at each step

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 10

slide-11
SLIDE 11

Data structures for priority queues

  • Worst case complexities

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 11

Structure insert removeMin Unordered array 𝑃 1 𝑃 𝑜 Ordered array 𝑃 𝑜 𝑃 𝑜 Unordered list 𝑃 1 𝑃 𝑜 Ordered list 𝑃 𝑜 𝑃 1 Binary search tree 𝑃 𝑜 𝑃 𝑜 AVL tree 𝑃 log 𝑜 𝑃 log 𝑜 Binary heap 𝑃 log 𝑜 𝑃 log 𝑜

Heap has asymptotically same performance as AVL tree, but MUCH simpler to implement

slide-12
SLIDE 12

Binary heap

  • A heap is binary tree with two properties
  • Heaps are complete

– All levels, except the bottom, must be completely filled in – The leaves on the bottom level are as far to the left as possible

  • Heaps are partially ordered

– For a max heap – the value of a node is at least as large as its children’s values – For a min heap – the value of a node is no greater than its children’s values

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 12

A complete, partially-ordered binary tree

slide-13
SLIDE 13

Review: complete binary trees

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 13

complete binary trees incomplete binary trees

slide-14
SLIDE 14

Partially ordered tree

  • max heap example

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 14

98 86 41 13 65 32 29 9 10 44 23 21 32 Heaps are not fully ordered – an in-order traversal would result in: 9, 13, 10, 86, 44, 65, 23, 98, 21, 32, 32, 41, 29

slide-15
SLIDE 15

Duplicate priority values

  • It is important to realise that two binary heaps can contain the

same data, but items may appear in different positions in the structure

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 15

Min heap examples

2 5 7 5 7 8 2 5 5 7 7 8

slide-16
SLIDE 16

Heap implementation

  • Heaps can be implemented

using arrays

  • There is a natural method of

indexing tree nodes

– Index nodes from top to bottom and left to right as shown on the right – Because heaps are complete binary trees there can be no gaps in the array (except index 0)

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 16

Using an array

1 2 3 4 5 6 7 1 2 3 4 5 6 .. .

slide-17
SLIDE 17

Referencing nodes

  • To move around in the tree, it will be necessary to find the index
  • f the parents of a node

– or the children of a node

  • The array is indexed from 1 to 𝑜
  • Each level’s nodes are indexed from:
  • 2𝑚𝑓𝑤𝑓𝑚 to 2𝑚𝑓𝑤𝑓𝑚+1 − 1 (where the root is level 0)

– The children of a node 𝑗, are the array elements indexed at 2𝑗 and 2𝑗 + 1 – The parent of a node 𝑗, is the array element indexed at 𝑗 2

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 17

slide-18
SLIDE 18

Array heap example

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 18

98 86 41 13 65 32 29 9 10 44 23 21 32 1 2 3 4 5 6 7 8 9 10 11 12 13 Heap Underlying array 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 32 10 11 12 13 8 9 value index

slide-19
SLIDE 19

Heap implementation

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 19

class MinHeap { private: int size; // number of stored elements int capacity; // maximum capacity of array int* arr; // array in dynamic memory public: ... }; MinHeap::MinHeap(int initcapacity) { size = 0; capacity = initcapacity; arr = new int[capacity+1]; }

slide-20
SLIDE 20

Heap insertion

  • On insertion the heap properties have to be maintained

– A heap is a complete binary tree and – A partially ordered binary tree

  • The insertion algorithm first ensures that the tree is complete

– new item is the first available (left-most) leaf on the bottom level – i.e. the first free element in the underlying array

  • Fix the partial ordering

– Compare the new value to its parent – Swap them if the new value is greater than the parent – Repeat until this is not the case

  • Referred to as heapify up, percolate up, or trickle up, bubble up, etc.

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 20

slide-21
SLIDE 21

Heap insertion example

  • max heap

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 21

98 86 41 13 65 32 29 9 10 44 23 21 32 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 32 10 11 12 13 8 9 value index 14 Insert 81

slide-22
SLIDE 22

Heap insertion example

  • max heap

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 22

98 86 41 13 65 32 29 9 10 44 23 21 32 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 32 10 11 12 13 8 9 value index 81 14 Insert 81 81 81 29 parent: 14 2 = 7 81 29

slide-23
SLIDE 23

Heap insertion example

  • max heap

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 23

98 86 41 13 65 32 81 9 10 44 23 21 32 98 86 41 13 65 32 81 9 10 1 2 3 4 5 6 7 44 23 21 32 10 11 12 13 8 9 value index 29 14 Insert 81 29 81 41 parent: 7 2 = 3 81 41 81 is less than 98 so finished

slide-24
SLIDE 24

Heap insertion complexity

  • Item is inserted at the bottom level in the first available space

– this can be tracked using the heap size attribute – 𝑃 1 access using array index

  • Repeated heapify-up operations. How many?

– each heapify-up operation moves the inserted value up one level in the tree – Upper limit on the number of levels in a complete tree? 𝑃 log 𝑜

  • Heap insertion has worst-case performance of 𝑃 log 𝑜

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 24

slide-25
SLIDE 25

Building a heap (an heap?)

  • A heap can be constructed by repeatedly inserting items into

an empty heap. Complexity?

– 𝑃 log 𝑜 per item, 𝑃 𝑜 items – 𝑃 𝑜 log 𝑜 total

  • More about this next class

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 25

slide-26
SLIDE 26

Readings for this lesson

  • Carrano & Henry

– Chapter 13.3 (ADT priority queue) – Chapter 17.1 – 17.3 (Heap)

  • Next class:

– Carrano & Henry, Chapter 17.4 (Heapsort)

March 13, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 26