Tải bản đầy đủ (.pdf) (680 trang)

Tài liệu Discrete mathematics for computer science

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (20.77 MB, 680 trang )



1

David Liben-Nowell
Department of Computer Science
Carleton College

Discrete Mathematics
for Computer Science
or

(A Bit of) The Math that
Computer Scientists
Need to Know


VP AND EDITORIAL DIRECTOR
SENIOR DIRECTOR
ACQUISITIONS EDITOR
EDITORIAL MANAGER
CONTENT MANAGEMENT DIRECTOR
CONTENT MANAGER
SENIOR CONTENT SPECIALIST
PRODUCTION EDITOR
PHOTO RESEARCHER
COVER PHOTO CREDIT

Laurie Rosatone
Don Fowley
Linda Ratts


Gladys Soto
Lisa Wojcik
Nichole Urban
Nicole Repasky
Rajeshkumar Nallusamy
Billy Ray
© slobo/Getty Images, Inc.

This book was set in TeXGyrePagella 10/12 by SPi Global and printed and bound by Strategic Content Imaging.
This book is printed on acid free paper. ∞
Founded in 1807, John Wiley & Sons, Inc. has been a valued source of knowledge and understanding for more
than 200 years, helping people around the world meet their needs and fulfill their aspirations. Our company is
built on a foundation of principles that include responsibility to the communities we serve and where we live
and work. In 2008, we launched a Corporate Citizenship Initiative, a global effort to address the environmental,
social, economic, and ethical challenges we face in our business. Among the issues we are addressing are carbon
impact, paper specifications and procurement, ethical conduct within our business and among our vendors,
and community and charitable support. For more information, please visit our website: www.wiley.com/go/
citizenship.
Copyright © 2018, John Wiley & Sons, Inc. All rights reserved. No part of this publication may be reproduced,
stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying,
recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States
Copyright Act, without either the prior written permission of the Publisher, or authorization through payment
of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA
01923 (Web site: www.copyright.com). Requests to the Publisher for permission should be addressed to the
Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, (201) 748-6011,
fax (201) 748-6008, or online at: www.wiley.com/go/permissions.
Evaluation copies are provided to qualified academics and professionals for review purposes only, for use
in their courses during the next academic year. These copies are licensed and may not be sold or transferred
to a third party. Upon completion of the review period, please return the evaluation copy to Wiley. Return
instructions and a free of charge return shipping label are available at: www.wiley.com/go/returnlabel. If you

have chosen to adopt this textbook for use in your course, please accept this book as your complimentary desk
copy. Outside of the United States, please contact your local sales representative.
ISBN: 978-1-118-06553-2 (PBK)
ISBN: 978-1-119-07073-3 (EVALC)
Library of Congress Cataloging in Publication Data:
Liben-Nowell, David, author.
Title: Discrete mathematics for computer science / by David Liben-Nowell.
Description: Hoboken, NJ : John Wiley & Sons, 2017. | Includes index. |
Identifiers: LCCN 2017025007 (print) | LCCN 2017035974 (ebook) | ISBN
9781119397199 (pdf) | ISBN 9781119397113 (epub) | ISBN 9781118065532 (pbk.)
Subjects: LCSH: Computer science—Mathematics.
Classification: LCC QA76.9.M35 (ebook) | LCC QA76.9.M35 L53 2017 (print) |
DDC 004.01/51—dc23
LC record available at />The inside back cover will contain printing identification and country of origin if omitted from this page. In
addition, if the ISBN on the back cover differs from the ISBN on this page, the one on the back cover is correct.


To MDSWM, with never-ending appreciation, and
in loving memory of my grandfather, Jay Liben, who
brought more joy, curiosity, and kvetching to this
world than anyone else I know.



Contents

1

On the Point of this Book


2

Basic Data Types

3

101

201
202

2.1

Why You Might Care

2.2

Booleans, Numbers, and Arithmetic

2.3

Sets: Unordered Collections

2.4

Sequences, Vectors, and Matrices: Ordered Collections

2.5

Functions


2.6

Chapter at a Glance

Logic

203

222

253
270

301
302

3.1

Why You Might Care

3.2

An Introduction to Propositional Logic

