100% found this document useful (1 vote)
182 views334 pages

Jon Pierre Fortney - Discrete Mathematics For Computer Science - An Example-Based Introduction-Chapman

This document is the preface to a textbook on discrete mathematics for computer science. It was written for students at community colleges and liberal arts colleges. The book covers key topics like algorithms, number representations, logic, set theory, Boolean algebra, functions, counting, graph theory, and trees. It uses simple language, examples, and short paragraphs to explain concepts clearly. There are over 200 worked examples and 200 practice problems to help students learn and apply the material. The goal is to provide an accessible introduction to discrete math topics relevant for computer science majors.

Uploaded by

Alya Faiza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
182 views334 pages

Jon Pierre Fortney - Discrete Mathematics For Computer Science - An Example-Based Introduction-Chapman

This document is the preface to a textbook on discrete mathematics for computer science. It was written for students at community colleges and liberal arts colleges. The book covers key topics like algorithms, number representations, logic, set theory, Boolean algebra, functions, counting, graph theory, and trees. It uses simple language, examples, and short paragraphs to explain concepts clearly. There are over 200 worked examples and 200 practice problems to help students learn and apply the material. The goal is to provide an accessible introduction to discrete math topics relevant for computer science majors.

Uploaded by

Alya Faiza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 334

Discrete Mathematics for Computer

Science
Discrete Mathematics for Computer
Science
An Example-Based Introduction

Jon Pierre Fortney


First Edition published 2021
by CRC Press
6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742

and by CRC Press


2 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN

© 2021 Jon Pierre Fortney

CRC Press is an imprint of Taylor & Francis Group, LLC

The right of Jon Pierre Fortney to be identi ed as author of this work has been asserted by him in
accordance with sections 77 and 78 of the Copyright, Designs and Patents Act 1988.

Reasonable efforts have been made to publish reliable data and information, but the author and
publisher cannot assume responsibility for the validity of all materials or the consequences of
their use. The authors and publishers have attempted to trace the copyright holders of all material
reproduced in this publication and apologize to copyright holders if permission to publish in this
form has not been obtained. If any copyright material has not been acknowledged please write
and let us know so we may rectify in any future reprint.

Except as permitted under U.S. Copyright Law, no part of this book may be reprinted,
reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means,
now known or hereafter invented, including photocopying, micro lming, and recording, or in
any information storage or retrieval system, without written permission from the publishers.

For permission to photocopy or use material electronically from this work, access
www.copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood
Drive, Danvers, MA 01923, 978-750-8400. For works that are not available on CCC, please
contact [email protected]

Trademark notice: Product or corporate names may be trademarks or registered trademarks and
used only for identi cation and explanation without intent to infringe.

Library of Congress Cataloging-in-Publication Data


Names: Fortney, Jon Pierre, author.
Title: Discrete mathematics for computer science : an example-based introduction /
Jon Pierre Fortney.
Description: First edition. | Boca Raton : C&H/CRC Press, 2021. | Includes
bibliographical references and index.
Identi ers: LCCN 2020034984 (print) | LCCN 2020034985 (ebook) | ISBN
9780367549886 (hardback) | ISBN 9781003091479 (ebook).
Subjects: LCSH: Computer science--Mathematics.
Classi cation: LCC QA76.9.M35 F67 2021 (print) | LCC QA76.9.M35 (ebook) |
DDC 004.01/51--dc23
LC record available at https://lccn.loc.gov/2020034984
LC ebook record available at https://lccn.loc.gov/2020034985
ISBN: 978-0-367-54988-6 (hbk)
ISBN: 978-0-367-54989-3 (pbk)
ISBN: 978-1-003-09147-9 (ebk)

Typeset in Computer Modern font


by KnowledgeWorks Global Ltd.
To Ron Ryan Noval
Contents

Preface

CHAPTER 1 ▪ Introduction to Algorithms

1.1 WHAT ARE ALGORITHMS?


1.2 CONTROL STRUCTURES
1.3 TRACING AN ALGORITHM
1.4 ALGORITHM EXAMPLES
1.5 PROBLEMS

CHAPTER 2 ▪ Number Representations

2.1 WHOLE NUMBERS


2.2 FRACTIONAL NUMBERS
2.3 THE RELATIONSHIP BETWEEN BINARY, OCTAL, AND
HEXADECIMAL NUMBERS
2.4 CONVERTING FROM DECIMAL NUMBERS
2.5 PROBLEMS

CHAPTER 3 ▪ Logic

3.1 PROPOSITIONS AND CONNECTIVES


3.2 CONNECTIVE TRUTH TABLES
3.3 TRUTH VALUE OF COMPOUND STATEMENTS
3.4 TAUTOLOGIES AND CONTRADICTIONS
3.5 LOGICAL EQUIVALENCE AND THE LAWS OF LOGIC
3.6 PROBLEMS

CHAPTER 4 ▪ Set Theory

4.1 SET NOTATION


4.2 SET OPERATIONS
4.3 VENN DIAGRAMS
4.4 THE LAWS OF SET THEORY
4.5 BINARY RELATIONS ON SETS
4.6 PROBLEMS

CHAPTER 5 ▪ Boolean Algebra

5.1 DEFINITION OF BOOLEAN ALGEBRA


5.2 LOGIC AND SET THEORY AS BOOLEAN ALGEBRAS
5.3 DIGITAL CIRCUITS
5.4 SUMS-OF-PRODUCTS AND PRODUCTS-OF-SUMS
5.5 PROBLEMS

CHAPTER 6 ▪ Functions

6.1 INTRODUCTION TO FUNCTIONS


6.2 REAL-VALUED FUNCTIONS
6.3 FUNCTION COMPOSITION AND INVERSES
6.4 PROBLEMS

CHAPTER 7 ▪ Counting and Combinatorics

7.1 ADDITION AND MULTIPLICATION PRINCIPLES


7.2 COUNTING ALGORITHM LOOPS
7.3 PERMUTATIONS AND ARRANGEMENTS
7.4 COMBINATIONS AND SUBSETS
7.5 PERMUTATION AND COMBINATION EXAMPLES
7.6 PROBLEMS

CHAPTER 8 ▪ Algorithmic Complexity

8.1 OVERVIEW OF ALGORITHMIC COMPLEXITY


8.2 TIME-COMPLEXITY FUNCTIONS
8.3 FINDING TIME-COMPLEXITY FUNCTIONS
8.4 BIG-O NOTATION
8.5 RANKING ALGORITHMS
8.6 PROBLEMS

CHAPTER 9 ▪ Graph Theory

9.1 BASIC DEFINITIONS


9.2 EULERIAN AND SEMI-EULERIAN GRAPHS
9.3 MATRIX REPRESENTATIONS OF GRAPHS
9.4 REACHABILITY FOR DIRECTED GRAPHS
9.5 PROBLEMS

CHAPTER 10 ▪ Trees

10.1 BASIC DEFINITIONS


10.2 MINIMAL SPANNING TREES OF WEIGHTED GRAPHS
10.3 MINIMAL DISTANCE PATHS
10.4 PROBLEMS

APPENDIX A ▪ Basic Circuit Design


A.1 BINARY ADDITION
A.2 THE HALF-ADDER
A.3 THE FULL-ADDER
A.4 ADDING TWO EIGHT-DIGIT BINARY NUMBERS

APPENDIX B ▪ Graph Theory

B.1 CHAPTER ONE ANSWERS


B.2 CHAPTER TWO ANSWERS
B.3 CHAPTER THREE ANSWERS
B.4 CHAPTER FOUR ANSWERS
B.5 CHAPTER FIVE ANSWERS
B.6 CHAPTER SIX ANSWERS
B.7 CHAPTER SEVEN ANSWERS
B.8 CHAPTER EIGHT ANSWERS
B.9 CHAPTER NINE ANSWERS
B.10 CHAPTER TEN ANSWERS

Index
Preface

One of the major challenges of teaching mathematics at the community


college or small liberal arts college level is nding 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 nd 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 eld 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 bene t most with a clear, clean,
uncluttered, down-to-earth exposition of the material.
This book is intended for a rst- 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 rst 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 de nitions of set theory, Venn diagrams, and the laws
of set theory. Chapter ve 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 de nition 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 mathematics 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
de nitions or explanations are made, they are quickly followed by
examples to aid students in their understanding. Words being de ned 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 de nition or
explanation, but also aids students to fully understand the written content.
Indeed, this textbook would be ideal for international students and non-
native English speakers. Furthermore, students without much
mathematical maturity or abstract thinking ability need numerous
examples to help them bridge the gap between concrete and abstract
thought. They also need a signi cant 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 students 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 super cial 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 nally,
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 rst two sections of Chapter seven and
the rst 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.
Jon Pierre Fortney
June 2020
CHAPTER 1
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 speci c task. Computer programs
are implementations of algorithms, so it is very important that computer science majors and
programmers have a good understanding of how algorithms work. Deciding on an algorithm is
generally the rst step of writing a computer program. Because algorithms will be used
throughout this book we begin by introducing some of the basic concepts and ideas related to
algorithms.

1.1 WHAT ARE ALGORITHMS?


An algorithm is a nite sequence of well-de ned steps to perform some task such that

each step is a clear instruction that can be done in a nite amount of time,
the sequence in which the steps are done is clear, and
the process is guaranteed to stop after a nite number of steps have been done.

Here the phrase well-de ned means the steps are very clear. There is no confusion or uncertainty
in what is meant. A computer program is the implementation of an algorithm in some
programming language. This means the program actually carries out, or executes, the algorithm.
Designing an algorithm is one of the rst steps in writing a computer program. In order to make
algorithms easier for us to understand they are written in a form of English called pseudocode.
This allows us to concentrate and think about the structure of the algorithm without worrying
about the details of a particular computer language. Let us start by looking at a very simple
algorithm written in pseudocode. Here is an algorithm to calculate the volume of a cube.

Algorithm: Finds the volume of a cube with side length l.

1. Input l. (l is the length of one side of the cube.)


2. volume ⟵ l 3

3. Output volume.

Notice the following things about this algorithm:

The steps are numbered for easy reference.


Comments explaining something are put in parenthesis.
The symbol ⟵ means assignment. Here the formula l3 is evaluated and the resulting number
is assigned to the variable called volume.
In algebra we may write x = 7. The x is a variable name and by saying x = 7 we are giving the
variable the value 7. In algebra variables are usually just letters, like x, y, or z. But in computer
science variables are often given names. This helps programmers remember what the variable
means. So, volume ⟵ l means that the variable named volume is given whatever value l3 is. In
3

other words, the number given by l3 is stored in, or assigned to, the computer's memory at an
address that is called volume.1

1.2 CONTROL STRUCTURES


Algorithms use something called control structures to tell the algorithm what to do at different
times. These are instructions that control how the algorithm works. For example, they tell how
many times other instructions are executed, or when they are executed. There are two kinds of
control structures, conditional controls and loop controls. Conditional controls give conditions for
the execution of an algorithm step. Conditional controls include

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.

Algorithm: Determines if a student has passed.

1. Input x. (x is a student's grade as a percent.)


2. If x ≥ 60 then
2.1 Output “passed.”

In line 1 the algorithm asks you to input a student's grade as a percentage. Line 2 says that if the
condition is true and the grade is greater than or equal to 60 then move to line 2.1, which outputs
the word passed. Suppose you input a 75. Since the grade is 75 ≥ 60 then the word passed is
output and the algorithm ends. But suppose the grade was 55. Then since 55 ≥ 60 we do not go
to line 2.1 and the algorithm ends with nothing output.

If-then-else
Now consider the following algorithm. It contains an if-then-else conditional control.
Algorithm: Determines if a student has passed or failed.

1. Input x. (x is a student's grade as a percent.)


2. If x ≥ 60 then
2.1 Output “passed.”
else
2.2 Output “failed.”

In line 1 the algorithm asks you to input a student's grade as a percentage. Line 2 says that if the
grade is greater than or equal to 60 then move to the next line, 2.1, which outputs the word passed.
But now there is an else. This tells the algorithm what to do if the condition is not met. Suppose
you input a 75. Then since 75 ≥ 60 the algorithm goes to line 2.1, outputs the word passed, and
ends. But now suppose that a grade of 55 was input. Since 55 ≥ 60 then the algorithm goes to
line 2.2, outputs the word failed, and ends. Since the if condition was not met the then line was
skipped and the algorithm goes to the else line.
It is also possible to nestif-then-else statements. This means we put an if-then-else statement
inside another if-then-else statement. Consider the following algorithm that prints out a student's
letter grade when their grade as a percent is input.

Algorithm: Gives a student's letter grade.

1. Input x. (x is a student's grade as a percent.)


2. If x ≥ 90 then
2.1 Output “A.”
else
3. If x ≥ 80 then
3.1 Output “B.”
else
4. If x ≥ 70 then
4.1 Output “C.”
else
5. If x ≥ 60 then
5.1 Output “D.”
else
5.2 Output “F.”

Here we can see several nested if-then-else statements. Consider what happens if we input the
grade 75. In line 2 the rst if condition is checked and 75 ≥ 90 is found so line 2.1 is skipped and
we go to line 3. In line 3 the second if condition is checked and 75 ≥ 80 is found so line 3.1 is
skipped and we go to line 4. In line 4 the third if condition is checked and 75 ≥ 70 is found so we
go to line 4.1 and output the letter grade C. At this point the algorithm ends.

For-do
Now we look at loop controls. Loop controls tell how many times a step in the algorithm should
be executed. We can think of the algorithm as looping around and repeating the same step a
number of times. First we look at the for-do loop control.

Algorithm: Finds the smallest number in a list of numbers.

1. Input the number of values in list, n.


2. Input the list of numbers x , x , … , x .
1 2 n

3. min ← x 1

4. i ← 2
5. For i = 2 to n do
5.1 If x < min then
i

5.1.1 min ← x 1

5.2 i ← i + 1
6. Output min.

The number of values in the list is input in step 1, then the list of values is input in step 2. In
step 3 the word min is the name of a variable and x1, the rst value in the list, is assigned to it.
Step 4 initializes the variable i with 2. This means the variable i is given the starting, or initial,
value 2. Step 5 contains the for-do loop. This line tells us we repeat steps 5.1, 5.1.1 if necessary,
and 5.2 for each value of i between 2 and n. Step 5.2 increases i by 1. This is often called
incrementingi. Once i becomes n + 1 we no longer repeat step 5 and we move onto step 6 where
we output the minimum value.
Remember, pseudocode is written in English at a level that allows us to fully understand the
algorithm. As long as we can fully understand what is supposed to happen it is enough. Suppose
we simplify this algorithm a little by taking out steps 4 and 5.2, the steps that involve initializing
and incrementing the variable i. Then the algorithm looks like this.

Algorithm: Finds the smallest number in a list of numbers.

1. Input the number of values in list, n.


2. Input the list of numbers x , x , … , x .
1 2 n

3. min ← x 1

4. For i = 2 to n do
4.1 If x < min then
i

4.1.1 min ← x 1

5. Output min.

In for-do loops we step through values of i. The i = 2 to n inside the for-do loop tells us for
which values of i we need to do the loop. We start with i = 2 and each time the loop is executed
the value of i increases by one. We keep executing the loop until we reach the last value of i,
which is n. When the i value is incremented to n + 1 we stop doing the loop. Hopefully this
should be clear to any human reading this algorithm and so we do not really need those other steps
to understand what is happening.
While-do
The while-do loops are a lot like the for-do loops. While-do loops are repeated while a given
condition is true. In other words, as long as the given condition is true the loop repeats. Consider
the following algorithm.

Algorithm: Finds the order of the factor two for the integer n.

1. Input a non-negative integer n.


2. order ← 0
3. While n is even do
3.1 n ← n

3.2 order ← order + 1


4. Output order.

In this example the steps 3.1 and 3.2 are repeated as long as n is an even number. At some point
the condition stops being true and the loop is no longer repeated.

Repeat-until
The repeat-until loops are very similar to the while-do loops. While-do loops are repeated while
a given condition is true. Repeat-until loops are repeated until a given condition is true. That
means the loop repeats while the condition is false and then stops when the condition becomes
true. In this example the steps 2.1 and 2.2 are repeated until the condition i = 0 is met.

Algorithm: Outputs a list of the numbers from n to 1.

1. Input a positive integer n


2. i ← n..
3. 3. Repeat
3.1. Output i
3.2. i ← i − 1
Until i = 0

1.3 TRACING AN ALGORITHM


One of the techniques often used to help us understand how an algorithm works is to trace it. We
trace an algorithm by choosing some particular input and then step through the algorithm and
write down the values of all the variables at each step until the algorithm ends. In other words, the
trace of an algorithm tell us what value every variable in the algorithm has at each step. By tracing
an algorithm for different possible inputs we understand what an algorithm does and how it works
much better. To learn how tracing an algorithm works we will rst trace the three algorithms given
as examples of loop controls.

Algorithm: Finds the smallest number in a list of numbers.


1. Input the number of values in list, n.
2. Input the list of numbers x , x , … , x .
1 2 n

3. min ← x 1

4. For i = 2 to n do
4.1 If x < min then
i

4.1.1 min ← x i

5. Output min.

Let us suppose we want to nd the smallest number in the list 5, 4, 8, 3. There are four numbers
in this list so n = 4 and x = 5, x = 4, x = 8, x = 3. We usually use a table to trace an
1 2 3 4

algorithm.

Step min i xi output

3 5 - - -
4 5 2 4 -
4.1 5 2 4 -
4.1.1 4 2 4 -
4 4 3 8 -
4.1 4 3 8 -
4 4 4 3 -
4.1 4 4 3 -
4.1.1 3 4 3 -
5 3 4 3 3

Often when we trace an algorithm we do not bother to write down the input steps in the table.
Here we did not write steps 1 or 2 but started with step 3 where the value x = 5 was assigned to
1

the variable min. After step 3 we included a row in the table for each of the following steps in the
algorithm. In step 4 the algorithm states “For i = 2 to n do” so we let i = 2. If i = 2 then we also
know what xi is and so also include that in the table. Step 4.1 states “If x < min then” so we
i

check to see if 4 < 5. Since it is, we go to the then part of the if-then statement, step 4.1.1. Here
x = 4 is assigned to the variable min. After that we return to the next step in the for-do
2

statement where i = 3. Notice that this time around x3 is not less than min and so step 4.1.1 is
skipped. The algorithm continues the for-do loop until i = n. Carefully follow the rest of the steps
of the algorithm to make sure you understand them.
Now we will look at the algorithm that contains the while-do loop.

Algorithm: Finds the order of the factor two for the integer n.

1. Input a non-negative integer n.


2. order ← 0
3. While n is even do
3.1 n ← n

3.2 order ← order + 1


4. Output order.
We will trace this algorithm for the non-negative integer n = 56.
Step order n output
2 0 56 -
3 0 56 -
3.1 0 28 -
3.2 1 28 -
3 1 28 -
3.1 1 14 -
3.2 2 14 -
3 2 14 -
3.1 2 7 -
3.2 3 7 -
3 3 7 -
4 3 7 3

As before we skip the input step and begin the trace of the algorithm with step 2. In step 2 we
assign the value 0 to order. In step 3 we check to see if n is even. Since it is we do steps 3.1 and
3.2. In step 3.1 the number n is divided by two and this new number is assigned to n. Then step 3.2
adds one to order and assigns this new number to order. We could also say we increase the value
of order by one. Then we return to step 3 where we check if the new value of n is even. While n is
still even we do steps 3.1 and 3.2. When n stops being even we skip steps 3.1 and 3.2 and continue
with the algorithm.
Now look at the two algorithm traces we presented. Notice that in both cases a number of lines
look exactly the same. In the rst trace all the lines 4 and 4.1 are exactly the same. In the second
trace when 3 follows a 3.2 then these two lines are the same. Often when one gets good at tracing
algorithms one skips writing down repeated lines.

Algorithm: Outputs a list of numbers going from n to 1.

1. Input a positive integer n.


2. i ← n..
3. Repeat
3.1. Output i
3.2. i ← i − 1
Until i = 0

This is a simple algorithm that shows how the repeat-until loop control works. We will trace it
for n = 3.
Step i output
2 3 -
3.1 3 3
3.2 2 -
3.1 2 2
3.2 1 -
3.1 1 1
3.2 0 -
Tracing this algorithm is straightforward. As long as the condition after until is not met we
continue to repeat steps 3.1 and 3.2. In step 3.1 we output i and in step 3.2 we subtract one from i
and assign the new value to i. This is called decrementingi and works very similarly to
incrementing i, only instead of adding one each time we subtract one each time.
Learning how to trace an algorithm is a very important skill. Tracing an algorithm multiple
times for different inputs is a very good way to help you understand what an algorithm does and
how it works. As you get better at tracing algorithms you will probably nd yourself skipping
steps or only writing down enough detail to understand what is happening. This is ne. Tracing
algorithms is simply a tool to help you understand an algorithm. Your understanding of how an
algorithm works is the most important thing.

1.4 ALGORITHM EXAMPLES


Now we will simply look at a number of different algorithms to get some practice in tracing and
trying to understand algorithms. Remember, tracing an algorithm is a tool to help you understand
how an algorithm works and what it does. Understanding the algorithm and how it behaves is the
most important part. We begin with an algorithm that is an example of the if-then-else conditional
control.

Algorithm: Finds the larger of two numbers.

1. Input x and y.
2. If x > y then
2.1 max ⟵ x
else
2.2 max ⟵ y
3. Output max.

Example 1.1

Use the above algorithm to nd the larger of the two numbers x = 7 and y = 9.

This algorithm is so simple that tracing it in a table is a little silly. Simply step through each
line of the algorithm. In line 1 the values x = 7 and y = 9 are input in line 1. In line 2 we
check if x > y. Since 7≯9 then we skip line 2.1 and do line 2.2 where we assign 9 to max. Then
in line 3 we output max which is 9.

Next we look at an algorithm that is an example of the for-do loop control.

Algorithm: Evaluates xn where x is a real number and n is a positive integer.

1. Input x and n.
2. answer ← x
3. For i = 1 to n − 1 do
3.1. answer ← answer × x
4. Output answer.

Example 1.2

Trace the above algorithm for x = 7 and n = 4. In other words, use the algorithm to nd 74.
Step i answer Output
2 - 7 -
3.1 1 49 ( ← 7 × 7) -
3.1 2 343 ( ← 49 × 7) -
3.1 3 2401 ( ← 343 × 7) -
4 3 2401 2401

Here we look at an algorithm is also an example of the while-do loop control. The exclamation
mark ! following a positive integer is called the factorial symbol. It will be covered in chapter 7.
Simply put, n! means to multiply n by all the numbers less than n. For example,

2! = 2 × 1, 3! = 3 × 2 × 1, 4! = 4 × 3 × 2 × 1,

and so on.

Algorithm: Evaluates n! where n is a positive integer.

1. Input positive integer n.


2. answer ← n
3. While n > 1do
3.1 n ← n − 1
3.2 answer ← answer × n
4. Output n.

Example 1.3

Trace the above algorithm for n = 4. In other words, use the algorithm to nd 4!.
Step n answer Output
2 4 4 -
3.1 3 4 -
3.2 3 12 ( ← 4 × 3) -
3.1 2 12 -
3.2 2 24 ( ← 12 × 2) -
3.1 1 24 -
Step n answer Output
3.2 1 24 ( ← 24 × 1) -
4 1 24 24

The next few algorithms are examples of search algorithms.

Algorithm: Searches a string of integers x 1, x2, … , xn to see if it contains the integer s.

1. Input string x , x , … , x and integers n and s.


1 2 n

2. i ← 1
3. int eger _ s _ det ected ← f alse
4. Repeat
4.1. If x = s then
i

4.1.1. int eger _ s _ det ected ← f alse


4.2. i ← i + 1
until integer _ s _ detected = true or i = n + 1
5. If integer _ s _ detected = true then
5.1 Output “String contains s.”
else
5.2 Output “String does not contain s.”

Here the steps 4.1, 4.1.1, and 4.2 are repeated until either integer _ s _ detected = true or
until i = n + 1. Thus, as soon as we have x = s in step 4.1 then step 4.1.1 is called and
i

integer_s_detected becomes true. Step 4.2 is still carried out but then the two conditions are
checked it see if we need to repeat step 4 again or not. We do not need to continue checking the
rest of the string once we have found x = s for some value of i. Finally, an if-then-else statement
i

is used in order to output the correct statement.

Example 1.4

Use the above algorithm to search the string 758 to see if it contains a 5.

The input is x 1 , ,
= 7 x2 = 5 x3 = 8 n = 3 , , and s = 5.
Step integer_s_detected i xi Output
2 - 1 - -
3 false 1 - -
4.1 false 1 7 -
4.2 false 2 ( ← 1 + 1) 7 -
4.1 false 2 5 -
4.1.1 int eger _ s _ det ected ← f alse 2 5 -
4.2 true 3 ( ← 2 + 1) 5 -
5.1 true 3 5 String contains 5.
This algorithm works ne, but we can make it much simpler by using a return. Return works a
lot like Output except that it also ends the algorithm. Sometimes we want an algorithm to end as
soon as something is output. That is when return is used.2 In this example, as soon as we
encounter an integer s in the string we know the string contains the integer s. We do not need to
continue checking the rest of the string. Thus, if x = s for some value of i then “String contains
i

s” is output and the algorithm ends right there.

Algorithm: Searches a string of integers x 1, x2, … , xn to see if it contains the integer s.

1. Input string x , … , x , n and s.


1 n

2. For i = 1 to n do
2.1. If x = s then
i

2.1.1. Return “String contains s.”


3. Return “String does not contain s.”

Example 1.6

Trace the above algorithm for the string 497316 and search for the integer 3.

The input is x 1 , , ,
= 4 x2 = 9 x3 = 7 x4 = 3 x5 = 1 x6 = 6 n = 6, , , , and s = 3.
Step i xi s Output
2 1 1 - -
2.1 1 4 3 -
2.1 2 9 3 -
2.1 3 7 3 -
2.1 4 3 3 -
2.1.1 4 3 3 String contains 3.

Notice, since x = s the then part of the if-then statement is executed, “String contains 3.” is
4

output, and the algorithm ends.

Algorithm: Searches two strings of integers x1, x2, … , xn and y1, y2, … , yn of equal length
to see if either string contains integer s.

1. Input strings x , … , x and y , … , y , n and s.


1 n 1 n

2. For i = 1 to n do
2.1. If x = s then
i

2.1.1. Return “String one contains s.”


3. For i = 1 to n do
3.1. If y = s then
i

3.1.1. Return “String two contains s.”


4. Return “Neither string contains s.”
Example 1.6

Use the above algorithm to see if either string 74 or string 89 contains an 8.


The input is x = 7, x = 4, y = 8, y = 9, n = 2, and s = 8.
1 2 1 2

Step i x i or y i Output
2.1 1 7 -
2.1 2 4 -
3.1 1 8 -
3.1.1 1 8 String two contains 8.

Algorithm: Checks if two strings x , … , x and y , … , y contain a common integer. (That


1 n 1 n

is, checks to see if there is an integer that is contained in both strings.)

1. Input strings x , … , x and y , … , y , and length n.


1 n 1 n

2. For i = 1 to n do
2.1 For j = 1 to n do
2.1.1 If x = y then
i j

2.1.1.1 Return “Element in common to both strings.”


3. Return “No elements in common to both strings.”

Example 1.7

Use the above algorithm to see if the strings 67 and 89 have any integers in common.

The input is x 1 ,
= 6 x2 = 7 y1 = 8 y2 = 9 , , , and n = 2.
Step i j xi yj Output
2.1.1 1 1 6 8 -
2.1.1 1 2 6 9 -
2.1.1 2 1 7 8 -
2.1.1 2 2 7 9 -
3 2 2 7 9 \beginsubarraycNoelementsincommon\\tobothstrings. \endsubarray

Algorithm: Checks to see if a string of integers x 1, x2, … , xn contains any duplicate integers.

1. Input string x , … , x . 1 n

2. For i = 1 to n − 1 do
2.1 For j = i + 1 to n do
2.1.1 If x = x then i j

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.

The input is x 1 , ,
= 9 x2 = 8 x3 = 7 x4 = 6 x5 = 7 , , , and n = 5.
Step i j xi xj Output
2.1.1 1 2 9 8 -
2.1.1 1 3 9 7 -
2.1.1 1 4 9 6 -
2.1.1 1 5 9 7 -
2.1.1 2 3 8 7 -
2.1.1 2 4 8 6 -
2.1.1 2 5 8 7 -
2.1.1 3 4 7 6 -
2.1.1 3 5 7 7 -
2.1.1.1 3 5 7 7 \beginsubarraycThereisaduplicate\\inthestring. \endsubarray

Algorithm: Determines if a string of characters x 1, x2, … , xn consists entirely of integers.

1. Input string x , x , … , x and string length n.


1 2 n

2. i ← 1
3. non int eger _ det ected ← f alse
4. 4. Repeat
4.1. Ifxi is not an integer then
4.1.1. non int eger _ det ected ← true
4.2. i ← i + 1
until non int eger _ det ected = true or i = n + 1
5. If noninteger _ detected = true then
5.1 Output “String contains non-integer characters.”
else
5.2 Output “String consists entirely of integers.”

Example 1.9

Use the above algorithm to determine if the string 68c5 consists entirely of integers or if a non-
integer is contained in the string.

The input it x 1 ,
= 6 x2 = 8 x3 = c , , and x
4 = 5 , and string length n = 4.
Step nondigit_detected i xi Output
2 - 1 - -
3 false 1 - -
Step nondigit_detected i xi Output
4.1 false 1 6 -
4.2 false 2 6 -
4.1 false 2 8 -
4.2 false 3 8 -
4.1 false 3 c -
4.1.1 true 3 c -
4.2 true 4 c -
5.1 true 4 c \beginsubarraycStringcontainsnon-integercharacters. \endsubarray

1.5 PROBLEMS

Question 1.1 Give a description of what the following algorithm does. What is the output of this
algorithm for the input days = 3, hours = 7, minutes = 51, seconds = 27?

1. Input days, hours, minutes, seconds.


2.
answer ← sec onds + (min utes × 60) + (hours × 60 × 60) + (days × 24 × 60 × 60)

3. Output answer.

Question 1.2 Trace the below algorithm for the following pairs of numbers.

(a) x = 6 and y = 12
(b) x = 15 and y = 9
(c) x = 7 and y = 7

1. Input x and y.
2. If x > y then
2.1 max ⟵ x
else
2.2 max ⟵ y
3. Output max.

Question 1.3 Trace the below algorithm for the following pairs of numbers.

(a) price = 74.36, paid = 82.50

(b) price = 75.00, paid = 55.50

(c) price = 48.36, paid = 54.92

1. Input price and paid.


2. If paid < price then
2.1 Output “Did not pay enough.”
else
2.2 change ← paid − price
3. Output “Change change.”

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

1. Input positive integer n.


2. f ac ← n
3. While n > 1do
3.1 n ← n − 1
3.2
f ac ← f ac × n

4. 4. Output fac.

Question 1.8 Trace the below algorithm for the following numbers. Then compare it to the
algorithm in the last question.

(a) n = 3
(b) n = 5
(c) n = 7

1. Input positive integer n.


2. f ac ← 1
3. For i = 1 to n do
3.1 f ac ← f ac × i
4. Output fac.

Question 1.9 Trace the following algorithm for n = 8, 490, 725, 727, 154, 368, 726, 402, 945.
1. Input a positive integer n.
2. d ← number of digits of n
3. While d > 1do
3.1 n ← sum of digits of n
3.2 d ← number of digits of n
4. Output n.

Question 1.10 Trace the following algorithm for

(a) string of integers 274390 to see if the integer s = 9 is contained in the string,
(b) string of integers 730285 to see if the integer s = 2 is contained in the string,
(c) string of integers 983650362 to see if the integer s = 5 is contained in the string.

1. Input string x , … , x , n and s.


1 n

2. For i = 1 to n do
2.1. If x = s then
i

2.1.1. Return “String contains s.”


3. Return “String does not contain s.”

Question 1.11 Trace the below algorithm for

(a) strings 2109 and 4071 to see if the integer s = 0 is contained in either one,
(b) strings 593 and 721 to see if the integer s = 2 is contained in either one,
(c) strings 397 and 443 to see if the integer s = 6 is contained in either one.

1. Input strings x , … , x and y , … , y , n and s.


1 n 1 n

2. For i = 1 to ndo
2.1. If x = s then
i

2.1.1. Return “String one contains integer s.”


3. For i = 1 to ndo
3.1. If y = s then
i

3.1.1. Return “String two contains integer s.”


4. Return “Neither string contains integer s.”

Question 1.12 Trace the below algorithm for

(a) strings 273 and 439,


(b) strings 593 and 725,
(c) strings 123 and 987.
1. Input strings x , … , x and y , … , y .
1 n 1 n

2. For i = 1 to n do
2.1 For j = 1 to n do
2.1.1 If x = y then
i j

2.1.1.1 Return “There is an element common to both strings.”


3. Return “There are no elements common to both strings.”

Question 1.13 Trace the below algorithm for

(a) string 63524,


(b) string 47372916,
(c) string 563732.

1. Input string x , … , x .
1 n

2. For i = 1 to n −1 do
2.1 For j = i + 1 to n do
2.1.1 If x = x then
i j

2.1.1.1 Return “There is a duplicate integer in the string.”


3. Return “There are no duplicate integers in the string.”

Question 1.14 Trace the below algorithm for

(a) n = 3,
(b) n = 4,
(c) n = 6.

1. Input n. (n a positive number.)


2. sum ← 0
3. For i = 1 to n do
3.1. sum ← sum + i 2

4. Output sum.

Question 1.15 Trace the below algorithm for

(a) n = 12,
(b) n = 90,
(c) n = 80.
1. Input a non-negative integer n.
2. order ← 0
3. While n is even do
3.1 n ← n

3.2 order ← order + 1


4. Output order.

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 list of numbers x , x1 2, … , xn .


2. min ← x , position ← 1
1

3. For i = 2 to n do
3.1If x < min then
i

3.1.1
min ← xi

3.1.2
position ← i

4. Output min, position.

Question 1.17 Trace the below algorithm for

(a) the string 04m3s8,


(b) the string 732r9,
(c) the string 890.

1. Input string x , x , … , x .
1 2 n

2. i ← 1
3. noninteger _ detected ← f alse
4. Repeat
4.1. Ifxi is not an integer then
4.1.1. noninteger _ detected ← true
i ← i + 1

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.”

Question 1.18 Rewrite the above algorithm using returns.


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 de ning two object to be the
same.
2 This is actually a simpli cation, the return statement exits the function in which it occurs, either with or without a value, and
returns control to the calling function.
CHAPTER 2
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.

2.1 WHOLE NUMBERS


Decimal numbers are often called base-ten numbers. Decimal numbers can be written in expanded form as
follows,

0
8 = 8 × 10 ,

1 0
72 = 7 × 10 + 2 × 10 ,

2 1 0
401 = 4 × 10 + 0 × 10 + 1 × 10 ,

3 2 1 0
8925 = 8 × 10 + 9 × 10 + 2 × 10 + 5 × 10 .

It is important to remember that 10 = 1 and to remember the order of operations. The exponents are done rst,
0

then all the multiplications are done from left to right, then all the additions are done from left to right. In the
above example the number 10 is called the base. Notice that when we use a base 10 the coef cients of the powers
of ten, that is, the numbers that are in front of the powers of ten, range from 0 to 9. These coef cients are also
call digits.
We can write numbers with different bases. For example, instead of using a 10 as we did above we can use a 2.
Numbers that use a two as a base are called either base-two numbers or binary numbers. The coef cients of the
powers of two range from 0 to 1. In other words, binary numbers have the digits 1 and 0. Binary numbers are
very important in computer science because these are the numbers that computers actually use. Computers use
electricity, and either there is an electrical current or there isn't. This means computers can only “understand”
two states; is there an electrical current present or is there no electrical current1 present? These two states are
often called called 1 and 0. So any numbers computers use can only have two coef cients, or digits. Numbers
that only have two digits are binary numbers and have a base of 2. Binary numbers can be written in expanded
form as follows

0
1 = 1 × 2 ,

1 0
10 = 1 × 2 + 0 × 2 ,

2 1 0
101 = 1 × 2 + 0 × 2 + 1 × 2 ,

3 2 1 0
1011 = 1 × 2 + 0 × 2 + 1 × 2 + 1 × 2 .

Again, remember that 2 = 1 and remember the order of operations. We always need to know what base a
0

number is written in. Sometimes it is obvious what base the numbers should have. Sometimes it isn't. When it is
not clear what base a number is written in sometimes a subscript is used. A subscript of 2 is used to indicate a
binary number and a subscript of 10 is used to indicate a decimal number.
110 2 is a base-two, or binary, number,

401 10 is a base-ten, or decimal, number,

1011 2 is a base-two, or binary, number,

7203 10 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, 1 = 1 . Therefore, in the below example there is no need to specify which base the digits are in
2 10

since they are the same in both bases.

Example 2.1

Convert 1102 to a decimal number.

2 1 0
110 2 = 1 × 2 + 1 × 2 + 0 × 2

=4 10 =2 10 =1 10

= 1 × 4 10 + 1 × 2 10 + 0 × 1 10

=4 10 =2 10 =0 10

= 4 10 + 2 10 + 0 10

= 6 10

Convert 10112 to a decimal number.

3 2 1 0
1011 2 = 1 × 2 + 0 × 2 + 1 × 2 + 1 × 2

= 1 × 8 10 + 0 × 4 10 + 1 × 2 10 + 1 × 1 10

= 8 10 + 0 10 + 2 10 + 1 10

= 11 10

Very quickly binary numbers become dif cult for us to understand. For example, consider the number

1000111110101010011100011101

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 dif cult for us to use them. So there are two other basis that are very important in computer
science, base-eight and base-sixteen.
For base-eight numbers instead of using a 10 or a 2 we use an 8 as the base. Base-eight numbers are also called
octal numbers. The coef cients of octal numbers range from 0 to 7. This means that octal numbers use the digits
. Sometimes we know from context that a number is octal, but sometimes it is not clear. In this
0, 1, 2, 3, 4, 5, 6, 7

case we often use a subscript of 8 to indicate the number is an octal number. Octal numbers can be written in
expanded form,

0
58 = 5 × 8 ,

1 0
70 8 = 7 × 8 + 0 × 8 ,

2 1 0
372 8 = 3 × 8 + 7 × 8 + 2 × 8 ,

3 2 1 0
6504 8 = 6 × 8 + 5 × 8 + 0 × 8 + 4 × 2 .

Again, it is easy to convert from octal numbers to decimal numbers. It should be clear that 0 in base-eight, or 08
is exactly the same as 0 in base-ten, or 010. Similarly, 1 = 1 and so on up to 7 = 7 . Therefore, in the below
8 10 8 10

example there is no need to specify which base the digits 0 through 7 are since they are the same in both bases.

Example 2.2

Convert 708 to a decimal number.

1 0
70 8 = 7 × 8 + 0 × 8

= 7 × 8 10 + 0 × 1 10

= 56 10 + 0 10

= 56 10

Convert 65048 to a decimal number.

3 2 1 0
6504 8 = 6 × 8 + 5 × 8 + 0 × 8 + 4 × 8

= 6 × 512 10 + 5 × 64 10 + 0 × 8 10 + 4 × 1 10

= 3072 10 + 320 10 + 0 10 + 4 10

= 3396 10

For base-sixteen numbers we use 16 as the base. Base-sixteen numbers are also called hexadecimal numbers.
Like before, we want the coef cients of the powers of 16 to range from 0 to 15. The problem is that the numbers
10 through 15 each have two decimal digits in them. Using these would be very inconvenient. Therefore we use
the capital letters A, B, C, D, E, and F instead. The table below summarizes the correspondence between the
decimal numbers and the digits used for hexadecimal numbers.
This means hexadecimal numbers look something like 3B7, A05C , E2, or D. Hexadecimal numbers can be
written in expanded form

0
D 16 = D × 16 ,

1 0
E2 16 = E × 16 + 2 × 16 ,

2 1 0
3B7 16 = 3 × 16 + B × 16 + 7 × 16 ,

3 2 1 0
A05C 16 = A × 16 + 0 × 16 + 5 × 16 + C × 16 .
Decimal Numbers Hexadecimal Digits
0 10 0
1 10 1
2 10 2
3 10 3
4 10 4
5 10 5
6 10 6
7 10 7
8 10 8
9 10 9
10 10 A
11 10 B
12 10 C
13 10 D
14 10 E
15 10 F

Converting hexadecimal numbers to decimal numbers works just like before, except that we need to change
the hexadecimal digits to decimal digits using the table. As with the binary and octal case it is obvious that
016 = 0 and so on up to 9 = 9 . Therefore, as before we make no effort to specify which base the digits 0
10 16 10

through 9 are since they are the same in both bases. However, when we convert from the digits A through F we
go ahead and specify the base. That is, we write A as 1010 and so on up to writing F as 1510.

Example 2.3

Convert 3B7 16 to a decimal number.

2 1 0
3B7 16 = 3 × 16 + B × 16 + 7 × 16

=11 10

= 3 × 256 10 + 11 10 × 16 10 + 7 × 1 10

= 768 10 + 176 10 + 7 10

= 951 10

Convert A05C 16 to a decimal number.

3 2 1 0
A05C 16 = A × 16 + 0 × 16 + 5 × 16 + C × 16

=10 10 =12 10

= 10 10 × 4096 10 + 0 × 256 10 + 5 × 16 10 + 12 10 × 1 10

= 40960 10 + 0 10 + 80 10 + 12 10

= 41052 10

2.2 FRACTIONAL NUMBERS


We all know what a decimal point is. That is, we know what numbers like 7.4 or 50.69 or 382.207 mean. Often
numbers with a decimal point are called decimal numbers, but we are already using that word to describe base-
ten numbers so here we will call them fractional numbers. Fractional numbers have a point and some digits after
the point. Fractional base-ten numbers can be written in an expanded form just like before,

1 0 −1 −2
50.69 10 = 5 × 10 + 0 × 10 + 6 × 10 + 9 × 10 ,

2 1 0 −1 −2 −3
382.207 10 = 3 × 10 + 8 × 10 + 2 × 10 + 2 × 10 + 0 × 10 + 7 × 10 .

Here we need to remember how to work with negative exponents,

−1 1 1
10 = 1
= = 0.1 10 ,
10 10

−2 1 1
10 = 2
= = 0.01 10 ,
10 100

−3 1 1
10 = 3
= = 0.001 10 ,
10 1000

and so on. Thus, if we take a closer look we can see what is really happening,

50.69 10 = 5 × 10 + 0 × 1 + 6 × 0.1 + 9 × 0.01

1 0 −1 −2
= 5 × 10 + 0 × 10 + 6 × 10 + 9 × 10 .

Fractional binary numbers can be written in expanded form similarly,

2 1 0 −1 −2
101.01 2 = 1 × 2 + 0 × 2 + 1 × 2 + 0 × 2 + 1 × 2 ,

3 2 1 0 −1 −2 −3
1101.101 2 = 1 × 2 + 1 × 2 + 0 × 2 + 1 × 2 + 1 × 2 + 0 × 2 + 1 × 2 .

To convert fractional binary numbers to fractional decimal numbers we need to know the negative powers of two,

−1 1 1
2 = 1
= = 0.5 10 ,
2 2

−2 1 1
2 = 2
= = 0.25 10 ,
2 4

−3 1 1
2 = 3
= = 0.125 10 .
2 8

Example 2.4

Convert 10.112 to a decimal number.

1 0 −1 −2
10.11 2 = 1 × 2 + 0 × 2 + 1 × 2 + 1 × 2

= 1 × 2 10 + 0 × 1 10 + 1 × 0.5 10 + 1 × 0.25 10

= 2 10 + 0 10 + 0.5 10 + 0.25 10

= 2.75 10
Convert 101.012 to a decimal number.

2 1 0 −1 −2
101.01 2 = 1 × 2 + 0 × 2 + 1 × 2 + 0 × 2 + 1 × 2

= 1 × 4 10 + 0 × 2 10 + 1 × 1 10 + 0 × 0.5 10 + 1 × 0.25 10

= 4 10 + 0 10 + 1 10 + 0 10 + 0.25 10

= 5.25 10

Fractional octal numbers of course work exactly the same way,

2 1 0 −1 −2
731.42 8 = 7 × 8 + 3 × 8 + 1 × 8 + 4 × 8 + 2 × 8 ,

3 2 1 0 −1 −2 −3
1605.734 8 = 1 × 8 + 6 × 8 + 0 × 8 + 5 × 8 + 7 × 8 + 3 × 8 + 4 × 8 .

In order to convert fractional octal numbers to fractional decimal numbers we need to know the negative powers
of eight,

−1 1 1
8 = 1
= = 0.125 10 ,
8 8

−2 1 1
8 = 2
= = 0.015625 10 ,
8 64

−3 1 1
8 = 3
= = 0.001953125 10 .
8 512

This starts to get messy, but for the questions in this book use all digits after the point. In other words, do not
round.

Example 2.5

Convert 3.78 to a decimal number.

0 −1
3.7 8 = 3 × 8 + 7 × 8

= 3 × 1 10 + 7 × 0.125 10

= 3 10 + 0.875 10

= 3.875 10

Convert 731.428 to a decimal number.

2 1 0 −1 −2
731.42 8 = 7 × 8 + 3 × 8 + 1 × 8 + 4 × 8 + 2 × 8

= 7 × 64 10 + 3 × 8 10 + 1 × 1 10 + 4 × 0.125 10 + 2 × 0.015625 10

= 448 10 + 24 10 + 1 10 + 0.5 10 + 0.03125 10

= 473.53125 10
Fractional hexadecimal numbers behave similarly, they just use the digits 0 through F. Fractional hexadecimal
numbers can be written in expanded form,

1 0 −1 −2
8A.41 16 = 8 × 16 + A × 16 + 4 × 16 + 1 × 16 ,

2 1 0 −1 −2 −3
A0D.4EB 16 = A × 16 + 0 × 16 + D × 16 + 4 × 16 + E × 16 + B × 16 .

In order to convert fractional hexadecimal numbers to fractional decimal numbers we need to know the negative
powers of 16,

−1 1 1
16 = 1
= = 0.0625 10 ,
16 16

−2 1 1
16 = 2
= = 0.00390625 10 ,
16 256

−3 1 1
16 = 3
= = 0.000244140625 10 .
16 4096

Example 1.6

Convert C.3 16 to a decimal number.

0 −1
C.3 16 = C × 16 + 3 × 16

= 12 10 × 1 16 + 3 × 0.0625 10

= 12 10 + 0.1875 10

= 12.1875 10

Convert 8A.41 16 to a decimal number.

1 0 −1 −2
8A.41 16 = 8 × 16 + A × 16 + 4 × 16 + 1 × 16

= 8 × 16 10 + 10 10 × 1 10 + 4 × 0.0625 10 + 1 × 0.00390625 10

= 128 10 + 10 10 + 0.25 10 + 0.00390625 10

= 138.25390625 10

2.3 THE RELATIONSHIP BETWEEN BINARY, OCTAL, AND HEXADECIMAL NUMBERS


Earlier we said that very quickly binary numbers become dif cult for us to understand and asked you which of
the numbers, 1000111110101010011100011101 or 1000111110111010011100011101, was larger just by looking
at them. Probably you would have to look a while before guring it out. In a lot of ways binary numbers are
rather dif cult to work with so computer scientists would prefer to be able to work with other number systems
that are easier for a human to understand. It turns out that either octal or hexadecimal numbers are the best
number systems for computer scientists to work with. The reason that these two number systems are convenient
is how easy it is to convert binary numbers to either octal numbers or hexadecimal numbers and then back again.
Because of this ease, computer scientists prefer these systems over the decimal system. With enough practice
they become very easy to use.
It should be easy for you to verify the correspondences shown in the table below. To convert from octal
numbers to binary numbers we just replace each octal digit with its corresponding three digit binary number
from the table.
Binary Numbers Octal Digits
000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7

Example 2.7

Converting octal numbers to binary numbers.

Convert the octal number 348 to a binary number. Notice we can drop the leading zero from the binary
number without changing the value of the number.

34 8 = 3 4

011 100

= 011100 2

= 11100 2

Convert the octal number 170458 to a binary number.

17043 8 = 1 7 0 4 3

001 111 000 100 011

= 001111000100011 2

= 1111000100011 2

Convert the octal number 35.2048 to a binary number. Notice we can also drop the nal zeros after the
point without changing the number. We can of course not drop nal zeros before the point. That would
change the value of the number.

35.204 8 = 3 5 . 2 0 4

011 101 010 000 100

= 011101 . 010000100 2

= 11101 . 0100001 2

Converting from binary numbers to octal numbers is equally easy. But pay close attention, it is easy to get
confused.
For a whole number we group the binary digits (before the point if there is one) in sets of three from right to
left, adding leading zeros if necessary, and then replace the three digit binary numbers with their
corresponding octal digits from the table.
For fractional numbers we group the binary digits after the point in sets of three from left to right, adding
zeros at the end if necessary. Then we replace the three digit binary numbers with their corresponding octal
digits.

Example 2.8

Converting binary numbers to octal numbers. The second example illustrates lling in zeros at the start of the
binary number and the third example illustrates lling in zeros at both the beginning and the end of the
number.

Convert 1011100010102 to an octal number.

101110001010 2 = 101 110 001 010

5 6 1 2

= 5612 8

Convert 10011011010012 to an octal number.

1001101101001 2 = 1 001 101 101 001

=001 1 5 5 1

= 11551 8

Convert 10100111.10011012 to an octal number.

10100111.1001101 2 = 10 100 111 . 100 110 1

=010 4 7 4 6 =100

2 4

= 247.464 8

Example 2.9

Which number, 10001111101010100111000111012 or 10001111101110100111000111012, is larger? We


convert the rst binary number into an octal number.

1000111110101010011100011101 2 = 1 000 111 110 101 010 011 100 011 1

=001 0 7 6 5 2 3 4 3

= 1076523435 8
Next we convert the second binary number into an octal number.

1000111110111010011100011101 2 = 1 000 111 110 111 010 011 100 011 1

=001 0 7 6 7 2 3 4 3

= 1076723435 8

Now we compare the two numbers;

1076523435 8 ,

1076723435 8 .

This is a much easier job than comparing the two original binary numbers. But it could be made easier yet by
using hexadecimal numbers.

This table shows the correspondences that are necessary to convert between binary numbers and hexadecimal
numbers. It should be easy for you to verify the relations in the table below. To convert hexadecimal numbers to
binary numbers we just replace each hexadecimal digit with the corresponding binary number.
Binary Numbers Hexadecimal Digits
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F

Example 2.10

Converting hexadecimal numbers to binary numbers.

Convert the hexadecimal number E2 16 to a binary number.

E2 16 = E 2

1110 0010

= 11100010 2

Convert the hexadecimal number 3F 9A 16 to a binary number.


3F 9A 16 = 3 F 9 A

0011 1111 1001 1010

= 11111110011010 2

Convert the hexadecimal number D06.0C 16 to a binary number.

D06.0C 16 = D 0 6 . 0 C

1101 0000 0110 0000 1100

= 110100000110.000011 2

Converting from binary numbers to hexadecimal numbers is similar to converting from binary to octal numbers.
But again, pay close attention to the directions, it is easy to get confused.

For a whole number we group the binary digits (before the point if there is one) in sets of four from right to
left, adding leading zeros if necessary, and then replace the four digit binary numbers with their
corresponding hexadecimal digits from the table.
For fractional numbers we group the binary digits after the point in sets of four from left to right, adding
zeros at the end if necessary. Then we replace the four digit binary numbers with their corresponding
hexadecimal digits.

So, when converting to octal numbers we use groups of three but when converting to hexadecimal numbers we
use groups of four.

Example 2.11

Converting binary numbers to hexadecimal numbers. The second example illustrates lling in zeros at the
start of the binary number and the third example illustrates lling in zeros at both the beginning and the end
of the number.

Convert 1001110101102 to a hexadecimal number.

100111010110 2 = 1001 1101 0110

9 D 6

= 9D6 16

Convert 10100111102 to a hexadecimal number.

1010011110 2 = 10 1001 1110

=0010 9 E

= 29E 16

Convert 1101001011.0011012 to a hexadecimal number.


1101001011.001101 2 = 11 0100 1011 . 0011 01

=0011 4 B 3 =0100

3 4

= 34B.34 16

Example 2.12

Which number, 10001111101010100111000111012 or 10001111101110100111000111012, is larger? We


convert the rst binary number into a hexadecimal number.

1000111110101010011100011101 2 = 1000 1111 1010 1010 0111 0001 1101

8 F A A 7 1 D

= 8F AA71D 16

Next we convert the second binary number into a hexadecimal number.

1000111110111010011100011101 2 = 1000 1111 1011 1010 0111 0001 1101

8 F B A 7 1 D

= 8F BA71D 16

Now we can compare these numbers;

8F AA71D,

8F BA71D.

This is a much easier job than comparing the two original binary numbers and even a little easier than
comparing the octal numbers.

Converting between hexadecimal numbers and octal numbers requires that you use the binary number as an in-
between step.

To convert from a hexadecimal number to an octal number you rst convert the hexadecimal number to a
binary number and then convert the binary number to an octal number.
To convert from an octal number to a hexadecimal number you rst convert the octal number to a binary
number and then convert the binary number to a hexadecimal number.

2.4 CONVERTING FROM DECIMAL NUMBERS


In Sections 1 and 2 we learned to convert binary numbers, octal numbers, and hexadecimal numbers into decimal
numbers. In this section we will go in the other direction; we will convert decimal numbers into binary numbers,
octal numbers, or hexadecimal numbers. To do this conversion we will use an algorithm. Since this class teaches
the basic mathematics necessary for computer science majors, it is important that you get used to working with
algorithms. We will begin with the algorithm for converting a whole decimal number to a binary number.
Algorithm: Converts a whole decimal number to a binary number.

1. Input n. (n is the whole decimal number.)


2. Repeat
2.1Output nmod2.
2.2
n ← n div 2

until n = 0
3. Read outputs in reverse order.

In order to use this algorithm we need to understand what both n mod 2 and ndiv2 mean. Both mod and div
are words that de ne a mathematical operation. You have probably encountered the idea before under a different
word.

= the quotient when n is divided by 2.


ndiv2

nmod2 = the remainder when n is divided by 2.

Let us consider the following long division, which hopefully you remember how to do.

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 nding n mod 2 for any number n the only possible remainders are 0
and 1 and so these are the only possible values that n mod 2 can take.

Example 1.13

Finding ndiv2 and nmod2 for n = 8 and n = 9.

n = 8

n = 9
Example 2.14

Finding ndiv2 and nmod2 for n = 37 and n = 38.

n = 37

We can also use our calculators, 37/2 = 18.5. Here 37 div 2 is the whole number part of the answer, or
18. We get 37 mod 2 by taking the fractional part of the answer and multiplying it by 2, so we have
0.5 × 2 = 1.

n = 38

Using our calculators, 38/2 = 19. Here 38 div 2 is the whole number part of the answer, or 19. We get 38
mod 2 by taking the fractional part of the answer and multiplying it by 2, but since the factional part is
simply 0 we have 0 × 2 = 0.

In order to convert a decimal number into a binary number all we have to do is trace the algorithm and then
use the output.

Example 1.15

Convert 1110 to a binary number. In order to do this we must follow the algorithm. As we follow the algorithm
we will trace it in a table. Tracing the algorithm means we follow it step by step.
Step n Output
1. 11 -
2.1 11 11 mod 2 = 1

2.2 5 ← 11 div 2 -
2.1 5 5 mod 2 = 1

2.2 2 ← 5 div 2 -
2.1 2 2 mod 2 = 0

2.2 1 ← 2 div 2 -
2.1 1 1 mod 2 = 1

2.2 0 ← 1 div 2 -

Notice, when n became 0 the algorithm ended. Now we just have to read off the answer. As we can see from
the algorithm we read the answer in reverse order, which means that we read the output numbers from the
bottom to the top. Thus we have
11 10 = 1011 2 .

When doing this yourselves, you probably will not put as much detail in the trace table as we did. We just
wanted to make this example very clear and easy to understand for you. We will do one more example to make
sure you understand.

Example 2.16

Convert 3710 to a binary number. We will make a table that traces the algorithm.
Step n Output
1. 37 -
2.1 37 37 mod 2 = 1

2.2 18 ← 37 div 2 -
2.1 18 18 mod 2 = 0

2.2 9 ← 18 div 2 -
2.1 9 9 mod 2 = 1

2.2 4 ← 9 div 2 -
2.1 4 4 mod 2 = 0

2.2 2 ← 4 div 2 -
2.1 2 2 mod 2 = 0

2.2 1 ← 2 div 2 -
2.1 1 1 mod 2 = 1

2.2 0 ← 1 div 2 -

Again, when n became 0 the algorithm ended. Now we read the answer in reverse order, which means that we
read the output numbers from the bottom to the top. Thus we have

37 10 = 100101 2 .

So far we have considered whole numbers. But we can convert fractional decimal numbers to binary numbers
as well. Here is the algorithm to do this:

Algorithm: Converts a fractional decimal numbers to binary numbers.

1. Input n and d. (n is a fractional decimal number and d is digits.)


2. i ⟵ 0
3. Repeat
3.1 i ⟵ i + 1
3.2 m ⟵ ←n
3.3 Output ⌊m⌋.
3.4 n ⟵ f rac(m)
until n = 0 or i = d
4. Read outputs in order. (Outputs follow the point.)

Notice that this algorithm requires us to decide how many digits after the point we want to have. In order to
use this algorithm we have to understand what ⌊m⌋ and f rac(m) mean. Fortunately, they are very easy to
understand.
f rac(m) = the fractional part of m

Usually ⌊m⌋ is called the oor of m.

Example 2.17

Find ⌊m⌋ and f rac(m) for m = 93.91.

⌊93.91⌋ = 93

f rac(93.91) = 0.91

Example 2.18

Find ⌊m⌋ and f rac(m) for m = 0.75.

⌊0.75⌋ = 0

f rac(0.75) = 0.75

Example 2.19

Find ⌊m⌋ and f rac(m) for m = 16.

⌊16⌋ = 16

f rac(16) = 0

Example 2.20

Convert 0.4210 to a binary number. Find ve digits after the point. That is, n = 0.42 10 and d = 5.
Step m i Output
1. 0.42 - -
2. 0.42 0 -
3.1 0.42 1 ← 0 + 1 -
3.2 0.84 ← 2(0.42) 1 -
3.3 0.84 1 ⌊0.84⌋ = 0

3.4 0.84 ← f rac(0.84) 1 -


3.1 0.84 2 ← 1 + 1 -
3.2 1.68 ← 2(0.84) 2 -
3.3 1.68 2 ⌊1.68⌋ = 1

3.4 0.68 ← f rac(1.68) 2 -


3.1 0.68 3 ← 2 + 1 -
3.2 1.36 ← 2(0.68) 3 -
Step m i Output
3.3 1.36 3 ⌊1.36⌋ = 1

3.4 0.36 ← f rac(1.36) 3 -


3.1 0.36 4 ← 3 + 1 -
3.2 0.72 ← 2(0.36) 4 -
3.3 0.72 4 ⌊0.72⌋ = 0

3.4 0.72 ← f rac(0.72) 4 -


3.1 0.72 5 ← 4 + 1 -
3.2 1.44 ← 2(0.72) 5 -
3.3 1.44 5 ⌊1.44⌋ = 1

3.4 0.44 ← f rac(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 fth digit in step 3.3. Now we read the outputs in order, placing them
after the point, to give us

0.42 10 ≈ 0.01101 2 .

Of course we could continue nding more digits. The answer we have obtained is not exact, it is just an
approximation. To make this clear we will expand out 0.011012,

−1 −2 −3 −4 −5
0.01101 2 = 0 × 2 + 1 × 2 + 1 × 2 + 0 × 2 + 1 × 2

= 0 × 0.5 10 + 1 × 0.25 10 + 1 × 0.125 10 + 0 × 0.0625 10 + 1 × 0.03125 10

= 0.25 10 + 0.125 10 + 0.03125 10

= 0.40625 10 .

So, to get a more accurate approximation for 0.4210, we would need d to be larger.

In order to convert a mixed decimal number like 37.4210 into a binary number we would need to:

1. Split the number into the whole number part and the fractional number part.
2. Use the rst algorithm on the whole number part.
3. Use the second algorithm on the fractional number part.
4. Join the two results together to get the nal answer.2

Example 2.21

Convert 37.4210 to a binary number nding ve digits after the point.


Finding 37.4210 as a binary number requires us to do the rst algorithm on 3710 to get 1001012 and the second
algorithm on 0.4210 to get 0.011012, both of which we have already done. Joining these two answers together
and we would have 37.42 ≈ 100101.01101 .
10 2

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.

Algorithm: Converts a whole decimal number to an octal number.

1. Input n. (n is the whole decimal number.)


2. Repeat
2.1 Output nmod8.
2.2 n ⟵ ndiv←
until n = 0
3. Read outputs in reverse order.

Example 2.22

Convert 24210 to an octal number. We will not put as much detail into the table as before.
Step n Output
1. 242 -
2.1 242 2
2.2 30 -
2.1 30 6
2.2 3 -
2.1 3 3
2.2 0 -

Notice, when n became 0 the algorithm ended. Now we read the answer in reverse order to give us
242 10= 362 . 8

The algorithm to convert a fractional number to an octal number works the same as in the binary case, only the
two is replaced by an eight.

Algorithm: Converts a fractional decimal number to an octal number.

1. Input n and d. (n is fractional decimal number and d is digits.)


2. i ⟵ 0
3. Repeat
3.1 i ⟵ i + 1
3.2 m ⟵ ←n
3.3 Output ⌊m⌋.
3.4 n ⟵ f rac(m)
until n = 0 or i = d
4. Read outputs in order. (Outputs follow the point.)

Example 2.23

Convert 0.8110 to an octal number. Find six digits after the point. That is, n = 0.81 10 and d = 6.
Step m i Output
1. 0.81 - -
2. 0.81 0 -
3.1 0.81 1 -
3.2 6.48 1 -
3.3 6.48 1 6
3.4 0.48 1 -
3.1 0.48 2 -
3.2 3.84 2 -
3.3 3.84 2 3
3.4 0.84 2 -
3.1 0.84 3 -
3.2 6.72 3 -
3.3 6.72 3 6
Step m i Output
3.4 0.72 3 -
3.1 0.72 4 -
3.2 5.76 4 -
3.3 5.76 4 5
3.4 0.76 4 -
3.1 0.76 5 -
3.2 6.08 5 -
3.3 6.08 5 6
3.4 0.08 5 -
3.1 0.08 6 -
3.2 0.64 6 -
3.3 0.64 6 0
3.4 0.64 6 -

Now we see that i = 6 and can stop. Thus we have 0.81 10 ≈ 0.636560 8 .

Example 1.24

Convert 242.8110 to an octal number using six digits after the point.
We have already used the rst algorithm to nd 242 = 362 10 8 and the second algorithm to nd
0.81 ≈ 0.636560 . Joining these two numbers, we now have 242.81
10 8 10 .
≈ 362.636560 8

Now we consider converting a decimal number to a hexadecimal number. Again, the only difference in the
algorithms is that we now use a sixteen.

Algorithm: Converts a whole decimal number to a hexadecimal number.

1. Input n. (n is the whole decimal number.)


2. Repeat
2.1 Output nmod16.
2.2 n ← n div 16
until n = 0
3. Read outputs in reverse order.

Example 2.25

Convert 362610 to a hexadecimal number.


Step n Output
1. 3626 -
2.1 3636 10 10 = A 16

2.2 226 -
2.1 226 2 10 = 2 16

2.2 14 -
2.1 14 14 10 = E 16

2.2 0 -

Now we read the outputs in reverse order to give us 3626 10 = E2A 16 .

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.

Algorithm: Converts a fractional decimal number to an hexadecimal number.


1. Input n and d. (n is fractional decimal number and d is digits.)
2. i ⟵ 0
3. Repeat
3.1 i ⟵ i + 1
3.2 m ← 16n
3.3 Output ⌊m⌋.
3.4 n ⟵ f rac(m)
until n = 0 or i = d
4. Read outputs in order. (Outputs follow the point.)

Example 1.26

Convert 0.6310 to a hexadecimal number. Find four digits after the point. That is, n = 0.63 10 and d = 4.
Step m i Output
1. 0.63 - -
2. 0.63 0 -
3.1 0.63 1 -
3.2 10.08 1 -
3.3 10.08 1 10 10 = A 16

3.4 0.08 1 -
3.1 0.08 2 -
3.2 1.28 2 -
3.3 1.28 2 1 10 = 1 16

3.4 0.28 2 -
3.1 0.28 3 -
3.2 4.48 3 -
3.3 4.48 3 4 10 = 4 16

3.4 0.48 3 -
3.1 0.48 4 -
3.2 7.68 4 -
3.3 7.68 4 7 10 = 7 16

3.4 0.68 4 -

Now we see that i = 4 and can stop. Reading the outputs in order we have 0.63 10 ≈ 0.A147 16 .

Example 2.27

Convert 3626.6310 to a hexadecimal number nding four digits after the point.
We have already used the rst algorithm to nd 3626 = E2A and the second algorithm to
10 16 nd
0.63 ≈ 0.A147
10 . Joining these two numbers, we now have 3626.63 ≈ E2A. A147 .
16 10 16

2.5 PROBLEMS

Question 2.1 Convert the following binary numbers to decimal numbers.

(a) 101
(b) 110
(c) 111
(d) 1011
(e) 1101
(f) 1010
(g) 10010
(h) 10110
(i) 11111

Question 2.2 Convert the following binary numbers to decimal numbers.

(a) 0110 1001


(b) 1011 1100
(c) 0011 0111
(d) 1111 0101
(e) 1001 0110
(f) 0110 1000
(g) 1011 0110
(h) 0010 1011
(i) 1101 1101

Question 2.3 Convert the following octal numbers to decimal numbers.

(a) 27
(b) 42
(c) 35
(d) 103
(e) 673
(f) 360
(g) 4721
(h) 2451
(i) 7715

Question 2.4 Convert the following octal numbers to decimal numbers.

(a) 7254
(b) 1602
(c) 4640
(d) 17530
(e) 72501
(f) 11101
(g) 365310
(h) 772531
(i) 417524

Question 2.5 Convert the following hexadecimal numbers to decimal numbers.

(a) A3
(b) 9F
(c) 17
(d) 3D2
(e) 9A0
(f) AED
(g) F 001
(h) 6D27
(i) 39CB

Question 2.6 Convert the following hexadecimal numbers to decimal numbers.

(a) 802
(b) 6A3
(c) F 2E
(d) 290B
(e) 4C71
(f) 1101
(g) ABCDE
(h) 2E916
(i) 97CA0

Question 2.7 Convert the following binary numbers to decimal numbers.

(a) 0.11
(b) 0.01
(c) 0.10
(d) 0.110
(e) 0.101
(f) 0.011
(g) 0.0111
(h) 0.0011
(i) 0.1001

Question 2.8 Convert the following binary numbers to decimal numbers.

(a) 100.110
(b) 110.011
(c) 111.001
(d) 1101.1000
(e) 0110.0111
(f) 1011.1001
(g) 1111.1111
(h) 0001.0001
(i) 1100.0101

Question 2.9 Convert the following octal numbers to decimal numbers.

(a) 0.4
(b) 0.1
(c) 0.7
(d) 0.72
(e) 0.03
(f) 0.14
(g) 0.553
(h) 0.407
(i) 0.321

Question 2.10 Convert the following octal numbers to decimal numbers.

(a) 7.5
(b) 4.2
(c) 3.6
(d) 37.63
(e) 56.12
(f) 25.25
(g) 427.014
(h) 510.442
(i) 703.635

Question 2.11 Convert the following hexadecimal numbers to decimal numbers.

(a) 0.7
(b) 0.A
(c) 0.3
(d) 0.A8
(e) 0.4C
(f) 0.82
(g) 0.C48
(h) 0.379
(i) 0.ABC

Question 2.12 Convert the following hexadecimal numbers to decimal numbers.

(a) 9.3
(b) B. E
(c) 4.C
(d) AC. DC
(e) F 2.39
(f) 5E. C5
(g) A04.BB8
(h) 7CF .9F 0
(i) 101.101

Question 2.13 Convert the following octal numbers to binary numbers.

(a) 62
(b) 31
(e) 571
(d) 31.72
(e) 42.15
(f) 72.37
(g) 313.011
(h) 643.026
(i) 211.361

Question 2.14 Convert the following hexadecimal numbers to binary numbers.

(a) A0
(b) 3B
(c) 27
(d) F 2.F 2
(e) 5B.93
(f) 88.7C
(g) 214.3C5
(h) AD2.0BC
(i) 101.011

Question 2.15 Convert the following binary numbers to octal numbers.

(a) 10
(b) 110
(c) 11
(d) 110.111
(e) 011.010
(f) 101.001
(g) 11011.001001
(h) 100011.10111001
(i) 11110001.00011101

Question 2.16 Convert the following binary numbers to hexadecimal numbers.

(a) 1101
(b) 10
(c) 101
(d) 1110.1101
(e) 1011.1010
(f) 0101.0011
(g) 111001011.0011011
(h) 1011101.01011010101
(i) 10111100101.100111

Question 2.17 Convert the following decimal numbers to binary numbers.

(a) 7
(b) 3
(c) 6
(d) 28
(e) 39
(f) 79
(g) 381
(h) 643
(i) 569

Question 2.18 Convert the following decimal numbers to octal numbers.

(a) 78
(b) 93
(c) 52
(d) 295
(e) 944
(f) 641
(g) 4862
(h) 5078
(i) 1532

Question 2.19 Convert the following decimal numbers to hexadecimal numbers.

(a) 34
(b) 23
(c) 536
(d) 285
(e) 1897
(f) 8321
(g) 90867
(h) 42778
(i) 28714

Question 2.20 Convert the following decimal numbers to binary numbers. Stop after you have obtained six digits.

(a) 0.5
(b) 0.4
(c) 0.8
(d) 0.32
(e) 0.81
(f) 0.77
(g) 0.239
(h) 0.552
(i) 0.798

Question 2.21 Convert the following decimal numbers to octal numbers. Stop after you have obtained six digits.
(a) 0.9
(b) 0.4
(c) 0.6
(d) 0.83
(e) 0.25
(f) 0.44
(g) 0.482
(h) 0.667
(i) 0.315

Question 2.22 Convert the following decimal numbers to hexadecimal numbers. Stop after you have obtained six
digits.

(a) 0.1
(b) 0.3
(c) 0.9
(d) 0.11
(e) 0.83
(f) 0.47
(g) 0.429
(h) 0.638
(i) 0.314

1 1 This is actually a bit of a simpli cation, there is either a high level of current or a very low level of residual current.
2 2 The technical term for this “joining” is concatenation.
CHAPTER 3
Logic

Logic plays a fundamental role in many areas of computer science, including software
engineering and design, expert systems, and arti cial intelligence. A basic understanding
of logic is necessary for many applications of computer science, as well as for many
future courses.

3.1 PROPOSITIONS AND CONNECTIVES


In this chapter we introduce the basic laws of prepositional logic that are used in the
design of computer systems. Prepositional logic is concerned with statements that can
either be true or false. We learn how to analyse these statements and decide when they
are true and when they are false.

De nition 3.1 A proposition is a statement that is either true or false.

Example 3.1

Some examples of statements that are propositions.

Paris is the capital of Great Britain.


Ten is greater than seven.
Every even number is the sum of two prime numbers.

A proposition may be false, like the rst proposition in the example, or it may be true,
like the second proposition in the example, or you may not know if it is true or false, like
the third proposition in the example. But think about the third proposition in the
example, “Every even number is the sum of two prime numbers.” It is clear that this
statement can only be either true or false. If we know a proposition is true we say the
truth-value of the proposition is true. If we know a proposition is false we say the truth-
value of the propositions is false. Thus the truth-value of a proposition is either true or
false.

Example 3.2
Some examples of statements that are not propositions.

Where are you going?


Close the door.
This statement is false.

The rst statement in this example is a question, it is neither true or false, it is a


request for information. The second statement is a command or order, asking someone to
do something. Again, it is neither true or false. The third statements is an example of a
paradox. If we assume the statement “this statement is false” is true, then the statement
itself says the statement must be false. If we assume “this statement is false” is false,
then the statement itself says the statement must be true. While paradoxes can be
interesting, we will not have anything more to say about them.

Example 3.3

Some ambiguous statements.

Jon is tall.
Chocolate is delicious.
x > 10

Here are some statements that are ambiguous. That means it is not clear if they are
propositions or not. How tall does one have to be to be considered tall? Some people
think chocolate is delicious, some do not. And what is the variable x? Most books would
say these statement are not propositions. Most of the time in computer science you will
not encounter ambiguous statements like this so we will not worry about these kinds of
statements.1
Propositions can be connected together using ve words or phrases called connectives.
There are ve connectives that can be used to connect propositions:

1. not (negation): ¬
2. and: ∧
3. or: ∨
4. if-then (implies): →
5. if-and-only-if (is-equivalent-to): ↔
Notice that each of these ve connectives has a symbol associated with it. You will need
to memorize these symbols. Please be aware that a few books use the symbol ∼ instead
of ¬ for not, which is also sometimes called negation. The phrase “if-then” is also called
“implies” and the phrase “if-and-only-if” is also called “is-equivalent-to.” The ve
connectives can be used to connect propositions into logical expressions. Logical
expressions are often just called expressions. They are also sometimes called compound
statements or just statements. Just like in algebra where one can use a variable to
represent a number, in logic we can use a variable to represent a proposition.

Example 3.4

Using the propositions p = ``TodayisMonday. and ′′


q = ``Itisraining.
′′
write the
following logical expressions in words.

¬q = It is not raining.
p ∧ q= Today is Monday and it is raining.
¬p ∧ q = Today is not Monday and it is raining.

p ∨ q = Today is Monday or it is raining.


p ∨ ¬q = Today is Monday or it is not raining.
p → q = If today is Monday then it is raining. = Today is Monday implies it is
raining.
¬p → q = If today is not Monday then it is raining. = Today is not Monday
implies it is raining.
p ↔ q = Today is Monday if-and-only-if it is raining. = Today is Monday is-

equivalent-to it is raining.
¬p ↔ q= Today is not Monday if-and-only-if it is raining. = Today is not
Monday is-equivalent-to it is raining.

Here we have seen a few of the possible combinations of the connectives. Of course,
the last example is a little silly. It is just meant to show you how to work with variables
and the connectives. In computer science the propositions would be more serious.
You should notice when using connectives that not takes precedence. In other words,
do the negation rst. For example, if we have p ∧ ¬q this really means p ∧ (¬q), we rst
nd ¬q and then we connect with ∧. Also, in logic, parenthesis are used a lot like they
are used in algebra, to help us determine the order in which we perform the operations.
For example, in p ∧ (q ∨ r) we would do q ∨ r rst before connecting p to it with ∧.
Example 3.5

Let p = "My program runs. " and q = "My program contains mistakes.′′ Write
the expression (p ∧ ¬q) ∨ q in words.

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 rst do what is inside the parenthesis,
(p ∧ ¬q) and then connect it to q using ∨. Notice in the rst sentence we use a comma
to separate the part of the compound statement that is in parenthesis. In the second
sentence we use the word “either” in addition to a comma to emphasize this
separation. This is more a matter of writing clearly in English than logic.

3.2 CONNECTIVE TRUTH TABLES


Each of the ve connectives has an associated truth table. A truth table is a table that
helps us understand when logical expressions are true and when they are false. We begin
with the simplest truth table, the truth table for the connective not:
p ¬p

T F
F T

The variable p of course represents a proposition. (Do not be confused, it does not
matter what letter we use as the variable. We could have used q or r or x or something
else.) The rst row of the table shows that if p is true then ¬p is false. The second row
shows that if p is false then ¬p is true. This should be obvious to you. If a proposition is
true then the negation of that proposition must be false. Similarly, if a proposition is
false then the negation of that proposition must be true.

Example 3.6

Let p = “ 2 + 2 = 4.” Clearly p is true. The negation of p is ¬p = “ 2 + 2 ≠ 4,”


which is clearly false. If we let q = “ 3 + 2 = 6” then clearly q is false. The negation
of q is ¬q = “ 3 + 2 ≠ 6,” which is clearly true.

The next truth table is for the connective and:


p q p ∧ q

T T T
T F F
F T F
F F F

Since the connective and actually connects two propositions, the truth table for and
has two propositions in it, p and q. The proposition p could be either true or false.
Similarly, the proposition q could be either true or false. A truth table must have enough
rows to include every possible combination of true or false for the variables in the table.
Therefore the number of rows a truth table has is equal to 2n where n is the number of
proposition variables we have. Here we have two proposition variables, p and q, so the
table must have 2 = 4 rows in it. According to the truth table, the only way to have
2

p ∧ q be true is if both p and q are true. If we think about this in terms of English

sentences it makes sense.

Example 3.7

Let

p = Paris is in France. (True) and q = London is in England. (True)

The whole statement

p ∧ q = Parisis in France and London is in England.

is true. But suppose instead we had

p = Paris is in France. (True) and q = London is in England. (False)

then the whole statement

p ∧ q = Paris is in France and London is in China.

is false since one part of the statement is false. Similarly, if we had


p = Paris is in Japan. (False) and q = London is in England. (True)

then the whole statement

p ∧ q = Paris is in Japan and London is in England.

is false since one part of the statements is false. Finally, if we had

p = Paris is in Japan. (False) and q = London is in China. (False)

then the whole statement

p ∧ q = Paris is in Japan and London is in China.

is false since both parts of the statements are false.

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
p q p ∨ q

F F F

Example 3.8

Let

p = Paris is in France. (True) and q = London is in England.(True)

The whole statement

p ∨ q = Paris is in France or London is in England.

is clearly true since both parts of the statement is true. But suppose we have

p = Paris is in France. (True) and q = London is in England. (False)

The whole statement

p ∨ q = Paris is in France or London is in China.

is still true since one of the parts of the statement is true. Similarly, if we have

p = Paris is in Japan. (False) and q = London is in England. (True)

The whole statement

p ∨ q = Paris is in Japan or London is in England.

is true since again one of the parts of the statement is true. Finally, suppose we had

p = Paris is in Japan. (False) and q = London is in China. (False)


then the whole statement

p ∨ q = Paris is in Japan or London is in China.

is false since both parts of the statement is false.

Example 3.9

Though we will not use it in this chapter, the exclusive-or is important as well in both
logic and Boolean algebra. We simply use xor to represent the exclusive-or. The
expression pxorq is true when only one of the propositions p or q is true. That means
that if both p and q are true then pxorq is false. Here is the truth table for the
exclusive-or.
p q pxorq

T T F
T F T
F T T
F F F

In this case the expression

pxorq = Paris is in France or London is in England.

would be considered false since both of the propositions are true.

The next two connectives, if-then (implies) and if-and-only-if (is-equivalent-to), can
seem a little strange if we rely too heavily on language for our understanding. We can
come up with some very strange examples that do not make much sense. This is because
logic in the real world is usually applied to mathematical propositions, not silly
propositions like “Paris is in France” or “Today is Monday.” Logic, when applied to
mathematical propositions, works perfectly.
Next we will consider the truth table for if-and-only-if. Another way of saying if-and-
only-if is to say is-equivalent-to. In English two things are considered to be equivalent if
they are the same in some way. In logic what is important is the truth-value of
propositions or expressions. Therefore, in logic two statements are considered equivalent
if they have the same truth-value. In other words, two statements are considered
equivalent if they are either both true or both false. Thus the truth table for if-and-only-if
is given by:
p q p ↔ q

T T T
T F F
F T F
F F T

Example 3.10

Let us consider the two true propositions

p = Paris is in France. (True) and q = London is in England. (True)

The whole statement

p ↔ q = Paris is in France if − and − only − if London is in England.

= Paris is in France is − equivalent − to London is in England.

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

p = Paris is in France. (True) and q = London is in China. (False)

The whole statement

p ↔ q = Paris is in France if − and − only − if London is in China.

= Paris is in France is − equivalent − to London is in China.

is now false since one part of the statement is true and the other part is false. But
again, the two parts of the statement seem unrelated to each other. Similarly, if we
had
p = Paris is in Japan. (False) and q = London is in England. (True)

The whole statement

p ↔ q = Paris is in Japan if − and − only − if London is in England.

= Paris is in Japan is − equivalent − to Londoni si n England.

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

p = Paris is in Japan. (False) and q = London is in China. (False)

then the whole statement

p ↔ q = Paris is in Japan if − and − only − if London is in China.

= Paris is in Japan is − equivalent − to London is in China.

is true according to our table since both parts of the statement are false. But again, as
an English sentence it makes very little sense. Just remember, when using logic in
real life, in problems that come from computer science or mathematics, logic works
perfectly.

We can convince ourselves that the if-and-only-if truth table works since we can use
the argument that true statements are equivalent in some sense and false statements are
equivalent in some sense. In other words, it is the truth-value of the propositions that is
important. We will also use this idea to try to understand the if-then truth table. In the
statement “ p → q” the rst part of the statement, p, is called the premise and the second
part of the statement, q, is called the conclusion. Here is one way to think about it:

A true premise can only imply a true conclusion. (So if p is true and q is true then
p → q is also true.)

A true premise can never imply a false conclusion. (So if p is true and q is false
then p → q must be false.)
A false premise, because it is false, can imply anything. (So if p is false and q is
true then p → q is true. But also, if p is false and q is false then p → q is also true. )
This gives the following truth table for the if-then connective:
p q p → q

T T T
T F F
F T T
F F T

Example 3.11

Let

p = Paris is in France. (True) and q = London is in England. (True)

then the whole statement

p → q = If Paris is in France then London is in England.

= Paris is in France implies London is in England.

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

p = Paris is in France. (True) and q = London is in China. (False)

The whole statement

p → q = If Paris is in France then London is in China.

= Paris is in France implies London is in China.

is now false since the premise, or rst part of the statement, is true and the
conclusion, or second part of the statement, is false. But again, the two parts of the
statement seem unrelated to each other. Now suppose we had

p = Paris is in Japan. (False) and q = London is in England. (True)


then the whole statement

p → q = If Paris is in Japan then London is in England.

= Paris is in Japan implies London is in England.

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

p = Paris is in Japan. (False) and q = London is in China. (False)

then the whole statement

p → q = If Paris is in Japan then London is in China.

= Paris is in Japan implies London is in China..

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.

3.3 TRUTH VALUE OF COMPOUND STATEMENTS


When we are given a compound statement we usually want to know if or when the
compound statement is true or false. This can be done in one of two ways. If we already
know the truth-value of each proposition then we can nd out directly. If we are not
given the truth-value of each proposition we need to use a truth table.
When the truth-values of all the individual propositions in a compound statement are
known, we can nd out the truth-value of the compound statement directly. We substitute
the truth-values of the propositions into the compound statement and use the connective
truth-tables. Remember that parenthesis in logic are used just like parenthesis in algebra,
they tell us in what order to evaluate things.

Example 3.12
If p is true and q is false nd the truth value of (p ∧ ¬q) ∨ q.

(T ∧ ¬F ) ∨ F substitute truth-values

≡ (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
connective which is also known as is-equivalent-to. Here the symbol ≡ operates a lot like
the equal sign in algebra. In algebra you would write 2(x + y) = 2x + 2y to say that
both 2(x + y) and 2x + 2y are the same expression just written in different ways. In
logic we use ≡ to say both sides have the same truth-value.

Example 3.13

If q is true nd the truth value of ¬(¬q ∧ ¬q).

¬(¬T ∧ ¬T )

≡ ¬(F ∧ F )

≡ ¬F

≡ T

Example 3.14

If p and q are both false nd the truth value of ¬(p ∧ q) → (¬q ∨ ¬q).

¬(F ∧ F ) → (¬F ∨ ¬F )

≡ ¬(F ∧ F ) → (T ∨ T )

≡ ¬F → T

≡ T → T

≡ T
Often you are asked simply to nd the truth table of a compound statement. This
means you are to nd the truth-value of the compound statement for every possible
proposition truth-value. You need to make and ll-out a truth-table. The number of rows
your truth table needs is 2n where n is the number of individual propositions your
compound statement has. The truth table for ¬(p ∧ q) would need 2 = 4 rows since it
2

has two propositions (p and q); the truth table for (¬p ∧ q) ∨ r would need 2 = 8 rows
3

since it has three propositions (p, q, and r); and the truth table for ¬(p ∨ q) → (¬r ∧ s)
would need 2 = 16 rows since it has four propositions (p, q, r, and s). To gure out the
4

columns a truth table needs it may be helpful to make a expression tree. (Trees will be
studied later in chapter 10.)

Example 3.15

Make an expression tree for ¬(p ∧ q) and use that to construct a truth table.

The construction of this expression tree should be clear, in order to nd ¬(p ∧ q) we


must rst nd (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. 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 rst two columns of the truth table we have lled in every possible
combination of truth values for p and q. The rest of the truth table needs to be lled
in.

Example 3.16
Make an expression tree for (p ∧ ¬q) ∨ q and use that to construct a truth table.

In the expression tree the variables are the vertices at the bottom. The rst thing we
do is nd ¬q. We then ∧ together p and ¬q to get p ∧ ¬q. Finally, we have to ∨ this
together with q to get (p ∧ ¬q) ∨ q. We make a truth table using the vertices of the
expression tree; each vertex results in a column in the truth table. Notice, in the truth
table we do not need to put the variable q in twice, once is enough.
p q ¬q p ∧ ¬q (p ∧ ¬q) ∨ q

T T
T F
F T
F F

Example 3.17

Make an expression tree for ¬(¬p ∧ q) ↔ (¬q ∨ r) and use that to construct a truth
table.

Study ¬(¬p ∧ q) ↔ (¬q ∨ r) and the expression tree until the construction of the tree
is clear. We make the columns in the truth table using the vertices of the expression
tree. We do not need to put the variable q in twice.
p q r ¬p ¬p ∧ q ¬(¬p ∧ q) ¬q ¬q ∨ r ¬(¬p ∧ q) ↔ (¬q ∨ r)

T T T
T T F
p q r ¬p ¬p ∧ q ¬(¬p ∧ q) ¬q ¬q ∨ r ¬(¬p ∧ q) ↔ (¬q ∨ r)

T F T
T F F
F T T
F T F
F F T
F F F

Example 3.18

Fill out the truth table for (p ∧ ¬q) ∨ q.


First we ll 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

Next we ll in the fourth column using the rst and third columns and the truth table
for ∧.

p q ¬q p ∧ ¬q (p ∧ ¬q) ∨ q

T T F F
T F T T
F T F F
F F T F

Finally we ll in the fth column using the second and fourth columns and the truth
table for ∨.
p q ¬q p ∧ ¬q (p ∧ ¬q) ∨ q

T T F F T
T F T T T
F T F F T
F F T F F

With the truth table we can see exactly when the compound statement (p ∧ ¬q) ∨ q is
true and when it is false. It is false when both p and q are false and true otherwise.
Notice this is exactly the same as the truth table for p ∨ q.

3.4 TAUTOLOGIES AND CONTRADICTIONS


A compound statement, or expression, is called a tautology if it is always true,
regardless of the proposition truth-values. An expression is called a contradiction if it is
always false, regardless of the proposition truth-values.

Example 3.19

Show that ¬(p ∧ q) → (¬p ∨ ¬q) it a tautology. We rst construct the expression
tree.

We use the expression tree to construct the truth table which we then ll out.
p q p ∧ q ¬(p ∧ q) ¬p ¬q ¬p ∨ ¬q ¬(p ∧ q) → (¬p ∨ ¬q)

T T T F F F F T
T F F T F T T T
F T F T T F T T
F F F T T T T T

We can see that the expression ¬(p ∧ q) → (¬p ∨ ¬q) is true regardless of the truth-
values of the propositions. Thus ¬(p ∧ q) → (¬p ∨ ¬q) is called a tautology. We
sometimes say that ¬(p ∧ q) → (¬p ∨ ¬q) is tautologically true.

Example 3.20

Show that (p ∧ q) ∧ ¬p is a contradiction. We rst 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.

We then use the expression tree to construct the truth table which we then ll out.
p q p ∧ q ¬p (p ∧ q) ∧ ¬p
p q p ∧ q ¬p (p ∧ q) ∧ ¬p

T T T F F
T F F F F
F T F T F
F F F T F

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.

3.5 LOGICAL EQUIVALENCE AND THE LAWS OF LOGIC


Two expressions that contain the same variables are called logically equivalent if they
have the same truth-values for every combination of variable truth-values. The symbol
used for this is ≡. In logic, the symbol ≡ operates a lot like the symbol = in algebra. The
symbol ≡ is read “is equivalent to,” but do not confuse this with the connective is-
equivalent-to.

Example 3.21

Show that the expressions ¬(p ∧ q) and ¬p ∨ ¬q are logically equivalent. This
requires constructing the truth table for each expression.
p q p ∧ q ¬(p ∧ q)

T T T F
T F F T
F T F T
F F F T

and
p q ¬p ¬q ¬p ∨ ¬q

T T F F F
T F F T T
F T T F T
F F T T T

Notice how the two expressions have the same truth-values for every combination of
variable truth-value. When both p and q are true then both expressions are false.
When p is true and q is false then both expressions are true. When p is false and q is
true then both expressions are true. When both p and q are false then both expressions
are false. Thus the expression ¬(p ∧ q) is logically equivalent to the expression
¬p ∨ ¬q . This is written as
¬(p ∧ q) ≡ ¬p ∨ ¬q.

This equivalence is the rst of DeMorgan's laws.

Example 3.22

Show that the expressions ¬(p ∨ q) and ¬p ∧ ¬q are logically equivalent. We do this
by constructing truth tables for the two expressions.
p q p ∨ q ¬(p ∨ q)

T T T F
T F T F
F T T F
F F F T

and
p q ¬p ¬q ¬p ∧ ¬q

T T F F F
T F F T F
F T T F F
F F T T T

Again, the two expressions have the same truth-values for every combination of
variable truth-value. When both p and q are true then both expressions are false.
When p is true and q is false then both expressions are false. When p is false and q is
true then both expressions are false. When both p and q are false then both
expressions are true. Thus the expression ¬(p ∨ q) is logically equivalent to the
expression ¬p ∧ ¬q,

¬(p ∨ q) ≡ ¬p ∧ ¬q.

This equivalence is the second of DeMorgan's laws.

Example 3.23

Show that the expressions p → q and ¬q → ¬p are equivalent. The expression


¬q → ¬p is called the contrapositive of p → q . We make the truth tables. The rst is
simply the truth-table for the if-then connective.
p q p → q

T T T
T F F
F T T
F F T

and
p q ¬p ¬q ¬q → ¬p

T T F F T
T F F T F
F T T F T
F F T T T

Thus we see that these two expressions are equivalent. We write

p → q ≡ ¬q → ¬p.

This may surprise you, but this equivalence is actually one of the most important
relationships in mathematics and computer science. As we said, the expression ¬q → ¬p
is called the contrapositive of p → q. It is a fundamental way that mathematicians and
computer scientists prove things. Many times you want to show a statement p → q is
true but showing this statement is true is dif cult while it is easy to show the
contrapositive ¬q → ¬p is true. But since these two statements are equivalent then
showing the contrapositive of p → q is true is the same thing as showing that p → q is
true.
There are two special laws that are almost like de nitions instead of laws. They allow
us to write → and ↔ in terms of ¬, ∧, and ∨. These can be shown to be true in exactly
the same way as in the last example.
Name Laws
Implication law p → q ≡ ¬p ∨ q

Equivalence law p ↔ q ≡ (p → q) ∧ (q → p) ≡ (¬p ∨ q) ∧ (¬q ∨ p)

There is also a standard list of equivalence relations that are often called the laws of
logic. Each of these laws of logic can be shown to be true just as in the examples above.
We can construct the truth tables for the expression on each side of the ≡ sign and
compare them. It is a good idea for you to do that for these laws and convince yourself
that they are true. We have already done this for both of DeMorgan's laws.
Name Laws of Logic
Commutative laws (a) p ∧ q ≡ q ∧ p (b) p ∨ q ≡ q ∨ p
Associative laws (a) (p ∧ q) ∧ r ≡ p ∧ (q ∧ r) (b) (p ∨ q) ∨ r ≡ p ∨ (q ∨ r)
Distributive laws (a) p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r) (b) p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r)
Identity laws (a) p ∧ T ≡ p (b) p ∨ F ≡ p
Name Laws of Logic
Inverse laws (a) p ∧ ¬p ≡ F (b) p ∨ ¬p ≡ T
Double negation law ¬¬p ≡ p

Idempotent laws (a) p ∧ p ≡ p (b) p ∨ p ≡ p


DeMorgan's laws (a) ¬(p ∧ q) ≡ ¬p ∨ ¬q (b) ¬(p ∨ q) ≡ ¬p ∧ ¬q
Annihilation laws (a) p ∧ F ≡ F (b) p ∨ T ≡ T
Absorption laws (a) p ∧ (p ∨ q) ≡ p (b) p ∨ (p ∧ q) ≡ p
Negation laws (a) ¬T = F (b) ¬F = T

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 ¬\big[p → ¬(p ∧ q)\big] 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
Example 3.26

Simplify the expression ¬\big[p ∨ (q ∧ ¬p)\big] 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.1 Are the following statements propositions?

(a) Cats eat a lot.


(b) How are you?
(c) Paris is in China
(d) 17 = 16
(e) 7 > 13
(f) 12 + 15 = 27
(g) Go y a kite.
(h) Close the door!
(i) Roses are owers.

Question 3.2 Let p = “Jon plays baseball” and q = “Ron plays football.” Write the
following logical statements in words.

(a) p ∨ q
(b) q ∧ p
(c) ¬p ∧ q
(d) p ∧ ¬q
(e) ¬q ∨ ¬p
(f) ¬(q ∨ p)

Question 3.3 Suppose p is true and q is false. Find the truth value of the following
compound statements.

(a) p ∧ q
(b) q ∧ p
(c) ¬q ∨ p
(d) ¬p ∨ q
(e) p ∧ ¬q
(f) ¬q ∨ ¬p
(g) p → q
(h) ¬q → p
(i) ¬q → ¬p
(j) p ↔ q
(k) ¬q ↔ p
(l) ¬p ↔ ¬q

Question 3.4 Construct the truth table for the following compound statements.

(a) p ∧ q
(b) q ∧ p
(c) ¬q ∨ p
(d) ¬p ∨ q
(e) p ∧ ¬q
(f) ¬q ∨ ¬p
(g) p → q
(h) ¬q → p
(i) ¬q → ¬p
(j) p ↔ q
(k) ¬q ↔ p
(l) ¬p ↔ ¬q
Question 3.5 Show the Implication law p → q ≡ ¬p ∨ q by constructing a truth table for
each side of the implication and showing these two truth tables are equivalent.

Question 3.6 Show the Equivalence law p ↔ q ≡ (p → q) ∧ (q → p) by constructing a


truth table for each side of the implication and showing these two truth tables are
equivalent. Then use the Implication law to rewrite p ↔ q ≡ (p → q) ∧ (q → p) in
terms of ∧, ∨, and ¬ only.

Question 3.7 Construct the truth table for the following compound statements.

(a) (p ∨ ¬r) → q
(b) (q ∧ p) → ¬p
(c) ¬p → (¬p ∨ q)
(d) ¬(p ∧ ¬q) ∨ r
(e) ¬[(¬p ∧ q) ∨ r]
(f) (p ∧ ¬q) ∧ (r ∨ q)
(g) (p ∨ q) ↔ [p ∨ (q ∧ r)]
(h) (q ∧ ¬r) ↔ ¬(p ∨ r)
(i) (p ↔ q) ∧ (r ↔ q)

Question 3.8 (Easy) Simplify the following Logical expressions with the laws of logic.

(a) p ∨ p
(b) p ∨ ¬p
(c) ¬(p ∨ ¬p)
(d) p ∨ (p ∧ q)
(e) p ∨ (¬p ∧ q)
(f) p ∧ (¬p ∨ q)
(h) p ∧ p
(h) p ∧ (p ∨ q)
(i) p ∧ (p ∨ q ∨ r)
(j) (p ∧ q) ∨ (p ∧ ¬q)
(k) ¬(¬p ∨ ¬p)
(l) (p ∧ q) ∨ (¬p ∧ q)

Question 3.9 (Moderate) Simplify the following Logical expressions with the laws of
logic.
(a) (¬p ∨ ¬q) ∧ (¬p ∨ q)
(b) q ∨ (q ∧ ¬q)
(c) ¬p ∨ (q ∧ ¬p)
(d) (p ∨ ¬q) ∧ (p ∨ q)
(e) r ∨ [r ∨ (r ∧ p)]
(f) p ∧ [p ∨ (p ∧ q)]
(g) r ∨ (r ∧ ¬p ∧ q ∧ s)
(h) ¬r ∧ ¬(r ∧ p ∧ q ∧ s)

Question 3.10 (Dif cult) Simplify the following Logical expressions with the laws of
logic.

(a) q ∧ [p ∨ (¬p ∧ q)]


(b) ¬[(p ∧ ¬q) ∨ ¬p]
(c) (¬p ∧ q) ∨ [p ∧ (p ∨ q)]
(d) ¬[p ∨ (p ∧ q)] ∧ q
(e) (p ∧ ¬r) ∨ (¬p ∧ q) ∨ ¬(q ∧ r)
(f) ¬(p ∧ q) ∨ (q ∧ r)
(g) [p ∨ (q ∧ r)] ∧ (¬p ∨ r)
(h) (p ∧ ¬r) ∨ (¬p ∧ q) ∨ ¬(p ∧ r)
(i) (p ∧ r) ∨ (¬p ∧ q) ∨ (r ∧ q)
(j) ¬p ∨ ¬q ∨ [p ∧ q ∧ ¬r]
(k) (p ∨ r) ∧ (¬p ∨ q) ∧ (r ∨ q)
(l) ¬(p ∧ q) ∧ (¬p ∨ q) ∧ (¬q ∨ q)

Question 3.11 Which of the following statements are tautologies?

(a) (p ∧ ¬q) → ¬q
(b) (¬p ∧ q) → ¬q
(c) (¬p ∧ q) → ¬p
(d) (¬p ∨ q) → ¬p

Question 3.12 Which of the following statements are contradictions?

(a) (p ∨ q) ∧ (¬p ∧ ¬q)


(b) (p ∨ q) ∧ (p ∧ ¬q)
(c) (¬p ∨ ¬q) ∧ (¬p ∧ q)
(d) (p ∨ ¬q) ∧ (¬p ∧ q)
1 These kinds of statements do play a role in advanced computer science, such as with arti cial intelligence,
fuzzy logic, probabilistic logic, and so on. But at our level we will not worry about them.
CHAPTER 4
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
de nitions and ideas that you will need to know later.

4.1 SET NOTATION


A set is a well-de ned collection of objects. The objects in a set are usually called the elements of the set, though
sometimes they are also called the members of the set. The phrase well-de ned just means that it is clear if an
object is in the set or not. Sets are usually indicated by curly-brackets { }.

Example 4.1

Some examples of sets.

\big{apple, orange, banana\big} is a set that has three elements, the words apple, orange, and banana.
{1, 2, 3, 4, 5} is a set that has ve elements, the numbers 1, 2, 3, 4, and 5.
{1, 2, 3, … , 50} is a set that has fty elements, the positive integers 1 through 50.
{2, 4, 6, 8, 10, 12, …}is a set that contains all the positive even integers. This set has an in nite number
of elements since there are an in nite number of positive even integers.
{a, b, c} is a set that contains the letters a, b, and c.
{a, b, c, … , z} is a set that contains the lowercase letters of the alphabet.

If x is an element of the set S then we write x ∈ S . They symbol ∈ is read “is an element of.” If x is not an
element of set S then we would write x ∉ S . The symbol ∉ is read “is not an element of.”

Example 4.2

Elements of sets.

apple ∈ \big{apple, orange, banana\big} but pear ∉ \big{apple, orange, banana\big}.


4 ∈ {1, 2, 3, 4, 5} but 6 ∉ {1, 2, 3, 4, 5}.
13 ∈ {1, 2, 3, … , 50} but 157 ∉ {1, 2, 3, … , 50}.
8342 ∈ {2, 4, 6, 8, 10, 12, …} but 6329 ∉ {2, 4, 6, 8, 10, 12, …}.

Now we will introduce some of the most important sets in mathematics and computer science.
N

R
=


the set of natural numbers

the set of positive integers

{1, 2, 3, 4, …},

the set of integers numbers

= {… , −4,

{x x =
n
− 3, − 2,

the set of rational numbers

m
− 1, 0, 1, 2, 3, 4, …},

where m, n ∈ Z and n ≠ 0},

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 counting numbersintegers, the rational numbers are often simply called the
rationals, and the real numbers are often simply called the reals. It is dif cult to give a precise de nition of the
real numbers, but you should be familiar with them as the real number line. That is a good way to think about the
real numbers.
Also, notice how we de ned the rational numbers ℚ. Sets can be written in one of two different ways. They
can be written in an enumerated formenumerated form (of set) or in a predicate form. Most of the sets above
were written in an enumerated form. To enumerate simply means to list, and in most of the above examples you
can see that we simply listed the elements in the set inside the curly-brackets. Writing a set in predicate form
means we use an expression to help us de ne the set. This is how we de ned predicate form (of set)ℚ above. A
predicatepredicate is a statement involving one or more variables such that when we assign values to the
variables the statement becomes a proposition which is either true for false. We write P (x) to mean a predicate
of x and read it as “pee-of-ex.”
Sets in predicate form are written either as

{x | P (x)},

which is read as “the set of x such that P (x) is true,” or

{x ∈ S | P (x)},

which is read as “the set of x in S such that P (x) is true.”


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 that x comes from S. 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 satis ed 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

Suppose that x ∈ Z. The statement

5 ≤ x ≤ 10

is a predicate of x. If we assign the value 7 to the variable x the statement becomes the proposition
5 ≤ 7 ≤ 10, which is true. If we assign the value −2 to the variable x the statement becomes the proposition

5 ≤ −2 ≤ 10, which is false.


Example 4.4

Suppose that x ∈ R. The statement

m
x = where m, n ∈ Z and n ≠ 0
n

is a predicate in x. If we assign the value 3.74 to the variable x we have the statement

m
3.74 = where m, n ∈ Z and n ≠ 0
n

which is true since for m = 374 ∈ Z and n = 100 ∈ Z we have 3.74 =


374

100
. If we assign the value π to the
variable x then we have the statement

m
π = where m, n ∈ Z
n

which is false since there are no values m, n ∈ Z such that π = m

n
.

Example 4.5

Examples of sets written in predicate form.

{x ∈ N | xmod2 = 0} = The set of positive even numbers.


{x ∈ N | xmod2 = 1} = The set of positive odd numbers.
{x ∈ N|5 ≤ x ≤ 10} = {5, 6, 7, 8, 9, 10 }

Q = {x x =
m

n
f or m, n ∈ Z, n ≠ 0} = The set of numbers m

n
where both m and n are elements of ℤ
and n ≠ 0= The set of all fractions.

Now consider the sets {1, 7, 3} and {3, 7, 1}. These are both the same set so we will call them equal to each
other. The order in which we write the elements does not matter at all. In fact, repeated elements do not matter
either. Now consider the sets {1, 7, 3} and {1, 3, 1, 7}. These are also the same set even though the element 1 was
repeated twice in the second set.

Example 4.6

Examples of equal sets.

{1, 7, 3} = {1, 3, 1, 7}

{2, 4, 6, 8, 10} = {10, 8, 10, 6, 10, 4, 10, 4, 10, 2, 6, 8}


1 1 2 1 1 1 1 2 2 2
{ , , } = { , , , , , , }
3 2 3 3 3 3 2 3 3 3

1 1 2 3 4 5 6
{ } = { , , , , , , …}
2 2 4 6 8 10 12

{
1

3
} = {
1

3
,
2

6
,
3

9
,
4

12
,
15
5
,
6

18
, …} There are many different ways to write any fraction.

There is one last, but very important, point that needs to be made. Sets can be elements of sets. This can cause
a lot of confusion if you are not careful. Let us consider the following set,

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.

But we would also say

{b, c} ∈ S, {a, b, c} ∈ S, {a, d, h} ∈ S, and {h, 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.

4.2 SET OPERATIONS


Let A and B be sets. We say that B is a subset of A if every element of B is an element of A. If B is a subset of A
we write B ⊆ A. If B contains an element that is not in A then B is not a subset of A and we write B⊈A. Notice
that since set A contains every element of set A that A is a subset of A. That is, A ⊆ A. Clearly we have A = A
too. That is why ⊆ has an extra line under the ⊂, it shows that the sets could possibly be equal. However, if we
have B ⊆ A but B ≠ A then B is said to be a proper subset of A and we write B ⊂ A.

Example 4.7

Some examples of subsets.

{2, 3, 5} ⊆ {1, 2, 3, 4, 5}. Furthermore, since {2, 3, 5} ≠ {1, 2, 3, 4, 5} then {2, 3, 5} is a proper subset
of {1, 2, 3, 4, 5} and we can also write {2, 3, 5} ⊂ {1, 2, 3, 4, 5}.
. Again, since {2, 4, 6, 8} ≠ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} we have
{2, 4, 6, 8} ⊆ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

a proper subset and could also write {2, 4, 6, 8} ⊂ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.


. This is a very special subset. It is the empty subset. Clearly { } ≠ {2, 4, 6, 8, 10} so
{ } ⊆ {2, 4, 6, 8, 10}

we could also write { } ⊂ {2, 4, 6, 8, 10}.


. Here, we have {1, 2, 3, 4} = {1, 2, 3, 4} so we do not have a proper subset and
{1, 2, 3, 4} ⊆ {1, 2, 3, 4}

we would write {1, 2, 3, 4} ⊂ {1, 2, 3, 4}.


N ⊆ Z ⊆ Q ⊆ R . By looking at our de nitions it is clear that we could also write N ⊂ Z ⊂ Q ⊂ R.
If A is a set then A ⊆ A. Clearly we also have A ⊂ A since A = A.

In the above example we saw the empty set, { } ⊆ {2, 4, 6, 8, 10}. The empty set is exactly that, a set that is
empty. Notice we did not put any elements at all inside the curly-brackets. The curly-brackets are empty. Every
set has the empty set as a subset. The empty set is so special it has its own symbol,

ϕ = {}.

The empty set is also called the null set. Null is an old-fashioned word meaning nothing. It is interesting to
notice that the empty set can be written in predicate form using a predicate that is always false,

ϕ = {x|x ≠ x}.

Clearly, there are no values of x that are not equal to themselves, thus this set has no values in it; it is empty.
Now let us return to the example we gave in the last section where some of the elements of the set S were also
sets themselves,

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,

S = { a , b , c , {b, c} , {a, b, c} , d , {a, d, h} , h , i , {h, i} }.

If we write {b, c} ⊂ S then what we mean is

{b, c} ⊂ S.

In other words {b, c} is the subset of S that contains the elements b and c written in gray. But if we write
{{b, c}} ⊂ S then what we mean is

{{b, c}} ⊂ S.

In other words {{b, c}} is the subset of S that contains the element {b, c} written in gray boldface. The subset
{b, c, {b, c}} would of course mean

{b, c, {b, c}} ⊂ S


where b, c, and {b, c} are all elements of S.

Example 4.8

Using the same set as before we give some examples of both elements and subsets to help you understand the
difference;

S = { a , b , c , {b, c} , {a, b, c} , d , {a, d, h} , h , i , {h, i} }.

The following are all elements in S;

a ∈ S, b ∈ S, c ∈ S, d ∈ S, h ∈ S, i ∈ S.

The following are all also elements in S;

{b, c} ∈ S, {a, b, c} ∈ S, {a, d, h} ∈ S, {h, i} ∈ S.

The following are all subsets of S that contain one element from S;

{a} ⊂ S, {b} ⊂ S, {c} ⊂ S, {d} ⊂ S, {h} ⊂ S, {i} ⊂ 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.
The intersection of two sets A and B is given by

A ∩


B = {x x ∈ A and x ∈ B }.

That is, the intersection of two sets A and B consists of the set of elements that are in set A and also in set B.
Another way of saying this is that the intersection of two sets consists of the elements that are common to both
sets.

Example 4.9

Intersections of sets.

If A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7} then A ∩ B = {4, 5}.


If A = {1, 2, 3, 4, 5, 6, 7, 8} and B = {2, 4, 6, 8, 10, 12} then A ∩ B = {2, 4, 6, 8}.
If A = {2, 4, 6, 8, 10, 12, …} and B = {1, 3, 5, 7, 9, 11, …} then A ∩ B = { } = ∅.

In the above example when A was the set of even numbers and B was the set of odd numbers we saw that
A ∩ B = ∅ . In other words, there are no elements common to the set of even numbers and the set of odd
numbers. A number simply cannot be both even and odd at the same time. These sets are called disjoint. If A and
B are sets and A ∩ B = ∅ then A and B are said to be disjoint.
The union of two sets A and B is given by

A ∪ B = {x x ∈ A or x ∈ B}.

That is, the union of two sets A and B consists of all the elements in A together with all the elements in B.

Example 4.10

Unions of sets.

U is.
If A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7} then A ∪ B = {1, 2, 3, 4, 5, 6, 7}.
If A = {1, 2, 3, 4, 5, 6, 7, 8} and B = {2, 4, 6, 8, 10, 12} then A ∪ B = {1, 2, 3, 4, 5, 6, 7, 8, 10, 12}.
If A = {2, 4, 6, 8, 10, 12, …} and B = {1, 3, 5, 7, 9, 11, …} then A ∪ B = N.

The complement of a set A is the set of all elements in the universal set but not in A. The complement of A is
written as Ac or as A or as A′. In order to nd the complement of a set you need to know what the universal set

Example 4.11

Complements of sets.

If U
If U
= {1, 2, 3, 4, 5, 6} and A = {2, 4, 6} then A
= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
c
= {1, 3, 5}

and A = {1, 2, 3, 9, 10} then A


.
c
= {4, 5, 6, 7, 8} .
If U = N and A = {2, 4, 6, 8, 10, 12, …} then A c
= {1, 3, 5, 7, 9, 11, …} .

The difference (of sets) between sets A and B is given by

A − B = {x | x ∈ Aand x ∉ B }.

That is, A − B is the set of elements in A that are not also in B.

Example 4.12

Differences of sets.

If A = {1, 2, 3, 4, 5, 8, 10} and B = {1, 5, 6, 7, 10} then A − B = {2, 3, 4, 8}.


If A = {4, 5, 6, 7, 8, 9} and B = {1, 3, 5, 7, 9, 11} then A − B = {4, 6, 8}.
If A = {2, 4, 6, 8, 10, 12, …} and B = {1, 3, 5, 7, 9, 11, …} then A − B = A.

The cardinality of a nite set is the number of elements in the set. If A is a set the cardinality of A is written as
|A| or as n(A). If the set has an in nite number of elements then we say the cardinality of the set is in nite.
Since the empty set has no elements then the cardinality of the empty set is zero.

Example 4.13

Cardinality of sets.

If A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} then |A| = 10.


If A = {2, 4, 6, 8, 10, 12, …} then |A| = ∞, that is, |A| is in nite.
If A = { } = ∅ then |A| = |∅| = 0.

The power set of a set A is the set that contains all the subsets of set A. The power set of A is written as P(A)
. Notice, here we are talking about a set that actually has sets as its elements. That is entirely possible.

Example 4.14

We will nd the power set of the set A = {a, b, c}. Notice that A has three elements, the letters a, b, and c. We
list the subsets of A:

∅, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}.

Notice that the empty set is a subset of A and the set A is a subsets of itself. These eight sets are the elements
of the power set of A. The power set of A is given by

P(A) = {∅, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}}.
Given a subset of A, for each element in A there are two possibilities, either that the element is in the subset or
it is not in the subset. Let us look at a few more examples to see how this works.

Example 4.15

Cardinality of power sets.

Let A = {a}. The set A only has one element. There are two possible subsets, one that has the element a
in it, {a}, and one that does not have the element a in it, { }. So A has 2 = 2 subsets and 1

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 ⋅ 2

Either a is in Either b is in

subset or not. subset or not.

f our possibilities

which give a total of 2 ⋅ 2 = 4 possibilities. That means there are 2


2
= 4 subsets,
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 ⋅ 2 ⋅ 2

Either a is in Either b is in Either c is in

subset or not. subset or not. subset or not.

eight possibilities

which give a total of 2 ⋅ 2 ⋅ 2 = 8 possibilities. This means A has 2 3


= 8 subsets. The power set is given
in the last example.

By now the pattern should be clear. This leads to the following theorem.
Theorem 4.1 If |A| = n then |P(A)| = 2 . n

Example 4.16

Supposes A = {a, b, c, d}. How many subsets does A have? How many proper subsets does A have?

The power set is the set that contains all the subsets of A, so the cardinality of the power set is equal to the
number of subsets that A has. Since |A| = 4 then P(A) = 2 = 16. Thus A has 16 subsets. However, one of
4

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
nd the power of two that is equal to 128. Since 2 = 128 we have |A| = 7. In other words, A has seven
7

elements.

Recall, that in sets the order we write the elements does not matter. Thus {a, b, c} is exactly the same as
{b, c, a}or {a, c, b} or {b, a, c} and so on. But there are many cases in computer science where order is very
important. We want a way to think about things that have an order. An ordered n-tuple is a list of n elements in a
particular order. Ordered n-tuples are written in parenthesis instead of curly-brackets to distinguish them from
sets. So (2, 3) is an ordered two-tuple while {2, 3} is a set and (3, −2, 4) is an ordered three-tuple while
{3, −2, 4} is a set. But while the order of elements in a set does not matter, the order of the terms in an ordered

n-tuple does matter. Thus,

(3, − 2, 4) ≠ (−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 ℝ2 the point (2, −1) is very different from the point (−1, 2). Similarly, in three-
dimensional space ℝ3 the point (−2, 5, 4) is very different from the point (5, −2, 4), which is different from the
point (4, −2, 5), and so on.

Figure 4.1 Points in the two-dimensional plane ℝ2 are ordered two-tuples.


Figure 4.2 Points in three-dimensional space ℝ3 are ordered three-tuples.

Can we have a set that consists of ordered n-tuples as elements? Of course we can. The Cartesian product3 of
two sets A and B is de ned to be

A × B = {(x, y) | x ∈ Aand y ∈ B}.

That is, A × B is the set of all ordered pairs where the rst term in the ordered pair comes from set A and the
second term in the ordered pair comes from B. Similarly, the Cartesian product of three sets A, B, and C is
de ned to be

A × B × C = {(x, y, z) | x ∈ Aand y ∈ Band z ∈ C}.

That is, A × B × C is the set of all ordered three-tuples where the rst term in the ordered three-tuple comes
from set A, the second term in the ordered three-tuple comes from B, and the third term in the ordered three-
tuple comes from C. In general we have

S 1 × S 2 × ⋯ × S n = {(x 1 , x 2 , … , x n )|x 1 ∈ S 1 , x 2 ∈ S 2 , ⋯ , x n ∈ S n }.

Example 4.18

Cartesian products of sets.

Let A = {a, b, c} and let B = {2, 4}. Then

A × B = {(a, 2), (b, 2), (c, 2), (a, 4), (b, 4), (c, 4)}.

The set of points in the plane are given by

R × R = {(x, y) | x ∈ R and y ∈ R}.


In fact, this explains why the plane is often written as ℝ2, since using the rule of exponents we can write
R = R × R. See Fig. 4.2.
2

The set of points in three-dimensional space are given by

R × R × R = {(x, y, z) | x ∈ R, y ∈ R and z ∈ R}.

In fact, this explains why three-dimensional space is often written as ℝ3, since using the rule of
exponents we can write R = R × R × R. See Fig. 4.2.
3

Let L = {a, b, c, d, … , x, y, z}. The set of all strings of four letters is given by L × L × L × L.

4.3 VENN DIAGRAMS


Venn diagrams are very common in set theory. They are a very powerful tool to help you visualize what is
happening in set theory. But be careful! Venn diagrams are only a picture, or cartoon, that helps us understand
what is going on. They are not always accurate. In fact, at times they can be misleading. However, they are very
helpful to understand what set operations do. We will use Venn diagrams to show pictures of some of the set
operations from the last section.
Look at Fig. 4.3. Venn diagrams always include the universal set which is usually shown as a box that surrounds
everything else and is often labeled U . However, very often for the universal set one does not bother to write the
label U down. The set A is shown as the oval. We imagine the elements of set A as being inside the oval labeled
A. The elements that are not inside set A are outside the oval but still inside the “universe” box. The following
example should make this clear.

Figure 4.3 The universal set

and the set A.

Example 4.19

In Fig. 4.4 we have the universal set U which consists of all the lowercase letters from the English alphabet.
There are also two sets in U , set A and set B. The letters in set A are shown inside the circle labeled A and the
elements in set B are shown inside circle B. Find sets A and B, and then nd A ∩ B, A ∪ B, Ac, Bc, (A ∪ B) , c

and (A ∪ B) . c

Figure 4.4 An illustration of how Venn diagrams work. Here the universal set

U
U

consists of the lowercase letters from the English alphabet. The uppercase letters A and B represent the two sets shown as circles.

We nd sets A and B by listing all the elements that are in the circle labeled A and in the circle labeled B.

A = {p, x, r, c, e, m, n, j, a, s, b},

B = {j, a, s, b, k, f , g, z, w, y, v}.

We nd the sets A ∩ B, A ∪ B, Ac, Bc, (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},

c
A = {i, u, t, o, h, l, d, q, k, f , g, z, w, y, v},

c
B = {i, u, t, o, h, l, d, q, p, x, r, c, e, m, n},
c
(A ∪ B) = {i, u, t, o, h, l, d, q},
c
(A ∩ B) = {p, x, r, c, e, m, n, k, f , g, z, w, y, v, i, u, t, o, h, l, d, q}.

It is also of course possible to draw a Venn diagram with more than two sets. Fig. 4.5 is a Venn diagram with
three sets A, B, and C along with elements shown. A lot of times Venn diagrams are drawn with the number of
elements in each region instead of with the actual elements themselves. An example of this is given in Fig. 4.6.
When we do this we do not know what the elements are and so we cannot write out the sets A, B, or C in
enumerated form. However, often we are more interested in simply knowing how many elements are in each set.
In other words, we only care about the cardinalities of the set. We can still use Venn diagrams for this.

Figure 4.5 A Venn diagram with three sets, A, B, and C, and with elements shown.

Figure 4.6 In this Venn diagram the number of elements in each region is indicted, not the actual elements themselves.
Example 4.20


Using the Venn diagram in Fig. 4.6 nd |A|, |B|, |C|, |A ∩ B|,
|A ∪ C|, |B ∪ C|, |A ∪ B ∪ C|, |A |, and |(A ∪ B ∪ C) |.

(A
A


B
c


|A |

C)
|A|

|B|

|C|

c
=

=
3,
c

9 + 5 + 3 + 4 = 21

12 + 5 + 3 + 7 = 27,

8 + 7 + 3 + 4 = 22,

5 + 3 = 8,

4 + 3 = 7,

7 + 3 = 10,
|A ∩ C|

9 + 4 + 5 + 3 + 12 + 7 = 40,

9 + 5 + 4 + 3 + 8 + 7 = 36,

12 + 5 + 7 + 3 + 8 + 4 = 39,
,

9 + 5 + 12 + 4 + 3 + 7 + 8 = 48,

12 + 7 + 8 + 11 = 38,

11.
|B ∩ C| , |A ∩ B ∩ C|

In a general situation we often use shading to indicate the elements in a set. In Fig. 4.6(a) the set U is shaded
which indicates we are considering all the elements in the “universe.” In Fig. 4.7(b) the set A is shaded which
indicates we are considering all the elements in the set A. In Fig 4.7(c) the set B is shaded indicating we are
considering all the elements in set B. Figs. 4.7(d) through 4.7(k) show all the other possibilities as well. These
pictures are a very nice way to visualize what union and intersection and complement all mean. Be careful
though, with more then three sets Venn diagrams can start to be misleading and confusing.
, |A ∪ B| ,
Figure 4.7 Using shading to indicate various regions in Venn diagrams.

4.4 THE LAWS OF SET THEORY


Just like there are laws of logic, there are also laws of set theorylaws of set theory. If you compare the laws of
logic with the laws of set theory you will see that they are basically the same. That is because the set operations
of intersection, union, and complement correspond in a natural way to the logical connectives and, or, and not.
The laws of logic could be used to simplify complicated logical expressions. Similarly, the laws of set theory can
be used to simplify complicated set theory expressions. Given a universal set U , the set operations ∩, ∪, and c

operate on subsets of the universal set. Thus we say that ∩, ∪, and operate on elements of P(U ), the powerset
c

of U .
Venn diagrams can be very useful in helping us understand the laws of set theory. But be careful, Venn diagrams
can be used to understand the laws of set theory, but they cannot be used to actually prove the laws of set theory.
To prove the laws of set theory you need to use the actual de nitions of the operations.
Name Laws of Set Theory
Commutative laws (a) A ∩ B = B ∩ A (b) A ∪ B = B ∪ A
Associative laws (a) (A ∩ B) ∩ C = A ∩ (B ∩ C) (b) (A ∪ B) ∪ C = A ∪ (B ∪ C)
Distributive laws (a) A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) (b) A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)
Identity laws (a) A ∩ U = A (b) A ∪ ∅ = A
Inverse laws (a) A ∩ A = ∅ c
(b) A ∪ A = U
c

Double complement law c


(A )
c
= A

Idempotent laws (a) A ∩ A = A (b) A ∪ A = A


DeMorgan's laws (a) (A ∩ B) = A ∪ B
c c c
(b) (A ∪ B) = A ∩ B
c c c

Annihilation laws (a) A ∩ ∅ = ∅ (b) A ∪ U = U


Absorption laws (a) A ∩ (A ∪ B) = A (b) A ∪ (A ∩ B) = A
Complement laws (a) U = ∅ c
(b) ∅ = U
c
Example 4.21

Use Venn diagrams to argue that the double complement law is true.

On the left the set A is shown. On the right we see Ac. But just by looking at these pictures it is obvious that
(A ) must be the same as A.
c c

Example 4.22

Use Venn diagrams to argue the rst of DeMorgan's laws, (A ∩ B) c


= A
c
∪ B
c

On the left we see set A ∩ B and on the right we see (A ∩ B) .c

On the left we show Ac. In the middle we show Bc. On the right we see the union of these two sets, A c
∪ B
c
.

4.5 BINARY RELATIONS ON SETS


In computer science relations on sets are important when working with databases. We use the language of set
theory when discussing relations. Suppose A and B are sets. A binary relation from set A to set B is a subset of
the cartesian product A × B. Usually this subset is called R. The word binary is used to indicate that there are
two sets involved, sets A and B. If we were to let sets A = B then we would say that a binary relation on set A is
a subset of A × A. Very often a binary relation is simply called a relation. We use some of the same terms from
function theory. The domain and range of a relation R are given by

domain(R) = {x|(x, y) ∈ R},

range(R) = {y|(x, y) ∈ R}.

If (x, y) ∈ R then we often write xRy. This means that x is related to y.

Example 4.23
Let A = {1, 3, 5, 7} and B = {2, 3, 4, 5}. First we nd the Cartesian product of sets A and B,

A × B = {(1, 2), (1, 3), (1, 4), (1, 5), (3, 2), (3, 3), (3, 4), (3, 5),

(5, 2), (5, 3), (5, 4), (5, 5), (7, 2), (7, 3), (7, 4), (7, 5)}.

The relation “is equal to” is given by the following subset of A × B,

R = {(3, 3), (5, 5)} ⊂ A × B.

Since (3, 3) ∈ R then we would write 3R3 and would read it out loud as “3-are-3.” Since the relation is “is
equal to” then 3R3 could also be read out loud as “3 is equal to 3.” Also, since (5, 5) ∈ R then we would write
that as 5R5 and read it as “5-are-5” or as “5 is equal to 5.” We also have

domain(R) = {3, 5} ⊂ A,

range(R) = {3, 5} ⊂ B.

We can also draw binary relations quite easily. The oval on the left represents set A and its elements are
shown inside the oval. The oval on the right represents set B and its elements are shown inside the oval. This
is very similar to how we draw Venn diagrams. The relation is represented as arrows between the elements of
set A and set B. For example, since (3, 3) ∈ R ⊂ A × B then there is an arrow from 3 ∈ A to 3 ∈ B. And
since (5, 5) ∈ R ⊂ A × B then there is another arrow from 5 ∈ A to 5 ∈ B.

Example 3.24

Let A = {1, 3, 5, 7} and B = {2, 3, 4, 5}. The Cartesian product A × B was found in the last example. The
relation “is less than” is given by the following subset of A × B,

R = {(1, 2), (1, 3), (1, 4), (1, 5), (3, 4), (3, 5)} ⊂ A × B.

Since (1, 2) ∈ R then we can write 1R2 which can be read “1-are-2” or “1 is less than 2.” Similarly,
(1, 3) ∈ R can be written as 1R3 or read as “1-are-3” or as “1 is less than 3,” and so on. We also have

domain(R) = {1, 3} ⊂ A,

range(R) = {2, 3, 4, 5} ⊂ B.
This relation is also shown in the following picture. Since (1, 2), (1, 3), (1, 4), (1, 5) ∈ R ⊂ A × B then we
have arrows going from 1 ∈ A to 2, 3, 4, 5 ∈ B. Since (3, 4), (3, 5) ∈ R ⊂ A × B then we have arrows going
from 3 ∈ A to 4, 5 ∈ B.

Example 4.25

Let A = {1, 3, 5, 7} and B = {2, 3, 4, 5}. The relation “is greater than” is given by the following subset of the
Cartesian product A × B,

R = {(3, 2), (5, 2), (5, 3), (5, 4), (7, 2), (7, 3), (7, 4), (7, 5)} ⊂ A × B.

Since (3, 2) ∈ R then we can write 3R2 which can be read “3-are-2” or “3 is greater than 2.” Similarly,
(5, 2) ∈ R can be written as 5R2 and read as “5-are-2” or as “5 is greater than 2,” and so on. We also have

domain(R) = {3, 5, 7} ⊂ A,

range(R) = {2, 3, 4, 5} ⊂ B.

This relation is also shown in the following picture,

When working with databases we may have relations that are not related to mathematics. Here are some
simple examples of relations that one could encounter when working with databases.

Example 4.26

Suppose A = {Sam, Bob, T im} and B = {F ido, H unter, Rex}. The relation OwnsDog is given by the
following subset of the Cartesian product A × B,

OwnsDog = {(Sam, F ido), (Bob, Rex), (T im, H unter)} ⊂ 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.

Example 4.27

Suppose A = {J ohn, Sally, Anne, T ed, Bill, Ed, J ane}. We have a relation called ChildOf given by the
following subset of the Cartesian product A × A,

ChildOf = {(J ohn, Sally), (T ed, Bill), (J ohn, T ed),

(Bill, Ed), (Bill, Anne), (T ed, J ane)} ⊂ A × A.

Since we have (J ohn, Sally) ∈ ChildOf we have “John ChildOf Sally” which is easy to understand means
that John is a child of Sally. “Ted ChildOf Bill” of course means Ted is a child of Bill, and so on. Can you
draw a family tree based on this relation?

When R is a binary relation on a nite set A, that is, when R ⊂ A × A, then there is another common way to
visualize the relation R as a directed graph. The set A becomes the vertex set of the directed graph and the
elements (x, y) ∈ R are used to make the edge set. The element (x, y) indicates a directed edge from vertex x to
vertex y. We will discuss directed graphs more in chapter 9, but for now we just want to point out the connection
between set theory and graph theory.

Example 4.28

Suppose A = {1, 2, 3} and we have the relation

R = {(1, 1), (1, 2), (3, 2)} ⊂ A × A.

The relation R can be depicted as a directed graph by

We interpret (1, 1) ∈ R as meaning one is sent to one, we interpret (1, 2) ∈ R as meaning one is sent to two,
and we interpret (3, 2) ∈ R as meaning three is sent to two.

Example 4.29
Functions can be de ned in terms of a binary relation. We will consider this in depth in chapter 6, but for now
we just want to point out the connection. Consider the function f shown below.

The domain is given by X = {1, 2, 3} and the codomain is given by Y = {a, b, c} . The Cartesian product of X
and Y is given by

X × Y = {(x, y)|x ∈ X, y ∈ Y }

= {(1, a), (2, a), (3, a), (1, b), (2, b), (3, b), (1, c), (2, c), (3, c) }.

The function f is a subset of X × Y given by

f = {(1, a), (2, c), (3, b)} ⊂ X × Y .

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 re exive if xRx for every x ∈ A.


2. R is called irre exive if there are no elements x ∈ A such that xRx.
3. R is called symmetric if xRy implies yRx for all x, y ∈ A.
4. R is called antisymmetric if xRy and yRx implies x = y for all x, y ∈ A.
5. R is called transitive if xRy and yRz implies xRz for all x, y, z ∈ A.

Example 4.30

Let A = {a, b, c} with relation R on A given by

R = {(a, a), (b, b), (c, c)} ⊂ A × A.

This means we have aRa, bRb, and cRc. In other words, we have xRx for every x ∈ A, which means that R is
a re exive relation.

Example 4.31

Let A = {a, b, c} with relation R on A given by


R = {(a, b), (b, c), (c, a)} ⊂ A × A.

This means we have aRb, bRc, and cRa. It is clear by looking at these that there is no element x ∈ A such
that xRx, which means that R is an irre exive relation.

Example 4.32

Let A = {1, 2, 3} with the relation R on A given by

R = {(1, 2), (2, 1), (2, 3), (3, 2)} ⊂ A × A.

Clearly we have 1R2 and 2R1 as well as 2R3 and 3R2. We can see that if xRy is in R then yRx is also in R.
Thus R is a symmetric relation.

Example 4.33

Let us consider the set ℕ of natural numbers with the relation R given by

R = {(x, y)|x ≤ y} ⊂ N × N.

The relation R is also known as “is less than or equal to.” For example, (1, 2) ∈ R since 1 ≤ 2. But also
(1, 1) ∈ R since 1 ≤ 1. Suppose that for x, y ∈ N we have both xRy and yRx. In other words, we have that

x ≤ y and y ≤ x. The only way this can happen is if x = y. This means that R is an antisymmetric relation.

Example 4.34

Let A = {1, 2, 3} with the relation R given by

R = {(1, 2), (2, 3), (1, 3)} ⊂ A × A.

We have 1R2 and 2R3 and also 1R3. Thus this is a transitive relation.

There are two more relations that are extremely important in computer science and mathematics. These
relations are de ned using the relations listed above.

1. A relation is called an equivalence relation if it is re exive, symmetric, and transitive. equivalence relation
2. A relation is called a partial ordering if it is re exive, antisymmetric, and transitive. partial ordering

Example 4.35

Let A = {1, 2, 3, 4, 5, 6} with the relation given by

{
R = {(1, 1), (1, 3), (1, 5), (3, 1), (3, 3), (3, 5), (5, 1),

(5, 3), (5, 5), (2, 2), (2, 6), (6, 2), (6, 6), (4, 4)} ⊂ A × A.

To check that R is re exive we need to check that xRx for every x ∈ A. This is exactly

(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6) ∈ R.

Thus R is re exive. To check that R is symmetric we must check that if xRy then yRx. This is exactly

(1, 3), (3, 1) ∈ R, (1, 5), (5, 1) ∈ R,

(3, 5), (5, 3) ∈ R, (2, 6), (6, 2) ∈ R.

Thus R is symmetric. To check that R is transitive we must check that if xRy and yRz then . This is
xRz

exactly

(1, 3), (3, 5), (1, 5) ∈ R.

Thus R is transitive. Therefore R is an equivalence relation on A.

Example 4.36

The relation ≤ de ned on ℕ is a partial ordering on ℕ. We have x ≤ x for any x ∈ N so ≤ is re exive. For
every x, y ∈ N we have that if x ≤ y and y ≤ x then x = y. This makes ≤ antisymmetric. And for any
x, y, z ∈ N we have that if x ≤ y and y ≤ z then x ≤ z. This makes ≤ transitive. Since ≤ is re exive,

antisymmetric, and transitive then it is a partial ordering.

If A is a set then a partitionpartition of A is a set of subsets of A such that every element of A is in exactly one
of the subsets. If R is an equivalence relation on A then for each x ∈ A we can de ne a subset

E(x) = {y ∈ A|yRx}.

The sets E(x) are called the equivalence classes of Rpartition and the set E(x) is called the equivalence class of
x. The set of equivalence classes of R is a partition of the set A. Thus equivalence relations can be used to
partition a set into subsets called equivalence classes.

Example 4.37

Let A = {1, 2, 3, 4, 5, 6} with the relation given by

R = {(1, 1), (1, 3), (1, 5), (3, 1), (3, 3), (3, 5), (5, 1),

(5, 3), (5, 5), (2, 2), (2, 6), (6, 2), (6, 6), (4, 4)} ⊂ A × A.
Using the de nition of equivalence class we nd that

E(1) = {1, 3, 5},

E(2) = {2, 6},

E(3) = {1, 3, 5},

E(4) = {4},

E(5) = {1, 3, 5},

E(6) = {2, 6}.

Clearly E(1) = E(3) = E(5) and E(2) = E(6) . Thus we have partitioned set A into three equivalence
classes,

A = {1, 3, 5} ∪ {2, 6} ∪ {4}.

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} . Determine if the following


statements are True or False.

(a) 4 ∈ A
(b) 6 ∈ A
(c) r ∈ A
(d) b ∈ A
(e) f ∈ A
(f) 1 ∈ A
(g) 7 ∈ B
(h) 9 ∈ B
(i) z ∈ B
(j) q ∈ B
(k) 8 ∈ B
(l) y ∈ B
(m) b ∉ A
(n) 8 ∉ A
(o) g ∉ A
(p) c ∉ A
(q) 5 ∉ A
(r) 3 ∉ A
(s) 5 ∉ B
(t) s ∉ B
(u) u ∉ B
(v) 8 ∉ B
(w) 5 ∉ B
(x) v ∉ B

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
(b) {2, 4, 6, 8} ⊂ A
(c) {g, 5, 6, j} ⊂ A
(d) {b, a, c} ⊂ A
(e) {a, 8} ⊂ A
(f) { } ⊂ A
(g) {9, 8, 7} ⊂ B
(h) {v, x, 8, 9} ⊂ B
(i) {v} ⊂ B
(j) { } ⊂ B
(k) {3, 6, 9} ⊂ B
(l) {z, 2} ⊂ B
(m) {2, 4, 6, 8} ⊂ A

(n) {a, e, i} ⊂ A

(o) {a, b} ⊂ A

(p) {4, h, 2} ⊂ A

(q) { } ⊂ A

(r) {9} ⊂ A

(s) {6, 8, t} ⊂ B

(t) {x, y, z} ⊂ B

(u) {8, 6} ⊂ B

(v) { } ⊂ B

(w) {v, r, 8, 2} ⊂ B

(x) {2, y, 3} ⊂ B
Question 4.3 Let U = {1, 2, 3, a, b, c, {a, b}, {1, 3}, {1, a, 3, c}}. Determine if the following statements are True
or False.
(a) 2 ∈ U (f) {b, a} ∈ U (k) {1, 2} ⊂ U (p) {1, a, 3, c} ⊂ U
(b) c ∈ U (g) {b, c} ∈ U (l) {1, 3} ⊂ U (q) {{1, a, 3, c}} ⊂ U
(c) {1, 2} ∈ U (h) {3, c, 1, a} ∈ U (m) {1, 2, 3} ⊂ U (r) {{3, 1}} ⊂ U
(d) {1, 3} ∈ U (i) b ∈ U (n) {2, {3, 1}} ⊂ U (s) {{b, c}, b, c} ⊂ U
(e) d ∈ U (j) a ∈ U (o) {\big{{a, b, c}, a, b} ⊂ U } (t) { } ⊂ U

Question 4.4 Find all the subsets of the following sets. Which of these subsets are proper subsets?

(a) {a}
(b) {5}
(c) {v}
(d) {a, b}
(e) {5, 7}
(f) {5, e}
(g) {2, 4, 6}
(h) {x, y, z}
(i) {2, b, c}

Question 4.5 Find all the subsets of the following sets. Which of these subsets are proper subsets?

(a) {{a, b, c}}


(b) {{x, y}}
(c) {{1, 2, 3}}
(d) {r, {s, t}}
(e) {{x}, {y, z}}
(f) {{2, 4}, 6}
(g) {2, {4}, 6}
(h) {{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
(b) A ∩ B
(c) Ac
(d) Bc
(e) (A ∪ B) c

(f) (A ∩ B) c

(g) A ∪ B
c c

(h) A ∩ B
c c

(i) A − B
(j) B − A

Question 4.7 Let U = {1, 2, 3, 4, 5, a, b, c, d, e} , A = {1, 2, a, b, c, d} , and B = {2, 3, 4, c, d, e} . Find the


following sets.

(a) A ∪ B
(b) A ∩ B
(c) Ac
(d) Bc
(e) (A ∪ B) c

(f) (A ∩ B) c

(g) A ∪ B
c c

(h) A ∩ B
c c

(i) A − B
(j) B − A
(b) A ∩ B
(c) Ac
(d) Bc

(h) A ∩ B

(g) {x
c

(i) A − B
(j) B − A


Question 4.8 Let
following sets.

(a) A ∪ B

(e) (A ∪ B)
(f) (A ∩ B)
(g) A ∪ B
c

(b) {x|(x + 1)(x − 1) = 0}


(c) {x|(x − 5)(x + 4) = 0}
(d) {x
(e) {x x

(i) {x|x = −x}

(g) {x|x = 2x}


x

x
2

(x
3

(x + 5)(x
c

= 16}

(h) {x|(x + 3)(x − 2) = 0}


c
U = {o, p, q, r, s, t, u, v, w, x, y, z} , A = {o, p, s, t, u, y}

Question 4.9 Write the following sets in enumerated form. We may assume x is from the set ℤ.

(a) {x x
2
= 4}

= 27}

= 25}

(f) {x|(2x − 8)(5x + 15) = 0}

Question 4.10 Write the following sets in enumerated form. We may assume x is from the set ℝ.

(a) {x x
3
= −1}

(b) {x|(2x − 3)(4x + 7) = 0}


(c) {x|(2x + 5)(3x + 4) = 0}
(d) {x 2

= 1}
− 4)(x − 3) = 0}

(e) {x|(5 − x)(7 + 3x) = 0}


(f) {x 3

(h) {x|(3x − 4)(2 − 3x) = 0}


(i) {x

Question 4.11 Given the sets


2
− 9) = 0}

O
=

=
{n ∈ N|n > 100},

{n ∈ N|n ≤ 300},

{n ∈ N|n is even},

{n ∈ N|n is odd}
, and B = {p, r, s, t, u, v} . Find the
nd the cardinality of the following sets.

(a) Ac
(b) Bc
(c) A c
∩ B
c

(d) Oc
(e) O ∩ E
(f) Ec
(g) A ∪ B
(h) B ∩ E
(i) A ∩ O
c

(j) O ∩ E c

(k) A ∩ O c

(l) B ∪ E

Question 4.12 If A = {1, 2, 3, 4, 5, 6} how many subsets does A have? How many proper subsets does A have?

Question 4.13 If B = {a, b, c, d, w, x, y, z} how many subsets does B have? How many proper subsets does B
have?

Question 4.14 If C = {1, 3, 5, 7, 9, r, s, t, u, v} what is the cardinality of the power set of of C?

Question 4.15 If D = {1, 2, 3, 4, 5, … , 20} what is the cardinality of the power set of of D?

Question 4.16 If A = {a, b, c} and B = {1, 2, 3} nd the Cartesian product A × B.

Question 4.17 If S = {e, f , g, h} and T = {9, 8, 7, 6} nd the Cartesian product S × T .

Question 4.18 If X = {0, 1, 2, 3, 4, 5} and Y = {t, f } nd the Cartesian product X × Y .

Question 4.19 If , X = {0, 1}, X = {0, 1}, and X = {0, 1}


X 1 = {0, 1} 2 3 4 nd the Cartesian product
X1 × X2 × X3 × X4 .
Question 4.20 Using the Venn diagram nd the sets A, B, A ∪ B, A ∩ B, Ac, Bc, , , ,
c c c
(A ∪ B) (A ∩ B) A ∪ B

and A ∪ B , A − B, B − A.
c

Question 4.21 Using the Venn diagram nd the sets A, B, , , Ac, Bc, , , ,
c c c
A ∪ B A ∩ B (A ∪ B) (A ∩ B) A ∪ B

and A ∪ B , A − B, B − A.
c
Question 4.22 Using the Venn diagram below to nd the following: A, B, C, A ∩ B, A ∩ C , B ∩ C , A ∪ B,
c c c
A ∪ C , B ∪ C , A ∩ B ∩ C , A ∪ B ∪ C , A , B , C , (A ∩ B) , (A ∩ C) , (B ∩ C) , (A ∪ B) , (A ∪ C) ,
c c c c c

(B ∪ C) , (A ∩ B ∩ C) , (A ∪ B ∪ C) , A − B, B − A, A − C , C − A, B − C , and C − B.
c c c

Question 4.23 Using the Venn diagram below to nd the following: A, B, C, A ∩ B, A ∩ C , B ∩ C , A ∪ B,


c c c
A ∪ C , B ∪ C , A ∩ B ∩ C , A ∪ B ∪ C , A , B , C , (A ∩ B) , (A ∩ C) , (B ∩ C) , (B ∪ C) , (A ∪ C) ,
c c c c c

(B ∪ C) , (A ∩ B ∩ C) , (A ∪ B ∪ C) , A − B, B − A, A − C , C − A, B − C , and C − B
c c c

Question 4.24 Using the Venn diagram below to nd the following: |A|, |B|, |C|, |A ∩ B|, |A ∩ C|, |B ∩ C|,
|A ∪ B|, |A ∪ C|, |B ∪ C|, |A ∩ B ∩ C|, |A ∪ B ∪ C|, |A |, |B |, |C |, |(A ∩ B) |, |(A ∩ C) |, |(B ∩ C) |,
c c c c c c

|(A ∪ B) |, |(A ∪ C) |, |(B ∪ C) |, |(A ∩ B ∩ C) |, |(A ∪ B ∪ C) |, |A − B|, |B − A|, |A − C|, |C − A|,


c c c c c

|B − C|, and |C − B|
Question 4.25 (Easy) Simplify the following set theoretic expressions using the laws of set theory.

(a) A ∪ A
(b) A ∪ A c

(c) (A ∪ A c
)
c

(d) A ∪ (A ∩ B)
(e) A ∪ (A c
∩ B)

(f) A ∩ (A c
∪ B)

(g) A ∩ A
(h) A ∩ (A ∪ B)
(i) A ∩ (A ∪ B ∪ C)
(j) (A ∩ B) ∪ (A ∩ B c
)

(k) (A c
∪ A )
c c

(l) (A ∩ B) ∪ (A c
∩ B)

Question 4.26 (Moderate) Simplify the following set theoretic expressions using the laws of set theory.

(a) (A c
∪ B ) ∩ (A
c c
∪ B)

(b) B ∪ (B ∩ B c
)

(c) A c
∪ (B ∩ A )
c

(d) (A ∪ B c
) ∩ (A ∪ B)

(e) C ∪ [C ∪ (C ∩ A)]
(f) A ∩ [A ∪ (A ∩ B)]
(g) C ∪ (C ∩ A c
∩ B ∩ D)

(h) C c
∩ (C ∩ A ∩ B ∩ D)
c

Question 4.27 (Dif cult) Simplify the following set theoretic expressions using the laws of set theory.

(a) B ∩ [A ∪ (A c
∩ B)]

(b) [(A ∩ B c
) ∪ A ]
c c

(c) (A c
∩ B) ∪ [A ∩ (A ∪ B)]

(d) [A ∪ (A ∩ B)] c
∩ B

(e) (A ∩ C c
) ∪ (A
c
∩ B) ∪ (B ∩ C)
c

(f) (A ∩ B) c
∪ (B ∩ C)

(g) [A ∪ (B ∩ C)] ∩ (A c
∪ C)

(h) (A ∩ C c
) ∪ (A
c
∩ B) ∪ (A ∩ c)
c

(i) (A ∩ C) ∪ (A c
∩ B) ∪ (C ∩ B)

(j) A c
∪ B
c
∪ (A ∩ B ∩ C )
c

(k) (A ∪ C) ∩ (A c
∪ B) ∩ (C ∪ B)

(l) (A ∩ B) c
∩ (A
c
∪ B) ∩ (B
c
∪ B)

Question 4.28 Given the sets A = {1, 2, 3, 4} and B = {1, 2, 3, 4} write out the sets R ⊂ A × B that correspond
to the following relations:

(a) is equal to
(b) is less than
(c) is greater than
(d) is less than or equal
(e) is greater than or equal
(f) is not equal to

Question 4.29 Given the sets A = {0, 2, 3, 5, 7} and B = {1, 2, 4, 5, 6} write out the sets R ⊂ A × B that
correspond to the following relations:

(a) is equal to
(b) is less than
(c) is greater than
(d) is less than or equal
(e) is greater than or equal
(f) is not equal to

Question 4.30 The below directed graph represents a relation. Find the domain and range of the relation. Then
write the relation as a subset of the Cartesian product of the domain and range.

Question 4.31 Find the stated relations on the set {1, 2, 3} . Then determine if they are re exive, irre exive,
symmetric, antisymmetric, or transitive.

(a) the relation ≤


(b) the relation <
(c) the relation =
(d) the relation ≠
(e) the relation >
(f) the relation ≥

Question 4.32 Show the relation “is greater than or equal to” is a partial ordering on ℕ.

Question 4.33 Let A = {1, 2, 3, 4, 5, 6} with the relation given by

R = {(1, 1), (1, 5), (5, 1), (5, 5), (2, 2), (2, 4), (2, 6)(4, 2), (4, 4), (4, 6), (6, 2), (6, 4), (6, 6), (3, 3)} ⊂ A × A.

Show this relation is an equivalence relation. Then nd the equivalence classes that partition the set A.

Question 4.34 Show the relation “is equal to” is both a partial ordering and an equivalence relation on ℕ.
1 Often the set of natural numbers also includes zero. From a computer science perspective this makes sense in terms of indexing arrays.
2 Much later in your computer science major you may see problems where there is no well-de ned universal set.
3 The Cartesian product is named after Cartesius, the Latinized version of the last name of the French philosopher Rene Descartes. Since it is
named after a person often it is capitalized, but sometimes it is not. Both conventions are considered grammatically correct.
CHAPTER 5
Boolean Algebra

There are two major reasons to study Boolean algebra in computer science. First,
Boolean algebra uni es both logic and set theory in a very nice way. Second, Boolean
algebra plays a very important role in circuit design in computer science. It is very
helpful for computer science majors to have some idea of how circuits are designed and
work. Though this topic is beyond the scope of this book, appendix A shows how the
the ideas from Boolean algebra can be used to design a circuit that adds two eight-digit
binary numbers.

5.1 DEFINITION OF BOOLEAN ALGEBRA


We will begin by seeing how Boolean algebra uni es logic and set theory. In logic, even
though we had propositions and expressions, what we really were interested in is the
truth-values. Remember, in the truth tables we connected truth-values (T or F) with
connectives like ∧, ∨, and ¬ to get a new truth-value. We then found a variety of
equivalence relations we called laws. In summary, we had

a set {T , F },
three operations ∧, ∨, and ¬ de ned on elements of the set {T , F },
a list of laws the operations satis ed.

In set theory we start out with a universal set U but then de ne the power set of U ,
P (U ). The power set P (U ) is the set of all subsets of U . We then operate on these

elements of P (U ) using ∩, ∪, and . Again, we found a variety of equalities we called


c

laws. In summary, we had

a set P (U ),
three operations ∩, ∪, and de ned on elements of the set P (U ),
c

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 , the F
c

with ∅, and the T with U the laws are exactly the same. This is because they are both
examples of what is called a Boolean algebra.
Instead of studying logic or set theory as we have done up till now, we can study the
laws themselves. When we study the laws themselves we are studying both logic and
set theory and many other things at the same time. Whatever we prove using Boolean
algebra must also be true for logic or set theory. Now we are ready for the de nition.

De nition 5.1. A Boolean algebra is a set B that has two binary operations, + called
addition, × called multiplication, and a unary operation called complement. The set B

contains two special elements written as 0 and 1 such that the following axioms are true
for all x, y, z ∈ B:
Name Axioms of Boolean Algebra
Commutative axiom (a) x × y = y × x (b) x + y = y + x
Associative axiom (a) x × (y × z) = (x × y) × z (b) x + (y + z) = (x + y) + z
Distributive axiom (a) x × (y + z) = (x × y) + (x × z) (b) x + (y × z) = (x + y) × (x + z)
Identity axiom (a) x × 1 = x (b) x + 0 = x
Inverse axiom (a) x × x = 0 ′
(b) x + x = 1

Notice the de nition does not say what the elements of B are, only that there are two
special elements called 0 and 1. There might be other elements in the Boolean algebra
set or there might not be. What the elements of B are depends on how we interpret the
set B. Also, our Boolean operations addition, multiplication, and complement may be
interpreted differently as well, depending on what the elements of B actually are.
These operations are NOT the addition and multiplication you know from grade school.
We used the same words but they mean something else.
We also use the word axiom instead of law because these ten equations are part of the
de nition of Boolean algebra. Why did we use only these ve axioms in the de nition
instead of more? We are missing the double complement laws, the idempotent laws,
DeMorgan's laws, the annihilation laws, and the absorption laws. All these other laws
can be proved from the axioms in the de nition. We list the rest of the laws:
Name Laws of Boolean Algebra
Double negation law x
′′
= x

Idempotent laws (a) x × x = x (b) x + x = x


DeMorgan's laws (a) (x × y) = x + y
′ ′ ′
(b) (x + y) = x × y
′ ′ ′

Annihilation laws (a) x × 0 = 0 (b) x + 1 = 1


Absorption laws (a) x × (x + y) = x (b) x + (x × y) = x
Complement laws (a) 1 = 0′
(b) 0 = 1

Now we will see how to prove some of the Boolean algebra laws from the axioms given
in the de nition of Boolean algebra.

Example 5.1
Prove the idempotent law for addition, x + x = x , using the Boolean algebra
axioms.

x + x = (x + x) × 1 second identity axiom



= (x + x) × (x + x ) f irst inverse axiom


= x + (x × x ) f irst distributive axiom

= x + 0 second inverse axiom

= x f irst identity axiom

It takes a little while to get used to seeing something like x + x = x. Just remember,
the + is not the addition you are used to from algebra.

If we have an expression in Boolean algebra we nd the dual of the expression by


replacing every + with ×, every × with +, every 0 with 1, and every 1 with 0.

Example 5.2

Finding the dual of an expression

The dual of x × (x × x ) is x + (x + x ) .
′ ′ ′ ′

The dual of (x + y) × (x × y ) is (x × y) + (x + y ).
′ ′

The dual of x + x = x is x × x = x.

The reason the dual is important is the following theorem, which we will not prove.
Theorem 5.1. Duality Principle: The dual of anything proved to be true in Boolean
algebra is also true.

Example 5.3

Show the idempotent law for multiplication, x × x = x.


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
Prove the annihilation law using the Boolean algebra axioms.

x + 0 = (x + 0) × 0 f irst identity axiom


= (x + 0) × (x × x ) second inverse axiom


= x + (0 + x ) f second distributive axiom


= x × (x + 0) f irst commutative axiom


= x × x f irst identity axiom

0 second inverse axiom

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

Prove the absorption law.

x × (x + y) = (x + 0) × (x + y) f irst identity axiom

= x + (0 × y) f irst distributive axiom

= x + (y + 0) second commutitive axiom

= x + 0 annihilation law (already proved)

= x f irst identity axiom

Thus we have shown x × (x + y) = x. The dual of this is x + (x × y) = x, which is


also true by the duality principle.

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

Simplify the Boolean expression (x + x × y) ′


× y .
′ ′ ′
(x + x × y) × y = x × (x × y) × y De Morgan"s law

′ ′
= x × y × (x × y) commutitive axiom

′ ′ ′
= x × y × (x + y ) DeMorgan"s law

′ ′ ′
= (x × y × x ) + (x × y × y ) distributive axiom

′ ′ ′
= (x × x × y) + (x × y × y ) commutitive axiom

′ ′
= (x × x × y) + (x × 0) inverse axiom


= (x × y) + (x × 0) idempotant law


= (x × y) + 0 annihilation law


= x × y identity axiom

Example 5.7

Simplify the Boolean expression x × (x × y ) . ′ ′

′ ′ ′ ′′
x × (x × y ) = x × (x + y ) DeMorgan"s law


= x × (x + y) double negation law


= (x × x ) + (x × y) distributive axiom

= 0 + (x × y) inverse axiom

= x × y identity axiom

5.2 LOGIC AND SET THEORY AS BOOLEAN ALGEBRAS


We want to just take a few moments to make sure the connection between logic, set
theory, and Boolean algebra is clear. Recall, in logic we had

A set {T , F }, the possible truth-values of propositions.1


Three operations ∧, ∨, and ¬ de ned on elements of the set {T , F } (usually
denoted as p, q, and r).
A list of laws the operations satis ed.

Name Laws of Logic


Commutative laws (a) p ∧ q ≡ q ∧ p (b) p ∨ q ≡ q ∨ p
Associative laws (a) (p ∧ q) ∧ r ≡ p ∧ (q ∧ r) (b) (p ∨ q) ∨ r ≡ p ∨ (q ∨ r)
Name Laws of Logic
Distributive laws (a) p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r) (b) p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r)
Identity laws (a) p ∧ T ≡ p (b) p ∨ F ≡ p
Inverse laws (a) p ∧ ¬p ≡ F (b) p ∨ ¬p ≡ T
Double negation law ¬¬p ≡ p

Idempotent laws (a) p ∧ p ≡ p (b) p ∨ p ≡ p


DeMorgan's laws (a) ¬(p ∧ q) ≡ ¬p ∨ ¬q (b) ¬(p ∨ q) ≡ ¬p ∧ ¬q
Annihilation laws (a) p ∧ F ≡ F (b) p ∨ T ≡ T
Absorption laws (a) p ∧ (p ∨ q) ≡ p (b) p ∨ (p ∧ q) ≡ p
Negation laws (a) ¬T = F (b) ¬F = T

In set theory we had

A set P (U ), the power set of the set U .


Three operations ∩, ∪, and de ned on elements of the set P (U ) (usually denoted
c

as A, B, and C).
A list of laws the operations satisfy.

Name Laws of Set Theory


Commutative laws (a) A ∩ B = B ∩ A (b) A ∪ B = B ∪ A
Associative laws (a) (A ∩ B) ∩ C = A ∩ (B ∩ C) (b) (A ∪ B) ∪ C = A ∪ (B ∪ C)
Distributive laws (a) A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) (b) A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)
Identity laws (a) A ∩ U = A (b) A ∪ ∅ = A
Inverse laws (a) A ∩ A = ∅ c
(b) A ∪ A = Uc

Double complement law (A )


c c
= A

Idempotent laws (a) A ∩ A = A (b) A ∪ A = A


DeMorgan's laws (a) (A ∩ B) = A ∪ B
c c c
(b) (A ∪ B) = A ∩ B
c c c

Annihilation laws (a) A ∩ ∅ = ∅ (b) A ∪ U = U


Absorption laws (a) A ∩ (A ∪ B) = A (b) A ∪ (A ∩ B) = A
Complement laws (a) U = ∅c
(b) ∅ = U
c

And in Boolean algebra we had

A set B which included two special elements we called 1 and 0.


Three operations ×, +, and de ned on elements of the set B (usually denoted as

x, y, and z).
A list of axioms and laws the operations satisfy.

Name Axioms of Boolean Algebra


Commutative axiom (a) x × y = y × x (b) x + y = y + x
Associative axiom (a) x × (y × z) = (x × y) × z (b) x + (y + z) = (x + y) + z
Distributive axiom (a) x × (y + z) = (x × y) + (x × z) (b) x + (y × z) = (x + y) × (x + z)
Identity axiom (a) x × 1 = x (b) x + 0 = x
Inverse axiom (a) x × x = 0 ′
(b) x + x = 1

Name Laws of Boolean Algebra


Double negation law x
′′
= x
Name Axioms of Boolean Algebra
Idempotent laws (a) x × x = x (b) x + x = x
DeMorgan's laws (a) (x × y) = x + y
′ ′ ′
(b) (x + y) = x × y
′ ′ ′

Annihilation laws (a) x × 0 = 0 (b) x + 1 = 1


Absorption laws (a) x × (x + y) = x (b) x + (x × y) = x
Complement laws (a) 1 = 0

(b) 0 = 1

In mathematics we would say that Boolean algebra is the theoretical framework that
encompass both logic and set theory. This is just a fancy way of saying both logic and
set theory are concrete examples of Boolean algebras. By comparing the laws in each
case we can see that the following symbols are equivalent.
Boolean Algebra Logic Set Theory
× (multiplication) ∧ (and) ∩ (intersection)

+ (addition) ∨ (or) ∪ (union)



(complement) ¬ (negation)
c
(complement)
1 T U

0 F ∅

As long as we remember the equivalence between these symbols we only have to


learn one set of laws, not three.
As computer scientists we are especially interested in one concrete example of a
Boolean algebra where

B = {no electrical current, electrical current}.

It is this Boolean algebra that is used by computer engineers to design the circuits that
run all computers. But of course, we usually make the following identi cation,

no electrical current = 0,

electrical current = 1,

which means we usually write our Boolean set as B = {0, 1}.2 Recall, the de nition
said that a Boolean algebra B must contain two special elements called 0 and 1. The
simplest possible Boolean algebra has only these two special elements and no other
elements. So, the simplest Boolean algebra is given by B = {0, 1}. This is exactly the
Boolean algebra that we computer scientists are most interested in.
We will use the laws and axioms to see how Boolean algebra addition and
multiplication work in B = {0, 1}. The rst identity axiom says that x × 1 = x. Since
x can either be 1 or 0 we have
1 × 1 = 1,

0 × 1 = 0.

The second identity axiom says x + 0 = x so we have

1 + 0 = 1,

0 + 0 = 0.

The rst commutative axiom says x × y = y × x so using this and the fact that
0 × 1 = 0 we know

0 × 1 = 1 × 0

⇒ 0 + 1 = 0.

Similarly, the second commutative axiom says x + y = y + x so using this and the fact
that 1 + 0 = 1 we know

1 + 1 = 0 + 1

⇒ 0 + 1 = 1.

Finally, we use the rst annihilation law x × 0 = 0 with x = 0 to get

0 × 0 = 0

and the second annihilation law x + 1 = 1 with x = 1 to get

1 + 1 = 1.

Putting all this together we have the following multiplication and addition tables for
Boolean algebra,
0 × 0 = 0,

1 × 0 = 0,

0 × 1 = 0,

1 × 1 = 1,

and

0 + 0 = 0,

1 + 0 = 1,

0 + 1 = 1,

1 + 1 = 1,

Everything is as you would expect from grade school math, except the funny 1 + 1 = 1.
This is what makes Boolean algebra different from grade school math. But this addition
rule 1 + 1 = 1 makes a little more sense when we think of it in terms of electricity,
since

electrical current + electrical current = electrical current.

Here the multiplication and addition tables are written in a slightly different way:
× 0 1
0 0 0
1 0 1

and
+ 0 1
0 0 1
1 1 1

5.3 DIGITAL CIRCUITS


Now we turn to the use of Boolean algebra for designing computer digital circuits.
Digital circuits operate with electricity. Either there is an electrical current or there
isn't. So there are only two possibilities for a signal in a computer, no current or
current. We call those possibilities 0 or 1.
At the most basic level digital circuits combine electrical signals to produce a new
electrical signal. The circuit does this at a gate. We will not describe how this is
physically done in the computer, that is the job of electrical engineers. We will only say
that circuits are made of gates that combine signals to make a new signal. We can
model these gates with a Boolean algebra B = {0, 1}. It turns out that this Boolean
algebra is just like the Boolean algebra for logic. We let × represent and, we let +
represent or, and we let represent not.

First, it is always possible for a circuit to “ ip” a signal. That is, it can change a 0 to
a 1 or a 1 to a 0. That is done with a not gate. Just like in logic we can build a table that
shows how the not gate changes a signal. Even though these tables do not contain trues
or falses we will still call them truth tables because they are so similar.


x x

1 0
0 1

It turns out to be very useful to use pictures to help computer scientists design
circuits using gates. Here is the picture that is used to indicate a not gate. It is a triangle
with a tiny circle attached to the right point. As it is drawn the input is signal x and the
output is signal x′. We generally “read” circuit diagrams going from left to right just
like we read English.

Next we will consider gates with two input signals x and y. We want to consider the
different ways those two input signals can be combined. Of course x can be either 1 or
0 and y can also be either 1 or 0. So there are 2 = 4 possible combinations of inputs.
2

Each gate needs to give a unique output for each of these four inputs.

x y gate output

1 1 0 or 1

1 0 0 or 1

0 1 0 or 1

0 0 0 or 1

There are a few combinations of outputs we are not interested in. We are not interested
in a gate that gives 0 no matter what the inputs are. Also, we are not interested in a gate
that gives 1 no matter what the inputs are. Finally, we do not want order to matter. That
means we want the same output for when x = 0 and y = 1 as for when x = 1 and y = 0
. With these restrictions there are only six possible gates.
The rst two-input gate we will look at is the and gate. The and gate works just like
the and connector in logic. However, we use × to indicate and in Boolean algebra. We
can also write x × y as xy just as one would in algebra. The truth table for the and gate
is given below.

x y x × y

1 1 1
1 0 0
0 1 0
0 0 0

Here is the picture we use to show an and gate. There are two inputs, x and y that enter
the gate on the left. The output from the gate is shown on the right and is given by
x × y.

The next two-input gate we will look at is the or gate. The or gate works just like the
or connector in logic. However, we use + to indicate or in Boolean algebra. The truth
table for the or gate is given below.

x y x + y

1 1 1
1 0 1
0 1 1
0 0 0

Here is the picture we use to show an or gate. There are two inputs, x and y that enter
the gate on the left. The output from the gate is shown on the right and is given by
x + y.

Now we will look at the nand gate. The nand gate is the not of the and gate,


x nand y = (x × y) .

We will simply use the term nand to mean the nand operation. The truth table for the
nand gate is given below.
x y xnandy

1 1 0
1 0 1
0 1 1
0 0 1

Here is the picture we use to show an nand gate. There are two inputs, x and y that enter
the gate on the left. The output from the gate is shown on the right and is given by
x nand y. Notice that the picture for an nand gate looks like an and gate, only has a

tiny circle on the right where the output comes out.

Next we look at the nor gate. The nor gate is the not of the or gate,


x nor y = (x + y) .

We will simply use the term nor to mean the nor operation. The truth table for the nor
gate is given.

x y xnory

1 1 0
1 0 0
0 1 0
0 0 1

Here is the picture we use to show a nor gate. There are two inputs, x and y that enter
the gate on the left. The output from the gate is shown on the right and is given by
xnory. Again, notice that the picture for a nor gate looks like an or gate, only has a

tiny circle on the right where the output comes out.

The next gate we look at is called the exclusive-or gate, or xor gate. In an example
from the logic chapter we said the expression p xor q is true when only one of the
propositions p or q is true. In other words, p xor q is true when one, or the other, of the
propositions are true but not both. That means that if both p and q are true, then p xor q
is false. Similarly, x xor y is equal to 1 only when either x or y is 1 but not both. We
can also write this as

′ ′
x xor y = x × y + x × y.

Sometimes ⊕ is also used for xor.

x y x xor y

1 1 0
1 0 1
0 1 1
0 0 0

Here is the picture we use to show a xor gate. There are two inputs, x and y that enter
the gate on the left. The output from the gate is shown on the right and is given by
x xor y. Again, notice that the picture for an xor gate looks like an or gate only with an

extra line on the left where the input go into the gate.

Finally, we look at the xnor gate. The xnor gate is the not of the xor gate. That is,

′ ′ ′ ′ ′
x xnor y = (x × y + x × y) = (x × y) + (x × y ).

We will simply use the term xnor to mean the xnor operation. The truth table for the
xnor gate is given.

x y xxnor y

1 1 1
1 0 0
0 1 0
0 0 1

Here is the picture we use to show an xnor gate. There are two inputs, x and y that enter
the gate on the left. The output from the gate is shown on the right and is given by
x xnor y. Again, notice that the picture for an xnor gate looks like an xor gate only

with a tiny circle on the right where the output comes out.
We provide all six of these Boolean algebra operations, or gates, in a single table. As
we said before, we were not interested in gates that gave a 0 or a 1 no matter what the
inputs were. And we are only interested in gates that give an output that are the same
when x = 0 and y = 1 as when x = 1 and y = 0.

x y x × y x + y x nand y x nor y x xor y x xnor y

1 1 1 1 0 0 0 1
1 0 0 1 1 0 1 0
0 1 0 1 1 0 1 0
0 0 0 0 1 1 0 1

Drawing circuits is almost exactly like drawing the expression trees for expressions in
logic. It is these “pictures” that actually are the basis for the circuits that are etched
into computer microprocessors, the “chips” that computers run on. We will draw a
circuit for evaluating the expression x × y + y. We begin by drawing an expression

tree similar to the expression trees from logic.

There are a couple differences between circuit diagrams and expression trees. First,
expression trees are usually read from the bottom to the top. Circuit diagrams are
usually read from the left to the right. Second, expression trees often have the same
variable listed more than once. In the above expression tree the variable y is shown
twice, once for each time it appears in the expression x × y + y. But circuit diagrams

generally have each input signal shown only once. Below we rotate the expression tree
so it can be read left to right and modify it a little so the y variable only appears once.
(Notice, it no longer looks like a tree after we modify it.)
This modi ed expression tree can be used as a basis for drawing the circuit diagram.
The vertices with the variables become the input signals. Each of the other vertices of
the expression tree is replaced by the correct gate. The is replaced with the not gate,

the × is replaced with the and gate, and the + is replaced with the or gate.

Finding the expression tree can be helpful for drawing circuit diagrams but is not
necessary once you get good at it.

Example 5.8

What is the expression that this circuit represents?

This circuit represents the expression (x × y) + z . ′

Example 5.9

Draw the circuit for the expression (x ′ ′ ′


+ y ) × (x + y ) × (y + z) .
Example 5.10

The circuit drawn in the last example is quite complicated. Use the laws of Boolean
algebra to simplify the expression (x + y ) × (x + y ) × (y + z) and then draw the
′ ′ ′

circuit for the simpli ed expression.

′ ′ ′ ′ ′ ′ ′ ′ ′
(x + y )(x + y )(y + z) = (x x + x y + y x + y y )(y + z)

′ ′ ′
= [0 + y (x + x) + y ](y + z)

′ ′
= (y 1 + y )(y + z)

′ ′
= (y + y )(y + z)


= y (y + z)

′ ′
= y y + y z


= y z

Which can be drawn as

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.

5.4 SUMS-OF-PRODUCTS AND PRODUCTS-OF-SUMS


Just like in logic we can nd the truth table for Boolean algebra expressions. But
instead of using T or F like in logic we use 0 and 1 in Boolean algebra.

Example 5.11

Find the truth table and draw the circuit for the following Boolean algebra
expression x × z + y.′

The truth table is given by:


x y z z′
′ ′
x × z x × z + y

1 1 1 0 0 1
1 1 0 1 1 1
1 0 1 0 0 0
1 0 0 1 1 1
0 1 1 0 0 1
0 1 0 1 0 1
0 0 1 0 0 0
0 0 0 1 0 0

The circuit diagram is given by:

Example 5.12

Find the truth table and draw the circuit for the following Boolean algebra
expression [(x + y) × z] . ′

The truth table is given by:


x y z

x + y (x + y) × z [(x + y) × z]

1 1 1 1 1 0
x y z

x + y (x + y) × z [(x + y) × z]

1 1 0 1 0 1
1 0 1 1 1 0
1 0 0 1 0 1
0 1 1 1 1 0
0 1 0 1 0 1
0 0 1 0 0 1
0 0 0 0 0 1

The circuit diagram is given by:

Suppose you were given two expressions, (x x + x y + y x + y y )(y + z) and ′ ′ ′ ′ ′ ′

y (y + z). Are these expressions equal or not? That is, do these expressions represent

the same thing or not? How would you nd out? One thing you could do is write out a
truth table for each and see if they are equivalent. In this case they are equivalent. In a
previous example we simpli ed the expression (x + y )(x + y )(y + z), ′ ′ ′

′ ′ ′ ′ ′ ′ ′ ′ ′
(x + y )(x + y )(y + z) = (x x + x y + y x + y y )(y + z)

′ ′ ′
= [0 + y (x + x) + y ](y + z)

′ ′
= (y 1 + y )(y + z)

′ ′
= (y + y )(y + z)


= y (y + z)

′ ′
= y y + y z


= y z.

The two expressions (x x + x y + y x + y y )(y + z) and y (y + z) are simply two


′ ′ ′ ′ ′ ′ ′

different lines in this above simpli cation. In fact, each of the above lines is a different
way of writing down the same thing.
Circuit designers need an easy way to know if different expressions really are the
same thing or not. Therefore circuit designers often write expressions in one of two
standardized, or canonical, ways. Every Boolean expression can be written in a sum-of-
products form or in a product-of-sums form. This makes it easy to see when
expressions are the same or not.
Additionally, sometimes you want to have a function that behaves in a certain way.
Suppose you know the truth table for what you want the function to do but do not know
a function that behaves that way. You want to nd an expression for a function that
behaves that way. The sum-of-products and product-of-sum expressions can be written
down using a truth table.
We will rst consider the sum-of-products form. The rst step in writing an
expression in the sum-of-products form is to nd the truth table. We use the truth table
for x × z + y from the above example. When there is a 1 in the nal column we

consider the values in the row.

If x value is 1 use x in the product term, if x value is 0 use x′ in the product term.
If y value is 1 use y in the product term, if y value is 0 use y′ in the product term.
If z value is 1 use z in the product term, if z value is 0 use z′ in the product term.

We then add up, or sum, all the product terms we get. This is the sum-of-products form
for the expression.
x y z ′
x × z + y product terms
1 1 1 1 xyz

1 1 0 1 xyz

1 0 1 0
1 0 0 1 xy z
′ ′

0 1 1 1 ′
x yz

0 1 0 1 ′
x yz

0 0 1 0
0 0 0 0

In the rst row there is a 1 in the nal column so we use this row. In this row the x
value is 1, the y value is 1, and the z value is 1 so we use x, y, and z in the product term
to get x × y × z. From now on we will write x × y × z as xyz. In the second row there
is also a 1 in the nal column so we use the second row. In this row the x value is 1, the
y value is 1, and the z value is 0 so we use x, y, and z′ in the product term to get xyz . In

the fourth row there is a 1 in the nal column so we use this row. In this row the x value
is 1, the y value is 0, and the z value is 0 so we use x, y′, and z′ in the product term to
get xy z . And so on. We then add up, or sum, all the product terms to get
′ ′

x × z + y = xyz + xyz + xy z + x yz + x yz .
′ ′ ′ ′ ′ ′ ′

Example 5.13

Write the sum-of-products form for [(x + y) × z] .



x y z [(x + y) × z]

product term

1 1 1 0
1 1 0 1 xyz

1 0 1 0
1 0 0 1 xy z
′ ′

0 1 1 0
0 1 0 1 ′
x yz

0 0 1 1 ′
x y z

0 0 0 1 ′
x y z
′ ′

So [(x + y) × z] .
′ ′ ′ ′ ′ ′ ′ ′ ′ ′ ′
= xyz + xy z + x yz + x y z + x y z

Next we will consider the product-of-sum form. The rst step in writing an
expression into the product-of-sum form is to nd the truth table. Again we use the
truth table for x × z + y from the above example. When there is a 0 in the nal

column we consider the values in the row.

If x value is 1 use x′ in the sum term, if x value is 0 use x in the sum term.
If y value is 1 use y′ in the sum term, if y value is 0 use y in the sum term.
If z value is 1 use z′ in the sum term, if z value is 0 use z in the sum term.

We then multiply, or take the product of, all the sum terms we get. This is the product-
of-sum form for the expression.
x y z x × z + y

sum terms
1 1 1 1
1 1 0 1
1 0 1 0 ′
x + y + z

1 0 0 1
0 1 1 1
0 1 0 1
0 0 1 0 x + y + z

0 0 0 0 x + y + z

In the third row the nal column is 0 so we use this row. In this row the x value is 1,
the y value is 0, and the z value is 1 so we use x′, y, and z′ in the sum term to get
x + y + z . In the seventh row the nal column is 0 so we use this row. In this row the
′ ′

x value is 0, the y value is 0, and the z value is 1 so we use x, y, and z′ in the sum term
to get x + y + z . In the eighth row the nal column is 0 so we use this row. In this row

the x value is 0, the y value is 0, and the z value is 0 so we use x, y, and z in the sum
term to get x + y + z. We then multiply, or take the product of, all the sum terms to get
x × z + y = (x + y + z )(x + y + z )(x + y + z).
′ ′ ′ ′
Example 5.14

Write the product-of-sums form for [(x + y) × z] .


x y z [(x + y) × z]

sum term

1 1 1 0 ′ ′
x + y + z

1 1 0 1
1 0 1 0 ′
x + y + z

1 0 0 1
0 1 1 0 ′
x + y + z

0 1 0 1
0 0 1 1
0 0 0 1

So [(x + y) × z] .
′ ′ ′ ′ ′ ′ ′ ′
= (x + y + z )(x + y + z )(x + y + z )

5.5 PROBLEMS

Question 5.1 (Easy) Simplify the following Boolean algebra expressions.

(a) x + x
(b) x + x ′

(c) (x + x ) ′ ′

(d) x + xy
(e) x + x y ′

(f) x(x + y) ′

(g) x(x)
(h) x(x + y)
(i) x(x + y + z)
(j) xy + xy ′

(k) (x ′
+ x )
′ ′

(l) xy + x y ′

Question 5.2 (Moderate) Simplify the following Boolean algebra expressions.

(a) (x ′
+ y )(x + y)
′ ′

(b) y + (yy ) ′

(c) x ′
+ yx

(d) (x + y )(x + y) ′

(e) w + [w + (wx)]
(f) x[x + (xy)]
(g) w + (wx yz) ′

(h) w (wxyz)
′ ′

Question 5.3 (Dif cult) Simplify the following Boolean algebra expressions.

(a) y[x + (x y)] ′

(b) [(xy ) + x ] ′ ′ ′

(c) x y + x(x + y)

(d) (x + xy) y ′

(e) xz ′
+ x y + (yz)
′ ′

(f) (xy) ′
+ (yz)

(g) [x + (yz)](x ′
+ z)

(h) xz ′
+ x y + (xz)
′ ′

(i) xz + x y + zy ′

(j) x + y + xyz
′ ′ ′

(k) (x + z)(x + y)(z + y) ′

(l) (xy) (x ′ ′
+ y)(y + y)

Question 5.4 Write down the Boolean expression associated with each of the following
circuits.
Question 5.5 Write down the Boolean expression associated with each of the following
circuits.

Question 5.6 Write down the Boolean expression associated with each of the following
circuits.

Question 5.7 Write down the Boolean expression associated with each of the following
circuits.
Question 5.8 For the expressions in question, 5.1 draw the associated circuit.

Question 5.9 For the expressions in question, 5.2 draw the associated circuit.

Question 5.10 For the expressions in question, 5.3 draw the associated circuit.

Question 5.11 Below are the truth tables for some functions f (x, y). First write f (x, y)

in sum-of-products form and then write f (x, y) in product-of-sums form.


(a)

x y f (x, y)

0 0 1
0 1 0
1 0 1
1 1 0

(b)

x y f (x, y)

0 0 1
0 1 1
1 0 0
1 1 0

(c)

x y f (x, y)

0 0 0
x y f (x, y)

0 1 1
1 0 0
1 1 0

(d)

x y f (x, y)

0 0 0
0 1 0
1 0 1
1 1 1

(e)

x y f (x, y)

0 0 1
0 1 0
1 0 0
1 1 1

(f)

x y f (x, y)

0 0 0
0 1 0
1 0 0
1 1 1

(g)

x y f (x, y)

0 0 1
0 1 1
1 0 1
1 1 0

(h)

x y f (x, y)

0 0 1
0 1 0
x y f (x, y)

1 0 1
1 1 1

(i)

x y f (x, y)

0 0 0
0 1 1
1 0 0
1 1 1

Question 5.12 Below are the truth tables for some functions f (x, y). First write f (x, y)

in sum-of-products form and then write f (x, y) in product-of-sums form.


(a)

x y z f (x, y, z)

0 0 0 0
0 0 1 0
0 1 0 1
0 1 1 0
1 0 0 0
1 0 1 1
1 1 0 0
1 1 1 1

(b)

x y z f (x, y, z)

0 0 0 1
0 0 1 0
0 1 0 1
0 1 1 1
1 0 0 1
1 0 1 0
1 1 0 1
1 1 1 0

(c)

x y z f (x, y, z)

0 0 0 1
x y z f (x, y, z)

0 0 1 0
0 1 0 1
0 1 1 0
1 0 0 0
1 0 1 1
1 1 0 1
1 1 1 0

(d)

x y z f (x, y, z)

0 0 0 1
0 0 1 0
0 1 0 0
0 1 1 1
1 0 0 0
1 0 1 0
1 1 0 1
1 1 1 0

(e)

x y z f (x, y, z)

0 0 0 1
0 0 1 1
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 1
1 1 0 1
1 1 1 1

(f)

x y z f (x, y, z)

0 0 0 1
0 0 1 1
0 1 0 0
0 1 1 1
1 0 0 0
1 0 1 0
1 1 0 0
1 1 1 1
Question 5.13 Write out the truth table for the following Boolean expressions.

(a) (x ′
+ y)x

(b) (x + y )x ′

(c) (x + y)y ′

(d) (x + y)(x + y)
(e) (x + y)(x ′
+ y)

(f) (x + y )(x ′ ′
+ y)

(g) xx + x(x + y)
(h) xy + y(x + y)
(i) x y + y (x + y)
′ ′

(j) x y′ ′ ′
+ y (x + y)

(k) xy(x + y)
(l) x y (x
′ ′ ′
+ y )

Question 5.14 Write out the truth table for the following Boolean expressions.

(a) x(x + y ′
+ z)

(b) x (x + y
′ ′
+ z)

(c) (x + y)(x + z)
(d) (x ′ ′
+ y )(x + z)

(e) x y′ ′
+ z (x + y)

(f) xy(x + y + z)
1 Another way of thinking about this is that our set is the set of all propositions which can be split into two
equivalence classes

T = {alltruepropositions}

and

F = {allf alsepropositions}

. Thus T is really the equivalence class of all true propositions and F is the equivalence class of all false
propositions. Then we could consider p , q , and r to be propositions instead of truth-values.
2 This is a simpli cation. Though it is nice to think of 0 meaning “no electrical current” there is actually a very
low level of residual current.
CHAPTER 6
Functions

Functions play an important role in computer science, particularly in complexity analysis, which is used to
analyze algorithms. An understanding of functions is also necessary for many topics in advanced computer
science, meaning that computer science majors need to have a working understanding of functions.1

6.1 INTRODUCTION TO FUNCTIONS


You have probably already studied real-valued functions of a real-variable. When you studied these you probably
thought of them as something with a formula, like x − 2x + 3, or something you could graph using an x and y
2

axes. But the real de nition of functions is more general.


De nition 1. De nition 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 de ned by

f (1) = a,

f (2) = c,

f (3) = b.

The function f is a rule that assigns to 1 ∈ X the value a ∈ Y . This is what f (1) = a means. The rule f
assigns to 2 ∈ X the value c ∈ Y . That is what f (2) = c means. And the rule f assigns to 3 ∈ X the value
b ∈ Y . That is what f (3) = b means. Another way to say this is the image of 1 is a, the image of 2 is c, and

the image of 3 is b. The function f is shown in a picture below.

Example 6.2

Let the domain be X = {1, 2, 3} and the codomain be Y = {a, b, c} . Then a function f : X → Y is de ned by
f (1) = c,

f (2) = a,

f (3) = c.

Notice that this function has f (1) = c and f (3) = c. According to the de nition, a function is a rule that
assigns to each element of X exactly one element of Y. For every element in X there is assigned exactly one
element of Y. It is perfectly okay that both 1 and 3 are assigned to the same element c.

Example 6.3

Let the domain be ℝ and the codomain be ℝ. Then a function f : R → R is de ned by the rule

2
f (x) = 4x − 10x.

Sometimes functions that are de ned by an algebraic rule are shown using a picture of a function-machine.

Example 6.4

Boolean expressions, and therefore circuits, can be viewed as multivariable functions. Consider the circuit

represented by the expression (x × y) + z . First recall that B = {0, 1}. This circuit represents a function

whose domain is the cartesian product B × B × B and whose codomain is B,

f : B × B × B → B.
The input of the function is an 3-tuple of Boolean variables (x, y, z) ∈ B × B × B and the output is an
element of B. The function rule is given by


f (x, y, z) = (x × y) + z .

We will not deal any further with multivariable functions in this class, but they are very common in both
mathematics and computer science.

Example 6.5

Let the domain be ℕ and the codomain be Y = {0, 1, 2, 3, 4, 5, 6, 7} . Then f : N → Y where

f (x) = x mod 8

is a function. We have seen this function before. This function takes a natural number x and nds the
remainder after division by 8.

Example 6.6

Let the domain be X = {all circle sin the plane} and the codomain be ℝ. Then f : X → R where

f (x) = the radius of x

is a function. This function take a circle and nds the radius of the circle.

We can see that the idea of a function can apply to many different situations. This is what makes it such an
important and useful concept. But before going further we want to consider an example of something that is not a
function. Consider the following set relation:

Is this a function? Remember what the de nition of function says, a function from X to Y is a rule that assigns to
each element of X exactly one element of Y. What is happening to the element 2 ∈ X? It is getting assigned to
both b and c. Thus 2 ∈ X is not assigned to exactly one element of Y, it is assigned to two elements of Y.
Therefore, this is not a function.
Now let us reconsider the second example, the function f : X → Y given by

f (1) = c,

f (2) = a,

f (3) = c,
which we showed as

Notice that no element in X is send to B ∈ Y . The image of a function f is given by

Im(f ) = {y ∈ Y | y = f (x)f or some x ∈ X}.

The image of a function is also called the range of the function. We will usually use the word range in this book.
In this example the range of f is given by the set {a, c}. Here the range of f is not equal to the codomain of f.
Instead, the range of f is a proper subset of the codomain of f,

range of f = {a, c} ⊂ {a, b, c} = codomain of f .

We always have range ⊆ codomain. So, for each function there are three sets of interest, the domain, the
codomain, and the range.
There is a second de nition of function that uses the language of set theory. This is an important connection
between set theory and functions. Recall, we studied set relations in chapter 4. Functions are a special kind of set
relation. This is the way that computer scientists usually think about functions.
De nition 2. De nition two of functions:
Let X and Y be sets. A function f : X → Y is a subset of the Cartesian product of X and Y,

f ⊂ X × Y

such that

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.)

Example 6.7

Consider the function f shown below.

The domain is given by X = {1, 2, 3} and the codomain is given by Y = {a, b, c} . Thus

X × Y = {(x, y)|x ∈ X, y ∈ Y }

= {(1, a), (2, a), (3, a), (1, b), (2, b), (3, b), (1, c), (2, c), (3, c) }.
The function f is a subset of X × Y given by

f = {(1, a), (2, c), (3, b)} ⊂ X × Y .

The rst property in the de nition is satis ed: if x ∈ X then there is a y ∈ Y such that (x, y) ∈ f . For 1 ∈ X,
we have a ∈ Y such that (1, a) ∈ f . For 2 ∈ X, we have c ∈ Y such that (2, c) ∈ f . For 3 ∈ X, we have
B ∈ Y such that (3, b) ∈ f . The second property is also satis ed: this y ∈ Y is unique. For 1 ∈ X this y ∈ Y

is unique, namely a ∈ Y . For 2 ∈ X this y ∈ Y is unique, namely c ∈ Y . For 3 ∈ X this y ∈ Y is unique,


namely B ∈ Y . Also notice that the range and the codomain are the same.

Example 6.8

Consider the function f shown below.

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)}.

The function f is again subset of X × Y ,

f = {(1, a), (2, c), (3, a)} ⊂ X × Y .

Convince yourself that the two properties in the de nition are satis ed. Consider the second property. For
1 ∈ X the y ∈ Y is unique, it is a ∈ Y since (1, a) ∈ f . For 3 ∈ X the y ∈ Y is unique, it is also a ∈ Y since

(3, a) ∈ f . It is okay that a ∈ Y is used twice. The element a ∈ Y is the unique element in Y associated to 1

and also the unique element in Y associated to 3. Also notice that

range = {a, c} ⊂ Y .

Example 6.9

Consider the function f shown below.


Here the domain is X = {1, 2, 3, 4, 5, 6, 7} and the codomain is given by Y = {a, b, c, d, e, f , g, h, i} . We
will not write out all 63 elements of X × Y . The function f is given by

f = {(1, e), (2, i), (3, b), (4, e), (5, d), (6, c), (7, g)} ⊂ X × Y .

Convince yourself that the two properties in the de nition are satis ed. Also, notice that

range = {b, c, d, e, g, i} ⊂ Y .

Example 6.10

Recall our example of something that was not a function.

Here X ,
= {1, 2, 3} Y = {a, b, c} , and

X × Y = {(x, y) | x ∈ X, y ∈ Y }

= {(1, a), (2, a), (3, a), (1, b), (2, b), (3, b), (1, c), (2, c), (3, c)}.

The set relation represented in this picture is given by

{(1, b), (2, b), (2, c), (3, a)} ⊂ 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 satis ed. For 2 ∈ X the y ∈ Y is not unique, there are two of them, namely b and c. Thus the
de nition for a function is not satis ed.

A function is called onto if the range of the function is equal to the codomain of the function. A function is
called one-to-one if no two distinct elements of the domain have the same image. Another way of saying this is
that for the elements x1 and x2 in the domain of f, if x ≠ x then
1 2 f (x 1 ) ≠ f (x 2 ) . Now it is a good time to
remember a little logic. If we de ne the propositions p and q to be

p =′′x 1 ≠ x 2 ′′ and q =′′f (x 1 ) ≠ f (x 2 )′′

then the statement “if x ≠ x then f (x ) ≠ f (x )” is the expression


1 2 1 2 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 x ≠ x then f (x ) ≠ f (x )” is logically equivalent to the statements “if
1 2 1 2

f (x ) = f (x ) then x = x .” This is another way to de ne one-to-one functions.


1 2 1 2

Example 6.11

Examples of functions that are one-to-one and/or onto.

Function is one-to-one and is onto.

Function is one-to-one but is not onto.

Function is not one-to-one but is onto.

Function is not one-to-one and is not onto.


6.2 REAL-VALUED FUNCTIONS
In this section we will discuss real-valued functions of one variable. You have probably studied real-valued
functions of one variable before. Real-valued means the codomain of the function is the set of real numbers, ℝ.
One variable means the function only requires one variable as input. Here we will assume that this one variable
comes from the real numbers ℝ as well. Thus, a real-valued function of one variable is a fancy way of saying the
domain and codomain of the function is ℝ, that is,

f : R ⟶ R.

The real-valued functions of one variable that you studied before were probably given in terms of an equation,
like

2 2
f (x) = x + 3x − 2 or like y = x + 3x − 2.

The equation is the rule that assigns to each element x ∈ R (the domain) exactly one element of ℝ (the
codomain). If we want to know where the function sends a particular element of the domain ℝ we evaluate the
function at that element. For example, if we want to nd where the function sends 2 we evaluate it at 2. That
means we replace the x in the equation with 2,

2
f (2) = 2 + 3(2) − 2 = 4 + 6 − 2 = 8.

So f sends 2 to 8.
You have seen functions graphed before. Fig. 6.1 shows the graph of the function f (x) = x + 3x − 2. The x-
2

axis is the set of inputs and the outputs are graphed directly above (or below) the input. So, the graph of a
function is really just a picture that shows the relationship between elements of the domain and elements of the
codomain. It turns out that the graphs of functions that you are used to seeing match exactly the set theory
de nition of a function. Consider our function f : R → R de ned by f (x) = x + 3x − 2. According to the set
2

theory de nition of a function, a function is a subset f ⊂ R × R that satis es two properties: (1) if x ∈ R then
there is a y ∈ R such that (x, y) ∈ f , and (2) this y is unique. Finally, recall that R × R is exactly the plane. For
every input x from ℝ there is an output y from ℝ so that (x, y) ∈ f ⊂ R × R. The output y is given by
x + 3x − 2, which is why we often write y = x + 3x − 2 for a function. So, according to the de nition the
2 2

function f is given by

2
f = {(x, x + 3x − 2) x ∈ R} ⊂ R × R.
Figure 6.1 The graph of the function

. This graph (gray) is exactly the set f de ned by

. The element

.
{(x, x
2
f (x) = x

+ 3x − 2)


2

(2, 8) ∈ f

is shown as a black dot. Every point on the graph (gray) represents an element of

f ⊂ R × R
+ 3x − 2

x ∈ R} ⊂ R × R

Every point in the graph of the function in Fig. 6.1 is an element of the function set f ⊂ R × R. This de nition
gives us a wonderful way to tell if a graph is a function or not called the vertical line test. If the y is not unique at
some value of x then there are at least two points (x, y ) and (x, y ), where y ≠ y , that are on the graph. Both
1 2 1

line that intersects the graph in two or more places then the graph does not represent a function, see Fig. 6.2.

a function.

Example 6.12

Find the domain and range of the graphed function and then nd the function rule. Here the graph of the
function is simply the four shown points.
2

of these points happen where the graph intersects a vertical line drawn at x. So, if it is possible to draw a vertical

Figure 6.2 Using the vertical line test. The vertical line (black) intersects the graph (gray) in three places (black dots). Therefore the graph is not
The function rule tells us what element from the range is assigned to an element from the domain,

f (−3) = −2, f (−2) = 4, f (1) = 2, f (4) = −3.

Thus the domain and range are given by

Domain of f = {−3, −2, 1, 4}, Range of f = {−3, −2, 2, 4}.

Another way to show this function is with the following picture.

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

Consider the function f : X → R given by

5x
f (x) = .
2x − 1

The graph of this function is shown.


We start by evaluating this function at the points x = −2, x = 0, and x = 6 . This means we want to nd out
what y value in the set ℝ the values −2, 0, and 6 are assigned.

5(−2) 5(0) 5(6)


f (−2) = = 2, f (0) = = 0, f (6) = = 2.727272 …
2(−2) − 1 2(0) − 1 2(6) − 1

Thus −2 is assigned to, or sent to, 2, 0 is sent to 0, and 6 is sent to 2.727272 …. Next we nd the domain of f.
For this we look carefully at the rule, or algebra expression, that de nes f,

5x
.
2x − 1

We know that division by zero is unde ned, so we are not allowed to use any x value that results in
2x − 1 = 0. To nd this x value we simply solve this equation to nd x = . Thus the domain is the set
1

1 1 1
Domain of f = R − { } = (−∞, ) ∪ ( , ∞).
2 2 2

Example 6.14

Use the vertical line test to determine if the following graph represents a function.

Since it is possible to draw a vertical line that intersects the graph (gray) more than once, the graph does not
represent a function.

Example 6.15
Use the vertical line test to determine if the following graph represents a function.

Since it is not possible to draw a vertical line that intersects the graph (gray) more than once, the graph does
represent a function.

6.3 FUNCTION COMPOSITION AND INVERSES


We will now study a way to link two or more functions together called composition. When we compose two
functions the output of the rst function becomes the input of the second function.
De nition 3. Suppose we have two functions, f : X ⟶ Y and g : Y ⟶ Z . The composition of f and g is a new
function g ∘ f : X ⟶ Z given by (g°f )(x) = g(f (x)).
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 rst then then apply g to the result.

Example 6.16

Suppose we are given the functions f : X → Y and g : Y → Z as pictured. Find g°f .

To nd the composition function g ∘ f : X ⟶ Z we rst apply f to the elements of X, nd their outputs, and
then apply g to these outputs,

(g°f )(1) = g(f (1)) = g(b) = δ, (g°f )(2) = g(f (2)) = g(c) = γ,

(g°f )(3) = g(f (3)) = g(a) = β, (g°f )(4) = g(f (4)) = g(d) = α.

The composite function of f and g is pictured as


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 de ned by

2
f (x) = x + 2x − 3

and

g(x) = 3x − 1.

We want to nd the composite function g°f . One can imagine this as a series of function machines. Here the
variable x enters the function machine f and the output is f (x), which then enters the next function machine g.
The output from function machine g is g(f (x)).

If we put x = 4 into the function machine f then f (4) = 4 + 2(4) − 3 = 21 would come out. Then the 21
2

would go into the g machine and g(21) = 3(21) − 1 = 62 would come out. The same thing would happen for
any other number you put in.
Now we nd the formula for the composite function by putting the variable x into the machine instead of a
number. Clearly x + 2x − 3 would come out of the f machine which would then go into the g machine. What
2

would come out of the g machine? To nd out we put the x + 2x − 3 into the formula for g(x) and then
2

simplify,

(g°f )(x) = g(f (x))

2
= g(x + 2x − 3)

2
= 3(x + 2x − 3) − 1

2
= 3x + 6x − 9 − 1

2
3x + 6x − 10.

This gives us a formula for (g°f )(x) = 3x 2


+ 6x − 10 . Notice that

2
(g°f )(4) = 3(4) + 6(4) − 10

= 3(16) + 24 − 10

= 62,

which is exactly what we got before.


To nd f °g we do the same thing. We can imagine a series of function machines.
Then to nd 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)

2
= (3x − 1) + 2(3x − 1) − 3

2
= (9x − 6x + 1) + (6x − 2) − 3

2
= 9x − 4.

This gives the formula for (f °g)(x) = 9x − 4. Notice, g°f


2
≠ f °g . Of course we can compose more than two
functions. We can compose three functions, or even more.

Example 6.17

Let f (x) = 2x − 3, g(x) = −3x, and h(x) = x 2


+ 3 .

Find (g°f )(x).

(g°f )(x) = g(f (x)) = g(2x − 3) = −3(2x − 3) = −6x + 9

Find (f °g)(x).

(f °g)(x) = f (g(x)) = f (−3x) = 2(−3x) − 3 = −6x − 3

Find (h°f )(x).

2 2 2
(h°f )(x) = h(f (x)) = h(2x − 3) = (2x − 3) + 3 = 4x − 12x + 9 + 3 = 4x − 12x + 12

Find (h°g°f )(x). We already know g(f (x)) = −6x + 9

2 2 2
(h°g°f )(x) = h(g(f (x))) = h(−6x + 9) = (−6x + 9) + 3 = 36x − 54x − 54x + 81 + 3 = 36x −

Find g 2
(x) = (g°g)(x) .

(g°g)(x) = g(g(x)) = g(−3x) = −3(−3x) = 9x.


Positive exponents on functions are different from exponents on numbers. Instead of meaning repeated
multiplications they mean repeated compositions.

We now will study how to nd the inverse of a function. Again, in a more advanced book we would have to be
very careful with domains and ranges, but here all of our examples will be very simple. We begin by de ning the
identity function and the inverse function.

De nition 4. Suppose X is a set. The identity function on X is the function i : X → X given by i(x) = x.

De nition 5. Suppose f : X → Y and g : Y → X are functions. If g°f : X → X is the identity function on X


and f °g : Y → Y is the identity function on Y then g is the inverse function of f and f is the inverse function of
g.
The inverse function of f is usually written as f . The exponent −1 tells you we are talking about the inverse
−1

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

If X = {1, 2, 3, 4} then the identity function on X is given by the rule

i(1) = 1, i(2) = 2, i(3) = 3, i(4) = 4.

The identity function on X is shown here.

Example 6.19

If X ,
= {1, 2, 3} Y = {a, b, c} , nd the inverse function of f for f : X → Y given by

f (1) = a, f (2) = c, f (3) = b.

Function f is pictured by
The inverse function f −1
: Y → X is given by

−1 −1 −1
f (a) = 1, f (c) = 2, f (b) = 3.

You can get the picture for f −1


just by reversing the arrows on the picture for f. This give us

Notice that

−1 −1 −1 −1 −1 −1 −1 −1
(f °f )(1) = f (f (1)) = f (a) = 1, (f °f )(2) = f (f (2)) = f (c) = 2, (f °f )(3) = f (f (3))

so f −1
°f is exactly the identity function i.

Example 6.20

Let f , g : R → R be given by f (x) = x + 6 and g(x) = x − 6. Show that g is the inverse function of f.

But that is easy to do since

(g°f )(x) = g(f (x)) = g(x + 6) = (x + 6) − 6 = x

and so g°f = i , the identity function. In other words, we have found that f −1
(x) = x − 6 .

Example 6.21

Let f : R → R be given by

3x − 2
f (x) = .
5

To nd f −1
: R → R we rst rewrite the function and call the outputs y,
3x − 2
y = .
5

We then switch the variables x and y in the equation,

3y − 2
x = .
5

We then solve for y,

3y − 2 5x + 2
x = ⇒ 5x = 3y − 2 ⇒ 3y = 5x + 2 ⇒ y = .
5 3

We have found the inverse function, f −1


(x) =
5x+2

3
.

6.4 PROBLEMS

Question 6.1 For the function pictured in Fig. 6.3,

(a) nd the domain, codomain, and range of the function,


(b) determine if the function is one-to-one and/or onto,
(c) write the function as a rule that acts on each element of the domain, and
(d) write the function as a subset of the cartesian product of the domain and the codomain.

Question 6.2 Let the domain be X = {1, 2, 3, 4} and the codomain be Y = {a, b, c, d, e} . The functions f1, f2,
and f2 are given by the rules:

f 1 (1) = cf 2 (1) = cf 3 (1) = bf 1 (2) = bf 2 (2) = df 3 (2) = bf 1 (3) = cf 2 (3) = ef 3 (3) = df 1 (4) = ef 2 (4) = bf

For each of these functions,

(a) nd the range of the function,


(b) determine if the function is one-to-one and/or onto,
(c) draw a picture showing how the function that acts on each element of the domain, and
(d) write the function as a subset of the cartesian product of the domain and the codomain.
Figure 6.3 Functions for questions 6.1 and 6.11.

Question 6.3 Let the domain be X = {1, 2, 3, 4, 5, 6, 7} and the codomain be Y = {a, b, c, d, e, f } . The functions
f1, f2, and f3 are given by the rules:

f 1 (1) = cf 2 (1) = af 3 (1) = bf 1 (2) = df 2 (2) = cf 3 (2) = cf 1 (3) = ef 2 (3) = ef 3 (3) = df 1 (4) = f f 2 (4) = b

For each of these functions,

(a) nd the range of the function,


(b) determine if the function is one-to-one and/or onto,
(c) draw a picture showing how the function that acts on each element of the domain, and
(d) write the function as a subset of the cartesian product of the domain and the codomain.
Question 6.4 Let the domain be X = {1, 2, 3, 4, 5} and the codomain be Y = {a, b, c, d, e}. The functions f1, f2,
and f3 are given as subsets of the Cartesian product of X and Y by:

f 1 = {(1, c), (2, b), (3, d), (4, c), (5, a)} ⊂ X × Y f 2 = {(1, d), (2, d), (3, c), (4, b), (5, a)} ⊂ X × Y f 3 = {(1

For each of these functions,

(a) nd the range of the function,


(b) determine if the function is one-to-one and/or onto,
(c) draw a picture showing how the function acts on each element of the domain, and
(d) write the function as rule.

Question 6.5 Let the domain be X = {1, 2, 3, 4, 5, 6} and the codomain be Y = {a, b, c, d, e, f } . The functions
f1, f2, and f3 are given as subsets of the Cartesian product of X and Y by:

f 1 = {(1, b), (2, c), (3, a), (4, e), (5, f ), (6, d)} ⊂ X × Y f 2 = {(1, b), (2, b), (3, b), (4, e), (5, e), (6, e)} ⊂ X ×

For each of these functions,

(a) nd the range of the function,


(b) determine if the function is one-to-one and/or onto,
(c) draw a picture showing how the function acts on each element of the domain, and
(d) write the function as a rule.

Question 6.6 For the functions graphed in Fig. 6.4,

(a) nd the domain and range of the function,


(b) draw a picture showing how the function acts on each element of the domain,
(c) write the function as a rule that acts on each element of the domain, and
(d) write the function as a subset of the cartesian product of the domain and the codomain.

Question 6.7 The following function rules are given by an equation. Evaluate these functions at the values
x = −3, x = 0, and x = 4.

(a) f (x) = 3x + 7
(b) f (x) = 3(x + 4) − 5
(c) f (x) = −4x + 6
(d) f (x) = 2 x

(e) f (x) = x 2
+ 3x

(f) f (x) = (x − 2)(x + 3)


(g) f (x) = −x 3
+ 2x
2
− x

(h) f (x) = 2 ⋅ 3 −x+1

(i) f (x) = (x
2
2
− 1)

Question 6.8 Use the vertical line test to determine if the graphs in Fig. 6.5 represent functions. If they do,
determine the domain and range of the functions.
Figure 6.4 Functions for question 6.6.

Question 6.9 Let X = {1, 2, 3, 4} and Y = {a, b, c, d} and Z = {α, β, γ, δ} . The functions f : X → Y and
g : Y → Z are given by

f (1) = cg(a) = δf (2) = bg(b) = γf (3) = dg(c) = αf (4) = ag(d) = β

Find g°f : X → Z

Question 6.10 Given the functions f and g in Fig. 6.6, nd g°f . What is the domain, codomain, and range of g°f ?
Then nd f °g. What is the domain, codomain, and range of f °g?

Question 6.11 For each function pictured in Fig. 6.3, nd the inverse function if it exits.
Figure 6.5 Graphs for question 6.8.

Question 6.12For each function given in question 6.2 nd the inverse function if it exists.

Question 6.13 For each function given in question 6.3 nd the inverse function if it exists.

Figure 6.6 Functions for question 6.9.

Question 6.14 For each function given in question 6.4 nd the inverse function if it exists.

Question 6.15 Are the following pairs of functions inverses of each other or not?

(a) f (x) = −10+x

4
and g(x) = 4x + 10
(b) f (x) = −(x + 2) and g(x) = 2 + x
3 3

(c) f (x) = 3

x
− 1 and g(x) = x+1
3

(d) f (x) = 2 − 2

3
x and g(x) = −2x+4

Question 6.16 Find the inverse of each of the following functions.

(a) f (x) = 7x
(b) f (x) = 1

x
− 3

(c) f (x) = −5x + 2


(d) f (x) = 8x+10

(e) f (x) = √x − 4
(f) f (x) = 2

x−3

1 Note, the word “function” is often used in programming in a different way. There it refers to a block of reusable code in a program that can be
called when it is needed.
CHAPTER 7
Number Representations

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.

7.1 ADDITION AND MULTIPLICATION PRINCIPLES


Throughout this course we have already, and will continue to, encounter a
few different counting problems. For example, given three propositions p,
q, and r, each of which is either true or false, how many possible
combinations of true and false are there for these three propositions? This
will give us the number of rows we need in making a truth table. Or we
want to know how many possible subsets a set A could have. This gives us
the cardinality of the power set of A. Or suppose we want to know how
many possible spanning trees a graph has. Or the maximum possible
number of times a loop in an algorithm could be executed. These are all
examples of counting problems in computer science. When it comes to
counting there are two very important ideas, or principles, that get used.
They are the addition principle and the multiplication principle. We will
begin with the addition principle.

De nition 1. The Addition Principle: Suppose there are n ways for event
1

E to occur, and n ways for event E to occur, and we cannot do both E


1 2 2 1

and E . If all these ways are distinct then the number of ways for E or E
2 1 2

to occur is n + n .
1 2
When we say that all the ways are distinct, we mean that none of the ways
are the same as any of the other ways. Let us look at an example to see how
this works.

Example 7.1

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 n = 10 ways for E1 to
1

occur and n = 8 ways for E2 to occur. There are


2

n + n = 10 + 8 = 18 ways for E1 or E
1 2 to occur. Thus you have 18
2

choices for buying a chocolate cake or buying a vanilla cake.

Using the language of set theory, the addition principle could also be
written this way.

De nition 2. The Addition Principle in the language of set theory: If A and


B are nite 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 de nition we
need to spend a moment to make sure we understand some notation. The
capital Greek letter sigma, written as Σ, means “sum” or add. Suppose we
had the formula
4

∑ xi .

i=1

Notice the i = 1 written below the Σ and the 4 is written above the Σ.
What exactly does this formula mean? It means we are to add all the
variables xi from i = 1 to 4. This is very similar to how the for-do loops
work. So we have

∑ xi = x1 + x2 + x3 + x4 .

i=1

Similarly, the large ∪ means to take the union. Suppose we had the formula

∪ Ai .
i=1

This works exactly like above, it means we take the union of all the sets Ai
from i = 1 to 6. Thus,

∪ Ai = A1 ∪ A2 ∪ A3 ∪ A4 ∪ A5 ∪ A6 .
i=1

Of course there is nothing special about 4 or 6. It could be any number at


all. Now we are ready for the next de nition.
De nition 3. The generalized Addition Principle: If A , A , … , A are
1 2 n

nite sets with A ∩ A = ∅ for all i, j with 1 ≤ i < j ≤ n, then


i j

∪ ∪ ∪
∣ A1 ∪ A2

which could also be written as

Example 7.2
∪ ⋯

i=1
n


Ai
An

=
=


i=1
A1 +

|A i |.

Suppose you want to go from town X to town Y. You can go by air, by


A2

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 |A | = 2. If A2 is


the set that contains the ways to go by land then |A | = 4. If A3 is the
set that contains the ways to go by sea then |A | = 3. Clearly
A ∩ A = ∅, A ∩ A = ∅, and A ∩ A = ∅. So we have
1 2

A1
1


3

A2 ∪ A3
2

Now we will take a look at the multiplication principle.


3
2

|A 1 | + |A 2 | + |A 3 |

2 + 4 + 3

9.

De nition 4. The Multiplication Principle: Suppose there are n ways for


event E to occur, and each possible way that E occurs allows for exactly
1 1
An ,

1
n2 ways for event E2 to occur. Then the number of ways for E1 and E2 to
occur is n ⋅ n .
1 2

Example 7.3

In a class of 11 boys and 12 girls, how many ways are there to select
two students, one a boy and one a girl?

Let E1 be the event of choosing one boy, and E2 be the event of


choosing one girl. There are n = 11 ways for E1 to occur and n = 12
1 2

ways for E2 to occur. There are n ⋅ n = 11 ⋅ 12 = 132 ways for E1 and


1 2

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.

De nition 7.5 The Multiplication Principle in the language of set theory: If


A and B are nite sets and A × B is the Cartesian product of A and B, then

|A × B| = |A| ⋅ |B|.

Example 7.4

You own 7 pairs of pants and 15 shirts. How many out ts can you
make?

Let A be the set that contains 7 pairs of pants and B be the set that
contains 15 shirts. Each out t consists of one pair of pants and one shirt,
so each out t is an element of A × B. The number of different out ts
possible is
|A × B| = |A| ⋅ |B| = 7 ⋅ 15 = 105.

The multiplication principle can also be generalized to more than two


events.

De nition 6. The generalized Multiplication Principle: If A1 , A2 , … , An

are nite sets then

|A 1 × A 2 × ⋯ × A n | = |A 1 | ⋅ |A 2 | ⋯ |A n |.

Example 7.5

How many different seven digit binary sequences can be formed?

Here we have A = {0, 1} for 1 ≤ i ≤ 7. A seven digit binary sequence


i

is an element of the set A × A × ⋯ × A . The number of different


1 2 7

seven digit binary sequences is given by

7
A1 × A2 × ⋯ × A7 = A1 ⋅ A2 ⋯ A7 = 2 = 128.

7.2 COUNTING ALGORITHM LOOPS


One of the most important applications of the addition principle and the
multiplication principle in computer science is in counting the maximum
number of times conditional controls and loops are executed in algorithms.
This is very important in helping computer scientists and programmers
know how long their algorithms will take to execute. We will cover this in
chapter 8. For now we will simply use the addition and multiplication
principles to see how they can help us.
Example 7.6

What is the maximum number of times the while-do loop in step 3 is


executed?

Algorithm: Evaluates n! where n is a positive integer.

1. Input positive integer n.


2. answer ← n
3. While n > 1do
3.1
n ← n − 1

3.2
answer ← answer × n

4. Output n.

Strictly speaking, this example does not require either the addition or
the multiplication principles. The while-do loop in step 3 executes
while n > 1. Notice that in step 3.1 we have n ⟵ n − 1, so n is
decreased by one each time the loop executes. Suppose we input
positive integer n = 5; then the while-do loop would execute when n
was 5, 4, 3, and 2. It would not execute when n = 1. Therefore the
while-do loop would execute four times. Suppose we input positive
integer n = 10. This time it would execute nine times. In general,
whenever we input the positive integer n the while-do loop executes
n − 1 times.

Example 7.7
What is the maximum number of times the for-do loop in step 2 is
executed? What is the maximum number of times the if-then
conditional control in step 2.1 is executed?

Algorithm: Searches a string of integers x1 , … , xn to see if it


contains the integer s.

1. Input string x , … , x , n and s.


1 n

2. For i = 1 to n do
2.1. If x = s then
i

2.1.1. Return “String contains s.”


3. Return “String does not contain s.”

Again, strictly speaking, this example does not require either the
addition or the multiplication principles. However, it shows us
something important. The for-do loop in step 2 is executed when i goes
from 1 to n. This means it is executed a maximum of n times. Each time
the for-do loop is executed the If -then conditional control is also
executed. Thus the maximum number of times the If -then conditional
control is executed is n times.
Notice, it is possible that both the for-do loop and If -then conditional
control are executed fewer times. If for some number i < n we have
x = s then the If -then conditional control step 2.1.1 is executed, the
i

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 x , … , x was input we
1 n

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?

Algorithm: Searches two strings of integers x , x , … , x and


1 2 n

y ,y ,…,y
1 2 of equal length to see if either string contains integer
n

s.

1. Input strings x , … , x and y , … , y , n and s.


1 n 1 n

2. For i = 1 to n do
2.1. If x = s then
i

2.1.1. Return “String one contains integer s.”


3. For i = 1 to n do
3.1. If y = s then
i

3.1.1. Return “String two contains integer s.”


4. Return “Neither string contains integer s.”

The for-do loop in step 2 is executed when i goes from 1 to n. This


means that the maximum number of times the for-do loop is executed is
n times. Similarly, the for-do 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 nd 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?

Algorithm: Checks if two strings x , … , x and y , … , y contain


1 n 1 n

a common integer. (That is, check and see if there is an integer s


that is contained in both strings.)

1. Input strings x , … , x and y , … , y .


1 n 1 n

2. For i = 1 to n do
2.1 For j = 1 to n do
2.1.1 If x = y then
i j

2.1.1.1 Return “There is an element common to both


strings.”
3. Return “There are no elements in common to both strings.”

The maximum number of times the for-do loop in step 2 is executed is


clearly n times. To gure out the maximum number of times the for-do
loop in step 2.1 is executed requires the multiplication principle. When
i = 1 the for-do loop in step 2.1 is executed n times. Then when i = 2

the for-do loop in step 2.1 is again executed n times. The same when
i = 3, and so on. So for each of the n times the for-do loop in step 2 is

executed the for-do loop in step 2.1 is executed n times. This means the
for-do loop in step 2.1 is executed a maximum of n × n = n times. 2

The If -then conditional control in step 2.1.1 is executed once for each
time the for-do loop in step 2.1 is executed, so it is also executed a
maximum of n2 times.

7.3 PERMUTATIONS AND ARRANGEMENTS


Often we need to know how many different ways can we order, or arrange,
a number of items. For example, how many different ways are there to
order the three letters a, b, and c? A little thought will give us

abc, acb, bac, bca, cab, cba.

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 rst 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 third space
we put whatever letter is left,

− − −

3 2 1

possiblities possiblities possiblities

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.

Even if it looks a little funny we also de ne 0! = 1. In general we have

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.

Algorithm: Evaluates n! where n is a positive integer.

1. Input positive integer n.


2. answer ← n
3. While n > 1do
3.1
n ← n − 1

3.2
answer ← answer × n

4. Output n.
Factorials are exactly the notation we need to help us write down how
many permutations are possible. The number of permutations of a set of n
elements is given by n!.

Example 7.10

How many permutations of the letters p, q, r, s, t, u, v are possible?


Since there are seven letters the number of permutations possible is
given by 7! = 5 040.

However, we are often interested in a slightly more complicated type of


permutation problem. How many ways are there to order three letters
selected from the alphabet? In other words, how many permutations are
possible if we choose three different elements from a set of 26 different
elements. It is a lot more dif cult to simply write down all the
possibilities, but we can gure out how many possibilities there are using
the same method as before. Since we are choosing three elements we need
three spaces, one for each element. In the rst space there are 26 possible
elements we could put. In the second space we can put one of the 25
remaining elements. And in the third space we can put one of the 24
elements that are still remaining,

− − −

26 25 24

possiblities possiblities possiblities

so we have 26 ⋅ 25 ⋅ 24 = 15 600 possible orderings. We can come up with


a nice formula for this,
26⋅25⋅24⋅ 23 ⋅ 22 ⋅ 21 ⋯ 3 ⋅ 2 ⋅ 1
26 ⋅ 25 ⋅ 24 =
23 ⋅ 22 ⋅ 21 ⋯ 3 ⋅ 2 ⋅ 1

26!
=
23!

26!
= .
(26−3)!

In general we may wish to choose r objects from a set of n objects to ll


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 P , which is read out loud as “n-pee-r.” Thus we
r
n

have the formula

n!
n
Pr = .
(n − r)!

Example 7.11

How many different ways are there to arrange 5 letters from the
alphabet?
The number of permutations of 5 letters from the 26 letters of the
alphabet is given by
26! 26!
26
P5 = = = 7 893 600.
(26 − 5)! 21!

Example 7.12

You are given a list of 20 different avors of ice cream and asked to
rank your four favorite avors. How many different possible responses
are there?

You are basically being asked to nd the number of permutations of


four items from 20 items, or P , which is given by
20
4

20! 20!
20
P4 = = = 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 rst person selected becomes
the chairperson, the second person becomes the secretary, and the third
person becomes the treasurer. Thus this becomes a permutation
question, how many permutations of three items from 12 items. The
answer is given by
12! 12!
12
P3 = = = 1 320.
(12 − 3)! 9!

7.4 COMBINATIONS AND SUBSETS


Now suppose we want to know how many subsets of size r there are in a set
of n elements. This is almost like the permutation question, only we no
longer care about the order. We only care about the actual combination of r
elements, not in the orderings, or permutations, of those r elements.
Consider the example where we wanted to know how many permutations
of three letters there are from the alphabet. Recall that we found the
number of possible orderings by considering how we would ll the three
spaces,

− − −

26 25 24

possiblities possiblities possiblities

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,

abc, acb, bac, bca, cab, cba.

Considering how the three letters could be ordered in the three spaces gave
us
− − −

3 2 1

possiblities possiblities possiblities

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 nd the total number of combinations we have to
divide the number of permutations of three letters, which was
= 15 600, by 3! = 6. This gave us a total of 2 600 different
26
P
3

combinations.
In the general case where we want to know how many combinations of r
objects drawn from n objects there are we need to divide the number of
permutations, P , by r! to give us
n
r

n
Pr n!
= .
r! r!(n − r)!

The standard notation for the number of combinations of r objects chosen


from n objects is n\chooser, though sometimes C is also used.1 Thus we
n
r

have

n n!
( ) = .
r r!(n − r)!

Example 7.14

How many different combinations of ve 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?

We want to nd the number of combinations of 2 elements from a set of


n elements. This is given by
2
n n! n(n − 1) n − n
2
( ) = = = = 0.5n − 0.5n.
2 2!(n − 2)! 2 2

7.5 PERMUTATION AND COMBINATION EXAMPLES


So far we have covered the addition principle, the multiplication principle,
permutations, and combinations. Most real-world and computer science
problems require us to apply more than one of these ideas at the same time
and often require a little creativity.

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 nd the number of ways from X to Y,


which is given by 3 + 5 = 8. We also use the addition principle to nd
the number of ways from Y to Z, which is given by 4 + 2 = 6. Then to
nd 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 P = 6! = 720.
6
6

(b) The word HELLO has ve letters but two of the letters are L.
There are P = 5! = 120 ways of permuting ve letters.
5
5

However, switching the two Ls does not change permutation so


there are 120/2 = 60 different permutations.
(c) The word MISSISSIPPI has 11 letters and there are P = 11! 11
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

Let X = {1, 2, 3, 4, 5} and Y = {a, b, c, d, e, f , g}. How many different


functions f : X → Y are there?

Recalling the de nition 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

5
7 × 7 × 7 × 7 × 7 = 7 = 16 807.
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 functions f : X → Y are there?

Recalling the de nition 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!
7
P5 = = = 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


22
given by ( ) and the number of ways we can choose three men from
3

27
27 men is given by ( ) . We then use the multiplication principle to
3

nd the total number of ways the committee can be formed,

22 27
( ) ⋅ ( ) = 4 504 500.
3 3
Example 7.22

A group of 20 individuals has to elect an executive board consisting of a


president, a secretary, and a treasurer and three at-large trustees. how
many different possible ways are there to do this?

For the rst 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 rst three positions rst there are P 20
3

possibilities. We then choose the second three positions where there are
now C possibilities. We now use the multiplication principle to get
17
3

the total number of possibilities.

20 17
P3 ⋅ C3 = (6 840)(680) = 4 651 200.

But suppose we decided to rst choose the three at-large trustees and
then choose the president, secretary, and treasurer. In this case the
number of possibilities for the rst three positions is C and the 20
3

number of possibility for the second three positions is P . Using the


17
3

multiplication principle we have

20 17
C3 ⋅ P3 = (1 140)(4 080) = 4 651 200.

It should not surprise you that these numbers are the same.

Example 7.23
What is the maximum number of times the for-do loop in step 2 is
executed? What is the maximum number of times the for-do loop in 2.1
is executed? What is the maximum number of times the If -then
conditional control in setp 2.1.1 is executed?

Algorithm: Check if a string of integers x1 , x2 , … , xn contains


any integer more than once.

1. Input string x , … , x .
1 n

2. For i = 1 to n −1 do
2.1 For j = i + 1 to n do
2.1.1 If x = x then
i j

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 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 nd this,

2
n n(n − 1) n − n
2
f (n) = ( ) = = = 0.5n − 0.5n,
2 2 2

so the maximum number of times the for-do loop in step 2.1 is executed
is 0.5n − 0.5n times. The maximum number of times the If -then loop
2

in step 2.1.1 is executed is the same.


7.6 PROBLEMS

Question 7.1 How many ve digit decimal number are there that contain
only odd digits?

Question 7.2 How many ve digit octal number are there that contain only
odd digits?

Question 7.3 How many ve digit hexadecimal number are there that
contain only odd digits?

Question 7.4 How many different ways can a rst, second, and third prize
be awarded in a class of 32 students.

Question 7.5 How many different ways are there to choose a ve person
committee that has the positions chair, vice-chair, communications
director, secretary, and treasurer from a group of 20 people?

Question 7.6 How many different ways are there to choose a committee of
ve people from a group of 20 people?

Question 7.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 identi cation numbers to its
staff. Teachers have an ID that starts with a T which is followed by four
digits. Administrators have an ID that starts with an A and is followed by
three digits. How many total identi cation numbers are available for the
school to use?

Question 7.9 How many anagrams are there for the following words?

(a) COMBINE
(b) PERMUTE
(c) ASSIGNMENT

Question 7.10 Let X = {u, v, w, x, y, z} and Y = {1, 2, 3, 4, 5, 6, 7, 8, 9}.


How many different functions f : X → Y are there? How many one-to-
one functions are there?

Question 7.11 Let X = {α, β, γ, δ} and


Y = {A, B, C, D, E, F , G, H , I , J , K}. How many different 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 rst two
pools have 10 questions each and the last two pools have 20 questions
each. If two questions are drawn at random from each of the rst two pools
and three questions are drawn at random from each of the second two pools
(for a total of ten questions) how many possible quizzes can be generated.
(Assume the order of the questions does not matter.)

Question 7.13 What is the maximum number of possible times the while-
do loop in line 4 could execute?

1. Input x , x , … , x .
1 2 n

2. i ← 1
3. order ← true
4. While i < n and order = true do
4.1
If x > x
i i+1 then
4.1.1
order ← f alse

4.2
i ← i + 1

5. If order = true then


5.1
Output “Numbers are in order.”
else
5.2
Output “Numbers are out of order.”

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 a where 1 ≤ i ≤ n and 1 ≤ j ≤ n. Thus the matrix A
ij

consists of n2 numbers. The matrix B is de ned 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?

1. Input a and b for 1 ≤ i ≤ n and 1 ≤ j ≤ n.


ij ij

2. For i = 1 to n do
2.1
For j = 1 to n do
2.1.1
sum ← 0

2.1.2
For k = i to n do
2.1.2.1
sum ← sum + x ik × x kj

2.1.3
c ij ← sum

3. Output c . ij
Question 7.15 The following is an algorithm that sorts a list of n numbers
into increasing order. What is the maximum number of times the for-do
loop in step 2 could execute? What is the maximum number of times the
while-do loop in step 2.3 could execute?

1. Input x , x , … , x .
1 2 n

2. For i = 2 to n do
2.1
insert ← x i

2.2
j ← i − 1

2.3While j ≥ 1 and x j > insert do


2.3.1
x j+1 ← x j

2.3.2
j ← j − 1

2.4
x j ← insert

3. Output x 1, 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?

1. Input adjacency matrix (a ). ij

2. For i = 1 to n do
2.1. a = 1
ii

3. For i = 1 to n do
3.1 For j = 1 to n do
3.1.1 If a = 1 then
ij

3.1.1.1 For k = 1 to n do
3.1.1.1.1 a ← a + a (Boolean Addition)
jk jk ik

4. Output (a ). ij

1 Other notation for


n
P
r

and
n
C
r

that one encounters are

nP r

and

nCr

or

P (n, r)

and

C(n, r)

.
CHAPTER 8
Algorithmic Complexity

One of the most important jobs for programmers is choosing, or designing, the fastest
algorithms to use when writing programs. Therefore, it is extremely important that they have
some way to compare the speed of different algorithms. But algorithms can be very
complicated and very different from each other, so comparing them is not easy. This chapter
focuses on how this is done.

8.1 OVERVIEW OF ALGORITHMIC COMPLEXITY


How long does it take for an algorithm, implemented as a computer program, to run? This is
an extremely important question for computer scientists and programmers. The answer
depends on many different factors, like how powerful the computer is, the programming
language, the computer operating system, the computer memory, and so on. But it also
depends on the algorithm being used and what the input is. In this chapter we will only
consider two factors, the algorithm and the input, and ignore the other factors. We are
basically analyzing how complex the algorithm is.
Because this chapter is quite complicated we will begin with a big-picture overview of
algorithmic complexity. The following owchart gives the general idea. Hopefully it will
help you to understand all that is going on in this chapter.

f : N ⟶ N

Algorithm

Find time-complexity f unction f or algorithm.

Find big-O category f or time-complexity f unction.

Use big-O category to rank algorithms by speed.

The general idea is that given any algorithm one can nd a function associated with that
algorithm that essentially describes how long it takes the algorithm to run. Because
algorithms can be so different and so complicated we need to make three simplifying
approximations. The function found is called the algorithm's time-complexity function.
Once the time-complexity function for the algorithm is obtained we nd what is called the
big- O (pronounced “big-oh”) category for the time-complexity function. This requires us to
make another two simplifying approximations. Doing this essentially groups together all the
algorithms that have roughly the same speed into the same category. In other words, we
usually think of all the algorithms whose time-complexity functions are in the same big- O
category as being equally fast. Finally, we then use the big- O categories to rank algorithms
by speed.
As we have already stated, algorithmic complexity analysis requires us to make a total of
ve simplifying approximations. These ve approximations are listed below. The rst three
approximations are needed to nd the time-complexity function for an algorithm and are
studied in section 8.2. The last two approximations are related to nding the big- O category
of the time-complexity function and will be studied in section 8.4.

Approximation One: Count only the most frequently executed operations.


Approximation Two: Of the most frequently executed operations, count only the most time-
consuming operation.
Approximation Three: Analyze the worst-case scenario.
Approximation Four: Assume the input is large.
Approximation Five: Do not distinguish between two time-complexities that are a constant
multiple of each other.

8.2 TIME-COMPLEXITY FUNCTIONS


We begin this section by giving the de nition of the time-complexity function for an
algorithm. But it will take the rest of the section to fully understand this de nition.
De nition 8.1 The time-complexity function of an algorithm is a function

f : N ⟶ N

where ℕ is the set of natural numbers and where

M aximum number of

do min ant operations

f (n) = perf ormed by the

a lg orithm if the input

is of size n.

This de nition of time-complexity actually uses the rst three approximations. The phrase
“dominant operations” means we are using the rst and second approximations, counting
only the most time consuming frequent operation. The phrase “maximum number” means
we are using the third approximations, using only the worst-case scenario. What exactly is
meant by the phrase “the input is of size n” depends on the exact nature of the algorithm. In
section 8.3 we will look at examples of algorithms and nd the time-complexity functions
for these algorithms.
What we are doing is using the time-complexity function as a way of indirectly measuring
how long an algorithm takes to run. Of course, the answer is not given in terms of a time like
seconds or hours or weeks, it is given in terms of how many times the dominant operations
are performed in the worst case scenario. How long it takes to run the algorithm depends on
the speed of the computer you are using, and every year the speed of computers keeps
getting faster, but the relative times needed to perform operations stays the same.

Approximations One and Two


The rst two approximations are related to nding the dominant operation in the
algorithm. The operations performed in the algorithms in this book are assignments (⟵),
comparisons ( =, ≠, >, <, ≮, ≯), addition, subtraction, multiplications, and division.
According to the rst approximation, we are interested in the operations that are executed
most frequently in the algorithm. Consider the following algorithm.

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 once, whereas the assignment and
the multiplication in step 3.1 are executed each time the for-do loop is executed. The rst
approximation tells us that we are only interested in the operations that are frequently
executed, therefore we are only interested in the operations in step 3.1, not in the operation
in step 2 since it is only executed once. In practice this means we are mostly interested in the
operations that happen inside loops.
The second approximation states that of the most frequently executed operations, we only
count the most time-consuming operation. This means we only count the slowest operation.
The most time-consuming frequent operation is the dominant operation of the algorithm.
Here we list the operations from fastest to slowest:

Assignment (⟵)
Comparisons ( =, ≠, >, <, ≮, ≯)
Addition and subtraction ( +, −)
Multiplication and division( ×, /)
We would like to know how long it would a computer to perform each of these operations.
In general we cannot know for sure, but we can say that assignments are usually faster than
comparisons, which are usually faster than addition and subtraction, which in turn are
usually faster than multiplication and division.
Let us consider the two operations from step 3.1 of the above algorithm. The two
operations were assignment and multiplication. According to the above list, assignment is
much faster than multiplication, so multiplication is the most time-consuming frequent
operation. That means that multiplication is the dominant operation in this algorithm. Let us
look at another example.

Algorithm: Finds the smallest value in a list of numbers.

1. Input the number of values in list, n.


2. Input the list of numbers x , x , … , x .
1 2 n

3. min ← x 1

4. For i = 2 to n do
4.1 If x < min then
i

4.1.1 min ← x 1

5. Output min.

There is an assignment ⟵ in step 3, which is outside the for-do loop. Inside the for-do
loop there are two operations, a comparison < in step 4.1 and an assignment ⟵ in step 4.1.1.
Using approximation one we can ignore the assignment in step 3 because it is only executed
once. We are only interested in the operations inside the for-do loop. Then, using
approximation two we only consider the most time-consuming of these two operations.
Since comparisons are slower than assignments then the comparison in step 4.1 is the
dominant operation in the algorithm.
Figuring out the dominant operation is a little dif cult sometimes. Notice how we said
that assignments are usually faster than comparisons, which are usually faster than addition
and subtraction, which in turn are usually faster than multiplication and division. If we are
only adding one, or subtracting one, or multiplying by one, we generally consider these as
very fast operations. It is easy to simply add or subtract one, and multiplying by one simply
gives us the original number back. So if an operation is only adding or subtracting one or
multiplying by one then it is usually not considered a dominant operation. Look at the below
algorithm.

Algorithm: Sorts a list x 1, x2, … , xn of n numbers into increasing order.

1. Input x , x , … , x .
1 2 n

2. For i = 2 to n do
2.1
insert ⟵ xi
2.2
j ⟵ i − 1

2.3
While j ≥ 1 and x j > insert do
2.3.1
xj+1 ⟵ xj

2.3.2
j ⟵ j − 1

2.4
xj ⟵ insert

3. Output x 1, x2, … , xn .

There are assignments in steps 2.1, 2.2, 2.3.1, 2.3.2, and 2.4. Assignments are all quite fast.
There are two comparisons in step 2.3. And it appears there is an addition in step 2.3.1 and
subtractions in steps 2.2 and 2.3.2. Using the above list we might be tempted to say that the
addition and subtractions are the dominant operations. But these are simply adding or
subtracting one. The comparison x > insert is almost certainly more time consuming.
j

Therefore in this case the comparisons would be the dominant operation. This is the sort of
thing that will be more obvious to you after you have taken some programming courses.

Approximation Three
The third approximation states that we should only analyze the worst-case scenario.
Consider the following algorithm.

Algorithm: Searches a string of integers x 1, … , xn to see if it contains integer s.

1. Input string x , … , x , n and s.


1 n

2. For i = 1 to n do
2.1. If x = s then
i

2.1.1. Return “String contains s.”


3. Return “String does not contain s.”

Suppose we wanted to use this algorithm to check if integer 7 was contained in each of these
two 50 digit strings,
01234567890123456789012345678901234567890123456789,

00000000000000000000000000000000000000000000000000.

For the rst string the for-do loop is executed eight times. For the second string the for-do
loop is executed all 50 times. The number of times the for-do loop is executed depends on
the input. If we are checking to see if integer s is contained in an n digit string then the
maximum possible number of times the for-do loop could be executed is n times. It may be
executed fewer times than that, but n times is the worst-case scenario. When analyzing
algorithms we always assume any loops are executed the maximum possible number of
times.

8.3 FINDING TIME-COMPLEXITY FUNCTIONS


So far we have discussed the three approximations we need to make in order to nd the time-
complexity function of an algorithm. In this section we use these approximations on some
speci c examples of algorithms to gure out their time-complexity functions.

Example 8.1

What is the time-complexity function of this algorithm?

Algorithm: Searches a string of integers x 1, … , xn to see if it contains the integer s.

1. Input string x , … , x , n and s.


1 n

2. For i = 1 to n do
2.1. If x = s then
i

2.1.1. Return “String contains s.”


3. Return “String does not contain s.”

First we ask ourselves, what is the dominant operation in this algorithm. The only
operation that is done in this algorithm is checking to see if xi is equal to s in step 2.1,
therefore this comparison is the dominant operation. Next we ask ourselves how many
times this operation is performed. We are concerned with the worst-case scenario. If there
is an s in the string then “String contains s.” is returned in step 2.1.1 and the algorithm
ends. But if there is no integer s in the string x , … , x then the for-do loop must run n
1 n

times, once for each xi in the string. This is the worst-case scenario. This means the
dominant operation is done n times. Thus the time-complexity function is

f (n) = n.
Example 8.2

What is the time-complexity function of this algorithm?

Algorithm: Searches two strings of integers x 1, … , xn and y


1, … , yn of equal length
to see if either string contains integer s.

1. Input strings x , … , x and y , … , y , n and s.


1 n 1 n

2. For i = 1 to n do
2.1. If x = s then
i

2.1.1. Return “String one contains integer s.”


3. For i = 1 to n do
3.1. If y = s then
i

3.1.1. Return “String two contains integer s.”


4. Return “Neither string contains integer s.”

The analysis of this algorithm is almost exactly the same as in the last example. In the
worst-case scenario the integer s is not contained in either string. This would mean the
for-do loop in step 2 is carried out n times and then the for-do loop in step 3 is also
carried out n times. The addition principle gives the time-complexity function as

f (n) = n + n = 2n.

Example 8.3

What is the time-complexity function of this algorithm?

Algorithm: Checks if two strings x , … , x and y , … , y contain a common


1 n 1 n

integer. (That is, check and see if there is an integer s that is contained in both
strings.)

1. Input strings x , … , x and y


1 n 1, … , yn .
2. For i = 1 to n do
2.1 For j = 1 to n do
2.1.1 If x = y then
i j
2.1.1.1 Return “There is an element common to both strings.”
3. Return “There are no elements in common to both strings.”

The dominant operation in this algorithm is the comparison in step 2.1.1 where we check
to see if xi is equal to yj. How many times is the operation executed in the worst-case
scenario? Notice that this operation is contained in a nested loop. There is the for-do loop
of step 2 and then the for-do loop of step 2.1. The worst-case scenario happens if there is
no integer that is common to both strings. If this is the case then for i = 1 the for-do loop
in step 2.1 runs n times. Then for i = 2 it runs n times again. Then for i = 3 it runs n
times again, and so on. The multiplication principle tells us the dominant operation
executes a total of n2 times. This means the time-complexity function for this algorithm
is

2
f (n) = n .

Example 8.4

What is the time-complexity function of this algorithm?

Algorithm: Checks to see if a string of integers x1, x2, … , xn contains any integer
more than once.

1. Input string x , … , x .
1 n

2. For i = 1 to n−1 do
2.1 For j = i + 1 to n do
2.1.1 If x = x then
i j

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 rst
for-do runs from i = 1 to n−1 but the second for-do loop only runs from j = i + 1 to n.
Roughly, this algorithm does half the work of the last example so f (n) ≈ . Another
2
n

way to think about it is that the algorithm does one comparison for each set of two non-
equal indices i and j. This is the same as the number of sets of two different numbers
{i, j} that are in the set {1, 2, … , n}. This is a combination problem. Thus the time-

complexity function is given by


n n(n−1) 2
n −n 2
f (n) = ( ) = = = 0.5n − 0.5n.
2 2
2

Example 8.5

What is the time-complexity function of this algorithm?

Algorithm: Sorts a list x 1, x2, … , xn of n numbers into increasing order.

1. Input x , x , … , x .
1 2 n

2. For i = 2 to n do
2.1
insert ⟵ xi

2.2
j ⟵ i − 1

2.3
While j ≥ 1 and x j > insert do
2.3.1
xj+1 ⟵ xj

2.3.2
j ⟵ j − 1

2.4
xj ⟵ insert

3. Output x 1, x2, … , xn.

We have already determined that the dominant operations in this algorithm are the
comparisons in step 2.3. But how many times are these operations performed? First, we
have the loop in step 2 happening n − 1 times. Each time this loop is executed the
variable j is initialized as i − 1. Then each time the while-do loop is executed the j value
is decreased by one. The while-do loop continues until j = 1. In other words, for each
value of i the values of j range form 1 to i − 1. This is just like the previous example, the
number of times the while-do loop is executed is the same as the number of subsets of
two non-equal numbers {i, j} that are in the set {1, 2, … , n}. This is a combination
problem. Since there are two comparisons in step 2.3 the time-complexity function is

n n(n−1)
2
f (n) = 2( ) = 2( ) = n − n.
2
2

Example 8.6

What is the time complexity function for the rst algorithm to nd xn.

Algorithm: First algorithm to nd xn.

1. Input x and n.
2. answer ⟵ x
3. For i = 2 to n do
3.1. answer ⟵ answer × x
4. Output answer.

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.

Example 8.7

What is the time complexity function for the second algorithm to nd xn.

Algorithm: Second algorithm to nd xn.

1. Input x and n.
2. d ⟵ number of bits in n. ( n 10 = bdbd−1bd−2 … b1 2 )
3. If b = 1 then
1
3.1
answer ⟵ x

else
3.2
answer ⟵ 1

4. For i = 2 to d do
4.1
x ⟵ x × x

4.2
If b = 1 then
i

4.2.1
answer ⟵ answer × x

5. Output answer.

We will take a closer look at this algorithm in the next section, but for now notice that
the dominant operation in this algorithm is multiplication in steps 4.1 and 4.2.1. What
controls the number of times the dominant operation is executed? The number of times
the multiplication in step 4.1 happens depends on d, the number of bits of n. The
multiplication in step 4.2.1 depends on whether a particular digit bi in the binary
expansion of n is a 1 or a 0. Thus, the number of dominant operations depends entirely on
the binary expansion of n.
The for-do loop says to repeat steps 4.1 and 4.2 from i = 2 to d, or d − 1 times. The if-
then conditional control tells us to do the multiplication in step 4.2.1 only if the digit bi of
the decimal representation of n is 1. Here we apply approximation three and assume the
worst-case scenario, that every digit in the decimal representation of n is 1. Hence this
multiplication is done d − 1 times as well. Thus, each time the for-do loop happens then
in the worst case scenario there are two multiplications, which means a total of 2(d − 1)
multiplications happen. As will be explained in the next section, d = ⌊log (n)⌋ + 1, so
2

we have

2(d − 1) = 2 ⌊log2(n)⌋ ≤ 2 log2(n)

so the time-complexity function for this second algorithm is given by

f2(n) = 2 log2(n).
A word of caution, by replacing ⌊log (n)⌋ by log (n) the codomain of this time-
2 2

complexity function is no longer ℕ but ℝ, but this is something that is done quite often in
practice.

8.4 BIG-O NOTATION


What we are now interested in is a way to compare algorithms with different time-
complexity functions. In computer science the big- O notation is used to help classify how
fast different algorithms are. We rst explain the two approximations that are used in
determining the big- O category for an algorithm before introducing the de nition toward
the end of the section.

Approximation Four
In the fourth approximation we assume the input is large. In order to understand why
approximation four is important we will consider the two different algorithms for computing
xn. This is the rst algorithm.

Algorithm: First algorithm to nd xn.

1. Input x and n.
2. answer ⟵ x
3. For i = 2 to n do
3.1. answer ⟵ answer × x
4. Output answer.

And here is the second algorithm for computing xn. Notice in the second step we are asked
to nd the number of bits of n. The word bits comes from binary digits and simply means the
number of digits that is in the binary number equal to n. Thus, if n is written as a binary
number with digits b , … , b then n is d bits. For example, if n = 41 = 101001 then
d 1 10 2

d = 6 and b = 1, b = 0, b = 1, b = 0, b = 0, and b = 1. Thus, n has six bits. In general,


6 5 4 3 2 1

the number of bits (binary digits) contained in the decimal number n is given by the formula

d = ⌊log2 (n)⌋ + 1.

Algorithm: Second algorithm to nd xn.

1. Input x and n.
2. d ⟵ number of bits in n. ( n 10 = bdbd−1bd−2 … b1 2 )
3. If b = 1 then
1
3.1
answer ⟵ x

else
3.2
answer ⟵ 1

4. For i = 2 to d do
4.1
x ⟵ x × x

4.2
If b = 1 then
i

4.2.1
answer ⟵ answer × x

5. Output answer.

The way the rst algorithm works for nding xn is obvious, but the way the second
algorithm works for nding xn may not be obvious at all. We will use a simple example to
illustrate how these algorithms differ. Suppose you wanted to nd 264. Using the rst
algorithm would require 63 multiplications since

64
7 = 7 × 7 × 7 × 7 × 7 × 7 × 7 × 7 × ⋯ × 7 .

64 sevens and 63 multiplications

But now notice that

64 32 16 8 4 2
7 = 7 × 7 × 7 × 7 × 7 × 7 × 7.

If we are clever then we can drastically reduce the number of multiplications necessary to
nd 764. First we nd 7 × 7 which takes one multiplication and that gives us 72. Then we do
2 4
7 × 7 which is a second multiplication and gives us 7 . Then we do 7 × 7 which is a third
2 4 4

multiplication and gives us 78. Then 7 × 7 is a fourth multiplication and gives us 716. Then
8 8

7
16
× 7
16
is a fth multiplication and gives us 732. Finally 7 × 7 is a sixth multiplication
32 32

which gives us 764. Thus by being clever we have reduced 63 multiplications to six
multiplications. This is a huge improvement.
Essentially the second algorithm does just this, it attempts to reduce the number of
multiplications necessary to nd xn. A carefully constructed algorithm can reduce the
amount of work necessary. This is where the fourth approximation becomes important.

Example 8.8

Suppose we wanted to nd 75. How many multiplications would be needed in each of the
two algorithms to nd xn?

This is easily seen using the time complexity functions for these algorithms that were
found in the last section. The time complexity function for the rst algorithm is
f (n) = n − 1, so for the
1 rst algorithm we have

f1(5) = 5 − 1 = 4.

The time complexity function for the second algorithm is f2(n) = 2 log2(n) , so for the
second algorithm we have

f2(4) = 2 log2(5) ≈ 4.64.

Based on this we might easily conclude that the rst algorithm is actually a little better
than the second algorithm.

Example 8.9

Suppose we wanted to nd 71024. How many multiplications would be needed in each of


the two algorithms to nd xn?

This is easily seen using the time complexity functions for these algorithms that were
found in the last section. For the rst algorithm we have

f1(1024) = 1024 − 1 = 1023

and for the second algorithm we have

f2(1024) = 2 log2(1024) = 20.


When we assume the input is large, it is very clear that the second algorithm is much
faster than the rst algorithm.

Here are a few more examples that make it clear why it is important to assume that the
input n is large.

Example 8.10

Suppose the time-complexity of algorithm one is f (n) = n and the time-complexity of


1
2

the second algorithm is f (n) = n . Which algorithm is faster?


2
3

It should be clear that for any natural number n > 1 that n2 is a smaller number than n3.
That means that algorithm one performs fewer dominant operations than algorithm two
and so it is faster. You can see this using a table.
n f1(n) = n
2
f2(n) = n
3

1 1 1
5 25 125
10 100 1, 000

100 10, 000 1, 000, 000

Example 8.11

Now suppose the time-complexity of algorithm one is f (n) = 1, 000, 000n = 10 n and
1
2 6 2

the time-complexity of the second algorithm is f (n) = n . Which algorithm is faster?


2
3

Let us use the same values of n that we used in the last example.
n f1(n) = 1, 000, 000n
2
f2(n) = n
3

1 1, 000, 000 1
5 25, 000, 000 125
10 100, 000, 000 1, 000

100 10, 000, 000, 000 1, 000, 000

According to this table, for all the values of n that we have chosen, algorithm two does
fewer dominant operations than algorithm one, thus we might think that algorithm two is
faster. This is where assumption four becomes very important. Let us make another table
for much larger values of n.
n f1(n) = 10 n
6 2
f2(n) = n
3

1 10 6 1
10 6 10 18 10 18
10 15 10 36 10 45
n f1(n) = 10 n
6 2
f2(n) = n
3

10 20 10 46 10 60

Up until n = 10 algorithm two is faster, but when n is larger than 106 algorithm one is
6

faster. So, for large n, algorithm one is much faster.

Approximation Five
In the fth approximation we decide not to distinguish between two time-complexities
that are a constant multiple of each other. A function f2 is said to be a constant multiple of f1
if there is some real number c such that f (n) = cf (n).
2 1

Example 8.12

Determine if two given time-complexity functions are a constant multiple of each other.

1. Are f1(n) = 5n
2
and f2(n) = 50n
2
constant multiples of each other? Since

f2(n) = 10f1(n)

then f1 and f2 are constant multiples of each other. Here the constant is 10.
2. Are f (n) = 500n and f (n) = constant multiples of each other? Since
4
4 n
1 2
100

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

Here the constant is 1

50,000
. So, it does not matter if we write f1 in terms of f2 or f2
in terms of f1.
3. Are f (n) = 200n and f
1
3
2(n) = 600n
4
constant multiples of each other? We have

f2(n) = 3nf1(n).
Here the multiple is 3n. Since n is a variable then 3n is not a constant but changes
when the value of n changes and so f2 is not a constant multiple of f1.

How this is applied is through the following de nition.

De nition 8.2. Suppose we have two time-complexity functions f and g. We say that f is
O (g) if there is some positive number c such that

f (n) ≤ cg(n)

for all n that are very large. O (g) is read “big-oh of g.”

Example 8.13

Suppose f (n) = 5n + 20 and g(n) = n. Show that f is O (g).


In order to show that f is O (g) we need to nd 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.

Thus, for all values of n larger than or equal to 20 we have 5n + 20 ≤ 6n and so f is O (g)
. We could also say that f is O (n).

Example 8.14

Suppose that f (n) = 10n


5
− 7n
4
+ 20n
2
+ 100 and g(n) = n
5
. Show that f is
O (g) = O (n ).
5

Since f and g are time-complexity functions and f : N ⟶ N then the domain of both f
and g is ℕ. This means that n is a positive number. A little thought should convince you
that
5 4 2 5 2
10n − 7n + 20n + 100 ≤ 10n + 20n + 100..

A little more thought should convince you that

5 2 5 5 5
10n + 20n + 100 ≤ 10n + 20n + 100n .

And nally we have

5 5 5 5
10n + 20n + 100n = 130n .

Putting all of this together we have

5 4 2 5
10n − 7n + 20n + 100 ≤ 130n .

Thus f is O (n ). In general, if f is any polynomial of degree k then f is O (n ). If f is a


5 k

sum of different terms, the f is big- O of the fastest growing term in the sum.

The functions that are of most interest in analyzing algorithms in computer science are 1,
2 n
log (n), √n, n, nlog (n), n , and 2 . Fig. 8.1 plots these functions for 0 ≤ n ≤ 100. The
2 2

function f (n) = 1 is not much of a function, no matter what the value of the input n is the
output is always 1. It should be apparent the function 1 does not grow at all as n increases.
The function log (n) grows very slowly, √n grows a little faster, n grows linearly, nlog (n)
2 2

grows faster yet, n2 grows even faster, and 2n grows fastest of the functions listed. By
looking at Fig. 1 we can see that for large n we have the following inequalities,

2 n
1 < log2(n) < √n < n < nlog2(n) < n < 2 .
Figure 8.1 A comparison of the growth rates for some common time-complexity functions.

Hopefully you also remember that

2 3 4 5 6
1 < n < n < n < n < n < n < ⋯.

Also, for any suf ciently large n we have

k n
n < 2

for any given value of k ∈ N. These hierarchies of functions can be used to help us gure out
what big- O category a time-complexity function is in.

Example 8.15

Consider the time-complexity function f (n) = 25√n + 100n + 5000 . Find the function
g such that f is O (g).

All we have to do is look at each term in the polynomial and use the above string of
inequalities. From the string of inequalities we know that for large n we have √n < n
and 1 < n, which gives us
25√n + 100n + 5000 < 25n + 100n + 5000n

< 5125n,

so f is O (n).

Example 8.16

Consider the time-complexity function f (n) = 20log2(n) + 10√n + 50nlog2(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
log (n) < nlog (n) and √n < nlog (n), which gives us
2 2 2

20n log2(n) + 10√n + 50n log2(n) < 20n log2(n) + 10n log2(n) + 50n log2(n)

< 80n log2(n),

so f is ℴ (n log (n))
2
.

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 rst term has an
n log (n), the second term has a √n, and the third term has an n. Using the chain of
2

inequalities we can see that

√n < n < n log2(n)

thus the slowest term involves . This is our big-


n log2(n) O category. Thus
f ∈ O (n log (n)).
2

Example 8.18
Find the big- O category for the time-complexity function
f (n) = 100log2(n) + 2n + 50n
2
+ 5000 .

The rst term of f has an log (n), the second term has an n, the third term has an n2 and
2

the fourth term is simply the constant 5000. Since 5000 = 5000(1) we could say that this
term has a 1. Putting these in order in terms of speed gives us

2
1 < log2(n) < n < n .

The slowest of these is n2 so f ∈ O (n )


2
.

Now we can actually understand the fth approximation better. Consider the following four
time-complexity functions,

5
f1(n) = √n, f2(n) = 10√n, f3(n) = 500√n, f4(n) = 10 √n.

These four time-complexity functions are all a constant multiple of each other. Also, all four
of these time-complexity functions are O (√n) and so all four of these algorithms are in the
same big- O category, the category of algorithms that have time-complexity functions that
are O (√n).
Of course, some algorithms might be a little faster and some might be a little slower, but
relatively speaking their speeds are all fairly close together and, for very large n, all are
much faster than any algorithm in a slower category like O (n) or O (n log (n)). Therefore 2

big- O categories are used to rank algorithm speeds and all algorithms in the same big- O
category are considered to be equally fast. Because all algorithms with time-complexity
functions in the same category are considered to be essentially the same this means we do
not distinguish between two time-complexities that are a constant multiple of each other.
This explains the wording of approximation ve very nicely.

8.5 RANKING ALGORITHMS


Not only do we use the above hierarchies of functions to help us decide what big- O category
a time-complexity function is in, but we also use them to help us rank the different big- O
categories themselves. Rewriting the above hierarchies we have, for suf ciently large n,

2 3 4 k n
1 < log2(n) < √n < n < n log2(n) < n < n < n < ⋯ < n < 2

where k is any integer.


Thus, algorithms that have time complexity functions that are O (1) are constant time
algorithms. In other words, how long the algorithm takes to execute does not depend on the
size of the input n. Algorithms that have time-complexity functions that are O (log (n)) are
2

considered fairly fast; algorithms that have time-complexity functions that are O (√n) are
slower, but still fast; and so on. An algorithm that has a time-complexity function which is
O (2 ) is considered very slow.
n

Example 8.19

Suppose algorithm one has time-complexity function f ∈ O (log (n)) and algorithm two
1 2

has time-complexity function f ∈ O (√n). Which algorithm is faster?


2

Comparing the big- O categories we see that

log2(n) < √ n

so algorithm one is faster than algorithm two.

Example 8.20

Example 8.20 Suppose algorithm one has time-complexity function f ∈ O (n ) and


1
3

algorithm two has time-complexity function f ∈ O (n). Which algorithm is faster?


2

Comparing the big- O categories we see that

3
n < n

so algorithm two is faster than algorithm one. We could also say that algorithm one is
slower than algorithm two.

Example 8.21

Suppose algorithm one has time-complexity function f (n) = 50n log (n) + 200√n and
1 2

algorithm two has time-complexity function f (n) = 30log (n) + 500n. Which
2 2

algorithm is slower?
First we have to nd the big- O categories of each algorithm. It is easy to see that
f ∈ O (n log (n)) and f ∈ O (n). Comparing the categories we have
1 2 2

n < n log2(n).

This means algorithm one is slower.

Example 8.22

Suppose algorithm one has time-complexity function f (n) = 25√n + 10n log (n) and 1 2

algorithm two has time-complexity function f (n) = 100n log (n) + 20n + 50. Which 2 2

algorithm is faster?

First we have to nd the big- O categories of each algorithm. It is easy to see that
f ∈ O (n log (n)) and f ∈ O (n log (n)). Both algorithms are in the same big- O
1 2 2 2

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.2 Let f (n) = 7n and g(n) = n. Show that f is O (g).

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 + 40n 2


+ 500n log2(n) nd g such that f is O (g).

Question 8.5 For f (n) = 800n 2


+ 40n
3
+ 50log2(n) nd g such that f is O (g).

Question 8.6 Suppose that algorithm one has the time complexity function f1 and algorithm
two has time complexity function f2. Given the time complexity functions below, which
algorithm would you expect to be faster, or would you expect the algorithms to be equally
fast.

(a) f1(n) = n
2
and f 2(n) = n
3
,
(b) f 1(n) = n
3
+ n
2
and f 2(n) = 20n
2
,
(c) f1(n) = 500n
3
and f 2(n) = 10n
3
,
(d) f 1(n) = 10n
2
− 20n and f 2(n) = 20n
3
− 50n
2
,
(e) f1(n) = 15n
5
and f 2(n) = 500n
4
+ 500n
3
,
(f) f1(n) = 10000n
2
and f 2(n) = 0.001n
3
,
(g) f 1(n) = n
4
+ 500n
2
and f 2(n) = n
5
+ 500n
2
,
(h) f 1(n) = 2n and f 2(n) = 7000n .

Question 8.7 Suppose that algorithm one has the time complexity function f1 and algorithm
two has time complexity function f2. Given the time complexity functions below, which
algorithm would you expect to be faster, or would you expect the two algorithms to be
equally fast.

(a) f 1(n) = 10log2(n) + 1000 and f 2(n) = 50√n ,


(b) f 1(n) = 10000n log2(n) and f2(n) = 0.00001n ,
(c) f 1(n) = 0.00001√ n and f 2(n) = 100000log2(n) + 50 ,
(d) f 1
(n) = 500n
2
and f (n) = 2
2
n
,
(e) f 1(n) = 500√n + log2(n) and f 2(n) = 500log2(n) + √n ,
(f) f1(n) = 0.1n + 10√n and f 2(n) = 0.1√n + 10log2(n) ,

Question 8.8 Arrange the following algorithms in order of increasing growth rate:

(a) 10n
(b) √n
(c) n1.5

Question 8.9 Arrange the following algorithms in order of increasing growth rate:

(a) 2n
(b) n log
2
2
(n)

(c) 2
n
2

Question 8.10 Arrange the following algorithms in order of increasing growth rate:

(a) 2 n log2(n)

(b) 2n
(c) 2 log2(n)

Question 8.11 What is the dominant operation in the following algorithm? What is the time
complexity function of this algorithm?

1. Input x , x , … , x .
1 2 n

2. i ⟵ 1
3. order ← true
4. While i < n and order = true do
4.1
If x > x then
i i+1

4.1.1
order ← f alse

4.2
i ⟵ i + 1

5. If order = true then


5.1Output “Numbers are in order.”
else
5.2Output “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 a where 1 ≤ i ≤ n and
ij

2
1 ≤ j ≤ n. Thus the matrix consists of n numbers.) What is the dominant operation in the

following algorithm? What is the time complexity function of this algorithm?

1. Input a and b for 1 ≤ i ≤ n and 1 ≤ j ≤ n.


ij ij

2. For i = 1 to n do
2.1
For j = 1 to n do
2.1.1
sum ← 0

2.1.2
For k = i to n do
2.1.2.1
sum ← sum + xik × xkj

2.1.3
cij ← sum

3. Output c . ij

Question 8.13 The following is Warshall's algorithm. What is the dominant operation in the
following algorithm? What is the time complexity function of this algorithm?
1. Input adjacency matrix (a ).
ij

2. For i = 1 to n do
2.1. a = 1
ii

3. For i = 1 to n do
3.1 For j = 1 to n do
3.1.1 If a = 1 then
ij

3.1.1.1 For k = 1 to n do
3.1.1.1.1 a ⟵ a + a (Boolean Addition)
jk jk ik

4. Output (a ).
ij
CHAPTER 9
Graph Theory

Graph theory is used to model many kinds of real world situations, relations, and processes that include
communication networks, data organization, and computational devices. Many of the tasks that programmers,
computer engineers, and computer scientists need to solve are made much easier by using graph theory.
Developing algorithms to handled graphs is very important in computer science. This makes it necessary for you
to understand the basic ideas of graph theory.

9.1 BASIC DEFINITIONS


The word graph in the phrase graph theory is very different from what the word graph means when we looked at
functions. Now the word graph refers to something else entirely.

De nition 1. A graph is a set of points called vertex (or vertex) and a set of lines called edges such that each edge
is attached to a vertex at each end.

Very often the vertices are labeled to tell them apart. Two vertices that are connected by an edge are called
adjacent vertices. An edge is said to be incident to the vertex to which it is attached. The various edges that are
connected to a single vertex are said to be incident to each other. The number of vertices of a graph is called the
order of the graph. A graph must have one vertex but it is allowable for a graph to have no edges. A graph that
has no edges is sometimes called a null graph.

Example 9.1

A graph with six vertices and seven edges. This graph has order six. The vertices are labeled with capital
letters.

A graph that is entirely connected is called a connected graph. However, it is possible for a graph to be
disconnected and have several pieces. These pieces are called components of the graph.

Example 9.2

A disconnected order nine graph with three components. One component consists of only a single vertex.
Graphs are made up of a set of vertices, called the vertex set, and a set of edges, called an edge set. Vertices
and edges can be labeled many different ways, but for now we will label vertices with capital letters. In order to
write down the edge set we have to have a way of labeling edges. In this book the edges will usually be labeled
by the two vertices connected by the edge. For example, an edge that connects vertices A and B can be labeled by
AB or BA. Usually we will stick with alphabetical ordering and write this edge as AB.

Example 9.3

Find the vertex set and the edge set of the following graph.

The vertex set is given by

V = {A, B, C, D, E, F }.

Notice that for this graph vertex A is adjacent to vertex B since they are connected by an edge. Similarly
vertex B is adjacent to vertices A, C, and F, and so on. The edge set is given by

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 ordering1 when writing down the edge set, as we did in the last example. If there is
more than one edge that connects two different vertices these edges are called parallel edges. An edge that
connects a vertex to itself is called a loop. A graph is called a simple graph if it does not have any parallel edges
or loops.

Example 9.4

Does this graph have any parallel edges? Does it have any loops? Find the vertex and edge sets of the
following graph.
This graph has two sets of parallel edges. The rst set of parallel edges are the three edges that connect the
vertices B and F and the second set of parallel edges are the two edges that connect vertices C and D. There is
also one loop, the edge that connects vertex E to itself. Notice that this edge indeed looks like a loop. The
vertex set of the graph is given by

V = {A, B, C, D, E, F }.

The edge set of the graph is given by

E = {AB, BC, BF , BF , BF , CD, CD, CE, DE, EE, EF }.

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

The complete graphs up to order ve.

Sometimes, depending on what the graph represents, the graph's edges need to have a direction assigned to
them. A graph whose edges have a direction assigned to them is called a directed graph. Very similar to the idea
of directed graphs is the idea of weighted graphs. A weighted graph is a graph where every edge has a number
called a weight assigned to it. Usually the weights are positive numbers. These “weights” can represent things
like distances or costs. We will have more to say about weighed graphs in chapter 10.

Example 9.6

An example of a directed graph.


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

An example of a weighted graph.

In this example the weight of edge AB is 10, the weight of AC is 7, and so on.

Of course, it should be obvious to you that there are literally an in nite number of different ways of drawing a
graph. We can put the vertex anywhere we want. We can draw the edges any way we want. All that matters when
drawing a graph is which vertices are adjacent to each other, that is, which vertices are connected to each other
by an edge. The locations of the vertices and the lengths of the edges do not matter. Consider the following
example. A little thought should convince you that even though these graphs are drawn differently that they all
are really the same. Graphs that are drawn differently but still represent the same graph are called isomorphic
graphs. If two graphs are isomorphic, then we consider them to be exactly the same.

Example 9.8

Four isomorphic graphs.

Example 9.9

Two isomorphic graphs.


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 de ned to be the number of edges that are connected to it. Loops are counted twice in
the degree of a vertex since one side of the edge is “out of” the vertex and the other side is “into” the vertex. A
vertex that is part of a graph but that has no edges connected to it is called an isolated vertex. With the degree of
the vertices de ned this way we have the following theorem.

Theorem 9.1 Given a graph G , the sum of the degrees of the vertices of G is equal to twice the number of edges
of G . Using set notation we can write this as

∑ deg(v) = 2|E |

v∈V

where V is the vertex set and E is the edge set.

Example 9.10

Find the degrees of the vertices of the following graph.

The degree of vertex A is one. We write deg(A) = 1. Similarly we have deg(B) = 5, deg(C) = 4,
deg(D) = 3, and deg(F ) = 4. But what about vertex E? There are four edges connected to vertex E but the

edge EE is connected to vertex E twice. In this case we count the edge EE twice to give us deg(E) = 5. And
what about vertex G? There are no edges connected to G so we have deg(G) = 0. It is worth mentioning that
even though in this picture vertex G is placed “inside” the graph it does not have to be. We could have placed
vertex G anywhere.

Example 9.11

Suppose a graph has four vertices and eight edges. Two vertices have degree two and one vertex has degree
three. What is the degree of the fourth vertex?
First we will label the vertices. Vertex v1 and v2 each have degree 2 and vertex v3 has degree 3. We want to
nd what degree vertex v4 has. Also, since there are eight edges we have |E | = 8. Using the above theorem we
have

∑ deg(v) = 2|E |
v∈V

⇒ deg(v 1 ) + deg(v 2 ) + deg(v 3 ) + deg(v 4 ) = 2|E |

⇒ 2 + 2 + 3 + deg(v 4 ) = 2(8)

⇒ 7 + deg(v 4 ) = 16

⇒ deg(v 4 ) = 9.

Thus vertex four must have degree nine.

A path is a sequence of vertices such that each vertex is adjacent to the next vertex. Thus a path consists of a
sequence of edges. By going along the path you travel across this sequence of edges. The number of edges
traveled across is called the path length. For us a path may include repeated vertices and edges. Some books may
de ne this a little differently and say a path cannot include repeated edges. If we want to talk about a path that
has no repeated edges we will say that explicitly. A path that starts and ends at the same vertex is called a graph
theory. There are different ways to write a path. Below are some of the acceptable ways to write down a path.

ABF BCD

A − B − F − B − C − D

A → B → F → B → C → D

A, B, F , B, C, D

Example 9.12

Some examples of paths.

The sequence ABF EC is a path of length four. You start at vertex A and then move along the edge that
connects vertex A to vertex B. Once at vertex B you move along one of the edges connecting vertex B to
vertex F and arrive at vertex F. Which edge you take is not speci ed so it does not matter. Then you move
from vertex F to vertex E along the edge that connects them, and then nally you move to vertex C along the
edge that connects those vertices. Some other examples of paths are F BCDEF BA or EEDCDEC . Some
examples of circuits are BF EDCB or F BF ECBF .

Example 9.13

Paths on a directed graph.


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.

An edge that keeps a graph connected is called a bridge.

Example 9.14

An example of a bridge.

Consider the graph on the left. It is a connected graph. However, if we were to remove edge DE then we
would have a disconnected graph with two components, as shown on the right. Thus the edge DE is called a
bridge. The graph G with the edge DE deleted is written as G − DE.

Example 9.15

For the following graph write out the vertex and edge sets, nd the degree of each vertex, list the vertices
adjacent to vertex B, list the edges incident to vertex D, list the loops (if any), list the parallel edges (if any),
and list the bridges (if any).

The vertex and edge sets are given by

V = {A, B, C, D, E, F },

E = {AB, AE, BC, BD, BE, CD, CE, DF , DF , EE}.

The degrees of the vertices are

deg(A) = 2, deg(B) = 4, deg(C) = 3, deg(D) = 4, deg(E) = 5, deg(F ) = 2.


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.

9.2 EULERIAN AND SEMI-EULERIAN GRAPHS


If G is a connected graph then an Euler path on graph G is a path that includes every edge of G exactly once.
Vertices may be repeated on an Euler path. An Euler circuit is an Euler path that starts and ends at the same
vertex. A connected graph that has an Euler circuit is called Eulerian graph. A connected graph that has an Euler
path but no Euler 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.

Figure 9.1 A semi-Eulerian graph. The path

BBADCDEBC

is an example of an Euler path. Notice, two vertices have odd degree.

Figure 9.2 An Eulerian graph. The path

CDCBBADEBC

is an example of an Euler circuit. Notice, all vertices have even degree.

Now notice that the graph that has an Euler path has exactly two vertices with an odd degree and the rest of the
vertices have an even degree. In the graph that has an Euler circuit all vertices have an even degree. There is a
theorem about this.
Theorem 9.2. Let G be a connected graph, then

1. if every vertex in G has an even degree then G has an Euler circuit,


2. if G has exactly two vertices of odd degree (with the rest being of even degree), then G has an Euler path,
and
3. if G has more than two vertices of odd degree, then G does not have either an Euler circuit or an Euler
path.

This theorem is not hard to prove, but we will not prove it here. Instead we will give an algorithm for nding
the Euler circuit or path called Fleury's algorithm. The algorithm that we will give works, but it is not what we
would use to write a computer program, so we will not write it in pseudocode. The pseudocode version is given
later.
Algorithm: Fleury's algorithm for nding an Euler path or Euler circuit.

1. Make sure the graph is connected and has either 0 or 2 odd vertices. If there are 0 odd vertices you
can start anywhere. If there are 2 odd vertices then you must start at one of the odd vertices. Set
current_vertex equal to the starting vertex and set current_path equal to the empty set.
2. Choose any edge incident to the current vertex, but only choose a bridge only if there is no
alternative. Add this edge to current_path.
3. Set current_vertex equal to the vertex at the other end of the edge. (If the edge is a loop the
current_vertex does not change.)
4. Delete the edge and then delete any isolated vertices from the graph.
5. Repeat steps 2 to 4 until all edges have been deleted from the graph. The nal current_path is either
the Euler circuit or Euler path.

Let use Fleury's algorithm on the graph G given in Fig. 9.3. This graph G has the vertex set V and edge set E ,

V = {A, B, C, D, E, F },

E = {AB, AC, BC, BD, BE, CE, CF , DE, EF }.

Figure 9.3 A graph

with all even degree vertices.

Notice, every vertex here is even so we choose to simply start at vertex A, the rst vertex in our vertex set V .

(a) Starting at vertex A we see both edges AB or edge AC are incident to A. We will simply chose whichever
edge is listed rst in the edge set E . We follow AB to vertex B which becomes our new current_vertex
and then delete edge AB from E and add it to current_path. See Fig. 9.4(a). We are left with

V = {A, B, C, D, E, F },

E = {AC, BD, BE, CE, CF , DE, EF },

current _ vertex = C,

current _ path = {AB, BC}.


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

= {A, B, C, D, E, F },

= {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 rst of the possible edges listed in E which is edge CE. We follow
this edge to vertex E which becomes the new current_vertex, and delete CE from E and add it to
current_path. See Fig. 9.4(c). We are left with

V = {A, B, C, D, E, F },

E = {AC, BD, BE, CF , DE, EF },

current _ vertex = E,

current _ path = {AB, BC, CE}.

(d) Next we could follow BE, DE, or EF since all of these edges are incident to E. However, by looking at
the graph in Fig. 9.4(c) we can see that edge EF is a bridge so we cannot use that edge since there are
other choices. Again we choose the rst of the possible edges listed in E which is edge BE. We follow
this edge to vertex B which becomes the new current_vertex, and delete BE from E and add it to
current_path. See Fig. 9.4(d). We are left with

V = {A, B, C, D, E, F },

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

= {A, C, D, E, F },

= {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 we delete F from V . See Fig. 9.4(h). We are left with

{ }
V = {A, C},

E = {AC},

current _ vertex = C,

current _ path = {AB, BC, CE, BE, BD, DE, EF , CF }.

(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}

which we use to construct our path. The path we have is given by

A − B − C − E − B − D − E − F − C − A

which is clearly an Euler circuit.


Why did we say that this algorithm is not what we would use to write a computer program? Yes, we do have to
make choices in the algorithm; which vertex to start at and which path to choose. But in both of those cases we
simply chose the rst appropriate choice in the set V or E . The real problem is that in step two we had to
determine if an edge was a bridge or not. As humans we can look at the graph and see quickly if an edge is or is
not a bridge. But a computer cannot “see” the graph the same way humans can and so cannot easily determine if
an edge is a bridge or not. Fleury's algorithm was written in such a way that certain choices in it need human
level intelligence. However, we can change Fleury's algorithm a little bit so that a computer can implement it.
The below algorithm is a modi cation of Fleury's algorithm that is more appropriate for a computer. We will
now trace this algorithm for the same graph we used when explaining Fleury's algorithm. This will allow you to
compare the two algorithms. Hopefully you will start to see why this algorithm is better for implementing in a
computer. It is modi ed in such a way that there is no need to decide if a given edge is a bridge or not.
In order to make it easier to trace this algorithm we will relabel the edges in the graph G from Fig. 9.3 in line
with the notation used in the algorithm,

AB = e 1 , AC = e 2 , BC = e 3 , BD = e 4 , BE = e 5 , CE = e 6 , CF = e 7 , DE = e 8 , EF = e 9 .

However, we will maintain the same vertex labels as before. See Fig. 9.5. Also, instead of doing a complete
trace we will consider the four steps in 3.3 as a singe step. We will also leave off the nal output column so the
table is not too wide to t on the page.
Figure 9.5 The graph from Fig. 9.3 with the edges relabeled.

Algorithm: Modi ed Fleury's Algorithm to nd an Euler circuit for an Eulerian graph.

1. Input Eulerian graph G with vertex set V = {v 1, v2 , … , vm } and edges set E = {e 1 , e 2 , … , e n } .


2. current_path ⟵ v ; unused_edges ⟵ E
1

3. While unused _ edges ≠ ∅do


3.1
insertion_point ⟵

rst vertex in currpent_path with unused edges incident to it


3.2
v ⟵ insertion_point

; new_path ⟵ v
3.3 Repeat
3.3.1 e ⟵ rst element of unused_edges incident to v
3.3.2 v ⟵ vertex adjacent to v via edge e
3.3.3 new_path ⟵ new_path, e, v
3.3.4 unused_edges ⟵ unused_edges − {e}
until no element of unused_edges is incident to v
3.4 current_path ⟵ (current_pathbef oreinsertion_point), new_path,
(current _ pathaf terinsertion _ point)

4. Output current_path.

The trace of the modi ed Fleury's algorithm for the graph in Fig. 9.5 is shown in Table 1. At the end of the trace
we have unused _ edges = ∅ and so the while-do loop ends and we move to step four, which outputs the
current_path,

Ae 1 Be 4 De 8 Ee 6 Ce 7 F e 9 Ee 5 Be 3 Ce 2 A.

Table 9.1 Trace of modi ed Fleury's algorithm for graph in Fig. 9.5.

Step current_path \beginsubarraycinser − tion _ point\endsubarray e v new_path unused_edges

2 A - - - - {e 1 , e 2 , … , e 9 }

3.1 A A - - - {e 1 , e 2 , … , e 9 }

3.2 A A - A A {e 1 , e 2 , … , e 9 }

3.3.1– e1
A A B Ae 1 B {e 2 , e 3 , … , e 9 }
4
Step current_path \beginsubarraycinser − tion _ point\endsubarray e v new_path unused_edges

3.3.1- e3
A A C Ae 1 Be 3 C {e 2 , e 4 , e 5 , e 6 , e 7 , e 8 ,
4

3.3.1- e2
A A A Ae 1 Be 3 Ce 2 A {e 4 , e 5 , e 6 , e 7 , e 8 , e 9
4

3.4 Ae 1 Be 3 Ce 2 A A e2 A Ae 1 Be 3 Ce 2 A {e 4 , e 5 , e 6 , e 7 , e 8 , e 9

3.1 Ae 1 Be 3 Ce 2 A B e2 A Ae 1 Be 3 Ce 2 A {e 4 , e 5 , e 6 , e 7 , e 8 , e 9

3.2 Ae 1 Be 3 Ce 2 A B e2 B B {e 4 , e 5 , e 6 , e 7 , e 8 , e 9

3.3.1- e4
Ae 1 Be 3 Ce 2 A B D Be 4 D {e 5 , e 6 , e 7 , e 8 , e 9 }
4

3.3.1- e8
Ae 1 Be 3 Ce 2 A B E Be 4 De 8 E {e 5 , e 6 , e 7 , e 9 }
4

3.3.1- e5
Ae 1 Be 3 Ce 2 A B B Be 4 De 8 Ee 5 B {e 6 , e 7 , e 9 }
4

3.4 Ae 1 Be 4 De 8 Ee 5 Be 3 Ce 2 A B e5 B Be 4 De 8 Ee 5 B {e 6 , e 7 , e 9 }

3.1 Ae 1 Be 4 De 8 Ee 5 Be 3 Ce 2 A E e5 B Be 4 De 8 Ee 5 B {e 6 , e 7 , e 9 }

3.2 Ae 1 Be 4 De 8 Ee 5 Be 3 Ce 2 A E e5 E E {e 6 , e 7 , e 9 }

3.3.1- e6
Ae 1 Be 4 De 8 Ee 5 Be 3 Ce 2 A E C Ee 6 C {e 7 , e 9 }
4

3.3.1- e7
Ae 1 Be 4 De 8 Ee 5 Be 3 Ce 2 A E F Ee 6 Ce 7 F {e 9 }
4

3.3.1- e9
Ae 1 Be 4 De 8 Ee 5 Be 3 Ce 2 A E E Ee 6 Ce 7 F e 9 E {}
4

Ae 1 Be 4 De 8 Ee 6 Ce 7 F e 9
3.4 E e9 E Ee 6 Ce 7 F e 9 E {}
Ee 5 Be 3 Ce 2 A

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 nding new paths using new_path and inserting them into current_path.
This algorithm needs to be changed just a little bit if you have a semi-Eulerian graph and want to nd an Euler
path instead. Recall, a semi-Eulerian graph has two vertices with odd degree with the rest of the vertices having
even degree.

Algorithm: Finds the Euler path for a semi-Eulerian graph.

1. Let v1 and v2 be the two vertices with odd degree. Insert a new edge, called e1, between v1 and v2. Our
new graph G is now an Eulerian graph.
2. Let the vertex set of G be V = {v , v , … , v } and let the edge set be E = {e , e , … , e }.
1 2 m 1 2 n

3. Apply the algorithm for nding the Euler circuit of an Eulerian graph to G .
9.3

for the 2 × 3 matrix

we would have
[
1

joining the two vertices of odd degree.

MATRIX REPRESENTATIONS OF GRAPHS

2
4

−9
−3

5
],

⎢⎥
4. Remove v , e from the beginning of the Euler circuit you obtain. What remains is an Euler path
1

The way we have drawn graphs up till now is a wonderful way for us to visualize and understand graphs. But of
course a computer cannot visualize or understand graphs the same way humans do. We need to be able to
represent a graph in a way that a computer can handle. One way of doing this is simply to use the vertex set and
edge set to represent the graph. This way of representing a graph is good enough for the modi ed Fleury's
algorithm. Another common way to represent graphs is to use matrices.
First we explain what a matrix is. A matrix is a rectangular array of numbers, generally written enclosed in
square brackets. For example,

a 11 = 7,

a 21 = 2,
7

2
2

11

4
−6

8

number. Here a 3 × 3 matrix is written using variables with subscripts,


a 11

a 21

a 31
a 12

a 22

a 32

−9
−3

a 12 = 4,

a 22 = −9,
,

a 13

a 23

a 33


.
and


2

−1

−2
−4

−4

are all examples of matrices. The size of the matrix is indicated with the number of rows and columns, usually

It is important to remember that the row subscript comes rst and the column subscript comes second, arc. Thus,

].

set is given by V = {v , v , … , v }. The elements of the adjacency matrix are de ned by


1 2 n
a 13 = −3,

a 23 = 5.

Since computers can handle arrays of numbers easily, a graph is represented in computers by using a special
matrix. The most common kind of matrix is called an adjacency matrix. Suppose we have a graph whose vertex
6

−3

written as (rows)×(columns). For example, the sizes of the above matrices are 2 × 3 (read “two by three”), 4 × 2
(read “four by two”), and 4 × 4 (read “four by four”). A matrix that has the same number of rows and columns is
called a square matrix. The entries in the matrix are sometimes called matrix elements and are denoted with
variables that have two subscripts, the rst subscript for the row number and the second subscript for the column
7

10

3


a ij

= thenumberof edgesf romvertexv i tovertexv j .

For the moment we will label vertices with vi instead of the usual capital letters to make it easier for you to see
how the adjacency matrix is made. Consider the graph in Fig. 9.6. Since there are no loops going from v1 to v1
we have a = 0. Since there are two edges from v1 to v2 we have a = 2. Since there is one edge from v1 to v3
11 12

we have a = 1. Since there is one edge from v1 to v4 we have a = 1. Thus we have just found the rst row in
13 14

the below table. The other rows are similar.

Figure 9.6 An undirected graph used to construct an adjacency matrix.

v1 v2 v3 v4
v1 0 2 1 1
v2 2 0 0 0
v3 1 0 1 1
v4 1 0 1 0

This table would give us the adjacency matrix

⎡ 0 2 1 1

2 0 0 0
.
1 0 1 1

⎣ 1 0 1 0

But notice something, for an undirected graph the number of edges from vertex vi to vertex vj is exactly the same
as the number of edges from vertex vj to vertex vi. This is because they are exactly the same edges. Therefore, in
the adjacency matrix we have a = a . Matrices that satisfy this property are called symmetric matrices. They
ij ji

are said to be symmetric “across the diagonal.” The diagonal of the matrix is represented by the matrix elements
a11, a22, a33, and so on. Notice that the lower left triangular region is a re ection of the upper right triangular
region. It is easy to see this matrix is symmetrical,

⎡ 0 2 1 1

2 0 0 0
.
1 0 1 1

⎣ 1 0 1 0

Because the matrix is symmetrical sometimes you will see the adjacency matrix written down with only the
diagonal and one of the triangular regions lled out. The upper triangular adjacency matrix and lower triangular
adjacency matrix are given by
Here is one possible answer.

Example 9.17

v1
v2
v3
v1
0
⎢⎥


0

v2
2
0
2


1

Find the adjacency matrix for the following graph.



1


0

1
1

v3
0
1
2
and

sometimes see an adjacency matrix written like either of these matrices.

Example 9.16

Draw the graph for the following adjacency matrix

⎡ ccccc0

1
1

0
0

1
1

1

0


0

.
0

0
1

1 0

respectively. Since both of these matrices give all the information needed you should not be surprised if you

We begin by lling out a table for this graph. Since this is an undirected graph we can simply ll out the
diagonal and one of the triangular regions.
v4
1
0
0
v5
0
1
2
v6
1
0
0
v1 v2 v3 v4 v5 v6
v4 0 0 2
v5 0 1
v6 1

It is then easy to write down the adjacency matrix using this table. In fact, this table already gives the upper
triangular adjacency matrix.

Finally, we will take a quick look at the adjacency matrix for a directed graph. We will use the same graph as
before in Fig. 9.6, but we will now add directions to each edge, see Fig. 9.7. This will allow you to compare
between the adjacency matrices for undirected and directed graphs. We follow basically the same procedure as
before, except that now we have to specify which vertices the edges are “from” and which vertices the edges go
“to.” In the below table we will say the edges go from the vertices along the left and to the vertices along the top.
So, the terms in the adjacency matrix of a directed graph are given by

a ij = the number of edges f rom vertex v i to vertex v j .

Figure 9.7 A directed graph used to construct an adjacency matrix.

Since the rst index in the subscript of a represents the row then the vi are the “from” vertices and since the
ij

second index in the subscript of a represents the column then the vj are the “to” vertices. The adjacency matrix
ij

for a directed graph will not usually be symmetric.


For example, we have two edges going from v1 to v2 making a = 2. We have one edge going from v1 to v3
12

so a = 1. We have one edge going from v1 to v4 making a = 1. The loop goes from v3 and then back to v3
13 14

giving us a = 1. And nally, we have one edge going from v3 to v4 giving us a = 1. The rest of the terms are
33 34

zero. This gives us the following table


to
v1 v2 v3 v4
from v1 0 2 1 1
v2 0 0 0 0
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

Notice that this adjacency matrix is no longer symmetric.


Also, notice that there is no reason the “from” vertices need to be along the left and the “to” vertices need to
be along the top. These can be switched easily. Sometime the adjacency matrix is de ned differently than we
have de ned it here. Whenever you read about or see an adjacency matrix used you should look carefully at how
it is de ned.
Finally, we will make one point. While there was nothing wrong with what we did if our graph modeled some
sort of real situation most of the time we only care that we can go from vertex v1 to vertex v2, we do not actually
care that there are two (or more) different paths to from v1 to v2. Thus directed graphs usually do not have
parallel edges that go in the same direction. An adjacency matrix for a directed graph where parallel edges are
not allowed would only have zeros and ones in it.

9.4 REACHABILITY FOR DIRECTED GRAPHS


Given a directed graph it is often important to know if you can reach some vertex when you are starting at some
other vertex. In this case we do not care if there are multiple paths from one vertex to another so we will say that
parallel edges in the same direction are not allowed. Recall that in the last section we de ned the terms in the
adjacency matrix by

a ij = the number of edges f rom vertex v i to vertex v j .

If a directed graphs does not have parallel edges this becomes

a ij = 1 if there is an edge f rom v i to v j ,

a ij = 0 if there is no edge f rom v i to v j .

Take a look at Fig. 9.8. This is a directed graph with no parallel edges going in the same direction. Suppose you
are sitting at vertex v1 and you want to know if you can get to v4. By looking at the directed graph we can see
that there is a path from v1 to v4, namely the path

v1 → v2 → v3 → v4

Figure 9.8 A directed graph.

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

a ij = 1 if there is a path f rom v i to v j ,

a ij = 0 if there is no path f rom v i to v j .


⎢⎥
Notice how this looks almost the same as the de nition for a in the adjacency matrix. The only difference is


0

0
1

1
1

1
1

1

.
ij

that instead of saying edge it says path. We can think of the adjacency matrix as a “one step” reachability matrix.
In other words, an adjacency matrix tells us what is reachable by a path of length one. A reachability matrix tells
us what is reachable by a path of any length, including length zero.

Example 9.18

Using the de nition, nd the reachability matrix for the directed graph in Fig. 9.8.

Just by looking we can see that if we are on vertex v1 we can reach v1 (just by staying where we are), v2, v3,
and v4. This gives a = 1, a = 1, a = 1, and a = 1. If we are on vertex v2 we can reach v2 (again by
11 12 13 14

staying where we are), v3, and v4 but can not reach v1. This gives a = 0, a = 1, a = 1, and a = 1.
21 22 23

Similarly, if we are on vertex v3 we can reach v3, v4 and v2 but not v1. This gives a = 0, a = 1, a = 1,

a42 42 = 1, and a
31 32

and a = 1. And nally, if we are on vertex v4 we can reach v4, v2, and v3 but not v1. This gives a = 0,
34

= 1, a = 1. Putting this together gives us the following reachability matrix,


44

⎡ 1 1

24

33

41

If we have the adjacency matrix for a directed graph there is an algorithm, called Warshall's algorithm, that
produces the reachability matrix. In order to use Warshall's algorithm we rst have to recall Boolean addition.
Boolean addition is de ned on the set {0, 1} by

0 + 0 = 0,

0 + 1 = 1,

1 + 0 = 1,

1 + 1 = 1.

Similar to the case of Fleury's algorithm, we will give a version of Warshall's algorithm that is easier to
understand before giving a more formal pseudocode version.

Algorithm: Warshall's algorithm to determine if there exists a directed path from vi to vj.
Given the adjacency matrix (a ) of a directed graph:
ij

1. Change all entries along the diagonal to 1.


2. Using the matrix obtained in step one, for every row where in column one there is a 1, add row one of
this matrix to the row that has the value 1 using Boolean addition.
3. Using the matrix obtained in step two, for every row where in column two there is a 1, add row two of
this matrix to the row that has the value 1 using Boolean addition.
4. Continue for all columns.

Using Warshall's algorithm we will nd the reachability matrix for the directed graph in Fig. 9.8. We begin by
writing down the adjacency matrix for this graph,
⎢⎥


0


0

ii

0
1

1
0

0
0

0

1
.

(a) In step 1 we change all the entries along the diagonal to one, which gives us the matrix

⎡ 1 ⎤


.

We do this because every vertex is reachable from itself. In other words, for each i we can reach vi from vi by
simply staying where we are so we make a = 1.
(b) In step 2 we look at column one of the matrix we obtained above. Column one is written in gray,

⎡ 1 1 0 ⎤
.

For every row where in column one there is a 1, we add row one of this matrix to the row that has the value 1
using Boolean addition. Row one of this matrix is given by [1, 1, 0, 0]. Notice we put commas in between the
elements of the row just to make it clear.
Since row one is the only row that has a 1 in it in column one then we add row one to itself using Boolean
addition. This means we add each term in the row to itself. Because we are using Boolean addition this does
not change the row at all,

[1, 1, 0, 0] + [1, 1, 0, 0] =

=
[1 + 1, 1 + 1, 0 + 0, 0 + 0]

[1, 1, 0, 0].

(c) In step 3 we look at column two of the matrix we obtained above. Column two is written in gray,


.

For every row where in column two there is a 1, we add row two of this matrix to the row that has the value 1
using Boolean addition. Row two of this matrix is given by [0, 1, 1, 0] and there are three rows in this column
that have a value 1, row one, row two, and row four.
First we will add row two and row one,
giving us matrix

⎢⎥
[0, 1, 1, 0] + [1, 1, 0, 0]


1

0
1

row three + row two

row three + row three

row three + row f our


1

1
=

1


[0 + 1, 1 + 1, 1 + 0, 0 + 0]

[1, 1, 1, 0]

which becomes our new row one. The Boolean addition of row two with row two gives us

[0, 1, 1, 0] + [0, 1, 1, 0] = [0 + 0, 1 + 1, 1 + 0, 0 + 0]

[0, 1, 1, 0]

which is exactly row two again. Last we will add row two and row four,

[0, 1, 1, 0] + [0, 1, 0, 1] [0 + 0, 1 + 1, 1 + 0, 0 + 1]

[0, 1, 1, 1]

which becomes our new row four. Putting this altogether we have obtained the matrix

(d) Next we look at column three of the matrix we obtained above. Column three is written in gray,

1


.

For every row where in column three there is a 1, we add row three of this matrix to the row that has the value
1 using Boolean addition. Row three of this matrix is given by [0, 0, 1, 1]. Since every row in this column has a
value 1, we add row three to every row using Boolean addition. We will not give the details, only the results

row three + row one =

=
[1, 1, 1, 1],

[0, 1, 1, 1],

[0, 0, 1, 1],

[0, 1, 1, 1]
giving us matrix

we obtained above,

21
⎢⎥


1

0
1

1
1

row f our + row two

row f our + row three

row f our + row f our

we would expect from looking at Fig. 9.8.


1

0
1

1
1

1
1

1


.

(e) Next we look at column four of the matrix we obtained above. Column four is written in gray,

⎡ ⎤
.

For every row where in column four there is a 1, we add row four of this matrix to the row that has the value 1
using Boolean addition. Row four of this matrix is given by [0, 1, 1, 1]. Since every row in this column has a
value 1, we add row four to every row using Boolean addition. We will not give the details, only the results

row f our + row one

.
=

=
[1, 1, 1, 1],

[0, 1, 1, 1],

[0, 1, 1, 1],

[0, 1, 1, 1]

(f) Since we have now nished all four columns we are done. The Reachability matrix is given by the matrix

Since a = 0 there is no path from v2 to v1, since a = 0 there is no path from v3 to v1, and since a = 0
31 41

there is no path from v4 to v1. However, there are paths in all the other cases. Notice how this is exactly what

This procedure is far easier to understand than looking directly at the pseudocode for Warshall's algorithm,
which is given below. However, when writing a program for a computer the pseudocode below is what you would
base your program on.
Algorithm: Warshall's algorithm to determine if there exists a directed path from vi to vj. (Pseudocode
version.)

1. Input adjacency matrix (a ). ij

2. For i = 1 to n do
2.1. a = 1
ii

3. For i = 1 to n do
3.1 For j = 1 to n do
3.1.1 If a = 1then
ij

3.1.1.1 For k = 1 to n do
3.1.1.1.1 a ← a + a (Boolean Addition)
jk jk ik

4. Output (a ).ij

9.5 PROBLEMS

Question 9.1 For the graphs in Fig. 9.9 do the following:

(a) write out the vertex and edge set,


(b) nd the order of the graph,
(c) nd the degree for each vertex,
(d) list the vertices that are adjacent to vertex B,
(e) list the edges incident to vertex C,
(f) list the loops (if any),
(g) list the parallel edges (if any), and
(h) list the bridges (if any).

Figure 9.9 The graphs for questions 9.1, 9.11, and 9.18.

Question 9.2 Assume you have a graph G with the following vertex and edge set,

V = {A, B, C, D, E}

E = {AA, AD, AE, BC, BD, CD, CE, DD}.

Which of the following pairs of vertices are adjacent?

(a) A and B
(b) A and C
(c) A and D
(d) B and C
(e) B and D
(f) B and E
(g) E and A
(h) E and C
(i) E and D

Question 9.3 Assume you have a graph G with the following vertex and edge set,

V = {A, B, C, D, E, F }

E = {AB, AC, BC, BC, DE, DF , DF , DD}.

Is G connected? If not, how many components does G have?

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 ve vertices. Two vertices have degree 3, two vertices have degree
5, and one vertex has degree 2. How many edges does the graph have?

Question 9.8 Suppose you have a graph with eight vertices, four vertices have degree 4, two vertices have degree
3, one vertex has degree 6, and one vertex has degree 8. How many edges does the graph have?

Question 9.9 Suppose a graph has four vertices and ve edges. One vertex has degree 1, one vertex has degree 2,
and one vertex has degree 3. What is the degree of the remaining vertex?
Question 9.10 Suppose a graph has six vertices and twelve edges. There are two vertices of degree 4, one vertex
with degree 2, one vertex with degree 3, and one vertex with degree 6. What is the degree of the remaining
vertex?

Question 9.11 Determine if the graphs in Fig. 9.9 are Eulerian, semi-Eulerian, or neither.

Question 9.12 Determine if the graphs in Fig. 9.10 are Eulerian, semi-Eulerian, or neither.

Figure 9.10 The graphs for questions 9.12 and 9.19.

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 }.

Is G Eulerian, semi-Eulerian, or neither?

Question 9.14 Use Fleury's algorithm to nd an Euler path on the below graph.

Question 9.15 Assume you have a graph G with the following vertex and edge set,

V = {A, B, C, D, E, F }

E = {AA, AD, AE, BC, BD, BD, CD, EF , F F }.

Use Fleury's algorithm to nd an Euler path on G .

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 modi ed Fleury's algorithm for nding the Euler circuit on an
Eulerian graph starting at vertex A. It helps to relabel the edges as AB = e , AC = e , BC = e , and so on.
1 2 3
Question 9.17 Assume you have a graph G with the following vertex and edge set,

V = {A, B, C, D, E}

E = {AB, AC, BC, BD, BE, CD, CE, DE, DE}.

Show that G is an Eulerian graph. Use the modi ed Fleury's algorithm for nding the Euler circuit on an
Eulerian graph starting at vertex A. It helps to relabel the edges as AB = e , AC = e , BC = e , and so on.
1 2 3

Question 9.18 Find the adjacency matrices for the graphs in Fig. 9.9.

Question 9.19 Find the adjacency matrices for the graphs in Fig. 9.10.

Question 9.20 Assume you have a graph G with the following vertex and edge set,

V = {A, B, C, D, E}

E = {AA, AD, AE, BC, BD, CD, CE, DD, }.

Find the adjacency matrix for G .

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 }.

Find the adjacency matrix for G .

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 }.

Find the adjacency matrix for G .

Question 9.23 Find the time complexity for Warshall's algorithm.

Question 9.24 Use Warshall's algorithm to nd the reachability matrix for the below graph.
Question 9.25 Use Warshall's algorithm to nd the reachability matrix for the below graph.

1 The technical term for this kind of ordering is lexicographical ordering.


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 les. Tree structures are used to store data that has a hierarchical structure.
These data structures are easy to search and sort using standard algorithms. Trees are also used to process the
syntax of computer languages. Recall, we used expression trees in logic and Boolean algebra to understand the
order of operations, write truth tables, and draw circuit diagrams. Also, in many applications it is important to
nd a tree that comes from a directed or weighted graph. A good understanding of tree basics is essential in
computer science.

10.1 BASIC DEFINITIONS


We will now study a particular type of graph, called a tree, that shows up often in computer science and
computer engineering. First, recall that a path is a sequence of vertices where each vertex is adjacent to the next
vertex and a circuit is a path that starts and ends at the same vertex. Though other books may have slightly
different de nitions, for us repeated edges or vertices are allowed in paths and circuits unless we say they are not
allowed.
A cycle is a circuit that includes at least one edge; has no repeated edges; and also has no repeated vertices
except for the rst and last vertices. Thus a cycle is a circuit where repeated edges and vertices, except for the
rst and last vertex, are not allowed. The length of a cycle is the number of edges in the cycle. If a cycle has
length n is called an n-cycle. A 1-cycle must be a loop and a 2-cycle must be a pair of parallel edges.

Example 10.1

Examples of paths, circuits, and cycles.

Both ABCE and ABCDCE are examples of paths and both BCEF B and BCDCB are examples of
circuits. This graph has three cycles: BCEF B, BCDEF B, and CDEC . Of course it is possible to write
these cycles in different ways. For example, the cycle BCEF B could also be written as CEF BC or as
F BCEF or as ECBF E , and so on.

A tree is a connected graph with no cycles. Trees are called trees because, well, they look like trees. Keeping
with the tree analogy, a vertex of a tree that has degree one is often called a leaf. Trees have several properties
that should be obvious just by looking at examples of trees. If T is a tree then the following properties are true;

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 rst of these properties below, but by looking at some examples of trees the other
properties should be clear.

Example 10.2

Four examples of trees.

Take a careful look at the trees shown in the last example. Count the number of vertices and edges in each tree.
You should notice that there is always one more vertex than there are edges. This is something that is always true
for trees. The proof of this theorem is not very dif cult so we will give it. It is also a very nice example of
induction. Induction is an technique often used for proving certain kinds of statements and is widely used in
computer science. That is why we give the proof of this theorem.

Theorem 10.1. Any tree with n vertices has n − 1 edges. (Another way of saying this is that any tree with n edges
has n + 1 vertices.)

Proof: Suppose T is a tree with one vertex. Since a tree cannot have any loops we know that there are no edges
1

in tree T . Thus T has 1 vertex and 0 edges.


1 1

Next suppose we have a tree T with two vertices v1 and v2. Since trees are connected we know that there is an
2

edge e connecting the two vertices. Remove v2, and e. We are left with a tree that has 1 vertex, and by the last
paragraph we know this tree has 0 edges. Now add back v2 and e to get back T , which increases the number of
2

vertices by one and the number of edges by one. We now know that T has 2 vertices and 1 edge.
2

Next suppose we have a tree T with three vertices v1, v2 and v3. Since trees are connected we know that there
3

is an edge e connecting v3 and some other vertex. Remove v3 and e. We are left with a tree that has 2 vertices,
and by the last paragraph we know it must have 2 vertices and 1 edge. Now add back v3 and e to get back T , 3

which increases the number of vertices by one and the number of edges by one. We now know that T has 3 3

vertices and 2 edges.


And of course if we were to continue for a tree T the argument would be exactly the same. As it would be for
4

T , T , and so on. But instead of continuing forever we assume that trees T


5 6 nwith n vertices have n − 1 edges.
We then do a step called an inductive step where we use this to prove that a tree T n+1has n + 1 vertices and n
edges. We now state the inductive step.
Suppose we have a tree T n+1 with vertices v , … , v . Since trees are connected we know that there is an
1 n+1

edge e connecting v with some other vertex. Remove v


n+1 n+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 v n+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. □
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. Recalling 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.

The vertex labeled “root” is the parent vertex of vertices A and H. Vertex A is the parent vertex of both B and
E, and so on. Vertex D is the child of vertex C, and both vertices F and G are children of vertex E, and so on.
Vertices D, F, G, J, K, L, O, and P are all leaves.

A spanning tree of a graph G is a tree with the same vertex set as G and with an edge set that is a subset of the
edge set of G .

Example 10.4

Example of a graph G along with nine of its possible spanning trees.

A graph G (left) along with nine possible spanning trees (right). Notice that each spanning tree has the same
four vertices as G and has three edges from G . And, of course, each spanning tree is a tree.
In some cases it is not dif cult to nd out how many different spanning trees a graph has.

Example 10.5

A simple example of counting the number of different spanning trees a graph has.

Consider graph G on the left. How many different spanning trees does it have? The key is to notice that this
graph has only one cycle, B − D − E − B that consists of three edges. If we remove any single edge from
this one cycle we break the cycle and get a spanning tree for the graph. Thus there are three possible spanning
trees, all of which are shown on the right.

Example 10.6

An example of counting the number of spanning trees a graph has.

How many different spanning trees does the graph on the left have? This graph has two disjoint cycles,
A − B − C − A and D − E − C − D, each with three edges. If we remove exactly one edge from the rst
cycle and one edge from the second cycle we break each cycle and get a spanning three. There are three
possible edges that we can remove from the rst cycle and three possible edges that we can remove from the
second cycle giving us a total of 3 × 3 = 9 possible ways to obtain a spanning tree. These nine different
spanning trees are shown on the right. Notice, this is exactly an application of the multiplication principle
from chapter 7.

Example 10.7

Counting the number of spanning trees a graph with two cycles which are not disjoint has.
In the graphthere are two cycles, A − C − D − B − A and B − E − D − B. The cycle
A − C − D − B − A contains four edges and the cycle B − E − D − B contains three edges. We may
assume that, like before, the number of possible spanning trees is given by 4 × 3 = 12 but this is not quite
right. Remember, before we were allowed to remove one edge from each cycle. However, now the two cycles
share edge BD. The option where we remove edge BD from the rst cycle and edge BD from the second
cycle does not give a tree and so is not allowed. So the number of possible spanning trees is given by
4 × 3 − 1 = 12 − 1 = 11. Try to draw all 11 spanning trees for this graph.

10.2 MINIMAL SPANNING TREES OF WEIGHTED GRAPHS


In many applications where the weights in a weighted graph represent something like costs or distances we are
interested in nding a spanning tree that has the minimal possible total cost or the minimal possible total
distance. In other words, we are interested in nding a spanning tree with the minimal possible total weights on
the spanning tree's edges. A spanning tree that satis es this is called a minimal spanning tree.
Let us consider the graph in Fig. 10.1. We want to nd the minimal spanning tree for this graph. To do this we
will use Prim's algorithm. We begin by listing the vertex set and edges set for G ,

= {A, B, C, D, E, F },

= {AB 3 , AC 5 , AD 9 , BD 7 , BE 12 , CD 11 , CF 2 , DE 7 , DF 8 , EF 10 }

where we have written the weight of each edge as a subscript to help us easily remember the weights.

Figure 10.1 We want to nd a minimal spanning tree for this weighted graph.

Prim's algorithm is an example of a greedy algorithm. In a greedy algorithm the “least expensive” option is
chosen at each step. Notice how in each step of the algorithm the link with the minimal weight is chosen.

Algorithm: Prim's algorithm to nd a minimal cost spanning tree of a weighted graph.

1. Input vertex set V = {v , v , … , v


1 2 n} and edge set E = {e 1 , e 2 , … , e m } .
2. T ← v ; unused_edges ⟵ E
1

3. For i = 1 to n − 1 do
3.1
e ⟵

rst edge with minimal weight which is incident to exactly one vertex in T
3.2
v ⟵
vertex incident to e which is not in T
3.3

T ← T ∪ {e, v}

3.4

unused_edges ⟵ unused_edges − {e}

4. Output T .

Instead of simply doing a trace of Prim's algorithm we will discuss each step to make sure you fully
understand the algorithm. We can start with any vertex so we will simply start with the rst vertex in V which is
A. In step two we assign the rst vertex to T which gives us T = {A} and we assign E to unused_edges so we
have unused _ edges = E . We then begin step three, which is a For -do loop which will loop ve times, one
fewer time than the number of vertices in the graph.

(a) We look for the rst edge with minimal weight which is incident to exactly one vertex in T . Since
T = {A} we simply have to look among the edges incident to vertex A, which are AB , AC , and AD .3 5 9

Clearly AB is the edge with the minimal weight. The vertex incident to AB which is not in T is clearly
3 3

B. Thus for steps 3.1-4 we have the following assignments,

⟵ AB 3 ,

⟵ B,

⟵ {A} ∪ {AB 3 , B}, unused_edges ⟵ E − {AB 3 }.

(b) Next we look for the rst edge with minimum weight which is incident to exactly one vertex in T . These
vertices are A and B so we need to look among AC , AD , BD , and BE . Among these edges AC has
5 9 7 12 5

the minimal weight. So for steps 3.1–4 we are left with the assignments

⟵ AC 5 ,

⟵ C,

⟵ {A, AB 3 , B} ∪ {AC 5 , C}, unused_edges ⟵ {AD 9 , BD 7 , BE 12 , CD 11 , CF 2 , DE 7 , DF 8 , EF 10 }

(c) Next we again look for the rst edge with minimum weight which is incident to exactly one vertex in T .
These vertices are A, B, and C so we now look among AD , BD , BE , CD , and CF . Among these
9 7 12 11 2

edges the one with the minimal weight is CF . So for steps 3.1–4 we have the assignments
2
e

⟵ CF 2 ,

⟵ F,

⟵ {A, AB 3 , B, AC 5 , C} ∪ {CF 2 , F }, unused_edges ⟵ {AD 9 , BD 7 , BE 12 , CD 11 , DE 7 , DF 8 , EF

(d) Next we again look for the rst edge with minimum weight which is incident to exactly one vertex in T .
These vertices are A, B, C, and F so we now look among AD , BD , BE , CD , DF , and EF . The
9 7 12 11 8 10

edge with the minimal wight is BD . So for steps 3.1–4 we have the assignments
7

⟵ BD 7 ,

⟵ D,

⟵ {A, AB 3 , B, AC 5 , C, CF 2 , F } ∪ {BD 7 , D}, unused_edges ⟵ {AD 9 , BE 12 , CD 11 , DE 7 , DF 8 ,

(e) Next we again look for the rst edge with minimum weight which is incident to exactly one vertex in T .
These vertices are A, B, C, F, and D so we now look among BE , DE , and EF . The edge with the
12 7 10

minimal weight is DE . So for steps 3.1–4 we have the assignments


7

⟵ DE 7 ,

⟵ E,

⟵ {A, AB 3 , B, AC 5 , C, CF 2 , F , BD 7 , D} ∪ {DE 7 , E}, unused_edges ⟵ {AD 9 , BE 12 , CD 11 , DF

At this point we have gone through the For -do loop ve times and we now move to step 4 which outputs the
tree

T = {A, AB 3 , B, AC 5 , C, CF 2 , F , BD 7 , D, DE 7 , E}.

See Fig. 10.2.


Figure 10.2 The weighted graph (top) with its minimal spanning tree (bottom) found using Prim's algorithm.

10.3 MINIMAL DISTANCE PATHS


Minimum distance problems are a little different from minimum spanning tree problems. Like the minimum
spanning tree problem, we start with a weighted graph. In this case we usually interpret the weights as being
distances. Given some vertex, say v1, from a weighted graph we are interested in nding the minimum distance
path from v1 to any other vertex in the graph. Again, we will end up with a tree that connects v1 with every other
vertex. Notice, nothing is said about the overall sum of the edge weights that are included in the tree being a
minimum. It is only the sum of the weights on the path from v1 to each vi must be the minimum of all the paths
from v1 to vi. A spanning try that satis es this is called called a minimal distance spanning tree. This can be done
using Dijkstra's algorithm. Note, we may end up with a different tree than the one found using Prim's algorithm.

Algorithm: Dijkstra's algorithm to nd a minimal distance path.

1. Input graph G with vertex set V = {v 1 , v 2 , … , v m } and edges set E = {e 1 , e 2 , … , e n } .


2. T ← {v }, d(v , v ) ⟵ 0
1 1 1

3. For j = 1 to n − 1 do
3.1
w ⟵

rst vertex w ∈ T for which d(v , w) + weight(e) is minimized, where e ranges over edges in G
1 − T

that are incident to w but not incident to another vertex in T .


3.2
e ⟵

the rst edge incident to w for which d(v 1, w) + weight(e) is minimized.


3.3
v ⟵

the vertex in G − T to which e is incident


3.4

T ← T ∪ {e, v}
3.5

d(v 1 , v) ⟵ d(v 1 , w) + weight(e)

4. Output T .

We will use the same graph as before, the one shown in Fig. 10.1. We will again start with vertex A. And as
before we will explain each step fully instead of doing a trace of the algorithm. Since we are starting at the
vertex v = A, in step two we set T = {A} and d(A, A) = 0. We then begin step 3 which is a For -do loop that
1

executes a total of ve times, one fewer than the number of vertices in our graph.

(a) In step 3.1 we are looking for the rst vertex w ∈ T for which d(A, w) + weight(e) is minimized, where
e ranges over edges in G − T that are incident to w but not incident to another vertex in T . This sentence
requires some explanation. First, it says that w ∈ T but since T = {A} we must have w = A. In step 3.2
we must also nd the edge e where d(A, A) + weight(e) is minimized. We were told the edges e we can
choose from are the edges in G − T that are incident to A but not incident to any other vertex in T .
Since there are no other vertices in T we do not have to worry about that part now. What edges are
incident to A? They are

AB 3 , AC 5 , AD 9 .

We now have to calculate the value d(A, A) + weight(e) for each of these three edges to nd out which is the
minimum. (If two values are equal we simply choose the rst of the two edges.) We have

d(A, A) + weight(AB 3 )

= 0 + 3 = 3,

d(A, A) + weight(AC 5 )

= 0 + 5 = 5,

d(A, A) + weight(AD 9 )

= 0 + 9 = 9.

The minimum of these values is 3 so we choose w = A and e = AB . With this we can make the rest of the
3

assignments in steps 3.3–5,

⟵ B,

⟵ {A} ∪ {AB 3 , B} = {A, AB 3 , B},

d(A, B)

⟵ d (A, A) + weight (AB 3 ) = 3.


(b) Now we look for the rst 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 , AD 9 , BD 7 , BE 12 .

We now have to calculate d(A, w) + weight(e) for each of these edges. Notice, when w = A we use the edges
incident to A but when w = B we use the edges incident to B. We have

d(A, A) + weight(AC 5 )

= 0 + 5 = 5,

d(A, A) + weight(AD 9 )

= 0 + 9 = 9,

d(A, B) + weight(BD 7 )

= 3 + 7 = 10,

d(A, B) + weight(BE 12 )

= 3 + 12 = 12.

Recall, we had found d(A, B) = 3 in the last step. The minimum of these values is 5 so we choose w = A and
e = AC . We now make the rest of the assignments in steps 3.3–5, writing them in a way that is more natural
5

for us,

= C,

= {A, AB 3 , B, AC 5 , C},

d(A, C)

= d(A, A) + weight(AC 5 ) = 0 + 5 = 5.

(c) Again we look for the rst vertex w in T where d(A, w) + weight(e) is minimized. There are now three
possibilities for w, namely vertices A, B, and C. We look among the edges in G − T that are incident to
only one of the vertices A, B, or C. We list these edges,

AD 9 , BD 7 , BE 12 , CD 11 , CF 2 .
We now have to calculate d(A, w) + weight(e) for each of these edges. Again, when w = A we use the edges
incident to A, when w = B we use the edges incident to B, and when w = C we use the edges incident to C.
We have

d(A, A) + weight(AD 9 )

= 0 + 9 = 9,

d(A, B) + weight(BD 7 )

= 3 + 7 = 10,

d(A, B) + weight(BE 12 )

= 3 + 12 = 15,

d(A, C) + weight(CD 11 )

= 5 + 11 = 16,

d(A, C) + weight(CF 2 )

= 5 + 2 = 7.

The minimum of these values is 7 so we choose w = C and e = CF 2 . We now make the rest of the
assignments in steps 3.3–5,

= F,

= {A, AB 3 , B, AC 5 , C, CF 2 , F },

d(A, F )

= d(A, C) + weight(CF 2 ) = 5 + 2 = 7.

(d) Again we look for the rst vertex w in T where d(A, w) + weight(e) is minimized. There are now four
possibilities for w, namely vertices A, B, C, and F. We look among the edges in G − T that are incident
to only one of these vertices. We list these edges,

AD 9 , BD 7 , BE 12 , CD 11 , DF 8 , EF 10 .

We now have to calculate d(A, w) + weight(e) for each of these edges. Again, when w = A we use the edges
incident to A, when w = B we use the edges incident to B, and so on. We have

d(A, A) + weight(AD 9 )

= 0 + 9 = 9,
d(A, B) + weight(BD 7 )

= 3 + 7 = 10,

d(A, B) + weight(BE 12 )

= 3 + 12 = 15,

d(A, C) + weight(CD 11 )

= 5 + 11 = 16,

d(A, F ) + weight(DF 8 )

= 7 + 8 = 15,

d(A, F ) + weight(EF 10 )

= 7 + 10 = 17.

The minimum of these values is 9 so we choose w = A and e = AD 9 . We now make the rest of the
assignments in steps 3.3–5,

= D,

= {A, AB 3 , B, AC 5 , C, CF 2 , F , AD 9 , D},

d(A, D)

= d(A, A) + weight(AD 9 ) = 0 + 9 = 9.

(e) Again we look for the rst vertex w in T where d(A, w) + weight(e) is minimized. There are now ve
possibilities for w, namely vertices A, B, C, F, and D. We look among the edges in G − T that are
incident to only one of these vertices. We list these edges,

BE 12 , DE 7 , EF 10 .

We now have to calculate d(A, w) + weight(e) for each of these edges. Again, when w = A we use the edges
incident to A, when w = B we use the edges incident to B, and so on. We have

d(A, B) + weight(BE 12 )

= 3 + 12 = 15,

d(A, D) + weight(DE 7 )

= 9 + 7 = 16,
d(A, F ) + weight(EF 10 )

= 7 + 10 = 17.

The minimum of these values is 15 so we choose w = B and e = BE 12 . We now make the rest of the
assignments in steps 3.3–5,

= E,

= {A, AB 3 , B, AC 5 , C, CF 2 , F , AD 9 , D, BE 12 , E},

d(A, E)

= d(A, B) + weight(BE 12 ) = 3 + 12 = 15.

At this point we have gone through the For -do loop ve times and we now move to step 4 which outputs the
tree

= {A, AB 3 , B, AC 5 , C, CF 2 , F , AD 9 , D, BE 12 , E}.

See Fig. 10.3.

Figure 10.3 The weighted graph (top) with the spanning tree (bottom) found using Dijkstra's algorithm to solve the minimum distance problem
from vertex A. Notice how different this spanning tree is from the one found using Prim's algorithm shown in Fig. 10.2.

10.4 PROBLEMS
Question 10.1 Suppose you have a tree T with 100 edges. How many vertices do you have? How many of the
100 edges are bridges?

Question 10.2 Suppose you have a tree T with 100 vertices. How many edges do you have? How many of the
edges are bridges?

Question 10.3 Suppose you have a tree T with 100 edges. You remove two edges at random. Is the new graph
disconnected? If so, how many components does the new graph have?

Question 10.4 Suppose you have a tree T with 20 vertices labeled {v , v , … , v }. How many paths (with no
1 2 20

repeated vertices or edges) are there between v1 and v20? How many paths are there between v4 and v17? How
many paths are there between vi and vj for any i and j where i ≠ j?

Question 10.5 Suppose you have a tree T with 20 vertices labeled {v , v , … , v }. You add an edge between v1
1 2 20

and v20. Does the new graph have a cycle? If so, how many cycles does it have? Is the new graph still a tree?

Question 10.6 Suppose you have a rooted binary tree T . Suppose vertex vi is not the root or a leaf. How many
parents does vi have? How many children does vi have?

Question 10.7 Suppose you have a rooted strict binary tree T . Suppose vertex vi is not the root or a leaf. How
many parents does vi have? How many children does vi have?

Question 10.8 Given a tree T with 100 vertices, what is the greatest number of leaves that T can have? What is
the smallest number of leaves that T can have?

Question 10.9 Suppose you have a graph with 6 vertices. One vertex has degree ve and the other vertices have
degree one. Draw the graph.

Question 10.10 For the graphs below answer the following questions.

(a) How many spanning trees does the graph contain?


(b) Which edge or edges must be contained in any spanning tree?

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?

Question 10.12 Use Prim's algorithm to nd the minimal tree of the following graph starting at vertex A.
Question 10.13 Use Prim's algorithm to nd the minimal tree of the following graph starting at vertex A. Assume
the edge set is given by E = {AB, AC, BC, CD, CE, CF , DF , EF }.

Question 10.14 Suppose you have ve buildings that you want to connect together using a local area network
(LAN). A table giving the price (in thousands of dollars) to link each pair of building using ber-optic cables is
given below:

A B C D E

A -

B 24 -

C 20 21 -

D 28 25 18 -

E 16 26 23 19 -

In order for any two buildings to communicate with each other over the LAN it is enough for there to exist a path
between the two buildings. We want to nd the least expensive possible LAN con guration. In other words, we
want to nd the minimal spanning tree. Use Prim's algorithm to nd the minimal spanning tree. Assume the
edges in the edge set are written in alphabetical order.

Question 10.15 Use Dijkstra's algorithm to nd the shortest path from vertex A to every other vertex. Assume the
edge set is written in alphabetical order.

Question 10.16 Use Dijkstra's algorithm to nd the shortest path from vertex A to every other vertex.
APPENDI X A
Basic Circuit Design

In this appendix we will try to see how several different topics interrelate
in the context of computer science. This appendix is meant to give you a
glimpse of things to come in computer science and to help illustrate in a
concrete way how some of the topics we have already studied get used in
the real world.
Computers use binary numbers because on a fundamental level
computers can only operate on two states, the presence or absence of an
electrical current. We humans interpret these signals as binary numbers.
The most fundamental thing that a computer can do is the adding of two
binary numbers. In this chapter we will see how computers do this.

A.1 BINARY ADDITION


Before seeing how the addition of binary numbers work we will remind
ourselves of how addition works. We will restrict ourselves to adding only
two numbers.

Example A.1

Add 82, 376, 514 and 47, 032, 867.

1 1 1

82376514

+ 47 032 867

129 409 381


In the rst column (on the far right) add the two digits in that
column, 4 + 7 = 11. Write 1 below the line and carry 1.
In the second column add the carried 1 along with the two digits in
that column, 1 + 1 + 6 = 8. Write 8 below the line.
In the third column add the two digits in that column, 5 + 8 = 13.
Write 3 below the and carry 1.
In the fourth column add the carried 1 along with the two digits in
that column, 1 + 6 + 2 = 9. Write 9 below the line.
In the fth column add the two digits in that column, 7 + 3 = 10.
Write 0 below the line and carry 1.
In the sixth column add the carried 1 along with the two digits in
that column, 1 + 3 + 0 = 4. Write 4 below the line.
In the seventh column add the digits in the column, 2 + 7 = 9.
Write 9 below the line.
In the eighth column add the digits in the column, 8 + 4 = 12.
Here we write 12 below the line.

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 1 1 1

1101 1001

+1110 1111

1 1011 1000

In the rst column (on the far right) add the two digits in that
column, 1 + 1 = 10. Write 0 below the line and carry 1.
In the second column add the carried 1 along with the two digits in
that column, 1 + 0 + 1 = 10. Write 0 below the line and carry 1.
In the third column add the carried 1 along with the two digits in
that column, 1 + 0 + 1 = 10. Write 0 below the line and carry 1.
In the fourth column add the carried 1 along with the two digits in
that column, 1 + 1 + 1 = 11. Write 1 below the line and carry 1.
In the fth column add the carried 1 along with the two digits in
that column, 1 + 0 + 0 = 1. Write 1 below the line.
In the sixth column add the two digits in that column, 0 + 1 = 1.
Write 1 below the line.
In the seventh column add the two digits in that column,
1 + 1 = 10. Write 0 below the line and carry 1.

In the eighth column add the carried 1 along with the two digits in
that column, 1 + 1 + 1 = 11. Here we write 11 below the line.

In this example the digits that we write below the line are called sum
bits and the digits that we carry (in gray) are called the carry bits.

A.2 THE HALF-ADDER


Our goal is to design a digital circuit using the logic gates presented in
chapter 5 that adds two eight digit binary numbers. But rst a little
background. A single binary digit is called a bit, which got its name from
binary digit by combining the rst 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 rst step we will design a circuit that does this addition. The key here
is that we will split the addition into two parts; one part will give the sum
bits and the other part will give the carry bits. We will rewrite the sum bits
in a slightly different way that will hopefully seem familiar. Notice that we
wrote the table in the reverse order of the sums.

x y
sum bit

1 1 0
1 0 1
0 1 1
0 0 0

This should look very familiar, it is nothing more than the xor table from
chapter 5.

x y x xor y

1 1 0
1 0 1
0 1 1
0 0 0

Thus the xor gate gives us the sum bit from this addition.

We now rewrite the gray carry bits in a table form. Again, we wrote the
table in the reverse order of the sums.

x y carry bit

1 1 1
1 0 0
0 1 0
0 0 0

This is exactly the and table from chapter 5.

x y x × y

1 1 1
1 0 0
x y x × y

0 1 0
0 0 0

Thus the and gate gives us the carry bit from this addition.

So if x is the rst digit and y is the second digit then the xor gate gives us
the sum bit and the and gate gives us the carry bit. The whole circuit is
given by

This circuit is called a half-adder. It works perfectly for adding two single-
digit binary numbers. So, when it comes to adding two eight-digit binary
numbers this circuit would work ne for the rst column on the right
where we do not yet have to worry about caries. But for the second column
on, this circuit is not enough since it is possible we have carries. If we have
a carry then we need to add three one-digit binary numbers.

A.3 THE FULL-ADDER


We want to be able to add a carry digit along with two other digits,
c + x + y. In other words, we want to be able to add three single-digit

binary numbers. We now rewrite sums of three single digit binary numbers
using both a gray carry bit and a black sum bit;
0 + 0 + 0 = 00, 1 + 0 + 0 = 01,

0 + 0 + 1 = 01, 1 + 0 + 1 = 10,

0 + 1 + 0 = 01, 1 + 1 + 0 = 10,

0 + 1 + 1 = 10, 1 + 1 + 1 = 11.

This is what we want our circuit to do.


The basic idea is that we can do this in stages. First we add x + y using the
half-adder. This is what we get,

0 + 0

= 00,

0 + 1

= 01,

1 + 0

= 01,

1 + 1

= 10.

Then we take the sum digit that the half-adder outputs, shown in black
above, and add it to the carry digit c using another half adder. The carry
digit can either be c = 1 or c = 0. The addition of this carry digit with the
sum digit turns out to be

0 + 0

= 00,

1 + 0

= 01,
0 + 1

= 01,

1 + 1

= 10,

0 + 1

= 01,

1 + 1

= 10,

0 + 0

= 00,

1 + 0

= 01.

Notice, the sum digits in black that we get here are exactly the same as the
sum digits that we want to get when we add three single-digit binary
numbers. So putting two half-adders in a row seems to give us the sum
digits that we want. But what about the carry digits? These do not exactly
match up with the carry digits we want when we add three numbers.
Suppose

a = Carry digit f rom the addition x + y,

b = Carry digit f rom the additon of c and sum digit of x + y.

Then we get the following table for a ∨ b. (Note, in the chapter on Boolean
Algebra we would have written this as a + b but meant Boolean addition.
Here since we are already talking about addition we will use the logical
symbol for or to avoid confusion.)
a b a ∨ b

0 0 0
0 0 0
0 0 0
1 0 1
0 0 0
0 1 1
0 1 1
1 0 1

And a + b are exactly the carry digits we want from adding three digits.
Thus, the circuit we want is a half-adder for x + y, and then another half-
adder for the sum digit of x + y and c, and an or gate for the carry digit of
x + y and c. In other words, we want this circuit:

It would be a good idea to trace through this circuit for all of the possible
values of x, y, and c and check that its outputs match the expected
in

outputs of the additions from the start of the section.

A.4 ADDING TWO EIGHT-DIGIT BINARY NUMBERS


Our goal is to add two eight-digit binary numbers. We rst found a circuit,
called the half-adder, that would add two single-digit binary numbers
x + y. We then used that to nd a circuit, called a full-adder, that would
add three single digit binary numbers c + x + y. Now we will nd a circuit
to add two eight-digit binary numbers. Suppose that we are adding
x x x x x x x x
8 7 6 5 4 3 and y y y y y y y y , where each xi and yi is a
2 1 8 7 6 5 4 3 2 1

single binary digit 0 or 1. Here we add these two numbers.


c7 c6 c5 c4 c3 c2 c1 0
x8 x7 x6 x5 x4 x3 x2 x1

+y 8 y 7 y 6 y 5 y 4 y 2 y 1

c8 s8 s7 s6 s5 s4 s3 s2 s1

In the rst column (on the far right) we add 0 + x + y . This addition 1 1

gives us a carry digit c1 and a sum digit s1. The sum digit s1 is written
below the line and the carry digit c1 is carried to column two. In the second
column we add c + x + y to get a carry digit c2 and a sum digit s2. The
1 2 2

sum digit s2 is written below the line and the carry digit c2 is carried to
column three. Columns three through seven are similar. In column eight
we add c + x + y to give us both a carry digit c8 and a sum digit s8.
7 8 8

Here we write both of these below the line. this is exactly what we want
our circuit to do.
The way we do this is to link up eight full-adders in a row, taking the carry
digit output from each full-adder as the carry digit input into the next full
adder and recording the sum digit output from each full adder. This full
circuit is shown in Fig. A.1. Notice, because we are using a full-adder for
the addition x + y , we automatically have the carry digit input into the
1 1

rst 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 rst 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.
Figure A.1 The full circuit for an eight-bit adder. This circuit adds the two eight-big binary
numbers x x x x x x x x and y y y y y y y y to obtain the binary number
8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1

cs s s s s s s s . Two sixteen-bit binary numbers could be added together using two eight-bit
8 7 6 5 4 3 2 1

adders in a row. The carry bit c would become the input c for the second eight-bit adder.
in
APPENDI X B
Answers to Problems

Jon Pierre Fortney


12.1 CHAPTER ONE ANSWERS

Question 1.1: This algorithm nds the number of seconds in a period of time that is given as a number of days,
hours, minutes, and seconds. The output is 287, 487.

Question 1.2:

a. Step x y max Output


1 6 12 - -
2.2 6 12 12 -
3 6 12 12 12

b. Step x y max Output


1 15 9 - -
2.1 15 9 15 -
3 15 9 15 15

c. Step x y max Output


1 7 7 - -
2.2 7 7 7 -
3 7 7 7 7

Question 1.3:

a. Step price paid change Output


1 74.36 82.50 - -
2.2 74.36 82.50 8.84 -
3 74.36 82.50 8.84 Change 8.14

b. Step price paid change Output


1 75.00 55.50 - -
2.1 75.00 55.50 - Did not pay enough

c. Step price paid change Output


1 48.36 54.92 - -
2.2 48.36 54.92 6.56 -
3 48.36 54.92 6.56 Change 6.56

Question 1.4:

a. Step x answer Output


1 1 - -
2 -1 - -
3.1 -1 4 -
4 -1 4 4

b. Step x answer Output


1 -3 - -
2 3 - -
3.2 3 -2 -
4 3 -2 -2

c. Step x answer Output


1 7 - -
2 -7 - -
3.2 -7 -12 -
4 -7 -12 -12

Question 1.5:

a. Step x answer Output


1 -6 - -
2.1 -6 -9 -
Step x answer Output
3 -6 -9 -9

b. Step x answer Output


1 2 - -
2.2.2 2 -7 -
3 2 -7 -7

c. Step x answer Output


1 8 - -
2.2.1 8 -13 -
3 8 -13 -13

Question 1.6:

a. Step x n i answer Output


1 2 4 - - -
2 2 4 - 2 -
3.1 2 4 1 4 -
3.1 2 4 2 8 -
3.1 2 4 3 16 -
4 2 4 3 16 16

b. Step x n i answer Output


1 3 5 - - -
2 3 5 - 3 -
3.1 3 5 1 9 -
3.1 3 5 2 27 -
3.1 3 5 3 81 -
3.1 3 5 4 243 -
4 3 5 4 243 243

c. Step x n i answer Output


1 5 6 - - -
2 5 6 - 5 -
3.1 5 6 1 25 -
3.1 5 6 2 125 -
3.1 5 6 3 625 -
3.1 5 6 4 3125 -
3.1 5 6 5 15625 -
4 5 6 5 15625 15625

Question 1.7:

a. Step n fac Output


1 3 - -
2 3 3 -
3.1 2 3 -
3.2 2 6 -
3.1 1 6 -
3.2 1 6 -
4 1 6 6

b. Step n fac Output


1 5 - -
2 5 5 -
3.1 4 5 -
3.2 4 20 -
3.1 3 20 -
3.2 3 60 -
3.1 2 60 -
3.2 2 120 -
3.1 1 120 -
3.2 1 120 -
Step n fac Output
4 1 120 120

c. Step n fac Output


1 7 - -
2 7 7 -
3.1 6 7 -
3.2 6 42 -
3.1 5 42 -
3.2 5 210 -
3.1 4 210 -
3.2 4 840 -
3.1 3 840 -
3.2 3 2520 -
3.1 2 2520 -
3.2 2 5040 -
3.1 1 5040 -
3.2 1 5040 -
4 1 5040 5040

Question 1.8:

a. Step n i fac Output


1 3 - - -
2 3 - 1 -
3.1 3 1 1 -
3.1 3 2 2 -
3.1 3 3 6 -
4 3 3 6 6

b. Step n i fac Output


1 5 - - -
2 5 - 1 -
3.1 5 1 1 -
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

c. Step n i fac Output


1 7 - - -
2 7 - 1 -
3.1 7 1 1 -
3.1 7 2 2 -
3.1 7 3 6 -
3.1 7 4 24 -
3.1 7 5 120 -
3.1 7 6 720 -
3.1 7 7 5040 -
4 7 7 5040 5040

Question 1.9:
Step d n Output
1 - 8, 490, 725, 727, 154, 368, 726, 402, 945 -
2 25 8, 490, 725, 727, 154, 368, 726, 402, 945 -
3.1 25 117(← 8 + 4 + 9 + 0 + 7 + ⋯ + 4 + 5 -
3.2 3 117 -
3.1 3 9(← 1 + 1 + 7) -
4 1 9 9

Question 1.10:
a. x 1 , , , ,
= 2 x2 = 7 x3 = 4 x4 = 3 x5 = 9 x6 = 0 s = 9 , ,
Step i j xi yj Output
2.1.1 1 1 2 4 -
2.1.1 1 2 2 3 -
2.1.1 1 3 2 9 -
2.1.1 2 1 7 4 -
2.1.1 2 2 7 3 -
2.1.1 2 3 7 9 -
2.1.1 3 1 3 4 -
2.1.1 3 2 3 3 -
2.1.1.1 3 2 3 3 There is an element common to both strings.

b. x 1 , , , ,
= 5 x2 = 9 x3 = 3 y1 = 7 y2 = 2 y3 = 5 n = 3 , ,
Step i j xi yj Output
2.1.1 1 1 5 7 -
2.1.1 1 2 5 2 -
2.1.1 1 3 5 5 -
2.1.1 1 3 5 5 There is an element common to both strings.

c. x 1 , , , ,
= 1 x2 = 2 x3 = 3 y1 = 9 y2 = 8 y3 = 7 n = 3 , ,
Step i j xi yj Output
2.1.1 1 1 1 9 -
2.1.1 1 2 1 8 -
2.1.1 1 3 1 7 -
2.1.1 2 1 2 9 -
2.1.1 2 2 2 8 -
2.1.1 2 3 2 7 -
2.1.1 3 1 3 9 -
2.1.1 3 2 3 8 -
2.1.1 3 3 3 7 -
3 3 3 3 7 There is no element common to both strings.

Question 1.11:

a. x1 = 2, x2 = 1, x3 = 0, x4 = 9, y1 = 4, y2 = 0, y3 = 7, y4 = 1, s = 0

Step i xi yi Output
2.1 1 2 - -
2.1 2 1 - -
2.1 3 0 - -
2.1.1 3 0 - String one contains 0.

b. x1 = 5, x2 = 9, x3 = 3, y1 = 7, y2 = 2, y3 = 1, s = 2

Step i xi yi Output
2.1 1 5 - -
2.1 2 9 - -
2.1 3 3 - -
3.1 1 - 7 -
3.1 2 - 2 -
3.1.1 2 - 2 String two contains 2.

c. x1 = 3, x2 = 9, x3 = 7, y1 = 4, y2 = 4, y3 = 3, s = 6

Step i xi yi Output
2.1 1 3 - -
2.1 2 9 - -
2.1 3 7 - -
3.1 1 - 4 -
3.1 2 - 4 -
Step i xi yi Output
3.1 3 - 7 -
4 3 - - Neither string contains 6.

Question 1.12:

a. x1 = 2, x2 = 7, x3 = 3, y1 = 4, y2 = 3, y3 = 9, n = 3

Step i j xi yi Output
2.1.1 1 1 2 4 -
2.1.1 1 2 2 3 -
2.1.1 1 3 2 9 -
2.1.1 2 1 7 4 -
2.1.1 2 2 7 3 -
2.1.1 2 3 7 9 -
2.1.1 3 1 3 4 -
2.1.1 3 2 3 3 -
2.1.1.1 3 2 3 3 There is an element common to both strings.

b. x1 = 5, x2 = 9, x3 = 3, y1 = 7, y2 = 2, y3 = 5, n = 3

Step i j xi yi Output
2.1.1 1 1 5 7 -
2.1.1 1 2 5 2 -
2.1.1 1 3 5 5 -
2.1.1 1 3 5 5 There is an element common to both strings.

c. x1 = 1, x2 = 2, x3 = 3, y1 = 9, y2 = 8, y3 = 7, n = 3

Step i j xi yi Output
2.1.1 1 1 1 9 -
2.1.1 1 2 1 8 -
2.1.1 1 3 1 7 -
2.1.1 2 1 2 9 -
2.1.1 2 2 2 8 -
2.1.1 2 3 2 7 -
2.1.1 3 1 3 9 -
2.1.1 3 2 3 8 -
2.1.1 3 3 3 7 -
3 3 3 3 7 There is no element common to both strings.

Question 1.13:

a. x 1 , , ,
= 6 x2 = 3 x3 = 5 x4 = 2 x5 = 4 ,
Step i j xi xj Output
2.1.1 1 2 6 3 -
2.1.1 1 3 6 5 -
2.1.1 1 4 6 2 -
2.1.1 1 5 6 4 -
2.1.1 2 3 3 5 -
2.1.1 2 4 3 2 -
2.1.1 2 5 3 4 -
2.1.1 3 4 5 2 -
2.1.1 3 5 5 4 -
2.1.1 4 5 2 4 -
3 4 5 2 4 There are no duplicate integer in the string.

b. x 1 , , ,
= 4 x2 = 7 x3 = 3 x4 = 7 x5 = 2 x6 = 9 x7 = 1 x8 = 6, , , ,
Step i j xi xj Output
2.1.1 1 2 4 7 -
2.1.1 1 3 4 3 -
Step i j xi xj Output
2.1.1 1 4 4 7 -
2.1.1 1 5 4 2 -
2.1.1 1 6 4 9 -
2.1.1 1 7 4 1 -
2.1.1 1 8 4 6 -
2.1.1 2 3 7 3 -
2.1.1 2 4 7 7 -
2.1.1.1 2 4 7 7 There is a duplicate integer in the string.

c. x 1 , , , ,
= 5 x2 = 6 x3 = 3 x4 = 7 x5 = 3 x6 = 2 ,
Step i j xi xj Output
2.1.1 1 2 5 6 -
2.1.1 1 3 5 3 -
2.1.1 1 4 5 7 -
2.1.1 1 5 5 3 -
2.1.1 1 6 5 1 -
2.1.1 2 3 6 3 -
2.1.1 2 4 6 7 -
2.1.1 2 5 6 3 -
2.1.1 2 6 6 2 -
2.1.1 3 4 3 7 -
2.1.1 3 5 3 3 -
2.1.1.1 3 5 3 3 There is a duplicate integer in the string.

Question 1.14:

a. Step i sum Output


2 - 0 -
3.1 1 1 -
3.1 2 5 -
3.1 3 14 -
4 3 14 14

b. Step i sum Output


2 - 0 -
3.1 1 1 -
3.1 2 5 -
3.1 3 14 -
3.1 4 30 -
4 4 30 30

c. Step i sum Output


2 - 0 -
3.1 1 1 -
3.1 2 5 -
3.1 3 14 -
3.1 4 30 -
3.1 5 55 -
3.1 6 91 -
4 6 91 91

Question 1.15:

a. Step n order Output


1 12 - -
2 12 0 -
3.1 6 0 -
3.2 6 1 -
3.1 3 1 -
3.2 3 2 -
4 3 2 2
b. Step n order Output
1 90 - -
2 90 0 -
3.1 45 0 -
3.2 45 1 -
4 45 1 1

c. Step n order Output


1 80 - -
2 80 0 -
3.1 40 0 -
3.2 40 1 -
3.1 20 1 -
3.2 20 2 -
3.1 10 2 -
3.2 10 3 -
3.1 5 3 -
3.2 5 4 -
4 5 4 4

Question 1.16:

a. x 1 , , ,
= 5 x2 = 7 x3 = 4 x4 = 6 x5 = 2 x6 = 8 , ,
Step min position i xi Output
2 5 1 - - -
3.1 5 1 2 7 -
3.1 5 1 3 4 -
3.1.1 4 1 3 4 -
3.1.2 4 3 3 4 -
3.1 4 3 4 6 -
3.1 4 3 5 2 -
3.1.1 2 3 5 2 -
3.1.2 2 5 5 2 -
3.1 2 5 6 8 -
4 2 5 6 8 2,5

b. x1 = 3

,x 2 , , ,
= 5 x3 = 7 x4 = 2 x5 = 4 x6 = 6 ,
Step min position i xi Output
2 3 1 - - -
3.1 3 1 2 5 -
3.1 3 1 3 7 -
3.1 3 1 4 2 -
3.1.1 2 1 4 2 -
3.1.2 2 4 4 2 -
3.1 2 4 5 4 -
3.1 2 4 6 6 -
4 2 4 6 6 2,4

c. x 1 , , , ,
= 7 x2 = 2 x3 = 5 x4 = 0 x5 = 9 x6 = 3 x7 = 1 , ,
Step min position i xi Output
2 7 1 - - -
3.1 7 1 2 2 -
3.1.1 2 1 2 2 -
3.1.2 2 2 2 2 -
3.1 2 2 3 5 -
3.1 2 2 4 0 -
3.1.1 0 2 4 0 -
3.1.2 0 4 4 0 -
3.1 0 4 5 9 -
Step min position i xi Output
3.1 0 4 6 3 -
3.1 0 4 7 1 -
4 0 4 7 1 0,4

Question 1.17:

a. x 1 , ,
= 0 x2 = 4 x3 = m x4 = 3 x5 = s x7 = 8 , , ,
Step i xi \beginsubarraycnoninteger _ \\detected\endsubarray Output
2 1 - - -
3 1 - false -
4.1 1 0 false -
4.2 2 0 false -
4.1 2 4 false -
4.2 3 4 false -
4.1 3 m false -
4.1.1 3 m true -
4.1 4 m true -
5.1 4 m true String contains non-integer character.

b. x 1 ,
= 8 x2 = 9 x3 = 0 ,
Step i xi \beginsubarraycnoninteger _ \\detected\endsubarray Output
2 1 - - -
3 1 - false -
4.1 1 8 false -
4.2 2 8 false -
4.1 2 9 false -
4.2 3 9 false -
4.1 3 0 false -
5.2 3 0 false String consists entierly of integers.

c. x1 = 7

,x 2 ,
= 3 x3 = 2 x4 = r x5 = 9 , ,
Step i xi noninteger _ detected Output
2 1 - - -
3 1 - false -
4.1 1 7 false -
4.2 2 7 false -
4.1 2 3 false -
4.2 3 3 false -
4.1 3 2 false -
4.2 4 2 false -
4.1 4 r false -
4.1.1 4 r true -
4.2 5 r true -
String contains
5.1 5 r true
non-digit character.

Question 1.18: One possible solution:

1. Input string x , x 1 2, … , xn .
2. For i = 1 to n do

1. If xi is not a digit then

1. 2.1.1. Return “String contains non-digit characters.”

3. Return “String consists entirely of digits.”


12.2 CHAPTER TWO ANSWERS

Question 2.1:

a. 101 = 5
2 10

b. 110 = 6
2 10

c. 111 = 7
2 10

d. 1011 = 11 2 10

e. 1101 = 13 2 10

f. 1010 = 10 2 10

g. 10010 = 18 2 10

h. 10110 = 22 2 10

i. 11111 = 31 2 10

Question 2.2:

a. 0110 1001 2 = 105 10

b. 1011 1100 2 = 188 10

c. 0011 0111 2 = 55 10

d. 1111 0101 2 = 245 10

e. 1001 0110 2 = 150 10

f. 0110 1000 2 = 104 10

g. 1011 0110 2 = 182 10

h. 0010 1011 2 = 43 10

i. 1101 1101 2 = 221 10

Question 2.3:

a. 27 = 23
8 10

b. 42 = 34
8 10

c. 35 = 29
8 10

d. 103 = 67
8 10

e. 673 = 443
8 10

f. 360 = 240
8 10

g. 4721 = 2513
8 10

h. 2451 = 1321
8 10

i. 7715 = 4045
8 10

Question 2.4:

a. 7254 = 3756
8 10

b. 1602 = 8988 10

c. 4640 = 2464
8 10

d. 17530 = 8024 8 10

e. 72501 = 30017 8 10

f. 11101 = 4673 8 10

g. 365310 = 125640 8 10

h. 772531 = 259417 8 10

i. 417524 = 139092 8 10

Question 2.5:

a. A3 = 163
16 10

b. 9F = 159
16 10

c. 17 = 23
16 10

d. 3D2 = 978
16 10
e. 9A0 16 = 2464 10

f. AED 16 = 2797 10

g. F 001 16 = 61441 10

h. 6D27 16 = 27943 10

i. 39CB 16 = 14795 10

Question 2.6:

a. 802 = 2050
16 10

b. 6A3 = 1699
16 10

c. F 2E = 3886
16 10

d. 290B = 10507
16 10

e. 4C71 = 19569
16 10

f. 1101 = 4353
16 10

g. ABCDE = 703710 16 10

h. 2E916 = 190742 16 10

i. 97CA0 = 621728 16 10

Question 2.7:

a. 0.11 = 0.75
2 10

b. 0.01 = 0.25
2 10

c. 0.10 = 0.5
2 10

d. 0.110 = 0.75
2 10

e. 0.101 = 0.625
2 10

f. 0.011 = 0.375
2 10

g. 0.0111 = 0.43752 10

h. 0.0011 = 0.18752 10

i. 0.1001 = 0.56252 10

Question 2.8:

a. 100.110 = 4.75 2 10

b. 110.011 = 6.375 2 10

c. 111.001 = 7.125 2 10

d. 1101.1000 = 13.5 2 10

e. 0110.0111 = 6.4375 2 10

f. 1011.1001 = 11.5625 2 10

g. 1111.1111 = 15.9375 2 10

h. 0001.0001 = 1.0625 2 10

i. 1100.0101 = 12.3125 2 10

Question 2.9:

a. 0.4 = 0.5
8 10

b. 0.1 = 0.125
8 10

c. 0.7 = 0.875
8 10

d. 0.72 = 0.90625
8 10

e. 0.03 = 0.046875
8 10

f. 0.14 = 0.1875
8 10

g. 0.553 = 0.708984375
8 10

h. 0.407 = 0.513671875
8 10

i. 0.321 = 0.408203125
8 10

Question 2.10:
a. 7.5 = 7.625
8 10

b. 4.2 = 4.25
8 10

c. 3.6 = 3.75
8 10

d. 37.63 = 31.796875
8 10

e. 56.12 = 46.15625
8 10

f. 25.25 = 21.328125
8 10

g. 427.014 = 279.0234375
8 10

h. 510.442 = 328.56640625
8 10

i. 703.635 = 451.806640625
8 10

Question 2.11:

a. 0.7 = 0.4375
16 10

b. 0.A = 0.625
16 10

c. 0.3 = 0.1875
16 10

d. 0.A8 = 0.65625
16 10

e. 0.4C = 0.295875
16 10

f. 0.82 = 0.5078125
16 10

g. 0.C48 = 0.767578125
16 10

h. 0.379 = 0.2170410156
16 10

i. 0.ABC = 0.6708984375
16 10

Question 2.12:

a. 9.3 = 9.1875
16 10

b. B. E = 11.875
16 10

c. 4.C = 4.75
16 10

d. AC. DC = 172.859375 16 10

e. F 2.39 = 242.22265625
16 10

f. 5E. C5 = 94.76953125
16 10

g. A04.BB8 = 2564.732421875
16 10

h. 7CF .9F 0 = 1999.62109375


16 10

i. 101.101 = 257.06274414063
16 10

Question 2.13:

a. 62 = 110010
8 2

b. 31 = 11001
8 2

c. 571 = 101111001
8 2

d. 31.72 = 11001.11101
8 2

e. 42.15 = 100010.001101
8 2

f. 72.37 = 111010.011111
8 2

g. 313.011 = 11001011.000001001
8 2

h. 643.026 = 110100011.00001011
8 2

i. 211.361 = 10001001.011110001
8 2

Question 2.14:

a. A0 = 10100000
16 2

b. 3B = 111011
16 2

c. 27 = 100111
16 2

d. F 2.F 2 = 11110010.1111001
16 2

e. 5B.93 = 1011011.10010011
16 2

f. 88.7C = 10001000.011111
16 2

g. 214.3C5 = 1000010100.001111000101
16 2

h. AD2.0BC = 101011010010.0000101111
16 2
i. 101.011 16 = 100000001.000000010001 2

Question 2.15:

a. 10 = 2
2 8

b. 110 = 6
2 8

c. 11 = 3
2 8

d. 110.111 = 6.7 2 8

e. 011.010 = 3.2 2 8

f. 101.001 = 5.1 2 8

g. 11011.001001 = 33.11 2 8

h. 100011.10111001 = 43.562 2 8

i. 11110001.00011101 = 361.072 2 8

Question 2.16:

a. 1101 = D 2 16

b. 10 = 2
2 16

c. 101 = 5
2 16

d. 1110.1101 = E. D 2 16

e. 1011.1010 = B. A 2 16

f. 0101.0011 = 5.3 2 16

g. 111001011.0011011 = 1CB.36 2 16

h. 1011101.01011010101 = 5D.5AA 2 16

i. 10111100101.100111 = 5E5.9C 2 16

Question 2.17:

a. 7 = 111
10 2

b. 3 = 11
10 2

c. 6 = 110
10 2

d. 28 = 11100
10 2

e. 39 = 100111
10 2

f. 79 = 1001111
10 2

g. 381 = 101111101
10 2

h. 643 = 1010000011
10 2

i. 569 = 1000111001
10 2

Question 2.18:

a. 78 = 116
10 8

b. 93 = 135
10 8

c. 52 = 64
10 8

d. 295 = 447
10 8

e. 944 = 1660
10 8

f. 641 = 1201
10 8

g. 4862 = 11376
10 8

h. 5078 = 11726
10 8

i. 1532 = 2774
10 8

Question 2.19:

a. 3410 = 22 16

b. 2310 = 17 16

c. 536 10 = 218 16

d. 285 10 = 11D 16
e. 1897 10 = 769 16

f. 8321 10 = 2081 16

g. 90867 10 = 162F 3 16

h. 42778 10 = A71A 16

i. 28714 10 = 702A 16

Question 2.20:

a. 0.5 = 0.1
10 2

b. 0.4 ≈ 0.011001
10 2

c. 0.8 ≈ 0.110011
10 2

d. 0.32 ≈ 0.010100
10 2

e. 0.81 ≈ 0.110011
10 2

f. 0.77 ≈ 0.110001
10 2

g. 0.239 ≈ 0.001111
10 2

h. 0.552 ≈ 0.100011
10 2

i. 0.798 ≈ 0.110011
10 2

Question 2.21:

a. 0.9 ≈ 0.714631
10 8

b. 0.4 ≈ 0.314631
10 8

c. 0.6 ≈ 0.463146
10 8

d. 0.83 ≈ 0.650753
10 8

e. 0.25 ≈ 0.2
10 8

f. 0.44 ≈ 0.341217
10 8

g. 0.482 ≈ 0.366621
10 8

h. 0.667 ≈ 0.525402
10 8

i. 0.315 ≈ 0.241217
10 8

Question 2.22:

a. 0.1 ≈ 0.199999
10 16

b. 0.3 ≈ 0.4CCCCC
10 16

c. 0.9 ≈ 0.E66666
10 16

d. 0.11 ≈ 0.1C28F 5
10 16

e. 0.83 ≈ 0.D47AE1
10 16

f. 0.47 ≈ 0.7851EB
10 16

g. 0.429 ≈ 0.6DD2F 1
10 16

h. 0.638 ≈ 0.A353F 7
10 16

i. 0.314 ≈ 0.50624D
10 16
12.3 CHAPTER THREE ANSWERS

Question 3.1:

a. No, an opinion.
b. No, a question.
c. Yes.
d. Yes.
e. Yes.
f. Yes.
g. No, a request.
h. No, a request.
i. Yes.

Question 3.2:

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.

Question 3.3:

a. p ∧ q ≡ T ∧ F ≡ F
b. q ∧ p ≡ F ∧ T ≡ F
c. ¬q ∨ p ≡ ¬F ∨ T ≡ T ∨ T ≡ T
d. ¬p ∨ q ≡ ¬T ∨ F ≡ F ∨ F ≡ F
e. p ∧ ¬q ≡ T ∧ ¬F ≡ T ∧ T ≡ T
f. ¬q ∨ ¬p ≡ ¬F ∨ ¬T ≡ T ∨ F ≡ T
g. p → q ≡ T → F ≡ F
h. ¬q → p ≡ ¬F → T ≡ T → T ≡ T
i. ¬q → ¬p ≡ ¬F → ¬T ≡ T → F ≡ F
j. p ↔ q ≡ T ↔ F ≡ F
k. ¬q ↔ p ≡ ¬F ↔ T ≡ T ↔ T ≡ T
l. ¬p ↔ ¬q ≡ ¬T ↔ ¬F ≡ F ↔ T ≡ F

Question 3.4:

p q p ∧ q

a.
T T T
T F F
F T F
F F F

p q q ∧ p

b.
T T T
T F F
F T F
F F F

p q ¬q ∨ p

c.
T T T
p q ¬q ∨ p

T F T
F T F
F F T

p q ¬p ∨ q

d.
T T T
T F F
F T T
F F T

p q p ∧ ¬q

e.
T T F
T F T
F T F
F F F

p q ¬q ∨ ¬p

f.
T T F
T F T
F T T
F F T

p q p → q

g.
T T T
T F F
F T T
F F T

p q ¬q → p

h.
T T T
T F T
F T T
F F F

p q ¬q → ¬p

i.
T T T
T F F
F T T
F F T

p q p ↔ q

j.
T T T
T F F
F T F
F F T

p q ¬q ↔ p

k.
T T F
T F T
F T T
F F F
p q ¬p ↔ ¬q

l.
T T T
T F F
F T F
F F T

Question 3.5:
We have

p q p → q

T T T
T F F
F T T
F F T

and

p q ¬p ∨ q

T T T
T F F
F T T
F F T

.
Since both p → q and ¬p ∨ q have identical truth tables we can say that p → q ≡ ¬p ∨ q.

Question 3.6:
We have

p q p ↔ q

T T T
T F F
F T F
F F T

and

p q p → q q → p (p → q) ∧ (q → p)

T T T T T
T F F T F
F T T F F
F F T T T

.
Since both p ↔ q and (p → q) ∧ (q → p) have identical truth tables we can say they are equivalent. Using the
implication law we can write p ↔ q ≡ (¬p ∨ q) ∧ (¬q ∨ p).

Question 3.7:

p q r (p ∨ ¬r) → q

a.
T T T T
T T F T
T F T F
T F F F
F T T T
p q r (p ∨ ¬r) → q

F T F T
F F T T
F F F F

p q (q ∧ p) → ¬p

b.
T T F
T F T
F T T
F F T

p q ¬p → (¬p ∨ q)

c.
T T T
T F T
F T T
F F T

p q r ¬(p ∧ ¬q) ∨ r

d.
T T T T
T T F T
T F T T
T F F F
F T T T
F T F T
F F T T
F F F T

p q r ¬[(¬p ∧ q) ∨ r]

e.
T T T F
T T F T
T F T F
T F F T
F T T F
F T F F
F F T F
F F F T

p q r (p ∧ ¬q) ∧ (r ∨ q)

f.
T T T F
T T F F
T F T T
T F F F
F T T F
F T F F
F F T F
F F F F

g. p q r (p ∨ q) ↔ [p ∨ (q ∧ r)]

T T T T
T T F T
T F T T
T F F T
F T T T
F T F F
F F T T
F F F T
p q r (q ∧ ¬r) ↔ ¬(p ∨ r)

h.
T T T T
T T F F
T F T T
T F F T
F T T T
F T F T
F F T T
F F F F

p q r (p ↔ q) ∧ (r ↔ q)

i.
T T T T
T T F F
T F T F
T F F F
F T T F
F T F F
F F T F
F F F T

Question 3.8:

a. p ∨ p = p
b. p ∨ ¬p = T
c. ¬(p ∨ ¬p) = F
d. p ∨ (p ∧ q) = p
e. p ∨ (¬p ∧ q) = p ∨ q
f. p ∧ (¬p ∨ q) = p ∧ q
g. p ∧ p = p
h. p ∧ (p ∨ q) = p
i. p ∧ (p ∨ q ∨ r) = p
j. (p ∧ q) ∨ (p ∧ ¬q) = p
k. ¬(¬p ∨ ¬p) = p
l. (p ∧ q) ∨ (¬p ∧ q) = q

Question 3.9:

a. (¬p ∨ ¬q) ∧ (¬p ∨ q) = ¬p


b. q ∨ (q ∧ ¬q) = q
c. ¬p ∨ (q ∧ ¬p) = ¬p
d. (p ∨ ¬q) ∧ (p ∨ q) = p
e. r ∨ [r ∨ (r ∧ p)] = r
f. p ∧ [p ∨ (p ∧ q)] = p
g. r ∨ (r ∧ ¬p ∧ q ∧ s) = r
h. ¬r ∧ ¬(r ∧ p ∧ q ∧ s) = ¬r

Question 3.10:

a. q ∧ [p ∨ (¬p ∧ q)] = q
b. ¬[(p ∧ ¬q) ∨ ¬p] = p ∧ q
c. (¬p ∧ q) ∨ [p ∧ (p ∨ q)] = p ∨ q
d. ¬[p ∨ (p ∧ q)] ∧ q = ¬p ∧ q
e. (p ∧ ¬r) ∨ (¬p ∧ q) ∨ ¬(q ∧ r) = ¬p ∨ ¬q ∨ ¬r
f. ¬(p ∧ q) ∨ (q ∧ r) = ¬p ∨ ¬q ∨ r
g. [p ∨ (q ∧ r)] ∧ (¬p ∨ r) = (p ∨ q) ∧ r
h. (p ∧ ¬r) ∨ (¬p ∧ q) ∨ ¬(p ∧ r) = ¬p ∨ ¬r
i. (p ∧ r) ∨ (¬p ∧ q) ∨ (r ∧ q) = (p ∧ r) ∨ (q ∧ ¬p)
j. ¬p ∨ ¬q ∨ [p ∧ q ∧ ¬r] = ¬p ∨ ¬q ∨ ¬r
k. (p ∨ r) ∧ (¬p ∨ q) ∧ (r ∨ q) = (p ∧ q) ∨ (r ∧ ¬p)
l. ¬(p ∧ q) ∧ (¬p ∨ q) ∧ (¬q ∨ q) = ¬p

Question 3.11:

a. tautology
b. not a tautology
c. tautology
d. not a tautology

Question 3.12

a. contradiction
b. not a contradiction
c. not a contradiction
d. contradiction
12.4 CHAPTER FOUR ANSWERS

Question 4.1:

a. True
b. True
c. False
d. True
e. False
f. False
g. False
h. True
i. True
j. False
k. True
l. False
m. False
n. False
o. False
p. True
q. True
r. True
s. True
t. False
u. True
v. False
w. True
x. False

Question 4.2:

a. True
b. True
c. False
d. False
e. True
f. True
g. False
h. True
i. True
j. True
k. False
l. True
m. False
n. True
o. False
p. False
q. False
r. True
s. False
t. True
u. False
v. False
w. False
x. True

Question 4.3:

a. True
b. True
c. False
d. True
e. False
f. True
g. False
h. True
i. True
j. True
k. True
l. True
m. True
n. True
o. False
p. True
q. True
r. True
s. False
t. True

Question 4.4:

a. { }, {a}
b. { }, {5}
c. { }, {v}
d. { }, {a}, {b}, {a, b}
e. { }, {5}, {7}, {5, 7}
f. { }, {5}, {e}{5, e}
g. { }, {2}, {4}, {6}, {2, 4},
{ }, {2}, {4}, {6}, {2, 4},
h.
{2, 6}, {4, 6}, {2, 4, 6}

{ }, {x}, {y}, {x}, {x, y},


i.
{x, z}, {y, z}, {x, y, z}

{ }, {2}, {b}, {c}, {2, b},


j.
{2, c}, {b, c}, {2, b, c}

Question 4.5:

a. { }, {{a, b, c}}
b. { }, {{x, y}}
c. { }, {{1, 2, 3}}
d. { }, {r}, {{s, t}}, {r, {s, t}}
e. { }, {{x}}, {{y, z}}, {{x}, {y, z}}
f. { }, {{2, 4}}, {6}, {{2, 4}, 6}
{ }, {2}, {{4}}, {6}, {2, {4}},
g.
{2, 6}, {{4}, 6}, {2, {4}, 6}

{ }, {2}, {{4}}, {{6}}, {{2}, {4}},


h.
{{2}, {6}}, {{4}, {6}}, {{2}, {4}, {6}}
{ }, {u}, {v}, {{w}}, {{u, v}},
i.
{u, {w}}, {v, {w}}, {u, v, {w}}

Question 4.6:

a. {2, 3, 4, 5, 6, 7, 8}
b. {4, 5, 6}
c. {0, 1, 7, 8, 9}
d. {0, 1, 2, 3, 9}
e. {0, 1, 9}
f. {0, 1, 2, 3, 7, 8, 9}
g. {0, 1, 2, 3, 7, 8, 9}
h. {0, 1, 9}
i. {2, 3}
j. {7, 8}

Question 4.7:

a. {1, 2, 3, 4, a, b, c, d, e}
b. {2, c, d}
c. {3, 4, 5, e}
d. {1, 5, a, b}
e. {5}
f. {1, 3, 4, 5, a, b, e}
g. {1, 3, 4, 5, a, b, e}
h. {5}
i. {1, a, b}
j. {3, 4, e}

Question 4.8:

a. {o,p,r,s,t,u,v,y}
b. {p,s,t,u}
c. {q,r,v,w,x,z}
d. {o,q,w,x,y,z}
e. {q,w,x,z}
f. {o,q,r,v,w,x,y,z}
g. {o,q,r,v,w,x,y,z}
h. {q,w,x,z}
i. {o,y}
j. {r,v}

Question 4.9:

a. {-2,2}
b. {-1,1}
c. {-4,5}
d. {3}
e. {-5,5}
f. {-3,4}
g. {-2,2}
h. {-3,2}
i. {0}

Question 4.10:
a. {-1}
b. {-1}
c. { , }
−7

4
3

d. { , }
−5

2
−4

e. {-2,2,3}
f. { , 5}
−7

g. {1}
h. {0}
i. { , }
4

3
2

j. {-5,-3,3}

Question 4.11:

a. |A | = 100
c

b. |B | = ∞
c

c. |A ∩ B | = 0
c c

d. |O | = ∞
c

e. |O ∩ E| = 0
f. |E | = ∞
c

g. |A ∪ B| = ∞
h. |B ∩ E| = 150
i. |A ∩ O| = 50
c

j. |O ∩ E | = ∞ c

k. |A ∩ O | = ∞ c

l. |B ∪ E| = ∞

Question 4.12: subsets: 2 6


= 64 , proper subsets: 2 6
− 1 = 63

Question 4.13: subsets: 2 8


= 256 , proper subsets: 2 8
− 1 = 255

Question 4.14: 2 10
= 1, 024

Question 4.15: 2 10
= 1, 024, 576

Question 4.16: A × B = {(a, 1), (a, 2), (a, 3), (b, 1), (b, 2), (b, 3), (c, 1), (c, 2), (c, 3)}

S × T = {(e, 9), (e, 8), (e, 7), (e, 6), (f , 9), (f , 8), (f , 7), (f , 6),
Question 4.17:
(g, 9), (g, 8), (g, 7), (g, 6), (h, 9), (h, 8), (h, 7), (h, 6)}

X × Y = {(0, t), (1, t), (2, t), (3, t), (4, t), (5, t),
Question 4.18:
(0, f ), (1, f ), (2, f ), (3, f ), (4, f ), (5, f )}

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),


Question 4.19:
(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:
c
A = {e, m, a, x, j, c, w} (A ∩ B) = {g, t, d, o}
c
B = {j, c, w, k, i, b, v} (A ∩ B) = {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}

c
A ∩ B = {j, c, w} A ∪ B = {g, t, d, o, e, m, a, x, j, c, w}

c
A = {g, t, d, o, k, i, b, v} A − B = {e, m, a, x}

c
B = {g, t, d, o, e, m, a, x} B − A = {k, i, b, v}

c
A = {g, i, d, t, b} (A ∩ B) = {m, j, z, c, l}
c
B = {t, b, p, w, v} (A ∩ B) = {m, j, z, c, l, g, i, d, p, w, v}

A ∪ B = {g, i, d, t, b, p, w, v} Ac ∪ B = {m, j, z, c, l, t, b, p, w, v}
Question 4.21: c
A ∩ B = {t, b} A ∪ B = {m, j, z, c, l, g, i, d, t, b}

c
A = {m, j, z, c, l, p, w, v} A − B = {g, i, d}

c
B = {m, j, z, c, l, g, i, d} B − A = {p, w, v}

Question 4.22:
A = {c, z, p, r, g, n, t}
B = {h, d, q, p, r, s, t}
C = {v, o, a, g, n, s, t}
A ∩ B = {p, r, t}
A ∩ C = {g, n, t}
B ∩ C = {s, t}
A ∪ B = {c, z, p, r, g, n, t, s, h, q, d}
A ∪ C = {c, z, p, r, g, n, t, s, v, o, a}
B ∪ C = {h, q, d, p, r, s, t, g, n, v, o, a}
A ∩ B ∩ C = {t}
A ∪ B ∪ C = {c, z, p, r, g, n, t, h, d, q, s, v, o, a}
Ac = {x, f, y, k, j, v, o, a, s, h, q, d}
Bc = {x, f, y, k, j, c, z, g, n, v, o, a}
Cc = {x, f, y, k, j, c, z, p, r, h, d, q}
(A ∩ B)c = {x, f, y, k, j, c, z, g, n, v, o, a, s, h, q, d}
(A ∩ C)c = {x, f, y, k, j, c, z, p, r, h, q, d, s, v, o, a}
(B ∩ C)c = {x, f, y, k, j, d, q, h, r, p, c, z, g, n, v, o, a}
(A ∪ B)c = {x, f, y, k, j, v, o, a}
(A ∪ C)c = {x, f, y, k, j, h, q, d}
(B ∪; C)c = {x, f, y, k, j, c, z}
(A ∩ B ∩ C)c = {x, f, y, k, j, c, z, p, r, g, n, h, q, d, s, v, o, a}
(A ∪ B ∪ C)c = {x, f, y, k, j}
A - B = {c, z, g, n}
B - A = {h, d, q, s}
A - C = {c, z, p, r}
C - A = {v, o, a, s}
B - C = {p, r, h, q, d}
C - B = {g, n, v, o, a}

Question 4.23:
A = {i, p, j, m, t, c, y, n, g, l}
B = {w, k, s, o, t, c, y, d, l}
C = {q, a, v, u, gn, g, d, l}
A ∩ B = {t, c, y, l}
A ∩ C = {n, g, l}
B ∩ C = {d, l}
A ∪ B = {p, i, j, m, t, c, y, n, g, l, k,w, s, o, d}
A ∪ C = {p, i, j, m, t, c, y, n, g, l, d, v, a, q, u}
B ∪ C = {w, k, s, o, t, c, y, d, l, n, g, v, a, q, u}
A ∩ B ∩ C = {l}
A ∪ B ∪ C = {p, i, j, m, t, c, y, n, g, l, k,w, s, o, d, v, a, q, u}
Ac = {e, x, f, r, h, b, z,w, k, s, o, d, u, a, q, u}
Bc = {e, x, f, r, h, b, z, p, i, j, m, n, g, v, a, q, u}
Cc = {e, x, f, r, h, b, z, p, i, j, m, t, c, y, k,w, s, o}
(A ∩ B)c = {e, x, f, r, h, b, z, p, i, j, mn, g, v, a, q, u, d,w, k, s, o}
(A ∩ B)c = {e, x, f, r, h, b, z, p, i, j, m, t, c, y,w, k, s, o, d, v, a, q, u}
(A ∩ B)c = {e, x, f, r, h, b, z, p, i, j, m, t, c, y, k,w, s, o, n, g, v, a, q, u}
(A ∪ B)c = {e, x, f, r, h, b, z, v, a, q, u}
(A ∪ B)c = {e, x, f, r, h, b, z,w, k, s, o}
(B ∪ C)c = {e, x, f, r, h, b, z, p, i, m, j}
(A ∩ B ∩ C)c = {e, x, f, r, h, b, z, p, i, j, m, t, c, y, k,w, s, o, n, g, v, a, q, u, d}
(A ∪ B ∪ C)c = {e, x, f, r, h, b, z}
A - B = {i, j, p, mn, g}
B - A = {w, k, s, o, d}
A - C = {i, j, p, m, t, c, y}
C - A = {a, v, q, u, d}
B - C = {t, c, y, k,w, s, o}
C - B = {n, g, a, v, q, u}

Question 4.24:
c
|A| = 27 |A ∪ B[C| = 57 |(A ∩ B ∩ C) | = 69

c c
|B| = 40 |A | = 47 |(A ∪ B ∪ C) | = 17

c
|C| = 26 |B | = 34 |A − B| = 13

c
|A ∩ B| = 14 |C | = 48 |B − A| = 26
c
|A ∩ C| = 11 |(A ∩ B) | = 60 |A − C| = 16
c
|B ∩ C| = 16 |(A ∩ C) | = 63 |C − A| = 15
c
|A ∪ B| = 53 |(B ∩ C) | = 58 |B − C| = 24
c
|A ∪ C| = 42 |(A ∪ B) | = 21 |C − B| = 10
c
|B ∪ C| = 50 |(A ∪ C) | = 32
c
|A ∩ B ∩ C| = 5 |(B ∪ C) | = 24

Question 4.25:

a. A ∪ A = A
b. A ∪ A = u
c

c. (A ∪ A ) = ∅
c c

d. A ∪ (A ∪ B) = A
e. A ∪ (A ∪ B) = A ∪ B
c

f. A ∩ (A ∩ B) = A ∩ B
c

g. A ∩ A = A
h. A ∩ (A ∪ B) = A
i. A ∩ (A ∪ B ∪ C) = A
j. (A ∩ B) ∪ (A ∩ B ) = A c

k. (A ∪ A ) = A
c c
l. (A ∩ B) ∪ (A c
∩ B) = B

Question 4.26:

a. (A ∪ B ) ∩ (A ∪ B) = A
c c c c

b. B ∪ (B ∩ B ) = B c

c. A ∪ (B ∩ A ) = A
c c c

d. (A ∪ B ) ∩ (A ∪ B) = A
c

e. C ∪ [C ∪ (C ∩ A)] = C
f. A ∩ [A ∪ (A ∩ B)] = A
g. C ∪ (C ∩ A ∩ B ∩ D) = C c

h. C ∩ (C ∩ A ∩ B ∩ D) = C
c c c

Question 4.27:

a. B ∩ [A ∪ (A c
∩ B)] = B

b. [(A ∩ B )]
c
c
= A ∩ B
c
c
[A ∪ (A ∩ B] ∩ B = A ∩ B
c. c c
c
c c c
(A ∩ C )∪ ) (A ∩ B) ∪ (B ∩ C) = A ∪ B ∪ C

d. (A ∩ C ) ∪ (A ∩ B) ∪ (B ∩ C) = A
c
c c c c c
∪ B ∪ C

e. (A ∩ B) ∪ (B ∩ C) = A ∪ B ∪ C
c
c c

c
[A ∪ (B ∩ C)] ∩ (A ∪ C) = (A ∪ B) ∩ C
f.
(A

g.
c
c c c c c
(A ∩ C ) ∪ (A ∩ B)∪(A ∩ r) = A ∪ C (A ∩ C) ∪ (A ∩ B) ∪ (C ∩ B) = (A ∩ C) ∪ (B

h.
c
c c c c c
(A ∩ C ) ∪ (A ∩ B)∪(A ∩ r) = A ∪ C (A ∩ C) ∪ (A ∩ B) ∪ (C ∩ B) = (A ∩ C) ∪ (B
c c c c c c
A ∪ B ∪ [A ∩ B ∩ C ] = A ∪ B ∪ C
i. c c
(A ∪ C) ∩ (A ∪ B) ∩ (C ∪ B) = (A ∩ B) ∪ (C ∩ A )

j. (A ∪ C) ∩ (A ∪ B) ∩ (C ∪ B) = (A ∩ B) ∪ (C ∩ A
c c
)

k. (A ∩ B) ∩ (A ∪ B) ∩ (B ∪ B) = A
c c c c

Question 4.28:

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)}

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)}
R = {(0, 1), (0, 2), (0, 4), (0, 5), (0, 6), (2, 1), (2, 4), (2, 5), (2, 6),
f.
(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. re exive, antisymmetric, transitive


b. transitive
c. re exive, symmetric, antisymmetric, transitive
d. irre exive, symmetric
e. transitive
f. re exive, 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 re exive, symmetric, antisymmetric, and transitive. Thus it is both a partial
ordering and an equivalence relation.
12.5 CHAPTER FIVE ANSWERS

Question 5.1:

a. x + x = x
b. x + x = 1 ′

c. (x + x ) = 0 ′ ′

d. x + xy = x
e. x + x y = x + y ′

f. x(x + y) = xy

g. x(x) = x
h. x(x + y) = x
i. x(x + y + z) = x
j. xy + xy = x ′

k. (x0 + x ) = x ′ ′

l. xy + x y = y ′

Question 5.2:

a. (x + y )(x + y) = x
′ ′ ′ ′

b. y + (yy ) = y ′

c. x + yx = x
′ ′

d. (x + y )(x + y) = x ′

e. w + [w + (wx)] = w
f. x[x + (xy)] = x
g. w + (wx yz) = w ′

h. w (wxyz) = w
′ ′ ′

Question 5.3:

a. y[x + (x y)] = y ′

b. [(xy ) + x ] = xy
′ ′ ′

c. x y + x(x + y) = x + y

d. (x + xy) y = x y ′ ′

e. xz + x y + (yz) = x + y + z
′ ′ ′ ′ ′ ′

f. (xy) + (yz) = x + y + z
′ ′ ′

g. [x + (yz)](x + z) = (x + y)z ′

h. xz + x y + (xz) = x + z
′ ′ ′ ′ ′

i. xz + x y + zy = xz + yx ′ ′

j. x + y + xyz = x + y + z
′ ′ ′ ′ ′ ′

k. (x + z)(x + y)(z + y) = xy + zx ′ ′

l. (xy) (x + y)(y + y) = x
′ ′ ′ ′

Question 5.4:

a. x y

b. x + y

c. (xy) + z or xy + z ′ ′

d. (x + y)z ′

e. (x + y) + z or x + y + z ′ ′

f. (xy)z or xyz ′

Question 5.5:
a. (xy ) + yorxy + y
′ ′

b. (x + y )y ′

c. (x + y ) + y or x + y ′ ′
+ y

d. (xy )yorxyy ′

Question 5.6:

a. [(x + y)z] ′

b. [(xy) + z] or(xy + z) ′ ′

c. [(xy)z] or (xyz) ′ ′

d. [(x + y) + z]; or (x + y + z) ′

Question 5.7:

a. (z x) + y or z x + y
′ ′

b. (z + x)y′

c. (z x)y or z xy
′ ′

d. (z + x) + y or z + x + y
′ ′

Question 5.8: Diagrams will vary.

Question 5.9: Diagrams will vary.

Question 5.10: Diagrams will vary.

Question 5.11:

a. x y + xy and (x + y )(x + y )
′ ′ ′ ′ ′ ′

b. x y + x y and (x + y)(x + y )
′ ′ ′ ′ ′ ′

c. x y and (x + y)(x + y)(x + y )


′ ′ ′ ′

d. xy + xy and (x + y)(x + y )
′ ′

e. x y + xy and (x + y )(x + y)
′ ′ ′ ′

f. xy and (x + y)(x + y )(x + y) ′ ′

g. x y + x y + xy and x + y
′ ′ ′ ′ ′ ′

h. x y + xy + xy and x + y
′ ′ ′ ′

i. x y + xy and (x + y)(x + y)
′ ′

Question 5.12:
′ ′ ′
x yz + xy z + xyz and
a. ′ ′ ′ ′ ′ ′
(x + y + z)(x + y + z )(x + y + z )(x + y + z)(x + y + z)
′ ′ ′ ′ ′ ′ ′ ′ ′
x y z + x yz + x yz + xy z + xyz and
b. ′ ′ ′ ′ ′ ′
(x + y + z )(x + y + z )(x + y + z )
′ ′ ′ ′ ′ ′ ′
x y z + x yz + xy z + xyz and
c. ′ ′ ′ ′ ′ ′ ′
(x + y + z )(x + y + z )(x + y + z)(x + y + z )
′ ′ ′ ′ ′
x y z + x yz + xyz and
d. ′ ′ ′ ′ ′ ′ ′ ′
(x + y + z )(x + y + z)(x + y + z)(x + y + z )(x + y + z )
′ ′ ′ ′ ′ ′ ′
x y z + x y z + xy z + xyz + xyz and
e. ′ ′ ′ ′
(x + y + z)(x + y + z )(x + y + z)
′ ′ ′ ′ ′ ′
x y z + x y z + x yz + xyx and
f. ′ ′ ′ ′ ′ ′
(x + y + z)(x + y + z)(x + y + z )(x + y + z)

Question 5.13:
x y (x + y)x

a.
0 0 0
0 1 0
1 0 0
1 1 1

x y (x + y )x

b.
0 0 0
0 1 0
1 0 1
1 1 1

x y (x + y)y

c.
0 0 0
0 1 0
1 0 1
1 1 0

x y (x + y)(x + y)

d.
0 0 0
0 1 1
1 0 1
1 1 1

x y (x + y)(x + y)

e.
0 0 0
0 1 1
1 0 0
1 1 1

x y ′
(x + y )(x + y)

f.
0 0 1
0 1 0
1 0 0
1 1 1

x y xx + x(x + y)

g.
0 0 0
0 1 0
1 0 1
1 1 1

x y xy + y(x + y)

h.
0 0 0
0 1 1
1 0 0
1 1 1

x y ′ ′
x y + y (x + y)

i.
0 0 0
0 1 1
1 0 1
x y ′
x y + y (x + y)

1 1 0

x y ′ ′
x y + y (x + y)

j.
0 0 1
0 1 0
1 0 1
1 1 0

x y xy(x + y)

k.
0 0 0
0 1 0
1 0 0
1 1 1

x y ′ ′
x y (x + y )
′ ′

l.
0 0 1
0 1 0
1 0 0
1 1 0

Question 5.14:

x y z x(x + y + z)

a.
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 1

x y z ′
x (x + y + z)

b.
0 0 0 1
0 0 1 1
0 1 0 0
0 1 1 1
1 0 0 0
1 0 1 0
1 1 0 0
1 1 1 0

x y z (x + y)(x + z)

c.
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 1
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 1
x y z ′
(x + y )(x + z)

d.
0 0 0 0
0 0 1 1
0 1 0 0
0 1 1 1
1 0 0 1
1 0 1 1
1 1 0 0
1 1 1 0

x y z ′ ′ ′
(x y + z )(x + y)

e.
0 0 0 1
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 1
1 1 1 0

x y z xy(x + y + z)

f.
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 0
1 1 0 1
1 1 1 1
12.6 CHAPTER SIX ANSWERS

Question 6.1:
For function in Fig. 6.3(a):

a. Domain: X = {1, 2, 3, 4, 5}, Codomain: Y = {a, b, c, d, e}, Range: R = {a, b, c, d, e}


b. Both one-to-one and onto
c. f (1) ≡ c, f (2) ≡ b, f (3) ≡ a, f (4) ≡ e, f (5) ≡ s
d. {(1, c), (2, b), (3, a), (4, e), (5, d)} ⊂ X × Y
For function in Fig. 6.3(b):

a. Domain: X = {1, 2, 3, 4, 5}, Codomain: Y = {a, b, c, d, e}, Range: R = {b, d}


b. Neither one-to-one nor onto
c. f (1) ≡ b, f (2) ≡ d, f (3) ≡ d, f (4) ≡ b, f (5) ≡ s
d. {(1, b), (2, d), (3, d), (4, b), (5, d)} ⊂ X × Y
For function in Fig. 6.3(c):

a. Domain: X = {1, 2, 3, 4, 5}, Codomain: Y = {a, b, c, d, e}, Range: R = {a, b, c, d, e}


b. Both one-to-one and onto
c. f (1) ≡ d, f (2) ≡ e, f (3) ≡ a, f (4) ≡ b, f (5) ≡ c
d. {(1, d), (2, e), (3, a), (4, b), (5, c)} ⊂ X × Y
For function in Fig. 6.3(d):

a. Domain: X = {1, 2, 3, 4, 5, 6, 7}, Codomain: Y = {a, b, c, d, e, f, g}, Range: R = {c, d, f, g}


b. Neither one-to-one nor onto
c. f (1) ≡ c, f (2) ≡ d, f (3) ≡ g, f (4) ≡ f , f (5) ≡ c, f (6) ≡ d, f (7) ≡ f
d. {(1, c), (2, d), (3, g), (4, f), (5, c), (6, d), (7, f)} ⊂ X × Y
For function in Fig. 6.3(e):

a. Domain: X = {1, 2, 3, 4, 5, 6, 7}, Codomain: Y = {a, b, c, d, e, f, g}, Range: R = {a, b, c, d, f, g}


b. Neither one-to-one nor onto
c. f (1) ≡ c, f (2) ≡ d, f (3) ≡ a, f (4) ≡ g, f (5) ≡ b, f (6) ≡ c, f (7) ≡ f
d. {(1, c), (2, d), (3, a), (4, g), (5, b), (6, c), (7, f)} ⊂ X × Y
For function in Fig. 6.3(f):

a. Domain: X = {1, 2, 3, 4, 5, 6, 7}, Codomain: Y = {a, b, c, d, e, f, g}, Range: R = {a, b, c, d, e, f, g}


b. Both one-to-one and onto
c. f (1) ≡ e, f (2) ≡ b, f (3) ≡ a, 4(6) ≡ c, f (5) ≡ s, f (6) ≡ g, f (7) ≡ f
d. f (6) ≡ g
e. {(1, e), (2, b), (3, a), (4, c), (5, d), (6, g), (7, f)} ⊂ X × Y

For function in Fig. 6.3(g):

a. Domain: X ={1, 2, 3, 4, 5, 6, 7}, Codomain: Y = {a, b, c, d, e, f, g}, Range: R ={a, c, d, f}


b. Neither one-to-one nor onto
c. f (1) ≡ d, f (2) ≡ d, f(3)=f, f (4) ≡ f , f (5) ≡ c, f (6) ≡ c, f (7) ≡ a
d. {(1, d), (2, d), (3, f), (4, f), (5, c), (6, c), (7, a)} ⊂ X × Y
For function in Fig. 6.3(h):

a. Domain: X = {1, 2, 3, 4, 5, 6, 7}, Codomain: Y = {a, b, c, d, e, f, g}, Range: R = {a, b, c, d, e, f, g}


b. Both one-to-one and onto
c. f (1) ≡ b, f(2)=c, f (3) ≡ d, f (4) ≡ a, f (5) ≡ f , f (6) ≡ g, f (7) ≡ e
d. {(1, b), (2, c), (3, d), (4, a), (5, f), (6, g), (7, e)} ⊂ X × Y
For function in Fig. 6.3(i):
a. Domain: X ={1, 2, 3, 4, 5, 6, 7}, Codomain: Y ={a, b, c, d, e, f, g}, Range: R ={a, b, c, f}
b. Neither one-to-one nor onto
c. f (1) ≡ b, f(2)=a, f(3)=c, f (4) ≡ b, f (5) ≡ a, f (6) ≡ f , f (7) ≡ f
d. {(1, b), (2, a), (3, c), (4, b), (5, a), (6, f), (7, f)} ⊂ X × Y

Question 6.2:
For function f1:

a. Range: R = {b, c, e}
b. Neither one-to-one nor onto

c. X Y
d. {(1,c),(2,b),(3,c),(4,e)} ⊂ X × Y
For function f2:

a. Range: R = {b, c, d, e}
b. One-to-one but not onto onto

c. X Y
d. {(1,c),(2,d),(3,e),(4,b)} ⊂ X × Y
For function f3:

a. Range: R = {b, d}
b. Neither one-to-one nor onto

c. {(1,b),(2,b),(3,d),(4,d)} ⊂ X × Y

Question 6.3:
For function f1:
a. Range: = R {a,b,c,d,e,f}
b. Onto but not one-to-one

c. {(1,c),(2,d),(3,e),(4,f),(5,a),(6,b),(7,f)} ⊂ X × Y
For function f2:

a. Range: = R {a,b,c,d,e,f}
b. Onto but not one-to-one

c. {(1,a),(2,c),(3,e),(4,b),(5,d),(6,f),(7,d)} ⊂ X × Y
For function f3:

a. Range: = R{b, c, d, e}
b. Neither onto nor one-to-one

c. {(1,b),(2,c),(3,d),(4,e),(5,d),(6,c),(7,b)} ⊂ X × Y

Question 6.4:
For function f1:

a. Range: = R{a,b,c,d}
b. Neither one-to-one nor onto
c. c
d. f (1) ≡ c, f (2) ≡ b, f (3) ≡ d, f (4) ≡ c, f (5) ≡ a
For function f2:

a. Range: = R{a,b,c,d}
b. Neither one-to-one nor onto

c. X Y
d. f(1) = d, f(2) = d, f(3) = c, f(4) = b, f(5) = a
For function f3:

a. Range: = R{a,b,c,d,e}
b. One-to-one and onto

c. X Y
d. f(1) = a, f(2) = d, f(3) = c, f(4) = b, f(5)= e

Question 6.5:
For function f1:

a. Range: = R{a,b,c,d,e,f}
b. One-to-one and onto
c. X Y
d. f(1) = b, f(2) = c, f(3) = a, f(4) = e, f(5) = f, f(6)= d
For function f2:

a. Range: = R{b, e}
b. Neither one-to-one nor onto

c. X Y
d. f(1) = b, f(2) = b, f(3) = b, f(4) = e, f(5) = e, f(6) = e
For function f3:

a. Range: = R{a,b,c,d,e,f}
b. One-to-one and onto

c. X Y
d. f(1) = c, f(2) = d, f(3) = e, f(4) = f, f(5) = a, f(6)= b

Question 6.6:
For function in Fig. 6.4(a):

a. Domain: X = {-3,-2,0,2,4},
Range: Y = {-3,-2,1,2,4}
b. X Y
c. f(-3)=1, f(-2)=2, f(0)=-2, f(2)=4, f(4)=-3
d. {(-3, 1), (-2, 2), (0,-2), (2, 4), (4,-3)} ⊂ X × Y
For function in Fig. 6.4(b):

a. Domain: X = {-4,-3,-2, 0, 2, 3, 4},


Range: Y = {-4,-3,-2, 0, 2, 3, 4}

b. X Y
c. f(-4)=4, f(-3)=-3, f(-2)=2, f(0)=0, f(2)=-2, f(3)=3, f(4)=-4
d. {(-4, 4), (-3,-3), (-2, 2), (0, 0), (2,-2), (3, 3), (4,-4)} ⊂ X × Y
For function in Fig. 6.4(c):

a. Domain: X = {-4,-2, 0, 1, 2, 4},


Range: Y = {-4,-3, 2, 3, 4, 5}

b. X Y
c. f(-4)=2, f(-2)=3, f(0)=-3, f(1)=4, f(2)=-4, f(4)=5
d. {(-4, 2), (-2, 3), (0,-3), (1, 4), (2,-4), (4, 5)} ⊂ X × Y

For function in Fig. 6.4(d):

a. Domain: X = {-3,-2,-1, 0, 1, 2, 3, 4, },
Range: Y = {-4,-3,-2,-1, 0, 1, 2, 3, }
b. X Y
c. f((-3)=-4, f((-2)=-3, f((-1)=-2, f((0)=-1, f((1)=0, f((2)=1, f((3)=2, f((4)=3
d. {(-3,-4), (-2,-3), (-1,-2), (0,-1), (1, 0), (2, 1), (3, 2), (4, 3)} ⊂ X × Y

Question 6.7:

a. f(-3)=-2, f(0)=7, f(4)=19


b. f(-3)=-2, f(0)=7, f(4)=19
c. f(-3)=18, f(0)=6, f(4)=-10
d. f(-3)=0.125, f(0)=1, f(4)=16
e. f(-3)=0, f(0)=0, f(4)=28
f. f(-3)=0, f(0)=-6, f(4)=14
g. f(-3)=48, f(0)=0, f(4)=-36
h. f(-3)=162, f(0)=6, f(4)≈0.074
i. f(-3)=64, f(0)=1, f(4)=225

Question 6.8:

a. not a function
b. function, domain: R, range: [-4,∞)
c. function, domain: R, range: R
d. function, domain: R, range: (-∞, 5]
e. not a function
f. function, domain: R, range: [-4,∞)

Question 6.9:
g ∘ f (1) = g(f (1)) = g(c) = α

g ∘ f (2) = g(f (2)) = g(b) = γ

g ∘ f (3) = g(f (3)) = g(d) = β

g ∘ f (4) = g(f (4)) = g(a) = δ

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


Question 6.10:
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}

Question 6.11:
For function in Fig. 6.3(a):
−1 −1 −1 −1 −1
f (a) = 3, f (b) = 2, f (c) = 1, f (d) = 5, f (e) = 4,
For function in Fig. 6.3(b):
f does not exist
−1

For function in Fig. 6.3(c):


−1 −1 −1 −1 −1
f (a) = 3, f (b) = 4, f (c) = 5, f (d) = 1, f (e) = 1

For function in Fig. 6.3(d):


f does not exist
−1

For function in Fig. 6.3(e):


f does not exist
−1

For function in Fig. 6.3(f):


−1 −1 −1 −1 −1 −1 −1
f (a) = 3, f (b) = 2, f (c) = 4, f (d) = 5, f (e) = 1, f (f ) = 7, f (g) = 6

For function in Fig. 6.3(g):


f does not exist
−1

For function in Fig. 6.3(h):


−1 −1 −1 −1 −1 −1 −1
f (a) = 4, f (b) = 1, f (c) = 2, f (d) = 3, f (e) = 7, f (f ) = 5, f (g) = 6

For function in Fig. 6.3(i):


f does not exist
−1

Question 6.12:
f and f do not exist; f
−1
1
−1
3
−1
2
(b) = 4, f
−1
2
(c) = 1, f
−1
2
(c) = 1, f
−1
2
(d) = 2, f
−1
2
(c) = 3

Question 6.13:
f , f , and f
−1

1
−1

2
−1

3
do not exist

Question 6.14:
f and f do not exist; f
−1

1
−1

2
−1

3
(a) = 1, f
−1

3
(b) = 4, f
−1

3
(c) = 3, f
−1

3
(d) = 2, f
3
−1
(c) = 5

Question 6.15:

a. Yes
b. No
c. Yes
d. No

Question 6.16:

a. f −1
(x) =
x

b. f −1
(x) = x+3
1

c. f −1
(x) =
2−x

d. f −1
(x) =
3x−10

e. f −1
(x) = (x + 4)
2

f. f −1
(x) =
2

x
+ 3
12.7 CHAPTER SEVEN ANSWERS

Question 7.1: There are ve odd decimal digits (1,3,5,7,9) so 55 = 3 125.

Question 7.2: There are four odd octal digits (1,3,5,7) so 45 = 1 025.

Question 7.3: There are eight odd hexadecimal digits (1,3,5,7,9,B,D,F) so 85 = 32 768.

Question 7.4: P 5
32
=
32!

29!
= 32 × 31 × 30 = 29 760

Question 7.5: P 5
20
=
20!

15!
= 20 × 19 × 18 × 17 × 16 = 1 860 480

Question 7.6: ( 20
5
) =
20!

5!15!
= 15 504

Question 7.7:

(8 + 6) × (5 + 9) = 196

Question 7.8: 10 4
+ 10
3
= 11 000

Question 7.9:

a. 5 040
b. 2 520
c. 907 200

Question 7.10: Number of functions: 531 441; Number of one-to-one functions: 60 480

Question 7.11: Number of functions: 14 641; Number of one-to-one functions: 7 920

Question 7.12: C 10
2
⋅ C
10
2
⋅ C
20
3
⋅ C
20
3
= 2 631 690 000

Question 7.13: while-do in step 4: n - 1

Question 7.14: for-do in step 2: n; for-do in step 2.1: n2; for-do in step 2.1: n3

Question 7.15: for-do in step 2: n - 1; while-do in step 2.3: ( n


2
) = 0.5n
2
− 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
12.8 CHAPTER EIGHT ANSWERS

Question 8.1:
1.1 Multiplication in step 2.
1.2 Comparison > in step 2.
1.3 Comparison < in step 2.
1.4 Addition and subtraction in steps 3.1 and 3.2.
1.5 Additions in steps 3.1 and 3.2.1.
1.6 Multiplication in step 3.1.
1.7 Multiplication in step 3.2.
1.8 Multiplication in step 3.1.
1.9 Addition (sum) in step 3.1.
1.10 Comparison = in step 2.1.
1.11 Comparisons = in steps 2.1 and 3.1.
1.12 Comparison = in step 2.1.1.
1.13 Comparison = in step 2.1.1.
1.14 Multiplication i × i in step 3.1.
1.15 Division in step 3.1.
1.16 Comparison < in step 3.1.
1.17 Comparisons (is xi a digit) in step 4.1.

Question 8.2: f (n) = 7n ≤ 7 ⋅ g(n)

Question 8.3: f (n) = 25n + 500 ≤ 25n + 500n = 525n = 525 ⋅ g(n)

Question 8.4: f (n) = 20√n + 40n 2


+ 500nlog 2 (n) ≤ 20n
2
+ 40n
2
+ 500n
2
= 560n
2
so 5g(n) = n
2

Question 8.5: f (n) = 800n 2


+ 40n
3
+ 50log 2 (n) 800n
3
+ 40n
3
+ 50n
3
= 890n
3
so g(n) = n .
3
.

Question 8.6:

a. Algorithm one is faster.


b. Algorithm two is faster.
c. The algorithms are equally fast.
d. Algorithm one is faster.
e. Algorithm two is faster.
f. Algorithm one is faster.
g. Algorithm one is faster.
h. The algorithms are equally fast.

Question 8.7:

a. Algorithm one is faster.


b. Algorithm two is faster.
c. Algorithm two is faster.
d. Algorithm one is faster.
e. The algorithms are equally fast.
f. Algorithm two is faster.
g. Algorithm two is faster.

Question 8.8: √n < n 1.5


< 10
n

Question 8.9: n
n
2 n 2
log 2 (n) < 2 < 2

Question 8.10: log 2


(n) < n < nlog 2 (n) so 2
log (n)
2
< 2
n
< 2
nlog (n)
2
Question 8.11: The dominant operation is the comparison in step 4.1 and the time complexity 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) = n 3

.
12.9 CHAPTER NINE ANSWERS

Question 9.1:
For graph in Fig. 9.9(a):

V = {A, B, C, D, E}

a.
ε = {AB, AE, AE, BD, BD, CE, DE}

b. The graph's order is ve.


c. deg(A)=3, deg(B)=3, deg(C)=1, deg(D)=3, deg(E)=4
d. Vertices A and D are adjacent to vertex B.
e. Edge CE is incident to vertex C.
f. There are no loops.
g. There are four parallel edges; {AE, AE, BD, BD} .
h. There is one bridge; CE.
For graph in Fig. 9.9(b):

V = {A, B, C, D}

a.
ε = {AA, AB, BD, BD, BD, CE, CD}

b. The graph's order is four.


c. deg(A)=3, deg(B)=4, deg(C)=3, deg(D)=4
d. Vertices A and D are adjacent to vertex B.
e. Edges CC and CD are incident to vertex C.
f. There are two loops; AA and CC .
g. There are three parallel edges; BD, BD, and BD.
h. There are two bridges; AB and CD.

For graph in Fig. 9.9(c):

V = {A, B, C, D, E}

a.
ε = {AA, AB, BD, CD, CE, CE, CE, DE}

b. The graph’s order is ve.


c. deg(A)=3, deg(B)=2, deg(C)=4, deg(D)=3, deg(E)=4
d. Vertices A and D are adjacent to vertex B.
e. Edges CD, CE, CE, CE are incident to vertex C.
f. There is one loops; AA.
g. There are three parallel edges; CE, CE, and CE.
h. There are two bridges; AB and BD.
For graph in Fig. 9.9(d):

V = {A, B, C, D, E, F }

a.
ε = {AB, AC, BC, DE, DF , EF }

b. The graph’s order is six.


c. deg(A)=2, deg(B)=2, deg(C)=2, deg(D)=2, deg(E)=2, deg(F)=2
d. Vertices A and C are adjacent to vertex B.
e. Edges AC and BC are incident to vertex C.
f. There are no loops.
g. There are no parallel edges.
h. This graph is not connected and therefore cannot have a bridge.

Question 9.2:
a. Not adjacent
b. Not adjacent
c. Adjacent
d. Adjacent
e. Adjacent
f. Not adjacent
g. Adjacent
h. Adjacent
i. Not adjacent

Question 9.3: The graph is not connected; it has two components.

Question 9.4: deg(A) = 4, deg(B) = 2, deg(C) = 3, deg(D) = 5, deg(E) = 2; there are two loops, AA and DD; there
are no parallel edges; there are no bridges

Question 9.5: deg(A) = 4, deg(B) = 3, deg(C) = 2, deg(D) = 4, deg(E) = 2, deg(F) = 3; There are two loops, AA

and F F ; there are two parallel edges, BD and BD; there are three bridges, AD, AE, and EF

Question 9.6: Three components

Question 9.7: Nine edges

Question 9.8: Eighteen edges

Question 9.9: Degree four

Question 9.10: Degree ve

Question 9.11:

a. Neither
b. Semi-Eulerian
c. Semi-Eulerian
d. Neither

Question 9.12:

a. Neither
b. Semi-Eulerian
c. Eulerian
d. Semi-Eulerian

Question 9.13: Semi-Eulerian

Question 9.14: One possible path: A − B − C − C − D − F − D − B − E − A − E − C

Question 9.15: One possible path: B − C − D − B − D − A − A − E − F − F

Question 9.16: The trace of the modi ed Fleury’s algorithm is given below:
Step current_path insertion_point e v new_path unused_edges
2 A - - - - {e1 , e2 , e3 , e4 , ee0 , e1 e2 }
3.1 A A - - - {e1 , e2 , e3 , e4 , ee0 , e1 e2 }
3.2 A A - A A {e1 , e2 , e3 , e4 , ee0 , e1 e2 }
3.3.1-4 A A e1 B Ae1 B {e2 , e3 , e4 , e5 , e6 }
3.3.1-4 A A e3 C Ae1 Be3 C {e2 , e4 , e5 , e6 }
Step

3.4
3.1
3.2

c.

d.

a.

⎢⎥
3.3.1-4

3.3.1-4
3.3.1-4
3.3.1-4
3.4

⎡0


1

Question 9.19:

⎡0


1

0
1

0
0

0
current_path

0⎦
A
Ae1 Be3 Ce2 A
Ae1 Be3 Ce2 A
Ae1 Be3 Ce2 A
Ae1 Be3 Ce2 A
Ae1 Be3 Ce2 A
Ae1 Be3 Ce2 A
Ae1 Be4 Ce6 De5 Be3 Ce2 A

Question 9.17: The Euler circuit given by the modi ed Fleury’s algorithm is


0

0⎦

or
0

or


3

0


1

1
0

1

1
1

0
insertion_point


1

1
A
A
B
B
B
B
B
B

The Euler circuit given by the modi ed Fleury’s algorithm is Ae1Be4Ce6De5Be3Ce2A, which we more normally
write as A − B − C − D − B − C − A or simply as ABCDBCA.

Ae1Be4De8Ee9De6Ce7Ee5Be3Ce2A, 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:

a.

b.
⎡0


⎡1


1

0
0

1
0

1


2

or

or


1

0


0

0
0

1
0


0

1 0

0


0

0
0

0


e
e2
e2
e2
e2
e4
e6
e5
e5

or


1

0
v
B
B
B
B
C
D
B
B

0
0

3
new_path
Ae1 Be3 Ce2 A
Ae1 Be3 Ce2 A
Ae1 Be3 Ce2 A

1
B
Be4 C
Be4 Ce6 D
Be4 Ce6 De5 B
Be4 Ce6 De5 B

0


unused_edges
{e4 , e5 , e6 }
{e4 , e5 , e6 }
{e4 , e5 , e6 }
{e4 , e5 , e6 }
{e5 , e6 }
{e5 }
{}
{}
⎢⎥


b.

c.

d.

0
⎡0


⎡0


⎡0

Question 9.20:
⎡ 1 0 0 1 1⎤
0 1

Question 9.21:
⎡0 1 1 0 0
2

Question 9.22:
⎡0 0 0 1 1


0 1

0
1

0
1

0

0

or

0⎦

function is given by f(n) = n3.

Question 9.24:


0

or

or



1
or

or

or


0

0




0

0
0

0
0

0


0

1
0

1



Question 9.23: The dominant operation is the Boolean addition in step 3.1.1.1.1. Thus the time complexity
⎢⎥
⎡1
1

⎣0
1

0
1

0
0

Question 9.25:
⎡1 1 0 0 0
0

⎣0
1

1
0

1
0

0
0

0
0

1


B.10 CHAPTER TEN ANSWERS

Question 10.1: T has 101 vertices. All 100 edges are bridges.

Question 10.2: T has 99 edges. All 99 edges are bridges.

Question 10.3: The new graph is disconnected and there are three components.

Question 10.4: There is one path with no repeated vertices or edges between v1 and v20. There is one path with no
repeated vertices or edges between v4 and v17. There is one path with no repeated vertices or edges between vi
and vj where i ≠ j.

Question 10.5: The new graph has one cycle so it is no longer a tree.

Question 10.6: The vertex vi has one parent and either one or two children.

Question 10.7: The vertex vi has one parent and two children.

Question 10.8: The greatest number of leaves that T can have is 99. The least number of leaves that T can have
is two.

Question 10.9: Any drawn graph should be isomorphic to:

Question 10.10:

a. three; AB and DE
b. four; AB and EF
c. four; AC, BD, EG, and F H

Question 10.11:

a. 6 × 4 = 24; F G
b. 4 × 4 = 16; F G
c. (5 × 4) − 1 = 19; none

Question 10.12: Using Prim’s algorithm and starting at vertex A we get the following minimal spanning tree:

Question 10.13: Notice that in step 3.1 of Prim’s algorithm we use the rst edge in the edge set with minimal
weight incident to exactly one vertex in T . This means the order in which the edges are given in ε matter. Using
Prim’s algorithm and starting at vertex A we get the following minimal spanning tree:
Question 10.14: The minimal spanning tree given by Prim’s algorithm has vertex set
andedgeset

Question 10.15: Notice that in step 3.2 of Dijkstra’s algorithm we use the rst edge in the edge set for which the
required condition is satis ed. This means the order in which the edges are given in ε matter. Using Dijkstra’s
algorithm we get the following minimal spanning tree:

Question 10.16: Using Dijkstra’s algorithm we get the following minimal spanning tree. Notice how different
this spanning tree is from the spanning tree given in question 10.12.
Index

addition (Boolean algebra), 90


addition principle, 133
adjacency matrix, 184
adjacent vertices, 169
algorithm, 1
algorithmic complexity assumptions, 150
anagram, 143
and (connective), 44
and gate, 96
assignment, 1
axioms (Boolean algebra), 90

base, 19
binary numbers, 19
binary relation, 76
binary tree, 199
Boolean algebra, 90
bridge, 175

cardinality, 68
Cartesian product, 70,, 114
child (of vertex), 199
digital circuits, 174
codomain, 111
coef cients, 19
combination, 141
complement, 67
complement (Boolean algebra), 90
complete graph, 171
components of graph, 169
composite function, 122
composition (functions), 122
compound statements, 44
conditional controls, 2
connected graph, 169
connectives, 44
contradiction, 55
contrapositive, 57
control structures, 2
counting numbers, 62
cycle, 197

decimal numbers, 19
decrement, 7
degree of vertex, 173
DeMorgan's laws, 56
difference (of sets), 68
digits, 19
directed graph, 171
disconnected graph, 169
disjoint (sets), 67
div, 30
domain, 111
dominant operation, 151
dual expression (Boolean algebra), 90
duality principle (Boolean algebra), 91

edge, 53
edge set, 170
element (of set), 61
empty set, 65
enumerated form (of set), 62
equivalence relation, 81
Euler circuit, 176
Euler path, 176
Eulerian graph, 176
evaluating functions, 118
exclusive-or, 47
expanded form, 19
expression tree, 52

factorial, 8,, 8,, 139


Fleury's algorithm, 177
Fleury's modi ed algorithm, 182
oor, 33
oor function, 33
for-do, 2
frac, 33
fractional numbers, 22
function, 111,, 114

gate, 96
graph, 169
greedy algorithm, 202

hexadecimal numbers, 21

identity function, 125


if-and-only-if (connective), 44
if-then, 2
if-then (connective), 44
if-then-else, 2
image, 111,, 114
implies (connective), 44
incident, 169
inclusive-or, 44,, 47
increment, 4
induction, 198
integer numbers, 62
intersection, 67
inverse function, 125
is-equivalent-to (connective), 44
isolated vertex, 173
isomorphic graphs, 172

laws of logic, 58
laws of set theory, 74
leaf, 198,, 199
logical equivalence, 56
logical expressions, 44
loop, 170
loop controls, 2

matrix, 183
minimal distance spanning tree, 204
minimal spanning tree, 202
mod, 30
multiplication (Boolean algebra), 90
multiplication principle, 135

nand gate, 97
natural numbers, 62
nest, 3
vertex, 53,, 169
nor gate, 98
not (connective), 44
not gate, 96
null graph, 169
null set, 65

octal numbers, 21
one variable functions, 118
one-to-one, 117
onto, 117
or (connective), 44
or gate, 97
order of graph, 169
ordered n-tuple, 70
paradox, 44
parallel edges, 170
parent (of vertex), 199
partial ordering, 81
partition, 81,, 82
path, 174
path length, 174
permutation, 138
power set, 68
predicate, 62
predicate form (of set), 62
Prim's algorithm, 202
product-of-sum (Boolean algebra), 104
proper subset, 64
proposition, 43
pseudocode, 1

quotient, 30

range, 114
rational numbers, 62
reachability matrix, 188
reachable, 188
real numbers, 62
real-valued functions, 118
relation, 76
remainder, 30
repeat-until, 2
return, 10
root, 199
rooted tree, 199

semi-Eulerian graph, 177


set, 61
simple graph, 170
spanning tree, 200
square matrix, 184
subset, 64
sum-of-products (Boolean algebra), 103
symmetric matrix, 185

tautology, 55
time-complexity function, 150
trace, 5
tree, 198
truth table, 46,, 52
truth-value, 43

union, 67
universal set, 66

Venn diagrams, 71
vertex, 169
vertex set, 170
vertical line test, 119

Warshall's algorithm, 188


weight (of edge), 172
weighted graph, 172
well-de ned, 61
while-do, 2
whole numbers, 19,, 62

xnor gate, 98
xor gate, 98

You might also like