Wednesday, Nov 14, 2018
Summary
Once you develop procedures, it becomes useful to have some sense as to how efficient the procedure is. For example, when working with a list of values, some procedures take a constant number of steps (e.g., car), some take a number of steps proportional to the length of the list (e.g., finding the last element in the list), some take a number of steps proportional to the square of the length of the list (e.g., finding the closest pair of colors in a list). In this reading, we consider ways in which you might analyze how slow or fast a procedure is.

## Introduction

At this point in your career, you know the basic tools to build algorithms, including conditionals, recursion, variables, and subroutines (procedures). You’ve also found that you can often write several different procedures that all solve the same problem and even produce the same results. How do you then decide which one to use? There are many criteria we use. One important one is readability - can we easily understand the way the algorithm works? A more readable algorithm is also easier to correct if we ever notice an error or to modify if we want to expand its capabilities.

However, most programmers care as much about efficiency - how many computing resources does the algorithm use? (Pointy-haired bosses care even more about such things.) Resources include memory and processing time. Most analyses of efficiency focus on running time, the amount of time the program takes to run. Running time, in turn depends on both how many steps the procedure executes and how long each step takes. Since almost every step in Scheme involves a procedure call, to get a sense of the approximate running time of Scheme algorithms, we can usually count procedure calls.

In this reading and the corresponding lab, we will consider some techniques for figuring out how many procedure calls are done.

## Some examples to explore

As we explore analysis, we’ll start with a few basic examples. First, consider two versions of the alphabetically-first procedure, which finds the alphabetically first string in a list.

;;; Procedure:
;;;   alphabetically-first
;;; Parameters:
;;;   strings, a list of strings
;;; Purpose:
;;;   Finds the alphabetically first string in the list.
;;; Produces:
;;;   first, a string
;;; Preconditions:
;;;   strings contains at least one element
;;; Postconditions:
;;;   * first is an element of strings
;;;   * For any string, str, in strings
(define alphabetically-first-1
(lambda (strings)
(cond
[(null? (cdr strings))
(car strings)]
[(string-ci<=? (car strings) (alphabetically-first-1 (cdr strings)))
(car strings)]
[else
(alphabetically-first-1 (cdr strings))])))

(define alphabetically-first-2
(lambda (strings)
(if (null? (cdr strings))
(car strings)
(first-of-two (car strings)
(alphabetically-first-2 (cdr strings))))))


You can find the helpers for this procedure in an appendix.

The two versions are fairly similar. Does it really matter which we use? We’ll see in a bit.

As a second example, consider how we might write the famous reverse procedure ourselves, rather than using the built-in version. In this example, we’ll use two very different versions.

;;; Procedure:
;;;   list-reverse
;;; Parameters:
;;;   lst, a list of length n
;;; Purpose:
;;;   Reverse lst.
;;; Produces:
;;;   reversed, a list
;;; Preconditions:
;;;   lst is a list. [Unverified]
;;; Postconditions:
;;;   * (length reversed) = (length lst)
;;;   * For all valid indices i,
;;;     (list-ref lst i) equals (list-ref reversed (- n i 1))
(define list-reverse-1
(lambda  (lst)
(if (null? lst)
null
(list-append (list-reverse-1 (cdr lst)) (list (car lst))))))

(define list-reverse-2
(lambda (lst)
(let kernel ([reversed null]
[remaining lst])
(if (null? remaining)
reversed
(kernel (cons (car remaining) reversed)
(cdr remaining))))))


You’ll note that we’ve used list-append rather than append. Why? Because we know that the append procedure is recursive, so we want to make sure that we can count the calls that happen there, too. We’ve used the standard implementation strategy for append in defining list-append.

;;; Procedure:
;;;   list-append
;;; Parameters:
;;;   front, a list of size n
;;;   back, a list of size m
;;; Purpose:
;;;   Put front and back together into a single list.
;;; Produces:
;;;   appended, a list of size n+m.
;;; Preconditions:
;;;   front is a list [Unverified]
;;;   back is a list [Unverified]
;;; Postconditions:
;;;   For all i, 0 <= i < n,
;;;    (list-ref appended i) is (list-ref front i)
;;;   For all i, <= i < n+m
;;;;   (list-ref appended i) is (list-ref back (- i n))
(define list-append
(lambda (front back)
(if (null? front)
back
(cons (car front) (list-append (cdr front) back)))))


## Strategy one: Counting steps through output

How do we figure out how many steps these procedures take? One obvious way is to add a bit of code to output something for each procedure call. While you may not know the details of output yet, all you need to know right now is that write prints its parameter, and newline prints a carriage return. To annotate the procedures for output, we simply add appropriate calls. For example, here are the first few lines of the annotated versions of alphabetically-first-1 and alphabetically-first-2.