3.3

Propositional Logic: Some Extensions

3.4


An Introduction to Predicate Logic

331

3.5

Predicate Logic: Nested Quantifiers

349

3.6

Chapter at a Glance

362

303
317

237


6

4

5

6


7

Proofs

401
402

4.1

Why You Might Care

4.2

Error-Correcting Codes

4.3

Proofs and Proof Techniques

4.4

Some Examples of Proofs

4.5

Common Errors in Proofs

4.6


Chapter at a Glance

403
423
441
458

469

501

Mathematical Induction

502

5.1

Why You Might Care

5.2

Proofs by Mathematical Induction

5.3

Strong Induction

5.4

Recursively Defined Structures and Structural Induction


5.5

Chapter at a Glance

503

521

Analysis of Algorithms

546

601
602

6.1

Why You Might Care

6.2

Asymptotics

6.3

Asymptotic Analysis of Algorithms

6.4


Recurrence Relations: Analyzing Recursive Algorithms

6.5

Recurrence Relations: The Master Method

6.6

Chapter at a Glance

Number Theory

533

603
617

657

701
702

7.1

Why You Might Care

7.2

Modular Arithmetic


7.3

Primality and Relative Primality

7.4

Multiplicative Inverses

7.5

Cryptography

7.6

Chapter at a Glance

703
734

745
756

717

647

631


7


8

9

801

Relations

802

8.1

Why You Might Care

8.2

Formal Introduction

8.3

Properties of Relations: Reflexivity, Symmetry, and Transitivity

8.4

Special Relations: Equivalence Relations and Partial/Total Orders

8.5

Chapter at a Glance


803

850

901

Counting

902

9.1

Why You Might Care

9.2

Counting Unions and Sequences

9.3

Using Functions to Count

9.4

Combinations and Permutations

9.5

Chapter at a Glance


903

926
944

965

1001

10 Probability

1002

10.1

Why You Might Care

10.2

Probability, Outcomes, and Events

10.3

Independence and Conditional Probability

10.4

Random Variables and Expectation


10.5

Chapter at a Glance

11 Graphs and Trees

1101
1102

Why You Might Care

11.2

Formal Introduction

11.3

Paths, Connectivity, and Distances

11.4

Trees

11.5

Weighted Graphs

11.6

Chapter at a Glance


1103

1147

1201

1041

1067

11.1

12 Index

1005

1164
1177

1129

1021

818
833



List of Computer Science Connections


Chapter 2: Basic Data Types
217

Integers and ints, Reals and floats

218

Computing Square Roots, and Not Computing Square Roots
Set Building in Languages

233

234

Clustering

The Vector Space Model
Rotation Matrices

248

249

Hash Tables and Hash Functions

267

Chapter 3: Logic
Natural Language Processing, Ambiguity, and Truth


314

Computational Complexity, Satisfiability, and $1,000,000

326

Short-Circuit Evaluation, Optimization, and Modern Compilers
Game Trees, Logic, and Winning Tic-Tac(-Toe)

344

Nonlocal Variables and Lexical vs. Dynamic Scoping
Gödel’s Incompleteness Theorem
Currying

357

346

345

327


10

Chapter 4: Proofs
Reed–Solomon Codes


418
437

Are Massive Computer-Generated Proofs Proofs?
Paul Erdős, “The Book,” and Erdős Numbers

438

Cryptography and the Generation of Prime Numbers

454

Other Uncomputable Problems (That You Might Care About)
The Cost of Missing Proofs: Some Famous Bugs in CS

455

464

Chapter 5: Mathematical Induction
517

Loop Invariants

Triangulation, Computer Graphics, and 3D Surfaces
Max Heaps

528

529


Grammars, Parsing, and Ambiguity

543

Chapter 6: Analysis of Algorithms
Moore’s Law

613

Multitasking, Garbage Collection, and Wall Clocks
Time, Space, and Complexity
AVL Trees

627

628

643

Divide-and-Conquer Algorithms and Matrix Multiplication

655

Chapter 7: Number Theory
Converting Between Bases, Binary Representation, and Generating Strings
Secret Sharing

730


Error Correction with Reed–Solomon Codes
Miller–Rabin Primality Test
Diffie–Hellman Key Exchange

