Discrete Structures for Computing
Notes 1
Introduction
------------
This course provides you with the mathematical foundations needed
to analyze computer algorithms for
* correctness
* efficiency (complexity)
The mathematical foundations are from the area of "discrete mathematics",
the study of discrete (as opposed to continuous) entities: used when
you want to
* count objects
* study relationships between finite (or countably infinite) sets
* study processes involving a finite number of steps
Computing typically involves discrete objects.
Types of problems:
* how many valid passwords for a particular system?
* how many valid Internet addresses are there?
* is there a way to get from one computer to another in a network?
* what is the shortest way to get from one computer to another?
* how can we design a program to identify spam?
* how can messages be encrypted so that only the intended recipient
can learn the contents?
* how can we sort integers?
* how do we know a proposed sorting algorithm is correct?
* how long does it take to sort the integers?
etc.
Goals of the course:
* learn specific facts from discrete mathematics that are useful
in computer science and engineering
* practice in problem solving and algorithms
* practice thinking logically and mathematically, in particular, how
to construct proofs
What areas of computer science use discrete structures? Most, including
(as hinted at by the list of questions above)
* data structures and algorithms
* database management systems
* formal languages and automata
* compilers
* computer security
* operating systems
<< go over syllabus >>
=====================================================================
Chapter 1: The Foundations: Logic and Proofs
=====================================================================
A *theorem* is a mathematical statement that is true.
Knowing the statements of theorems in an area can be useful,
but it is even more useful to know *why* these statements
are true, i.e., know their proofs:
* can combine theorems to prove something new
* can modify an existing proof to prove a related fact
* generally leads to better understanding of the fact
In particular, having some facility with proofs is helpful in
computer science in order to:
* verify program correctness
* show that a system is secure
* develop AI applications
In fact, people have developed automated reasoning systems to
help with proving correctness of programs and systems.
The language of mathematical statements is *logic*.
The rules of logic are how we specify the meaning of mathematical
statements.
Also, logic is used in computer science applications including:
* design of computers
* specification of computer systems
* computer programming (including "logic programming")
* artificial intelligence
------------------------------------------------------------------------
1.1 Propositional Logic
------------------------------------------------------------------------
PROPOSITION: a sentence that states a fact that is either true or false
Ex:
* 1 + 1 = 2 (true)
* College Station is the capital of Texas. (false)
But not:
* x + 1 = 2 (neither true nor false, depends on what x is)
* What is your name? (does not state a fact)
NEGATION of a proposition p: ~p ("not p")
Creates a new proposition that has the opposite truth value of the
original.
Ex:
* p = "2 + 2 = 3", truth value is F
* ~p = "2 + 2 != 3", truth value is T
Truth table:
p ~p
--------
T F
F T
CONJUNCTION of two propositions p and q: p /\ q ("p and q").
Creates a new proposition that is true if p and q are both true,
and is false otherwise.
Ex:
* p = "2 + 2 = 3", q = "1 + 1 = 2"
* p /\ q = "2 + 2 = 3, and 1 + 1 = 2"
* since p is false and q is true, p /\ q is false.
Truth table:
p q p /\ q
----------------
T T T
T F F
F T F
F F F
DISJUNCTION of two propositions p and q: p \/ q ("p or q").
Creates a new proposition that is true if at least one of p and q is true,
and is false otherwise.
Ex:
* p = "2 + 2 = 3", q = "1 + 1 = 2"
* p /\ q = "2 + 2 = 3, and 1 + 1 = 2"
* since p is false and q is true, p \/ q is true.
Truth table:
p q p \/ q
----------------
T T T
T F T
F T T
F F F
Warning: In natural English language usage, we sometimes use "or"
to mean only one choice, e.g., you get soup or salad (but not both)
with a meal. In mathematics, "or" is inclusive.
EXCLUSIVE OR of two propositions p and q: p (+) q
Creates a new proposition that is true if exactly one of p and q is true,
and is false otherwise.
Ex:
* p = "2 + 2 = 3", q = "1 + 1 = 2"
* p /\ q = "2 + 2 = 3, and 1 + 1 = 2"
* since p is false and q is true, p (+) q is true.
Truth table:
p q p (+) q
----------------
T T F
T F T
F T T
F F F
CONDITIONAL STATEMENT is p -> q, "if p then q".
Creates a new proposition that false when p is true and q is false,
and is true otherwise.
p is the hypothesis/antecedent/premise
q is the conclusion/consequence
Truth table:
p q p -> q
----------------
T T T
T F F
F T T
F F T
If p is true, then q must be true.
If p is false, then q is allowed to be either true or false.
It is not allowed for p to be true and q to be false.
Some alternative English formulations of "if p then q":
* p implies q
* p only if q (PERHAPS UNINTUITIVE)
"I will go to the store only if it is not raining"
is the same as
"If I will go to the store then it is not raining"
but is not the same as
"If it is not raining then I will go to the store" (because it is
possible that I would go to the store even if it were raining).
Do not confuse the logical statement "if p then q" with the
programming construct of an "if statement".
An important statement that is related to the conditional statement p -> q:
CONTRAPOSITIVE of p -> q is ~q -> ~p
Truth table:
contrapos.
p q p -> q ~p ~q ~q -> ~p
----------------------------------------
T T T F F T
T F F F T F
F T T T F T
F F T T T T
Notice that p -> q and ~q -> ~p have the same column in the truth table!
Thus a conditional and its contraposition are EQUIVALENT.
Do not confuse the contrapositive with two other variations:
CONVERSE of p -> q is q -> p
INVERSE of p -> q is ~p -> ~q
You can work out from the truth tables that neither the converse nor
the inverse is equivalent to p -> q (but they are equivalent to each other).
Ex:
Conditional: If it is raining, then the roof leaks.
Contrapositive: If the roof does not leak, then it is not raining.
Converse: If the roof leaks, then it is raining.
Inverse: If it is not raining, then the roof does not leak.
BICONDITIONAL statement using two propositions p and q is
p <-> q, "p if and only if q" abbreviated "p iff q".
This statement is true if p and q have the same truth value (both true
or both false) and is false otherwise.
Ex: p = "the store is open"
q = "the sign is on"
p <-> q becomes "the store is open if and only if the sign is on"
Truth table:
p q p <-> q
------------------
T T T
T F F
F T F
F F T
Be aware of imprecision in natural language usage: sometimes we
state something as a conditional, when it is really a biconditional.
For instance, "If you eat your vegetables, you can have dessert."
Combining Logical Operators
---------------------------
We have the operators negation, or, and, conditional, and biconditional.
We can combine propositions with them to make new propositions.
Just like with arithmetic operators (addition, multiplication, division, etc.),
we have conventions regarding the *precedence* of the operators, and
we can change the precedence using parentheses.
Precedence:
1. negation, ~
2. and, /\
3. or, \/
4. conditional, ->
5. biconditional, <->
Ex:
~p /\ q -> r means ((~p) /\ q) -> r.
Using parentheses, we can have ~(p /\ (q -> r)), which is different.
CS APPLICATION:
---------------
An application of logic is to see if system specifications are
*consistent*. I.e., they should not contain conflicting requirements.
At least as a first step, system specs are given in
some natural language (say English). Then they can be translated
into logic, and checked for consistency, i.e., see whether they can
produce a contradiction. Later in Chapter 1, we will learn about
systematic ways to search for contradictions.
CS APPLICATION:
---------------
Logical operators (and, or, not) are used in web searches to
specify the search key.
CS APPLICATION:
---------------
* A BIT is a variable that can take on two values, 0 and 1.
* In most computer systems, 0 corresponds to the truth value false
and 1 to the truth value 1.
* Computer bit operations correspond to the logical connectives,
for instance, OR is \/, AND is /\, XOR is (+)
------------------------------------------------------------------------
1.2 Propositional Equivalences
------------------------------------------------------------------------
TAUTOLOGY: a proposition that is always true, no matter what truth
values are assigned to the component propositions.
Ex: p \/ ~p
Truth table:
p ~p p \/ ~p
----------------
T F T
F T T
CONTRADICTION: a proposition that is always false, no matter what truth
values are assigned to the component propositions.
Ex: p /\ ~p
Truth table:
p ~p p /\ ~p
----------------
T F F
F T F
CONTINGENCY: a proposition that is neither a tautology nor a contradiction.
Ex: p /\ p
Truth table:
p p /\ p
----------
T T
F F