(define alphabetically-first-1
(lambda (strings)
(write (list 'alphabetically-first-1 strings)) (newline)
(cond
[(null? (cdr strings))
(car strings)]
[(string-ci<=? (car strings) (alphabetically-first-1 (cdr strings)))
(car strings)]
[else
(alphabetically-first-1 (cdr strings))])))

(define alphabetically-first-2
(lambda (strings)
(write (list 'alphabetically-first-2 strings)) (newline)
(if (null? (cdr strings))
(car strings)
(first-of-two (car strings)
(alphabetically-first-2 (cdr strings))))))


What happens when we call the two procedures on a simple list of strings? Let’s see.

> (define animals (list "armadillo" "badger" "capybara" "donkey" "emu"))
> (alphabetically-first-1 animals)
(alphabetically-first-1 ("capybara" "donkey" "emu"))
(alphabetically-first-1 ("donkey" "emu"))
(alphabetically-first-1 ("emu"))
> (alphabetically-first-2 animals)
(alphabetically-first-2 ("capybara" "donkey" "emu"))
(alphabetically-first-2 ("donkey" "emu"))
(alphabetically-first-2 ("emu"))


So far, so good. Each has about five calls for a list of length five. Let’s try a slightly different list.

> (alphabetically-first-1 (reverse animals))


Wow! That’s a lot of lines to count, 31 if we’ve counted correctly. That seems like a few more than one might expect. That may be a problem. So, how many does the other version take?

> (alphabetically-first-2 (reverse animals))


Still five calls for a list of length five. Clearly, the second one is better on this input. Why and how much? That’s an issue for a bit later.

In case you haven’t noticed, we’ve now tried two special cases, one in which the list is organized first to last and one in which the list is organized last to first. In the first case, the two versions are equivalent in terms of the number of recursive calls. In the second case, the second implementation is significantly faster. Clearly, the cases you test for efficiency matter a lot. We should certainly try some others.

Let’s try the other example of exploring costs, reversing lists. We’ll start by reversing a list of length five.

> (reverse-1 (list 1 2 3 4 5))
(reverse-1 (1 2 3 4 5))
(reverse-1 (2 3 4 5))
(reverse-1 (3 4 5))
(reverse-1 (4 5))
(reverse-1 (5))
(reverse-1 ())
(5 4 3 2 1)
> (reverse-2 (list 1 2 3 4 5))
(reverse-2 (1 2 3 4 5))
(reverse-2-kernel (1 2 3 4 5) ())
(reverse-2-kernel (2 3 4 5) (1))
(reverse-2-kernel (3 4 5) (2 1))
(reverse-2-kernel (4 5) (3 2 1))
(reverse-2-kernel (5) (4 3 2 1))
(reverse-2-kernel () (5 4 3 2 1))
(5 4 3 2 1)


So far, so good. The two seem about equivalent. But what about the other procedures that each calls? The kernel of reverse-2 calls cdr, cons, and car once for each recursive call. Hence, there are probably five times as many procedure calls as we just counted. On the other hand, reverse-1 calls list-append and list. The list procedure is not recursive, so we don’t need to worry about it. But what about list-append? It is recursive, so let’s add an output annotation to that procedure, too. Now, what happens?

> (reverse-1 (list 1 2 3 4 5))
(reverse-1 (1 2 3 4 5))
(reverse-1 (2 3 4 5))
(reverse-1 (3 4 5))
(reverse-1 (4 5))
(reverse-1 (5))
(reverse-1 ())
(list-append () (5))
(list-append (5) (4))
(list-append () (4))
(list-append (5 4) (3))
(list-append (4) (3))
(list-append () (3))
(list-append (5 4 3) (2))
(list-append (4 3) (2))
(list-append (3) (2))
(list-append () (2))
(list-append (5 4 3 2) (1))
(list-append (4 3 2) (1))
(list-append (3 2) (1))
(list-append (2) (1))
(list-append () (1))
(5 4 3 2 1)


Hmmm, that’s a few calls to list-append. Not the 31 we saw for the brightest element in a list, but still a lot. Let’s see … fifteen, if we count correctly. Now, let’s see what happens when we make the list one element longer.

> (reverse-1 (list 1 2 3 4 5 6))
(reverse-1 (1 2 3 4 5 6))
(reverse-1 (2 3 4 5 6))
(reverse-1 (3 4 5 6))
(reverse-1 (4 5 6))
(reverse-1 (5 6))
(reverse-1 (6))
(reverse-1 ())
(list-append () (6))
(list-append (6) (5))
(list-append () (5))
(list-append (6 5) (4))
(list-append (5) (4))
(list-append () (4))
(list-append (6 5 4) (3))
(list-append (5 4) (3))
(list-append (4) (3))
(list-append () (3))
(list-append (6 5 4 3) (2))
(list-append (5 4 3) (2))
(list-append (4 3) (2))
(list-append (3) (2))
(list-append () (2))
(list-append (6 5 4 3 2) (1))
(list-append (5 4 3 2) (1))
(list-append (4 3 2) (1))
(list-append (3 2) (1))
(list-append (2) (1))
(list-append () (1))
(6 5 4 3 2 1)
> (reverse-2 (list 1 2 3 4 5 6))
(reverse-2 (1 2 3 4 5 6))(kernel (1 2 3 4 5 6) ())
(kernel (2 3 4 5 6) (1))
(kernel (3 4 5 6) (2 1))
(kernel (4 5 6) (3 2 1))
(kernel (5 6) (4 3 2 1))
(kernel (6) (5 4 3 2 1))
(kernel () (6 5 4 3 2 1))
(6 5 4 3 2 1)


Hmmm … we added one element to the list, and we added six calls to list-append (we’re now up to 21). In the case of reverse-2, we seem to have added only one call.

What if there are ten elements? You probably don’t want to count that high. However, we’re pretty sure the we’ll end up with 55 total calls to list-append, and only ten recursive calls to the kernel.

## Strategy two: Automating the counting of steps

We’ve seen a few problems in the previous strategy for analyzing the running time of procedures. First, it’s a bit of a pain to add the output annotations to our code. Second, it’s even more of a pain to count the number of lines those annotations produce. Finally, there are some procedure calls that we didn’t count. So, what should we do? We want to transition from manual to automatic counting.

Just as we updated the code to print lines, we will update our code just a bit to do the counting. At least the counting will be automatic.

What do we do? For each procedure we want to count, we create a vector. Why a vector? Because we know how to change the elements in vectors, and we are going to want to have a count that we can change. For convenience, we will also include the name of the thing we are counting.

;;; Procedure:
;;;   counter-new
;;; Parameters:
;;;   name, a string
;;; Purpose:
;;;   Create a counter associated with the given name.
;;; Produces:
;;;   counter, a counter
;;; Preconditions:
;;; Postconditions:
;;;   counter can be used as a parameter to the various counter
;;;   procedures.
;;; Process:
;;;   Counters are two element vectors.  Element 0 is the name, and
;;;   should not change.  Element 1 is the count, and should change.
(define counter-new
(lambda (name)
(vector name 0)))


Now, let’s create some procedures that we can use with that counter. First, we want to be able to increment the counter (that is, to count one more procedure call).

;;; Procedure:
;;;   counter-increment!
;;; Parameters:
;;;   counter, a counter
;;; Purpose:
;;;   count the counter
;;; Produces:
;;;   counter, the same counter, now mutated
;;; Preconditions:
;;;   counter was created by counter-new (or something similar) and
;;;   has only been modified by the counter procedures.
;;; Postconditions:
;;;   (counter-get counter) gives a number one higher than it
;;;   did before.
(define counter-increment!
(lambda (counter)
(vector-set! counter 1 (+ 1 (vector-ref counter 1)))
counter))


And, of course, we want to be able to get the value of the counter.

;;; Procedure:
;;;   counter-get
;;; Parameters:
;;;   counter, a counter
;;; Purpose:
;;;   Get the number of times that counter-increment! has been called
;;;   on this counter.
;;; Produces:
;;;   count, a non-negative integer
;;; Preconditions:
;;;   counter was created by counter-new and has only been modified
;;;   by the counter procedures.
;;; Postconditions:
;;;   count is the number of calls to counter-increment on this counter
;;;   since the last call to counter-reset! on this counter, or since the
;;;   counter was created, if there have been no calls to counter-reset!
(define counter-get
(lambda (counter)
(vector-ref counter 1)))


As that documentation suggests, we may, on occasion, want to reset the counter.

;;; Procedure:
;;;   counter-reset!
;;; Parameters:
;;;   counter, a counter
;;; Purpose:
;;;   reset the counter
;;; Produces:
;;;   counter, the same counter, now set to 0
;;; Preconditions:
;;;   counter was created by counter-new (or something similar) and
;;;   has only been modified by the other counter procedures.
;;; Postconditions:
;;;   (counter-get counter) gives 0.
(define counter-reset!
(lambda (counter)
(vector-set! counter 1 0)
counter))


Finally, it may be useful to print out the value of the counter.

;;; Procedure:
;;;   counter-print
;;; Parameters:
;;;   counter, a counter
;;; Purpose:
;;;   Print out the information associated with the counter.
;;; Produces:
;;;   counter, the same counter
;;; Preconditions:
;;;   counter was created by counter-new and has only been modified
;;;   by the various counter procedures.
;;; Postconditions:
;;;   counter is unchanged.
;;;   The output port now contains information on counter.
(define counter-print
(lambda (counter)
(display (vector-ref counter 0))
(display ": ")
(display (vector-ref counter 1))
(newline)))


Now, we can try another analysis of the alphabetically-first procedures and even do a bit less manual counting.

First, we create counters. We’ve chosen to make these match the names of the underlying procedures, but they don’t have to.

(define alphabetically-first-1-counter (counter-new "alphabetically-first-1"))
(define alphabetically-first-2-counter (counter-new "alphabetically-first-2"))
(define counters (list alphabetically-first-1-counter alphabetically-first-2-counter))


Next, we update the code to alphabetically-first-1 and alphabetically-first-2.

(define alphabetically-first-1
(lambda (strings)
(counter-increment! alphabetically-first-1-counter)
(cond
[(null? (cdr strings))
(car strings)]
[(string-ci<=? (car strings) (alphabetically-first-1 (cdr strings)))
(car strings)]
[else
(alphabetically-first-1 (cdr strings))])))

(define alphabetically-first-2
(lambda (strings)
(counter-increment! alphabetically-first-2-counter)
(if (null? (cdr strings))
(car strings)
(first-of-two (car strings)
(alphabetically-first-2 (cdr strings))))))


One detour: You’ll note that we have a list of counters. We often create lists so that we can do something to each element. For example, we might want to reset each counter with

> (map counter-reset! counters)
'(#("alphabetically-first-1" 0) #("alphabetically-first-2" 0))


However, as you see, that generates a list that we don’t really care about. For side-effecting procedures, like counter-reset!, Scheme provides an alternative to map, called for-each, that applies a procedure to each value in a list, but returns nothing.

> (for-each counter-reset! counters)


You’ll see us use for-each a lot. You can find a definition at the end of this reading.

We are now ready to do some analysis.

> (for-each counter-reset! counters)
> (alphabetically-first-1 animals)
> (for-each counter-print counters)
alphabetically-first-1: 5
alphabetically-first-2: 0
> (alphabetically-first-2 animals)
> (for-each counter-print counters)
alphabetically-first-1: 5
alphabetically-first-2: 5
> (for-each counter-reset! counters)
> (for-each counter-print counters)
alphabetically-first-1: 0
alphabetically-first-2: 0
> (alphabetically-first-1 (reverse animals))
> (alphabetically-first-2 (reverse animals))
> (for-each counter-print counters)
alphabetically-first-1: 31
alphabetically-first-2: 5


What if we try a slightly bigger list? We didn’t really want to do so when we were counting by hand, but now the computer can count for us.

> (define letters (map (o (l-s make-string 2)
integer->char
(l-s + (char->integer #\a)))
(iota 16)))
> letters
'("aa" "bb" "cc" "dd" "ee" "ff" "gg" "hh" "ii" "jj" "kk" "ll" "mm" "nn" "oo" "pp")
> (for-each counter-reset! counters)
> (alphabetically-first-1 letters)
"aa"
> (alphabetically-first-2 letters)
"aa"
> (for-each counter-print counters)
alphabetically-first-1: 16
alphabetically-first-2: 16
> (for-each counter-reset! counters)
> (alphabetically-first-1 (reverse letters))
"aa"
> (alphabetically-first-2 (reverse letters))
"aa"
> (for-each counter-print counters)
alphabetically-first-1: 65535
alphabetically-first-2: 16


We really didn’t want to count more than 64,000 calls by hand.

Now that we’ve done some preliminary exploration of these procedures, which one would you prefer to use?

You may be waiting for us to analyze the two forms of reverse, but we’ll leave that as a task for the lab.

## Interpreting Results

You now have a variety of ways to count steps. But what should you do with those results in comparing algorithms? The strategy most computer scientists use is to look for patterns that describe the relationship between the size of the input and the number of procedure calls. We find it useful to look at what happens when you add one to the input size (e.g., go from a list of length 4 to a list of length 5) or when you double the input size (e.g., go from a list of length 4 to a list of length 8, or go from the number 8 to the number 16).

The most efficient algorithms generally use a number of procedure calls that does not depend on the size of the input. For example, car always takes one step, whether the list of length 1, 2, 4, or even 1024. Computer scientists refer to those as constant time algorithms.

At times, we’ll find algorithms that add a constant number of steps when you double the input size (we haven’t seen any of those so far). Those are also very good. (Computer scientists and mathematicians say that these algorithms are logarithmic in the size of the input.)

However, for most problems, the best we’ll be able to do is write algorithms that take twice as many steps when we double the size of the input. For example, in processing a list of length n, we probably have to visit every element of the list. In fact, we’ll probably do a few steps for each element. If we double the length of the list, we double the number of steps. Most of the list problems you face in this class should have solutions that have this form. For these algorithms, we say that the running time is linear in the size of the input.

However, there are times that the running time grows much more quickly. For example, you may notice that when you double the input, the number of steps seems to go up by about a factor of four. Such algorithms are sometimes necessary, but get very slow as input size increases. When the running time grows by the square of the growth in input size, we say that algorithms are quadratic.

But there are even worse cases. At times, you’ll find that when you double the input size, the number of steps goes up much more than a factor of four. (For example, that happens in the first version of alphabetically-first-1.) If you find your code exhibiting that behavior, it’s probably time to write a new algorithm.

Note: While we generally don’t like to use algorithms that exhibit worse behavior than quadrupling steps for doubled input, there are cases in which these slow algorithms are the best that any computer scientist has developed. There are even some problems for which we have slow algorithms but theorize that there cannot be a fast algorithm. To make life even more confusing, there are some problems for which it has been proven that no algorithm can exist. If you continue your study of computer science, you will have the opportunity to explore these puzzling but powerful ideas.

## What went wrong?

We started the reading by considering pairs of algorithms for the same problem, and found that one of each pair was much slower than the other. Why are the slower versions of the two algorithms above so slow? In the case of alphabetically-first-1, there is a case in which one call spans two identical recursive calls. That doubling gets painful fairly quickly. From 1 to 3 to 7 to 15 to 31 to 63 to ….

If you notice that you are doing multiple identical recursive calls, see if you can apply a helper to the recursive call, as we did in alphabetically-first-2. Rewriting your procedure using the primary tail recursion strategy (that is, accumulating a result as you go) may also help.

In the case of reverse-1, the difficulty is only obvious when we include list-append. And what’s the problem? The problem is that list-append is not a constant-time algorithm, but one that needs to visit every element in the first list. Since reverse keeps calling list-append with larger and larger lists, we spend a lot of time appending.

## Self checks

### Check 1: Categorizing algorithms

For each function, explain whether it is a constant time or linear time algorithm.

• cdr
• cddr
• list-ref
• vector-ref
• map
• iota

### Check 2: Exploring the tools

Explain briefly what each of the following commands from the reading does, and how it works.

(define alphabetically-first-1-counter (counter-new "alphabetically-first-1"))
(define alphabetically-first-2-counter (counter-new "alphabetically-first-2"))
(define counters (list alphabetically-first-1-counter alphabetically-first-2-counter))

> (for-each counter-reset! counters)
> (alphabetically-first-1 animals)
> (alphabetically-first-2 animals)
> (for-each counter-print counters)


### Check 3: Manual analysis

a. Make a copy of analysis-lab.rkt, which contains code from the reading.

b. Add the following line to the beginning of list-append (immediately after the line containing the lambda).

(write (list 'list-append front back)) (newline)


c. Determine how many times list-append is called when reversing a list of length seven using list-reverse-1.

d. Add the following line to the kernel of list-reverse-2 (immediately after the line containing the lambda for the kernel).

(write (list 'kernel remaining reversed)) (newline)


e. Determine how many times the kernel is called when reversing a list of length seven using list-reverse-2.

## Appendix: Utilities for working with strings

At the beginning of this reading, we considered two techniques for computing the alphabetically first element of a list. In those procedures, we relied on some additional procedures, such as first-of-two. Those procedures may be found here.

;;; Procedure:
;;;   first-of-two
;;; Parameters:
;;;   str1, a string
;;;   str2, a string
;;; Purpose:
;;;   Find the alphabetically first of two strings.
;;; Produces:
;;;   first, a string
;;; Preconditions:
;;; Postconditions:
;;;   * first is either str1 or str2
;;;   * (string-ci<= first str1)
;;;   * (string-ci<= first str2)
(define first-of-two
(lambda (str1 str2)
(if (string-ci<=? str1 str2)
str1
str2)))


## Appendix: Defining for-each

Although for-each comes with DrRacket, we could define it ourselves.

(define my-for-each
(lambda (proc! lst)
(when (not (null? lst))
(proc! (car lst))
(my-for-each proc! (cdr lst)))))