742
753

731

714


11

Chapter 8: Relations
Relational Databases

815

Regular Expressions

830

Deterministic Finite Automata (DFAs)

846
847

The Painter’s Algorithm and Hidden-Surface Removal


Chapter 9: Counting
Running out of IP addresses, and IPv6

919
920

A Lower Bound for Comparison-Based Sorting

Infinite Cardinalities (and Problems that Can’t Be Solved by Any Program)
Lossy and Lossless Compression

938
959

Brute Force Algorithms and Dynamic Programming
960

The Enigma Machine and the First Computer

Chapter 10: Probability
1016

Quantum Computing

Information, Charles Dickens, and the Entropy of English
Speech Recognition, Bayes’ Rule, and Language Models
Bayesian Modeling and Spam Filtering

1036


1037

A Randomized Algorithm for Finding Medians
The Monte Carlo Method

1017

1060

1062

Chapter 11: Graphs and Trees
Degree Distributions and the Heavy Tail

1123

Graph Drawing, Graph Layouts, and the 9/11 Memorial
The Bowtie Structure of the Web
Garbage Collection

1142

1143

Directed Graphs, Cycles, and Kidney Transplants
Binary Search Trees

1160

Random Walks and Ranking Web Pages


1174

1159

1124

937



Acknowledgements
Would thou hadst less deserved,
That the proportion both of thanks and payment
Might have been mine! only I have left to say,
More is thy due than more than all can pay.
William Shakespeare (1564–1616)
The Scottish Play

To everyone who has helped, directly and indirectly, with everything over these last
years—these words cannot adequately convey my thanks, but at least they’re a start:
thank you!
I owe special thanks to a very long list of generous and warm people—many more
than I can mention here—for advice and kindness and support, both technical and
emotional, as this book came into being. For those whom I haven’t named by name,
please know that it’s only because I have gotten such great support from so many
people, and I hope that you’ll consider this sentence the promise that, when we next
see each other, the first round’s on me. While I’m leaving out the names of the many
people who have helped make my life happy and fulfilling while I’ve been working on
this book, I do want to give specific thanks to a few people:

I want to thank my colleagues—near and far, including many who are not just colleagues but also dear friends and beloved family members—for their wisdom and patience, for answering my endlessly annoying questions, and for conversations that led
to examples or exercises or bug fixes or the very existence of this entire book (even if
you didn’t know that’s what we were talking about at the time): Eric Alexander, Tanya
Berger-Wolf, Kelly Connole, Amy Csizmar Dalal, Josh Davis, Roger Downs, Laura
Effinger-Dean, Eric Egge, Adriana Estill, Andy Exley, Alex Freeman, Sherri Goings,
Jack Goldfeather, Deanna Haunsperger, Pierre Hecker, David Huyck, Sue Jandro,
Sarah Jansen, Iris Jastram, Jon Kleinberg, Carissa Knipe, Mark Krusemeyer, Jessica
Leiman, Lynn Liben, Jadrian Miles, Dave Musicant, Gail Nelson, Rich Nowell, Layla
Oesper, Jeff Ondich, Sam Patterson, Anna Rafferty, Alexa Sharp, Julia Strand, Mike Tie,
Zach Weinersmith, Tom Wexler, Kevin Woods, Jed Yang, and Steve Zdancewic.
I also owe my appreciation to Don Fowley, Bryan Gambrel, Beth Golub, Jessy Moor,
Anna Pham, Sondra Scott, and Gladys Soto at Wiley. Thanks to Judy Brody for relentless and efficient pursuit of permissions (from many different people and publishers)


14

