Worksheets for Computer Science Orientation Version 1.1, 22 January 2007 Daniel Balls This document is in the public domain. Funding for this project was provided by the Hewlett Foundation through its Engineering Schools of the West Initiative. CS 160 Computer Science Orientation Name ________________________ Section Meeting Time____________ Logic Gates Worksheet In order to add, subtract, and compare numbers, computers manipulate 0s and 1s. Operations that manipulate individual 0s and 1s are called Boolean operations. A logic gate is a mechanism that performs a Boolean operation by taking one or more 0s and 1s as inputs and producing a 0 or 1 as output. The Boolean operations we will study are either unary or binary. A unary operation has a single input, and a binary operation has two inputs. Finding the output of a logic gate is simple, once a few principles are understood. A f A B f(A) A unary operation (one input). f f(A, B) A binary operation (two inputs). The only unary Boolean operation is the NOT function. The NOT gate returns the complement of the operand. An input of 1 results in an output of 0; conversely, an input of 0 results in an output of 1. The latter of the two possibilities for the NOT operation is shown below. 0 1 NOT The NOT gate returns the complement of the input. The most common binary Boolean operations include AND, OR, and XOR (short for ‘exclusive OR’). If at least one of the two operands is 1, the OR operation gives an output of 1. The output of the XOR operation is 1 when exactly one of the operands is 1. The AND operation returns 1 only if both inputs are 1. There are four possible combinations of input pairs for each of these three gates. One of these combinations for each gate is shown below. 0 1 OR 1 The OR gate returns 1 when at least one of its inputs is 1. 1 1 1 0 XOR The XOR gate returns 0 when both operands are 1 or both operands are 0. 1 0 0 AND When at least one of the operands is 0, the output of the AND gate is 0. Other logical gates can be formed by appending the NOT gate to any of the binary gates. These gates are denoted by NAND (NOT AND), NOR (NOT OR), and XNOR (NOT XOR). The output of these gates can be found by first determining the output of the binary gate and then using this output as the input of the NOT gate, as shown below. 1 0 0 AND 1 0 NAND 1 NOT 1 The NAND gate is equivalent to an AND gate followed by a NOT gate. The symbols that we have used above for the different logic gates are easily distinguishable and widely used, with one difference: the name of the gate is not written inside the symbol. Henceforth we will follow that practice. The symbols for the seven logic gates are as follows: AND OR XOR NOT NAND NOR XNOR These are the symbols for the seven logic gates presented in this worksheet. 2 One way to represent a Boolean function is through the use of a truth table. The first column(s) give all possible input combinations. The values in the last column(s) represent the output of the function given the inputs that precede it. Such tables are called truth tables. An example of the truth tables for the NOT and OR operations are shown below (I1 stands for the first input, I2 stands for the second input and O represents the output): I1 I2 O 0 0 0 1 0 1 0 1 1 1 1 1 I1 O 1 0 0 1 Truth table for the NOT gate. Truth table for the OR gate. An applet that will help you become more familiar with the basic logic gates can be accessed at eecs.oregonstate.edu/~quinn/education/JavaApplets/applets/LogicGates.html. 1. 2. 3. 4. 5. Make sure the option 1 Gate is selected. Double-click on the rectangle and choose one of the logic gates available. Next change the values of the inputs by clicking on the values. Repeat steps 2 and 3 for all the logic gates. Notice than when gate is selected, a table appears to the right. This is a truth table for the selected gate. Once inputs have been selected, the row that corresponds to these inputs is highlighted. Critical Thinking: Answer the following questions, using the applet whenever helpful. Q1: Using the space below, explain in your own words the difference between the XOR gate and the NOR gate. Q2: Which pairs of input values will yield an output value of 1 for the NAND gate? Which pairs of input values will yield an output value of 0 for the AND gate? 3 Q3: In the space below, construct truth tables for the five other Boolean operations listed above. 4 Combinatorial Circuits An arrangement of more than one logic gate is called a combinatorial circuit. The gates in a circuit are linked. Often it is the case that one gate has an output that serves as the input for one or more gates within the circuit. The use of logical gates and combinatorial circuits play a fundamental role in how computers operate. An example of a combinatorial circuit is shown below. Notice that in this circuit the lower input serves as the input for the NOT gate as well as the second input of the XNOR gate. An example of a combinatorial circuit. Use the applet eecs.oregonstate.edu/~quinn/education/JavaApplets/applets/LogicGates.html to help you understand more effectively the ideas behind combinatorial circuits. 1. 2. 3. 4. Select the option labeled 2 Gates. Double click on each rectangle and select a logic gate for each one. Change the inputs to investigate the nature of the circuit. Create a different circuit by selecting different gates and investigate its nature. Notice how the truth table changes as the logic gates are changed. 5. Select the options for 3 Gates (2 inputs), 3 Gates (4 inputs), and 4 Gates, and repeat steps 2 – 4. Critical Thinking: Answer the following questions using the applet whenever helpful. Q4: Construct a truth table for the combinatorial circuit drawn above. 5 Q5: For each of the circuits below, identify the pair(s) of input combinations that produce an output of 1. Q6: Find a combination of gates (using only AND, NAND, OR and NOR gates—but not necessarily all of them) that will make the following circuit equivalent to the XOR gate. ? ? ? ? 6 Challenge Question (Optional): Create a combinatorial circuit with three inputs (I1, I2, and I3) and two outputs (O1 and O2) that has as its truth table the one shown below. For what purpose could this circuit be used? I1 0 0 0 0 1 1 1 1 I2 0 0 1 1 0 0 1 1 I3 O1 O2 0 0 0 0 0 1 0 0 1 1 1 0 0 0 1 1 1 0 0 1 0 1 1 1 7 8 CS 160 Computer Science Orientation Name ________________________ Section Meeting Time____________ Binary Number System Worksheet Digital computers store information in the form of 0s and 1s. An important use of digital computers is performing computations. That means we must be able to store numbers as patterns of 0s and 1s. The binary number system is a way of representing integers using only 0s and 1s. Before we explore the binary system, it will be helpful to review the number system we are more familiar with—the decimal system. The decimal, or base-ten, number system uses ten digits—0 through 9. The position of each digit in a number is associated with a specific power of ten. The rightmost position in a decimal representation has a value of 100 (or 1), followed on the left by 101 (or 10), then 102 (or 100), then 103 (or 1000), and so on. For the number represented in decimal form by 247, the digit 7 is associated with 100, the 4 with 101, and the 2 with 102. The value of the representation (e.g., 247) can be found by multiplying each digit by its position’s value: the representation 247 has the value (2·102 + 7·101 + 4·100). 2 4 7 = 2·100 + 4·10 + 7·1 2 1 10 10 100 The value of the decimal representation 247. The binary number system has a structure similar to the decimal system. In the binary, or base-two, system there are only 2 digits—0 and 1. The position of each bit (binary digit) is associated with a specific power of 2—20, 21, 22, 23, etc. (moving from right to left). For example, the decimal equivalent of the binary representation 1101 can be found by multiplying each digit by its position value: 1·23 + 1·22 + 0·21 + 1·20 = 13. Therefore, the binary number 1011 is equivalent to the decimal number 13. 1101 = 1 23 binary representation 1 22 0 21 1 = 1·8 + 1·4 + 0·2 + 1·1 = 13 20 decimal representation The binary number 1101 is equivalent to the decimal number 13. When we express a binary number verbally, it is helpful to say the digits in order from left to right. For example, as opposed to “one thousand one hundred and one,” the binary number 1101 would be pronounced “one-one-zero-one.” As a computer science major, you need to become adept with the binary number system. The following activity will help you achieve this goal. 9 Exercise: Fill in the following blanks with the binary equivalent of the decimal number given. To make sure you are on track, some of the binary representations have been filled in for you. Binary Decimal Binary Decimal Binary Decimal Binary Decimal __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ ___1101___ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 __________ __________ ___11100__ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ _ 1000111_ __________ __________ __________ __________ 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 __________ __________ __________ __________ __________ __________ __________ __________ __________ _ 1010101_ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 Critical Thinking: a. What is the binary representation of the decimal value 17? b. What is the binary representation of the decimal value 34? c. What is the binary representation of the decimal value 68? d. What is the binary representation of the decimal value 22? e. What is the binary representation of the decimal value 44? f. What is the binary representation of the decimal value 88? 10 Q1: Look for a relationship between the binary representations of 17, 34, and 68. The same relationship should hold between the binary representations of 22, 44, and 88. What is the relationship? What is the analogous rule in the decimal system? Q2: How many bits are there in the binary representation of the decimal value 100? What is the largest decimal number you can represent with that number of bits? Converting Between Binary and Decimal Representation There are two methods for converting numbers from their decimal to binary representations. One method involves successively dividing by 2. The quotient at each step will always be a whole number with a remainder of 0 or 1 (think about why this is true). If the remainder is recorded but not included in the next step of the division, the process will continue until the quotient is 0. The string of remainders (going from last to first) is the binary representation (written left to right) of the decimal number. An example is shown in the next figure. R0 R0 R1 R0 R0 R0 R1 … 68 ÷ 2 = 34 34 ÷ 2 = 17 17 ÷ 2 = 8 8÷2=4 4÷2=2 2÷2=1 1÷2=0 1 0 0 0 1 0 0 = 68 (base 10) The division method for converting decimal numbers into their binary representation is shown here. The remainders are recorded and become the digits of the binary number. This example shows 68 base ten is 1000100 base 2. An alternative method of finding the binary equivalent of a decimal number makes use of one’s knowledge of the powers of 2—2, 4, 8, 16, 32, 64, 128, etc. The procedure begins by determining the greatest power of 2 less than or equal to the decimal number being converted. For example, when converting 68 into its binary equivalent, we notice that 11 64 = 26 is the largest power of 2 less than 68. We subtract this power of 2 from the original number, and repeat the process: 68 – 64 = 4, so what is the largest power of 2 less than or equal to 4? The answer is 22 = 4. We stop when the difference between the number and the power of 2 is zero. So we have determined that 68 consists of one 64 and one 4. That means the binary representation of 68 is found by creating a string of bits that has a 1 in the places representing 64 and 4, and a 0 everywhere else: 1000100. Converting a binary number into its decimal equivalent is related to the method just described. We simply add up the powers of 2 represented by the 1s in the binary representation. For example, the number 1011001 can be converted to a decimal representation as follows: 1·26 + 0·25 + 1·24 + 1·23 + 0·22 + 0·21 + 1·20 = 64 + 16 + 8 + 1 = 89. The following exercises will help you improve your ability to convert numbers between the decimal and binary system. Critical Thinking: Answer the following questions. Q3: Convert the decimal numbers 14 and 49 into their binary equivalents using the first method described above (successive divisions by 2). Q4: Convert the decimal numbers 19 and 57 into their binary equivalents using the second method described above (greatest power of 2 less than or equal to the number). 12 Q5: Using your favorite method, convert the decimal numbers 161 and 585 into their binary equivalents. Q6: Convert each of the following binary numbers into their decimal equivalent. 1011000110 1100101101110 13 10000001111 Challenge Question: Professor I. M. Smart has invented a new kind of computer storage device in which fundamental unit of information is a trit (trinary digit). A trit takes on one of the three values 0, 1, or 2. In your own words explain how to convert a number from its trinary representation to its decimal representation. Then explain how to convert a decimal number to its trinary equivalent. 14 CS 160 Computer Science Orientation Name ________________________ Section Meeting Time____________ Octal and Hexadecimal Integers Worksheet In the previous worksheet we worked with a number system that is very important to computer operation—the binary system. Another non-decimal number system that plays an important role in computer science is the hexadecimal system. This number system has 16 digits. Since there are only 10 Arabic numerals, we need additional symbols to represent the values 10, 11, 12, 13, 14, and 15. We’ll let the letter A represent the value 10, B represent the value 11, and so on, through F, which will represent the value 15. An example of a hexadecimal number is 4C7. To compute the decimal value of this number, it is important to know that each place value is associated with a power of 16 in the same way the powers of ten are associated with the decimal system and the powers of two are associated with the binary system. So 4C7 is equivalent to the following decimal number: 4·162 + C·161 + 7·160 = 4·256 + 12·16 + 7·1 = 1024 + 192 + 70 = 1223. Another number system that has application in the field of computer science is the octal number system. This system has 8 digits—0, 1, 2, 3, 4, 5, 6, and 7. As its positional values, the octal system uses the powers of 8 (but you may have guessed that by now). Then 452 in the octal number system is equivalent to the decimal number: 4·82 + 5·81 + 2·80 = 4·64 + 5·8 + 2·1 = 256 + 40 + 2 = 298. The next activity will make you more familiar with hexadecimal and octal numbers. 15 Exercise: Fill in the blanks with the appropriate hexadecimal and octal integers. Decimal 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 Octal __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ ____26 ___ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ ___ 62_ ___ Hex. __________ __________ __________ __________ __________ __________ __________ __________ __________ ____A_____ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __ _16_ __ __________ __________ __________ __________ __________ __________ __________ __ __ ____ __________ __________ ___21____ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ 16 Decimal 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 Octal __________ __________ __________ __________ __________ __________ ____71____ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __ 117 _ _ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ Hex. __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ ____3D____ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ ____59____ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ Hexadecimal and Octal Conversions To convert an octal number to its hexadecimal equivalent, first convert the number to its binary form and then put the bits into groups of four, working from right to left. Once the bits are in groups of four, change each group into the appropriate hexadecimal digit. A detailed illustration of the process follows. Suppose we want to convert the octal number 736 to its hexadecimal equivalent. First we’ll convert this number to its binary equivalent. The simplest way to do this is to write each digit (i.e. 7, 3, and 6) in a binary form consisting of exactly three bits. If a digit requires less than three bits (the digits 0 through 3), one or more leading zeros are placed at the beginning of the string—000, 001, 010, and 011. All the other octal digits (4 – 7) have a binary representation that requires exactly three bits, and no leading zeros are required. In our example, the digit 7 is equivalent to the binary 111, 3 is equivalent to the binary 011, and 6 is equivalent to the binary 110. The binary representation of the octal 736 is found by concatenating these binary strings: 111011110. Once the binary equivalent of our original octal number has been obtained, we’ll use the fact that 16 is also a power of 2. The binary string representing the octal integer is arranged, from right to left into groups of four bits (because 16 = 24) with leading zeros added to the leftmost group if necessary. Thus 111011110 is arranged in this manner: 0001 1101 1110. To find the hexadecimal equivalent of the original octal number, we write down the hexadecimal digit corresponding to each group of four bits—0001 is equivalent to the hexadecimal 1, 1101 is equivalent to D, and 1110 is equivalent to E. Therefore the hexadecimal integer 1DE is equivalent to the octal number 736. To convert a number from its hexadecimal form to its octal equivalent, we reverse the process. Convert the hexadecimal number to its binary form (by writing the hexadecimal digits as binary strings that are four bits long), regroup the bits (this time into groups of three), and write the octal digits that correspond to these strings of size three. For example, to find the octal equivalent of the hexadecimal number 3C8E, write the binary equivalents (made up of four bits each) of the digits 3, C, 8, and E: 3 is written 0011, C is written 1100, 8 is written 1000, and E is written 1110. Thus the hexadecimal number 3C8E has the binary form 0011110010001110. Notice it’s much easier to write long strings of bits in hexadecimal form! Now we regroup the bits, starting at the right, into strings of length three: 011 110 010 001 110. Each of these strings of three bits represents an octal digit: 011 becomes 3, 110 becomes 6, 010 becomes 2, 001 becomes 1, and 110 becomes 6. We have just converted the hexadecimal number 3C8E into its octal equivalent: 36216. 17 Critical Thinking: Answer the following questions. Q1: Convert the following hexadecimal integers to their equivalents in the octal number system: CFE, 12D9, and 1F39A. Q2: Convert the following octal integers to their equivalents in the hexadecimal number system: 6655, 7426, and 175264. Hexadecimal and Decimal Conversions Although the procedure for converting numbers between the hexadecimal and decimal number systems is not as elegant as converting between octal and hexadecimal, it should have a familiar feel: to convert a decimal number to its hexadecimal equivalent, first find the decimal number’s binary form, then arrange the bits from right to left into groups of four. For example, to find the hexadecimal equivalent of the decimal 269, we first need to obtain the binary form of 269. Using either of the methods present in the last worksheet, it can be shown that the binary equivalent of 269 is 100001101. Regrouping this binary form into strings of 4 bits (and adding three leading zeros) shows that the decimal 269 can be written as 0001 0000 1101. These three strings correspond to the hexadecimal 1, 0, and D, respectively. Therefore the hexadecimal equivalent of the decimal 269 is 10D. To find the decimal equivalent of the hexadecimal integer 82C, we’ll first write this number in a binary form: 1000 0010 1100 = 100000101100. Next, we add appropriate powers of two to obtain the result: 1·211 + 1·25 + 1·23 + 1·22 = 2048 + 32 + 8 + 4 = 2092. Therefore the hexadecimal number 82C has an equivalent decimal form of 2092. 18 Critical Thinking: Answer the following questions. Q3: Convert the following hexadecimal integers to their decimal equivalents: 20A, F39, and 1552. Q4: Convert the following decimal integers to their equivalents in the hexadecimal number system: 161, 2576, and 4095. Q5: In your own words describe a procedure that one could use to convert an octal integer into an equivalent decimal integer. Q6: In your own words describe a procedure that one could use to convert a decimal number into an equivalent one in the octal system. 19 Q7: Using the method you described in Q5, convert the following octal numbers into their decimal equivalents: 335, 552, and 2156. Q8: Using the method you described in Q6, convert the following decimal numbers into their octal equivalents: 171, 888, and 1945. Challenge Question: What is the largest integer that can be represented by… a. two bits? b. two octal digits? c. two decimal digits? d. two hexadecimal digits? e. two digits from a base-k number system? 20 CS 160 Computer Science Orientation Name ________________________ Section Meeting Time____________ Two’s Complement Worksheet Up to this point we have seen how it is possible to represent whole numbers (e.g. 0, 1, 2, 3, …) using bits. But can one represent the integers (i.e., the counting numbers and their negative counterparts) using the binary system? The answer to this question is yes. The most common system for representing the integers is called two’s complement notation. Although most computers today use a 32 bits to represent the integers, learning the two’s complement system with that many bits would be too cumbersome. Therefore, we will study the features of the two’s complement system using only 6 bits. As we shall soon see, using a 6-bit pattern, we are able to represent all the integers from -32 to 31. It is important to note that the 6-bit pattern and the 32-bit pattern are identical in structure, so studying the 6-bit pattern does have merit. The 32-bit pattern is used in actual computers so that more integers (from -2,147,483,648 to 2,147,483,647) can be represented. The table below contains some of the representations of the integers in a 6-bit two’s complement system. Value Represented Bit Pattern 31 011111 30 011110 … … 5 000101 4 000100 3 000011 2 000010 1 000001 0 000000 –1 111111 –2 111110 –3 111101 –4 111100 –5 111011 … … –30 100010 –31 100001 –32 100000 In any two’s complement system, the leftmost bit represents the sign (+ or –) of the integer. For this reason, it is called the sign bit. The integer is negative when the sign bit is 1; the integer is either zero or positive when the sign bit is 0. To represent the positive integers leading zeros are added as needed to the left of the normal binary representation of the integer. For example the number 23, which has a binary representation 10111, is represented as 010111 in a 6-bit two’s complement system. 21 The easiest way to find the representation of a negative integer is to begin with its positive counterpart. For example, to find the two’s complement representation for the integer –23, we start with the two’s complement representation of 23: 010111. At this point there are two methods that can be used to determine the 6-bit two’s complement representation of –23. Both methods are described below. The first method is referred to as the ‘copy and complement’ method. Beginning at the right, the bits of the positive integer are copied up to and including the first 1 bit. In our example, since the rightmost digit is 1, it is the only digit copied: __ __ __ __ __ _1_. The remaining digits of the negative integer are the complement of the remaining bits of the positive integer. The complement of a pattern is found by changing all 0s to 1s and all 1s to 0s. Thus, the complement of the remaining portion of 23 is 1 0 1 0 0 1, and the integer –23 is written 101001 in two’s complement notation Another method to find the negation of a number in two’s complement notation could be named the ‘complement and add 1’ method. We obtain the complement of the original number and add one to it. Using the same example of 23, we begin by determining the complement of 010111, which is 101000. Adding 1 to this complement yields the same result as before: 101001. Notice that regardless of what method is used, a positive integer—which always begins with 0—will always have an opposite or negation that begins with a 1—and is therefore negative. Exercise: Using either of the methods described above, find the negation of each of the following numbers, represented in 6-bit two’s complement notation: 001110 negation: ____________________________________ 010010 negation: ____________________________________ 110011 negation: ____________________________________ 000000 negation: ____________________________________ Critical Thinking: Answer the following question. Q1: In your own words and based on evidence, explain what happens when you negate the negation of a number in two’s complement notation. 22 The ability to negate a number written in two’s complement form is crucial in finding the value of any number written in a two’s complement system—for example what integer does the number 110110 represent? The first task is to determine whether the integer is positive or negative. This factor depends on the sign bit—the first bit of the string. If the first bit is 0, finding the value of the number amounts to converting the number—which is basically in binary form—into its decimal equivalent. If however the number’s sign bit is 1 (and therefore the number is negative), treating the number as if it is part of the binary system will not work. Instead, we need to find the magnitude of the number and then place a negative sign in front of this magnitude. And what is the magnitude of a negative number? It is that number’s negation. In our example, the negation of 110110 is 001010 and this number corresponds to the binary number 1010. This binary number has a decimal equivalent of 10, which means 110110 is equivalent to the negation of 10, or –10. Exercise: Convert these 6-bit, two’s complement integers into decimal form. 101000 ______________________________________________ 110011 ______________________________________________ 111111 ______________________________________________ Exercise: Convert these decimal integers to 6-bit, two’s complement form. 14 ______________________________________________ –27 ______________________________________________ –11 ______________________________________________ Binary and Two’s Complement Addition One method of verifying that 101001 represents –23 is to add this string to the two’s complement representation of 23 and check the result. If the sum is 0, which is the sum of 23 and –23, then we can be sure that 101001 is indeed the representation of –23. Adding numbers written in two’s complement notation is equivalent to adding numbers in the binary system. To add two binary numbers, one must be familiar with some basic base-2 addition facts, much like we must understand basic base-10 addition facts. Fortunately, there are only a few facts to know! 0+0=0 0+1=1 1+0=1 1 + 1 = 10 23 10 + 1 = 11 The base-10 addition practice of ‘carrying a leftover’ is used when adding binary numbers. Thus if the binary number 110 is added to the number 111, the result is 1101 as expected and is described below. 1 1 110 110 + 111 01 +1 1 1 1 1101 The 1 and 1 in the middle column add to 10 and the 1 (from 10) is carried to the leftmost column. The 1 and 1 in the leftmost column, combined with the 1 carried from the middle column, add to 11. The leading 1 (from 11) is carried once again. This method is analogous to addition in the decimal system. However, when adding numbers in two’s complement notation, a change in the familiar method is necessary since the number of bits of the sum must be equal to the number of bits in each of the addends. In other words, when two numbers that are represented in a 6-bit two’s complement system are added together, the result must also have exactly six bits. What happens when there is an extra 1 that, according to our decimal rule, should be put into a seventh bit position? The answer is that it is simply discarded. (This may be counterintuitive, but it works!) Using our knowledge of addition in the two’s complement system, we can now compute the sum of 101001 and 010111, which as shown below does equal 0 (or in 6-bit two’s complement notation 000000). This carried is discarded. 1 1 1 1 1 + 1 101001 010111 000000 The sum of 101001 and 010111 is 00000, which means they are negations of each other. One of the powerful features of a two’s complement system is that it contains the structure to not only perform additions, but also subtractions. At the foundation of this process is the fact that any subtraction problem can be rewritten as an addition problem [for example, 12 – 8 = 12 + (–8)]. So if a computer has the ability to perform addition (i.e., if it has an adder) it will also be able to perform subtraction. The subtraction problem 12 – 8 could be performed by writing the integers 12 and –8 in two’s complement notation and then adding them. This addition is shown below. Note that while we could have used a 6-bit (or 32-bit) system, a simpler 5-bit system is sufficient, 24 since the positive integer is less than or equal to 15 and the negative integer is greater than or equal to 16. This 1 is discarded. 1 + 1 01100 11000 00100 A model of the subtraction problem 12 – 8 using two’s complement notation. The number 01100 represents 12 and 11000 represents –8. The expected result 4 will is evident after the leftmost ‘carried 1’ (created by the addition of 1 and 1 in the leftmost column) is discarded. Exercise: Using a 6-bit two’s complement system, perform the following addition and subtraction problems: 23 + 7 –13 – 18 28 – 19 16 + 20 Did you run into a problem with the last sum (16 + 20)? If you did everything correctly, your sum should have been 100100. But this doesn’t make sense, since the sum of two positive numbers is positive. The integer 100100 begins with a one and is therefore negative. The solution to this conflict has to do with a phenomenon called overflow. This problem is due to the fact that the integer 36, which is the true sum of 16 and 20, cannot be represented in a 6-bit system. Whenever the sum of two numbers cannot be represented in the current two’s complement system, overflow occurs. The problem of overflow is serious because, unless the computer reports the error, the computation could return an erroneous result. 25 An applet that will help you become more familiar with the concept of overflow is found at eecs.oregonstate.edu/~quinn/education/JavaApplets/applets/TwosComplement.html. 1. Make sure the option Demo Mode is selected. 2. Click on the individual bits to change them from 1 to 0 and 0 to 1. 3. See if you can get the applet to achieve overflow. The circle above the word Overflow will turn red when overflow occurs. 4. By clicking on the arrow to the right of number 4 you can change the number of bits in the system to 6 or 8. Critical Thinking: Answer the following questions, using the applet whenever helpful. Assume we are using 6 bits to represent two’s complement integers. Q2: Under what conditions is the overflow bit set (i.e. what types of integers being added together will cause overflow to occur? Q3: What is the fewest number of bits set to 1 that causes overflow? Q4: What is the most number of bits set to 1 that doesn’t cause overflow? Challenge Question: Of the two techniques for negating integers described in this worksheet (‘copy and complement’ and ‘complement and add one’), which do you think would be easiest to incorporate into a computer’s circuitry? Explain your answer. 26 CS 160 Computer Science Orientation Name ________________________ Section Meeting Time____________ Machine Language Worksheet In this worksheet we will explore how the major components of a computer work together to perform simple tasks. We begin with the architecture of an extremely primitive computer, which is a slight modification of the computer described in the ninth edition of J. Glenn Brookshear’s Computer Science: An Overview, Addison-Wesley (2007). The central processing unit (CPU) is the circuitry that controls how information (in the form of bit patterns) is manipulated and stored. The CPU is made up of two parts—the logic/arithmetic unit and the control unit. The logic/arithmetic unit has the ability to perform simple mathematical calculations in addition to other operations. The control unit contains many individual cells, called registers, of which there are two types: general-purpose registers and special-purpose registers. The function of general-purpose registers is to temporarily store bit patterns that will be manipulated by the logic/arithmetic unit. There are two special-purpose registers: the instruction register and the program counter. The instruction register contains the instruction the control unit is currently carrying out. The program counter contains the address of the next instruction to be performed. Another critical part of computer architecture is the main memory. The main memory consists of cells. Each cell has a unique address and contains 8 bits. These bits may correspond to a program instruction or to a data value. Bit patterns are transferred to and from the main memory to the CPU through a series of wires called a bus. A Sample Machine We will discuss how the CPU and main memory of a simple computer work together to perform basic functions. Our machine has 16 general-purpose registers, numbered in hexadecimal notation from 0 to F. Main memory consists of 256 cells— each having as its address a hexadecimal number between 00 and FF, as shown in the diagram below. Computer programmers typically write programs in a high-level language, but in order for computer programs to be executed on a computer, they must be translated into low-level instructions called machine language. We will deal with the operations of our computer at the machine language level. In our simple computer, a machine instruction is a sequence of 16 bits that directs the CPU to perform a particular task. An example of a machine instruction may be 0010 1101 0011 1100. We’ll use hexadecimal notation to simplify the reading and writing of these instructions. In this system the example instruction above would be written 2D3C. We’ll now explain what purpose each of these four digits has in the machine language. 27 CPU Register 0 Program Counter Main Memory Bus 1 2 3 Instruction Register Memory Cells Addres Address 00 01 02 03 04 05 FF FE F The first hexadecimal digit (2 in our example) in the machine instruction is called the operation code, or op-code, for short. The digit in the op-code field indicates which basic function is to be performed. These basic functions can be classified into three categories: data relocation, arithmetic/logic, and control. We will discuss selected functions from each of these categories. An important data relocation function is LOAD. There are two types of LOAD function. One of the LOAD instructions copies a value from the main memory and puts it into a specified general-purpose register. The other kind of LOAD function — distinguished from the previous LOAD function by a different op-code (see below) — puts a constant value into a specified general-purpose register. Another data relocation function, MOVE, copies a value from one register and puts it into another register. The STORE function copies a value from a register and writes it into a specific location in the main memory. There are two ADD functions in the computer’s arithmetic/logic repertoire. One ADD function adds integers represented in two’s complement notation. The other ADD function adds two floating-point numbers. There are also logical functions that the CPU can perform on bit patterns. For example, the NOT function returns the complement of the number that is given to it. Other examples of logical functions are described below. One of the control functions the computer utilizes is the JUMP function. In the normal sequence of events, the computer executes instructions in order; the program counter advances from 0 to 2 to 4, and so on. The JUMP instruction may set the program counter to an arbitrary address. 28 Opcode Operand 1 RXY 2 RXY 3 RXY 4 0RS 5 RST 6 RST 7 RST 8 RST 9 RST A R0X B RXY C 000 D RXY E 0RS Description of Function Example 17D2 loads bit pattern found in memory cell D2 into register 7. LOAD the bit pattern XY into 2D3C loads the bit pattern register with address R. 00111100 into register D. STORE the bit pattern found in 3A03 stores the bit pattern in the register R in memory cell register A into memory cell XY. 03. MOVE (copy) bit pattern from 403F copies bit pattern in register R to register S. register 3 into register F. ADD integers in registers S and 5B64 adds integers in T (two’s complement form) and registers 6 and 4; puts result in place result in register R. register B. ADD floating-point numbers in 6AE1 adds the floating point registers S and T and place result numbers in registers E and 1; in register R. puts result in register A. Combine bit pattern in register S 7D29 combines bit patterns and register T using the OR found in register 2 and 9 using operator and place result in the OR operator and places register R. the result in register D. Combine bit pattern in register S 81F2 combines bit patterns and register T using the AND found in register F and 2 using operator and place result in the AND operator and places register R. the result in register 1. Combine bit pattern in register S 94C3 combines bit patterns and register T using the found in register C and 3 EXCLUSIVE OR operator and using the XOR operator and place result in register R. places the result in register 4. ROTATE bit pattern in register R A907 rotates bit pattern in to the right X times. register 9 to the right 7 times. BDA7 compares bit pattern in JUMP to instruction located in register D with that in register memory cell XY IF the bit 0. If they are equal, program pattern in register R is EQUAL to jumps to instruction located in bit pattern in register 0; otherwise cell A7; if they are not, the continue to next instruction. program moves to the next instruction. HALT the program’s execution. C000 causes program stop. D18D skips to instruction JUMP to instruction XY IF the located in cell 8D if bit pattern bit pattern in register R is LESS in register 1 begins with 1; THAN ZERO; otherwise otherwise, the program moves continue to next instruction. to the next instruction. NOT—negate each bit in pattern E03C finds complement of bit found in register S and put pattern in register C and puts resulting bit pattern in register R. the result in register 3. LOAD bit pattern found in memory cell XY into register R. 29 One JUMP occurs when the value in specified register is equal to the value in register 0. The second JUMP occurs when the value in a specified register is less than zero. Another control instruction is the HALT command, which causes the execution of the program to stop. For our purposes, each of these (and several other) commands has been assigned a specific hexadecimal digit, as shown in the Op-Code portion in the above table. Next to each op-code in the table is a description of the operand that provides details necessary for the instruction to be carried out. In our example instruction (2D3C), the first hexadecimal digit 2 indicates that the machine is to load a particular bit pattern. The other hexadecimal digits (D3C) in the instruction are known as the operand. The letter D in this case represents the address of the register into which the bit pattern will be loaded. The 3C signifies that the bit pattern to be loaded is 00111100. Taken together, the instruction 2D3C indicates the machine will load the bit pattern 00111100 into the general-purpose register with address D. Another example of an instruction is 87F2. The op-code 8 instructs the machine to combine two bit-patterns into one using the AND function. The hexadecimal digits F and 2, tell us to combine the bit patterns stored in those two registers and place them in register 7. For the next few paragraphs we will assume that bit pattern 01101110 is in register F and the bit pattern 11110011 is in register 2. To combine these two patterns into one using the AND operation, we first stack one bit pattern on top of the other so each of the eight digits is aligned: 0 1 1 0 1 1 1 0 1 1 1 1 0 0 1 1 Next we’ll start at the right (though this is arbitrary) and, according to rules of the AND operation, place a one under this column of numbers if they are both one, and a zero if they aren’t both one. The same procedure is applied to each of the remaining seven columns of numbers, and the result is represented below: AND 0 1 1 0 1 1 1 0 1 1 1 1 0 0 1 1 0 1 1 0 0 0 1 0 The instruction 87F2 would combine 0110110 and 11110011 (which are the bit patterns in registers F and 2, respectively) using the AND operation and put the resulting bit pattern, 01100010, into register 7. The combining of two bit patterns into one can also be performed by other logical operators—OR, XOR, etc. You may want to verify that the result of combining the bit patterns in registers F and 2 using the OR operation will yield the bit pattern 11111111, while the resulting bit pattern using the XOR operation is 10011101. The NOT operator requires only one bit pattern and simply negates each of the bits in the pattern. For example, the instruction E07F will place the bit pattern 10010001 in register 7, which you may also want to verify. 30 The ROTATE function rotates the bits in a bit pattern a certain number of positions, in a circular fashion. For example, the instruction AF04 will rotate the bit pattern in register F four positions to the right. If the original pattern is 01101110, the resulting bit pattern is 11100110, as shown below. 0 1 1 0 1 1 1 0 original byte 1 1 1 0 0 1 1 0 rotated byte In order to help you become more familiar with the intricacies of machine language, we will walk through a few examples. First, let’s add two values that are stored in the main memory and place the sum back in the main memory. Suppose 02 and 08 are the values of the bytes stored in the main memory cells with addresses F0 and F1, respectively. Our objective is to write a machine language program that adds these two values and places their sum in the main memory cell with address F2. The steps that we will need the computer to perform in order to realize this objective are outlined below: 1. 2. 3. 4. 5. Load the value of the memory cell with address F0 into register 0. Load the value of the memory cell with address F1 into register 1. Add the values in register 0 and register 1 and place the result in register 2. Store the value of register 2 into the memory cell with address F2. Halt the program. For each of the steps above, there is an associated two-byte instruction written in machine language that the CPU can use. These instructions are: 10F0 11F1 5201 32F2 C000 We will now represent the changes that occur in the main memory and CPU as these instructions are carried out at the machine level. Notice that the machine language instructions are stored in the main memory in cells 00 to 09. As the CPU progresses through the five instructions, watch the program counter, the instruction register, the effected registers and main memory cells change accordingly. 31 32 Main Memory CPU Registers 02 0 08 1 A0 2 3 Address Program Counter Bus 0A Instruction Register Address 00 10 F0 01 02 11 F1 03 04 52 01 05 06 32 F2 07 08 C0 00 09 F0 02 08 F1 F2 A0 C000 F 33 F3 There are a couple of items that should be noted. Since each instruction is two bytes long, each line of instruction takes up two memory cells; consequently, the program counter only identifies the first address of the cell containing the line of instruction. This address is always an even number Also, due to space constraints, only those cells that are necessary for the program are shown in the graphical representations. This convention will be used in the following exercises. It’s now time for you to try your hand at machine language. Question 1: Based on the following representations of the CPU and main memory, describe in your own words what the following machine instruction program accomplishes. Include in your description the reasons for any changes in general register or main memory cells. 34 35 Main Memory CPU Registers Address 00 17 B1 01 02 28 C4 03 04 99 78 05 06 39 B2 07 08 C0 00 09 C1 9 B0 89 05 B1 - - A B2 C1 -- B3 - - 0 Address Program Counter Bus 0A 05 7 Instruction Register C4 8 C0 0 0 Question 2: Below you will find a list of 8 instructions. Assume that memory cell D2 contains the bit pattern 00001011 and memory cell F0 contains the bit pattern 00011010. a) Translate each of these instructions into machine language. b) Draw the relevant memory locations and CPU registers. Put the initial binary bit patterns in memory locations D2 and F0. The program places values in memory location E3 and registers 0 through 5. Write in each cell the binary bit pattern written by the program. c) To what simple arithmetic problem does this list of instructions provide an answer? 1. 2. 3. 4. 5. 6. 7. 8. Load the contents of memory cell D2 into register 0. Load bit pattern 00000001 into register 1. Load the contents of memory cell F0 into register 2. Compute the logical NOT the bit pattern in register 0 and place the result in register 3. Add the values in register 1 and register 3 as if they were integers written in two’s complement form and place the result register 4. Add the values in register 2 and register 4 as if they were integers written in two’s complement form and place the result in register 5. Store the bit pattern in register 5 in memory cell with address E3. Stop. 36 In our final example we will illustrate the characteristic of one of the two JUMP functions in our arsenal. We’ll begin by assuming that the value of register 0 is known to have the bit pattern 06 and the value of register 5 is 0. Also, for the time being, we’ll assume the value of the memory cell with address C3 is unknown. We’d like to subtract the value of C2 from 06, the value in register 0, and place the result in the memory cell C3. If the value of C2 is also 06, the difference is clearly 0, and in this case we don’t want to have to run the rest of the program. First we will create a list of instructions that will accomplish the objective. They are as follows: 1. Load the contents of memory cell C2 into register 1. 2. JUMP to the last line of instruction if the value of C2 is equal to the value in register 0; otherwise, move on to step three. 3. Load the value 00000001 into register 2. 4. Compute the logical NOT of the bit pattern in register 1 and place the result into register 3. 5. Add the integers in registers 2 and 3 as if they were in two’s complement form and place the result in register 4. 6. Add the integers in registers 0 and 4 as if they were in two’s complement form and place the result in register 5. 7. Store the value of register 5 in the memory cell with address C3. 8. End the program. We’ll assume that these 8 machine instructions are located in the memory cells with addresses 00 through 0F. Here is the resulting machine language program. 1 1 C 2 37 B 2 E 5 5 3 C 1 2 0 4 5 5 0 D 0 3 2 0 C 0 C 1 1 3 4 3 0 Question 3: Write a machine language program that takes a value from a memory cell, say 2C, computes its absolute value, and then stores its absolute value in memory cell 2D. An efficient way to do this is to use the JUMP IF LESS THAN ZERO operation. Be sure to halt the program and to include the following in your solution: 1. A list of instructions using written English descriptions. 2. A list of the instructions translated into machine language. 38 CS 160 Computer Science Orientation Name ________________________ Section Meeting Time____________ Problem-Solving By Enumeration Worksheet The ability to solve problems is important in many disciplines, including computer programming. Computer programmers seek to obtain effective and efficient to perform desired tasks. Let’s elaborate on three important terms in the previous sentence: algorithm, effective, and efficient. “An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process” (Brookshear, Computer Science: An Overview, 2007). An algorithm is effective if it finds the solution (or a close enough approximation). An algorithm is efficient if the number of steps it performs is reasonable. In this worksheet and the ones to follow, we will explore several important problem-solving strategies. It should be noted that there is no single set of rules that, if followed, always leads to an effective and efficient algorithm. In fact, problem solving relies so much on creativity and ingenuity that some people regard computer programming as an art form! Looking for better algorithms can be a lot of fun. With this in mind, think of the following worksheets as an opportunity to develop your own creative problem-solving techniques. The first technique we are going to explore is called enumeration. Some types of problems have many possible solutions that are partially correct, but only one that satisfies all the criteria outlined. Enumeration means listing each of the possibilities and then systematically eliminating those that don’t fit the requirements set out in the statement of the problem. To illustrate problem-solving by enumeration , let’s look at an example. Boronoff, Pavlow, Revistsky, and Sukarek are individuals who work in different fields of the arts and humanities: one is a painter, one is a writer, one a dancer, and one is a singer. Using the information below, determine the profession of each of these four people. 1. Boronoff and Revitsky were both in the audience the night the singer made his debut on the concert stage. 2. The writer’s biography of Sukarek was a best seller. He also wants to write Boronoff’s biography, but hasn’t met him yet. 3. Both Pavlow and the writer have sat for portraits by the painter. 4. Boronoff has never heard of Revitsky. 39 To solve this problem by the enumeration technique, we will begin by listing all possible combinations of the individuals and their professions: Dancer Singer Painter Writer Dancer Singer Painter Writer Boronoff Boronoff Boronoff Boronoff Boronoff Boronoff Pavlow Pavlow Pavlow Pavlow Pavlow Pavlow Revitsky Sukarek Revitsky Pavlow Pavlow Sukarek Revitsky Sukarek Revitsky Boronoff Boronoff Sukarek Revitsky Revitsky Revitsky Revitsky Revitsky Revitsky Sukarek Sukarek Sukarek Sukarek Sukarek Sukarek Boronoff Sukarek Boronoff Pavlow Pavlow Sukarek Boronoff Revitsky Boronoff Pavlow Pavlow Revitsky Pavlow Pavlow Sukarek Sukarek Revitsky Revitsky Boronoff Boronoff Sukarek Sukarek Revitsky Revitsky Sukarek Revitsky Pavlow Revitsky Sukarek Pavlow Sukarek Revitsky Boronoff Revitsky Sukarek Boronoff Pavlow Pavlow Sukarek Sukarek Boronoff Boronoff Pavlow Pavlow Revitsky Revitsky Boronoff Boronoff Sukarek Boronoff Pavlow Boronoff Sukarek Pavlow Revitsky Boronoff Pavlow Boronoff Revitsky Pavlow Now we’ll use the clues to eliminate those possibilities that do not meet the first criterion. Since Boronoff and Revitsky were both in the audience when the singer was on stage, neither of them can be the singer. Therefore we will cross out (i.e. eliminate) each row in which either Boronoff or Revitsky is the singer, as shown below. Dancer Singer Painter Writer Dancer Singer Painter Writer Boronoff Boronoff Boronoff Boronoff Boronoff Boronoff Pavlow Pavlow Pavlow Pavlow Pavlow Pavlow Revitsky Sukarek Revitsky Pavlow Pavlow Sukarek Revitsky Sukarek Revitsky Boronoff Boronoff Sukarek Revitsky Revitsky Revitsky Revitsky Revitsky Revitsky Sukarek Sukarek Sukarek Sukarek Sukarek Sukarek Boronoff Sukarek Boronoff Pavlow Pavlow Sukarek Boronoff Revitsky Boronoff Pavlow Pavlow Revitsky Pavlow Pavlow Sukarek Sukarek Revitsky Revitsky Boronoff Boronoff Sukarek Sukarek Revitsky Revitsky Sukarek Revitsky Pavlow Revitsky Sukarek Pavlow Sukarek Revitsky Boronoff Revitsky Sukarek Boronoff 40 Pavlow Pavlow Sukarek Sukarek Boronoff Boronoff Pavlow Pavlow Revitsky Revitsky Boronoff Boronoff Sukarek Boronoff Pavlow Boronoff Sukarek Pavlow Revitsky Boronoff Pavlow Boronoff Revitsky Pavlow The second piece of information lets us know that the writer must be someone other than Boronoff or Sukarek. Therefore we will eliminate any row in which Boronoff or Sukarek is the writer, as shown below. Dancer Singer Painter Writer Dancer Singer Painter Writer Boronoff Boronoff Boronoff Boronoff Boronoff Boronoff Pavlow Pavlow Pavlow Pavlow Pavlow Pavlow Revitsky Sukarek Revitsky Pavlow Pavlow Sukarek Revitsky Sukarek Revitsky Boronoff Boronoff Sukarek Revitsky Revitsky Revitsky Revitsky Revitsky Revitsky Sukarek Sukarek Sukarek Sukarek Sukarek Sukarek Boronoff Sukarek Boronoff Pavlow Pavlow Sukarek Boronoff Revitsky Boronoff Pavlow Pavlow Revitsky Pavlow Pavlow Sukarek Sukarek Revitsky Revitsky Boronoff Boronoff Sukarek Sukarek Revitsky Revitsky Sukarek Revitsky Pavlow Revitsky Sukarek Pavlow Sukarek Revitsky Boronoff Revitsky Sukarek Boronoff Pavlow Pavlow Sukarek Sukarek Boronoff Boronoff Pavlow Pavlow Revitsky Revitsky Boronoff Boronoff Sukarek Boronoff Pavlow Boronoff Sukarek Pavlow Revitsky Boronoff Pavlow Boronoff Revitsky Pavlow The third sentence informs us that Pavlow is neither the writer nor the painter and we will eliminate the rows in which Pavlow is the writer or the painter, as shown below. Dancer Singer Painter Writer Dancer Singer Painter Writer Boronoff Boronoff Boronoff Boronoff Boronoff Boronoff Pavlow Pavlow Pavlow Pavlow Pavlow Pavlow Revitsky Sukarek Revitsky Pavlow Pavlow Sukarek Revitsky Sukarek Revitsky Boronoff Boronoff Sukarek Revitsky Revitsky Revitsky Revitsky Revitsky Revitsky Sukarek Sukarek Sukarek Sukarek Sukarek Sukarek Boronoff Sukarek Boronoff Pavlow Pavlow Sukarek Boronoff Revitsky Boronoff Pavlow Pavlow Revitsky Pavlow Pavlow Sukarek Sukarek Revitsky Revitsky Boronoff Boronoff Sukarek Sukarek Revitsky Revitsky Sukarek Revitsky Pavlow Revitsky Sukarek Pavlow Sukarek Revitsky Boronoff Revitsky Sukarek Boronoff Pavlow Pavlow Sukarek Sukarek Boronoff Boronoff Pavlow Pavlow Revitsky Revitsky Boronoff Boronoff Sukarek Boronoff Pavlow Boronoff Sukarek Pavlow Revitsky Boronoff Pavlow Boronoff Revitsky Pavlow After these three rows have been eliminated, we are left with only two rows: one in which Boronoff is the dancer and Revitsky is the writer (option A), and one in which Boronoff is the painter and Revitsky is the writer (option B). It is clear that Revitsky is the writer, and, according to the third piece of information, Revitsky sat for a portrait by the painter (who is Boronoff if option B is true). The last clue, however, states that Boronoff has never heard of Revitsky, which means option B cannot be true. Thus Boronoff must be the dancer, Pavlow is the singer, Revitsky is the writer, and Sukarek is the painter. 41 Exercises: Find a solution to the following problems using the method of enumeration. P1: In a certain bank three employees—Sonya, Kirk, and Dakota—work as a teller, branch manager, and loan officer (not necessarily in that order). Use the information below to determine who fills what position at the bank. 1. The teller, who is an only child, earns the least amount of money. 2. Dakota, who married Sonya’s brother, earns more than the branch manager does. 42 P2: One weekend a reporter from a campus newspaper came by to do a feature on four college roommates—Keon, Randall, Yoshio, and Fernando, but each was out of town. The reporter asked a neighbor if he knew anything about the roommates. After thinking for a while, the neighbor replied, “One of them is a computer science major, another is an avid golfer and mows the lawn at the country club, a third has had a mohawk since 5th grade, and the last one is allergic to kiwi.” The reporter then rudely reminded the neighbor that he was writing a feature and needed a little more information than that. The annoyed neighbor replied by giving the following intentionally veiled facts: “Fernando, who is bald, usually waits for the golfer to finish his putting practice before they walk to campus together. Unlike Keon, who prefers a traditionally conservative hairstyle, Yoshio is thinking about getting a mohawk after the term. The roommate with a kiwi allergy always cooks enough food for Keon who is deathly allergic to grass pollen.” With that the neighbor bid the reporter good day and went back into his home. Based on the neighbor’s information, match up each roommate with his unique characteristic. 43 Challenge Question: Five sisters all have their birthday in a different month—February, March, June, July, and December. Using the information below, determine each of the sister's birthday months. (Adapted from Puzzlers Paradise Website, http://www.puzzlersparadise.com/article1021.html.) 1. Paula and the sister born in June have their birthdays earlier in the year than Brenda. 2. Abigail and Brenda have their birthdays in consecutive months. 3. Within a calendar year, Paula’s birthday is between Mary’s and that of the sister born in February. 44 CS 160 Computer Science Orientation Name ________________________ Section Meeting Time____________ Problem-Solving by Greedy Methods Worksheet The next problem-solving technique we will explore is called problem solving using greedy methods. The greedy method solves a problem by focusing on particular local aspects of the problem, making a choice that is best for the local sub-problem, and hoping that a series of locally optimal choices will lead to the best overall solution. For example, suppose a department store clerk owes $17.68 in change to a customer who has just made a purchase. What is the fewest number of bills and coins (assuming the clerk has at his disposal only $20, $10, $5, and $1 bills and quarters, dimes, nickels and pennies) the clerk can give to the customer to provide proper change? If we disregard the ‘fewest number of bills and coins’ phrase, there are a myriad of ways that this change-making process can be brought about. For example, the clerk could give the customer 1768 pennies, or 176 dimes and 8 pennies. However, if the greedy method is applied to this situation, the clerk will try to give the greatest monetary piece (bill or coin) at each ‘phase’ of the change-making process. First, he will place a $10 bill on the table, since that is the largest single piece that he can give without giving more than the amount owed. Next, a $5 bill will be given, followed by two $1 bills. At this point $17 of the customer’s change has been paid and bills can no longer be given. The clerk will then give two quarters, a dime, a nickel and three pennies. This solution of four bills ($10 + $5 + $1 + $1) and six coins ($0.25 + $0.25 + $0.10 + $0.01 + $0.01 + $0.01) is the optimal one—there is no way of combining less than 10 of the specified monetary pieces so that their sum is exactly $17.86. While this rather mundane example is one we are all familiar with, it is indeed an example of the greedy technique at work—the next piece of money to be given was always the largest one that could be given at the time, and this series of ‘local’ optimal decisions led to a ‘global’ optimal solution. However, there are situations—and some very similar to the one just described—in which the greedy method fails to provide the optimal global solution. For example, let’s imagine a society whose monetary system consisted of the following coins: 10-krunk, 8krunk, 5-krunk and 1-krunk. If a sales clerk is obliged to give a customer 17 krunks in change, the greedy method would yield the following solution: one 10-krunk coin, one 5krunk coin, and two 1-krunk coins. This global solution of four coins is not optimal. Rather, it is possible to provide 17 krunks with only three coins—two 8-krunk coins and one 1-krunk coin. This example illustrates the fact that the greedy method should be used with caution. 45 Exercises: Try to solve each of the following problems using the greedy method. Explain your reasoning in each case. P1: An international thief has gained entry to a high security medical laboratory and intends to steal valuable powdered chemicals from the lab. There are 8 different types of powder, and each type of powder is stored in a single large container. She plans to pour the precious powders from their containers into individual, thin plastic bags and then replace the containers. After putting the plastic bags containing the stolen powder in a brief case, the thief will attempt to bypass the lab’s security equipment again. In order to successfully escape, she has calculated that, after accounting for the weight of the briefcase and the plastic bags, she can put no more than 25 kilograms of the stolen powder into the briefcase. Below is a table listing the powders, their weight, and their value. If the thief wants to escape with the most valuable collection of powders in her briefcase, how much of each type of powder should she steal? Note: The “Weight of Powder” column indicates the total available amount of each type of powder. For example, the thief can steal up to 3.25 kg of IM2, but no more. Type of Powder Weight of Powder Value of Powder 3.25 kg 1975 £ IM2 9.50 kg 2350 £ B24 1.25 kg 3000 £ CP 7.50 kg 1100 £ D+ 8.50 kg 4800 £ Rz 8.00 kg 3600 £ Q32 0 9.50 kg 2600 £ W7 6.00 kg 4320 £ 57/J 46 P2: Chris has won a shopping spree at a gourmet donut wholesale bakery. The bakery owners have agreed to allow Chris to have 1 minute to gather as many boxes of donuts as possible, with two restrictions: all donuts must remain in their original (bulk) boxes and no more than ten pounds of donuts are to be chosen. Chris has created a list of the types of donuts the bakery sells, the weight of each box and the value per box (see table below). If Chris wants to end up with the most valuable ten pounds of donuts, which boxes of donuts should he choose? Donut Type Package Weight Value of Box Jelly Filled Donuts 2 lbs $15.00 Maple/Chocolate Bars 5 lb $86.25 Chocolate-Covered Cake Donuts 1 lbs $12.50 Custard Cream Filled Donuts 6 lbs $105.00 Cinnamon Rolls 5 lbs $85.00 Apple Fritters 3 lb $50.00 47 P3: “In regional news, Blueneck County has decided to pave some of the roads connecting the cities in their county. County officials have decided that there must be at least one paved route between every pair of cities. Due to the economic shortfalls that have swept the region, the county also wants to minimize the cost of the project.” According to the map of Blueneck County shown on the next page, which roads should county officials have paved? Appleton 10 miles Durazno 12 miles Berry Hill 18 miles 11 miles 8 miles Cider Canyon 10 miles 12 miles Elberta 10 miles 17 miles Fruit Heights Challenge Question: The greedy method was successful at determining the optional solution in two of the three problems above. Which problems were they? What characteristic distinguishes the two problems that could be optimally solved by the greedy method from the third problem, which couldn’t be optimally solved by the greedy method? 48 CS 160 Computer Science Orientation Name ________________________ Section Meeting Time____________ Divide-and-Conquer Problem-Solving Worksheet Another problem-solving method that is often utilized in computer science is called divide-and-conquer. In this paradigm, a problem is divided into smaller sub-problems, which are then solved. The solutions to these sub-problems are then combined to form a solution to the original problem. Divide-and-conquer algorithms often require fewer steps than greedy algorithms when solving the same problem. For example, suppose we want to arrange the following words in order from longest to shortest: desire to latent sometimes go misfortune awakens overwhelming on. One method that would surely work is to apply a greedy method called selection sort. In this method we seek to find the longest word in the group, and once that is accomplished, we put it in the first position. Then the second longest word is determined and put in the second position and so on. Here is how selection sort would sort the words from longest to shortest. First, it would search the list for the longest word: desire to latent sometimes go misfortune awakens overwhelming on The longest word is “overwhelming,” so it would swap “desire” and “overwhelming.” overwhelming to latent sometimes go misfortune awakens desire on In the second iteration, it would search the remainder of the list for the longest word: overwhelming to latent sometimes go misfortune awakens desire on The longest word found is “misfortune,” so it would swap “to” and “misfortune.” overwhelming misfortune latent sometimes go to awakens desire on The algorithm would continue in this fashion until the list of words is completely sorted: overwhelming misfortune sometimes awakens latent desire to go on Certainly the selection sort algorithm is effective—it sorts the words correctly—but is it efficient? To determine how efficient the selection sort algorithm is, we need to think about how many times it compared a pair of words. During the first step, the number of comparisons carried out was eight—one less than the number of words in the list. At the next step, there were seven comparisons, and six at the next, and so on. In general, if there are n objects in a list to be compared, there will be (n – 1) comparisons during the first step of the selection sort process, (n – 2) during the second step, and so on until only 49 1 comparison is made at the (n – 1)st and last step of the process. In general, if the list contains n elements, the number of comparisons being made using the selection sort algorithm is: (n – 1) + (n – 2) + · · · + 3 + 2 + 1. This type of sum occurs so frequently in computer science that we’ll briefly divert our attention to understanding its features. We would like to have a formula so that regardless of the value of n, we can always quickly find the result of adding the first n natural numbers: 1 + 2 + 3 + · · · + (n – 1) + n. When n is small, say 3, the sum is trivial: 1 + 2 + 3 = 6. But what if n is 30,000? It would be a waste of time to do 29,999 additions, even with a calculator or computer, because there is a simple formula that will do the job. One way to understand this formula is to look at a geometric argument. Suppose we have an arbitrary n by n square made up of smaller squares of side length 1. In total there would be n2 of the smaller squares—n rows and n columns (see figure below with n = 6). To model the desired sum geometrically we could fill in the first square of the first row, the first two squares of the second row, and so on until we fill in the first n squares of the nth row (i.e. we would fill in each square of the last row of squares). The area of the shaded squares is equal to 1 + 2 + 3 + · · · + (n – 1) + n, the exact sum in which we are interested. Drawing a diagonal from the top left corner to the bottom right corner will split the n by n square exactly in half, so that the area to the left of the diagonal is n2/2. But there is still some shaded area that has not been counted—the n half-shaded squares. The total area of the half-shaded squares is n/2.Thus the entire n 2 n n 2 n nn 1 area of the shaded squares is . 2 2 2 2 Another way to arrive at this formula is to write the numbers in a list and then pair the first and last numbers, then the second and second to last, and so on, as shown in the figure below. 1 + 2 + 3 + · · · + (n - 2) + (n - 1) + n Each of these pairs adds to (n + 1), and if n is one can see that there are n/2 pairs that n n 2 n nn 1 have been matched up. Thus the sum of the pairs is (n 1) , which 2 2 2 is the same result obtained using the geometric argument. 50 Question: Answer the following question, showing your work. Q1: What is the sum of the first 30,000 whole numbers? (i.e. 1 + 2 + 3 + · · · + 29,998 + 29,999 + 30,000) Getting back to an analysis of the selection sort algorithm, we can now see that the sum n1 (n 1) 2 (n 1) n 2 n (n – 1) + (n – 2) + · · · + 3 + 2 + 1 = i . Since the leading 2 2 i1 term in this sum is n2, we say that the selection sort algorithm has order n2, and often write this fact in the following way: O(n2). Sort Algorithm Merging Lists and the Merge At this point we will focus on a different and more efficient way of sorting a list of objects. The algorithm that we will describe uses the divide-and-conquer problemsolving strategy. To begin, we introduce the idea of merging two lists that have already been sorted. For example, the two numerical lists that follow have each been sorted from least to greatest: 5 13 27 32 8 12 17 20. Merging these two lists into one in which all eight numbers have been sorted will begin, as you might expect, by comparing the first numbers of each list—in this case 5 and 8. Once these numbers have been compared (and the lesser number, 5, is placed in the first position of the merged list) the second number in the first list—13—is compared with the first number in the other list. The lesser number, 8, takes its place in the second position of the merged list and the next comparison, between the second numbers in both lists, is made. (See the diagram below for a complete illustration of this example.) This process will go on until one of two things happens: the last number in one of the lists is compared to, and determined to be smaller than, a number from the other list; or the last two numbers in each list are compared. In the best-case scenario, the last number in one of the lists—say list A—is smaller than the first number of the other list—call it list B. In this case we will make four comparisons—comparing each of the numbers in list A with the first number in list B. Once the last number in list A has been compared with, and is determined to be smaller than, the first number in list B, no other comparisons are necessary, for list B is already in order. At this point list B is simply copied and the two lists have been merged after only ½·n comparisons. 51 In the worst-case scenario, the merging process ends with the last numbers from list A and list B being compared. In this case there will have been (n – 1) comparisons. In general, if n is even, it will take at most (n – 1) comparison to merge two lists of size n/2 as we shall demonstrate below. We will now apply the divide-and-conquer paradigm to the general problem of sorting a large list of numbers. The process that we will outline below is called the merge sort algorithm. For sake of example, let’s suppose we have a list of 1024 numbers. We will view the list as 1024 sorted lists of size and note that our objective is to obtain one sorted list of size 1024. At the first step we will merge the first two single ‘sorted lists’ (i.e. the single numbers viewed as individual lists of size one) into a list of size two. This will require only one comparison (we’ve only got two numbers to compare). We’ll then do the same with the next pair of numbers, and so on until there are 512 sorted lists of size two. With one comparison per merge and (½)·(1024) = 512 merges, there will be 1·512 = 512 comparisons at the first step. At the second step we will merge two sorted lists of size two into a sorted group of four numbers. There will be at most 3 comparisons per merge (n – 1 where n = 4 = number of objects being merged) and (½)·(512) = 256 merges. So during the second step there will be no more than 3·256 = 768 comparisons made. Recall we may make fewer comparisons, but we’ll be conservative and assume the worst-case scenario. We will look at one more step to establish the pattern: during the third step—in which we will merge sorted groups of four numbers into sorted groups of eight numbers—there will be at most 7 comparisons per merge and (½)·(256) = 128 merges. Thus at the third step we are guaranteed to complete all merges using no more than 7·128 = 896 comparisons. In general, if we begin with an unsorted list of n numbers, at the kth step of the merge-sort n process, there will be at most 2k – 1 comparisons per merge and k merges. Thus there 2 n will be less than 2k k n comparisons. 2 52 An applet that compares the selection sort and merge-sort algorithms is found here: eecs.oregonstate.edu/~quinn/education/JavaApplets/applets/SortingAlgorithms.html. 1. Change the sorting method on the left from Quick Sort to Selection Sort. Change the sorting method on the right from Bubble Sort to Merge Sort. 2. With your mouse, drag the blue circle in the Number of Elements box until the number of elements is 16. Choosing this number of elements will help illuminate the merge sort process. 3. At a steady rate, continually click the step button, which will move the sorting process along a step at a time. Once you get the basic idea, you can click the play button, which will move the process along at a quicker pace. If you want to slow the process down again, simply click the pause button and then the step button. 4. In the Selection Sort demonstration, the red bar represents the current position that is being filled and the moving yellow bar represents the bar-bybar comparisons that are being made to determine the shortest remaining bar. 5. In the Merge Sort demonstration, bars 1 and 2 are merge-sorted, followed by the merge-sorting of bars 3 and 4. Now those two ‘sorted lists’ are mergedsorted. Thus we have an ordered list of bars 1 – 4. The next step the applet takes is to merge-sort the 5th and 6th bars and then the 7th and 8th bars. These two sorted lists of size two are now merged—forming another sorted list of four bars (representing bars 5 – 8). The two lists of four bars are merge-sorted into one list of eight bars. This same process is now applied to bars 9 – 16, until they have been sorted from shortest to tallest. The last step is to merge the two sorted lists of 8 bars, the first one consisting of bars 1 – 8 (in order from shortest to tallest) and the last one consisting of bars 9 – 16 (in order from shortest to tallest). 6. Now change the number of elements to be sorted and compare the algorithms again. Another piece of information we need to know is how many steps there are going to be in the merge sort process. In our example of 1024 words, we can ask this question: How many times will we decrease the number of sorted lists in half (recall we started with 1024 lists of size one, and then reduced these to 512 lists of size two, and so on) before we get to one sorted list. The nature of this problem suggests we use the log function, as log2(n) can be defined as the number of times the quantity n can be cut in half until it has been reduced to the value 1. So log2(1024) = 10, and we know that there will be 10 steps involved in reducing 1024 sorted lists of size one to one sorted list of size 1024. Recall that at each step there are less than n comparisons which means we are guaranteed to be able to sort the list of size 1024 by using fewer than 10,240 comparisons. This is significantly less than 523,776, which is the number of comparisons required for accomplishing the same task using the greedy selection sort method. In general, if there are n words to be sorted, there will be log2(n) steps and no more than n comparisons at each step. So we are guaranteed that there will be less than n·log2(n) comparisons made over the course of the entire process. In other words the merge sort algorithm is O(n·log n). As was mentioned earlier, the selection sort algorithm is O(n2). 53 The first exercise will give you a greater understanding of the difference between n2 and n·log n. Exercises: Answer each of the following questions and explain your reasoning as necessary. Q2: Fill in the following table for the given values of n. n n2/2 n·log2 n 10 100 10,000 100,000 1,000,000 Q3: Use the merge sort algorithm to alphabetize the following names: Rachel, Sammy, Wilma, Tyrone, Carlos, Olga, Bethany, Jasmine, Pedro, Mikhail. 54 Q4: This question concerns your experience with the applet. When the number of elements is 16, which of the two sorting methods (selection sort and merge sort) is fastest? What about when the number of elements is 75? Why is there a difference? Q4: A king has 18 golden coins, one of which he knows is counterfeit. The counterfeit gold coin weighs less than a regular coin. You have been assigned to use a balance (scale) to determine which one of the coins is not pure gold. Explain how you would accomplish this using the scale a minimum number of times. How many times did you use the scale? Can you prove there is no method that requires fewer weighing steps? 55 n Challenge Question: Prove that i i1 nn 1 when n is odd. 2 56 CS 160 Computer Science Orientation Name ________________________ Section Meeting Time____________ Searching Methods Worksheet Some situations present problems that have many solutions, but only one that is optimal. In such a situation a searching strategy may be helpful in finding the optimal solution. To illustrate problem solving by searching, we’ll consider the 8-puzzle. The 8-puzzle is tile game consisting of nine squares—eight numbered tiles and one ‘empty’ square (see square below, left which was adapted from Zhu, S. C., http://www.stat.ucla.edu/~sczhu/Courses/UCLA/Stat_232B/Handouts/Introduction.pdf.). The goal of the puzzle is to use the free space to move the tiles so that the numbers are in order starting at the top left and proceeding around the square until the 8, which is right below the 1 (see square below, right). When the numbers are in this order, we have found a solution. 1 2 3 8 4 7 6 5 2 8 3 1 6 4 7 5 The puzzle on the left represents an 8-puzzle in a non-solution state, while the puzzle on the right represents the solution, or objective, of an 8-puzzle. An optimal solution consists of one that consists of the fewest slides. To track the progress toward the optimal solution, we’ll implement a penalty rule system. Each time we slide a tile we’ll be assessed one point. Further, for each number that is out of its solution position, we will assess one penalty point. So if the 8 – puzzle we want to solve is the one on the left above, the initial position has a score of 4 points—we’ve made no slides, and there are four numbers (2, 8, 6, and 1) out of place. First, we consider the three possible first moves and the number of points for each of the resulting states, as shown below. We’ll continue to search from the puzzle with the lowest point total, in this case the puzzle with 4 points. 57 2 8 3 1 6 4 7 5 1 + 5 = 6 pts 1 + 3 = 4 pts 1 + 5 = 6 pts 2 8 3 1 6 4 7 5 2 8 3 1 4 7 6 5 2 8 3 1 6 4 7 5 Diagram note: a shaded puzzle represents one whose ‘next steps’ have been expanded. Solid arrows represent the current ‘puzzle path’ we are currently exploring. Dotted arrows represent paths to puzzles we have not currently chosen, but may choose in the future if they become more desirable than the current path. Since two puzzles tie for the lowest number of points (i.e. two of them have only five points) we’ll choose one of the two arbitrarily, the one with the empty square in the top row. 2 8 3 1 6 4 7 5 1 + 5 = 6 pts 1 + 3 = 4 pts 1 + 5 = 6 pts 2 8 3 1 6 4 7 5 2 8 3 1 4 7 6 5 2 8 3 1 6 4 7 5 2 + 4 = 6 pts 2 + 3 = 5 pts 2 + 3 = 5 pts 2 8 3 1 4 7 6 5 2 3 1 8 4 7 6 5 2 8 3 1 4 7 6 5 58 2 8 3 1 6 4 7 5 1 + 5 = 6 pts 2 8 3 1 6 4 7 5 3 + 3 = 6 pts 8 3 2 1 4 7 6 5 1 + 3 = 4 pts 1 + 5 = 6 pts 2 8 3 1 4 7 6 5 2 8 3 1 6 4 7 5 2 + 4 = 6 pts 2 + 3 = 5 pts 2 + 3 = 5 pts 2 8 3 1 4 7 6 5 2 3 1 8 4 7 6 5 2 8 3 1 4 7 6 5 3 + 4 = 7 pts 2 8 3 7 1 4 6 5 3 + 3 = 6 pts 2 8 3 1 4 7 6 5 Searching this path leads to three puzzles with point totals of six (two have this many points) and seven. At this point if we were using the greedy method, we would follow the path of one of the puzzles having six points. However, according to the search method we will take a step back and survey all possible remaining paths and then choose from them the one that is most desirable. Currently there are seven puzzles that have not yet been expanded. Each of these puzzles has five, six or seven points. There is only one puzzle with five points and we’ll expand that puzzle next, as it is currently the most desirable path. As shown on the next page, by following this path and implementing the search strategy, the optimal solution to the puzzle can be obtained in five slides, indicated in the diagram by the solid black arrows. 59 2 8 3 1 6 4 7 5 1 + 5 = 6 pts 2 8 3 1 6 4 7 5 1 + 3 = 4 pts 1 + 5 = 6 pts 2 8 3 1 4 7 6 5 2 8 3 1 6 4 7 5 2 + 4 = 6 pts 2 + 3 = 5 pts 2 + 3 = 5 pts 2 8 3 1 4 7 6 5 2 3 1 8 4 7 6 5 2 8 3 1 4 7 6 5 3 + 3 = 6 pts. 3 + 4 = 7 pts. 3 + 3 = 6 pts. 8 3 2 1 4 7 6 5 2 8 3 7 1 4 6 5 2 8 3 1 4 7 6 5 3 + 3 = 6 pts 2 8 3 1 4 7 6 5 3 + 2 = 5 pts 3 + 4 = 7 pts 2 3 1 8 4 7 6 5 2 3 1 8 4 7 6 5 4 + 3 = 7 pts 4 + 1 = 5 pts 2 3 1 8 4 7 6 5 1 2 3 8 4 7 6 5 5 + 0 = 5 pts 5 + 2 = 7 pts 1 2 3 8 4 7 6 5 1 2 3 7 8 4 6 5 SOLUTION! The key principle of this searching method is always following the path that appears to be leading to the lowest-cost solution. If the algorithm is following a promising search path, but at some point the path seems to be inferior to another path 60 through the search tree, the algorithm should switch focus and pursue the path that now seems to be better. Exercises: Answer the following questions using the searching strategy outlined in this worksheet. P1: On a dark, cold and stormy night four friends find themselves on one side of a bridge. There is only one lamp between them and because the bridge is so old, at most two of them can cross the bridge at once. Whenever anyone crosses the bridge—either alone or with a partner—the lamp must go with him. One of the friends can cross the bridge in 15 minutes, the second in 10 minutes, the third in 2 minutes and the last in 1 minute. Find the minimum amount of time needed before they each reach the other side of the bridge. 61 P2: What is the minimum number of steps required to transform the eight-puzzle below into its solution state? 1 3 6 2 4 8 7 5 62 P3: Two boys with a small boat agree to help three soldiers cross a river without a bridge. The boat is so small it can support only one soldier or two boys. A soldier and a boy can't be in the boat at the same time for fear of sinking it. What is the minimum number of trips it will take to ferry all the soldiers across? (Adapted from Science News for Kids, http://www.sciencenewsforkids.org/pages/puzzlezone/muse/muse0904.asp). 63 64 CS 160 Computer Science Orientation Name ________________________ Section Meeting Time____________ Monte Carlo Problem-Solving Worksheet One way to incorporate technology into problem solving is to use a computer or other machine to simulate an activity or experiment. This process can often save time and money and can yield results very similar to what would have occurred if the experiment had actually taken place. The Monte Carlo method is a problem solving strategy that finds an approximate solution by sampling points in the solution space. The strategy gets its name because the sample points are created using a random number generator, the computer equivalent of throwing dice. We will illustrate the Monte Carlo method by using a simple spreadsheet to provide a (rough) estimate of . We begin with a circle of radius one, inscribed inside a square of side length two, as shown below. The area of this circle is . The ratio of the area of the circle to the area of the square is /4. Now look at the upper right quadrant (shaded in the diagram below). The area of the shaded quarter circle to the area of the shared square is also /4. 1 unit 2 units 1 unit 2 units One way to apply the Monte Carlo method to finding an approximation of is to select n random points in the shaded square and then compute the ratio of points that lie within the circle to n, the total number of points randomly selected. The Law of Large Numbers (sometimes called the Law of Averages) says that as n increases, the proportion of points within the circle will approach /4. Multiplying this proportion by4 will give us an approximation to . An illustration of this strategy is shown below. A spreadsheet is used to compute two columns of 15 random numbers between 0 and 1. The first column represents the xcoordinate of the point and the second column represents the y-coordinate. The spreadsheet can determine which of the points are within the quarter circle. We can assume that the center of the circle is at the origin (0, 0) so that the equation of the circle of radius 1 centered at the origin is x2 + y2 = 1. With this in mind, the spreadsheet 65 identifies those points that are inside the quarter circle by determining which of them satisfies the inequality x 2 y 2 1. In the simulation shown below, 12 of the 15 points are inside the circle. Thus our approximation for /4 is 12/15, or 0.8, and our approximation for is 3.2 . (0, 1) (1, 1) (0, 0) (1, 0) Increasing the number of samples tends to increase the accuracy of the estimate, as shown by the graph below. 3.19 Estimate of pi = 3.14159 3.18 3.17 3.16 3.15 3.14 3.13 3.12 3.11 3.1 0 2000 4000 6000 8000 10000 12000 14000 16000 Number of Points Chosen 18000 20000 22000 24000 Nobody would use the Monte Carlo method to compute because we already know the value of . We chose that as our example so we could evaluate the quality of the estimate. In actual practice, the Monte Carlo algorithm is used in situations where it is impossible to calculate the true answer analytically. 66 Exercise: Use Microsoft Excel or another spreadsheet program to solve the following problem using the Monte Carlo method. P1: A homeowner wants to improve the looks of his property by fertilizing the lawn, part of which is adjacent to a river. He figures he will need a bag of fertilizer for every 10 square yards of grass. Based on the plot of his property below, how many bags of fertilizer should he buy? You can assume that the entire property is lawn, except for the building and the driveway. Height of curve above dotted line = 10 * sin (0.07854 * x) where x is yards from left edge of property 20 yards 5 40 yards 67 30 yards 10 yards Driveway 10 yards House