1 of 38

Pre-Announcements

Justin: Alpha Kappa Psi Blockchain Event, next Wednesday in Chevron Auditorium

  • Companies like Oracle, Ripple, Coinbase, etc.
  • An overview of blockchains and its implications.
  • 7 PM next Wednesday in iHouse.
    • Pizza, ice cream, and a raffle

Fliers up front if you want to do this thing.

2 of 38

Announcements

Midterm 2:

  • 3/20, 8-10 PM in various rooms.
  • Covers material through 3/16 (this Friday).
  • Study using study guides.
    • THE KEY IS METACOGNITION: Reflect on your problem solving strategies and those of your fellow students.
    • Understanding a handful of solutions to old midterm problems is less helpful than you might think -- look at answers as late as possible.
  • There is an alternate 61C midterm from 6 - 8 in 1 LeConte.

3 of 38

CS61B

Lecture 24: Priority Queues and Heaps

  • Priority Queues
  • Heaps
  • Tree Representations
  • Data Structures Summary

4 of 38

The Priority Queue Interface

Useful if you want to keep track of the “smallest”, “largest”, “best” etc. seen so far.

/** (Min) Priority Queue: Allowing tracking and removal of the

* smallest item in a priority queue. */

public interface MinPQ<Item> {

/** Adds the item to the priority queue. */

public void add(Item x);

/** Returns the smallest item in the priority queue. */

public Item getSmallest();

/** Removes the smallest item from the priority queue. */

public Item removeSmallest();

/** Returns the size of the priority queue. */

public int size();

}

5 of 38

Usage Example: Unharmonious Texts

Imagine that you’re part of Grand Leader’s Information Compliance and Happiness Enhancement (GLICHE) team.

  • Your job: Monitor the text messages of the citizens to make sure that they are not having any unharmonious conversations.
  • Each day, you prepare a report of the M messages that seem most unharmonious using the HarmoniousnessComparator.

Naive approach: Create a list of all messages sent for the entire day. Sort it using your comparator. Return the M messages that are largest.

6 of 38

Naive Implementation: Store and Sort

Potentially uses a huge amount of memory Θ(N), where N is number of texts.

  • Goal: Do this in Θ(M) memory using a MinPQ.

public List<String> unharmoniousTexts(Sniffer sniffer, int M) {

ArrayList<String> allMessages = new ArrayList<String>();

for (Timer timer = new Timer(); timer.hours() < 24; ) {

allMessages.add(sniffer.getNextMessage());

}

Comparator<String> cmptr = new HarmoniousnessComparator();

Collections.sort(allMessages, cmptr, Collections.reverseOrder());

return allMessages.sublist(0, M);

}

MinPQ<String> unharmoniousTexts = new HeapMinPQ<Transaction>(cmptr);

7 of 38

Better Implementation: Track the M Best

Can track top M transactions using only M memory. API for MinPQ also makes code very simple (don’t need to do explicit comparisons).

public List<String> unharmoniousTexts(Sniffer sniffer, int M) {

Comparator<String> cmptr = new HarmoniousnessComparator();

MinPQ<String> unharmoniousTexts = new HeapMinPQ<Transaction>(cmptr);

for (Timer timer = new Timer(); timer.hours() < 24; ) {

unharmoniousTexts.add(sniffer.getNextMessage());

if (unharmoniousTexts.size() > M)

{ unharmoniousTexts.removeSmallest(); }

}

ArrayList<String> textlist = new ArrayList<String>();

while (unharmoniousTexts.size() > 0) {

textlist.add(unharmoniousTexts.removeSmallest());

}

return textlist;

}

8 of 38

How Would We Implement a MinPQ?

Some possibilities:

  • Ordered Array
  • Bushy BST: Maintaining bushiness is annoying. Handling duplicate priorities is awkward.
  • HashTable: No good! Items go into random places.

Ordered Array

Bushy BST

Hash Table

Heap

add

Θ(N)

Θ(log N)

Θ(1)

getSmallest

Θ(1)