to use the quotes that appear as epigraphs throughout the book. And thanks as well to
the many insightful reviewers of previous drafts of this material. So many times I got
chapter reviews back and put them aside in a huff, only to come back to the reviewers’
comments months later and realize that their suggestions were exactly right. (And, to
be clear: blame me, not them, for the errors that I’m sure remain.)
I specifically want to thank Eric Alexander, Laura Biester, Josh Davis, Charlotte
Foran, Jadrian Miles, Dave Musicant, Layla Oesper, Anna Rafferty, Jed Yang, and the
Carleton CS 202 students from 2013–2017 for their willingness to work with early,
and buggy, drafts of this book. And thanks to those and many other students at
Carleton for their patience, and for sending their comments and suggestions for
improvements—in particular: Hami Abdi, David Abel, Alexander Auyeung, Andrew
Bacon, Kharmen Bharucha, John Blake, Caleb Braun, Macallan Brown, Adam Canady,
Noah Carnahan, Yitong Chen, Jinny Cho, Leah Cole, Katja Collier, Lila Conlee, Eric
Ewing, Greg Fournier, Andy Freeland, Emma Freeman, Samuel Greaves, Reilly Hallstrom,

Jacob Hamalian, Sylvie Hauser, Jack Hessel, Joy Hill, Matt Javaly, Emily Johnston,
Emily Kampa, Carlton Keedy, Henry Keiter, Jonathan Knudson, Julia Kroll, Brennan
Kuo, Edward Kwiatkowski, Dimitri Lang, Tristan Leigh, Zach Levonian, Daniel Levy,
Rhys Lindmark, Gordon Loery, David Long, Robert Lord, Inara Makhmudova, Elliot
Mawby, Javier Moran Lemus, Sean Mullan, Micah Nacht, Justin Norden, Laurel Orr,
Raven Pillmann, Josh Pitkofsky, Matthew Pruyne, Nikki Rhodes, Will Schifeling,
Colby Seyferth, Alex Simonides, Oscar Smith, Kyung Song, Frederik Stensaeth, Patrick
Stephen, Maximiliano Villarreal, Alex Voorhees, Allie Warren, Ben Wedin, Michael
Wheatman, Jack Wines, Christopher Winter, and Andrew Yang.
This book would not have been possible without the support of Carleton College,
not only for the direct support of this project, but also for providing a wonderfully
engaging place to make my professional home. When I started at Carleton, my friends
and family back east thought that moving to Minnesota (the frontier!) was nothing less
than a sign that I had finally lost it, and I have to admit that I thought they had a point.
But it’s been a fabulous place to have landed, with great friends and colleagues and
students—the kind who don’t let you get away with anything, but in a good way.
Some of the late stages of the work on this book occurred while I was visiting the
University of Cambridge. Thanks to Churchill College and the Computer Laboratory,
and especially to Melissa Hines and Cecilia Mascolo, for their hospitality and support.
And my thanks to the somewhat less formal host institutions that have fueled this
writing: Brick Oven Bakery, Cakewalk, Goodbye Blue Monday, Tandem Bagels, The
Hideaway (Northfield, MN); Anodyne, Blue Moon, Bull Run, Caffetto, Common Roots,
Espresso Royale, Isles Bun & Coffee, Keen Eye, Plan B, Precision Grind, Reverie, Spyhouse, Sebastian Joe’s, The Beat, The Nicollet, The Purple Onion, Turtle Bread Company, Uncommon Grounds, Urban Bean (Minneapolis, MN); Ginkgo, Grand Central, Kopplin’s (St. Paul, MN); Collegetown Bagels (Ithaca, NY); Slave to the Grind
(Bronxville, NY); Bloc Eleven, Diesel Cafe (Somerville, MA); Lyndell’s (Cambridge,
MA); Tryst (Washington, DC); Hot Numbers, Espresso Library (Cambridge, England);
and various Starbucks, Caribous, and Dunn Brothers.


15


And, last but certainly not least, my deepest gratitude to my friends and family for
all your help and support while this project has consumed both hours and years. You
know who you are, and I hope you also know how much I appreciate you. Thank you!

David Liben-Nowell
Northfield, MN
May 2017
PS: I would be delighted to receive any comments or suggestions from readers. Please
don’t hesitate to get in touch.



