1 of 30

Lecture 23: Projects

& Priority Queues

CS 136: Spring 2024

Katie Keith

2 of 30

Record on Zoom

3 of 30

  • Lab: 1-on-1s for Lab 6
  • Lab 7 this week
  • CS Colloquium (2:35pm Friday): Pre-registration information session

📣 Announcements

4 of 30

  • Final Project Specifications
  • (Briefly) Assertions
  • Priority Queues
  • Binary Heaps

🎯 Today’s Learning Objectives

5 of 30

📚Readings

  • Sedgewick and Wayne. Algorithms. Section 2.4.

6 of 30

Final Project Outline

  • Demo two outstanding projects from last spring
  • Discuss Final Project Specifications

Groups of two!

7 of 30

Tip: Choose a project that you are both excited about

Source: Generated by ChatGPT 4o with the prompt “Create a 3D cartoon image of computer science students being extremely excited, in love, and joyful about their computer science final projects. "

Application possibilities:

  • Games
  • Analyzing/Visualizing Data
  • Exploring CS/Math concepts

8 of 30

Demo #1: Conway's Game of Life on a Torus

Start with a graph structure and then connect it…

A torus structure!

javac -d bin torusGameOfLife/*.java

java -cp bin torusGameOfLife.Panel

The original Conway’s Game of Life Game

9 of 30

Demo #2: Mapping New York City

Excellent example of a thorough README.md

Database of over 850,000 taxplots (roughly corresponding to individual buildings) in NYC and information on each taxplot's location, address, land use type, owner, and more

10 of 30

Final Project Outline

  • Demo two outstanding projects from last spring
  • Discuss Final Project Specifications

11 of 30

Git/Github workflow

12 of 30

My goal: Help everyone succeed on this final project!

Source: Generated by ChatGPT 4o with the prompt “Create a 3-D cartoon image of a female computer science professor emanating "I’m on your team! Use me as a resource!"

13 of 30

  • Final Project Specifications
  • (Briefly) Assertions
  • Priority Queues
  • Binary Heaps

🎯 Today’s Learning Objectives

14 of 30

Assert Statements

assert true == true : "Should never see this message…";

assert true == false : "Bad! True does not equal false.";

An assert statement verifies that a specific condition holds true at a certain point in the execution of a program.

Examples in Java:

reserved keyword

boolean expression we wish to check

message if the boolean expression is false

15 of 30

Using assertions

javac Recurse.java

java -ea Recurse

Assertions in Java are disabled by default at runtime. This is because assertions are typically used during testing and development, rather than production.

To enable assertions, use the -ea command line option at execution time.

Example:

stands for “enable assertions”

16 of 30

  • Final Project Specifications
  • (Briefly) Assertions
  • Priority Queues
  • Binary Heaps

🎯 Today’s Learning Objectives

17 of 30

Priority Queues

Priority queues are an ADT that removes the largest (smallest) item using an operation called poll.

Insert

Poll

(Return and remove max element)

Note: Like queues, we can have duplicate items

18 of 30

Where do we use priority queues?

Dijkstra’s algorithm

(Shortest path in weighted graph)

A* search algorithm

(AI pathfinding)

Priority queues are helpful for applications that need to return a max/min value but don’t need the data in full sorted order.

19 of 30

Using a Priority Queue in Java

import java.util.PriorityQueue;

PriorityQueue<Integer> pq = new PriorityQueue<>(); // natural order → minimum is

// highest priority

pq.add(5);

pq.add(2);

pq.add(10);

System.out.println(pq.poll()); // Prints 2 (O(log n))

Data structure next: Binary heap

Can also pass in a Comparator to the constructor to change priority

20 of 30

Complete Binary Tree

Bottom level filled from the left

21 of 30

Heap-ordered complete binary tree

(Max) Heap-ordered complete binary tree:

  • Keys are in nodes
  • A parent node’s key ≥ each child’s key

Note: NOT completely sorted like a Binary Search Tree

22 of 30

4

7

A

B

C

6

6

6

For each of the trees below, choose true/false for the statement “this tree is a heap-ordered complete binary tree”.

💡Think-pair-share

23 of 30

Data structure for Priority Queues: Binary heap

A binary heap is an array representation of a heap-ordered complete binary tree.

Binary heap (array representation):

  • Take nodes in level order (take all nodes at current level before moving onto the next level)
  • No explicit links needed!
  • Indices start at 1 (explained on next slide)

Index in array

(Max) Heap-ordered complete binary tree:

  • Keys are in nodes
  • A parent node’s key ≥ each child’s key

24 of 30

Properties of Binary Heaps

We begin at index 1 (not 0) because it makes for “easy” arithmetic for parent-child indices

Integer division

25 of 30

Max Binary Heap: Insertion

To insert a node into a binary heap, add the node at the bottom of the tree and swim up (swap a node with its parent whenever the heap order is violated, until the new node is in the correct spot).

26 of 30

Max Binary Heap: poll (return and remove maximum)

To poll (remove the maximum node) in a binary heap, first, exchange root with node at end. Then sink down (swap a node with its the larger of its two child until the heap property holds again).

Note: For min binary heaps, it would be the smaller of the two children

27 of 30

For the tree below,

  1. First, write its representation in the array.
  2. Rewrite the tree and array after calling poll.

Array larger than may be needed

💡Think-pair-share

28 of 30

Analysis of Binary Heaps

insert

poll

Binary Heaps

(Worst case)

O(log n)

O(log n)

Fast! So we can take advantage of this data structure in other algorithms

A* search algorithm

(AI pathfinding)

29 of 30

Analysis of Symbol Table’s Data Structures

Data Structure

put

get

(Unordered) Linked List

O(n)

O(n)

Two arrays (keys sorted, binary search)

O(n)

O(log n)

Binary Search Tree

Ave = O(log n)

Worst = O(n)

Ave = O(log n)

Worst = O(n)

Hash Table (with separate chaining)

Ave = O(1)*

Worst= O(n)

Ave = O(1)*

Worst= O(n)

Hash table (with linear probing)

Ave = O(1)*

Worst= O(n)

Ave = O(1)*

Worst= O(n)

*Depends on uniform hashing

Classic time-space tradeoff

If all we care about is poll (max/min), then a binary heap has better worst-case guarantees than symbol tables

30 of 30

  • Final Project Specifications
  • (Briefly) Assertions
  • Priority Queues
  • Binary Heaps

🎯 Today’s Learning Objectives