## Session 5

### Let's Play a Game!

Today we play Chutes and Ladders, another simple two-player game played with positive numbers. We'll limit ourselves to numbers in the range [2..100].

This game is different from the ones we have played before. It is collaborative, that is, the two players cooperate to achieve a common goal.

The game starts with a single number. The players take turns adding a number to the board. The only requirements are that:

• the next number must be either a factor or a multiple of the previous number, and

• no number can be repeated.

Continue like this, building a chain of numbers in the range [2..100] in which each number is either a factor or a multiple of its predecessor in the chain. The players' common goal is to create the longest non-repeating chain of numbers possible.

For example, consider the starting position is 47. Player 1 must play 94, which is (47x2). Player 2 must play 2, which is (94/47). Player 1 now has plenty of legal moves to choose from.

Your opening exercise: Play this game twice with a classmate, with each person going first once. As you play, try to give yourself and your partner a way to keep growing the chain.

The winners of our competition will be the pair who create the longest chain!

After each game, identify any patterns you notice in how the game proceeds, maybe even a strategy for creating the longest chain possible.

Initial positions:

• 64
• 10

... much fun ensues ...

• Which numbers don't you want to see as the game's starting position?
• Other than those, does it matter which number starts the game?

... more fun ensues ...

Now, tell us what you learned.

### Exploring the Game

... seeking a different kind of algorithm: collaborative versus adversarial. The latter has been dominant over most of the history of algorithms and AI, with the former becoming more prominent with the rise of networks and a new class of problems we are interested in solving.

Like the Difference Game last time, this game depends on the factors of numbers. What role to prime numbers play in this game?

We are looking for the longest chain possible. What is the theoretical upper bound on our answer?

Prime numbers larger than fifty are dead ends. They have no factors or multiples in the set of legal moves. So they can participate only in chains of length 1. These are 53, 59, 61, 67, 71, 73, 79, 83, 89, 97.

The longest possible chain in theory is contains the remaining 99 - 10 = 89 numbers.

This uncovers a few more questions:

• Are all numbers reachable from a single starting position? If not, then there are multiple, disconnected chains of length two or more.

• If yes, then are all numbers reachable from any starting position? If not, then the chain is linear, with now way to "circle back".

### ... Representation

... one of CS: many ways to represent data. Each way makes asking some questions easier than others. (Two meanings here.)

... numbers chained together. Values and links. A common representation: the graph. Thinking in terms of a graph makes some questions easier to ask than others.

What kinds of questions are there? What kinds of representations are there? The answers to these questions are intertwined...

### Common Types of Problem

Quick Exercise. Write down as many different kinds of problem. That is, given a set of data, what questions might we want to ask of it?

... Here is a quick starting point, based on what you find in many algorithms textbooks:

• search: find an item that meets a criterion
• sort: order a set of items according to a criterion
• filter: find the subset of items that meet a criterion
• matching: find a subsequence in a longer sequence, including inexact matches
• path finding: find a way to get from one item to another, given limitations in the moves available
• constraint satisfaction: solve an equation or set of equations; evaluate or approximate the value of a function at a given value
• abstraction: find a function that fits a set of data points with desired precision

... generally about collections of items. There are similarities across the list. What is fundamental?

... Reminded of an old wisdom that there are only six kinds of algorithm: search, sort, map, filter, count, and combine.

Each problem type presupposes some things. For example, to search for an item presupposes a collection of items, each of which can be evaluated with respect to the criterion. Either we need to know the set in advance, or we need to be able to generate its items as we need them (systematically).

What do the other problem types presuppose? Is there a common set of presuppositions, or a theme among them?

Notice that "search" is a problem type. "Linear search" is a technique -- actually, a family of techniques -- for solving search problems. This is an example of the basic distinction between what? and how?.

### Common Types of Representations

... The most common data structures used when framing algorithmic problems are ones you see, or might see, in a data structures course:

• sequence: array, string, linked list, stack, queue
• graph: a set of items and a set of pairwise connections between items
• tree: a connected, acyclic graph
• set: an unordered collection of distinct items; generalized to a bag or multiset if items are not distinct
• dictionary: a collection of key/value pairs
• hash table: a collection of key/value pairs

... This list is at multiple levels of abstraction. Some mostly abstract. Some mostly implementation.

... we'll add some new data structures to this list throughout the semester, based on more recent work.

Review your data structures text to remind yourself about the concepts and implementation details of the data structures you have learned so far. Be prepared to learn the others as we go along (with my help, of course!)

... relationships between problems and data structures. Problems take different form, with different challenges, when applied to different data structures. We will reconsider the relationships among problems, algorithms, and data structures throughout the course.

### Graphs

As we saw earlier, our thinking about Chutes and Ladders lends itself naturally to representation in a graph. Let's use the game as a way to (re)learn the vocabulary of graphs. At the same time, the vocabulary of graphs may help us find a solution to the game.

... vocabulary:

• a generalization of tree
• G = (V, E)
• complete, dense, sparse
• directed and undirected
• weighted and unweighted
• connected and disconnected
• labeled and unlabeled
• paths and cycles

... versatile! Many applications: computer networks, concepts (in AI). Almost no limits.

Representing graphs:

• dictionary?!

### References

The idea for our opening exercise comes from this Maths Challenge by Kevin Rutherford, British software developer. Rutherford is well known for his work in the Ruby and agile software development communities. He is also the creator of Reek, one of my favorite tools for programming in Ruby. It is a "code smell detector", similar to lint in the C world.

One source of the six kinds of algorithm is Peter Norvig's Tutorial on Good Lisp Programming Style. (That is a postscript file. I have a PDF version for your convenience.) Most of you don't know Lisp. All of you can learn a lot about programming and algorithms from reading this tutorial!

A similar though very different idea comes from Smalltalk, which implements six iterators that cover the vast majority of loops we write over sequences and collections: map (called 'do'), select, detect, reject, filter, and sum (called 'inject').