This is a must-read for all the programmers. Codes are in C or C++ but the idea is the fruit of this book.

Some summary as below:

Among all data structures taught in classes, we never talk about bit vectors but in fact, it is the one most compact and solves a number of problems. For example, to check if some integer already in a set.

Always there is a trade off between space and time. But there is also cases to trade between code size and time. Some code optimization technique includes: Loop unrolling, use of sentinels to reduce number of check for loop-stop conditions, replacement of one function by another for speed reduction (e.g. division is time consuming compared to multiplications), and so on. Sometimes, trade off between space and time may not work as it should be. An example from the book is, comparing a linked list to an array for insertion of an item into a sorted item, although linked list needs only one change, but pointer operation is so expensive that makes array more favorable.

Some nice (eureka!) algorithms are proposed:

1) The Aho-Hopcroft-Ullman lazy initialization: Given an array of large size, we don’t want to initialize all items to zero at the beginning but they need to be initialized right before it is updated. How can we tell if an item in an array is initialized or not?

Assume the array of data is

`D`

, we create two supplementary arrays of integers`X`

and`Y`

, and a pointer (integer index)`L`

. Initially`L`

is zero. To check if`D[k]`

is initialized, we check`Y[X[k]]=k`

and`X[k] < L`

. Before using`D[k]`

, we first set`Y[L]=k`

,`X[k]=L`

, then increment`L`

. In this case, we trade the space for the time of initialization. And no initialization done in any arrays, ever.

2) How to store a sparse matrix efficiently?

An element of position in the matrix is a 3-tuple . If the matrix is large and it is unlikely that a row/column is completely empty, we can store using array and a array, where the matrix is and the total number of non-empty matrix element is . The method is as follows. The array

`Col[]`

corresponds to the columns in matrix,`Col[k]`

denoting the aggregate number of elements in columns before column`k`

inclusive.`Col[0]`

is by definition zero. Then the total number of elements in column`k`

is therefore`Col[k]-Col[k-1]`

. The 2D array`E[][]`

sore s the row number and the element value, e.g.`E[i][0]`

is the row number and`E[i][1]`

is the element value. Then to look for an element in column`k`

row`h`

, we do a for-loop of`h`

from`Col[k-1]`

to`Col[k]`

and check for`E[h][0]==k`

.

3) How to print a random line in a file in one pass, without knowing the total number of lines in the file before hand?

If there are lines in the file, each line is print with probability . Because , we can do in this way: Mark line as the line to print with probability , and print the last marked line when all lines in the file are read. Then line will be print at the end satisfies the above probability .

## Bibliographic data

```
@book{
title = "Programming Pearls",
edition = "2nd",
author = "Jon Louis Bentley",
publish = "Addison-Wesley",
year = "1999",
library = "Dibner",
classification = "QA76.6.B454 2000",
url = "http://www.cs.bell-labs.com/cm/cs/pearls/",
}
```