False Proof – The Reals are Countable

It seems that false proofs are quickly becoming some of the most popular posts on Math ∩ Programming. I have been preparing exciting posts on applications of graph coloring, deck stacking, and serial killers. Unfortunately, each requires resources which exist solely on my home desktop, which is currently dismantled in California while I am on vacation in Costa Rica. Until I return from the tropics, I will continue with more of the ever -popular false proofs.

Problem: Show the real numbers \mathbb{R} are countable. [For a primer on set theory and countability, see our post on the subject].

“Solution”: Recall from our post on well-orderings that every set has a well-order (assuming the axiom of choice). So \mathbb{R} has a well-ordering. Call it <. Recall additionally that by the definition of a well-order, every nonempty subset of \mathbb{R} has a least element with respect to <. We construct a sequence x_i \in \mathbb{R} that uses this well-order in the following way:

Let x_1 be the smallest element of \mathbb{R} with respect to <. Let x_2 be the smallest element of \mathbb{R} - \left \{ x_1 \right \}. Let x_i be the smallest element of \mathbb{R} - \left \{ x_1, x_2, \dots, x_{i-1} \right \}.

Define f: \mathbb{N} \to \mathbb{R} by f(i)=x_i. Clearly this map is injective, and we argue that it is surjective because any element y \in \mathbb{R} is the smallest element of the subset of \mathbb{R} that contains strictly greater elements with respect to <. So this map is a bijection, and the reals are countable.

Explanation: The reader should be extremely wary of this argument, because it does not only claim that the reals are countable. If the argument above held, then we could apply it to any set (since every set has a well-order!). So something is definitely fishy here.

The problem with analyzing this argument is that the well-order of the reals is not known explicitly. However, our noses point us to recognize that the only place a flaw could exist is in the claim of surjectivity.  We prove the falsity of the above argument now, by proving that the same argument fails to show the integers \mathbb{Z} are countable with respect to a well-order of our choosing.

Define a well-order <_{\xi} on \mathbb{Z} by letting non-positive numbers be larger than all positive numbers, and ordering negative numbers by increasing absolute value. In other words, we order the integers like 1,2, \dots, n, \dots, 0, -1, -2, \dots, -n, \dots. We leave it as an exercise to the reader to prove <_{\xi} is a well-order.

Notice that with the argument above, we never choose -1, or any negative number, as an x_i. Specifically, the sequence of x_i is precisely the natural numbers. Hence, we cannot enumerate all of \mathbb{Z} in this fashion. Clearly the argument fails for \mathbb{R} as well.

This false proof is significant in that we get some intuition about well-orders. In particular, a function from the natural numbers to a set S is also a way to “order” S. However, a well-order is more general and more expressive than a bijection from \mathbb{N} \to S, in that we can construct such an order on any set. As we have seen, with a well-order we don’t have to worry about a contiguous sequence of elements from smallest to greatest. We are allowed the power to say “all of these guys are bigger than all of these guys,” regardless of how many are in each group.

And, as always, with more power comes the responsibility to manage counter-intuitive results, either by accepting them through rigorous proof or rejecting false constructions like the one above.

Set Theory – A Primer

It’s often that a student’s first exposure to rigorous mathematics is through set theory, as originally studied by Georg Cantor. This means we will not treat set theory axiomatically (as in ZF set theory), but rather we will take the definition of a set for granted, and allow any operation to be performed on a set. This will be clear when we present examples, and it will be clear why this is a bad idea when we present paradoxes.

The Basics of Sets

Definition: A set S is a collection of distinct objects, each of which is called an element of S. For a potential element x, we denote its membership in S and lack thereof by the infix symbols \in, \notin, respectively. The proposition x \in S is true if and only if x is an element of S.

Definition: The cardinality of S, denoted |S|, is the number of elements in S.

The elements of a set can in principal be anything: numbers, equations, cats, morals, and even (especially) other sets. There is no restriction on their size, and the order in which we list the objects is irrelevant. For now, we will stick to sets of numbers and later we will move on to sets of other sets.

There are many ways to construct sets, and for finite sets it suffices to list the objects:

