CS 15-122: Principles of Imperative Computation
(Spring 2018)

Schedule of Classes

At a glance ...

January 2018
UMTWRFS
 123456
78910111213
14151617181920
21222324252627
28293031   
       
February 2018
UMTWRFS
    123
45678910
11121314151617
18192021222324
25262728   
       
March 2018
UMTWRFS
    123
45678910
11121314151617
18192021222324
25262728293031
       
April 2018
UMTWRFS
1234567
891011121314
15161718192021
22232425262728
2930     
       
May 2018
UMTWRFS
  12345
6789101112
13141516171819
20212223242526
2728293031  
       

Outline[+]


Mon 15 Jan
No class (Martin Luther King Day)
Tue 16 Jan
Lecture 1
Welcome and Course Introduction
We outline the course, its goals, and talk about various administrative issues.

A mysterious function ...
We examine a program we know nothing about, making hypotheses about what it is supposed to do. We notice that this function has no meaningful output for some inputs, which leads us to restricting its valid inputs using preconditions. We use a similar mechanism, postconditions, to describe the value it returns. Along the way, we get acquainted to C0 and its support for checking pre- and post-conditions. We then notice that this function doesn't return the expected outputs even for some valid inputs ...
Concepts:
  • Pre- and post-conditions
  • Testing
  • Contract support in C0
Readings:
Thu 18 Jan
Lecture 2
Contracts
Contracts are program annotations that spell out what the code is supposed to do. They are the key to connecting algorithmic ideas to their implementation as a program. In this lecture, we illustrate the use of contracts by means of a simple C0 program. As we do so, we learn to verify loop invariants — an important type of contract, we see how contracts can help us write correct code, and we get acquainted with C0's automated support to validating contracts.
Concepts:
  • Loop invariants
  • Assertions
  • Using contracts to write correct programs
  • Contract support in C0
Fri 19 Jan
Recitation 1
C0 Basics
This recitation reviews elementary C0 constructs and practices reasoning about code.
Sat 20 Jan
Lab 1
Setup
This lab practices using Linux and running the C0 interpreter and compiler.
Tue 23 Jan
Lecture 3
Ints
In this lecture, we explore how number representation interplays with the the ability to write effective contracts. We focus on integers and see how the binary representation called two's complement supports the laws of modular arithmetic, which C0 embraces. We also examine operations that allow exploiting the bit pattern of a binary number to achieve compact representations of other entities of interest, and to manipulate them.
  • Representation of integers
  • Two's complement
  • Modular arithmetic
  • Bit-level operations
Thu 25 Jan
Lecture 4
Arrays
In this lecture, we examine arrays as our first composite data structure, i.e., a data construction designed to hold multiple values, together with operations to access them. Accessing an array element outside of its range is undefined — it is a safety violation — and we see how to use contracts, in particular loop invariants, to ensure the safety of array accesses in a program. Arrays are stored in memory, which means that they are manipulated through an address. This raises the possibility of aliasing, a notorious source of bugs in carelessly written programs.
  • Arrays
  • Memory allocation
  • Safe access
  • Loop invariants for arrays
  • Aliasing
Fri 26 Jan
Recitation 2
A Bit about Bytes
This recitation practices base conversion and writing code that manipulates bits.
Mon 29 Jan
Lab 2
A Reversal of Fortune
This lab practices working with bitwise operations on integers and with arrays.
Tue 30 Jan
Lecture 5
Searching Arrays
We practice reasoning about arrays by implementing a function that searches whether an array contains a given value — this is the gateway to a whole class of useful operations. We notice that this function returns its result more quickly when the array is sorted. We write a specialized variant that assumes that the array is sorted, and show that it works correctly by reasoning about array bounds. The first (simpler but less efficient) version acts as a specification for the the second (slightly more complex but often faster). Using the specification in the contract for the implementation is a standard technique to help writing correct code.
  • Linear search
  • Reasoning about arrays
  • Sorted arrays
  • Performance as number of operations executed
  • Specification vs. implementation
Thu 1 Feb
Lecture 6
Sorting Arrays
We examine big-O notation as a mathematical tool to describe the running time of algorithms, especially for the purpose of comparing two algorithms that solve the same problem. As a case study, we use the problem of sorting an array, and for now a single sorting algorithm, selection sort. As we implement selection sort, we see that starting with contracts gives us high confidence that the resulting code will work correctly. Along the way, we develop a useful library of functions about sorted arrays to be used in contracts.
  • Big-O notation
  • Selection sort
  • Deliberate programming
  • Asymptotic complexity analysis