Θ(log N)

Θ(N)

removeSmallest

Θ(N)

Θ(log N)

Θ(N)

Caveats

Dups tough

Worst Case Θ(·) Runtimes

9 of 38

Heaps

10 of 38

Introducing the Heap

BSTs would work, but need to be kept bushy and duplicates are awkward.

Binary min-heap: Binary tree that is complete and obeys min-heap property.

  • Min-heap: Every node is less than or equal to both of its children.
  • Complete: Missing items only at the bottom level (if any), all nodes are as far left as possible.

0

5

8

0

8

1

2

6

5

8

0

8

1

6

5

8

0

8

1

2

5

4

0

8

1

6

Incomplete

Lacks min-heap property

11 of 38

Heap Comprehension Test: http://yellkey.com/baby

How many of these are min heaps?

  1. 0
  2. 1
  3. 2
  4. 3
  5. 4

8

8

8

8

8

8

6

8

5

7

2

3

4

0

7

9

5

1

0

12 of 38

Heap Comprehension Test: http://yellkey.com/present

How many of these are min heaps?

  • 0
  • 1
  • 2
  • 3
  • 4

8

8

8

8

8

8

6

8

5

7

2

3

4

0

7

9

5

1

0

Incomplete

Lacks min-heap property

13 of 38

What Good Are Heaps?

Heaps lend themselves very naturally to implementation of a priority queue.

Hopefully easy question:

  • How would you support getSmallest()?

5

8

0

8

1

6

14 of 38

How Do We Add To A Heap?

Challenge: Come up with an algorithm for add(x).

  • How would we insert 3?

Runtime must be logarithmic.

Bonus: Come up with an algorithm for removeSmallest().

Solution: See https://goo.gl/wBKdFQ for an animated demo.

5

5

1

6

1

3

6

7

7

8

?

15 of 38

Heap Operations Summary

Given a heap, how do we implement PQ operations?

  • getSmallest() - return the item in the root node.
  • add(x) - place the new employee in the last position, and promote as high as possible.
  • removeSmallest() - assassinate the president (of the company), promote the rightmost person in the company to president. Then demote repeatedly, always taking the ‘better’ successor.

See https://goo.gl/wBKdFQ for an animated demo.

Remaining question: How would we do all this in Java?

16 of 38

Tree Representations

17 of 38

How do we Represent a Tree in Java?

Approach 1a, 1b and 1c: Create mapping from node to children.

w

x

y

z

1a: Fixed-Width Nodes (BSTMap used this approach)

public class Tree1A<Key> {

Key k; // e.g. 0

Tree1A left;

Tree1A middle;

Tree1A right;

...

w

z

x

y

18 of 38

How do we Represent a Tree in Java?

Approach 1a, 1b and 1c: Create mapping from node to children.

w

x

y

z

1b: Variable-Width Nodes

w

z

x

y

public class Tree1B<Key> {

Key k; // e.g. 0

Tree1B[] children;

...

19 of 38

How do we Represent a Tree in Java?

Approach 1a, 1b and 1c: Create mapping from node to children.

w

x

y

z

1c: Sibling Tree

public class Tree1C<Key> {

Key k; // e.g. 0

Tree1C favoredChild;

Tree1C sibling;

...

w

z

x

y

20 of 38

How do we Represent a Tree in Java?

Approach 1a, 1b and 1c: Create mapping from node to children.

w

x

y

z

w

x

y

z

w

x

y

z

1a: Fixed-Width Nodes

1b: Variable-Width Nodes

1c: Sibling Tree

w

z

x

y

21 of 38

How do we Represent a Tree in Java?

Approach 2: Store keys in an array. Store parentIDs in an array.

