Wednesday, October 29, 2014

Week 7 - Polya’s Approach to Penny Piles:


Understanding the Problem:
*Note: The original problem given on the worksheet in class has been generalized here for some initial condition (n, m), where n represents the number of pennies in the left drawer and m represents the number of pennies in the right drawer.

Here is some notation that will be used in our solution below:
(x, y) = a configuration with x number of pennies in the left drawer and y number of pennies in the right drawer
(n, m) = the initial condition
“0” = operation L
“1” = operation R
“0101…” = some sequence of operations L and R

Given some initial condition (n, m), for the number of pennies in the left and right drawers respectively, the problem is to determine whether a certain new configuration (x, y) is achievable given only two possible operations:
·      L: transfer half of the pennies in the left drawer to the right drawer only if the left has an even number of pennies
·      R: transfer half of the pennies in the right drawer to the left drawer only if the right has an even number of pennies.
Moreover, we would also like to determine all possible configurations for the number of pennies in each drawer given some initial condition (n, m) as well as the sequence of steps required to achieve each new configuration.

Of course, the set of new configurations depends on the initial condition.  For example, if n and m were two odd numbers then there would be no other possible configurations.

To break this down, our input would be some ordered pair (n, m) and our output would be all possible new ordered pairs (x, y) that can be achieved by operations L and R above.  The sequence of operations needed to achieve each new configuration should also be included in our output.

Devising a Plan:
This problem falls under the category of a binary tree problem similar to what is currently being taught in another programming course.  Since there are only two possible operations, then each node (representing a specific configuration) of the tree has at most two subtrees.  The leaves of the tree represent the configurations where no further operations can be performed.  To simplify the solution further, we can say that the leaves also encompass the configurations that have been previously identified from exploring some other part of the tree. These two types of configurations provide our stopping conditions or “base cases” for a possible recursive solution.

Here is a small diagram showing that the tree will look like.
 

Our plan here is to develop a recursive python function that computes and prints all possible configurations given some initial condition input.

Each configuration and its sequence of operations will be stored in a python dictionary, which is passed into a recursive function along with the current configuration and current sequence of steps used to arrive at the current state.  During each iteration, the program checks if any further operation can be performed or whether the current configuration has been encountered before.  If so, then it stops exploring this branch and passes the information back up the line.  If a new operation can be performed, then the new configuration and new sequence is stored in the dictionary and we move down the branch.

Eventually, all possible branches (sequence of operations) will be explored thus giving us all possible configurations for the number of pennies in each pile.

Carrying Out the Plan:
To carry out this plan I have implemented the python function penny_piles( ) shown below.  The output of the function can be verified by performing the sequence of operations on the initial condition.  This check can also be implemented in a separate python function to verify that each output is correct.

def penny_piles(start, steps = "", poss_piles = None):
    """ (tuple of (int, int), str, dict(tuple of (int, int), str) -> None

    Prints all possible configurations for two piles of pennies given in
    start followed by a binary string showing the sequence of operations
    performed on the start condition to obtain the new configuration.

    A "0" represents a left operation and a "1" represents a right operation.

    left operation = shift half of the pennies in the left to the right if
    the left side is even

    right operation = shift half of the pennies on the right to the left if
    right side is even

    >>> penny_piles((0,4))
    (0, 4) -
    (1, 3) 10
    (2, 2) 1
    (3, 1) 11
    """

    if poss_piles == None:
        poss_piles = {}

    left = start[0]
    right = start[1]

    if steps == "" and left % 2 != 0 and right % 2 != 0:
        print(start)
        return None

    if left % 2 != 0 and right % 2 != 0:
        return {}
    else:
        if left % 2 == 0 and left != 0:
            new_start = (int(left / 2), int(right + left / 2))
            if new_start not in poss_piles:
                new_steps = steps + "0"
                poss_piles[new_start] = new_steps
                poss_piles.update(penny_piles(new_start, new_steps, poss_piles))
        if right % 2 == 0 and right != 0:
            new_start = (int(left + right / 2), int(right / 2))
            if new_start not in poss_piles:
                new_steps = steps + "1"
                poss_piles[new_start] = new_steps
                poss_piles.update(penny_piles(new_start, new_steps, poss_piles))
        if steps == "":
            poss_piles[start] = "-"
            lst_of_keys = list(poss_piles)
            lst_of_keys.sort()
            for key in lst_of_keys:
                print(key, poss_piles[key])
            return None
        return poss_piles

