Lecture 5

Priority queues

ADT

Alt text

class entry: def __init__(self, k, v): self._key = k self._value = v def __lt__(self, other): return self._key < other._key def get_key(self): return self._key

Comparator ADT

# 2D coordinate class stub class point: def __init__(self, x, y): self._x = x self._y = y def get_x(self): return self._x def get_y(self): return self._y # Less than ‘<‘ operator def __lt__(self, other): xa = self.get_x() ya = self.get_y() xb = other.get_x() yb = other.get_y() if xa == xb: return ya < yb return xa < xb

Sequence-based priority queue

Heaps

Heap properties

Insertion into a heap

Upheap

addition of key 3 addition of key 4 final result
Alt text Alt text Alt text

removal from a heap

removeMin in the PQ ADT corresponds to removing the root key from the heap

  1. Replace the root key with the key of the last node w
  2. Remove w
  3. Restore the heap-order property (downheap)

Downheap

removeMin intemediate step result
Alt text Alt text Alt text

Updating/Inserting another node

Array-Based Heap Implementation

Heap-sort

Heap construction

Merging two heaps

initial add key: intemediate step result
Alt text Alt text Alt text

Analysis

Summary:

Performance artefact
O(n)O(n) space
O(n)O(n) build
O(logn)O(\log n) getMin()

Adaptable priority queues

ADT Adaptable Priority Queue

Method Description
remove(e) remove an arbitrary element (not necessarily the minimum or maximum)
replaceKey(e, k) Essentially a priority update
replaceValue(e, v) changing the value of the item

Alt text

Locating entries

Alt text

Location-Aware Entries

List implementation

Alt text

List APQ Run time

Heap APQ Run Time

Operation performance
replaceValue(e, x) O(1)O(1)
remove(e) O(logn)O(\log n)
replaceKey(e, k) O(logn)O(\log n)

Performance

Method Unsorted List Sorted List Heap
size, isEmpty O(1)O(1) O(1)O(1) O(1)O(1)
insert O(1)O(1) O(n)O(n) O(logn)O(\log n)
min O(n)O(n) O(1)O(1) O(1)O(1)
removeMin O(n)O(n) O(1)O(1) O(logn)O(\log n)
remove O(1)O(1) O(1)O(1) O(logn)O(\log n)
replaceKey O(1)O(1) O(n)O(n) O(logn)O(\log n)
replaceValue O(1)O(1) O(1)O(1) O(1)O(1)

Min-heap vs maxheap

Min-heap

Can we do removeMin() in O(1)O(1)?

Maps

Map-ADT

Function Description
get(k)
put(k, v)
remove(k)
size()
isEmpty()
entrySet() yield a collection of key/value pairs - python items()
keySet() yield an collection of keys
values() yield a collection of values

Use of Null as Sentinel

Alt text

put(2, e): this particular implementation returns the old value

Comparison with Earlier Data Structures

Simple List-Based Map

Implemented using an unsorted list

Alt text

get(k) Algorithm. Complexity: O(n)O(n)

Algorithm get(k): B = S.positions() {B is an iterator of the positions in S} while B.hasNext() do p = B.next() { the next position in B } if p.element().getKey() = k then return p.element().getValue() return null {there is no entry with key equal to k}

put(k) Algorithm. Complexity: O(n)O(n)

Algorithm put(k, v): B = S.positions() while B.hasNext() do p = B.next() if p.element().getKey() = k then t = p.element().getValue() S.set(p,(k,v)) return t {return the old value} S.addLast((k,v)) n = n + 1 {increment variable storing number of entries} return null {there was no entry with key equal to k}

Multimaps

Sets

Set ADT:

Function name Description
add(e) Adds the element e to S (if not already present)
remove(e) Removes the element e from SS if present
contains(e) Returns whether e is an element of S
iterator() Returns an iterators of the elements of S
addAll(T) Updates SS to include all elements of set TT, essentially STS \cup T
retainAll(T) Updates SS so that is only keeps elements that are also elements of set tt, Effectively replacing SS by STS \cap T
removeAll(T) Updates by removing any of its elements that also occur in set TT, essentially STS - T

Intuition: Union on sorted lists

Generalised merge of two sorted lists A and B