Credits
This book was typeset using LATEX, and I produced all but a few figures from scratch
using a combination of PSTricks and TikZ. The other figures are reprinted with permission from their copyright holders. The illustrations that open every chapter were
drawn by Carissa Knipe (), who was a complete delight to
work with—both on these illustrations and when she was a student at Carleton. I took
the photograph of a house in Figure 2.48 myself. Figure 4.5 (the Therac-25 diagram)
is reproduced from Nancy Leveson’s book Safeware: System Safety and Computers with
permission from Pearson Education. Figure 4.27 (a poem proving the undecidability
of the Halting Problem) is reproduced with permission from Geoffrey K. Pullum. Figure 5.22 (triangulations of a rabbit) is reproduced from a paper by Tobias Isenberg,
Knut Hartmann, and Henry König with permission from the Society for Modeling and
Simulation International (SCS). Figure 11.15 (a map of some European train routes) is
reproduced with permission from RGBAlpha/Getty Images.1

For their kind permission to use quotes that appear as epigraphs in sections throughout the book, thanks to:
Kurt Vonnegut, p. 102. Excerpt from Hocus Pocus by Kurt Vonnegut, copyright ©1990
by Kurt Vonnegut. Used by permission of G. P. Putnam’s Sons, an imprint of Penguin Publishing Group, a division of Penguin Random House LLC. All rights reserved. Any third party use of this material, outside of this publication, is prohibited. Interested parties must apply directly to Penguin Random House LLC for
permission.
Pablo Picasso, p. 203. ©2017 Estate of Pablo Picasso / Artists Rights Society (ARS), New

York. Reprinted with permission.
Laurence J. Peter, p. 317. Reprinted with permission of the estate of Laurence J. Peter.
Carl Sagan, p. 331. From Broca’s Brain: Reflections on the Romance of Science, ©1979 Carl
Sagan. Reprinted with permission from Democritus Properties, LLC.
Peter De Vries, p. 349. Copyright ©1967 by Peter De Vries. Reprinted by permission of
Curtis Brown, Ltd. All rights reserved.

1
Nancy Leveson.
Safeware: System
Safety and Computers. Pearson
Education, Inc.,
New York, 1995; Tobias Isenberg, Knut
Hartmann, and
Henry König. Interest value driven
adaptive subdivision. In Simulation
and Visualisation
(SimVis), pages
139–149. SCS European Publishing
House, 2003; and
Geoffrey K. Pullum.
Scooping the loop
snooper: A proof
that the halting
problem is undecidable. Mathematics
Magazine, 73(4):319–
320, 2000. Used
by permission of
Geoffrey K. Pullum.



18

Edna St. Vincent Millay, p. 521. Edna St. Vincent Millay, excerpt from a letter to Arthur
Davidson Ficke (October 24, 1930) from Letters of Edna St. Vincent Millay, edited by
Allan Ross Macdougall, ©1952 by Norma Millay Ellis. Reprinted with the permission of The Permissions Company, Inc., on behalf of Holly Peppe, Literary Executor,
The Millay Society, www.millay.org.
George C. Marshall, p. 533. Reprinted with permission of the George C. Marshall Foundation.
Peter Drucker, p. 602. Reprinted with permission of the Drucker 1996 Literary Works
Trust.
Bob Dylan, p. 603. Lyrics from Bob Dylan’s “Don’t Think Twice, It’s All Right” (1963).
Copyright ©1963 by Warner Bros. Inc.; renewed 1991 by Special Rider Music. All
rights reserved. International copyright secured. Reprinted by permission.
Mario Andretti, p. 617. Printed with permission of Sports Management Network, Inc.
E. B. White, p. 631. E. B. White / The New Yorker; ©Conde Nast. The quote originally
appeared in the Notes and Comment section of the July 3, 1943 issue of The New
Yorker, “The 40s: The Story of a Decade.” Reprinted with permission.
Charles de Gaulle, p. 647. © Editions Plon. Reprinted with permission.
W. H. Auden, p. 703. “Notes on the Comic” from The Dyer’s Hand and Other Essays by
W. H. Auden, copyright ©1948, 1950, 1952, 1953, 1954, 1956, 1957, 1958, 1960, 1962
by W. H. Auden. Used by permission of Random House, an imprint and division
of Penguin Random House LLC. All rights reserved. Any third party use of this
material, outside of this publication, is prohibited. Interested parties must apply
directly to Penguin Random House LLC for permission.
Bill Watterson, p. 833. Quote from a Calvin & Hobbes cartoon; reprinted with permission from Universal Uclick.
Tom Lehrer, p. 926. Lyrics from “Poisoning Pigeons In The Park” reprinted with permission from Maelstrom Music/Tom Lehrer.
Dick Cavett, p. 1021. Reprinted with permission from Dick Cavett.
Tom Stoppard, p. 1108. Excerpts from Rosencrantz and Guildenstern Are Dead, copyright
© 1967 by Tom Stoppard. Used by permission of Grove/Atlantic, Inc. Any third
party use of this material, outside of this publication, is prohibited.

