Lectures
Here we have space to list detailed reading expectations for each lecture, along with instructor commentary that may be a little tangential.
Lecture 24: Priority queues and heaps
Textbook reading
- Chapter 7: 7.19–7.21
- Chapter 8: 8.34
- Chapter 24: 24.32–24.34
- Chapter 27
Instructor’s notes
The textbook places the root of the tree at array index 1 instead of 0, which simplifies the parent/children formulas a bit.
It later reimplements reheap()
(analogous to our bubbleDown()
) using a 0-based convention for use in heapsort.
We’ll stick to the 0-based convention.
The textbook makes a point of avoiding swaps when bubbling up and down by only moving existing values and not writing the new value until its final location is known. This is a legitimate optimization, but we’ll stick with swaps because they make it easier to visualize the comparisons that are taking place.
At this point in the textbook, its method specifications range from poor to missing altogether. We’ll try to do better in our slides and demo code.
Lecture 23: Shortest paths
Textbook reading
- Chapter 29: 29.16–29.24
- Chapter 30: 30.24
Online reading
Instructor’s notes
The online reading (including videos) from JavaHyperText is much more thorough than the textbook on this topic, highlighting the loop invariant and proof of correctness.
Lecture 22: Graph traversals
Textbook reading
- Chapter 29: 29.11–29.15
- Chapter 30: 30.23
Instructor’s notes
DFS is most cleanly implemented recursively (as in lecture), which is not shown in the textbook. The textbook’s iterative implementation does a better job of matching a recursive version than others I’ve seen (e.g. JavaHyperText), but the pseudocode leaves out some details, and there is no concrete implementation in chapter 30.
The textbook’s treatment of topological sorting describes Kahn’s algorithm, but in reverse, starting with the last vertex in the order and accumulating vertices in a Stack to effectively reverse the order that they were added in. It also only provides pseudocode, leaving out details for how to efficiently find nodes with no successors after vertices have been removed. The lecture slides and demo code show the “forward” Kahn’s algorithm, including an efficient way of identifying nodes with no predecessors. Discussion activity 11 will introduce an alternative algorithm that uses depth-first search instead (which I find more elegant). It turns out that reversing the DFS “settled” order gives a topological order.
Lecture 21: Graphs
Textbook reading
- Chapter 29: 29.1–29.10, 29.25–29.26
- Chapter 30: 30.1–30.22
Instructor’s notes
We will cover chapters 29 and 30 over the course of two or three lectures. The graph algorithms parts of the chapters are the main sections we skip in this introductory lecture. So for today’s reading you can skip over anything about traversals or shortest paths; we’ll come back to those later. Do concentrate on terminology and on representations.
Lecture 20: Synchronization
Online reading
Instructor’s notes
The textbook does not cover concurrency. The Java Tutorial lesson Concurrency is a good reference for this lecture and the previous. For more detail, including lots of examples, read Java Concurrency in Practice. You should be familiar with the following general concepts:
- Producer–consumer problems
- Ring buffers
- Locks (mutexes) and the
synchronized
keyword - Deadlock
- Condition variables:
wait()
andnotifyAll()
Lecture 19: Concurrency
Online reading
- Processes and Threads
- Thread Objects (including subsections)
- Thread Interference, Memory Consistency Errors
Instructor’s notes
The textbook does not cover concurrency. The Java Tutorial lesson Concurrency is a good reference for this lecture and the next. You should be familiar with the following general concepts:
- Threads of execution
- Java’s
Thread
class Runnable
- Concurrent execution via time slicing or parallelism
- Race conditions
Lecture 18: Event-driven programming
Online reading
Instructor’s notes
The textbook does not cover graphical user interfaces, and there are too many classes and methods pr ovided by the Swing framework to ask you to read about them all. But you should be faimilar with the following general concepts:
- Inversion of control
- Events, event sources, and event handlers
- The Event Dispatch Thread (EDT)
- Painting
The Java Tutorial lesson Writing Event Listeners is a good reference, and I recommend reading at least the first two sections to supplement our slides. The lesson Concurrency in Swing is also important and relates to our lecture on concurrency.
Lambda expressions have many applications outside of GUIs.
You have already seen assertThrows()
in JUnit tests for assignments.
Lecture 17: Graphical User Interfaces
Instructor’s notes
The textbook does not cover graphical user interfaces, and there are too many classes and methods provided by the Swing framework to ask you to read about them all. But you should be faimilar with the following general concepts:
- Windows and
JFrame
- Containers and layout managers
- Components and common subclasses (buttons, labels, text fields)
- Separating concerns into Model, View, and Controller (MVC)
The Java Tutorial trail Creating a GUI With Swing is a good reference, and sections of it are required reading for A5. Two pages relevant to this lecture are Using Top-Level Containers and A Visual Guide to Layout Managers.
Lecture 16: Dictionaries and hashing
Textbook reading
- Chapter 20
- Chapter 22: 22.1–22.8, 22.10–22.15, 22.24–22.26
- Chapter 23: 23.1–23.2, 23.6–23.8, 23.16–23.17
Instructor’s notes
Lecture 15: Sorting
Textbook reading
- Chapter 15: 15.1–15.14
- Chapter 16: 16.1–16.7, 16.9–16.19, 16.23
Instructor’s notes
The textbook’s implementation of partition()
is more complicated than what we present in class and requires a non-trivial precondition (a minimum array size of 4).
I think our version, derived from a simple loop invariant, is much easier to reason about and implement correctly.
That being said, the textbook’s version can produce a more balanced partition when lots of duplicate elements equal to the pivot value are present.
Note that the textbook’s functions operate on closed array ranges (e.g. a[first..last]
), whereas lecture examples preferred half-open ranges (e.g. a[begin..end)
).
Using half-open ranges avoids a lot of +1
s and -1
s and is the convention adopted by many library functions in both Java and C++.
Lecture 14: Loop invariants, searching
Online reading
Textbook reading
- Chapter 19
Lecture 13: Trees
Textbook reading
- Chapter 24: 24.1–24.17, 24.22–22.24, 24.28–24.31
- Chapter 25: 25.1–25.2, 25.9–25.11, 25.18
- Chapter 26: 26.1–26.18, 26.40–26.43
Instructor’s notes
The textbook implements tree traversals using Iterator
s so that the client is responsible for whatever operation should be done when each node is “visited.”
This is a good design, but we have not discussed how to implement Iterator
s yet in this class, and they are not quite as intuitive as a recursive implementation, since they need to explicitly maintain state between calls.
I find the textbook’s definition of a TreeInterface
to be a little superfluous.
Seen as a recursive data structure, a Node
class is sufficient to represent a tree data structure, which could then be used to implement ADTs like Set, Dictionary, and Priority Queue, as well as more application-specific data types.
Lecture 12: Recursion
Textbook reading
- Chapter 9
Lecture 11: Efficiency
Textbook reading
- Chapter 4