Math 220: Discrete Mathematics for Computer Science and Engineering Carl Mummert mummertc@marshall.edu Edits by A Mummert, August 2022 January 20, 2020 Contents Introduction 3 A Algorithms and Number Representations 1 Number Representations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1 The Euclidean Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Egyptian Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . Additional problems for Part A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 5 10 10 12 13 B Boolean Logic 3 Boolean Connectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Extended Truth Tables and Logical Equivalence . . . . . . . . . . . . . . . . . . . 5 Rules of Inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Disjunctive and Conjunctive Normal Forms . . . . . . . . . . . . . . . . . . . . . 7 Complete Set of Connectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Additional Problems for Part B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 14 18 21 24 27 27 C Sets and Functions 8 Sets, Subsets and Set Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Relations and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Multisets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Additional Problems for Part C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 29 34 37 38 D Proofs and Counting 11 Mathematical Proofs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 The Multiplication Principle and Words on an Alphabet . . . . . . . . . . . . . . . 13 Combinations and Permutations . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Permutations from Multisets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Probability and Search Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Additional Problems for Part D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 39 42 46 52 55 59 E Proof by Induction 16 Induction for Number Theoretic Identities . . . . . . . . . . . . . . . . . . . . . . 17 Induction for Combinatorial Identities . . . . . . . . . . . . . . . . . . . . . . . . 18 Induction for Algorithm Correctness . . . . . . . . . . . . . . . . . . . . . . . . . Additional Problems for Part E . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 62 65 68 69 1 CONTENTS 2 F Graph Theory 19 Families of Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Eulerian Graphs and Hamiltonian Graphs . . . . . . . . . . . . . . . . . . . . . . 21 Trees and Traversals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Spanning Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Shortest Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Additional Problems for Part F . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 70 74 79 85 89 93 G Recurrence Relations 94 24 Intro to Recurrence Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 25 Second-order Linear Recurrence Relations . . . . . . . . . . . . . . . . . . . . . . 99 Additional Problems for Part G . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Answers to Warm-up and Additional Problems 104 Introduction This is a set of lecture notes for Math 220 at Marshall University. The course includes topics in discrete mathematics, and the primary audience is computer science and engineering majors. I have not found a textbook that includes the precise collection of topics, and many books also have a viewpoint that is not aligned with the audience for this class. So, I have written these notes with several goals: • Present some facts of discrete math useful in computer science, along with the mathematical viewpoint. • Keep the notes self-contained, so it is reasonable to learn everything in the notes during the Math 220 class at Marshall University. This means that many valuable topics are left out. Far more could be presented, but then there would be too much material for a one-semester course. • Give some of the cultural viewpoint on mathematics and computer science, including prominent historical figures as well as notable topics from more advanced classes. Each section is intended to take 75 minutes, including time for students to present the exercises on the board for class discussion. Like most mathematics, the notes will often use letters from the Greek alphabet, which is shown in Table 1. Problems There are three kinds of problems in these notes: • Warm up problems are meant to ensure you have understood the basics of each section. • Exercises are the homework you will submit. These will focus on the material from the section, but they may also bring in additional concepts to complement the section. • Challenges are problems that are more difficult, and which are not part of the regular homework. These problems help you see some of the more advanced topics that relate to the material at hand. I have also included many culture problems. Each of these problems introduces a concept, term, state of mind, or historical figure related to mathematics or computer science. You can greatly increase the amount you take from the class by following up on these problems. 3 CONTENTS A, α B, β Γ, γ ∆, δ E, ε, ε Z, ζ alpha beta gamma delta epsilon zeta 4 H, η Θ, θ , ϑ I, ι K, κ Λ, λ M, µ eta theta iota kappa lambda mu N, ν Ξ, ξ O, o Π, π P, ρ Σ, σ nu xi omicron pi rho sigma Table 1: The Greek alphabet T, τ Y, υ Φ, φ X, χ Ψ, ψ Ω, ω tau upsilon phi chi psi omega Part A Algorithms and Number Representations In the first part of Math 220, we will look at the concept of an algorithm, a set of rules for performing a computation by hand or with a computer. Algorithms don’t run for their own sake: they solve problems of interest. This means that algorithms need to work with numbers and other mathematical objects. A description of an algorithm will need to begin with a description of how these objects will be represented. 1 Number Representations In this section, we will be looking at ways to represent non-negative integers. In particular, we will look at binary, decimal, and hexadecimal representations. To understand this section, first think of the integers and the non-negative integers as theoretical concepts. Each non-negative integer is a count of some quantity, such as the number of words on this page. The goal of a representation is to find a way to communicate that count to another person by expressing (representing) the integer in some concrete form. Decimal representation In decimal representation, also called base 10, a nonnegative integer is represented as a sequence of one or more digits, where each digits is in the set {0, . . . , 9}. For example, each of these represents a nonnegative integer: 534 002 9034 00 1001. Suppose that we have a sequence of one or more digits from the set {0, . . . , 9} and we want to figure out which integer it represents. For convenience, we write the sequence as an an−1 . . . a0 . We use the notation (an an−1 . . . a0 )10 to represent the nonnegative integer that this sequence represents in base 10. For example, in the sequence “534” we have a2 = 5, a1 = 3, and a0 = 4. The integer being represented can be found by adding up multiples of powers of 10: (534)10 = 5 × 102 + 3 × 101 + 4 × 100 and, more generally, n (an an−1 . . . a0 )10 = ∑ ai × 10i . i=0 5 PART A . ALGORITHMS AND NUMBER REPRESENTATIONS 6 Why do we go through all this complication when we already know what “534” represents? One reason is that the same string, “534”, could be intended to be in base 16 (hexadecimal), or base 8 (octal), or some other base. When we write (534)10 we make it clear that we are reading the string in base 10. If we write (534)16 then we are using hexadecimal, and if we write (534)8 we are using octal. In this way, we can keep a separation between the string of digits “534” and the number that it represents. When we don’t use any special notation, we mean base 10, as is usual in mathematics. Binary representation In binary representation, also called base 2, a nonnegative integer is represented as a sequence of one or more digits, where each digit is in the set {0, 1}. For example, each of these sequences is a binary representation of a nonnegative integer: 1101 0010 110101. As with base 10, if we denote the sequence of binary digits by (an an−1 . . . a0 )2 then the nonnegative integer being represented is n (an an−1 . . . a0 )2 = ∑ ai × 2i . i=0 For example, (11010)2 = 1 × 24 + 1 × 23 + 0 × 22 + 1 × 21 + 0 × 20 = 16 + 8 + 0 + 2 = 26. Hexadecimal representation In binary representation, also called base 16, a nonnegative integer is represented as a sequence of one or more digits, where each digit comes from the set {0, 1, . . . , 9, A, B,C, D, E, F}. For this purpose, the letters A through F are considered as single digits, with values 10 through 15 as shown in Figure A .1. For example, each of these sequences is a hexadecimal representation of a number: 1101 A1B CAFE We often write 0x in front of a hexadecimal representation as a clue that is it hexadecimal, instead of using the (· · · )16 notation. We find the value of a hexadecimal number by adding multiples of powers of 16. For example, 0xA1B = (A1B)16 = 10 × 162 + 1 × 16 + 11 × 160 = 2560 + 16 + 11 = 2587. Converting between representations To convert between the binary and hexidecimal representations, use the fact that each set of 4 binary digits represents 1 hexidecimal digit. Example 1. From binary to hexidecimal, split a binary number into groups of 4, adding zeros on the left if necessary. Convert each set of 4 binary digits to the corresponding hexidecimal digit. (1110010110100)2 = (0001 1100 1011 0100)2 = 0x1CB4 PART A . ALGORITHMS AND NUMBER REPRESENTATIONS 7 Decimal 0–9 10 11 12 13 14 15 Hexadecimal Same A B C D E F Figure A .1: Decimal digits 0 through 9 are represented by the same digit in hexadecimal. Decimal numbers 10 through 15 are represented in hexadecimal by the digits A through F. Decimal Binary Hexadecimal Decimal Binary Hexadecimal 0 1 2 3 4 5 6 7 0000 0001 0010 0011 0100 0101 0110 0111 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1000 1001 1010 1011 1100 1101 1110 1111 8 9 A B C D E F Figure A .2: Conversion between binary and hexadecimal can be done quickly by knowing the representations for decimal digits 0–15. Break up binary digits into groups of 4 starting from the right and replace with hexidecimal representation. Example 2. From hexidecimal to binary, replace each hexidecimal digit with its 4 digit binary representation. 0xC1 = (1100 0001)2 To converting from decimal to any base x, divide the number by x, note the quotient q1 and remainder r1 . Divide the quotient q1 by x, note the new quotient q2 and remainder r2 . Repeat until the quotient is 0. The number in the new base x is the remainders r1 , r2 , and so on expressed right to left ...r2 r1 . Example 3. To convert 42,425 to hexidecimal, start by dividing by 16. 42, 425/16 = 2, 651 R 9 2, 651/16 = 165 R 11 165/16 = 10 R 5 10/16 = 0 R 10 The remainders expressed right to left are 10, 5, 11, 9. Using the hexidecimal digit representation for each of these gives 42, 425 = 0xA5B9 Comparison of representations Each of the three representations we have mentioned – decimal, binary, hexadecimal – has its own advantages and disadvantages. PART A . ALGORITHMS AND NUMBER REPRESENTATIONS 8 • Decimal notation is familiar and easy for humans to work with. But it does not match well with the binary logic of computers and electric circuits. • Binary notation directly shows the sequence of bits that is used to represent a number. It is convenient when we want to look at the specific patterns in these bits. But it takes a lot of space to write down even modest size numbers in binary. For example, 65, 536 is 216 , and so it requires 17 binary digits to write: (1 0000 0000 0000 0000)2 = 65, 536. Remember that the rightmost digit is the 20 place, so the 17th digit is the 216 place. • Hexadecimal notation gives a compromise between the shortness of decimal and the bit expressiveness of binary. See Figure A .2. Experienced users can identify the bit pattern of each hexadecimal digit, and a pair of hexadecimal digits gives the bits of one byte of computer memory. However, hexadecimal notation is not convenient for human calculation because few people are well practiced in performing hexadecimal arithmetic by hand. Other representations There are additional ways of representing integers in computers, such as the famous “two’s complement” method. Representations in computers differ from the common mathematical representations in two ways: 1. Typically, a computer can only natively represent finite subset of the integers, based on the number of bits that the processor handles as once. For example, a 64-bit computer is typically unable to directly process integers larger than 264 in hardware. Larger integers have to be handled in software . The mathematical representations, defined without looking at an particular computer, can represent arbitrarily large integers. 2. Computer representations such as two’s complement are optimized for hardware, rather than for human usage. They can be more challenging for hand computations than the representations used in mathematics. PART A . ALGORITHMS AND NUMBER REPRESENTATIONS 9 Exercises for Section 1 Warm up 1.1. Fill in the empty cells of the following table by converting between decimal, binary, and hexadecimal as appropriate. Binary Decimal Hexadecimal (11010110)2 2017 0x3A5 Answer available. Exercise 1.2. Fill in the empty cells of the following table by converting between decimal, binary, and hexadecimal as appropriate. Binary Decimal Hexadecimal (01010011)2 3424 0xDA Exercise 1.3. Write out the addition and multiplication tables for single digit hexadecimal numbers. If you went to an elementary school that taught hexadecimal arithmetic you would have memorized these tables instead of the base 10 tables. Because you already know the table for 1 through 10, you only need to fill in the table for A through F. For example, 0xA multiplied by 0xB is 10 times 11, which is 110, which is 0x6E. A A B C D E F B 0x6E C D E F 0x6E Culture of Computer Science 1.4. Investigate the binary coded decimal representation of nonnegative integers. How does it represent the number 534 as a sequence of bytes? What are the main use cases for binary coded decimal? Culture of Computer Science 1.5. Convert the number 3, 735, 928, 559 to hexadecimal and investigate the history of this particular number in computer science. PART A . ALGORITHMS AND NUMBER REPRESENTATIONS 2 10 Algorithms Mathematics and computer science are full of algorithms. An algorithm is, informally, a precise set of rules for computing something. It is challenging, however, to pin down exactly what we mean by “algorithm”. In The Art of Computer Programming (first edition published in 1968), Donald Knuth gives a 5-part definition of algorithm. This definition doesn’t cover all kinds of algorithms, but it covers a very large collection of algorithms, including all the algorithms we will consider in this course. Knuth’s definition of an algorithm 1. Inputs The algorithm may take inputs, which are values provided before the algorithm begins to execute. If the algorithm takes inputs, the set of possible inputs and the way they are provided to the algorithm must be precisely specified. 2. Outputs The algorithm may produce one or more outputs, which are quantities or values that the algorithm seeks to compute from the inputs. The set of all possible outputs, and their meanings, must be precisely specified. 3. Finiteness An algorithm runs in discrete steps, and if it produces an output it must do so after a finite number of steps. The number of steps may depend on the input, and the algorithm might not return an output for every possible input. 4. Definiteness The steps of an algorithm have to be specified precisely, so that there is no ambiguity or vagueness about the operations that will be performed at each step. 5. Effectiveness The operations that are performed must be simple enough that, in principle, they could be carried out by a person with only pencil and paper, without any ingenuity or cleverness. Each step must take only a finite amount of time to carry out. 2.1 The Euclidean Algorithm The goal of the Euclidean algorithm is to find the greatest common divisor of two positive integers n and m. In this section, we will look at two versions of this algorithm. The first version uses subtraction, while the second version uses division. Definition 1. If n and m are positive integers, their greatest common divisor is the largest positive integer that divides evenly into both n and m. Because 1 divides evenly into every integer, the greatest common divisor of two positive integers n and m is at least 1. In principle, we could try every number from 1 to the smaller of n and m, and check whether each of these is a common divisor of n and m. That gives an algorithm, but it is not efficient. The Euclidean algorithm is much more efficient, particularly the division form. We start with the subtraction form, which is simpler. PART A . ALGORITHMS AND NUMBER REPRESENTATIONS 11 Algorithm 1 (Euclidean algorithm via subtraction). Inputs: two positive integers n and m. Outputs: one positive integer, gcd(m, n). Instructions: 1. If n = m then return their common value. 2. Otherwise, replace the larger of the two numbers with their positive difference and go back to instruction 1. For example, suppose we begin with n = 18 and m = 30. We obtain the following sequence of calculations. Step 1 2 3 4 n 18 18 6 6 m 30 12 12 6 Difference 12 6 6 0 The algorithm then returns 6 as the greatest common divisor. Later in the course, we will explain why this algorithm gives the correct answer. Sometimes, the algorithm seems to run for a long time, when we already know where it will end up. For example, suppose we run it with n = 6 and n = 75. Step 1 2 3 .. . n 6 6 6 .. . m 75 69 63 .. . 12 13 14 6 6 3 9 3 3 Here, steps 2 through 12 seem very repetitive, as we just subtract 6 from m over and over. The 6 is subtracted 12 times, with the final subtraction resulting in 3. We could use division instead of repeated subtraction: 75 = 6 × 12 + 3. The remainder after division is the same as the result of the final subtraction. The following variation of the Euclidean algorithm speeds things up. Algorithm 2 (Euclidean algorithm via division). Inputs: two positive integers n and m. Outputs: one positive integer, gcd(m, n). Instructions: 1. If one of the numbers has become zero, return the other number. 2. Otherwise, replace the larger of the two numbers with the remainder when the larger is divided by the smaller. Then go back to instruction 1. If we run this algorithm with n = 6 and m = 75, we obtain the following table. The algorithm stops when one number becomes zero, and returns the other number. PART A . ALGORITHMS AND NUMBER REPRESENTATIONS Step 1 2 3 n 6 6 0 12 m 75 3 3 As you can see, this saved a large number of steps compared to the subtraction version of the algorithm. But is this version correct? We will prove it is correct later in the course. 2.2 Egyptian Multiplication In this section, we look at another algorithm for multiplication. Most people learn the long multiplication algorithm in school. The downside of long multiplication is that it requires the user to memorize all the basic multiplication tables for single digits. The following algorithm, which has been used in various cultures, only requires the user to be able to multiply and divide by 2, and to perform addition. In particular, it is closely related to a method used by scribes in ancient Egypt, so we will call it “Egyptian multiplication”. Algorithm 3 (Egyptian multiplication). Inputs: Two positive integers n and m. Outputs: One positive integer, m × n. Instructions: 1. First, we make a table one row at a time. Each row has two numbers. (a) The first number in the first row is n, and the second number in the first row is m. (b) To make the next row, multiply the first number in the previous row by 2, and divide the second number in the previous row by 2, rounding down. (c) Continue performing the previous instruction until the second number becomes 1. 2. Go through the table and place a ∗ beside each row in which the second number is odd. 3. Add up the first numbers in the rows that were marked with a ∗. Ignore the umarked rows. 4. Output the sum from the previous step. For example, to compute 15 × 19, we first make the following table. Step n m Mark 1 2 3 4 5 15 30 60 120 240 19 9 4 2 1 ∗ ∗ ∗ We then add the first numbers from the rows marked with ∗: 15 × 19 = 15 + 30 + 240 = 285. We can verify that the product is correct using ordinary “long” multiplication. PART A . ALGORITHMS AND NUMBER REPRESENTATIONS 13 Exercises for Section 2 Warm up 2.1. Use the subtraction form of the Euclidean algorithm to find the gcd of 65 and 156. Answer available. Warm up 2.2. Use the division form of the Euclidean algorithm to find the gcd of 85 and 204. Answer available. Warm up 2.3. Use Egyptian Multiplication to find the product of 156 and 204. Answer available. Warm up 2.4. Write a formal algorithm to convert a decimal number to hexidecimal representation, as described in Section 1. Answer available. Exercise 2.5. Use the subtraction form of the Euclidean algorithm to find the gcd of each of the following pairs: 1. 15 and 50 2. 136 and 52 Show the steps of the algorithm along with the final output. Exercise 2.6. Use the division form of the Euclidean algorithm to find the gcd of each of the following pairs: 1. 690 and 570 2. 144 and 89 Show the steps of the algorithm along with the final output. Exercise 2.7. Use Egyptian Multiplication to find the following products. 1. 101 × 19 2. 548 × 1234 Show the steps of the algorithm along with the final output. Exercise 2.8. Suppose that we will receive a sequence of open and closed parentheses, and we want to determine if the sequence is balanced. This means that each open parenthesis is paired with a closing parenthesis in the usual way, following order of operations from the inside out. For example “(()(()))” and “(()()())()” are balanced, while “)()” and “(()()” are not. Write an algorithm to determine if a sequence of open and closed parentheses is balanced. Make sure that your algorithm spells out all the steps so that a person could follow them without adding any additional information beyond what you have provided. Additional Problems for Part A A.1 Convert the number 2015 to binary and hexadecimal. Answer available. A.2 Convert the hexadecimal number 0x13A to binary and decimal. Answer available. A.3 Use the Euclidean algorithm to find gcd(1978, 6969156). Answer available. A.4 Use Egyptian multiplication to multiply 58 and 62. Answer available. Part B Boolean Logic In this chapter, we will look at Boolean logic. This is the logic of the truth values True and False. This logic is important in formal reasoning, where we use Boolean logic to study theorems. It is also the logic of digital control systems in machinery and electronics. Boolean logic is named after George Boole (1815–1864), who wrote about the subject in a book The Laws of Thought in 1854. A less well known logician of the 19th century, Charles Sanders Peirce, may have been the first to realize that the same logic could be used by electrical devices to communicate information and make decisions. This idea is now a basic fact of computer science, where Boolean logic is ubiquitous – it is used in computer processors, networking protocols, and almost every other setting of computing and electronics. 3 Boolean Connectives Definition 2. A Boolean connective is a function that takes zero or more truth values as inputs, and returns a single truth value as output. The easiest way to describe a Boolean connective is to give a table that shows every possible collection of inputs along with the corresponding output. These truth tables can be used to give complete description of any Boolean connective. For example, there is a particular Boolean connective, known as AND, which takes two inputs x and y, and returns True if and only if both x and y are True – it returns False if either input is False. The AND connective is given by the following truth table. x y AND(x, y) F F F F T F T F F T T T There are four rows because, overall, there are four possible values for the inputs, depending on whether the first and/or second input is True. Example 4. A Boolean connective that takes only one argument is called unary. There are four unary Boolean connectives, whose truth tables are shown below. Each of these tables has only two rows, because these connectives have only one input variable. 14 PART B . BOOLEAN LOGIC Identity: x 15 Negation: x Constant True Constant False x x x x x True x False F F F T F T F F T T T F T T T F Example 5. A connective that takes 2 arguments is called binary. How many of these connectives are there? There are 4 rows in a truth values with two variables. We can fill in each row however we like – True or False– so there are 2 options for each row. Therefore there are 24 = 16 binary connectives. Each of these connectives has a name, as shown in Figure B .1 Example 6. A Boolean connective that takes no arguments is called nullary. These connectives have no input, so they simply return a constant value. There are only two nullary connectives. One always returns True, the other always returns False. Example 7. A connective that takes 3 arguments is called ternary. How many of these connectives are there? There are 8 possibilities for the truth values of three variables, so a truth table for a ternary connective will have 8 rows. We can fill in the output of each row however we like – True or False– so there are two options for the output of each row. Therefore, there are 28 = 256 ternary connectives. Unlike with binary connectives, there are not well known names for all 256 ternary connectives. More complicated Boolean connectives can be built out of simpler connectives by composition. For example, we can make a particular connective F(x, y, z) by defining F(x, y, z) = x ∧ (y ∨ (¬z)). Here ∧ represents AND, ∨ represents OR, and ¬ represents NOT. In this way we can build up very complex connectives that take any number of variables, by combining the simpler binary and unary connectives we are familiar with. Definition 3. An expression that is correctly built up from variables along with unary and binary connectives is called a formula. Order of Operations When we start to build up long formulas, the parentheses can become cumbersome, as in ((x ∧ (y ∧ z)) → (z → (¬w))). There is an order of operations, shown below, which allows us to omit some parentheses. For example, the formula above becomes (x ∧ y ∧ z) → (z → ¬w), which is much easier to read. Order of operations for Boolean connectives • First, take care of any expressions in parentheses, from the inside out. • Next, take care of negations. • Now, perform conjunctions and disjunctions from right to left. • Next, perform implications from right to left . • Finally, perform biconditionals. PART B . BOOLEAN LOGIC 16 Identity: x Negation: x Identity: y Negation: y x y x x y x x y y x y y F F F F F T F F F F F T F T F F T T F T T F T F T F T T F F T F F T F T T T T T T F T T T T T F Conjunction, AND (∧) NAND (↑) Disjunction, OR (∨) NOR (↓) x y x∧y x y x NAND y x y x∨y x y x NOR y F F F F F T F F F F F T F T F F T T F T T F T F T F F T F T T F T T F F T T T T T F T T T T T F Implication (→) Nonimplication (6→) XOR (6↔) Bi-implication (↔) x y x→y x y x 6→ y x y x↔y x y x XOR y F F T F F F F F T F F F F T T F T F F T F F T T T F F T F T T F F T F T T T T T T F T T T T T F Back Implication Back Nonimplication True (>) False (⊥) x y x←y x y x 6← y x y > x y ⊥ F F T F F F F F T F F F F T F F T T F T T F T F T F T T F F T F T T F F T T T T T F T T T T T F Figure B .1: The 16 binary connectives PART B . BOOLEAN LOGIC 17 In our order of operations, when we have a chain of instances of the same operator, we evaluate them from right to left. So x → y → z becomes x → (y → z). This convention is called right associativity in computer science. We could group the operators in the opposite order to have a different order of operations. It is an arbitrary choice, but there are some reasons for using right associativity. Exercises for Section 3 Warm up 3.1. Consider truth tables for one connective. How many rows would a truth table have if the connective is nullary (no arguments)? unary (one argument)? binary (two arguments)? ternary (three arguments)? quaternary (four arguments)? Answer available. Warm up 3.2. Suppose that is a binary connective. We say that is commutative if the formula x y always has the same truth value as the formula y x. Determine whether each of the following connectives is commutative. 1. AND (∧) 2. IF (→) 3. NOR Answer available. Warm up 3.3. Suppose that is a binary connective. We say that is idempotent if the formula x x always has the same truth value as x. Determine whether each of the following connectives is idempotent. 1. OR (∨) 2. IF (→) 3. NAND Answer available. Exercise 3.4. Use our order of operations to fill in any omitted parentheses from the following formulas, so that your final formula shows the precise order that the connectives will be evaluated. 1. x ∧ y ∨ ¬z 2. x ∧ y ∧ z ∨ q 3. x → ¬y → z Exercise 3.5. Suppose that is a binary connective. We say that is commutative if the formula x y always has the same truth value as the formula y x. Determine whether each of the following connectives is commutative. 1. OR (∨) 2. ↔ 3. XOR 4. NAND Exercise 3.6. Suppose that is a binary connective. We say that is idempotent if the formula x x always has the same truth value as x. Determine whether each of the following connectives is idempotent. 1. AND (∧) 2. ↔ 3. XOR 4. NOR Culture of Computer Science 3.7. Investigate reverse Polish notation. What is it and why is it useful? Where did its name come from? PART B . BOOLEAN LOGIC 4 18 Extended Truth Tables and Logical Equivalence We can use extended truth tables to evaluate formulas. As with basic truth tables, there is one row for every possible combination of values of the inputs. Example 8. The following truth table shows the values of x → (y ∨ ¬z). We include a column for y ∨ ¬z to make the table easier to construct. x y z y ∨ ¬z x → (y ∨ ¬z) F F F T T F F T F T F T F T T F T T T T T F F T T T F T F F T T F T T T T T T T The following table shows the truth values of ¬(x ∨ y) ↔ (¬x ∧ ¬y). Again, we use two intermediate columns to make the table easier to construct. We fill in the table one column at a time from left to right. We also label the intermediate columns A and B so we don’t have to rewrite the formulas later in the table. x y z A ¬(x ∨ y) B ¬x ∧ ¬y A↔B F F F T T T F F T T T T F T F F F T F T T F F T T F F F F T T F T F F T T T F F F T T T T F F T Tautologies and Logical Equivalence We are particularly interested in formulas that are always True. We will see that these formulas often correspond to patterns of correct reasoning. Definition 4. A formula that is always True, regardless of the truth values of its inputs, is called logically valid. These formulas are also called tautologies. A formula that is always False, regardless of the truth values of its inputs, is called unsatisfiable. A formula that is True for at least one set of input values is called satisfiable. PART B . BOOLEAN LOGIC 19 Thus every tautology is satisfiable, but not every satisfiable formula is a tautology. Definition 5. Two formulas that always have the same truth values, regardless of the values of their inputs, are called logically equivalent. So two formulas φ and ψ are logically equivalent if and only if the longer formula φ ↔ ψ is logically valid. We saw above that ¬(x ∨ y) and ¬x ∧ ¬y are logically equivalent. Example 9. We can verify that x → y and ¬x ∨ y are logically equivalent by writing a truth table. x y x→y ¬x ∨ y F F T T F T T T T F F F T T T T Rather than writing “logically equivalent” over and over, we can just write “equivalent” if the meaning is clear from context. Exercises for Section 4 Warm up 4.1. Write truth tables for each of the following formulas: 1. (x ∧ y) → x 2. (x XOR y) ∨ (y ∧ z) Answer available. Warm up 4.2. Suppose that is a binary connective. We say that is associative if the formula x (y z) always has the same truth value as the formula (x y) z. Determine whether each of the following connectives is associative. 1. AND (∧) 2. XOR 3. NAND Answer available. Warm up 4.3. Is each of the following a tautology? Is it satisfiable? 1. x ∨ ¬y 2. x ∨ ¬x 3. x ∧ (¬x ∨ y) Answer available. PART B . BOOLEAN LOGIC 20 Exercise 4.4. Write truth tables for each of the following formulas: 1. (y ∧ x) ∨ (y ∧ ¬x) 2. ((x → y) → z) → (x ∧ y) → z Exercise 4.5. Suppose that is a binary connective. We say that is associative if the formula x (y z) always has the same truth value as the formula (x y) z. Determine whether each of the following connectives is associative. 1. OR (∨) 2. IF (→) 3. ↔ Exercise 4.6. The majority vote connective M(x, y, z) is a particular logical connective that takes three inputs. It returns True if at least two of the inputs are True, and it returns False otherwise. So the inputs “vote” on the output, in a certain sense. Write a truth table for M(x, y, z). It will have 8 rows. Exercise 4.7. Is each of the following a tautology? Is it satisfiable? 1. ¬(x → ¬x) 2. x ∨ (¬x → y) 3. (x → y) ∧ ¬y Culture of Computer Science 4.8. Verify that the following formula is a tautology: (x → y → z) ↔ (x ∧ y) → z. Investigate why this tautology is called Currying. PART B . BOOLEAN LOGIC 5 21 Rules of Inference In principle, we can verify that two formulas are equivalent by computing a truth table for each and checking whether all of the rows come out to the same value. But this is an inefficient method. If there are n input variables, we need to write 2n rows of the truth table. This would be impractical for n = 10 and impossible for n = 1000. In applications of Boolean logic, it is common to work with formulas that have hundreds or thousands of variables. In some cases, we can verify that particular formulas are equivalent by transforming one of them into the other via operations that we know preserve the truth table of the formula. These operations are called rules of inference, because they allow us to “infer” a new formula from a given formula. We will survey several important rules of inference in this section, but there are many more that are sometimes of interest. Definition 6 (De Morgan’s Laws). Suppose that A and B are any formulas. The following formulas are tautologies: ¬(A ∧ B) ↔ (¬A ∨ ¬B), ¬(A ∨ B) ↔ (¬A ∧ ¬B). These tautologies give us four rules of inference: • Replace ¬(A ∧ B) with (¬A ∨ ¬B), and vice versa. • Replace (¬A ∨ ¬B) with ¬(A ∧ B) and vice versa. Example 10. Suppose that we begin with ¬(A ∧ ¬(B ∨C)). Using De Morgan’s law, we can replace this with (¬A) ∨ ¬¬(B ∨ C). Because the negation of the negation of a formula has the same truth value as the original formula, we can cancel the ¬¬. Therefore, our original formula is equivalent to the formula (¬A) ∨ B ∨C. Definition 7 (Distributive laws). Suppose that A, B, and C are any formulas. The following formulas are tautologies: A ∧ (B ∨C) ↔ (A ∧ B) ∨ (A ∧C), A ∨ (B ∧C) ↔ (A ∨ B) ∧ (A ∨C). These tautologies give us four more rules of inference: • Replace A ∧ (B ∨C) with (A ∧ B) ∨ (A ∧C), and vice versa. • Replace A ∨ (B ∧C) with (A ∨ B) ∧ (A ∨C), and vice versa. Definition 8 (Implication to disjunction rule). Suppose A and B are any formulas. The following formula is a tautology: (A → B) ↔ (¬A ∨ B). This tautology gives us a rule of inference: • Replace A → B with ¬A ∨ B, and vice versa. We can use negation with the implication to disjunction rule, and apply De Morgan’s Law, to get another tautology: ¬(A → B) ↔ ¬(¬A ∨ B) ↔ (A ∧ ¬B). The rules we have seen so far can be used in either direction. Some rules, like the next one, only work in one direction. PART B . BOOLEAN LOGIC 22 ¬¬A ⇔ A De Morgan’s Laws ¬(A ∧ B) ¬(A ∨ B) ⇔ ⇔ (¬A ∨ ¬B) (¬A ∧ ¬B) Distributivity A ∨ (B ∧C) A ∧ (B ∨C) ⇔ ⇔ (A ∨ B) ∧ (A ∨C). (A ∧ B) ∨ (A ∧C). Associativity A ∧ (B ∧C) A ∨ (B ∨C) ⇔ ⇔ (A ∧ B) ∧C (A ∨ B) ∨C Implication A→B ⇔ (¬A) ∨ B Equivalence A↔B ⇔ (A → B) ∧ (B → A) Currying A→B→C ⇔ (A ∧ B) → C Double negation elimination (Involution) Figure B .2: Table of some rules of inference that come from logical equivalences. Each rule says that the formulas in the second and third columns are logically equivalent. Modus ponens A ∧ (A → B) ⇒ B Modus tollens ¬B ∧ (A → B) ⇒ ¬A Hypothetical syllogism (A → B) ∧ (B → C) ⇒ A→C Resolution (A ∨ B) ∧ (¬A ∨C) ⇒ B ∨C Figure B .3: Table of some one-way inference rules. Each rule says that the formula in the third column can be inferred from the formula in the second column. Definition 9 (Modus ponens). Suppose A and B are any formulas. The following formula is a tautology: A ∧ (A → B) → B This tautology gives us a rule of inference: • Replace A ∧ (A → B) with B. Unlike the previous rules, this rule only works in one direction, “from left to right”. This is because the tautology that verifies this rule uses → instead of ↔. Overall, any tautology based on ↔ or → will give a rule of inference. Some of these rules are listed in Figures B .2 and B .3. PART B . BOOLEAN LOGIC 23 Exercises for Section 5 Warm up 5.1. Use inference rules until you eliminate the need for any parentheses. 1. (y ∧ z) ∨ (¬y) 2. (x → y) ∧ ¬y Answer available. Warm up 5.2. Use De Morgan’s law repeatedly until the negation are directly applied to the variables. 1. ¬((x ∧ y) ∨ (z ∧ w)) 2. ¬((x ∨ y ∨ z) ∧ (w ∨ u)) Answer available. Exercise 5.3. Use De Morgan’s law repeatedly until the negation is directly applied to the variables. 1. ¬((x ∨ y) ∧ (z ∨ w)) 2. ¬((x ∧ y ∧ z) ∨ (w ∧ u)) Exercise 5.4. Apply distribution repeatedly until the expression is fully expanded. 1. (x ∨ y) ∧ (z ∨ w) 2. (x ∧ y) ∨ (z ∧ w) Exercise 5.5. List at least two correct inference rules that are in not in the tables above. Explain why each rule is correct. Exercise 5.6. Give an argument in complete sentences that explains why the Resolution inference rule is correct. Exercise 5.7. Give a complete argument, in your own words, that explains why the following statement is correct: Two formulas A and B are logically equivalent if and only if A ↔ B is a tautology. Your answer should be written in complete sentences. Culture of Computer Science 5.8. Explore why the resolution inference rule is used in computer science and artificial intelligence. PART B . BOOLEAN LOGIC 6 24 Disjunctive and Conjunctive Normal Forms In this section, we will consider two normal forms for formulas: disjunctive normal form and conjunctive normal form. These are special kinds of expressions that can be used to represent any Boolean connective. The motivation for using normal forms is that arbitrary formulas may be written in a very complicated way, with many nested parentheses and many Boolean connectives. The normal forms express the formulas in a simpler way, and never have a parentheses depth more than 2. Algorithms can be designed specifically for formulas in normal form, making use of the simpler structure of these formulas. The normal forms we will study begin with simple expressions known as literals. Definition 10. A literal is a Boolean variable or the negation of a Boolean variable. Instead of writing ¬x for the negation of a variable x, we will often write x̄, to save space. So both x and x̄ are literals, for every variable x. Definition 11 (Disjunctive normal form). A conjunctive clause is a conjunction (“and”) of some number of literals. A Boolean connective is in disjunctive normal form if it is written as the disjunction (“or”) of some number of conjunctive clauses. Example 11. The following formulas are all in disjunctive normal form. We can identify the conjunctive (“and”) clauses in each. (x ∧ y) ∨ (x̄ ∧ y) (x ∧ y ∧ z) ∨ (z̄ ∧ q) ∨ (x ∧ ȳ ∧ w) w∨z x ∧ y ∧ z. Theorem 1. Every Boolean function has at least one disjunctive normal form. Proof. As we write the proof, we also show an example. Suppose that the formula is x ↔ (y XOR z). First, write a truth table for the formula. For our example, the truth table is: x y z y XOR z x ↔ (y XOR z) Mark F F F F T * F F T T F F T F T F F T T F T T F F F F T F T T T * T T F T T * T T T F F * Next, write a star beside each row that evaluates to True. Make a conjunctive clause out of each of these rows. There will be one conjunct for each variable, and the conjunct will have a bar if that PART B . BOOLEAN LOGIC 25 variable was set to False in the row we are looking at. The clauses from the table above are x̄ ∧ ȳ ∧ z̄ x̄ ∧ y ∧ z x ∧ ȳ ∧ z x ∧ y ∧ z̄ Finally, form the disjunctive normal form by taking the disjunction of the conjunctive clauses that were formed in the previous step: (x̄ ∧ ȳ ∧ z̄) ∨ (x̄ ∧ y ∧ z) ∨ (x ∧ ȳ ∧ z) ∨ (x ∧ y ∧ z̄). We need to argue that this formula is logically equivalent to the original Boolean function. This follows from the construction: each conjunctive clause in the DNF we made is True in exactly one row of the truth table, and we chose the conjunctive clauses to match exactly the rows where the original formula was True. Each formula has many equivalent DNF formulas. Our algorithm only produces one of them, but this is enough to prove that there is always at least one DNF for each formula. Conjunctive Normal Form Definition 12 (Conjunctive normal form). A disjunctive clause is a disjunction (“or”) of some number of literals. A Boolean connective is in conjunctive normal form if it is written as the conjunction (“and”) of some number of disjunctive clauses. Example 12. The following formulas are all in conjunctive normal form. We can identify the disjunctive (“or”) clauses in each. (x ∨ y) ∧ (x̄ ∨ y) (x ∨ y ∨ z) ∧ (z̄ ∨ q) ∧ (x ∨ ȳ ∨ w) w∨z x ∧ y ∧ z. Theorem 2. Every Boolean function has at least one conjunctive normal form. Proof. We begin by making a truth table for our formula, as in the previous proof, and marking the rows that are True with a star. We can make a CNF formula by looking at the rows of the truth table that are not marked with a star. We want to write a formula that says that “we are not in one of these rows”. For example, to say that we are not in the row that begins FFT, we could write (x ∨ y ∨ z̄). We use an “or” because any combination of x being True, y being True, and/or z being False is enough to guarantee we are not looking at the FFT row. We combine these disjunctive clauses with “and” because we want to say that we are not in any of the rows that evaluate to False. For example, for the formula x ↔ (y XOR z), we obtain this CNF: (x ∨ y ∨ z̄) ∧ (x ∨ ȳ ∨ z) ∧ (x̄ ∨ y ∨ z) ∧ (x̄ ∨ ȳ ∨ z̄). Again, we need to prove that this formula is equivalent to the original formula. If the original formula evaluates to True for some values of the inputs, then we are not in any of the rows that evaluate to False. So all of the disjunctive clauses in our CNF will be True, and so their conjunction will be True. PART B . BOOLEAN LOGIC 26 Conversely, if our CNF is True for some values of the inputs, then all of the disjunctive clauses are True. But this means we are not in any of the rows where the original formula evaluated to False. So whichever row we are in, the original formula must have evaluated to True. There are also many different CNF formulas for a given starting formula. Our algorithm only produces one of them. Equivalence of DNF and CNF Every formula can be written in disjunctive normal form and also in conjunctive normal form. The two distrubutivity rules of inference give a method to convert between the disjuntive and conjunctive normal forms of a formula. From the distribution rule x ∧ (y ∨ z) ↔ (x ∧ y) ∨ (x ∧ z), the conjunctive normal form x ∧ (y ∨ z) is equivalent to the disjunctive normal form (x ∧ y) ∨ (x ∧ z). From the distribution rule x ∨ (y ∧ z) ↔ (x ∨ y) ∧ (x ∧ z), the disjunctive normal from x ∨ (y ∧ z) is equivalent to the conjunctive normal form (x ∨ y) ∧ (x ∧ z). Exercises for Section 6 Warm up 6.1. Come up with an example of a disjunctive normal form that is not satisfiable. Answer available. Warm up 6.2. Use the truth table to write a formula for x → y in disjunctive normal form and to write a formula for x → y in conjunctive normal form. Answer available. Exercise 6.3. For each of the following, is it in disjunctive normal form, conjunctive normal form, both, or neither? 1. (x ∨ y) ∧ (y ∨ z) 2. (z ∧ x) ∨ y 3. x ∨ y 4. x ∧ x 5. (x ∧ y) ∨ (x ∨ z) Exercise 6.4. Use inference rules to write the formula (x → y) ∧ (y ∨ w) in disjunctive normal form and also in conjunctive normal form (without using a truth table). Exercise 6.5. If we have any formula, we can use a truth table to tell whether the formula is satisfiable. But if the formula is in disjunctive normal form, there is an easier way to tell. Come up with a better algorithm that takes a formula in disjunctive normal form and tells whether the formula is satisfiable. Exercise 6.6. The majority vote connective M(x, y, z) is a particular logical connective that takes three inputs. It returns True if at least two of the inputs are True, and it returns False otherwise. So the inputs “vote” on the output, in a certain sense. Use the truth table to write a formula for M(x, y, z) in disjunctive normal form and to write a formula for M(x, y, z) in conjunctive normal form. PART B . BOOLEAN LOGIC 27 Culture of Computer Science 6.7. What is the 3-SAT problem in computer science, and how is it related to normal forms? Culture of Mathematics 6.8. Research George Boole, C.S. Peirce, and Augustus De Morgan. Sketch the timelines of their work. What contributions did each of them make to logic? 7 Complete Set of Connectives Definition 13. A set of connectives is complete if every formula can be rewritten to only use those connectives. Example 13. The set {∧, ∨, ¬} is complete, because every formula can be written in DNF, and the DNF will only use these three connectives. To show that another set of connectives is complete, you need to show that each of AND, OR, and NOT can be expressed in terms of the connectives in the set. Example 14. To show that {IF, NOT} is a complete set of connectives, we need to show that we can convert any formula to use those connectives. We know that any formula can be written using AND, OR, and NOT using disjunctive normal form. So we need to show that we can write each of AND, OR, and NOT in terms of IF and NOT. To begin, we note that NOT can be written as NOT. To write OR in terms of NOT, we can use the tautology (P → Q) ↔ (¬P ∨ Q). If we replace P with ¬P in the tautology, we obtain (¬P → Q) ↔ (¬¬P ∨ Q). Canceling the double negative, (¬P → Q) is equivalent to P ∨ Q. So we can rewrite OR using only NOT and IF. To write AND in terms of IF and NOT, we use De Morgan’s law: (P ∧ Q) ↔ ¬(¬P ∨ ¬Q). Because we know how to rewrite OR using only IF and NOT, we can rewrite ¬(¬P ∨ ¬Q) to use only IF and NOT, which is the same as writing P ∧ Q using only IF and NOT. In Example 14, we used the complete set of connectives {AND, OR, NOT}. Nothing is particularly special about this set. You can use any set of connectives that is known to be complete to verify other sets are or are not complete. Warm up 7.1. Complete Example 14 by formally writing AND using only IF and NOT. Answer available. Warm up 7.2. Show that {NAND} is a complete set of connectives. In other words, every formula can be rewritten to only use NAND. Answer available. Exercise 7.3. Let ⊥ be a connective that always returns False. Show that {IF, ⊥} is a complete set of connectives. Again, you need to show that some other complete set of connectives can be expressed in terms of IF and ⊥. Exercise 7.4. Show that {NOR} is a complete set of connectives. In other words, every formula can be rewritten to only use NOR. Additional Problems for Part B B.1 Be able to write the truth table for each of the binary connectives (e.g. implication, XOR, NAND, etc.) Answer available. PART B . BOOLEAN LOGIC 28 B.2 Make a truth table for x ∧ (y ↔ z). Then write an equivalent disjunctive normal form formula and an equivalent conjunctive normal form formula. Answer available. B.3 Decide whether each of these is satisfiable. If it is, tell what values of the variables will make the formula true. (a) (w ∧ x ∧ z ∧ ¬w) ∨ (w ∧ y ∧ x̄ ∧ z) (b) (x ∧ y ∧ z̄ ∧ x̄) ∨ (y ∧ z̄ ∧ x ∧ ȳ). Answer available. B.4 Use distributivity to expand (x ∨ y) ∧ (p ∨ q) until it is in disjunctive normal form. Answer available. B.5 Use rules of inference to convert the following formula into conjunctive normal form: ¬(x ∨ ¬y) ∨ (y) Show the steps you took to perform the simplification, and label the rule used for each step. Answer available. Part C Sets and Functions 8 Sets, Subsets and Set Equality Definition 14. A set is a collection of objects. These objects can be of any kind: they can be numbers, functions, people, shapes, other sets, or anything else. The objects in the collection are called the elements (also members) of the set. The defining characteristic of a set is that each object either is, or is not, an element of the set. If an object a is an element of a set X, we write a ∈ X. Example 15. The set {1, 2, 3, 4} contains the numbers 1, 2, 3, and 4, and contains nothing else. The set {a, b, c} contains a, b, c, and nothing else. Example 16. There is a set that has no elements at all. We call this the empty set, denoted 0. / Definition 15. Certain common sets have special symbols: • N is the set {0, 1, 2, . . .} of nonnegative integers – also called the set of natural numbers. • Z is the set of integers. • Q is the set of rational numbers. • R is the set of real numbers. • C is the set of complex numbers. The font for these is called blackboard bold, because in handwriting they have an extra vertical line that is easy to draw on a blackboard. Definition 16. The size of a set A (also called the cardinality) is the number of elements in the set. The size of a set A is written |A|. The size of a set can be finite or infinite. In this course, we will only look at sizes of finite sets after the next example. The size of a finite set is always a nonnegative integer, but there are many additional sizes of infinite sets. For example, the size of the set of natural numbers is less than the size of the set of real numbers. The theory of infinite cardinalities is taught in Math 300 and higher math classes, and is a basic part of set theory. Example 17. The size of {1, 2, 3, 4} is 4. The size of {a, b, c} is 3. The size of N is infinite, as is the size of Q. The size of the empty set is zero. Set Operations Just as there are addition and multiplication operations on integers, there are several operations on sets. 29 PART C . SETS AND FUNCTIONS Set A Set B 30 Set A A Set A B Set B Set A Union: A ∪ B Set A Set B Set B Intersection: A ∩ B Set B Relative complement: A \ B Set A Set B Symmetric difference: A 4 B Figure C .1: Venn diagrams showing sets A, B, A ∪ B, A ∩ B, A \ B, and A 4 B. The set A contains all the points in the left circle; the set B contains all the points in the right circle. In each of the remaining diagrams, the set of shaded points is the set produced by the given operation. For each diagram, the points inside the box represents all the points in the universal set. PART C . SETS AND FUNCTIONS 31 Definition 17 (Union and intersection). Suppose that A and B are sets. The union of A and B is a new set, written A ∪ B, which contains every element that is in A, in B, or in both: A ∪ B = {x : x ∈ A or x ∈ B}. The intersection of A and B is a new set, written A ∩ B, which contains every element that is in both A and B: A ∩ B = {x : x ∈ A and x ∈ B}. We often use Venn diagrams to visualize set operations. Figure C .1 shows how these diagrams work. Definition 18 (Set difference). Given sets A and B, the set difference A \ B is a new set that consists of the points that are in A and not in B: A \ B = {x : x ∈ A and x 6∈ B}. The set difference is sometimes written as A − B. The symmetric difference A 4 B is the set (A \ B) ∪ (B \ A). It consists of the points that are in exactly one of the two sets A and B. The set difference operation is also called relative complement. The reason for the name is that A \ B is the part of A that is not in B, so A \ B is like “the complement of B from the perspective of A”. In many settings, we have a universal set based on the context of our work. For example, if we are looking at several sets of natural numbers, our universal set would be the set of all natural numbers. If we are looking at several sets of binary strings, our universal set would be the set of all binary strings. Definition 19 (Set complement). If we have a universal set U in mind, the complement of a set B ⊆ U is the difference U \ B. We write the complement of B as Bc or as B. Unlike the other set operations, the complement operation leaves the universal set unwritten. Therefore, it is always necessary to be sure that you and your reader will have the same universal set in mind before applying the complement operation. Definition 20 (Cartesian product). The Cartesian product of two sets A and B, written A × B, is the set of all ordered pairs (a, b) where a ∈ A and b ∈ B: A × B = {(a, b) : a ∈ A and b ∈ B}. Set Equality and Subsets Definition 21. Sets A and B are equal if they have the same elements. Definition 22. A set A is a subset of a set B if every element of A is also an element of B. If A is a subset of B we write A ⊆ B. Example 18. Every set is a subset of itself, and the empty set is a subset of every set. If A = {0, 1}, B = {0, 1, 3}, and C = {1, 3} then A ⊆ B, C ⊆ B, A 6⊆ C, and C 6⊆ A. Definition 23 (Powerset). The powerset of a set A is the set of all subsets of A. The powerset of a set A is written P(A). PART C . SETS AND FUNCTIONS 32 Example 19. The powerset of {1, 2} is the set {0, / {1}, {2}, {1, 2}}. The powerset of {a, b, c} is {0, / {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}}. We will show later that if a set A has size n then P(A) has size 2n . Exercises for Section 8 Warm up 8.1. For the Venn diagram below, for each of the regions R1 through R4 , write out an expression using set operations to give that region in terms of the sets A, and B. Assume that the universal set, U, consists of all points in the rectangle. Answer available. Set A Set B R2 R3 R1 R4 Exercise 8.2. For the Venn diagram below, for each of the regions R1 through R8 , write out an expression using set operations to give that region in terms of the sets A, B, and C. Assume that the universal set, U, consists of all points in the rectangle. R1 Set C R2 R4 R3 Set A Set B R6 R5 R7 R8 PART C . SETS AND FUNCTIONS 33 Exercise 8.3. Recall N = {0, 1, 2, 3, . . .}. Consider the following sets: A = {1, 2, 6} B = {n ∈ N : n ≤ 10} C = {n ∈ N : n < 15 and n is prime}. 1. Write out explicitly the elements of C. 2. Find A ∪ B, A ∩ B. 3. Find A −C. 4. Find A ×C. 5. Find P(A). Exercise 8.4. Write out an argument in English sentences to explain why, if A and B are finite sets, it is always correct that |A ∪ B| = |A| + |B| − |A ∩ B|. This formula is called the inclusion-exclusion principle. Culture of Mathematics 8.5. The letters Z and Q are used for the set of integers and the set of rational numbers, respectively, in blackboard bold. This notation was introduced by Nicholas Bourbaki. Give a short biography of Bourbaki and explain why they chose these particular letters. PART C . SETS AND FUNCTIONS 9 34 Relations and Functions Definition 24. A relation between a set A and a set B is a subset of A × B. If R is a relation between A and B, we often write aRb or R(a, b) instead of (a, b) ∈ R. Example 20. Let People be the set {Brandi, John, Molly, Tyrone} representing four people. Let the set Animals be the set {Cat, Dog, Fish} that represents three particular kinds of pets. To represent the pets that the people have, we may consider the relation Pet = {(Brandi, Cat), (Brandi, Dog), (Molly, Cat), (Tyrone, Cat), (Tyrone, Fish)}. So we have Pet ⊆ People × Animals. According to the relation, Brandi has two kinds of pets, John has no pets, Molly has one kind of pet, and Tyrone has two kinds of pets. In computer science, it is common to write relations as tables rather than lists. The Pet relation would be written in the following way. Person Brandi Brandi Molly Tyrone Tyrone Animal Cat Dog Cat Cat Fish If we consider the set Toys = {Bone, Catnip, Castle}, we can form a relation BestToy = {(Dog, Bone), (Cat, Catnip), (Fish, Castle)}. The relation BestToy tells us which kind of toy is preferred by each kind of pet. It is a subset of Animals × Toys. In tabular form, we would have: Animal Dog Cat Fish Toy Bone Catnip Castle Definition 25. Let A and B be sets and let R ⊆ A × B be a relation. We say that R is a function from A to B if for each a ∈ A there is exactly one b ∈ B so that the pair (a, b) is in R. Example 21. The relation BestToy above is a function from Animals to Toys. On the other hand, Pet is not a function from People to Animals, for two reasons. First, there is no pair in the relation Pet whose first element is John. Second, some elements of People are the first element of more than one pair in Pet. Either of these is enough to show that Pet is not a function. Example 22. Consider the relations on R × R given by S1 = {(x, y) : y = x3 }, S2 = {(x, y) : x = 2y }. Then: • The pairs (2, 8) and (3, 27) are elements of S1 , while (16, 4) and (8, 3) are elements of S2 . • The relation S1 is a function from R to R. For each x ∈ R there is a unique pair (x, x3 ) in S1 that begins with x. • The relation S2 is a function from the interval (0, ∞) to R, because for each x in the interval there is exactly one pair in S2 starting with x, namely (x, log2 (x)). The relation S2 is not a function from R to R, however, because (for example) there is no pair in S2 beginning with −1. PART C . SETS AND FUNCTIONS 35 Composition We can combine functions and relations with composition. Definition 26. If R ⊆ A × B is a relation, and S ⊆ B × C is a relation, the composition S ◦ R is a particular subset of A ×C, that is, a relation between A and C: S ◦ R = {(a, c) : for some b ∈ B, (a, b) ∈ R and (b, c) ∈ S}. Example 23. Suppose R = {(1, a), (2, a), (2, b), (3, c)} and S = {(a, x), (b, x), (b, z), (c, y), (c, z)}.. Then S ◦ R = {(1, x), (2, x), (2, z), (3, y), (3, z)}.. We can view this using the following diagram. 1 a x 2 b y 3 R c z S 1 x 2 y 3 S◦R z We can compose any two relations. In particular cases, though, when we compose two functions we obtain a function. Theorem 3. If R is a function from A to B and S is a function from B to C then S ◦ R is a function from A to C. Proof. First, we need to prove that for every a ∈ A there is some c ∈ C with (a, c) ∈ S ◦ R. Because R is a function from A to B, there is some b ∈ B with (a, b) ∈ R. Because S is a function from B to C, there is some c ∈ C with (b, c) ∈ S. By the definition of S ◦ R, this means that (a, c) ∈ S ◦ R. Second, we need to show that if (a, c) and (a, c0 ) are pairs in S ◦ R, with the same first coordinate, then c = c0 . Since (a, c) and (a, c0 ) are in S ◦ R, there must be b, b0 ∈ B with (a, b) ∈ R, (a, b0 ) ∈ R, (b, c) ∈ S, and (b0 , c0 ) ∈ S. Because R is a function, we see that b = b0 . Then, because (b, c) ∈ S and (b, c0 ) ∈ S, and S is a function, we have c = c0 , as desired. Definition 27 (Multiple Cartesian Products). . If X = {1, 2}, Y = {a, b}, and Z = {p, q}, then we have: X ×Y = {(1, a), (1, b), (2, a), (2, b)}, Y × Z = {(a, p), (a, q), (b, p), (b, q)}. PART C . SETS AND FUNCTIONS 36 If we think about the “triple Cartesian product” X ×Y × Z, we could form it out the the binary Cartesian product in two ways: (X ×Y ) × Z = {((1, a), p), ((1, b), p), ((2, a), p), ((2, b), p), ((1, a), q), ((1, b), q), ((2, a), q), ((2, b), q)}, X × (Y × Z) = {(1, (a, p)), (1, (a, q)), (1, (b, p)), (1, (b, q)), (2, (a, p)), (2, (a, q)), (2, (b, p)), (2, (b, q))}. In many cases, these sets serve equivalent purposes, and we can simply view X ×Y × Z as the set of all triples (x, y, z) where x ∈ X, y ∈ Y , and z ∈ Z: X ×Y × Z = {(1, a, p), (1, b, p), (2, a, p), (2, b, p), (1, a, q), (1, b, q), (2, a, q), (2, b, q)}. Exercises for Section 9 Warm up 9.1. Each of the following relations is intended to be a subset of R × R. Determine whether each of these is a function from R to R. Justify each answer. 1. R1 = {(x, y) : x < y} 2. R2 = {(x, y) : y = x2 } 3. R3 = {(x, y) : x = y2 } 4. R4 = {(x, y) : y = 1/x} Answer available. Exercise 9.2. Let A = {1, 2, 3}, B = {cat, dog}, and C = {mouse, cheese, bone}. Let R = {(1, cat), (2, cat), (2, dog), (3, dog)}, S = {(cat, mouse), (cat, cheese), (dog, cheese), (dog, bone)}. Explicitly write out the set S ◦ R. Exercise 9.3. If R ⊆ A × B and S ⊆ B × C, the join of R and S is a new set which is a subset of A × B ×C. We will write this set as R ./ S. It is defined as follows: R ./ S = {(a, b, c) : (a, b) ∈ R and (b, c) ∈ S} Let A = {1, 2, 3}, B = {cat, dog}, and C = {mouse, cheese, bone}. Let R = {(1, cat), (2, cat), (2, dog), (3, dog)}, S = {(cat, mouse), (cat, cheese), (dog, cheese), (dog, bone)}. Explicitly write out the set R ./ S. Culture of Mathematics 9.4. Research Russell’s paradox. How is it related to sets? Why is it of interest to mathematicians and computer scientists? PART C . SETS AND FUNCTIONS 10 37 Multisets Definition 28. A multiset is like a set, except that an element of a multiset can be a member more than one time. The number of times that an element is a member of a multiset is the multiplicity of the element. As notation, we will write {m1 · a1 , m2 · a2 , m3 · · · a3 , . . . , mn · an } for a multiset that has element a1 with multiplicity m1 , has element a2 with multiplicity m2 , etc. Example 24. Every set is a multiset, where each element has multiplicity 1. In the multiset A = {p, p, q, q, q}, the element p has multiplicity 2 and the element q has multiplicity 3. We can write A = {2 · p, 3 · q}. Example 25. One example of a multiset is the collection of prime divisors of a nonzero natural number. For example 24 = 3 × 2 × 2 × 2, so the multiset of divisors of 24 has 3 with multiplicity 1 and 2 with multiplicity 3. Definition 29 (Operations on multisets). Let A and B be multisets. • The union of A and B is the multiset A ∪ B in which the multiplicity of each element is the maximum of its multiplicity in A and its multiplicity in B. • The intersection of A and B is the multiset A ∩ B in which the multiplicity of each element is the smaller of its multiplicity in A and its multiplicity in B. • The difference of A and B is the multiset in which the multiplicity of each element is its multiplicity in A minus its multiplicity in B, except that the multiplicity is zero if the difference is negative. • The disjoint union (also called the sum) of A and B is the multiset A t B in which the multiplicity of each element is the sum of its multiplicity in A and its multiplicity in B. Example 26. Let A = {2 · p, 3 · q} and B = {1 · p, 4 · q, 2 · r}. Then A ∪ B = {2 · p, 4 · q, 2 · r}, and A ∩ B = {1 · p, 3 · q}. We also have B \ A = {1 · q, 2 · r} and A t B = {3 · p, 7 · q, 2 · r}. Example 27. Because every set can already be viewed as a multiset, we can also apply the disjoint union operation to sets. For example, if X = {1, 3, 4} and Y = {3, 4, 5}, then X tY = {1 · 1, 2 · 3, 2 · 4, 1 · 5}. Exercises for Section 10 Warm up 10.1. For each nonzero natural number n, let D(n) be the multiset of prime divisors of n. Write out the multisets D(60), D(15), D(4) and D(900). Answer available. Exercise 10.2. Let A = {1 · x, 2 · y, 4 · z}, B = {3 · x, 1 · y, 5 · z}. Write out A ∪ B, A ∩ B, A \ B, B \ A, and A t B. PART C . SETS AND FUNCTIONS 38 Exercise 10.3. For each nonzero natural number n, let D(n) be the multiset of prime divisors of n. • Let m, n > 1. How is D(m × n) related to D(m) and D(n)? Explain in a few sentences. • Let m, n > 1. Assume that m is a multiple of n How is D(m|n) related to D(m) and D(n)? Explain in a few sentences. Exercise 10.4. Another application of multisets is in tracking resources. Suppose that baking bread requires three units of flour, one unit of yeast, and one unit of salt: B = {3 · Flour, 1 · Yeast, 1 · Salt}. Baking a cake requires one unit of flour, two units of sugar, on unit of butter, and one unit of eggs: C = {1 · Flour, 2 · Sugar, 1 · Butter, 1 · Eggs}. I am going to go to the supermarket to buy supplies, but I don’t know yet what I will make. • Suppose I know that I will bake a cake or bake bread, but not both. What multiset of supplies do I need to purchase? How is this related to the multisets B and C? • Suppose I want to bake a cake and bake bread. What multiset of supplies do I need to purchase? How is this related to the multisets B and C? Additional Problems for Part C C.1 Let P be the set of prime numbers that are less than 12 and let Q be the set of positive odd numbers that are less than 10. (a) Write out explicitly the elements of P and Q. (b) Write out P ∪ Q, P ∩ Q, P \ Q, Q \ P, and P × Q. Answer available. C.2 Let L be the “less than” relation on R: L = {(x, y) ∈ R × R : x < y}. Explain why L ◦ L is the same relation as L. Answer available. C.3 In a collection of 30 old laptop computers, 18 will boot completely and 15 have a working keyboard. If there are 7 that don’t boot completely and don’t have a working keyboard, how many are there that do boot completely and do have a working keyboard? Answer available. Part D Proofs and Counting 11 Mathematical Proofs In mathematics and computer science, we often want to prove results. Definition 30. A proof is a argument, aimed at a particular audience, that convinces a reasonably skeptical person in that audience that a particular claim is correct. There are several key aspects to this definition: • Argument – the argument of a proof is usually written in natural language with mathematical symbols mixed in. An argument that is purely symbolic might be useful for a computer, but it is not likely to be clear to a human reader. • Audience – a proof written by a professional mathematician for another professional mathematician may be very different from a proof written by a professional mathematician for an undergraduate. The author of the proof has to consider the background knowledge of the audience and their familiarity with the topic and the methods being used. • Convincing – the goal of a proof is to convince the audience that the result is always correct, with no exceptions. • Reasonably skeptical – a reasonably skeptical audience member will ask for justification for statements they do not already know, and will be very sensitive to any vagueness or ambiguity in the argument. They will insist that all cases are covered. But they will not refuse to believe the argument once all statements are justified to their satisfaction. So they will start out skeptical about the result, but will be reasonable about accepting proofs that are correct. Proofs are a central part of mathematics and theoretical science such as theoretical computer science and theoretical physics. In math, the reason that we prove claims is so that we know they are always true. This allows us to rely on the later without revisiting the proof. If we had to re-check each result every time we used it, we would not be able to make much progress. By proving each result once, and then re-using it many times, we are able to achieve much more progress. Definition 31. The claims that are proved are called by many names. • A proposition is any claim that will be proved. • A theorem is an important result, or just the result being discussed currently. 39 PART D . PROOFS AND COUNTING 40 • A lemma is a result that is intended to be a stepping stone towards proving a more important proposition or theorem. • A corollary is a proposition whose proof follows quickly or easily from the proof of a different proposition. • A axiom is a result that is taken as an assumption, without needing its own proof. Most proofs rely on one or more axioms. • A definition is a relationship between a particular term and a more complicated mathematical statement. Definitions are not proved, but they can be used to help prove other propositions. The terms proposition, theorem, and lemma are very flexible, so in the end any of them could be used to refer to any claim that is being proved. A proposition that has not yet been proved, but is believed to be true, may be called a conjecture, an hypothesis, or an assumption. Theorem 4. If A is a set with n elements, then the powerset of A has 2n elements. Proof. We may number the elements of A with the numbers 1 through n. Then every binary number with n digits corresponds to a single subset of A, where each digit tells whether a particular element is, or is not, in the subset. (The ith digit tells whether element i is in the subset.) At the same time, every subset of A is named by some binary number with n digits. We put a 1 at each location of the binary number where the corresponding element of A is in the subset, and 0 everywhere else. So the number of subsets of A is exactly the same as the number of binary numbers with n digits. The largest binary number that we can express with n digits is 2n − 1, and the smallest number we can express with n digits is 0. So, including 0, there are exactly 2n distinct binary numbers with n digits. Therefore, there are also exactly 2n distinct subsets of our set A. Definition 32. If a particular claim leads to a contradiction, then the claim must be false. If we prove that a claim is true by showing that its negation leads to a contradiction, this is called a proof by contradiction. Theorem 5. The square root of 2 is not a rational number. Proof by contradiction. √ To proceed by contradiction, assume that the square root of 2 is rational. Then we can write 2 = mn where m and n are positive integers. We can also assume that mn is in lowest terms,√so that there are no common prime factors of m and n. 2 Now, if 2 = mn then 2 = mn2 , so 2n2 = m2 . Now, since 2n2 is even, this means m2 is even, so we can say m = 2r for some positive integer r. So we have 2n2 = (2r)2 = 4r2 , which means that n2 = 2m2 . This means n2 is even, so n itself must be even. We have reached a contradiction, because we started with mn in lowest terms, but now we have shown that m and n are √ both even. Thus there is no way to write the square root of 2 as a fraction mn in lowest terms, so 2 is not a rational number. PART D . PROOFS AND COUNTING 41 Exercises for Section 11 Warm up 11.1. Write a proof that shows the following: if n is an even integer then for any integer m, the product nm is even. Answer available. Warm up 11.2. Write a proof by contradiction that shows the following: there is no smallest rational number greater than 0. Answer available. Exercise 11.3. Write a proof that shows the following: if n is an even integer then n3 is a multiple of 8. Exercise 11.4. Write a proof that shows the following: there are positive irrational numbers r and s √ √2 s so that r is rational. Hint: consider ( 2) . PART D . PROOFS AND COUNTING 12 42 The Multiplication Principle and Words on an Alphabet This part of the course presents the beginnings of enumerative combinatorics. This field of mathematics aims to count how many objects have a particular kind of combinatorial property. For example, how many ways can we arrange the letters in ABBA? We assume here that the two copies of “A” are indistinguishable, as are the two copies of ”B”. By trying all the options, there are only six ways: AABB ABBA ABAB BABA BAAB BBAA. What if we have a longer word, such as MARSHALL or SCHOOLKEEPERS? How many ways can we arrange the letters? Would it be feasible to try it by hand? The Multiplication Principle In a typical “counting problem”, we will have a description of a particular set, and we want to count the number of elements in the set. The most basic combinatorial principle applies to situations where the overall counting problem can be split into two steps. For example, suppose that we have a class consisting of 10 first year students and 15 second year students, and we want to select one first year student and one second year student. We can split this into two steps: first choose the first year student, and then choose the second year student. Neither step affects the other. Multiplication Principle. If a process of counting can be broken into two steps, so that the first step has m choices and the second step has n choices, then the overall number of choices is m × n. Example 28. 1. We have a class consisting of 10 first year students and 15 second year students, and we want to select one first year student and one second year student. There are 10 × 15 = 150 ways to make the selection. 2. Suppose I need to choose a letter from A to Z and then a digit from 0 to 9. In the first step, there are 26 choices. In the second, there are 10 choices. So, overall, the number of ways to choose a letter from A to Z and then a digit from 0 to 9 is 26 × 10 = 260. In the example above, phrases such as “the number of ways 26 × 10 = 260” have a specific meaning for the equality sign. The quoted phrase should be read as “the number of ways is 26 × 10, which is 260.” Generalized Multiplication Principle. If a process of counting can be broken into k steps, so that the first step has n1 choices, the second step has n2 choices, and so on, then the overall number of choices is n1 × n2 × · · · × nk . Example 29. 1. We have a class consisting of 10 first year students, 15 second year students, 5 third year students, and 2 fourth year students. We want to select one student from each of the four levels. There are 10 × 15 × 5 × 2 = 1500 ways to make the selection. PART D . PROOFS AND COUNTING 43 2. In Pittsburgh, there is a restaurant chain called Burgatory that allows you to build custom burgers. A typical menu is shown in Figure D .1. If I want to choose one burger patty, one bun, one cheese, and one sauce, how many ways can I do this? There are 6 options for the patty, 6 options for the bun, 8 options for the cheese, and 17 options for the sauce. So, overall, there are 6 × 6 × 8 × 17 = 4896 ways to make the selection. Words on an Alphabet A very common counting problem involves words on an alphabet. Definition 33. An alphabet is a set of symbols – usually finite. A word on an alphabet is a finite sequence of symbols from the alphabet. A symbol may be used more than one time in a particular word, and not all the symbols of the alphabet need to appear. There is one empty word of length zero, which is denoted ε. Example 30. If our alphabet is Σ = {a, b, c} then these are all words on Σ: “abab”, “ca”, ε, “b”. Their lengths are 4, 2, 0, and 1, respectively. Theorem 6. The number of k-words on an alphabet with n symbols is nk . Proof. Suppose we have an alphabet with n letters. To make a k-word, we need to select one element of the alphabet for each of the k positions. The choice we make for each position is independent of the choices we make for the other positions. So there are n choices for the first position, n choices for the second position, and so on. According to the generalized multiplication principle, this means there are n × n × · · · × n (k times) overall ways to build a k-word. This is exactly nk . Example 31. Suppose that we have an alphabet Σ = {a, b, c}. Then we have n = 3, and: 1. The number of 2-words on Σ is 32 = 9. The complete list of these words is: aa ab ac ba bb bc ca cb cc. 2. The number of words from Σ of length 10 is 310 . 3. The number of words from Σ of length 0 is 30 = 1. This is the empty word, denoted ε. Scientific notation In combinatorics, we often encounter very large numbers. We can use scientific notation to estimate the size of a number, to make it easier for someone to understand the magnitude of the answer. For example, (52)6 = 19, 770, 609, 664, which is a large number. We can approximate the number as 1.97 × 1010 , or as 1.97E10, to show that the number is slightly larger than 1010 . In many cases, the magnitude is just as important as the exact value. PART D . PROOFS AND COUNTING Figure D .1: The “custom creation” menu from the Burgatory chain in Pittsburgh, circa 2011. 44 PART D . PROOFS AND COUNTING 45 Exercises for Section 12 Warning! There is no simple algorithm for solving counting problems. In many cases, the total number of possibilities is too large to write out by hand to check your answer. So you should always have an argument in words to explain why your answer is correct – this is what “checking your answer” means in the context of counting problems. Warm up 12.1. Compute the following and justify your answer: 1. The number of ways to select a third year student and a fourth year student from a collection of 5 third year students and 3 fourth year students. 2. The number of words of length 4 on an alphabet of size 26. Answer available. Warm up 12.2. Compute the following and justify your answer. 1. A bike lock has four dials, each of which reads 0 through 9. How many possible combinations are there? 2. A pass phrase is made by randomly choosing four English words from a list of 10,000 English words. How many possible pass phrases are there? Answer available. Exercise 12.3. 1. A password is made from an alphabet consisting of 26 upper case letters and 10 digits. How many 8 character passwords are there? 2. A password is made from an alphabet consisting of 26 upper case letters, 26 lower cases letters, and 10 digits. How many 7 character passwords are there? 3. A password is made from an alphabet consisting of 26 upper case letters, 26 lower case letters, 10 digits, and 33 special characters. How many 6 character passwords are there? Exercise 12.4. This exercise refers to the menu in Figure D .1. 1. How many ways are there to make a burger with one patty, one bun, one cheese, and one free topping? 2. How many ways are there to make a burger with one patty, one bun, one rub, and two sauces (possibly the same)? Exercise 12.5. Suppose that Σ = {a, b, c}. 1. Explain why the number of 4-words on Σ that begin with a is 33 . 2. How many 4-words on Σ end with c? 3. How many 4-words on Σ begin with a and end with c? Write out the entire list. 4. How many 4-words on Σ begin with a or end with c? Hint: think about the inclusion-exclusion principle. Exercise 12.6. 1. In a class of 30 students, 20 are mathematics majors and 15 are education majors. If all students are a mathematics major or an education major, how many are in both majors? 2. In a class of 25 students, 12 are history majors and 10 are Japanese majors. If 3 students are double majoring in history and Japanese, how many students in the class are in neither of the two majors? PART D . PROOFS AND COUNTING 13 46 Combinations and Permutations This section will look at two more counting problems: counting words that have distinct letters in each position, and counting subsets of a given size from a set. Definition 34. The factorial of a number n is the number n! = (n) × (n − 1) × · · · × (2) × (1). For example, 4! = 4 × 3 × 2 × 1 = 24. Definition 35. If Σ is an alphabet, a permutation of Σ is a word that uses each letter of Σ exactly once. A k-permutation is a sequence of k distinct letters from Σ. If Σ is an alphabet of size n, the number of k-permutations is represented by the expression P(n, k). The order of the distinct letters from Σ is important in a permutation Example 32. If Σ = {a, b, c, d}, there are 24 permutations of Σ; the set of permutations of Σ is abcd abdc acbd acdb adcb adbc bacd badc bcad bcda bdac bdca cabd cadb cbad cbda cdab cdba dabc dacb dbac dbca dcab dcba. This means that P(4, 4) = 24. There are 12 words that are 2-permutations; the set of 2-permutations of Σ is ab ac ad ba bc bd ca cb cd da db dc. This means that P(4, 2) = 12. Theorem 7. If Σ is an alphabet of size n, and k ≤ n, the number P(n, k) is given by the formula P(n, k) = (n) × (n − 1) × · · · × (n − (k − 1)) = n! . (n − k)! In particular, the number of permutations of Σ is n!. Proof. There are n choices for the first element of the permutation. Then there are n − 1 choices for the second element, because the first element cannot be repeated. Then there are n − 2 choices for the third element, because neither of the first two choices can be repeated. Continuing in this way, there are (n − (k − 1)) choices for the kth element. Applying the generalized multiplication principle, there will be (n) × (n − 1) × · · · × (n − (k − 1)) permutations of length k. Using algebra, we can verify this is the same as (n!)/(n − k)!. Example 33. Suppose that a committee of 9 people must choose a chair, a vice chair, and a secretary. No person can hold two more more of the jobs. How many ways can the committee make this choice? We can view our alphabet as consisting of the nine people. Then a selection of this kind is exactly a 3-permutation, where the first “letter” is the chair, the second “letter” is the vice chair, and the third “letter” is the secretary. Therefore, there are P(9, 3) = 9 × 8 × 7 = 504 ways to make the selection. PART D . PROOFS AND COUNTING 47 Combinations: Counting Subsets Definition 36. If Σ is a set of size n, a k-subset of Σ is a subset ofΣ with size k. The number of k-subsets of an n-element set is denoted C(n, k), and also denoted nk . These numbers are known as binomial coefficients. Remember that in a subset, each letter is distinct. The order of the letters is not important in a subset. Theorem 8. The number of k-subsets of a set of size n is given by the formula C(n, k) = P(n, k) n! = . k! (n − k)! × k! Proof. Suppose that Σ is a set of size n and that S is a k-subset of Σ. Then there are k! distinct permutations of S. So, overall, the number of k-permutations of Σ will be k! times the number of k-subsets: k! ·C(n, k) = P(n, k). The theorem follows by dividing both sides by k!. Example 34. A class has 10 first year student and 15 second year students. 1. If we want to choose a collection of 3 first year student from the class, the number of ways to do so is 10! C(10, 3) = = 120. 7! × 3! 2. If we want to choose a collection of 4 second year students, the number of ways to do this is C(15, 4) = 15! = 1, 365. 11! × 4! 3. If we want to choose 3 first year student and also choose 4 second year students, the number of ways to do this is 120 × 1365 = 163, 800. 4. If we want to choose 3 first year student and 4 second year students, and then those 7 students will select a team leader, there are 7 options for the team leader, so the overall number of ways to choose 3 first year student from 10, and choose 4 second year students from 15, and then choose a leader from those 7 students is 120 × 1365 × 7 = 1, 146, 600. The previous example shows the phenomenon of combinatorial explosion. Even when we start with a collection of modest size, when we begin to select multiple subsets or combine multiple selections, the overall number of possibilities can grow extremely quickly. PART D . PROOFS AND COUNTING 48 Pascal’s Triangle One of the most famous ways of studying binomial coefficients is Pascal’s triangle. This is an infinite triangle of natural numbers. The first 6 rows are shown below. 1 1 1 2 3 1 1 5 1 3 6 4 1 1 10 1 4 10 1 5 1 The triangle is made by filling in 1s on the edges and filling in the center by adding the two entries diagonally above each position. For example, in the diagram above, 2 and 1 are added to make 3, and 1 and 3 are added to make 4. We can use the triangle to evaluate binomial coefficients. You will prove this method is correct in Exercise 13.6. 0 0 0 1 2 1 1 1 3 2 1 2 1 3 3 3 1 1 6 4 1 4 4 1 5 5 10 10 5 1 1 6 1 6 15 20 15 6 1 1 0 1 2 1 1 1 3 2 1 2 1 3 3 3 1 1 6 4 1 4 4 1 5 5 10 10 5 1 1 6 1 6 15 20 15 6 1 To compute C(5, 3), count down the rows starting with zero, to find row 5, then then count down the diagonals starting with zero, to find diagonal 3. The point of intersection shows that C(5, 3) = 10. To compute C(6, 2), count down the rows starting with zero, to find row 6, then then count down the diagonals starting with zero, to find diagonal 2. The point of intersection shows that C(6, 2) = 15. 1 PART D . PROOFS AND COUNTING 49 Card Counting Many combinatorial problems involve cards. The basic facts about a standard American deck of cards are summarized below. Other countries have other standard decks of cards.1 A standard deck of cards • A standard American deck of cards has 52 cards. • There are 4 suits: spades ♠, clubs ♣, hearts ♥, and diamonds ♦. • There are 13 face values: 2 through 10, Jack, Queen, King, and Ace. • There is exactly one card for each combination of suit and face value. • There are 13 cards of each suit (one of each face value) and 4 cards of each face value (one of each suit). • There are two colors: black and red. The suits of spades and clubs are black. The suits of hearts and diamonds are red. There are 26 black cards and 26 red cards. Example 35. The following counting problems can be applied to study the game of poker. 1. There are C(52, 5) = 2, 598, 960 ways to select 5 distinct cards from a standard deck. In poker, a set of five cards is called a hand. 2. A selection of 5 cards is called a flush if all 5 cards have the same suit. We can break the problem of constructing a flush into two steps: first choose the suit, then choose the cards from that suit. So there are 4 ×C(13, 5) = 5, 148 flushes. 3. A full house is a collection of 5 cards so that there are two cards of one face value and three cards of a second, different face value. We can break the problem of constructing a full house into several steps: first choose the two face values, then choose two cards of the first face value, then choose three cards of the second face value. This gives P(13, 2) ×C(4, 2) ×C(4, 3) = 156 × 6 × 4 = 3, 744 as the total number of full houses from a standard deck of cards. 1 For examples of differing card decks, see List of traditional card and tile packs on Wikipedia. PART D . PROOFS AND COUNTING 50 Exercises for Section 13 Warm up 13.1. If we select hands of 5 cards from a standard deck: 1. How many hands will contain the ace of spades? 2. How many hands will contain the ace of spades and the two of hearts? 3. How many hands will not contain any hearts? Answer available. Warm up 13.2. According to Figure D .1: 1. How many possible burgers have one patty, one bun, two different cheeses, and three different free toppings? 2. How many possible burgers are there that have one patty, one bun, one sauce, and any collection of free toppings? Answer available. Warm up 13.3. If we select hands of 5 cards from a standard deck: 1. How many hands will contain exactly two aces? 2. How many hands will contain only red cards? Answer available. Exercise 13.4. In some variations of poker, a straight is a hand of 5 cards that can be put in a sequence that begins with a card in the range {A, . . . , 10} and then increases in face value by one step at a time. So the following are straights: (4♥, 5♠, 6♠, 7♦, 8♥), (9♣, 10♣, J♣, Q♣, K♣), and (A♥, 2♣, 3♦ 4♣, 5♥). However, these are not straights: (7♣, 8♣, 10♣, J♣, Q♣) and (K♠, A♥, 2♣, 3♦ 4♣). 1. How many 5 card hands are straights, according to the rules just mentioned? 2. A straight flush is a straight that only has one suit. How many 5 card hands are straight flushes, according to the rules just mentioned? Exercise 13.5. Suppose that I have a roster of 30 baseball players. I want to select a team of 9 players from my roster, and then select one of the nine players as the team captian. Let T be the number of ways I can do this. 1. One Math 220 student said that this formula will give the right number for T : 30 T= × 9. 9 Justify this answer in words. 2. Another Math 220 student said that this formula will give the right number for T : 29 T = 30 × . 8 Justify this answer in words. Exercise 13.6. 1. Give a proof that, if r ≥ s ≥ 0, then C(r + 1, s + 1) = C(r, s) +C(r, s + 1). Hint: interpret both sides as different ways of solving the same counting problem. 2. Use part (1) to explain why Pascal’s triangle gives the correct value for C(r, s) if we look at row r and diagonal s. Exercise 13.7. In part 3 of Example 35, why did we use P(13, 2) instead of C(13, 2)? Explain in words. PART D . PROOFS AND COUNTING 51 Exercise 13.8. In Section 11, we proved: If a set A has n elements, then the powerset of A has 2n elements. Let A be a set with n = 5 elements. To count the number of subsets of A (all the sets in the powerset of A), you could use the formula C(5, 0) +C(5, 1) +C(5, 2) +C(5, 3) +C(5, 4) +C(5, 5). 1. Compute the value from this formula for set A and confirm it matches with the value from Section 11. 2. Explain why this formula works to count the powerset of A. PART D . PROOFS AND COUNTING 14 52 Permutations from Multisets If we permute the letters in the word HERALD, no two permutations will appear to be the same word. But, it we permute the letters in BEET, several permutations will be the same. The difference is that the set of letters in HERALD is a set, while the collection of letters in BEET is a multiset that is not a set, because of the repeated E. Definition 37. A k-permutation on a multiset is a sequence of k elements from the multiset so that no element appears in the permutation more often than its multiplicity in the multiset. In the word BEET, if we delete the first E or the second E, either way wer obtain BET. So the same 3-permutation can appear in more than one way. We will generally want to ignore this phenomenon, and only consider the collection of distinguishable permutations. Definition 38. Two k-permutations on a multiset are distinguishable if there is at least one location where different letters appear in the two permutations. Example 36. The following are all the distinguishable 3-permutations of the letters in BEET. There can be at most one B, at most one T, and at most two E’s. We make the table by choosing the first letter, then the second letter, and so on. BEE BET BTE EBE EBT EEB EET ETB ETE TBE TEB TEE The sequence BEB is not a 3-permutation of the letters, because B occurs too many times. We can also visualize the table in terms of a tree diagram. Each branch of the tree diagram shows how a 3-permutation is formed, step by step, from the empty sequence ε. Third letter Second letter First letter BEE BET BTE EBE EBT EEB EET ETE ETB TBE TEE TEB BE BT B EB EE E ET TB TE T ε Example 37. In general, there are 5! permutations of a 5-element set. But there are only 20 distinguishable permutations of the multiset of letters in ERROR. The reason is that the three copies of R cannot be distinguished - we can permute the three Rs without giving a distinguishable permutation. This means that the 5! permutations can be divided into groups of 3! indistinguishable permutations, so that permutations from different groups are distinguishable. So the number of distinguishable permutations will be 5!/3! = 20. A complete list of the 20 distinguishable permutations of the letters in ERROR is shown below. PART D . PROOFS AND COUNTING EORRR OERRR REORR ROERR ERORR ORERR REROR RORER 53 ERROR ORRER RERRO RORRE ERRRO ORRRE RREOR RROER RRERO RRORE RRREO RRROE There is another way to count these permutations. There are C(5, 2) = 10 ways to choose the places where the O and E will be, and then the O and E can appear in either of 2 orders. After that, the remaining places will all be R, so overall there are 10 × 2 = 20 distinguishable permutations of the letters in ERROR. Example 38. How many distinguishable permutations are there of the multiset of letters in the word ALABAMA? The multiset is {4 · A, 1 · B, 1 · L, 1 · M}. Inspired by the previous example, we know that permuting the 4 copies of A will lead to 4! indistinguishable copies of each permutation. So there are 7! = 210 4! distinguishable permutations of ALABAMA. To count these via the alternate method: we need to choose 3 locations into which will place B, L, and M in some order, and then we need to place the three letters into these positions. There are C(7, 3) = 35 ways to choose the three locations, and 3! = 6 ways to place B, L, and M into the three selected positions. So, overall, there will be 35 × 6 = 210 distinguishable permutations of the letters in ALABAMA. Example 39. How many distinguishable permutations are there of the multiset of letters in the word BANANA? The multiset is M = {3 · A, 1 · B, 2 · N}. Inspired by the previous example, we know that permuting the 3 copies of A will lead to 3! indistinguishable copies of each permutation, permuting the two copies of N will lead to two indistinguishable copies, and we can also permute copies of A while also permuting copies of N. So, overall, there are 6! = 60 3! 2! distinguishable permutations of the multiset M. Again, we can also count the distinguishable permutations by choosing locations. There are C(6, 1) = 6 ways to choose where B will go. Then there are 5 remaining places, and we need to choose 2 of them to hold the Ns. There are C(5, 2) = 10 ways to do this. The remaining three places have to have the letter A. So, overall, there are 6 × 10 = 60 distinguishable permutations of the letters in BANANA. Counting Summary exactly k of element a at least k of element a at most k of element a (number of ways to place the k a’s) * (number of ways to fill other places with other elements (not a)) split into cases of exact possibilities; add number of ways for each case split into cases of exact possibilities; add number of ways for each case PART D . PROOFS AND COUNTING 54 Exercises for Section 14 Warm up 14.1. Find the exact values of these formulas: 1. C(6, 2) 2. C(5, 3) 3. P(5, 2) 4. P(7, 3). Answer available. Warm up 14.2. Draw the tree diagram that shows how to count the distinguishable 3-permutations of the word NOON. Answer available. Warm up 14.3. How many distinguishable permutations are there of the letters in the following words? 1. CAT 2. TATER 3. PHOENIX Answer available. Warm up 14.4. How many distinguishable 3-permutations are there of the letters in the word CLOCK? You should consider two cases: 1. The 3-permutation has both copies of C. 2. The 3-permutation has at most one copy of C. Answer available. Warm up 14.5. How many distinguishable 3-permutations are there of the letters in the following words? 1. CAT 2. TATER 3. PHOENIX Answer available. Exercise 14.6. How many distinguishable permutations are there of the letters in the following words? 1. MUMMERT 2. MARSHALL Exercise 14.7. How many distinguishable 3-permutations are there of the letters in the following words? 1. MUMMERT 2. MARSHALL Exercise 14.8. How many distinguishable 3-permutations are there of the letters in the word ABRACADABRA? It may help to draw a tree diagram to keep track of the cases. Culture of Computer Science 14.9. Many students, at first, find it hard to believe there are 210 distinguishable permutations of the letters in ALABAMA. There is a brute force solution: list all 210 of them. Write a program that, given a word in upper case letters, lists all the distinguishable permutations of the letters and prints out the total count. This is the way that many computer scientists would first investigate the issue of distinguishable permutations. You can also use your program to check your answers to the exercises in this section. PART D . PROOFS AND COUNTING 15 55 Probability and Search Time One application of counting problems is to estimate probabilities and running times. Definition 39. Suppose that S is a set of possibilities, known as a sample space, and we will select one possibility from S at random. If T is a subset of S, then the probability of selecting an element of T is the number |T | P(T ) = . |S| This number will always be between 0 and 1. We often express the probability as a percentage. It is the ratio of “desired” outcomes (|T |) over “possible” outcomes (|S|). Example 40. 1. Let S be the set of cards in a standard deck and let T be the set of aces. The probability of drawing an ace at random is P(A) = 4/52 ≈ 7.69%. 2. Let S be the set of hands of five cards and let F be the set of flushes. We saw before that there are 5,148 flushes. The probability that a random hand is a flush is P(F) = 5148 5148 2 = ≈ 0.00198 ≈ 0.198% ≈ C(52, 5) 2598960 1000 So, if we play many hands of poker, about 2 hands in 1000 will be flushes. Example 41. Let S be the set of distinguishable permutations of the letters in BEET, so |S| = 12. We have listed all of these permutations above. The probability that a random element of S starts with B is 3/12 = 25%. The probability that a random element of S starts with E is 6/12 = 50%. Running Time The final topic in this section is running time. Suppose that we have a large set S, and we go through the elements of S one at a time, for example with a computer program. We often view this as a search, if we are looking for one particular element, or a enumeration if we want to go through all of the elements. If the search or enumeration takes t seconds to consider all the elements of S, then the search rate is |S| items r= . t sec If we know that the search rate is r items per second, then the total time for the search will be t= |S| sec. r The same formulas work for other units of time, such as minutes, hours, days, and years. For most purposes, it is safe to assume there are 365.25 days in a year. In practice, we often estimate the rate by running a search on a small portion of the sample space. We compute the overall size of the sample space theoretically, and then use our measured rate to estimate the total running time. Example 42. There are 268 passwords that consist of 8 upper case letters. This is about 2.088×1011 passwords. Suppose that we can test 10,000 passwords per second by brute force. How long will this take to try all the passwords? We know r = 104 passwords per second, so we will have t≈ 2.088 × 1011 = 2.088 × 107 sec. 104 PART D . PROOFS AND COUNTING 56 To make the answer meaningful, we can convert it to hours and then days, as follows: t = (2.088 × 107 sec) × 1 hour ≈ 5801 hours, 3600 sec 1 day ≈ 241 days. 24 hour So, if we can search 10,000 password per second, it will take us about 241 days to search all of the 8-letter uppercase passwords. What if we want to search all 10-letter uppercase passwords instead? The search will take t = (5801 hours) × t = 2610 × 1 1 hour 1 day 1 year sec × × × ≈ 447.3 years. 10000 3600 sec 24 hour 365.25 day This is another example of combinatorial explosion: allowing two additional letters completely changes the time needed for the search. Interpreting Search Time Very often, we are not worried about the exact time of a search, only about the relative magnitude. The following table gives an informal sense of how to interpret various running times by magnitude. It is not meant to be read in an excessively formal manner... Running time < 1 second < 10 seconds < 1 minute < 10 minutes < 1 hour < 12 hours < 1 week < 1 month < 1 year < 1, 000 years > 1, 000, 000 years Ha Ha Only Serious Rule of Thumb Free unless you have to do it many times. Basically free. Almost free. Hurts productivity to run it over and over. Can get coffee while it runs. Hurts productivity to run it over and over. Can run it over lunch. Think about a better algorithm. Can run it overnight. Think more about a better algorithm. Time consuming to run over and over. Can run it if needed on a single computer, but don’t want to run it more than once. Think about a better algorithm. Strongly consider better algorithms and parallel computing. Don’t want to run it more than once. When is your deadline? A parallel supercomputer can probably bring the running time down to something reasonable. Will be expensive - really don’t want to run it more than once. Check with the boss first? Maybe a parallel supercomputer can bring the time down to a reasonable amount. Unless you absolutely have to, do something else. Unless there is a breakthrough in computing, or a completely new algorithm, even the most powerful supercomputer isn’t going to help. Do something else. There is also a useful rule of thumb if you will be making repeated searches for different elements of the same search space. Expected Time for Searches. If you repeatedly search for elements that are randomly distributed in the same search space, the average amount of time it will take to find each element is half of the time it would take to search the entire search space. PART D . PROOFS AND COUNTING 57 Counting Summary alphabet size n with no repeats length k k-word k-permutation k-subset multiset m distinct letters; total letters n = n1 + n2 + · · · + nm k-permutation any k ∈ N k≤n k≤n order matters Y Y N repetition count allowed Y nk N P(n, k) N C(n, k) k≤n Y Y, as in multiset count using cases by repeated letters; add cases PART D . PROOFS AND COUNTING 58 Exercises for Section 15 Warm up 15.1. Suppose that I want to guess a password which I know is 8 characters long and made of some combination of upper case letters, lower case letters, and digits. I can test 1 million passwords per second. How long will it take to test all possible passwords of that form? Answer available. Exercise 15.2. A latin square is a square grid of size n × n, containing the numbers 1 through n, so that each number appears once in each row and once in each column. The following is a latin square of size 4: 1 2 3 4 2 4 1 3 3 1 4 2 4 3 2 1 It is known there are 576 latin squares of size 4. 1. How many 4 × 4 grids are there so that each element of the grid is a number from 1 to 4? These grids do not need to be latin squares. 2. If I randomly choose a 4 × 4 grid whose entries are all numbers from 1 to 4, what is the probability the grid is a latin square? Exercise 15.3. A checksum is a small number derived from a longer piece of data. The idea is that, if the data is changed, the checksum will change, allowing the error to be detected. 1. Suppose that a particular checksum gives outputs that are 32 binary digits long. Thus there must be at least two 33-bit strings that have the same checksum. This is called a collision. We can make an algorithm to find a collision by exhaustively computing the checksum of every 33-bit string. If we can check one million 33-bit strings per second, how long will the algorithm require to check all the 33-bit strings? 2. A stronger checksum uses more bits for the output. What if the checksum from part (a) is replaced with one that uses 64 bits? Now we must check all 65-bit strings to find a duplicate. How long will this take, if we can check one million per second? 3. The length of the checksum in part (b) is twice the length in part (a). Why is the time in part (b) not equal to twice the time in part (a)? 4. Which will take longer to find a collision – a checksum that returns a string of 64 binary digits, or a checksum that returns a string of 12 capital letters? Culture of Computer Science 15.4. Investigate the meaning of the phrase “ha ha only serious” using the New Hacker’s Dictionary. PART D . PROOFS AND COUNTING 59 Additional Problems for Part D D.1 I have a class that has 10 first year student and 8 second year students. (a) How many ways are there to select a group containing 4 first year student and 2 second year students? (b) How many ways are there to pair each second year student with a first year student, so that no two second year students are paired with the same first year student? (There will be 2 first year student left over.) (c) How many ways are there for me to put the class into a single file line with all the second year students ahead of all the first year student? (d) How many ways are there to break the first year student into a group of 4 students and two groups of 3 students? (e) How many ways are there to break the second year students into four teams of two students each? Answer available. D.2 In the game of blackjack, face cards count as 10 points, aces count as 1 or 11 points, and other cards count as their face value. Examples of hands that add up to 21 include A♠ 10♣ and A♥ J♦ . (a) How many ways are there to have a hand of two cards that adds up to 21 points? (b) If I draw 2 random cards from a deck, what is the probability that the hand I get adds up to 21 points? Answer available. D.3 How many 5-card hands from a standard deck have exactly two aces, or have all red cards, or both? Answer available. D.4 Let Σ = {a, b, c, d}. (a) How many 3-permutations on Σ are there? (b) How many 5-words on Σ are there? (c) How many 5-words on Σ start with d? (d) How many 5-wordss on Σ contain at least one a? (e) How many 5-words on Σ start with a d and contain at least one a? (f) How many 5-words on Σ start with a d or contain at least one a? (g) How many nonempty words on Σ of length less than 4 are there? Answer available. D.5 If I draw 7 cards at random from a standard deck, what is the probability that I get two aces while not getting the queen of spades? Answer available. D.6 To landscape my yard, I will plant an oak tree and four bushes. Three of the bushes are female holly bushes that are indistinguishable from each other, and the last bush is a male holly that looks different from the female hollies. I will place all 5 plants evenly spaced in a row. How many distinguishable arrangements of the plants are there? Answer available. D.7 A standard chessboard is an 8 × 8 grid of black and white squares. A particular piece, called a rook, is said to attack any other piece on the same row or column of the board. How many PART D . PROOFS AND COUNTING 60 ways are there for me to put 8 rooks onto a chessboard so that no two of them attack each other? Answer available. D.8 How many distinguishable permutations are there for the letters in SUBBOOKKEEPER? Answer available. D.9 I begin with the word TROUBLEMAKING, which has no repeated letters, and begin to exhaustively search through all permutations of its letters. If I can examine 250,000 permutations each second, how long will the overall search take? Choose appropriate units of time for your answer. Answer available. Part E Proof by Induction We will now look at an important proof technique known as mathematical induction. This method is particularly useful when we want to prove a “for all” statement, such as “for all n, 7n − 1 is a multiple of 6”. We will see applications of induction to three scenarios: n 1. Proving number theoretic identities, such as “for all n, ∑ i = i=1 n(n + 1) .” 2 2. Proving combinatorial facts such as “if A has n elements then the powerset of A has 2n elements.” 3. Proving the correctness of algorithms, and verifying the running time. Principle of Mathematical Induction. The principle of mathematical induction says the following. Suppose we have a property P(n) which is true or false for each natural number n. Suppose we can prove both of the following: 1. The base case: P(0) is true. 2. The inductive step: For all k, if P(k) is true then P(k + 1) is true. Then we know that P(n) is true for all n. There is also a variation of the principle of induction, which allows us to start at a number larger than 0. Principle of Mathematical Induction (Variant) . A modified principle of mathematical induction says the following. Suppose we have a property P(n) which is true or false for each natural number n. Suppose we can prove both of the following: 1. The base case: P(r) is true for some r. 2. The inductive step: For all k ≥ r, if P(k) is true then P(k + 1) is true. Then we know that P(n) is true for all n greater than or equal to r. We don’t prove the principle of induction. Instead, we use it as a sort of hypothesis to prove other theorems. The justification for induction comes from our understanding of the way the natural numbers are ordered: if you begin at 0 and proceed to move along the natural numbers in order, every natural number will eventually be visited. 61 PART E . PROOF BY INDUCTION 0 62 1 3 2 4 5 6 7 8 This leads to the domino interpretation of induction: In an infinite line of dominos, if the first domino falls and we know that every domino will push over the next one, then we know every domino will eventually fall. 16 Induction for Number Theoretic Identities In this section, we will see how induction is used to prove mathematical identities. These identities often involve simplifying sums (series) or simplifying algebraic expressions. Remember that the notation ∑bi=a denotes a sum where i takes each value from a to b (inclusive). For example 5 ∑i = 0+1+2+3+4+5 i=0 and 3 1 1 1 1 ∑ 2i = 2 × 1 + 2 × 2 + 2 × 3 . i=1 Identities about series are often proved by induction. Proposition 1. For all n ∈ N, n ∑i = i=0 n(n + 1) . 2 Proof. We let P(n) say that ∑ni=0 = n(n+1) 2 . We need to prove two things: 1. Base case: P(0) is true. 2. Inductive step: For all k, if P(k) is true then P(k + 1) is true. For (1) we just check that P(0) is true. Now P(0) says 0 ∑i = i=0 0(0 + 1) . 2 Both sides of that equation are 0, so the base case holds. For (2) we assume P(k) and we need to prove we also have P(k + 1). Now P(k) says k ∑i = i=0 k(k + 1) 2 and P(k + 1) says k+1 ∑i= i=0 (k + 1)(k + 2) . 2 PART E . PROOF BY INDUCTION 63 k To prove P(k + 1), we start with the left side ∑k+1 i=0 i, which we can rewrite as ∑i=0 i + (k + 1). Now, using P(k), we can replace the sum with its value, so we have ! k+1 k k(k + 1) ∑ i = ∑ i + (k + 1) = 2 + (k + 1). i=0 i=0 Now just need to simplify the final expression: k(k + 1) k(k + 1) 2(k + 1) + (k + 1) = + 2 2 2 k2 + k + 2k + 2 k2 + 3k + 2 = = 2 2 (k + 1)(k + 2) = . 2 So, by assuming P(k), we have deduced that k+1 ∑i= i=0 (k + 1)(k + 2) . 2 This is exactly what P(k + 1) says, so the inductive case is done. Because we were able to verify the base case and the induction step, the principle of induction says that P(n) holds for all n, so n n(n + 1) ∑i = 2 i=0 for all n. Proposition 2. For all n, 7n − 1 is a multiple of 6. Proof. We let P(n) say that 7n − 1 is a multiple of 6. To apply induction, we need to verify two things: 1. Base case: 70 − 1 is a multiple of 6. 2. Inductive step: for each k, if 7k − 1 is a multiple of 6 then 7k+1 − 1 is a multiple of 6. For the base case, we can compute 70 − 1 = 1 − 1 = 0. Now 0 = 6 × 0 so 0 is a multiple of 6, which means 70 − 1 is a multiple of 6. The base case is proved. For the inductive step, assume we have some k so that 7k − 1 is a multiple of 6. Then there is some r so that 7k − 1 = 6r. Now this means 7k = 6r + 1, so 7k+1 = 7 × (6r + 1) = 42r + 7 = 42r + 6 + 1 = 6(7r + 1) + 1. So, if 7k − 1 is a multiple of 6, then 7k+1 − 1 is a multiple of 6. This shows that the inductive step holds. Because we were able to prove the base case and the inductive step, we can conclude that 7n − 1 is a multiple of 6 for every natural number n. PART E . PROOF BY INDUCTION 64 Exercises for Section 16 Warm up 16.1. Prove that, for every natural number n, ∑ni=1 c = cn, for any constant c. Answer available. Exercise 16.2. Prove that, for every natural number n, 8n − 1 is a multiple of 7. Exercise 16.3. Prove that, for every natural number n ≥ 1, 32n − 1 is a multiple of 4. Exercise 16.4. Prove that, for every natural number n, n ∑ (2i + 1) = (n + 1)2 i=0 Culture of Mathematics 16.5. George Polya proposed a famous intentionally flawed proof that “all horses have the same color”. Look up Polya’s argument. In your own words, summarize the argument and explain the flaw in its reasoning. PART E . PROOF BY INDUCTION 17 65 Induction for Combinatorial Identities The second use of induction we will see is to verify combinatorial identities. Proposition 3. If A is a set with n elements, then |P(A)| = 2n . Proof. We proceed by induction. For the base case, if A has zero elements then A is the empty set, so P(A) = P(0) / = {0}, / which has one element. Because 1 = 20 , this proves the base case. For the inductive step, assume that for every set B with k elements, |P(B)| = 2k . Let A be a set with k + 1 elements. Then A is not empty, so we can choose some element x of A. There are two kinds of subsets of A: • Subsets that do not contain x. These are subsets of A \ {x}. So there are |P(A \ {x})| subsets of A that do not contain x. Because |A \ {x}| = k, by induction we have |P(A \ {x})| = 2k . • Subsets that do contain x. If we remove x from one of these subsets, we obtain a subset of A \ {x}; if we add x back to that subset, we obtain the subset we started with. So there are the same number of subsets of A that contain x as there are subsets that don’t contain x. So there are 2k subsets of A that do not contain x, and 2k subsets that do contain x. Therefore, the total number of subsets of A is 2k + 2k = 2(2k ) = 2k+1 . This proves the induction step. Because we can prove the base case and the induction step, by mathematical induction we know that if A is a set with n elements then |P(A)| = 2n . The next theorem is called the hockey stick identity. The following two special cases are illustrated in the diagram below. 5 6 2 3 4 5 i = + + + =∑ 3 2 2 2 2 i=2 2 6 7 4 5 6 i = + + =∑ 5 4 4 4 i=4 4 1 1 1 6 1 1 7 3 10 15 21 1 6 4 5 1 2 3 1 1 1 4 10 20 35 1 1 5 15 35 1 6 21 1 7 1 PART E . PROOF BY INDUCTION 66 Proposition 4. If n and k are positive natural numbers with k ≤ n, then n i n+1 = . ∑ k+1 i=k k Proof. We prove this using induction on n. For the base case, we have n = k. Then the left side becomes k k k ∑ k = k =1 i=k and the right side becomes k+1 k+1 = 1. Because these are equal, we have proved the base case. Now, assuming that the identify holds for a specific value ofn, we want to show that it also holds for n + 1. If we put n + 1 into the right side, we obtain n+2 k+1 . Putting n + 1 into the left side and simplifying gives n+1 ∑ i=k + n+1 Now n+1 k k+1 = inductive case. n i n+1 n = +∑ k k i=k k n+1 n+1 = + . k k+1 n+2 k+1 , by Exercise 13.6. This is what we hoped to obtain. This proves the Exercises for Section 17 Warm up 17.1. We look at rectangular grids of dots that begin at point (0, 0) and end at a point (m, n) on the plane. An NE grid path is a path that starts at (0,0), and moves one unit at a time either due north or due east, ending at the top right corner of the grid (m, n). An NE grid path ending at (2, 2) Another NE grid path ending at (2, 2) An NE grid path ending at (3, 2) Another NE grid path ending at (3, 2) 1. Draw all the NE grid paths that start at (0, 0) and end at (2, 2). There will be 6 of them. 2. Draw all the NE grid paths that start at (0, 0) and end at (3, 2). There will be 10 of them. Answer available. Warm up 17.2. For the background to this problem, see Warm up 17.1. Consider a NE grid path that starts at (0, 0) and ends at (m, n). 1. How many north steps will the path have? How many east steps? How many total steps? 2. Explain why the number of paths is C(m + n, n). 3. Explain why the number of paths is C(m + n, m). 4. Using the formula, verify C(m + n, n) = C(m + n, m). Answer available. PART E . PROOF BY INDUCTION 67 Exercise 17.3. For the background to this problem, see Warm up 17.1. 1. Let f (m, n) be the number of NE grid paths that start at (0, 0) and end at (m, n). Prove that for all m, n ≥ 0, f (m + 1, n + 1) = f (m, n + 1) + f (m + 1, n). Hint: how are the grids related in each case? Draw a picture. 2. Prove by induction that, for all m, n ≥ 0, f (m, n) = C(m + n, n). Use the total steps k = m + n as your induction variable. PART E . PROOF BY INDUCTION 18 68 Induction for Algorithm Correctness In this section, we will see our third application of induction: to prove that algorithms are correct. To do this, we choose the statement P in the principle of induction to be a fact about the state of the algorithm. These facts are sometimes called invariants. Proposition 5. When it halts, the subtraction form of the Euclidean algorithm always returns the greatest common divisor of the inputs. Proof. Let m and n be the two inputs. We let P(k) be the statement If the algorithm creates row k of the table, the gcd of the numbers on row k of the table is the same as the gcd of the original two numbers m and n. The statement P(0) is true because the gcd of the numbers on the first row is the same as the gcd of the numbers on that same row. This proves the base case. Now, assume that mk and nk are the numbers on row k of the table, with mk > nk , and assume the algorithm moves to step k + 1. Then the two numbers on row k + 1 are nk and mk − nk . So we want to prove that gcd(nk , mk ) = gcd(nk , mk − nk ). To do this, we just check that any number d that divides nk and mk also divides nk and mk − nk , and vice versa. • If a number d divides nk and mk then we can write nk = ds and mk = dt for some s and t. Then mk − nk = dt − ds = d(t − s), so d also divides mk − nk . • If a number e divides nk and mk − nk then we can write nk = eu and mk − nk = ev for some u and v. Then mk = (mk − nk ) + nk = ev + eu = e(v + u). So e also divides mk . So, since mk and nk have exactly the same set of common divisors as nk and mk − nk , the greatest common divisor is the same in both cases. This proves P(k + 1) holds. Because we proved the base case and the inductive case, we know that for all s, P(s) holds. When the algorithm halts, it returns the gcd of the numbers in the final row. By the induction proof, this is the same as the gcd of the numbers in the first row. So the algorithm returns the correct answer. Exercises for Section 18 Warm up 18.1. Consider the division form of the Euclidean algorithm. Let mk and nk be the numbers on row k of the table, with mk > nk . Assume nk divides into mk with divisor dk and remainder rk . 1. What are the two numbers on row k + 1? 2. Let a divide both mk and nk . Justify a divides the remainder rk . 3. Let b divide both numbers on row k + 1. Justify b divides mk . Answer available. Exercise 18.2. Modify the proof of Proposition 5 to show that the division form of the Euclidean algorithm is correct. You will not be able to do this by just replacing a few words – reread the proof in the notes from the start, rewrite the proof from the start using the proof in the notes as inspiration. PART E . PROOF BY INDUCTION 69 Additional Problems for Part E E.1 Prove that for every natural number n ≥ 1, n 1 n ∑ j( j + 1) = n + 1 . j=1 Answer available. E.2 Prove that for every natural number n ≥ 1, n 1 1 1 ∑ i − i+1 = 1− i+1. i=1 Answer available. E.3 Prove that, for every natural number n ≥ 1, the number 11n − 2n is a multiple of 9. Answer available. xn+1 − 1 and prove that your formula is correct for every natural number n. x−1 You might want to start by simplifying the expression for n = 1, n = 2, and n = 3 to look for a pattern. Answer available. E.4 Find a formula for E.5 Prove that 2n < n! whenever n ≥ 4. Answer available. E.6 The notation ∏ni=1 f (i) indicates the product f (1) · f (2) · f (3) · · · · · f (n). This is just like ∑ni=1 f (i) except that the terms are multiplied instead of being added. For example n ∏ i = 1 · 2 · 3 · · · · · n = n! i=1 and n ∏ 2 = 2| · 2 · 2{z· · · · · 2} = 2n . i=1 n times Prove by induction that n (2n)! ∏(2i − 1) = 2n (n!) . i=1 Answer available. Part F Graph Theory 19 Families of Graphs A graph is a mathematical model of relationships – especially connections – among a collection of objects. Definition 40. A graph G consists of two sets: a set VG of vertices and a set EG of edges. Each edge has either one or two vertices as endpoints, and an edge is said to be incident to each of its endpoints. Vertices that are connected by an edge are called adjacent vertices. For this class, we will always assume the number of vertices is finite. Example 43. We can make graphs in many ways: 1. Let the vertex set consist of all students at a particular university, and place an edge between each pair of individuals who have taken at least one class together. 2. Let the vertex set consist of all actors in the Internet Movie Database, and place an edge between each pair of actors who have appeared in a movie together. 3. Let the vertex set consist of all airports in the United States, and place an edge between each pair of airports that have regular commercial flights between them. Example 44. We can draw graphs by using a dot for each vertex and a curve or line for each edge. Here are five pictures of graphs. The same graph can have more than one picture, if we draw it in a different way. Graph A Graph B Graph C Graph D Graph E • A vertex can be connected to no edges, as in graph (B). These vertices are called isolated. • A vertex can have more than one edge, as in graphs (A), (B), (C), (D), and (E). • Two vertices can be connected by more than one edge, as in graph (D). Edges of this kind are called multiple edges. • Some edges can begin and end at a single vertex, as in graph (E). Edges of this kind are called loops. An edge that is not a loop is a simple edge. 70 PART F . GRAPH THEORY 71 • A graph in which there are no multiple edges and no loops is called a simple graph. Graphs (A), (B), and (C) are simple graphs. Definition 41. The degree of a vertex is the number of simple edges connected to the node, plus two times the number of loops connected to each node. Intuitively, the degree is the number of “ends” or “connections” that touch a particular vertex, and each edge adds 2 to the total of all the degrees of vertices in the graph. We denote the degree of a vertex v by deg(v). Example 45. Each vertex below is labeled with its degree. 3 2 0 1 3 2 3 3 3 2 2 3 1 3 Graph A 1 3 Graph B 3 Graph C 3 2 2 3 Graph D Graph E Generalizations There are several generalizations of graphs. 1. In a weighted graph, each edge has a number associated with it, which is called the weight of the edge. The weight might represent the length or capacity of the edge. We will see these in a later section. 2. In a directed graph, each edge has a direction – an orientation – that tells which end of the edge is the beginning and which edge is the end. The orientation may represent the direction of flow along an edge, like a one way street. We will see these in a later section. 3. In a hypergraph, a hyperedge can connect more than two vertices simultaneously. We will not look at hypergraphs in this course. Families of Graphs There are several important families of graphs. • A path is a graph that consists of a sequence of n vertices connected one to the next by n − 1 edges. We denote a path graph of length n by Pn . P1 P2 P3 P4 • A cycle is a simple graph that consists of a single sequence of n vertices connected by n edges into a circular pattern. If we exclude loops and multiple edges, this is only possible when the number of vertices is at least 3. We denote a cycle of n vertices by Cn . C3 C4 C5 PART F . GRAPH THEORY 72 • A complete graph is a simple graph that consists of a number of vertices and every possible edge between distinct vertices. We denote a complete graph on n vertices with Kn . K1 K2 K3 K4 K5 • A complete bipartite graph has two sets of vertices, A and B, along with every possible edge between a vertex in A and vertex in B. There are no edges between vertices in A or between vertices in B. If A has m vertices and B has n vertices, the graph is denoted Km,n . K2,2 K2,3 , also K3,2 K3,3 K3,4 , also K4,3 • A graph is is bipartite if it is possible to split the vertices into two sets, A and B, so that every edge connects a vertex in A to a vertex in B, and no edge goes from A to A or B to B. Vertices of Odd Degree The definition of a graph seems very general, but there are many combinatorial facts about graphs that are not trivial or obvious. The next theorem is one useful fact about all graphs. Theorem 9. If G is a graph, the number of vertices that have odd degree is even. Proof. Let G be a graph and let e be the number of edges in G. Let AG be the set of vertices that have odd degree, and let BG be the set of vertices with even degree. If we add up the degrees of all the vertices in the graph, we will get ∑ deg(v) = 2e v∈VG because every every edge adds 2 to the total of all the degrees. So ∑ deg(v) + ∑ deg(v) = 2e. v∈BG v∈AG Every vertex in BG has even degree, so the sum of the degrees of vertices in BG is even. Let ∑v∈BG deg(v) = 2m. Then ! ∑ deg(v) + 2m = 2e, v∈AG so ∑ deg(v) = 2e − 2m = 2(e − m). v∈AG This shows that ∑v∈AG deg(v) is even. Now, the sum of an even number of odd numbers is even, and the sum of an odd number of odd numbers is odd. Because ∑v∈AG deg(v) is a sum of odd numbers, and the sum itself is even, there must be an even number of odd numbers being added. So |AG | is even, which is what the theorem claims. PART F . GRAPH THEORY 73 Exercises for Section 19 Warm up 19.1. Draw K6 and K4,4 . For each, how many edges does the graph have? What are the degree(s) of the vertices? Answer available. Warm up 19.2. If G = (VG , EG ) is a graph, with |Vg | = v and |EG | = e, the incidence matrix of the graph is an v × e matrix with one row for each vertex and one column for each edge. There is a 1 in position (i, j) if vertex i is incident to edge j, and a 0 at position (i, j) otherwise. Write the incidence matrix for C3 and for P3 . Answer available. Warm up 19.3. If G = (VG , EG ) is a graph, with |Vg | = v and |EG | = e, the adjacency matrix of the graph is an v × v matrix with one row for each vertex and one column for each vertex. There is a 1 in position (i, j) if there is an edge between vertex i and vertex j. Write the adjacency matrix for C3 and for P3 . Answer available. Exercise 19.4. If G = (VG , EG ) is a graph, with |Vg | = v and |EG | = e, the incidence matrix of the graph is an v × e matrix with one row for each vertex and one column for each edge There is a 1 in position (i, j) if vertex i is incident to edge j, and a 0 at position (i, j) otherwise. Write the incidence matrix for K5 and for K2,2 . Exercise 19.5. If G = (VG , EG ) is a graph, with |Vg | = v and |EG | = e, the adjacency matrix of the graph is an v × v matrix with one row for each vertex and one column for each vertex. There is a 1 in position (i, j) if there is an edge between vertex i and vertex j. Write the adjacency matrix for K5 and K2,2 . Exercise 19.6. 1. For n ≥ 1, how many edges does Pn have? 2. For n ≥ 3, how many edges does Cn have? 3. For n ≥ 1, how many edges does Kn have? 4. For m, n ≥ 1, how many edges does Km,n have? 5. If a simple graph has n vertices, what is the maximum number of edges it can have? Exercise 19.7. 1. For n ≥ 1, what are the degree(s) of the vertices of Pn ? 2. For n ≥ 3, what are the degree(s) of the vertices of Cn ? 3. For n ≥ 1, what are the degree(s) of the vertices of Kn ? 4. For m, n ≥ 1, what are the degree(s) of the vertices of Km,n ? 5. If a simple graph has n vertices, what is the maximum degree that a vertex can have? PART F . GRAPH THEORY 20 74 Eulerian Graphs and Hamiltonian Graphs Definition 42. A walk in a graph is a sequence v1 , e1 , v2 , e2 , . . . , vn where each vi is a vertex and each ei is an edge between vi and vi+1 . The length of a walk is the number of edges in the walk. We will be interested in several specific kinds of walks: • A trail in a graph is a walk in which no edge appears more than once. • A tour (closed trail) in a graph is a trail that ends at the same vertex where it begins. • A path in a graph is a walk in which no vertex appears more than once. • A cycle (closed path) is a walk in which no vertex appears more than once except that the first vertex is the same as the last vertex. Example 46. The following graph has 4 vertices and 5 edges. β B D ε α γ A C δ One trail is A, α, B, β , D, ε, A, δ ,C, γ, D. This trail is not a path because it repeats the vertex A. Note that, once we know the starting vertex A, we don’t need to specify the remaining vertices, so we could abbreviate the tour to A, α, β , ε, δ , γ. This trail is not a tour because it does not end at the same vertex where it begins. One cycle in the graph is is A, α, B, β , D, ε, A. If we don’t need to know the specific edges used, we could abbreviate the cycle as A, B, D, A. We will be interested in various kinds of walks. We are often particularly interested in graphs where it is possible to walk from any vertex to any other vertex. Definition 43. A graph is connected if, for every pair of distinct vertices v and w, there is a walk from v to w. Eulerian Tours Definition 44. An Eulerian tour is a tour in a graph that uses every edge exactly once, and ends at the same vertex where it begins. An Eulerian trail is a trail in a graph that uses every edge exactly once, but may end at a different vertex than the first one. Eulerian tours are named after Leonhard Euler (1707–1783),1 who was motivated by a problem of making a walking tour of the city of Königsburg (now Kaliningrad, Russia). The city had 7 bridges, and the goal was to find a way to make a tour of the city that crossed each bridge exactly once. The figure below shows a map of the seven bridges, and the idealized graph in which each bridge is an edge and each land mass is a vertex. 1 The name Euler rhymes with “boiler”. PART F . GRAPH THEORY A map of Königsburg with the bridges highlighted.2 75 The idealized graph of the problem. Each bridge is an edge, and each land mass is a vertex. Theorem 10. A graph has an Eulerian tour if and only if it is connected and every vertex has even degree. Proof. If the graph has loops, we can handle them by just following each loop when we arrive at the vertex that is connected to the loop. Therefore, it is safe to assume that the graph has no loops, and each edge connects two distinct vertices. So assume G is a loopless graph in which every vertex has even degree. Pick a vertex v1 and make a trail from v1 by blindly following edges. That is, choose an edge from v1 , mark the edge as used, and follow the edge to v2 . If there is an unused edge incident to v2 , mark it as used and follow it to a vertex v3 . If there is an unused edge incident to v3 , mark it as used and follow it to a vertex v4 . Continue doing this until you arrive at a vertex where every edge has been used. We claim that vertex must be v1 . The reason is that, when we arrive at a vertex via an edge, marking that edge used reduces the degree by 1. Because the vertex had even degree, the remaining degree of v2 before we arrived at v2 had to be at least 2. Therefore, even after marking the edge as used, there must be another edge that we can follow out of v2 . The only place we can become stuck is at v1 , because we took 1 from the degree of v1 with the initial edge, and so we could use up the last end connected to v1 when we return later. At this point, we have constructed a tour that begins and ends at v1 , but the tour may not use every edge. As long as there are still unused edges, we will “augment” our tour to make it longer. First, we claim that some vertex along the existing tour must have an unused edge. If not, then we have a situation where: • Some vertex w has an unused edge. • Every edge from every vertex in our tour is used. This means there is no path in the original graph from v1 to w, which is a contradiction. So, if any vertex has unused edges, some vertex in our path has unused edges. Call that vertex w. Repeat the original construction to make a tour that begins and ends at w. Then, take this new tour and splice it into the existing tour as follows: when the existing tour reaches w, take a “detour” and follow the new tour until it arrives back at w. Then resume the original tour to return to v1 . 2 Source: https://en.wikipedia.org/wiki/File:Konigsberg bridges.png PART F . GRAPH THEORY 76 As long as there are unused edges, we can continue augmenting our original tour in this way. So, eventually, we will run out of unused edges. At that point, we must have a tour that uses every edge in the graph. This will be the Eulerian tour that we wanted. Example 47. Suppose we begin with the following graph. Every vertex has even degree, so we are certain we can make an Eulerian tour. B C H A D E G F I We make an initial tour starting at A and following edges. Our first attempt might make a tour that visits A, B, C, and D before returning to A. B C H A D E G F I Our original tour reads A, B,C, D, A. We notice that, in our tour, vertex D has unused edges. We make a tour starting at D that visits E, F, and G. B C H A D E G F I We use this to augment our original tour from vertex A, by splicing in at vertex D, to obtain A, B,C, D, E, F, G, D, A. Now, we notice that E in our tour still has unused edges, so we make a tour starting at E that visits H and I: B C H A D E G F I Finally, we augment the tour starting at A to include the newly created tour: A, B,C, D, E, H, I, E, F, G, D, A. At this point, every edge is used, so we have an Eulerian tour beginning at vertex A. PART F . GRAPH THEORY 77 Theorem 11. A connected graph has an Eulerian trail if and only if there are no vertices of odd degree or two vertices of odd degree. Proof. Let G be a graph. If there are no vertices of odd degree, then there is an Eulerian tour by the previous theorem, which is also an Eulerian trail. If there are two distinct vertices v and w of odd degree, we begin by modifying the graph by adding a new “temporary” edge from v to w. In the resulting graph, every vertex has even degree. Therefore, we can construct an Eulerian tour of the new graph, as in the previous theorem, ensuring that the first edge taken is from v to w. If we then delete the temporary edge from the tour, we will have an Eulerian trail of our graph G starting at vertex w and ending at vertex v. If there are three or more vertices of odd degree, we will prove there cannot be an Eulerian trail. Because the trail has only two endpoints, one of the three vertices of odd degree must only appear in the middle portion of the trail. But every vertex in the middle portion is exited the same number of times it is entered, and thus will have even degree because all of its edges are used. This contradicts the assumption that there is a vertex appearing only in the middle portion that has odd degree. Corollary 1. There is no Eulerian tour for the graph corresponding to Königsburg, and there is also no Eulerian trail. Proof. There are more than two vertices of odd degree in the Königsburg graph. Hamiltonian Cycles The definition of an Eulerian graph was in terms of edges. We can ask the same question about vertices: given a graph, can we make a walk that visits each vertex exactly once, and ends at the same vertex where it begins? Definition 45. A graph is Hamiltonian if there is a cycle in the graph that visits every vertex exactly once. Unlike the Eulerian property, there is no simple characterization of Hamiltonian graphs. In many cases, we must check each graph individually to tell whether it is Hamiltonian. Example 48 (Herschel graph). The following graph was discussed by Alexander Stewart Herschel in 1862.3 This graph has no Hamiltonian cycle, but this is not obvious on initial inspection. The graph also happens to be bipartite, as the red and blue coloring shows. 3 Herschel, A. S. (1862), ”Sir Wm. Hamilton’s Icosian Game”, The Quarterly Journal of Pure and Applied Mathemat- ics, 5: 305. PART F . GRAPH THEORY 78 Exercises for Section 20 Warm up 20.1. Find an Eulerian tour for K4 and one for K2,4 , if possible. If it’s not possible, explain why not. Answer available. Warm up 20.2. Decide if K2,3 is Hamiltonian. If so, give (or sketch) the cycle. If not, explain why not. Answer available. Exercise 20.3. Find an Eulerian tour of the following graph beginning at vertex A. P Q C F I B E H A D G N M Exercise 20.4. For which n does the graph Kn have an Eulerian tour? For which values of m and n does Km,n have an Eulerian tour? Exercise 20.5. Prove the following. 1. A bipartite graph with an odd number of vertices is not Hamiltonian. 2. The Herschel graph is not Hamiltonian. Culture of Mathematics 20.6. Leonhard Euler is often viewed as one of the greatest mathematicians of all time. Why? Research the contributions that Euler made to mathematics during his lifetime. Culture of Mathematics 20.7. It might bother you to not know whether the graph in Exercise 20.3 is Hamiltonian. Find a way to answer the question with a mathematical proof. Culture of Computer Science 20.8. It might bother you to not know whether the graph in Exercise 20.3 is Hamiltonian. Write a computer program that searches for a Hamiltonian path, and use your program to answer the question. PART F . GRAPH THEORY 21 79 Trees and Traversals Definition 46. A tree is a connected simple graph in which there are no cycles. Example 49. Each of the following four connected graphs is a tree. Trees are an important family of graphs because they have a kind of balance: they have just enough edges to be connected, without having enough edges to have any cycles. This property of trees is stated precisely by the next theorem. Fundamental Theorem of Trees. Let G be a simple graph with n vertices. The following are equivalent: 1. G is a tree: a connected simple graph with no cycles. 2. G is connected and has n − 1 edges. 3. G has no cycles, and if any new edge is added to G the resulting graph has a cycle. 4. G is connected, and if any edge is removed from G then G is disconnected. This theorem shows that tree are balanced in terms of edges: a tree has just enough edges to be connected without having cycles. If we add an edge, the graph will now have a cycle, while if remove an edge the graph will be disconnected. Moreover, as long as the graph is connected, we can tell if it is a tree simply by counting the number of edges. Partial proof. We will prove that (1) and (4) are equivalent. You will prove that (1) and (3) are equivalent in Exercise 21.4, and we will prove that (1) and (2) are equivalent in the next section. First, assume G is a tree. Assume, for a contradiction, that (4) fails: there is some edge e that we can remove, so that G without e will still be connected. Let u and v be the vertices incident to e. Because G is a simple graph, u and v are different vertices. Because G without e is connected, there is a walk in G without e that goes from u to v. By Additional Problem 2 for part F, we can assume the walk never uses any edge more than once. If we then put e at the end of the walk we will have a cycle from u to u in G. This is impossible, because we assumed G is a tree. So it is impossible for (4) to be false. Therefore, (4) is true for every tree. Now, assume that G is a simple graph that satisfies (4). Assume G is not a tree. Then G must have a cycle, by the definition of a tree. Let C be a cycle in G and let e be an edge in C. If we remove e from G, the resulting graph is still connected, by Additional Problem 3 for Part F. We have a contradiction, because we assumed (4) was true, but we were able to remove an edge without making the graph disconnected. So the assumption that G was not a tree must be wrong, which means that G must be a tree. We have proved that any simple graph that satisfies (4) must be a tree. PART F . GRAPH THEORY 80 Breadth-first traversal One common task when we have a tree, or even just a graph, is to search through all the vertices. Algorithms to do this are called traversals. We will look at two traversals that can be applied to any graph: breadth-first traversals and depth-first traversals. We can give an informal way to think of the difference between these two types of traversals. Imagine that the graph is a maze, and we want to search for a particular vertex that contains the exit. We are currently located at a particular starting vertex. • A breadth-first search corresponds to checking all the locations 1 unit away from our starting position, then checking all the locations 2 units away, and so forth. We never check a location 4 units away until we have checked every location that is 3 or less units away. • A depth-first search corresponds to following a path until we reach a dead end. We then back up to the first branch in another direction, and go that way until we reach a dead end. In this way, we alternate between walking to find a dead end and backtracking to find a different path. The formal algorithm tells how to do those procedures in a way that has no ambiguity. We first look at the breadth-first version. Algorithm 4. Breadth First Traversal Input: A finite simple connected graph G and a starting vertex v. Output: A list of the vertices in G, in a particular order General rules: At each step, we will have a working list of vertices. Also, at each step, certain vertices are marked as seen. Once a vertex is marked, it stays marked for the rest of the algorithm. We assume that the vertices are labeled with letters or words, and when we have a choice we will consider vertices in the order of those letters or words. Steps: 1. Begin by putting the starting vertex v as the only element of the working list, and mark vertex v as seen. 2. While there are still vertices in the working list: (a) Call the leftmost vertex on the working list w. (b) Remove w from the working list and print out w. (c) For each vertex adjacent to w, if that vertex is not marked as seen, add it to the right end of the working list, and mark it as seen. Example 50. We begin with the graph below. A C E B D F PART F . GRAPH THEORY 81 We will do a breadth-first traversal starting at vertex (A). When a vertex is marked as seen, we shade that vertex in the graph. Step Output List 1 2 3 4 5 6 7 Working List A A A, B A, B, C A, B, C, D A, B, C, D, E A, B, C, D, E, F B, C C, D, E D, E E F Graph A A A A A A A C E B D C E B D C E B D C E B D C E B D C E B D C E B D F F F F F F F Steps in the breadth-first traversal of a graph. The vertex we consider at each step is marked in red. Vertices that are marked as seen are shaded. The algorithm halts at stage 7 because the working list is empty. PART F . GRAPH THEORY 82 Depth-first traversal The algorithm for a depth-first traversal is similar – sometimes confusingly so. There are two main differences: 1. We place vertices on the left end of the working list, rather than the right side. 2. We don’t mark a vertex as seen until we take it off the working list. Algorithm 5. Depth First Traversal Input: A finite simple connected graph G and a starting vertex v. Output: A list of the vertices in G, in a particular order General rules: At each step, we will have a working list of vertices. Also, at each step, certain vertices are marked as seen. Once a vertex is marked, it stays marked for the rest of the algorithm. We assume that the vertices are labeled with letters or words, and when we have a choice we will consider vertices in the order of those letters or words. Steps: 1. We begin by putting the starting vertex v as the only element of the working list. 2. While there are still vertices in the working list: (a) Call the leftmost vertex on the working list w. (b) Remove w from the working list. If it is marked as seen, then discard it and return to step 2. (c) Label w as seen and print out w. (d) For each vertex adjacent to w, if that vertex is not seen, add that vertex to the left end of the working list. Example 51. We begin with the graph below. A C E B D F We will do a depth-first traversal starting at vertex (A). When a vertex is marked as seen, we shade that vertex in the graph. PART F . GRAPH THEORY Step 83 Output List 1 2 3 4 5 6 7 8 9 10 Working List A A A, B A, B, C A, B, C, E A, B, C, E, D A, B, C, E, D, F A, B, C, E, D, F A, B, C, E, D, F A, B, C, E, D, F B, C C, D, E, C E, D, E, C D, F, D, E, C F, D, E, C D, E, C E, C C Graph A A A A A A A A A A C E B D C E B D C E B D C E B D C E B D C E B D C E B D C E B D C E B D C E B D F F F F F F F F F F Steps in the depth-first traversal of a graph. The vertex we consider at each step is marked in red. Vertices that are marked as seen are shaded. The algorithm halts at stage 10 because the working list is empty. PART F . GRAPH THEORY 84 Exercises for Section 21 Warm up 21.1. Perform a breadth-first traversal and a depth-first traversal of the following graph, starting at vertex A. A B C D E G H F Answer available. Warm up 21.2. Perform a breadth-first traversal and a depth-first traversal of the following graph, starting at vertex A. A B C D E F G Answer available. Exercise 21.3. Perform a breadth-first traversal and a depth-first traversal of the following graph, starting at vertex E. G H I D E F A B C Exercise 21.4. Prove that (1) and (3) are equivalent in the Fundamental Theorem of Trees: a simple graph is a tree if and only if the graph has no cycles but adding any new edge to the graph results in a graph with a cycle. PART F . GRAPH THEORY 22 85 Spanning Trees In this section, we will look at weighted graphs. Each edge will have a nonnegative real number as its weight. Definition 47. If G is a connected simple graph or connected weighted simple graph, a spanning tree of G is a set S of edges from G so that the graph (VG , S) is a tree. In other words, if we throw out all the edges except those in S, we will have a tree with the same vertices as G. The following diagram shows a weighted graph and three spanning trees. 3 D 4 3 D 4 E 5 4 E C 4 2 A 3 B Weighted graph 5 4 C 4 E 2 A 3 Spanning tree Weight 14 3 D 4 3 D 4 B 5 4 E C 4 2 A 3 Spanning tree Weight 15 B 5 4 C 4 2 A 3 B Spanning tree Weight 12 Definition 48. The weight of a spanning tree on a weighted connected simple graph is the sum of the weights of the edges in the spanning tree. If the graph is not weighted, we give weight 1 to each edge, so the weight of a spanning tree in an unweighed graph is the number of edges in the spanning tree. Definition 49. A minimal spanning tree of a weighted simple connected graph G is a spanning tree S so that the weight of S is less than or equal to the weight of every other spanning tree of G. Example 52. Let VG be a set of cities within driving distance of each other, and let G be the complete graph on VG . Let the weight of each edge be the cost to build a road between the two cities that the edge connects. Then a spanning tree of G is a set of roads that allows someone to drive from any city in VG to any other city in VG (perhaps not directly). A minimal spanning tree will minimize the cost of building the entire road network. Prim’s algorithm to find a spanning tree begins with a particular city (perhaps the capital). At each step, it enlarges the road network of cities that can drive to the capital, by adding the city that is cheapest to connect to the existing road network. Prim’s algorithm is named for Robert Prim (1921–), who described it in 1957. The algorithm was first described by Vojtěch Jarnı́k (1897– 1970) in 1930, however, and was also rediscovered by Edsger Dijksta in 1959. Algorithm 6 (Prim’s algorithm). Inputs: A simple connected weighted graph G and a starting vertex v. Outputs: A minimal spanning tree of G. General rules: At each step, we will have a tree T which spans part of G. We will decide which vertex from G − T to adjoin to T at each step. Steps: 1. Let the initial tree be T0 = {v}, containing just the starting vertex. 2. As long as VG \VTi is not empty: (a) Look at every edge that goes from a vertex in VT to a vertex in VG \VT . Choose an edge ei of minimal weight from that collection of edges. (b) Let Ti+1 be Ti together with the vertex in VG \VTi that is incident to ei . PART F . GRAPH THEORY 86 Example 53. We will apply Prim’s algorithm to the following weighted graph, with initial vertex A. A 3 4 D 2 5 7 5 C Tree A 5 Spanning Tree 3 4 B 2 5 C A E 6 3 B D 7 E 6 T0 4 D 2 5 7 5 C A E 6 T1 3 B 4 D 2 5 7 5 C A E 6 T2 3 B 4 D 2 5 7 5 C A E 6 T3 3 B 4 D 2 5 7 5 C T4 B 6 E Steps in the Prim’s algorithm. The spanning tree at each step is shown in blue. The algorithm halts with T4 because T4 contains every vertex of G. The weight of the spanning tree is 3 + 4 + 2 + 6 = 15. Notice that the spanning tree does not just use the cheapest edges: two edges of weight 5 are not used, but an edge of weight 6 is used. PART F . GRAPH THEORY 87 Theorem 12. If G is a connected simple graph with n vertices, Prim’s algorithm will halt in with a spanning tree for G. Proof. At each stage of execution, Prim’s algorithm adds one additional vertex and one additional edge to the tree it is forming. Let Ti be the graph that the algorithm produces after i iterations of Step 2, assuming that Step 2 runs i times. We prove the following by induction on i, for i ≥ 1: if the graph has at least i + 1 vertices then step i will run and the graph Ti is a tree with i + 1 vertices. For the base case, we can check that after step 1, if the graph has at least 2 vertices, the algorithm has produced a tree with exactly 2 vertices. Now assume the induction holds up to step k. At step k + 1, as long as there is at least one more vertex to add, the algorithm selects a vertex not already in the tree, and an edge from the tree to that vertex, and adds the new vertex with the selected edge. This new graph is connected if the previous one was connected, and must also be a tree, because adding a single edge to a new vertex cannot produce a cycle. This proves that the induction hypothesis continues to hold after step k + 1. Because one vertex is selected at the start of the algorithm, it will take exactly n − 1 iterations of Step 2 to add the remaining n − 1 vertices. By induction, after iteration n − 1 the algorithm will have made a tree Tn−1 with n vertices, which means that the algorithm must have made a spanning tree for the original graph G. The following result is harder to prove, but the proof can also be obtained by induction. The challenge is to find a suitable induction hypothesis. Theorem 13. If G is a weighted connected simple graph, Prim’s algorithm will halt with a minimal spanning tree for G. We will use Prim’s algorithm to verify the final part of the Fundamental Theorem of Trees. Theorem 14. A simple connected graph with n vertices is a tree if and only if it has n − 1 edges. Proof. First, assume G is a tree with n vertices. Running Prim’s algorithm on G will yield G itself, including all the original edges, because removing any edge from G will cause G to become disconnected. But Prim’s algorithm will add exactly n − 1 edges to give a spanning tree with n vertices. So G itself had n − 1 edges. Conversely, suppose that G is a simple connected graph with n vertices and n−1 edges. Because G is connected, Prim’s algorithm will create a spanning tree of G. To create a spanning tree of G, Prim’s algorithm must use n − 1 edges to include all n vertices. Because G only has n − 1 edges, this means that every edge is used, so the spanning tree created is exactly G. Hence G is a tree. PART F . GRAPH THEORY 88 Exercises for Section 22 Warm up 22.1. Draw every spanning tree of each of the following graphs. A A C B C B D Answer available. Exercise 22.2. Draw every spanning tree of the following graph. D C A B Exercise 22.3. Apply Prim’s algorithm to the graph below, starting at vertex (A). Show the tree obtained at each step. If you have more than one choice at a given step, choose the vertex that comes first in the alphabet. 2 F A 3 B 2 C 3 E I 4 H 2 3 2 1 3 1 D 2 G 2 Challenge 22.4. Suppose that G is a simple weighted graph in which no two different edges have the same weight as each other. Prove that there is only one minimal spanning tree of G. (Note: it is not enough to show that Prim’sm algorithm only makes one tree – you have to prove that any minimal spanning tree will be the same as the one that Prim’s algorithm creates. Culture of Computer Science 22.5. Investigate the Spanning Tree Protocol (STP), an algorithm for Ethernet networks. Why are the designers interested in a spanning tree? What issues does the algorithm solve? PART F . GRAPH THEORY 23 89 Shortest Paths In this section, we will look at weighted graphs. Each edge will have a nonnegative real number as its weight. Definition 50. If G is a connected simple weighted graph, the cost (also distance) of a path is the sum of the weights of the edges in the path. A shortest path (also cheapest path) from a vertex v to a vertex u is a path that has the minimum possible cost, out of all the possible paths from u to v. The following diagram shows three paths from vertex A to vertex D in the same weighted graph. The cheapest path is A–B–E–D. The path A–B–D has fewer edges, but we think of the weight of each edge as representing its length, so a path with more edges can sometimes be shorter than a path with fewer edges. If the graph had no weights, we often take the weight of each edge to be 1, in which case the cost of a path is the number of edges. A 3 B C D 3 2 7 6 8 A 2 E 3 B C Cost 8 D 3 2 7 6 8 Cost 9 2 E A 3 B C D 3 2 7 6 8 2 E Cost 15 The following algorithm for finding the shortest path between vertices was first described in 1956 by Edsger W. Dijkstra (1930–2002), a pioneering computer scientist.4 Algorithm 7 (Dijkstra’s shortest path algorithm). Inputs: A weighted simple graph G and a vertex v. Outputs: a list showing the the cost of the shortest path from v to each other vertex. Instructions: 1. The algorithm will proceed in stages. Before beginning, each vertex is assigned a number, which is an upper bound on the distance from v to that vertex. We will call this number the tenatitve distance. It may decrease as the algorithm runs. To initialize the algorithm, the vertex v is assigned tentative distance 0 and all other vertices are assigned tentative distance ∞. 2. As we go, we will mark vertices as visited once we have examined them. To initialize the algorithm, all vertices are marked as unvisited. 3. While there is an unvisited vertex that is marked with a non-infinite tentative distance: (a) Choose an unvisited vertex u that has minimum tentative distance. (b) For each vertex w adjacent to u, determine if the path to w through u gives a shorter distance than the current tentative distance of w. For example, if u has tentative distance 5 and the edge from u to w has weight 2, and w has tentative distance 8, replace the 8 with 7. But, if w has tentative distance less than 7, we do not replace that with 7. (c) Mark u as visited. 4. When the loop from step 3 ends, the final tentative distance of each vertex is the actual distance from v to that vertex. 4 The first syllable of Dijkstra sounds like “dike”. PART F . GRAPH THEORY 90 The following example shows the execution of Dijkstra’s algorithm on a particular graph, with starting vertex A. Vertices that are marked visited are shaded in grey. A: 0 7 3 B: ∞ 3 2 C: ∞ 6 8 D: ∞ 2 7 E: ∞ C: 5 After initialization. The starting vertex A has tentative distance 0 and all other vertices have tentative distance ∞. A: 0 7 3 B: 3 3 2 C: 7 6 8 7 3 B: 3 3 2 C: 5 6 A: 0 2 7 E: ∞ C: 5 8 D: 9 A: 0 7 C: 5 3 2 B: 3 6 3 A: 0 7 E: 6 C: 5 D: 9 2 E: 6 8 Step 3. Visit vertex C. Update tentative distance for E. Because 6 < 5 + 8, the tentative distance for E does not change. B: 3 6 3 2 D: 8 2 E: 6 8 3 B: 3 6 3 2 D: 8 2 E: 6 8 Step 5. Visit vertex D. Because all other nodes have been visited, nothing is updated. 2 Step 2. Visit vertex B. Update tentative distances for C, D, and E. Because 3 + 2 < 7, the tentative distance from A to C is reduced from 7 to 5. 3 Step 4. Visit vertex E. Because 2 + 6 < 9, the tentative distance for D is updated to 8. D: ∞ Step 1. Visit vertex A. Update tentative distances for B and C. A: 0 A: 0 3 B: 3 6 3 2 8 D: 8 2 E: 6 Step 6. All nodes have been visited. The tentative distance for each vertex is the shortest distance of a path from A to that vertex. PART F . GRAPH THEORY 91 Example 54. Prim’s algorithm and Dijkstra’s algorithm are not interchangeable. This is because a spanning tree has to take into account all the edges in the spanning tree, while the shortest path between two vertices only has to look at the edges in the path. For example, in the following weighted graph, the shortest path from A to D has weight 7, while the least cost of a spanning tree is 8. If we try to make a spanning tree that includes edge AD, there is no way to make our spanning tree have a weight of 8. The moral is that minimum spanning trees do not generally give shortest paths in the original graph, and the shortest path between particular vertices may not be included in any minimal spanning tree. D 1 C 7 A D 1 C 6 1 B The cheapest path from A to D has cost 7. 6 A 1 B The minimum spanning tree has total weight 8. PART F . GRAPH THEORY 92 Exercises for Section 23 Warm up 23.1. Apply Dijkstra’s algorithm to the graph below, starting at vertex (D). Show each step. If you have more than one choice at a given step, choose the vertex that comes first in the alphabet. 1 D C 3 7 A 6 B 1 Answer available. Exercise 23.2. Apply Dijkstra’s algorithm to the graph below, starting at vertex (A). Show each step. If you have more than one choice at a given step, choose the vertex that comes first in the alphabet. 1 D C 3 7 A 1 6 B Exercise 23.3. Apply Dijkstra’s algorithm to the graph below, starting at vertex (A). Show each step. If you have more than one choice at a given step, choose the vertex that comes first in the alphabet. 2 F 1 A 3 B 3 C E I 4 H 2 3 2 3 1 D 2 G 2 Challenge 23.4. Prove that, in Dijkstra’s algorithm, the tentative distance of a vertex when the node is marked as visited is the actual shortest distance from the starting vertex. Hint: prove by induction on n that the tentative distance of the nth node that is visited is correct at the step when that node is visited. The induction hypothesis will be complex. Culture of Computer Science 23.5. Investigate the Open Shortest Path First (OSPF) algorithm, part of the Internet Protocol (IP) used for routing on the internet. How does OSPF apply Dijkstra’s algorithm? PART F . GRAPH THEORY 93 Additional Problems for Part F F.1 Justify your answer to each of the following: (a) For which n does Kn have a Hamiltonian cycle? (b) For which m, n does Km,n have a Hamiltonian cycle? F.2 Prove that if G is a graph and there is a walk from a vertex u to a vertex v, then there is a walk from u to v that does not use any vertex more than once. Answer available. F.3 Prove that if G is a connected graph, C is a cycle in G, and e is any edge in C, then the graph obtained by removing e from G is still connected. Answer available. F.4 An alkane is a kind of molecule made from carbon and hydrogen atoms. The molecule is arranged in a tree, with each hydrogen atom linked to one other atom and each carbon atom linked to 4 other atoms. The names of alkenes are based on how many carbon atoms are present. H H C H H H Methane (CH4 ) H H C C H H H H Ethane (C2 H6 ) H H H C C C H H H H Propane (C3 H8 ) Once there are 4 or more carbon atoms, it is possible to have different molecular structures with the same atomic formula. For example, butane (C4 H10 ) has two of these isomers. Because each alkane has a tree structure, we can use graph theory to study the number of isomers.5 H H H H H C C C C H H H H H H H H H C C C H H H H C H H Butane (C4 H10 ) Isobutane (C4 H10 ) (a) Make a complete list showing the diagrams for all isomers of pentane (C5 H12 ) and hexane (C6 H14 ). (b) Count the edges (or links) two ways: use the chemical structure and use the tree structure. Use these two counts to prove that, in any alkane with c carbon atoms and h hydrogen atoms, the equation h = 2c + 2 holds. We have noticed this pattern already in the alkanes above. Answer available. F.5 (Challenge) Prove that if G is a simple graph in which every vertex has degree at least 2 then G contains a cycle. Answer available. 5 An early publication in this area was by graph theorist Arthur Cayley, “On the mathematical theory of isomers,” The London, Edinburgh, and Dublin Philosophical Magazine and Journal of Science, v. 47, n. 314, pp. 444–447, 1874. DOI: 10.1080/14786447408641058 Part G Recurrence Relations 24 Intro to Recurrence Relations A recurrence relation is a formula that describes the value of a function on a particular input in terms of the values on other inputs. 1. Let g(n) be the factorial function n!, defined for n ≥ 1. We can describe g by the following recurrence relation: g(n + 1) = (n + 1) · g(n), g(1) = 1. 2. A famous parable goes as follows: a visitor, playing chess with a king, makes a wager: if the visitor can win a game of chess against the king, then the king will pay by putting one grain of rice on the first square of the chessboard, 2 grains on the second square, 4 grains on the third square, and so forth, until all 64 squares have rice. Each square will get double the amount on the previous square. The king agrees, and loses the game. When the king begins to pay the prize, the total amount is quickly realized. At that point, the visitor is chased out of town and told never to return! If we let A(n) be the amount on the first n square, we obtain the recurrence A(n + 1) = 2A(n), A(1) = 1. The first few values of the recurrence are shown in the following table. n 1 2 3 4 5 6 7 A(n) 1 2 4 8 16 32 64 3. Fibonnaci considered the following situation: a population of rabbits will be introduced to an environment that previously had no rabbits. The rabbits will always be in pairs of one male and one female. Newborn rabbits do not produce baby rabbits in their first month, but each month after that they produce one new pair of baby rabbits. If one pair of newborn rabbits is introduced, then the number of rabbits on the island after n + 2 months will be the number that were present after n + 1 months, plus the number of new pairs, which is equal to the number of pairs that were present after n months. Month 1 2 3 4 5 6 7 Pairs 1 1 2 3 5 8 13 94 PART G . RECURRENCE RELATIONS 95 If we let F(n) be the number of pairs that are present after n months, we have the following recurrence relation: F(n + 2) = F(n + 1) + F(n), F(1) = 1, F(2) = 1. 4. An algorithm for sorting behaves as follows. Assume that the set of items to be sorted has size 2n+1 . First, the algorithm divides the items into two sets of size 2n and sorts each of those sets. Then, it merges the two sets together. Assuming that the set to be sorted has size m = 2n+1 , the running time R(m) is given by R(m + 1) = 2R(m) + m, R(1) = 1. Definition 51. A first order linear recurrence with constant coefficients is of the form A(n + 1) = r · A(n) + f (n) A(n0 ) = C0 for some constants r and C0 and a function f (n). The recurrence begins at n = n0 and proceeds to larger n. The constant C0 is called the initial condition. The function f (n) is called the forcing term. If f (n) = 0 we have a homogeneous first order recurrence with constant coefficients, which is of the form A(n + 1) = r · A(n) A(0) = C0 . A second order linear recurrence with constant coefficients is of the form B(n + 2) = r1 B(n + 1) + r2 B(n) + f (n), B(n0 ) = C0 , B(n0 + 1) = C1 . Again, the recurrence begins at n = n0 , C0 and C1 are the initial conditions, and f (n) is the forcing term. Here is a summary of the terminology: • first order recurrence: gives A(n + 1) in terms of A(n). • second order recurrence: gives A(n + 2) in terms of A(n + 1) and A(n). • initial condition(s): the first known value(s) of the function, one for first order, two for second order. • linear recurrence: the recurrence formula is linear. • constant coefficients: the coefficients do not change as n increases. • forcing term: an additional function added to the end of the recurrence formula. • homogeneous recurrence: the forcing term is zero. Example 55. 1. A(n + 1) = 2A(n), A(0) = 4. This is a first order linear, homogeneous recurrence. The initial condition is A(0) = 4. 2. B(n + 1) = 1.5B(n) + 3, B(0) = 3. This is a first order linear, inhomogeneous recurrence with forcing term f (n) = 3. The initial condition is B(0) = 3. 3. C(n + 1) = 4C(n) + n,C(2) = 1. This is a first order linear, inhomogeneous recurrence with forcing term f (n) = n. The initial condition is C(2) = 1. The recurrence is only valid for n ≥ 2. PART G . RECURRENCE RELATIONS 96 4. D(n + 2) = D(n + 1) + D(n), D(1) = 1, D(2) = 1 is a second order linear homogeneous recurrence relation. The two initial conditions are D(1) = 1 and D(2) = 1. 5. E(n + 2) = 2E(n + 1) − E(n) + n, E(4) = 3, E(5) = 6 is a second order linear recurrence relation. The two initial conditions are E(4) = 3 and E(5) = 6. It is inhomogeneous. The recurrence is valid for n ≥ 4. Definition 52. A solution for a recurrence relation is a function s(n) that satisfies the initial condition(s) and which also satisfies the recurrence formula. Table G .1 shows general solutions for first order linear recurrences with forcing terms of the form 0, c, and c + dn. Example 56. The function s(n) = (3/2) · 2n is a solution to the recurrence A(n + 1) = 2A(n), A(1) = 3. Proof. First, we check the initial condition: s(1) = (3/2) · 21 = 3 = A(1). Then we need to check the recurrence. To do this, we put s(n) into each side of the recurrence, separately, and simplify. First, we have 3 s(n + 1) = · 2n+1 = 3 · 2n . 2 Second, we have 3 n 2s(n) = 2 ·2 = 3 · 2n . 2 Because these simplify to the same thing, we see that s(n + 1) = 2s(n), so s(n) satisfies the recurrence. Example 57. Let r be nonzero and let n r −1 . s(n) = C0 r + c r−1 n Then s(n) is a solution to the recurrence A(n + 1) = rA(n) + c, A(0) = C0 . Proof. First, we check that s(0) = C0 : 0 r −1 s(0) = C0 r + c r−1 1−1 = C0 · 1 + c r−1 = C0 + c · 0 = C0 . 0 Then we need to check the recurrence. For the left side, we have s(n + 1) = C0 rn+1 + c n+1 r −1 r−1 . PART G . RECURRENCE RELATIONS 97 For the right side, we need to do more algebraic manipulation: n r −1 n rs(n) + c = r · C0 r + c +c r−1 n r −1 n+1 = C0 r + cr +c r−1 n r −1 n+1 = C0 r +c r +1 r−1 n+1 r −r n+1 +1 = C0 r +c r−1 n+1 r −r r−1 n+1 = C0 r +c + r−1 r−1 n+1 r −r+r−1 n+1 = C0 r +c r−1 n+1 r −1 n+1 +c = C0 r . r−1 Because s(n + 1) = rs(n) + c, we see that s(n) satisfies the recurrence formula. Example 58. We want to use Table G .1 to solve this recurrence: A(n + 1) = 3A(n) + 5, A(1) = 11. Looking at the table, we want to use the second part with r = 3 and c = 5. We can use the second part because 3 6∈ {0, 1}. However, we have A(1) instead of A(0). So we solve for a “theoretical” A(0): A(n + 1) = 3A(n) + 5 A(1) = 3A(0) + 5 11 = 3A(0) + 5 6 = 3A(0) 2 = A(0). So we have C0 = A(0) = 2. According to the table, the solution is n r −1 n A(n) = C0 r + c . r−1 Substituting the values we found, we have n 3 −1 A(n) = 2 · 3 + r . 2 n PART G . RECURRENCE RELATIONS 98 Recurrence Solution A(n + 1) = rA(n) A(0) = C0 A(n) = C0 rn A(n + 1) = rA(n) + c A(n) = C0 rn + c n r −1 r−1 For r 6∈ {0, 1} A(0) = C0 A(n + 1) = A(n) + c + dn A(n) = C0 + cn + d n(n − 1) 2 A(0) = C0 Table G .1: Formulas for first-order recurrence relations Exercises for Section 24 Warm up 24.1. An army preparing for attack arranges itself into rows so that the first row has 5 soldiers and each row has 8 more soldiers than the previous row. Let S(n) be the number of soldiers in row n. Write a recurrence relation for S(n), including the initial condition. Answer available. Warm up 24.2. Use table G .1 to find the solution to your recurrence from the previous warm up problem. Answer available. Exercise 24.3. In a particular game, a player can purchase power-ups using an in-game currency called zorkmids. The first power-up costs 2 zorkmids; the second costs 6 zorkmids; the third costs 12 zorkmids; and in general the kth power-up costs 2k zorkmids more than the previous one. Let Z(k) be the cost of the kth power-up. 1. Write a recurrence relation for Z(k) including the initial condition. 2. Use Table G .1 to find the solution to your recurrence. Exercise 24.4. We make a sequence of trees as follows. T1 has one node and no edges. T (k) is made by taking three copies of T (k − 1) and attaching a single new node to the top of each of those three copies. 1. Draw T1 , T2 , and T3 . 2. Let N(k) be the total number of nodes in Tk . Write a recurrence relation for N(k). 3. Use table G .1 to find the solution to your recurrence. Exercise 24.5. Verify that A(n) = 3 · 4n − 5 · 2n is a solution to the recurrence A(n + 2) = 6A(n + 1) − 8A(n), A(1) = 2, A(2) = 28. PART G . RECURRENCE RELATIONS 25 99 Second-order Linear Recurrence Relations A second-order linear recurrence relation with constant coefficients looks like A(n + 2) = r1 A(n + 1) + r2 A(n) + f (n). The coefficients r1 and r2 do not change, as the name indicates. The function f (n) is the forcing term. In this section, we will only look at second order linear recurrences in which the forcing term is zero – these are called homogeneous recurrences. A key example of this kind of recurrence is the Fibonacci recurrence: A(n + 2) = A(n + 1) + A(n), A(1) = 1, A(2) = 1. The main problem is: how do we find a solution to a recurrence like this? This section will present a general method for finding solutions, based on the ability to factor quadratic polynomials. Definition 53. If we write a second order linear homogeneous recurrence with constant coefficients in this form: A(n + 2) − r1 A(n + 1) − r2 A(n) = 0, A(n0 ) = C0 , A(n0 + 1) = C1 then the characteristic polynomial is the polynomial x2 − r1 x − r2 . The roots of the characteristic polynomial will help us find solutions to the recurrence, as the following proposition shows. Proposition 6. If α is a root of x2 − r1 x − r2 then s(n) = α n satisfies the recurrence formula s(n + 2) = r1 s(n + 1) + r2 s(n). Proof. Suppose α is a root of x2 − r1 x − r2 . This means that α 2 = r1 α + r2 . Multiplying both sides by α n gives α n+2 = r1 α n+1 + r2 α n . This shows that s(n + 2) = r1 s(n + 1) + r2 s(n). The proof of the following theorem requires more advanced techniques from linear algebra. The theorem shows us how to solve second order linear homogeneous recurrences with constant coefficients, when the characteristic polynomial has distinct roots. Theorem 15. If we have a second-order linear recurrence so that the characteristic polynomial has distinct roots α and β then every solution of the recurrence is of the form s(n) = Pα n + Qβ n for some constants P and Q that depend on the initial conditions. Example 59. We will solve the recurrence A(n + 2) = 5A(n + 1) − 6A(n), A(1) = 7, A(2) = 17. The first step is to write the characteristic polynomial, which is x2 − 5x + 6. The second step is to find the roots, which we can do by factoring: x2 − 5x + 6 = (x − 2)(x − 3) so we have α = 2 and β = 3. From the theorem, we know our solution will be of the form s(n) = Pα n + Qβ n = P · 2n + Q · 3n PART G . RECURRENCE RELATIONS 100 The last step is to find P and Q. We want S(1) = 7 and S(2) = 17, so we need to solve the system 7 = s(1) = P · 21 + Q · 31 = 2P + 3Q 59 = s(2) = P · 22 + Q · 32 = 4P + 9Q. If we multiply the first equation by 2, we obtain 14 = 4P + 6Q 17 = 4P + 9Q. Subtracting the equations gives 3 = 3Q so Q = 1. Putting this into the first equation gives 7 = 2P+3, which means 2P = 4 so P = 2. Therefore, our solution is s(n) = 2 · 2n + 3n Example 60. We will solve the Fibonacci recurrence A(n + 2) = A(n + 1) + A(n), A(1) = 1, A(2) = 1. The first step is to write the characteristic polynomial, which is x2 − x − 1. The next step is to find the two roots, which we can find by the quadratic formula: √ √ 1+ 5 1− 5 φ= , φ̂ = . 2 2 The first of these roots is commonly known as the golden ratio and denoted by φ , so we adopt that notation here. We will call the other root φ̂ . It will help us later to compute φ − φ̂ now: √ √ √ 1+ 5 1− 5 2 5 √ φ − φ̂ = − = = 5. 2 2 2 Also, because φ and φ̂ are both roots of x2 = x + 1, we have φ 2 = φ + 1 and φ̂ 2 = φ̂ + 1. From the theorem, we know our solution will be of the form s(n) = Pφ n + Qφ̂ n . We substitute s(1) = 1 and s(2) = 1: 1 = s(1) = Pφ 1 + Qφ̂ 1 = Pφ + Qφ̂ 1 = s(2) = Pφ 2 + Qφ̂ 2 = P(1 + φ ) + Q(1 + φ̂ ) = P + Pφ + Q + Qφ̂ . Subtracting the first equation from the second gives 0 = P + Q, so we have Q = −P. Substituting Q = −P into the first equation gives 1 = Pφ + (−P)φ̂ 1 = P(φ − φ̂ ) √ 1=P 5 1 P= √ . 5 √ This gives us the value of P; then we know that Q = −(1/ 5) and our solution is 1 1 s(n) = √ φ n − √ φ̂ n . 5 5 PART G . RECURRENCE RELATIONS 101 Repeated roots In the examples above, the characteristic polynomial had distinct roots. We may also encounter recurrences such as A(n + 2) = 4A(n + 1) − 4A(n). The characteristic polynomial is x2 − 4x + 4 which has a repeated root of 2. So, in a sense, we have “β = α”, and we can’t write our general solution in terms of α and β . The next proposition shows how we can find another solution when the characteristic polynomial has a repeated root. Proposition 7. If we have a recurrence formula A(n + 2) = r1 A(n + 1) + r2 A(n), so that the characteristic polynomial has a repeated root α, then s(n) = nα n is a solution of the recurrence. Proof. We compute both sides. For the left side, we obtain s(n + 2) = (n + 2)α n+2 . For the right side, we first note that if α is a repeated root then (x − α)2 = x2 − r1 x − r2 . So, by multiplying out the left side we have x2 − 2α + α 2 = x2 − r1 x − r2 , which means that r1 = 2α and r2 = −α 2 . Now we put s(n) = nα n into the right side of the recurrence: r1 s(n + 1) + r2 s(n) = r1 (n + 1)α n+1 + r2 nα n = (2α)(n + 1)α n+1 + (−α 2 )nα n = 2(n + 1)α n+2 − nα n+2 = (2(n + 1) − n)α n+2 = (2n + 2 − n)α n+2 = (n + 2)α n+2 So the left side is equal to the right side, which shows that s(n) satisfies the recurrence formula. The next theorem, like the first theorem in this section, is proved using techniques from linear algebra. Theorem 16. If we have a second-order linear recurrence so that the characteristic polynomial has a repeated root α then every solution of the recurrence is of the form s(n) = Pα n + Qnα n for some constants P and Q that depend on the initial conditions. Example 61. We solve the recurrence B(n + 2) = 4B(n + 1) − 4B(n), B(1) = 1, B(2) = 14. The characteristic polynomial is x2 − 4x + 4 which has a repeated root α = 2. So we know the solution will be of the form s(n) = P · 2n + Q · n · 2n . We substitute the two initial conditions: 1 = s(1) = P · 21 + Q · 1 · 21 = 2P + 2Q 14 = s(2) = P · 22 + Q · 2 · 22 = 4P + 8Q. PART G . RECURRENCE RELATIONS 102 Multiplying the first equation by 2 gives 2 = 4P + 4Q 14 = 4P + 8Q. Subtracting the first equation from the second gives 12 = 4Q, so Q = 3. Putting Q = 3 into the first equation gives 1 = 2P + 2(3) 1 = 2P + 6 −2P = 5 P = −5/2 Therefore, the solution to the recurrence is 5 s(n) = − · 2n + 3n · 2n . 2 Exercises for Section 25 Warm up 25.1. Consider the recurrence A(n + 2) = 3A(n + 1) + 4A(n). Write the characteristic polynomial of the recurrence and find the roots. Answer available. Exercise 25.2. Find the solution to this recurrence: B(n + 2) = 9B(n + 1) − 20B(n), B(1) = 6, B(2) = 34. C(1) = 9, C(2) = 63. Exercise 25.3. Find the solution to this recurrence: C(n + 2) = 6C(n + 1) − 9C(n), Challenge 25.4. Consider the sequence 1, 0, −1, −1, 0, 1, 1, 0, −1, −1, . . .. This sequence satisfies the recurrence D(n + 2) = D(n + 1) − D(n), D(1) = 1, D(2) = 0. Using complex numbers, find a solution to the recurrence. This is an example that shows that, even if the solution takes only real numbers as values, the roots of the characteristic polynomial can be complex numbers. PART G . RECURRENCE RELATIONS 103 Additional Problems for Part G G.1 Solve each of the following recurrence relations. (a) f (0) = 4 (b) g(0) = 2 (c) h(1) = 7 (d) j(0) = 15 (e) d(1) = 36 f (k + 1) = 3 f (k) + 2 g(k + 1) = g(k) + k − 2 h(k + 1) = 1.5h(k) + 4 j(1) = 9 j(k + 2) = j(k + 1) + 2 j(k) d(2) = 135 d(k + 2) = 6d(k + 1) − 9d(k) Answer available. G.2 Each year, on May 1, 500 trout are placed into a large lake to supplement the population already present. Even with reproduction, only 80% of the trout that are in the lake on May 1 of one year will still be around on May 1 of the next year. A trout population study shows that just before the new trout were added in the year 2017 there were 8,000 trout in the lake. (a) How many trout will be in the lake just before new trout are added in 2018? In 2019? (b) Write a recurrence relation for the number of fish in the lake just before the new fish are added each year. It may help to use k = 0 to stand for the year 2017. (c) Solve your recurrence relation from part (b). Answer available. Answers to Warm-up and Additional Problems These are only summary answers, intended to help you check your work. Homework solutions must include enough work to show how the answer was obtained, in addition to the summary answer. Part A • 1.1: Binary Decimal Hexadecimal (11010110)2 214 0xD6 (11111100001)2 2017 0x7E1 (1110100101)2 933 0x3A5 • 2.1: gcd(65,156) = 13 (found in 6 steps) • 2.2: gcd(85,204) = 17 (found in 4 steps) • 2.3: 156 × 204 = 31, 824 (table of 8 rows, add 4 numbers) • 2.4: 104 ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 105 Algorithm 8 (Decimal to hexidecimal conversion). Inputs: One non-negative integer n given in decimal representation. Outputs: The non-negative integer n given in hexidecimal representation. Instructions: 1. First, we make a table one row at a time. Each row has four numbers. (a) The first number in the first row is n, The second number in the first row is the quotient of n when divided by 16. The third number in the first row is the remainder of n when divided by 16. The fourth number in the first row is the hexidecimal representation of the remainder of n when divided by 16. (b) To make the next row, the quotient from the previous row becomes the first number in the next row. The second number is the quotient of the first number when divided by 16. The third number is the remainder of the first number when divided by 16. The fourth number is the hexidecimal representation of the remainder of the first number when divided by 16. (c) Continue performing the previous instruction until the second number becomes 0. 2. Go through the table from top to bottom and list the hexidecimal representation of each remainder from right to left. 3. Output the hexidecimal number from the previous step. Additional Problems for Part A • 1 2015 = (111 1101 1111)2 = 0x7DF • 2 0x13A = 314 = (1 0011 1010)2 • 3 gcd(1978,6969156) = 2 (found in 7 steps using division algorithm) • 4 58 × 62 = 3, 596 (table of 6 rows, add 5 numbers) Part B • 3.1: A truth table would have 1 = 20 rows if the connective was nullary (no arguments); 2 = 21 rows if unary (one argument); 4 = 22 rows if binary (two arguments); 8 = 23 rows if ternary (three arguments); 16 = 24 rows if quaternary (four arguments). • 3.2: AND is commutative because the formula x ∧ y has the same truth value as the formula y ∧ x. x y x∧y y∧x F F F F F T F F T F F F T T T T IF is not commutative. NOR is commutative. ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 106 • 3.3: OR is idempotent. IF is not idempotent. NAND is not idempotent because xNANDx does not have the same truth value as x. x x xNANDx F F T T T F • 4.1. x F F T T y F T F T x∧y (x ∧ y) → x F F F T T T T T x y z φ x XOR y ψ y∧z φ ∨ψ F F F F T T T T F F T T F F T T F T F T F T F T F F T T T T F F F F F T F F F T F F T T T T F T • 4.2: AND is associative. XOR is associative because x XOR (y XOR z) has the same truth value as (x XOR y) XOR z. x y z (y XOR z) x XOR (y XOR z) (x XOR y) (x XOR y) XOR z F F F F F F F F F T T T F T F T F T T T T F T T F F T F T F F F T T T T F T T F T F T T F T F F F T T T F T F T NAND is not associative. • 4.3: x ∨ ¬y is not a tautology. x ∨ ¬x is a tautology. x ∧ (¬x ∨ y) is not a tautology. • 5.1: 1. (y ∧ z) ∨ (¬y) can be expanded to (y ∨ ¬y) ∧ (z ∨ ¬y). Because (y ∨ ¬y) is always True, we can simplify the formula to z ∨ ¬y. 2. (x → y) ∧ ¬y can be rewritten as (¬x ∨ y) ∧ ¬y. This distributes to (¬x ∧ ¬y) ∨ (y ∧ ¬y). Now y ∧ ¬y is always False, so the formula simplifies to ¬x ∧ ¬y. • 5.2: ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 107 1. ¬((x ∧ y) ∨ (z ∧ w)) is equivalent to ¬(x ∧ y) ∧ ¬(z ∧ w), which is equivalent to (¬x ∨ ¬y) ∨ (¬z ∨ ¬w) 2. ¬((x ∨ y ∨ z) ∧ (w ∨ u)) is equivalent to ¬(x ∨ y ∨ z) ∨ ¬(w ∨ u), which is equivalent to (¬x ∧ ¬y ∧ ¬z) ∨ (¬w ∧ ¬u) • 6.1: The formula x ∧ x̄ is one example. • 6.2: Conjunctive normal form (x̄ ∧ ȳ) ∨ (x̄ ∧ y) ∨ (x ∧ y); disjunctive normal form x̄ ∨ y. • 7.1: First rewrite AND using OR and NOT as (P ∧ Q) ↔ ¬(¬P ∨ ¬Q). Now use the known expression for OR to get (P ∧ Q) ↔ ¬(¬P ∨ ¬Q) ↔ ¬(¬¬P → ¬Q) ↔ ¬(P → ¬Q). • 7.2: We need to show that we can write each of AND, OR, and NOT in terms of NAND. To write NOT in terms of NAND, we can use the tautology ¬P ↔ (P ↑ P). To write AND in terms of NAND, we can use the tautology (P ∧ Q) ↔ ¬(P ↑ Q). To write OR in terms of NAND, we can use the tautology (P ∨ Q) ↔ (¬P ↑ ¬Q). Additional Problems for Part B • 1 See Figure B .1. x y z (y ↔ z) x ∧ (y ↔ z) F F F T F F F T F F F T F F F • 2 F T T T F T F F T T T F T F F T T F F F T T T T T Disjunctive normal form : (x ∧ ¬y ∧ ¬z) ∨ (x ∧ y ∧ z) Conjunctive normal form : (x ∨ y ∨ z) ∧ (x ∨ y ∨ ¬z) ∧ (x ∨ ¬y ∨ z) ∧ (x ∨ ¬y ∨ ¬z) ∧ (¬x ∨ y ∨ ¬z) ∧ (¬x ∨ ¬y ∨ z) • 3 (a) The first clause is not satisfiable because of the term w ∧ ¬w. The second clause is satisfiable with w = T , x = F, y = T , z = T . The two clauses are connected with OR, meaning the expression is satisfiable. (b) Both clauses are not satifiable because of the terms x ∧ ¬x and y ∧ ¬y. Therefore, the expression is not satisfiable. • 4 (x ∧ p) ∨ (x ∧ q) ∨ (y ∧ p) ∨ (y ∧ q) ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS ¬(x ∨ ¬y) ∨ (y) (¬x ∧ ¬¬y) ∨ (y) • 5 (¬x ∧ y) ∨ (y) (¬x ∨ y) ∧ (y ∨ y) 108 De Morgan’s Law Double negation elimination (involution) Distributivity Part C • 8.1: R1 = A \ B. R2 = A ∩ B. R3 = B \ A. R4 = (A ∪ B)c . • 9.1: 1. Not a function; contains (1, 3) and (2, 3). 2. Is a function. contains (1, 1) and (1, −1). 4. Not a function; no pair starts with 0. • 10.1: D(60) = {2 · 2, 1 · 3, 1 · 5}. D(900) = {2 · 2, 2 · 3, 2 · 5}. D(15) = {1 · 3, 1 · 5}. 3. Not a function; D(4) = {2 · 2}. Additional Problems for Part C • 1 (a) P = {2, 3, 5, 7, 11} Q = {1, 3, 5, 7, 9} (b) P ∪ Q = {1, 2, 3, 5, 7, 9, 11} P ∩ Q = {2, 3, 5, 7} P \ Q = {2, 11} Q \ P = {1, 9} P×Q = {(2, 2), (2, 3), (2, 5), (2, 7), (2, 9), (3, 2), (3, 3), (3, 5), (3, 7), (3, 9), (5, 2), (5, 3), (5, 5), (5, 7), (5, 9), (7, 2), (7 • 2 L ◦ L = (a, c) : for some b ∈ R, (a, b) ∈ L and (b, c) ∈ L. So for (a, c) ∈ L ◦ L we have a b ∈ R with a < b and b < c. In particular, a < c, meaning (a, c) ∈ L. On the other hand, if (a, c) ∈ L then a < c. Properties of the real numbers give there is a b ∈ R with a < b < c. Thus (a, b) ∈ L and (b, c) ∈ L, meaning (a, c) ∈ L ◦ L. • 3 Let A be the computers that boot correctly; we know |A| = 18. Let B be the computers that have a working keyboard; we know B = 15. The universal set U is all computers and we know |U| = 30. A ∪ B is the set of computers that either boot correctly or have a working computer, or both. U \ (A ∪ B) is all the computers that both don’t boot correctly and don’t have a working keyboard. We know |U \ (A ∪ B)| = 7, meaning |A ∪ B| = 23. We want to find |A ∩ B|, the number of computers that both boot correctly and have a working keyboard. Using the inclusion-exclusion principle, 23 = 18 + 15 − |A ∩ B|. Thus |A ∩ B| = 10. ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 109 Part D • 11.1: Proof. Let n be an even integer. This means that there is an integer k so that n = 2k. Then for any integer m, we have the product nm = (2k)m = 2(km). Because k and m are integers, so is their product km. Thus, the product nm is 2 times an integer, meaning it is an even integer. • 11.2: Proof. To proceed by contradiction, assume that there is a smallest positive rational number, say r. Consider the rational number r/2. The number r/2 is positive and smaller than r. We have reached a contradiction to our original assumption, because we have found a positive rational number smaller than r. Thus there is no smallest positive rational number. • 12.1: 1. 5 ways to choose a junior, 3 ways to choose a senior. Overall 15 ways to choose one of each. 2. (26)4 . • 12.2: 1. Each dial has 10 options and the choices are independent, so the overall number of combinations is (10)4 = 10, 000. 2. (10000)4 = 1.00 × 1012 . • 13.1: 1. Need to choose the remaining 4 cards: C(51, 4). 2. Need to choose the remaining three cards: C(50, 3). 3. Need to choose 5 cards out of the other three suits: C(39, 5). • 13.2 1. 6 × 6 ×C(8, 2) ×C(13, 3). 2. 6 × 6 × 17 × 213 . There are 213 sets of free toppings, one for each subset of the set of free toppings. • 13.3 1. C(4, 2) ×C(48, 3). Choose 2 aces and 3 non-aces. 2. C(26, 5). Choose 5 red cards. • 14.1: 1. (6!)/(4!2!) = (6 · 5)/(2) = 15. 2. (5!)/(3!2!) = (5 · 4)/2 = 10. 5 · 4 = 20. 4. (7!)/(4!) = 7 · 6 · 5 = 210. 3. (5!)/(3!) = • 14.2 NNO NON NOO ONN ONO OON Third letter NN Second letter First letter NO ON N O OO ε • 14.3: 1. 3!. 2. 5!/2!. 3. 7!. • 14.4: If we have 2 copies of C, then we need to choose one more letter out of LOK and then permute, so there will be C(3, 1) × 3!/2! = 9 options. If there is at most one C, then we need to choose 3 letters from CLOK and permute, so there will be C(4, 3) × 3! = 24 options. Overall, there are 9 + 24 = 33 distinguishable 3-permutations of CLOCK. ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS • 14.5: 1. 3!. 2. 33, for the same reason as 14.4. 110 3. C(7, 3) · 3! = 35. • 15.1: The best units for this answer turn out to be years. (62)8 passwords × 1 sec 1 hour 1 day 1 year × × × ≈ 7 years. 1000000 passwords 3600 sec 24 hour 365.25 day Additional Problems for Part D • 1 (a) C(10, 4) ×C(8, 2) = 210 × 28 = 5, 880 (b) C(10, 8) × 8! = 45 × 8! = 1, 814, 400 (c) 8!10! ≈ 1.4 × 1011 (d) C(10, 4) ×C(6, 3) × 1 = 210 × 20 = 4, 200 (e) C(8, 2) ×C(6, 2) ×C(4, 2) ×C(2, 2) = 28 × 15 × 6 × 1 = 2, 520 • 2 (a) C(4, 1) ×C(4, 1) = 4 × 4 = 16 (b) The total number of hands of two cards is C(52, 2) = 1, 326. Therefore the probability of 16 = 0.012 = 1.2%. drawing two cards that add to 21 is 1, 326 • 3 Let A be the event “contains exactly 2 aces”. There are 2 of 4 aces C(4, 2). The remaining cards are not aces, so the three are drawn from 48, C(48, 3). Thus, |A| = 103, 776. Let B be the event “all red cards”. There are 26 red cards, C(26, 5). Thus |B| = 65, 780. The event A ∩ B is both contains exactly 2 aces and all red cards. There are only 2 red aces, so both appear in this hand and there is one way to select them both. The remaining 3 cards are selected from the non-ace red cards, C(24, 3) = 2, 024. Thus |A ∩ B| = 2, 024. The event A ∪ B is one or the other or both. Using the inclusion-exclusion principle, |A ∪ B| = |A| + |B| − |A ∩ B| = 103, 776 + 65, 780 − 2, 024 = 167, 532. • 4 (a) P(4, 3) = 24 (b) 45 = 1, 024 (c) 44 = 256 (d) Case 1 a : Choose location of the a, C(5, 1) = 5. The remaining 4 locations are filled with any letter other than a, 34 . Thus, 5 ∗ 34 = 405. Case 2 a’s : Choose location of the a’s, C(5, 2) = 10. The remaining 3 locations are filled with any letter other than a, 33 . Thus, 10 ∗ 33 = 270. Case 3 a’s : Choose location of the a’s, C(5, 3) = 10. The remaining 2 locations are filled with any letter other than a, 32 . Thus, 10 ∗ 32 = 90. ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 111 Case 4 a’s : Choose location of the a’s, C(5, 4) = 5. The remaining 1 locations are filled with any letter other than a, 3. Thus, 5 ∗ 3 = 15. Case 5 a’s : 1 way; all spots filled with a. All together : 405 + 270 + 90 + 15 + 1 = 781 (e) The sequence starts with d, leaving 4 positions to be filled. Case 1 a : Choose location of the a, C(4, 1) = 4. The remaining 3 locations are filled with any letter other than a, 33 . Thus, 4 ∗ 33 = 108. Case 2 a’s : Choose location of the a’s, C(4, 2) = 6. The remaining 2 locations are filled with any letter other than a, 32 . Thus, 6 ∗ 32 = 54. Case 3 a’s : Choose location of the a’s, C(4, 3) = 4. The remaining 1 locations are filled with any letter other than a, 3. Thus, 4 ∗ 3 = 12. Case 4 a’s : 1 way; all remaining spots filled with a. All together : 108 + 54 + 12 = 174 (f) Let A be the event “starts with a d”. We know |A| = 256. Let B be the event “contains at least one a”. We know |B| = 781. We know the number of 5-sequences that both start with a d and contain at least one a is |A ∩ B| = 174. Using the inclusion-exclusion principle, the number of 5-sequences that start with a d or contain at least one a is |A ∪ B| = |A| + |B| − |A ∩ B| = 265 + 781 − 174 = 863. (g) Sequences of length 1 : 4 Sequences of length 2 : 42 = 16 Sequences of length 3 : 43 = 64 All together : 64 + 16 + 4 = 84 • 5 The total number of sets of 7 cards from a standard deck is C(52, 7) = 133, 784, 560 A hand without the queen of spades is drawn from a set of 51 cards, instead of 52. A hand with two aces is drawing 2 of 4 aces, C(4, 2). That leaves a total of 48 other cards that are not aces and 47 without the queen of hearts as well. The remaining 5 non-ace cards are drawn from these 47, C(47, 5). All togher, there are C(4, 2)C(47, 5) = 9, 203, 634 hands of 7 cards containing exactly 2 aces and not the queen of hearts. 9, 203634 Therefore, the probability of selecting such a set of 7 cards is = 0.069 = 6.9%. 133, 784, 560 • 6 C(5, 3) ×C(2, 1) ×C(1, 1) = C(5, 1) ×C(4, 1) = 20 • 7 64 × 49 × 36 × 25 × 16 × 9 × 4 × 1 ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 112 • 8 13! 2!2!2!3! • 9 The number of permutations for TROUBLEMAKING is P(13, 13) = 13! = 6, 227, 020, 800. 24, 980 seconds 6, 227, 020, 800 permutations = = 6.9 hours 250, 000 permutations per second 3, 600 seconds per hour ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 113 Part E • 16.1: Proof. We let P(n) say that ∑ni=1 c = cn, for a fixed constant c. The summation begins with i = 1 and so we will show P(n) holds for all n ≥ 1. To apply induction, we need to verify two things: 1. Base case: ∑1i=1 c = c × 1. k+1 2. Inductive step: for each k, ∑ki=1 c = ck, then ∑i=1 c = c(k + 1). For the base case, ∑1i=1 c = c = c × 1. The base case is proved. k+1 c as ∑ki=1 c + For the inductive step, assume we have some k so that ∑ki=1 c = ck. Rewrite ∑i=1 c. Using P(k), we can replace the sum with its value, so we have k+1 k ∑ c = ∑ c + c = ck + c. i=1 i=1 We can simplify the final expression as ck + c = c(k + 1). So, if we have some k so that ∑ki=1 c = ck, then ∑k+1 i=1 c = c(k +1). This shows that the inductive step holds. Because we were able to prove the base case and the inductive step, we can conclude that ∑ni=1 c = cn, for a fixed constant c, for all n ≥ 1. • 17.1: NE grid paths ending at (2, 2): NE grids paths ending at (3, 2): ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 114 • 17.2 1. The path will have m east steps and n north steps, for a total of m + n steps. 2. Of the m + n steps a path is determined by the location of the n north steps. The count of such paths is C(m + n, n). 3. Of the m + n steps a path is determined by the location of the m east steps. The count of such paths is C(m + n, m). (m + n)! 4. C(m + n, n) = = C(m + n, m) m!n! Additional Problems for Part E • 1 Proof. We proceed by induction. First, we verify the base case n = 1. The left-hand side is 1 1 1 1 ∑ j( j + 1) = 1(1 + 1) = 2 . j=1 The right-hand side is 1 1 = . 1+1 2 Since the left-hand side is equal to the right-hand side, the formula holds for n = 1. Second, we assume the formula holds for n = k. That is, k 1 k ∑ j( j + 1) = k + 1 j=1 Third, we show the formula holds for n = k + 1. The left-hand side is k+1 1 k 1 1 ∑ j( j + 1) = ∑ j( j + 1) + (k + 1)((k + 1) + 1) . j=1 j=1 Using the assumption for n = k, this becomes k 1 1 k 1 ∑ j( j + 1) + (k + 1)((k + 1) + 1) = k + 1 + (k + 1)((k + 1) + 1) j=1 = k(k + 2) + 1 k+1 = . (k + 1)(k + 2) k + 2 The right-hand side is k+1 k+1 = . (k + 1) + 1 k + 2 Since the left-hand side is equal to the right-hand side, the formula holds for n = k + 1. Thus we have shown the formula holds for all n by induction. • 2 ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 115 Proof. We proceed by induction. First, we verify the base case n = 1. The left-hand side is 1 1 1 1 1 1 ∑ i − i−1 = 1 − 1+1 = 2. i=1 The right-hand side is 1 1 = . 1+1 2 Since the left-hand side is equal to the right-hand side, the formula holds for n = 1. 1− Second, we assume the formula holds for n = k. That is, k 1 1 1 ∑ i − i+1 = 1− k+1 i=1 Third, we show the formula holds for n = k + 1. The left-hand side is k+1 k 1 1 1 1 1 1 ∑ i − i + 1 = ∑ i − i + 1 + k + 1 − (k + 1) + 1 . i=1 i=1 Using the assumption for n = k, this becomes k 1 1 1 1 1 1 1 ∑ − i + 1 + k + 1 − (k + 1) + 1 = 1 − k + 1 + k + 1 − (k + 1) + 1 i=1 i 1 = 1− . (k + 1) + 1 The right-hand side is 1− 1 . (k + 1) + 1 Since the left-hand side is equal to the right-hand side, the formula holds for n = k + 1. Thus we have shown the formula holds for all n by induction. • 3 Proof. We proceed by induction. Let P(n) say that 11n − 2n is a multiple of 9. First, we verify the base case, n = 1. We have 111 − 21 = 11 − 2 = 9. Thus the base case P(1) holds. Second, we assume P(k) holds, that is, 11k − 2k is a multiple of 9. Then there is an integer r so that 11k − 2k = 9 · r. This means 11k = 9 · r + 2k . Third, we show P(k + 1) holds. Starting with the expression for n = k + 1, we have 11k+1 −2k+1 = 11k ·11−2k+1 = 11·(9r −2k )−2k+1 = 9·11·r −11·2k −2·2k = 9·11·r −9·2k = 9(11·r −2k ). This means P(k + 1) holds. We have shown 11n − 2n is a multiple of 9 for n ≥ 1 by induction. • 4 The formula for n ≥ 0 is xn+1 − 1 = xn + xn−1 + · · · + x + 1 x−1 ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 116 Proof. We proceed by induction. First, we verify the base case n = 0. The left-hand side is x0+1 − 1 = 1. x−1 The right-hand side is 1. Since the left-hand side is equal to the right-hand side, the formula holds for n = 1. Second, we assume the formula holds for n = k. That is, xk+1 − 1 = xk + xk−1 + · · · + x + 1. x−1 Rearranging the expression gives xk+1 − 1 = (x − 1)(xk + xk−1 + · · · + x + 1). Third, we show the formula holds for n = k + 1. Starting with the assumption, multiply both sides of the expression by x to get xk+2 − x = (x − 1)(xk+1 + xk + · · · + x2 + x). Add and subtract 1 on the left-hand side to get xk+2 − x = (xk+2 − 1) − (x − 1) = (x − 1)(xk+1 + xk + · · · + x2 + x). Now add (x − 1) to both sides to get xk+2 − 1 = (x − 1)(xk+1 + xk + · · · + x2 + x) + (x − 1). Finally, factor out x − 1 from both terms on the right-hand side to get xk+2 − 1 = (x − 1)(xk+1 + xk + · · · + x2 + x + 1). Thus the formula holds for n = k + 1 and we have shown the formula holds for all n by induction. • 5 Proof. We proceed by induction. First, we verify the base case, n = 4. Notice that 16 = 24 < 4! = 24. Thus the formula holds for n = 4. Second, we assume the formula holds for n = k. That is, 2k < k!. Third, we show the formula holds for n = k + 1. Using the assumption and the fact that 2 < k + 1, we have 2k+1 = 2k · 2 < k! · 2 < k! · (k + 1) = (k + 1)! Thus we have shown the formula holds for all n ≥ 4 by induction. • 6 ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 117 Proof. We proceed by induction. First, we verify the base case, n = 1. The left-hand side is 1 ∏(2i − 1) = 2(1) − 1 = 1. i=1 The right-hand side is (2(1))! 2 = = 1. 21 (1!) 2 Since the left-hand side is equal to the right-hand side, the formula holds for n = 1. Second, we assume the formula holds for n = k. That is, k (2k)! ∏(2i − 1) = 2k (k!) . i=1 Third, we show the formula holds for n = k + 1. The left-hand side is k+1 k ∏(2i − 1) = ∏(2i − 1) · (2(k + 1) − 1) i=1 i=1 Using the assumption for n = k, this becomes k (2k)! (2k)! ∏(2i − 1) · (2(k + 1) − 1) = 2k (k!) · (2(k + 1) − 1) = 2k (k!) · (2k + 1). i=1 The right-hand side is (2(k + 1))! 2k+1 ((k + 1)!) = (2k)! · (2k + 1) · (2k + 2) (2k)! (2k + 1)(2k + 2) (2k)! = k = k (2k + 1). k 2 · 2 · k! · (k + 1) 2 (k!) 2 · (k + 1) 2 (k!) Since the left-hand side is equal to the right-hand side, the formula holds for n = k + 1. Thus we have shown the formula holds for all n by induction. Part F • 18.1: 1. nk and rk 2. Write mk = as and nk = at, for some integers s and t. So mk = as = nk dk + rk = atdk + rk . Rearranging gives rk = a(s − tdk ). 3. Write nk = bs and rk = bt, for some integers s and t. So mk = nk dk + rk = bsdk + bt = b(sdk + t). • 19.1: K6 K4,4 ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 118 K6 has 15 edges and each vertex has degree 5. K4,4 has 16 edges and each vertex has degree 4. • 19.2: We label the vertices and edges as follows. We put the rows in order A, B, C and the columns in order e1 , e2 , e3 . B e1 A B e2 e3 e1 C A e2 C An incidence matrix has one column for each edge and one row for each vertex. The incidence matrix for C3 is: e1 e2 e3 A "1 0 1# B 1 1 0 C 0 1 1 The incidence matrix for P3 is: e1 " A 1 B 1 C 0 e2 0# 1 1 • 19.3: An adjacency matrix has one row and one column for each vertex. We put the rows and columns in order A, B, C. The adjacency matrix for C3 is: A " A 0 B 1 C 1 B 1 0 1 C 1# 1 0 A A "0 B 1 C 0 B 1 0 1 C 0# 1 0 The adjacency matrix for P3 is: • 20.1: Eulerian tour for K4 is impossible by Theorem 10; the graph is connected but every vertex has degree 3. An Eulerian tour for K2,4 : label the 2 elements α, β and the 4 elements A, B, C, D, then one Eulerian tour is α, A, β , B, α, C, β , D, α. • 20.2: The graph K2,3 is not Hamiltonian. Let A be the vertex set with 2 vertices and B be the vertex set with 3 vertices. The bipartite structure means any walk must visit a vertex in A, ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 119 followed by a vertex in B, followed by a vertex in A, and so on (or starting with a vertex in B). The longest cycles on the bipartite graph K2,3 will use all of the vertices in A and will have the form a1 b1 a2 b2 a1 or b1 a1 b2 a2 b1 . Each such cycle visits 4 vertices and visits only 2 of the vertices in B. There is no way to construct a cycle that visits the remaining vertex in B. • 21.1: We list the vertices in the order they are output. Breadth-first: A, B, C, D, E, F, G, H. Depth-first: A, B, D, G, C, E, H, F. • 21.2: We list the vertices in the order they are output. Breadth-first: A, B, C, D, E, F, G. Depth-first: A, B, D, E, C, G, F. A • 22.1: C B A C A C B D A B A C C B B D A A C C B B A D C B D A C B D • 23.1 Initialization: starting vertex D has tentative distance 0, all others have tentative distance ∞. Step 1: Visit vertex D. Update tentative distances for A (7), B (3), and C (1). Step 2: Visit vertex C. No updates to tentative distances. Step 3: Visit vertex B. Update tentative distance for A (4). Step 4: Visit vertex A. All other nodes have been visited, so nothing is updated. Step 5: All nodes have been visited. The shortest distance path from D is A (4), B (3), C (1), and D (0). In tabular form, initialization A ∞ B ∞ C ∞ D 0 step 1 7 3 1 v; 0 step 2 7 3 v; 1 0 step 3 4 v; 3 1 0 step 4 v; 4 3 1 0 step 5 4 3 1 0 path D-B-A D-B D-C D Additional Problems for Part F • 1 (a) Every Kn has a Hamiltonian cycle. For example, number the vertices 1 through n and visit the vertices in numerical order. This is possible since the graph is complete, every vertex is connected to every other vertex. (b) A graph Km,n will have a Hamiltonian cycle if m = n. Notice, this will imply that the total number of vertices is even. Exercise 20.5 proves that bipartite graphs must have an even number of vertices to potentially have a Hamiltonian cycle. • 2 Proof. Let G be a graph with a walk from vertex u to vertex v. If the walk uses vertex w more than once, then reduce the length of the walk by eliminating all of the vertices in the ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 120 walk between the two visits to w. The resulting walk is between u and v and does not contain this instance of the repeated visit to w. Repeat this elimination process for all vertices that are visited more than once. The resulting walk from u to v will not use any vertex more than once. • 3 Proof. Let G be a connected graph, C be a cycle in G, and e an edge of C. Since G is connected, there is a walk in G between any two vertices. For any walk connecting two vertices that contain edge e, there is a second walk between the two vertices using all the other edges of the cycle. If e is removed, there remains a walk between all pairs of vertices. If the walk did not include e, then the same walk remains in the new graph. If the walk did include e, then use the second walk consisting of the other vertices in the cycle. • 4 (a) pentane (C5 H1 0) H H H H H H H H H C C C C C H H H H H H H H H C C C C H H H H H C C H H H C H C H H H H H C H H C H H (a) hexane (C6 H14 ) H H H H H H H H C C C C C C H H H H H H H H H H H C C C C C H H H H H H H C H H H H H H H H H H C C C C C H H H H H C H H C H H H C C H H H H H H C C H C H H H ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 121 H H C H H C C H H C H H H C C H H H H H (b) Using the tree structure, the graph will have one fewer edge than nodes. That is, the graph will have e = h + c − 1 edges. Using the chemical structure, each hydrogen is connected to one other atom and each carbon is connected to four other atoms. So each hydrogen contributes to 1 link and each carbon to 4. The total number of links is l = (h + 4c). Each edge corresponds to two links, so the total number of links is twice the number of edges. This gives 2(h + c − 1) = 2e = l = h + 4c. Solving for h gives h = 2c + 2. • 5 Proof. Let G be a simple graph in which every vertex has degree at least 2. Construct a walk on G in the following way. Choose any vertex v1 and take any edge e1 to another vertex v2 . Because G is simple, G has no loops and we know vertex v2 is not vertex v1 . From v2 follow a different edge e2 to a vertex v3 . Since the degree of each vertex is at least 2, we know that there will be an unused edge. Also, because G is simple, G has no multiple edges e1 will not be the same as edge e2 . This also gives that v2 will not be the same as v1 because G is simple with no multiple edges. Repeat this process. Continue to form a walk by following unused edges until one of the vertices, say v∗ is visited twice. At every vertex, until possibly the one visited twice, there will be an unused edge because the degree of each vertex is even. The walk starting and ending with v∗ will be a cycle. By construction, the only vertex visited twice in the original walk and thus the restricted walk is v∗ . Part G • 24.1 A(n + 1) = 8 + A(n), A(1) = 5. • 24.2 We use the final part of the table. We can’t use the second part of the table because we would have r = 1. Working backwards, we have A(1) = 8 + A(0), so 5 = 8 + A(0), which means A(0) = −3. There is no row zero; we only use A(0) to match the table. According to the table, C0 = −3, c = 8, d = 0. So the solution is A(n) = −3 + 8n. • 25.1 The characteristic polynomial is x2 − 3x − 4. This factors as (x − 4)(x + 1). The roots are 4 and −1. ANSWERS TO WARM-UP AND ADDITIONAL PROBLEMS 122 Additional Problems for Part G • 1 n 3 −1 (a) f (n) = 4 · 3 + 2 3−1 n(n − 1) (b) g(n) = 2 − 2n + 2 n 1.5n − 1 (c) 7 = h(1) = 1.5h(0) + 4 gives h(0) = 2. h(n) = 2 · 1.5 + 2 1.5 − 1 n (d) The characteristic polynomial is x2 − x − 2; it has roots −1, 2. j(n) = 7 · (−1)n + 8 · 2n (e) The characteristic polynomial is x2 − 6x + 9; it has repeated root 3. d(n) = 9 · 3n + 3 · n · 3n • 2 (a) 8,000 in 2017; 0.8(8,000 + 500) = 6,800 in 2018; 0.8(6,800 + 500) = 5,840 in 2019. (b) Let n = 0 mean year 2017, specifically just before new fish are added in May. The initial condition is A(0) = 8, 000. The recurrence formula is A(n+1) = 0.8(A(n)+500) = 0.8A(n)+ 400. n 0.8 − 1 n (c) A(n) = 8, 000 · 0.8 + 400 0.8 − 1