Comp 271 Week 4

Matrix

Why does this work?
    public void addColumn() {
        for (int i = 0; i<rowcount;i++) {
            rows.get(i).add(null);
        }
        colcount++;
    }

Recursion

starts page 94
We saw a recursive version of the gcd(m,n) function earlier. How about the following recursive version of factorial(n)?
    int factorial(int n) {
       if (n==0) return 1;
       else return n*factorial(n-1);
    }
(Bailey uses a recursive function sum3, p 95, to add the numbers 1..n instead of multiply them)

Note this mirrors the following definition of n!:
    0! = 1
    n! = n*(n-1)! for n>0

Stack-trace demo from System.out.println(factorial(4));
Base case

Empty base case
    example: printing a vector, p 97.


Fibonacci problem
    recursive version
    problem with n~45 (11 sec)
    overflow at n=? (sort of fixed by moving to long from int)

Thursday

sorting time

Note that the first two tries take longer.
However, they do so only if we have recompiled, or reset the machine: more tries on the same object, or creating a new object and doing the subsequent tries on that, are the same smaller time (approx).

I still have no idea why insertion sort is running so much faster than selection sort. It shouldn't.

factorial tail-recursion

    in the recursive case, it is the recursive value that is returned directly, with no further work done to it.

mathematical induction

Suppose we want to prove 1+2+...+n = n*(n+1)/2 = P(n).
Or 12 + 22 + 32 + ... + n2 = n*(n+1)*(2n+1)/6 = Q(n) = n3/3 + n2/2 + n/6.
(P(n) and Q(n) are defined to be shorthands for the righthand sides here)

Step 1: verify for n=0, or n=1.
Step 2: assuming the claim for n, prove it for n+1. That is, assuming

    12 + 22 + 32 + ... + n2 = Q(n),       CLAIM(n)
prove
    12 + 22 + 32 + ... + n2 + (n+1)2 = Q(n+1).       CLAIM(n+1)

If we start from our CLAIM(n) and add (n+1)2 to both sides, we get

    12 + 22 + 32 + ... + n2 + (n+1)2 = Q(n)+n2+2n+1.

Similarly, Q(n+1) = (n+1)3/3 + (n+1)2/2 + n+1 = (n3/3 + n2 + n + 1/3) + (n2/2 + n + 1/2) + n/6 + 1/6
          = n3/3 + n2/2 + n/6 + n2 + 2n + 1 = Q(n)+n2+2n+1.

Question: where did this formula come from? The 1+2+3+...+n formula at least has a natural derivation: add it to itself in reverse order, and you have n columns where each column adds up to n+1. The doubled total is thus n*(n+1), and the original sum is thus P(n) above.

Induction is tricky: note exactly where we assume the n-stage hypothesis at the start.

Postage-stamp problem, p 98

Suppose you want change in stamps, either 44 cents or 28 cents or 1 cent. For a given amount, what is the fewest number of stamps you can receive as change?

Note that the obvious approach of getting as many 44's as possible, then as many 28's as possible, etc, is not optimal in all (or even very many) cases. For example, if you need 60cents back, that is either 1 44-cent stamp and 16 1-cent stamps (total of 17) or two 28's and four 1's (total of 6).

Strategy: if we have N cents to make change for, then the optimum is one of the following:
So we calculate stampCount(N-44), stampCount(N-28), and stampCount(N-1), take the minimum, and add one more stamp (which stamp depends on which subproblem was the minimum).

simple recursive version: each call generates three subcalls
efficient version: creating an array of all values so far; p 99

Some sample results from first, inefficient, version:
stampCount(200) = 6:     404061476 calls, 2900 ms
stampCount(201) = 7:     443663991 calls, 3061 ms
stampCount(202) = 8:     487148470 calls, 3462 ms
stampCount(204) = 5:     587322736 calls, 4010 ms
stampCount(206) = 7:     708097261 calls, 4982 ms
stampCount(208) = 9:     853706337 calls, 6003 ms
stampCount(210) = 11: 1029255633 calls, 7222 ms

Note the time/space tradeoff, rather decidedly in favor of less time.


Chapter 6: Sorting

Page 119
Bubble sort: p 120
Each time we pass through, we swap adjacent elements if they are out of order. Note that the same value may be carried forward in several swaps.

Selection sort, p 122
    Find the biggest, move it to the Nth position (data[N-1])
    Find the second biggest (that is, the biggest of positions 0 up to N-1) and move it to the N-1th position.
    ...

Insertion sort, p 125
 for (i=0, i<N, i++)
    insert data[i] into the sorted portion data[0]..data[i-1].

Thursday

Merge sort

Basic idea: split, and merge.

One strategy: back-and-forth.

Book's strategy: a little different

Count comparisons: log(n) steps, and all the merges at each step take O(n) together. Total: O(n log n)