Download presentation
Presentation is loading. Please wait.
Published byDennis Johnson Modified over 6 years ago
1
Ch.1 (Part 1): The Foundations: Logic and Proofs
Introduction Propositional Logic (Section 1.1) © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007
2
Introduction: Areas in which discrete mathematics concepts are present
Formal Languages (computer languages) Compiler Design Data Structures Computability Automata Theory Algorithm Design Relational Database Theory Complexity Theory (counting) Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
3
Example (counting): The Traveling Salesman Problem Important in
• circuit design • many other CS problems ______________________ Given: • n cities c1, c2, , cn • distance between city i and j, dij Find the shortest tour. Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
4
A tour requires n-1 additions. How many different tours?
Assume a very fast PC: 1 flop = 1 nanosecond = 10-9 sec. = 1,000,000,000 ops/sec = 1 GHz. A tour requires n-1 additions. How many different tours? Choose the first city n ways, the second city n-1 ways, the third city n-2 ways, etc. # tours = n (n-1) (n-2) (2) (1) = n! (Combinations) Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
5
Total number of additions = n(n-1)! (Rule of Product)
If n=8, T(n) = 8•7! = 40,320 flops < 1/3 second. HOWEVER If n=50, T(n) = 50•49! = = seconds = minutes = hours = days = weeks = years. ...a long time. You’ll be an old person (dead) before it’s finished! There are some problems for which we do not know if efficient algorithms exist to solve them! Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
6
Section 1.1: Propositional Logic
proposition : true = T (or 1) or false = F (or 0) (binary logic) •‘the moon is made of green cheese’ •‘ go to town!’ X - imperative •‘What time is it?’ X – interrogative propositional variables: P, Q, R, S, . . . New Propositions from old: calculus of propositions - relate new propositions to old using TRUTH TABLES Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
7
Logical operators: unary, binary Unary:
Negation Binary Conjunction Disjunction Exclusive OR Implication Biconditional Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
8
Unary Negation ‘not’ Symbol: Example: P: I am going to town
P: I am not going to town; It is not the case that I am going to town; I ain’t goin’. Truth Table P P F(0) T(1) Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
9
Binary Truth Table P Q P Q 1 Conjunction: ‘and’ Symbol:
1 Binary Conjunction: ‘and’ Symbol: Example: P - ‘I am going to town’ Q - ‘It is going to rain’ P Q: ‘I am going to town and it is going to rain.’ Note: Both P and Q must be true!!!!! Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
10
Binary Disjunction: inclusive ‘or’ Symbol:
Example: P - ‘I am going to town’ Q - ‘It is going to rain’ P Q: ‘I am going to town or it is going to rain.’ Note: Only one of P and Q must be true. Hence, the inclusive nature. Truth Table: P Q P Q 1 Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
11
Binary Truth Table P Q P Q 1 Exclusive OR: Symbol Example:
1 Binary Exclusive OR: Symbol Example: P - ‘I am going to town’ Q - ‘It is going to rain’ P Q: ‘Either I am going to town or it is going to rain.’ Note: Only one of P and Q must be true. Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
12
Binary Implication: ‘If...then...’ Example: P - ‘I am going to town’
Symbol: Example: P - ‘I am going to town’ Q - ‘It is going to rain’ P Q: ‘If I am going to town then it is going to rain.’ Truth Table P Q P Q 1 Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
13
Implication (cont.) Equivalent forms: If P, then Q P implies Q If P, Q
P only if Q P is a sufficient condition for Q Q if P Q whenever P Q is a necessary condition for P Note: The implication is false only when P is true and Q is false! Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
14
There is no causality implied here!
‘If the moon is made of green cheese then I have more money than Bill Gates’ (T) ‘If the moon is made of green cheese then I’m on welfare’ (T) ‘If 1+1=3 then your grandma wears combat boots’ (T) ‘If I’m wealthy then the moon is not made of green cheese.’ (T) ‘If I’m not wealthy then the moon is not made of green Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
15
P = premise, hypothesis, antecedent Q = conclusion, consequence
Terminology: P = premise, hypothesis, antecedent Q = conclusion, consequence More terminology: Q P is the CONVERSE of P Q Q P is the CONTRAPOSITIVE of P Q Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
16
Find the converse and contrapositive of the following statement:
Example: Find the converse and contrapositive of the following statement: R: ‘Raining tomorrow is a sufficient condition for my not going to town.’ Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
17
Step 1: Assign propositional variables to component propositions
P: It will rain tomorrow Q: I will not go to town Step 2: Symbolize the assertion R: PQ Step 3: Symbolize the converse QP Step 4: Convert the symbols back into words ‘If I don’t go to town then it will rain tomorrow’ or ‘Raining tomorrow is a necessary condition for my not going to town.’ ‘My not going to town is a sufficient condition for it raining tomorrow.’ Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
18
‘If you finish your meal then you can have dessert’
Truth Table P Q P Q 1 Binary Biconditional: ‘if and only if’, ‘iff’ Symbol: Example: P - ‘I am going to town’, Q - ‘It is going to rain’ P Q: ‘I am going to town if and only if it is going to rain.’ Note: Both P and Q must have the same truth value. Imprecision of the natural language: ‘If you finish your meal then you can have dessert’ Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
19
Breaking assertions into component propositions - look for the logical operators!
Example:‘If I go to Harry’s or go to the country I will not go shopping.’ P: I go to Harry’s Q: I go to the country R: I will go shopping If......P......or.....Q.....then....not..…R (P V Q)R Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
20
Constructing a truth table:
- one column for each propositional variable - one for the compound proposition - count in binary - n propositional variables = 2n rows You may find it easier to include columns for propositions which themselves are component propositions. Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
21
Truth Table P Q R (P V Q)R 1
1 Ch.1 (part 1): The foundations: Logic & Proof, Sets, and Functions
22
Chapter 1: (Part 2): The Foundations: Logic and Proofs
Propositional Equivalence (Section 1.2) Predicates & Quantifiers (Section 1.3) © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007
23
Propositional Equivalences (1.2)
A tautology is a proposition which is always true . Classic Example: P V P A contradiction is a proposition which is always false . Classic Example: P P A contingency is a proposition which neither a tautology nor a contradiction. Example: (P V Q) R CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
24
Propositional Equivalences (1.2) (cont.)
Two propositions P and Q are logically equivalent if P Q is a tautology. We write: P Q CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
25
Propositional Equivalences (1.2) (cont.)
Example: (P Q) (Q P) (P Q) Proof: The left side and the right side must have the same truth values independent of the truth value of the component propositions. To show a proposition is not a tautology: use an abbreviated truth table try to find a counter example or to disprove the assertion. search for a case where the proposition is false CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
26
Propositional Equivalences (1.2) (cont.)
Case 1: Try left side false, right side true Left side false: only one of PQ or Q P need be false. 1a. Assume PQ = F. Then P = T , Q = F. But then right side PQ = F. Wrong guess. 1b. Try Q P = F. Then Q = T, P = F. Then PQ = F. Another wrong guess. CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
27
Propositional Equivalences (1.2)
Case 2. Try left side true, right side false If right side is false, P and Q cannot have the same truth value. 2a. Assume P =T, Q = F. Then PQ = F and the conjunction must be false so the left side cannot be true in this case. Another wrong guess. 2b. Assume Q = T, P = F. Again the left side cannot be true. We have exhausted all possibilities and not found a counterexample. The two propositions must be logically equivalent. Note: Because of this equivalence, if and only if or iff is also stated as is a necessary and sufficient condition for. CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
28
Equivalence Name P T P P V F P Identity Laws P V T T P F F
Domination Laws P V P P P P P Idempotent Laws ( P) P Double Negation Law P V Q Q V P P Q Q P Commutative Law CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
29
Implication Equivalence
Name (P V Q) V R P V (Q V R) Associative Law P V (Q R) (P V Q) (P V R) Distributive Law (P Q) P V Q (P V Q) P Q De Morgan’s Laws P Q P V Q Implication Equivalence P Q Q P Contrapositive Law Note: equivalent expressions can always be substituted for each other in a more complex expression - useful for simplification. CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
30
Propositional Equivalences (1.2) (cont.)
Normal or Canonical Forms Unique representations of a proposition Examples: Construct a simple proposition of two variables which is true only when P is true and Q is false: P Q P is true and Q is true: P Q P is true and Q is false or P is true and Q is true: (P Q) V (P Q) CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
31
Propositional Equivalences (1.2) (cont.)
A disjunction of conjunctions where every variable or its negation is represented once in each conjunction (a minterm) each minterms appears only once Disjunctive Normal Form (DNF) Important in switching theory, simplification in the design of circuits. CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
32
Propositional Equivalences (1.2) (cont.)
Method: To find the minterms of the DNF. Use the rows of the truth table where the proposition is 1 or True If a zero appears under a variable, use the negation of the propositional variable in the minterm If a one appears, use the propositional variable. CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
33
Propositional Equivalences (1.2) (cont.)
Example: Find the DNF of (P V Q) R P Q R (P V Q) R 1 CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
34
Propositional Equivalences (1.2) (cont.)
There are 5 cases where the proposition is true, hence 5 minterms. Rows 1,2,3, 5 and 7 produce the following disjunction of minterms: (P V Q) R (P Q R) V (P Q R) V (P Q R) V (P Q R) V (P Q R) Note that you get a Conjunctive Normal Form (CNF) if you negate a DNF and use DeMorgan’s Laws. CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
35
Predicates & Quantifiers (1.3)
A generalization of propositions - propositional functions or predicates: propositions which contain variables Predicates become propositions once every variable is bound- by assigning it a value from the Universe of Discourse U or quantifying it CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
36
Predicates & Quantifiers (1.3) (cont.)
Examples: Let U = Z, the integers = { , -1, 0 , 1, 2, 3, . . .} P(x): x > 0 is the predicate. It has no truth value until the variable x is bound. Examples of propositions where x is assigned a value: P(-3) is false, P(0) is false, P(3) is true. The collection of integers for which P(x) is true are the positive integers. CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
37
Predicates & Quantifiers (1.3) (cont.)
P(y) V P(0) is not a proposition. The variable y has not been bound. However, P(3) V P(0) is a proposition which is true. Let R be the three-variable predicate R(x, y z): x + y = z Find the truth value of R(2, -1, 5), R(3, 4, 7), R(x, 3, z) CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
38
Predicates & Quantifiers (1.3) (cont.)
Universal P(x) is true for every x in the universe of discourse. Notation: universal quantifier x P(x) ‘For all x, P(x)’, ‘For every x, P(x)’ The variable x is bound by the universal quantifier producing a proposition. CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
39
Predicates & Quantifiers (1.3) (cont.)
Example: U = {1, 2, 3} x P(x) P(1) P(2) P(3) CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
40
Predicates & Quantifiers (1.3) (cont.)
Existential P(x) is true for some x in the universe of discourse. Notation: existential quantifier x P(x) ‘There is an x such that P(x),’ ‘For some x, P(x)’, ‘For at least one x, P(x)’, ‘I can find an x such that P(x).’ Example: U={1,2,3} x P(x) P(1) V P(2) V P(3) CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
41
Predicates & Quantifiers (1.3) (cont.)
Unique Existential P(x) is true for one and only one x in the universe of discourse. Notation: unique existential quantifier !x P(x) ‘There is a unique x such that P(x),’ ‘There is one and only one x such that P(x),’ ‘One can find only one x such that P(x).’ CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
42
Predicates & Quantifiers (1.3) (cont.)
Example: U = {1, 2, 3, 4} P(1) P(2) P(3) !xP(x) 1 How many minterms are in the DNF? CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
43
Predicates & Quantifiers (1.3) (cont.)
REMEMBER! A predicate is not a proposition until all variables have been bound either by quantification or assignment of a value! CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
44
Predicates & Quantifiers (1.3) (cont.)
Equivalences involving the negation operator (x P(x )) x P(x) (x P(x)) x P(x) Distributing a negation operator across a quantifier changes a universal to an existential and vice versa. (x P(x)) (P(x1) P(x2) … P(xn)) P(x1) V P(x2) V … V P(xn) x P(x) CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
45
Predicates & Quantifiers (1.3) (cont.)
Multiple Quantifiers: read left to right . . . Example: Let U = R, the real numbers, P(x,y): xy= 0 x y P(x, y) x y P(x, y) x y P(x, y) x y P(x, y) The only one that is false is the first one. What’s about the case when P(x,y) is the predicate x/y=1? CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
46
Predicates & Quantifiers (1.3) (cont.)
Multiple Quantifiers: read left to right . . . Example: Let U = {1,2,3}. Find an expression equivalent to x y P(x, y) where the variables are bound by substitution instead: Expand from inside out or outside in. Outside in: y P(1, y) y P(2, y) y P(3, y) [P(1,1) V P(1,2) V P(1,3)] [P(2,1) V P(2,2) V P(2,3)] [P(3,1) V P(3,2) V P(3,3)] CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
47
Predicates & Quantifiers (1.3) (cont.)
Converting from English (Can be very difficult!) “Every student in this class has studied calculus” transformed into: “For every student in this class, that student has studied calculus” C(x): “x has studied calculus” x C(x) This is one way of converting from English! CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
48
Predicates & Quantifiers (1.3) (cont.)
Multiple Quantifiers: read left to right (cont.) Example: F(x): x is a fleegle S(x): x is a snurd T(x): x is a thingamabob U={fleegles, snurds, thingamabobs} (Note: the equivalent form using the existential quantifier is also given) CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
49
Predicates & Quantifiers (1.3) (cont.)
Everything is a fleegle x F( x) (x F(x)) Nothing is a snurd. x S(x) (x S( x)) All fleegles are snurds. x [F(x)S(x)] x [F(x) V S(x)] x [F(x) S(x)] (x [F(x) V S(x)]) CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
50
Predicates & Quantifiers (1.3) (cont.)
Some fleegles are thingamabobs. x [F(x) T(x)] (x [F(x) V T(x)]) No snurd is a thingamabob. x [S(x) T(x)] (x [S(x ) T(x)]) If any fleegle is a snurd then it's also a thingamabob x [(F(x) S(x)) T(x)] (x [F(x) S(x) T( x)]) CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
51
Predicates & Quantifiers (1.3) (cont.)
Extra Definitions: An assertion involving predicates is valid if it is true for every universe of discourse. An assertion involving predicates is satisfiable if there is a universe and an interpretation for which the assertion is true. Else it is unsatisfiable. The scope of a quantifier is the part of an assertion in which variables are bound by the quantifier CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
52
Predicates & Quantifiers (1.3) (cont.)
Examples: Valid: x S(x) [x S( x)] Not valid but satisfiable: x [F(x) T(x)] Not satisfiable: x [F(x) F(x)] Scope: x [F(x) V S( x)] vs. x [F(x)] V x [S(x)] CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
53
Predicates & Quantifiers (1.3) (cont.)
Dangerous situations: Commutativity of quantifiers x y P(x, y) y x P( x, y)? YES! x y P(x, y) y x P(x, y)? NO! DIFFERENT MEANING! Distributivity of quantifiers over operators x [P(x) Q(x)] x P( x) x Q( x)? x [P( x) Q( x)] [x P(x) x Q( x)]? CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
54
Sets (1.6) A set is a collection or group of objects or elements or members. (Cantor 1895) A set is said to contain its elements. There must be an underlying universal set U, either specifically stated or understood. CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
55
Sets (1.6) (cont.) Notation: list the elements between braces:
S = {a, b, c, d}={b, c, a, d, d} (Note: listing an object more than once does not change the set. Ordering means nothing.) specification by predicates: S= {x| P(x)}, S contains all the elements from U which make the predicate P true. brace notation with ellipses: S = { , -3, -2, -1}, the negative integers. CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
56
Sets (1.6) (cont.) Common Universal Sets Notation: R = reals
N = natural numbers = {0,1, 2, 3, }, the counting numbers Z = all integers = {. . , -3, -2, -1, 0, 1, 2, 3, 4, . .} Z+ is the set of positive integers Notation: x is a member of S or x is an element of S: x S. x is not an element of S: x S. CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
57
Sets (1.6) (cont.) Subsets Definition: The set A is a subset of the set B, denoted A B, iff x [x A x B] Definition: The void set, the null set, the empty set, denoted , is the set with no members. Note: the assertion x is always false. Hence x [x x B] is always true(vacuously). Therefore, is a subset of every set. Note: A set B is always a subset of itself. CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
58
Sets (1.6) (cont.) Definition: If A B but A B the we say A is a proper subset of B, denoted A B (in some texts). Definition: The set of all subset of a set A, denoted P(A), is called the power set of A. Example: If A = {a, b} then P(A) = {, {a}, {b}, {a,b}} CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
59
Sets (1.6) (cont.) Definition: The number of (distinct) elements in A, denoted |A|, is called the cardinality of A. If the cardinality is a natural number (in N), then the set is called finite, else infinite. Example: A = {a, b}, |{a, b}| = 2, |P({a, b})| = 4. A is finite and so is P(A). Useful Fact: |A|=n implies |P(A)| = 2n CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
60
Sets (1.6) (cont.) N is infinite since |N| is not a natural number. It is called a transfinite cardinal number. Note: Sets can be both members and subsets of other sets. Example: A = {,{}}. A has two elements and hence four subsets: , {}, {{}}. {,{}} Note that is both a member of A and a subset of A! Russell's paradox: Let S be the set of all sets which are not members of themselves. Is S a member of itself? Another paradox: Henry is a barber who shaves all people who do not shave themselves. Does Henry shave himself? CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
61
Sets (1.6) (cont.) Definition: The Cartesian product of A with B, denoted A x B, is the set of ordered pairs {<a, b> | a A b B} Notation: Note: The Cartesian product of anything with is . (why?) Example: A = {a,b}, B = {1, 2, 3} AxB = {<a, 1>, <a, 2>, <a, 3>, <b, 1>, <b, 2>, <b, 3>} What is BxA? AxBxA? If |A| = m and |B| = n, what is |AxB|? CS 210, Ch.1 (part 2): The foundations: Logic & Proof, Sets, and Functions
62
Chapter 2: Basic Structures: Sets, Functions, Sequences and Sums
Sets (Section 2.1) Set Operations (Section 2.2) Functions (Section 2.3) Sequences and Summations (Section 2.4) © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sifth Edition, Mc Graw-Hill, 2007
63
Sets (2.1) A set is a collection or group of objects or elements or members. (Cantor 1895) A set is said to contain its elements. There must be an underlying universal set U, either specifically stated or understood. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
64
Sets (2.1) (cont.) Notation: list the elements between braces:
S = {a, b, c, d}={b, c, a, d, d} (Note: listing an object more than once does not change the set. Ordering means nothing.) specification by predicates: S= {x| P(x)}, S contains all the elements from U which make the predicate P true. brace notation with ellipses: S = { , -3, -2, -1}, the negative integers. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
65
Common Universal Sets Notation: Sets (2.1) (cont.) R = reals
N = natural numbers = {0,1, 2, 3, }, the counting numbers Z = all integers = {. . , -3, -2, -1, 0, 1, 2, 3, 4, . .} Z+ is the set of positive integers Notation: x is a member of S or x is an element of S: x S. x is not an element of S: x S. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
66
Sets (2.1) (cont.) Subsets Definition: The set A is a subset of the set B, denoted A B, iff x [x A x B] Definition: The void set, the null set, the empty set, denoted , is the set with no members. Note: the assertion x is always false. Hence x [x x B] is always true(vacuously). Therefore, is a subset of every set. Note: A set B is always a subset of itself. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
67
Example: If A = {a, b} then
Sets (2.1) (cont.) Definition: If A B but A B the we say A is a proper subset of B, denoted A B (in some texts). Definition: The set of all subset of a set A, denoted P(A), is called the power set of A. Example: If A = {a, b} then P(A) = {, {a}, {b}, {a,b}} CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
68
A is finite and so is P(A). Useful Fact: |A|=n implies |P(A)| = 2n
Sets (2.1) (cont.) Definition: The number of (distinct) elements in A, denoted |A|, is called the cardinality of A. If the cardinality is a natural number (in N), then the set is called finite, else infinite. Example: A = {a, b}, |{a, b}| = 2, |P({a, b})| = 4. A is finite and so is P(A). Useful Fact: |A|=n implies |P(A)| = 2n CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
69
Sets (2.1) (cont.) N is infinite since |N| is not a natural number. It is called a transfinite cardinal number. Note: Sets can be both members and subsets of other sets. Example: A = {,{}}. A has two elements and hence four subsets: , {}, {{}}. {,{}} Note that is both a member of A and a subset of A! Russell's paradox: Let S be the set of all sets which are not members of themselves. Is S a member of itself? Another paradox: Henry is a barber who shaves all people who do not shave themselves. Does Henry shave himself? CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
70
Definition: The Cartesian product of A with B, denoted
Sets (2.1) (cont.) Definition: The Cartesian product of A with B, denoted A x B, is the set of ordered pairs {<a, b> | a A b B} Notation: Note: The Cartesian product of anything with is . (why?) Example: A = {a,b}, B = {1, 2, 3} AxB = {<a, 1>, <a, 2>, <a, 3>, <b, 1>, <b, 2>, <b, 3>} What is BxA? AxBxA? If |A| = m and |B| = n, what is |AxB|? CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
71
Set Operations (2.2) (cont.)
Propositional calculus and set theory are both instances of an algebraic system called a Boolean Algebra. The operators in set theory are defined in terms of the corresponding operator in propositional calculus As always there must be a universe U. All sets are assumed to be subsets of U CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
72
Set Operations (2.2) (cont.)
Definition: Two sets A and B are equal, denoted A = B, iff x [x A x B]. Note: By a previous logical equivalence we have A = B iff x [(x A x B) (x B x A)] or A = B iff A B and B A CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
73
Set Operations (2.2) (cont.)
Definitions: The union of A and B, denoted A U B, is the set {x | x A x B} The intersection of A and B, denoted A B, is the set {x | x A x B} Note: If the intersection is void, A and B are said to be disjoint. The complement of A, denoted , is the set {x | (x A)} Note: Alternative notation is Ac, and {x|x A}. The difference of A and B, or the complement of B relative to A, denoted A - B, is the set A Note: The (absolute) complement of A is U - A. The symmetric difference of A and B, denoted A B, is the set (A - B) U (B - A)
74
Set Operations (2.2) (cont.)
Examples: U = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} A= {1, 2, 3, 4, 5}, B = {4, 5, 6, 7, 8}. Then AB = {1, 2, 3, 4, 5, 6, 7, 8} A B = {4, 5} = {0, 6, 7, 8, 9, 10} = {0, 1, 2, 3, 9, 10} A - B = {1, 2, 3} B - A = {6, 7, 8} AB = {1, 2, 3, 6, 7, 8} CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
75
Set Operations (2.2) (cont.)
Venn Diagrams A useful geometric visualization tool (for 3 or less sets) The Universe U is the rectangular box Each set is represented by a circle and its interior All possible combinations of the sets must be represented Shade the appropriate region to represent the given set operation. U U B A A B C For 2 sets For 3 sets CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
76
Set Operations (2.2) (cont.)
Set Identities Set identities correspond to the logical equivalences. Example: The complement of the union is the intersection of the complements: = Proof: To show: x [x x ] To show two sets are equal we show for all x that x is a member of one set if and only if it is a member of the other. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
77
Set Operations (2.2) (cont.)
We now apply an important rule of inference (defined later) called Universal Instantiation In a proof we can eliminate the universal quantifier which binds a variable if we do not assume anything about the variable other than it is an arbitrary member of the Universe. We can then treat the resulting predicate as a proposition. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
78
Then we can treat the predicates as propositions:
We say 'Let x be arbitrary.' Then we can treat the predicates as propositions: CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
79
Set Operations (2.2) (cont.)
Hence x x is a tautology. Since • x was arbitrary • we have used only logically equivalent assertions and definitions CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
80
Set Operations (2.2) (cont.)
we can apply another rule of inference called Universal Generalization We can apply a universal quantifier to bind a variable if we have shown the predicate to be true for all values of the variable in the Universe. and claim the assertion is true for all x, i.e., x [x x ] Q. E. D. (Latin phrase “Quod Erat Demonstrandum”) CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
81
Set Operations (2.2) (cont.)
Note: As an alternative which might be easier in some cases, use the identity A = B [A B and B A] Example: Show A (B - A) = The void set is a subset of every set. Hence, A (B - A) Therefore, it suffices to show A (B - A) or x [xA (B - A) x ] So as before we say 'let x be arbitrary’. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
82
Set Operations (2.2) (cont.)
Example (cont.) Show xA (B - A) x is a tautology. But the consequent is always false. Therefore, the antecedent better always be false also. Apply the definitions: CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
83
Set Operations (2.2) (cont.)
Example (cont.) Hence, because P P is always false, the implication is a tautology. The result follows by Universal Generalization. Q. E. D. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
84
Set Operations (2.2) (cont.)
Union and Intersection of Indexed Collections Let A1,A2 ,..., An be an indexed collection of sets. Union and intersection are associative (because 'and' and 'or' are) we have: CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
85
Set Operations (2.2) (cont.)
Examples Let CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
86
Functions (2.3) Definition: Let A and B be sets. A function (mapping, map) f from A to B, denoted f :AB, is a subset of A*B such that x [x A y [y B < x, y > f ]] and [< x, y1 > f < x, y2 > f ] y1 = y2 CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
87
Functions (2.3) (cont.) Note: f associates with each x in A one and only one y in B. A is called the domain and B is called the codomain. If f(x) = y y is called the image of x under f x is called a preimage of y (note there may be more than one preimage of y but there is only one image of x). The range of f is the set of all images of points in A under f. We denote it by f(A). CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
88
Functions (2.3) (cont.) If S is a subset of A then
f(S) = {f(s) | s in S}. Example: • f(a) = Z • the image of d is Z • the domain of f is A = {a, b, c, d} • the codomain is B = {X, Y, Z} • f(A) = {Y, Z} • the preimage of Y is b • the preimages of Z are a, c and d • f({c,d}) = {Z} A B a X b Y c Z d CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
89
Functions (2.3) (cont.) Injections, Surjections and Bijections
Let f be a function from A to B. Definition: f is one-to-one (denoted 1-1) or injective if preimages are unique. Note: this means that if a b then f(a) f(b). Definition: f is onto or surjective if every y in B has a preimage. Note: this means that for every y in B there must be an x in A such that f(x) = y. Definition: f is bijective if it is surjective and injective (one-to-one and onto). CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
90
Functions (2.3) (cont.) Examples:
The previous Example function is neither an injection nor a surjection. Hence it is not a bijection. A B a X b Y c Z d Surjection but not an injection CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
91
Functions (2.3) (cont.) A B A B a a V V b b W W c c X X d d Y Y
Injection & a surjection, hence a bijection Z Injection but not a surjection CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
92
or the same cardinality.
Functions (2.3) (cont.) Note: Whenever there is a bijection from A to B, the two sets must have the same number of elements or the same cardinality. That will become our definition, especially for infinite sets. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
93
Functions (2.3) (cont.) Examples:
Let A = B = R, the reals. Determine which are injections, surjections, bijections: • f(x) = x, • f(x) = x2, • f(x) = x3, • f(x) = x + sin(x), • f(x) = | x | CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
94
Sorry! It gets worse before it gets better.
Functions (2.3) (cont.) Let E be the set of even integers {0, 2, 4, 6, }. Then there is a bijection f from N to E , the even nonnegative integers, defined by f(x) = 2x. Hence, the set of even integers has the same cardinality as the set of natural numbers. OH, NO! IT CAN’T BE....E IS ONLY HALF AS BIG!!! Sorry! It gets worse before it gets better. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
95
Functions (2.3) (cont.) Inverse Functions
Definition: Let f be a bijection from A to B. Then the inverse of f, denoted f-1, is the function from B to A defined as f-1(y) = x iff f(x) = y CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
96
Functions (2.3) (cont.) Example: Let f be defined by the diagram: A
V V b b W W c c X X d d Y Y Note: No inverse exists unless f is a bijection CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
97
Functions (2.3) (cont.) Definition: Let S be a subset of B. Then
f-1(S) = {x | f(x) S} Note: f need not be a bijection for this definition to hold. Example: Let f be the following function: A B a X f-1({Z}) = {c, d} f-1({X, Y}) = {a, b} b Y c Z d CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
98
Functions (2.3) (cont.) Composition
Definition: Let f: B C, g: A B. The composition of f with g, denoted fg, is the function from A to C defined by f g(x) = f(g(x)) CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
99
A g B f C a V h Examples: b W i c X j d Y A fg C a h b i c j d
CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
100
Functions (2.3) (cont.) If f(x) = x2 and g(x) = 2x + 1, then f(g(x)) = (2x+1)2 and g(f(x)) = 2x2 + 1 Definition: The floor function, denoted f ( x) = x or f(x) = floor(x), is the largest integer less than or equal to x. The ceiling function, denoted f ( x) = x or f(x) = ceiling(x), is the smallest integer greater than or equal to x. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
101
Functions (2.3) (cont.) Examples: 3.5 = 3, 3.5 = 4.
Note: the floor function is equivalent to truncation for positive numbers. Example: Suppose f: B C, g: A B and f g is injective. What can we say about f and g? • We know that if a b then f(g(a)) f(g(b)) since the composition is injective. • Since f is a function, it cannot be the case that g(a) = g(b) since then f would have two different images for the same point. • Hence, g(a) g(b) It follows that g must be an injection. However, f need not be an injection (you show). CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
102
Sequences and Summations (2.4)
Definition: A sequence is a function from a subset of the natural numbers (usually of the form {0, 1, 2, } to a set S. Note: the sets {0, 1, 2, 3, , k} and {1, 2, 3, 4, , k} are called initial segments of N. Notation: if f is a function from {0, 1, 2, . . .} to S we usually denote f(i) by ai and we write where k is the upper limit (usually ). CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
103
Sequences and Summations (2.4) (cont.)
Examples: Using zero-origin indexing, if f(i) = 1/(i + 1). then the Sequence f = {1, 1/'2,1/3,1/4, } = {a0, a1, a2, a3, . . } Using one-origin indexing the sequence f becomes {1/2, 1/3, . . .} = {a1, a2, a3, . . .} CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
104
Sequences and Summations (2.4) (cont.)
Summation Notation Given a sequence we can add together a subset of the sequence by using the summation and function notation or more generally CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
105
Sequences and Summations (2.4) (cont.)
Examples: Similarity for the product notation:
106
Sequences and Summations (2.4) (cont.)
Definition: A geometric progression is a sequence of the form a, ar, ar2, ar3, ar4, Your book has a proof that (you can figure out what it is if r = 1). You should also be able to determine the sum • if the index starts at k vs. 0 • if the index ends at something other than n (e.g., n-1, n+1, etc.). CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
107
Sequences and Summations (2.4) (cont.)
Cardinality Definition: The cardinality of a set A is equal to the cardinality of a set B, denoted | A | = | B |, if there exists a bijection from A to B. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
108
Sequences and Summations (2.4)
Definition: If a set has the same cardinality as a subset of the natural numbers N, then the set is called countable. If |A| = |N|, the set A is countably infinite. The (transfinite) cardinal number of the set N is aleph null = 0. If a set is not countable we say it is uncountable. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
109
Sequences and Summations (2.4)
Examples: The following sets are uncountable (we show later) The real numbers in [0, 1] P(N), the power set of N Note: With infinite sets proper subsets can have the same cardinality. This cannot happen with finite sets. Countability carries with it the implication that there is a listing of the elements of the set. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
110
Sequences and Summations (2.4)
Definition: | A | | B | if there is an injection from A to B. Note: as you would hope, Theorem: If | A | | B | and | B | | A | then | A | = | B |. This implies if there is an injection from A to B if there is an injection from B to A then there must be a bijection from A to B This is difficult to prove but is an example of demonstrating existence without construction. It is often easier to build the injections and then conclude the bijection exists. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
111
Sequences and Summations (2.4)
Example: Theorem: If A is a subset of B then | A | | B |. Proof: the function f(x) = x is an injection from A to B. Example: {0, 2, 5}| 0 The injection f: {0, 2, 5} N defined by f(x) = x is shown below: … CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
112
Sequences and Summations (2.4)
Some Countably Infinite Sets The set of even integers E ( 0 is considered even) is countably infinite. Note that E is a proper subset of N, Proof: Let f(x) = 2x. Then f is a bijection from N to E Z+, the set of positive integers is countably infinite. … … CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
113
Sequences and Summations (2.4)
The set of positive rational numbers Q+ is countably infinite. Proof: Z+ is a subset of Q+ so |Z+| = 0 |Q+|. Now we have to show that |Q+| 0. To do this we show that the positive rational numbers with repetitions, QR, is countably infinite. Then, since Q+ is a subset of QR, it follows that |Q+| 0 and hence |Q+| = 0. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
114
Sequences and Summations (2.4)
CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
115
Sequences and Summations (2.4)
The position on the path (listing) indicates the image of the bijective function f from N to QR: f(0) = 1/1, f(1) = 1/2, f(2) = 2/1, f(3) = 3/1, and so forth. Every rational number appears on the list at least once, some many times (repetitions). Hence, |N| = |QR| = Q. E. D The set of all rational numbers Q, positive and negative, is countably infinite. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
116
Sequences and Summations (2.4)
The set of (finite length) strings S over a finite alphabet A is countably infinite. To show this we assume that A is nonvoid There is an “alphabetical” ordering of the symbols in A Proof: List the strings in lexicographic order: all the strings of zero length, then all the strings of length 1 in alphabetical order, then all the strings of length 2 in alphabetical order, etc. This implies a bijection from N to the list of strings and hence it is a countably infinite set. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
117
Sequences and Summations (2.4)
For example: Let A = {a, b, c}. Then the lexicographic ordering of A is { , a, b, c, aa, ab, ac, ba, bb, bc, ca, cb, cc, aaa, aab, aac, aba, ....} = {f(0), f(1), f(2), f(3), f(4), } CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
118
Sequences and Summations (2.4)
The set of all C programs is countable. Proof: Let S be the set of legitimate characters which can appear in a C program. A C compiler will determine if an input program is a syntactically correct C program (the program doesn't have to do anything useful). Use the lexicographic ordering of S and feed the strings into the compiler. If the compiler says YES, this is a syntactically correct C program, we add the program to the list. Else we move on to the next string. In this way we construct a list or an implied bijection from N to the set of C programs. Hence, the set of C programs is countable Q. E. D. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
119
Sequences and Summations (2.4)
Cantor Diagonalization An important technique used to construct an object which is not a member of a countable set of objects with (possibly) infinite descriptions Theorem: The set of real numbers between 0 and 1 is uncountable. Proof: We assume that it is countable and derive a contradiction. If it is countable we can list them (i.e., there is a bijection from a subset of N to the set). We show that no matter what list you produce we can construct a real number between 0 and 1 which is not in the list. Hence, there cannot exist a list and therefore the set is not countable CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
120
Sequences and Summations (2.4)
It's actually much bigger than countable. It is said to have the cardinality of the continuum, c. Represent each real number in the list using its decimal expansion. e.g., 1/3 = 1/2 = = If there is more than one expansion for a number, it doesn't matter as long as our construction takes this into account. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
121
Sequences and Summations (2.4)
THE LIST.... r1 = .d11d12d13d14d15d r2 = .d21d22d23d24d25d r3 = .d31d32d33d34d35d ... Now construct the number x = .x1x2x3x4x5x6x xi = 3 if dii 3 xi = 4 if dii = 3 (Note: choosing 0 and 9 is not a good idea because of the non uniqueness of decimal expansions.) Then x is not equal to any number in the list. Hence, no such list can exist and hence the interval (0,1) is uncountable Q. E. D.
122
Sequences and Summations (2.4)
An extra goody: Definition: a number x between 0 and 1 is computable if there is a C program which when given the input i, will produce the ith digit in the decimal expansion of x. Example: The number 1/3 is computable. The C program which always outputs the digit 3, regardless if the input, computes the number. CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
123
Sequences and Summations (2.4)
Theorem: There is exists a number x between 0 and 1 which is not computable. There does not exist a C program (or a program in any other language) which will compute it! Why? Because there are more numbers between 0 and 1 than there are C programs to compute them. (in fact there are c such numbers!) Our second example of the nonexistence of programs to compute things! CSE 504, Ch.1 (part 3): The foundations: Logic & Proof, Sets, and Functions
124
Chapter 3 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
Algorithms (Section 3.1) The Growth of Functions (Section 3.2) Complexity of Algorithms (Section 3.3) © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007
125
Algorithms (3.1) Some Applications:
Use of number theory to make message secret Generate pseudorandom numbers Assign memory locations to computer files Internet security CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
126
Algorithms (3.1) (cont.) Introduction
Given a sequence of integers, find the largest one Given a set, list all of his subsets Given a set of integers, put them in increasing order Given a network, find the shortest path between two vertices CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
127
Algorithms (3.1) (cont.) Methodology:
Construct a model that translates the problem into a mathematical context Build a method that will solve the general problem using the model Ideally, we need a procedure that follows a sequence of steps that leads to the desired answer. Such a sequence is called an algorithm. History: the term algorithm is a corruption of the name Al-Khowarizmi (mathematician of the 9th century) CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
128
Algorithms (3.1) (cont.) Definition: An algorithm is a finite set of precise instructions for performing a computation or for solving a problem. Example: Describe an algorithm for finding the largest value in a finite sequence of integers Solution: We perform the following steps: CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
129
Algorithms (3.1) (cont.) Set the temporary maximum equal to the first integer in the sequence Compare the next integer in the sequence to the temporary maximum, and if it is larger that the temporary maximum, set the temporary maximum equal to this integer Repeat the previous step if there are more integers in the sequence Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence Pseudocode: intermediate step between an English language description of an algorithm and an implementation of this algorithm in a programming language CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
130
Algorithms (3.1) (cont.) Algorithm: Finding the maximum element in a finite sequence Procedure max(a1, a2, …, an: integer) max := a1 For i := 2 to n If max < ai then max := ai {max is the largest element} CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
131
Algorithms (3.1) (cont.) Properties of an algorithm:
Input: an algorithm has input values from a specified set Output: from each set of input values an algorithm produces output values from a specified set. The output values are the solution to the problem Definiteness: the steps of an algorithm must be defined precisely Correctness: an algorithm should produce the correct output values for each set of input values Finiteness: an algorithm should produce the desired output after a finite (but perhaps large) number of steps for input in the set Effectiveness: it must be possible to perform each step of an algorithm exactly and in a finite amount of time Generality: the procedure should be applicable for all problems of the desired form not just for a particular set of input values. CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
132
Algorithms (3.1) (cont.) Searching Algorithms
Problem: “Locate an element x in a list of distinct elements a1, a2, …, an, or determine that it is not in the list.” We should provide as a solution to this search problem the location of the term in the list that equals x. CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
133
Algorithms (3.1) (cont.) The linear search
Algorithm: The linear search algorithm Procedure linear search(x: integer, a1, a2, …, an: distinct integers) i := 1 while (i n and x ai) i := i + 1 if i n then location := i else location := 0 {location is the subscript of the term that equals x, or is 0 if x is not found} CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
134
Algorithms (3.1) (cont.) The binary search
Constraint: can be used when the list has terms occurring in order of increasing size (words listed in lexicographic order) Methodology: Compare the element to be located to the middle term of the list CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
135
Algorithms (3.1) (cont.) Example: Search 19 in the list
split the list into 2 subsets with 8 terms each Compare 19 with the largest element of the first set 10< 19 search 19 in the second set Split the second subset into 2 smaller subsets Compare 19 with 16 16 < 19 search 19 in the second set Split the second subset as: Compare 19 > 19 is false search 19 in Split the subset as : Since 18 < 19 search restricted to the second list Finally 19 is located at the 14th element of the original list CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
136
Algorithms (3.1) (cont.) Algorithm: the binary search algorithm
Procedure binary search (x: integer, a1, a2,…,an: increasing integers) i := 1 {i is left endpoint of search interval} j := n {j is right endpoint of search interval} While i < j Begin m := (i + j)/2 If x > am then i := m + 1 else j := m End If x := ai then location := i Else location := 0 {location is the subscript of the term equal to x, or 0 if x is not found} CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
137
Algorithms (3.1) (cont.) Sorting
Goal: “Order the elements of a list”. For example, sorting the list 7, 2, 1, 4, 5, 9 produces the list 1, 2, 4, , 5, 7, 9. Similarly, sorting the list d, h, c, a, f produces a, c, d, f, h. CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
138
Steps of the Bubble sort
Example: Sort the list 3, 2, 4, 1, 5 into increasing order using the Bubble sort Steps of the Bubble sort 3 2 4 1 5 2 3 1 4 5 2 1 3 4 5 1 2 3 4 5 1st pass 2nd pass 3rd pass 4th pass = ordered = permute CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
139
Algorithms (3.1) (cont.) Algorithm: the Bubble sort
Procedure Bubblesort (a1, …, an) for i := 1 to n-1 {count number of passes} for j := 1 to n-i if aj > aj+1 then interchange aj and aj+1 {a1, …, an is the increasing order} CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
140
Algorithms (3.1) (cont.) Greedy algorithms
Goal: Solving optimization problems. Find a solution to the given problem that either minimizes or maximizes the value of some parameter Some examples that involves optimization: Find a route between 2 cities with smallest total mileage Determine a way to encode messages using the fewest bits possible Find a set of fiber links between networks nodes using the least amount of fiber CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
141
Algorithms (3.1) (cont.) The change making problem
Problem statement: Consider the problem of making n cents change with quarters, dimes, nickels and pennies, and using the least total number of coins. For example, to make change for 67 cents, we do the following: Select a quarter, leaving 42 cents Select a second quarter, leaving 17 cents Select a dime, leaving 7 cents Select a nickel, leaving 2 cents Select a penny, leaving 1 cent Select a penny. CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
142
Algorithms (3.1) (cont.) Algorithm: Greedy change making
Procedure change (c1, c2, …, cr: values of denominations of coins where c1 > c2 >…> cr; n: positive integer) For i := 1 to r while n ci begin add a coin with value ci to the change n := n-ci end CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
143
Algorithms (3.1) (cont.) Remark: if we have only quarters, dimes and pennies the change for 30 cents would be made using 6 coins = 1 quarter + 5 pennies. Whereas a better solution is equal to 3 coins = 3 dimes! Therefore: “The greedy algorithm selects the best choice at each step, instead of considering all sequences of steps that may lead to an optimal solution. The greedy algorithm often leads to a solution!” CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
144
Algorithms (3.1) (cont.) Lemma: If is a positive integer, then n cents in change using quarters, dimes, nickels and pennies using the fewest coins possible has at most 2 dimes, at most 1 nickel, at most 4 pennies and cannot have 2 dimes and 1 nickel. The amount of change in dimes, nickels and pennies cannot exceed 24 cents. CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
145
Algorithms (3.1) (cont.) Proof of the lemma using contradiction: If we had more than the number of coins specified, then we will be able to replace them using fewer coins that have the same value. 3 dimes will be replaced by a quarter and 1 nickel 2 nickels replaced by a dime 5 pennies replaced by a nickel 2 dimes and I nickel replaced by a quarter Besides, we cannot have 2 dimes and 1 nickel 24 cents is the most money we can have in dimes, nickels and pennies when we make change using the fewest number of coins for n cents. Theorem [Greedy]: The greedy algorithm produces chab=nge using the fewest coins possible. CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
146
The Growth of Functions (Section 3.2)
We quantify the concept that g grows at least as fast as f. What really matters in comparing the complexity of algorithms? We only care about the behavior for large problems. Even bad algorithms can be used to solve small problems. Ignore implementation details such as loop counter incrementation, etc. We can straight-line any loop. CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
147
The Growth of Functions (3.2) (cont.)
The Big-O Notation Definition: Let f and g be functions from N to R. Then g asymptotically dominates f, denoted f is O(g) or 'f is big-O of g,' or 'f is order g,' iff k C n [n > k |f(n)| C |g(n)|] Note: Choose k Choose C; it may depend on your choice of k Once you choose k and C, you must prove the truth of the implication (often by induction) CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
148
The Growth of functions (3.2) (cont.)
An alternative for those with a calculus background: Definition: Theorem: If f is o(g) then f is O(g). Proof: by definition of limit as n goes to infinity, f(n)/g(n) gets arbitrarily small. That is for any >0, there must be an integer N such that when n > N, | f(n)/g(n) | < . Hence, choose C = and k = N. Q. E. D. CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
149
The Growth of functions (3.2) (cont.)
It is usually easier to prove f is o(g) using the theory of limits using L'Hospital's rule using the properties of logarithms etc. Example: 3n + 5 is O(n2) Proof: Using the theory of limits, it's easy to show Hence 3n + 5 is o(n2) and so it is O(n2). Q. E. D. We will use induction later to prove the result from scratch. CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
150
The Growth of functions (3.2) (cont.)
Also note that O(g) is a set called a complexity class. It contains all the functions which g dominates. f is O(g) means f O(g). CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
151
The Growth of functions (3.2) (cont.)
Properties of Big-O f is O(g) iff O(f) O(g) If f is O(g) and g is O(f) then O(f) = O(g) The set O(g) is closed under addition : If f is O(g) and h is O(g) then f + h is O(g) The set O(g) is closed under multiplication by a scalar a (real number): If f is O(g) then a*f is O(g) that is, O(g) is a vector space. (The proof is in the book). Also, as you would expect, if f is O(g) and g is O(h), then f is O(h). In particular O( f ) O(g) O(h) CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
152
The Growth of functions (3.2) (cont.)
Theorem: If f1 is O(g1) and f2 is O(g2) then f1f2 is O(g1g2) (1) f1 + f2 is O(max{ g1, g2}) (2) CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
153
The Growth of functions (3.2) (cont.)
Proof of (2): There is a k1 and C1 such that f1(n) < C1g1(n) when n > k1. There is a k2 and C2 such that f2(n) < C2g2(n) when n > k2. We must find a k3 and C3 such that f1(n)f2(n) < C3g1(n)g2(n) when n > k3. We use the inequality if 0 < a < b and 0 < c < d then ac < bd to conclude that f1(n)f2(n) < C1C2g1(n)g2(n) as long as k > max{k1, k2} so that both inequalities 1 and 2. hold at the same time. Therefore, choose C3 = C1C2 and k3 = max{k1, k2} Q.E.D. CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
154
The Growth of functions (3.2) (cont.)
Important Complexity Classes O(1) O(log n) O(n) O(n log n) O(n2) O(nj) O(cn) O(n!) where j>2 and c>1. CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
155
The Growth of functions (3.2) (cont.)
Example: Find the complexity class of the function (nn!+ 3n+2 + 3n100 )(nn + n2n ) Solution: This means to simplify the expression. Throw out stuff which you know doesn't grow as fast. We are using the property that if f is O(g) then f+g is O(g). CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
156
The Growth of functions (3.2) (cont.)
Solution (cont.) Eliminate the 3n100 term since n! grows much faster. Eliminate the 3n+2 term since it also doesn't grow as fast as the n! term. Now simplify the second term: Which grows faster, the nn or the n2n? Take the log (base 2) of both. Since the log is an increasing function whatever conclusion we draw about the logs will also apply to the original functions (why?). Compare n log n or log n + n. n log n grows faster so we keep the nn term The complexity class is O( n n! nn) CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
157
The Growth of functions (3.2) (cont.)
If a flop takes a nanosecond, how big can a problem be solved (the value of n) in a minute? a day? a year? for the complexity class O( n n! nn). Note: We often want to compare algorithms in the same complexity class CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
158
The Growth of functions (3.2) (cont.)
Example: Suppose Algorithm 1 has complexity n2 - n + 1 Algorithm 2 has complexity n2/2 + 3n +2 Then both are O(n2) but Algorithm 2 has a smaller leading coefficient and will be faster for large problems. Hence we write Algorithm 1 has complexity n2 + O(n) Algorithm 2 has complexity n2/2 + O(n) CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
159
Complexity of Algorithms (3.3)
Time Complexity: Determine the approximate number of operations required to solve a problem of size n. Space Complexity: Determine the approximate memory required to solve a problem of size n. CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
160
Complexity of Algorithms (3.3) (cont.)
Time Complexity Use the Big-O notation Ignore house keeping Count the expensive operations only Basic operations: searching algorithms - key comparisons sorting algorithms - list component comparisons numerical algorithms - floating point ops. (flops) - multiplications/divisions and/or additions/subtractions CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
161
Complexity of Algorithms (3.3) (cont.)
Worst Case: maximum number of operations Average Case: mean number of operations assuming an input probability distribution CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
162
Complexity of Algorithms (3.3) (cont.)
Examples: Multiply an n x n matrix A by a scalar c to produce the matrix B: procedure (n, c, A, B) for i from 1 to n do for j from 1 to n do B(i, j) = cA(i, j) end do Analysis (worst case): Count the number of floating point multiplications. n2 elements requires n2 multiplications. time complexity is O(n2) or quadratic complexity. CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
163
Complexity of Algorithms (3.3) (cont.)
Multiply an n x n upper triangular matrix A A(i, j) = 0 if i > j by a scalar c to produce the (upper triangular) matrix B. procedure (n, c, A, B) /* A (and B) are upper triangular */ for i from 1 to n do for j from i to n do B(i, j) = cA(i, j) end do Analysis (worst case): Count the number of floating point multiplications. CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
164
Complexity of Algorithms (3.3) (cont.)
The maximum number of non-zero elements in an n x n upper triangular matrix = n or • remove the diagonal elements (n) from the total (n2) • divide by 2 • add back the diagonal elements to get (n2 - n)/2 + n = n2/2 + n/2 which is n2/2 + O(n). Quadratic complexity but the leading coefficient is 1/2 CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
165
Complexity of Algorithms (3.3) (cont.)
Bubble sort: L is a list of elements to be sorted. We assume nothing about the initial order The list is in ascending order upon completion. Analysis (worst case): Count the number of list comparisons required. Method: If the jth element of L is larger than the (j + 1)st, swap them. Note: this is not an efficient implementation of the algorithm CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
166
Complexity of Algorithms (3.3) (cont.)
procedure bubble (n, L) /* - L is a list of n elements - swap is an intermediate swap location */ for i from n - 1 to 1 by -1 do for j from 1 to i do if L(j) > L(j + 1) do swap = L(j + 1) L(j + 1) = L (j) L(j) = swap end do CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
167
Complexity of Algorithms (3.3) (cont.)
Bubble the largest element to the 'top' by starting at the bottom - swap elements until the largest in the top position. Bubble the second largest to the position below the top. Continue until the list is sorted. n-1 comparison on the first pass n-2 comparisons on the second pass . 1 comparison on the last pass Total: (n - 1)+ (n - 2) = O(n2) or quadratic complexity (what is the leading coefficient?) CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
168
Complexity of Algorithms (3.3) (cont.)
An algorithm to determine if a function f from A to B is an injection: Input: a table with two columns: Left column contains the elements of A. Right column contains the images of the elements in the left column. Analysis (worst case): Count comparisons of elements of B. Recall that two elements of column 1 cannot have the same images in column 2. CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
169
Complexity of Algorithms (3.3) (cont.)
One solution: Sort the right column Worst case complexity (using Bubble sort) O(n2) Compare adjacent elements to see if they agree Worst case complexity O(n) Total: O(n2) + O(n) = O(n2) Can it be done in linear time? CSE 504, Chapter 2 (Part 1): The Fundamentals: Algorithms, the Integers & Matrices
170
CS 210 Discrete Mathematics The Integers and Division (Section 3.4)
Dr. Djamel Bouchaffra Chapter 3 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices The Integers and Division (Section 3.4) © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 3 (part 2)
171
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Introduction Review basics concepts of number theory Divisibility, greatest common divisors, modular arithmetic Computer arithmetic using binary expansions Application to computer arithmetic, cryptology, secret messages CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
172
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Division Definition 1: If a and b are integers with a 0, we say that a divides b if there is an integer c such that b = ac. When a divides b we say that a is a factor of b and that b is multiple of a. The notation a | b denotes that a divides b. We write a b when a does not divide b CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
173
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Example : 4 | 7, since 7 | 3 is not an integer. Example: Let n and d be positive integers. How many positive integers not exceeding n are divisible by d? Solution: They are of the form {dk}, where k is a positive integer. 0 < dk n 0 < k n/p There are n/p positive integers not exceeding n that are divisible by d. CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
174
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Theorem 1: Let a, b and c be integers. Then If a|b and a|c, then a | (b + c); If a|b, then a|bc c Z; If a|b and b|c, then a|c Proof: Suppose a|b and a|c s Z, t Z such that: b = as and c = at. Therefore: b + c = as + at = a (s + t); which means that a | (b + c). This establishes part 1 of the theorem. Parts 2 and 3 are left to you! Q.E.D. CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
175
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Corollary 1: if a, b and c are integers such that a|b and a|c, then a | mb +nc whenever m and n are integers. Proof: Part 2 of theorem 1 shows that: a | mb and a | nc whenever m and n are integers. Using part 1 of the theorem a | mb + nc Q.E.D. CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
176
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Primes Definition 2: A positive integer p greater than 1 is called prime if the only positive factors of p are 1 and p. A positive integer that is greater than 1 and is not prime is called composite. Example: 7 is prime, 9 is composite. CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
177
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Theorem 2: The Fundamental Theorem of Arithmetic (FTA) Every positive integer greater than 1 can be written in a unique way as a prime or as the product of two or more primes where the prime factors are written in a nondecreasing size Example: 100 = = 22.52 641 = 641 999 = = 33.37 1024 = = 210 Large numbers are used for secret messages in cryptology. CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
178
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Theorem 3: If n is a composite integer, then n has a prime divisor less than or equal to n. Proof: if n is composite, n has a factor such that: 1 < a < n and b Z such that: n = ab, where both a and b are positive integers greater than 1. Therefore a n or b n, since otherwise ab > n . n = n n has positive divisor n. This divisor is either prime or using the FTA, has a prime divisor. In either case, n has a prime divisor n Q.E.D. CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
179
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Example: Prove that the integer 101 is prime. Solution: Using the contrapositive form of theorem 3, states that : “n has not a prime number divisor n n prime” The only primes not exceeding 101 are {2, 3, 5, 7}, since these numbers do not divide 101 101 is prime! Q.E.D. CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
180
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Theorem 4: There are infinitely many primes. Proof: Let’s assume there are only finitely many primes p1, p2, …, pn. Let Q = p1p2 … pn + 1. Using FTA, Q is prime or Q can be written as the product of two or more primes. However, none of the primes pj divides Q, (since pj|Q pj | (Q – p1p2 … pn) = 1 impossible since pj prime) if none of the primes pj divides Q Q is prime. Q Pj contradiction, because we assumed that we have listed all the primes Q.E.D. CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
181
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Remark: the largest prime known has been an integer of the form 2p –1, where p is also prime (Mersenne primes.) Example: 22 – 1 = 3, 23 –1 =7, 25 –1 =31 are Mersenne primes, whereas 211 – 1 = 2047 is not a Mersenne prime since 2047 = Remark: the largest number known so far (year 2001) is 213,466,917 – 1 (over four million digits!!!) Visit GIMPS (Great Internet Mersenne Search) CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
182
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Theorem 5: The Prime Number Theorem The ratio of the number of primes not exceeding x and (x/lnx) approaches 1 as x grows without bound. (Conjectured by Legendre & Gauss) CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
183
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra The Division Algorithm Theorem 6: Let a be an integer and d a positive integer. Then !(q, r) Z2; 0 r < d: a = dq +r. CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
184
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Definition 3: d is called the divisor, a the dividend, q the quotient and r the remainder. q = a div d, r = a mod d. Example: = Quotient = 101 div 11 Remainder = 2 = 101 mod 11 CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
185
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Greatest Common Divisors & Least Common Multiples Definition 4 Let a and b be integers, not both zero. The largest integer d such that d|a and d|b is called the greatest common divisor of a and b. It is denoted gcd (a, b). Example: gcd (24, 36) Div (24) = {1,2,3,4,6,8,12,24} Div (36) = {1,2,3,4,6,8,9,12,18,36} Com(24,36) = = {1,2,3,4,6,12} gcd(24,36) = 12 CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
186
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Definition 5: The integers a and b are relatively prime (rp) if gcd(a, b) =1. Example: 17 and 22 are rp since gcd(17,22) = 1. CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
187
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Definition 7: The least common multiple (lcm) of the positive integers a and b is the smallest positive integer that is divisible by both a and b. where max(x,y) denotes the maximum of x and y. Example : What is the least common multiple of: and 2433? Solution: lcm( ,2433) = 2 max(3,4). 3max(5,3). 7max(2,0) = CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
188
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Theorem 7: Let a and b be positive integers. Then ab = gcd(a,b).lcm(a.b). CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
189
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Modular Arithmetic Definition 8: Let (a, b) Z2,, m Z+ then a is a congruent to b modulo m if m divides a –b. Notation: a b (mod m). Theorem 8 Let a and b be integers, and let m be a positive integer. Then a b (mod m) if and only if a mod m = b mod m. CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
190
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Example: 5 (mod 6) 24 14 (mod 6)? Since: 6|(17 – 5) = 12 17 5 (mod 6) 6 does not divide 24 is not congruent to 14 (mod 6) Theorem 9: Let m be a positive integer. The integers a and b are congruent modulo m if and only if k Z; a = b + km CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
191
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Applications of Congruences Hashing Functions Pseudorandom Numbers Cryptology (Caesar Cepher) CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
192
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Hashing Functions Assignment of memory location to a student record h(k) = k mod m Example: h ( ) = mod 111 = 14 when m = 111 Key: social security # # of available memory location CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
193
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Pseudorandom Numbers Needed for computer simulation Linear congruential method : xn+1 = (axn + c) mod m Put them between 0 and 1 as: yn = xn/m CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
194
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Cryptology (Caesar Cepher) Encryption: Making messages secrets by shifting each letter three letters forward in the alphabet B E X A Mathematical expression: f(p) = (p + 3) mod p 25 CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
195
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Example: What is the secret message produced from the message “Meet you in the park” Solution: Replace letters with numbers: meet = you = in = the = park = Replace each of these numbers p by f(p) = (p + 3) mod 26 meet = you = in = the = park = Translate back into letters: “PHHW BRX LQ WKH SDUN” CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
196
CS 210 Discrete Mathematics
Dr. Djamel Bouchaffra Decryption (Deciphering) f(p) = (p + k) mod 26 (shift cepher) f -1(p) = (p – k) mod 26 Caesar’s method and shift cipher are very vulnerable and thus have low level of security (reason frequency of occurrence of letters in the message) Replace letters with blocks of letters. CSE 504, Chapter 2 (Part 2): The Fundamentals: Algorithms, the Integers & Matrices Chapter 3 (part 2)
197
CSE 504 Discrete Mathematics & Foundations of Computer Science
Dr. Djamel Bouchaffra Chapter 3 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices Integers & Algorithms (Section 3.6) © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Ch. 2 (Part 2) Section 2.5 & 2.7
198
Integers & Algorithms (3.6)
Introduction Algorithm is a set a procedures that handle arithmetic operations Use binary representations to enter the world of computer arithmetic Good illustration of the complexity of an algorithm Role of the Euclidean algorithm Base b expansion and modular exponentiation, important in cryptography CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices
199
Integers & Algorithms (3.6) (cont.)
Representations of integers Theorem 1 Let b be a positive integer grater than 1. Then if n is a positive integer, it can be expressed uniquely in the form n = akbk + ak-1bk-1 +…+ a1b + a0, where k is a nonnegative integer, a0, a1, …,ak are nonnegative integers less than b, and ak 0. The representation of n given in theorem 1 is known as the base b expansion of n. CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices
200
Integers & Algorithms (3.6) (cont.)
Example: What is the decimal expansion of the integer that has ( )2 as its binary expansion? Solution: ( )2 = 1*28 + 0*27 + 1*26 + 0*25 + 1* *23 + 1*22 + 1*21 + 1* = 351 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices
201
Integers & Algorithms (3.6) (cont.)
Hexadecimal expansions = base 16 expansion 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F The letters A through F represent the digits corresponding to the number 10 through 15. Example: What is the decimal expansion of the hexadecimal expansion of (2AE0B)16? Solution: (2AE0B)16 = 2* * * * *160 = (175627)10 Each hexadecimal digit can be represented using 4 bits. ( )2 = (E5) (E16 = (1110)2 ; 516 = (0101)2) Bytes: bit strings of length 8 represented by two hexadecimal digits. CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices
202
Integers & Algorithms (3.6) (cont.)
Representations of integers (cont.) Base conversion Example: Find the base 8 (or octal) expansion of (12345)10 Solution: First, divide by = 8 * 1543 = 8 * = 8 * 24 = 8 * 3 +0 3 = 8 * (12345)10 = (30071)8 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices
203
Integers & Algorithms (3.6) (cont.)
Example: Find the hexadecimal expansion of ( )2 and the binary expansion of (A8D)16. Solution: Form blocks of 4 digits and add zeros at the start of the leftmost block if necessary. They are: 0011, 1110, 1011 and 1100. Convert each binary block into decimal (0011)2 = 316; (1110)2 = E16; (1011)2 = B16; (1100)2 = C16 Therefore, ( )2 = (3EBC)16. The conversion of (A8D)16 into binary notation requires replacing each hexadecimal digit by a block of 4 binary digits. They are: A16 = (1010)2, 816 = (1000)2, D16 = (1101)2 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices
204
Integers & Algorithms (3.6) (cont.)
Algorithms for integer operations Goal 1: Compute the sum of two integers expressed with their binary expansions. The complexity will be based on the number of bits used. a = (an-1an-2 … a1a0)2, b = (bn-1bn-2 … b1b0)2 a + b? (sum of two n-bits numbers) First: a0 + b0 = c0* 2 + s0 where s0 = rightmost bit in the binary expansion of a + b c0 = carry = 0 or 1. Second: a1 + b1 + c0 = c1* 2 + s1 where s1 = next bit of the sum expansion c1 = carry … a + b = (snsn-1sn-2 … s1s0)2 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices
205
Integers & Algorithms (3.6) (cont.)
Example: Add a = (1110)2 and b = (1011)2 Solution: a0 + b0 = = 0 * c0 = 0, s0 = 1 a1 + b1 + c0 = = 1 * c1 = 1, s1 = 0 a2 + b2 + c1 = = 1 * c2 = 1, s2 = 0 a3 + b3 + c2 = = 1 * c3 = 1, s3 = 1 s4 = c3 = 1 a + b = ( )2 1 = a = b 1 1 = c CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices
206
Integers & Algorithms (3.6) (cont.)
Goal 2: Compute the product since b = bn-1bn-2…b1b0 then: ab = a (b020 + b121 + … + bn-12n-1) = a (b020) + a(b121) + … + a(bn-12n-1) Using this latter equation, we can compute a * b. abj = a if bj = 1 and 0 otherwise. Each time we multiply a term by 2, we shift its binary expansion one place to the left and add a zero at the tail end of the expansion. CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices
207
Integers & Algorithms (3.6) (cont.)
Example: Find the product of a = (110)2 and b = (101)2 Solution: ab020 = (110)2 * 1 * 20 = (110)2 ab121 = (110)2 * 0 * 21 = (0000)2 (4 zeros-shift) ab222 = (110)2 * 1 * 22 = (11000)2 add 2-zero bits CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices
208
Integers & Algorithms (3.6) (cont.)
To find the product, we need to perform: (110)2 + (0000)2 + (11000) ab = (11110)2 Other exercise: check for example: 7 * 5 = 35 is true in binary expansion. 1 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices
209
Integers & Algorithms (3.6) (cont.)
The Euclidean algorithm Goal: Compute the great common divisor (gcd) of two integers avoiding the prime factorization of each number which is time-consuming. CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices
210
Integers & Algorithms (3.6) (cont.)
Illustration: Find gcd(91, 287) Divide 287 (the larger) by 91 (the smaller) 287 = 91 * Any divisor of 91 and 287 must be a divisor of 287 – 91 * 3 =14 (Theorem 1, part 1 & 2) Any divisor of 91 and 14 divides 287 = 91 * gcd(91,287) = gcd(91,14) Next divide 91 by 14 to obtain: 91 = 14 * Using the same reasoning, our next step is to: Divide 14 by 7: 14 = 7 * 2 since 7/14 gcd(14,7) = 7. But since gcd(287,91) = gcd(91,14) = gcd (14,7) = 7, we have solved the problem CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices
211
Integers & Algorithms (3.6) (cont.)
Lemma 1: Let a = bq + r, where a, b, q and r are integers. Then we can write: gcd(a,b) = gcd(b,r) Example: Determine the gcd of 414 and 662 using the Euclidean algorithm. Solution: 662 = 414 * = 248 * 248 = 166 * = 82 * = 2 * 41 Therefore: gcd(414,662) = 2 (since 2 is the last nonzero remainder !) CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices
212
CSE 504 Discrete Mathematics & Foundations of Computer Science
Dr. Djamel Bouchaffra Chapter 3 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices Matrices (Section 3.8) © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Ch. 2 (Part 4) Section 2.7
213
Introduction Express relationship between elements in set
Solve large systems of equations Useful in graph theory CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
214
Example: The matrix is a 3 X 2 matrix.
Definition 1 A matrix is a rectangular array of numbers. A matrix with m rows and n columns is called an m x n matrix. The plural of matrix is matrices. A matrix with the same number of rows as columns is called square. Two matrices are equal if they have the same number of rows and the same number of columns and the corresponding entries in every position are equal. Example: The matrix is a 3 X 2 matrix. CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
215
Definition 2 Let The ith row of A is the 1 x n matrix [ai1, ai2, …, ain]. The jth column of A is the n x 1 matrix The (i, j)th element or entry of A is the element aij, that is, the number in the ith row and jth column of A. A convenient shorthand notation for expressing the matrix A is to write A = [aij], which indicates that A is the matrix with its (i, j)th element equal to aij. CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
216
Matrix Arithmetic Definition 3
Let A = [aij] and B = [bij] be m x n matrices. The sum of A and B, denoted by A + B, is the m x n matrix that has aij + bij as its (i, j)th element. In other words, A + B = [aij + bij]. Example: CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
217
Cij = ai1b1j + ai2b2j + … + aikbkj.
Definition 4 Let A be an m x k matrix and B be a k x n matrix. The product of A and B, denoted by AB, is the m x n matrix with its (i, j)th entry equal to the sum of the products of the corresponding elements from the ith row of A and the jth column of B. In other words, if AB = [cij], then Cij = ai1b1j + ai2b2j + … + aikbkj. CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
218
Example: Let Find AB if it is defined. Solution: Since A is a 4 x 3 matrix and B is a 3 x 2 matrix, the product AB is defined and is a 4 x 2 matrix. To find the elements of AB, the corresponding elements of the rows of A and the columns of B are first multiplied and then these products are added. For instance, the element in the (3, 1)th position of AB is the sum of the products of the corresponding elements of the third row of A and the first column of B; namely 3 * * * 3 = 7. When all the elements of AB are computed, we see that Matrix multiplication is not commutative.
219
Example: Let Does AB = BA? Solution: We find that Hence, AB BA.
CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
220
Matrix chain multiplication
Problem: How should the matrix-chain A1A2…An be computed using the fewest multiplication of integers, where A1A2…An are m1 x m2, m2 x m3, …, mn x m n+1 matrices respectively and each has integers as entries? CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
221
Solution: 2 possibilities to compute A1A2A3
Example: A1 = 30 x 20 (30 rows and 20 columns) A2 = 20 x A3 = 40 x 10 Solution: 2 possibilities to compute A1A2A3 A1 (A2A3) (A1A2)A3 1) First A2A3 requires 20 * 40 * 10 = 8000 multiplications A1(A2A3) requires 30 * 20 * 10 = 6000 multiplications Total: multiplications. 2) First A1A2 requires 30 * 20 * 40 = multiplications (A1A2)A3 requires 30 * 40 * 10 = Total: multiplications. (1) is more efficient! CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
222
Transposes and power matrices
Definition 5 The identity matrix of order n is the n x n matrix In = [ij], where ij = 1 if i = j and ij = 0 if i j. Hence CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
223
Example: The transpose of the matrix is .
Definition 6 Let A = [aij] be an m x n matrix. The transpose of A, denoted At, is the n x m matrix obtained by interchanging the rows and the columns of A. In other words, if At = [bij], then bij = aij for i = 1, 2, …, n and j = 1, 2, …, m. Example: The transpose of the matrix is CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
224
Example: The matrix is symmetric.
Definition 7 A square matrix A is called symmetric if A = At. Thus A = [aij] is symmetric if aij = aji for all i and j with 1 i n and 1 j n. Example: The matrix is symmetric. CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
225
Zero-one matrices It is a matrix with entries that are 0 or 1. They represent discrete structures using Boolean arithmetic. We define the following Boolean operations: CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
226
Definition 8 Let A = [aij] and B = [bij] be m x n zero-one matrices. Then the join of A and B is the zero-one matrix with (i, j)th entry aij bij. The join of A and B is denoted A B. The meet of A and B is the zero-one matrix with (i, j)th entry aij bij. The meet of A and B is denoted by A B. CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
227
Example: Find the join and meet of the zero-one matrices Solution: We find that the joint of A and B is: The meet of A and B is: CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
228
cij = (ai1 b1j) (ai2 b2j) … (aik bkj).
Definition 9 Let A = [aij] be an m x k zero-one matrix and B = [bij] be a k x n zero-one matrix. Then the Boolean product of A and B, denoted by A B, is the m x n matrix with (i, j)th entry [cij] where cij = (ai1 b1j) (ai2 b2j) … (aik bkj). CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
229
Example: Find the Boolean product of A and B, where
Solution: CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
230
Algorithm The Boolean Product
procedure Boolean product (A,B: zero-one matrices) for i := 1 to m for j := 1 to n begin cij := 0 for q := 1 to k cij := cij (aiq bqj) end {C = [cij] is the Boolean product of A and B} CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
231
Definition 10 Let A be a square zero-one matrix and let r be a positive integer. The rth Boolean power of A is the Boolean product of r factors of A. The rth Boolean product of A is denoted by A[r]. Hence (This is well defined since the Boolean product of matrices is associative.) We also define A[0] to be In. CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
232
Example: Let . Find A[n] for all positive integers n.
Solution: We find that We also find that Additional computation shows that The reader can now see that A[n] = A[5] for all positive integers n with n 5.
233
Exercises 2a p.204 4b p.204 8 p.205 28 p.206 30 p.206 CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
234
Chapter 4 (Part 1): Induction & Recursion
Proof Strategy (4.1)
235
CS 210 Discrete Structures, Chapter 4 (Part 1)
Proof Strategy (4.1) Introduction Provide advice on how to find a proof of a theorem How proofs are constructed Method of proof by cases
236
Proof Strategy (4.1) (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 1) Proof Strategy (4.1) (cont.) Proof Strategies Proof of a statement Replace terms by their definition Analyze what the hypotheses and conclusions mean
237
Proof Strategy (4.1) (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 1) Proof Strategy (4.1) (cont.) Prove the result using one of the available methods of proof Example: p q Go from p and derive q Assume q false and p true and derive a contradiction Prove the contrapositive q p.
238
Proof Strategy (4.1) (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 1) Proof Strategy (4.1) (cont.) Strategy of proof of an implication The forward reasoning Start with hypotheses Use axioms and known theorem with hypotheses Use a sequence of logical steps to derive the conclusion This is the most common type of reasoning to prove relatively simple results.
239
Proof Strategy (4.1) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science Dr. Djamel Bouchaffra CS 210 Discrete Structures, Chapter 4 (Part 1) Proof Strategy (4.1) (cont.) The backward reasoning Start with the negation of the conclusion Use a sequence of logical steps to derive the negation of the hypotheses Unfortunately, forward reasoning is often difficult to use to prove more complicated results. We often use backward reasoning. Ch. 3 (Part 1): Sections 3.1 & 3.2
240
Proof Strategy (4.1) (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 1) Proof Strategy (4.1) (cont.) Example: Prove that the arithmetic mean is always greater than the geometric mean for positive real numbers. Proof: (a + b)/2 > ab (a + b)2/4 > ab (a + b)2 > 4ab a2 + 2ab + b2 > 4ab a2 – 2ab + b2 > 0 (a – b)2 > 0 since this last inequality is always true if a b and since all these inequalities are equivalent, it follows that (a + b)/2 > ab (a b).
241
Proof Strategy (4.1) (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 1) Proof Strategy (4.1) (cont.) Example: Show that there are no solutions in integers x and y of x2 + 3y2 = 8. Proof: when |x| 3 x2 > 8 when |y| 2 3y2 > 8 The remaining cases are those when x takes one of the values –2, -1, 0, 1 or 2 and y takes one of the values –1, 0 or 1. By computing x2 and 3y2 with all these possible values and by combining the sum x2 + 3y2, we find that the largest sum is equal to 7. Therefore, it is impossible for x2 + 3y2 = 8 to hold when x and y are integers.
242
Proof Strategy (4.1) (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 1) Proof Strategy (4.1) (cont.) Conjecture and Proof Conjecture: Statement whose truth value is unknown No matter how a conjecture was made, once it has been formulated, the goal is to prove or disprove it When mathematicians believe a conjecture may be true, they try to find a proof If proofs cannot be found by mathematicians, they may look for a counterexample Few conjecture resist attack for hundred of years and lead to the development of new parts of mathematics
243
Proof Strategy (4.1) (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 1) Proof Strategy (4.1) (cont.) Example: Conjecture: Do other primes of the special form an –1, where a and n are positive integers, exist? 26 –1 = 63, –1 = 255 34 –1 = 80, –1 = 1023 These numbers are not prime! We cannot find any primes beside the Mersenne primes (2p-1) We can conjecture that an-1 is composite when a > or when a = 2 and n is composite
244
Proof Strategy (4.1) (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 1) Proof Strategy (4.1) (cont.) Proof: We need to find a factor of an-1 when a > 2 or a = 2 and n is composite. an-1 = (a –1) (an-1 + an-2 + … + a + 1) when a =2 a –1 = 2 –1 = 1 we cannot conclude! when a > 2 a –1 is a factor of an –1 with 1 < a –1 < an –1 an –1 is not prime. when a = 2 and n is composite r and s with 1 < r < n and 1 < s < n such that n = rs an-1 = ars –1 = (ar –1) (ar(s-1) + …+ ar +1) ar –1 is a factor of an –1.
245
Proof Strategy (4.1) (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 1) Proof Strategy (4.1) (cont.) Theorem 1: The integer an-1 is composite when a > 2 or when a = 2 and n is composite
246
Proof Strategy (4.1) (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 1) Proof Strategy (4.1) (cont.) Conjectures & Counterexamples The role of open problems One famous problem unsolved for approximately three hundred years led to the development of an entire branch of number theory This problem asked whether the statement known as Fermat’s last theorem is true
247
Proof Strategy (4.1) (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 1) Proof Strategy (4.1) (cont.) Theorem 2: Fermat’s Last Theorem The equation xn + yn = zn has no solutions in integers x, y, and z with xyz 0 whenever n is an integer with n > 2. Proof: Refer to Andrew Wiles (Theory of Elliptic Curves)
248
Proof Strategy (4.1) (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 1) Proof Strategy (4.1) (cont.) Example: The 3x + 1 conjecture When we repeatedly apply the function f, we will eventually reach the integer 1.
249
Proof Strategy (4.1) (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 1) Proof Strategy (4.1) (cont.) x = 13 f(13) = 3 * = 40 x = 40 f(40) = 40/2 = 20 x = 20 f(20) = 20/2 = 10 x = 10 f(10) = 10/2 = 5 x = 5 f(5) = 3 * = 16 x = 16 f(16) =16/2 = 8 x = 8 f(8) = 8/2 = 4 x = 4 f(4) = 4/2 =2 x = 2 f(2) = 1 This conjecture has been verified for all integers x up to 5 * 6 * 1013.
250
CS 210 Discrete Structures, Chapter 4 (Part 1)
Exercises Section 3.1: #6, 8, 10 and 12.
251
Chapter 4 (Part 2): Mathematical Reasoning, Induction & Recursion
CSE 504 Discrete Structures & Foundations of Computer Science Dr. Djamel Bouchaffra Chapter 4 (Part 2): Mathematical Reasoning, Induction & Recursion Recursive Definitions (3.4) Ch. 3 (Part 2): Sections 3.4
252
CS 210 Discrete Structures, Chapter 4 (Part 2)
Introduction Define an object in terms of itself: this process is called recursion
253
CS 210 Discrete Structures, Chapter 4 (Part 2)
Introduction (cont.) We can use recursion to define sequences, functions and sets Example: the sequence of powers of 2 is given by an = 2n for n = 0, 1, 2, … (explicitly defined) a0 = 1; an+1 = 2an for n = 0, 1, 2, … (recursively defined)
254
Recursively Defined Functions
CS 210 Discrete Structures, Chapter 4 (Part 2) Recursively Defined Functions We use 2 steps to define a function recursively: Basis step: specify the the value of the function at 0 Recursive step: give a rule for determining its value at an integer from its value at smaller integers Such a definition is called a recursive or induction definition
255
Recursively Defined Functions (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 2) Recursively Defined Functions (cont.) Example: f defined recursively as: f(0) = 3 f(n + 1) = 2 f(n) + 3 determine f(1), f(2), f(3) and f(4) Solution: by definition: f(1) = 2f(0) + 3 = 2 * = 9 f(2) = 2f(1) + 3 = 2 * = 21 f(3) = 2f(2) + 3 = 2 * = 45 f(4) = 2f(3) + 3 = 2 * = 93
256
Recursively Defined Functions (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 2) Recursively Defined Functions (cont.) Example: Give an inductive definition of f(n) = n! Solution: Basis step: Specify the initial value of this function; f(0) = 1. Recursive step: give a rule for determining f(n + 1) from f(n) (n + 1)! = (n + 1) * n! f(n + 1) = (n + 1)*f(n)
257
Recursively Defined Functions (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 2) Recursively Defined Functions (cont.) Example: Give a recursive definition of Solution: Basis step: Recursive step:
258
Recursively Defined Functions (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 2) Recursively Defined Functions (cont.) Definition 1: The Fibonacci numbers, f0, f1, f2, …, are defined by the equations f0 = 0, f1 = 1, and fn = fn-1 + fn-2 for n = 2, 3, 4, …
259
Recursively Defined Functions (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 2) Recursively Defined Functions (cont.) Example: Determine the Fibonacci numbers f2, f3, f4, f5 and f6 Solution: Basis step: f0 = 0 and f1 = 1 Recursive step: fn = fn-1 + fn-2 f2 = f1 + f0 = = 1 f3 = f2 + f1 = = 2 f4 = f3 + f2 = = 3 f5 = f4 + f3 = = 5 f6 = f5 + f4 = = 8 Recursively defined sets and structures.
260
Recursively Defined Functions (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 2) Recursively Defined Functions (cont.) Question: How can sets be defined recursively? Example: Consider the subset S of the set of integers defined by: Basis step: 3 S Recursive steps: if x S y S x + y S. 3 S (basis step) 3 S 3 S = 6 S (recursive step) 6 S 3 S = 9 S (recursive step) 6 S 6 S = 12 S (recursive step)
261
Recursively Defined Functions (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 2) Recursively Defined Functions (cont.) Definition 2: The set * of strings over the alphabet can be defined by Basis step: * (where is the empty string containing no symbols) Recursive step: if w * and x , then wx *. Example: if = {0, 1} Basis step: * Recursive step: 0, 1 (0 *, 1 *) Recursive step: *
262
Recursively Defined Functions (cont.)
Definition 3 Two strings can be combined via the operation of concatenation. Let be a set of symbols and * the set of strings formed from symbols in . We can define the concatenation of two strings, denoted by recursively as follows: Basis step: if w *, then w = w, where is the empty string. Recursive step: if w1 *, w2 * and x , then w1 (w2x) = (w1 w2) x. Example: w1= abra w2 = cadabra w1w2 = abracadabra
263
Recursively Defined Functions (cont.)
CS 210 Discrete Structures, Chapter 4 (Part 2) Recursively Defined Functions (cont.) Definition 4 The set of rooted trees, where a rooted tree consists of a set of vertices containing a distinguished vertex called the root, and edges connecting these vertices, can be defined recursively by these steps: Basis step: a single vertex r is a rooted tree. Recursive step: Suppose that T1, T2, …, Tn are rooted trees with roots r1, r2, …, rn, respectively. Then the graph formed by starting with a root r, which is not in any of the rooted trees T1, T2, …, Tn , and adding an edge from r to each of the vertices r1, r2, …, rn, is also a rooted tree.
264
Building up rooted trees
Basis step Step 1 Step 2 … …
265
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
CSE 504 Discrete Structures & Foundations of Computer Science Dr. Djamel Bouchaffra Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion Recursive Algorithms (4.4) Program Correctness (4.5) Ch. 3 (Part 3): Sections 3.5 & 3.6
266
Recursive Algorithm (4.4)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive Algorithm (4.4) Goal: Reduce the solution to a problem with a particular set of input to the solution of the same problem with smaller input values Example: Greater Common Divisor (gcd) gcd(a,b) = gcd(b mod a, a) gcd(4,8) = gcd(8 mod 4, 4) = gcd(0,4) = 4
267
Recursive algorithm (4.4) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.) Definition 1: An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input. Example: Give a recursive algorithm for computing an; a 0, n>0 Solution: an+1 = a*an for n> a0 = 1
268
Recursive algorithm (4.4) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.) A recursive algorithm for computing an Procedure power(a: nonzero real number, n: nonnegative integer) if n = 0 then power(a, n):= 1 Else power(a,n) := a * power(a, n-1)
269
Recursive algorithm (4.4) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.) Example: Give a recursive algorithm for computing the greatest common divisor of two nonnegative integers a and b (a<b) Solution: gcd(a,b) = gcd(b mod a, a) and the condition gcd(0,b) = b (b>0).
270
Recursive algorithm (4.4) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.) A recursive algorithm for computing gcd(a,b) procedure gcd(a, b: nonnegative integers with a<b) if a = 0 then gcd(a,b) := b else gcd(a,b) := gcd(b mod a, a)
271
Recursive algorithm (4.4) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.) Example: Express the linear search as a recursive procedure: search x in the search sequence aiai+1… aj. A Recursive linear search algorithm procedure search(i, j, x) if ai = x then location := i else if i = j then location := 0 else search(i + 1, j,x)
272
Recursive algorithm (4.4) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.) Recursion & iteration We need to express the value of a function at a positive integer in terms of the value of the function at smaller integers Example: Compute a recursive procedure for the evaluation of n!
273
Recursive algorithm (4.4) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.) A recursive procedure for factorials procedure factorial(n: positive integer if n = 1 then factorial(n) := 1 else factorial(n) := n * factorial(n - 1)
274
Recursive algorithm (4.4) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.) Recursion & iteration (cont.) However, instead of reducing the computation to the evaluation of the function at smaller integers, we can start by 1 and explore larger in an iterative way
275
Recursive algorithm (4.4) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.) An iterative (vs. recursive) procedure for factorials procedure iterative factorial(n: positive integer) x := 1 for i := 1 to n x := i * x {x is n!}
276
Recursive algorithm (4.4) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Recursive algorithm (4.4) (cont.) Recursion & iteration (cont.) An other example is the recursive algorithm for Fibonacci numbers procedure fibonacci(n: nonnegative integer) if n = 0 then fibonacci(0) := 0 else if n = 1 then fibonacci(1) := 1 else fibonacci(n) := fibonacci(n – 1) + fibonacci(n – 2)
277
Recursive algorithm (4.4) (cont.)
An iterative algorithm for computing Fibonacci numbers procedure iterative fibonacci(n: nonnegative integer) if n = 0 then y := 0 else begin x := 0 y := 1 for i := 1 to n – 1 z := x + y x := y y := z end {y is the nth Fibonacci number}
278
Program Correctness (4.5)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) Introduction Question: How can we be sure that a program always produces the correct answer? The syntax is correct (all bugs removed!) Testing a program with a randomly selected sample of input data is not sufficient Correctness of a program should be proven! Theoretically, it is never possible to mechanize the proof of correctness of complex programs We will cover some of the concepts and methods that prove that “simple” programs are corrects
279
Program Correctness (4.5) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) Program verification To prove that a program is correct, we need two parts: For every possible input, the correct answer is obtained if the program terminates The program always terminates
280
Program Correctness (4.5) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) Program verification (cont.) Definition 1: A program, or program segment, S is said to be partially correct with respect to the initial assertion p and the final assertion q if whenever p is true for the input values of S and S terminates, then q is true for the output values of S. The notation p{S}q indicates that the program, or program segment, S is partially correct with respect to the initial assertion p and the final assertion q.
281
Program Correctness (4.5) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) This definition of partial correctness (has nothing to do whether a program terminates) is due to Tony Hoare Example: Show that the program segment y := 2 z := x + y is correct with respect to the initial assertion p: x =1 and the final assertion q: z =3. Solution: p is true x = 1 y := 2 z := 3 partially correct w.r.t. p and q
282
Program Correctness (4.5) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) Rules of inference Goal: Split the program into a series of subprograms and show that each subprogram is correct. This is done through a rule of inference. The program S is split into 2 subprograms S1 and S2 (S = S1; S2) Assume that we have S1 correct w.r.t. p and q (initial and final assertions) Assume that we have S2 correct w.r.t. q and r (initial and final assertions)
283
Program Correctness (4.5) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) It follows: “if p is true (S1 executed and terminates) then q is true” “if q is true (S2 executed and terminates) then r is true” “thus, if p = true and S = S1; S2 is executed and terminates then r = true” This rule of inference is known as the composition rule. It is written as: p {S1}q q {S2}r p {S1; S2) r
284
Program Correctness (4.5) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) Conditional Statements Assume that a program segment has the following form: “if condition then S” where S is a block of statement Goal: Verify that this piece of code is correct Strategy: We must show that when p is true and condition is also true, then q is true after S terminates We also must show that when p is true and condition is false, then q is true
285
Program Correctness (4.5) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) We can summarize the conditional statement as: (p condition) {S}q (p condition) q p {if condition then S) q Example: Verify that the following program segment is correct w.r.t. the initial assertion T and the final assertion y x if x > y then y:= x Solution: If T = true and x>y is true then the final assertion y x is true If T = true and x>y is false then x y is true final assertion is true again
286
Program Correctness (4.5) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) “if condition then S1 else S2” if condition is true then S1 executes; otherwise S2 executes This piece of code is correct if: If p = true condition = true q = true after S1 terminates If p = true condition = false q = true after S2 terminates (p condition) {S1}q (p condition) {S2}q p {if condition then S1 else S2) q
287
Program Correctness (4.5) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) Example: Check that the following program segment if x < 0 then abs := -x else abs := x is correct w.r.t. the initial assertion T and the final assertion abs = |x|. Solution: If T = true and (x<0) = true abs := -x; compatible with definition of abs If T = true and (x<0)= false (x 0) = true abs := x; compatible with abs definition.
288
Program Correctness (4.5) (cont.)
Loop invariants In this case, we prove codes that contain the while loop: “while condition S” Goal: An assertion that remains true each time S is executed must be chosen: Such an assertion is called a loop invariant. In other words, p is a loop invariant if: (p condition) {S}p is true If p is a loop invariant, then if p is true before the program segment is executed, p and condition are true after termination, if it occurs. We can write the rule of inference as: (p condition) {S}p p {while condition S} (condition p)
289
Program Correctness (4.5) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) Example: Determine the loop invariant that verifies that the following program segment terminates with factorial = n! when n 0. i := 1 factorial := 1 While i < n begin i := i + 1 factorial := factorial * i end
290
Program Correctness (4.5) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) Solution: Choose p = (factorial = i! (i n)) Prove that p is in fact a loop invariant If p is true before execution, p and condition are true after termination Prove that the while loop terminates
291
Program Correctness (4.5) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) P is a loop invariant: Suppose p is true at the beginning of the execution of the while loop and the while condition holds; factorial = i! i < n inew = i + 1 factorialnew = factorial * (i + 1) = (i + 1)! = inew! Since i < n inew = i + 1 n p true at the end of the execution of the loop p is a loop invariant
292
Program Correctness (4.5) (cont.)
CSE 504 Discrete Structures & Foundations of Computer Science, Chapter 4 (Part 3) Program Correctness (4.5) (cont.) Before entering the loop, i = 1 n and factorial :=1 = 1! = i! (i n) (factorial = i!) = true p = true Since p is a loop invariant through the inference rule, if the while loop terminates p = true and i < n false i = n and factorial = i! = n! While loop terminates: Because p is a loop invariant, the rule of inference implies that if the while loop terminates, it terminates with p true and with i<n false. In this case, at the end, factorial = i! and i<=n are true, but i<n is false, in other words, i=n and factorial= i!=n!, as desired.
293
CSE 504 Discrete Structures & Foundations of Computer Science
Dr. Djamel Bouchaffra Chapter 5: Counting The Basics of Counting (5.1) The Pigeonhole Principle (5.2) Permutations & Combinations (5.3) Generating Permutations & Combinations (5.6) Ch. 4: Counting (sections: 4.1, 4.2, 4.3 & 4.6
294
Basics of Counting (5.1) Introduction
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Basics of Counting (5.1) Introduction Study of gambling games Complexity of algorithms Probabilities of events Allowable passwords on a computer system Pigeonhole problem (among a set of 15 or more students at least 3 were born on the same day of the week) Dr. Djamel Bouchaffra
295
Basics of Counting (5.1) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Basics of Counting (5.1) (cont.) Basics counting principles Product rule Suppose that a procedure can be broken down into a sequence of two tasks. If there are n1 ways to do the first task and n2 ways to do the second task after the first task has been done, then there are n1n2 ways to do the procedure. Dr. Djamel Bouchaffra
296
Basics of Counting (5.1) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Basics of Counting (5.1) (cont.) Example: There are 32 microcomputers in a computer center. Each microcomputer has 24 ports. How many different ports to a microcomputer in the center are there? Solution: The procedure of choosing consists of two tasks: Picking a microcomputer Picking a port on this microcomputer Since there are 32 ways to choose the microcomputer and 24 to choose the port no matter which microcomputer has been selected. Product rule 32 * 24 = 768 ports. Dr. Djamel Bouchaffra
297
Basics of Counting (5.1) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Basics of Counting (5.1) (cont.) Product rule is often phrased in terms of sets: If A1 , A2, …, Am are finite sets, then the number of elements in the Cartesian product of these sets is the product of the number of elements in each set |A1 * A2 * … * Am| = |A1|* |A2| * …* |Am| Dr. Djamel Bouchaffra
298
Basics of Counting (5.1) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Basics of Counting (5.1) (cont.) The sum rule If a first task can be done in n1 ways and a second task in n2 ways, and if theses tasks cannot be done at the same time, then there are n1 + n2 ways to do one of these tasks Dr. Djamel Bouchaffra
299
Basics of Counting (5.1) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Basics of Counting (5.1) (cont.) Example: What is the value of k after the following code has been executed? k := 0 for i1 := 1 to n1 k := k + 1 for i2 := 1 to n2 … for im := 1 to nm Dr. Djamel Bouchaffra
300
Basics of Counting (5.1) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Basics of Counting (5.1) (cont.) Solution: Initial value of k = 0 m = different loops Each time a loop is traversed k+1 Ti = task of traversing the ith loop. The task Ti can be done in ni ways, since the ith loop is traversed ni times. Since no two of these tasks can be done at the same time, the sum rule shows that the final value of k, which is the number of ways to do one of the tasks Ti, i = 1, 2, …, m, is n1 + n2 + … + nm. Dr. Djamel Bouchaffra
301
Basics of Counting (5.1) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Basics of Counting (5.1) (cont.) The sum rule can be phrased in terms of sets If A1, A2, …, Am are disjoint sets then the number of elements in the union of theses sets is the sum of the numbers of the elements in them. |A1 A2 … Am| = |A1| + |A2| + …+ |Am| Dr. Djamel Bouchaffra
302
Basics of Counting (5.1) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Basics of Counting (5.1) (cont.) The inclusion –exclusion principle When two tasks can be done at the same time, we cannot use the sum rule to count the number of ways to do one of the two tasks. To correctly count the number of ways to do one of the two tasks, we add the number of ways to do each of the two tasks and then subtract the number of ways to do both tasks. This technique is called the principle of inclusion-exclusion Dr. Djamel Bouchaffra
303
Basics of Counting (5.1) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Basics of Counting (5.1) (cont.) Example: How many bit strings of length eight either start with a 1 bit or end with a the two bits 00? Solution: Number of bit strings of length 8 beginning with a 1 bit = 27 = 128 ways (first bit can be chosen in only one way; and each of the other seven bits can be chosen in two ways product rule: 27 * 1 = 27) Number of bits strings of length 8 ending with the 2 bits 00 = 26 = 64 ways (each of the first six bits can be chosen in two ways and the last two bits can be chosen in only one way) product rule: 26 * 1 = 26 Dr. Djamel Bouchaffra
304
Basics of Counting (5.1) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Basics of Counting (5.1) (cont.) Solution (cont.): Both previous tasks constructs a bit string of length 8 that begins with 1 and ends with 00 which are 25 = 32 ways ( first bit can be chosen in only one way, each of the second through the sixth bits can be chosen in two ways, and the last two bits can be chosen in only one way 25 * 1 * 1 = 25 Conclusion: the number of bit strings of length 8 that begin with a 1 bit or end with a 00 = – 32 = 160. |A1 A2| = |A1| + |A2| - |A1 A2| Dr. Djamel Bouchaffra
305
The Pigeonhole Principle (5.2)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting The Pigeonhole Principle (5.2) Introduction Suppose that a flock of pigeons flies into a set of pigeonholes to roost. The pigeonhole principle states that if there are more pigeons than pigeonholes, then there must be at least one pigeonhole with at least two pigeons in it Pigeonhole principle theorem If k + 1 or more objects are placed into k boxes, then there is at least one box containing two or more of the objects Dr. Djamel Bouchaffra
306
The Pigeonhole Principle (5.2) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting The Pigeonhole Principle (5.2) (cont.) Proof: Assume that none of the k boxes contains more than one object. Then the total number of objects would be at most k. This is a contradiction, since there are at least k + 1 objects. The pigeonhole principle is also known as the Dirichlet drawer principle (French mathematician of the 19th century) Dr. Djamel Bouchaffra
307
The Pigeonhole Principle (5.2) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting The Pigeonhole Principle (5.2) (cont.) Examples: Among any group of 367 people, there must be at least two with the same birthday, because there are only 366 possible birthdays In any group of 27 English words, there must be at least two that begin with the same letter, since there are 26 letters in the English alphabet Dr. Djamel Bouchaffra
308
The Pigeonhole Principle (5.2) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting The Pigeonhole Principle (5.2) (cont.) The generalized pigeonhole principle When the number of objects exceeds a multiple of the number of boxes When 21 objects are distributed into 10 boxes. One box must have more than 2 objects Dr. Djamel Bouchaffra
309
The Pigeonhole Principle (5.2) (cont.)
The generalized pigeonhole principle theorem states: If N objects are placed into k boxes, then there is at least one box containing at least N/k objects Proof: Suppose that none of the boxes contains more than N/k - 1 objects. Then, the total number of objects is at most where the inequality N/k < (N/k) + 1 has been used. This is a contradiction since there are a total of N objects.
310
The Pigeonhole Principle (5.2) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting The Pigeonhole Principle (5.2) (cont.) Example: Among 100 people there are at least 100/12 = 9 who were born in the same month Example: What is the minimum number of students required in a discrete mathematics class to be sure that at least 6 will receive the same grade, if there are five possible grades, A, B, C, D and F? Solution: The minimum number of students needed to ensure that at least 6 students receive the same grade is the smallest integer N such that N/5 =6. The smallest such integer is N = 5 * = 26. If you have only 25 students, it is possible for there to be five who have received each grade so that no 6 students have received the same grade. Thus, 26 is the minimum number of students needed to ensure that at least 6 students will receive the same grade Dr. Djamel Bouchaffra
311
Permutations & Combinations (5.3)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Permutations & Combinations (5.3) Introduction A tennis team has ten members The coach has to select 5 players to make the trip to a match at another school In addition, the coach has to prepare an ordered list of 4 players to play the 4 single matches Our goal in this section is to count the different unordered collections of the 5 players to make the trip; and the different ordered lists of 4 players to play single matches (answered later!). Dr. Djamel Bouchaffra
312
Permutations & Combinations (5.3) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Permutations & Combinations (5.3) (cont.) Permutations A permutation of a set of distinct objects is an ordered arrangements of these objects. An ordered arrangement of r elements of a set is called an r-permutation Example: Let S = {1, 2, 3, }. The arrangement 3, 1, 2 is a permutation of S. The arrangement 3, 2, is a 2-permutation of S Dr. Djamel Bouchaffra
313
Permutations & Combinations (5.3) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Permutations & Combinations (5.3) (cont.) Theorem 1: The number of r-permutations of a set with n distinct element is: P(n, r) = n(n –1) (n –2) … (n – r +1). Proof: First element can be chosen in n ways Second element can be chosen in (n – 1) ways … rth element can be chosen in (n – r +1) ways Finally using the product rule, there are n(n – 1)(n – 2) … (n – r + 1) ways r-permutations of the set. Consequence: P(n, r) = n (n – 1) (n – 2) … (n – r +1) = n! / [(n – r)!] P(n, n) = n! Dr. Djamel Bouchaffra
314
Permutations & Combinations (5.3) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Permutations & Combinations (5.3) (cont.) Example: How many ways are there to select a first-prize winner, a second-prize winner and a third-prize winner from 100 different people who have entered a contest? Solution: Because it matters which person wins which prize, the number of ways to pick the 3 prize winners is the number of ordered selections of three elements from a set of 100 elements, that is, the number of 3 permutations of a set of 100 elements. P(100, 3) = 100 * 99 * 98 = 970,200 Dr. Djamel Bouchaffra
315
Permutations & Combinations (5.3) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Permutations & Combinations (5.3) (cont.) Combinations An r-combination of elements of a set is an unordered selection of r elements from the set. Thus, an r-combination is simply a subset (of the set) with r elements. Example: Let S be the set {1, 2, 3, 4}. Then {1, 3, 4} is a 3-combination from S. Number of r-combinations of a set with n distinct elements is denoted by C(n, r) Example: We see that C(4,2) = 6, since the two-combinations of {a, b, c, d} are the six subsets {a, b}, {a, c}, {a, d}, {b, c}, {b, d}, {c, d} Dr. Djamel Bouchaffra
316
Permutations & Combinations (5.3) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Permutations & Combinations (5.3) (cont.) Theorem 2: The number of r-combinations of a set with n elements, where n is a nonnegative integer and r is an integer with 0 r n, equals Dr. Djamel Bouchaffra
317
Permutations & Combinations (5.3) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Permutations & Combinations (5.3) (cont.) Corollary 1 Let n and r be nonnegative integers with r n. Then C(n, r) = C(n, n – r). Proof: From theorem 2 it follows that and Hence, C(n, r) = C(n, n –r). Dr. Djamel Bouchaffra
318
Permutations & Combinations (5.3) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Permutations & Combinations (5.3) (cont.) Definition 1: A combinatorial proof is a proof that uses counting arguments to prove a theorem, rather than some other method such as algebraic techniques. Many identities involving binomial coefficients can be proved using combinatorial proofs. An identity can be proved using a combinatorial proof if it can be shown that the two sides of the identity count the same elements, but in different ways. Dr. Djamel Bouchaffra
319
Permutations & Combinations (5.3) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Permutations & Combinations (5.3) (cont.) Example: How many ways are there to select 5 players from a 10-member tennis team to make a trip to a match at another school? Solution: The answer is given by the number of 5-combinations of a set with 10 elements. By theorem 2, the number of such combinations is C(10, 5) = 10! / (5! 5!) = 252. Dr. Djamel Bouchaffra
320
Permutations & Combinations (5.3) (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Permutations & Combinations (5.3) (cont.) Example: How many bit strings of length n contain exactly r 1s? Solution: The positions of r 1s in a bit string of length n form an r-combination of the set {1, 2, 3, …, n}. Hence, there are C(n, r) bit strings of length n that contain exactly r 1s. Dr. Djamel Bouchaffra
321
Generating Permutations & Combinations (5.6)
We are capable to count how many permutation we can obtain to solve a problem However, we were unable to sequentially generate one by one in order to test a certain property of a permutation (such as the distance of a circuit in the salesman problem) Examples: Salesman problem: a salesman must visit 6 different cities. In which order must these cities be visited in order to minimize the total travel time? (determine travel time one by one among the 6! = 720 possibilities!) Some numbers from a set of six numbers have 100 as their sum. One way to find these numbers is to generate all 26 =64 subsets and check the sum of their terms
322
Generating Permutations & Combinations (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Generating permutations Any set with n elements can be placed in a one-to-one correspondence with the set {1, 2, …, n} We can list the permutations of any set of n elements by generating the permutations of the n smallest positive integers and then replacing these integers with the corresponding elements Many algorithms have been developed to generate the n! permutations of this set Dr. Djamel Bouchaffra
323
Generating Permutations & Combinations (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting In the lexicographic ordering of the set of permutations of {1, 2, …, n}, the permutation a1a2…an precedes the permutation b1b2…bn, for some k, 1 k n, a1 = b1; a2 = b2 …; ak-1 = bk-1 and ak < bk Example: The permutation of the set {1, 2, 3, 4, 5} precedes the permutation 23514, since these permutations agree in the first two positions, but the number in the third position in the first permutation, 4, is smaller than the number in the third position in the second permutation, 5. Similarly, the permutation precedes Dr. Djamel Bouchaffra
324
Generating the next largest permutation
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Generating the next largest permutation Case an-1 < an: interchange an-1 and an to obtain a larger permutation Example: Next (234156) = Case an-1 > an a larger permutation cannot be obtained by interchanging these last two terms Example: Next (234165) The last pair of integers aj and aj+1 where aj< aj+1 is a4=1 and a5=6. The least integer to the right of 1 that is greater than 1 in the permutation is a6=5. Hence, 5 is placed in the fourth position. Then the integers 1, 6 are placed in order in the last two positions, giving 16 as the last two positions. Hence, Next (234165) = Dr. Djamel Bouchaffra
325
Algorithm: Generating the next largest permutation ion lexicographic order
procedure next permutation (a1a2… an: permutation of {1,2,…,n} not equal to n n-1 … 2 1) j := n – 1 While aj > aj+1 j := j – 1 {j is the largest subscript with aj < aj+1} k := n While aj > ak k := k – 1 {ak is the smallest integer greater than aj to the right of aj} r := n s := j + 1 while r > s begin interchange ar and as r := r – 1 s := s +1 end {this puts the tail end of the permutation after the jth position in increasing order}
326
Generating Permutations & Combinations (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Generating Combinations Since a combination is a subset, we can use the correspondence between subsets of {a1, a2,… , an} and bit strings of length n 1 ak is in the subset 0 ak is not in the subset {a, b, c, d} is a set of cardinality = 4 cda is a subset 1011 (string of length 4) We need to generate the next largest bit string Dr. Djamel Bouchaffra
327
Generating Permutations & Combinations (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Strategy for string of length n Start with the bit string 000…00 with n zeros Successively find the next largest expansion until the bit string 111…11 is obtained At each stage the next largest binary expansion is found by locating the first position from the right that is not 1 Change all the 1s to the right of this position to 0s and making this first 0 (from the right) a 1 Dr. Djamel Bouchaffra
328
Generating Permutations & Combinations (cont.)
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Example: Find the next largest bit string after Solution: the first bit from the right that is not a 1 is the 4th bit from the right. Change this bit to a 1 and change all the following bits to 0s. This produces the next largest bit string, Dr. Djamel Bouchaffra
329
Algorithm: Generating the next r-combination in lexicographic order
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Algorithm: Generating the next r-combination in lexicographic order procedure next r-combination ({a1, a2, …, ar}: proper subset of {1, 2, …, n} not equal to {n – r + 1, …, n} with a1 < a2 < … < ar) i := r While ai = n – r + i i := i – 1 ai := ai + 1 For j := i + 1 to r aj := ai + j - i Dr. Djamel Bouchaffra
330
Algorithm: Generating the next r-combination in lexicographic order
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Algorithm: Generating the next r-combination in lexicographic order procedure next r-combination ({a1, a2, …, ar}: proper subset of {1, 2, …, n} not equal to {n – r + 1, …, n} with a1 < a2 < … < ar) i := r While ai = n – r + i i := i – 1 ai := ai + 1 For j := i + 1 to r aj := ai + j - i Dr. Djamel Bouchaffra
331
Algorithm: Generating the next r-combination in lexicographic order
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Algorithm: Generating the next r-combination in lexicographic order procedure next r-combination ({a1, a2, …, ar}: proper subset of {1, 2, …, n} not equal to {n – r + 1, …, n} with a1 < a2 < … < ar) i := r While ai = n – r + i i := i – 1 ai := ai + 1 For j := i + 1 to r aj := ai + j - i Dr. Djamel Bouchaffra
332
Algorithm: Generating the next r-combination in lexicographic order
CS 210 Discrete Structure & Foundations of Computer Science, Ch. 5: Counting Algorithm: Generating the next r-combination in lexicographic order procedure next r-combination ({a1, a2, …, ar}: proper subset of {1, 2, …, n} not equal to {n – r + 1, …, n} with a1 < a2 < … < ar) i := r While ai = n – r + i i := i – 1 ai := ai + 1 For j := i + 1 to r aj := ai + j - i Dr. Djamel Bouchaffra
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.