S = \left \{ 0, 2,4,6,8,10 \right \}

Clearly this set has cardinality six. The left and right curly braces are standard notation for the stuff inside a set. Another shorter construction is to simply state the contents of a set (let S be the set of even numbers between 0 and 10, inclusive). Sometimes, it will be very important that we construct the sets in a more detailed way than this, because, as we will see, sets can become rather convoluted.

We may construct sets using an implied pattern, i.e. for the positive evens:

E = \left \{ 2, 4, 6, 8, \dots \right \}

For now, we simply allow that this set has infinite cardinality, though we will revisit this notion in more detail later. In this way we define two basic sets of numbers:

\mathbb{N} = \left \{ 1, 2, 3, \dots \right \} \\ \mathbb{Z} = \left \{ 0, -1, 1, -2, 2, \dots \right \}

We name \mathbb{N} the natural numbers, and \mathbb{Z} the integers. Yet another construction allows us to populate a set with all values that satisfy a particular equation or proposition. We denote this \left \{ \textup{variable} | \textup{condition} \right \}. For example, we may define \mathbb{Q}, the rational numbers (fractional numbers) as follows:

\displaystyle \mathbb{Q} = \left \{ \frac{p}{q} | p \in \mathbb{Z} \textup{ and } q \in \mathbb{Z} q \neq 0 \right \}

This is not quite a complete description of how rational numbers work: some fractions are “equivalent” to other fractions, like 2/4 and 1/2. There is an additional piece of data called a “relation” that’s imposed on this set, and any two things which are related are considered equivalent. We’re not going to go into the details of this, but the interested reader should look up an equivalence relation.

Next, we want to describe certain pieces of sets:

Definition: A set R is a subset of a set S, denoted R \subset S, if all elements of R are in S. i.e., for all x, x \in R implies x \in S.

And we recognize that under our standard equality of numbers (i.e. \frac{4}{1} = 4), we have \mathbb{N} \subset \mathbb{Z} \subset \mathbb{Q}.

We may now define equality on sets, extending the natural idea that two sets are equal if they contain precisely the same elements:

Definition: Two sets R,S are equal if R \subset S and S \subset R.

A natural set to construct now is the power set of a given set:

Definition: the power set of S, denoted P(S), is the set of all subsets of S. i.e. P(S) = \left \{ R | R \subset S \right \}.

Elements of this set are sets themselves, and there are two trivial, yet important sets to recognize are in P(S), namely S \subset S itself and \left \{ \right \} \subset S, the empty set which contains no elements, is vacuously a subset of every set.

For a finite set S, power sets are strictly larger in size, since there exists a singleton set \left \{ x \right \} \in P(S) for each x \in S. As an exercise for the reader, determine the size of P(S) for any finite set S, expressed in terms of |S|. For infinite sets, we simply admit that their power sets are also infinite, since we don’t yet have a way to describe “larger” infinities.

Building Sets From Other Sets

We have a couple of nice operations we may define on sets, which are rather trivial to define.

Definition: The union of two sets R,S, denoted R \cup S, is \left \{ x | x \in S \textup{ or } x \in R \right \}.

Definition: The intersection of two sets R,S, denoted R \cap S, is \left \{ x | x \in S \textup{ and } x \in R \right \}.

As an exercise, try to prove that |S \cup R| = |S| + |R| - |S \cap R|.

The next definition requires one to remember what an ordered tuple (a_1,a_2, \dots , a_n) is. Specifically, an ordered pair is just like a set which allows for repetition and respects the presented order of elements. So (a,b) \neq (b,a) \neq (a,a,b).

Definition: The direct product (or simply product) of two sets R,S, denoted R \times S, is \left \{ (x,y) | x \in R \textup{ and } y \in S \right \}.

This is just like in defining the Cartesian Plane \mathbb{R}^2 = \mathbb{R} \times \mathbb{R} as ordered pairs of real numbers. We can extend this even further by defining \mathbb{S}^n to be the set of all n-tuples of elements in S.

Functions, and Their -Jections

Now that we have sets and ways to build interesting sets, we may define mathematical objects which do stuff with them.