Marshall Dodge and Robert Bryan, p. 1129. From “Which Way to Millinocket?,” Bert and I
(1958). Reprinted with permission from Islandport Press, Inc.


1
On the Point of this Book

In which our heroes decide, possibly encouraged by a requirement for
graduation, to set out to explore the world.


102

CHAPTER 1. ON THE POINT OF THIS BOOK

Why You Might Care
Just because some of us can read and write and do a
little math, that doesn’t mean we deserve to conquer
the Universe.
Kurt Vonnegut (1922–2007)
Hocus Pocus (1990)

This book is designed for an undergraduate student who has taken a computer science class or three—most likely, you are a sophomore or junior prospective or current
computer science major taking your first non-programming-based CS class. If you
are a student in this position, you may be wondering why you’re taking this class (or
why you have to take this class!). Computer science students taking a class like this one
sometimes don’t see why this material has anything to do with computer science—
particularly if you enjoy CS because you enjoy programming.
I want to be clear: programming is awesome! I get lost in code all the time—let’s
not count the number of hours that I spent writing the code to draw the fractals in

Figure 5.1 in LATEX, for example. (LATEX, the tool used to typeset this book, is the standard typesetting package for computer scientists, and it’s actually also a full-fledged, if
somewhat bizarre, programming language.)
But there’s more to CS than programming. In fact, many seemingly unrelated problems rely on the same sorts of abstract thinking. It’s not at all obvious that an optimizing compiler (a program that translates source code in a programming language like C
into something directly executable by a computer) would have anything important in
common with a program to play chess perfectly. But, in fact, they’re both tasks that are
best understood using logic (Chapter 3) as a central component of any solution. Similarly, filtering spam out of your inbox (“given a message m, should m be categorized as
spam?”) and doing speech recognition (“given an audio stream s of a person speaking
in English, what is the best ‘transcript’ reflecting the words spoken in s?”) are both
best understood using probability (Chapter 10).
And these, of course, are just examples; there are many, many ways in which we
can gain insight and efficiency by thinking more abstractly about the commonalities of
interesting and important CS problems. That is the goal of this book: to introduce the
kind of mathematical, formal thinking that will allow you to understand ideas that are
shared among disparate applications of computer science—and to make it easier for
you to make your own connections, and to extend CS in even more new directions.

How To Use This Book
Read much, but not many Books.
Benjamin Franklin (1706–1790)
Poor Richard’s Almanack (1738)

The brief version of the advice for how to use this book is: it’s your book; use it however you’d like. (Will Shortz, the puzzle editor of The New York Times, gives the analogous advice about crossword puzzles when he’s asked whether Googling for an


103

answer is cheating.) But my experience is that students do best when they read actively, with scrap paper close by; most people end up with a deeper understanding of a
problem by trying to solve it themselves first, before they look at the solution.
I’ve assumed throughout that you’re comfortable with programming in at least one
language, including familiarity with recursion. It doesn’t much matter which particular programming language you know; we’ll use features that are shared by almost all

modern languages—things like conditionals, loops, functions, and recursion. You may
or may not have had more than one programming-based CS course; many, but not all,
institutions require Data Structures as a prerequisite for this material. There are times
in the book when a data structures background may give you a deeper understanding
(but the same is true in reverse if you study data structures after this material). There
are similarly a handful of topics for which rudimentary calculus background is valuable. But knowing/remembering calculus will be specifically useful only a handful of
times in this book; the mathematical prerequisite for this material is really algebra and
“mathematical maturity,” which basically means having some degree of comfort with
the idea of a mathematical definition and with the manipulation of a mathematical
expression. (The few places where calculus is helpful are explicitly marked.)
There are 10 chapters after this one in the book.
Their dependencies are as shown at right. Aside from
these dependencies, there are some occasional references to other chapters, but these references are light.
If you’ve skipped Chapter 6—many instructors will
choose not cover this material, as it is frequently included in a course on Algorithms instead of this one—
then it will still be useful to have an informal sense of
O, Ω, and Θ notation in the context of the worst-case
running time of an algorithm. (You might skim Sections 6.1 and 6.6 before reading Chapters 7–11.)

