Jon Pierre Fortney - Discrete Mathematics For Computer Science - An Example-Based Introduction-Chapman
Jon Pierre Fortney - Discrete Mathematics For Computer Science - An Example-Based Introduction-Chapman
Science
Discrete Mathematics for Computer
Science
An Example-Based Introduction
The right of Jon Pierre Fortney to be identi ed as author of this work has been asserted by him in
accordance with sections 77 and 78 of the Copyright, Designs and Patents Act 1988.
Reasonable efforts have been made to publish reliable data and information, but the author and
publisher cannot assume responsibility for the validity of all materials or the consequences of
their use. The authors and publishers have attempted to trace the copyright holders of all material
reproduced in this publication and apologize to copyright holders if permission to publish in this
form has not been obtained. If any copyright material has not been acknowledged please write
and let us know so we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted,
reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means,
now known or hereafter invented, including photocopying, micro lming, and recording, or in
any information storage or retrieval system, without written permission from the publishers.
For permission to photocopy or use material electronically from this work, access
www.copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood
Drive, Danvers, MA 01923, 978-750-8400. For works that are not available on CCC, please
contact [email protected]
Trademark notice: Product or corporate names may be trademarks or registered trademarks and
used only for identi cation and explanation without intent to infringe.
Preface
CHAPTER 3 ▪ Logic
CHAPTER 6 ▪ Functions
CHAPTER 10 ▪ Trees
Index
Preface
Algorithms play an extremely important role in both computer science and mathematics.
Algorithms are detailed instructions on how to carry out some speci c task. Computer programs
are implementations of algorithms, so it is very important that computer science majors and
programmers have a good understanding of how algorithms work. Deciding on an algorithm is
generally the rst step of writing a computer program. Because algorithms will be used
throughout this book we begin by introducing some of the basic concepts and ideas related to
algorithms.
each step is a clear instruction that can be done in a nite amount of time,
the sequence in which the steps are done is clear, and
the process is guaranteed to stop after a nite number of steps have been done.
Here the phrase well-de ned means the steps are very clear. There is no confusion or uncertainty
in what is meant. A computer program is the implementation of an algorithm in some
programming language. This means the program actually carries out, or executes, the algorithm.
Designing an algorithm is one of the rst steps in writing a computer program. In order to make
algorithms easier for us to understand they are written in a form of English called pseudocode.
This allows us to concentrate and think about the structure of the algorithm without worrying
about the details of a particular computer language. Let us start by looking at a very simple
algorithm written in pseudocode. Here is an algorithm to calculate the volume of a cube.
3. Output volume.
other words, the number given by l3 is stored in, or assigned to, the computer's memory at an
address that is called volume.1
if-then,
if-then-else.
Loop controls tell how many times a step in an algorithm should be executed. Loop controls
include
for-do,
while-do,
repeat-until.
If-then
Consider the following simple algorithm that contains an if-then conditional control. Notice
that each line, or step, is numbered.
In line 1 the algorithm asks you to input a student's grade as a percentage. Line 2 says that if the
condition is true and the grade is greater than or equal to 60 then move to line 2.1, which outputs
the word passed. Suppose you input a 75. Since the grade is 75 ≥ 60 then the word passed is
output and the algorithm ends. But suppose the grade was 55. Then since 55 ≥ 60 we do not go
to line 2.1 and the algorithm ends with nothing output.
If-then-else
Now consider the following algorithm. It contains an if-then-else conditional control.
Algorithm: Determines if a student has passed or failed.
In line 1 the algorithm asks you to input a student's grade as a percentage. Line 2 says that if the
grade is greater than or equal to 60 then move to the next line, 2.1, which outputs the word passed.
But now there is an else. This tells the algorithm what to do if the condition is not met. Suppose
you input a 75. Then since 75 ≥ 60 the algorithm goes to line 2.1, outputs the word passed, and
ends. But now suppose that a grade of 55 was input. Since 55 ≥ 60 then the algorithm goes to
line 2.2, outputs the word failed, and ends. Since the if condition was not met the then line was
skipped and the algorithm goes to the else line.
It is also possible to nestif-then-else statements. This means we put an if-then-else statement
inside another if-then-else statement. Consider the following algorithm that prints out a student's
letter grade when their grade as a percent is input.
Here we can see several nested if-then-else statements. Consider what happens if we input the
grade 75. In line 2 the rst if condition is checked and 75 ≥ 90 is found so line 2.1 is skipped and
we go to line 3. In line 3 the second if condition is checked and 75 ≥ 80 is found so line 3.1 is
skipped and we go to line 4. In line 4 the third if condition is checked and 75 ≥ 70 is found so we
go to line 4.1 and output the letter grade C. At this point the algorithm ends.
For-do
Now we look at loop controls. Loop controls tell how many times a step in the algorithm should
be executed. We can think of the algorithm as looping around and repeating the same step a
number of times. First we look at the for-do loop control.
3. min ← x 1
4. i ← 2
5. For i = 2 to n do
5.1 If x < min then
i
5.1.1 min ← x 1
5.2 i ← i + 1
6. Output min.
The number of values in the list is input in step 1, then the list of values is input in step 2. In
step 3 the word min is the name of a variable and x1, the rst value in the list, is assigned to it.
Step 4 initializes the variable i with 2. This means the variable i is given the starting, or initial,
value 2. Step 5 contains the for-do loop. This line tells us we repeat steps 5.1, 5.1.1 if necessary,
and 5.2 for each value of i between 2 and n. Step 5.2 increases i by 1. This is often called
incrementingi. Once i becomes n + 1 we no longer repeat step 5 and we move onto step 6 where
we output the minimum value.
Remember, pseudocode is written in English at a level that allows us to fully understand the
algorithm. As long as we can fully understand what is supposed to happen it is enough. Suppose
we simplify this algorithm a little by taking out steps 4 and 5.2, the steps that involve initializing
and incrementing the variable i. Then the algorithm looks like this.
3. min ← x 1
4. For i = 2 to n do
4.1 If x < min then
i
4.1.1 min ← x 1
5. Output min.
In for-do loops we step through values of i. The i = 2 to n inside the for-do loop tells us for
which values of i we need to do the loop. We start with i = 2 and each time the loop is executed
the value of i increases by one. We keep executing the loop until we reach the last value of i,
which is n. When the i value is incremented to n + 1 we stop doing the loop. Hopefully this
should be clear to any human reading this algorithm and so we do not really need those other steps
to understand what is happening.
While-do
The while-do loops are a lot like the for-do loops. While-do loops are repeated while a given
condition is true. In other words, as long as the given condition is true the loop repeats. Consider
the following algorithm.
Algorithm: Finds the order of the factor two for the integer n.
In this example the steps 3.1 and 3.2 are repeated as long as n is an even number. At some point
the condition stops being true and the loop is no longer repeated.
Repeat-until
The repeat-until loops are very similar to the while-do loops. While-do loops are repeated while
a given condition is true. Repeat-until loops are repeated until a given condition is true. That
means the loop repeats while the condition is false and then stops when the condition becomes
true. In this example the steps 2.1 and 2.2 are repeated until the condition i = 0 is met.
3. min ← x 1
4. For i = 2 to n do
4.1 If x < min then
i
4.1.1 min ← x i
5. Output min.
Let us suppose we want to nd the smallest number in the list 5, 4, 8, 3. There are four numbers
in this list so n = 4 and x = 5, x = 4, x = 8, x = 3. We usually use a table to trace an
1 2 3 4
algorithm.
3 5 - - -
4 5 2 4 -
4.1 5 2 4 -
4.1.1 4 2 4 -
4 4 3 8 -
4.1 4 3 8 -
4 4 4 3 -
4.1 4 4 3 -
4.1.1 3 4 3 -
5 3 4 3 3
Often when we trace an algorithm we do not bother to write down the input steps in the table.
Here we did not write steps 1 or 2 but started with step 3 where the value x = 5 was assigned to
1
the variable min. After step 3 we included a row in the table for each of the following steps in the
algorithm. In step 4 the algorithm states “For i = 2 to n do” so we let i = 2. If i = 2 then we also
know what xi is and so also include that in the table. Step 4.1 states “If x < min then” so we
i
check to see if 4 < 5. Since it is, we go to the then part of the if-then statement, step 4.1.1. Here
x = 4 is assigned to the variable min. After that we return to the next step in the for-do
2
statement where i = 3. Notice that this time around x3 is not less than min and so step 4.1.1 is
skipped. The algorithm continues the for-do loop until i = n. Carefully follow the rest of the steps
of the algorithm to make sure you understand them.
Now we will look at the algorithm that contains the while-do loop.
Algorithm: Finds the order of the factor two for the integer n.
As before we skip the input step and begin the trace of the algorithm with step 2. In step 2 we
assign the value 0 to order. In step 3 we check to see if n is even. Since it is we do steps 3.1 and
3.2. In step 3.1 the number n is divided by two and this new number is assigned to n. Then step 3.2
adds one to order and assigns this new number to order. We could also say we increase the value
of order by one. Then we return to step 3 where we check if the new value of n is even. While n is
still even we do steps 3.1 and 3.2. When n stops being even we skip steps 3.1 and 3.2 and continue
with the algorithm.
Now look at the two algorithm traces we presented. Notice that in both cases a number of lines
look exactly the same. In the rst trace all the lines 4 and 4.1 are exactly the same. In the second
trace when 3 follows a 3.2 then these two lines are the same. Often when one gets good at tracing
algorithms one skips writing down repeated lines.
This is a simple algorithm that shows how the repeat-until loop control works. We will trace it
for n = 3.
Step i output
2 3 -
3.1 3 3
3.2 2 -
3.1 2 2
3.2 1 -
3.1 1 1
3.2 0 -
Tracing this algorithm is straightforward. As long as the condition after until is not met we
continue to repeat steps 3.1 and 3.2. In step 3.1 we output i and in step 3.2 we subtract one from i
and assign the new value to i. This is called decrementingi and works very similarly to
incrementing i, only instead of adding one each time we subtract one each time.
Learning how to trace an algorithm is a very important skill. Tracing an algorithm multiple
times for different inputs is a very good way to help you understand what an algorithm does and
how it works. As you get better at tracing algorithms you will probably nd yourself skipping
steps or only writing down enough detail to understand what is happening. This is ne. Tracing
algorithms is simply a tool to help you understand an algorithm. Your understanding of how an
algorithm works is the most important thing.
1. Input x and y.
2. If x > y then
2.1 max ⟵ x
else
2.2 max ⟵ y
3. Output max.
Example 1.1
Use the above algorithm to nd the larger of the two numbers x = 7 and y = 9.
This algorithm is so simple that tracing it in a table is a little silly. Simply step through each
line of the algorithm. In line 1 the values x = 7 and y = 9 are input in line 1. In line 2 we
check if x > y. Since 7≯9 then we skip line 2.1 and do line 2.2 where we assign 9 to max. Then
in line 3 we output max which is 9.
1. Input x and n.
2. answer ← x
3. For i = 1 to n − 1 do
3.1. answer ← answer × x
4. Output answer.
Example 1.2
Trace the above algorithm for x = 7 and n = 4. In other words, use the algorithm to nd 74.
Step i answer Output
2 - 7 -
3.1 1 49 ( ← 7 × 7) -
3.1 2 343 ( ← 49 × 7) -
3.1 3 2401 ( ← 343 × 7) -
4 3 2401 2401
Here we look at an algorithm is also an example of the while-do loop control. The exclamation
mark ! following a positive integer is called the factorial symbol. It will be covered in chapter 7.
Simply put, n! means to multiply n by all the numbers less than n. For example,
2! = 2 × 1, 3! = 3 × 2 × 1, 4! = 4 × 3 × 2 × 1,
and so on.
Example 1.3
Trace the above algorithm for n = 4. In other words, use the algorithm to nd 4!.
Step n answer Output
2 4 4 -
3.1 3 4 -
3.2 3 12 ( ← 4 × 3) -
3.1 2 12 -
3.2 2 24 ( ← 12 × 2) -
3.1 1 24 -
Step n answer Output
3.2 1 24 ( ← 24 × 1) -
4 1 24 24
2. i ← 1
3. int eger _ s _ det ected ← f alse
4. Repeat
4.1. If x = s then
i
Here the steps 4.1, 4.1.1, and 4.2 are repeated until either integer _ s _ detected = true or
until i = n + 1. Thus, as soon as we have x = s in step 4.1 then step 4.1.1 is called and
i
integer_s_detected becomes true. Step 4.2 is still carried out but then the two conditions are
checked it see if we need to repeat step 4 again or not. We do not need to continue checking the
rest of the string once we have found x = s for some value of i. Finally, an if-then-else statement
i
Example 1.4
Use the above algorithm to search the string 758 to see if it contains a 5.
The input is x 1 , ,
= 7 x2 = 5 x3 = 8 n = 3 , , and s = 5.
Step integer_s_detected i xi Output
2 - 1 - -
3 false 1 - -
4.1 false 1 7 -
4.2 false 2 ( ← 1 + 1) 7 -
4.1 false 2 5 -
4.1.1 int eger _ s _ det ected ← f alse 2 5 -
4.2 true 3 ( ← 2 + 1) 5 -
5.1 true 3 5 String contains 5.
This algorithm works ne, but we can make it much simpler by using a return. Return works a
lot like Output except that it also ends the algorithm. Sometimes we want an algorithm to end as
soon as something is output. That is when return is used.2 In this example, as soon as we
encounter an integer s in the string we know the string contains the integer s. We do not need to
continue checking the rest of the string. Thus, if x = s for some value of i then “String contains
i
2. For i = 1 to n do
2.1. If x = s then
i
Example 1.6
Trace the above algorithm for the string 497316 and search for the integer 3.
The input is x 1 , , ,
= 4 x2 = 9 x3 = 7 x4 = 3 x5 = 1 x6 = 6 n = 6, , , , and s = 3.
Step i xi s Output
2 1 1 - -
2.1 1 4 3 -
2.1 2 9 3 -
2.1 3 7 3 -
2.1 4 3 3 -
2.1.1 4 3 3 String contains 3.
Notice, since x = s the then part of the if-then statement is executed, “String contains 3.” is
4
Algorithm: Searches two strings of integers x1, x2, … , xn and y1, y2, … , yn of equal length
to see if either string contains integer s.
2. For i = 1 to n do
2.1. If x = s then
i
Step i x i or y i Output
2.1 1 7 -
2.1 2 4 -
3.1 1 8 -
3.1.1 1 8 String two contains 8.
2. For i = 1 to n do
2.1 For j = 1 to n do
2.1.1 If x = y then
i j
Example 1.7
Use the above algorithm to see if the strings 67 and 89 have any integers in common.
The input is x 1 ,
= 6 x2 = 7 y1 = 8 y2 = 9 , , , and n = 2.
Step i j xi yj Output
2.1.1 1 1 6 8 -
2.1.1 1 2 6 9 -
2.1.1 2 1 7 8 -
2.1.1 2 2 7 9 -
3 2 2 7 9 \beginsubarraycNoelementsincommon\\tobothstrings. \endsubarray
Algorithm: Checks to see if a string of integers x 1, x2, … , xn contains any duplicate integers.
1. Input string x , … , x . 1 n
2. For i = 1 to n − 1 do
2.1 For j = i + 1 to n do
2.1.1 If x = x then i j
Use the above algorithm to check if the string 98767 contains any duplicate integers.
The input is x 1 , ,
= 9 x2 = 8 x3 = 7 x4 = 6 x5 = 7 , , , and n = 5.
Step i j xi xj Output
2.1.1 1 2 9 8 -
2.1.1 1 3 9 7 -
2.1.1 1 4 9 6 -
2.1.1 1 5 9 7 -
2.1.1 2 3 8 7 -
2.1.1 2 4 8 6 -
2.1.1 2 5 8 7 -
2.1.1 3 4 7 6 -
2.1.1 3 5 7 7 -
2.1.1.1 3 5 7 7 \beginsubarraycThereisaduplicate\\inthestring. \endsubarray
2. i ← 1
3. non int eger _ det ected ← f alse
4. 4. Repeat
4.1. Ifxi is not an integer then
4.1.1. non int eger _ det ected ← true
4.2. i ← i + 1
until non int eger _ det ected = true or i = n + 1
5. If noninteger _ detected = true then
5.1 Output “String contains non-integer characters.”
else
5.2 Output “String consists entirely of integers.”
Example 1.9
Use the above algorithm to determine if the string 68c5 consists entirely of integers or if a non-
integer is contained in the string.
The input it x 1 ,
= 6 x2 = 8 x3 = c , , and x
4 = 5 , and string length n = 4.
Step nondigit_detected i xi Output
2 - 1 - -
3 false 1 - -
Step nondigit_detected i xi Output
4.1 false 1 6 -
4.2 false 2 6 -
4.1 false 2 8 -
4.2 false 3 8 -
4.1 false 3 c -
4.1.1 true 3 c -
4.2 true 4 c -
5.1 true 4 c \beginsubarraycStringcontainsnon-integercharacters. \endsubarray
1.5 PROBLEMS
Question 1.1 Give a description of what the following algorithm does. What is the output of this
algorithm for the input days = 3, hours = 7, minutes = 51, seconds = 27?
3. Output answer.
Question 1.2 Trace the below algorithm for the following pairs of numbers.
(a) x = 6 and y = 12
(b) x = 15 and y = 9
(c) x = 7 and y = 7
1. Input x and y.
2. If x > y then
2.1 max ⟵ x
else
2.2 max ⟵ y
3. Output max.
Question 1.3 Trace the below algorithm for the following pairs of numbers.
Question 1.4 Trace the below algorithm for the following pairs of numbers.
(a) x = 1
(b) x = −3
(c) x = 7
1. Input x.
2. x ← −x
3. If x = −1 then
3.1. answer ← x + 5
else
3.2. answer ← x − 5
4. Output answer.
Question 1.5 Trace the below algorithm for the following pairs of numbers.
(a) x = −6
(b) x = 2
(c) x = 8
1. Input x.
2. If x < −5 then
2.1. answer ← 2x + 3
else
2.2 If x > 5 then
2.2.1 answer ← −2x + 3
else
2.2.2 answer ← −7
3. Output answer.
Question 1.6 Trace the below algorithm for the following pairs of numbers.
(a) x = 2 and n = 4
(b) x = 3 and n = 5
(c) x = 5 and n = 6
1. Input x and n.
2. answer ← x
3. For i = 1 to n − 1 do
3.1. answer ← answer × x
4. Output answer.
Question 1.7 Trace the below algorithm for the following numbers.
(a) n = 3
(b) n = 5
(c) n = 7
4. 4. Output fac.
Question 1.8 Trace the below algorithm for the following numbers. Then compare it to the
algorithm in the last question.
(a) n = 3
(b) n = 5
(c) n = 7
Question 1.9 Trace the following algorithm for n = 8, 490, 725, 727, 154, 368, 726, 402, 945.
1. Input a positive integer n.
2. d ← number of digits of n
3. While d > 1do
3.1 n ← sum of digits of n
3.2 d ← number of digits of n
4. Output n.
(a) string of integers 274390 to see if the integer s = 9 is contained in the string,
(b) string of integers 730285 to see if the integer s = 2 is contained in the string,
(c) string of integers 983650362 to see if the integer s = 5 is contained in the string.
2. For i = 1 to n do
2.1. If x = s then
i
(a) strings 2109 and 4071 to see if the integer s = 0 is contained in either one,
(b) strings 593 and 721 to see if the integer s = 2 is contained in either one,
(c) strings 397 and 443 to see if the integer s = 6 is contained in either one.
2. For i = 1 to ndo
2.1. If x = s then
i
2. For i = 1 to n do
2.1 For j = 1 to n do
2.1.1 If x = y then
i j
1. Input string x , … , x .
1 n
2. For i = 1 to n −1 do
2.1 For j = i + 1 to n do
2.1.1 If x = x then
i j
(a) n = 3,
(b) n = 4,
(c) n = 6.
4. Output sum.
(a) n = 12,
(b) n = 90,
(c) n = 80.
1. Input a non-negative integer n.
2. order ← 0
3. While n is even do
3.1 n ← n
Question 1.16 Give a description of what the following algorithm does. Then trace this algorithm
for
(a) string 5, 7, 4, 6, 2, 8,
(b) string 3, 5, 7, 2, 4, 6,
(c) string 7, 2, 5, 0, 9, 3, 1.
3. For i = 2 to n do
3.1If x < min then
i
3.1.1
min ← xi
3.1.2
position ← i
1. Input string x , x , … , x .
1 2 n
2. i ← 1
3. noninteger _ detected ← f alse
4. Repeat
4.1. Ifxi is not an integer then
4.1.1. noninteger _ detected ← true
i ← i + 1
Binary, or base-two, numbers are extremely important in computer science because they are used in designing
computers. Computer scientists also use octal and hexadecimal numbers, which are closely related to binary
numbers. Therefore computer science majors need to have a good understanding of all these number systems and
be able to convert between them and decimal, or base-ten, numbers.
0
8 = 8 × 10 ,
1 0
72 = 7 × 10 + 2 × 10 ,
2 1 0
401 = 4 × 10 + 0 × 10 + 1 × 10 ,
3 2 1 0
8925 = 8 × 10 + 9 × 10 + 2 × 10 + 5 × 10 .
It is important to remember that 10 = 1 and to remember the order of operations. The exponents are done rst,
0
then all the multiplications are done from left to right, then all the additions are done from left to right. In the
above example the number 10 is called the base. Notice that when we use a base 10 the coef cients of the powers
of ten, that is, the numbers that are in front of the powers of ten, range from 0 to 9. These coef cients are also
call digits.
We can write numbers with different bases. For example, instead of using a 10 as we did above we can use a 2.
Numbers that use a two as a base are called either base-two numbers or binary numbers. The coef cients of the
powers of two range from 0 to 1. In other words, binary numbers have the digits 1 and 0. Binary numbers are
very important in computer science because these are the numbers that computers actually use. Computers use
electricity, and either there is an electrical current or there isn't. This means computers can only “understand”
two states; is there an electrical current present or is there no electrical current1 present? These two states are
often called called 1 and 0. So any numbers computers use can only have two coef cients, or digits. Numbers
that only have two digits are binary numbers and have a base of 2. Binary numbers can be written in expanded
form as follows
0
1 = 1 × 2 ,
1 0
10 = 1 × 2 + 0 × 2 ,
2 1 0
101 = 1 × 2 + 0 × 2 + 1 × 2 ,
3 2 1 0
1011 = 1 × 2 + 0 × 2 + 1 × 2 + 1 × 2 .
Again, remember that 2 = 1 and remember the order of operations. We always need to know what base a
0
number is written in. Sometimes it is obvious what base the numbers should have. Sometimes it isn't. When it is
not clear what base a number is written in sometimes a subscript is used. A subscript of 2 is used to indicate a
binary number and a subscript of 10 is used to indicate a decimal number.
110 2 is a base-two, or binary, number,
It is easy to convert from binary numbers to decimal numbers. As we said above, the digits for binary numbers
are simply 0 and 1. It should be clear that 0 in base-two, or 02 is exactly the same as 0 in base-ten, or 010.
Similarly, 1 = 1 . Therefore, in the below example there is no need to specify which base the digits are in
2 10
Example 2.1
2 1 0
110 2 = 1 × 2 + 1 × 2 + 0 × 2
=4 10 =2 10 =1 10
= 1 × 4 10 + 1 × 2 10 + 0 × 1 10
=4 10 =2 10 =0 10
= 4 10 + 2 10 + 0 10
= 6 10
3 2 1 0
1011 2 = 1 × 2 + 0 × 2 + 1 × 2 + 1 × 2
= 1 × 8 10 + 0 × 4 10 + 1 × 2 10 + 1 × 1 10
= 8 10 + 0 10 + 2 10 + 1 10
= 11 10
Very quickly binary numbers become dif cult for us to understand. For example, consider the number
1000111110101010011100011101
1000111110111010011100011101.
Just by looking can you easily tell which of these two numbers was larger? Even though computers use binary
numbers it is dif cult for us to use them. So there are two other basis that are very important in computer
science, base-eight and base-sixteen.
For base-eight numbers instead of using a 10 or a 2 we use an 8 as the base. Base-eight numbers are also called
octal numbers. The coef cients of octal numbers range from 0 to 7. This means that octal numbers use the digits
. Sometimes we know from context that a number is octal, but sometimes it is not clear. In this
0, 1, 2, 3, 4, 5, 6, 7
case we often use a subscript of 8 to indicate the number is an octal number. Octal numbers can be written in
expanded form,
0
58 = 5 × 8 ,
1 0
70 8 = 7 × 8 + 0 × 8 ,
2 1 0
372 8 = 3 × 8 + 7 × 8 + 2 × 8 ,
3 2 1 0
6504 8 = 6 × 8 + 5 × 8 + 0 × 8 + 4 × 2 .
Again, it is easy to convert from octal numbers to decimal numbers. It should be clear that 0 in base-eight, or 08
is exactly the same as 0 in base-ten, or 010. Similarly, 1 = 1 and so on up to 7 = 7 . Therefore, in the below
8 10 8 10
example there is no need to specify which base the digits 0 through 7 are since they are the same in both bases.
Example 2.2
1 0
70 8 = 7 × 8 + 0 × 8
= 7 × 8 10 + 0 × 1 10
= 56 10 + 0 10
= 56 10
3 2 1 0
6504 8 = 6 × 8 + 5 × 8 + 0 × 8 + 4 × 8
= 6 × 512 10 + 5 × 64 10 + 0 × 8 10 + 4 × 1 10
= 3072 10 + 320 10 + 0 10 + 4 10
= 3396 10
For base-sixteen numbers we use 16 as the base. Base-sixteen numbers are also called hexadecimal numbers.
Like before, we want the coef cients of the powers of 16 to range from 0 to 15. The problem is that the numbers
10 through 15 each have two decimal digits in them. Using these would be very inconvenient. Therefore we use
the capital letters A, B, C, D, E, and F instead. The table below summarizes the correspondence between the
decimal numbers and the digits used for hexadecimal numbers.
This means hexadecimal numbers look something like 3B7, A05C , E2, or D. Hexadecimal numbers can be
written in expanded form
0
D 16 = D × 16 ,
1 0
E2 16 = E × 16 + 2 × 16 ,
2 1 0
3B7 16 = 3 × 16 + B × 16 + 7 × 16 ,
3 2 1 0
A05C 16 = A × 16 + 0 × 16 + 5 × 16 + C × 16 .
Decimal Numbers Hexadecimal Digits
0 10 0
1 10 1
2 10 2
3 10 3
4 10 4
5 10 5
6 10 6
7 10 7
8 10 8
9 10 9
10 10 A
11 10 B
12 10 C
13 10 D
14 10 E
15 10 F
Converting hexadecimal numbers to decimal numbers works just like before, except that we need to change
the hexadecimal digits to decimal digits using the table. As with the binary and octal case it is obvious that
016 = 0 and so on up to 9 = 9 . Therefore, as before we make no effort to specify which base the digits 0
10 16 10
through 9 are since they are the same in both bases. However, when we convert from the digits A through F we
go ahead and specify the base. That is, we write A as 1010 and so on up to writing F as 1510.
Example 2.3
2 1 0
3B7 16 = 3 × 16 + B × 16 + 7 × 16
=11 10
= 3 × 256 10 + 11 10 × 16 10 + 7 × 1 10
= 768 10 + 176 10 + 7 10
= 951 10
3 2 1 0
A05C 16 = A × 16 + 0 × 16 + 5 × 16 + C × 16
=10 10 =12 10
= 10 10 × 4096 10 + 0 × 256 10 + 5 × 16 10 + 12 10 × 1 10
= 40960 10 + 0 10 + 80 10 + 12 10
= 41052 10
1 0 −1 −2
50.69 10 = 5 × 10 + 0 × 10 + 6 × 10 + 9 × 10 ,
2 1 0 −1 −2 −3
382.207 10 = 3 × 10 + 8 × 10 + 2 × 10 + 2 × 10 + 0 × 10 + 7 × 10 .
−1 1 1
10 = 1
= = 0.1 10 ,
10 10
−2 1 1
10 = 2
= = 0.01 10 ,
10 100
−3 1 1
10 = 3
= = 0.001 10 ,
10 1000
and so on. Thus, if we take a closer look we can see what is really happening,
1 0 −1 −2
= 5 × 10 + 0 × 10 + 6 × 10 + 9 × 10 .
2 1 0 −1 −2
101.01 2 = 1 × 2 + 0 × 2 + 1 × 2 + 0 × 2 + 1 × 2 ,
3 2 1 0 −1 −2 −3
1101.101 2 = 1 × 2 + 1 × 2 + 0 × 2 + 1 × 2 + 1 × 2 + 0 × 2 + 1 × 2 .
To convert fractional binary numbers to fractional decimal numbers we need to know the negative powers of two,
−1 1 1
2 = 1
= = 0.5 10 ,
2 2
−2 1 1
2 = 2
= = 0.25 10 ,
2 4
−3 1 1
2 = 3
= = 0.125 10 .
2 8
Example 2.4
1 0 −1 −2
10.11 2 = 1 × 2 + 0 × 2 + 1 × 2 + 1 × 2
= 1 × 2 10 + 0 × 1 10 + 1 × 0.5 10 + 1 × 0.25 10
= 2 10 + 0 10 + 0.5 10 + 0.25 10
= 2.75 10
Convert 101.012 to a decimal number.
2 1 0 −1 −2
101.01 2 = 1 × 2 + 0 × 2 + 1 × 2 + 0 × 2 + 1 × 2
= 1 × 4 10 + 0 × 2 10 + 1 × 1 10 + 0 × 0.5 10 + 1 × 0.25 10
= 4 10 + 0 10 + 1 10 + 0 10 + 0.25 10
= 5.25 10
2 1 0 −1 −2
731.42 8 = 7 × 8 + 3 × 8 + 1 × 8 + 4 × 8 + 2 × 8 ,
3 2 1 0 −1 −2 −3
1605.734 8 = 1 × 8 + 6 × 8 + 0 × 8 + 5 × 8 + 7 × 8 + 3 × 8 + 4 × 8 .
In order to convert fractional octal numbers to fractional decimal numbers we need to know the negative powers
of eight,
−1 1 1
8 = 1
= = 0.125 10 ,
8 8
−2 1 1
8 = 2
= = 0.015625 10 ,
8 64
−3 1 1
8 = 3
= = 0.001953125 10 .
8 512
This starts to get messy, but for the questions in this book use all digits after the point. In other words, do not
round.
Example 2.5
0 −1
3.7 8 = 3 × 8 + 7 × 8
= 3 × 1 10 + 7 × 0.125 10
= 3 10 + 0.875 10
= 3.875 10
2 1 0 −1 −2
731.42 8 = 7 × 8 + 3 × 8 + 1 × 8 + 4 × 8 + 2 × 8
= 7 × 64 10 + 3 × 8 10 + 1 × 1 10 + 4 × 0.125 10 + 2 × 0.015625 10
= 473.53125 10
Fractional hexadecimal numbers behave similarly, they just use the digits 0 through F. Fractional hexadecimal
numbers can be written in expanded form,
1 0 −1 −2
8A.41 16 = 8 × 16 + A × 16 + 4 × 16 + 1 × 16 ,
2 1 0 −1 −2 −3
A0D.4EB 16 = A × 16 + 0 × 16 + D × 16 + 4 × 16 + E × 16 + B × 16 .
In order to convert fractional hexadecimal numbers to fractional decimal numbers we need to know the negative
powers of 16,
−1 1 1
16 = 1
= = 0.0625 10 ,
16 16
−2 1 1
16 = 2
= = 0.00390625 10 ,
16 256
−3 1 1
16 = 3
= = 0.000244140625 10 .
16 4096
Example 1.6
0 −1
C.3 16 = C × 16 + 3 × 16
= 12 10 × 1 16 + 3 × 0.0625 10
= 12 10 + 0.1875 10
= 12.1875 10
1 0 −1 −2
8A.41 16 = 8 × 16 + A × 16 + 4 × 16 + 1 × 16
= 8 × 16 10 + 10 10 × 1 10 + 4 × 0.0625 10 + 1 × 0.00390625 10
= 138.25390625 10
Example 2.7
Convert the octal number 348 to a binary number. Notice we can drop the leading zero from the binary
number without changing the value of the number.
34 8 = 3 4
011 100
= 011100 2
= 11100 2
17043 8 = 1 7 0 4 3
= 001111000100011 2
= 1111000100011 2
Convert the octal number 35.2048 to a binary number. Notice we can also drop the nal zeros after the
point without changing the number. We can of course not drop nal zeros before the point. That would
change the value of the number.
35.204 8 = 3 5 . 2 0 4
= 011101 . 010000100 2
= 11101 . 0100001 2
Converting from binary numbers to octal numbers is equally easy. But pay close attention, it is easy to get
confused.
For a whole number we group the binary digits (before the point if there is one) in sets of three from right to
left, adding leading zeros if necessary, and then replace the three digit binary numbers with their
corresponding octal digits from the table.
For fractional numbers we group the binary digits after the point in sets of three from left to right, adding
zeros at the end if necessary. Then we replace the three digit binary numbers with their corresponding octal
digits.
Example 2.8
Converting binary numbers to octal numbers. The second example illustrates lling in zeros at the start of the
binary number and the third example illustrates lling in zeros at both the beginning and the end of the
number.
5 6 1 2
= 5612 8
=001 1 5 5 1
= 11551 8
=010 4 7 4 6 =100
2 4
= 247.464 8
Example 2.9
=001 0 7 6 5 2 3 4 3
= 1076523435 8
Next we convert the second binary number into an octal number.
=001 0 7 6 7 2 3 4 3
= 1076723435 8
1076523435 8 ,
1076723435 8 .
This is a much easier job than comparing the two original binary numbers. But it could be made easier yet by
using hexadecimal numbers.
This table shows the correspondences that are necessary to convert between binary numbers and hexadecimal
numbers. It should be easy for you to verify the relations in the table below. To convert hexadecimal numbers to
binary numbers we just replace each hexadecimal digit with the corresponding binary number.
Binary Numbers Hexadecimal Digits
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F
Example 2.10
E2 16 = E 2
1110 0010
= 11100010 2
= 11111110011010 2
D06.0C 16 = D 0 6 . 0 C
= 110100000110.000011 2
Converting from binary numbers to hexadecimal numbers is similar to converting from binary to octal numbers.
But again, pay close attention to the directions, it is easy to get confused.
For a whole number we group the binary digits (before the point if there is one) in sets of four from right to
left, adding leading zeros if necessary, and then replace the four digit binary numbers with their
corresponding hexadecimal digits from the table.
For fractional numbers we group the binary digits after the point in sets of four from left to right, adding
zeros at the end if necessary. Then we replace the four digit binary numbers with their corresponding
hexadecimal digits.
So, when converting to octal numbers we use groups of three but when converting to hexadecimal numbers we
use groups of four.
Example 2.11
Converting binary numbers to hexadecimal numbers. The second example illustrates lling in zeros at the
start of the binary number and the third example illustrates lling in zeros at both the beginning and the end
of the number.
9 D 6
= 9D6 16
=0010 9 E
= 29E 16
=0011 4 B 3 =0100
3 4
= 34B.34 16
Example 2.12
8 F A A 7 1 D
= 8F AA71D 16
8 F B A 7 1 D
= 8F BA71D 16
8F AA71D,
8F BA71D.
This is a much easier job than comparing the two original binary numbers and even a little easier than
comparing the octal numbers.
Converting between hexadecimal numbers and octal numbers requires that you use the binary number as an in-
between step.
To convert from a hexadecimal number to an octal number you rst convert the hexadecimal number to a
binary number and then convert the binary number to an octal number.
To convert from an octal number to a hexadecimal number you rst convert the octal number to a binary
number and then convert the binary number to a hexadecimal number.
until n = 0
3. Read outputs in reverse order.
In order to use this algorithm we need to understand what both n mod 2 and ndiv2 mean. Both mod and div
are words that de ne a mathematical operation. You have probably encountered the idea before under a different
word.
Let us consider the following long division, which hopefully you remember how to do.
75 div 2 = 37,
75 mod 2 = 1.
It should be obvious to you that when nding n mod 2 for any number n the only possible remainders are 0
and 1 and so these are the only possible values that n mod 2 can take.
Example 1.13
n = 8
n = 9
Example 2.14
n = 37
We can also use our calculators, 37/2 = 18.5. Here 37 div 2 is the whole number part of the answer, or
18. We get 37 mod 2 by taking the fractional part of the answer and multiplying it by 2, so we have
0.5 × 2 = 1.
n = 38
Using our calculators, 38/2 = 19. Here 38 div 2 is the whole number part of the answer, or 19. We get 38
mod 2 by taking the fractional part of the answer and multiplying it by 2, but since the factional part is
simply 0 we have 0 × 2 = 0.
In order to convert a decimal number into a binary number all we have to do is trace the algorithm and then
use the output.
Example 1.15
Convert 1110 to a binary number. In order to do this we must follow the algorithm. As we follow the algorithm
we will trace it in a table. Tracing the algorithm means we follow it step by step.
Step n Output
1. 11 -
2.1 11 11 mod 2 = 1
2.2 5 ← 11 div 2 -
2.1 5 5 mod 2 = 1
2.2 2 ← 5 div 2 -
2.1 2 2 mod 2 = 0
2.2 1 ← 2 div 2 -
2.1 1 1 mod 2 = 1
2.2 0 ← 1 div 2 -
Notice, when n became 0 the algorithm ended. Now we just have to read off the answer. As we can see from
the algorithm we read the answer in reverse order, which means that we read the output numbers from the
bottom to the top. Thus we have
11 10 = 1011 2 .
When doing this yourselves, you probably will not put as much detail in the trace table as we did. We just
wanted to make this example very clear and easy to understand for you. We will do one more example to make
sure you understand.
Example 2.16
Convert 3710 to a binary number. We will make a table that traces the algorithm.
Step n Output
1. 37 -
2.1 37 37 mod 2 = 1
2.2 18 ← 37 div 2 -
2.1 18 18 mod 2 = 0
2.2 9 ← 18 div 2 -
2.1 9 9 mod 2 = 1
2.2 4 ← 9 div 2 -
2.1 4 4 mod 2 = 0
2.2 2 ← 4 div 2 -
2.1 2 2 mod 2 = 0
2.2 1 ← 2 div 2 -
2.1 1 1 mod 2 = 1
2.2 0 ← 1 div 2 -
Again, when n became 0 the algorithm ended. Now we read the answer in reverse order, which means that we
read the output numbers from the bottom to the top. Thus we have
37 10 = 100101 2 .
So far we have considered whole numbers. But we can convert fractional decimal numbers to binary numbers
as well. Here is the algorithm to do this:
Notice that this algorithm requires us to decide how many digits after the point we want to have. In order to
use this algorithm we have to understand what ⌊m⌋ and f rac(m) mean. Fortunately, they are very easy to
understand.
f rac(m) = the fractional part of m
Example 2.17
⌊93.91⌋ = 93
f rac(93.91) = 0.91
Example 2.18
⌊0.75⌋ = 0
f rac(0.75) = 0.75
Example 2.19
⌊16⌋ = 16
f rac(16) = 0
Example 2.20
Convert 0.4210 to a binary number. Find ve digits after the point. That is, n = 0.42 10 and d = 5.
Step m i Output
1. 0.42 - -
2. 0.42 0 -
3.1 0.42 1 ← 0 + 1 -
3.2 0.84 ← 2(0.42) 1 -
3.3 0.84 1 ⌊0.84⌋ = 0
Now we see that i = 5 and can stop. Notice that one does not check that if i is the same as d = 5 until after
step 3.4, which allows us to generate the fth digit in step 3.3. Now we read the outputs in order, placing them
after the point, to give us
0.42 10 ≈ 0.01101 2 .
Of course we could continue nding more digits. The answer we have obtained is not exact, it is just an
approximation. To make this clear we will expand out 0.011012,
−1 −2 −3 −4 −5
0.01101 2 = 0 × 2 + 1 × 2 + 1 × 2 + 0 × 2 + 1 × 2
= 0.40625 10 .
So, to get a more accurate approximation for 0.4210, we would need d to be larger.
In order to convert a mixed decimal number like 37.4210 into a binary number we would need to:
1. Split the number into the whole number part and the fractional number part.
2. Use the rst algorithm on the whole number part.
3. Use the second algorithm on the fractional number part.
4. Join the two results together to get the nal answer.2
Example 2.21
Now we consider converting a decimal number to an octal number. Notice that the only difference in the
algorithms is that the two is replaced by an eight.
Example 2.22
Convert 24210 to an octal number. We will not put as much detail into the table as before.
Step n Output
1. 242 -
2.1 242 2
2.2 30 -
2.1 30 6
2.2 3 -
2.1 3 3
2.2 0 -
Notice, when n became 0 the algorithm ended. Now we read the answer in reverse order to give us
242 10= 362 . 8
The algorithm to convert a fractional number to an octal number works the same as in the binary case, only the
two is replaced by an eight.
Example 2.23
Convert 0.8110 to an octal number. Find six digits after the point. That is, n = 0.81 10 and d = 6.
Step m i Output
1. 0.81 - -
2. 0.81 0 -
3.1 0.81 1 -
3.2 6.48 1 -
3.3 6.48 1 6
3.4 0.48 1 -
3.1 0.48 2 -
3.2 3.84 2 -
3.3 3.84 2 3
3.4 0.84 2 -
3.1 0.84 3 -
3.2 6.72 3 -
3.3 6.72 3 6
Step m i Output
3.4 0.72 3 -
3.1 0.72 4 -
3.2 5.76 4 -
3.3 5.76 4 5
3.4 0.76 4 -
3.1 0.76 5 -
3.2 6.08 5 -
3.3 6.08 5 6
3.4 0.08 5 -
3.1 0.08 6 -
3.2 0.64 6 -
3.3 0.64 6 0
3.4 0.64 6 -
Now we see that i = 6 and can stop. Thus we have 0.81 10 ≈ 0.636560 8 .
Example 1.24
Convert 242.8110 to an octal number using six digits after the point.
We have already used the rst algorithm to nd 242 = 362 10 8 and the second algorithm to nd
0.81 ≈ 0.636560 . Joining these two numbers, we now have 242.81
10 8 10 .
≈ 362.636560 8
Now we consider converting a decimal number to a hexadecimal number. Again, the only difference in the
algorithms is that we now use a sixteen.
Example 2.25
2.2 226 -
2.1 226 2 10 = 2 16
2.2 14 -
2.1 14 14 10 = E 16
2.2 0 -
The algorithm to convert a fractional number to a hexadecimal number works the same as in the binary and
octal cases, the only difference in the algorithms is that we now use a sixteen.
Example 1.26
Convert 0.6310 to a hexadecimal number. Find four digits after the point. That is, n = 0.63 10 and d = 4.
Step m i Output
1. 0.63 - -
2. 0.63 0 -
3.1 0.63 1 -
3.2 10.08 1 -
3.3 10.08 1 10 10 = A 16
3.4 0.08 1 -
3.1 0.08 2 -
3.2 1.28 2 -
3.3 1.28 2 1 10 = 1 16
3.4 0.28 2 -
3.1 0.28 3 -
3.2 4.48 3 -
3.3 4.48 3 4 10 = 4 16
3.4 0.48 3 -
3.1 0.48 4 -
3.2 7.68 4 -
3.3 7.68 4 7 10 = 7 16
3.4 0.68 4 -
Now we see that i = 4 and can stop. Reading the outputs in order we have 0.63 10 ≈ 0.A147 16 .
Example 2.27
Convert 3626.6310 to a hexadecimal number nding four digits after the point.
We have already used the rst algorithm to nd 3626 = E2A and the second algorithm to
10 16 nd
0.63 ≈ 0.A147
10 . Joining these two numbers, we now have 3626.63 ≈ E2A. A147 .
16 10 16
2.5 PROBLEMS
(a) 101
(b) 110
(c) 111
(d) 1011
(e) 1101
(f) 1010
(g) 10010
(h) 10110
(i) 11111
(a) 27
(b) 42
(c) 35
(d) 103
(e) 673
(f) 360
(g) 4721
(h) 2451
(i) 7715
(a) 7254
(b) 1602
(c) 4640
(d) 17530
(e) 72501
(f) 11101
(g) 365310
(h) 772531
(i) 417524
(a) A3
(b) 9F
(c) 17
(d) 3D2
(e) 9A0
(f) AED
(g) F 001
(h) 6D27
(i) 39CB
(a) 802
(b) 6A3
(c) F 2E
(d) 290B
(e) 4C71
(f) 1101
(g) ABCDE
(h) 2E916
(i) 97CA0
(a) 0.11
(b) 0.01
(c) 0.10
(d) 0.110
(e) 0.101
(f) 0.011
(g) 0.0111
(h) 0.0011
(i) 0.1001
(a) 100.110
(b) 110.011
(c) 111.001
(d) 1101.1000
(e) 0110.0111
(f) 1011.1001
(g) 1111.1111
(h) 0001.0001
(i) 1100.0101
(a) 0.4
(b) 0.1
(c) 0.7
(d) 0.72
(e) 0.03
(f) 0.14
(g) 0.553
(h) 0.407
(i) 0.321
(a) 7.5
(b) 4.2
(c) 3.6
(d) 37.63
(e) 56.12
(f) 25.25
(g) 427.014
(h) 510.442
(i) 703.635
(a) 0.7
(b) 0.A
(c) 0.3
(d) 0.A8
(e) 0.4C
(f) 0.82
(g) 0.C48
(h) 0.379
(i) 0.ABC
(a) 9.3
(b) B. E
(c) 4.C
(d) AC. DC
(e) F 2.39
(f) 5E. C5
(g) A04.BB8
(h) 7CF .9F 0
(i) 101.101
(a) 62
(b) 31
(e) 571
(d) 31.72
(e) 42.15
(f) 72.37
(g) 313.011
(h) 643.026
(i) 211.361
(a) A0
(b) 3B
(c) 27
(d) F 2.F 2
(e) 5B.93
(f) 88.7C
(g) 214.3C5
(h) AD2.0BC
(i) 101.011
(a) 10
(b) 110
(c) 11
(d) 110.111
(e) 011.010
(f) 101.001
(g) 11011.001001
(h) 100011.10111001
(i) 11110001.00011101
(a) 1101
(b) 10
(c) 101
(d) 1110.1101
(e) 1011.1010
(f) 0101.0011
(g) 111001011.0011011
(h) 1011101.01011010101
(i) 10111100101.100111
(a) 7
(b) 3
(c) 6
(d) 28
(e) 39
(f) 79
(g) 381
(h) 643
(i) 569
(a) 78
(b) 93
(c) 52
(d) 295
(e) 944
(f) 641
(g) 4862
(h) 5078
(i) 1532
(a) 34
(b) 23
(c) 536
(d) 285
(e) 1897
(f) 8321
(g) 90867
(h) 42778
(i) 28714
Question 2.20 Convert the following decimal numbers to binary numbers. Stop after you have obtained six digits.
(a) 0.5
(b) 0.4
(c) 0.8
(d) 0.32
(e) 0.81
(f) 0.77
(g) 0.239
(h) 0.552
(i) 0.798
Question 2.21 Convert the following decimal numbers to octal numbers. Stop after you have obtained six digits.
(a) 0.9
(b) 0.4
(c) 0.6
(d) 0.83
(e) 0.25
(f) 0.44
(g) 0.482
(h) 0.667
(i) 0.315
Question 2.22 Convert the following decimal numbers to hexadecimal numbers. Stop after you have obtained six
digits.
(a) 0.1
(b) 0.3
(c) 0.9
(d) 0.11
(e) 0.83
(f) 0.47
(g) 0.429
(h) 0.638
(i) 0.314
1 1 This is actually a bit of a simpli cation, there is either a high level of current or a very low level of residual current.
2 2 The technical term for this “joining” is concatenation.
CHAPTER 3
Logic
Logic plays a fundamental role in many areas of computer science, including software
engineering and design, expert systems, and arti cial intelligence. A basic understanding
of logic is necessary for many applications of computer science, as well as for many
future courses.
Example 3.1
A proposition may be false, like the rst proposition in the example, or it may be true,
like the second proposition in the example, or you may not know if it is true or false, like
the third proposition in the example. But think about the third proposition in the
example, “Every even number is the sum of two prime numbers.” It is clear that this
statement can only be either true or false. If we know a proposition is true we say the
truth-value of the proposition is true. If we know a proposition is false we say the truth-
value of the propositions is false. Thus the truth-value of a proposition is either true or
false.
Example 3.2
Some examples of statements that are not propositions.
Example 3.3
Jon is tall.
Chocolate is delicious.
x > 10
Here are some statements that are ambiguous. That means it is not clear if they are
propositions or not. How tall does one have to be to be considered tall? Some people
think chocolate is delicious, some do not. And what is the variable x? Most books would
say these statement are not propositions. Most of the time in computer science you will
not encounter ambiguous statements like this so we will not worry about these kinds of
statements.1
Propositions can be connected together using ve words or phrases called connectives.
There are ve connectives that can be used to connect propositions:
1. not (negation): ¬
2. and: ∧
3. or: ∨
4. if-then (implies): →
5. if-and-only-if (is-equivalent-to): ↔
Notice that each of these ve connectives has a symbol associated with it. You will need
to memorize these symbols. Please be aware that a few books use the symbol ∼ instead
of ¬ for not, which is also sometimes called negation. The phrase “if-then” is also called
“implies” and the phrase “if-and-only-if” is also called “is-equivalent-to.” The ve
connectives can be used to connect propositions into logical expressions. Logical
expressions are often just called expressions. They are also sometimes called compound
statements or just statements. Just like in algebra where one can use a variable to
represent a number, in logic we can use a variable to represent a proposition.
Example 3.4
¬q = It is not raining.
p ∧ q= Today is Monday and it is raining.
¬p ∧ q = Today is not Monday and it is raining.
equivalent-to it is raining.
¬p ↔ q= Today is not Monday if-and-only-if it is raining. = Today is not
Monday is-equivalent-to it is raining.
Here we have seen a few of the possible combinations of the connectives. Of course,
the last example is a little silly. It is just meant to show you how to work with variables
and the connectives. In computer science the propositions would be more serious.
You should notice when using connectives that not takes precedence. In other words,
do the negation rst. For example, if we have p ∧ ¬q this really means p ∧ (¬q), we rst
nd ¬q and then we connect with ∧. Also, in logic, parenthesis are used a lot like they
are used in algebra, to help us determine the order in which we perform the operations.
For example, in p ∧ (q ∨ r) we would do q ∨ r rst before connecting p to it with ∧.
Example 3.5
Let p = "My program runs. " and q = "My program contains mistakes.′′ Write
the expression (p ∧ ¬q) ∨ q in words.
Both of these sentences are correct. We rst do what is inside the parenthesis,
(p ∧ ¬q) and then connect it to q using ∨. Notice in the rst sentence we use a comma
to separate the part of the compound statement that is in parenthesis. In the second
sentence we use the word “either” in addition to a comma to emphasize this
separation. This is more a matter of writing clearly in English than logic.
T F
F T
The variable p of course represents a proposition. (Do not be confused, it does not
matter what letter we use as the variable. We could have used q or r or x or something
else.) The rst row of the table shows that if p is true then ¬p is false. The second row
shows that if p is false then ¬p is true. This should be obvious to you. If a proposition is
true then the negation of that proposition must be false. Similarly, if a proposition is
false then the negation of that proposition must be true.
Example 3.6
T T T
T F F
F T F
F F F
Since the connective and actually connects two propositions, the truth table for and
has two propositions in it, p and q. The proposition p could be either true or false.
Similarly, the proposition q could be either true or false. A truth table must have enough
rows to include every possible combination of true or false for the variables in the table.
Therefore the number of rows a truth table has is equal to 2n where n is the number of
proposition variables we have. Here we have two proposition variables, p and q, so the
table must have 2 = 4 rows in it. According to the truth table, the only way to have
2
p ∧ q be true is if both p and q are true. If we think about this in terms of English
Example 3.7
Let
Next we will consider the connective or. However, with this word we have to be
careful. In English the word or can be used in two distinct ways. Consider the following
two sentences:
If you are at a restaurant and the menu says you may have either tea or coffee with
your breakfast, then you are expected to either choose tea or choose coffee. You cannot
choose both. The or in this situation is called an exclusive-or. You can chose one or the
other but not both. If a movie has a discount for students or children then what about
children who are students? The discount is available to people who are students, it is
available to people who are children, and it is available to people who are both students
and children. In this situation the or is called an inclusive-or. The connective ∨ refers to
the inclusive-or. The expression q ∨ p is true if p is true, if q is true, or if both p and q
are true. This helps us understand the truth table for or:
p q p ∨ q
T T T
T F T
F T T
p q p ∨ q
F F F
Example 3.8
Let
is clearly true since both parts of the statement is true. But suppose we have
is still true since one of the parts of the statement is true. Similarly, if we have
is true since again one of the parts of the statement is true. Finally, suppose we had
Example 3.9
Though we will not use it in this chapter, the exclusive-or is important as well in both
logic and Boolean algebra. We simply use xor to represent the exclusive-or. The
expression pxorq is true when only one of the propositions p or q is true. That means
that if both p and q are true then pxorq is false. Here is the truth table for the
exclusive-or.
p q pxorq
T T F
T F T
F T T
F F F
The next two connectives, if-then (implies) and if-and-only-if (is-equivalent-to), can
seem a little strange if we rely too heavily on language for our understanding. We can
come up with some very strange examples that do not make much sense. This is because
logic in the real world is usually applied to mathematical propositions, not silly
propositions like “Paris is in France” or “Today is Monday.” Logic, when applied to
mathematical propositions, works perfectly.
Next we will consider the truth table for if-and-only-if. Another way of saying if-and-
only-if is to say is-equivalent-to. In English two things are considered to be equivalent if
they are the same in some way. In logic what is important is the truth-value of
propositions or expressions. Therefore, in logic two statements are considered equivalent
if they have the same truth-value. In other words, two statements are considered
equivalent if they are either both true or both false. Thus the truth table for if-and-only-if
is given by:
p q p ↔ q
T T T
T F F
F T F
F F T
Example 3.10
is true. But as a sentence in English it does not make a whole lot of sense. Why
should we expect the locations of Paris and London to be related to each other? Now
suppose we have
is now false since one part of the statement is true and the other part is false. But
again, the two parts of the statement seem unrelated to each other. Similarly, if we
had
p = Paris is in Japan. (False) and q = London is in England. (True)
is also false since one part of the statement is true and the other part is false. But
again, the two parts of the statement seem unrelated to each other. Finally, suppose
we had
is true according to our table since both parts of the statement are false. But again, as
an English sentence it makes very little sense. Just remember, when using logic in
real life, in problems that come from computer science or mathematics, logic works
perfectly.
We can convince ourselves that the if-and-only-if truth table works since we can use
the argument that true statements are equivalent in some sense and false statements are
equivalent in some sense. In other words, it is the truth-value of the propositions that is
important. We will also use this idea to try to understand the if-then truth table. In the
statement “ p → q” the rst part of the statement, p, is called the premise and the second
part of the statement, q, is called the conclusion. Here is one way to think about it:
A true premise can only imply a true conclusion. (So if p is true and q is true then
p → q is also true.)
A true premise can never imply a false conclusion. (So if p is true and q is false
then p → q must be false.)
A false premise, because it is false, can imply anything. (So if p is false and q is
true then p → q is true. But also, if p is false and q is false then p → q is also true. )
This gives the following truth table for the if-then connective:
p q p → q
T T T
T F F
F T T
F F T
Example 3.11
Let
is true. But again as a sentence in English this does not make a whole lot of sense.
Why should we expect the locations of Paris and London to be related to each other?
Now suppose we have
is now false since the premise, or rst part of the statement, is true and the
conclusion, or second part of the statement, is false. But again, the two parts of the
statement seem unrelated to each other. Now suppose we had
is true according to our table since the premise is false and the conclusion is true. The
fact that the whole statement is considered to be true just seems silly with this
example. Now suppose
is true according to our table since the premise is false and the conclusion is false.
Again, the fact that the whole statement is considered to be true just seems silly with
this example. But when using logic in problems that come from computer science or
mathematics, everything works perfectly.
Example 3.12
If p is true and q is false nd the truth value of (p ∧ ¬q) ∨ q.
(T ∧ ¬F ) ∨ F substitute truth-values
The symbol ≡ is read “is equivalent to.” This is different than the if-and-only-if
connective which is also known as is-equivalent-to. Here the symbol ≡ operates a lot like
the equal sign in algebra. In algebra you would write 2(x + y) = 2x + 2y to say that
both 2(x + y) and 2x + 2y are the same expression just written in different ways. In
logic we use ≡ to say both sides have the same truth-value.
Example 3.13
¬(¬T ∧ ¬T )
≡ ¬(F ∧ F )
≡ ¬F
≡ T
Example 3.14
If p and q are both false nd the truth value of ¬(p ∧ q) → (¬q ∨ ¬q).
¬(F ∧ F ) → (¬F ∨ ¬F )
≡ ¬(F ∧ F ) → (T ∨ T )
≡ ¬F → T
≡ T → T
≡ T
Often you are asked simply to nd the truth table of a compound statement. This
means you are to nd the truth-value of the compound statement for every possible
proposition truth-value. You need to make and ll-out a truth-table. The number of rows
your truth table needs is 2n where n is the number of individual propositions your
compound statement has. The truth table for ¬(p ∧ q) would need 2 = 4 rows since it
2
has two propositions (p and q); the truth table for (¬p ∧ q) ∨ r would need 2 = 8 rows
3
since it has three propositions (p, q, and r); and the truth table for ¬(p ∨ q) → (¬r ∧ s)
would need 2 = 16 rows since it has four propositions (p, q, r, and s). To gure out the
4
columns a truth table needs it may be helpful to make a expression tree. (Trees will be
studied later in chapter 10.)
Example 3.15
Make an expression tree for ¬(p ∧ q) and use that to construct a truth table.
This negation is shown by connecting ∧ to ¬ with an edge. Thus we get ¬(p ∧ q). The
truth table requires a column for each vertex in the expression tree.
p q p ∧ q ¬(p ∧ q)
T T
T F
F T
F F
In the rst two columns of the truth table we have lled in every possible
combination of truth values for p and q. The rest of the truth table needs to be lled
in.
Example 3.16
Make an expression tree for (p ∧ ¬q) ∨ q and use that to construct a truth table.
In the expression tree the variables are the vertices at the bottom. The rst thing we
do is nd ¬q. We then ∧ together p and ¬q to get p ∧ ¬q. Finally, we have to ∨ this
together with q to get (p ∧ ¬q) ∨ q. We make a truth table using the vertices of the
expression tree; each vertex results in a column in the truth table. Notice, in the truth
table we do not need to put the variable q in twice, once is enough.
p q ¬q p ∧ ¬q (p ∧ ¬q) ∨ q
T T
T F
F T
F F
Example 3.17
Make an expression tree for ¬(¬p ∧ q) ↔ (¬q ∨ r) and use that to construct a truth
table.
Study ¬(¬p ∧ q) ↔ (¬q ∨ r) and the expression tree until the construction of the tree
is clear. We make the columns in the truth table using the vertices of the expression
tree. We do not need to put the variable q in twice.
p q r ¬p ¬p ∧ q ¬(¬p ∧ q) ¬q ¬q ∨ r ¬(¬p ∧ q) ↔ (¬q ∨ r)
T T T
T T F
p q r ¬p ¬p ∧ q ¬(¬p ∧ q) ¬q ¬q ∨ r ¬(¬p ∧ q) ↔ (¬q ∨ r)
T F T
T F F
F T T
F T F
F F T
F F F
Example 3.18
T T F
T F T
F T F
F F T
Next we ll in the fourth column using the rst and third columns and the truth table
for ∧.
p q ¬q p ∧ ¬q (p ∧ ¬q) ∨ q
T T F F
T F T T
F T F F
F F T F
Finally we ll in the fth column using the second and fourth columns and the truth
table for ∨.
p q ¬q p ∧ ¬q (p ∧ ¬q) ∨ q
T T F F T
T F T T T
F T F F T
F F T F F
With the truth table we can see exactly when the compound statement (p ∧ ¬q) ∨ q is
true and when it is false. It is false when both p and q are false and true otherwise.
Notice this is exactly the same as the truth table for p ∨ q.
Example 3.19
Show that ¬(p ∧ q) → (¬p ∨ ¬q) it a tautology. We rst construct the expression
tree.
We use the expression tree to construct the truth table which we then ll out.
p q p ∧ q ¬(p ∧ q) ¬p ¬q ¬p ∨ ¬q ¬(p ∧ q) → (¬p ∨ ¬q)
T T T F F F F T
T F F T F T T T
F T F T T F T T
F F F T T T T T
We can see that the expression ¬(p ∧ q) → (¬p ∨ ¬q) is true regardless of the truth-
values of the propositions. Thus ¬(p ∧ q) → (¬p ∨ ¬q) is called a tautology. We
sometimes say that ¬(p ∧ q) → (¬p ∨ ¬q) is tautologically true.
Example 3.20
We then use the expression tree to construct the truth table which we then ll out.
p q p ∧ q ¬p (p ∧ q) ∧ ¬p
p q p ∧ q ¬p (p ∧ q) ∧ ¬p
T T T F F
T F F F F
F T F T F
F F F T F
Example 3.21
Show that the expressions ¬(p ∧ q) and ¬p ∨ ¬q are logically equivalent. This
requires constructing the truth table for each expression.
p q p ∧ q ¬(p ∧ q)
T T T F
T F F T
F T F T
F F F T
and
p q ¬p ¬q ¬p ∨ ¬q
T T F F F
T F F T T
F T T F T
F F T T T
Notice how the two expressions have the same truth-values for every combination of
variable truth-value. When both p and q are true then both expressions are false.
When p is true and q is false then both expressions are true. When p is false and q is
true then both expressions are true. When both p and q are false then both expressions
are false. Thus the expression ¬(p ∧ q) is logically equivalent to the expression
¬p ∨ ¬q . This is written as
¬(p ∧ q) ≡ ¬p ∨ ¬q.
Example 3.22
Show that the expressions ¬(p ∨ q) and ¬p ∧ ¬q are logically equivalent. We do this
by constructing truth tables for the two expressions.
p q p ∨ q ¬(p ∨ q)
T T T F
T F T F
F T T F
F F F T
and
p q ¬p ¬q ¬p ∧ ¬q
T T F F F
T F F T F
F T T F F
F F T T T
Again, the two expressions have the same truth-values for every combination of
variable truth-value. When both p and q are true then both expressions are false.
When p is true and q is false then both expressions are false. When p is false and q is
true then both expressions are false. When both p and q are false then both
expressions are true. Thus the expression ¬(p ∨ q) is logically equivalent to the
expression ¬p ∧ ¬q,
¬(p ∨ q) ≡ ¬p ∧ ¬q.
Example 3.23
T T T
T F F
F T T
F F T
and
p q ¬p ¬q ¬q → ¬p
T T F F T
T F F T F
F T T F T
F F T T T
p → q ≡ ¬q → ¬p.
This may surprise you, but this equivalence is actually one of the most important
relationships in mathematics and computer science. As we said, the expression ¬q → ¬p
is called the contrapositive of p → q. It is a fundamental way that mathematicians and
computer scientists prove things. Many times you want to show a statement p → q is
true but showing this statement is true is dif cult while it is easy to show the
contrapositive ¬q → ¬p is true. But since these two statements are equivalent then
showing the contrapositive of p → q is true is the same thing as showing that p → q is
true.
There are two special laws that are almost like de nitions instead of laws. They allow
us to write → and ↔ in terms of ¬, ∧, and ∨. These can be shown to be true in exactly
the same way as in the last example.
Name Laws
Implication law p → q ≡ ¬p ∨ q
There is also a standard list of equivalence relations that are often called the laws of
logic. Each of these laws of logic can be shown to be true just as in the examples above.
We can construct the truth tables for the expression on each side of the ≡ sign and
compare them. It is a good idea for you to do that for these laws and convince yourself
that they are true. We have already done this for both of DeMorgan's laws.
Name Laws of Logic
Commutative laws (a) p ∧ q ≡ q ∧ p (b) p ∨ q ≡ q ∨ p
Associative laws (a) (p ∧ q) ∧ r ≡ p ∧ (q ∧ r) (b) (p ∨ q) ∨ r ≡ p ∨ (q ∨ r)
Distributive laws (a) p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r) (b) p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r)
Identity laws (a) p ∧ T ≡ p (b) p ∨ F ≡ p
Name Laws of Logic
Inverse laws (a) p ∧ ¬p ≡ F (b) p ∨ ¬p ≡ T
Double negation law ¬¬p ≡ p
The reason the laws of logic are so important is that we can often use them to simplify
complicated compound expressions. Doing this feels a lot like simplifying expressions
in algebra, but you have to be careful. The laws of logic are a bit different than the laws
of algebra. You want to make sure that you can justify each step with a law of logic.
Example 3.24
Simplify the expression (p ∨ ¬q) ∧ (p ∨ q) using the laws of logic. Justify each step.
(p ∨ ¬q) ∧ (p ∨ q)
≡ p ∨ F Inverse law
≡ p Identity law.
Example 3.25
Simplify the expression ¬\big[p → ¬(p ∧ q)\big] using the laws of logic. Justify
each step.
≡ (p ∧ p) ∧ q Associative law
≡ p ∧ q Idempotent law
Example 3.26
Simplify the expression ¬\big[p ∨ (q ∧ ¬p)\big] using the laws of logic. Justify each
step.
¬[p ∨ (q ∧ ¬p)]
≡ ¬p ∧ ¬q Identity law
3.6 PROBLEMS
Question 3.2 Let p = “Jon plays baseball” and q = “Ron plays football.” Write the
following logical statements in words.
(a) p ∨ q
(b) q ∧ p
(c) ¬p ∧ q
(d) p ∧ ¬q
(e) ¬q ∨ ¬p
(f) ¬(q ∨ p)
Question 3.3 Suppose p is true and q is false. Find the truth value of the following
compound statements.
(a) p ∧ q
(b) q ∧ p
(c) ¬q ∨ p
(d) ¬p ∨ q
(e) p ∧ ¬q
(f) ¬q ∨ ¬p
(g) p → q
(h) ¬q → p
(i) ¬q → ¬p
(j) p ↔ q
(k) ¬q ↔ p
(l) ¬p ↔ ¬q
Question 3.4 Construct the truth table for the following compound statements.
(a) p ∧ q
(b) q ∧ p
(c) ¬q ∨ p
(d) ¬p ∨ q
(e) p ∧ ¬q
(f) ¬q ∨ ¬p
(g) p → q
(h) ¬q → p
(i) ¬q → ¬p
(j) p ↔ q
(k) ¬q ↔ p
(l) ¬p ↔ ¬q
Question 3.5 Show the Implication law p → q ≡ ¬p ∨ q by constructing a truth table for
each side of the implication and showing these two truth tables are equivalent.
Question 3.7 Construct the truth table for the following compound statements.
(a) (p ∨ ¬r) → q
(b) (q ∧ p) → ¬p
(c) ¬p → (¬p ∨ q)
(d) ¬(p ∧ ¬q) ∨ r
(e) ¬[(¬p ∧ q) ∨ r]
(f) (p ∧ ¬q) ∧ (r ∨ q)
(g) (p ∨ q) ↔ [p ∨ (q ∧ r)]
(h) (q ∧ ¬r) ↔ ¬(p ∨ r)
(i) (p ↔ q) ∧ (r ↔ q)
Question 3.8 (Easy) Simplify the following Logical expressions with the laws of logic.
(a) p ∨ p
(b) p ∨ ¬p
(c) ¬(p ∨ ¬p)
(d) p ∨ (p ∧ q)
(e) p ∨ (¬p ∧ q)
(f) p ∧ (¬p ∨ q)
(h) p ∧ p
(h) p ∧ (p ∨ q)
(i) p ∧ (p ∨ q ∨ r)
(j) (p ∧ q) ∨ (p ∧ ¬q)
(k) ¬(¬p ∨ ¬p)
(l) (p ∧ q) ∨ (¬p ∧ q)
Question 3.9 (Moderate) Simplify the following Logical expressions with the laws of
logic.
(a) (¬p ∨ ¬q) ∧ (¬p ∨ q)
(b) q ∨ (q ∧ ¬q)
(c) ¬p ∨ (q ∧ ¬p)
(d) (p ∨ ¬q) ∧ (p ∨ q)
(e) r ∨ [r ∨ (r ∧ p)]
(f) p ∧ [p ∨ (p ∧ q)]
(g) r ∨ (r ∧ ¬p ∧ q ∧ s)
(h) ¬r ∧ ¬(r ∧ p ∧ q ∧ s)
Question 3.10 (Dif cult) Simplify the following Logical expressions with the laws of
logic.
(a) (p ∧ ¬q) → ¬q
(b) (¬p ∧ q) → ¬q
(c) (¬p ∧ q) → ¬p
(d) (¬p ∨ q) → ¬p
An understanding of sets and set notation is necessary for you in the future. The language of set theory is used
throughout computer science. Relations between sets are also very important in some areas of computer science,
especially when studying databases. We will not cover databases in this course, but we will cover the basic
de nitions and ideas that you will need to know later.
Example 4.1
\big{apple, orange, banana\big} is a set that has three elements, the words apple, orange, and banana.
{1, 2, 3, 4, 5} is a set that has ve elements, the numbers 1, 2, 3, 4, and 5.
{1, 2, 3, … , 50} is a set that has fty elements, the positive integers 1 through 50.
{2, 4, 6, 8, 10, 12, …}is a set that contains all the positive even integers. This set has an in nite number
of elements since there are an in nite number of positive even integers.
{a, b, c} is a set that contains the letters a, b, and c.
{a, b, c, … , z} is a set that contains the lowercase letters of the alphabet.
If x is an element of the set S then we write x ∈ S . They symbol ∈ is read “is an element of.” If x is not an
element of set S then we would write x ∉ S . The symbol ∉ is read “is not an element of.”
Example 4.2
Elements of sets.
Now we will introduce some of the most important sets in mathematics and computer science.
N
R
=
∣
the set of natural numbers
{1, 2, 3, 4, …},
= {… , −4,
{x x =
n
− 3, − 2,
m
− 1, 0, 1, 2, 3, 4, …},
Sometimes the natural numbers1 are also called the whole numbers or counting numbers. Also, the integer
numbers are often simply called the counting numbersintegers, the rational numbers are often simply called the
rationals, and the real numbers are often simply called the reals. It is dif cult to give a precise de nition of the
real numbers, but you should be familiar with them as the real number line. That is a good way to think about the
real numbers.
Also, notice how we de ned the rational numbers ℚ. Sets can be written in one of two different ways. They
can be written in an enumerated formenumerated form (of set) or in a predicate form. Most of the sets above
were written in an enumerated form. To enumerate simply means to list, and in most of the above examples you
can see that we simply listed the elements in the set inside the curly-brackets. Writing a set in predicate form
means we use an expression to help us de ne the set. This is how we de ned predicate form (of set)ℚ above. A
predicatepredicate is a statement involving one or more variables such that when we assign values to the
variables the statement becomes a proposition which is either true for false. We write P (x) to mean a predicate
of x and read it as “pee-of-ex.”
Sets in predicate form are written either as
{x | P (x)},
{x ∈ S | P (x)},
Example 4.3
5 ≤ x ≤ 10
is a predicate of x. If we assign the value 7 to the variable x the statement becomes the proposition
5 ≤ 7 ≤ 10, which is true. If we assign the value −2 to the variable x the statement becomes the proposition
m
x = where m, n ∈ Z and n ≠ 0
n
is a predicate in x. If we assign the value 3.74 to the variable x we have the statement
m
3.74 = where m, n ∈ Z and n ≠ 0
n
100
. If we assign the value π to the
variable x then we have the statement
m
π = where m, n ∈ Z
n
n
.
Example 4.5
Q = {x x =
m
n
f or m, n ∈ Z, n ≠ 0} = The set of numbers m
n
where both m and n are elements of ℤ
and n ≠ 0= The set of all fractions.
Now consider the sets {1, 7, 3} and {3, 7, 1}. These are both the same set so we will call them equal to each
other. The order in which we write the elements does not matter at all. In fact, repeated elements do not matter
either. Now consider the sets {1, 7, 3} and {1, 3, 1, 7}. These are also the same set even though the element 1 was
repeated twice in the second set.
Example 4.6
{1, 7, 3} = {1, 3, 1, 7}
1 1 2 3 4 5 6
{ } = { , , , , , , …}
2 2 4 6 8 10 12
{
1
3
} = {
1
3
,
2
6
,
3
9
,
4
12
,
15
5
,
6
18
, …} There are many different ways to write any fraction.
There is one last, but very important, point that needs to be made. Sets can be elements of sets. This can cause
a lot of confusion if you are not careful. Let us consider the following set,
Here we have put all the elements of the set in gray. These elements are separated by black commas. So we
would say
a ∈ S, b ∈ S, c ∈ S, d ∈ S, h ∈ S, and i ∈ S.
These elements of S are actually sets themselves. This is allowable. Often in computer science there are
situations where sets are considered elements of other sets. If you are given an example like this you must look
very closely at the commas. We use commas to separate elements in a set. Here the commas that separate the
elements of the set S are shown in black. But consider the element {b, c} in S. It is a set that also uses commas to
separate the elements b and c in it. In the set S this comma is in gray since it is part of the element {b, c}. Most
of the time we will not write the elements of a set in gray so you must be careful and pay special attention to the
commas.
Example 4.7
{2, 3, 5} ⊆ {1, 2, 3, 4, 5}. Furthermore, since {2, 3, 5} ≠ {1, 2, 3, 4, 5} then {2, 3, 5} is a proper subset
of {1, 2, 3, 4, 5} and we can also write {2, 3, 5} ⊂ {1, 2, 3, 4, 5}.
. Again, since {2, 4, 6, 8} ≠ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} we have
{2, 4, 6, 8} ⊆ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
In the above example we saw the empty set, { } ⊆ {2, 4, 6, 8, 10}. The empty set is exactly that, a set that is
empty. Notice we did not put any elements at all inside the curly-brackets. The curly-brackets are empty. Every
set has the empty set as a subset. The empty set is so special it has its own symbol,
ϕ = {}.
The empty set is also called the null set. Null is an old-fashioned word meaning nothing. It is interesting to
notice that the empty set can be written in predicate form using a predicate that is always false,
ϕ = {x|x ≠ x}.
Clearly, there are no values of x that are not equal to themselves, thus this set has no values in it; it is empty.
Now let us return to the example we gave in the last section where some of the elements of the set S were also
sets themselves,
Again we have written the elements of S in gray. Pay close attention to the commas. Now we will write S with
the elements b and c in gray, the element {b, c} in gray boldface, and the rest of the elements in black,
{b, c} ⊂ S.
In other words {b, c} is the subset of S that contains the elements b and c written in gray. But if we write
{{b, c}} ⊂ S then what we mean is
{{b, c}} ⊂ S.
In other words {{b, c}} is the subset of S that contains the element {b, c} written in gray boldface. The subset
{b, c, {b, c}} would of course mean
Example 4.8
Using the same set as before we give some examples of both elements and subsets to help you understand the
difference;
a ∈ S, b ∈ S, c ∈ S, d ∈ S, h ∈ S, i ∈ S.
The following are all subsets of S that contain one element from S;
The following are all also subsets of S that contain one element from S;
The following are some examples of subsets of S that contain two elements;
The following are some examples of subsets of S that contain three elements;
{c, d, i} ⊂ S, {h, i, {h, i}} ⊂ S, {d, {b, c}, {h, i}} ⊂ S, {{h, i}, {a, d, h}, {a, b, c}} ⊂ S.
There is another set that is very important in set theory called the universal set. The universal set is not the
universe, but it is the “universe” of our problem. That means the universal set is the set that contains all the
elements that arise in a particular problem.2 So what we call the universal set for a problem depends on the
problem. Each problem could have a different universal set. The universal set is usually represented by U . We
will use the universal set in the next section.
The intersection of two sets A and B is given by
A ∩
∣
B = {x x ∈ A and x ∈ B }.
That is, the intersection of two sets A and B consists of the set of elements that are in set A and also in set B.
Another way of saying this is that the intersection of two sets consists of the elements that are common to both
sets.
Example 4.9
Intersections of sets.
In the above example when A was the set of even numbers and B was the set of odd numbers we saw that
A ∩ B = ∅ . In other words, there are no elements common to the set of even numbers and the set of odd
numbers. A number simply cannot be both even and odd at the same time. These sets are called disjoint. If A and
B are sets and A ∩ B = ∅ then A and B are said to be disjoint.
The union of two sets A and B is given by
A ∪ B = {x x ∈ A or x ∈ B}.
That is, the union of two sets A and B consists of all the elements in A together with all the elements in B.
Example 4.10
Unions of sets.
U is.
If A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7} then A ∪ B = {1, 2, 3, 4, 5, 6, 7}.
If A = {1, 2, 3, 4, 5, 6, 7, 8} and B = {2, 4, 6, 8, 10, 12} then A ∪ B = {1, 2, 3, 4, 5, 6, 7, 8, 10, 12}.
If A = {2, 4, 6, 8, 10, 12, …} and B = {1, 3, 5, 7, 9, 11, …} then A ∪ B = N.
The complement of a set A is the set of all elements in the universal set but not in A. The complement of A is
written as Ac or as A or as A′. In order to nd the complement of a set you need to know what the universal set
Example 4.11
Complements of sets.
If U
If U
= {1, 2, 3, 4, 5, 6} and A = {2, 4, 6} then A
= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
c
= {1, 3, 5}
A − B = {x | x ∈ Aand x ∉ B }.
Example 4.12
Differences of sets.
The cardinality of a nite set is the number of elements in the set. If A is a set the cardinality of A is written as
|A| or as n(A). If the set has an in nite number of elements then we say the cardinality of the set is in nite.
Since the empty set has no elements then the cardinality of the empty set is zero.
Example 4.13
Cardinality of sets.
The power set of a set A is the set that contains all the subsets of set A. The power set of A is written as P(A)
. Notice, here we are talking about a set that actually has sets as its elements. That is entirely possible.
Example 4.14
We will nd the power set of the set A = {a, b, c}. Notice that A has three elements, the letters a, b, and c. We
list the subsets of A:
∅, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}.
Notice that the empty set is a subset of A and the set A is a subsets of itself. These eight sets are the elements
of the power set of A. The power set of A is given by
P(A) = {∅, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}}.
Given a subset of A, for each element in A there are two possibilities, either that the element is in the subset or
it is not in the subset. Let us look at a few more examples to see how this works.
Example 4.15
Let A = {a}. The set A only has one element. There are two possible subsets, one that has the element a
in it, {a}, and one that does not have the element a in it, { }. So A has 2 = 2 subsets and 1
A = {a, b} . The set A has two elements in it. For each element there are two possibilities, either it is in
the set or it is not in the set. Thus we have
2 ⋅ 2
Either a is in Either b is in
f our possibilities
2 ⋅ 2 ⋅ 2
eight possibilities
By now the pattern should be clear. This leads to the following theorem.
Theorem 4.1 If |A| = n then |P(A)| = 2 . n
Example 4.16
Supposes A = {a, b, c, d}. How many subsets does A have? How many proper subsets does A have?
The power set is the set that contains all the subsets of A, so the cardinality of the power set is equal to the
number of subsets that A has. Since |A| = 4 then P(A) = 2 = 16. Thus A has 16 subsets. However, one of
4
Suppose that A has 127 proper subsets. How many elements does A have?
Since A has 127 proper subsets it must have 128 subsets in all. This is because there is one subset that is not a
proper subset, namely A itself, which must be added to 127. This means that |P(A)| = 128. We now need to
nd the power of two that is equal to 128. Since 2 = 128 we have |A| = 7. In other words, A has seven
7
elements.
Recall, that in sets the order we write the elements does not matter. Thus {a, b, c} is exactly the same as
{b, c, a}or {a, c, b} or {b, a, c} and so on. But there are many cases in computer science where order is very
important. We want a way to think about things that have an order. An ordered n-tuple is a list of n elements in a
particular order. Ordered n-tuples are written in parenthesis instead of curly-brackets to distinguish them from
sets. So (2, 3) is an ordered two-tuple while {2, 3} is a set and (3, −2, 4) is an ordered three-tuple while
{3, −2, 4} is a set. But while the order of elements in a set does not matter, the order of the terms in an ordered
(3, − 2, 4) ≠ (−2, 3, 4)
but
In fact, you have doubtless already studied ordered two-tuples and ordered three-tuples when you studied points
in two or three dimensions. Points in n dimensions are nothing more than ordered n-tuples of numbers. See Figs.
4.1 and 4.2 that show how points are n-tuples. But of course the order we use when writing down a point is very
important. In the plane ℝ2 the point (2, −1) is very different from the point (−1, 2). Similarly, in three-
dimensional space ℝ3 the point (−2, 5, 4) is very different from the point (5, −2, 4), which is different from the
point (4, −2, 5), and so on.
Can we have a set that consists of ordered n-tuples as elements? Of course we can. The Cartesian product3 of
two sets A and B is de ned to be
That is, A × B is the set of all ordered pairs where the rst term in the ordered pair comes from set A and the
second term in the ordered pair comes from B. Similarly, the Cartesian product of three sets A, B, and C is
de ned to be
That is, A × B × C is the set of all ordered three-tuples where the rst term in the ordered three-tuple comes
from set A, the second term in the ordered three-tuple comes from B, and the third term in the ordered three-
tuple comes from C. In general we have
S 1 × S 2 × ⋯ × S n = {(x 1 , x 2 , … , x n )|x 1 ∈ S 1 , x 2 ∈ S 2 , ⋯ , x n ∈ S n }.
Example 4.18
A × B = {(a, 2), (b, 2), (c, 2), (a, 4), (b, 4), (c, 4)}.
In fact, this explains why three-dimensional space is often written as ℝ3, since using the rule of
exponents we can write R = R × R × R. See Fig. 4.2.
3
Let L = {a, b, c, d, … , x, y, z}. The set of all strings of four letters is given by L × L × L × L.
Example 4.19
In Fig. 4.4 we have the universal set U which consists of all the lowercase letters from the English alphabet.
There are also two sets in U , set A and set B. The letters in set A are shown inside the circle labeled A and the
elements in set B are shown inside circle B. Find sets A and B, and then nd A ∩ B, A ∪ B, Ac, Bc, (A ∪ B) , c
and (A ∪ B) . c
Figure 4.4 An illustration of how Venn diagrams work. Here the universal set
U
U
consists of the lowercase letters from the English alphabet. The uppercase letters A and B represent the two sets shown as circles.
We nd sets A and B by listing all the elements that are in the circle labeled A and in the circle labeled B.
A = {p, x, r, c, e, m, n, j, a, s, b},
B = {j, a, s, b, k, f , g, z, w, y, v}.
A ∩ B = {j, a, s, b},
A ∪ B = {p, x, r, c, e, m, n, j, a, s, b, k, f , g, z, w, y, v},
c
A = {i, u, t, o, h, l, d, q, k, f , g, z, w, y, v},
c
B = {i, u, t, o, h, l, d, q, p, x, r, c, e, m, n},
c
(A ∪ B) = {i, u, t, o, h, l, d, q},
c
(A ∩ B) = {p, x, r, c, e, m, n, k, f , g, z, w, y, v, i, u, t, o, h, l, d, q}.
It is also of course possible to draw a Venn diagram with more than two sets. Fig. 4.5 is a Venn diagram with
three sets A, B, and C along with elements shown. A lot of times Venn diagrams are drawn with the number of
elements in each region instead of with the actual elements themselves. An example of this is given in Fig. 4.6.
When we do this we do not know what the elements are and so we cannot write out the sets A, B, or C in
enumerated form. However, often we are more interested in simply knowing how many elements are in each set.
In other words, we only care about the cardinalities of the set. We can still use Venn diagrams for this.
Figure 4.5 A Venn diagram with three sets, A, B, and C, and with elements shown.
Figure 4.6 In this Venn diagram the number of elements in each region is indicted, not the actual elements themselves.
Example 4.20
∣
Using the Venn diagram in Fig. 4.6 nd |A|, |B|, |C|, |A ∩ B|,
|A ∪ C|, |B ∪ C|, |A ∪ B ∪ C|, |A |, and |(A ∪ B ∪ C) |.
(A
A
∪
∩
B
c
∪
∩
|A |
C)
|A|
|B|
|C|
c
=
=
3,
c
9 + 5 + 3 + 4 = 21
12 + 5 + 3 + 7 = 27,
8 + 7 + 3 + 4 = 22,
5 + 3 = 8,
4 + 3 = 7,
7 + 3 = 10,
|A ∩ C|
9 + 4 + 5 + 3 + 12 + 7 = 40,
9 + 5 + 4 + 3 + 8 + 7 = 36,
12 + 5 + 7 + 3 + 8 + 4 = 39,
,
9 + 5 + 12 + 4 + 3 + 7 + 8 = 48,
12 + 7 + 8 + 11 = 38,
11.
|B ∩ C| , |A ∩ B ∩ C|
In a general situation we often use shading to indicate the elements in a set. In Fig. 4.6(a) the set U is shaded
which indicates we are considering all the elements in the “universe.” In Fig. 4.7(b) the set A is shaded which
indicates we are considering all the elements in the set A. In Fig 4.7(c) the set B is shaded indicating we are
considering all the elements in set B. Figs. 4.7(d) through 4.7(k) show all the other possibilities as well. These
pictures are a very nice way to visualize what union and intersection and complement all mean. Be careful
though, with more then three sets Venn diagrams can start to be misleading and confusing.
, |A ∪ B| ,
Figure 4.7 Using shading to indicate various regions in Venn diagrams.
operate on subsets of the universal set. Thus we say that ∩, ∪, and operate on elements of P(U ), the powerset
c
of U .
Venn diagrams can be very useful in helping us understand the laws of set theory. But be careful, Venn diagrams
can be used to understand the laws of set theory, but they cannot be used to actually prove the laws of set theory.
To prove the laws of set theory you need to use the actual de nitions of the operations.
Name Laws of Set Theory
Commutative laws (a) A ∩ B = B ∩ A (b) A ∪ B = B ∪ A
Associative laws (a) (A ∩ B) ∩ C = A ∩ (B ∩ C) (b) (A ∪ B) ∪ C = A ∪ (B ∪ C)
Distributive laws (a) A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) (b) A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)
Identity laws (a) A ∩ U = A (b) A ∪ ∅ = A
Inverse laws (a) A ∩ A = ∅ c
(b) A ∪ A = U
c
Use Venn diagrams to argue that the double complement law is true.
On the left the set A is shown. On the right we see Ac. But just by looking at these pictures it is obvious that
(A ) must be the same as A.
c c
Example 4.22
On the left we show Ac. In the middle we show Bc. On the right we see the union of these two sets, A c
∪ B
c
.
Example 4.23
Let A = {1, 3, 5, 7} and B = {2, 3, 4, 5}. First we nd the Cartesian product of sets A and B,
A × B = {(1, 2), (1, 3), (1, 4), (1, 5), (3, 2), (3, 3), (3, 4), (3, 5),
(5, 2), (5, 3), (5, 4), (5, 5), (7, 2), (7, 3), (7, 4), (7, 5)}.
Since (3, 3) ∈ R then we would write 3R3 and would read it out loud as “3-are-3.” Since the relation is “is
equal to” then 3R3 could also be read out loud as “3 is equal to 3.” Also, since (5, 5) ∈ R then we would write
that as 5R5 and read it as “5-are-5” or as “5 is equal to 5.” We also have
domain(R) = {3, 5} ⊂ A,
range(R) = {3, 5} ⊂ B.
We can also draw binary relations quite easily. The oval on the left represents set A and its elements are
shown inside the oval. The oval on the right represents set B and its elements are shown inside the oval. This
is very similar to how we draw Venn diagrams. The relation is represented as arrows between the elements of
set A and set B. For example, since (3, 3) ∈ R ⊂ A × B then there is an arrow from 3 ∈ A to 3 ∈ B. And
since (5, 5) ∈ R ⊂ A × B then there is another arrow from 5 ∈ A to 5 ∈ B.
Example 3.24
Let A = {1, 3, 5, 7} and B = {2, 3, 4, 5}. The Cartesian product A × B was found in the last example. The
relation “is less than” is given by the following subset of A × B,
R = {(1, 2), (1, 3), (1, 4), (1, 5), (3, 4), (3, 5)} ⊂ A × B.
Since (1, 2) ∈ R then we can write 1R2 which can be read “1-are-2” or “1 is less than 2.” Similarly,
(1, 3) ∈ R can be written as 1R3 or read as “1-are-3” or as “1 is less than 3,” and so on. We also have
domain(R) = {1, 3} ⊂ A,
range(R) = {2, 3, 4, 5} ⊂ B.
This relation is also shown in the following picture. Since (1, 2), (1, 3), (1, 4), (1, 5) ∈ R ⊂ A × B then we
have arrows going from 1 ∈ A to 2, 3, 4, 5 ∈ B. Since (3, 4), (3, 5) ∈ R ⊂ A × B then we have arrows going
from 3 ∈ A to 4, 5 ∈ B.
Example 4.25
Let A = {1, 3, 5, 7} and B = {2, 3, 4, 5}. The relation “is greater than” is given by the following subset of the
Cartesian product A × B,
R = {(3, 2), (5, 2), (5, 3), (5, 4), (7, 2), (7, 3), (7, 4), (7, 5)} ⊂ A × B.
Since (3, 2) ∈ R then we can write 3R2 which can be read “3-are-2” or “3 is greater than 2.” Similarly,
(5, 2) ∈ R can be written as 5R2 and read as “5-are-2” or as “5 is greater than 2,” and so on. We also have
domain(R) = {3, 5, 7} ⊂ A,
range(R) = {2, 3, 4, 5} ⊂ B.
When working with databases we may have relations that are not related to mathematics. Here are some
simple examples of relations that one could encounter when working with databases.
Example 4.26
Suppose A = {Sam, Bob, T im} and B = {F ido, H unter, Rex}. The relation OwnsDog is given by the
following subset of the Cartesian product A × B,
Example 4.27
Suppose A = {J ohn, Sally, Anne, T ed, Bill, Ed, J ane}. We have a relation called ChildOf given by the
following subset of the Cartesian product A × A,
Since we have (J ohn, Sally) ∈ ChildOf we have “John ChildOf Sally” which is easy to understand means
that John is a child of Sally. “Ted ChildOf Bill” of course means Ted is a child of Bill, and so on. Can you
draw a family tree based on this relation?
When R is a binary relation on a nite set A, that is, when R ⊂ A × A, then there is another common way to
visualize the relation R as a directed graph. The set A becomes the vertex set of the directed graph and the
elements (x, y) ∈ R are used to make the edge set. The element (x, y) indicates a directed edge from vertex x to
vertex y. We will discuss directed graphs more in chapter 9, but for now we just want to point out the connection
between set theory and graph theory.
Example 4.28
We interpret (1, 1) ∈ R as meaning one is sent to one, we interpret (1, 2) ∈ R as meaning one is sent to two,
and we interpret (3, 2) ∈ R as meaning three is sent to two.
Example 4.29
Functions can be de ned in terms of a binary relation. We will consider this in depth in chapter 6, but for now
we just want to point out the connection. Consider the function f shown below.
The domain is given by X = {1, 2, 3} and the codomain is given by Y = {a, b, c} . The Cartesian product of X
and Y is given by
X × Y = {(x, y)|x ∈ X, y ∈ Y }
= {(1, a), (2, a), (3, a), (1, b), (2, b), (3, b), (1, c), (2, c), (3, c) }.
Functions are a special kind of binary relation that satisfy two extra conditions:
There are certain types of relations that are important in many applications. Suppose that A is a set and R is a
relation on A × A. If (x, y) ∈ R then we write xRy.
Example 4.30
This means we have aRa, bRb, and cRc. In other words, we have xRx for every x ∈ A, which means that R is
a re exive relation.
Example 4.31
This means we have aRb, bRc, and cRa. It is clear by looking at these that there is no element x ∈ A such
that xRx, which means that R is an irre exive relation.
Example 4.32
Clearly we have 1R2 and 2R1 as well as 2R3 and 3R2. We can see that if xRy is in R then yRx is also in R.
Thus R is a symmetric relation.
Example 4.33
Let us consider the set ℕ of natural numbers with the relation R given by
R = {(x, y)|x ≤ y} ⊂ N × N.
The relation R is also known as “is less than or equal to.” For example, (1, 2) ∈ R since 1 ≤ 2. But also
(1, 1) ∈ R since 1 ≤ 1. Suppose that for x, y ∈ N we have both xRy and yRx. In other words, we have that
x ≤ y and y ≤ x. The only way this can happen is if x = y. This means that R is an antisymmetric relation.
Example 4.34
We have 1R2 and 2R3 and also 1R3. Thus this is a transitive relation.
There are two more relations that are extremely important in computer science and mathematics. These
relations are de ned using the relations listed above.
1. A relation is called an equivalence relation if it is re exive, symmetric, and transitive. equivalence relation
2. A relation is called a partial ordering if it is re exive, antisymmetric, and transitive. partial ordering
Example 4.35
{
R = {(1, 1), (1, 3), (1, 5), (3, 1), (3, 3), (3, 5), (5, 1),
(5, 3), (5, 5), (2, 2), (2, 6), (6, 2), (6, 6), (4, 4)} ⊂ A × A.
To check that R is re exive we need to check that xRx for every x ∈ A. This is exactly
(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6) ∈ R.
Thus R is re exive. To check that R is symmetric we must check that if xRy then yRx. This is exactly
Thus R is symmetric. To check that R is transitive we must check that if xRy and yRz then . This is
xRz
exactly
Example 4.36
The relation ≤ de ned on ℕ is a partial ordering on ℕ. We have x ≤ x for any x ∈ N so ≤ is re exive. For
every x, y ∈ N we have that if x ≤ y and y ≤ x then x = y. This makes ≤ antisymmetric. And for any
x, y, z ∈ N we have that if x ≤ y and y ≤ z then x ≤ z. This makes ≤ transitive. Since ≤ is re exive,
If A is a set then a partitionpartition of A is a set of subsets of A such that every element of A is in exactly one
of the subsets. If R is an equivalence relation on A then for each x ∈ A we can de ne a subset
E(x) = {y ∈ A|yRx}.
The sets E(x) are called the equivalence classes of Rpartition and the set E(x) is called the equivalence class of
x. The set of equivalence classes of R is a partition of the set A. Thus equivalence relations can be used to
partition a set into subsets called equivalence classes.
Example 4.37
R = {(1, 1), (1, 3), (1, 5), (3, 1), (3, 3), (3, 5), (5, 1),
(5, 3), (5, 5), (2, 2), (2, 6), (6, 2), (6, 6), (4, 4)} ⊂ A × A.
Using the de nition of equivalence class we nd that
E(4) = {4},
Clearly E(1) = E(3) = E(5) and E(2) = E(6) . Thus we have partitioned set A into three equivalence
classes,
4.6 PROBLEMS
(a) 4 ∈ A
(b) 6 ∈ A
(c) r ∈ A
(d) b ∈ A
(e) f ∈ A
(f) 1 ∈ A
(g) 7 ∈ B
(h) 9 ∈ B
(i) z ∈ B
(j) q ∈ B
(k) 8 ∈ B
(l) y ∈ B
(m) b ∉ A
(n) 8 ∉ A
(o) g ∉ A
(p) c ∉ A
(q) 5 ∉ A
(r) 3 ∉ A
(s) 5 ∉ B
(t) s ∉ B
(u) u ∉ B
(v) 8 ∉ B
(w) 5 ∉ B
(x) v ∉ B
(a) {6, a, e} ⊂ A
(b) {2, 4, 6, 8} ⊂ A
(c) {g, 5, 6, j} ⊂ A
(d) {b, a, c} ⊂ A
(e) {a, 8} ⊂ A
(f) { } ⊂ A
(g) {9, 8, 7} ⊂ B
(h) {v, x, 8, 9} ⊂ B
(i) {v} ⊂ B
(j) { } ⊂ B
(k) {3, 6, 9} ⊂ B
(l) {z, 2} ⊂ B
(m) {2, 4, 6, 8} ⊂ A
(n) {a, e, i} ⊂ A
(o) {a, b} ⊂ A
(p) {4, h, 2} ⊂ A
(q) { } ⊂ A
(r) {9} ⊂ A
(s) {6, 8, t} ⊂ B
(t) {x, y, z} ⊂ B
(u) {8, 6} ⊂ B
(v) { } ⊂ B
(w) {v, r, 8, 2} ⊂ B
(x) {2, y, 3} ⊂ B
Question 4.3 Let U = {1, 2, 3, a, b, c, {a, b}, {1, 3}, {1, a, 3, c}}. Determine if the following statements are True
or False.
(a) 2 ∈ U (f) {b, a} ∈ U (k) {1, 2} ⊂ U (p) {1, a, 3, c} ⊂ U
(b) c ∈ U (g) {b, c} ∈ U (l) {1, 3} ⊂ U (q) {{1, a, 3, c}} ⊂ U
(c) {1, 2} ∈ U (h) {3, c, 1, a} ∈ U (m) {1, 2, 3} ⊂ U (r) {{3, 1}} ⊂ U
(d) {1, 3} ∈ U (i) b ∈ U (n) {2, {3, 1}} ⊂ U (s) {{b, c}, b, c} ⊂ U
(e) d ∈ U (j) a ∈ U (o) {\big{{a, b, c}, a, b} ⊂ U } (t) { } ⊂ U
Question 4.4 Find all the subsets of the following sets. Which of these subsets are proper subsets?
(a) {a}
(b) {5}
(c) {v}
(d) {a, b}
(e) {5, 7}
(f) {5, e}
(g) {2, 4, 6}
(h) {x, y, z}
(i) {2, b, c}
Question 4.5 Find all the subsets of the following sets. Which of these subsets are proper subsets?
Question 4.6 Let U = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} , A = {2, 3, 4, 5, 6} , and B = {4, 5, 6, 7, 8} . Find the following
sets.
(a) A ∪ B
(b) A ∩ B
(c) Ac
(d) Bc
(e) (A ∪ B) c
(f) (A ∩ B) c
(g) A ∪ B
c c
(h) A ∩ B
c c
(i) A − B
(j) B − A
(a) A ∪ B
(b) A ∩ B
(c) Ac
(d) Bc
(e) (A ∪ B) c
(f) (A ∩ B) c
(g) A ∪ B
c c
(h) A ∩ B
c c
(i) A − B
(j) B − A
(b) A ∩ B
(c) Ac
(d) Bc
(h) A ∩ B
(g) {x
c
(i) A − B
(j) B − A
∣
Question 4.8 Let
following sets.
(a) A ∪ B
(e) (A ∪ B)
(f) (A ∩ B)
(g) A ∪ B
c
x
2
(x
3
(x + 5)(x
c
= 16}
Question 4.9 Write the following sets in enumerated form. We may assume x is from the set ℤ.
(a) {x x
2
= 4}
= 27}
= 25}
Question 4.10 Write the following sets in enumerated form. We may assume x is from the set ℝ.
(a) {x x
3
= −1}
= 1}
− 4)(x − 3) = 0}
O
=
=
{n ∈ N|n > 100},
{n ∈ N|n ≤ 300},
{n ∈ N|n is even},
{n ∈ N|n is odd}
, and B = {p, r, s, t, u, v} . Find the
nd the cardinality of the following sets.
(a) Ac
(b) Bc
(c) A c
∩ B
c
(d) Oc
(e) O ∩ E
(f) Ec
(g) A ∪ B
(h) B ∩ E
(i) A ∩ O
c
(j) O ∩ E c
(k) A ∩ O c
(l) B ∪ E
Question 4.12 If A = {1, 2, 3, 4, 5, 6} how many subsets does A have? How many proper subsets does A have?
Question 4.13 If B = {a, b, c, d, w, x, y, z} how many subsets does B have? How many proper subsets does B
have?
Question 4.15 If D = {1, 2, 3, 4, 5, … , 20} what is the cardinality of the power set of of D?
and A ∪ B , A − B, B − A.
c
Question 4.21 Using the Venn diagram nd the sets A, B, , , Ac, Bc, , , ,
c c c
A ∪ B A ∩ B (A ∪ B) (A ∩ B) A ∪ B
and A ∪ B , A − B, B − A.
c
Question 4.22 Using the Venn diagram below to nd the following: A, B, C, A ∩ B, A ∩ C , B ∩ C , A ∪ B,
c c c
A ∪ C , B ∪ C , A ∩ B ∩ C , A ∪ B ∪ C , A , B , C , (A ∩ B) , (A ∩ C) , (B ∩ C) , (A ∪ B) , (A ∪ C) ,
c c c c c
(B ∪ C) , (A ∩ B ∩ C) , (A ∪ B ∪ C) , A − B, B − A, A − C , C − A, B − C , and C − B.
c c c
(B ∪ C) , (A ∩ B ∩ C) , (A ∪ B ∪ C) , A − B, B − A, A − C , C − A, B − C , and C − B
c c c
Question 4.24 Using the Venn diagram below to nd the following: |A|, |B|, |C|, |A ∩ B|, |A ∩ C|, |B ∩ C|,
|A ∪ B|, |A ∪ C|, |B ∪ C|, |A ∩ B ∩ C|, |A ∪ B ∪ C|, |A |, |B |, |C |, |(A ∩ B) |, |(A ∩ C) |, |(B ∩ C) |,
c c c c c c
|B − C|, and |C − B|
Question 4.25 (Easy) Simplify the following set theoretic expressions using the laws of set theory.
(a) A ∪ A
(b) A ∪ A c
(c) (A ∪ A c
)
c
(d) A ∪ (A ∩ B)
(e) A ∪ (A c
∩ B)
(f) A ∩ (A c
∪ B)
(g) A ∩ A
(h) A ∩ (A ∪ B)
(i) A ∩ (A ∪ B ∪ C)
(j) (A ∩ B) ∪ (A ∩ B c
)
(k) (A c
∪ A )
c c
(l) (A ∩ B) ∪ (A c
∩ B)
Question 4.26 (Moderate) Simplify the following set theoretic expressions using the laws of set theory.
(a) (A c
∪ B ) ∩ (A
c c
∪ B)
(b) B ∪ (B ∩ B c
)
(c) A c
∪ (B ∩ A )
c
(d) (A ∪ B c
) ∩ (A ∪ B)
(e) C ∪ [C ∪ (C ∩ A)]
(f) A ∩ [A ∪ (A ∩ B)]
(g) C ∪ (C ∩ A c
∩ B ∩ D)
(h) C c
∩ (C ∩ A ∩ B ∩ D)
c
Question 4.27 (Dif cult) Simplify the following set theoretic expressions using the laws of set theory.
(a) B ∩ [A ∪ (A c
∩ B)]
(b) [(A ∩ B c
) ∪ A ]
c c
(c) (A c
∩ B) ∪ [A ∩ (A ∪ B)]
(d) [A ∪ (A ∩ B)] c
∩ B
(e) (A ∩ C c
) ∪ (A
c
∩ B) ∪ (B ∩ C)
c
(f) (A ∩ B) c
∪ (B ∩ C)
(g) [A ∪ (B ∩ C)] ∩ (A c
∪ C)
(h) (A ∩ C c
) ∪ (A
c
∩ B) ∪ (A ∩ c)
c
(i) (A ∩ C) ∪ (A c
∩ B) ∪ (C ∩ B)
(j) A c
∪ B
c
∪ (A ∩ B ∩ C )
c
(k) (A ∪ C) ∩ (A c
∪ B) ∩ (C ∪ B)
(l) (A ∩ B) c
∩ (A
c
∪ B) ∩ (B
c
∪ B)
Question 4.28 Given the sets A = {1, 2, 3, 4} and B = {1, 2, 3, 4} write out the sets R ⊂ A × B that correspond
to the following relations:
(a) is equal to
(b) is less than
(c) is greater than
(d) is less than or equal
(e) is greater than or equal
(f) is not equal to
Question 4.29 Given the sets A = {0, 2, 3, 5, 7} and B = {1, 2, 4, 5, 6} write out the sets R ⊂ A × B that
correspond to the following relations:
(a) is equal to
(b) is less than
(c) is greater than
(d) is less than or equal
(e) is greater than or equal
(f) is not equal to
Question 4.30 The below directed graph represents a relation. Find the domain and range of the relation. Then
write the relation as a subset of the Cartesian product of the domain and range.
Question 4.31 Find the stated relations on the set {1, 2, 3} . Then determine if they are re exive, irre exive,
symmetric, antisymmetric, or transitive.
Question 4.32 Show the relation “is greater than or equal to” is a partial ordering on ℕ.
R = {(1, 1), (1, 5), (5, 1), (5, 5), (2, 2), (2, 4), (2, 6)(4, 2), (4, 4), (4, 6), (6, 2), (6, 4), (6, 6), (3, 3)} ⊂ A × A.
Show this relation is an equivalence relation. Then nd the equivalence classes that partition the set A.
Question 4.34 Show the relation “is equal to” is both a partial ordering and an equivalence relation on ℕ.
1 Often the set of natural numbers also includes zero. From a computer science perspective this makes sense in terms of indexing arrays.
2 Much later in your computer science major you may see problems where there is no well-de ned universal set.
3 The Cartesian product is named after Cartesius, the Latinized version of the last name of the French philosopher Rene Descartes. Since it is
named after a person often it is capitalized, but sometimes it is not. Both conventions are considered grammatically correct.
CHAPTER 5
Boolean Algebra
There are two major reasons to study Boolean algebra in computer science. First,
Boolean algebra uni es both logic and set theory in a very nice way. Second, Boolean
algebra plays a very important role in circuit design in computer science. It is very
helpful for computer science majors to have some idea of how circuits are designed and
work. Though this topic is beyond the scope of this book, appendix A shows how the
the ideas from Boolean algebra can be used to design a circuit that adds two eight-digit
binary numbers.
a set {T , F },
three operations ∧, ∨, and ¬ de ned on elements of the set {T , F },
a list of laws the operations satis ed.
In set theory we start out with a universal set U but then de ne the power set of U ,
P (U ). The power set P (U ) is the set of all subsets of U . We then operate on these
a set P (U ),
three operations ∩, ∪, and de ned on elements of the set P (U ),
c
So, we can see that in a lot of ways logic and set theory are very similar. And when
you look at the list of laws in logic and in set theory you see that even these laws are
very similar. In fact, if we exchanged the ∧ with ∩, the ∨ with ∪, the ¬ with , the F
c
with ∅, and the T with U the laws are exactly the same. This is because they are both
examples of what is called a Boolean algebra.
Instead of studying logic or set theory as we have done up till now, we can study the
laws themselves. When we study the laws themselves we are studying both logic and
set theory and many other things at the same time. Whatever we prove using Boolean
algebra must also be true for logic or set theory. Now we are ready for the de nition.
De nition 5.1. A Boolean algebra is a set B that has two binary operations, + called
addition, × called multiplication, and a unary operation called complement. The set B
′
contains two special elements written as 0 and 1 such that the following axioms are true
for all x, y, z ∈ B:
Name Axioms of Boolean Algebra
Commutative axiom (a) x × y = y × x (b) x + y = y + x
Associative axiom (a) x × (y × z) = (x × y) × z (b) x + (y + z) = (x + y) + z
Distributive axiom (a) x × (y + z) = (x × y) + (x × z) (b) x + (y × z) = (x + y) × (x + z)
Identity axiom (a) x × 1 = x (b) x + 0 = x
Inverse axiom (a) x × x = 0 ′
(b) x + x = 1
′
Notice the de nition does not say what the elements of B are, only that there are two
special elements called 0 and 1. There might be other elements in the Boolean algebra
set or there might not be. What the elements of B are depends on how we interpret the
set B. Also, our Boolean operations addition, multiplication, and complement may be
interpreted differently as well, depending on what the elements of B actually are.
These operations are NOT the addition and multiplication you know from grade school.
We used the same words but they mean something else.
We also use the word axiom instead of law because these ten equations are part of the
de nition of Boolean algebra. Why did we use only these ve axioms in the de nition
instead of more? We are missing the double complement laws, the idempotent laws,
DeMorgan's laws, the annihilation laws, and the absorption laws. All these other laws
can be proved from the axioms in the de nition. We list the rest of the laws:
Name Laws of Boolean Algebra
Double negation law x
′′
= x
Now we will see how to prove some of the Boolean algebra laws from the axioms given
in the de nition of Boolean algebra.
Example 5.1
Prove the idempotent law for addition, x + x = x , using the Boolean algebra
axioms.
′
= x + (x × x ) f irst distributive axiom
It takes a little while to get used to seeing something like x + x = x. Just remember,
the + is not the addition you are used to from algebra.
Example 5.2
The dual of x × (x × x ) is x + (x + x ) .
′ ′ ′ ′
The dual of (x + y) × (x × y ) is (x × y) + (x + y ).
′ ′
The dual of x + x = x is x × x = x.
The reason the dual is important is the following theorem, which we will not prove.
Theorem 5.1. Duality Principle: The dual of anything proved to be true in Boolean
algebra is also true.
Example 5.3
Example 5.4
Prove the annihilation law using the Boolean algebra axioms.
′
= (x + 0) × (x × x ) second inverse axiom
′
= x + (0 + x ) f second distributive axiom
′
= x × (x + 0) f irst commutative axiom
′
= x × x f irst identity axiom
Example 5.5
The other laws are a little more complicated so we will not do those here. However,
they can all be proved using the axiom and the laws already proved. These axioms and
laws can be used to simplify complicated Boolean algebra expressions.
Example 5.6
′ ′
= x × y × (x × y) commutitive axiom
′ ′ ′
= x × y × (x + y ) DeMorgan"s law
′ ′ ′
= (x × y × x ) + (x × y × y ) distributive axiom
′ ′ ′
= (x × x × y) + (x × y × y ) commutitive axiom
′ ′
= (x × x × y) + (x × 0) inverse axiom
′
= (x × y) + (x × 0) idempotant law
′
= (x × y) + 0 annihilation law
′
= x × y identity axiom
Example 5.7
′ ′ ′ ′′
x × (x × y ) = x × (x + y ) DeMorgan"s law
′
= x × (x + y) double negation law
′
= (x × x ) + (x × y) distributive axiom
= 0 + (x × y) inverse axiom
= x × y identity axiom
as A, B, and C).
A list of laws the operations satisfy.
x, y, and z).
A list of axioms and laws the operations satisfy.
In mathematics we would say that Boolean algebra is the theoretical framework that
encompass both logic and set theory. This is just a fancy way of saying both logic and
set theory are concrete examples of Boolean algebras. By comparing the laws in each
case we can see that the following symbols are equivalent.
Boolean Algebra Logic Set Theory
× (multiplication) ∧ (and) ∩ (intersection)
0 F ∅
It is this Boolean algebra that is used by computer engineers to design the circuits that
run all computers. But of course, we usually make the following identi cation,
no electrical current = 0,
electrical current = 1,
which means we usually write our Boolean set as B = {0, 1}.2 Recall, the de nition
said that a Boolean algebra B must contain two special elements called 0 and 1. The
simplest possible Boolean algebra has only these two special elements and no other
elements. So, the simplest Boolean algebra is given by B = {0, 1}. This is exactly the
Boolean algebra that we computer scientists are most interested in.
We will use the laws and axioms to see how Boolean algebra addition and
multiplication work in B = {0, 1}. The rst identity axiom says that x × 1 = x. Since
x can either be 1 or 0 we have
1 × 1 = 1,
0 × 1 = 0.
1 + 0 = 1,
0 + 0 = 0.
The rst commutative axiom says x × y = y × x so using this and the fact that
0 × 1 = 0 we know
0 × 1 = 1 × 0
⇒ 0 + 1 = 0.
Similarly, the second commutative axiom says x + y = y + x so using this and the fact
that 1 + 0 = 1 we know
1 + 1 = 0 + 1
⇒ 0 + 1 = 1.
0 × 0 = 0
1 + 1 = 1.
Putting all this together we have the following multiplication and addition tables for
Boolean algebra,
0 × 0 = 0,
1 × 0 = 0,
0 × 1 = 0,
1 × 1 = 1,
and
0 + 0 = 0,
1 + 0 = 1,
0 + 1 = 1,
1 + 1 = 1,
Everything is as you would expect from grade school math, except the funny 1 + 1 = 1.
This is what makes Boolean algebra different from grade school math. But this addition
rule 1 + 1 = 1 makes a little more sense when we think of it in terms of electricity,
since
Here the multiplication and addition tables are written in a slightly different way:
× 0 1
0 0 0
1 0 1
and
+ 0 1
0 0 1
1 1 1
First, it is always possible for a circuit to “ ip” a signal. That is, it can change a 0 to
a 1 or a 1 to a 0. That is done with a not gate. Just like in logic we can build a table that
shows how the not gate changes a signal. Even though these tables do not contain trues
or falses we will still call them truth tables because they are so similar.
′
x x
1 0
0 1
It turns out to be very useful to use pictures to help computer scientists design
circuits using gates. Here is the picture that is used to indicate a not gate. It is a triangle
with a tiny circle attached to the right point. As it is drawn the input is signal x and the
output is signal x′. We generally “read” circuit diagrams going from left to right just
like we read English.
Next we will consider gates with two input signals x and y. We want to consider the
different ways those two input signals can be combined. Of course x can be either 1 or
0 and y can also be either 1 or 0. So there are 2 = 4 possible combinations of inputs.
2
Each gate needs to give a unique output for each of these four inputs.
x y gate output
1 1 0 or 1
1 0 0 or 1
0 1 0 or 1
0 0 0 or 1
There are a few combinations of outputs we are not interested in. We are not interested
in a gate that gives 0 no matter what the inputs are. Also, we are not interested in a gate
that gives 1 no matter what the inputs are. Finally, we do not want order to matter. That
means we want the same output for when x = 0 and y = 1 as for when x = 1 and y = 0
. With these restrictions there are only six possible gates.
The rst two-input gate we will look at is the and gate. The and gate works just like
the and connector in logic. However, we use × to indicate and in Boolean algebra. We
can also write x × y as xy just as one would in algebra. The truth table for the and gate
is given below.
x y x × y
1 1 1
1 0 0
0 1 0
0 0 0
Here is the picture we use to show an and gate. There are two inputs, x and y that enter
the gate on the left. The output from the gate is shown on the right and is given by
x × y.
The next two-input gate we will look at is the or gate. The or gate works just like the
or connector in logic. However, we use + to indicate or in Boolean algebra. The truth
table for the or gate is given below.
x y x + y
1 1 1
1 0 1
0 1 1
0 0 0
Here is the picture we use to show an or gate. There are two inputs, x and y that enter
the gate on the left. The output from the gate is shown on the right and is given by
x + y.
Now we will look at the nand gate. The nand gate is the not of the and gate,
′
x nand y = (x × y) .
We will simply use the term nand to mean the nand operation. The truth table for the
nand gate is given below.
x y xnandy
1 1 0
1 0 1
0 1 1
0 0 1
Here is the picture we use to show an nand gate. There are two inputs, x and y that enter
the gate on the left. The output from the gate is shown on the right and is given by
x nand y. Notice that the picture for an nand gate looks like an and gate, only has a
Next we look at the nor gate. The nor gate is the not of the or gate,
′
x nor y = (x + y) .
We will simply use the term nor to mean the nor operation. The truth table for the nor
gate is given.
x y xnory
1 1 0
1 0 0
0 1 0
0 0 1
Here is the picture we use to show a nor gate. There are two inputs, x and y that enter
the gate on the left. The output from the gate is shown on the right and is given by
xnory. Again, notice that the picture for a nor gate looks like an or gate, only has a
The next gate we look at is called the exclusive-or gate, or xor gate. In an example
from the logic chapter we said the expression p xor q is true when only one of the
propositions p or q is true. In other words, p xor q is true when one, or the other, of the
propositions are true but not both. That means that if both p and q are true, then p xor q
is false. Similarly, x xor y is equal to 1 only when either x or y is 1 but not both. We
can also write this as
′ ′
x xor y = x × y + x × y.
x y x xor y
1 1 0
1 0 1
0 1 1
0 0 0
Here is the picture we use to show a xor gate. There are two inputs, x and y that enter
the gate on the left. The output from the gate is shown on the right and is given by
x xor y. Again, notice that the picture for an xor gate looks like an or gate only with an
extra line on the left where the input go into the gate.
Finally, we look at the xnor gate. The xnor gate is the not of the xor gate. That is,
′ ′ ′ ′ ′
x xnor y = (x × y + x × y) = (x × y) + (x × y ).
We will simply use the term xnor to mean the xnor operation. The truth table for the
xnor gate is given.
x y xxnor y
1 1 1
1 0 0
0 1 0
0 0 1
Here is the picture we use to show an xnor gate. There are two inputs, x and y that enter
the gate on the left. The output from the gate is shown on the right and is given by
x xnor y. Again, notice that the picture for an xnor gate looks like an xor gate only
with a tiny circle on the right where the output comes out.
We provide all six of these Boolean algebra operations, or gates, in a single table. As
we said before, we were not interested in gates that gave a 0 or a 1 no matter what the
inputs were. And we are only interested in gates that give an output that are the same
when x = 0 and y = 1 as when x = 1 and y = 0.
1 1 1 1 0 0 0 1
1 0 0 1 1 0 1 0
0 1 0 1 1 0 1 0
0 0 0 0 1 1 0 1
Drawing circuits is almost exactly like drawing the expression trees for expressions in
logic. It is these “pictures” that actually are the basis for the circuits that are etched
into computer microprocessors, the “chips” that computers run on. We will draw a
circuit for evaluating the expression x × y + y. We begin by drawing an expression
′
There are a couple differences between circuit diagrams and expression trees. First,
expression trees are usually read from the bottom to the top. Circuit diagrams are
usually read from the left to the right. Second, expression trees often have the same
variable listed more than once. In the above expression tree the variable y is shown
twice, once for each time it appears in the expression x × y + y. But circuit diagrams
′
generally have each input signal shown only once. Below we rotate the expression tree
so it can be read left to right and modify it a little so the y variable only appears once.
(Notice, it no longer looks like a tree after we modify it.)
This modi ed expression tree can be used as a basis for drawing the circuit diagram.
The vertices with the variables become the input signals. Each of the other vertices of
the expression tree is replaced by the correct gate. The is replaced with the not gate,
′
the × is replaced with the and gate, and the + is replaced with the or gate.
Finding the expression tree can be helpful for drawing circuit diagrams but is not
necessary once you get good at it.
Example 5.8
Example 5.9
The circuit drawn in the last example is quite complicated. Use the laws of Boolean
algebra to simplify the expression (x + y ) × (x + y ) × (y + z) and then draw the
′ ′ ′
′ ′ ′ ′ ′ ′ ′ ′ ′
(x + y )(x + y )(y + z) = (x x + x y + y x + y y )(y + z)
′ ′ ′
= [0 + y (x + x) + y ](y + z)
′ ′
= (y 1 + y )(y + z)
′ ′
= (y + y )(y + z)
′
= y (y + z)
′ ′
= y y + y z
′
= y z
This circuit is easier and cheaper to build and faster than the circuit shown in the last
example. This shows why simplifying Boolean algebra expressions is so important
in computer science.
Example 5.11
Find the truth table and draw the circuit for the following Boolean algebra
expression x × z + y.′
1 1 1 0 0 1
1 1 0 1 1 1
1 0 1 0 0 0
1 0 0 1 1 1
0 1 1 0 0 1
0 1 0 1 0 1
0 0 1 0 0 0
0 0 0 1 0 0
Example 5.12
Find the truth table and draw the circuit for the following Boolean algebra
expression [(x + y) × z] . ′
1 1 1 1 1 0
x y z
′
x + y (x + y) × z [(x + y) × z]
1 1 0 1 0 1
1 0 1 1 1 0
1 0 0 1 0 1
0 1 1 1 1 0
0 1 0 1 0 1
0 0 1 0 0 1
0 0 0 0 0 1
y (y + z). Are these expressions equal or not? That is, do these expressions represent
′
the same thing or not? How would you nd out? One thing you could do is write out a
truth table for each and see if they are equivalent. In this case they are equivalent. In a
previous example we simpli ed the expression (x + y )(x + y )(y + z), ′ ′ ′
′ ′ ′ ′ ′ ′ ′ ′ ′
(x + y )(x + y )(y + z) = (x x + x y + y x + y y )(y + z)
′ ′ ′
= [0 + y (x + x) + y ](y + z)
′ ′
= (y 1 + y )(y + z)
′ ′
= (y + y )(y + z)
′
= y (y + z)
′ ′
= y y + y z
′
= y z.
different lines in this above simpli cation. In fact, each of the above lines is a different
way of writing down the same thing.
Circuit designers need an easy way to know if different expressions really are the
same thing or not. Therefore circuit designers often write expressions in one of two
standardized, or canonical, ways. Every Boolean expression can be written in a sum-of-
products form or in a product-of-sums form. This makes it easy to see when
expressions are the same or not.
Additionally, sometimes you want to have a function that behaves in a certain way.
Suppose you know the truth table for what you want the function to do but do not know
a function that behaves that way. You want to nd an expression for a function that
behaves that way. The sum-of-products and product-of-sum expressions can be written
down using a truth table.
We will rst consider the sum-of-products form. The rst step in writing an
expression in the sum-of-products form is to nd the truth table. We use the truth table
for x × z + y from the above example. When there is a 1 in the nal column we
′
If x value is 1 use x in the product term, if x value is 0 use x′ in the product term.
If y value is 1 use y in the product term, if y value is 0 use y′ in the product term.
If z value is 1 use z in the product term, if z value is 0 use z′ in the product term.
We then add up, or sum, all the product terms we get. This is the sum-of-products form
for the expression.
x y z ′
x × z + y product terms
1 1 1 1 xyz
1 1 0 1 xyz
′
1 0 1 0
1 0 0 1 xy z
′ ′
0 1 1 1 ′
x yz
0 1 0 1 ′
x yz
′
0 0 1 0
0 0 0 0
In the rst row there is a 1 in the nal column so we use this row. In this row the x
value is 1, the y value is 1, and the z value is 1 so we use x, y, and z in the product term
to get x × y × z. From now on we will write x × y × z as xyz. In the second row there
is also a 1 in the nal column so we use the second row. In this row the x value is 1, the
y value is 1, and the z value is 0 so we use x, y, and z′ in the product term to get xyz . In
′
the fourth row there is a 1 in the nal column so we use this row. In this row the x value
is 1, the y value is 0, and the z value is 0 so we use x, y′, and z′ in the product term to
get xy z . And so on. We then add up, or sum, all the product terms to get
′ ′
x × z + y = xyz + xyz + xy z + x yz + x yz .
′ ′ ′ ′ ′ ′ ′
Example 5.13
1 1 1 0
1 1 0 1 xyz
′
1 0 1 0
1 0 0 1 xy z
′ ′
0 1 1 0
0 1 0 1 ′
x yz
′
0 0 1 1 ′
x y z
′
0 0 0 1 ′
x y z
′ ′
So [(x + y) × z] .
′ ′ ′ ′ ′ ′ ′ ′ ′ ′ ′
= xyz + xy z + x yz + x y z + x y z
Next we will consider the product-of-sum form. The rst step in writing an
expression into the product-of-sum form is to nd the truth table. Again we use the
truth table for x × z + y from the above example. When there is a 0 in the nal
′
If x value is 1 use x′ in the sum term, if x value is 0 use x in the sum term.
If y value is 1 use y′ in the sum term, if y value is 0 use y in the sum term.
If z value is 1 use z′ in the sum term, if z value is 0 use z in the sum term.
We then multiply, or take the product of, all the sum terms we get. This is the product-
of-sum form for the expression.
x y z x × z + y
′
sum terms
1 1 1 1
1 1 0 1
1 0 1 0 ′
x + y + z
′
1 0 0 1
0 1 1 1
0 1 0 1
0 0 1 0 x + y + z
′
0 0 0 0 x + y + z
In the third row the nal column is 0 so we use this row. In this row the x value is 1,
the y value is 0, and the z value is 1 so we use x′, y, and z′ in the sum term to get
x + y + z . In the seventh row the nal column is 0 so we use this row. In this row the
′ ′
x value is 0, the y value is 0, and the z value is 1 so we use x, y, and z′ in the sum term
to get x + y + z . In the eighth row the nal column is 0 so we use this row. In this row
′
the x value is 0, the y value is 0, and the z value is 0 so we use x, y, and z in the sum
term to get x + y + z. We then multiply, or take the product of, all the sum terms to get
x × z + y = (x + y + z )(x + y + z )(x + y + z).
′ ′ ′ ′
Example 5.14
x y z [(x + y) × z]
′
sum term
1 1 1 0 ′ ′
x + y + z
′
1 1 0 1
1 0 1 0 ′
x + y + z
′
1 0 0 1
0 1 1 0 ′
x + y + z
′
0 1 0 1
0 0 1 1
0 0 0 1
So [(x + y) × z] .
′ ′ ′ ′ ′ ′ ′ ′
= (x + y + z )(x + y + z )(x + y + z )
5.5 PROBLEMS
(a) x + x
(b) x + x ′
(c) (x + x ) ′ ′
(d) x + xy
(e) x + x y ′
(f) x(x + y) ′
(g) x(x)
(h) x(x + y)
(i) x(x + y + z)
(j) xy + xy ′
(k) (x ′
+ x )
′ ′
(l) xy + x y ′
(a) (x ′
+ y )(x + y)
′ ′
(b) y + (yy ) ′
(c) x ′
+ yx
′
(d) (x + y )(x + y) ′
(e) w + [w + (wx)]
(f) x[x + (xy)]
(g) w + (wx yz) ′
(h) w (wxyz)
′ ′
Question 5.3 (Dif cult) Simplify the following Boolean algebra expressions.
(b) [(xy ) + x ] ′ ′ ′
(c) x y + x(x + y)
′
(d) (x + xy) y ′
(e) xz ′
+ x y + (yz)
′ ′
(f) (xy) ′
+ (yz)
(g) [x + (yz)](x ′
+ z)
(h) xz ′
+ x y + (xz)
′ ′
(i) xz + x y + zy ′
(j) x + y + xyz
′ ′ ′
(l) (xy) (x ′ ′
+ y)(y + y)
′
Question 5.4 Write down the Boolean expression associated with each of the following
circuits.
Question 5.5 Write down the Boolean expression associated with each of the following
circuits.
Question 5.6 Write down the Boolean expression associated with each of the following
circuits.
Question 5.7 Write down the Boolean expression associated with each of the following
circuits.
Question 5.8 For the expressions in question, 5.1 draw the associated circuit.
Question 5.9 For the expressions in question, 5.2 draw the associated circuit.
Question 5.10 For the expressions in question, 5.3 draw the associated circuit.
Question 5.11 Below are the truth tables for some functions f (x, y). First write f (x, y)
x y f (x, y)
0 0 1
0 1 0
1 0 1
1 1 0
(b)
x y f (x, y)
0 0 1
0 1 1
1 0 0
1 1 0
(c)
x y f (x, y)
0 0 0
x y f (x, y)
0 1 1
1 0 0
1 1 0
(d)
x y f (x, y)
0 0 0
0 1 0
1 0 1
1 1 1
(e)
x y f (x, y)
0 0 1
0 1 0
1 0 0
1 1 1
(f)
x y f (x, y)
0 0 0
0 1 0
1 0 0
1 1 1
(g)
x y f (x, y)
0 0 1
0 1 1
1 0 1
1 1 0
(h)
x y f (x, y)
0 0 1
0 1 0
x y f (x, y)
1 0 1
1 1 1
(i)
x y f (x, y)
0 0 0
0 1 1
1 0 0
1 1 1
Question 5.12 Below are the truth tables for some functions f (x, y). First write f (x, y)
x y z f (x, y, z)
0 0 0 0
0 0 1 0
0 1 0 1
0 1 1 0
1 0 0 0
1 0 1 1
1 1 0 0
1 1 1 1
(b)
x y z f (x, y, z)
0 0 0 1
0 0 1 0
0 1 0 1
0 1 1 1
1 0 0 1
1 0 1 0
1 1 0 1
1 1 1 0
(c)
x y z f (x, y, z)
0 0 0 1
x y z f (x, y, z)
0 0 1 0
0 1 0 1
0 1 1 0
1 0 0 0
1 0 1 1
1 1 0 1
1 1 1 0
(d)
x y z f (x, y, z)
0 0 0 1
0 0 1 0
0 1 0 0
0 1 1 1
1 0 0 0
1 0 1 0
1 1 0 1
1 1 1 0
(e)
x y z f (x, y, z)
0 0 0 1
0 0 1 1
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 1
1 1 0 1
1 1 1 1
(f)
x y z f (x, y, z)
0 0 0 1
0 0 1 1
0 1 0 0
0 1 1 1
1 0 0 0
1 0 1 0
1 1 0 0
1 1 1 1
Question 5.13 Write out the truth table for the following Boolean expressions.
(a) (x ′
+ y)x
(b) (x + y )x ′
(c) (x + y)y ′
(d) (x + y)(x + y)
(e) (x + y)(x ′
+ y)
(f) (x + y )(x ′ ′
+ y)
(g) xx + x(x + y)
(h) xy + y(x + y)
(i) x y + y (x + y)
′ ′
(j) x y′ ′ ′
+ y (x + y)
(k) xy(x + y)
(l) x y (x
′ ′ ′
+ y )
′
Question 5.14 Write out the truth table for the following Boolean expressions.
(a) x(x + y ′
+ z)
(b) x (x + y
′ ′
+ z)
(c) (x + y)(x + z)
(d) (x ′ ′
+ y )(x + z)
(e) x y′ ′
+ z (x + y)
′
(f) xy(x + y + z)
1 Another way of thinking about this is that our set is the set of all propositions which can be split into two
equivalence classes
T = {alltruepropositions}
and
F = {allf alsepropositions}
. Thus T is really the equivalence class of all true propositions and F is the equivalence class of all false
propositions. Then we could consider p , q , and r to be propositions instead of truth-values.
2 This is a simpli cation. Though it is nice to think of 0 meaning “no electrical current” there is actually a very
low level of residual current.
CHAPTER 6
Functions
Functions play an important role in computer science, particularly in complexity analysis, which is used to
analyze algorithms. An understanding of functions is also necessary for many topics in advanced computer
science, meaning that computer science majors need to have a working understanding of functions.1
f : X ⟶ Y.
Lower case Latin letters or Greek letters are usually used to represent functions. If f : X → Y is a function
from the set X to the set Y then the set X is called the domain of f and the set Y is called the codomain of f. If the
rule f sends x in the domain to y in the codomain then y is called the image of x.
Example 6.1
Let the domain be X = {1, 2, 3} and the codomain be Y = {a, b, c} . Then a function f : X → Y is de ned by
f (1) = a,
f (2) = c,
f (3) = b.
The function f is a rule that assigns to 1 ∈ X the value a ∈ Y . This is what f (1) = a means. The rule f
assigns to 2 ∈ X the value c ∈ Y . That is what f (2) = c means. And the rule f assigns to 3 ∈ X the value
b ∈ Y . That is what f (3) = b means. Another way to say this is the image of 1 is a, the image of 2 is c, and
Example 6.2
Let the domain be X = {1, 2, 3} and the codomain be Y = {a, b, c} . Then a function f : X → Y is de ned by
f (1) = c,
f (2) = a,
f (3) = c.
Notice that this function has f (1) = c and f (3) = c. According to the de nition, a function is a rule that
assigns to each element of X exactly one element of Y. For every element in X there is assigned exactly one
element of Y. It is perfectly okay that both 1 and 3 are assigned to the same element c.
Example 6.3
Let the domain be ℝ and the codomain be ℝ. Then a function f : R → R is de ned by the rule
2
f (x) = 4x − 10x.
Sometimes functions that are de ned by an algebraic rule are shown using a picture of a function-machine.
Example 6.4
Boolean expressions, and therefore circuits, can be viewed as multivariable functions. Consider the circuit
represented by the expression (x × y) + z . First recall that B = {0, 1}. This circuit represents a function
′
f : B × B × B → B.
The input of the function is an 3-tuple of Boolean variables (x, y, z) ∈ B × B × B and the output is an
element of B. The function rule is given by
′
f (x, y, z) = (x × y) + z .
We will not deal any further with multivariable functions in this class, but they are very common in both
mathematics and computer science.
Example 6.5
f (x) = x mod 8
is a function. We have seen this function before. This function takes a natural number x and nds the
remainder after division by 8.
Example 6.6
Let the domain be X = {all circle sin the plane} and the codomain be ℝ. Then f : X → R where
is a function. This function take a circle and nds the radius of the circle.
We can see that the idea of a function can apply to many different situations. This is what makes it such an
important and useful concept. But before going further we want to consider an example of something that is not a
function. Consider the following set relation:
Is this a function? Remember what the de nition of function says, a function from X to Y is a rule that assigns to
each element of X exactly one element of Y. What is happening to the element 2 ∈ X? It is getting assigned to
both b and c. Thus 2 ∈ X is not assigned to exactly one element of Y, it is assigned to two elements of Y.
Therefore, this is not a function.
Now let us reconsider the second example, the function f : X → Y given by
f (1) = c,
f (2) = a,
f (3) = c,
which we showed as
The image of a function is also called the range of the function. We will usually use the word range in this book.
In this example the range of f is given by the set {a, c}. Here the range of f is not equal to the codomain of f.
Instead, the range of f is a proper subset of the codomain of f,
We always have range ⊆ codomain. So, for each function there are three sets of interest, the domain, the
codomain, and the range.
There is a second de nition of function that uses the language of set theory. This is an important connection
between set theory and functions. Recall, we studied set relations in chapter 4. Functions are a special kind of set
relation. This is the way that computer scientists usually think about functions.
De nition 2. De nition two of functions:
Let X and Y be sets. A function f : X → Y is a subset of the Cartesian product of X and Y,
f ⊂ X × Y
such that
Example 6.7
The domain is given by X = {1, 2, 3} and the codomain is given by Y = {a, b, c} . Thus
X × Y = {(x, y)|x ∈ X, y ∈ Y }
= {(1, a), (2, a), (3, a), (1, b), (2, b), (3, b), (1, c), (2, c), (3, c) }.
The function f is a subset of X × Y given by
The rst property in the de nition is satis ed: if x ∈ X then there is a y ∈ Y such that (x, y) ∈ f . For 1 ∈ X,
we have a ∈ Y such that (1, a) ∈ f . For 2 ∈ X, we have c ∈ Y such that (2, c) ∈ f . For 3 ∈ X, we have
B ∈ Y such that (3, b) ∈ f . The second property is also satis ed: this y ∈ Y is unique. For 1 ∈ X this y ∈ Y
Example 6.8
Like before the domain is given by X = {1, 2, 3} , the codomain is given by Y = {a, b, c} , and
X × Y = {(x, y)|x ∈ X, y ∈ Y }
= {(1, a), (2, a), (3, a), (1, b), (2, b), (3, b), (1, c), (2, c), (3, c)}.
Convince yourself that the two properties in the de nition are satis ed. Consider the second property. For
1 ∈ X the y ∈ Y is unique, it is a ∈ Y since (1, a) ∈ f . For 3 ∈ X the y ∈ Y is unique, it is also a ∈ Y since
(3, a) ∈ f . It is okay that a ∈ Y is used twice. The element a ∈ Y is the unique element in Y associated to 1
range = {a, c} ⊂ Y .
Example 6.9
f = {(1, e), (2, i), (3, b), (4, e), (5, d), (6, c), (7, g)} ⊂ X × Y .
Convince yourself that the two properties in the de nition are satis ed. Also, notice that
range = {b, c, d, e, g, i} ⊂ Y .
Example 6.10
Here X ,
= {1, 2, 3} Y = {a, b, c} , and
X × Y = {(x, y) | x ∈ X, y ∈ Y }
= {(1, a), (2, a), (3, a), (1, b), (2, b), (3, b), (1, c), (2, c), (3, c)}.
For 2 ∈ X we have B ∈ Y such that (2, b) ∈ f . But we also have c ∈ Y such that (2, c) ∈ f . Thus the second
property is not satis ed. For 2 ∈ X the y ∈ Y is not unique, there are two of them, namely b and c. Thus the
de nition for a function is not satis ed.
A function is called onto if the range of the function is equal to the codomain of the function. A function is
called one-to-one if no two distinct elements of the domain have the same image. Another way of saying this is
that for the elements x1 and x2 in the domain of f, if x ≠ x then
1 2 f (x 1 ) ≠ f (x 2 ) . Now it is a good time to
remember a little logic. If we de ne the propositions p and q to be
p → q ≡ ¬q → ¬p.
Therefore the statement “if x ≠ x then f (x ) ≠ f (x )” is logically equivalent to the statements “if
1 2 1 2
Example 6.11
f : R ⟶ R.
The real-valued functions of one variable that you studied before were probably given in terms of an equation,
like
2 2
f (x) = x + 3x − 2 or like y = x + 3x − 2.
The equation is the rule that assigns to each element x ∈ R (the domain) exactly one element of ℝ (the
codomain). If we want to know where the function sends a particular element of the domain ℝ we evaluate the
function at that element. For example, if we want to nd where the function sends 2 we evaluate it at 2. That
means we replace the x in the equation with 2,
2
f (2) = 2 + 3(2) − 2 = 4 + 6 − 2 = 8.
So f sends 2 to 8.
You have seen functions graphed before. Fig. 6.1 shows the graph of the function f (x) = x + 3x − 2. The x-
2
axis is the set of inputs and the outputs are graphed directly above (or below) the input. So, the graph of a
function is really just a picture that shows the relationship between elements of the domain and elements of the
codomain. It turns out that the graphs of functions that you are used to seeing match exactly the set theory
de nition of a function. Consider our function f : R → R de ned by f (x) = x + 3x − 2. According to the set
2
theory de nition of a function, a function is a subset f ⊂ R × R that satis es two properties: (1) if x ∈ R then
there is a y ∈ R such that (x, y) ∈ f , and (2) this y is unique. Finally, recall that R × R is exactly the plane. For
every input x from ℝ there is an output y from ℝ so that (x, y) ∈ f ⊂ R × R. The output y is given by
x + 3x − 2, which is why we often write y = x + 3x − 2 for a function. So, according to the de nition the
2 2
function f is given by
2
f = {(x, x + 3x − 2) x ∈ R} ⊂ R × R.
Figure 6.1 The graph of the function
. The element
.
{(x, x
2
f (x) = x
+ 3x − 2)
∣
2
(2, 8) ∈ f
is shown as a black dot. Every point on the graph (gray) represents an element of
f ⊂ R × R
+ 3x − 2
x ∈ R} ⊂ R × R
Every point in the graph of the function in Fig. 6.1 is an element of the function set f ⊂ R × R. This de nition
gives us a wonderful way to tell if a graph is a function or not called the vertical line test. If the y is not unique at
some value of x then there are at least two points (x, y ) and (x, y ), where y ≠ y , that are on the graph. Both
1 2 1
line that intersects the graph in two or more places then the graph does not represent a function, see Fig. 6.2.
a function.
Example 6.12
Find the domain and range of the graphed function and then nd the function rule. Here the graph of the
function is simply the four shown points.
2
of these points happen where the graph intersects a vertical line drawn at x. So, if it is possible to draw a vertical
Figure 6.2 Using the vertical line test. The vertical line (black) intersects the graph (gray) in three places (black dots). Therefore the graph is not
The function rule tells us what element from the range is assigned to an element from the domain,
Finally, we could also have represented the function f as a subset of the domain × range,
f = {(−3, −2), (−2, 4), (1, 2), (4, −3)} ⊂ Domain × Range.
Pay attention to the different ways we could write down a function. Make sure you understand how they are
related.
Example 6.13
5x
f (x) = .
2x − 1
Thus −2 is assigned to, or sent to, 2, 0 is sent to 0, and 6 is sent to 2.727272 …. Next we nd the domain of f.
For this we look carefully at the rule, or algebra expression, that de nes f,
5x
.
2x − 1
We know that division by zero is unde ned, so we are not allowed to use any x value that results in
2x − 1 = 0. To nd this x value we simply solve this equation to nd x = . Thus the domain is the set
1
1 1 1
Domain of f = R − { } = (−∞, ) ∪ ( , ∞).
2 2 2
Example 6.14
Use the vertical line test to determine if the following graph represents a function.
Since it is possible to draw a vertical line that intersects the graph (gray) more than once, the graph does not
represent a function.
Example 6.15
Use the vertical line test to determine if the following graph represents a function.
Since it is not possible to draw a vertical line that intersects the graph (gray) more than once, the graph does
represent a function.
Example 6.16
To nd the composition function g ∘ f : X ⟶ Z we rst apply f to the elements of X, nd their outputs, and
then apply g to these outputs,
(g°f )(1) = g(f (1)) = g(b) = δ, (g°f )(2) = g(f (2)) = g(c) = γ,
(g°f )(3) = g(f (3)) = g(a) = β, (g°f )(4) = g(f (4)) = g(d) = α.
2
f (x) = x + 2x − 3
and
g(x) = 3x − 1.
We want to nd the composite function g°f . One can imagine this as a series of function machines. Here the
variable x enters the function machine f and the output is f (x), which then enters the next function machine g.
The output from function machine g is g(f (x)).
If we put x = 4 into the function machine f then f (4) = 4 + 2(4) − 3 = 21 would come out. Then the 21
2
would go into the g machine and g(21) = 3(21) − 1 = 62 would come out. The same thing would happen for
any other number you put in.
Now we nd the formula for the composite function by putting the variable x into the machine instead of a
number. Clearly x + 2x − 3 would come out of the f machine which would then go into the g machine. What
2
would come out of the g machine? To nd out we put the x + 2x − 3 into the formula for g(x) and then
2
simplify,
2
= g(x + 2x − 3)
2
= 3(x + 2x − 3) − 1
2
= 3x + 6x − 9 − 1
2
3x + 6x − 10.
2
(g°f )(4) = 3(4) + 6(4) − 10
= 3(16) + 24 − 10
= 62,
(f °g)(x) = f (g(x))
= f (3x − 1)
2
= (3x − 1) + 2(3x − 1) − 3
2
= (9x − 6x + 1) + (6x − 2) − 3
2
= 9x − 4.
Example 6.17
Find (f °g)(x).
2 2 2
(h°f )(x) = h(f (x)) = h(2x − 3) = (2x − 3) + 3 = 4x − 12x + 9 + 3 = 4x − 12x + 12
2 2 2
(h°g°f )(x) = h(g(f (x))) = h(−6x + 9) = (−6x + 9) + 3 = 36x − 54x − 54x + 81 + 3 = 36x −
Find g 2
(x) = (g°g)(x) .
We now will study how to nd the inverse of a function. Again, in a more advanced book we would have to be
very careful with domains and ranges, but here all of our examples will be very simple. We begin by de ning the
identity function and the inverse function.
De nition 4. Suppose X is a set. The identity function on X is the function i : X → X given by i(x) = x.
function of f. It works differently than negative exponents on numbers. Also, if f is not a one-to-one function
then f does not have an inverse.
Example 6.18
Example 6.19
If X ,
= {1, 2, 3} Y = {a, b, c} , nd the inverse function of f for f : X → Y given by
Function f is pictured by
The inverse function f −1
: Y → X is given by
−1 −1 −1
f (a) = 1, f (c) = 2, f (b) = 3.
Notice that
−1 −1 −1 −1 −1 −1 −1 −1
(f °f )(1) = f (f (1)) = f (a) = 1, (f °f )(2) = f (f (2)) = f (c) = 2, (f °f )(3) = f (f (3))
so f −1
°f is exactly the identity function i.
Example 6.20
Let f , g : R → R be given by f (x) = x + 6 and g(x) = x − 6. Show that g is the inverse function of f.
and so g°f = i , the identity function. In other words, we have found that f −1
(x) = x − 6 .
Example 6.21
Let f : R → R be given by
3x − 2
f (x) = .
5
To nd f −1
: R → R we rst rewrite the function and call the outputs y,
3x − 2
y = .
5
3y − 2
x = .
5
3y − 2 5x + 2
x = ⇒ 5x = 3y − 2 ⇒ 3y = 5x + 2 ⇒ y = .
5 3
3
.
6.4 PROBLEMS
Question 6.2 Let the domain be X = {1, 2, 3, 4} and the codomain be Y = {a, b, c, d, e} . The functions f1, f2,
and f2 are given by the rules:
f 1 (1) = cf 2 (1) = cf 3 (1) = bf 1 (2) = bf 2 (2) = df 3 (2) = bf 1 (3) = cf 2 (3) = ef 3 (3) = df 1 (4) = ef 2 (4) = bf
Question 6.3 Let the domain be X = {1, 2, 3, 4, 5, 6, 7} and the codomain be Y = {a, b, c, d, e, f } . The functions
f1, f2, and f3 are given by the rules:
f 1 (1) = cf 2 (1) = af 3 (1) = bf 1 (2) = df 2 (2) = cf 3 (2) = cf 1 (3) = ef 2 (3) = ef 3 (3) = df 1 (4) = f f 2 (4) = b
f 1 = {(1, c), (2, b), (3, d), (4, c), (5, a)} ⊂ X × Y f 2 = {(1, d), (2, d), (3, c), (4, b), (5, a)} ⊂ X × Y f 3 = {(1
Question 6.5 Let the domain be X = {1, 2, 3, 4, 5, 6} and the codomain be Y = {a, b, c, d, e, f } . The functions
f1, f2, and f3 are given as subsets of the Cartesian product of X and Y by:
f 1 = {(1, b), (2, c), (3, a), (4, e), (5, f ), (6, d)} ⊂ X × Y f 2 = {(1, b), (2, b), (3, b), (4, e), (5, e), (6, e)} ⊂ X ×
Question 6.7 The following function rules are given by an equation. Evaluate these functions at the values
x = −3, x = 0, and x = 4.
(a) f (x) = 3x + 7
(b) f (x) = 3(x + 4) − 5
(c) f (x) = −4x + 6
(d) f (x) = 2 x
(e) f (x) = x 2
+ 3x
(i) f (x) = (x
2
2
− 1)
Question 6.8 Use the vertical line test to determine if the graphs in Fig. 6.5 represent functions. If they do,
determine the domain and range of the functions.
Figure 6.4 Functions for question 6.6.
Question 6.9 Let X = {1, 2, 3, 4} and Y = {a, b, c, d} and Z = {α, β, γ, δ} . The functions f : X → Y and
g : Y → Z are given by
Find g°f : X → Z
Question 6.10 Given the functions f and g in Fig. 6.6, nd g°f . What is the domain, codomain, and range of g°f ?
Then nd f °g. What is the domain, codomain, and range of f °g?
Question 6.11 For each function pictured in Fig. 6.3, nd the inverse function if it exits.
Figure 6.5 Graphs for question 6.8.
Question 6.12For each function given in question 6.2 nd the inverse function if it exists.
Question 6.13 For each function given in question 6.3 nd the inverse function if it exists.
Question 6.14 For each function given in question 6.4 nd the inverse function if it exists.
Question 6.15 Are the following pairs of functions inverses of each other or not?
4
and g(x) = 4x + 10
(b) f (x) = −(x + 2) and g(x) = 2 + x
3 3
(c) f (x) = 3
x
− 1 and g(x) = x+1
3
(d) f (x) = 2 − 2
3
x and g(x) = −2x+4
(a) f (x) = 7x
(b) f (x) = 1
x
− 3
(e) f (x) = √x − 4
(f) f (x) = 2
x−3
1 Note, the word “function” is often used in programming in a different way. There it refers to a block of reusable code in a program that can be
called when it is needed.
CHAPTER 7
Number Representations
De nition 1. The Addition Principle: Suppose there are n ways for event
1
and E . If all these ways are distinct then the number of ways for E or E
2 1 2
to occur is n + n .
1 2
When we say that all the ways are distinct, we mean that none of the ways
are the same as any of the other ways. Let us look at an example to see how
this works.
Example 7.1
n + n = 10 + 8 = 18 ways for E1 or E
1 2 to occur. Thus you have 18
2
Using the language of set theory, the addition principle could also be
written this way.
A ∪ B = A + B .
Recall that |A| mean the cardinality of set A, that is, the number of
elements contained in set A. The addition principle can of course be
generalized to more than two events. But before giving that de nition we
need to spend a moment to make sure we understand some notation. The
capital Greek letter sigma, written as Σ, means “sum” or add. Suppose we
had the formula
4
∑ xi .
i=1
Notice the i = 1 written below the Σ and the 4 is written above the Σ.
What exactly does this formula mean? It means we are to add all the
variables xi from i = 1 to 4. This is very similar to how the for-do loops
work. So we have
∑ xi = x1 + x2 + x3 + x4 .
i=1
Similarly, the large ∪ means to take the union. Suppose we had the formula
∪ Ai .
i=1
This works exactly like above, it means we take the union of all the sets Ai
from i = 1 to 6. Thus,
∪ Ai = A1 ∪ A2 ∪ A3 ∪ A4 ∪ A5 ∪ A6 .
i=1
∪ ∪ ∪
∣ A1 ∪ A2
Example 7.2
∪ ⋯
i=1
n
∪
∪
Ai
An
=
=
∑
i=1
A1 +
|A i |.
land, or by sea. There are two different ways to go by air, four different
ways to go by land, and three ways to go by sea. How many ways are
there to go from town X to town Y?
+ ⋯ +
A1
1
∪
3
A2 ∪ A3
2
|A 1 | + |A 2 | + |A 3 |
2 + 4 + 3
9.
1
n2 ways for event E2 to occur. Then the number of ways for E1 and E2 to
occur is n ⋅ n .
1 2
Example 7.3
In a class of 11 boys and 12 girls, how many ways are there to select
two students, one a boy and one a girl?
E2 to occur. Thus there are 132 ways to select one boy and one girl from
the class.
Using the language of set theory, the multiplication principle could also be
written this way.
|A × B| = |A| ⋅ |B|.
Example 7.4
You own 7 pairs of pants and 15 shirts. How many out ts can you
make?
Let A be the set that contains 7 pairs of pants and B be the set that
contains 15 shirts. Each out t consists of one pair of pants and one shirt,
so each out t is an element of A × B. The number of different out ts
possible is
|A × B| = |A| ⋅ |B| = 7 ⋅ 15 = 105.
|A 1 × A 2 × ⋯ × A n | = |A 1 | ⋅ |A 2 | ⋯ |A n |.
Example 7.5
7
A1 × A2 × ⋯ × A7 = A1 ⋅ A2 ⋯ A7 = 2 = 128.
3.2
answer ← answer × n
4. Output n.
Strictly speaking, this example does not require either the addition or
the multiplication principles. The while-do loop in step 3 executes
while n > 1. Notice that in step 3.1 we have n ⟵ n − 1, so n is
decreased by one each time the loop executes. Suppose we input
positive integer n = 5; then the while-do loop would execute when n
was 5, 4, 3, and 2. It would not execute when n = 1. Therefore the
while-do loop would execute four times. Suppose we input positive
integer n = 10. This time it would execute nine times. In general,
whenever we input the positive integer n the while-do loop executes
n − 1 times.
Example 7.7
What is the maximum number of times the for-do loop in step 2 is
executed? What is the maximum number of times the if-then
conditional control in step 2.1 is executed?
2. For i = 1 to n do
2.1. If x = s then
i
Again, strictly speaking, this example does not require either the
addition or the multiplication principles. However, it shows us
something important. The for-do loop in step 2 is executed when i goes
from 1 to n. This means it is executed a maximum of n times. Each time
the for-do loop is executed the If -then conditional control is also
executed. Thus the maximum number of times the If -then conditional
control is executed is n times.
Notice, it is possible that both the for-do loop and If -then conditional
control are executed fewer times. If for some number i < n we have
x = s then the If -then conditional control step 2.1.1 is executed, the
i
cannot say for sure if the for-do loop will execute less than n times.
Computer scientists and programmers are most interested in the
maximum number of times a loop or conditional control is executed.
Example 7.8
What is the maximum number of times the for-do loops in steps 2 and 3
are executed? What is the maximum number of times the If -then
conditional controls in steps 2.1 and 3.1 are executed?
y ,y ,…,y
1 2 of equal length to see if either string contains integer
n
s.
2. For i = 1 to n do
2.1. If x = s then
i
Example 7.9
What is the maximum number of times the for-do loop in step 2 is
executed? What is the maximum number of times the for-do loop in
step 2.1 is executed? What is the maximum number of times the If -then
conditional control in step 2.1.1 is executed?
2. For i = 1 to n do
2.1 For j = 1 to n do
2.1.1 If x = y then
i j
the for-do loop in step 2.1 is again executed n times. The same when
i = 3, and so on. So for each of the n times the for-do loop in step 2 is
executed the for-do loop in step 2.1 is executed n times. This means the
for-do loop in step 2.1 is executed a maximum of n × n = n times. 2
The If -then conditional control in step 2.1.1 is executed once for each
time the for-do loop in step 2.1 is executed, so it is also executed a
maximum of n2 times.
− − −
3 2 1
2! = 2 ⋅ 1 = 2,
3! = 3 ⋅ 2 ⋅ 1 = 6,
4! = 4 ⋅ 3 ⋅ 2 ⋅ 1 = 24,
5! = 5 ⋅ 4 ⋅ 3 ⋅ 2 ⋅ 1 = 120,
6! = 6 ⋅ 5 ⋅ 4 ⋅ 3 ⋅ 2 ⋅ 1 = 720,
7! = 7 ⋅ 6 ⋅ 5 ⋅ 4 ⋅ 3 ⋅ 2 ⋅ 1 = 5 040,
8! = 8 ⋅ 7 ⋅ 6 ⋅ 5 ⋅ 4 ⋅ 3 ⋅ 2 ⋅ 1 = 40 320,
9! = 9 ⋅ 8 ⋅ 7 ⋅ 6 ⋅ 5 ⋅ 4 ⋅ 3 ⋅ 2 ⋅ 1 = 362 880.
n! = n ⋅ (n − 1) ⋅ (n − 2) ⋯ 3 ⋅ 2 ⋅ 1.
3.2
answer ← answer × n
4. Output n.
Factorials are exactly the notation we need to help us write down how
many permutations are possible. The number of permutations of a set of n
elements is given by n!.
Example 7.10
− − −
26 25 24
26!
=
23!
26!
= .
(26−3)!
n!
.
(n − r)!
n!
n
Pr = .
(n − r)!
Example 7.11
How many different ways are there to arrange 5 letters from the
alphabet?
The number of permutations of 5 letters from the 26 letters of the
alphabet is given by
26! 26!
26
P5 = = = 7 893 600.
(26 − 5)! 21!
Example 7.12
You are given a list of 20 different avors of ice cream and asked to
rank your four favorite avors. How many different possible responses
are there?
20! 20!
20
P4 = = = 116 280.
(20 − 4)! 16!
Example 7.13
Here order matters. We may say that the rst person selected becomes
the chairperson, the second person becomes the secretary, and the third
person becomes the treasurer. Thus this becomes a permutation
question, how many permutations of three items from 12 items. The
answer is given by
12! 12!
12
P3 = = = 1 320.
(12 − 3)! 9!
− − −
26 25 24
Considering how the three letters could be ordered in the three spaces gave
us
− − −
3 2 1
combinations.
In the general case where we want to know how many combinations of r
objects drawn from n objects there are we need to divide the number of
permutations, P , by r! to give us
n
r
n
Pr n!
= .
r! r!(n − r)!
have
n n!
( ) = .
r r!(n − r)!
Example 7.14
26 26! 26!
( ) = = = 65 780.
5 5!(26 − 5)! 5!21!
Example 7.15
For people on a committee it does not matter how they are arranged, so
we are asking how many combinations of three are there from 12 items,
which is given by
12 12! 12!
( ) = = = 220.
3 3!(12 − 3)! 3!9!
Example 7.16
How many subsets of size two are there from a set of size n?
Example 7.17
Example 7.18
(b) The word HELLO has ve letters but two of the letters are L.
There are P = 5! = 120 ways of permuting ve letters.
5
5
11!
= 34 650.
4! ⋅ 4! ⋅ 2!
Example 7.19
5
7 × 7 × 7 × 7 × 7 = 7 = 16 807.
Example 7.20
7! 7!
7
P5 = = = 2 520.
(7 − 5)! 2!
Example 7.21
27
27 men is given by ( ) . We then use the multiplication principle to
3
22 27
( ) ⋅ ( ) = 4 504 500.
3 3
Example 7.22
possibilities. We then choose the second three positions where there are
now C possibilities. We now use the multiplication principle to get
17
3
20 17
P3 ⋅ C3 = (6 840)(680) = 4 651 200.
But suppose we decided to rst choose the three at-large trustees and
then choose the president, secretary, and treasurer. In this case the
number of possibilities for the rst three positions is C and the 20
3
20 17
C3 ⋅ P3 = (1 140)(4 080) = 4 651 200.
It should not surprise you that these numbers are the same.
Example 7.23
What is the maximum number of times the for-do loop in step 2 is
executed? What is the maximum number of times the for-do loop in 2.1
is executed? What is the maximum number of times the If -then
conditional control in setp 2.1.1 is executed?
1. Input string x , … , x .
1 n
2. For i = 1 to n −1 do
2.1 For j = i + 1 to n do
2.1.1 If x = x then
i j
2
n n(n − 1) n − n
2
f (n) = ( ) = = = 0.5n − 0.5n,
2 2 2
so the maximum number of times the for-do loop in step 2.1 is executed
is 0.5n − 0.5n times. The maximum number of times the If -then loop
2
Question 7.1 How many ve digit decimal number are there that contain
only odd digits?
Question 7.2 How many ve digit octal number are there that contain only
odd digits?
Question 7.3 How many ve digit hexadecimal number are there that
contain only odd digits?
Question 7.4 How many different ways can a rst, second, and third prize
be awarded in a class of 32 students.
Question 7.5 How many different ways are there to choose a ve person
committee that has the positions chair, vice-chair, communications
director, secretary, and treasurer from a group of 20 people?
Question 7.6 How many different ways are there to choose a committee of
ve people from a group of 20 people?
Question 7.8 A school assigns two kinds of identi cation numbers to its
staff. Teachers have an ID that starts with a T which is followed by four
digits. Administrators have an ID that starts with an A and is followed by
three digits. How many total identi cation numbers are available for the
school to use?
Question 7.9 How many anagrams are there for the following words?
(a) COMBINE
(b) PERMUTE
(c) ASSIGNMENT
Question 7.12 An online quiz has four pools of questions. The rst two
pools have 10 questions each and the last two pools have 20 questions
each. If two questions are drawn at random from each of the rst two pools
and three questions are drawn at random from each of the second two pools
(for a total of ten questions) how many possible quizzes can be generated.
(Assume the order of the questions does not matter.)
Question 7.13 What is the maximum number of possible times the while-
do loop in line 4 could execute?
1. Input x , x , … , x .
1 2 n
2. i ← 1
3. order ← true
4. While i < n and order = true do
4.1
If x > x
i i+1 then
4.1.1
order ← f alse
4.2
i ← i + 1
2. For i = 1 to n do
2.1
For j = 1 to n do
2.1.1
sum ← 0
2.1.2
For k = i to n do
2.1.2.1
sum ← sum + x ik × x kj
2.1.3
c ij ← sum
3. Output c . ij
Question 7.15 The following is an algorithm that sorts a list of n numbers
into increasing order. What is the maximum number of times the for-do
loop in step 2 could execute? What is the maximum number of times the
while-do loop in step 2.3 could execute?
1. Input x , x , … , x .
1 2 n
2. For i = 2 to n do
2.1
insert ← x i
2.2
j ← i − 1
2.3.2
j ← j − 1
2.4
x j ← insert
3. Output x 1, x2 , … , xn .
2. For i = 1 to n do
2.1. a = 1
ii
3. For i = 1 to n do
3.1 For j = 1 to n do
3.1.1 If a = 1 then
ij
3.1.1.1 For k = 1 to n do
3.1.1.1.1 a ← a + a (Boolean Addition)
jk jk ik
4. Output (a ). ij
and
n
C
r
nP r
and
nCr
or
P (n, r)
and
C(n, r)
.
CHAPTER 8
Algorithmic Complexity
One of the most important jobs for programmers is choosing, or designing, the fastest
algorithms to use when writing programs. Therefore, it is extremely important that they have
some way to compare the speed of different algorithms. But algorithms can be very
complicated and very different from each other, so comparing them is not easy. This chapter
focuses on how this is done.
f : N ⟶ N
Algorithm
The general idea is that given any algorithm one can nd a function associated with that
algorithm that essentially describes how long it takes the algorithm to run. Because
algorithms can be so different and so complicated we need to make three simplifying
approximations. The function found is called the algorithm's time-complexity function.
Once the time-complexity function for the algorithm is obtained we nd what is called the
big- O (pronounced “big-oh”) category for the time-complexity function. This requires us to
make another two simplifying approximations. Doing this essentially groups together all the
algorithms that have roughly the same speed into the same category. In other words, we
usually think of all the algorithms whose time-complexity functions are in the same big- O
category as being equally fast. Finally, we then use the big- O categories to rank algorithms
by speed.
As we have already stated, algorithmic complexity analysis requires us to make a total of
ve simplifying approximations. These ve approximations are listed below. The rst three
approximations are needed to nd the time-complexity function for an algorithm and are
studied in section 8.2. The last two approximations are related to nding the big- O category
of the time-complexity function and will be studied in section 8.4.
f : N ⟶ N
M aximum number of
is of size n.
This de nition of time-complexity actually uses the rst three approximations. The phrase
“dominant operations” means we are using the rst and second approximations, counting
only the most time consuming frequent operation. The phrase “maximum number” means
we are using the third approximations, using only the worst-case scenario. What exactly is
meant by the phrase “the input is of size n” depends on the exact nature of the algorithm. In
section 8.3 we will look at examples of algorithms and nd the time-complexity functions
for these algorithms.
What we are doing is using the time-complexity function as a way of indirectly measuring
how long an algorithm takes to run. Of course, the answer is not given in terms of a time like
seconds or hours or weeks, it is given in terms of how many times the dominant operations
are performed in the worst case scenario. How long it takes to run the algorithm depends on
the speed of the computer you are using, and every year the speed of computers keeps
getting faster, but the relative times needed to perform operations stays the same.
1. Input x and n.
2. answer ⟵ x
3. For i = 2 to n do
3.1. answer ⟵ answer × x
4. Output answer.
In step 2 there is an assignment and in step 3.1 there is both an assignment and a
multiplication. The assignment in step 2 is executed only once, whereas the assignment and
the multiplication in step 3.1 are executed each time the for-do loop is executed. The rst
approximation tells us that we are only interested in the operations that are frequently
executed, therefore we are only interested in the operations in step 3.1, not in the operation
in step 2 since it is only executed once. In practice this means we are mostly interested in the
operations that happen inside loops.
The second approximation states that of the most frequently executed operations, we only
count the most time-consuming operation. This means we only count the slowest operation.
The most time-consuming frequent operation is the dominant operation of the algorithm.
Here we list the operations from fastest to slowest:
Assignment (⟵)
Comparisons ( =, ≠, >, <, ≮, ≯)
Addition and subtraction ( +, −)
Multiplication and division( ×, /)
We would like to know how long it would a computer to perform each of these operations.
In general we cannot know for sure, but we can say that assignments are usually faster than
comparisons, which are usually faster than addition and subtraction, which in turn are
usually faster than multiplication and division.
Let us consider the two operations from step 3.1 of the above algorithm. The two
operations were assignment and multiplication. According to the above list, assignment is
much faster than multiplication, so multiplication is the most time-consuming frequent
operation. That means that multiplication is the dominant operation in this algorithm. Let us
look at another example.
3. min ← x 1
4. For i = 2 to n do
4.1 If x < min then
i
4.1.1 min ← x 1
5. Output min.
There is an assignment ⟵ in step 3, which is outside the for-do loop. Inside the for-do
loop there are two operations, a comparison < in step 4.1 and an assignment ⟵ in step 4.1.1.
Using approximation one we can ignore the assignment in step 3 because it is only executed
once. We are only interested in the operations inside the for-do loop. Then, using
approximation two we only consider the most time-consuming of these two operations.
Since comparisons are slower than assignments then the comparison in step 4.1 is the
dominant operation in the algorithm.
Figuring out the dominant operation is a little dif cult sometimes. Notice how we said
that assignments are usually faster than comparisons, which are usually faster than addition
and subtraction, which in turn are usually faster than multiplication and division. If we are
only adding one, or subtracting one, or multiplying by one, we generally consider these as
very fast operations. It is easy to simply add or subtract one, and multiplying by one simply
gives us the original number back. So if an operation is only adding or subtracting one or
multiplying by one then it is usually not considered a dominant operation. Look at the below
algorithm.
1. Input x , x , … , x .
1 2 n
2. For i = 2 to n do
2.1
insert ⟵ xi
2.2
j ⟵ i − 1
2.3
While j ≥ 1 and x j > insert do
2.3.1
xj+1 ⟵ xj
2.3.2
j ⟵ j − 1
2.4
xj ⟵ insert
3. Output x 1, x2, … , xn .
There are assignments in steps 2.1, 2.2, 2.3.1, 2.3.2, and 2.4. Assignments are all quite fast.
There are two comparisons in step 2.3. And it appears there is an addition in step 2.3.1 and
subtractions in steps 2.2 and 2.3.2. Using the above list we might be tempted to say that the
addition and subtractions are the dominant operations. But these are simply adding or
subtracting one. The comparison x > insert is almost certainly more time consuming.
j
Therefore in this case the comparisons would be the dominant operation. This is the sort of
thing that will be more obvious to you after you have taken some programming courses.
Approximation Three
The third approximation states that we should only analyze the worst-case scenario.
Consider the following algorithm.
2. For i = 1 to n do
2.1. If x = s then
i
Suppose we wanted to use this algorithm to check if integer 7 was contained in each of these
two 50 digit strings,
01234567890123456789012345678901234567890123456789,
00000000000000000000000000000000000000000000000000.
For the rst string the for-do loop is executed eight times. For the second string the for-do
loop is executed all 50 times. The number of times the for-do loop is executed depends on
the input. If we are checking to see if integer s is contained in an n digit string then the
maximum possible number of times the for-do loop could be executed is n times. It may be
executed fewer times than that, but n times is the worst-case scenario. When analyzing
algorithms we always assume any loops are executed the maximum possible number of
times.
Example 8.1
2. For i = 1 to n do
2.1. If x = s then
i
First we ask ourselves, what is the dominant operation in this algorithm. The only
operation that is done in this algorithm is checking to see if xi is equal to s in step 2.1,
therefore this comparison is the dominant operation. Next we ask ourselves how many
times this operation is performed. We are concerned with the worst-case scenario. If there
is an s in the string then “String contains s.” is returned in step 2.1.1 and the algorithm
ends. But if there is no integer s in the string x , … , x then the for-do loop must run n
1 n
times, once for each xi in the string. This is the worst-case scenario. This means the
dominant operation is done n times. Thus the time-complexity function is
f (n) = n.
Example 8.2
2. For i = 1 to n do
2.1. If x = s then
i
The analysis of this algorithm is almost exactly the same as in the last example. In the
worst-case scenario the integer s is not contained in either string. This would mean the
for-do loop in step 2 is carried out n times and then the for-do loop in step 3 is also
carried out n times. The addition principle gives the time-complexity function as
f (n) = n + n = 2n.
Example 8.3
integer. (That is, check and see if there is an integer s that is contained in both
strings.)
The dominant operation in this algorithm is the comparison in step 2.1.1 where we check
to see if xi is equal to yj. How many times is the operation executed in the worst-case
scenario? Notice that this operation is contained in a nested loop. There is the for-do loop
of step 2 and then the for-do loop of step 2.1. The worst-case scenario happens if there is
no integer that is common to both strings. If this is the case then for i = 1 the for-do loop
in step 2.1 runs n times. Then for i = 2 it runs n times again. Then for i = 3 it runs n
times again, and so on. The multiplication principle tells us the dominant operation
executes a total of n2 times. This means the time-complexity function for this algorithm
is
2
f (n) = n .
Example 8.4
Algorithm: Checks to see if a string of integers x1, x2, … , xn contains any integer
more than once.
1. Input string x , … , x .
1 n
2. For i = 1 to n−1 do
2.1 For j = i + 1 to n do
2.1.1 If x = x then
i j
This algorithm is similar to the last one, but with one notable difference. The dominant
operation is the comparison in step 2.1.1, which is contained in a double loop. The rst
for-do runs from i = 1 to n−1 but the second for-do loop only runs from j = i + 1 to n.
Roughly, this algorithm does half the work of the last example so f (n) ≈ . Another
2
n
way to think about it is that the algorithm does one comparison for each set of two non-
equal indices i and j. This is the same as the number of sets of two different numbers
{i, j} that are in the set {1, 2, … , n}. This is a combination problem. Thus the time-
Example 8.5
1. Input x , x , … , x .
1 2 n
2. For i = 2 to n do
2.1
insert ⟵ xi
2.2
j ⟵ i − 1
2.3
While j ≥ 1 and x j > insert do
2.3.1
xj+1 ⟵ xj
2.3.2
j ⟵ j − 1
2.4
xj ⟵ insert
We have already determined that the dominant operations in this algorithm are the
comparisons in step 2.3. But how many times are these operations performed? First, we
have the loop in step 2 happening n − 1 times. Each time this loop is executed the
variable j is initialized as i − 1. Then each time the while-do loop is executed the j value
is decreased by one. The while-do loop continues until j = 1. In other words, for each
value of i the values of j range form 1 to i − 1. This is just like the previous example, the
number of times the while-do loop is executed is the same as the number of subsets of
two non-equal numbers {i, j} that are in the set {1, 2, … , n}. This is a combination
problem. Since there are two comparisons in step 2.3 the time-complexity function is
n n(n−1)
2
f (n) = 2( ) = 2( ) = n − n.
2
2
Example 8.6
What is the time complexity function for the rst algorithm to nd xn.
1. Input x and n.
2. answer ⟵ x
3. For i = 2 to n do
3.1. answer ⟵ answer × x
4. Output answer.
f1(n) = n − 1.
Example 8.7
What is the time complexity function for the second algorithm to nd xn.
1. Input x and n.
2. d ⟵ number of bits in n. ( n 10 = bdbd−1bd−2 … b1 2 )
3. If b = 1 then
1
3.1
answer ⟵ x
else
3.2
answer ⟵ 1
4. For i = 2 to d do
4.1
x ⟵ x × x
4.2
If b = 1 then
i
4.2.1
answer ⟵ answer × x
5. Output answer.
We will take a closer look at this algorithm in the next section, but for now notice that
the dominant operation in this algorithm is multiplication in steps 4.1 and 4.2.1. What
controls the number of times the dominant operation is executed? The number of times
the multiplication in step 4.1 happens depends on d, the number of bits of n. The
multiplication in step 4.2.1 depends on whether a particular digit bi in the binary
expansion of n is a 1 or a 0. Thus, the number of dominant operations depends entirely on
the binary expansion of n.
The for-do loop says to repeat steps 4.1 and 4.2 from i = 2 to d, or d − 1 times. The if-
then conditional control tells us to do the multiplication in step 4.2.1 only if the digit bi of
the decimal representation of n is 1. Here we apply approximation three and assume the
worst-case scenario, that every digit in the decimal representation of n is 1. Hence this
multiplication is done d − 1 times as well. Thus, each time the for-do loop happens then
in the worst case scenario there are two multiplications, which means a total of 2(d − 1)
multiplications happen. As will be explained in the next section, d = ⌊log (n)⌋ + 1, so
2
we have
f2(n) = 2 log2(n).
A word of caution, by replacing ⌊log (n)⌋ by log (n) the codomain of this time-
2 2
complexity function is no longer ℕ but ℝ, but this is something that is done quite often in
practice.
Approximation Four
In the fourth approximation we assume the input is large. In order to understand why
approximation four is important we will consider the two different algorithms for computing
xn. This is the rst algorithm.
1. Input x and n.
2. answer ⟵ x
3. For i = 2 to n do
3.1. answer ⟵ answer × x
4. Output answer.
And here is the second algorithm for computing xn. Notice in the second step we are asked
to nd the number of bits of n. The word bits comes from binary digits and simply means the
number of digits that is in the binary number equal to n. Thus, if n is written as a binary
number with digits b , … , b then n is d bits. For example, if n = 41 = 101001 then
d 1 10 2
the number of bits (binary digits) contained in the decimal number n is given by the formula
d = ⌊log2 (n)⌋ + 1.
1. Input x and n.
2. d ⟵ number of bits in n. ( n 10 = bdbd−1bd−2 … b1 2 )
3. If b = 1 then
1
3.1
answer ⟵ x
else
3.2
answer ⟵ 1
4. For i = 2 to d do
4.1
x ⟵ x × x
4.2
If b = 1 then
i
4.2.1
answer ⟵ answer × x
5. Output answer.
The way the rst algorithm works for nding xn is obvious, but the way the second
algorithm works for nding xn may not be obvious at all. We will use a simple example to
illustrate how these algorithms differ. Suppose you wanted to nd 264. Using the rst
algorithm would require 63 multiplications since
64
7 = 7 × 7 × 7 × 7 × 7 × 7 × 7 × 7 × ⋯ × 7 .
64 32 16 8 4 2
7 = 7 × 7 × 7 × 7 × 7 × 7 × 7.
If we are clever then we can drastically reduce the number of multiplications necessary to
nd 764. First we nd 7 × 7 which takes one multiplication and that gives us 72. Then we do
2 4
7 × 7 which is a second multiplication and gives us 7 . Then we do 7 × 7 which is a third
2 4 4
multiplication and gives us 78. Then 7 × 7 is a fourth multiplication and gives us 716. Then
8 8
7
16
× 7
16
is a fth multiplication and gives us 732. Finally 7 × 7 is a sixth multiplication
32 32
which gives us 764. Thus by being clever we have reduced 63 multiplications to six
multiplications. This is a huge improvement.
Essentially the second algorithm does just this, it attempts to reduce the number of
multiplications necessary to nd xn. A carefully constructed algorithm can reduce the
amount of work necessary. This is where the fourth approximation becomes important.
Example 8.8
Suppose we wanted to nd 75. How many multiplications would be needed in each of the
two algorithms to nd xn?
This is easily seen using the time complexity functions for these algorithms that were
found in the last section. The time complexity function for the rst algorithm is
f (n) = n − 1, so for the
1 rst algorithm we have
f1(5) = 5 − 1 = 4.
The time complexity function for the second algorithm is f2(n) = 2 log2(n) , so for the
second algorithm we have
Based on this we might easily conclude that the rst algorithm is actually a little better
than the second algorithm.
Example 8.9
This is easily seen using the time complexity functions for these algorithms that were
found in the last section. For the rst algorithm we have
Here are a few more examples that make it clear why it is important to assume that the
input n is large.
Example 8.10
It should be clear that for any natural number n > 1 that n2 is a smaller number than n3.
That means that algorithm one performs fewer dominant operations than algorithm two
and so it is faster. You can see this using a table.
n f1(n) = n
2
f2(n) = n
3
1 1 1
5 25 125
10 100 1, 000
Example 8.11
Now suppose the time-complexity of algorithm one is f (n) = 1, 000, 000n = 10 n and
1
2 6 2
Let us use the same values of n that we used in the last example.
n f1(n) = 1, 000, 000n
2
f2(n) = n
3
1 1, 000, 000 1
5 25, 000, 000 125
10 100, 000, 000 1, 000
According to this table, for all the values of n that we have chosen, algorithm two does
fewer dominant operations than algorithm one, thus we might think that algorithm two is
faster. This is where assumption four becomes very important. Let us make another table
for much larger values of n.
n f1(n) = 10 n
6 2
f2(n) = n
3
1 10 6 1
10 6 10 18 10 18
10 15 10 36 10 45
n f1(n) = 10 n
6 2
f2(n) = n
3
10 20 10 46 10 60
Up until n = 10 algorithm two is faster, but when n is larger than 106 algorithm one is
6
Approximation Five
In the fth approximation we decide not to distinguish between two time-complexities
that are a constant multiple of each other. A function f2 is said to be a constant multiple of f1
if there is some real number c such that f (n) = cf (n).
2 1
Example 8.12
Determine if two given time-complexity functions are a constant multiple of each other.
1. Are f1(n) = 5n
2
and f2(n) = 50n
2
constant multiples of each other? Since
f2(n) = 10f1(n)
then f1 and f2 are constant multiples of each other. Here the constant is 10.
2. Are f (n) = 500n and f (n) = constant multiples of each other? Since
4
4 n
1 2
100
then f1 and f2 are constant multiples of each other. Here the constant is 50, 000. We
also have
1
f2(n) = f1(n).
50,000
50,000
. So, it does not matter if we write f1 in terms of f2 or f2
in terms of f1.
3. Are f (n) = 200n and f
1
3
2(n) = 600n
4
constant multiples of each other? We have
f2(n) = 3nf1(n).
Here the multiple is 3n. Since n is a variable then 3n is not a constant but changes
when the value of n changes and so f2 is not a constant multiple of f1.
De nition 8.2. Suppose we have two time-complexity functions f and g. We say that f is
O (g) if there is some positive number c such that
f (n) ≤ cg(n)
for all n that are very large. O (g) is read “big-oh of g.”
Example 8.13
5n + 20 ≤ 6n.
When is this inequality true? By subtracting 5n from both sides of the inequality we can
see it is true when
20 ≤ n.
Thus, for all values of n larger than or equal to 20 we have 5n + 20 ≤ 6n and so f is O (g)
. We could also say that f is O (n).
Example 8.14
Since f and g are time-complexity functions and f : N ⟶ N then the domain of both f
and g is ℕ. This means that n is a positive number. A little thought should convince you
that
5 4 2 5 2
10n − 7n + 20n + 100 ≤ 10n + 20n + 100..
5 2 5 5 5
10n + 20n + 100 ≤ 10n + 20n + 100n .
5 5 5 5
10n + 20n + 100n = 130n .
5 4 2 5
10n − 7n + 20n + 100 ≤ 130n .
sum of different terms, the f is big- O of the fastest growing term in the sum.
The functions that are of most interest in analyzing algorithms in computer science are 1,
2 n
log (n), √n, n, nlog (n), n , and 2 . Fig. 8.1 plots these functions for 0 ≤ n ≤ 100. The
2 2
function f (n) = 1 is not much of a function, no matter what the value of the input n is the
output is always 1. It should be apparent the function 1 does not grow at all as n increases.
The function log (n) grows very slowly, √n grows a little faster, n grows linearly, nlog (n)
2 2
grows faster yet, n2 grows even faster, and 2n grows fastest of the functions listed. By
looking at Fig. 1 we can see that for large n we have the following inequalities,
2 n
1 < log2(n) < √n < n < nlog2(n) < n < 2 .
Figure 8.1 A comparison of the growth rates for some common time-complexity functions.
2 3 4 5 6
1 < n < n < n < n < n < n < ⋯.
k n
n < 2
for any given value of k ∈ N. These hierarchies of functions can be used to help us gure out
what big- O category a time-complexity function is in.
Example 8.15
Consider the time-complexity function f (n) = 25√n + 100n + 5000 . Find the function
g such that f is O (g).
All we have to do is look at each term in the polynomial and use the above string of
inequalities. From the string of inequalities we know that for large n we have √n < n
and 1 < n, which gives us
25√n + 100n + 5000 < 25n + 100n + 5000n
< 5125n,
so f is O (n).
Example 8.16
Again, we rely on the string of inequalities above. For large n we know that
log (n) < nlog (n) and √n < nlog (n), which gives us
2 2 2
20n log2(n) + 10√n + 50n log2(n) < 20n log2(n) + 10n log2(n) + 50n log2(n)
so f is ℴ (n log (n))
2
.
Example 8.17
Of course we could continue exactly as in the last two examples, but the key point in the
last two examples is that we look for the slowest term in f (n). The rst term has an
n log (n), the second term has a √n, and the third term has an n. Using the chain of
2
Example 8.18
Find the big- O category for the time-complexity function
f (n) = 100log2(n) + 2n + 50n
2
+ 5000 .
The rst term of f has an log (n), the second term has an n, the third term has an n2 and
2
the fourth term is simply the constant 5000. Since 5000 = 5000(1) we could say that this
term has a 1. Putting these in order in terms of speed gives us
2
1 < log2(n) < n < n .
Now we can actually understand the fth approximation better. Consider the following four
time-complexity functions,
5
f1(n) = √n, f2(n) = 10√n, f3(n) = 500√n, f4(n) = 10 √n.
These four time-complexity functions are all a constant multiple of each other. Also, all four
of these time-complexity functions are O (√n) and so all four of these algorithms are in the
same big- O category, the category of algorithms that have time-complexity functions that
are O (√n).
Of course, some algorithms might be a little faster and some might be a little slower, but
relatively speaking their speeds are all fairly close together and, for very large n, all are
much faster than any algorithm in a slower category like O (n) or O (n log (n)). Therefore 2
big- O categories are used to rank algorithm speeds and all algorithms in the same big- O
category are considered to be equally fast. Because all algorithms with time-complexity
functions in the same category are considered to be essentially the same this means we do
not distinguish between two time-complexities that are a constant multiple of each other.
This explains the wording of approximation ve very nicely.
2 3 4 k n
1 < log2(n) < √n < n < n log2(n) < n < n < n < ⋯ < n < 2
considered fairly fast; algorithms that have time-complexity functions that are O (√n) are
slower, but still fast; and so on. An algorithm that has a time-complexity function which is
O (2 ) is considered very slow.
n
Example 8.19
Suppose algorithm one has time-complexity function f ∈ O (log (n)) and algorithm two
1 2
log2(n) < √ n
Example 8.20
3
n < n
so algorithm two is faster than algorithm one. We could also say that algorithm one is
slower than algorithm two.
Example 8.21
Suppose algorithm one has time-complexity function f (n) = 50n log (n) + 200√n and
1 2
algorithm two has time-complexity function f (n) = 30log (n) + 500n. Which
2 2
algorithm is slower?
First we have to nd the big- O categories of each algorithm. It is easy to see that
f ∈ O (n log (n)) and f ∈ O (n). Comparing the categories we have
1 2 2
n < n log2(n).
Example 8.22
Suppose algorithm one has time-complexity function f (n) = 25√n + 10n log (n) and 1 2
algorithm two has time-complexity function f (n) = 100n log (n) + 20n + 50. Which 2 2
algorithm is faster?
First we have to nd the big- O categories of each algorithm. It is easy to see that
f ∈ O (n log (n)) and f ∈ O (n log (n)). Both algorithms are in the same big- O
1 2 2 2
8.6 PROBLEMS
Question 8.1 Determine the dominant operation for all the algorithms presented in the
problem section of chapter Introduction to Algorithms.
Question 8.3 Let f (n) = 25n + 500 and g(n) = n. Show that f is O (g).
Question 8.6 Suppose that algorithm one has the time complexity function f1 and algorithm
two has time complexity function f2. Given the time complexity functions below, which
algorithm would you expect to be faster, or would you expect the algorithms to be equally
fast.
(a) f1(n) = n
2
and f 2(n) = n
3
,
(b) f 1(n) = n
3
+ n
2
and f 2(n) = 20n
2
,
(c) f1(n) = 500n
3
and f 2(n) = 10n
3
,
(d) f 1(n) = 10n
2
− 20n and f 2(n) = 20n
3
− 50n
2
,
(e) f1(n) = 15n
5
and f 2(n) = 500n
4
+ 500n
3
,
(f) f1(n) = 10000n
2
and f 2(n) = 0.001n
3
,
(g) f 1(n) = n
4
+ 500n
2
and f 2(n) = n
5
+ 500n
2
,
(h) f 1(n) = 2n and f 2(n) = 7000n .
Question 8.7 Suppose that algorithm one has the time complexity function f1 and algorithm
two has time complexity function f2. Given the time complexity functions below, which
algorithm would you expect to be faster, or would you expect the two algorithms to be
equally fast.
Question 8.8 Arrange the following algorithms in order of increasing growth rate:
(a) 10n
(b) √n
(c) n1.5
Question 8.9 Arrange the following algorithms in order of increasing growth rate:
(a) 2n
(b) n log
2
2
(n)
(c) 2
n
2
Question 8.10 Arrange the following algorithms in order of increasing growth rate:
(a) 2 n log2(n)
(b) 2n
(c) 2 log2(n)
Question 8.11 What is the dominant operation in the following algorithm? What is the time
complexity function of this algorithm?
1. Input x , x , … , x .
1 2 n
2. i ⟵ 1
3. order ← true
4. While i < n and order = true do
4.1
If x > x then
i i+1
4.1.1
order ← f alse
4.2
i ⟵ i + 1
Question 8.12 Given two n × n matrices A and B the following algorithm performs matrix
operation. (An n × n matrix is an n × n array of numbers labeled a where 1 ≤ i ≤ n and
ij
2
1 ≤ j ≤ n. Thus the matrix consists of n numbers.) What is the dominant operation in the
2. For i = 1 to n do
2.1
For j = 1 to n do
2.1.1
sum ← 0
2.1.2
For k = i to n do
2.1.2.1
sum ← sum + xik × xkj
2.1.3
cij ← sum
3. Output c . ij
Question 8.13 The following is Warshall's algorithm. What is the dominant operation in the
following algorithm? What is the time complexity function of this algorithm?
1. Input adjacency matrix (a ).
ij
2. For i = 1 to n do
2.1. a = 1
ii
3. For i = 1 to n do
3.1 For j = 1 to n do
3.1.1 If a = 1 then
ij
3.1.1.1 For k = 1 to n do
3.1.1.1.1 a ⟵ a + a (Boolean Addition)
jk jk ik
4. Output (a ).
ij
CHAPTER 9
Graph Theory
Graph theory is used to model many kinds of real world situations, relations, and processes that include
communication networks, data organization, and computational devices. Many of the tasks that programmers,
computer engineers, and computer scientists need to solve are made much easier by using graph theory.
Developing algorithms to handled graphs is very important in computer science. This makes it necessary for you
to understand the basic ideas of graph theory.
De nition 1. A graph is a set of points called vertex (or vertex) and a set of lines called edges such that each edge
is attached to a vertex at each end.
Very often the vertices are labeled to tell them apart. Two vertices that are connected by an edge are called
adjacent vertices. An edge is said to be incident to the vertex to which it is attached. The various edges that are
connected to a single vertex are said to be incident to each other. The number of vertices of a graph is called the
order of the graph. A graph must have one vertex but it is allowable for a graph to have no edges. A graph that
has no edges is sometimes called a null graph.
Example 9.1
A graph with six vertices and seven edges. This graph has order six. The vertices are labeled with capital
letters.
A graph that is entirely connected is called a connected graph. However, it is possible for a graph to be
disconnected and have several pieces. These pieces are called components of the graph.
Example 9.2
A disconnected order nine graph with three components. One component consists of only a single vertex.
Graphs are made up of a set of vertices, called the vertex set, and a set of edges, called an edge set. Vertices
and edges can be labeled many different ways, but for now we will label vertices with capital letters. In order to
write down the edge set we have to have a way of labeling edges. In this book the edges will usually be labeled
by the two vertices connected by the edge. For example, an edge that connects vertices A and B can be labeled by
AB or BA. Usually we will stick with alphabetical ordering and write this edge as AB.
Example 9.3
Find the vertex set and the edge set of the following graph.
V = {A, B, C, D, E, F }.
Notice that for this graph vertex A is adjacent to vertex B since they are connected by an edge. Similarly
vertex B is adjacent to vertices A, C, and F, and so on. The edge set is given by
In this graph the edges AB, BC , and BF are all incident to each other since they are all connected to vertex
B. Similarly, the edges CD and DE are incident to each other since they are both connected to vertex D, and
so on.
Often we use dictionary ordering1 when writing down the edge set, as we did in the last example. If there is
more than one edge that connects two different vertices these edges are called parallel edges. An edge that
connects a vertex to itself is called a loop. A graph is called a simple graph if it does not have any parallel edges
or loops.
Example 9.4
Does this graph have any parallel edges? Does it have any loops? Find the vertex and edge sets of the
following graph.
This graph has two sets of parallel edges. The rst set of parallel edges are the three edges that connect the
vertices B and F and the second set of parallel edges are the two edges that connect vertices C and D. There is
also one loop, the edge that connects vertex E to itself. Notice that this edge indeed looks like a loop. The
vertex set of the graph is given by
V = {A, B, C, D, E, F }.
Think back to the chapter on set theory. There we said that the sets {1, 7, 3} and {1, 3, 1, 7} were considered as
the same set even though the element 1 is repeated in the second set. We said that repeated elements did not
matter. In the edge set of the above example it looks like the elements BF is repeated three times and CD is
repeated twice. However, each of the elements BF represents a different edge and each of the elements CD
represents a different edge. The edge set is a set that contains the edges, we just happened to use the same name
for different edges. As long as you understand this hopefully there will be no confusion. Also consider the
element EE. This is the loop, it connects the vertex E to itself.
A simple graph is called a complete graph if every vertex of the graph is adjacent to (joined by an edge to) every
other vertex of the graph.
Example 9.5
Sometimes, depending on what the graph represents, the graph's edges need to have a direction assigned to
them. A graph whose edges have a direction assigned to them is called a directed graph. Very similar to the idea
of directed graphs is the idea of weighted graphs. A weighted graph is a graph where every edge has a number
called a weight assigned to it. Usually the weights are positive numbers. These “weights” can represent things
like distances or costs. We will have more to say about weighed graphs in chapter 10.
Example 9.6
Example 9.7
In this example the weight of edge AB is 10, the weight of AC is 7, and so on.
Of course, it should be obvious to you that there are literally an in nite number of different ways of drawing a
graph. We can put the vertex anywhere we want. We can draw the edges any way we want. All that matters when
drawing a graph is which vertices are adjacent to each other, that is, which vertices are connected to each other
by an edge. The locations of the vertices and the lengths of the edges do not matter. Consider the following
example. A little thought should convince you that even though these graphs are drawn differently that they all
are really the same. Graphs that are drawn differently but still represent the same graph are called isomorphic
graphs. If two graphs are isomorphic, then we consider them to be exactly the same.
Example 9.8
Example 9.9
The degree of a vertex is de ned to be the number of edges that are connected to it. Loops are counted twice in
the degree of a vertex since one side of the edge is “out of” the vertex and the other side is “into” the vertex. A
vertex that is part of a graph but that has no edges connected to it is called an isolated vertex. With the degree of
the vertices de ned this way we have the following theorem.
Theorem 9.1 Given a graph G , the sum of the degrees of the vertices of G is equal to twice the number of edges
of G . Using set notation we can write this as
∑ deg(v) = 2|E |
v∈V
Example 9.10
The degree of vertex A is one. We write deg(A) = 1. Similarly we have deg(B) = 5, deg(C) = 4,
deg(D) = 3, and deg(F ) = 4. But what about vertex E? There are four edges connected to vertex E but the
edge EE is connected to vertex E twice. In this case we count the edge EE twice to give us deg(E) = 5. And
what about vertex G? There are no edges connected to G so we have deg(G) = 0. It is worth mentioning that
even though in this picture vertex G is placed “inside” the graph it does not have to be. We could have placed
vertex G anywhere.
Example 9.11
Suppose a graph has four vertices and eight edges. Two vertices have degree two and one vertex has degree
three. What is the degree of the fourth vertex?
First we will label the vertices. Vertex v1 and v2 each have degree 2 and vertex v3 has degree 3. We want to
nd what degree vertex v4 has. Also, since there are eight edges we have |E | = 8. Using the above theorem we
have
∑ deg(v) = 2|E |
v∈V
⇒ 2 + 2 + 3 + deg(v 4 ) = 2(8)
⇒ 7 + deg(v 4 ) = 16
⇒ deg(v 4 ) = 9.
A path is a sequence of vertices such that each vertex is adjacent to the next vertex. Thus a path consists of a
sequence of edges. By going along the path you travel across this sequence of edges. The number of edges
traveled across is called the path length. For us a path may include repeated vertices and edges. Some books may
de ne this a little differently and say a path cannot include repeated edges. If we want to talk about a path that
has no repeated edges we will say that explicitly. A path that starts and ends at the same vertex is called a graph
theory. There are different ways to write a path. Below are some of the acceptable ways to write down a path.
ABF BCD
A − B − F − B − C − D
A → B → F → B → C → D
A, B, F , B, C, D
Example 9.12
The sequence ABF EC is a path of length four. You start at vertex A and then move along the edge that
connects vertex A to vertex B. Once at vertex B you move along one of the edges connecting vertex B to
vertex F and arrive at vertex F. Which edge you take is not speci ed so it does not matter. Then you move
from vertex F to vertex E along the edge that connects them, and then nally you move to vertex C along the
edge that connects those vertices. Some other examples of paths are F BCDEF BA or EEDCDEC . Some
examples of circuits are BF EDCB or F BF ECBF .
Example 9.13
Example 9.14
An example of a bridge.
Consider the graph on the left. It is a connected graph. However, if we were to remove edge DE then we
would have a disconnected graph with two components, as shown on the right. Thus the edge DE is called a
bridge. The graph G with the edge DE deleted is written as G − DE.
Example 9.15
For the following graph write out the vertex and edge sets, nd the degree of each vertex, list the vertices
adjacent to vertex B, list the edges incident to vertex D, list the loops (if any), list the parallel edges (if any),
and list the bridges (if any).
V = {A, B, C, D, E, F },
paths. The graph in Fig. 9.2 is an example of a Eulerian graph. An example of an Euler circuit is
CDCBBADEBC . Another is ABBCBEDCDA. An Eulerian graph will generally have many different Euler
circuits.
BBADCDEBC
CDCBBADEBC
Now notice that the graph that has an Euler path has exactly two vertices with an odd degree and the rest of the
vertices have an even degree. In the graph that has an Euler circuit all vertices have an even degree. There is a
theorem about this.
Theorem 9.2. Let G be a connected graph, then
This theorem is not hard to prove, but we will not prove it here. Instead we will give an algorithm for nding
the Euler circuit or path called Fleury's algorithm. The algorithm that we will give works, but it is not what we
would use to write a computer program, so we will not write it in pseudocode. The pseudocode version is given
later.
Algorithm: Fleury's algorithm for nding an Euler path or Euler circuit.
1. Make sure the graph is connected and has either 0 or 2 odd vertices. If there are 0 odd vertices you
can start anywhere. If there are 2 odd vertices then you must start at one of the odd vertices. Set
current_vertex equal to the starting vertex and set current_path equal to the empty set.
2. Choose any edge incident to the current vertex, but only choose a bridge only if there is no
alternative. Add this edge to current_path.
3. Set current_vertex equal to the vertex at the other end of the edge. (If the edge is a loop the
current_vertex does not change.)
4. Delete the edge and then delete any isolated vertices from the graph.
5. Repeat steps 2 to 4 until all edges have been deleted from the graph. The nal current_path is either
the Euler circuit or Euler path.
Let use Fleury's algorithm on the graph G given in Fig. 9.3. This graph G has the vertex set V and edge set E ,
V = {A, B, C, D, E, F },
Notice, every vertex here is even so we choose to simply start at vertex A, the rst vertex in our vertex set V .
(a) Starting at vertex A we see both edges AB or edge AC are incident to A. We will simply chose whichever
edge is listed rst in the edge set E . We follow AB to vertex B which becomes our new current_vertex
and then delete edge AB from E and add it to current_path. See Fig. 9.4(a). We are left with
V = {A, B, C, D, E, F },
current _ vertex = C,
(b) Next we could follow BC , BD, or BE since all of these edges are incident to B and none is a bridge. We
simply choose the edge listed next in the edge set E which is edge BC . We follow BC to vertex C which
becomes the new current_vertex, and delete BC from E and add it to current_path. See Fig. 9.4(b). We are
left with
= {A, B, C, D, E, F },
(c) Next we could follow AC , CE, or CF since all of these edges are incident to C. However, by looking at
the graph in Fig. 9.4(b) we can see that edge AC is a bridge so we cannot use that edge since there are
other choices. Again we choose the rst of the possible edges listed in E which is edge CE. We follow
this edge to vertex E which becomes the new current_vertex, and delete CE from E and add it to
current_path. See Fig. 9.4(c). We are left with
V = {A, B, C, D, E, F },
current _ vertex = E,
(d) Next we could follow BE, DE, or EF since all of these edges are incident to E. However, by looking at
the graph in Fig. 9.4(c) we can see that edge EF is a bridge so we cannot use that edge since there are
other choices. Again we choose the rst of the possible edges listed in E which is edge BE. We follow
this edge to vertex B which becomes the new current_vertex, and delete BE from E and add it to
current_path. See Fig. 9.4(d). We are left with
V = {A, B, C, D, E, F },
current _ vertex = B,
(e) Looking at Fig. 9.4(d) there is only one edge we can possibly follow now, edge BD, which we follow to
vertex D which becomes our new current_vertex. We then delete BD from E and add it to current_path.
We would then have an isolated vertex B so we delete B from V . See Fig. 9.4(e). We are left with
= {A, C, D, E, F },
(f) Looking at Fig. 9.4(e) there is only one edge we can possibly follow now, edge DE, which we follow to
vertex E which becomes our new current_vertex. We then delete DE from E and add it to current_path.
We would then have an isolated vertex D so we delete D from V . See Fig. 9.4(f). We are left with
V = {A, C, E, F },
E = {AC, CF , EF },
current _ vertex = E,
(g) Looking at Fig. 9.4(f) there is only one edge we can possibly follow now, edge EF , which we follow to
vertex F which becomes our new current_vertex. We then delete EF from E and add it to current_path.
We would then have an isolated vertex E so we delete E from V . See Fig. 9.4(g). We are left with
V = {A, C, F },
E = {AC, CF },
current _ vertex = F,
(h) Looking at Fig. 9.4(g) there is only one edge we can possibly follow now, edge CF , which we follow to
vertex C which becomes our new current_vertex. We then delete CF from E and add it to current_path.
We would then have an isolated vertex F so we delete F from V . See Fig. 9.4(h). We are left with
{ }
V = {A, C},
E = {AC},
current _ vertex = C,
(i) Looking at Fig. 9.4(h) there is only one edge we can possibly follow now, edge AC , which we follow to
vertex A which becomes our new current_vertex. We then delete AC from E and add it to current_path.
We would then have an isolated vertex C so we delete C from V . See Fig. 9.4(i). We are left with
V = {A},
E = { },
current _ vertex = A,
At this point all edges have been deleted from the graph and we are now done. The current_path set is given by
A − B − C − E − B − D − E − F − C − A
AB = e 1 , AC = e 2 , BC = e 3 , BD = e 4 , BE = e 5 , CE = e 6 , CF = e 7 , DE = e 8 , EF = e 9 .
However, we will maintain the same vertex labels as before. See Fig. 9.5. Also, instead of doing a complete
trace we will consider the four steps in 3.3 as a singe step. We will also leave off the nal output column so the
table is not too wide to t on the page.
Figure 9.5 The graph from Fig. 9.3 with the edges relabeled.
; new_path ⟵ v
3.3 Repeat
3.3.1 e ⟵ rst element of unused_edges incident to v
3.3.2 v ⟵ vertex adjacent to v via edge e
3.3.3 new_path ⟵ new_path, e, v
3.3.4 unused_edges ⟵ unused_edges − {e}
until no element of unused_edges is incident to v
3.4 current_path ⟵ (current_pathbef oreinsertion_point), new_path,
(current _ pathaf terinsertion _ point)
4. Output current_path.
The trace of the modi ed Fleury's algorithm for the graph in Fig. 9.5 is shown in Table 1. At the end of the trace
we have unused _ edges = ∅ and so the while-do loop ends and we move to step four, which outputs the
current_path,
Ae 1 Be 4 De 8 Ee 6 Ce 7 F e 9 Ee 5 Be 3 Ce 2 A.
Table 9.1 Trace of modi ed Fleury's algorithm for graph in Fig. 9.5.
2 A - - - - {e 1 , e 2 , … , e 9 }
3.1 A A - - - {e 1 , e 2 , … , e 9 }
3.2 A A - A A {e 1 , e 2 , … , e 9 }
3.3.1– e1
A A B Ae 1 B {e 2 , e 3 , … , e 9 }
4
Step current_path \beginsubarraycinser − tion _ point\endsubarray e v new_path unused_edges
3.3.1- e3
A A C Ae 1 Be 3 C {e 2 , e 4 , e 5 , e 6 , e 7 , e 8 ,
4
3.3.1- e2
A A A Ae 1 Be 3 Ce 2 A {e 4 , e 5 , e 6 , e 7 , e 8 , e 9
4
3.4 Ae 1 Be 3 Ce 2 A A e2 A Ae 1 Be 3 Ce 2 A {e 4 , e 5 , e 6 , e 7 , e 8 , e 9
3.1 Ae 1 Be 3 Ce 2 A B e2 A Ae 1 Be 3 Ce 2 A {e 4 , e 5 , e 6 , e 7 , e 8 , e 9
3.2 Ae 1 Be 3 Ce 2 A B e2 B B {e 4 , e 5 , e 6 , e 7 , e 8 , e 9
3.3.1- e4
Ae 1 Be 3 Ce 2 A B D Be 4 D {e 5 , e 6 , e 7 , e 8 , e 9 }
4
3.3.1- e8
Ae 1 Be 3 Ce 2 A B E Be 4 De 8 E {e 5 , e 6 , e 7 , e 9 }
4
3.3.1- e5
Ae 1 Be 3 Ce 2 A B B Be 4 De 8 Ee 5 B {e 6 , e 7 , e 9 }
4
3.4 Ae 1 Be 4 De 8 Ee 5 Be 3 Ce 2 A B e5 B Be 4 De 8 Ee 5 B {e 6 , e 7 , e 9 }
3.1 Ae 1 Be 4 De 8 Ee 5 Be 3 Ce 2 A E e5 B Be 4 De 8 Ee 5 B {e 6 , e 7 , e 9 }
3.2 Ae 1 Be 4 De 8 Ee 5 Be 3 Ce 2 A E e5 E E {e 6 , e 7 , e 9 }
3.3.1- e6
Ae 1 Be 4 De 8 Ee 5 Be 3 Ce 2 A E C Ee 6 C {e 7 , e 9 }
4
3.3.1- e7
Ae 1 Be 4 De 8 Ee 5 Be 3 Ce 2 A E F Ee 6 Ce 7 F {e 9 }
4
3.3.1- e9
Ae 1 Be 4 De 8 Ee 5 Be 3 Ce 2 A E E Ee 6 Ce 7 F e 9 E {}
4
Ae 1 Be 4 De 8 Ee 6 Ce 7 F e 9
3.4 E e9 E Ee 6 Ce 7 F e 9 E {}
Ee 5 Be 3 Ce 2 A
A − B − D − E − C − F − E − B − C − A.
This is indeed an Euler circuit for the graph G though it is different from the one we had obtained using Fleury's
algorithm. Notice how this algorithm was constructed so we never needed to decide if a loop was a bridge or not.
Instead, we kept nding new paths using new_path and inserting them into current_path.
This algorithm needs to be changed just a little bit if you have a semi-Eulerian graph and want to nd an Euler
path instead. Recall, a semi-Eulerian graph has two vertices with odd degree with the rest of the vertices having
even degree.
1. Let v1 and v2 be the two vertices with odd degree. Insert a new edge, called e1, between v1 and v2. Our
new graph G is now an Eulerian graph.
2. Let the vertex set of G be V = {v , v , … , v } and let the edge set be E = {e , e , … , e }.
1 2 m 1 2 n
3. Apply the algorithm for nding the Euler circuit of an Eulerian graph to G .
9.3
we would have
[
1
2
4
−9
−3
5
],
⎢⎥
4. Remove v , e from the beginning of the Euler circuit you obtain. What remains is an Euler path
1
The way we have drawn graphs up till now is a wonderful way for us to visualize and understand graphs. But of
course a computer cannot visualize or understand graphs the same way humans do. We need to be able to
represent a graph in a way that a computer can handle. One way of doing this is simply to use the vertex set and
edge set to represent the graph. This way of representing a graph is good enough for the modi ed Fleury's
algorithm. Another common way to represent graphs is to use matrices.
First we explain what a matrix is. A matrix is a rectangular array of numbers, generally written enclosed in
square brackets. For example,
a 11 = 7,
a 21 = 2,
7
2
2
11
4
−6
8
⎤
⎣
a 11
a 21
a 31
a 12
a 22
a 32
−9
−3
a 12 = 4,
a 22 = −9,
,
a 13
a 23
a 33
⎤
⎦
.
and
⎡
⎣
2
−1
−2
−4
−4
are all examples of matrices. The size of the matrix is indicated with the number of rows and columns, usually
It is important to remember that the row subscript comes rst and the column subscript comes second, arc. Thus,
].
a 23 = 5.
Since computers can handle arrays of numbers easily, a graph is represented in computers by using a special
matrix. The most common kind of matrix is called an adjacency matrix. Suppose we have a graph whose vertex
6
−3
written as (rows)×(columns). For example, the sizes of the above matrices are 2 × 3 (read “two by three”), 4 × 2
(read “four by two”), and 4 × 4 (read “four by four”). A matrix that has the same number of rows and columns is
called a square matrix. The entries in the matrix are sometimes called matrix elements and are denoted with
variables that have two subscripts, the rst subscript for the row number and the second subscript for the column
7
10
3
⎤
⎦
a ij
For the moment we will label vertices with vi instead of the usual capital letters to make it easier for you to see
how the adjacency matrix is made. Consider the graph in Fig. 9.6. Since there are no loops going from v1 to v1
we have a = 0. Since there are two edges from v1 to v2 we have a = 2. Since there is one edge from v1 to v3
11 12
we have a = 1. Since there is one edge from v1 to v4 we have a = 1. Thus we have just found the rst row in
13 14
v1 v2 v3 v4
v1 0 2 1 1
v2 2 0 0 0
v3 1 0 1 1
v4 1 0 1 0
⎡ 0 2 1 1
⎤
2 0 0 0
.
1 0 1 1
⎣ 1 0 1 0
⎦
But notice something, for an undirected graph the number of edges from vertex vi to vertex vj is exactly the same
as the number of edges from vertex vj to vertex vi. This is because they are exactly the same edges. Therefore, in
the adjacency matrix we have a = a . Matrices that satisfy this property are called symmetric matrices. They
ij ji
are said to be symmetric “across the diagonal.” The diagonal of the matrix is represented by the matrix elements
a11, a22, a33, and so on. Notice that the lower left triangular region is a re ection of the upper right triangular
region. It is easy to see this matrix is symmetrical,
⎡ 0 2 1 1
⎤
2 0 0 0
.
1 0 1 1
⎣ 1 0 1 0
⎦
Because the matrix is symmetrical sometimes you will see the adjacency matrix written down with only the
diagonal and one of the triangular regions lled out. The upper triangular adjacency matrix and lower triangular
adjacency matrix are given by
Here is one possible answer.
Example 9.17
v1
v2
v3
v1
0
⎢⎥
⎡
⎣
0
v2
2
0
2
⎣
1
⎦
0
1
1
v3
0
1
2
and
Example 9.16
⎡ ccccc0
1
1
0
0
1
1
1
⎡
0
⎤
⎦
0
.
0
0
1
1 0
⎤
respectively. Since both of these matrices give all the information needed you should not be surprised if you
We begin by lling out a table for this graph. Since this is an undirected graph we can simply ll out the
diagonal and one of the triangular regions.
v4
1
0
0
v5
0
1
2
v6
1
0
0
v1 v2 v3 v4 v5 v6
v4 0 0 2
v5 0 1
v6 1
It is then easy to write down the adjacency matrix using this table. In fact, this table already gives the upper
triangular adjacency matrix.
Finally, we will take a quick look at the adjacency matrix for a directed graph. We will use the same graph as
before in Fig. 9.6, but we will now add directions to each edge, see Fig. 9.7. This will allow you to compare
between the adjacency matrices for undirected and directed graphs. We follow basically the same procedure as
before, except that now we have to specify which vertices the edges are “from” and which vertices the edges go
“to.” In the below table we will say the edges go from the vertices along the left and to the vertices along the top.
So, the terms in the adjacency matrix of a directed graph are given by
Since the rst index in the subscript of a represents the row then the vi are the “from” vertices and since the
ij
second index in the subscript of a represents the column then the vj are the “to” vertices. The adjacency matrix
ij
so a = 1. We have one edge going from v1 to v4 making a = 1. The loop goes from v3 and then back to v3
13 14
giving us a = 1. And nally, we have one edge going from v3 to v4 giving us a = 1. The rest of the terms are
33 34
0 2 1 1
⎡ ⎤
0 0 0 0
.
0 0 1 1
⎣ 0 0 0 0
⎦
Take a look at Fig. 9.8. This is a directed graph with no parallel edges going in the same direction. Suppose you
are sitting at vertex v1 and you want to know if you can get to v4. By looking at the directed graph we can see
that there is a path from v1 to v4, namely the path
v1 → v2 → v3 → v4
Now suppose we wanted to go from vertex v2 to v1. By looking at the graph we can see that there is no way to
actually get to v1. When the directed graphs are small it is easy for us to look at the graph and answer this
question. It is not so easy for computers. Nor is it easy for humans if the directed graph is large.
If there exists a path from vi to vj we say that vj is reachable from vi. We also say that vi is reachable from vi.
This just means that if you are standing at a vertex you can reach that vertex by not moving. You can think of this
as a path of length zero. A matrix that tells us when we can reach vj from vi is called a reachability matrix. In a
reachability matrix
⎣
0
0
1
1
1
1
1
1
⎦
.
ij
that instead of saying edge it says path. We can think of the adjacency matrix as a “one step” reachability matrix.
In other words, an adjacency matrix tells us what is reachable by a path of length one. A reachability matrix tells
us what is reachable by a path of any length, including length zero.
Example 9.18
Using the de nition, nd the reachability matrix for the directed graph in Fig. 9.8.
Just by looking we can see that if we are on vertex v1 we can reach v1 (just by staying where we are), v2, v3,
and v4. This gives a = 1, a = 1, a = 1, and a = 1. If we are on vertex v2 we can reach v2 (again by
11 12 13 14
staying where we are), v3, and v4 but can not reach v1. This gives a = 0, a = 1, a = 1, and a = 1.
21 22 23
Similarly, if we are on vertex v3 we can reach v3, v4 and v2 but not v1. This gives a = 0, a = 1, a = 1,
a42 42 = 1, and a
31 32
and a = 1. And nally, if we are on vertex v4 we can reach v4, v2, and v3 but not v1. This gives a = 0,
34
⎡ 1 1
⎤
24
33
41
If we have the adjacency matrix for a directed graph there is an algorithm, called Warshall's algorithm, that
produces the reachability matrix. In order to use Warshall's algorithm we rst have to recall Boolean addition.
Boolean addition is de ned on the set {0, 1} by
0 + 0 = 0,
0 + 1 = 1,
1 + 0 = 1,
1 + 1 = 1.
Similar to the case of Fleury's algorithm, we will give a version of Warshall's algorithm that is easier to
understand before giving a more formal pseudocode version.
Algorithm: Warshall's algorithm to determine if there exists a directed path from vi to vj.
Given the adjacency matrix (a ) of a directed graph:
ij
Using Warshall's algorithm we will nd the reachability matrix for the directed graph in Fig. 9.8. We begin by
writing down the adjacency matrix for this graph,
⎢⎥
⎡
⎣
0
⎣
0
ii
0
1
1
0
0
0
0
⎤
1
.
(a) In step 1 we change all the entries along the diagonal to one, which gives us the matrix
⎡ 1 ⎤
⎦
.
We do this because every vertex is reachable from itself. In other words, for each i we can reach vi from vi by
simply staying where we are so we make a = 1.
(b) In step 2 we look at column one of the matrix we obtained above. Column one is written in gray,
⎡ 1 1 0 ⎤
.
For every row where in column one there is a 1, we add row one of this matrix to the row that has the value 1
using Boolean addition. Row one of this matrix is given by [1, 1, 0, 0]. Notice we put commas in between the
elements of the row just to make it clear.
Since row one is the only row that has a 1 in it in column one then we add row one to itself using Boolean
addition. This means we add each term in the row to itself. Because we are using Boolean addition this does
not change the row at all,
[1, 1, 0, 0] + [1, 1, 0, 0] =
=
[1 + 1, 1 + 1, 0 + 0, 0 + 0]
[1, 1, 0, 0].
(c) In step 3 we look at column two of the matrix we obtained above. Column two is written in gray,
⎡
.
For every row where in column two there is a 1, we add row two of this matrix to the row that has the value 1
using Boolean addition. Row two of this matrix is given by [0, 1, 1, 0] and there are three rows in this column
that have a value 1, row one, row two, and row four.
First we will add row two and row one,
giving us matrix
⎢⎥
[0, 1, 1, 0] + [1, 1, 0, 0]
⎣
1
0
1
1
=
1
⎤
⎦
[0 + 1, 1 + 1, 1 + 0, 0 + 0]
[1, 1, 1, 0]
which becomes our new row one. The Boolean addition of row two with row two gives us
[0, 1, 1, 0] + [0, 1, 1, 0] = [0 + 0, 1 + 1, 1 + 0, 0 + 0]
[0, 1, 1, 0]
which is exactly row two again. Last we will add row two and row four,
[0, 1, 1, 0] + [0, 1, 0, 1] [0 + 0, 1 + 1, 1 + 0, 0 + 1]
[0, 1, 1, 1]
which becomes our new row four. Putting this altogether we have obtained the matrix
(d) Next we look at column three of the matrix we obtained above. Column three is written in gray,
1
⎤
⎦
.
For every row where in column three there is a 1, we add row three of this matrix to the row that has the value
1 using Boolean addition. Row three of this matrix is given by [0, 0, 1, 1]. Since every row in this column has a
value 1, we add row three to every row using Boolean addition. We will not give the details, only the results
=
[1, 1, 1, 1],
[0, 1, 1, 1],
[0, 0, 1, 1],
[0, 1, 1, 1]
giving us matrix
we obtained above,
21
⎢⎥
⎡
⎣
1
0
1
1
1
0
1
1
1
1
1
1
⎤
⎦
.
(e) Next we look at column four of the matrix we obtained above. Column four is written in gray,
⎡ ⎤
.
For every row where in column four there is a 1, we add row four of this matrix to the row that has the value 1
using Boolean addition. Row four of this matrix is given by [0, 1, 1, 1]. Since every row in this column has a
value 1, we add row four to every row using Boolean addition. We will not give the details, only the results
.
=
=
[1, 1, 1, 1],
[0, 1, 1, 1],
[0, 1, 1, 1],
[0, 1, 1, 1]
(f) Since we have now nished all four columns we are done. The Reachability matrix is given by the matrix
Since a = 0 there is no path from v2 to v1, since a = 0 there is no path from v3 to v1, and since a = 0
31 41
there is no path from v4 to v1. However, there are paths in all the other cases. Notice how this is exactly what
This procedure is far easier to understand than looking directly at the pseudocode for Warshall's algorithm,
which is given below. However, when writing a program for a computer the pseudocode below is what you would
base your program on.
Algorithm: Warshall's algorithm to determine if there exists a directed path from vi to vj. (Pseudocode
version.)
2. For i = 1 to n do
2.1. a = 1
ii
3. For i = 1 to n do
3.1 For j = 1 to n do
3.1.1 If a = 1then
ij
3.1.1.1 For k = 1 to n do
3.1.1.1.1 a ← a + a (Boolean Addition)
jk jk ik
4. Output (a ).ij
9.5 PROBLEMS
Figure 9.9 The graphs for questions 9.1, 9.11, and 9.18.
Question 9.2 Assume you have a graph G with the following vertex and edge set,
V = {A, B, C, D, E}
(a) A and B
(b) A and C
(c) A and D
(d) B and C
(e) B and D
(f) B and E
(g) E and A
(h) E and C
(i) E and D
Question 9.3 Assume you have a graph G with the following vertex and edge set,
V = {A, B, C, D, E, F }
Question 9.4 Assume you have a graph G with the following vertex and edge set,
V = {A, B, C, D, E}
Find the degree of all the vertices of G . Find all loops, all parallel edges, and all bridges.
Question 9.5 Assume you have a graph G with the following vertex and edge set,
V = {A, B, C, D, E, F }
Find the degree of all the vertices of G . Find all loops, all parallel edges, and all bridges.
Question 9.6 Assume you have a graph G with the following vertex and edge set,
V = {A, B, C, D, E, F }
If the edges AD and EF were removed, how many components would the resulting graph have?
Question 9.7 Suppose you have a graph with ve vertices. Two vertices have degree 3, two vertices have degree
5, and one vertex has degree 2. How many edges does the graph have?
Question 9.8 Suppose you have a graph with eight vertices, four vertices have degree 4, two vertices have degree
3, one vertex has degree 6, and one vertex has degree 8. How many edges does the graph have?
Question 9.9 Suppose a graph has four vertices and ve edges. One vertex has degree 1, one vertex has degree 2,
and one vertex has degree 3. What is the degree of the remaining vertex?
Question 9.10 Suppose a graph has six vertices and twelve edges. There are two vertices of degree 4, one vertex
with degree 2, one vertex with degree 3, and one vertex with degree 6. What is the degree of the remaining
vertex?
Question 9.11 Determine if the graphs in Fig. 9.9 are Eulerian, semi-Eulerian, or neither.
Question 9.12 Determine if the graphs in Fig. 9.10 are Eulerian, semi-Eulerian, or neither.
Question 9.13 Assume you have a graph G with the following vertex and edge set,
V = {A, B, C, D, E, F }
Question 9.14 Use Fleury's algorithm to nd an Euler path on the below graph.
Question 9.15 Assume you have a graph G with the following vertex and edge set,
V = {A, B, C, D, E, F }
Question 9.16 Assume you have a graph G with the following vertex and edge set,
V = {A, B, C, D}
Show that G is an Eulerian graph. Use the modi ed Fleury's algorithm for nding the Euler circuit on an
Eulerian graph starting at vertex A. It helps to relabel the edges as AB = e , AC = e , BC = e , and so on.
1 2 3
Question 9.17 Assume you have a graph G with the following vertex and edge set,
V = {A, B, C, D, E}
Show that G is an Eulerian graph. Use the modi ed Fleury's algorithm for nding the Euler circuit on an
Eulerian graph starting at vertex A. It helps to relabel the edges as AB = e , AC = e , BC = e , and so on.
1 2 3
Question 9.18 Find the adjacency matrices for the graphs in Fig. 9.9.
Question 9.19 Find the adjacency matrices for the graphs in Fig. 9.10.
Question 9.20 Assume you have a graph G with the following vertex and edge set,
V = {A, B, C, D, E}
Question 9.21 Assume you have a graph G with the following vertex and edge set,
V = {A, B, C, D, E, F }
Question 9.22 Assume you have a graph G with the following vertex and edge set,
V = {A, B, C, D, E, F }
Question 9.24 Use Warshall's algorithm to nd the reachability matrix for the below graph.
Question 9.25 Use Warshall's algorithm to nd the reachability matrix for the below graph.
Trees are a special kind of graph that show up everywhere in computer science. Operating systems use a tree
structure for directories, folders, and les. Tree structures are used to store data that has a hierarchical structure.
These data structures are easy to search and sort using standard algorithms. Trees are also used to process the
syntax of computer languages. Recall, we used expression trees in logic and Boolean algebra to understand the
order of operations, write truth tables, and draw circuit diagrams. Also, in many applications it is important to
nd a tree that comes from a directed or weighted graph. A good understanding of tree basics is essential in
computer science.
Example 10.1
Both ABCE and ABCDCE are examples of paths and both BCEF B and BCDCB are examples of
circuits. This graph has three cycles: BCEF B, BCDEF B, and CDEC . Of course it is possible to write
these cycles in different ways. For example, the cycle BCEF B could also be written as CEF BC or as
F BCEF or as ECBF E , and so on.
A tree is a connected graph with no cycles. Trees are called trees because, well, they look like trees. Keeping
with the tree analogy, a vertex of a tree that has degree one is often called a leaf. Trees have several properties
that should be obvious just by looking at examples of trees. If T is a tree then the following properties are true;
Example 10.2
Take a careful look at the trees shown in the last example. Count the number of vertices and edges in each tree.
You should notice that there is always one more vertex than there are edges. This is something that is always true
for trees. The proof of this theorem is not very dif cult so we will give it. It is also a very nice example of
induction. Induction is an technique often used for proving certain kinds of statements and is widely used in
computer science. That is why we give the proof of this theorem.
Theorem 10.1. Any tree with n vertices has n − 1 edges. (Another way of saying this is that any tree with n edges
has n + 1 vertices.)
Proof: Suppose T is a tree with one vertex. Since a tree cannot have any loops we know that there are no edges
1
Next suppose we have a tree T with two vertices v1 and v2. Since trees are connected we know that there is an
2
edge e connecting the two vertices. Remove v2, and e. We are left with a tree that has 1 vertex, and by the last
paragraph we know this tree has 0 edges. Now add back v2 and e to get back T , which increases the number of
2
vertices by one and the number of edges by one. We now know that T has 2 vertices and 1 edge.
2
Next suppose we have a tree T with three vertices v1, v2 and v3. Since trees are connected we know that there
3
is an edge e connecting v3 and some other vertex. Remove v3 and e. We are left with a tree that has 2 vertices,
and by the last paragraph we know it must have 2 vertices and 1 edge. Now add back v3 and e to get back T , 3
which increases the number of vertices by one and the number of edges by one. We now know that T has 3 3
Example 10.3
A rooted tree.
The vertex labeled “root” is the parent vertex of vertices A and H. Vertex A is the parent vertex of both B and
E, and so on. Vertex D is the child of vertex C, and both vertices F and G are children of vertex E, and so on.
Vertices D, F, G, J, K, L, O, and P are all leaves.
A spanning tree of a graph G is a tree with the same vertex set as G and with an edge set that is a subset of the
edge set of G .
Example 10.4
A graph G (left) along with nine possible spanning trees (right). Notice that each spanning tree has the same
four vertices as G and has three edges from G . And, of course, each spanning tree is a tree.
In some cases it is not dif cult to nd out how many different spanning trees a graph has.
Example 10.5
A simple example of counting the number of different spanning trees a graph has.
Consider graph G on the left. How many different spanning trees does it have? The key is to notice that this
graph has only one cycle, B − D − E − B that consists of three edges. If we remove any single edge from
this one cycle we break the cycle and get a spanning tree for the graph. Thus there are three possible spanning
trees, all of which are shown on the right.
Example 10.6
How many different spanning trees does the graph on the left have? This graph has two disjoint cycles,
A − B − C − A and D − E − C − D, each with three edges. If we remove exactly one edge from the rst
cycle and one edge from the second cycle we break each cycle and get a spanning three. There are three
possible edges that we can remove from the rst cycle and three possible edges that we can remove from the
second cycle giving us a total of 3 × 3 = 9 possible ways to obtain a spanning tree. These nine different
spanning trees are shown on the right. Notice, this is exactly an application of the multiplication principle
from chapter 7.
Example 10.7
Counting the number of spanning trees a graph with two cycles which are not disjoint has.
In the graphthere are two cycles, A − C − D − B − A and B − E − D − B. The cycle
A − C − D − B − A contains four edges and the cycle B − E − D − B contains three edges. We may
assume that, like before, the number of possible spanning trees is given by 4 × 3 = 12 but this is not quite
right. Remember, before we were allowed to remove one edge from each cycle. However, now the two cycles
share edge BD. The option where we remove edge BD from the rst cycle and edge BD from the second
cycle does not give a tree and so is not allowed. So the number of possible spanning trees is given by
4 × 3 − 1 = 12 − 1 = 11. Try to draw all 11 spanning trees for this graph.
= {A, B, C, D, E, F },
= {AB 3 , AC 5 , AD 9 , BD 7 , BE 12 , CD 11 , CF 2 , DE 7 , DF 8 , EF 10 }
where we have written the weight of each edge as a subscript to help us easily remember the weights.
Figure 10.1 We want to nd a minimal spanning tree for this weighted graph.
Prim's algorithm is an example of a greedy algorithm. In a greedy algorithm the “least expensive” option is
chosen at each step. Notice how in each step of the algorithm the link with the minimal weight is chosen.
3. For i = 1 to n − 1 do
3.1
e ⟵
rst edge with minimal weight which is incident to exactly one vertex in T
3.2
v ⟵
vertex incident to e which is not in T
3.3
T ← T ∪ {e, v}
3.4
4. Output T .
Instead of simply doing a trace of Prim's algorithm we will discuss each step to make sure you fully
understand the algorithm. We can start with any vertex so we will simply start with the rst vertex in V which is
A. In step two we assign the rst vertex to T which gives us T = {A} and we assign E to unused_edges so we
have unused _ edges = E . We then begin step three, which is a For -do loop which will loop ve times, one
fewer time than the number of vertices in the graph.
(a) We look for the rst edge with minimal weight which is incident to exactly one vertex in T . Since
T = {A} we simply have to look among the edges incident to vertex A, which are AB , AC , and AD .3 5 9
Clearly AB is the edge with the minimal weight. The vertex incident to AB which is not in T is clearly
3 3
⟵ AB 3 ,
⟵ B,
(b) Next we look for the rst edge with minimum weight which is incident to exactly one vertex in T . These
vertices are A and B so we need to look among AC , AD , BD , and BE . Among these edges AC has
5 9 7 12 5
the minimal weight. So for steps 3.1–4 we are left with the assignments
⟵ AC 5 ,
⟵ C,
(c) Next we again look for the rst edge with minimum weight which is incident to exactly one vertex in T .
These vertices are A, B, and C so we now look among AD , BD , BE , CD , and CF . Among these
9 7 12 11 2
edges the one with the minimal weight is CF . So for steps 3.1–4 we have the assignments
2
e
⟵ CF 2 ,
⟵ F,
(d) Next we again look for the rst edge with minimum weight which is incident to exactly one vertex in T .
These vertices are A, B, C, and F so we now look among AD , BD , BE , CD , DF , and EF . The
9 7 12 11 8 10
edge with the minimal wight is BD . So for steps 3.1–4 we have the assignments
7
⟵ BD 7 ,
⟵ D,
(e) Next we again look for the rst edge with minimum weight which is incident to exactly one vertex in T .
These vertices are A, B, C, F, and D so we now look among BE , DE , and EF . The edge with the
12 7 10
⟵ DE 7 ,
⟵ E,
At this point we have gone through the For -do loop ve times and we now move to step 4 which outputs the
tree
T = {A, AB 3 , B, AC 5 , C, CF 2 , F , BD 7 , D, DE 7 , E}.
3. For j = 1 to n − 1 do
3.1
w ⟵
rst vertex w ∈ T for which d(v , w) + weight(e) is minimized, where e ranges over edges in G
1 − T
T ← T ∪ {e, v}
3.5
4. Output T .
We will use the same graph as before, the one shown in Fig. 10.1. We will again start with vertex A. And as
before we will explain each step fully instead of doing a trace of the algorithm. Since we are starting at the
vertex v = A, in step two we set T = {A} and d(A, A) = 0. We then begin step 3 which is a For -do loop that
1
executes a total of ve times, one fewer than the number of vertices in our graph.
(a) In step 3.1 we are looking for the rst vertex w ∈ T for which d(A, w) + weight(e) is minimized, where
e ranges over edges in G − T that are incident to w but not incident to another vertex in T . This sentence
requires some explanation. First, it says that w ∈ T but since T = {A} we must have w = A. In step 3.2
we must also nd the edge e where d(A, A) + weight(e) is minimized. We were told the edges e we can
choose from are the edges in G − T that are incident to A but not incident to any other vertex in T .
Since there are no other vertices in T we do not have to worry about that part now. What edges are
incident to A? They are
AB 3 , AC 5 , AD 9 .
We now have to calculate the value d(A, A) + weight(e) for each of these three edges to nd out which is the
minimum. (If two values are equal we simply choose the rst of the two edges.) We have
d(A, A) + weight(AB 3 )
= 0 + 3 = 3,
d(A, A) + weight(AC 5 )
= 0 + 5 = 5,
d(A, A) + weight(AD 9 )
= 0 + 9 = 9.
The minimum of these values is 3 so we choose w = A and e = AB . With this we can make the rest of the
3
⟵ B,
d(A, B)
AC 5 , AD 9 , BD 7 , BE 12 .
We now have to calculate d(A, w) + weight(e) for each of these edges. Notice, when w = A we use the edges
incident to A but when w = B we use the edges incident to B. We have
d(A, A) + weight(AC 5 )
= 0 + 5 = 5,
d(A, A) + weight(AD 9 )
= 0 + 9 = 9,
d(A, B) + weight(BD 7 )
= 3 + 7 = 10,
d(A, B) + weight(BE 12 )
= 3 + 12 = 12.
Recall, we had found d(A, B) = 3 in the last step. The minimum of these values is 5 so we choose w = A and
e = AC . We now make the rest of the assignments in steps 3.3–5, writing them in a way that is more natural
5
for us,
= C,
= {A, AB 3 , B, AC 5 , C},
d(A, C)
= d(A, A) + weight(AC 5 ) = 0 + 5 = 5.
(c) Again we look for the rst vertex w in T where d(A, w) + weight(e) is minimized. There are now three
possibilities for w, namely vertices A, B, and C. We look among the edges in G − T that are incident to
only one of the vertices A, B, or C. We list these edges,
AD 9 , BD 7 , BE 12 , CD 11 , CF 2 .
We now have to calculate d(A, w) + weight(e) for each of these edges. Again, when w = A we use the edges
incident to A, when w = B we use the edges incident to B, and when w = C we use the edges incident to C.
We have
d(A, A) + weight(AD 9 )
= 0 + 9 = 9,
d(A, B) + weight(BD 7 )
= 3 + 7 = 10,
d(A, B) + weight(BE 12 )
= 3 + 12 = 15,
d(A, C) + weight(CD 11 )
= 5 + 11 = 16,
d(A, C) + weight(CF 2 )
= 5 + 2 = 7.
The minimum of these values is 7 so we choose w = C and e = CF 2 . We now make the rest of the
assignments in steps 3.3–5,
= F,
= {A, AB 3 , B, AC 5 , C, CF 2 , F },
d(A, F )
= d(A, C) + weight(CF 2 ) = 5 + 2 = 7.
(d) Again we look for the rst vertex w in T where d(A, w) + weight(e) is minimized. There are now four
possibilities for w, namely vertices A, B, C, and F. We look among the edges in G − T that are incident
to only one of these vertices. We list these edges,
AD 9 , BD 7 , BE 12 , CD 11 , DF 8 , EF 10 .
We now have to calculate d(A, w) + weight(e) for each of these edges. Again, when w = A we use the edges
incident to A, when w = B we use the edges incident to B, and so on. We have
d(A, A) + weight(AD 9 )
= 0 + 9 = 9,
d(A, B) + weight(BD 7 )
= 3 + 7 = 10,
d(A, B) + weight(BE 12 )
= 3 + 12 = 15,
d(A, C) + weight(CD 11 )
= 5 + 11 = 16,
d(A, F ) + weight(DF 8 )
= 7 + 8 = 15,
d(A, F ) + weight(EF 10 )
= 7 + 10 = 17.
The minimum of these values is 9 so we choose w = A and e = AD 9 . We now make the rest of the
assignments in steps 3.3–5,
= D,
= {A, AB 3 , B, AC 5 , C, CF 2 , F , AD 9 , D},
d(A, D)
= d(A, A) + weight(AD 9 ) = 0 + 9 = 9.
(e) Again we look for the rst vertex w in T where d(A, w) + weight(e) is minimized. There are now ve
possibilities for w, namely vertices A, B, C, F, and D. We look among the edges in G − T that are
incident to only one of these vertices. We list these edges,
BE 12 , DE 7 , EF 10 .
We now have to calculate d(A, w) + weight(e) for each of these edges. Again, when w = A we use the edges
incident to A, when w = B we use the edges incident to B, and so on. We have
d(A, B) + weight(BE 12 )
= 3 + 12 = 15,
d(A, D) + weight(DE 7 )
= 9 + 7 = 16,
d(A, F ) + weight(EF 10 )
= 7 + 10 = 17.
The minimum of these values is 15 so we choose w = B and e = BE 12 . We now make the rest of the
assignments in steps 3.3–5,
= E,
= {A, AB 3 , B, AC 5 , C, CF 2 , F , AD 9 , D, BE 12 , E},
d(A, E)
At this point we have gone through the For -do loop ve times and we now move to step 4 which outputs the
tree
= {A, AB 3 , B, AC 5 , C, CF 2 , F , AD 9 , D, BE 12 , E}.
Figure 10.3 The weighted graph (top) with the spanning tree (bottom) found using Dijkstra's algorithm to solve the minimum distance problem
from vertex A. Notice how different this spanning tree is from the one found using Prim's algorithm shown in Fig. 10.2.
10.4 PROBLEMS
Question 10.1 Suppose you have a tree T with 100 edges. How many vertices do you have? How many of the
100 edges are bridges?
Question 10.2 Suppose you have a tree T with 100 vertices. How many edges do you have? How many of the
edges are bridges?
Question 10.3 Suppose you have a tree T with 100 edges. You remove two edges at random. Is the new graph
disconnected? If so, how many components does the new graph have?
Question 10.4 Suppose you have a tree T with 20 vertices labeled {v , v , … , v }. How many paths (with no
1 2 20
repeated vertices or edges) are there between v1 and v20? How many paths are there between v4 and v17? How
many paths are there between vi and vj for any i and j where i ≠ j?
Question 10.5 Suppose you have a tree T with 20 vertices labeled {v , v , … , v }. You add an edge between v1
1 2 20
and v20. Does the new graph have a cycle? If so, how many cycles does it have? Is the new graph still a tree?
Question 10.6 Suppose you have a rooted binary tree T . Suppose vertex vi is not the root or a leaf. How many
parents does vi have? How many children does vi have?
Question 10.7 Suppose you have a rooted strict binary tree T . Suppose vertex vi is not the root or a leaf. How
many parents does vi have? How many children does vi have?
Question 10.8 Given a tree T with 100 vertices, what is the greatest number of leaves that T can have? What is
the smallest number of leaves that T can have?
Question 10.9 Suppose you have a graph with 6 vertices. One vertex has degree ve and the other vertices have
degree one. Draw the graph.
Question 10.10 For the graphs below answer the following questions.
Question 10.11 For the graphs below answer the following questions.
Question 10.12 Use Prim's algorithm to nd the minimal tree of the following graph starting at vertex A.
Question 10.13 Use Prim's algorithm to nd the minimal tree of the following graph starting at vertex A. Assume
the edge set is given by E = {AB, AC, BC, CD, CE, CF , DF , EF }.
Question 10.14 Suppose you have ve buildings that you want to connect together using a local area network
(LAN). A table giving the price (in thousands of dollars) to link each pair of building using ber-optic cables is
given below:
A B C D E
A -
B 24 -
C 20 21 -
D 28 25 18 -
E 16 26 23 19 -
In order for any two buildings to communicate with each other over the LAN it is enough for there to exist a path
between the two buildings. We want to nd the least expensive possible LAN con guration. In other words, we
want to nd the minimal spanning tree. Use Prim's algorithm to nd the minimal spanning tree. Assume the
edges in the edge set are written in alphabetical order.
Question 10.15 Use Dijkstra's algorithm to nd the shortest path from vertex A to every other vertex. Assume the
edge set is written in alphabetical order.
Question 10.16 Use Dijkstra's algorithm to nd the shortest path from vertex A to every other vertex.
APPENDI X A
Basic Circuit Design
In this appendix we will try to see how several different topics interrelate
in the context of computer science. This appendix is meant to give you a
glimpse of things to come in computer science and to help illustrate in a
concrete way how some of the topics we have already studied get used in
the real world.
Computers use binary numbers because on a fundamental level
computers can only operate on two states, the presence or absence of an
electrical current. We humans interpret these signals as binary numbers.
The most fundamental thing that a computer can do is the adding of two
binary numbers. In this chapter we will see how computers do this.
Example A.1
1 1 1
82376514
+ 47 032 867
0 + 0
= 0,
0 + 1
= 1,
1 + 0
= 1,
1 + 1
= 10.
Next, since dealing with carries requires us to add three single digit binary
numbers we write down all these sums as well;
0 + 0 + 0
= 0,
1 + 0 + 0
= 1,
0 + 0 + 1
= 1,
1 + 0 + 1
= 10,
0 + 1 + 0
= 1,
1 + 1 + 0
= 10,
0 + 1 + 1
= 10,
1 + 1 + 1
= 11.
With this we are prepared to add any two binary numbers as we do in the
next example.
Example A.2
Add the binary numbers 1100 1001 and 1110 1111. (Since it is clear we
are dealing with binary numbers we will not place a small subscript of 2
after each binary number.)
1 1 1 1 1
1101 1001
+1110 1111
1 1011 1000
In the rst column (on the far right) add the two digits in that
column, 1 + 1 = 10. Write 0 below the line and carry 1.
In the second column add the carried 1 along with the two digits in
that column, 1 + 0 + 1 = 10. Write 0 below the line and carry 1.
In the third column add the carried 1 along with the two digits in
that column, 1 + 0 + 1 = 10. Write 0 below the line and carry 1.
In the fourth column add the carried 1 along with the two digits in
that column, 1 + 1 + 1 = 11. Write 1 below the line and carry 1.
In the fth column add the carried 1 along with the two digits in
that column, 1 + 0 + 0 = 1. Write 1 below the line.
In the sixth column add the two digits in that column, 0 + 1 = 1.
Write 1 below the line.
In the seventh column add the two digits in that column,
1 + 1 = 10. Write 0 below the line and carry 1.
In the eighth column add the carried 1 along with the two digits in
that column, 1 + 1 + 1 = 11. Here we write 11 below the line.
In this example the digits that we write below the line are called sum
bits and the digits that we carry (in gray) are called the carry bits.
0 + 0
= 00,
0 + 1
= 01,
1 + 0
= 01,
1 + 1
= 10.
As a rst step we will design a circuit that does this addition. The key here
is that we will split the addition into two parts; one part will give the sum
bits and the other part will give the carry bits. We will rewrite the sum bits
in a slightly different way that will hopefully seem familiar. Notice that we
wrote the table in the reverse order of the sums.
x y
sum bit
1 1 0
1 0 1
0 1 1
0 0 0
This should look very familiar, it is nothing more than the xor table from
chapter 5.
x y x xor y
1 1 0
1 0 1
0 1 1
0 0 0
Thus the xor gate gives us the sum bit from this addition.
We now rewrite the gray carry bits in a table form. Again, we wrote the
table in the reverse order of the sums.
x y carry bit
1 1 1
1 0 0
0 1 0
0 0 0
x y x × y
1 1 1
1 0 0
x y x × y
0 1 0
0 0 0
Thus the and gate gives us the carry bit from this addition.
So if x is the rst digit and y is the second digit then the xor gate gives us
the sum bit and the and gate gives us the carry bit. The whole circuit is
given by
This circuit is called a half-adder. It works perfectly for adding two single-
digit binary numbers. So, when it comes to adding two eight-digit binary
numbers this circuit would work ne for the rst column on the right
where we do not yet have to worry about caries. But for the second column
on, this circuit is not enough since it is possible we have carries. If we have
a carry then we need to add three one-digit binary numbers.
binary numbers. We now rewrite sums of three single digit binary numbers
using both a gray carry bit and a black sum bit;
0 + 0 + 0 = 00, 1 + 0 + 0 = 01,
0 + 0 + 1 = 01, 1 + 0 + 1 = 10,
0 + 1 + 0 = 01, 1 + 1 + 0 = 10,
0 + 1 + 1 = 10, 1 + 1 + 1 = 11.
0 + 0
= 00,
0 + 1
= 01,
1 + 0
= 01,
1 + 1
= 10.
Then we take the sum digit that the half-adder outputs, shown in black
above, and add it to the carry digit c using another half adder. The carry
digit can either be c = 1 or c = 0. The addition of this carry digit with the
sum digit turns out to be
0 + 0
= 00,
1 + 0
= 01,
0 + 1
= 01,
1 + 1
= 10,
0 + 1
= 01,
1 + 1
= 10,
0 + 0
= 00,
1 + 0
= 01.
Notice, the sum digits in black that we get here are exactly the same as the
sum digits that we want to get when we add three single-digit binary
numbers. So putting two half-adders in a row seems to give us the sum
digits that we want. But what about the carry digits? These do not exactly
match up with the carry digits we want when we add three numbers.
Suppose
Then we get the following table for a ∨ b. (Note, in the chapter on Boolean
Algebra we would have written this as a + b but meant Boolean addition.
Here since we are already talking about addition we will use the logical
symbol for or to avoid confusion.)
a b a ∨ b
0 0 0
0 0 0
0 0 0
1 0 1
0 0 0
0 1 1
0 1 1
1 0 1
And a + b are exactly the carry digits we want from adding three digits.
Thus, the circuit we want is a half-adder for x + y, and then another half-
adder for the sum digit of x + y and c, and an or gate for the carry digit of
x + y and c. In other words, we want this circuit:
It would be a good idea to trace through this circuit for all of the possible
values of x, y, and c and check that its outputs match the expected
in
+y 8 y 7 y 6 y 5 y 4 y 2 y 1
c8 s8 s7 s6 s5 s4 s3 s2 s1
In the rst column (on the far right) we add 0 + x + y . This addition 1 1
gives us a carry digit c1 and a sum digit s1. The sum digit s1 is written
below the line and the carry digit c1 is carried to column two. In the second
column we add c + x + y to get a carry digit c2 and a sum digit s2. The
1 2 2
sum digit s2 is written below the line and the carry digit c2 is carried to
column three. Columns three through seven are similar. In column eight
we add c + x + y to give us both a carry digit c8 and a sum digit s8.
7 8 8
Here we write both of these below the line. this is exactly what we want
our circuit to do.
The way we do this is to link up eight full-adders in a row, taking the carry
digit output from each full-adder as the carry digit input into the next full
adder and recording the sum digit output from each full adder. This full
circuit is shown in Fig. A.1. Notice, because we are using a full-adder for
the addition x + y , we automatically have the carry digit input into the
1 1
cs s s s s s s s . Two sixteen-bit binary numbers could be added together using two eight-bit
8 7 6 5 4 3 2 1
adders in a row. The carry bit c would become the input c for the second eight-bit adder.
in
APPENDI X B
Answers to Problems
Question 1.1: This algorithm nds the number of seconds in a period of time that is given as a number of days,
hours, minutes, and seconds. The output is 287, 487.
Question 1.2:
Question 1.3:
Question 1.4:
Question 1.5:
Question 1.6:
Question 1.7:
Question 1.8:
Question 1.9:
Step d n Output
1 - 8, 490, 725, 727, 154, 368, 726, 402, 945 -
2 25 8, 490, 725, 727, 154, 368, 726, 402, 945 -
3.1 25 117(← 8 + 4 + 9 + 0 + 7 + ⋯ + 4 + 5 -
3.2 3 117 -
3.1 3 9(← 1 + 1 + 7) -
4 1 9 9
Question 1.10:
a. x 1 , , , ,
= 2 x2 = 7 x3 = 4 x4 = 3 x5 = 9 x6 = 0 s = 9 , ,
Step i j xi yj Output
2.1.1 1 1 2 4 -
2.1.1 1 2 2 3 -
2.1.1 1 3 2 9 -
2.1.1 2 1 7 4 -
2.1.1 2 2 7 3 -
2.1.1 2 3 7 9 -
2.1.1 3 1 3 4 -
2.1.1 3 2 3 3 -
2.1.1.1 3 2 3 3 There is an element common to both strings.
b. x 1 , , , ,
= 5 x2 = 9 x3 = 3 y1 = 7 y2 = 2 y3 = 5 n = 3 , ,
Step i j xi yj Output
2.1.1 1 1 5 7 -
2.1.1 1 2 5 2 -
2.1.1 1 3 5 5 -
2.1.1 1 3 5 5 There is an element common to both strings.
c. x 1 , , , ,
= 1 x2 = 2 x3 = 3 y1 = 9 y2 = 8 y3 = 7 n = 3 , ,
Step i j xi yj Output
2.1.1 1 1 1 9 -
2.1.1 1 2 1 8 -
2.1.1 1 3 1 7 -
2.1.1 2 1 2 9 -
2.1.1 2 2 2 8 -
2.1.1 2 3 2 7 -
2.1.1 3 1 3 9 -
2.1.1 3 2 3 8 -
2.1.1 3 3 3 7 -
3 3 3 3 7 There is no element common to both strings.
Question 1.11:
a. x1 = 2, x2 = 1, x3 = 0, x4 = 9, y1 = 4, y2 = 0, y3 = 7, y4 = 1, s = 0
Step i xi yi Output
2.1 1 2 - -
2.1 2 1 - -
2.1 3 0 - -
2.1.1 3 0 - String one contains 0.
b. x1 = 5, x2 = 9, x3 = 3, y1 = 7, y2 = 2, y3 = 1, s = 2
Step i xi yi Output
2.1 1 5 - -
2.1 2 9 - -
2.1 3 3 - -
3.1 1 - 7 -
3.1 2 - 2 -
3.1.1 2 - 2 String two contains 2.
c. x1 = 3, x2 = 9, x3 = 7, y1 = 4, y2 = 4, y3 = 3, s = 6
Step i xi yi Output
2.1 1 3 - -
2.1 2 9 - -
2.1 3 7 - -
3.1 1 - 4 -
3.1 2 - 4 -
Step i xi yi Output
3.1 3 - 7 -
4 3 - - Neither string contains 6.
Question 1.12:
a. x1 = 2, x2 = 7, x3 = 3, y1 = 4, y2 = 3, y3 = 9, n = 3
Step i j xi yi Output
2.1.1 1 1 2 4 -
2.1.1 1 2 2 3 -
2.1.1 1 3 2 9 -
2.1.1 2 1 7 4 -
2.1.1 2 2 7 3 -
2.1.1 2 3 7 9 -
2.1.1 3 1 3 4 -
2.1.1 3 2 3 3 -
2.1.1.1 3 2 3 3 There is an element common to both strings.
b. x1 = 5, x2 = 9, x3 = 3, y1 = 7, y2 = 2, y3 = 5, n = 3
Step i j xi yi Output
2.1.1 1 1 5 7 -
2.1.1 1 2 5 2 -
2.1.1 1 3 5 5 -
2.1.1 1 3 5 5 There is an element common to both strings.
c. x1 = 1, x2 = 2, x3 = 3, y1 = 9, y2 = 8, y3 = 7, n = 3
Step i j xi yi Output
2.1.1 1 1 1 9 -
2.1.1 1 2 1 8 -
2.1.1 1 3 1 7 -
2.1.1 2 1 2 9 -
2.1.1 2 2 2 8 -
2.1.1 2 3 2 7 -
2.1.1 3 1 3 9 -
2.1.1 3 2 3 8 -
2.1.1 3 3 3 7 -
3 3 3 3 7 There is no element common to both strings.
Question 1.13:
a. x 1 , , ,
= 6 x2 = 3 x3 = 5 x4 = 2 x5 = 4 ,
Step i j xi xj Output
2.1.1 1 2 6 3 -
2.1.1 1 3 6 5 -
2.1.1 1 4 6 2 -
2.1.1 1 5 6 4 -
2.1.1 2 3 3 5 -
2.1.1 2 4 3 2 -
2.1.1 2 5 3 4 -
2.1.1 3 4 5 2 -
2.1.1 3 5 5 4 -
2.1.1 4 5 2 4 -
3 4 5 2 4 There are no duplicate integer in the string.
b. x 1 , , ,
= 4 x2 = 7 x3 = 3 x4 = 7 x5 = 2 x6 = 9 x7 = 1 x8 = 6, , , ,
Step i j xi xj Output
2.1.1 1 2 4 7 -
2.1.1 1 3 4 3 -
Step i j xi xj Output
2.1.1 1 4 4 7 -
2.1.1 1 5 4 2 -
2.1.1 1 6 4 9 -
2.1.1 1 7 4 1 -
2.1.1 1 8 4 6 -
2.1.1 2 3 7 3 -
2.1.1 2 4 7 7 -
2.1.1.1 2 4 7 7 There is a duplicate integer in the string.
c. x 1 , , , ,
= 5 x2 = 6 x3 = 3 x4 = 7 x5 = 3 x6 = 2 ,
Step i j xi xj Output
2.1.1 1 2 5 6 -
2.1.1 1 3 5 3 -
2.1.1 1 4 5 7 -
2.1.1 1 5 5 3 -
2.1.1 1 6 5 1 -
2.1.1 2 3 6 3 -
2.1.1 2 4 6 7 -
2.1.1 2 5 6 3 -
2.1.1 2 6 6 2 -
2.1.1 3 4 3 7 -
2.1.1 3 5 3 3 -
2.1.1.1 3 5 3 3 There is a duplicate integer in the string.
Question 1.14:
Question 1.15:
Question 1.16:
a. x 1 , , ,
= 5 x2 = 7 x3 = 4 x4 = 6 x5 = 2 x6 = 8 , ,
Step min position i xi Output
2 5 1 - - -
3.1 5 1 2 7 -
3.1 5 1 3 4 -
3.1.1 4 1 3 4 -
3.1.2 4 3 3 4 -
3.1 4 3 4 6 -
3.1 4 3 5 2 -
3.1.1 2 3 5 2 -
3.1.2 2 5 5 2 -
3.1 2 5 6 8 -
4 2 5 6 8 2,5
b. x1 = 3
,x 2 , , ,
= 5 x3 = 7 x4 = 2 x5 = 4 x6 = 6 ,
Step min position i xi Output
2 3 1 - - -
3.1 3 1 2 5 -
3.1 3 1 3 7 -
3.1 3 1 4 2 -
3.1.1 2 1 4 2 -
3.1.2 2 4 4 2 -
3.1 2 4 5 4 -
3.1 2 4 6 6 -
4 2 4 6 6 2,4
c. x 1 , , , ,
= 7 x2 = 2 x3 = 5 x4 = 0 x5 = 9 x6 = 3 x7 = 1 , ,
Step min position i xi Output
2 7 1 - - -
3.1 7 1 2 2 -
3.1.1 2 1 2 2 -
3.1.2 2 2 2 2 -
3.1 2 2 3 5 -
3.1 2 2 4 0 -
3.1.1 0 2 4 0 -
3.1.2 0 4 4 0 -
3.1 0 4 5 9 -
Step min position i xi Output
3.1 0 4 6 3 -
3.1 0 4 7 1 -
4 0 4 7 1 0,4
Question 1.17:
a. x 1 , ,
= 0 x2 = 4 x3 = m x4 = 3 x5 = s x7 = 8 , , ,
Step i xi \beginsubarraycnoninteger _ \\detected\endsubarray Output
2 1 - - -
3 1 - false -
4.1 1 0 false -
4.2 2 0 false -
4.1 2 4 false -
4.2 3 4 false -
4.1 3 m false -
4.1.1 3 m true -
4.1 4 m true -
5.1 4 m true String contains non-integer character.
b. x 1 ,
= 8 x2 = 9 x3 = 0 ,
Step i xi \beginsubarraycnoninteger _ \\detected\endsubarray Output
2 1 - - -
3 1 - false -
4.1 1 8 false -
4.2 2 8 false -
4.1 2 9 false -
4.2 3 9 false -
4.1 3 0 false -
5.2 3 0 false String consists entierly of integers.
c. x1 = 7
,x 2 ,
= 3 x3 = 2 x4 = r x5 = 9 , ,
Step i xi noninteger _ detected Output
2 1 - - -
3 1 - false -
4.1 1 7 false -
4.2 2 7 false -
4.1 2 3 false -
4.2 3 3 false -
4.1 3 2 false -
4.2 4 2 false -
4.1 4 r false -
4.1.1 4 r true -
4.2 5 r true -
String contains
5.1 5 r true
non-digit character.
1. Input string x , x 1 2, … , xn .
2. For i = 1 to n do
Question 2.1:
a. 101 = 5
2 10
b. 110 = 6
2 10
c. 111 = 7
2 10
d. 1011 = 11 2 10
e. 1101 = 13 2 10
f. 1010 = 10 2 10
g. 10010 = 18 2 10
h. 10110 = 22 2 10
i. 11111 = 31 2 10
Question 2.2:
c. 0011 0111 2 = 55 10
h. 0010 1011 2 = 43 10
Question 2.3:
a. 27 = 23
8 10
b. 42 = 34
8 10
c. 35 = 29
8 10
d. 103 = 67
8 10
e. 673 = 443
8 10
f. 360 = 240
8 10
g. 4721 = 2513
8 10
h. 2451 = 1321
8 10
i. 7715 = 4045
8 10
Question 2.4:
a. 7254 = 3756
8 10
b. 1602 = 8988 10
c. 4640 = 2464
8 10
d. 17530 = 8024 8 10
e. 72501 = 30017 8 10
f. 11101 = 4673 8 10
g. 365310 = 125640 8 10
h. 772531 = 259417 8 10
i. 417524 = 139092 8 10
Question 2.5:
a. A3 = 163
16 10
b. 9F = 159
16 10
c. 17 = 23
16 10
d. 3D2 = 978
16 10
e. 9A0 16 = 2464 10
f. AED 16 = 2797 10
g. F 001 16 = 61441 10
h. 6D27 16 = 27943 10
i. 39CB 16 = 14795 10
Question 2.6:
a. 802 = 2050
16 10
b. 6A3 = 1699
16 10
c. F 2E = 3886
16 10
d. 290B = 10507
16 10
e. 4C71 = 19569
16 10
f. 1101 = 4353
16 10
g. ABCDE = 703710 16 10
h. 2E916 = 190742 16 10
i. 97CA0 = 621728 16 10
Question 2.7:
a. 0.11 = 0.75
2 10
b. 0.01 = 0.25
2 10
c. 0.10 = 0.5
2 10
d. 0.110 = 0.75
2 10
e. 0.101 = 0.625
2 10
f. 0.011 = 0.375
2 10
g. 0.0111 = 0.43752 10
h. 0.0011 = 0.18752 10
i. 0.1001 = 0.56252 10
Question 2.8:
a. 100.110 = 4.75 2 10
b. 110.011 = 6.375 2 10
c. 111.001 = 7.125 2 10
d. 1101.1000 = 13.5 2 10
e. 0110.0111 = 6.4375 2 10
f. 1011.1001 = 11.5625 2 10
g. 1111.1111 = 15.9375 2 10
h. 0001.0001 = 1.0625 2 10
i. 1100.0101 = 12.3125 2 10
Question 2.9:
a. 0.4 = 0.5
8 10
b. 0.1 = 0.125
8 10
c. 0.7 = 0.875
8 10
d. 0.72 = 0.90625
8 10
e. 0.03 = 0.046875
8 10
f. 0.14 = 0.1875
8 10
g. 0.553 = 0.708984375
8 10
h. 0.407 = 0.513671875
8 10
i. 0.321 = 0.408203125
8 10
Question 2.10:
a. 7.5 = 7.625
8 10
b. 4.2 = 4.25
8 10
c. 3.6 = 3.75
8 10
d. 37.63 = 31.796875
8 10
e. 56.12 = 46.15625
8 10
f. 25.25 = 21.328125
8 10
g. 427.014 = 279.0234375
8 10
h. 510.442 = 328.56640625
8 10
i. 703.635 = 451.806640625
8 10
Question 2.11:
a. 0.7 = 0.4375
16 10
b. 0.A = 0.625
16 10
c. 0.3 = 0.1875
16 10
d. 0.A8 = 0.65625
16 10
e. 0.4C = 0.295875
16 10
f. 0.82 = 0.5078125
16 10
g. 0.C48 = 0.767578125
16 10
h. 0.379 = 0.2170410156
16 10
i. 0.ABC = 0.6708984375
16 10
Question 2.12:
a. 9.3 = 9.1875
16 10
b. B. E = 11.875
16 10
c. 4.C = 4.75
16 10
d. AC. DC = 172.859375 16 10
e. F 2.39 = 242.22265625
16 10
f. 5E. C5 = 94.76953125
16 10
g. A04.BB8 = 2564.732421875
16 10
i. 101.101 = 257.06274414063
16 10
Question 2.13:
a. 62 = 110010
8 2
b. 31 = 11001
8 2
c. 571 = 101111001
8 2
d. 31.72 = 11001.11101
8 2
e. 42.15 = 100010.001101
8 2
f. 72.37 = 111010.011111
8 2
g. 313.011 = 11001011.000001001
8 2
h. 643.026 = 110100011.00001011
8 2
i. 211.361 = 10001001.011110001
8 2
Question 2.14:
a. A0 = 10100000
16 2
b. 3B = 111011
16 2
c. 27 = 100111
16 2
d. F 2.F 2 = 11110010.1111001
16 2
e. 5B.93 = 1011011.10010011
16 2
f. 88.7C = 10001000.011111
16 2
g. 214.3C5 = 1000010100.001111000101
16 2
h. AD2.0BC = 101011010010.0000101111
16 2
i. 101.011 16 = 100000001.000000010001 2
Question 2.15:
a. 10 = 2
2 8
b. 110 = 6
2 8
c. 11 = 3
2 8
d. 110.111 = 6.7 2 8
e. 011.010 = 3.2 2 8
f. 101.001 = 5.1 2 8
g. 11011.001001 = 33.11 2 8
h. 100011.10111001 = 43.562 2 8
i. 11110001.00011101 = 361.072 2 8
Question 2.16:
a. 1101 = D 2 16
b. 10 = 2
2 16
c. 101 = 5
2 16
d. 1110.1101 = E. D 2 16
e. 1011.1010 = B. A 2 16
f. 0101.0011 = 5.3 2 16
g. 111001011.0011011 = 1CB.36 2 16
h. 1011101.01011010101 = 5D.5AA 2 16
i. 10111100101.100111 = 5E5.9C 2 16
Question 2.17:
a. 7 = 111
10 2
b. 3 = 11
10 2
c. 6 = 110
10 2
d. 28 = 11100
10 2
e. 39 = 100111
10 2
f. 79 = 1001111
10 2
g. 381 = 101111101
10 2
h. 643 = 1010000011
10 2
i. 569 = 1000111001
10 2
Question 2.18:
a. 78 = 116
10 8
b. 93 = 135
10 8
c. 52 = 64
10 8
d. 295 = 447
10 8
e. 944 = 1660
10 8
f. 641 = 1201
10 8
g. 4862 = 11376
10 8
h. 5078 = 11726
10 8
i. 1532 = 2774
10 8
Question 2.19:
a. 3410 = 22 16
b. 2310 = 17 16
c. 536 10 = 218 16
d. 285 10 = 11D 16
e. 1897 10 = 769 16
f. 8321 10 = 2081 16
g. 90867 10 = 162F 3 16
h. 42778 10 = A71A 16
i. 28714 10 = 702A 16
Question 2.20:
a. 0.5 = 0.1
10 2
b. 0.4 ≈ 0.011001
10 2
c. 0.8 ≈ 0.110011
10 2
d. 0.32 ≈ 0.010100
10 2
e. 0.81 ≈ 0.110011
10 2
f. 0.77 ≈ 0.110001
10 2
g. 0.239 ≈ 0.001111
10 2
h. 0.552 ≈ 0.100011
10 2
i. 0.798 ≈ 0.110011
10 2
Question 2.21:
a. 0.9 ≈ 0.714631
10 8
b. 0.4 ≈ 0.314631
10 8
c. 0.6 ≈ 0.463146
10 8
d. 0.83 ≈ 0.650753
10 8
e. 0.25 ≈ 0.2
10 8
f. 0.44 ≈ 0.341217
10 8
g. 0.482 ≈ 0.366621
10 8
h. 0.667 ≈ 0.525402
10 8
i. 0.315 ≈ 0.241217
10 8
Question 2.22:
a. 0.1 ≈ 0.199999
10 16
b. 0.3 ≈ 0.4CCCCC
10 16
c. 0.9 ≈ 0.E66666
10 16
d. 0.11 ≈ 0.1C28F 5
10 16
e. 0.83 ≈ 0.D47AE1
10 16
f. 0.47 ≈ 0.7851EB
10 16
g. 0.429 ≈ 0.6DD2F 1
10 16
h. 0.638 ≈ 0.A353F 7
10 16
i. 0.314 ≈ 0.50624D
10 16
12.3 CHAPTER THREE ANSWERS
Question 3.1:
a. No, an opinion.
b. No, a question.
c. Yes.
d. Yes.
e. Yes.
f. Yes.
g. No, a request.
h. No, a request.
i. Yes.
Question 3.2:
Question 3.3:
a. p ∧ q ≡ T ∧ F ≡ F
b. q ∧ p ≡ F ∧ T ≡ F
c. ¬q ∨ p ≡ ¬F ∨ T ≡ T ∨ T ≡ T
d. ¬p ∨ q ≡ ¬T ∨ F ≡ F ∨ F ≡ F
e. p ∧ ¬q ≡ T ∧ ¬F ≡ T ∧ T ≡ T
f. ¬q ∨ ¬p ≡ ¬F ∨ ¬T ≡ T ∨ F ≡ T
g. p → q ≡ T → F ≡ F
h. ¬q → p ≡ ¬F → T ≡ T → T ≡ T
i. ¬q → ¬p ≡ ¬F → ¬T ≡ T → F ≡ F
j. p ↔ q ≡ T ↔ F ≡ F
k. ¬q ↔ p ≡ ¬F ↔ T ≡ T ↔ T ≡ T
l. ¬p ↔ ¬q ≡ ¬T ↔ ¬F ≡ F ↔ T ≡ F
Question 3.4:
p q p ∧ q
a.
T T T
T F F
F T F
F F F
p q q ∧ p
b.
T T T
T F F
F T F
F F F
p q ¬q ∨ p
c.
T T T
p q ¬q ∨ p
T F T
F T F
F F T
p q ¬p ∨ q
d.
T T T
T F F
F T T
F F T
p q p ∧ ¬q
e.
T T F
T F T
F T F
F F F
p q ¬q ∨ ¬p
f.
T T F
T F T
F T T
F F T
p q p → q
g.
T T T
T F F
F T T
F F T
p q ¬q → p
h.
T T T
T F T
F T T
F F F
p q ¬q → ¬p
i.
T T T
T F F
F T T
F F T
p q p ↔ q
j.
T T T
T F F
F T F
F F T
p q ¬q ↔ p
k.
T T F
T F T
F T T
F F F
p q ¬p ↔ ¬q
l.
T T T
T F F
F T F
F F T
Question 3.5:
We have
p q p → q
T T T
T F F
F T T
F F T
and
p q ¬p ∨ q
T T T
T F F
F T T
F F T
.
Since both p → q and ¬p ∨ q have identical truth tables we can say that p → q ≡ ¬p ∨ q.
Question 3.6:
We have
p q p ↔ q
T T T
T F F
F T F
F F T
and
p q p → q q → p (p → q) ∧ (q → p)
T T T T T
T F F T F
F T T F F
F F T T T
.
Since both p ↔ q and (p → q) ∧ (q → p) have identical truth tables we can say they are equivalent. Using the
implication law we can write p ↔ q ≡ (¬p ∨ q) ∧ (¬q ∨ p).
Question 3.7:
p q r (p ∨ ¬r) → q
a.
T T T T
T T F T
T F T F
T F F F
F T T T
p q r (p ∨ ¬r) → q
F T F T
F F T T
F F F F
p q (q ∧ p) → ¬p
b.
T T F
T F T
F T T
F F T
p q ¬p → (¬p ∨ q)
c.
T T T
T F T
F T T
F F T
p q r ¬(p ∧ ¬q) ∨ r
d.
T T T T
T T F T
T F T T
T F F F
F T T T
F T F T
F F T T
F F F T
p q r ¬[(¬p ∧ q) ∨ r]
e.
T T T F
T T F T
T F T F
T F F T
F T T F
F T F F
F F T F
F F F T
p q r (p ∧ ¬q) ∧ (r ∨ q)
f.
T T T F
T T F F
T F T T
T F F F
F T T F
F T F F
F F T F
F F F F
g. p q r (p ∨ q) ↔ [p ∨ (q ∧ r)]
T T T T
T T F T
T F T T
T F F T
F T T T
F T F F
F F T T
F F F T
p q r (q ∧ ¬r) ↔ ¬(p ∨ r)
h.
T T T T
T T F F
T F T T
T F F T
F T T T
F T F T
F F T T
F F F F
p q r (p ↔ q) ∧ (r ↔ q)
i.
T T T T
T T F F
T F T F
T F F F
F T T F
F T F F
F F T F
F F F T
Question 3.8:
a. p ∨ p = p
b. p ∨ ¬p = T
c. ¬(p ∨ ¬p) = F
d. p ∨ (p ∧ q) = p
e. p ∨ (¬p ∧ q) = p ∨ q
f. p ∧ (¬p ∨ q) = p ∧ q
g. p ∧ p = p
h. p ∧ (p ∨ q) = p
i. p ∧ (p ∨ q ∨ r) = p
j. (p ∧ q) ∨ (p ∧ ¬q) = p
k. ¬(¬p ∨ ¬p) = p
l. (p ∧ q) ∨ (¬p ∧ q) = q
Question 3.9:
Question 3.10:
a. q ∧ [p ∨ (¬p ∧ q)] = q
b. ¬[(p ∧ ¬q) ∨ ¬p] = p ∧ q
c. (¬p ∧ q) ∨ [p ∧ (p ∨ q)] = p ∨ q
d. ¬[p ∨ (p ∧ q)] ∧ q = ¬p ∧ q
e. (p ∧ ¬r) ∨ (¬p ∧ q) ∨ ¬(q ∧ r) = ¬p ∨ ¬q ∨ ¬r
f. ¬(p ∧ q) ∨ (q ∧ r) = ¬p ∨ ¬q ∨ r
g. [p ∨ (q ∧ r)] ∧ (¬p ∨ r) = (p ∨ q) ∧ r
h. (p ∧ ¬r) ∨ (¬p ∧ q) ∨ ¬(p ∧ r) = ¬p ∨ ¬r
i. (p ∧ r) ∨ (¬p ∧ q) ∨ (r ∧ q) = (p ∧ r) ∨ (q ∧ ¬p)
j. ¬p ∨ ¬q ∨ [p ∧ q ∧ ¬r] = ¬p ∨ ¬q ∨ ¬r
k. (p ∨ r) ∧ (¬p ∨ q) ∧ (r ∨ q) = (p ∧ q) ∨ (r ∧ ¬p)
l. ¬(p ∧ q) ∧ (¬p ∨ q) ∧ (¬q ∨ q) = ¬p
Question 3.11:
a. tautology
b. not a tautology
c. tautology
d. not a tautology
Question 3.12
a. contradiction
b. not a contradiction
c. not a contradiction
d. contradiction
12.4 CHAPTER FOUR ANSWERS
Question 4.1:
a. True
b. True
c. False
d. True
e. False
f. False
g. False
h. True
i. True
j. False
k. True
l. False
m. False
n. False
o. False
p. True
q. True
r. True
s. True
t. False
u. True
v. False
w. True
x. False
Question 4.2:
a. True
b. True
c. False
d. False
e. True
f. True
g. False
h. True
i. True
j. True
k. False
l. True
m. False
n. True
o. False
p. False
q. False
r. True
s. False
t. True
u. False
v. False
w. False
x. True
Question 4.3:
a. True
b. True
c. False
d. True
e. False
f. True
g. False
h. True
i. True
j. True
k. True
l. True
m. True
n. True
o. False
p. True
q. True
r. True
s. False
t. True
Question 4.4:
a. { }, {a}
b. { }, {5}
c. { }, {v}
d. { }, {a}, {b}, {a, b}
e. { }, {5}, {7}, {5, 7}
f. { }, {5}, {e}{5, e}
g. { }, {2}, {4}, {6}, {2, 4},
{ }, {2}, {4}, {6}, {2, 4},
h.
{2, 6}, {4, 6}, {2, 4, 6}
Question 4.5:
a. { }, {{a, b, c}}
b. { }, {{x, y}}
c. { }, {{1, 2, 3}}
d. { }, {r}, {{s, t}}, {r, {s, t}}
e. { }, {{x}}, {{y, z}}, {{x}, {y, z}}
f. { }, {{2, 4}}, {6}, {{2, 4}, 6}
{ }, {2}, {{4}}, {6}, {2, {4}},
g.
{2, 6}, {{4}, 6}, {2, {4}, 6}
Question 4.6:
a. {2, 3, 4, 5, 6, 7, 8}
b. {4, 5, 6}
c. {0, 1, 7, 8, 9}
d. {0, 1, 2, 3, 9}
e. {0, 1, 9}
f. {0, 1, 2, 3, 7, 8, 9}
g. {0, 1, 2, 3, 7, 8, 9}
h. {0, 1, 9}
i. {2, 3}
j. {7, 8}
Question 4.7:
a. {1, 2, 3, 4, a, b, c, d, e}
b. {2, c, d}
c. {3, 4, 5, e}
d. {1, 5, a, b}
e. {5}
f. {1, 3, 4, 5, a, b, e}
g. {1, 3, 4, 5, a, b, e}
h. {5}
i. {1, a, b}
j. {3, 4, e}
Question 4.8:
a. {o,p,r,s,t,u,v,y}
b. {p,s,t,u}
c. {q,r,v,w,x,z}
d. {o,q,w,x,y,z}
e. {q,w,x,z}
f. {o,q,r,v,w,x,y,z}
g. {o,q,r,v,w,x,y,z}
h. {q,w,x,z}
i. {o,y}
j. {r,v}
Question 4.9:
a. {-2,2}
b. {-1,1}
c. {-4,5}
d. {3}
e. {-5,5}
f. {-3,4}
g. {-2,2}
h. {-3,2}
i. {0}
Question 4.10:
a. {-1}
b. {-1}
c. { , }
−7
4
3
d. { , }
−5
2
−4
e. {-2,2,3}
f. { , 5}
−7
g. {1}
h. {0}
i. { , }
4
3
2
j. {-5,-3,3}
Question 4.11:
a. |A | = 100
c
b. |B | = ∞
c
c. |A ∩ B | = 0
c c
d. |O | = ∞
c
e. |O ∩ E| = 0
f. |E | = ∞
c
g. |A ∪ B| = ∞
h. |B ∩ E| = 150
i. |A ∩ O| = 50
c
j. |O ∩ E | = ∞ c
k. |A ∩ O | = ∞ c
l. |B ∪ E| = ∞
Question 4.14: 2 10
= 1, 024
Question 4.15: 2 10
= 1, 024, 576
Question 4.16: A × B = {(a, 1), (a, 2), (a, 3), (b, 1), (b, 2), (b, 3), (c, 1), (c, 2), (c, 3)}
S × T = {(e, 9), (e, 8), (e, 7), (e, 6), (f , 9), (f , 8), (f , 7), (f , 6),
Question 4.17:
(g, 9), (g, 8), (g, 7), (g, 6), (h, 9), (h, 8), (h, 7), (h, 6)}
X × Y = {(0, t), (1, t), (2, t), (3, t), (4, t), (5, t),
Question 4.18:
(0, f ), (1, f ), (2, f ), (3, f ), (4, f ), (5, f )}
A ∪ B = {e, m, a, x, j, c, w, k, i, b, v} Ac ∪ B = {g, t, d, o, j, c, w, k, i, v, b}
c
A ∩ B = {j, c, w} A ∪ B = {g, t, d, o, e, m, a, x, j, c, w}
c
A = {g, t, d, o, k, i, b, v} A − B = {e, m, a, x}
c
B = {g, t, d, o, e, m, a, x} B − A = {k, i, b, v}
c
A = {g, i, d, t, b} (A ∩ B) = {m, j, z, c, l}
c
B = {t, b, p, w, v} (A ∩ B) = {m, j, z, c, l, g, i, d, p, w, v}
A ∪ B = {g, i, d, t, b, p, w, v} Ac ∪ B = {m, j, z, c, l, t, b, p, w, v}
Question 4.21: c
A ∩ B = {t, b} A ∪ B = {m, j, z, c, l, g, i, d, t, b}
c
A = {m, j, z, c, l, p, w, v} A − B = {g, i, d}
c
B = {m, j, z, c, l, g, i, d} B − A = {p, w, v}
Question 4.22:
A = {c, z, p, r, g, n, t}
B = {h, d, q, p, r, s, t}
C = {v, o, a, g, n, s, t}
A ∩ B = {p, r, t}
A ∩ C = {g, n, t}
B ∩ C = {s, t}
A ∪ B = {c, z, p, r, g, n, t, s, h, q, d}
A ∪ C = {c, z, p, r, g, n, t, s, v, o, a}
B ∪ C = {h, q, d, p, r, s, t, g, n, v, o, a}
A ∩ B ∩ C = {t}
A ∪ B ∪ C = {c, z, p, r, g, n, t, h, d, q, s, v, o, a}
Ac = {x, f, y, k, j, v, o, a, s, h, q, d}
Bc = {x, f, y, k, j, c, z, g, n, v, o, a}
Cc = {x, f, y, k, j, c, z, p, r, h, d, q}
(A ∩ B)c = {x, f, y, k, j, c, z, g, n, v, o, a, s, h, q, d}
(A ∩ C)c = {x, f, y, k, j, c, z, p, r, h, q, d, s, v, o, a}
(B ∩ C)c = {x, f, y, k, j, d, q, h, r, p, c, z, g, n, v, o, a}
(A ∪ B)c = {x, f, y, k, j, v, o, a}
(A ∪ C)c = {x, f, y, k, j, h, q, d}
(B ∪; C)c = {x, f, y, k, j, c, z}
(A ∩ B ∩ C)c = {x, f, y, k, j, c, z, p, r, g, n, h, q, d, s, v, o, a}
(A ∪ B ∪ C)c = {x, f, y, k, j}
A - B = {c, z, g, n}
B - A = {h, d, q, s}
A - C = {c, z, p, r}
C - A = {v, o, a, s}
B - C = {p, r, h, q, d}
C - B = {g, n, v, o, a}
Question 4.23:
A = {i, p, j, m, t, c, y, n, g, l}
B = {w, k, s, o, t, c, y, d, l}
C = {q, a, v, u, gn, g, d, l}
A ∩ B = {t, c, y, l}
A ∩ C = {n, g, l}
B ∩ C = {d, l}
A ∪ B = {p, i, j, m, t, c, y, n, g, l, k,w, s, o, d}
A ∪ C = {p, i, j, m, t, c, y, n, g, l, d, v, a, q, u}
B ∪ C = {w, k, s, o, t, c, y, d, l, n, g, v, a, q, u}
A ∩ B ∩ C = {l}
A ∪ B ∪ C = {p, i, j, m, t, c, y, n, g, l, k,w, s, o, d, v, a, q, u}
Ac = {e, x, f, r, h, b, z,w, k, s, o, d, u, a, q, u}
Bc = {e, x, f, r, h, b, z, p, i, j, m, n, g, v, a, q, u}
Cc = {e, x, f, r, h, b, z, p, i, j, m, t, c, y, k,w, s, o}
(A ∩ B)c = {e, x, f, r, h, b, z, p, i, j, mn, g, v, a, q, u, d,w, k, s, o}
(A ∩ B)c = {e, x, f, r, h, b, z, p, i, j, m, t, c, y,w, k, s, o, d, v, a, q, u}
(A ∩ B)c = {e, x, f, r, h, b, z, p, i, j, m, t, c, y, k,w, s, o, n, g, v, a, q, u}
(A ∪ B)c = {e, x, f, r, h, b, z, v, a, q, u}
(A ∪ B)c = {e, x, f, r, h, b, z,w, k, s, o}
(B ∪ C)c = {e, x, f, r, h, b, z, p, i, m, j}
(A ∩ B ∩ C)c = {e, x, f, r, h, b, z, p, i, j, m, t, c, y, k,w, s, o, n, g, v, a, q, u, d}
(A ∪ B ∪ C)c = {e, x, f, r, h, b, z}
A - B = {i, j, p, mn, g}
B - A = {w, k, s, o, d}
A - C = {i, j, p, m, t, c, y}
C - A = {a, v, q, u, d}
B - C = {t, c, y, k,w, s, o}
C - B = {n, g, a, v, q, u}
Question 4.24:
c
|A| = 27 |A ∪ B[C| = 57 |(A ∩ B ∩ C) | = 69
c c
|B| = 40 |A | = 47 |(A ∪ B ∪ C) | = 17
c
|C| = 26 |B | = 34 |A − B| = 13
c
|A ∩ B| = 14 |C | = 48 |B − A| = 26
c
|A ∩ C| = 11 |(A ∩ B) | = 60 |A − C| = 16
c
|B ∩ C| = 16 |(A ∩ C) | = 63 |C − A| = 15
c
|A ∪ B| = 53 |(B ∩ C) | = 58 |B − C| = 24
c
|A ∪ C| = 42 |(A ∪ B) | = 21 |C − B| = 10
c
|B ∪ C| = 50 |(A ∪ C) | = 32
c
|A ∩ B ∩ C| = 5 |(B ∪ C) | = 24
Question 4.25:
a. A ∪ A = A
b. A ∪ A = u
c
c. (A ∪ A ) = ∅
c c
d. A ∪ (A ∪ B) = A
e. A ∪ (A ∪ B) = A ∪ B
c
f. A ∩ (A ∩ B) = A ∩ B
c
g. A ∩ A = A
h. A ∩ (A ∪ B) = A
i. A ∩ (A ∪ B ∪ C) = A
j. (A ∩ B) ∪ (A ∩ B ) = A c
k. (A ∪ A ) = A
c c
l. (A ∩ B) ∪ (A c
∩ B) = B
Question 4.26:
a. (A ∪ B ) ∩ (A ∪ B) = A
c c c c
b. B ∪ (B ∩ B ) = B c
c. A ∪ (B ∩ A ) = A
c c c
d. (A ∪ B ) ∩ (A ∪ B) = A
c
e. C ∪ [C ∪ (C ∩ A)] = C
f. A ∩ [A ∪ (A ∩ B)] = A
g. C ∪ (C ∩ A ∩ B ∩ D) = C c
h. C ∩ (C ∩ A ∩ B ∩ D) = C
c c c
Question 4.27:
a. B ∩ [A ∪ (A c
∩ B)] = B
b. [(A ∩ B )]
c
c
= A ∩ B
c
c
[A ∪ (A ∩ B] ∩ B = A ∩ B
c. c c
c
c c c
(A ∩ C )∪ ) (A ∩ B) ∪ (B ∩ C) = A ∪ B ∪ C
d. (A ∩ C ) ∪ (A ∩ B) ∪ (B ∩ C) = A
c
c c c c c
∪ B ∪ C
e. (A ∩ B) ∪ (B ∩ C) = A ∪ B ∪ C
c
c c
c
[A ∪ (B ∩ C)] ∩ (A ∪ C) = (A ∪ B) ∩ C
f.
(A
g.
c
c c c c c
(A ∩ C ) ∪ (A ∩ B)∪(A ∩ r) = A ∪ C (A ∩ C) ∪ (A ∩ B) ∪ (C ∩ B) = (A ∩ C) ∪ (B
h.
c
c c c c c
(A ∩ C ) ∪ (A ∩ B)∪(A ∩ r) = A ∪ C (A ∩ C) ∪ (A ∩ B) ∪ (C ∩ B) = (A ∩ C) ∪ (B
c c c c c c
A ∪ B ∪ [A ∩ B ∩ C ] = A ∪ B ∪ C
i. c c
(A ∪ C) ∩ (A ∪ B) ∩ (C ∪ B) = (A ∩ B) ∪ (C ∩ A )
j. (A ∪ C) ∩ (A ∪ B) ∩ (C ∪ B) = (A ∩ B) ∪ (C ∩ A
c c
)
k. (A ∩ B) ∩ (A ∪ B) ∩ (B ∪ B) = A
c c c c
Question 4.28:
Question 4.29:
Question 4.31:
Question 4.33: Similar to Example 4.37. Equivalence classes are {1, 5}, {2, 4, 6}, {3}.
Question 4.34: This relation is re exive, symmetric, antisymmetric, and transitive. Thus it is both a partial
ordering and an equivalence relation.
12.5 CHAPTER FIVE ANSWERS
Question 5.1:
a. x + x = x
b. x + x = 1 ′
c. (x + x ) = 0 ′ ′
d. x + xy = x
e. x + x y = x + y ′
f. x(x + y) = xy
′
g. x(x) = x
h. x(x + y) = x
i. x(x + y + z) = x
j. xy + xy = x ′
k. (x0 + x ) = x ′ ′
l. xy + x y = y ′
Question 5.2:
a. (x + y )(x + y) = x
′ ′ ′ ′
b. y + (yy ) = y ′
c. x + yx = x
′ ′
d. (x + y )(x + y) = x ′
e. w + [w + (wx)] = w
f. x[x + (xy)] = x
g. w + (wx yz) = w ′
h. w (wxyz) = w
′ ′ ′
Question 5.3:
a. y[x + (x y)] = y ′
b. [(xy ) + x ] = xy
′ ′ ′
c. x y + x(x + y) = x + y
′
d. (x + xy) y = x y ′ ′
e. xz + x y + (yz) = x + y + z
′ ′ ′ ′ ′ ′
f. (xy) + (yz) = x + y + z
′ ′ ′
g. [x + (yz)](x + z) = (x + y)z ′
h. xz + x y + (xz) = x + z
′ ′ ′ ′ ′
i. xz + x y + zy = xz + yx ′ ′
j. x + y + xyz = x + y + z
′ ′ ′ ′ ′ ′
k. (x + z)(x + y)(z + y) = xy + zx ′ ′
l. (xy) (x + y)(y + y) = x
′ ′ ′ ′
Question 5.4:
a. x y
′
b. x + y
′
c. (xy) + z or xy + z ′ ′
d. (x + y)z ′
e. (x + y) + z or x + y + z ′ ′
f. (xy)z or xyz ′
Question 5.5:
a. (xy ) + yorxy + y
′ ′
b. (x + y )y ′
c. (x + y ) + y or x + y ′ ′
+ y
d. (xy )yorxyy ′
Question 5.6:
a. [(x + y)z] ′
b. [(xy) + z] or(xy + z) ′ ′
c. [(xy)z] or (xyz) ′ ′
d. [(x + y) + z]; or (x + y + z) ′
Question 5.7:
a. (z x) + y or z x + y
′ ′
b. (z + x)y′
c. (z x)y or z xy
′ ′
d. (z + x) + y or z + x + y
′ ′
Question 5.11:
a. x y + xy and (x + y )(x + y )
′ ′ ′ ′ ′ ′
b. x y + x y and (x + y)(x + y )
′ ′ ′ ′ ′ ′
d. xy + xy and (x + y)(x + y )
′ ′
e. x y + xy and (x + y )(x + y)
′ ′ ′ ′
g. x y + x y + xy and x + y
′ ′ ′ ′ ′ ′
h. x y + xy + xy and x + y
′ ′ ′ ′
i. x y + xy and (x + y)(x + y)
′ ′
Question 5.12:
′ ′ ′
x yz + xy z + xyz and
a. ′ ′ ′ ′ ′ ′
(x + y + z)(x + y + z )(x + y + z )(x + y + z)(x + y + z)
′ ′ ′ ′ ′ ′ ′ ′ ′
x y z + x yz + x yz + xy z + xyz and
b. ′ ′ ′ ′ ′ ′
(x + y + z )(x + y + z )(x + y + z )
′ ′ ′ ′ ′ ′ ′
x y z + x yz + xy z + xyz and
c. ′ ′ ′ ′ ′ ′ ′
(x + y + z )(x + y + z )(x + y + z)(x + y + z )
′ ′ ′ ′ ′
x y z + x yz + xyz and
d. ′ ′ ′ ′ ′ ′ ′ ′
(x + y + z )(x + y + z)(x + y + z)(x + y + z )(x + y + z )
′ ′ ′ ′ ′ ′ ′
x y z + x y z + xy z + xyz + xyz and
e. ′ ′ ′ ′
(x + y + z)(x + y + z )(x + y + z)
′ ′ ′ ′ ′ ′
x y z + x y z + x yz + xyx and
f. ′ ′ ′ ′ ′ ′
(x + y + z)(x + y + z)(x + y + z )(x + y + z)
Question 5.13:
x y (x + y)x
′
a.
0 0 0
0 1 0
1 0 0
1 1 1
x y (x + y )x
′
b.
0 0 0
0 1 0
1 0 1
1 1 1
x y (x + y)y
′
c.
0 0 0
0 1 0
1 0 1
1 1 0
x y (x + y)(x + y)
d.
0 0 0
0 1 1
1 0 1
1 1 1
x y (x + y)(x + y)
′
e.
0 0 0
0 1 1
1 0 0
1 1 1
x y ′
(x + y )(x + y)
′
f.
0 0 1
0 1 0
1 0 0
1 1 1
x y xx + x(x + y)
g.
0 0 0
0 1 0
1 0 1
1 1 1
x y xy + y(x + y)
h.
0 0 0
0 1 1
1 0 0
1 1 1
x y ′ ′
x y + y (x + y)
i.
0 0 0
0 1 1
1 0 1
x y ′
x y + y (x + y)
′
1 1 0
x y ′ ′
x y + y (x + y)
′
j.
0 0 1
0 1 0
1 0 1
1 1 0
x y xy(x + y)
k.
0 0 0
0 1 0
1 0 0
1 1 1
x y ′ ′
x y (x + y )
′ ′
l.
0 0 1
0 1 0
1 0 0
1 1 0
Question 5.14:
x y z x(x + y + z)
′
a.
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 1
x y z ′
x (x + y + z)
′
b.
0 0 0 1
0 0 1 1
0 1 0 0
0 1 1 1
1 0 0 0
1 0 1 0
1 1 0 0
1 1 1 0
x y z (x + y)(x + z)
c.
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 1
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 1
x y z ′
(x + y )(x + z)
′
d.
0 0 0 0
0 0 1 1
0 1 0 0
0 1 1 1
1 0 0 1
1 0 1 1
1 1 0 0
1 1 1 0
x y z ′ ′ ′
(x y + z )(x + y)
e.
0 0 0 1
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 1
1 1 1 0
x y z xy(x + y + z)
f.
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 0
1 1 0 1
1 1 1 1
12.6 CHAPTER SIX ANSWERS
Question 6.1:
For function in Fig. 6.3(a):
Question 6.2:
For function f1:
a. Range: R = {b, c, e}
b. Neither one-to-one nor onto
c. X Y
d. {(1,c),(2,b),(3,c),(4,e)} ⊂ X × Y
For function f2:
a. Range: R = {b, c, d, e}
b. One-to-one but not onto onto
c. X Y
d. {(1,c),(2,d),(3,e),(4,b)} ⊂ X × Y
For function f3:
a. Range: R = {b, d}
b. Neither one-to-one nor onto
c. {(1,b),(2,b),(3,d),(4,d)} ⊂ X × Y
Question 6.3:
For function f1:
a. Range: = R {a,b,c,d,e,f}
b. Onto but not one-to-one
c. {(1,c),(2,d),(3,e),(4,f),(5,a),(6,b),(7,f)} ⊂ X × Y
For function f2:
a. Range: = R {a,b,c,d,e,f}
b. Onto but not one-to-one
c. {(1,a),(2,c),(3,e),(4,b),(5,d),(6,f),(7,d)} ⊂ X × Y
For function f3:
a. Range: = R{b, c, d, e}
b. Neither onto nor one-to-one
c. {(1,b),(2,c),(3,d),(4,e),(5,d),(6,c),(7,b)} ⊂ X × Y
Question 6.4:
For function f1:
a. Range: = R{a,b,c,d}
b. Neither one-to-one nor onto
c. c
d. f (1) ≡ c, f (2) ≡ b, f (3) ≡ d, f (4) ≡ c, f (5) ≡ a
For function f2:
a. Range: = R{a,b,c,d}
b. Neither one-to-one nor onto
c. X Y
d. f(1) = d, f(2) = d, f(3) = c, f(4) = b, f(5) = a
For function f3:
a. Range: = R{a,b,c,d,e}
b. One-to-one and onto
c. X Y
d. f(1) = a, f(2) = d, f(3) = c, f(4) = b, f(5)= e
Question 6.5:
For function f1:
a. Range: = R{a,b,c,d,e,f}
b. One-to-one and onto
c. X Y
d. f(1) = b, f(2) = c, f(3) = a, f(4) = e, f(5) = f, f(6)= d
For function f2:
a. Range: = R{b, e}
b. Neither one-to-one nor onto
c. X Y
d. f(1) = b, f(2) = b, f(3) = b, f(4) = e, f(5) = e, f(6) = e
For function f3:
a. Range: = R{a,b,c,d,e,f}
b. One-to-one and onto
c. X Y
d. f(1) = c, f(2) = d, f(3) = e, f(4) = f, f(5) = a, f(6)= b
Question 6.6:
For function in Fig. 6.4(a):
a. Domain: X = {-3,-2,0,2,4},
Range: Y = {-3,-2,1,2,4}
b. X Y
c. f(-3)=1, f(-2)=2, f(0)=-2, f(2)=4, f(4)=-3
d. {(-3, 1), (-2, 2), (0,-2), (2, 4), (4,-3)} ⊂ X × Y
For function in Fig. 6.4(b):
b. X Y
c. f(-4)=4, f(-3)=-3, f(-2)=2, f(0)=0, f(2)=-2, f(3)=3, f(4)=-4
d. {(-4, 4), (-3,-3), (-2, 2), (0, 0), (2,-2), (3, 3), (4,-4)} ⊂ X × Y
For function in Fig. 6.4(c):
b. X Y
c. f(-4)=2, f(-2)=3, f(0)=-3, f(1)=4, f(2)=-4, f(4)=5
d. {(-4, 2), (-2, 3), (0,-3), (1, 4), (2,-4), (4, 5)} ⊂ X × Y
a. Domain: X = {-3,-2,-1, 0, 1, 2, 3, 4, },
Range: Y = {-4,-3,-2,-1, 0, 1, 2, 3, }
b. X Y
c. f((-3)=-4, f((-2)=-3, f((-1)=-2, f((0)=-1, f((1)=0, f((2)=1, f((3)=2, f((4)=3
d. {(-3,-4), (-2,-3), (-1,-2), (0,-1), (1, 0), (2, 1), (3, 2), (4, 3)} ⊂ X × Y
Question 6.7:
Question 6.8:
a. not a function
b. function, domain: R, range: [-4,∞)
c. function, domain: R, range: R
d. function, domain: R, range: (-∞, 5]
e. not a function
f. function, domain: R, range: [-4,∞)
Question 6.9:
g ∘ f (1) = g(f (1)) = g(c) = α
Question 6.11:
For function in Fig. 6.3(a):
−1 −1 −1 −1 −1
f (a) = 3, f (b) = 2, f (c) = 1, f (d) = 5, f (e) = 4,
For function in Fig. 6.3(b):
f does not exist
−1
Question 6.12:
f and f do not exist; f
−1
1
−1
3
−1
2
(b) = 4, f
−1
2
(c) = 1, f
−1
2
(c) = 1, f
−1
2
(d) = 2, f
−1
2
(c) = 3
Question 6.13:
f , f , and f
−1
1
−1
2
−1
3
do not exist
Question 6.14:
f and f do not exist; f
−1
1
−1
2
−1
3
(a) = 1, f
−1
3
(b) = 4, f
−1
3
(c) = 3, f
−1
3
(d) = 2, f
3
−1
(c) = 5
Question 6.15:
a. Yes
b. No
c. Yes
d. No
Question 6.16:
a. f −1
(x) =
x
b. f −1
(x) = x+3
1
c. f −1
(x) =
2−x
d. f −1
(x) =
3x−10
e. f −1
(x) = (x + 4)
2
f. f −1
(x) =
2
x
+ 3
12.7 CHAPTER SEVEN ANSWERS
Question 7.2: There are four odd octal digits (1,3,5,7) so 45 = 1 025.
Question 7.3: There are eight odd hexadecimal digits (1,3,5,7,9,B,D,F) so 85 = 32 768.
Question 7.4: P 5
32
=
32!
29!
= 32 × 31 × 30 = 29 760
Question 7.5: P 5
20
=
20!
15!
= 20 × 19 × 18 × 17 × 16 = 1 860 480
Question 7.6: ( 20
5
) =
20!
5!15!
= 15 504
Question 7.7:
(8 + 6) × (5 + 9) = 196
Question 7.8: 10 4
+ 10
3
= 11 000
Question 7.9:
a. 5 040
b. 2 520
c. 907 200
Question 7.10: Number of functions: 531 441; Number of one-to-one functions: 60 480
Question 7.12: C 10
2
⋅ C
10
2
⋅ C
20
3
⋅ C
20
3
= 2 631 690 000
Question 7.14: for-do in step 2: n; for-do in step 2.1: n2; for-do in step 2.1: n3
Question 7.16: for-do in step 2: n; for-do in step 3: n; for-do in step 3.1: n2; for-do in step 3.1.1.1: n3
12.8 CHAPTER EIGHT ANSWERS
Question 8.1:
1.1 Multiplication in step 2.
1.2 Comparison > in step 2.
1.3 Comparison < in step 2.
1.4 Addition and subtraction in steps 3.1 and 3.2.
1.5 Additions in steps 3.1 and 3.2.1.
1.6 Multiplication in step 3.1.
1.7 Multiplication in step 3.2.
1.8 Multiplication in step 3.1.
1.9 Addition (sum) in step 3.1.
1.10 Comparison = in step 2.1.
1.11 Comparisons = in steps 2.1 and 3.1.
1.12 Comparison = in step 2.1.1.
1.13 Comparison = in step 2.1.1.
1.14 Multiplication i × i in step 3.1.
1.15 Division in step 3.1.
1.16 Comparison < in step 3.1.
1.17 Comparisons (is xi a digit) in step 4.1.
Question 8.3: f (n) = 25n + 500 ≤ 25n + 500n = 525n = 525 ⋅ g(n)
Question 8.6:
Question 8.7:
Question 8.9: n
n
2 n 2
log 2 (n) < 2 < 2
Question 8.12: The dominant operation is the multiplication in step 2.1.2.1 and the time complexity is f (n) = n 3
.
12.9 CHAPTER NINE ANSWERS
Question 9.1:
For graph in Fig. 9.9(a):
V = {A, B, C, D, E}
a.
ε = {AB, AE, AE, BD, BD, CE, DE}
V = {A, B, C, D}
a.
ε = {AA, AB, BD, BD, BD, CE, CD}
V = {A, B, C, D, E}
a.
ε = {AA, AB, BD, CD, CE, CE, CE, DE}
V = {A, B, C, D, E, F }
a.
ε = {AB, AC, BC, DE, DF , EF }
Question 9.2:
a. Not adjacent
b. Not adjacent
c. Adjacent
d. Adjacent
e. Adjacent
f. Not adjacent
g. Adjacent
h. Adjacent
i. Not adjacent
Question 9.4: deg(A) = 4, deg(B) = 2, deg(C) = 3, deg(D) = 5, deg(E) = 2; there are two loops, AA and DD; there
are no parallel edges; there are no bridges
Question 9.5: deg(A) = 4, deg(B) = 3, deg(C) = 2, deg(D) = 4, deg(E) = 2, deg(F) = 3; There are two loops, AA
and F F ; there are two parallel edges, BD and BD; there are three bridges, AD, AE, and EF
Question 9.11:
a. Neither
b. Semi-Eulerian
c. Semi-Eulerian
d. Neither
Question 9.12:
a. Neither
b. Semi-Eulerian
c. Eulerian
d. Semi-Eulerian
Question 9.16: The trace of the modi ed Fleury’s algorithm is given below:
Step current_path insertion_point e v new_path unused_edges
2 A - - - - {e1 , e2 , e3 , e4 , ee0 , e1 e2 }
3.1 A A - - - {e1 , e2 , e3 , e4 , ee0 , e1 e2 }
3.2 A A - A A {e1 , e2 , e3 , e4 , ee0 , e1 e2 }
3.3.1-4 A A e1 B Ae1 B {e2 , e3 , e4 , e5 , e6 }
3.3.1-4 A A e3 C Ae1 Be3 C {e2 , e4 , e5 , e6 }
Step
3.4
3.1
3.2
c.
d.
a.
⎢⎥
3.3.1-4
3.3.1-4
3.3.1-4
3.3.1-4
3.4
⎡0
⎣
1
Question 9.19:
⎡0
⎣
1
0
1
0
0
0
current_path
0⎦
A
Ae1 Be3 Ce2 A
Ae1 Be3 Ce2 A
Ae1 Be3 Ce2 A
Ae1 Be3 Ce2 A
Ae1 Be3 Ce2 A
Ae1 Be3 Ce2 A
Ae1 Be4 Ce6 De5 Be3 Ce2 A
Question 9.17: The Euler circuit given by the modi ed Fleury’s algorithm is
⎤
0
0⎦
or
0
or
⎣
3
0
⎡
⎣
1
1
0
1
⎡
1
1
0
insertion_point
⎦
1
1
A
A
B
B
B
B
B
B
The Euler circuit given by the modi ed Fleury’s algorithm is Ae1Be4Ce6De5Be3Ce2A, which we more normally
write as A − B − C − D − B − C − A or simply as ABCDBCA.
Question 9.18:
a.
b.
⎡0
⎣
⎡1
⎣
1
0
0
1
0
1
⎤
⎦
2
or
⎦
⎤
or
⎣
1
0
⎡
⎣
0
0
0
1
0
⎦
0
1 0
⎤
0
⎤
⎦
0
0
0
0
⎤
⎦
e
e2
e2
e2
e2
e4
e6
e5
e5
or
⎡
⎣
1
0
v
B
B
B
B
C
D
B
B
0
0
3
new_path
Ae1 Be3 Ce2 A
Ae1 Be3 Ce2 A
Ae1 Be3 Ce2 A
1
B
Be4 C
Be4 Ce6 D
Be4 Ce6 De5 B
Be4 Ce6 De5 B
0
⎤
⎦
unused_edges
{e4 , e5 , e6 }
{e4 , e5 , e6 }
{e4 , e5 , e6 }
{e4 , e5 , e6 }
{e5 , e6 }
{e5 }
{}
{}
⎢⎥
⎣
⎣
b.
c.
d.
0
⎡0
⎣
⎡0
⎣
⎡0
Question 9.20:
⎡ 1 0 0 1 1⎤
0 1
Question 9.21:
⎡0 1 1 0 0
2
Question 9.22:
⎡0 0 0 1 1
⎣
0 1
0
1
0
1
0
⎦
0
or
0⎦
Question 9.24:
⎤
⎦
0
or
or
⎦
⎦
⎤
⎦
⎤
1
or
or
or
⎣
0
0
⎡
⎣
⎡
⎣
⎡
⎣
0
0
0
0
0
0
⎤
⎦
0
1
0
1
⎤
⎦
⎤
⎦
⎤
Question 9.23: The dominant operation is the Boolean addition in step 3.1.1.1.1. Thus the time complexity
⎢⎥
⎡1
1
⎣0
1
0
1
0
0
Question 9.25:
⎡1 1 0 0 0
0
⎣0
1
1
0
1
0
0
0
0
0
1
⎤
⎦
B.10 CHAPTER TEN ANSWERS
Question 10.1: T has 101 vertices. All 100 edges are bridges.
Question 10.3: The new graph is disconnected and there are three components.
Question 10.4: There is one path with no repeated vertices or edges between v1 and v20. There is one path with no
repeated vertices or edges between v4 and v17. There is one path with no repeated vertices or edges between vi
and vj where i ≠ j.
Question 10.5: The new graph has one cycle so it is no longer a tree.
Question 10.6: The vertex vi has one parent and either one or two children.
Question 10.7: The vertex vi has one parent and two children.
Question 10.8: The greatest number of leaves that T can have is 99. The least number of leaves that T can have
is two.
Question 10.10:
a. three; AB and DE
b. four; AB and EF
c. four; AC, BD, EG, and F H
Question 10.11:
a. 6 × 4 = 24; F G
b. 4 × 4 = 16; F G
c. (5 × 4) − 1 = 19; none
Question 10.12: Using Prim’s algorithm and starting at vertex A we get the following minimal spanning tree:
Question 10.13: Notice that in step 3.1 of Prim’s algorithm we use the rst edge in the edge set with minimal
weight incident to exactly one vertex in T . This means the order in which the edges are given in ε matter. Using
Prim’s algorithm and starting at vertex A we get the following minimal spanning tree:
Question 10.14: The minimal spanning tree given by Prim’s algorithm has vertex set
andedgeset
Question 10.15: Notice that in step 3.2 of Dijkstra’s algorithm we use the rst edge in the edge set for which the
required condition is satis ed. This means the order in which the edges are given in ε matter. Using Dijkstra’s
algorithm we get the following minimal spanning tree:
Question 10.16: Using Dijkstra’s algorithm we get the following minimal spanning tree. Notice how different
this spanning tree is from the spanning tree given in question 10.12.
Index
base, 19
binary numbers, 19
binary relation, 76
binary tree, 199
Boolean algebra, 90
bridge, 175
cardinality, 68
Cartesian product, 70,, 114
child (of vertex), 199
digital circuits, 174
codomain, 111
coef cients, 19
combination, 141
complement, 67
complement (Boolean algebra), 90
complete graph, 171
components of graph, 169
composite function, 122
composition (functions), 122
compound statements, 44
conditional controls, 2
connected graph, 169
connectives, 44
contradiction, 55
contrapositive, 57
control structures, 2
counting numbers, 62
cycle, 197
decimal numbers, 19
decrement, 7
degree of vertex, 173
DeMorgan's laws, 56
difference (of sets), 68
digits, 19
directed graph, 171
disconnected graph, 169
disjoint (sets), 67
div, 30
domain, 111
dominant operation, 151
dual expression (Boolean algebra), 90
duality principle (Boolean algebra), 91
edge, 53
edge set, 170
element (of set), 61
empty set, 65
enumerated form (of set), 62
equivalence relation, 81
Euler circuit, 176
Euler path, 176
Eulerian graph, 176
evaluating functions, 118
exclusive-or, 47
expanded form, 19
expression tree, 52
gate, 96
graph, 169
greedy algorithm, 202
hexadecimal numbers, 21
laws of logic, 58
laws of set theory, 74
leaf, 198,, 199
logical equivalence, 56
logical expressions, 44
loop, 170
loop controls, 2
matrix, 183
minimal distance spanning tree, 204
minimal spanning tree, 202
mod, 30
multiplication (Boolean algebra), 90
multiplication principle, 135
nand gate, 97
natural numbers, 62
nest, 3
vertex, 53,, 169
nor gate, 98
not (connective), 44
not gate, 96
null graph, 169
null set, 65
octal numbers, 21
one variable functions, 118
one-to-one, 117
onto, 117
or (connective), 44
or gate, 97
order of graph, 169
ordered n-tuple, 70
paradox, 44
parallel edges, 170
parent (of vertex), 199
partial ordering, 81
partition, 81,, 82
path, 174
path length, 174
permutation, 138
power set, 68
predicate, 62
predicate form (of set), 62
Prim's algorithm, 202
product-of-sum (Boolean algebra), 104
proper subset, 64
proposition, 43
pseudocode, 1
quotient, 30
range, 114
rational numbers, 62
reachability matrix, 188
reachable, 188
real numbers, 62
real-valued functions, 118
relation, 76
remainder, 30
repeat-until, 2
return, 10
root, 199
rooted tree, 199
tautology, 55
time-complexity function, 150
trace, 5
tree, 198
truth table, 46,, 52
truth-value, 43
union, 67
universal set, 66
Venn diagrams, 71
vertex, 169
vertex set, 170
vertical line test, 119
xnor gate, 98
xor gate, 98