Definition: A relation \sim on R and S, is a subset of R \times S. Denotationally, we write a \sim b as shorthand for (a,b) \in \sim .

Relations are natural generalizations of = on numbers. In general relations need no additional properties, but they are not very interesting unless they do. For more discussion on relations, we point the reader to the Wikipedia page on equivalence relations. As an exercise to the reader, prove that set equality (defined above) is an equivalence relation, as expected.

Now, we get to the meat of our discussion on sets: functions.

Definition: A function f : S \to R is a relation on S and R, a subset of S \times R, with the additional properties that for each x \in S, there is exactly one element of the form (x, \cdot ) \in f.

Colloquially, functions ‘accept’ a value from S and output something in R. This is why we may only have one ordered pair for each x \in S, because functions are deterministic. Furthermore, we must be able to put every value from S into our function, so no values x \in S may be without a corresponding element of f.

We have special notation for functions, which was established long before set theory was invented. If x \in S, we write f(x) to denote the corresponding element y in (x,y) \in f. In addition, we say that a value x maps to y to mean f(x)=y. If the function is implicitly understood, we sometimes just write x \mapsto y. Then we have the following definitions:

Definition: The domain of a function f: S \to R, sometimes denoted \textup{dom}(f), is the set of input values S. The codomain, denoted \textup{codom}(f), is the set R. Since not every value in R must be in a pair in f, we call the subset of values of R which are produced by some input in S the range of f. Rigorously, the range of f is \left \{ f(x) | x \in S \right \}.

Now we may speak of some “interesting” functions:

Definition: A function f : S \to R is a surjection if its range is equal to its codomain. In other words, for every y \in R, there is some x \in S with f(x) = y, or, equivalently, f(S) = R.

Note that f being a surjection on finite sets implies that the domain is at least as big as the codomain. Though it seems trivial, we can use functions in this way to reason about the cardinalities of their domains and codomains.

Definition: A function f : S \to R is an injection if no two different values x \in S map to the same y \in R. In other words, if f(a) = f(b), then a=b.

Similarly, for finite domains/codomains an injection forces the codomain to be at least as big as the domain in cardinality.

Now, we may combine the two properties to get a very special kind of function.

Definition: A function f: S \to R is a bijection if it is both a surjection and an injection.

A bijection specifically represents a “relabeling” of a given set, in that each element in the domain has exactly one corresponding element in the codomain, and each element in the codomain has exactly one corresponding element in the domain. Thus, the bijection represents changing the label x into the label f(x).

Note that for finite sets, since a bijection is both a surjection and an injection, the domain and codomain of a bijection must have the same cardinality! What’s better, is we can extend this to infinite sets.

To Infinity, and Beyond! (Literally)

Definition: Two infinite sets have equal cardinality if there exists a bijection between them.

Now we will prove that two different infinite sets, the natural numbers and the integers, have equal cardinality. This is surprising, because despite the fact that the sets are not equal, one is a subset of the other and they have equal cardinality! So here we go.

Define f : \mathbb{N} \to \mathbb{Z} as follows. Let f(1) = 0, f(2) = 1, f(3) = -1 \dots. Continuing in this way, we see that f(2k+1) = -k, and f(2k) = k for all k \in \mathbb{N}. This is clearly a bijection, and hence |\mathbb{N}| = |\mathbb{Z}|.

We can extend this to any bijection between an infinite set S of positive numbers and the set \left \{ \pm x | x \in S \right \}.

Let’s try to push bijections a bit further. Let’s see if we can construct a bijection between the natural numbers and the positive rationals (and hence the set of all rationals). If integers seemed bigger than the naturals, then the rational numbers must be truly huge. As it turns out, the rationals also have cardinality equal to the natural numbers!

It suffices to show that the natural numbers are equal in cardinality to the nonnegative rationals. Here is a picture describing the bijection:

We arrange the rationals into a grid, such that each blue dot above corresponds to some \frac{p}{q}, where p is the x-coordinate of the grid, and q is the y-coordinate. Then, we assign to each blue dot a nonnegative integer in the diagonal fashion described by the sequence of arrows. Note these fractions are not necessarily in lowest terms, so some rational numbers correspond to more than one blue dot. To fix this, we simply eliminate the points (p,q) for which their greatest common divisor is not 1. Then, in assigning the blue dots numbers, we just do so in the same fashion, skipping the places where we deleted bad points.