2

data types

3

logic

4

proofs


5

induction

6

7

8

9

11

analysis of
algorithms

number
theory

relations

counting

graphs/trees

I’ve tried to include some helpful tips for problem
solving in the margins throughout the book, along with
a few warnings about common confusions and some

notes on terminology/notation that may be helpful in
keeping the words and symbols straight. There are also two kinds of extensions to the
main material. The “Taking it Further” blocks give more technical details about the
material under discussion—an alternate way of thinking about a definition, or a way
that a concept is used in CS or a related field. You should read the “Taking it Further”
blocks if—but only if!—you find them engaging. Each section also ends with one or
more boxed-off “Computer Science Connections” that show how the core material can
be used to solve a wide variety of (interesting, I hope!) CS applications. No matter how
interesting the core technical material may be, I think that it is what we can do with it
that makes it worth studying.

10
probability


104

CHAPTER 1. ON THE POINT OF THIS BOOK

What This Book Is About
All truths are easy to understand once they are
discovered; the point is to discover them.
Galileo Galilei (1564–1642)

This book focuses on discrete mathematics, in which the entities of interest are distinct and separate. Discrete mathematics contrasts with continuous mathematics, as
in calculus, which addresses infinitesimally small objects, which cannot be separated.
We’ll use summations rather than integrals, and we’ll generally be thinking about
things more like the integers (“1, 2, 3, . . .”) than like the real numbers (“all numbers
between π and 42”). Because this book is mostly focused on non-programming-based
parts of computer science, in general the “output” that you produce when solving a

problem will be something different from a program. Most typically, you will be asked
to answer some question (quantitatively or qualitatively) and to justify that answer—
that is, to prove your answer. (A proof is an ironclad, airtight argument that convinces
its reader of your claim.) Remember that your task in solving a problem is to persuade
your reader that your purported solution genuinely solves the problem. Above all, that
means that your main task in writing is communication and persuasion.
There are three very reasonable ways of thinking about this book.
View #1 is that this book is about the mathematical foundations of computation.
This book is designed to give you a firm foundation in mathematical concepts that are
crucial to computer science: sets and sequences and functions, logic, proofs, probability, number theory, graphs, and so forth.
View #2 is that this book is about practice. Essentially no particular example that
we consider matters; what’s crucial is for you to get exposure to and experience with
formal reasoning. Learning specific facts about specific topics is less important than
developing your ability to reason rigorously about formally defined structures.
View #3 is that this book is about applications of computer science: it’s about errorcorrecting codes (how to represent data redundantly so that the original information
is recoverable even in the face of data corruption); cryptography (how to communicate securely so that your information is understood by its intended recipient but not
by anyone else); natural language processing (how to interpret the “meaning” of an
English sentence spoken by a human using an automated customer service system);
and so forth. But, because solutions to these problems rely fundamentally on sets and
counting and number theory and logic, we have to understand basic abstract structures in order to understand the solutions to these applied problems.
In the end, of course, all three views are right: I hope that this book will help to introduce some of the foundational technical concepts and techniques of theoretical
computer science, and I hope that it will also help demonstrate that these theoretical
approaches have relevance and value in work throughout computer science—in topics
both theoretical and applied. And I hope that it will be at least a little bit of fun.

Bon voyage!

Be careful; there
are two different
words that are pronounced identically:

discrete, adj.: individually separate
and distinct.
discreet, adj.: careful and judicious
in speech, especially to maintain
privacy or avoid
embarrassment.
You wouldn’t read a
book about discreet
mathematics;
instead, someone
who trusts you
might quietly share
it while making
sure no one was
eavesdropping.


2
Basic Data Types

In which our heroes equip themselves for the journey ahead, by taking on
the basic provisions that they will need along the road.


×