Fri 2 Feb
Recitation 3
Function Family Reunion
This recitation practices understanding and using big-O notation.
Mon 5 Feb
Lab 3
Loopty-Loopty Loop
This lab practices testing and timing running code to estimate its complexity.
Tue 6 Feb
Lecture 7
Binary search
When searching for a value in a sorted array, examining the middle element allows us to discard half of the array in the worst case. The resulting algorithm, binary search, has logarithmic complexity which is much better than linear search (which is linear). Achieving a correct imperative implementation can be tricky however, and we use once more contracts as a mechanism to reach this goal.
  • Binary search
  • Divide-and-conquer
  • Deliberate implementation
  • Checking complex loop invariants
Thu 8 Feb
Lecture 8
Quicksort
We use the key idea underlying binary search to implement two sorting algorithms with better complexity than selection sort. We examine one of them, quicksort, in detail, again using contracts to achieve a correct implementation, this time a recursive implementation. We observe that the asymptotic complexity of quicksort depends on the the value of a quantity the algorithm use (the pivot) and discuss ways to reduce the chances of making a bad choice for it. We conclude by examining another sorting algorithm, mergesort, which is immune from this issue.
  • Quicksort
  • Deliberate programming
  • Recursion
  • Best, average, and worst case complexity
  • Randomness
  • Choosing an algorithm for a problem
Fri 9 Feb
Recitation 4
A Strange Sort of Proof
This recitation reviews proving the correctness of functions.
Mon 12 Feb
Lab 4
TA Training
This lab practices working with algorithms with radically different complexity for the same problem.
Tue 13 Feb
Lecture 9
Data structures
Arrays are homogeneous collections, where all components have the same type. structs enable building heterogeneous collections, that allow combining components of different types. They are key to building pervasively used data structures. In C0, a struct resides in allocated memory and is accessed through an address, which brings up a new form of safety violation: the NULL pointer violation. We extend the language of contracts to reason about pointers.
Now that we have a two ways to build complex collections, we start exploring the idea of segregating the definition of a data structure and the operations that manipulate it into a library. Code that uses this data structure only needs to be aware of the type, operations and invariants of the data structure, not the way they are implemented. This is the basis of a form of modular programming called abstract data types, in which client code uses a data structure exclusively through an interface without being aware of the underlying implementation.
  • struct
  • Pointers
  • Abstract data types
  • Interfaces, client code and library code
  • Data structure invariants
  • Testing
Thu 15 Feb
Lecture 10
Stacks and Queues
In this lecture, we examine the interface of two fundamental data structures, stacks and queues. We practice using the exported functions to write client code that implements operations of stacks and queues that are not provided by the interface. By relying only of the interface functions and their contracts, we can write code that is correct for any implementation of stacks and queues.
  • Interface of stacks and queues
  • Using an interface
Fri 16 Feb
Recitation 5
A queue_t Interface
This recitation practices programming against an interface.
Mon 19 Feb
Lab 5
Misclaculation
This lab practices understanding postfix notation and stack-based machines.
Tue 20 Feb
Lecture 11
Linked Lists
We observe that we can implement array-like collections using a struct that packages each element with a pointer to the next element. This idea underlies linked lists, a data structure pervasively used in computer science. Writing correct code about linked lists is however tricky as we often rely on stricter invariants than natively supported, in particular the absence of cycles. We develop code to be used in contracts to check for common such properties. We then use linked lists to write code that implements the stack interface, and similarly for queues. We could have given an array-based implementation, and we note the advantages and drawbacks of each choice.
  • Linked lists
  • Checking data structure invariants
  • Linked list implementation of stacks and queues
  • Choosing an implementation: trade-offs
Thu 22 Feb
Midterm 1
Midterm 1
Fri 23 Feb
Recitation 6
Link it All Together
This recitation practices working with linked lists.
Mon 26 Feb
Lab 6
List(en) Up!
This lab practices working with linked lists.
Tue 27 Feb
Lecture 12
Unbounded Arrays
When implementing a data structure for a homogeneous collection, using an array has the advantage that each element can be accessed in constant time, but the drawback that we must fix the number of elements a priori. Linked lists can have arbitrarily length but access takes linear time. Can we have the best of both worlds? Unbounded arrays rely on an array to store the data, but double it when we run out of place for new elements. The effect is that adding an element can be either very cheap or very expensive depending on how full the array is. However, a series of insertions will appear as if each one of them takes constant time in average. Showing that this is the case requires a technique called amortized analysis, which we explore at length in this lecture.
  • Better trade-offs
  • Amortized analysis
  • Unbounded arrays