This bijection establishes that the natural numbers and rationals have identical cardinality. Despite how big the rationals seem, they are just a relabeling of the natural numbers! Astounding.

With this result it seems like every infinite set has cardinality equal to the natural numbers. It should be totally easy to find a bijection between the naturals and the real numbers \mathbb{R}.

Unfortunately, try as we might, no such bijection exists. This was a huge result proven by Georg Cantor in his study of infinite sets, and its proof has become a staple of every mathematics education, called Cantor’s Diagonalization Proof.

First, we recognize that every real number has a representation in base 2 as an infinite sequence of 0’s and 1’s. Thus, if there were such a bijection between the natural numbers and reals, we could list the reals in order of their corresponding naturals, as:

1 \mapsto d_{1,1}d_{1,2}d_{1,3} \dots \\ 2 \mapsto d_{2,1}d_{2,2}d_{2,3} \dots \\ 3 \mapsto d_{3,1}d_{3,2}d_{3,3} \dots
\vdots

Here each d_{i,j} \in \left \{ 0,1 \right \} corresponds to the jth digit of the ith number in the list. Now we may build a real number r = d_{1,1}d_{2,2}d_{3,3} \dots, the diagonal elements of this matrix. If we take r and flip each digit from a 0 to a 1 and vice versa, we get the complement of r, call it r'. Notice that r' differs from every real number at some digit, because the ith real number shares digit i with r, and hence differs from r' at the same place. But r' is a real number, so it must occur somewhere in this list! Call that place k \mapsto r'. Then, r' differs from r' at digit k, a contradiction.

Hence, no such bijection can exist. This is amazing! We have just found two infinite sets which differ in size! Even though the natural numbers are infinitely large, there are so many mind-bogglingly more real numbers that it is a larger infinity! We need new definitions to make sense of this:

Definition: An infinite set S is countably infinite if there exists a bijection \mathbb{N} \to S. If no such bijection exists, we call it uncountably infinite, or just uncountable.

Georg Cantor went on to prove this in general, that there cannot exist a bijection from a set onto its power set (we may realize the reals as the power set of the naturals by a similar decimal expansion). He did so simply by extending the diagonalization argument. But since we are dealing with infinite sets, we need even more definitions of “how hugely infinite” these infinite sets can be. These new measures of set cardinality were also invented by Cantor, and they are called transfinite numbers. Their investigation is beyond the scope of this post, but we encourage the reader to follow up on this fascinating subject.

We have still only scratched the surface of set theory, and we have even left out a lot of basic material to expedite our discussion of uncountability. There is a huge amount of debate that resulted from Cantor’s work, and it inspired many to further pick apart the foundations of mathematics, leading to more rigorous formulations of set theory, and extensions or generalizations such as category theory.

Sets of Sets of Sets, and so on Ad Insanitum

We wrap up this post with a famous paradox, which makes one question whether all of the operations performed in set theory are justified. It is called Russell’s Paradox, after Bertrand Russell.

Suppose we define a set S, which contains itself as an element. This does not break any rules of Cantor’s set theory, because we said a set could be any collection of objects. We may even speak of the set of all sets which contain themselves as elements.

Now let us define the set of all sets which do not contain themselves. Call this set X. It must be true that either X \in X or X \notin X. If X \in X, then X is contained in itself, obviously, but then by the definition of X, it does not contain itself as an element. This is a contradiction, so X \notin X. However, if X \notin X, then X satisfies the definition of a set which does not contain itself, so X \in X. Again, a contradiction.

This problem has no resolution within Cantor’s world of sets. For this reason (and other paradoxes based on wild constructions of sets), many have come to believe that Cantor’s set theory is not well-founded. That is not to say his arguments and famous results are wrong, but rather that they need to be reproved within a more constrained version of set theory, in which such paradoxes cannot happen.

