Discrete Mathematics For Computer Science
Discrete Mathematics For Computer Science
Computer Science
Discrete Mathematics for
Computer Science
An Example-Based Introduction
The right of Jon Pierre Fortney to be identified as author of this work has been asserted by him in accor-
dance 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 publica-
tion 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, trans-
mitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter
invented, including photocopying, microfilming, 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 mpkbookspermissions@tandf.co.uk
Trademark notice: Product or corporate names may be trademarks or registered trademarks and are used
only for identification and explanation without intent to infringe.
Preface xi
Chapter 3 Logic 43
vii
viii Contents
Index 255
Preface
One of the major challenges of teaching mathematics at the community college or small
liberal arts college level is finding books that are genuinely appropriate for the student
population. Writing this book for the discrete mathematics for computer science course
at Zayed University happened because I was completely unable to find any textbook on
the market that both (1) covered the material I needed to cover and (2) was written at a
level appropriate for my students. Often textbooks cover a huge range of material, usually
far more material than even good students can fully assimilate and understand in a single
semester. On some level this is not surprising; textbooks are written by experts in the topic
who deeply understand the nuances of the field and want to make everything as precise
and “clear” as possible, while still illustrating the subtleties and complexities of the topic.
Unfortunately, the meaning of the word “clear” depends on the individual. Books that are
extremely precise and that delve into subtleties or nuance are completely inappropriate for
many college level students. I believe students benefit most with a clear, clean, uncluttered,
down-to-earth exposition of the material.
This book is intended for a first or second year discrete mathematics course for computer
science majors. In it I cover many of the most important mathematical topics that future
computer science majors need to be aware of. The first chapter introduces algorithms. As an
understanding of algorithm basics is essential for computer science majors, I have integrated
simple algorithms throughout the book. Chapter two covers number representations and
converting between decimal, binary, octal, and hexadecimal numbers. Chapter three on logic
covers propositions, connectives, truth tables, and the laws of logic. Chapter four covers all
the basic definitions of set theory, Venn diagrams, and the laws of set theory. Chapter five
on Boolean algebra introduces the laws of Boolean Algebra and the application of Boolean
algebra to digital circuits. This chapter also ties together logic, set theory, and Boolean
algebra. Chapter six introduces functions, and in particular, introduces the set-theoretic
definition of a function. Chapter seven on counting and combinatorics covers the addition
and multiplication principles, permutations and combinations. Chapter eight uses what
was learned in chapters one, six, and seven to introduce algorithmic complexity and big-O
notation. Chapter nine covers basic graph theory, Euler and semi-Euler graphs, and matrix
representations for graphs. Chapter ten introduces trees and then covers minimal spanning
trees and minimal distance paths. A variety of the covered topics are utilized in appendix A
to design a circuit to add two eight-digit binary numbers. Appendix B contains the answers
to all the end-of-chapter problems in the book.
This book has several features that make it essentially unique among discrete mathe-
matics books, particularly discrete mathematics books aimed at computer science majors.
It is written with students at community colleges or small liberal arts colleges in mind. The
language in this book is very straightforward. Sentences are not complex and, as much as
possible, the vocabulary and explanations are kept simple. Whenever definitions or explana-
tions are made, they are quickly followed by examples to aid students in their understand-
ing. Words being defined are always given in bold face. Paragraphs are kept short and the
amount of prose a student is required to read between examples is kept to a minimum. All
of this not only strongly reinforces the definition or explanation, but also aids students to
xi
xii Preface
fully understand the written content. Indeed, this textbook would be ideal for international
students and non-native English speakers. Furthermore, students without much mathemat-
ical maturity or abstract thinking ability need numerous examples to help them bridge the
gap between concrete and abstract thought. They also need a significant amount of practice
to help them both learn and internalize the material. I have attempted to provide both
numerous examples and practice problems for students to do just this. There are over 200
worked examples scattered throughout the book, boxed for easy reference. There are also
over 200 end-of-chapter problems, many of which include multiple parts, which provide stu-
dents ample practice opportunities. The answers to these problems are provided in appendix
B, allowing students to check their work.
Another strategy I have used is to relate the mathematical topics back to computer
science as much as is reasonably possible. At the level the textbook is written, many of
the examples drawn from computer science are necessarily quite superficial and simple,
but they still provide students interested in computer science motivation to understand the
mathematics. Making the mathematics relevant to their future classes and lives is essential
in encouraging students to learn the material. Explaining the importance and relevance
of algorithms in Chapter one and then integrating algorithms throughout the rest of the
material both gives students the opportunity to practice and better understand algorithms,
and gives them a computer science-based approach to interacting with and learning other
mathematical topics. There are about 40 algorithms integrated into the text, highlighted
with a light gray background for easy reference. These algorithms form an integral part of
the text and help the students learn how to read and understand pseudocode. While logic,
set theory, and Boolean algebra all have their relevance to computer science, they are also
all deeply related. In the chapter on Boolean algebra the relationship between these topics is
explored. Similarly, the connection between functions, algorithms, and counting principles
is considered in the chapter on algorithmic complexity. The chapters on graph theory and
trees have numerous applications in computer science, some of which are considered, along
with the algorithms that implement them. And finally, appendix A explores the relation
between binary numbers, Boolean algebra, and circuit design.
As written, I believe the entire book can be easily covered in a single semester. If used
in a school that follows the quarter system, if necessary, Chapters seven and ten could be
omitted without compromising the mathematical material too much. For example, when
teaching in the summer I generally only cover the first two sections of Chapter seven and
the first section of Chapter ten.
Finally, I would like to express my appreciation to both Ron Noval and Rene Hinojosa
for their ongoing support throughout the writing of this book. I would also like to thank
my many past students who offered invaluable comments on the various drafts of this book
and who pointed out innumerable typos.
Introduction to Algorithms
Algorithms play an extremely important role in both computer science and mathematics.
Algorithms are detailed instructions on how to carry out some specific 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 first 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.
Here the phrase well-defined means the steps are very clear. There is no confusion or un-
certainty 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 first 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 look-
ing at a very simple algorithm written in pseudocode. Here is an algorithm to calculate the
volume of a cube.
1
2 Discrete Mathematics for Computer Science
• 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.
We look at each of these control structures in turn.
If-then
Consider the following simple algorithm that contains an if-then conditional control.
Notice that each line, or step, is numbered.
1 In computer science when one sees an equal sign then that usually means that one is comparing two
objects to see if they are the same. This is addressed more in Chapter 8. An equal sign could also mean
that one is defining two object to be the same.
Introduction to Algorithms 3
If-then-else
Now consider the following algorithm. It contains an if-then-else conditional control.
Algorithm: Finds the order of the factor two for the integer n.
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 x1 = 5
was assigned to 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 xi < min then” so we 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 x2 = 4 is assigned to the variable min.
After that we return to the next step in the for-do 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 first 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.
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 decrementing i 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
find yourself skipping steps or only writing down enough detail to understand what is
happening. This is fine. 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 find 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 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
find 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.
Introduction to Algorithms 9
Example 1.3
Trace the above algorithm for n = 4. In other words, use the algorithm to find 4!.
Example 1.4
Use the above algorithm to search the string 758 to see if it contains a 5.
This algorithm works fine, 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 algo-
rithm 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 xi = s for some value
of i then “String contains s” is output and the algorithm ends right there.
Example 1.5
Trace the above algorithm for the string 497316 and search for the integer 3.
2 This is actually a simplification, the return statement exits the function in which it occurs, either with
Example 1.6
Example 1.7
Use the above algorithm to see if the strings 67 and 89 have any integers in common.
1. Input string x1 , . . . , xn .
2. For i = 1 to n − 1 do
2.1 For j = i + 1 to n do
2.1.1 If xi = xj then
2.1.1.1 Return “There is duplicate integer in the string.”
3. Return “There are no duplicate integers in the string.”
Example 1.8
Use the above algorithm to check if the string 98767 contains any duplicate integers.
Example 1.9
Use the above algorithm to determine if the string 68c5 consists entirely of integers of
if a non-integer is contained in the string.
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?
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.
(a) price = 74.36, paid = 82.50 (b) price = 75.00, paid = 55.50 (c) price = 48.36, paid = 54.92
14 Discrete Mathematics for Computer Science
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
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 string x1 , . . . , xn .
2. For i = 1 to n do
2.1 For j = i + 1 to n do
2.1.1 If xi = xj then
2.1.1.1 Return “There is a duplicate integer in the string.”
3. Return “There are no duplicate integers in the string.”
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.
1. Input string x1 , x2 , . . . , xn .
2. i ←− 1
3. noninteger detected ←− false
4. Repeat
4.1. If xi is not an integer then
4.1.1. noninteger detected ←− true
4.2. i ←− i + 1
until noninteger detected = 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.”
Number Representations
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.
8 = 8 × 100 ,
72 = 7 × 101 + 2 × 100 ,
401 = 4 × 102 + 0 × 101 + 1 × 100 ,
8925 = 8 × 103 + 9 × 102 + 2 × 101 + 5 × 100 .
It is important to remember that 100 = 1 and to remember the order of operations. The
exponents are done first, 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 coefficients of the powers of ten, that is, the
numbers that are in front of the powers of ten, range from 0 to 9. These coefficients 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 coefficients 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 coefficients, or digits. Numbers that only have two digits are binary
1 This is actually a bit of a simplification, there is either a high level of current or a very low level of
residual current.
19
20 Discrete Mathematics for Computer Science
numbers and have a base of 2. Binary numbers can be written in expanded form as follows
1 = 1 × 20 ,
10 = 1 × 21 + 0 × 20 ,
101 = 1 × 22 + 0 × 21 + 1 × 20 ,
1011 = 1 × 23 + 0 × 22 + 1 × 21 + 1 × 20 .
Again, remember that 20 = 1 and remember the order of operations. We always need to
know what base a 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. Here
1102 is a base-two, or binary, number,
40110 is a base-ten, or decimal, number,
10112 is a base-two, or binary, number,
720310 is a base-ten, or decimal, 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, 12 = 110 . Therefore, in the below
example there is no need to specify which base the digits are in since they are the same in
both bases.
Example 2.1
22 + 1 × |{z}
1102 = 1 × |{z} 21 + 0 × |{z}
20
=410 =210 =110
10112 = 1 × 23 + 0 × 22 + 1 × 21 + 1 × 20
= 1 × 810 + 0 × 410 + 1 × 210 + 1 × 110
= 810 + 010 + 210 + 110
= 1110
Very quickly binary numbers become difficult for us to understand. For example, consider
the number
1000111110101010011100011101
and the number
1000111110111010011100011101.
Just by looking can you easily tell which of these two numbers was larger? Even though
computers use binary numbers it is difficult for us to use them. So there are two other basis
that are very important in computer science, base-eight and base-sixteen.
Number Representations 21
58 = 5 × 80 ,
708 = 7 × 81 + 0 × 80 ,
3728 = 3 × 82 + 7 × 81 + 2 × 80 ,
65048 = 6 × 83 + 5 × 82 + 0 × 81 + 4 × 20 .
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, 18 = 110 and so
on up to 78 = 710 . Therefore, in the below 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
708 = 7 × 81 + 0 × 80
= 7 × 810 + 0 × 110
= 5610 + 010
= 5610
65048 = 6 × 83 + 5 × 82 + 0 × 81 + 4 × 80
= 6 × 51210 + 5 × 6410 + 0 × 810 + 4 × 110
= 307210 + 32010 + 010 + 410
= 339610
For base-sixteen numbers we use 16 as the base. Base-sixteen numbers are also called
hexadecimal numbers. Like before, we want the coefficients 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. Hexadeci-
mal numbers can be written in expanded form
D16 = D × 160 ,
E216 = E × 161 + 2 × 160 ,
3B716 = 3 × 162 + B × 161 + 7 × 160 ,
A05C16 = A × 163 + 0 × 162 + 5 × 161 + C × 160 .
22 Discrete Mathematics for Computer Science
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 = 010 and so on up to 916 = 910 . Therefore, as
before we make no effort to specify which base the digits 0 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
B × 161 + 7 × 160
3B716 = 3 × 162 + |{z}
=1110
To convert fractional binary numbers to fractional decimal numbers we need to know the
negative powers of two,
1 1
2−1 = = = 0.510 ,
21 2
1 1
2−2 = 2 = = 0.2510 ,
2 4
1 1
2−3 = 3 = = 0.12510 .
2 8
Example 2.4
In order to convert fractional octal numbers to fractional decimal numbers we need to know
the negative powers of eight,
1 1
8−1 = = = 0.12510 ,
81 8
1 1
8−2 = 2 = = 0.01562510 ,
8 64
1 1
8−3 = 3 = = 0.00195312510 .
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
3.78 = 3 × 80 + 7 × 8−1
= 3 × 110 + 7 × 0.12510
= 310 + 0.87510
= 3.87510
Fractional hexadecimal numbers behave similarly, they just use the digits 0 through F .
Fractional hexadecimal numbers can be written in expanded form,
Example 2.6
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.
348 = |{z}
3 |{z}
4
011 100
= 0111002
= 111002
170438 = |{z}
1 |{z}
7 |{z}
0 |{z}
4 |{z}
3
001 111 000 100 011
= 0011110001000112
= 11110001000112
• Convert the octal number 35.2048 to a binary number. Notice we can also drop
the final zeros after the point without changing the number. We can of course
not drop final zeros before the point. That would change the value of the number.
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 filling
in zeros at the start of the binary number and the third example illustrates filling in
zeros at both the beginning and the end of the number.
• Convert 1011100010102 to an octal number.
1011100010102 = |{z}
101 |{z}
110 |{z}
001 |{z}
010
5 6 1 2
= 56128
Number Representations 27
10011011010012 = |{z}
1 |{z}001 |{z}
101 |{z}
101 |{z}
001
= 001}
| {z
1 5 5 1
= 115518
• Convert 10100111.10011012 to an octal number.
10100111.10011012 = |{z}
10 |{z} 111 . |{z}
100 |{z} 100 |{z} 1
110 |{z}
= 010}
| {z
4 7 4 6 = 100}
| {z
2 4
= 247.4648
Example 2.9
10001111101010100111000111012 = |{z}
1 |{z}000 |{z} 101 |{z}
110 |{z}
111 |{z} 100 |{z}
011 |{z}
010 |{z} 101
011 |{z}
= 001}
| {z
0 7 6 5 2 3 4 3 5
= 10765234358
10001111101110100111000111012 = |{z}
1 |{z}000 |{z} 111 |{z}
110 |{z}
111 |{z} 100 |{z}
011 |{z}
010 |{z} 101
011 |{z}
= 001}
| {z
0 7 6 7 2 3 4 3 5
= 10767234358
10765234358 ,
10767234358 .
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.
28 Discrete Mathematics for Computer Science
Example 2.10
Example 2.11
= 29E16
= 34B.3416
Example 2.12
This is a much easier job than comparing the two original binary numbers and even a
little easier than comparing the octal numbers.
30 Discrete Mathematics for Computer Science
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 first 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 first convert the octal
number to a binary number and then convert the binary number to a hexadecimal
number.
37
2 75
60
15
14
1
In this example the quotient is 37 and the remainder is 1. Thus we have
75 div 2 = 37,
75 mod 2 = 1.
It should be obvious to you that when finding 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.
Number Representations 31
Example 2.13
• n=8
4
2 8 gives 8 div 2 = 4
8
8 mod 2 = 0
0
• n=9
4
2 9 gives 9 div 2 = 4
8
9 mod 2 = 1
1
Example 2.14
• n = 37
18
2 37
20 gives 37 div 2 = 18
17
16 37 mod 2 = 1
1
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
19
2 38
20 gives 38 div 2 = 19
18
18 38 mod 2 = 0
0
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.
32 Discrete Mathematics for Computer Science
Example 2.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
1110 = 10112 .
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 mod2 = 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
3710 = 1001012 .
Number Representations 33
So far we have considered whole numbers. But we can convert fractional decimal num-
bers to binary numbers as well. Here is the algorithm to do this:
Example 2.17
b93.91c = 93
frac(93.91) = 0.91
Example 2.18
b0.75c = 0
frac(0.75) = 0.75
Example 2.19
b16c = 16
frac(16) = 0
34 Discrete Mathematics for Computer Science
Example 2.20
Convert 0.4210 to a binary number. Find five digits after the point. That is, n = 0.4210
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 b0.84c = 0
3.4 0.84 ← frac(0.84) 1 -
3.1 0.84 2←1+1 -
3.2 1.68 ← 2(0.84) 2 -
3.3 1.68 2 b1.68c = 1
3.4 0.68 ← frac(1.68) 2 -
3.1 0.68 3←2+1 -
3.2 1.36 ← 2(0.68) 3 -
3.3 1.36 3 b1.36c = 1
3.4 0.36 ← frac(1.36) 3 -
3.1 0.36 4←3+1 -
3.2 0.72 ← 2(0.36) 4 -
3.3 0.72 4 b0.72c = 0
3.4 0.72 ← frac(0.72) 4 -
3.1 0.72 5←4+1 -
3.2 1.44 ← 2(0.72) 5 -
3.3 1.44 5 b1.44c = 1
3.4 0.44 ← frac(1.44) 5 -
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 fifth digit in step
3.3. Now we read the outputs in order, placing them after the point, to give us
0.4210 ≈ 0.011012 .
Of course we could continue finding 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 ,
So, to get a more accurate approximation for 0.4210 , we would need d to be larger.
Number Representations 35
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 first 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 final answer.2
Example 2.21
Convert 37.4210 to a binary number finding five digits after the point.
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 24210 = 3628 .
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.8110
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
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.8110 ≈ 0.6365608 .
Number Representations 37
Example 2.24
Convert 242.8110 to an octal number using six digits after the point.
We have already used the first algorithm to find 24210 = 3628 and the second
algorithm to find 0.8110 ≈ 0.6365608 . Joining these two numbers we now have
242.8110 ≈ 362.6365608 .
Example 2.25
Step n Output
1. 3626 -
2.1 3636 1010 = A16
2.2 226 -
2.1 226 210 = 216
2.2 14 -
2.1 14 1410 = E16
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 2.26
Convert 0.6310 to a hexadecimal number. Find four digits after the point. That is,
n = 0.6310 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 1010 = A16
3.4 0.08 1 -
3.1 0.08 2 -
3.2 1.28 2 -
3.3 1.28 2 110 = 116
3.4 0.28 2 -
3.1 0.28 3 -
3.2 4.48 3 -
3.3 4.48 3 410 = 416
3.4 0.48 3 -
3.1 0.48 4 -
3.2 7.68 4 -
3.3 7.68 4 710 = 716
3.4 0.68 4 -
Now we see that i = 4 and can stop. Reading the outputs in order we have 0.6310 ≈
0.A14716 .
Example 2.27
Convert 3626.6310 to a hexadecimal number finding four digits after the point.
We have already used the first algorithm to find 362610 = E2A16 and the sec-
ond algorithm to find 0.6310 ≈ 0.A14716 . Joining these two numbers, we now have
3626.6310 ≈ E2A.A14716 .
2.5 PROBLEMS
Question 2.20 Convert the following decimal numbers to binary numbers. Stop after you
have obtained six digits.
(a) 0.5 (d) 0.32 (g) 0.239
(b) 0.4 (e) 0.81 (h) 0.552
(c) 0.8 (f) 0.77 (i) 0.798
Number Representations 41
Question 2.21 Convert the following decimal numbers to octal numbers. Stop after you
have obtained six digits.
(a) 0.9 (d) 0.83 (g) 0.482
(b) 0.4 (e) 0.25 (h) 0.667
(c) 0.6 (f) 0.44 (i) 0.315
Question 2.22 Convert the following decimal numbers to hexadecimal numbers. Stop after
you have obtained six digits.
(a) 0.1 (d) 0.11 (g) 0.429
(b) 0.3 (e) 0.83 (h) 0.638
(c) 0.9 (f) 0.47 (i) 0.314
CHAPTER 3
Logic
Logic plays a fundamental role in many areas of computer science, including software en-
gineering and design, expert systems, and artificial 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 first 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.
43
44 Discrete Mathematics for Computer Science
Example 3.2
Example 3.3
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 five words or phrases called connectives.
There are five 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 five 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 five 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.
1 These kinds of statements do play a role in advanced computer science, such as with artificial intelligence,
fuzzy logic, probabilistic logic, and so on. But at our level we will not worry about them.
Logic 45
Example 3.4
Using the propositions p = “Today is Monday.” and q = “It is raining.” write the
following logical expressions in words.
• ¬p = Today is not Monday.
• ¬q = It is not raining.
• p ∧ q = Today is Monday and 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 first. For example, if we have p ∧ ¬q this really means p ∧ (¬q), we first find ¬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 first 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.
• My program runs and it contains no mistakes, or my program contains mistakes.
• Either my program runs and it contains no mistakes, or my program contains
mistakes.
Both of these sentences are correct. We first do what is inside the parenthesis, (p ∧ ¬q)
and then connect it to q using ∨. Notice in the first 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.
46 Discrete Mathematics for Computer Science
Example 3.6
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:
• You may have either tea or coffee with breakfast. (exclusive-or)
• The movie has a discount for students or children. (inclusive-or)
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
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 p xor q is true when only one of the propositions p or q is true. That
means that if both p and q are true, then p xor q is false. Here is the truth table for
the exclusive-or.
p q p xor q
T T F
T F T
F T T
F F F
In this case the expression
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
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 first 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 first 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
(T ∧ ¬F ) ∨ F substitute truth-values
≡ (T ∧ T ) ∨ F use truth-table of ¬
≡T ∨F use truth-table of ∧
≡T use truth-table of ∨
The symbol ≡ is read “is equivalent to.” This is different than the if-and-only-if con-
nective 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 find 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 find the truth table of a compound statement. This
means you are to find the truth-value of the compound statement for every possible propo-
sition truth-value. You need to make and fill-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 22 = 4 rows since it has two propo-
sitions (p and q); the truth table for (¬p ∧ q) ∨ r would need 23 = 8 rows since it has three
propositions (p, q, and r); and the truth table for ¬(p ∨ q) → (¬r ∧ s) would need 24 = 16
rows since it has four propositions (p, q, r, and s). To figure out the columns a truth table
needs it may be helpful to make a expression tree. (Trees will be studied later in chapter
10.)
Logic 53
Example 3.15
Make an expression tree for ¬(p ∧ q) and use that to construct a truth table.
¬
∧
p q
The construction of this expression tree should be clear, in order to find ¬(p ∧ q) we
must first find (p ∧ q) and then take the negation of that. We start at the bottom of
the tree where the variables p and q each appear in a circle called a vertex.a These
variables are then combined with ∧ which is in the vertex above. The “combining” is
shown by connecting the variables p and q to ∧ by lines called edges. The result is
p ∧ q. The result of this “combining” is then negated, which happens at the top vertex.
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 first two columns of the truth table we have filled in every possible combination
of truth values for p and q. The rest of the truth table needs to be filled in.
a The plural of vertex is either vertices or vertexes.
Example 3.16
Make an expression tree for (p ∧ ¬q) ∨ q and use that to construct a truth table.
∨
∧ q
p ¬
In the expression tree the variables are the vertices at the bottom. The first thing we
do is find ¬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
54 Discrete Mathematics for Computer Science
Example 3.17
Make an expression tree for ¬(¬p ∧ q) ↔ (¬q ∨ r) and use that to construct a truth
table.
↔
¬ ∨
∧ ¬ r
¬ q q
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
T F T
T F F
F T T
F T F
F F T
F F F
Example 3.18
First we fill in the third column using the second column and the truth table for ¬.
When q is true then ¬q is false, and when q is false then ¬q is true.
p q ¬q p ∧ ¬q (p ∧ ¬q) ∨ q
T T F
T F T
F T F
F F T
Logic 55
Next we fill in the fourth column using the first 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 fill in the fifth 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 first construct the expression tree.
→
¬ ∨
∧ ¬ ¬
p q p q
We use the expression tree to construct the truth table which we then fill 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.
56 Discrete Mathematics for Computer Science
Example 3.20
Show that (p ∧ q) ∧ ¬p is a contradiction. We first make the expression tree. If you feel
comfortable making the truth table without making the expression tree then there is
no need to make the tree.
∧
∧ ¬
p q p
We then use the expression tree to construct the truth table which we then fill out.
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
We can see that the expression (p ∧ q) ∧ ¬p is false regardless of the truth-value of the
propositions. Thus (p ∧ q) ∧ ¬p is called a contradiction. A contradiction is never true.
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) p q ¬p ¬q ¬p ∨ ¬q
T T T F T T F F F
T F F T and T F F T T
F T F T F T T F T
F F 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) p q ¬p ¬q ¬p ∧ ¬q
T T T F T T F F F
T F T F and T F F T F
F T T F F T T F F
F F F T 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
p → q ≡ ¬q → ¬p.
This may surprise you, but this equivalence is actually one of the most important re-
lationships 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 com-
puter scientists prove things. Many times you want to show a statement p → q is true but
showing this statement is true is difficult 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 definitions 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
Equivalence law p ↔ q ≡ (p → q) ∧ (q → p) ≡ (¬p ∨ q) ∧ (¬q ∨ p)
58 Discrete Mathematics for Computer Science
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.
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 ∨ (¬q ∧ q) Distributive law
≡ p ∨ (q ∧ ¬q) Commutative law
≡p∨F Inverse law
≡p Identity law
Example 3.25
Simplify the expression ¬ p → ¬(p ∧ q) using the laws of logic. Justify each step.
¬ p → ¬(p ∧ q)
≡ ¬ ¬p ∨ ¬(p ∧ q) Implication law
≡ ¬¬p ∧ ¬¬(p ∧ q) DeMorgan’s law
≡ p ∧ (p ∧ q) Double negation law
≡ (p ∧ p) ∧ q Associative law
≡p∧q Idempotent law
Logic 59
Example 3.26
Simplify the expression ¬ p ∨ (q ∧ ¬p) using the laws of logic. Justify each step.
¬ p ∨ (q ∧ ¬p)
≡ ¬p ∧ ¬(q ∧ ¬p) DeMorgan’s law
≡ ¬p ∧ (¬q ∨ ¬¬p) DeMorgan’s law
≡ ¬p ∧ (¬q ∨ p) Double negation law
≡ ¬p ∧ (p ∨ ¬q) Commutative law
≡ (¬p ∧ p) ∨ (¬p ∧ ¬q) Distributive law
≡ F ∨ (¬p ∧ ¬q) Inverse law
≡ ¬p ∧ ¬q Identity law
3.6 PROBLEMS
Question 3.2 Let p = “Jon plays baseball” and q = “Ron plays football.” Write the follow-
ing logical statements in words.
(a) p ∨ q (c) ¬p ∧ q (e) ¬q ∨ ¬p
(b) q ∧ p (d) p ∧ ¬q (f) ¬(q ∨ p)
Question 3.3 Suppose p is true and q is false. Find the truth value of the following com-
pound statements.
(a) p ∧ q (d) ¬p ∨ q (g) p → q (j) p ↔ q
(b) q ∧ p (e) p ∧ ¬q (h) ¬q → p (k) ¬q ↔ p
(c) ¬q ∨ p (f) ¬q ∨ ¬p (i) ¬q → ¬p (l) ¬p ↔ ¬q
Question 3.4 Construct the truth table for the following compound statements.
(a) p ∧ q (d) ¬p ∨ q (g) p → q (j) p ↔ q
(b) q ∧ p (e) p ∧ ¬q (h) ¬q → p (k) ¬q ↔ p
(c) ¬q ∨ p (f) ¬q ∨ ¬p (i) ¬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 (d) ¬(p ∧ ¬q) ∨ r (g) (p ∨ q) ↔ [p ∨ (q ∧ r)]
(b) (q ∧ p) → ¬p (e) ¬[(¬p ∧ q) ∨ r] (h) (q ∧ ¬r) ↔ ¬(p ∨ r)
(c) ¬p → (¬p ∨ q) (f) (p ∧ ¬q) ∧ (r ∨ q) (i) (p ↔ q) ∧ (r ↔ q)
Question 3.8 (Easy) Simplify the following Logical expressions with the laws of logic.
(a) p ∨ p (e) p ∨ (¬p ∧ q) (i) p ∧ (p ∨ q ∨ r)
(b) p ∨ ¬p (f) p ∧ (¬p ∨ q) (j) (p ∧ q) ∨ (p ∧ ¬q)
(c) ¬(p ∨ ¬p) (g) p ∧ p (k) ¬(¬p ∨ ¬p)
(d) p ∨ (p ∧ q) (h) p ∧ (p ∨ q) (l) (p ∧ q) ∨ (¬p ∧ q)
Question 3.9 (Moderate) Simplify the following Logical expressions with the laws of logic.
(a) (¬p ∨ ¬q) ∧ (¬p ∨ q) (e) r ∨ [r ∨ (r ∧ p)]
(b) q ∨ (q ∧ ¬q) (f) p ∧ [p ∨ (p ∧ q)]
(c) ¬p ∨ (q ∧ ¬p) (g) r ∨ (r ∧ ¬p ∧ q ∧ s)
(d) (p ∨ ¬q) ∧ (p ∨ q) (h) ¬r ∧ ¬(r ∧ p ∧ q ∧ s)
Question 3.10 (Difficult) Simplify the following Logical expressions with the laws of logic.
(a) q ∧ [p ∨ (¬p ∧ q)] (g) [p ∨ (q ∧ r)] ∧ (¬p ∨ r)
(b) ¬[(p ∧ ¬q) ∨ ¬p] (h) (p ∧ ¬r) ∨ (¬p ∧ q) ∨ ¬(p ∧ r)
(c) (¬p ∧ q) ∨ [p ∧ (p ∨ q)] (i) (p ∧ r) ∨ (¬p ∧ q) ∨ (r ∧ q)
(d) ¬[p ∨ (p ∧ q)] ∧ q (j) ¬p ∨ ¬q ∨ [p ∧ q ∧ ¬r]
(e) (p ∧ ¬r) ∨ (¬p ∧ q) ∨ ¬(q ∧ r) (k) (p ∨ r) ∧ (¬p ∨ q) ∧ (r ∨ q)
(f) ¬(p ∧ q) ∨ (q ∧ r) (l) ¬(p ∧ q) ∧ (¬p ∨ q) ∧ (¬q ∨ q)
Set Theory
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 definitions and ideas that
you will need to know later.
Example 4.1
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.”
61
62 Discrete Mathematics for Computer Science
Example 4.2
Elements of sets.
• apple ∈ apple, orange, banana but pear ∈
/ apple, orange, banana .
• 4 ∈ {1, 2, 3, 4, 5} but 6 ∈
/ {1, 2, 3, 4, 5}.
• 13 ∈ {1, 2, 3, . . . , 50} but 157 ∈
/ {1, 2, 3, . . . , 50}.
Now we will introduce some of the most important sets in mathematics and computer
science.
N = the set of natural numbers
= the set of positive integers
= {1, 2, 3, 4, . . .},
Z = the set of integer numbers
= {. . . , −4, −3, −2, −1, 0, 1, 2, 3, 4, . . .},
Q = the set of rational numbers
n m o
= x x= where m, n ∈ Z and n 6= 0 ,
n
R = the set of real numbers.
Sometimes the natural numbers1 are also called the whole numbers or counting
numbers. Also, the integer numbers are often simply called the integers, the rational
numbers are often simply called the rationals, and the real numbers are often simply
called the reals. It is difficult to give a precise definition 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 defined the rational numbers Q. Sets can be written in one of two
different ways. They can be written in an enumerated form 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 define the set. This is how we defined Q above. A predicate 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) ,
1 Often the set of natural numbers also includes zero. From a computer science perspective this makes
The curly-brackets tells us we have a set. The x tells us the name of the variable, and
if we have written x ∈ S then that tells us what set the values of x come from. The | is
read as “such that.” Some books use a : instead of a | for “such that.” The P (x) tells us
the condition that must be satisfied for a value of x to be in the set. This means that P (x)
must be true for a value of x in order for x to be in the set. First we look at examples of
predicates before looking at examples of sets written in predicate form.
Example 4.3
5 ≤ x ≤ 10
Example 4.4
Example 4.5
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
• 13 , 12 , 23 = 13 , 13 , 13 , 12 , 23 , 23 , 32
• 12 = 12 , 24 , 36 , 48 , 10
5 6
, 12 ,...
1 1 2 3 4 5 6
• 3 = 3 , 6 , 9 , 12 , 15 , 18 , . . . There are many different ways to write any frac-
tion.
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,
n o
S = a , b , c , {b, c} , {a, b, c} , d , {a, d, h} , h , i , {h, i} .
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
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 6= 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,
n o
S = a , b , c , {b, c} , {a, b, c} , d , {a, d, h} , h , i , {h, i} .
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,
n o
S = a , b , c , {b, c} , {a, b, c} , d , {a, d, h} , h , i , {h, i} .
{b, c} ⊂ S.
66 Discrete Mathematics for Computer Science
Example 4.8
Using the same set as before we give some examples of both elements and subsets to
help you understand the difference;
n o
S = a , b , c , {b, c} , {a, b, c} , d , {a, d, h} , h , i , {h, i} .
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;
{b, c} ⊂ S, {a, b, c} ⊂ S, {a, d, h} ⊂ S, {h, i} ⊂ S.
• The following are some examples of subsets of S that contain two elements;
a, b ⊂ S, c, d ⊂ S, a, {b, c} ⊂ S, {b, c}, {a, d, h} ⊂ S.
• 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.
2 Much later in your computer science major you may see problems where there is no well-defined universal
set.
Set Theory 67
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.
• If A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7} then A ∩ B = {4, 5}.
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.
• 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 Ā or as A0 . In order to find the complement of
a set you need to know what the universal set U is.
Example 4.11
Complements of sets.
• If U = {1, 2, 3, 4, 5, 6} and A = {2, 4, 6} then Ac = {1, 3, 5}.
• If U = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} and A = {1, 2, 3, 9, 10} then Ac = {4, 5, 6, 7, 8}.
Example 4.12
Differences of sets.
The cardinality of a finite 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 infinite number of elements
then we say the cardinality of the set is infinite. Since the empty set has no elements then
the cardinality of the empty set is zero.
Example 4.13
Cardinality of sets.
• If A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} then |A| = 10.
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 find 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
n o
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.
Set Theory 69
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 21 = 2 subsets and P(A) = ∅, {a} .
• 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
{z }·| 2
{z }
Either a is in Either b is in
subset or not. subset or not.
| {z }
four possibilities
2
a total of 2 · 2 = 4 possibilities. That means there are 2 = 4 subsets,
which give
P(A) = { }, {a}, {b}, {a, b} .
• A = {a, b, c}. The set A has three 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
{z }·| 2
{z }·| 2
{z }
Either a is in Either b is in Either c is in
subset or not. subset or not. subset or not.
| {z }
eight possibilities
By now the pattern should be clear. This leads to the following theorem.
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) = 24 = 16.
Thus A has 16 subsets. However, one of these subsets is A itself, so there are only 15
proper subsets of A.
Example 4.17
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 find the power of two that is equal to
128. Since 27 = 128 we have |A| = 7. In other words, A has seven elements.
70 Discrete Mathematics for Computer Science
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 n-tuple does matter. Thus,
(3, −2, 4) 6= (−2, 3, 4)
but
{3, −2, 4} = {−2, 3, 4}.
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 R2 the point (2, −1) is very different from the point (−1, 2). Similarly, in three-
dimensional space R3 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 defined to be
A × B = (x, y) x ∈ A and y ∈ B .
That is, A × B is the set of all ordered pairs where the first 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 defined to be
A × B × C = (x, y, z) x ∈ A and y ∈ B and z ∈ C .
That is, A × B × C is the set of all ordered three-tuples where the first 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
S1 × S2 × · · · × Sn = (x1 , x2 , . . . , xn ) x1 ∈ S1 , x2 ∈ S2 , · · · , xn ∈ Sn .
Example 4.18
In fact, this explains why the plane is often written as R2 , since using the rule
of exponents we can write R2 = R × R. See Fig. 4.1.
3 The Cartesian product is named after Cartesius, the Latinized version of the last name of the French
philosopher Renťe Descartes. Since it is named after a person often it is capitalized, but sometimes it is not.
Both conventions are considered grammatically correct.
Set Theory 71
z-axis
10
y-axis
4
(−4, 3)
3 (2, 5, 4)
5
2
(3, 1) y-axis
1
x-axis
10
−10 5
−5 −4 −3 −2 −1 1 2 3 4 5 −5
−1 x-axis
−5 5
10
−2 −10 (0, 0, 0)
−3
Origin
(−2, −3) −5
(−3, −6, −4)
−4
−10
A q B
p k d
i x j f w
r m a
s g y
c n b
e z v
u o l
t h
Figure 4.4An illustration of how Venn diagrams work. Here the universal set U consists
of the lowercase letters from the English alphabet. The uppercase letters A and B
represent the two sets shown as circles.
are not inside set A are outside the oval but still inside the “universe” box. The following
example should make this clear.
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 find A∩B, A∪B, Ac , B c , (A∪B)c , and (A∪B)c .
We find 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 .
We find the sets A∩B, A∪B, Ac , B c , (A∪B)c , and (A∪B)c by listing all the elements
in the appropriate region.
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 ,
Ac = i, u, t, o, h, l, d, q, k, f, g, z, w, y, v ,
B c = i, u, t, o, h, l, d, q, p, x, r, c, e, m, n ,
(A ∪ B)c = i, u, t, o, h, l, d, q ,
(A ∩ B)c = 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.
Set Theory 73
A e B
z A B
x p t w
i c k 5
m y s 9 12
j o
l 3
n g d 4 7
f b
a v u h 8
r q
C C
11
Figure 4.5A Venn diagram with three Figure 4.6In this Venn diagram the num-
sets, A, B, and C, and with elements ber of elements in each region is indicted,
shown. not the actual elements themselves.
Example 4.20
Using the Venn diagram in Fig. 4.6 find |A|, |B|, |C|, |A∩B|, |A∩C|, |B∩C|, |A∩B∩C|,
|A ∪ B|, |A ∪ C|, |B ∪ C|, |A ∪ B ∪ C|, |Ac |, and |(A ∪ B ∪ C)c |.
|A| = 9 + 5 + 3 + 4 = 21
|B| = 12 + 5 + 3 + 7 = 27,
|C| = 8 + 7 + 3 + 4 = 22,
|A ∩ B| = 5 + 3 = 8,
|A ∩ C| = 4 + 3 = 7,
|B ∩ C| = 7 + 3 = 10,
|A ∩ B ∩ C| = 3,
|A ∪ B| = 9 + 4 + 5 + 3 + 12 + 7 = 40,
|A ∪ C| = 9 + 5 + 4 + 3 + 8 + 7 = 36,
|B ∪ C| = 12 + 5 + 7 + 3 + 8 + 4 = 39,
|A ∪ B ∪ C| = 9 + 5 + 12 + 4 + 3 + 7 + 8 = 48,
|Ac | = 12 + 7 + 8 + 11 = 38,
|(A ∪ B ∪ C)c | = 11.
In a general situation we often use shading to indicate the elements in a set. In Fig. 4.7(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.
74 Discrete Mathematics for Computer Science
A B A B A B
A B A B A B
A B A B A B
A B A B
Example 4.21
Use Venn diagrams to argue that the double complement law is true.
A B A B
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 (Ac )c must be the same as A.
Example 4.22
On the left we show Ac . In the middle we show B c . On the right we see the union of
these two sets, Ac ∪ B c .
76 Discrete Mathematics for Computer Science
Example 4.23
Let A = {1, 3, 5, 7} and B = {2, 3, 4, 5}. First we find the Cartesian product of sets A
and B,
n
A × B = (1, 2), (1, 3), (1, 4), (1, 5), (3, 2), (3, 3), (3, 4), (3, 5),
o
(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.
R
1• •2
3• •3
5• •4
7• •5
A B
Set Theory 77
Example 4.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.
R
1• •2
3• •3
5• •4
7• •5
A 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.
1• •2
3• •3
5• •4
7• •5
A B
When working with databases we may have relations that are not related to mathemat-
ics. 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, Hunter, Rex}. The relation OwnsDog
is given by the following subset of the Cartesian product A × B,
n o
OwnsDog = (Sam, F ido), (Bob, Rex), (T im, Hunter) ⊂ A × B.
Since (Sam, F ido) ∈ OwnsDog we have “Sam OwnsDog Fido” which clearly means
that Sam owns the dog named Fido. “Bob OwnsDog Rex” means that Bob owns the
dog Rex. And similarly Tim owns the dog Hunter. The relation can be pictured as
below.
OwnsDog
Sam • • F ido
Bob • • Hunter
T im • • Rex
A B
Example 4.27
Suppose A = {John, Sally, Anne, T ed, Bill, Ed, Jane}. We have a relation called
ChildOf given by the following subset of the Cartesian product A × A,
n
ChildOf = (John, Sally), (T ed, Bill), (John, T ed),
o
(Bill, Ed), (Bill, Anne), (T ed, Jane) ⊂ A × A.
Since we have (John, 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 finite 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.
Set Theory 79
Example 4.28
2 3
Example 4.29
Functions can be defined 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.
f
1• •a
2• •b
3• •c
X Y
(domain) (codomain)
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:
1. If x ∈ X then there exists a y ∈ Y such that (x, y) ∈ f ,
2. and this y ∈ Y is unique. (That means if (x, y) and (x, z) are in f then y = z.)
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.
1. R is called reflexive if xRx for every x ∈ A.
2. R is called irreflexive if there are no elements x ∈ A such that xRx.
80 Discrete Mathematics for Computer Science
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 reflexive 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 irreflexive 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 N 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.
Set Theory 81
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 defined using the relations listed above.
1. A relation is called an equivalence relation if it is reflexive, symmetric, and transi-
tive.
2. A relation is called a partial ordering if it is reflexive, antisymmetric, and transitive.
Example 4.35
To check that R is reflexive 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 reflexive. 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 xRz. This is exactly
Example 4.36
The sets E(x) are called the equivalence classes of R 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
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
Question 4.1 Let A = 2, 4, 8, 6, a, e, g, h, b, e and B = 6, 9, 2, 8, r, v, x, z, t, s . Deter-
mine if the following statements are True or False.
(a) 4 ∈ A (g) 7 ∈ B (m) b ∈/A (s) 5 ∈/B
(b) 6 ∈ A (h) 9 ∈ B (n) 8 ∈
/A (t) s ∈/B
(c) r ∈ A (i) z ∈ B (o) g ∈
/A (u) u ∈ /B
(d) b ∈ A (j) q ∈ B (p) c ∈
/A (v) 8 ∈ /B
(e) f ∈ A (k) 8 ∈ B (q) 5 ∈
/A (w) 5 ∈ /B
(f) 1 ∈ A (l) y ∈ B (r) 3 ∈
/A (x) v ∈ /B
Question 4.2 Let A = 2, 4, 8, 6, a, e, g, h, b and B = 6, 9, 2, 8, r, v, x, z, t, s . Determine
if the following statements are True or False.
(a) {6, a, e} ⊂ A (g) {9, 8, 7} ⊂ B (m) {2, 4, 6, 8} 6⊂ A (s) {6, 8, t} 6⊂ B
(b) {2, 4, 6, 8} ⊂ A (h) {v, x, 8, 9} ⊂ B (n) {a, e, i} 6⊂ A (t) {x, y, z} 6⊂ B
(c) {g, 5, 6, j} ⊂ A (i) {v} ⊂ B (o) {a, b} ⊂6 A (u) {8, 6} 6⊂ B
(d) {b, a, c} ⊂ A (j) { } ⊂ B (p) {4, h, 2} 6⊂ A (v) { } 6⊂ B
(e) {a, 8} ⊂ A (k) {3, 6, 9} ⊂ B (q) { } 6⊂ A (w) {v, r, 8, 2} 6⊂ B
(f) { } ⊂ A (l) {z, 2} ⊂ B (r) {9} 6⊂ A (x) {2, y, 3} 6⊂ B
Set Theory 83
n o
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) {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} (d) {a, b} (g) {2, 4, 6}
(b) {5} (e) {5, 7} (h) {x, y, z}
(c) {v} (f) {5, e} (i) {2, b, c}
Question 4.5 Find all the subsets of the following sets. Which of these subsets are proper
subsets?
n o n o n o
(a) {a, b, c} (d) r, {s, t} (g) 2, {4}, 6
n o n o n o
(b) {x, y} (e) {x}, {y, z} (h) {2}, {4}, {6}
n o n o n o
(c) {1, 2, 3} (f) {2, 4}, 6 (i) u, v, {w}
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 (c) Ac (e) (A ∪ B)c (g) Ac ∪ B c (i) A − B
c c c c
(b) A ∩ B (d) B (f) (A ∩ B) (h) A ∩ B (j) B − A
Question 4.9 Write the following sets in enumerated form. We may assume x is from the
set Z.
(a) x x2 = 4 (d) x x3 = 27 (g) x x4 = 16
(b) x (x+1)(x−1) = 0 (e) x x2 = 25 (h) x (x+3)(x−2) = 0
(c) x (x−5)(x+4) = 0 (f) x (2x−8)(5x+15) = 0 (i) x x = −x
84 Discrete Mathematics for Computer Science
Question 4.10 Write the following sets in enumerated form. We may assume x is from
the set R.
(a) x x3 = −1 (f) x x3 = 1
(b) x (2x − 3)(4x + 7) = 0
(g) x x = 2x
(c) x (2x + 5)(3x + 4) = 0
2
(d) x (x − 4)(x − 3) = 0 (h) x (3x − 4)(2 − 3x) = 0
(e) x (5 − x)(7 + 3x) = 0 (i) x (x + 5)(x2 − 9) = 0
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?
Question 4.19 If X1 = {0, 1}, X2 = {0, 1}, X3 = {0, 1}, and X4 = {0, 1} find the Carte-
sian product X1 × X2 × X3 × X4 .
Set Theory 85
Question 4.20 Using the Venn diagram find the sets A, B, A∪B, A∩B, Ac , B c , (A∪B)c ,
(A ∩ B)c , Ac ∪ B, and A ∪ B c , A − B, B − A.
A B
k o
g e j b
m c
a w i
x v
t d
Question 4.21 Using the Venn diagram find the sets A, B, A∪B, A∩B, Ac , B c , (A∪B)c ,
(A ∩ B)c , Ac ∪ B, and A ∪ B c , A − B, B − A.
A z B
g c
m t w
i
d p
b v
l
j
Question 4.22 Using the Venn diagram below to find the following: A, B, C, A∩B, A∩C,
B ∩ C, A ∪ B, A ∪ C, B ∪ C, A ∩ B ∩ C, A ∪ B ∪ C, Ac , B c , C c , (A ∩ B)c , (A ∩ C)c ,
(B ∩ C)c , (A ∪ B)c , (A ∪ C)c , (B ∪ C)c , (A ∩ B ∩ C)c , (A ∪ B ∪ C)c , A − B, B − A, A − C,
C − A, B − C, and C − B.
A B
j
x c
p r h
d
z t q
g n s
f v
a k
y o
C
86 Discrete Mathematics for Computer Science
Question 4.23 Using the Venn diagram below to find the following: A, B, C, A∩B, A∩C,
B ∩ C, A ∪ B, A ∪ C, B ∪ C, A ∩ B ∩ C, A ∪ B ∪ C, Ac , B c , C c , (A ∩ B)c , (A ∩ C)c ,
(B ∩ C)c , (B ∪ C)c , (A ∪ C)c , (B ∪ C)c , (A ∩ B ∩ C)c , (A ∪ B ∪ C)c , A − B, B − A, A − C,
C − A, B − C, and C − B
A e B
z
x p t w
i c k
m y s
j o
l
n g d
f b
a v u h
r q
C
Question 4.24 Using the Venn diagram below to find the following: |A|, |B|, |C|, |A ∩ B|,
|A∩C|, |B ∩C|, |A∪B|, |A∪C|, |B ∪C|, |A∩B ∩C|, |A∪B ∪C|, |Ac |, |B c |, |C c |, |(A∩B)c |,
|(A ∩ C)c |, |(B ∩ C)c |, |(A ∪ B)c |, |(A ∪ C)c |, |(B ∪ C)c |, |(A ∩ B ∩ C)c |, |(A ∪ B ∪ C)c |,
|A − B|, |B − A|, |A − C|, |C − A|, |B − C|, and |C − B|
A B
9
7 15
5
6 11
C
17
Question 4.25 (Easy) Simplify the following set theoretic expressions using the laws of set
theory.
(a) A ∪ A (e) A ∪ (Ac ∩ B) (i) A ∩ (A ∪ B ∪ C)
c
(b) A ∪ A (f) A ∩ (Ac ∪ B) (j) (A ∩ B) ∪ (A ∩ B c )
(c) (A ∪ Ac )c (g) A ∩ A (k) (Ac ∪ Ac )c
(d) A ∪ (A ∩ B) (h) A ∩ (A ∪ B) (l) (A ∩ B) ∪ (Ac ∩ B)
Question 4.26 (Moderate) Simplify the following set theoretic expressions using the laws
of set theory.
(a) (Ac ∪ B c ) ∩ (Ac ∪ B) (e) C ∪ [C ∪ (C ∩ A)]
(b) B ∪ (B ∩ B c ) (f) A ∩ [A ∪ (A ∩ B)]
(c) Ac ∪ (B ∩ Ac ) (g) C ∪ (C ∩ Ac ∩ B ∩ D)
(d) (A ∪ B c ) ∩ (A ∪ B) (h) C c ∩ (C ∩ A ∩ B ∩ D)c
Set Theory 87
Question 4.27 (Difficult) Simplify the following set theoretic expressions using the laws of
set theory.
(a) B ∩ [A ∪ (Ac ∩ B)] (g) [A ∪ (B ∩ C)] ∩ (Ac ∪ C)
c c c
(b) [(A ∩ B ) ∪ A ] (h) (A ∩ C c ) ∪ (Ac ∩ B) ∪ (A ∩ r)c
c
(c) (A ∩ B) ∪ [A ∩ (A ∪ B)] (i) (A ∩ C) ∪ (Ac ∩ B) ∪ (C ∩ B)
(d) [A ∪ (A ∩ B)]c ∩ B (j) Ac ∪ B c ∪ [A ∩ B ∩ C c ]
c c c
(e) (A ∩ C ) ∪ (A ∩ B) ∪ (B ∩ C) (k) (A ∪ C) ∩ (Ac ∪ B) ∩ (C ∪ B)
c
(f) (A ∩ B) ∪ (B ∩ C) (l) (A ∩ B)c ∩ (Ac ∪ 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 (c) is greater than (e) is greater than or equal
(b) is less than (d) is less 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 (c) is greater than (e) is greater than or equal
(b) is less than (d) is less 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.
1 3
2 4
Question 4.31 Find the stated relations on the set {1, 2, 3}. Then determine if they are
reflexive, irreflexive, symmetric, antisymmetric, or transitive.
(a) the relation ≤ (c) the relation = (e) the relation >
(b) the relation < (d) the relation 6= (f) the relation ≥
Question 4.32 Show the relation “is greater than or equal to” is a partial ordering on N.
Show this relation is an equivalence relation. Then find 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 N.
CHAPTER 5
Boolean Algebra
There are two major reasons to study Boolean algebra in computer science. First, Boolean
algebra unifies 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 ¬ defined on elements of the set {T, F },
• a list of laws the operations satisfied.
In set theory we start out with a universal set U but then define 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 elements of
P(U) using ∩, ∪, and c . Again, we found a variety of equalities we called laws. In summary,
we had
• a set P(U),
• three operations ∩, ∪, and c defined on elements of the set P(U),
• a list of laws the operations satisfy.
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 c , the F 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 definition.
89
248 Discrete Mathematics for Computer Science
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
Example 5.2
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
From the last two examples we already know that x + x = x is true and the dual of
this statement is x × x = x. Thus by the duality principle x × x = x is also true.
Example 5.4
Thus we have shown x × 0 = 0. The dual of this is x + 1 = 1, which is also true by the
duality principle.
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
Example 5.7
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 identification,
no electrical current = 0,
electrical current = 1,
which means we usually write our Boolean set as B = {0, 1}.2 Recall, the definition 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 first identity axiom says that x × 1 = x. Since x can either be 1 or
0 we have
1 × 1 = 1,
0 × 1 = 0.
2 This is a simplification. Though it is nice to think of 0 meaning “no electrical current” there is actually
× 0 1
0 0 0
1 0 1
and
+ 0 1
0 0 1
1 1 1
96 Discrete Mathematics for Computer Science
x x0
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 x0 . We generally “read” circuit diagrams going from left to right just like we read
English.
x x0
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 22 = 4 possible combinations of inputs. 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 first 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.
Boolean Algebra 97
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.
x
x×y
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.
x
x+y
y
Now we will look at the nand gate. The nand gate is the not of the and gate,
x nand y = (x × y)0 .
We will simply use the term nand to mean the nand operation. The truth table for the
nand gate is given below.
x y x nand y
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
tiny circle on the right where the output comes out.
x
x nand y
y
98 Discrete Mathematics for Computer Science
Next we look at the nor gate. The nor gate is the not of the or gate,
x nor y = (x + y)0 .
We will simply use the term nor to mean the nor operation. The truth table for the nor
gate is given.
x y x nor y
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
x nor y. Again, notice that the picture for a nor gate looks like an or gate, only has a
tiny circle on the right where the output comes out.
x
x nor y
y
The next gate we look at is called the exclusive-or gate, or xor gate. In an exam-
ple 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 0 + x0 × 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.
x
x xor y
y
Finally, we look at the xnor gate. The xnor gate is the not of the xor gate. That is,
We will simply use the term xnor to mean the xnor operation. The truth table for the
xnor gate is given.
Boolean Algebra 99
x y x xnor 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.
x
x xnor y
y
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.
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 0 + y. We begin by drawing an expression tree similar to the
expression trees from logic.
+
× y
x 0
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 0 + 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.)
100 Discrete Mathematics for Computer Science
+
y
0
This modified expression tree can be used as a basis for drawing the circuit diagram.
The nodes with the variables become the input signals. Each of the other nodes of the
expression tree is replaced by the correct gate. The 0 is replaced with the not gate, the ×
is replaced with the and gate, and the + is replaced with the or gate.
y
x × y0 + y
Finding the expression tree can be helpful for drawing circuit diagrams but is not necessary
once you get good at it.
Example 5.8
x
y
Example 5.9
Example 5.10
The circuit drawn in the last example is quite complicated. Use the laws of Boolean
algebra to simplify the expression (x0 + y 0 ) × (x + y 0 ) × (y + z) and then draw the circuit
for the simplified expression.
y 0 z.
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.
102 Discrete Mathematics for Computer Science
Example 5.11
Find the truth table and draw the circuit for the following Boolean algebra expression
x × z 0 + y.
x × z0 + y
y
Example 5.12
Find the truth table and draw the circuit for the following Boolean algebra expression
[(x + y) × z]0 .
x
y
[(x + y) × z]0
z
Suppose you were given two expressions, (x0 x + x0 y 0 + y 0 x + y 0 y 0 )(y + z) and y 0 (y + z).
Are these expressions equal or not? That is, do these expressions represent the same thing
or not? How would you find 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
simplified the expression (x0 + y 0 )(x + y 0 )(y + z),
The two expressions (x0 x + x0 y 0 + y 0 x + y 0 y 0 )(y + z) and y 0 (y + z) are simply two different
lines in this above simplification. 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 find 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 first consider the sum-of-products form. The first step in writing an expression
in the sum-of-products form is to find the truth table. We use the truth table for x × z 0 + y
from the above example. When there is a 1 in the final column we consider the values in
the row.
• If x value is 1 use x in the product term, if x value is 0 use x0 in the product term.
• If y value is 1 use y in the product term, if y value is 0 use y 0 in the product term.
• If z value is 1 use z in the product term, if z value is 0 use z 0 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.
104 Discrete Mathematics for Computer Science
x y z x × z0 + y product terms
1 1 1 1 xyz
1 1 0 1 xyz 0
1 0 1 0
1 0 0 1 xy 0 z 0
0 1 1 1 x0 yz
0 1 0 1 x0 yz 0
0 0 1 0
0 0 0 0
In the first row there is a 1 in the final 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
final 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 0 in the product term to get xyz 0 . In the fourth row there is
a 1 in the final 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 0 , and z 0 in the product term to get xy 0 z 0 . And so on. We then
add up, or sum, all the product terms to get x × z 0 + y = xyz + xyz 0 + xy 0 z 0 + x0 yz + x0 yz 0 .
Example 5.13
Next we will consider the product-of-sum form. The first step in writing an expression
into the product-of-sum form is to find the truth table. Again we use the truth table for
x × z 0 + y from the above example. When there is a 0 in the final column we consider the
values in the row.
• If x value is 1 use x0 in the sum term, if x value is 0 use x in the sum term.
• If y value is 1 use y 0 in the sum term, if y value is 0 use y in the sum term.
• If z value is 1 use z 0 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.
Boolean Algebra 105
x y z x × z0 + y sum terms
1 1 1 1
1 1 0 1
1 0 1 0 x0 + y + z 0
1 0 0 1
0 1 1 1
0 1 0 1
0 0 1 0 x + y + z0
0 0 0 0 x+y+z
In the third row the final 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 x0 , y, and z 0 in the sum term to get
x0 + y + z 0 . In the seventh row the final 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 0 in the sum term
to get x + y + z 0 . In the eighth row the final 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 0 + y = (x0 + y + z 0 )(x + y + z 0 )(x + y + z).
Example 5.14
5.5 PROBLEMS
Question 5.4 Write down the Boolean expression associated with each of the following
circuits.
x x
x y y
y
(a)
z z
(c) (e)
x x
y y
z z
y
(b) (d) (f)
Question 5.5 Write down the Boolean expression associated with each of the following
circuits.
x x
y y
(a) (c)
x x
y y
(b) (d)
Boolean Algebra 107
Question 5.6 Write down the Boolean expression associated with each of the following
circuits.
x x
y y
z z
(a) (c)
x x
y y
z z
(b) (d)
Question 5.7 Write down the Boolean expression associated with each of the following
circuits.
z z
x x
y y
(a) (c)
z z
x x
y y
(b) (d)
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.
108 Discrete Mathematics for Computer Science
Question 5.11 Below are the truth tables for some functions f (x, y). First write f (x, y)
in sum-of-products form and then write f (x, y) in product-of-sums form.
Question 5.12 Below are the truth tables for some functions f (x, y). First write f (x, y)
in sum-of-products form and then write f (x, y) in product-of-sums form.
Question 5.13 Write out the truth table for the following Boolean expressions.
(a) (x0 + y)x (e) (x + y)(x0 + y) (i) x0 y + y 0 (x + y)
(b) (x + y 0 )x (f) 0 0
(x + y )(x + y) (j) x0 y 0 + y 0 (x + y)
0
(c) (x + y)y (g) xx + x(x + y) (k) xy(x + y)
(d) (x + y)(x + y) (h) xy + y(x + y) (l) x0 y 0 (x0 + y 0 )
Boolean Algebra 109
Question 5.14 Write out the truth table for the following Boolean expressions.
(a) x(x + y 0 + z) (c) (x + y)(x + z) (e) x0 y 0 + z 0 (x + y)
0 0 0 0
(b) x (x + y + z) (d) (x + y )(x + z) (f) xy(x + y + z)
CHAPTER 6
Functions
Definition 6.1 Definition one of function: Let X and Y be sets. A function from X
to Y is a rule that assigns to each element of X exactly one element of Y . A function f
from X to Y is written as
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 defined 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
1 Note, the word “function” is often used in programming in a different way. There it refers to a block of
111
112 Discrete Mathematics for Computer Science
way to say this is the image of 1 is a, the image of 2 is c, and the image of 3 is b. The
function f is shown in a picture below.
f
1• •a
2• •b
3• •c
X Y
(domain) (codomain)
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 defined 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 definition, 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.
f
1• •a
2• •b
3• •c
X Y
(domain) (codomain)
Example 6.3
Sometimes functions that are defined by an algebraic rule are shown using a picture
of a function-machine.
x 4x2 − 10x
f
Functions 113
Example 6.4
represented by the expression (x × y) + z 0 . First recall that B = {0, 1}. This circuit
represents a function whose domain is the cartesian product B × B × B and whose
codomain is B,
f : B × B × B → B.
f (x, y, z) = (x × y) + z 0 .
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 finds the remainder after division by 8.
Example 6.6
Let the domain be X = {all circles in the plane} and the codomain be R. Then f :
X → R where
is a function. This function take a circle and finds 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:
114 Discrete Mathematics for Computer Science
f :X →Y
1• •a
2• •b
3• •c
X Y
Is this a function? Remember what the definition 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
f
1• •a
2• •b
3• •c
X Y
(domain) (codomain)
Example 6.7
1• •a
2• •b
3• •c
X Y
(domain) (codomain)
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) .
Example 6.8
1• •a
2• •b
3• •c
X Y
(domain) (codomain)
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 definition are satisfied. 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 and also the
unique element in Y associated to 3. Also notice that
range = {a, c} ⊂ Y.
Example 6.9
f •a
1• •b
2• •c
3• •d
4• •e
5• •f
6• •g
7• •h
•i
X
domain
Y
codomain
Convince yourself that the two properties in the definition are satisfied. Also, notice
that
range = {b, c, d, e, g, i} ⊂ Y.
Example 6.10
1• •a
2• •b
3• •c
X Y
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 satisfied. For 2 ∈ X the y ∈ Y is not
unique, there are two of them, namely b and c. Thus the definition for a function is
not satisfied.
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 x1 6= x2 then f (x1 ) 6= f (x2 ). Now it is a good time to remember a little logic. If we
define the propositions p and q to be
then the statement “if x1 6= x2 then f (x1 ) 6= f (x2 )” is the expression p → q. We showed
that the expression p → q was logically equivalent to its contrapositive ¬q → ¬p,
p → q ≡ ¬q → ¬p.
Therefore the statement “if x1 6= x2 then f (x1 ) 6= f (x2 )” is logically equivalent to the
statements “if f (x1 ) = f (x2 ) then x1 = x2 .” This is another way to define one-to-one
functions.
Example 6.11
f f
•a
1• •a 1•
•b
2• •b 2•
•c
3• •c 3•
•d
X Y X
(domain) (codomain) (domain) Y
(codomain)
f f
1•
•a 1• •a
2•
•b 2• •b
3•
•c 3• •c
4•
Y X Y
(codomain) (domain) (codomain)
X
(domain)
118 Discrete Mathematics for Computer Science
f (x)
30
20
10
x
−4 −2 2 4
Figure 6.1The graph of the function f (x) = x2 + 3x − 2. This graph (gray) is exactly
the set f defined by (x, x2 + 3x − 2) x ∈ R ⊂ R × R. The element (2, 8) ∈ f
is shown as a black dot. Every point on the graph (gray) represents an element of
f ⊂ R × R.
f : R −→ R.
The real-valued functions of one variable that you studied before were probably given in
terms of an equation, like
f (x) = x2 + 3x − 2 or like y = x2 + 3x − 2.
The equation is the rule that assigns to each element x ∈ R (the domain) exactly one
element of R (the codomain). If we want to know where the function sends a particular
element of the domain R we evaluate the function at that element. For example, if we want
to find where the function sends 2 we evaluate it at 2. That means we replace the x in the
equation with 2,
f (2) = 22 + 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) =
x2 + 3x − 2. The x-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
definition of a function. Consider our function f : R → R defined by f (x) = x2 + 3x − 2.
According to the set theory definition of a function, a function is a subset f ⊂ R × R that
satisfies two properties: (1) if x ∈ R then there is a y ∈ R such that (x, y) ∈ f , and (2) this
Functions 119
10
0
0 2 4 6 8 10
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 a function.
y is unique. Finally, recall that R × R is exactly the plane. For every input x from R there
is an output y from R so that (x, y) ∈ f ⊂ R × R. The output y is given by x2 + 3x − 2,
which is why we often write y = x2 + 3x − 2 for a function. So, according to the definition
the function f is given by
f = (x, x2 + 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 definition 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, y1 ) and (x, y2 ), where y1 6= y2 , that are on the graph. Both of these
points happen where the graph intersects a vertical line drawn at x. So, if it is possible to
draw a vertical line that intersects the graph in two or more places then the graph does not
represent a function, see Fig. 6.2.
Example 6.12
Find the domain and range of the graphed function and then find the function rule.
Here the graph of the function is simply the four shown points.
5
f (x)
4
3
2
1
x
−5 −4 −3 −2 −1
−1 1 2 3 4 5
−2
−3
−4
−5
120 Discrete Mathematics for Computer Science
The function rule tells us what element from the range is assigned to an element from
the domain,
1• •2
−2 • • −2
−3 • • −3
X Y
(domain) (range)
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
100 f (x)
x
−4 −2 2 4
−100
assigned.
Thus −2 is assigned to, or sent to, 2, 0 is sent to 0, and 6 is sent to 2.727272 . . .. Next
we find the domain of f . For this we look carefully at the rule, or algebra expression,
that defines f ,
5x
.
2x − 1
We know that division by zero is undefined, so we are not allowed to use any x value
that results in 2x − 1 = 0. To find this x value we simply solve this equation to find
x = 12 . Thus the domain is the set
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.
10
5
x
−10 −5 5 10
−5
−10
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.
200
x
−4 −2 2 4
−200
Since it is not possible to draw a vertical line that intersects the graph (gray) more
than once, the graph does represent a function.
122 Discrete Mathematics for Computer Science
The new function g ◦ f is sometimes called the composite function of f and g. Notice
that when we write g ◦ f we apply f first then then apply g to the result.
Example 6.16
1• •a a• •α
2• •b b• •β
3• •c c• •γ
4• •d d• •δ
X Y Y Z
(domain) (range) (domain) (range)
a
1• • •α
b
2• • •β
c
3• • •γ
d
4• • •δ
X Y Z
(domain) (range/domain) (range)
If this were a more advanced math book we would have to be very careful with our
domains and ranges. But in this book we will only consider simple functions where we will
not need to worry about the domains and ranges.
Now suppose we are given two functions, f, g : R → R defined by
f (x) = x2 + 2x − 3
and
g(x) = 3x − 1.
Functions 123
We want to find 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) .
x f (x) g f (x)
f g
If we put x = 4 into the function machine f then f (4) = 42 + 2(4) − 3 = 21 would come
out. Then the 21 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 find the formula for the composite function by putting the variable x into the
machine instead of a number. Clearly x2 + 2x − 3 would come out of the f machine which
would then go into the g machine. What would come out of the g machine? To find out we
put the x2 + 2x − 3 into the formula for g(x) and then simplify,
(g ◦ f )(x) = g f (x)
= g x2 + 2x − 3
= 3 x2 + 2x − 3 − 1
= 3x2 + 6x − 9 − 1
= 3x2 + 6x − 10.
x g(x) f g(x)
g f
Then to find f ◦ g we put the formula for g(x) into the formula for f (x) and simplify,
(f ◦ g)(x) = f g(x)
= f 3x − 1
= (3x − 1)2 + 2(3x − 1) − 3
= (9x2 − 6x + 1) + (6x − 2) − 3
= 9x2 − 4.
This gives the formula for (f ◦ g)(x) = 9x2 − 4. Notice, g ◦ f 6= f ◦ g. Of course we can
compose more than two functions. We can compose three functions, or even more.
124 Discrete Mathematics for Computer Science
Example 6.17
We now will study how to find 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 defining the identity function and the inverse function.
Definition 6.4 Suppose X is a set. The identity function on X is the function i : X →
X given by i(x) = x.
The inverse function of f is usually written as f −1 . The exponent −1 tells you we are
talking about the inverse 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
1• •1
2• •2
3• •3
4• •4
X X
(domain) (codomain)
Example 6.19
If X = {1, 2, 3}, Y = {a, b, c}, find the inverse function of f for f : X → Y given by
Function f is pictured by
f :X →Y
1• •a
2• •b
3• •c
X Y
(domain) (codomain)
You can get the picture for f −1 just by reversing the arrows on the picture for f . This
give us
f −1 : Y → X
a• •1
b• •2
c• •3
Y X
(domain) (codomain)
Notice that
Example 6.20
x f (x) g f (x) = i(x)
f g
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
Functions 127
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:
1• •a 1• •a 1• •a
2• •b 2• •b 2• •b
3• •c 3• •c 3• •c
4• •d 4• •d 4• •d
5• •e 5• •e 5• •e
X Y X Y X Y
1• •a 1• •a 1• •a
2• •b 2• •b 2• •b
3• •c 3• •c 3• •c
4• •d 4• •d 4• •d
5• •e 5• •e 5• •e
6• •f 6• •f 6• •f
7• •g 7• •g 7• •g
X Y X Y X Y
1• •a 1• •a 1• •a
2• •b 2• •b 2• •b
3• •c 3• •c 3• •c
4• •d 4• •d 4• •d
5• •e 5• •e 5• •e
6• •f 6• •f 6• •f
7• •g 7• •g 7• •g
X Y X Y X Y
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 prod-
uct of X and Y by:
n o
f1 = (1, b), (2, c), (3, a), (4, e), (5, f ), (6, d) ⊂ X × Y
n o
f2 = (1, b), (2, b), (3, b), (4, e), (5, e), (6, e) ⊂ X × Y
n o
f3 = (1, c), (2, d), (3, e), (4, f ), (5, a), (6, b) ⊂ X × Y
Question 6.7 The following function rules are given by an equation. Evaluate these func-
tions at the values x = −3, x = 0, and x = 4.
(a) f (x) = 3x + 7 (d) f (x) = 2x (g) f (x) = −x3 + 2x2 − x
(b) f (x) = 3(x + 4) − 5 (e) f (x) = x2 + 3x (h) f (x) = 2 · 3−x+1
(c) f (x) = −4x + 6 (f) f (x) = (x − 2)(x + 3) (i) f (x) = (x2 − 1)2
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.
130 Discrete Mathematics for Computer Science
5 f (x) 5 f (x)
4 4
3 3
2 2
1 1
x x
−5 −4 −3 −2 −1 1 2 3 4 5 −5 −4 −3 −2 −1 1 2 3 4 5
−1 −1
−2 −2
−3 −3
−4 −4
−5 −5
(a) (b)
5 f (x) 5 f (x)
4 4
3 3
2 2
1 1
x x
−5 −4 −3 −2 −1 1 2 3 4 5 −5 −4 −3 −2 −1 1 2 3 4 5
−1 −1
−2 −2
−3 −3
−4 −4
−5 −5
(c) (d)
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
f (1) = c g(a) = δ
f (2) = b g(b) = γ
f (3) = d g(c) = α
f (4) = a g(d) = β
Find g ◦ f : X → Z
Question 6.10 Given the functions f and g in Fig. 6.6, find g ◦ f . What is the domain,
codomain, and range of g ◦ f ? Then find f ◦ g. What is the domain, codomain, and range
of f ◦ g?
Question 6.11 For each function pictured in Fig. 6.3, find the inverse function if it exits.
Functions 131
x x
(a) (b)
x x
(c) (d)
x x
(e) (f)
Question 6.12 For each function given in question 6.2 find the inverse function if it exists.
Question 6.13 For each function given in question 6.3 find the inverse function if it exists.
132 Discrete Mathematics for Computer Science
f g
1• •a a• •1
2• •b b• •2
3• •c c• •3
4• •d d• •4
5• •e e• •5
X Y Y X
Question 6.14 For each function given in question 6.4 find the inverse function if it exists.
Question 6.15 Are the following pairs of functions inverses of each other or not?
(a) f (x) = −10+x
4 and g(x) = 4x + 10 (c) f (x) = x3 − 1 and g(x) = x+1
3
−2x+4
(b) f (x) = −(x + 2)3 and g(x) = 2 + x3 (d) f (x) = 2 − 23 x and g(x) = 3
1 8x+10 2
(b) f (x) = x −3 (d) f (x) = 3 (f) f (x) = x−3
CHAPTER 7
The basic concepts of counting and combinatorics help us understand a wide range of
different situations. In computer science they help us understand such things as how many
rows a truth table needs, how many different subsets a set can have, the maximum possible
number of times a loop is executed in an algorithm, and how many possible spanning trees
a graph has.
Definition 7.1 The Addition Principle: Suppose there are n1 ways for event E1 to
occur, and n2 ways for event E2 to occur, and we cannot do both E1 and E2 . If all these
ways are distinct then the number of ways for E1 or E2 to occur is n1 + n2 .
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
Suppose you go to a bakery and want either a chocolate cake or a vanilla cake. The
bakery has 10 different chocolate cakes and 8 different vanilla cakes to choose from.
How many choices of cake do you have?
Let E1 be the event of buying a chocolate cake and E2 be the event of buying a vanilla
cake. Then clearly there are n1 = 10 ways for E1 to occur and n2 = 8 ways for E2 to
occur. There are n1 + n2 = 10 + 8 = 18 ways for E1 or E2 to occur. Thus you have 18
choices for buying a chocolate cake or buying a vanilla cake.
133
134 Discrete Mathematics for Computer Science
Using the language of set theory, the addition principle could also be written this way.
Definition 7.2 The Addition Principle in the language of set theory: If A and B
are finite sets with A ∩ B = ∅, then
|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 definition 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
X
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
4
X
xi = x1 + x2 + x3 + x4 .
i=1
S
Similarly, the large means to take the union. Suppose we had the formula
6
[
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,
6
[
Ai = A1 ∪ A2 ∪ A3 ∪ A4 ∪ A5 ∪ A6 .
i=1
Of course there is nothing special about 4 or 6. It could be any number at all. Now we are
ready for the next definition.
Example 7.2
Suppose you want to go from town X to town Y . You can go by air, by 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 ?
If A1 is the set that contains the ways to go by air then |A1 | = 2. If A2 is the set that
contains the ways to go by land then |A2 | = 4. If A3 is the set that contains the ways
to go by sea then |A3 | = 3. Clearly A1 ∩ A2 = ∅, A1 ∩ A3 = ∅, and A2 ∩ A3 = ∅. So
we have
Definition 7.4 The Multiplication Principle: Suppose there are n1 ways for event E1
to occur, and each possible way that E1 occurs allows for exactly n2 ways for event E2 to
occur. Then the number of ways for E1 and E2 to occur is n1 · n2 .
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?
Let E1 be the event of choosing one boy, and E2 be the event of choosing one girl.
There are n1 = 11 ways for E1 to occur and n2 = 12 ways for E2 to occur. There are
n1 · n2 = 11 · 12 = 132 ways for E1 and 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 outfits can you make?
Let A be the set that contains 7 pairs of pants and B be the set that contains 15 shirts.
Each outfit consists of one pair of pants and one shirt, so each outfit is an element of
A × B. The number of different outfits possible is
The multiplication principle can also be generalized to more than two events.
136 Discrete Mathematics for Computer Science
Example 7.5
Example 7.6
What is the maximum number of times the while-do loop in step 3 is executed?
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?
Notice, it is possible that both the for-do loop and if-then conditional control is
executed fewer times. If for some number i < n we have xi = s then the if-then
conditional control step 2.1.1 is executed, the phrase “String contains s” is returned,
and the algorithm ends after executing both the for-do loop and if-then conditional
control only i times. But since we do not know what string x1 , . . . , xn was input
we 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?
loop in step 3 is executed when i goes from 1 to n, so the maximum number of times
this loop is executed is n. We use the addition principle to find the maximum number
of times the for-do loops in both steps 2 and 3 are executed; n times in step 2 and n
times in step 3 for a total of n + n = 2n times.
It is also clear that the if-then conditional control in step 2.1 is executed once for each
time the for-do loop in step 2 is executed. Similarly, the if-then conditional control
in step 3.1 is executed once for each time the for-do loop in step 3 is executed. Using
the addition principle, the maximum number of times the if-then conditional control
in steps 2.1 and 3.1 are executed is n + n = 2n times.
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?
Each ordering is called a permutation of the letters a, b, and c. There are a total of six
permutations of the letters a, b, and c. One way to think about this is that there are three
letters, so we need three spaces, one for each letter. In the first space we can put any of the
three letters. In the second space we can put either of the two remaining letters, and in the
Counting and Combinatorics 139
| {z } | {z } | {z }
3 2 1
possibilites possibilites possibility
Finding out how many different permutations are possible becomes an application of the
multiplication principle. There are a total of 3 · 2 · 1 = 6 permutations possible.
We will introduce a little bit of notation to help us called the factorial symbol. The
factorial symbol is simply the exclamation mark ! following a positive integer. By looking
at a few examples it should be clear how the factorial sign works
1! = 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.
We have actually encountered factorials before when we were looking at algorithms. Here
is one possible algorithm for computing factorials.
Example 7.10
Since there are seven letters the number of permutations possible is given by 7! = 5 040.
140 Discrete Mathematics for Computer Science
| {z } | {z } | {z }
26 25 24
possibilites possibilites possibility
so we have 26 · 25 · 24 = 15 600 possible orderings. We can come up with a nice formula for
this,
26 · 25 · 24 · 3 ·
2 2 ·
2 21 · · · 3 · 2 · 1
26 · 25 · 24 =
·
23
2 ·
2 1 · · · 3 · 2 · 1
2
26!
=
23!
26!
= .
(26 − 3)!
In general we may wish to choose r objects from a set of n objects to fill r spaces
where the ordering of the objects matters. We have the number of permutations of r objects
selected from a set of n objects, where n > r, given by
n!
.
(n − r)!
The standard notation used for the number of permutations of r objects selected from n
objects is Prn , which is read out loud as “n-pee-r.” Thus we have the formula
n!
Prn = .
(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!
P526 = = = 7 893 600.
(26 − 5)! 21!
Counting and Combinatorics 141
Example 7.12
You are given a list of 20 different flavors of ice cream and asked to rank your four
favorite flavors. How many different possible responses are there?
You are basically being asked to find the number of permutations of four items from
20 items, or P420 , which is given by
20! 20!
P420 = = = 116 280.
(20 − 4)! 16!
Example 7.13
How many different ways are there to select a committee consisting of a chairperson,
a secretary, and a treasurer from a group of twelve people?
Here order matters. We may say that the first 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!
P312 = = = 1 320.
(12 − 3)! 9!
| {z } | {z } | {z }
26 25 24
possibilites possibilites possibility
gave us 26 · 25 · 24 = 15 600 possible orderings. But what if the three letters were a, b, and
c. Then there are a total of six possible ways these three letters can be ordered,
Considering how the three letters could be ordered in the three spaces gave us
| {z } | {z } | {z }
3 2 1
possibilites possibilites possibility
or 3! possible permutations. These six permutations are all considered the same combination
of three letters. This is true of any combination of three letters. Thus, in order to find the
142 Discrete Mathematics for Computer Science
total number of combinations we have to divide the number of permutations of three letters,
which was P326 = 15 600, by 3! = 6. This gave us a total of 2 600 different 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, Prn , by r! to give
us
Prn n!
= .
r! r!(n − r)!
The standard notation for the number of combinations of r objects chosen from n objects
is nr , though sometimes Crn is also used.1 Thus we have
n n!
= .
r r!(n − r)!
Example 7.14
How many different combinations of five letters from the alphabet are there?
The number of combinations of 5 letters from the 26 letters of the alphabet is given
by
26 26! 26!
= = = 65 780.
5 5!(26 − 5)! 5!21!
Example 7.15
How many different committees of three people can be selected out of a total of twelve
people?
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
A student wants to go from her home X to her friend’s home Z. To get from X to Z
she must travel through Y. From X to Y there are 3 different bus routes or 5 different
train routs. To go from Y to Z there are 4 bus routes or 2 train routes. How many
different routes are there from X to Z?
We use the addition principle to find the number of ways from X to Y, which is given
by 3 + 5 = 8. We also use the addition principle to find the number of ways from Y to
Z, which is given by 4 + 2 = 6. Then to find the number of ways from X to Z we use
the multiplication principle to get 8 · 6 = 48 different routes.
Example 7.18
An anagram of a word is a permutation of the letters of the word. How many anagrams
are there for the following words? (a) ANSWER, (b) HELLO, and (c) MISSISSIPPI
(a) The word ANSWER has six different letters so the number of permutations is
given by P66 = 6! = 720.
(b) The word HELLO has five letters but two of the letters are L. There are P55 =
5! = 120 ways of permuting five letters. However, switching the two Ls does not
change permutation so there are 120/2 = 60 different permutations.
11
(c) The word MISSISSIPPI has 11 letters and there are P11 = 11! different ways of
permuting 11 letters. However, there are 4 Ss, 4 Is, and 2 Ps. Switching any of
the 4 Ss does not change the anagram, nor does switching any of the 4 Is or the
2 Ps. There are 4! ways to switch the 4 Ss, 4! ways to switch the 4 Is, and 2!
ways of switching the 2 Ps. Thus the total number of anagrams is
11!
= 34 650.
4! · 4! · 2!
Example 7.19
Recalling the definition of functions, we know that each element in the domain must
be sent to an element in the codomain. We use the multiplication principle. Each of
the 5 elements in the domain could be sent to one of the 7 elements in the codomain.
Thus the number of possible functions is given by
7 × 7 × 7 × 7 × 7 = 75 = 16 807.
144 Discrete Mathematics for Computer Science
Example 7.20
Let X = {1, 2, 3, 4, 5} and Y = {a, b, c, d, e, f, g}. How many different one-to-one func-
tions f : X → Y are there?
Recalling the definition of one-to-one functions, we know that each element in the
domain must be sent to a different element in the codomain. In other words, how
many permutations are of of 5 elements from 7 elements. This is
7! 7!
P57 = = = 2 520.
(7 − 5)! 2!
Example 7.21
A committee consisting of three women and three men are to be chosen from a group
of 22 women and 27 men. In how many ways can this be done?
The number of ways we can choose three women from 22 women is given by 22
3 and
27
the number of ways we can choose three men from 27 men is given by 3 . We then
use the multiplication principle to find the total number of ways the committee can
be formed,
22 27
· = 4 504 500.
3 3
Example 7.22
For the first three positions (president, secretary, treasurer) order matters, but for the
second three positions (three at-large trustees) order does not matter. If we choose
the first three positions first there are P320 possibilities. We then choose the second
three positions where there are now C317 possibilities. We now use the multiplication
principle to get the total number of possibilities.
P320 · C317 = (6 840)(680) = 4 651 200.
But suppose we decided to first choose the three at-large trustees and then choose the
president, secretary, and treasurer. In this case the number of possibilities for the first
three positions is C320 and the number of possibility for the second three positions is
P317 . Using the multiplication principle we have
C320 · P317 = (1 140)(4 080) = 4 651 200.
It should not surprise you that these numbers are the same.
Counting and Combinatorics 145
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 x1 , . . . , xn .
2. For i = 1 to n − 1 do
2.1 For j = i + 1 to n do
2.1.1 If xi = xj then
2.1.1.1 Return “There is duplicate integer in the string.”
3. Return “There are no duplicate integers in the string.”
This algorithm is a little more complicated than the algorithms in section 7.2. We
begin with the for-do loop in step 2. This for-do loop runs from i = 1 to n − 1 so it
is executed a maximum of n − 1 times. However, the for-do loop in step 2.1 runs from
j = 1 + 1 to n. So, when i = 1 this loop runs from j = 2 to n, or n − 1 times. When
i = 2 this loop runs from j = 3 to n, or n − 2 times, and so on.
A better way to think about this algorithm it so recognize that it does one comparison
(in step 2.1.1) for each set of two non-equal indices i and j. In other words, once for
each set {i, j} of two numbers in the set {1, 2, . . . , n}. We can use combinations to find
this,
n2 − n
n n(n − 1)
f (n) = = = = 0.5n2 − 0.5n,
2 2 2
so the maximum number of times the for-do loop in step 2.1 is executed is 0.5n2 −0.5n
times. The maximum number of times the if-then loop in step 2.1.1 is executed is the
same.
7.6 PROBLEMS
Question 7.1 How many five digit decimal number are there that contain only odd digits?
Question 7.2 How many five digit octal number are there that contain only odd digits?
Question 7.3 How many five digit hexadecimal number are there that contain only odd
digits?
Question 7.4 How many different ways can a first, second, and third prize be awarded in
a class of 32 students.
146 Discrete Mathematics for Computer Science
Question 7.5 How many different ways are there to choose a five 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 five people from
a group of 20 people?
Question 7.7 An individual needs to travel from X to Z. To get from X to Z one must
travel through Y. From X to Y there are 8 different bus routes or 6 different train routs.
To go from Y to Z there are 5 bus routes or 9 train routes. How many different routes are
there from X to Z?
Question 7.8 A school assigns two kinds of identification 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 identification 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.10 Let X = {u, v, w, x, y, z} and Y = {1, 2, 3, 4, 5, 6, 7, 8, 9}. How many differ-
ent functions f : X → Y are there? How many one-to-one functions are there?
Question 7.12 An online quiz has four pools of questions. The first 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 first 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 x1 , x2 , . . . , xn .
2. i ←− 1
3. order ←− true
4. While i < n and order = true do
4.1. If xi > xi+1 then
4.1.1. order ←− false
4.2. i ←− i + 1
5. If order = true then
5.1. Output “Numbers are in order.”
else
5.2. Output “Numbers are out of order.”
Counting and Combinatorics 147
Question 7.14 Given two n×n matrices A and B the following algorithm performs matrix
operation. (An n × n matrix A is an n × n array of numbers labeled aij where 1 ≤ i ≤ n and
1 ≤ j ≤ n. Thus the matrix A consists of n2 numbers. The matrix B is defined similarly.)
What is the maximum number of times the for-do loop in step 2 could execute? What is the
maximum number of times the for-do loop in step 2.1 could execute? What is the maximum
number of times the for-do loop in step 2.1.2 could execute?
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 x1 , x2 , . . . , xn .
2. For i = 2 to n do
2.1. insert ←− xi
2.2. j ←− i − 1
2.3. While j ≥ 1 and xj > insert do
2.3.1. xj+1 ←− xj
2.3.2. j ←− j − 1
2.4. xj ←− insert
3. Output x1 , x2 , . . . , xn .
Question 7.16 The following is Warshall’s algorithm to determine if there exists a directed
path from vi to vj . 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 3 is executed?
What is the maximum number of times the for-do loop in step 3.1 is executed? What is the
maximum number of times the if-then conditional control in step 3.1.1 is executed? What
is the maximum number of times the for-do loop in step 3.1.1.1 is executed?
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.
Algorithm
↓
Find time-complexity function for algorithm.
↓
Find big-O category for time-complexity function.
↓
Use big-O category to rank algorithms by speed.
The general idea is that given any algorithm one can find 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 ap-
proximations. The function found is called the algorithm’s time-complexity function. Once
the time-complexity function for the algorithm is obtained we find 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 algo-
rithms 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.
149
150 Discrete Mathematics for Computer Science
f : N −→ N
This definition of time-complexity actually uses the first three approximations. The
phrase “dominant operations” means we are using the first 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 find the time-complexity
functions for these algorithms.
What we are doing is using the time-complexity function as a way of indirectly measur-
ing 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.
Approximations One and Two
The first two approximations are related to finding the dominant operation in the al-
gorithm. The operations performed in the algorithms in this book are assignments (←−),
comparisons (=, 6=, >, <, ≮, ≯), addition, subtraction, multiplications, and division. Accord-
ing to the first approximation, we are interested in the operations that are executed most
frequently in the algorithm. Consider the following algorithm.
Algorithmic Complexity 151
Algorithm: Calculates xn .
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 one time, whereas the assignment
and the multiplication in step 3.1 are executed each time the for-do loop is executed. The
first 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 (=, 6=, >, <, ≮, ≯)
• 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 oper-
ations 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.
Figuring out the dominant operation is a little difficult sometimes. Notice how we said
that assignments are usually faster than comparisons, which are usually faster than addi-
tion 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 x1 , x2 , . . . , xn .
2. For i = 2 to n do
2.1. insert ←− xi
2.2. j ←− i − 1
2.3. While j ≥ 1 and xj > insert do
2.3.1. xj+1 ←− xj
2.3.2. j ←− j − 1
2.4. xj ←− insert
3. Output x1 , 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 xj > insert is almost certainly more time con-
suming. 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.
For the first 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.
Algorithmic Complexity 153
Example 8.1
Example 8.2
Example 8.3
Example 8.4
1. Input string x1 , . . . , xn .
2. For i = 1 to n − 1 do
2.1 For j = i + 1 to n do
2.1.1 If xi = xj then
2.1.1.1 Return “There is duplicate integer in the string.”
3. Return “There are no duplicate integers in the string.”
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 first
for-do runs from i = 1 to n − 1 but the second for-do loop only runs from j = i + 1
2
to n. Roughly, this algorithm does half the work of the last example so f (n) ≈ n2 .
Another 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-complexity function is given by
n2 − n
n n(n − 1)
f (n) = = = = 0.5n2 − 0.5n.
2 2 2
Algorithmic Complexity 155
Example 8.5
1. Input x1 , x2 , . . . , xn .
2. For i = 2 to n do
2.1. insert ←− xi
2.2. j ←− i − 1
2.3. While j ≥ 1 and xj > insert do
2.3.1. xj+1 ←− xj
2.3.2. j ←− j − 1
2.4. xj ←− insert
3. Output x1 , x2 , . . . , xn .
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 nonequal 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)
f (n) = 2 =2 = n2 − n.
2 2
Example 8.6
What is the time complexity function for the first algorithm to find xn .
1. Input x and n.
2. answer ←− x
3. For i = 2 to n do
3.1. answer ←− answer × x
4. Output answer.
Multiplication is the dominant operation in this algorithm. The dominant operation
is executed each time the for-do loop is executed, which happens from i = 2 to n, or
n − 1 times. This gives a time complexity function of
f1 (n) = n − 1.
156 Discrete Mathematics for Computer Science
Example 8.7
What is the time complexity function for the second algorithm to find xn .
1. Input x and n.
2. d ←− number of bits in n. (n10 = bd bd−1 bd−2 . . . b1 2 )
3. If b1 = 1 then
3.1 answer ←− x
else
3.2 answer ←− 1
4. For i = 2 to d do
4.1 x ←− x × x
4.2 If bi = 1 then
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 = blog2 (n)c + 1, so we have
2(d − 1) = 2blog2 (n)c ≤ 2 log2 (n)
A word of caution, by replacing blog2 (n)c by log2 (n) the codomain of this time-
complexity function is no longer N but R, 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 first 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 find 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 bd , . . . , b1 then n is d bits. For example, if n = 4110 = 1010012
then d = 6 and b6 = 1, b5 = 0, b4 = 1, b3 = 0, b2 = 0, and b1 = 1. Thus, n has six bits. In
general, the number of bits (binary digits) contained in the decimal number n is given by
the formula
d = blog2 (n)c + 1.
1. Input x and n.
2. d ←− number of bits in n. (n10 = bd bd−1 bd−2 . . . b1 2 )
3. If b1 = 1 then
3.1 answer ←− x
else
3.2 answer ←− 1
4. For i = 2 to d do
4.1 x ←− x × x
4.2 If bi = 1 then
4.2.1 answer ←− answer × x
5. Output answer.
The way the first algorithm works for finding xn is obvious, but the way the second
algorithm works for finding xn may not be obvious at all. We will use a simple example
to illustrate how these algorithms differ. Suppose you wanted to find 264 . Using the first
algorithm would require 63 multiplications since
764 = 7 × 7 × 7 × 7 × 7 × 7 × 7 × 7 × · · · × 7 .
| {z }
64 sevens and 63 multiplications
If we are clever then we can drastically reduce the number of multiplications necessary to
find 764 . First we find 7 × 7 which takes one multiplication and that gives us 72 . Then we
do 72 × 72 which is a second multiplication and gives us 74 . Then we do 74 × 74 which is
a third multiplication and gives us 78 . Then 78 × 78 is a fourth multiplication and gives us
158 Discrete Mathematics for Computer Science
716 . Then 716 × 716 is a fifth multiplication and gives us 732 . Finally 732 × 732 is a sixth
multiplication 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 mul-
tiplications necessary to find xn . A carefully constructed algorithm can reduce the amount
of work necessary. This is where the fourth approximation becomes important.
Example 8.8
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 first algorithm is f1 (n) =
n − 1, so for the first 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 first algorithm is actually a little
better than the second algorithm.
Example 8.9
Suppose we wanted to find 71024 . How many multiplications would be needed in each
of the two algorithms to find xn ?
This is easily seen using the time complexity functions for these algorithms that were
found in the last section. For the first algorithm we have
When we assume the input is large it is very clear that the second algorithm is much
faster than the first algorithm.
Here are a few more examples that make it clear why it is important to assume that the
input n is large.
Algorithmic Complexity 159
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) = n2 f2 (n) = n3
1 1 1
5 25 125
10 100 1, 000
100 10, 000 1, 000, 000
Example 8.11
Now suppose the time-complexity of algorithm one is f1 (n) = 1, 000, 000n2 = 106 n2
and the time-complexity of the second algorithm is f2 (n) = n3 . Which algorithm is
faster?
Let us use the same values of n that we used in the last example.
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 algo-
rithm two is faster. This is where assumption four becomes very important. Let us
make another table for much larger values of n.
Up until n = 106 algorithm two is faster, but when n is larger than 106 algorithm
one is faster. So, for large n, algorithm one is much faster.
Approximation Five
In the fifth 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 f2 (n) = cf1 (n).
160 Discrete Mathematics for Computer Science
Example 8.12
Determine if two given time-complexity functions are a constant multiple of each other.
1. Are f1 (n) = 5n2 and f2 (n) = 50n2 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.
n4
2. Are f1 (n) = 500n4 and f2 (n) = 100 constant multiples of each other? Since
f1 (n) = 50, 000f2 (n)
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
1
Here the constant is 50,000 . So, it does not matter if we write f1 in terms of f2
or f2 in terms of f1 .
3. Are f1 (n) = 200n3 and f2 (n) = 600n4 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 .
Definition 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
In order to show that f is O(g) we need to find some c such that f (n) ≤ cg(n) for all
very large values of n. If we choose c = 5 then it is clear that 5n + 20 5n for any
values of n no matter how large. But suppose we choose c = 6. Then we have
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.
Example 8.14
Suppose that f (n) = 10n5 − 7n4 + 20n2 + 100 and g(n) = n5 . Show that f is
O(g) = O(n5 ).
Since f and g are time-complexity functions and f : N −→ N then the domain of both
f and g is N. This means that n is a positive number. A little thought should convince
you that
The functions
√ that are of most interest in analyzing algorithms in computer science are
1, log2 (n), n, n, n log2 (n), n2 , and 2n . Figure 8.1 plots these functions for 0 ≤ n ≤ 100.
The 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 log2 (n) grows very slowly, n grows a little faster, n grows linearly,
n log2 (n) grows faster yet, n2 grows even faster, and 2n grows fastest of the functions listed.
By looking at Fig. 8.1 we can see that for large n we have the following inequalities,
√
1 < log2 (n) < n < n < n log2 (n) < n2 < 2n .
Hopefully you also remember that
1 < n < n2 < n3 < n4 < n5 < n6 < · · · .
Also, for any sufficiently large n we have
nk < 2 n
for any given value of k ∈ N. These hierarchies of functions can be used to help us figure
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
162 Discrete Mathematics for Computer Science
100
2n
80 n2
60 nlog2 (n)
f (n)
n
40
20
√
n
log2 (n)
1
0
0 10 20 30 40 50 60 70 80 90 100
n
Figure 8.1 A comparison of the growth rates for some common time-complexity func-
tions.
so f is O(n).
Example 8.16
√
Consider the time-complexity function f (n) = 20 log2 (n) + 10 n + 50n log2 (n). Find
the function g such that f is O(g).
Again, we rely√on the string of inequalities above. For large n we know that log2 (n) <
n log2 (n) and n < n log2 (n), which gives us
√
20 log2 (n) + 10 n + 50n log2 (n) < 20n log2 (n) + 10n log2 (n) + 50n log2 (n)
< 80n log2 (n),
so f is O n log2 (n) .
Algorithmic Complexity 163
Example 8.17
√
Find the big-O category for the time-complexity function f (n) = 30n log2 (n)+50 n+
200n.
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 first term has
an n log2 (n), the second term has a n, and the third term has an n. Using the chain
of inequalities we can see that
√
n < n < n log2 (n)
thus the slowest term involves n log2 (n). This is our big-O category. Thus f ∈
O(n log2 (n)).
Example 8.18
Find the big-O category for the time-complexity function f (n) = 100 log2 (n) + 2n +
50n2 + 5000.
The first term of f has an log2 (n), the second term has an n, the third term has an n2
and 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
Now we can actually understand the fifth approximation better. Consider the following
four time-complexity functions,
√ √ √ √
f1 (n) = n, f2 (n) = 10 n, f3 (n) = 500 n, f4 (n) = 105 n.
categories themselves. Rewriting the above hierarchies we have, for sufficiently large n,
√
1 < log2 (n) < n < n < n log2 (n) < n2 < n3 < n4 < · · · < nk < 2n
Example 8.19
Example 8.20
Suppose algorithm one has time-complexity function f1 ∈ O(n3 ) and algorithm two
has time-complexity function f2 ∈ O(n). Which algorithm is faster?
n < n3
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 f1 (n) = 50n log2 (n) + 200 n and
algorithm two has time-complexity function f2 (n) = 30 log2 (n) + 500n. Which algo-
rithm is slower?
First we have to find the big-O categories of each algorithm. It is easy to see that
f1 ∈ O(n log2 (n)) and f2 ∈ O(n). Comparing the categories we have
Example 8.22
√
Suppose algorithm one has time-complexity function f1 (n) = 25 n + 10n log2 (n) and
algorithm two has time-complexity function f2 (n) = 100n log2 (n) + 20n + 50. Which
algorithm is faster?
First we have to find the big-O categories of each algorithm. It is easy to see that
f1 ∈ O(n log2 (n)) and f2 ∈ O(n log2 (n)). Both algorithms are in the same big-O
category. This means we consider these two algorithms to be equally fast.
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.4 For f (n) = 20 n + 40n2 + 500n log2 (n) find g such that f is O(g).
Question 8.5 For f (n) = 800n2 + 40n3 + 50 log2 (n) find g such that f is O(g).
Question 8.6 Suppose that algorithm one has the time complexity function f1 and algo-
rithm 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) = n2 and f2 (n) = n3 ,
(b) f1 (n) = n3 + n2 and f2 (n) = 20n2 ,
(c) f1 (n) = 500n3 and f2 (n) = 10n3 ,
(d) f1 (n) = 10n2 − 20n and f2 (n) = 20n3 − 50n2 ,
(e) f1 (n) = 15n5 and f2 (n) = 500n4 + 500n3 ,
(f) f1 (n) = 10000n2 and f2 (n) = 0.001n3 ,
(g) f1 (n) = n4 + 500n2 and f2 (n) = n5 + 500n2 ,
(h) f1 (n) = 2n and f2 (n) = 7000n.
Question 8.7 Suppose that algorithm one has the time complexity function f1 and algo-
rithm 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. √
(a) f1 (n) = 10 log2 (n) + 1000 and f2 (n) = 50 n,
(b) f1 (n) = 10000n log2 (n) and f2 (n) = 0.00001n,
√
(c) f1 (n) = 0.00001 n and f2 (n) = 100000 log2 (n) + 50,
(d) f1 (n) = 500n2 and f2 (n) = 2n ,
√ √
(e) f1 (n) = 500 n + log2 (n) and f2 (n) = 500 log2 (n) + n,
√ √
(f) f1 (n) = 0.1n + 10 n and f2 (n) = 0.1 n + 10 log2 (n),
166 Discrete Mathematics for Computer Science
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:
n
(a) 2n (b) n2 log2 (n) (c) 22
Question 8.10 Arrange the following algorithms in order of increasing growth rate:
(a) 2n log2 (n) (b) 2n (c) 2log2 (n)
Question 8.11 What is the dominant operation in the following algorithm? What is the
time complexity function of this algorithm?
1. Input x1 , x2 , . . . , xn .
2. i ←− 1
3. order ←− true
4. While i < n and order = true do
4.1. If xi > xi+1 then
4.1.1. order ←− false
4.2. i ←− i + 1
5. If order = true then
5.1. Output “Numbers are in order.”
else
5.2. Output “Numbers are out of order.”
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 aij where 1 ≤ i ≤ n and
1 ≤ j ≤ n. Thus the matrix consists of n2 numbers.) What is the dominant operation in the
following algorithm? What is the time complexity function of this algorithm?
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?
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.
Example 9.1
A graph with six vertices and seven edges. This graph has order six. The vertices are
labeled with capital letters.
A
C
B D
E
F
167
168 Discrete Mathematics for Computer Science
for a graph to be disconnected and have several pieces. These pieces are called compo-
nents 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.
A
C
B 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
E = AB, BC, BF , CD, CE, DE, EF .
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 ordering2 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.
2 The technical term for this kind of ordering is lexicographical ordering.
Graph Theory 169
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.
A
C
B D
E
F
This graph has two sets of parallel edges. The first 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
170 Discrete Mathematics for Computer Science
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
In this example the edge between vertices A and B goes only in one direction, from
vertex A to vertex B. We can see this by noticing the little arrow on the edge AB.
The other edges are similar.
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 infinite number of
different ways of drawing a graph. We can put the vertices 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.
Graph Theory 171
Example 9.8
Example 9.9
B D
B D
A C
A C
Both graphs have edges AD and BC. In the graph to the left it appears that these
two edges cross each other. You may think that this means the two edges are somehow
connected. They are not. The graph on the right is the correct way to imagine these
two edges. However, it is easier to draw the graph on the left, so you will often see edges
that are not connected draw crossing over each other. Just remember that this does
not mean the edges are connected. But you should realize that some graphs cannot be
drawn unless we cross edges that are not connected.
The degree of a vertex is defined 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
defined 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
X
deg(v) = 2|E|
v∈V
Example 9.10
B C
G
D
F E
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 find what degree vertex v4 has. Also, since there are eight edges
we have |E| = 8. Using the above theorem we have
X
deg(v) = 2 |E|
v∈V
⇒ deg(v1 ) + deg(v2 ) + deg(v3 ) + deg(v4 ) = 2 |E|
⇒ 2 + 2 + 3 + deg(v4 ) = 2(8)
⇒ 7 + deg(v4 ) = 16
⇒ deg(v4 ) = 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 define 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 circuit (graph theory). There are different ways to write a path. Below are some
Graph Theory 173
ABF BCD
A−B−F −B−C −D
A→B→F →B→C→D
A, B, F, B, C, D
Example 9.12
B C
G
D
F E
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 specified so it does not matter. Then you move from vertex F to vertex
E along the edge that connects them, and then finally 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
A path on a directed graph must follow the arrows, so the path CABC is acceptable
but CDBC is not since to go from C to D would require going along the edge CD in
the wrong direction.
Example 9.14
An example of a bridge.
C H C H
A D E G A D E G
B F B F
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, find 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).
D A
The vertices adjacent to vertex B are A, C, D, and E. The edges incident to vertex D
are BD, CD, DF , and DF . There is one loop, EE, and there are two parallel edges
DF and DF . There are no bridges.
deg(A) = 2 A B deg(B) = 5
C deg(C) = 3
deg(E) = 2 E D deg(D) = 4
deg(A) = 2 A B deg(B) = 6
C deg(C) = 4
deg(E) = 2 E D deg(D) = 4
circuit is called a semi-Eulerian graph. A graph that is not connected cannot have either
an Euler path or an Euler circuit.
The graph in Fig. 9.1 is an example of a semi-Eulerian graph. One example of an Euler
path is BBADCDEBC. Another is CDCBBADEB. A semi-Eulerian graph will generally
have many different Euler 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.
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.
This theorem is not hard to prove, but we will not prove it here. Instead we will give an
algorithm for finding 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.
176 Discrete Mathematics for Computer Science
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
final current path is either the Euler circuit or Euler path.
B C
D E F
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 ,
E = AB, AC, BC, BD, BE, CE, CF , DE, EF .
Notice, every vertex here is even so we choose to simply start at vertex A, the first 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 first 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 ,
E = AC, BC, BD, BE, CE, CF , DE, EF ,
current vertex = B,
current path = AB .
Graph Theory 177
A A A
B C B C B C
D E F D E F D E F
B C C C
D E F D E F E F
C C
Figure 9.4 The steps of Fleury’s algorithm for the graph given in Fig. 9.3
(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
V = A, B, C, D, E, F ,
E = AC, BD, BE, CE, CF , DE, EF ,
current vertex = C,
current path = AB, BC .
(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 first 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 ,
E = AC, BD, BE, CF , DE, EF ,
current vertex = E,
current path = AB, BC, CE .
178 Discrete Mathematics for Computer Science
(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 first 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 ,
E = AC, BD, CF , DE, EF ,
current vertex = B,
current path = AB, BC, CE, BE .
(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
V = A, C, D, E, F ,
E = AC, CF , DE, EF ,
current vertex = D,
current path = AB, BC, CE, BE, BD .
(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,
current path = AB, BC, CE, BE, BD, DE .
(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,
current path = AB, BC, CE, BE, BD, DE, EF .
(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
Graph Theory 179
(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,
current path = AB, BC, CE, BE, BD, DE, EF , CF , AC .
At this point all edges have been deleted from the graph and we are now done. The
current path set is given by
current path = AB, BC, CE, BE, BD, DE, EF , CF , AC
A−B−C −E−B−D−E−F −C −A
AB = e1 , AC = e2 , BC = e3 , BD = e4 , BE = e5 , CE = e6 , CF = e7 , DE = e8 , EF = e9 .
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 final output column so the table is not too wide to fit on the page.
180 Discrete Mathematics for Computer Science
e1 e2
e3
B C
e4 e5 e6 e7
D E F
e8 e9
Figure 9.5 The graph from Fig. 9.3 with the edges relabeled.
Writing current path as we have written graph paths in this chapter we have
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 finding 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 find an Euler path instead. Recall, a semi-Eulerian graph has two vertices with
odd degree with the rest of the vertices having even degree.
Graph Theory 181
inser-
Step current path tion e v new path unused edges
point
2 A - - - - {e1 , e2 , . . . , e9 }
3.1 A A - - - {e1 , e2 , . . . , e9 }
3.2 A A - A A {e1 , e2 , . . . , e9 }
3.3.1–4 A A e1 B Ae1 B {e2 , e3 , . . . , e9 }
3.3.1–4 A A e3 C Ae1 Be3 C {e2 , e4 , e5 , e6 , e7 , e8 , e9 }
3.3.1–4 A A e2 A Ae1 Be3 Ce2 A {e4 , e5 , e6 , e7 , e8 , e9 }
3.4 Ae1 Be3 Ce2 A A e2 A Ae1 Be3 Ce2 A {e4 , e5 , e6 , e7 , e8 , e9 }
3.1 Ae1 Be3 Ce2 A B e2 A Ae1 Be3 Ce2 A {e4 , e5 , e6 , e7 , e8 , e9 }
3.2 Ae1 Be3 Ce2 A B e2 B B {e4 , e5 , e6 , e7 , e8 , e9 }
3.3.1–4 Ae1 Be3 Ce2 A B e4 D Be4 D {e5 , e6 , e7 , e8 , e9 }
3.3.1–4 Ae1 Be3 Ce2 A B e8 E Be4 De8 E {e5 , e6 , e7 , e9 }
3.3.1–4 Ae1 Be3 Ce2 A B e5 B Be4 De8 Ee5 B {e6 , e7 , e9 }
3.4 Ae1 Be4 De8 Ee5 Be3 Ce2 A B e5 B Be4 De8 Ee5 B {e6 , e7 , e9 }
3.1 Ae1 Be4 De8 Ee5 Be3 Ce2 A E e5 B Be4 De8 Ee5 B {e6 , e7 , e9 }
3.2 Ae1 Be4 De8 Ee5 Be3 Ce2 A E e5 E E {e6 , e7 , e9 }
3.3.1–4 Ae1 Be4 De8 Ee5 Be3 Ce2 A E e6 C Ee6 C {e7 , e9 }
3.3.1–4 Ae1 Be4 De8 Ee5 Be3 Ce2 A E e7 F Ee6 Ce7 F {e9 }
3.3.1–4 Ae1 Be4 De8 Ee5 Be3 Ce2 A E e9 E Ee6 Ce7 F e9 E {}
Ae1 Be4 De8 Ee6 Ce7 F e9
3.4 Ee5 Be3 Ce2 A E e9 E Ee6 Ce7 F e9 E {}
Table 9.1 Trace of modified Fleury’s algorithm for graph in Fig. 9.5.
are all examples of matrices. The size of the matrix is indicated with the number of rows and
columns, usually 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 first subscript for the row number and the second subscript
for the column number. Here a 3 × 3 matrix is written using variables with subscripts,
a11 a12 a13
a21 a22 a23 .
a31 a32 a33
It is important to remember that the row subscript comes first and the column subscript
comes second, arc . Thus, for the 2 × 3 matrix
7 4 −3
.
2 −9 5
we would have
a11 = 7, a12 = 4, a13 = −3,
a21 = 2, a22 = −9, a23 = 5.
v1 v3
v2 v4
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 aij = aji .
Matrices that satisfy this property are called symmetric matrices. They 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 reflection
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 filled out. The upper tri-
angular adjacency matrix and lower triangular adjacency matrix are given by
0 2 1 1 0
0 0 0 2 0
and
1 1 1 0 1
0 1 0 1 0
respectively. Since both of these matrices give all the information needed you should not be
surprised if you sometimes see an adjacency matrix written like either of these matrices.
Example 9.16
v2
v3 v5
184 Discrete Mathematics for Computer Science
Example 9.17
v1 v3 v5
v2 v4 v6
We begin by filling out a table for this graph. Since this is an undirected graph we can
simply fill out the diagonal and one of the triangular regions.
v1 v2 v3 v4 v5 v6
v1 0 2 0 1 0 1
v2 0 1 0 1 0
v3 2 0 2 0
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.
v1 v3
v2 v4
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 first index in the subscript of aij represents the row then the vi are the “from”
vertices and since the second index in the subscript of aij represents the column then the
vj are the “to” vertices. The adjacency matrix for a directed graph will not usually be
symmetric.
For example, we have two edges going from v1 to v2 making a12 = 2. We have one edge
going from v1 to v3 so a13 = 1. We have one edge going from v1 to v4 making a14 = 1. The
loop goes from v3 and then back to v3 giving us a33 = 1. And finally, we have one edge
going from v3 to v4 giving us a34 = 1. The rest of the terms are zero. This gives us the
following table
to
v1 v2 v3 v4
v1 0 2 1 1
v2 0 0 0 0
from
v3 0 0 1 1
v4 0 0 0 0
which in turn gives us the adjacency matrix
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 .
186 Discrete Mathematics for Computer Science
v3 v4
v1 v2
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
aij = 1 if there is a path from vi to vj ,
aij = 0 if there is no path from vi to vj .
Notice how this looks almost the same as the definition for aij in the adjacency matrix. The
only difference is 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 definition, find 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 a11 = 1, a12 = 1, a13 = 1, and a14 = 1. If we
are on vertex v2 we can reach v2 (again by staying where we are), v3 and v4 but can
not reach v1 . This gives a21 = 0, a22 = 1, a23 = 1, and a24 = 1. Similarly, if we are on
vertex v3 we can reach v3 , v4 , and v2 but not v1 . This gives a31 = 0, a32 = 1, a33 = 1,
and a34 = 1. And finally, if we are on vertex v4 we can reach v4 , v2 , and v3 but not v1 .
This gives a41 = 0, a42 = 1, a42 = 1, and a44 = 1. Putting this together gives us the
following reachability matrix,
1 1 1 1
0 1 1 1
0 1 1 1 .
0 1 1 1
If we have the adjacency matrix for a directed graph there is an algorithm, called War-
shall’s algorithm, that produces the reachability matrix. In order to use Warshall’s algorithm
Graph Theory 187
we first have to recall Boolean addition. Boolean addition is defined 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.
Using Warshall’s algorithm we will find the reachability matrix for the directed graph
in Fig. 9.8. We begin by writing down the adjacency matrix for this graph,
0 1 0 0
0 0 1 0
0 0 0 1 .
0 1 0 0
(a) In step 1 we change all the entries along the diagonal to one, which gives us the matrix
1 1 0 0
0 1 1 0
0 0 1 1 .
0 1 0 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 aii = 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 0
0 1 1 0
.
0 0 1 1
0 1 0 1
For every row where in column one there is a 1, we add row one of this matrix to the
188 Discrete Mathematics for Computer Science
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,
1 1 0 0
0 1 1 0
0 0 1 1 .
0 1 0 1
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,
[0, 1, 1, 0] + [1, 1, 0, 0] = 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 + 1, 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
1 1 1 0
0 1 1 0
0 0 1 1 .
0 1 1 1
(d) Next we look at column three of the matrix we obtained above. Column three is
written in gray,
1 1 1 0
0 1 1 0
.
0 0 1 1
0 1 1 1
Graph Theory 189
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
giving us matrix
1 1 1 1
0 1 1 1
.
0 0 1 1
0 1 1 1
(e) Next we look at column four of the matrix we obtained above. Column four is written
in gray,
1 1 1 1
0 1 1 1
0 0 1 1 .
0 1 1 1
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
giving us matrix
1 1 1 1
0 1 1 1
.
0 1 1 1
0 1 1 1
(f) Since we have now finished all four columns we are done. The Reachability matrix is
given by the matrix we obtained above,
1 1 1 1
0 1 1 1
0 1 1 1 .
0 1 1 1
190 Discrete Mathematics for Computer Science
Since a21 = 0 there is no path from v2 to v1 , since a31 = 0 there is no path from v3
to v1 , and since a41 = 0 there is no path from v4 to v1 . However, there are paths in
all the other cases. Notice how this is exactly what we would expect from looking at
Fig. 9.8.
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 com-
puter the pseudocode below is what you would base your program on.
9.5 PROBLEMS
A B D E
A C A C A
E
E B
C
C D B D B D F
Figure 9.9 The graphs for questions 9.1, 9.11, and 9.18.
Graph Theory 191
Question 9.2 Assume you have a graph G with the following vertex and edge set,
V = A, B, C, D, E
E = AA, AD, AE, BC, BD, CD, CE, DD .
Question 9.3 Assume you have a graph G with the following vertex and edge set,
V = A, B, C, D, E, F
E = AB, AC, BC, BC, DE, DF , DF , DD .
Question 9.4 Assume you have a graph G with the following vertex and edge set,
V = A, B, C, D, E
E = AA, AD, AE, BC, BD, CD, CE, DD .
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
E = AA, AD, AE, BC, BD, BD, CD, EF , F 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
E = AD, AE, BC, BD, BD, CD, EF , F 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 five 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?
192 Discrete Mathematics for Computer Science
Question 9.9 Suppose a graph has four vertices and five 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 nei-
ther.
A A A A
B C B C B C B C
D E D E D E D E
Question 9.13 Assume you have a graph G with the following vertex and edge set,
V = A, B, C, D, E, F
E = AA, AD, AE, BC, BD, BD, CD, EF , F F .
Question 9.14 Use Fleury’s algorithm to find an Euler path on the below graph.
F
E
D A
B
C
Question 9.15 Assume you have a graph G with the following vertex and edge set,
V = A, B, C, D, E, F
E = AA, AD, AE, BC, BD, BD, CD, EF , F F .
Question 9.16 Assume you have a graph G with the following vertex and edge set,
V = A, B, C, D
E = AB, AC, BC, BC, BD, CD .
Show that G is an Eulerian graph. Use the modified Fleury’s algorithm for finding the Euler
circuit on an Eulerian graph starting at vertex A. It helps to relabel the edges as AB =
e1 , AC = e2 , BC = e3 , and so on.
Question 9.17 Assume you have a graph G with the following vertex and edge set,
V = A, B, C, D, E
E = AB, AC, BC, BD, BE, CD, CE, DE, DE .
Show that G is an Eulerian graph. Use the modified Fleury’s algorithm for finding the Euler
circuit on an Eulerian graph starting at vertex A. It helps to relabel the edges as AB =
e1 , AC = e2 , BC = e3 , and so on.
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
E = AA, AD, AE, BC, BD, CD, CE, DD .
Question 9.21 Assume you have a graph G with the following vertex and edge set,
V = A, B, C, D, E, F
E = AB, AC, BC, BC, DD, DE, DF , DF , .
Question 9.22 Assume you have a graph G with the following vertex and edge set,
V = A, B, C, D, E, F
E = AD, AE, BC, BD, BD, CD, EF , F F .
Question 9.24 Use Warshall’s algorithm to find the reachability matrix for the below graph.
D E
A
B
C
F
Question 9.25 Use Warshall’s algorithm to find the reachability matrix for the below graph.
A B C
D E F
CHAPTER 10
Trees
Trees are a special kind of graph that show up everywhere in computer science. Operating
systems use a tree structure for directories, folders, and files. 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 find a tree that comes from a directed or weighted graph. A good understanding
of tree basics is essential in computer science.
Example 10.1
C
B D
E
F
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.
195
196 Discrete Mathematics for Computer Science
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;
• if T has n vertices then it has n − 1 edges,
• given any two vertices in T there is only one path between the vertices that does not
repeat any vertices or edges,
• inserting an edge between any two vertices in T produces a new graph that has a
cycle,
• every edge of T is a bridge. That means that if any edge is removed it produces a
disconnected graph.
We will prove only the first of these properties below, but by looking at some examples of
trees the other properties should be clear.
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 difficult 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 T1 is a tree with one vertex. Since a tree cannot have any loops we know
that there are no edges in tree T1 . Thus T1 has 1 vertex and 0 edges.
Next suppose we have a tree T2 with two vertices v1 and v2 . Since trees are connected
we know that there is an 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
Trees 197
add back v2 and e to get back T2 , which increases the number of vertices by one and the
number of edges by one. We now know that T2 has 2 vertices and 1 edge.
Next suppose we have a tree T3 with three vertices v1 , v2 , and v3 . Since trees are
connected we know that there 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 T3 , which increases
the number of vertices by one and the number of edges by one. We now know that T3 has
3 vertices and 2 edges.
And of course if we were to continue for a tree T4 the argument would be exactly the
same. As it would be for T5 , T6 , and so on. But instead of continuing forever we assume
that trees Tn with n vertices have n − 1 edges. We then do a step called an inductive step
where we use this to prove that a tree Tn+1 has n + 1 vertices and n edges. We now state
the inductive step.
Suppose we have a tree Tn+1 with vertices v1 , . . . , vn+1 . Since trees are connected we
know that there is an edge e connecting vn+1 with some other vertex. Remove vn+1 and e.
We are left with a tree that has n vertices. By the assumption we know that this tree has
n vertices and n − 1 edges. Now add back vn+1 and e to get back Tn+1 , which clearly has
n + 1 vertices and n edges.
Since this inductive step works for any n we have proved the theorem. t u
A rooted tree is a tree where one special vertex is called the root. Rooted trees are
usually drawn with the root at the top and the rest of the tree “hanging” from the root.
The vertex directly above a given vertex is called the parent of the given vertex. Similarly,
the vertex directly below a given vertex is called the child of the given vertex. Recall-
ing that leaves are vertices with degree one, leaves on rooted trees turn out to be vertices
with no children. A strict binary tree is a rooted tree such that every vertex which is
not a leaf has exactly two children. A binary tree is a rooted tree such that every vertex
which is not a leaf has at most two children. This means it is possible to have only one child.
Example 10.3
A rooted tree.
root
A H
B E I M
C F G J K L N
D O P
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.
198 Discrete Mathematics for Computer Science
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 difficult to find 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.
A B A B A B A B
E E E E
C D C D C D C D
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.
Trees 199
Example 10.6
C C C
B E B E B E
A D A D A D
A D
C C C
C
B E B E B E
B E
A D A D A D
C C C
B E B E B E
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 first 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 first 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.
A B
C D
12 10
7
7 8
B D F
9
3 11 2
5
A C
Figure 10.1 We want to find a minimal spanning tree for this weighted graph.
(a) We look for the first 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 3 , AC 5 , and AD9 . Clearly AB 3 is the edge with the minimal weight.
The vertex incident to AB 3 which is not in T is clearly B. Thus for steps 3.1–4 we
have the following assignments,
e ←− AB 3 ,
v ←− B,
T ←− {A} ∪ {AB 3 , B},
unused edges ←− E − {AB 3 }.
(b) Next we look for the first 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 5 , AD9 , BD7 ,
and BE 12 . Among these edges AC 5 has the minimal weight. So for steps 3.1–4 we are
left with the assignments
e ←− AC 5 ,
v ←− C,
T ←− {A, AB 3 , B} ∪ {AC 5 , C},
unused edges ←− AD9 , BD7 , BE 12 , CD11 , CF 2 , DE 7 , DF 8 , EF 10 .
(c) Next we again look for the first 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 AD9 , BD7 ,
BE 12 , CD11 , and CF 2 . Among these edges the one with the minimal weight is CF 2 .
So for steps 3.1–4 we have the assignments
e ←− CF 2 ,
v ←− F,
T ←− {A, AB 3 , B, AC 5 , C} ∪ {CF 2 , F },
unused edges ←− AD9 , BD7 , BE 12 , CD11 , DE 7 , DF 8 , EF 10 .
(d) Next we again look for the first 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 AD9 , BD7 ,
BE 12 , CD11 , DF 8 , and EF 10 . The edge with the minimal wight is BD7 . So for steps
3.1–4 we have the assignments
e ←− BD7 ,
v ←− D,
T ←− {A, AB 3 , B, AC 5 , C, CF 2 , F } ∪ {BD7 , D},
unused edges ←− AD9 , BE 12 , CD11 , DE 7 , DF 8 , EF 10 .
(e) Next we again look for the first 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 12 ,
DE 7 , and EF 10 . The edge with the minimal weight is DE 7 . So for steps 3.1–4 we
have the assignments
e ←− DE 7 ,
v ←− E,
T ←− {A, AB 3 , B, AC 5 , C, CF 2 , F, BD7 , D} ∪ {DE 7 , E},
unused edges ←− AD9 , BE 12 , CD11 , DF 8 , EF 10 .
202 Discrete Mathematics for Computer Science
12 10
7
7 8
B D F
9
3 11 2
5
A C
7
B D F
3 2
5
A C
The weighted graph (top) with its minimal spanning tree (bottom) found
Figure 10.2
using Prim’s algorithm.
At this point we have gone through the for-do loop five times and we now move to step
4 which outputs the tree
12 10
7
7 8
B D F
9
3 11 2
5
A C
12
B D F
9
3 2
5
A C
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.
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 find 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 , AD9 .
We now have to calculate the value d(A, A) + weight(e) for each of these three edges
to find out which is the minimum. (If two values are equal we simply choose the first
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(AD9 ) = 0 + 9 = 9.
v ←− B,
T ←− {A} ∪ {AB 3 , B} = {A, AB 3 , B},
d(A, B) ←− d(A, A) + weight(AB 3 ) = 3.
(b) Now we look for the first vertex w in T where d(A, w)+weight(e) is minimized. There
are now two possibilities for w, namely vertices A and B. We now look among the
edges in G −T that are incident to only one of the vertices A or B. We list the possible
edges,
AC 5 , AD9 , BD7 , 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(AD9 ) = 0 + 9 = 9,
d(A, B) + weight(BD7 ) = 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 5 . We now make the rest of the assignments in steps
3.3–5, writing them in a way that is more natural for us,
v = C,
T = {A, AB 3 , B, AC 5 , C},
d(A, C) = d(A, A) + weight(AC 5 ) = 0 + 5 = 5.
Trees 205
(c) Again we look for the first 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,
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(AD9 ) = 0 + 9 = 9,
d(A, B) + weight(BD7 ) = 3 + 7 = 10,
d(A, B) + weight(BE 12 ) = 3 + 12 = 15,
d(A, C) + weight(CD11 ) = 5 + 11 = 16,
d(A, C) + weight(CF 2 ) = 5 + 2 = 7.
v = F,
T = {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 first 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,
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(AD9 ) = 0 + 9 = 9,
d(A, B) + weight(BD7 ) = 3 + 7 = 10,
d(A, B) + weight(BE 12 ) = 3 + 12 = 15,
d(A, C) + weight(CD11 ) = 5 + 11 = 16,
d(A, F ) + weight(DF 8 ) = 7 + 8 = 15,
d(A, F ) + weight(EF 10 ) = 7 + 10 = 17.
v = D,
T = {A, AB 3 , B, AC 5 , C, CF 2 , F, AD9 , D},
d(A, D) = d(A, A) + weight(AD9 ) = 0 + 9 = 9.
206 Discrete Mathematics for Computer Science
(e) Again we look for the first vertex w in T where d(A, w) + weight(e) is minimized.
There are now five 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.
At this point we have gone through the for-do loop five times and we now move to step
4 which outputs the tree
T = {A, AB 3 , B, AC 5 , C, CF 2 , F, AD9 , D, BE 12 , E}.
See Fig. 10.3.
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 ran-
dom. 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 v1 , v2 , . . . , v20 . How
many paths (with no 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 6= j?
Question 10.5 Suppose you have a tree T with 20 vertices labeled v1 , v2 , . . . , v20 . You
add an edge between v1 and v20 . Does the new graph have a cycle? If so, how many cycles
does it have? Is the new graph still a tree?
Trees 207
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 five and
the other vertices have degree one. Draw the graph.
Question 10.10 For the graphs below answer the following questions.
(a) How many spanning trees does the graph contain?
(b) Which edge or edges must be contained in any spanning tree?
B E B E A G
C E
D F
A C D A C D F B H
Question 10.11 For the graphs below answer the following questions.
(a) How many spanning trees does the graph contain?
(b) Which edge or edges must be contained in any spanning tree?
B D H A C F G B D F
A F G J A
C E I B D E H C E F
Question 10.12 Use Prim’s algorithm to find the minimal tree of the following graph start-
ing at vertex A.
11 A 10
B C
9 5 7
6 D 4
13 8
12
E F
A D
5 3 3
4
5 C F
2 2 4
B E
Question 10.14 Suppose you have five 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 fiber-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 find the least expensive possible
LAN configuration. In other words, we want to find the minimal spanning tree. Use Prim’s
algorithm to find the minimal spanning tree. Assume the edges in the edge set are written
in alphabetical order.
Question 10.15 Use Dijkstra’s algorithm to find the shortest path from vertex A to every
other vertex. Assume the edge set is written in alphabetical order.
7
B D
11 10 13
A 12 15 F
9 8 14
C E
12
Question 10.16 Use Dijkstra’s algorithm to find the shortest path from vertex A to every
other vertex.
A
11 10
B C
5
9 7
6 D 4
13 8
12
E F
APPENDIX A
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
209
210 Discrete Mathematics for Computer Science
Something similar happens when we add binary numbers. First we write down all the
sums of two single digit binary numbers;
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 111
1100 1001
+ 1110 1111
1 1011 1000
• In the first 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 fifth 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.
a bit, which got its name from binary digit by combining the first two letter of binary with
the last two letters of digit. Computers must store data in memory, and in order to both
know where to put data and where to retrieve data, the areas of the computer’s memory
are given addresses. In most modern computers each address area consists of eight bits,
which is called a byte. Thus, each addressable area of a computer’s memory can contain an
eight digit binary number. So a single byte can store the binary numbers 0000 0000 through
1111 1111, which are the decimal numbers 0 through 255. If a binary number is larger than
1111 1111 (as most are) then more than one byte must be used to store the number. This
is not a course on computer architecture so we will not go into any more detail here, but
this is enough to understand why we are interested in adding eight digit binary numbers.
Now we are ready to begin trying to design a circuit to add two eight digit binary
numbers. First we again write down all the sums of two single digit binary numbers, only
now we will write the answer using both a gray carry bit and a black sum bit;
0 + 0 = 00,
0 + 1 = 01,
1 + 0 = 01,
1 + 1 = 10.
As a first 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.
x
sum bit out
y
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
212 Discrete Mathematics for Computer Science
x
carry bit out
y
So if x is the first 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
x
sum bit out
y
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 fine for the first 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.
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
Basic Circuit Design 213
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:
x
y sout
cout
cin
It would be a good idea to trace through this circuit for all of the possible values of x, y,
and cin and check that its outputs match the expected outputs of the additions from the
start of the section.
214 Discrete Mathematics for Computer Science
In the first column (on the far right) we add 0 + x1 + y1 . This addition 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 c1 + x2 + y2 to get a carry digit
c2 and a sum digit s2 . The 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
c7 + x8 + y8 to give us both a carry digit c8 and a sum digit s8 . 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 x1 + y1 , we automatically have the carry digit input
into the first full-adder as zero.
If we wanted to add two sixteen-digit binary numbers then we would put two of these
circuits in a row. and the carry bit output from the first circuit would be the carry bit input
into the second circuit. And of course adding two twenty-four-digit binary numbers would
require three of these circuits in row, and so on.
Basic Circuit Design 215
x1 x5
y1 y5
cin = 0
x2 x6
y2 y6
x3 x7
y3 y7
x4 x8
y4 y8
c s8 s7 s6 s5 s4 s3 s2 s1
Figure A.1 The full circuit for an eight-bit adder. This circuit adds the two eight-big
binary numbers x8 x7 x6 x5 x4 x3 x2 x1 and y8 y7 y6 y5 y4 y3 y2 y1 to obtain the binary number
cs8 s7 s6 s5 s4 s3 s2 s1 . Two sixteen-bit binary numbers could be added together using two
eight-bit adders in a row. The carry bit c would become the input cin for the second
eight-bit adder.
APPENDIX B
Answers to Problems
Question 1.2:
Step x y max Output Step x y max Output
1 6 12 - - 1 7 7 - -
(a) (c)
2.2 6 12 12 - 2.2 7 7 7 -
3 6 12 12 12 3 7 7 7 7
Step x y max Output
1 15 9 - -
(b)
2.1 15 9 15 -
3 15 9 15 15
Question 1.3:
Step price paid change Output
1 74.36 82.50 - -
(a)
2.2 74.36 82.50 8.84 -
3 74.36 82.50 8.84 Change 8.14
Question 1.4:
Step x answer Output Step x answer Output
1 1 - - 1 -3 - -
(a) 2 -1 - - (b) 2 3 - -
3.1 -1 4 - 3.2 3 -2 -
4 -1 4 4 4 3 -2 -2
217
218 Discrete Mathematics for Computer Science
Question 1.5:
Step x answer Output Step x answer Output
1 -6 - - 1 8 - -
(a) (c)
2.1 -6 -9 - 2.2.1 8 -13 -
3 -6 -9 -9 3 8 -13 -13
Step x answer Output
1 2 - -
(b)
2.2.2 2 -7 -
3 2 -7 -7
Question 1.6:
Step x n i answer Output Step x n i answer Output
1 2 4 - - - 1 5 6 - - -
2 2 4 - 2 - 2 5 6 - 5 -
(a) 3.1 2 4 1 4 - 3.1 5 6 1 25 -
3.1 2 4 2 8 - (c) 3.1 5 6 2 125 -
3.1 2 4 3 16 - 3.1 5 6 3 625 -
4 2 4 3 16 16 3.1 5 6 4 3125 -
Step x n i answer Output 3.1 5 6 5 15625 -
1 3 5 - - - 4 5 6 5 15625 15625
2 3 5 - 3 -
3.1 3 5 1 9 -
(b)
3.1 3 5 2 27 -
3.1 3 5 3 81 -
3.1 3 5 4 243 -
4 3 5 4 243 243
Question 1.7:
Step n fac Output Step n fac Output
1 3 - - 1 5 - -
2 3 3 - 2 5 5 -
3.1 2 3 - 3.1 4 5 -
(a)
3.2 2 6 - 3.2 4 20 -
3.1 1 6 - 3.1 3 20 -
(b)
3.2 1 6 - 3.2 3 60 -
4 1 6 6 3.1 2 60 -
3.2 2 120 -
3.1 1 120 -
3.2 1 120 -
4 1 120 120
Answers to Problems 219
Question 1.8:
Step n i fac Output Step n i fac Output
1 3 - - - 1 7 - - -
2 3 - 1 - 2 7 - 1 -
(a) 3.1 3 1 1 - 3.1 7 1 1 -
3.1 3 2 2 - 3.1 7 2 2 -
3.1 3 3 6 - (c) 3.1 7 3 6 -
4 3 3 6 6 3.1 7 4 24 -
Step n i fac Output 3.1 7 5 120 -
1 5 - - - 3.1 7 6 720 -
2 5 - 1 - 3.1 7 7 5040 -
3.1 5 1 1 - 4 7 7 5040 5040
(b) 3.1 5 2 2 -
3.1 5 3 6 -
3.1 5 4 24 -
3.1 5 5 120 -
4 5 5 120 120
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
220 Discrete Mathematics for Computer Science
Question 1.10:
(a) x1 = 2, x2 = 7, x3 = 4, x4 = 3, x5 = 9, x6 = 0, s = 9
Step i xi s Output
2.1 1 2 9 -
2.1 2 7 9 -
2.1 3 4 9 -
2.1 4 3 9 -
2.1 5 9 9 -
2.1.1 5 9 9 String contains 9.
(b) x1 = 7, x2 = 3, x3 = 0, x4 = 2, x5 = 8, x6 = 5, s = 2
Step i xi s Output
2.1 1 7 2 -
2.1 2 3 2 -
2.1 3 0 2 -
2.1 4 2 2 -
2.1.1 4 2 2 String contains 2.
(c) x1 = 9, x2 = 8, x3 = 3, x4 = 6, x5 = 5, x6 = 0, x7 = 3, x8 = 6, x9 = 2, s = 5
Step i xi s Output
2.1 1 9 5 -
2.1 2 8 5 -
2.1 3 3 5 -
2.1 4 6 5 -
2.1 5 5 5 -
2.1.1 5 5 5 String contains 5.
Question 1.11:
(a) x1 = 2, x2 = 1, x3 = 0, x4 = 9, (c) x1 = 3, x2 = 9, x3 = 7, y1 = 4, y2 = 4,
y1 = 4, y2 = 0, y3 = 7, y4 = 1, s = 0 y3 = 3, s = 6
Step i xi yi Output Step i xi yi Output
2.1 1 2 - - 2.1 1 3 - -
2.1 2 1 - - 2.1 2 9 - -
2.1 3 0 - - 2.1 3 7 - -
String one
2.1.1 3 0 - contains 0. 3.1 1 - 4 -
(b) x1 = 5, x2 = 9, x3 = 3, y1 = 7, y2 = 2, 3.1 2 - 4 -
y3 = 1, s = 2 3.1 3 - 7 -
Neither string
Step i xi yi Output 4 3 - - contains 6.
2.1 1 5 - -
2.1 2 9 - -
2.1 3 3 - -
3.1 1 - 7 -
3.1 2 - 2 -
String two
3.1.1 2 - 2 contains 2.
Answers to Problems 221
Question 1.12:
(a) x1 = 2, x2 = 7, x3 = 3, y1 = 4, y2 = 3, (c) x1 = 1, x2 = 2, x3 = 3, y1 = 9, y2 = 8,
y3 = 9, n = 3 y3 = 7, n = 3
Step i j xi yj Output Step i j xi yj Output
2.1.1 1 1 2 4 - 2.1.1 1 1 1 9 -
2.1.1 1 2 2 3 - 2.1.1 1 2 1 8 -
2.1.1 1 3 2 9 - 2.1.1 1 3 1 7 -
2.1.1 2 1 7 4 - 2.1.1 2 1 2 9 -
2.1.1 2 2 7 3 - 2.1.1 2 2 2 8 -
2.1.1 2 3 7 9 - 2.1.1 2 3 2 7 -
2.1.1 3 1 3 4 - 2.1.1 3 1 3 9 -
2.1.1 3 2 3 3 - 2.1.1 3 2 3 8 -
There is an
2.1.1.1 3 2 3 3 element common 2.1.1 3 3 3 7 -
to both strings. There is no
(b) x1 = 5, x2 = 9, x3 = 3, y1 = 7, y2 = 2, 3 3 3 3 7 element common
to both strings.
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 -
There is an
2.1.1 1 3 5 5 element common
to both strings.
Question 1.13:
(a) x1 = 6, x2 = 3, x3 = 5, x4 = 2, x5 = 4 (c) x1 = 5, x2 = 6, x3 = 3, x4 = 7, x5 = 3,
Step i j xi xj Output x6 = 2
2.1.1 1 2 6 3 - Step i j xi xj Output
2.1.1 1 3 6 5 - 2.1.1 1 2 5 6 -
2.1.1 1 4 6 2 - 2.1.1 1 3 5 3 -
2.1.1 1 5 6 4 - 2.1.1 1 4 5 7 -
2.1.1 2 3 3 5 - 2.1.1 1 5 5 3 -
2.1.1 2 4 3 2 - 2.1.1 1 6 5 1 -
2.1.1 2 5 3 4 - 2.1.1 2 3 6 3 -
2.1.1 3 4 5 2 - 2.1.1 2 4 6 7 -
2.1.1 3 5 5 4 - 2.1.1 2 5 6 3 -
2.1.1 4 5 2 4 - 2.1.1 2 6 6 2 -
There are no duplicate 2.1.1 3 4 3 7 -
3 4 5 2 4 integer in the string.
(b) x1 = 4, x2 = 7, x3 = 3, x4 = 7, x5 = 2, 2.1.1 3 5 3 3 -
There is a
x6 = 9, x7 = 1, x8 = 6 2.1.1.1 3 5 3 3 duplicate integer
in the string.
Step i j xi xj Output
2.1.1 1 2 4 7 -
2.1.1 1 3 4 3 -
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 -
There is a duplicate
2.1.1.1 2 4 7 7 integer in the string.
222 Discrete Mathematics for Computer Science
Question 1.14:
Step i sum Output Step i sum Output
2 - 0 - 2 - 0 -
3.1 1 1 - 3.1 1 1 -
(a)
3.1 2 5 - 3.1 2 5 -
3.1 3 14 - (c) 3.1 3 14 -
4 3 14 14 3.1 4 30 -
Step i sum Output 3.1 5 55 -
2 - 0 - 3.1 6 91 -
3.1 1 1 - 4 6 91 91
(b) 3.1 2 5 -
3.1 3 14 -
3.1 4 30 -
4 4 30 30
Question 1.15:
Step n order Output Step n order Output
1 12 - - 1 80 - -
2 12 0 - 2 80 0 -
3.1 6 0 - 3.1 40 0 -
(a)
3.2 6 1 - 3.2 40 1 -
3.1 3 1 - 3.1 20 1 -
(c)
3.2 3 2 - 3.2 20 2 -
4 3 2 2 3.1 10 2 -
Step n order Output 3.2 10 3 -
1 90 - - 3.1 5 3 -
2 90 0 - 3.2 5 4 -
(b) 4 5 4 4
3.1 45 0 -
3.2 45 1 -
4 45 1 1
Question 1.16:
(a) x1 = 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
Answers to Problems 223
(b) x1 = 3, x2 = 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) x1 = 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 -
3.1 0 4 6 3 -
3.1 0 4 7 1 -
4 0 4 7 1 0,4
Question 1.17:
(a) x1 = 0, x2 = 4, x3 = m, x4 = 3, x5 = s, x7 = 8
Step i xi noninteger
detected 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 -
String contains
5.1 4 m true non-integer character.
(b) x1 = 8, x2 = 9, x3 = 0
Step i xi noninteger
detected 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 -
String consists
5.2 3 0 false entierly of integers.
224 Discrete Mathematics for Computer Science
(c) x1 = 7, x2 = 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.
Question 2.2:
(a) 0110 10012 = 10510 (f) 0110 10002 = 10410
(b) 1011 11002 = 18810 (g) 1011 01102 = 18210
(c) 0011 01112 = 5510
(d) 1111 01012 = 24510 (h) 0010 10112 = 4310
(e) 1001 01102 = 15010 (i) 1101 11012 = 22110
Question 2.3:
(a) 278 = 2310 (f) 3608 = 24010
(b) 428 = 3410 (g) 47218 = 251310
(c) 358 = 2910
(d) 1038 = 6710 (h) 24518 = 132110
(e) 6738 = 44310 (i) 77158 = 404510
Answers to Problems 225
Question 2.4:
(a) 72548 = 375610 (f) 111018 = 467310
(b) 16028 = 89810 (g) 3653108 = 12564010
(c) 46408 = 246410
(d) 175308 = 802410 (h) 7725318 = 25941710
(e) 725018 = 3001710 (i) 4175248 = 13909210
Question 2.5:
(a) A316 = 16310 (f) AED16 = 279710
(b) 9F16 = 15910 (g) F 00116 = 6144110
(c) 1716 = 2310
(d) 3D216 = 97810 (h) 6D2716 = 2794310
(e) 9A016 = 246410 (i) 39CB16 = 1479510
Question 2.6:
(a) 80216 = 205010 (f) 110116 = 435310
(b) 6A316 = 169910 (g) ABCDE16 = 70371010
(c) F 2E16 = 388610
(d) 290B16 = 1050710 (h) 2E91616 = 19074210
(e) 4C7116 = 1956910 (i) 97CA016 = 62172810
Question 2.7:
(a) 0.112 = 0.7510 (f) 0.0112 = 0.37510
(b) 0.012 = 0.2510 (g) 0.01112 = 0.437510
(c) 0.102 = 0.510
(d) 0.1102 = 0.7510 (h) 0.00112 = 0.187510
(e) 0.1012 = 0.62510 (i) 0.10012 = 0.562510
Question 2.8:
(a) 100.1102 = 4.7510 (f) 1011.10012 = 11.562510
(b) 110.0112 = 6.37510 (g) 1111.11112 = 15.937510
(c) 111.0012 = 7.12510
(d) 1101.10002 = 13.510 (h) 0001.00012 = 1.062510
(e) 0110.01112 = 6.437510 (i) 1100.01012 = 12.312510
Question 2.9:
(a) 0.48 = 0.510 (f) 0.148 = 0.187510
(b) 0.18 = 0.12510 (g) 0.5538 = 0.70898437510
(c) 0.78 = 0.87510
(d) 0.728 = 0.9062510 (h) 0.4078 = 0.51367187510
(e) 0.038 = 0.04687510 (i) 0.3218 = 0.40820312510
Question 2.10:
(a) 7.58 = 7.62510 (f) 25.258 = 21.32812510
(b) 4.28 = 4.2510 (g) 427.0148 = 279.023437510
(c) 3.68 = 3.7510
(d) 37.638 = 31.79687510 (h) 510.4428 = 328.5664062510
(e) 56.128 = 46.1562510 (i) 703.6358 = 451.80664062510
226 Discrete Mathematics for Computer Science
Question 2.11:
(a) 0.716 = 0.437510 (f) 0.8216 = 0.507812510
(b) 0.A16 = 0.62510 (g) 0.C4816 = 0.76757812510
(c) 0.316 = 0.187510
(d) 0.A816 = 0.6562510 (h) 0.37916 = 0.217041015610
(e) 0.4C16 = 0.29587510 (i) 0.ABC16 = 0.670898437510
Question 2.12:
(a) 9.316 = 9.187510 (f) 5E.C516 = 94.7695312510
(b) B.E16 = 11.87510 (g) A04.BB816 = 2564.73242187510
(c) 4.C16 = 4.7510
(d) AC.DC16 = 172.85937510 (h) 7CF.9F 016 = 1999.6210937510
(e) F 2.3916 = 242.2226562510 (i) 101.10116 = 257.0627441406310
Question 2.13:
(a) 628 = 1100102 (f) 72.378 = 111010.0111112
(b) 318 = 110012 (g) 313.0118 = 11001011.0000010012
(c) 5718 = 1011110012
(d) 31.728 = 11001.111012 (h) 643.0268 = 110100011.000010112
(e) 42.158 = 100010.0011012 (i) 211.3618 = 10001001.0111100012
Question 2.14:
(a) A016 = 101000002 (f) 88.7C16 = 10001000.0111112
(b) 3B16 = 1110112 (g) 214.3C516 = 1000010100.0011110001012
(c) 2716 = 1001112
(d) F 2.F 216 = 11110010.11110012 (h) AD2.0BC16 = 101011010010.00001011112
(e) 5B.9316 = 1011011.100100112 (i) 101.01116 = 100000001.0000000100012
Question 2.15:
(a) 102 = 28 (f) 101.0012 = 5.18
(b) 1102 = 68 (g) 11011.0010012 = 33.118
(c) 112 = 38
(d) 110.1112 = 6.78 (h) 100011.101110012 = 43.5628
(e) 011.0102 = 3.28 (i) 11110001.000111012 = 361.0728
Question 2.16:
(a) 11012 = D16 (f) 0101.00112 = 5.316
(b) 102 = 216 (g) 111001011.00110112 = 1CB.3616
(c) 1012 = 516
(d) 1110.11012 = E.D16 (h) 1011101.010110101012 = 5D.5AA16
(e) 1011.10102 = B.A16 (i) 10111100101.1001112 = 5E5.9C16
Question 2.17:
(a) 710 = 1112 (f) 7910 = 10011112
(b) 310 = 112 (g) 38110 = 1011111012
(c) 610 = 1102
(d) 2810 = 111002 (h) 64310 = 10100000112
(e) 3910 = 1001112 (i) 56910 = 10001110012
Answers to Problems 227
Question 2.18:
(a) 7810 = 1168 (f) 64110 = 12018
(b) 9310 = 1358 (g) 486210 = 113768
(c) 5210 = 648
(d) 29510 = 4478 (h) 507810 = 117268
(e) 94410 = 16608 (i) 153210 = 27748
Question 2.19:
(a) 3410 = 2216 (f) 832110 = 208116
(b) 2310 = 1716 (g) 9086710 = 162F 316
(c) 53610 = 21816
(d) 28510 = 11D16 (h) 4277810 = A71A16
(e) 189710 = 76916 (i) 2871410 = 702A16
Question 2.20:
(a) 0.510 = 0.12 (f) 0.7710 ≈ 0.1100012
(b) 0.410 ≈ 0.0110012 (g) 0.23910 ≈ 0.0011112
(c) 0.810 ≈ 0.1100112
(d) 0.3210 ≈ 0.0101002 (h) 0.55210 ≈ 0.1000112
(e) 0.8110 ≈ 0.1100112 (i) 0.79810 ≈ 0.1100112
Question 2.21:
(a) 0.910 ≈ 0.7146318 (f) 0.4410 ≈ 0.3412178
(b) 0.410 ≈ 0.3146318 (g) 0.48210 ≈ 0.3666218
(c) 0.610 ≈ 0.4631468
(d) 0.8310 ≈ 0.6507538 (h) 0.66710 ≈ 0.5254028
(e) 0.2510 ≈ 0.28 (i) 0.31510 ≈ 0.2412178
Question 2.22:
(a) 0.110 ≈ 0.19999916 (f) 0.4710 ≈ 0.7851EB16
(b) 0.310 ≈ 0.4CCCCC16 (g) 0.42910 ≈ 0.6DD2F 116
(c) 0.910 ≈ 0.E6666616
(d) 0.1110 ≈ 0.1C28F 516 (h) 0.63810 ≈ 0.A353F 716
(e) 0.8310 ≈ 0.D47AE116 (i) 0.31410 ≈ 0.50624D16
Question 3.2:
(a) Jon plays baseball or Ron plays football.
(b) Ron plays football and Jon plays baseball.
(c) Jon does not play baseball and Ron plays football.
(d) Jon plays baseball and Ron does not play football.
(e) Ron does not play football or Jon does not play baseball.
(f) It is not the case that Ron plays football or Jon plays baseball.
228 Discrete Mathematics for Computer Science
Question 3.3:
(a) p ∧ q ≡ T ∧ F ≡ F (g) p→q≡T →F ≡F
(b) q ∧ p ≡ F ∧ T ≡ F (h) ¬q → p ≡ ¬F → T ≡ T → T ≡ T
(c) ¬q ∨ p ≡ ¬F ∨ T ≡ T ∨ T ≡ T (i) ¬q → ¬p ≡ ¬F → ¬T ≡ T → F ≡ F
(d) ¬p ∨ q ≡ ¬T ∨ F ≡ F ∨ F ≡ F (j) p↔q≡T ↔F ≡F
(e) p ∧ ¬q ≡ T ∧ ¬F ≡ T ∧ T ≡ T (k) ¬q ↔ p ≡ ¬F ↔ T ≡ T ↔ T ≡ T
(f) ¬q ∨ ¬p ≡ ¬F ∨ ¬T ≡ T ∨ F ≡ T (l) ¬p ↔ ¬q ≡ ¬T ↔ ¬F ≡ F ↔ T ≡ F
Question 3.4:
(a) (d) (g) (j)
p q p∧q p q ¬p ∨ q p q p→q p q p↔q
T T T T T T T T T T T T
T F F T F F T F F T F F
F T F F T T F T T F T F
F F F F F T F F T F F T
Question 3.5:
p q p→q p q ¬p ∨ q
T T T T T T
We have T F F and T F F .
F T T F T T
F F 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:
p q p↔q p q p → q q → p (p → q) ∧ (q → p)
T T T T T T T T
We have T F F and T F F T F .
F T F F T T F F
F F T 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).
Answers to Problems 229
Question 3.7:
p q r (p ∨ ¬r) → q p q r (p ∧ ¬q) ∧ (r ∨ q)
T T T T T T T F
T T F T T T F F
T F T F T F T T
(a) T F F F (f) T F F F
F T T T F T T F
F T F T F T F F
F F T T F F T F
F F F F F F F F
p q (q ∧ p) → ¬p
p q r (p ∨ q) ↔ [p ∨ (q ∧ r)]
T T F
(b) T T T T
T F T
T T F T
F T T
T F T T
F F T
(g) T F F T
p q ¬p → (¬p ∨ q) F T T T
T T T F T F F
(c) T F T F F T T
F T T F F F T
F F T
p q r (q ∧ ¬r) ↔ ¬(p ∨ r)
p q r ¬(p ∧ ¬q) ∨ r
T T T T
T T T T
T T F F
T T F T
T F T T
T F T T (h) T F F T
(d) T F F F
F T T T
F T T T
F T F T
F T F T
F F T T
F F T T
F F F F
F F F T
p q r ¬[(¬p ∧ q) ∨ r] p q r (p ↔ q) ∧ (r ↔ q)
T T T F T T T T
T T F T T T F F
T F T F T F T F
(e) T F F T (i) T F F F
F T T F F T T F
F T F F F T F F
F F T F F F T F
F F F T F F F T
Question 3.8:
(a) p ∨ p = p (g) p∧p=p
(b) p ∨ ¬p = T (h) p ∧ (p ∨ q) = p
(c) ¬(p ∨ ¬p) = F (i) p ∧ (p ∨ q ∨ r) = p
(d) p ∨ (p ∧ q) = p (j) (p ∧ q) ∨ (p ∧ ¬q) = p
(e) p ∨ (¬p ∧ q) = p ∨ q (k) ¬(¬p ∨ ¬p) = p
(f) p ∧ (¬p ∨ q) = p ∧ q (l) (p ∧ q) ∨ (¬p ∧ q) = q
230 Discrete Mathematics for Computer Science
Question 3.9:
(a) (¬p ∨ ¬q) ∧ (¬p ∨ q) = ¬p (e) r ∨ [r ∨ (r ∧ p)] = r
(b) q ∨ (q ∧ ¬q) = q (f) p ∧ [p ∨ (p ∧ q)] = p
(c) ¬p ∨ (q ∧ ¬p) = ¬p (g) r ∨ (r ∧ ¬p ∧ q ∧ s) = r
(d) (p ∨ ¬q) ∧ (p ∨ q) = p (h) ¬r ∧ ¬(r ∧ p ∧ q ∧ s) = ¬r
Question 3.10:
(a) q ∧ [p ∨ (¬p ∧ q)] = q (g) [p ∨ (q ∧ r)] ∧ (¬p ∨ r) = (p ∨ q) ∧ r
(b) ¬[(p ∧ ¬q) ∨ ¬p] = p ∧ q (h) (p ∧ ¬r) ∨ (¬p ∧ q) ∨ ¬(p ∧ r) = ¬p ∨ ¬r
(c) (¬p ∧ q) ∨ [p ∧ (p ∨ q)] = p ∨ q (i) (p∧r)∨(¬p∧q)∨(r∧q) = (p∧r)∨(q∧¬p)
(d) ¬[p ∨ (p ∧ q)] ∧ q = ¬p ∧ q (j) ¬p ∨ ¬q ∨ [p ∧ q ∧ ¬r] = ¬p ∨ ¬q ∨ ¬r
(e) (p∧¬r)∨(¬p∧q)∨¬(q∧r) = ¬p∨¬q∨¬r (k) (p∨r)∧(¬p∨q)∧(r∨q) = (p∧q)∨(r∧¬p)
(f) ¬(p ∧ q) ∨ (q ∧ r) = ¬p ∨ ¬q ∨ r (l) ¬(p ∧ q) ∧ (¬p ∨ q) ∧ (¬q ∨ q) = ¬p
Question 3.11:
(a) tautology (c) tautology
(b) not a tautology (d) not a tautology
Question 3.12
(a) contradiction (c) not a contradiction
(b) not a contradiction (d) contradiction
Question 4.2:
(a) True (g) False (m) False (s) False
(b) True (h) True (n) True (t) True
(c) False (i) True (o) False (u) False
(d) False (j) True (p) False (v) False
(e) True (k) False (q) False (w) False
(f) True (l) True (r) True (x) True
Question 4.3:
(a) True (f) True (k) True (p) True
(b) True (g) False (l) True (q) True
(c) False (h) True (m) True (r) True
(d) True (i) True (n) True (s) False
(e) False (j) True (o) False (t) True
Answers to Problems 231
Question 4.4:
(a) { }, {a} (g) { }, {2}, {4}, {6}, {2, 4},
(b) { }, {5} {2, 6}, {4, 6}, {2, 4, 6}
(c) { }, {v} (h) { }, {x}, {y}, {z}, {x, y},
(d) { }, {a}, {b}, {a, b} {x, z}, {y, z}, {x, y, z}
(e) { }, {5}, {7}, {5, 7} (i) { }, {2}, {b}, {c}, {2, b},
(f) { }, {5}, {e}{5, e} {2, c}, {b, c}, {2, b, c}
Question
n o 4.5:
n o no n o n o n o n o
(a) , {a, b, c} (g) , 2 , {4} , 6 , 2, {4} ,
no n o n o n o n o
(b) , {x, y} 2, 6 , {4}, 6 , 2, {4}, 6
no n o no n o n o n o n o
(c) , {1, 2, 3} (h) , {2} , {4} , {6} , {2}, {4} ,
no n o n o n o n o n o n o
(d) , r , {s, t} , r, {s, t} {2}, {6} , {4}, {6} , {2}, {4}, {6}
no n o n o n o no n o n o n o n o
(e) , {x} , {y, z} , {x}, {y, z} (i) , u , v , {w} , u, v ,
no n o n o n o n o n o n o
(f) , {2, 4} , 6 , {2, 4}, 6 u, {w} , v, {w} , u, v, {w}
Question 4.6:
(a) {2, 3, 4, 5, 6, 7, 8} (e) {0, 1, 9} (i) {2, 3}
(b) {4, 5, 6} (f) {0, 1, 2, 3, 7, 8, 9} (j) {7, 8}
(c) {0, 1, 7, 8, 9} (g) {0, 1, 2, 3, 7, 8, 9}
(d) {0, 1, 2, 3, 9} (h) {0, 1, 9}
Question 4.7:
(a) {1, 2, 3, 4, a, b, c, d, e} (e) {5} (i) {1, a, b}
(b) {2, c, d} (f) {1, 3, 4, 5, a, b, e} (j) {3, 4, e}
(c) {3, 4, 5, e} (g) {1, 3, 4, 5, a, b, e}
(d) {1, 5, a, b} (h) {5}
Question 4.8:
(a) {o, p, r, s, t, u, v, y} (e) {q, w, x, z} (i) {o, y}
(b) {p, s, t, u} (f) {o, q, r, v, w, x, y, z} (j) {r, v}
(c) {q, r, v, w, x, z} (g) {o, q, r, v, w, x, y, z}
(d) {o, q, w, x, y, z} (h) {q, w, x, z}
Question 4.9:
(a) {−2, 2} (d) {3} (g) {−2, 2}
(b) {−1, 1} (e) {−5, 5} (h) {−3, 2}
(c) {−4, 5} (f) {−3, 4} (i) {0}
Question 4.10:
(a) {−1} (d) {−2, 2, 3} (g) {0}
(b) −7 3
(e) −7
(h) 43 , 23
4 , 2
−5 3 ,5
−4
(c) 2 , 3 (f) {1} (i) {−5, −3, 3}
232 Discrete Mathematics for Computer Science
Question 4.11:
(a) |Ac | = 100 (g) |A ∪ B| = ∞
(b) |B c | = ∞ (h) |B ∩ E| = 150
(c) |Ac ∩ B c | = 0 (i) |Ac ∩ O| = 50
(d) |Oc | = ∞ (j) |O ∩ E c | = ∞
(e) |O ∩ E| = 0 (k) |A ∩ Oc | = ∞
(f) |E c | = ∞ (l) |B ∪ E| = ∞
Question 4.16: A × B = {(a, 1), (a, 2), (a, 3), (b, 1), (b, 2), (b, 3), (c, 1), (c, 2), (c, 3)}
Question 4.17: S × T = {(e, 9), (e, 8), (e, 7), (e, 6), (f, 9), (f, 8), (f, 7), (f, 6),
(g, 9), (g, 8), (g, 7), (g, 6), (h, 9), (h, 8), (h, 7), (h, 6)}
Question 4.18: X × Y = {(0, t), (1, t), (2, t), (3, t), (4, t), (5, t),
(0, f ), (1, f ), (2, f ), (3, f ), (4, f ), (5, f )}
Question 4.19: X1 × X2 × X3 × X4 = {(0, 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), (0, 1, 1, 1),
(1, 0, 0, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 1, 1),
(1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0), (1, 1, 1, 1)}
Question 4.20:
A = {e, m, a, x, j, c, w} (A ∪ B)c = {g, t, d, o}
B = {j, c, w, k, i, b, v} (A ∩ B)c = {g, t, d, o, e, m, a, x, k, i, b, v}
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}
A ∩ B = {j, c, w} A ∪ B c = {g, t, d, o, e, m, a, x, j, c, w}
Ac = {g, t, d, o, k, i, b, v} A − B = {e, m, a, x}
B c = {g, t, d, o, e, m, a, x} B − A = {k, i, b, v}
Question 4.21:
A = {g, i, d, t, b} (A ∪ B)c = {m, j, z, c, l}
B = {t, b, p, w, v} (A ∩ B)c = {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}
A ∩ B = {t, b} A ∪ B c = {m, j, z, c, l, g, i, d, t, b}
Ac = {m, j, z, c, l, p, w, v} A − B = {g, i, d}
B c = {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}
Answers to Problems 233
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}
B c = {x, f, y, k, j, c, z, g, n, v, o, a}
C c = {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}
B c = {e, x, f, r, h, b, z, p, i, j, m, n, g, v, a, q, u}
C c = {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 ∩ C)c = {e, x, f, r, h, b, z, p, i, j, m, t, c, y, w, k, s, o, d, v, a, q, u}
(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}
(A ∪ B)c = {e, x, f, r, h, b, z, v, a, q, u}
(A ∪ C)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}
234 Discrete Mathematics for Computer Science
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:
|A| = 27 |A ∪ B ∪ C| = 57 |(A ∩ B ∩ C)c | = 69
|B| = 40 |Ac | = 47 |(A ∪ B ∪ C)c | = 17
|C| = 26 |B c | = 34 |A − B| = 13
|A ∩ B| = 14 |C c | = 48 |B − A| = 26
|A ∩ C| = 11 |(A ∩ B)c | = 60 |A − C| = 16
|B ∩ C| = 16 |(A ∩ C)c | = 63 |C − A| = 15
|A ∪ B| = 53 |(B ∩ C)c | = 58 |B − C| = 24
|A ∪ C| = 42 |(A ∪ B)c | = 21 |C − B| = 10
|B ∪ C| = 50 |(A ∪ C)c | = 32
|A ∩ B ∩ C| = 5 |(B ∪ C)c | = 24
Question 4.25:
(a) A ∪ A = A (g) A∩A=A
(b) A ∪ Ac = U (h) A ∩ (A ∪ B) = A
(c) (A ∪ Ac )c = ∅ (i) A ∩ (A ∪ B ∪ C) = A
(d) A ∪ (A ∩ B) = A (j) (A ∩ B) ∪ (A ∩ B c ) = A
(e) A ∪ (Ac ∩ B) = A ∪ B (k) (Ac ∪ Ac )c = A
(f) A ∩ (Ac ∪ B) = A ∩ B (l) (A ∩ B) ∪ (Ac ∩ B) = B
Question 4.26:
(a) (Ac ∪ B c ) ∩ (Ac ∪ B) = Ac (e) C ∪ [C ∪ (C ∩ A)] = C
(b) B ∪ (B ∩ B c ) = B (f) A ∩ [A ∪ (A ∩ B)] = A
(c) Ac ∪ (B ∩ Ac ) = Ac (g) C ∪ (C ∩ Ac ∩ B ∩ D) = C
(d) (A ∪ B c ) ∩ (A ∪ B) = A (h) C c ∩ (C ∩ A ∩ B ∩ D)c = C c
Question 4.27:
(a) B ∩ [A ∪ (Ac ∩ B)] = B (h) (A∩C c )∪(Ac ∩B)∪(A∩C)c = Ac ∪C c
(b) [(A ∩ B c ) ∪ Ac ]c = A ∩ B (i) (A ∩ C) ∪ (Ac ∩ B) ∪ (C ∩ B) = (A ∩
(c) (Ac ∩ B) ∪ [A ∩ (A ∪ B)] = A ∪ B C) ∪ (B ∩ Ac )
(d) [A ∪ (A ∩ B)]c ∩ B = Ac ∩ B
(j) Ac ∪ B c ∪ [A ∩ B ∩ C c ] = Ac ∪ B c ∪ C c
(e) (A ∩ C c ) ∪ (Ac ∩ B) ∪ (B ∩ C)c =
Ac ∪ B c ∪ C c (k) (A ∪ C) ∩ (Ac ∪ B) ∩ (C ∪ B) = (A ∩
(f) (A ∩ B)c ∪ (B ∩ C) = Ac ∪ B c ∪ C B) ∪ (C ∩ Ac )
(g) [A ∪ (B ∩ C)] ∩ (Ac ∪ C) = (A ∪ B) ∩ C (l) (A ∩ B)c ∩ (Ac ∪ B) ∩ (B c ∪ B) = Ac
Question 4.28:
(a) R = {(1, 1), (2, 2), (3, 3), (4, 4)}
(b) R = {(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)}
(c) R = {(2, 1), (3, 1), (3, 2), (4, 1), (4, 2), (4, 3)}
(d) R = {(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)}
(e) R = {(1, 1), (2, 1), (2, 2), (3, 1), (3, 2), (3, 3), (4, 1), (4, 2), (4, 3), (4, 4)}
(f) R = {(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)}
Answers to Problems 235
Question 4.29:
(a) R = {(2, 2), (5, 5)}
(b) R = {(0, 1), (0, 2), (0, 4), (0, 5), (0, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (5, 6)}
(c) R = {(2, 1), (3, 1), (3, 2), (5, 1), (5, 2), (5, 4), (7, 1), (7, 2), (7, 4), (7, 5), (7, 6)}
(d) R = {(0, 1), (0, 2), (0, 4), (0, 5), (0, 6), (2, 2), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (5, 5), (5, 6)}
(e) R = {(2, 1), (2, 2), (3, 1), (3, 2), (5, 1), (5, 2), (5, 4), (5, 5)(7, 1), (7, 2), (7, 4), (7, 5), (7, 6)}
(f) R = {(0, 1), (0, 2), (0, 4), (0, 5), (0, 6), (2, 1), (2, 4), (2, 5), (2, 6),
(3, 1), (3, 2), (3, 4), (3, 5), (3, 6), (5, 1), (5, 2), (5, 4), (5, 6), (7, 1), (7, 2), (7, 4), (7, 5), (7, 6)}
Question 4.30: Domain = {1, 3}, Range = {2, 3, 4}, R = {(1, 2), (1, 3), (1, 4), (3, 3), (3, 4)} ⊂
Domain × Range
Question 4.31:
(a) reflexive, antisymmetric, transitive
(b) transitive
(c) reflexive, symmetric, antisymmetric, transitive
(d) irreflexive, symmetric
(e) transitive
(f) reflexive, antisymmetric, transitive
Question 4.32: Similar to Example 4.36.
Question 4.33: Similar to Example 4.37. Equivalence classes are {1, 5}, {2, 4, 6}, {3}.
Question 4.34: This relation is reflexive, symmetric, antisymmetric, and transitive. Thus it
is both a partial ordering and an equivalence relation.
Question 5.2:
(a) (x0 + y 0 )(x0 + y) = x0 (e) w + [w + (wx)] = w
(b) y + (yy 0 ) = y (f) x[x + (xy)] = x
(c) x0 + yx0 = x0 (g) w + (wx0 yz) = w
(d) (x + y 0 )(x + y) = x (h) w0 (wxyz)0 = w0
236 Discrete Mathematics for Computer Science
Question 5.3:
(a) y[x + (x0 y)] = y (g) [x + (yz)](x0 + z) = (x + y)z
(b) [(xy 0 ) + x0 ]0 = xy (h) xz 0 + x0 y + (xz)0 = x0 + z 0
(c) x0 y + x(x + y) = x + y (i) xz + x0 y + zy = xz + x0 y
(d) (x + xy)0 y = x0 y (j) x0 + y 0 + xyz 0 = x0 + y 0 + z 0
(e) xz 0 + x0 y + (yz)0 = x0 + y 0 + z 0 (k) (x + z)(x0 + y)(z + y) = xy + x0 z
(f) (xy)0 + (yz) = x0 + y 0 + z (l) (xy)0 (x0 + y)(y 0 + y) = x0
Question 5.4:
(a) x0 y (c) (xy) + z 0 or xy + z 0 (e) (x + y) + z 0 or x + y + z 0
(b) x0 + y (d) (x + y)z 0 (f) (xy)z 0 or xyz 0
Question 5.5:
(a) (xy 0 ) + y or xy 0 + y (c) (x + y 0 ) + y or x + y 0 + y
(b) (x + y 0 )y (d) (xy 0 )y or xy 0 y
Question 5.6:
(a) [(x + y)z]0 (c) [(xy)z]0 or (xyz)0
(b) [(xy) + z]0 or (xy + z)0 (d) [(x + y) + z]; or (x + y + z)0
Question 5.7:
(a) (z 0 x) + y or z 0 x + y (c) (z 0 x)y or z 0 xy
(b) (z 0 + x)y (d) (z 0 + x) + y or z 0 + x + y
Question 5.11:
(a) x0 y 0 + xy 0 and (x + y 0 )(x0 + y 0 ) (f) xy and (x + y)(x + y 0 )(x0 + y)
(b) x0 y 0 + x0 y and (x0 + y)(x0 + y 0 ) (g) x0 y 0 + x0 y + xy 0 and x0 + y 0
(c) x0 y and (x + y)(x0 + y)(x0 + y 0 )
(d) xy 0 + xy and (x + y)(x + y 0 ) (h) x0 y 0 + xy 0 + xy and x + y 0
(e) x0 y 0 + xy and (x + y 0 )(x0 + y) (i) x0 y + xy and (x + y)(x0 + y)
Question 5.12:
(a) x0 yz 0 + xy 0 z + xyz and
(x + y + z)(x + y + z 0 )(x + y 0 + z 0 )(x0 + y + z)(x0 + y 0 + z)
(b) x0 y 0 z 0 + x0 yz 0 + x0 yz + xy 0 z 0 + xyz 0 and
(x + y + z 0 )(x0 + y + z 0 )(x0 + y 0 + z 0 )
(c) x0 y 0 z 0 + x0 yz 0 + xy 0 z + xyz 0 and
(x + y + z 0 )(x + y 0 + z 0 )(x0 + y + z)(x0 + y 0 + z 0 )
(d) x0 y 0 z 0 + x0 yz + xyz 0 and
(x + y + z 0 )(x + y 0 + z)(x0 + y + z)(x0 + y + z 0 )(x0 + y 0 + z 0 )
(e) x0 y 0 z 0 + x0 y 0 z + xy 0 z + xyz 0 + xyz and
(x + y 0 + z)(x + y 0 + z 0 )(x0 + y + z)
(f) x0 y 0 z 0 + x0 y 0 z + x0 yz + xyx and
(x + y 0 + z)(x0 + y + z)(x0 + y + z 0 )(x0 + y 0 + z)
Answers to Problems 237
Question 5.13:
x y (x + y 0 )x x y (x + y 0 )(x0 + y) x y x0 y 0 + y 0 (x + y)
0 0 0 0 0 1 0 0 1
(b) 0 1 0 (f) 0 1 0 (j) 0 1 0
1 0 1 1 0 0 1 0 1
1 1 1 1 1 1 1 1 0
Question 5.14:
x y z x(x + y 0 + z) x y z (x + y)(x + z)
0 0 0 0 0 0 0 0
0 0 1 0 0 0 1 0
0 1 0 0 0 1 0 0
(a) 0 1 1 0 (c) 0 1 1 1
1 0 0 1 1 0 0 1
1 0 1 1 1 0 1 1
1 1 0 1 1 1 0 1
1 1 1 1 1 1 1 1
x y z x0 (x + y 0 + z) x y z (x0 + y 0 )(x + z)
0 0 0 1 0 0 0 0
0 0 1 1 0 0 1 1
0 1 0 0 0 1 0 0
(b) 0 1 1 1 (d) 0 1 1 1
1 0 0 0 1 0 0 1
1 0 1 0 1 0 1 1
1 1 0 0 1 1 0 0
1 1 1 0 1 1 1 0
238 Discrete Mathematics for Computer Science
x y z x0 y 0 + z 0 (x + y) x y z xy(x + y + z)
0 0 0 1 0 0 0 0
0 0 1 1 0 0 1 0
0 1 0 1 0 1 0 0
(e) 0 1 1 0 (f) 0 1 1 0
1 0 0 1 1 0 0 0
1 0 1 0 1 0 1 0
1 1 0 1 1 1 0 1
1 1 1 0 1 1 1 1
X Y X Y
(d) {(1, c), (2, b), (3, c), (4, e)} ⊂ X × Y (d) {(1, b), (2, b), (3, d), (4, d)} ⊂ X × Y
For function f2 :
(a) Range: R = {b, c, d, e}
(b) One-to-one but not onto onto
(c) •a
1•
•b
2•
•c
3•
•d
4•
•e
X Y
(d) {(1, c), (2, d), (3, e), (4, b)} ⊂ X × Y
240 Discrete Mathematics for Computer Science
Question 6.3:
For function f1 :
(a) Range: R = {a, b, c, d, e, f }
(b) Onto but not one-to-one
(c)
1•
•a
2•
•b
3•
•c
4•
•d
5•
•e
6•
•f
7•
X Y
(d) {(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•
•b
3•
•c
4•
•d
5•
•e
6•
•f
7•
X Y
(d) {(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•
•a
2•
•b
3•
•c
4•
•d
5•
•e
6•
•f
7•
X Y
(d) {(1, b), (2, c), (3, d), (4, e), (5, d), (6, c), (7, b)} ⊂ X ×Y
Answers to Problems 241
Question 6.4:
For function f1 :
(a) Range: R = {a, b, c, d}
(b) Neither one-to-one nor onto
(c) 1• •a
2• •b
3• •c
4• •d
5• •e
X Y
(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) 1• •a
2• •b
3• •c
4• •d
5• •e
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) 1• •a
2• •b
3• •c
4• •d
5• •e
X Y
(d) f (1) = a, f (2) = d, f (3) = c, f (4) = b, f (5) = e
242 Discrete Mathematics for Computer Science
Question 6.5:
For function f1 :
(a) Range: R = {a, b, c, d, e, f }
(b) One-to-one and onto
(c) 1• •a
2• •b
3• •c
4• •d
5• •e
6• •f
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) 1• •a
2• •b
3• •c
4• •d
5• •e
6• •f
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) 1• •a
2• •b
3• •c
4• •d
5• •e
6• •f
X Y
(d) f (1) = c, f (2) = d, f (3) = e, f (4) = f , f (5) = a, f (6) = b
Answers to Problems 243
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) −3 • • −3
−2 • • −2
0• •1
2• •2
4• •4
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
(b) −4 • • −4
−3 • • −3
−2 • • −2
0• •0
2• •2
3• •3
4• •4
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):
(a) Domain: X = {−4, −2, 0, 1, 2, 4},
Range: Y = {−4, −3, 2, 3, 4, 5}
(b) −4 • • −4
−2 • • −3
0• •2
1• •3
2• •4
4• •5
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
244 Discrete Mathematics for Computer Science
−2 • • −3
−1 • • −2
0• • −1
1• •0
2• •1
3• •2
4• •3
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:
(a) f (−3) = −2, f (0) = 7, f (4) = 19 (f) f (−3) = 0, f (0) = −6, f (4) = 14
(b) f (−3) = −2, f (0) = 7, f (4) = 19 (g) f (−3) = 48, f (0) = 0, f (4) = −36
(c) f (−3) = 18, f (0) = 6, f (4) = −10
(d) f (−3) = 0.125, f (0) = 1, f (4) = 16 (h) f (−3) = 162, f (0) = 6, f (4) ≈ 0.074
(e) f (−3) = 0, f (0) = 0, f (4) = 28 (i) f (−3) = 64, f (0) = 1, f (4) = 225
Question 6.8:
(a) not a function (d) function, domain: R, range: (−∞, 5]
(b) function, domain: R, range: [−4, ∞) (e) not a function
(c) function, domain: R, range: R (f) function, domain: R, range: [−4, ∞)
Question 6.9:
g ◦ f (1) = g(f (1)) = g(c) = α
g ◦ f (2) = g(f (2)) = g(b) = γ
g ◦ f (3) = g(f (3)) = g(d) = β
g ◦ f (4) = g(f (4)) = g(a) = δ
Question 6.10:
g ◦ f (1) = g(f (1)) = g(c) = 4 f ◦ g(a) = f (g(a)) = f (2) = b
g ◦ f (2) = g(f (2)) = g(b) = 4 f ◦ g(b) = f (g(b)) = f (4) = e
g ◦ f (3) = g(f (3)) = g(a) = 2 f ◦ g(c) = f (g(c)) = f (4) = e
g ◦ f (4) = g(f (4)) = g(e) = 4 f ◦ g(d) = f (g(d)) = f (2) = b
g ◦ f (5) = g(f (5)) = g(d) = 2 f ◦ g(e) = f (g(e)) = f (4) = e
Domain: {1, 2, 3, 4, 5}, Domain: {a, b, c, d, e},
Codomain: {1, 2, 3, 4, 5}, Codomain: {a, b, c, d, e},
Range: {2, 4} Range: {b, e}
Answers to Problems 245
Question 6.11:
For function in Fig. 6.3(a):
f −1 (a) = 3, f −1 (b) = 2, f −1 (c) = 1, f −1 (d) = 5, f −1 (e) = 4
For function in Fig. 6.3(b):
f −1 does not exist
For function in Fig. 6.3(c):
f −1 (a) = 3, f −1 (b) = 4, f −1 (c) = 5, f −1 (d) = 1, f −1 (e) = 1
For function in Fig. 6.3(d):
f −1 does not exist
For function in Fig. 6.3(e):
f −1 does not exist
For function in Fig. 6.3(f):
f −1 (a) = 3, f −1 (b) = 2, f −1 (c) = 4, f −1 (d) = 5, f −1 (e) = 1, f −1 (f ) = 7, f −1 (g) = 6
For function in Fig. 6.3(g):
f −1 does not exist
For function in Fig. 6.3(h):
f −1 (a) = 4, f −1 (b) = 1, f −1 (c) = 2, f −1 (d) = 3, f −1 (e) = 7, f −1 (f ) = 5, f −1 (g) = 6
For function in Fig. 6.3(i):
f −1 does not exist
Question 6.12:
f1−1 and f3−1 do not exist; f2−1 (b) = 4, f2−1 (c) = 1, f2−1 (d) = 2, f2−1 (e) = 3
Question 6.13:
f1−1 , f2−1 , and f3−1 do not exist
Question 6.14:
f1−1 and f2−1 do not exist; f3−1 (a) = 1, f3−1 (b) = 4, f3−1 (c) = 3, f3−1 (d) = 2, f3−1 (e) = 5
Question 6.15:
(a) Yes (b) No (c) Yes (d) No
Question 6.16:
(a) f −1 (x) = x
7 (c) f −1 (x) = 2−x
5 (e) f −1 (x) = (x + 4)2
(b) f −1 (x) = 1
x+3 (d) f −1 (x) = 3x−10
8 (f) f −1 (x) = x2 + 3
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.
32!
Question 7.4: P332 = 29! = 32 × 31 × 30 = 29 760
20!
Question 7.5: P520 = 15! = 20 × 19 × 18 × 17 × 16 = 1 860 480
20 20!
Question 7.6: 5 = 5!15! = 15 504
246 Discrete Mathematics for Computer Science
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.14: for-do in step 2: n; for-do in step 2.1: n2 ; for-do in step 2.1.2: n3
n
Question 7.15: for-do in step 2: n − 1; while-do in step 2.3: 2 = 0.5n2 − 0.5n
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
Question 8.3: f (n) = 25n + 500 ≤ 25n + 500n = 525n = 525 · g(n)
√
Question 8.4: f (n) = 20 n + 40n2 + 500n log2 (n) ≤ 20n2 + 40n2 + 500n2 = 560n2 so
g(n) = n2
Question 8.5: f (n) = 800n2 +40n3 +50 log2 (n) ≤ 800n3 +40n3 +50n3 = 890n3 so g(n) = n3 .
Question 8.6:
(a) Algorithm one is faster. (e) Algorithm two is faster.
(b) Algorithm two is faster. (f) Algorithm one is faster.
(c) Algorithms are equally fast. (g) Algorithm one is faster.
(d) Algorithm one is faster. (h) Algorithms are equally fast.
Answers to Problems 247
Question 8.7:
(a) Algorithm one is faster. (e) The algorithms are equally fast.
(b) Algorithm two is faster.
(f) Algorithm two is faster.
(c) Algorithm two is faster.
(d) Algorithm one is faster. (g) Algorithm two is faster.
√
Question 8.8: n < n1.5 < 10n
n
Question 8.9: n2 log2 (n) < 2n < 22
Question 8.10: log2 (n) < n < n log2 (n) so 2log2 (n) < 2n < 2n log2 (n)
Question 8.11: The dominant operation is the comparison in step 4.1 and the time com-
plexity is f (n) = n − 1.
Question 8.12: The dominant operation is the multiplication in step 2.1.2.1 and the time
complexity is f (n) = n3 .
Question 8.13: The dominant operation is the addition in step 3.1.1.1.1 and the time com-
plexity is f (n) = n3 .
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.11:
(a) Neither (c) Semi-Eulerian
(b) Semi-Eulerian (d) Neither
Question 9.12:
(a) Neither (c) Eulerian
(b) Semi-Eulerian (d) Semi-Eulerian
Question 9.16: The trace of the modified Fleury’s algorithm is given below:
insertion
Step current path point e v new path unused edges
2 A - - - - {e1 , e2 , e3 , e4 , e5 , e6 }
3.1 A A - - - {e1 , e2 , e3 , e4 , e5 , e6 }
3.2 A A - A A {e1 , e2 , e3 , e4 , e5 , e6 }
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 }
3.3.1–4 A A e2 B Ae1 Be3 Ce2 A {e4 , e5 , e6 }
3.4 Ae1 Be3 Ce2 A A e2 B Ae1 Be3 Ce2 A {e4 , e5 , e6 }
3.1 Ae1 Be3 Ce2 A B e2 B Ae1 Be3 Ce2 A {e4 , e5 , e6 }
3.2 Ae1 Be3 Ce2 A B e2 B B {e4 , e5 , e6 }
3.3.1–4 Ae1 Be3 Ce2 A B e4 C Be4 C {e5 , e6 }
3.3.1–4 Ae1 Be3 Ce2 A B e6 D Be4 Ce6 D {e5 }
3.3.1–4 Ae1 Be3 Ce2 A B e5 B Be4 Ce6 De5 B { }
3.4 Ae1 Be4 Ce6 De5 Be3 Ce2 A B e5 B Be4 Ce6 De5 B { }
The Euler circuit given by the modified Fleury’s algorithm is Ae1 Be4 Ce6 De5 Be3 Ce2 A,
which we more normally write as A − B − C − D − B − C − A or simply as ABCDBCA.
Question 9.17: The Euler circuit given by the modified Fleury’s algorithm is
Ae1 Be4 De8 Ee9 De6 Ce7 Ee5 Be3 Ce2 A, which we more normally write as A − B − D − E −
D − C − E − B − C − A or simply as write as ABDEDCEBCA.
Question 9.18:
0 1 0 0 2 0
0 0 2 0
1 0
(a)
0 0 1 or
0 0 0
0 1 0 2 0 0
0 2 0 1 1 0
1 1 0 0 1
0 0 3 1 0
(b) or
1 1 0 0 1
0 0 3 1 0
250 Discrete Mathematics for Computer Science
1
1 0 0 0 1
0 0 1 0
1
0
(c)
or 0
0 1 3 0 0
0 1 0 1 1 0
0 0 0 3 1 0
0 1 1 0 0 0 0
0 1 0 0 0
1 0
0 0 0 0 1 1 0
(d) or
0 1 1
0 0 0 0
0 1 0 0 0 1 0
0 0 0 0 1 1 0
Question 9.19:
0 1 1 0 0 0
0 0 1 1
1 0
(a)
0 1 1
or
1 0 0
0 1 0 1 1 0
0 0 1 1 1 0
0 1 1 0 0 0
0 0 2 1
1 0
(b)
0 1 1
or
1 0 0
0 1 0 2 1 0
0 0 1 1 1 0
0 1 1 0 0 0
0 0 2 1
1 0
(c)
0 1 2
or
1 0 0
0 1 0 2 1 0
0 0 1 2 1 0
0 2 2 0 0 0
0 0 1 1
2 0
(d)
0 1 1
or
2 0 0
0 1 0 1 1 0
0 0 1 1 1 0
Question
9.20:
1 0 0 1 1 1
0 1 1 0
0
0
0 1 1 or 0
1 0
1 0 1 1 1 1
0 1 0 1 0 0
Question
9.21:
0 1 1 0 0 0 0
0 2 0 0 0
1
0
0 0 0 0 or 1
2 0
1 1 2
0
0 0 1
0 0 0 0 0 1 0
0 0 0 0 2 0 0
Answers to Problems 251
Question
9.22:
0 0 0 1 1 0 0
0 1 2 0 0
0
0
0 1 0 0 or 0
1 0
0 0 0
1
2 1 0
0 1 1 0 0 0 0
1 0 0 0 0 1 1
Question 9.23: The dominant operation is the Boolean addition in step 3.1.1.1.1. Thus the
time complexity function is given by f (n) = n3 .
Question
9.24:
1 1 1 0 0 0
1 1 1 0 0 0
1 1 1 0 0 0
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 1 1 1
Question
9.25:
1 1 0 0 0 0
0 1 0 0 0 0
0 1 1 0 0 0
0 1 1 1 1 1
0 1 1 0 1 1
0 1 1 0 0 1
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 6= 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.
252 Discrete Mathematics for Computer Science
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; CE (c) (5 × 4) − 1 = 19; none
Question 10.12: Using Prim’s algorithm and starting at vertex A we get the following min-
imal spanning tree:
B C
9 5 7
6 D 4
E F
Question 10.13: Notice that in step 3.1 of Prim’s algorithm we use the first 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 E matter. Using Prim’s algorithm and starting at vertex A we get
the following minimal spanning tree:
A D
3 3
5 C F
2 2
B E
Question
10.14: The minimal spanning
tree given by Prim’s algorithm has vertex set
V = A, B, C, D, E and edge set E = AE, BC, CD, DE .
Question 10.15: Notice that in step 3.2 of Dijkstra’s algorithm we use the first edge in
the edge set for which the required condition is satisfied. This means the order in which
the edges are given in E matter. Using Dijkstra’s algorithm we get the following minimal
spanning tree:
B D
11 10 13
A F
9 8
C E
Answers to Problems 253
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.
11 A 10
B C
5
6 D
8
E F
Index
255
256 Index
xnor gate, 98
xor gate, 98