if __name__ == "__main__":
    pass


Looking Back:
This function has been tested and verified for various inputs including corner cases such as (0, 0) and odd number only values.  The results are easily checked by hand or using another Python function.

This solution can be re-implemented in many other ways including ones that do not use recursion explicitly, although it does help to think recursively for this problem. I can say, however, that this function is efficient in that it does not explore duplicate branches.

Recursion can be used a wide variety of problems where the problem can be broken down into smaller problems of the same type (e.g. a linked-list or a family tree).  In this case we had an initial condition and all the sub-configurations of this initial condition are made up of all the other sub-configurations of each new-configuration.



Monday, October 20, 2014

Week 6 – Methods for Proofs


I gather there are about 6-7 different methods for proofs that we’ve learned so far.  Here is a list of what I’ve got going:

1)    Direct proofs w/ transitivity – This appears to be the most common where one conclusion leads to another until you eventually arrive at the statement you were trying to prove.
2)    Equivalent proof w/ contra-positive – Initially, I felt this method overcomplicated the proof but I am starting to understand where it becomes useful. My only concern is spending the time to work out the contra-positive on an exam only to find that the original statement was just as easy to prove. I guess we need to practice and develop some intuition when it comes to using this method.
3)    Proof of Existence – Most of the time I see this used with sequences or some other set of concrete numbers.
4)    Proof By Contradiction – This method is the most interesting so far. The approach is to prove a statement by disproving its negation, i.e. we must find some elementary result that is falsified when the statement is negated. What's interesting is that we're able to prove one giant universally quantified statement by finding just one miniscule falsity in its negation. That to me is very powerful. The power behind proof by contradiction is perfectly demonstrated in Euclid’s proof of infinite prime numbers which we touched on in class (more discussion below).
5)    Non-Boolean Proofs – Further study is required here. I’m not sure where the “non-boolean” part comes into play. From what I can see the statements still evaluate to True or False.
6)    Proving the Falsity of Statements – Seems just like method 1 (direct proof with transitivity) except we are instead proving the negation. I guess the trick is figuring out whether to prove the negation or show a contradiction in the original statement. More practice and intuition.
7)    Proof by Cases – Walk down all paths and show the statement is true in each.  Sounds exhausting, I mean exhaustive. So far only used on even or odd cases so, not too bad.

I also wanted to talk a little about Euclid’s proof of infinite primes.  We only got halfway through this in class and, like many other students; I left questioning whether the proof made sense.

It kind of went like this:
·      We used proof by contradiction by assuming there are a finite number of primes all contained in some set P.
·      We then showed that the product of all elements of P plus 1 is either a prime (a contradiction to the set P) or it is not a prime and is thus divisible by some element of P (another contradiction, since no element of P can divide the new number without a remainder of 1).
So that is all well and good.  But then the professor says, “By the way, not all sequential products of primes plus one will lead to another prime. And that’s the end of class. Have a great day.” (To his credit, he did, later on, promise us a full proof of infinite primes).

What?!  That makes no sense!?

It turns out this is a common misconception with this proof. We aren’t saying that some product of n primes plus one is also a prime. What we are saying is if the new number is not a prime then there must be some larger prime outside of the n primes that can divide it. Otherwise, that new number is a prime.  In either case, (yup proof by cases and by contradiction) there is always another prime number outside of the group of n primes.  Thus infinite number of primes. Now if only they could put this on an assignment….

Saturday, October 11, 2014