Such a set theory was eventually found that bypassed Russell’s paradox, and it is called Zermelo-Fraenkel set theory. But even that was not enough! Additional statements, like the Axiom of Choice (which nevertheless does lead to some counter-intuitive theorems), were found which cannot be proved or disproved by the other axioms of ZF set theory.

Rather than give up all that work on axiomatizing set theory, most mathematicians today accept the Axiom of Choice and work around any oddities that arise, resulting in ZFC (ZF +  Choice), doing their mathematical work there.

So along with paradoxical curiosities, we have laid all the foundation necessary for reasoning about countability and uncountability, which has already shown up numerous times on this blog.

Until next time!

Well Orderings and Search

Binary Search

Binary search is perhaps the first and most basic nontrivial algorithm a student learns. For the mathematicians out there, binary search is a fast procedure to determine whether a sorted list contains a particular element. Here is a pseudocode implementation:

# Binary Search:
# Given a list L, sorted via the total order <, and a sought
# element x, return true iff L contains x.

function binarySearch(L, x, <):
   # base case
   if(length(L) == 1):
      return L[0] == x
   middleIndex = floor(length(L) / 2)
   if (L[middleIndex] == x):
      return true

   # inductive step, with ellipsis notation meaning slices of L
   # from the beginning and to the end, respectively
   if (x < L[middleIndex]):
      return binarySort(L[...middleIndex-1], x, <)
   else:
      return binarySort(L[middleIndex+1...], x, <)

Colloquially, this is the optimal strategy in a game of “guess the number,” where the guesser is told if her guess is correct, too high, or too low. Try the middle number in the range of possible numbers. If the guess is too high, try the number which is 1/4th in the ordering, otherwise try 3/4ths, continuing this process until the number is guessed. This algorithm is obviously made for recursion (and for those advanced programmers, we resign to hope our working language supports tail-call optimization).

Binary search’s runtime is rather easy to analyze. At each step of the algorithm, we either finish, or cut the problem size in half. By no stretch of the imagination, binary search runs in O(\log n) where n is the length of the input list. In other words, in at worst \log_2 n steps, we will reduce the input list to have size 1, and can definitively say whether the list as a whole contains the sought element.

Notice that the success of the algorithm depends on the fact that the list is sorted, but the specific total order < is irrelevant. We will investigate this idea further, but first we need some deeper maths.

Well and Total Orders

For those of us who aren’t set theorists, it isn’t fair to talk about total orders and well orders without defining them. So here comes a definition:

Definition: A strict total order < is a relation on a set S with the following statements holding for all a, b, c \in S:

  • It is never the case that a < a. (anti-reflexivity)
  • Exactly one of the following are true: a < b, b < a, or a = b. (trichotomy)
  • If a < b and b < c, then a < c. (transitivity)

These conditions create an environment in which sorting is possible: we need to be able to compare any two elements (trichotomy), we need to be able to inspect two elements at a time and know that our analysis generalizes to the whole list (transitivity), and we can’t break the world of strictness (anti-reflexivity).

Aside: The use of a strict total order as opposed to a non-strict total order is irrelevant, because each strict total order corresponds bijectively to a non-strict total order. Hence, there are two equivalent formulations of sorting with respect to strict and non-strict total orders, and we may choose one arbitrarily.

Now, we may elevate a total order < to a well order if every non-empty subset R \subset S has a least element with respect to <. We computer scientists only sort finite lists, so every total order is automatically a well order. However, the reader may be interested in the mathematical need for such a distinction, so here it is:

Consider the integers \mathbb{Z} with the standard ordering <. While \mathbb{Z} itself has no smallest element, neither does any subset which has infinitely many negative numbers, such as the evens or odds. More generally, any open interval in the real numbers \mathbb{R} obviously doesn’t have a least element with respect to the natural order. In contrast, we rely on the crucial fact that the set of natural numbers \mathbb{N} is well-ordered to apply mathematical induction.

Interestingly enough, a theorem due to Ernst Zermelo states that every set can be well ordered, and it is equivalent to the Axiom of Choice. While many people have a hard time visualizing a well-ordering of the real numbers \mathbb{R}, we simply resign (as mystical as it is) to admit there is one out there somewhere, though we may never know what it is.