Thu 1 Mar
Lecture 13
Hash Tables
Associative arrays are data structures that allow efficiently retrieving a value on the basis of a key: arrays are the special case where valid indices into the array are the only possible keys. One popular way to implement associative arrays is to use hash tables, which computes an array index out of each key and uses that index to find the associated value. However, multiple keys can map to the same index, something called a collision. We discuss several approaches to dealing with collisions, focusing on one called separate chaining. The cost of access depends on the contents of the hash table. While a worst case analysis is useful, it is not typically representative of normal usage. We compute the average case complexity of an access relative to as few simple parameters of the hash table.
  • Associative arrays AKA dictionaries AKA maps
  • Implementation using hash tables
  • Dealing with collisions
  • Randomness
  • Average case complexity
  • Genericity — part I: void pointers
Fri 2 Mar
Recitation 7
waiting on id recitation7...
Mon 5 Mar
Lab 7
Hashing
This lab practices understanding collisions in hash functions.
Tue 6 Mar
Lecture 14
Hash Dictionaries
In this lecture, we look at the interface of modular code at greater depth, using hash functions as a case study. In this and many example, it is useful for the client to fill in some parameters used by the library code so that it delivers the functionalities needed by the client. One such parameter is the type of some quantities the library acts upon, keys in our case. It is also often the case that the client wants to provide some of the operations used by the library, here how to hash a key and how to compare elements. This is a first step towards making libraries generic, so that they implement the general functionalities of a data structure but let the client choose specific details.
  • Adaptable libraries
  • Client-supplied operations
Thu 8 Mar
Lecture 15
Generic Data Structures
In large (and not-so-large) systems, it is common to make multiple uses of the same library, each instantiated with different parameters. This is not possible in C0, however. To achieve this goal, we look at a richer language, called C1. C1 provides two new features: generic pointers and function pointers. Generic pointers, void * in C, allow a same library type to be instantiated with different client types at once, which gives us a way to use a hash table library with both integers and strings as keys for example. Function pointers allow a library to be instantiated with different client-supplied operations in the same program.
  • Genericity — part II: function pointers
Fri 9 Mar
waiting on id h2...
Mon 12 Mar
waiting on id break...
Tue 13 Mar
Thu 15 Mar
Fri 16 Mar
Mon 19 Mar
Lab 8
Legacy of the void*
This lab practices defining generic libraries.
Tue 20 Mar
Lecture 16
Binary Search Trees
We discuss trees as an approach to representing a collection, and binary search trees as an effective data structure to store and operate on sorted data. In particular, most operations on balanced binary search trees have a logarithm cost on the number of contained data. Binary search trees can however become unbalanced over time.
  • Trees
  • Binary search trees
  • Ordering invariant
  • Exponential speedup
Thu 22 Mar
Lecture 17
AVL trees
Self-balancing trees guarantee the performance advantages of binary search trees by making sure that common operations keep the tree roughly balanced. This assurance comes at the price of more complex code for these operations, which rely on more complex invariants to tame it.
  • AVL trees
  • AVL invariants
  • Rotations
  • Experimental validation
Fri 23 Mar
Recitation 8
waiting on id recitation8...
Mon 26 Mar
Lab 9
waiting on id lab9...
Tue 27 Mar
Lecture 18
waiting on id lecture18...
Thu 29 Mar
Lecture 19
waiting on id lecture19...
Fri 30 Mar
Recitation 9
waiting on id recitation9...
Mon 2 Apr
Lab 10
waiting on id lab10...
Tue 3 Apr
Lecture 20
waiting on id lecture20...
Thu 5 Apr
Midterm 2
waiting on id e2...
Fri 6 Apr
Recitation 10
waiting on id recitation10...
Mon 9 Apr
Lab 11
waiting on id lab11...
Tue 10 Apr
Lecture 21
waiting on id lecture21...
Thu 12 Apr
Lecture 22
waiting on id lecture22...
Fri 13 Apr
Recitation 11
waiting on id recitation11...
Mon 16 Apr
Lab 12
waiting on id lab12...
Tue 17 Apr
Lecture 23
waiting on id lecture23...
Thu 19 Apr
waiting on id h3...
Fri 20 Apr
waiting on id h4...
Mon 23 Apr
Lab 13
waiting on id lab13...
Tue 24 Apr
Lecture 24
waiting on id lecture24...
Thu 26 Apr
Lecture 25
waiting on id lecture25...
Fri 27 Apr
Recitation 12
waiting on id recitation12...
Mon 30 Apr
Lab 14
waiting on id lab14...
Tue 1 May
Lecture 26
waiting on id lecture26...
Thu 3 May
Lecture 27
waiting on id lecture27...
Fri 4 May
Recitation 13
waiting on id recitation13...
Thu 10 May
(5:30-8:30)
[null]
final
waiting on id FINAL...

2018 Iliano Cervesato iliano@cmu.edu