  • Similar to what we did with disjointSets.

w

x

y

z

e

b

g

a

d

f

j

v

p

y

m

r

x

k

0

1

2

3

4

5

6

7

Key[] keys

int[] parents

0

0

0

0

k

e

v

b

g

p

y

a

d

f

j

m

r

x

Key[] keys

int[] parents

0 1 2 3

0 1 2 3 4

5 6 7 8 9 10 11 12 13

0

0

0

1

1

2

2

3

3

4

4

5

5

6

0 1 2 3 4

5 6 7 8 9 10 11 12 13

8

9

10

11

12

13

public class Tree2<Key> {

Key[] keys;

int[] parents;

...

w

z

x

y

22 of 38

How do we Represent a Tree in Java?

Approach 3: Store keys in an array. Don’t store structure anywhere.

  • To interpret array: Simply assume tree is complete.
  • Obviously only works for “complete” trees.

w

x

y

z

e

b

g

a

d

f

j

v

p

y

m

r

x

k

0

1

2

3

4

5

6

7

Key[] keys

k

e

v

b

g

p

y

a

d

f

j

m

r

x

Key[] keys

0 1 2 3 4

5 6 7 8 9 10 11 12 13

8

9

10

11

12

13

0 1 2 3

public class Tree3<Key> {

Key[] keys;

...

w

z

x

y

23 of 38

A Deep Look at Approach 3

Challenge: Write the parent(k) method for approach 3.

w

x

y

z

e

b

g

a

d

f

j

v

p

y

m

r

x

k

0

1

2

3

4

5

6

7

Key[] keys

w

z

x

y

k

e

v

b

g

p

y

a

d

f

j

m

r

x

Key[] keys

0 1 2 3 4

5 6 7 8 9 10 11 12 13

8

9

10

11

12

13

0 1 2 3

public class Tree3<Key> {

Key[] keys;

...

public void swim(int k) {

if (keys[parent(k)] ≻ keys[k]) {

swap(k, parent(k));

swim(parent(k));

}

}

24 of 38

A Deep Look at Approach 3

Challenge: Write the parent(k) method for approach 3.

w

x

y

z

e

b

g

a

d

f

j

v

p

y

m

r

x

k

0

1

2

3

4

5

6

7

Key[] keys

k

e

v

b

g

p

y

a

d

f

j

m

r

x

Key[] keys

0 1 2 3 4

5 6 7 8 9 10 11 12 13

8

9

10

11

12

13

0 1 2 3

public class Tree3<Key> {

Key[] keys;

...

public void swim(int k) {

if (keys[parent(k)] ≻ keys[k]) {

swap(k, parent(k));

swim(parent(k));

}

}

public int parent(int k) {

return (k - 1) / 2;

}

w

z

x

y

25 of 38

Tree Representations (Summary)

w

x

y

z

w

x

y

z

w

x

y

z

1a: Fixed Number of Links (One Per Child)

1b: Array of Child Links

1c: FirstBorn/Sibling Links

w

x

y

z

Key[] keys

int[] parents

0

0

0

0

0 1 2 3

w

x

y

z

Key[] keys

2: Array of Keys, Array of Structure

3: Array of Keys

w

z

x

y

26 of 38

Approach 3B (book implementation): Leaving One Empty Spot

Approach 3b: Store keys in an array. Offset everything by 1 spot.

  • Same as 3, but leave spot 0 empty.
  • Makes computation of children/parents “nicer”.
    • leftChild(k) = k*2
    • rightChild(k) = k*2 + 1
    • parent(k) = k/2

w

x

y

z

e

b

g

a

d

f

j

v

p

y

m

r

x

k

1

2

3

4

5

6

7

8

Key[] keys

w

z

x

y

k

e

v

b

g

p

y

a

d

f

j

m

r

x

Key[] keys

0 1 2 3 4

5 6 7 8 9 10 11 12 13 14

9

10

11

12

13

14

-

0 1 2 3 4

-

In next week’s lab, you’ll implement a MinPQ.

27 of 38

Heap Implementation of a Priority Queue

Notes:

  • Why “priority queue”? Can think of position in tree as its “priority.”
  • Heap is log N time AMORTIZED (some resizes, but no big deal).
  • BST can have constant getSmallest if you keep a pointer to smallest.
  • Heaps handle duplicate priorities much more naturally than BSTs.
  • Array based heaps take less memory (very roughly about 1/3rd the memory of representing a tree with approach 1a).

Ordered Array

Bushy BST

Hash Table

Heap

add

Θ(N)

Θ(log N)

Θ(1)

Θ(log N)

getSmallest

Θ(1)

Θ(log N)

Θ(N)

Θ(1)

removeSmallest

Θ(N)

Θ(log N)

Θ(N)

Θ(log N)

Items with same priority hard to handle.

28 of 38

Some Implementation Questions

  1. How does a PQ know how to determine which item in a PQ is larger?
    1. What could we change so that there is a default comparison?
  2. What constructors are needed to allow for different orderings?

/** (Min) Priority Queue: Allowing tracking and removal of the

* smallest item in a priority queue. */

public interface MinPQ<Item> {

/** Adds the item to the priority queue. */

public void add(Item x);

/** Returns the smallest item in the priority queue. */

public Item getSmallest();

/** Removes the smallest item from the priority queue. */

public Item removeSmallest();

/** Returns the size of the priority queue. */

public int size();

}

29 of 38

Data Structures Summary

30 of 38

The Search Problem

Given a stream of data, retrieve information of interest.

  • Examples:
    • Website users post to personal page. Serve content only to friends.
    • Given logs for thousands of weather stations, display weather map for specified date and time.

31 of 38

Search Data Structures (The particularly abstract ones)

Name

Storage Operation(s)

Primary Retrieval Operation

Retrieve By:

List

add(key)

insert(key, index)

get(index)

index

Map

put(key, value)

get(key)

key identity

Set

add(key)

containsKey(key)

key identity

PQ

add(key)

getSmallest()

key order (a.k.a. key size)

Disjoint Sets

connect(int1, int2)

isConnected(int1, int2)

two int values

32 of 38

Searching Data Structures:

PQ

List

Set

Map

DisjointSets

Stack

Hash Table

BST

LLRB

2-3 Tree

33 of 38

Searching Data Structures:

PQ

List

Set

Map

DisjointSets

Stack

BST

2-3 Tree

LLRBs

Separate Chaining HT

Linear Probing HT

Linked List

Resizing Arrays

Linked List

Resizing Arrays

Ordered Array

LLRB

Hash Table

Heap

2017 version

34 of 38

Searching Data Structures:

PQ

List

Set

Map

DisjointSets

Chaining HT

Linear Probing HT

LinkedList

Resizing Array

Heap

Red Black

BST (Vanilla)

B-Trees (2-3 / 2-3-4)

Heap

Chaining HT (lacks order, very odd)

Ordered Linked List

Balanced Tree

Resizing Array

LinkedList

Quick Find

Quick Union

Weighted QU

WQUPC

Stack

2016 version

35 of 38

PQ

Heap Ordered Tree

External Chaining HT

Array of Buckets

Abstraction often happens in layers!

Bucket

ArrayList

Resizing Array

LinkedList

BST (requires comparable items)

Tree

Approach 1A

Approach 1B

Approach 1C

Approach 2

Approach 3

Approach 3B

ArrayMap (lab 9)

36 of 38

Specialized Searching Data Structures:

Set

PQ

Map

Sorted Set

Sorted Map

2-3 Tree

RedBlack

BST

In Java:

java.util.SortedSet

java.util.SortedMap

List

Stack

Queue

Deque

Linked List

Resizing Array (slightly suboptimal due to resizing)

Don’t usually consider MinPQ and MaxPQ to be different data structures, since we can just provide the opposite comparator.

37 of 38

Data Structures

Data Structure: A particular way of organizing data.

  • We’ve covered many of the most fundamental abstract data types, their common implementations, and the tradeoffs thereof.
  • We’ll do two more in this class:
    • The quadtree (Friday).
    • The graph (starting after exam).

38 of 38

Citations

Title slide Andre the Giant picture: Unknown source

Friendster screenshot: http://jeremy.zawodny.com/i/friendster_rss.jpg

�Weather screenshot: weather.com