As another aside, it turns out that we only need one of the inequalities in (<, \leq, >, \geq) and the standard logical operations and (infix &&), or (infix ||), and not (prefix !) in order to define the other three (and indeed, to define =, \neq as well). This is a computer science trick that comes in handy, as we’ll see later, so here is the described equivalence. Given <, we define the remaining operations as follows:

  • x > y is equivalent to y < x
  • x \geq y is equivalent to !(x < y)
  • x \leq y is equivalent to !(y < x)
  • x = y is equivalent to !(x < y) \textup{ and } !(y < x)
  • x \neq y is equivalent to x < y \textup{ or } y < x

So if we are interested in sorting a set via some procedure, all we need from the user is the < operation, and then we may compare any way our heart desires.

Defining a New Well Order

Consider a deck of cards which is initially sorted (with some arbitrary ordering on the suits), and then is “cut” at some arbitrary point and the bottom part is placed on top of the deck. We may simplify this “cut” operation to a list of numbers, say ten, and provide the following example of a cut:

(5,6,9,10,11,13,1,3,3,4)

To pick a standard working language, we say the “cut point” of this list is 5, not 4.

We have a few (naive) options to search through cut data: we may sort it with respect to the natural total order on \mathbb{N}, and then search through it; we may stick the elements of the list into a hash set (a constant-time lookup table), and then query existence that way; or we may traverse the list element-by element looking for a particular value.

The problem with the first two methods, though they determine existence, is that they don’t allow us to know where the value is in the list. If this it not important, and we are searching many times (compared to the size of the list) on many different values, then a has table would be the correct choice. However, if we are searching only a few times, and need to know where the value is hidden, all three of the above approaches are slow and inelegant.

Enter well orders. You may have noticed that a cut list of numbers has a very simple well ordering in terms of the natural order on \mathbb{N}. Verbally, if the two numbers are separated across the cut point, then the larger number is in fact the smaller number. Otherwise, we may appeal to the regular ordering. Here it is in pseudocode:

function lessThan(x, y):
   if (y < cutPoint <= x):
      return true
   else if (x < cutPoint <= y):
      return false
   else:
      return x < y

And we may compress these if statements into a single condition:

function lessThan(cutPoint, x, y): 
   y < cutPoint <= x || (!(x < cutPoint <= y) && x < y)

function makeCutOrdering(cutPoint): 
   lambda x,y: lessThan(cutPoint, x, y)

So we have found that a cut list of numbers is in fact well ordered with respect to this new relation. Forget about preprocessing the data, we can just do a binary search using this new ordering! Here’s a Mathematica script that does just that. Here we assume constant-time list length calculations, and fast slicing (which Mathematica has). Note that list indexing and slicing has double square bracket [ [ ] ] syntax, while function application is single square brackets [ ]. It should look very similar to the earlier pseudocode for binary search.

eq[x_, y_, lt_] := !lt[x,y] && !lt[y,x];

(* base case *)
BinarySearch[{oneElt_}, sought_, lt_] := eq[oneElt, sought, lt];

(* inductive step *)
BinarySearch[lst_List, sought_, lt_] :=
   Module[{size = Length[lst], midVal, midNdx},
      midNdx = Floor[size/2];
      midVal = lst[[midNdx]];
      If[eq[midVal, sought, lt],
         True,
         If[lt[sought, midVal],
            BinarySearch[lst[[ ;; midNdx - 1]], sought, lt]
            BinarySearch[lst[[midNdx + 1 ;; ]], sought, lt]
         ]
      ]
   ];

Notice that if we use the standard < (in Mathematica, the function Less), then the BinarySearch function reverts to a standard binary search. Marvelous! Now we have a reusable piece of code that searches through any well-ordered set, provided we provide the correct well order.

The lesson to take from this is know your data! If your input list is not sorted, but still structured in some way, then there’s a good chance it is sorted with respect to a non-natural total order. For example, many operating systems order filenames which end in numbers oddly (e.g. “file1”, “file11”, “file12”, “file2”), and in certain places in the world, financial calendars are ordered differently (In Australia, the fiscal year starts in July). So take advantage of that, and you’ll never need to write binary search again.