Week 5 - Midterm Test and Proofs


First lets talk midterm test. Overall, I felt pretty good coming out of it.  I was wrestling with the last question (#3) for longer than I should have been. I noticed that one of them was an “exclusive-or” statement while the second one was a “not the intersection” statement and at first glance they seemed almost identical (negating one would also mean negating the other).  So I put away my truth tables and dusted off my Venn diagrams. Luckily, I noticed the rest of the universe “not (P or Q)” would also satisfy “not (P and Q)” but would negate the exclusive or “P and not Q or Q and not P”.  So I think I have this one pinned down. Venn diagrams: 1 v.s. Truth Tables: 0.

Prof. Heap posted solutions almost immediately and we even took it up the next class, which was great! I will note that for the first two questions I did not provide specific examples that listed elements like he did. He also mentioned that some students simply translated the statements into English. I hope I was not one of them but I don’t remember. Question 2 is where I’m uncertain because the proofs for the two statements about natural numbers are so basic that you could do a proof “by inspection” like I’ve seen in a lot of textbooks. So it is quite possible that my explanations for this question inadvertently became direct translations of the statements into English.  Lets hope not, but I guess what's done is done.

On the subject of proofs, moreover, simple proofs (like the one above), I find it tedious to have to prove things that are “by definition” true, or “by inspection” true. Prof. Heap had warned us that we were going to struggle with how far our proofs should go. But a perfect example would be in class where we'd get to a “simple proof” stage and I would be struggling to find a proof without saying this is “by definition” true, or “by inspection” true and then all of a sudden he would turn around and tell us, “Oh, you don’t have to prove that.”  Hopefully, our homework problems will be at least one step above a basic definition so we aren't running around in circles saying, “it’s true because I said it’s true!”

Saturday, October 4, 2014

Week 4 - Tutorial 3 Recap and For All, One…For One, All


So what happened to the truth tables?  We got introduced to them and then for the tutorial I find out they are just as inefficient as Venn diagrams.  With 4 or more predicates it seems like just as much work. Take for example: P => (Q => ( R=> S)), 4 predicates = 24 = 16 rows x (3 implications + 4 predicates) columns = 112 T/F entries.  I guess these are meant more for computer logic proofs rather than “people” logic proofs.

By the way, that example was from Tutorial 3.  Here are some more oddities I ran into from Tutorial 3.

1)    Every dog has its day, or perhaps its cat.
If I were speaking to an ordinary person I would have assumed that either the dog or the cat was the subject because that is what makes sense in English. What I still struggle with here is the separation between the syntax (logical meaning) and the semantics (English meaning).  Logic-wise I should have recognized that both instances of “its” refers to the dog and not the cat. I’ve said this before; this is an English course in mathematics. When will I learn.

2)    A and B are both guarantees that C are true.
The easy conversion to a symbolic statement was (A => C) ^ (B => C).  But then someone mentioned (A v B) => C and I, without hesitation, disagreed.  The “or” in the symbolic statement just didn't conform to the “and” in the sentence.  This time the English syntax has led me astray.  It turns out they ARE both equal.  So to my TA, if you are reading, I believe you now and here’s the proof (to the question that is, or I guess both work).

            (A => C) ^ (B => C)
            (not A v C) ^ (not B v C)  # implication equality
            C v (not A ^ not B) # distributive law
            not(A v B) v C # commutative law and DeMorgan’s law
            (A v B) => C # implication equality

The last thing I’d like to talk about are the examples from the Week 4 Lecture where we used symbolic notation to represent limits.  Prof. Heap mentioned that he decided to use “e” and “d” to represent the epsilons and deltas. I personally would have caught on a bit faster if we actually did use ε and Δ or δ.  The delta symbols occur naturally with x in calculus whereas I keep mixing up the “e” with the base of the natural log.  The “e” and “d” felt sort of arbitrary and I had to flip back and forth between pages to understand the intervals drawn on the graph.  But that's no matter; just a change in notation.