Academia.eduAcademia.edu

Slide 1 - logic and Proof (16 files merged)

AI-generated Abstract

The paper discusses various logical operations and structures, including fundamental Boolean functions and their representations through truth tables and expressions. It explores isomorphism in mathematical structures, specifically within the context of modular arithmetic and transformations of geometric figures, like equilateral triangles. Additionally, the study addresses the nature of noncommutativity in certain operations and emphasizes the significance of associativity and identity elements in mathematical operations.

1 Definition A proposition is a declarative sentence (that is, a sentence that declares a fact) that is either true or false, but not both. Example • Washington, D.C., is the capital of the United States of America • Dhaka is the capital of Bangladesh • Dhaka is the capital of India • 8 + 2 = 10 • 4 + 4 = 10 2 • What time is it? • Read this carefully • a + 2 = 10 • x+y =z 3 Definition variables that represent propositions, just as letters are used to denote numerical variables. The conventional letters used for propositional variables are p, q, r, s, ... . Example : p = Dhaka is the capital of Bangladesh. 4 The truth value The truth value of a proposition is true, denoted by T, if it is a true proposition, and the truth value of a proposition is false, denoted by F, if it is a false proposition. p = Dhaka is the capital of Bangladesh. p = Dhaka is the capital of Pakistan. Truth value : T Truth value : F 5 Propositional Calculus or Propositional Logic. The area of logic that deals with propositions is called the propositional calculus or propositional logic. Compound Propositions New propositions formed from existing propositions using logical operators are called compound propositions. 6 • About a dozen logical operators • Similar to algebraic operators + * - / Basic Operators 1. Negation (NOT) 2. Conjunction (AND) 3. Disjunction (OR) 7 Definition A “not” operation switches (negates) the truth value. Symbol:  or ~ • If 𝑝 = 𝑇𝑜 𝑑𝑎𝑦 𝑖𝑠 𝑀𝑜𝑛𝑑𝑎𝑦. • ¬𝑝 = ? Truth table P p T F F T 8 Definition An “and” operation is true if both operands are true. Symbol:  p = “Today is Friday” q = “Today is my birthday” p q pq T T T p q = ? T F F pq = “Today is Friday and today is my birthday” F T F F F F 9 Definition An “or” operation is true if either operands or both are true. Symbol:  p q p p = “Today is Friday” = “Today is my birthday” q=?  q = “Today is Friday or today is my birthday” p q pq T T T T F T F T T F F F 10 More Operators 1. Exclusive OR 2. Conditional 3. Bicondition 11 Definition Let p and q be propositions. The exclusive or of p and q, denoted by p⊕q, is the proposition that is true when exactly one of p and q is true and is false otherwise. • Symbol:  • Often called XOR • pq  (p  q)  ¬(p  q) pq = “Today is Friday or today is my birthday, but not both” p q pq T T F T F T F T T F F F 12 Definition Let p and q be propositions. The conditional statement p→q is the proposition “if p, then q.” The conditional statement p→q is false when p is true and q is false, and true otherwise. pq = “If today is Friday, then today is my birthday” • In the conditional statement p→q, • p is called the hypothesis (or antecedent or premise) • q is called the conclusion (or consequence) • • • • • • • p implies q If p, q p only if q p is sufficient for q q if p q whenever p q is necessary for p 13 Logical operators: Conditional  p → q = ¬p q the antecedent the consequence Let, p = “Maria learns discrete mathematics” and q = “Maria will find a good job.” Express the statement p→q as a statement in English. p q pq T T T T F F F T T F F T If I am elected, then I will lower taxes. I will do only for myself Definition Let p and q be propositions. The biconditional statement p↔q is the proposition “p if and only if q.” The biconditional statement p↔q is true when p and q have the same truth values, and is false otherwise. Biconditional statements are also called bi-implications. • “p is necessary and sufficient for q” • “if p then q, and conversely” • “p iff q.” 15  True when both has same truth values and otherwise false. • Alternatively, it means “(if p then q) and (if q then p)” (p→q)∧(q→p). • Note that a bi-conditional has the opposite truth values of the exclusive or • Let p = “You take this class” and q = “You get a grade” • p q = “You take this class if and only if you get a grade” • Alternatively, it means “If you take this class, then you get a grade and if you get a grade then you take (took) this class” p q pq T T T T F F F T F F F T Example Construct the truth table of the compound proposition (p∨¬q)→(p∧q). 17 and or xor conditional Bi-conditional p q pq pq pq pq pq T T T T F T T T F F T T F F F T F T T T F F F F F F T T Learn what they mean, don’t just memorize the table! 18 • Just as in algebra, operators have precedence 4+3*2 = ?  4+3*2 = 4+(3*2)  4+3*2 = (4+3)*2 19 Precedence of operators  p  q  ¬r → s ↔ t = ? Operators Precedence =(p  (q  (¬r)) → s) ↔ (t)   First three are the most important Not is always performed before any other operation ¬ 1   → ↔ 2 3 4 5 21 • Translating English Sentences • System Specifications • Boolean Searches • Logic Puzzles • Logic Circuits 22 Example p = “It is below freezing” q = “It is snowing” It is below freezing and it is snowing pq It is below freezing but not snowing p¬q If it is below freezing, it is also snowing p→q It is either below freezing or it is snowing, but it is not snowing if it is below freezing (pq)(p→¬q) That it is below freezing is necessary and sufficient for it to be snowing p↔q 23 Problem: How can this English sentence be translated into a logical expression? “You can access the Internet from campus only if you are a computer science major or you are not a freshman.” Solve: Let, a = “You can access the Internet from campus,” c = “You are a computer science major,” and f = “You are a freshman,” Then the statement can be represented as a → (c ∨ ¬f). 24 Problem: How can this English sentence be translated into a logical expression? “You can access the Internet from campus only if you are a computer science major or you are not a freshman.” Solve: Let, a = “You can access the Internet from campus,” c = “You are a computer science major,” and f = “You are a freshman,” Then the statement can be represented as a → (c ∨ ¬f). 25 How can this English sentence be translated into a logical expression? “You can not ride the roller coaster if you are under 4 feet tall unless you are older than 16 years old.” Solution: Let q, r, and s represent “You can ride the roller coaster,” “You are under 4 feet tall,” and “You are older than 16 years old,” respectively. Then the sentence can be translated to (r ∧ ¬s) → ¬q. 26 Propositional Equivalences Back to the ¬Future Propositional Calculus or Propositional Logic. The area of logic that deals with propositions is called the propositional calculus or propositional logic. Compound Propositions New propositions formed from existing propositions using logical operators are called compound propositions. 28 Tautology A compound proposition that is always true, no matter what the truth values of the propositional variables that occur in it, is called a tautology. Contradiction A compound proposition that is always false is called a contradiction. Contingency A compound proposition that is neither a tautology nor a contradiction is called a contingency. 29 Tautology p  ¬p Contradiction p  ¬p Contingency p  ¬p 30 Demonstrate that [¬p (p q )]q is a tautology in two ways: 1. Using a truth table – show that [¬p (p q )]q is always true 2. Using a proof (will get to this later). 31 p q ¬p p q ¬p (p q ) [¬p (p q )]q T T T F F T F F L3 32 p q ¬p p q ¬p (p q ) [¬p (p q )]q T T F T F F F T T F F T L3 33 p q ¬p p q ¬p (p q ) [¬p (p q )]q T T F T T F F T F T T T F F T F L3 34 p q ¬p p q ¬p (p q ) [¬p (p q )]q T T F T F T F F T F F T T T T F F T F F L3 35 p q ¬p p q ¬p (p q ) [¬p (p q )]q T T F T F T T F F T F T F T T T T T F F T F F T L3 36 Definition • Compound propositions that have the same truth values in all possible cases are called logically equivalent. 37  Two methods: ◦ Using truth tables  Not good for long formula  In this course, only allowed if specifically stated! ◦ Using the logical equivalences  The preferred method  p q  ¬q¬p  (p  r)  (q  r)  (p  q)  r The easiest way to check for logical equivalence is to see if the truth tables of both variants have identical last columns. (p q  ¬q¬p) p q p q p q ¬q ¬p ¬q¬p L3 39 The easiest way to check for logical equivalence is to see if the truth tables of both variants have identical last columns: p q p q T T F F T F T F T F T T p q ¬q ¬q¬p ¬p L3 40 The easiest way to check for logical equivalence is to see if the truth tables of both variants have identical last columns: p q p q p q T T F F T F T F T F T T T T F F T F T F ¬q ¬q¬p ¬p L3 41 The easiest way to check for logical equivalence is to see if the truth tables of both variants have identical last columns: p q p q p q T T F F T F T F T F T T T T F F T F T F ¬q F T F T ¬q¬p ¬p L3 42 The easiest way to check for logical equivalence is to see if the truth tables of both variants have identical last columns: p q p q p q T T F F T F T F T F T T T T F F T F T F ¬q F T F T ¬q¬p ¬p F F T T L3 43 The easiest way to check for logical equivalence is to see if the truth tables of both variants have identical last columns: p q p q p q T T F F T F T F T F T T T T F F T F T F ¬q F T F T ¬p F F T T L3 ¬q¬p T F T T 44  Two methods: ◦ Using truth tables  Not good for long formula  In this course, only allowed if specifically stated! ◦ Using the logical equivalences  The preferred method  p q  ¬q¬p  (p  r)  (q  r)  (p  q)  r  (p  r)  (q  r)  (p  q)  r p q r p→r q →r pq (p→r)(q →r) (pq) →r T T T T T T T T T T F F F T F F T F T T T F T T T F F F T F T T F T T T T F T T F T F T F F T T F F T T T F T T F F F T T F T T Logical Equivalences pTp pFp Identity Laws (p  q)  r  p  (q  r) (p  q)  r  p  (q  r) Associative laws pTT pFF Domination Law p  (q  r)  (p  q)  (p  r) p  (q  r)  (p  q)  (p  r) Distributive laws ppp ppp Idempotent Laws  (p  q)   p   q  (p  q)   p   q De Morgan’s laws ( p)  p Double negation law p  (p  q)  p p  (p  q)  p Absorption laws pqqp pqqp Commutative Laws ppT ppF Negation lows pq  pq Definition of Implication p  q  (p  q)  (q  p) Definition of Biconditional 48 (p  r)  (q  r)      ( p  r)  ( q  r) Definition of implication Associative prqr Commutative pqrr ( p   q)  (r  r) Associative De Morgan, Idempotent  (p  q)  r  (p  q)  r Definition of implication Logical Equivalences : Example  Show that (p  q)  (p  q) is a Tautology. (Proof) (p  q)  (p  q)  (p  q)  (p  q)  ( p   q)  (p  q)  ( p  p)  ( q  q) TT T Implication De Morgan Commutative, Associative Negation Identity Tautology by proof [¬p (p q )]q  [(¬p p)(¬p q)]q  [ F  (¬p q)]q  [¬p q ]q  ¬ [¬p q ]  q  [¬(¬p) ¬q ]  q  [p  ¬q ]  q  p  [¬q q ]  p  [q ¬q ] pT T Distributive Negation Identity Negation De Morgan Double Negation Associative Commutative Negation Domination L3 51 What can’t we say?      Quantification: every student has a father. Relations: If X is married to Y, then Y is married to X. Probability: There is an 80% chance of rain. Combine Evidence: This car is better than that one because… Uncertainty: Maybe John is playing golf. • So far we can represent the statement – “10 > 100” or “10 <= 100” • But we can’t represent - “x > 3,” “x = y + 3,” “x + y = z,” • Can represent – “Jorna knows C programming.” • Can’t represent – “X knows C programming.” • Can’t represent – “Every student know C programming.” • Can’t represent – “At least one student know C programming.” 54 “x is greater than 3” has two parts • “x is greater than 3” – the variable x, is the subject of the statement. • “x is greater than 3” – the predicate, refers to a property that the subject of the statement can have. • We can denote the statement “x is greater than 3” by P(x), • Where P denotes the predicate “is greater than 3” and • x is the variable. 55 • We can denote the statement “x is greater than 3” by P(x), • Where P denotes the predicate “is greater than 3” and • x is the variable. • The statement P(x) is also said to be the value of the propositional function P at x. • Once a value has been assigned to the variable x, the statement P(x) becomes a proposition and has a truth value. Q: Let P(x) denote the statement “x > 3.” What are the truth values of P(4) and P(2)? 56 Example 1 Let Q(x, y) denote the statement “x = y + 3.” What are the truth values of the propositions Q(1, 2)and Q(3, 0)? Example 3 Let R(x, y, z) denote the statement “x + y = z.” What are the truth values of the propositions R(1,2,3) and R(0,0,1)? 57 What is it? Quantification is a way to create a proposition from a propositional function (predicate). Quantification expresses the extent to which a predicate is true over a range of elements. • English words all, some, many, none, and few are used in quantifications. 58 • THE UNIVERSAL QUANTIFIER • THE EXISTENTIAL QUANTIFIER 59 Definition 60 Stateme nt ∀x P(x) When True? When False? P(x) is true for There is an x for which P(x) is every x false. P(x) = “Student x knows C Domain : This programming.” Class! When true? If every student knows programming When false? If a least one student does not know 61 Definition 62 Stateme nt ∃x P(x) When True? When False? There is an x for which P(x) P(x) is false for every is true. x. P(x) = “Student x knows C Domain : This programming.” Class! When true? If at least one student knows programming When false? If a every student does not know 63 64 Let Q(x) be the statement “x < 2.” What is the truth value of the quantification ∀x Q(x), where the domain consists of all real numbers? Solution : Q(x) is not true for every real number x, because, for instance, Q(3) is false. That is, x = 3 is a counter example for the statement ∀x Q(x). Thus ∀x Q(x) is false. Let P(x) denote the statement “x > 3”. What is the truth value of the quantification ∃x P (x), where the domain consists of all real numbers? Solution : Because “ x > 3” is sometimes true—for instance, when x = 4, the existential quantification of P(x), which is ∃x P(x), is true. 65 P(x) = “Student x in your class has taken a course in calculus.” • ∀x P(x) = “Every student in your class has taken a course in calculus.” • ∃x P(x) = “At least one student in this class has taken a course in calculus.” • ∀x ¬P(x) • ∃x ¬P(x) • ¬ ∀x P(x) • ¬ ∃x P(x) 66 67 • Translating English Sentences • System Specifications • Boolean Searches • Logic Puzzles • Logic Circuits 68 Express the statement “Every student in this class has studied calculus” using predicates and quantifiers. • Rewrite: “For every student in this class, that student has studied calculus.” • Use variable x : “For every student x in this class, x has studied calculus.” • C(x), which is the statement “x has studied calculus.” • ∀x C(x) [OK, if the domain for x consists of the students in the class] 69 Express the statement “Every student in this class has studied calculus” using predicates and quantifiers. • “For every person x, if person x is a student in this class then x has studied calculus.” • If S(x) represents the statement that person x is in this class. • ∀x (S(x) → C(x)) 70 M(X, Y) : X has emailed Y • ∀X ∀Y M(X, Y) • ∀X ∃Y M(X, Y) • ∃X ∀Y M(X, Y) • ∃X ∃Y M(X, Y) 71 M(X, Y) : X has emailed Y • ¬ ∀X ∀Y M(X, Y) • ¬ ∀X ∃Y M(X, Y) • ¬ ∃X ∀Y M(X, Y) • ¬ ∃X ∃Y M(X, Y) ≡ ∃X ∃Y ¬M(X, Y) ≡ ∃X ∀Y¬M(X, Y) ≡ ∀X ∃Y ¬M(X, Y) ≡ ∃X ∀Y ¬M(X, Y) 72 De-Morgans Law ¬∀𝑥 ∀𝑦 𝑃 𝑥, 𝑦 ⋀Q x, y ≡ ∃𝑥 ∃𝑦 ¬𝑝(𝑥, 𝑦)⋁¬𝑄(𝑥, 𝑦) 73 An argument in propositional logic is a sequence of propositions. All but the final proposition in the argument are called premises and the final proposition is called the conclusion. An argument is valid if the truth of all its premises implies that the conclusion is true. An argument form in propositional logic is a sequence of compound propositions involving propositional variables. An argument form is valid no matter which particular propositions are substituted for the propositional variables in its premises, the conclusion is true if the premises are all true. 74 Whenever the premises are true, the conclusion must also be true. However, this can be a tedious approach. To do this we can first establish the validity of some relatively simple argument forms, called rules of inference. These rules of inference can be used as building blocks to construct more complicated valid argument forms. We will now introduce the most important rules of inference in propositional logic. 75 The tautology (p ∧ (p → q)) → q is the basis of the rule of inference called modus ponens, or the law of detachment. (Modus ponens is Latin for mode that affirms.) This tautology leads to the following valid argument form, which we have already seen in our initial discussion about arguments (where, as before, the symbol ∴ denotes “therefore”): p P→q ∴q Using this notation, the hypotheses are written in a column, followed by a horizontal bar followed by a line that begins with the therefore symbol and ends with the conclusion. In particular, modus ponens tells us that if a conditional statement and the hypothesis of this conditional statement are both true, then the conclusion must also be true. Example 1 illustrates the use of modus ponens. 76 Example from Book!!! 77 Example from Book!!! 78 Some common forms of incorrect reasoning, which lead to invalid arguments are called fallacies. 79 Looks OK but Not OK!!! 𝑃→𝑄 ¬𝑝 ∴ ¬𝑄 𝑃→𝑄 𝑄 ∴𝑃 80 lemma A less important theorem that is helpful in the proof of other results is called a lemma. Proof A proof is a valid argument that establishes the truth of a theorem. 82 Theorem A theorem is a statement that can be shown to be true. In mathematical writing, the term theorem is usually reserved for a statement that is considered at least somewhat important. Propositions Less important theorems sometimes are called propositions. 83 Lemma A lemma is really just another word for a theorem. The idea behind the distinction is that a lemma is a proven statement which is not interesting in and of itself, but which is proven as a step in the proof of some more interesting statement. In a proof of a complex theorem, we often break it down into steps – smaller theorems which can be combined to prove the complex theorem. When those smaller theorems don’t have any particular interest to the author of the proof except as stepping stones towards the proof the main theorem, they’re called lemmas. 84  A theorem is a statement which is proven by valid logical inference within a mathematical theory from the fundamental axioms of that theory. So, for example, the pythagorean theorem is a proven statement within the mathematical theory of geometry: given the basic axioms of euclidean geometry, you can prove the pythagorean theorem. In some sense, a theorem is similar to what most scientists call a theory, although it’s not the same thing.  A scientific theory is a statement inferred from a set of observations or facts, and which is consistent with all of the observations related to whatever phenomenon the theory explains. But a theory is never proven to be true: a new observation can always invalidate a scientific theory. A theorem is a statement which is proven from known facts: if it’s really a theorem, then it’s a theorem forever: no “new facts” can come along and cause a theorem to become invalid. In Euclidean geometry, the square of the length of the hypotenuse of a right triangle will always be the sum of the squares of the other two sides. No new observation can ever change that: within the realm of Euclidean geometry, that is an absolute, unchangeable fact: it’s a theorem. General Techniques • Direct Proof • Proof by Contraposition (Indirect Proof) • Proof by Contradiction The square of an even number is even. 86 Direct Proof [𝑷 → 𝑸] Proof: N even → N2 is even ∃𝐾 N = 2K N2 = (2K)2 N2 = 4K2 N2 = 2.2K2 (this is an even number) Hence, the square of an even number is even. (Proofed) 87 Proof by Contraposition [𝑷 → 𝑸 ≡ ¬𝑸 → ¬𝑷] Sometime we can not provide direct proof. If N2 is even then N is even. 88 PROVE: The square of an even number is even. (Ex. 1.16.b) 1. Suppose k 2 is not even. 2. So k 2 is odd. 3. n k 2 = 2n + 1 4. n k 2 - 1 = 2n 5. n (k - 1)(k + 1) = 2n 6. (k - 1)(k + 1) | 2 = n 7. (k - 1) | 2  (k + 1) | 2 since 2 is prime 8. a k - 1 = 2a  b k+1 = 2b 9. a k = 2a + 1  b k = 2b – 1 10. In both cases k is odd 11. So k is not even L14 89 PROVE: kZ k 1(mod 3)  k 31(mod 9) L14 90 PROVE: kZ k 1(mod 3)  k 31(mod 9) 1. k 1(mod 3) 2. n k-1 = 3n 3. n k = 3n + 1 4. n k 3 = (3n + 1)3 5. n k 3 = 27n 3 + 27n 2 + 9n + 1 6. n k 3-1 = 27n 3 + 27n 2 + 9n 7. n k 3-1 = (3n 3 + 3n 2 + n)·9 8. m k 3-1 = m·9 9. k 31(mod 9) L14 91 Set Theory Page  1 1 Set Basics Examination [5] 1. What is Set? 2. State whether the sets in each pair are equal or not. a) {a, b, c, d} and {a, c, d, b} b) {2, 4, 6} and {x | x is an even number, 0<x<8} Page  2 Set Basics Definition A set is an unordered collection of objects, called elements or members of the set. A set is said to contain its elements. Example  People in a class: {Jui, Sujit, Salman, Koni}  Districts in the BD : {Rajshahi, Dhaka, Nator, … }  Sets can contain non-related elements: {3, a, Potato}  All positive numbers less than or equal to 5: {1, 2, 3, 4, 5} Page  3 Set Basics Definition A set is an unordered collection of objects, called elements or members of the set. A set is said to contain its elements. Example People in a class: {Trisha, Tanvir, Tonmoy, Keya} Districts in the BD : {Rajshahi, Dhaka, Nator, … } Sets can contain non-related elements: {3, a, Potato} All positive numbers less than or equal to 5: {1, 2, 3, 4, 5} Page  4 Set Basics Definition A set is an unordered collection of objects, called elements or members of the set. A set is said to contain its elements. • We write a ∈ A to denote that a is an element of the set A. (∈ = belongs to) • The notation a ∈ A denotes that a is not an element of the set A. ( ∉ = not belongs to) Page  5 Set Basics Definition A set is an unordered collection of objects, called elements or members of the set. A set is said to contain its elements. • We write a ∈ Ato denote that a is an element of the set A. (∈ = belongs to) • The notation a ∈ A denotes that a is not an element of the set A. ( ∉ = not belongs to) • It is common for SETS to be denoted using uppercase letters. • Lowercase letters are usually used to denote elements of sets. Page  6 Set and Elements Let, A = { 1, a, e, u, i, o, 2, 3} • Name of the Set? • 1 ∉ 𝐴 (true or false) • a ∈ A (true or false) Page  7 7 How to describe a Set? Three popular methods 1. Word description Set of even counting numbers less than 10 2. The listing method / Roster method {2, 4, 6, 8} 3. Set-builder notation {x | x is an even counting number less than 10} Page  8 8 How to describe a Set? 1. Word description • Make a word description of the set. Page  9 9 How to describe a Set? 2. Roster Method • Represented by listing its elements between braces {} • Example : 𝐴 = { 1, 2, 3, 4} • Sometime use ellipses (...) rather than listing all elements. • The set of positive integers less than 100 can be denoted by {1,2,3,...,99}. Page  10 10 How to describe a Set? 3. Set-builder notation • characterize all elements in the set by stating the property or properties they must have to be members. • the set O of all odd positive integers less than 10 can be written as O = { x | x is an odd positive integer less than 10 } O = { x ∈ Z+ | x is odd and x < 10 } Example: B = {x | x is an even integer, x > 0} • Read as- “B is the set of x such that x is an even integer and x is greater than 0” • | is read as “such that” and comma as “and”. Page  11 11 How to describe a Set? 3. Set-builder notation with interval • the notation for intervals of real numbers. When a and b are real numbers with a < b, we write • [a, b] = {x | a ≤ x ≤ b} • [a, b) = {x | a ≤ x < b} • (a, b] = {x | a < x ≤ b} • (a, b) = {x | a < x < b} • Note that [a, b] is called the closed interval from a to b and (a, b) is called the open interval from a to b. Page  12 12 Often used sets • N = {0, 1, 2, 3, …} is the set of natural numbers • Z = {…, -2, -1, 0, 1, 2, …} is the set of integers • Z+ = {1, 2, 3, …} is the set of positive integers (a.k.a whole numbers) – Note that people disagree on the exact definitions of whole numbers and natural numbers • Q = {p/q | p  Z, q  Z, q ≠ 0} is the set of rational numbers – Any number that can be expressed as a fraction of two integers (where the bottom one is not zero) • R is the set of real numbers • R+ the set of positive real numbers • C the set of complex numbers. Page  13 13 Specifying Set Specifying Sets (cont.) • A = {a, e, i, o, u} A = {x | x is a letter in English, x is a vowel} • B = {x | x is an even integer, x > 0} B = {2, 4, 6, …….} • E = {x | 𝑥 2 − 3𝑥 + 2 = 0} E = {1, 2} Page  14 14 Specifying Set Specifying Sets (cont.) • A = {x: x  Z, x is even, x <15 } A = {… -8, -6, -4, -2, 0, 2, 4, …., 14} • B = {x: x  Z, x + 4 = 3 } B = {-1} • C = {x: x  Z, x2 + 2 = 6 } E = {-2, +2} Page  15 15 Set - properties Order does not matter – {1, 2, 3, 4, 5} is equivalent to {3, 5, 2, 4, 1} Frequency does not matter - Consider the list of students in this class • It does not make sense to list somebody twice Page  16 16 Set Terminology : The universal set Definition U is the universal set – the set of all of elements (or the “universe”) from which given any set is drawn. • For the set {-2, 0.4, 2}, U would be the real numbers • For the set {0, 1, 2}, U could be the N, Z, Q, R depending on the context • For the set of the vowels of the alphabet, U would be all the letters of the alphabet Page  17 17 Set Terminology : The Empty Set Definition If a set has zero elements, it is called the empty (or null) set • Written using the symbol  • Thus,  = { }  VERY IMPORTANT • It can be a element of other sets { , 1, 2, 3, x } is a valid set • ≠{} The first is a set of zero elements The second is a set of 1 element [A set with one element is called a singleton set] Page  18 18 Venn diagrams • Represents sets graphically – The box represents the universal set – Circles represent the set(s) • Consider set S, which is the set of all vowels in the alphabet • The individual elements are usually not written in a Venn diagram Page  19 b c d f g h k l j m n p q r s t v w x y z 19 U S e a o i u Set Terminology : Subset Definition The set A is a sub set of B if and only if every element of A is also an element of B. • We use the notation A ⊆ B to indicate that A is a subset of the set B. We see that A ⊆ B if and only if the quantification ∀x (x∈ A → x ∈ B) is true Page  20 20 Set Terminology : Subset Example • If A = {2, 4, 6} and B = {1, 2, 3, 4, 5, 6, 7}; A is a subset of B • If A = {1, 2, 3, 4} and B = {1, 2, 3, 4}; A is a subset of B • Every nonempty set S has at least two subset For any set S, S  S (S S  S) For any set S,   S (S   S) Page  21 21 Set Terminology : Proper Subset Definition When a set A is a subset of a set B but that A ≠ B, we write A ⊂ B and say that A is a proper subset of B. • For A ⊂ B to be true, it must be the case that A ⊆ B and there must exist an element y of B that is not an element of A. That is, A is a proper subset of B if and only if Page  22 ∀x (x ∈ A → x ∈ B) ∧ ∃y (y ∈ B ∧ y ∉A) is true 22 Set Terminology : Proper Subset Example • If A = {2, 4, 6} and B = {1, 2, 3, 4, 5, 6, 7}; A is a subset of B and also proper subset A ⊂ B and A ⊆ B both are true. • If A = {1, 2, 3, 4} and B = {1, 2, 3, 4}; A is not a proper subset of B but subset. A ⊆ B but A ⊄ B. Page  23 23 Set Terminology : Set Equality Definition Two sets are equal if and only if they have the same elements. We write A = B if A and B are equal sets. • Therefore, if A and B are sets, then A and B are equal if and only if ∀x (x ∈ A ↔ x ∈ B) Page  24 24 Set Terminology : Set Equality Example • Let two sets A = {1, 2, 3} and B = {3, 2, 1} then A = B (true or false?) • Let two sets A = {1, 2, 3} and B = {3, 3, 2, 1, 2, 1} then A = B (true or false?) A = {x: x is an odd positive integer less than 10} B = {1, 3, 5, 7, 9} A=B? Page  25 25 Set Terminology : Set Cardinality Definition Let S be a set. If there are exactly n distinct elements in S where n is a nonnegative integer, we say that S is a finite set and that n is the cardinality of S. The cardinality of S is denoted by |S|. The term cardinality comes from the common usage of the term cardinal number as the size of a finite set. Page  26 26 Set Terminology : Set Cardinality Example • Let A be the set of odd positive integers less than 10. Then |A| = 5. • Let S be the set of letters in the English alphabet. Then |S| = 26 • Let R = {1, 2, 3, 4, 5}. Then |R| = • || = Page  27 • | 𝜙 |= 5. 0 1 27 Set Terminology : Finite Set and Infinite Set Definition : Finite Set Let S be a set. If there are exactly n distinct elements in S where n is a nonnegative integer, we say that S is a finite set • R = {1, 2, 3, 4, 5} finite set Definition : Infinite Set A set is said to be infinite if it is not finite. • The set of positive integers is infinite. Page  28 28 Set Terminology : Power Set Definition Given a set S, the power set of S is the set of all subsets of the set S. The power set of S is denoted by P(S). • What is the power set of the set {0,1,2}? • What is the power set of the empty set? • What is the power set of the set{∅}? Page  29 P({})={{}} P({∅})={∅,{∅}} 29 Set Terminology : Cartesian Product Definition Let A and B be sets. The Cartesian product of A and B, denoted by A x B, is the set of all ordered pairs (a, b) where a  A and b B. Hence A×B = {(a, b) | a ∈ A ∧ b ∈ B}. Let, A = {1, 2} and b = {a, b, c} A x B = {(1, a), (1, b), (1, c), (2, a), (2, b), (2, c)} BxA=? Page  30 30 Set Operations Page  31 31 Set Operation Operations • • • • • Page  32 Union () Intersection () Difference (-) Complement (“—”) Symmetric Difference () Operation into tow sets A and B give us new sets • AB, • AB, • A-B, • AB, and • 𝐴 32 Set Operation : Union Definition Let A and B be sets. The union of the sets A and B, denoted by A ∪ B, is the set that contains those elements that are either in A or in B, or in both. A U B = { x | x A ∨x B } A = {1, 2, 5, 7}, B = {3, 4, 5, 6} Page  33 AUB= { 1, 2, 3, 4, 5, 6, 7} 33 Set Operation : Intersection Definition Let A and B be sets. The intersection of the sets A and B, denoted by A∩B, is the set containing those elements in both A and B. A ∩B = { x | x A ∧x B } A = {1, 2, 5, 7}, B = {3, 4, 5, 6} Page  34 A ∩B = {5} 34 Union and Intersection in VD Page  35 35 Set Operation : Union and Intersection Examples A = {1, 2, 3, 4}, B = {3, 4, 5, 6, 7}, C = {2, 3, 5, 7} • AUB=? • AUC=? • A∩B=? • B∩C=? Page  36 36 Disjoint Sets Definition Two sets are called disjoint if their intersection is the empty set. i.e. A  B =  . {a, b} and {3, 4} are disjoint U A Page  37 37 B Disjoint Union Definition • When A and B are disjoint, the disjoint union operation is well defined. The circle above the union symbol indicates disjointedness. U  A B A Page  38 B 38 Set Operation : principle of inclusion–exclusion Page  39 39 Set Operation : Difference Definition Let A and B be sets. The difference of A and B, denoted by A−B, is the set containing those elements that are in A but not in B. The difference of A and B is also called the complement of B with respect to A. A − B = { x | x ∈ A ∧ x ∉ B} • The difference of A and B is also called the complement of B with respect to A. • The difference of sets A and B is sometimes denoted by A \ B. Page  40 40 Set Operation : Difference Example • Let A = {1, 3, 5}, B = {1, 2, 3} A – B = {5} • Let A = {1, 3, 5, 6}, B = {1, 2, 3,9,10} A – B = {5, 6} Page  41 41 Set Operation : Complement Definition Let U be the universal set. The complement of the set A, denoted by A, is the complement of A with respect to U. Therefore, the complement of the set A is U−A. A = {x ∈ U | x ∉ A}. Page  42 42 Set Operation : Symmetric Difference Definition Let A and B be sets. The symmetric difference of A and B, denoted by A ⊕ B, is the set containing those elements that are either in A or in B, but not in both. AB U A ⊕ B = { x | x ∈ A ∨ x ∈ B ∧ x ∉ (A ∩ 𝑩)} Page  43 A B 43 How to Prove a Set identities Five Methods • Use the basic set identities • Use membership tables • Prove each set is a subset of each other • Use set builder notation and logical equivalences • Use Venn Diagram Page  44 44 Set identities A = A AU = A Identity Law AU = U A =  Domination law AA = A AA = A Idempotent Law (Ac)c = A Complement Law Commutative Law (AB)c = Ac  Bc (AB)c = Ac  Bc De Morgan’s Law Associative Law A(BC) = (AB)(AC) A(BC) = (AB)(AC) Distributive Law Absorption Law A  Ac = U A  Ac =  Complement Law AB = BA AB = BA A(BC) = (AB)C A(BC) = (AB)C A(AB) = A A(AB) = A Page  45 Set identities: (A  B )  C = A  (B  C ) Using set definition and set builder notation Proof : (AB )C = {x | x  A B  x  C } (by def. ) = {x | (x  A  x  B )  x  C } (by def.) = {x | x  A  ( x  B  x  C ) } (logical assoc.) = {x | x  A  (x  B  C ) } (by def.) = A(B C ) (by def.) • Page  46 46 Set identities 𝑨 ∩ 𝑩=𝑨∪𝑩 Use set builder notation and logical equivalences Page  47 47 Set identities : A ∩ (B∪C) = (A∩B) ∪ (A∩C) Use a membership table Page  48 48 Set identities 𝑨 ∪ 𝑩 ∩ 𝑪 = (𝑪∪𝑩) ∩ 𝑨 Use set identity law Page  49 49 Set Identities via Venn It’s often simpler to understand an identity by drawing a Venn Diagram. For example DeMorgan’s first law A B  can be visualized as follows. Page  50 A B 50 Visual DeMorgan A: Page  51 B: 51 Visual DeMorgan B: A: B : A Page  52 52 Visual DeMorgan A: Page  53 L5 B: 53 Visual DeMorgan B: A: A: Page  54 B: 54 Visual DeMorgan B: A: A: B: A B : Page  55 55 Visual DeMorgan A B  = A B  Page  56 L5 56 Function Function Definition • Let A and B be sets. A function f from A to B is an assignment of exactly one element of B to each element of A. • We write f(a) = b if b is the unique element of B assigned by the function f to the element a of A. Function • If f is a function from A to B, we write f : A →B. • Set A is called the domain of the function. • Set B is called the codomain of the function. • If f(a) = b, then b is the image of a and a is the pre-image of b. Function Definition of a function f :R Z f maps R to Z Domain R Z f f(4.3) 4 4.3 Pre-image of 4 Image of 4.3 Co-domain More functions Function A pre-image of 1 The image of “a” Domain Co-domain Antora A “a” 1 x B “bb“ 2 Asad C “cccc” 3 y D “dd” 4 z F “e” 5 A class grade function A string length function Function Even more functions Range a 1 “a” 1 e 2 “bb“ 2 i 3 “cccc” 3 o 4 “dd” 4 u 5 “e” 5 Some function… Not a valid function! Also not a valid function! Function: Domain, Codomain Function and Formula  f(x) = 𝑥 2 , where x ∈ Z+∧ 0<x<2  f(x) = 𝑥 2 + 2𝑥-3, where x ∈ Z+∧ 0<x<3 Function arithmetic Definition Let f1 and f2 be function from A to B. Then f1 + f2 and f1 f2 are also functions from A to B defined by • (f1 + f2)(x) = f1(x) + f2(x) • (f1 f2)(x) = f1(x) f2(x) If f1(x) = 𝑥 2 and f2(x) = x - 𝑥 2 . What are the functions (f1 + f2)(x) and (f1 f2)(x) ? (f1 + f2)(x)= x (f1 f2)(x)=𝑥 3 − 𝑥 4 Function : Loss vs NO Loss • f(x) = x2 • f grading system • f(x) = 0.1x One to One Function One-to-one functions Definition A function is one-to-one if elements in the co-domain has a unique pre-image A function is not one-to-one, if and only if f(x) ≠f(y) whenever x ≠y a 1 a 1 e 2 e 2 i 3 i 3 o 4 o 4 5 A one-to-one function 5 A function that is not one-to-one One to One Function More on one-to-one  Injective is synonymous with one-to-one  “A function is injective”  A function is an injection if it is one-to-one  Note that there can be un-used elements in the co-domain. a 1 e 2 i 3 o 4 5 A one-to-one function OneMore to One Function on one-to-one  Determine whether the function f form {a, b, c, d} to {1, 2, 3, 4, 5} with f(a)= 4, f(b)= 5, f(c)= 1, f(d)= 3 is one-to-one or not.  Determine whether the function f(x) = x2 from the set of integers to the set of integers is one-to-one?  Determine whether the function f(x) = x+1 from the set of integers to the set of integers is one-to-one? Onto functions Definition A function is onto if each element in the co-domain is an image of some preimage(s). Onto functions Definition A function is onto if each element in the co-domain is an image of some preimage a 1 a 1 e 2 e 2 i 3 i 3 o 4 o 4 u An onto function 5 A function that is not onto Onto functions More on onto  Surjective is synonymous with onto  “A function is surjective”  A function is an surjection if it is onto  Note that there can be multiply used elements in the co-domain a 1 e 2 i 3 o 4 u An onto function Onto functions More onto  Determine whether the function f form {a, b, c, d} to {1, 2, 3} with f(a)= 3, f(b)= 2, f(c)= 1, f(d)= 3 is onto or not.  Determine whether the function f(x) = x2 from the set of integers to the set of integers is onto?  Determine whether the function f(x) = x+1 from the set of integers to the set of integers is onto? Onto vs. one-to-one Are the following functions onto, one-to-one, both, or neither? a 1 b 2 c 3 4 1-to-1, not onto a 1 b 2 c 3 d 4 Both 1-to-1 and onto a 1 a 1 b 2 b 2 c 3 c 3 d 4 d Onto, not 1-to-1 Neither 1-to-1 nor onto a 1 b 2 c 3 4 Not a valid function Bijections Definition • A function that is both one-to-one and onto Such a function is a one-to-one correspondence, or a bijection. • We only can find the inverse of a function if it is a bijection function. a 1 b 2 c 3 d 4 Bijections  Let f be the function from {a,b,c,d} to {1,2,3,4} with f(a)=4, f(b)=2, f(c)=1 and f(d)=3. Is f bijection? Identity functions Definition A function such that the image and the pre-image are ALWAYS equal  f(x) = 1*x  f(x) = x + 0  The domain and the co-domain must be the same set Inverse functions Inverse functions Let f(x) = 2*x R f R f-1 f(4.3) 8.6 4.3 f-1(8.6) Then f-1(x) = x/2 Inverse functions Definition Let f be a one-to-one correspondence from the set A to the set B. The inverse function of f is the function that assigns to an element b belonging to B the unique element a in A such that f(a) = b. • The inverse function of f is denoted f-1 . • Hence, f-1 (b) = a when f(a) = b More on inverse functions Inverse functions  Can we define the inverse of the following functions? a 1 a 1 b 2 b 2 c 3 c 3 4 d What is f-1(2)? Not onto! What is f-1(2)? Not 1-to-1!  An inverse function can ONLY be done defined on a bijection Few functions Few Examples  f: Z  Z  f(x) = x  f(x) = 2x  f(x) = x+1  f: R  R  f(x) = 2x  f(x) = x2  f(x) = x3  f: R  R+  {0}  f(x) = x2 Compositions of functions (f ○ g)(x) = f(g(x)) f○g A B C g f g(a) f(b) a f(g(a)) b = g(a) (f ○ g)(a) Compositions of functions Definition • Let g be a function from the set A to the set B and let f be a function from the set B to the set C. The composition of the functions f and g, denoted f o g is defined by (f o g)(a) = f(g(a)) Note that the composition f o g can not be defined unless the range of g is a subset of the domain of f. Compositions of functions Compositions of functions Let f(x) = 2x+3 f○g R Let g(x) = 3x+2 R R g f g(1) f(5) f(g(1))=13 1 g(1)=5 (f ○ g)(1) f(g(x)) = 2(3x+2)+3 = 6x+7 Compositions of functions Compositions of functions Does f(g(x)) = g(f(x))? Let f(x) = 2x+3 f(g(x)) = 2(3x+2)+3 = 6x+7 g(f(x)) = 3(2x+3)+2 = 6x+11 Let g(x) = 3x+2 Not equal! Function composition is not commutative! Graphs of functions f(x)=3 x=1 Let f(x)=2x+1 f(x)=5 Plot (x, f(x)) This is a plot of f(x) x=2 Graphs of functions Not one-to- one Not onto Both one-to- one and onto one-to- one Nor onto Not one-to- one But onto Useful functions  Floor: x means take the greatest integer less than or equal to the number  Ceiling: x means take the lowest integer greater than or equal to the number  round(x) =  x+0.5  Floor, Ceiling Examples  Find these values  1.1  1.1  -0.1  -0.1 1 2 -1 0 Relation 1 Relation Definition a binary relation from A to B is a set R of ordered pairs where the first element of each ordered pair comes from A and the second element comes from B. We use the notation a R b to denote that (a, b) ∈ R and a R b to denote that (a, b) ∉R. Moreover, when (a, b)belongs to R, a is said to be related to b by R. Let A and B be sets. A binary relation from A to B is a subset of A×B. Application : Relational database model is based on the concept of relation. Relation : Example • Let A be the students in the Dept. of CSE • A = {Ruhul, Shanta, Sarfar, Dipti} • Let B be the courses the department offers • B = {CSE 201, CSE 202, CSE 203} • We specify relation R  A  B as the set that lists all students a  A enrolled in class b  B • R = {(Ruhul, CSE 201), (Shanta, CSE 202), (Shanta, CSE 203), (Sarfar, CSE 202), (Dipti, CSE 203)} More relation examples Relation : More Example • Another relation example: • Let A be the Districts in the Bangladesh • Let B be the Division in the Bangladesh • We define R to mean a is a district in division b • Thus, the following are in our relation: • (Nator, Rahjshahi) • (Jayputhat, Rajshahi) • (Tongi, Dhaka) • (Bagerhat, Khulna) • etc… • Most relations we will see deal with ordered pairs of integers Representing relations We can represent relations graphically We can represent relations in a table Nadira CSE CSE CSE 1201 1202 1203 Nadira 1 0 0 Sojib 0 1 1 Sakira 0 0 0 Asma 0 1 1 CSE 1201 Sojib CSE 1202 Sakira CSE 1203 Asma Not valid functions! Relations vs. functions • If R  X  Y is a relation, then is R a function? • If f: X  Y is a function, then is f a relation? Inverse Relation Definition Let R be any relation from a set A to a set B. The inverse of R, denoted by R-1, is the relation from B to A, which consists of those ordered pairs which, when reversed, belong to R; that is, R-1 = {(b, a)|(a, b)∈R} • For example, let A={1,2,3} and B={x, y, z}. Then the inverse of R={(1, y), (1, z), (3,y)} • Then the inverse relation is R-1 = {(y,1), (z,1), (y,3)} • Inverse or Inverse is ? Relations on a set Definition • A relation on the set A is a relation from A to A • In other words, the domain and co-domain are the same set • We will generally be studying relations of this type Relations on a Set : Example • Let A be the set { 1, 2, 3, 4 } • Which ordered pairs are in the relation R = { (a,b) | a divides b } • R = {(1,1), (1,2), (1,3), (1,4), (2,2), (2,4), (3,3), (4,4)} 1 1 R 1 2 3 4 1 X X X X 2 2 3 3 2 3 4 4 4 X X X X Directed Graphs of Relations on Sets • Consider the relation R on the set A={1,2,3,4} : R={(1,2), (2,2), (2,4), (3,2), (3,4), (4,1), (4,3)} • • • • There is an important way of picturing a relation R on a finite set. First we write down the elements of the set Draw an arrow from each element x to each element y when ever x is related to y. This diagram is called the directed graph of the relation. RelationsMore on examples a Set : Example • Consider some relations on the set Z • Are the following ordered pairs in the relation? (1,1) (1,2) (2,1) (1,-1) (2,2) • R1 = { (a,b) | a≤b } X X • R2 = { (a,b) | a>b } X • R3 = { (a,b) | a=|b| } X • R4 = { (a,b) | a=b } X X X X X X X • R5 = { (a,b) | a=b+1 } • R6 = { (a,b) | a+b≤3 } X X X X Relation properties Properties of Relations • Properties of relations we will study: • Reflexive • Symmetric • Anti symmetric • Transitive Reflexivity • Reflexivity • Definition: A relation is reflexive if • (a, a)  R for all a  A Symmetric and Antisymmetric Relations Definition Symmetric and Antisymmetric Relations • A relation is symmetric if • for all a, b  A, (a,b)  R (b,a)  R • A relation is antisymmetric if • for all a, b  A, (a,b)  R  (b,a)  R Transitivity Definition Transitivity Transitivity • A relation is transitive if • for all a, b, c  A, ((a,b)R  (b,c)R)  (a,c)R • If a < b and b < c, then a < c • Thus, < is transitive • If a = b and b = c, then a = c • Thus, = is transitive Summary of properties of relations reflexive a (a, a)  R irreflexive a (a, a)  R symmetric  a, b  A, (a ,b)  R  (b ,a)  R asymmetric  a, b  A, (a,b)  R  (b,a)  R  a, b  A, ((a ,b)  R  (b ,a)  R)  a=b antisymmetric (*) for all a, b  A, ((a ,b)  R  a  b)  (b ,a)  R) transitive (*)  a, b, c  A, ((a ,b)  R  (b ,c)  R)  (a ,c)  R Alternative definition… Mathematical Induction I know I can change my life by changing my attitude. Everyday I am creating a better life by changing my attitude. References Discrete Mathematics and its Applications by Kenneth H. Rosen • Chapter 4 : Mathematical Reasoning, Induction, and Recursion • • • • 3.3 : Mathematical Induction 3.4 : Recursive Definitions and structural Induction 3.5 : Recursive Algorithm 3.6 : Program Correctness Proofs (PPT) by H. Turgut Uyar Ay¸seg¨ ul Gen¸cata Yayımlı Emre Harmanc Induction (PPT) by Zeph Grunschlag Introduction Problem What is the formula for the sum of the first n positive odd integers? When n = 1, 2, 3, 4, 5 • 1=1 • 1+3=4 • 1+3+5=9 • 1 + 3 + 5 + 7 = 16 • 1 + 3 + 5 + 7 + 9 = 25 Introduction Problem What is the formula for the sum of the first n positive odd integers? When n = 1, 2, 3, 4, 5 • 1=1 • 1+3+5=9 From these values it is reasonable to guess that the sum of the first n positive odd integers is n2 • 1 + 3 + 5 + 7 = 16 How to prove it? • 1+3=4 • 1 + 3 + 5 + 7 + 9 = 25 Introduction Problem What is the formula for the sum of the first n positive odd integers? When n = 1, 2, 3, 4, 5 • 1=1 • 1+3+5=9 From these values it is reasonable to guess that the sum of the first n positive odd integers is n2 • 1 + 3 + 5 + 7 = 16 How to prove it? • 1+3=4 • 1 + 3 + 5 + 7 + 9 = 25 Introduction Problem What is the formula for the sum of the first n positive odd integers? When n = 1, 2, 3, 4, 5 • 1=1 • 1+3+5=9 From these values it is reasonable to guess that the sum of the first n positive odd integers is n2 • 1 + 3 + 5 + 7 = 16 How to prove it? • 1+3=4 • 1 + 3 + 5 + 7 + 9 = 25 Why should we know? • An extremely important proof technique that can be used to assertions of this type. • Is used to prove results about a large variety of discrete objects. • To prove results about the complexity of algorithms • The correctness of certain type of computer program • Theorem about graphs and trees Introduction Extremely important to note • Mathematical inductions can be used only to prove results obtained in some other way. • It is not a tool for discovering formulae or theorem Illustrations of Mathematical Induction • People telling secrets • Fall of Dominos Let, P(n) be the proposition that domino n is knocked over. If P(n)  P(n+1) is true – then all the dominos are knocked over. Illustrations of Mathematical Induction Let sequence of propositions is a sequence of dominos. P (0) P (1) P (2) P (n) P (n+1) … L14 10 Illustrations of Mathematical Induction When the domino falls, the corresponding proposition is considered true: P (n) L14 11 Illustrations of Mathematical Induction Mathematical Induction When the domino falls (to right), the corresponding proposition is considered true: P (n) true L14 12 Illustrations of Mathematical Induction Mathematical Induction Suppose that the dominos satisfy two constraints. 1) Well-positioned: If any domino falls (to right), next domino (to right) must fall also. P (n) P (n+1) 2) First domino has fallen to right L14 P (0) true 13 Illustrations of Mathematical Induction Mathematical Induction Suppose that the dominos satisfy two constraints. 1) Well-positioned: If any domino falls to right, the next domino to right must fall also. P (n) P (n+1) 2) First domino has fallen to right L14 P (0) true 14 Illustrations of Mathematical Induction Mathematical Induction Suppose that the dominos satisfy two constraints. 1) Well-positioned: If any domino falls to right, the next domino to right must fall also. P (n) true 2) First domino has fallen to right L14 P (n+1) true P (0) true 15 Illustrations of Mathematical Induction Mathematical Induction Then can conclude that all the dominos fall! P (0) P (1) P (2) P (n) P (n+1) … L14 16 Illustrations of Mathematical Induction Mathematical Induction Then can conclude that all the dominos fall! P (0) P (1) P (2) P (n) P (n+1) … L14 17 Illustrations of Mathematical Induction Mathematical Induction Then can conclude that all the dominos fall! P (1) P (0) true L14 P (2) P (n) P (n+1) … 18 Illustrations of Mathematical Induction Mathematical Induction Then can conclude that all the dominos fall! P (2) P (0) true L14 P (1) true P (n) P (n+1) … 19 Illustrations of Mathematical Induction Mathematical Induction Then can conclude that all the dominos fall! P (n) P (0) true L14 P (1) true P (2) true P (n+1) … 20 Illustrations of Mathematical Induction Mathematical Induction Then can conclude that all the dominos fall! P (n) P (0) true L14 P (1) true P (2) true P (n+1) … 21 Illustrations of Mathematical Induction Mathematical Induction Then can conclude that all the dominos fall! P (n+1) P (0) true L14 P (1) true P (2) true … P (n) true 22 Illustrations of Mathematical Induction Mathematical Induction Then can conclude that all the dominos fall! P (0) true L14 P (1) true P (2) true … P (n) true P (n+1) true 23 Illustrations of Mathematical Induction Mathematical Induction Principle of Mathematical Induction: If: 1) [basis] P (0) is true 2) [induction] k P(k)P(k+1) is true P (0) true P (1) true P (2) true … P (k) true P (k+1) true Then: n P(n) is true This formalizes what occurred to dominos. L14 24 Mathematical Induction A proof by Mathematical Induction that P(n) is true for every positive integer n consists of two steps: • Basic Step: The proposition P(n0) usually P(1) is shown to be true • Inductive Step: The implication P(k) ⇒ P(k + 1) is shown to be true for every positive integer k. • When we complete both steps of a proof. We prove that P(n) is true for all positive integers n. • That is we have shown that ∀n P(n) is true Mathematical Induction Theorem • P(n): a predicate defined on n ∈ Z+ • P(n0) ∧ (∀k ≥ n0 [P(k) ⇒ P(k + 1)]) ⇒ ∀n ≥ n0 P(n) • P(n0): Base Step • (∀k ≥ n0 [P(k) ⇒ P(k + 1) : Induction step Mathematical Induction Theorem : Discrete Mathematics and its Applications by Kenneth H. Rosen • P(n): a predicate defined on n ∈ Z+ • [P(1) ∧ ∀k P(k) ⇒ P(k + 1)] ⇒ ∀n P(n) Introduction Example Problem: Use mathematical induction to prove that the sum of the first n odd positive integers in n2 Answer in the Examination Q1: Use mathematical induction to prove that the sum of the first n odd positive integers in n2 Answer Solution: Let P(n) denote the proposition that the sum of the first n odd positive integers is n2. That is, P(n) : ∀n ∈ Z+ , [1 + 3 + 5 + · · · + (2n − 1) = n2] Basic Step: P(1) state that the sum of the first one odd positive integer is 1. This is true since the sum of the first odd positive integer is 1. Inductive Step: Suppose that P(k) is true for a positive integer k, that is 1 + 3 + 5 + · · · + (2k − 1) = k2 Answer in the Examination Q1: Use mathematical induction to prove that the sum of the first n odd positive integers in n2 Answer…… ……. Now, we have to show that P(k + 1) is true, assuming that P(k) is true. That is we have to show that 1 + 3 + 5 + · · · + (2k − 1) + (2k + 1) = (k + 1) 2 So, assuming that P(k) is true, it follows that 1 + 3 + 5 + · · · + (2k − 1) + (2k + 1) = [1 + 3 + 5 + · · · + (2k − 1)] + (2k + 1) Answer in the Examination Q1: Use mathematical induction to prove that the sum of the first n odd positive integers in n2 Answer…… ……. 1 + 3 + 5 + · · · + (2k − 1) + (2k + 1) = [1 + 3 + 5 + · · · + (2k − 1)] + (2k + 1) = k2 + (2k + 1) = k2 + 2k + 1 = (k + 1)2 That is shows that P(k+1) is true if P(k) is true. Answer in the Examination Q1: Use mathematical induction to prove that the sum of the first n odd positive integers in n2 Answer…… ……. Science P(1) is true and the implication P(k) ⇒ P(k + 1) is true for all positive integers k, the principle of mathematical induction shows that P(n) is true for all positive integers n. (Proved) Introduction Example Geometric interpretation. To get next square, need to add next odd number: L14 33 Introduction Example Geometric interpretation. To get next square, need to add next odd number: 1 L14 34 Introduction Example Geometric interpretation. To get next square, need to add next odd number: 1 +3 L14 35 Introduction Example Geometric interpretation. To get next square, need to add next odd number: 1 +3 +5 L14 36 Introduction Example Geometric interpretation. To get next square, need to add next odd number: 1 +3 +5 +7 L14 37 Introduction Example Geometric interpretation. To get next square, need to add next odd number: 1 +3 +5 +7 +9 L14 38 Introduction Example Geometric interpretation. To get next square, need to add next odd number: 1 +3 +5 +7 +9 +11 L14 39 Introduction Example Geometric interpretation. To get next square, need to add next odd number: 1 +3 +5 +7 +9 +11 +13 L14 40 Introduction Example Geometric interpretation. To get next square, need to add next odd number: 1 +3 +5 +7 +9 +11 +13 =72 L14 41 Introduction Example Problem: Use mathematical induction to prove the inequality n < 2n • P(n) : n < 2n • P(1) : 1 < 21 • P(k) : assume k < 2k • P (k + 1) : have to show that k + 1 < 2k + 1 Adding 1 both sides of K < 2k • K + 1 < 2k + 1 <= 2k + 2k = 2k + 1 Introduction Example + • ∀𝑛 ∈ 𝑧 , 1 + 2 + 3 + ⋯ + 𝑛 = • ∀𝑛 𝑛 ∈ 𝑧 + , 𝑛 ≥ 4 , 2𝑛 < 𝑛! 𝑛(𝑛+1) 2 Introduction Example Strong Induction • Another form of mathematical induction • Basic step is same as before • In inductive step, we assume that P(j) is true for j = 1, 2 ….k and show that P(k+1) must also be true based on this assumption. Strong Induction Principle of Mathematical Induction: P(n) : The proposition If: 1) [basis] P (0) (sometimes need more base cases) 1) [strong induction] k [P (0)P (1) … P (k)]  P(k+1) Then: n P(n) L14 46 Strong Induction Example Flawed Induction Example Sequences 1 Sequences Definition 1. Arithmetic Sequences 2. Geometric Sequences 3. Recurrence Relations Sequences Sequences We all know this sequence of numbers! What is the rule? 1 2 3 4 5 6 7 8 9 Of course its add one! In today's lesson we are going to be looking at lots of different sequences! Sequences Look at these number sequences carefully can you guess the next 2 numbers? What about guess the rule? +10 30 40 50 60 70 80 --------------------------------------------------------------------------------------------------------------------- 17 20 23 26 29 32 +3 --------------------------------------------------------------------------------------------------------------------- 48 41 34 27 20 13 -7 Arithmetic Sequences • A = First term • D = Common difference • Series : A, A+D, A+2D, A+3D, ……… • TN (nth Term) : A + (N – 1)D • SN (Sum up to nth term) : 𝑁 2 {2𝐴 + 𝑁 − 1 𝐷} Sequences Now try these sequences – think carefully and guess the last number! 1 2 4 7 11 16 +1, +2, +3 … --------------------------------------------------------------------------------------------------------------------- 3 6 12 24 48 96 double Geometric Sequences • A = First term • R = Common Ratio • Series : A, AR, AR2, AR3, ……… • TN (nth Term) : ARN-1 • SN (Sum up to nth term) : 𝑎(𝑅 𝑁 −1) 𝑅−1 𝑤ℎ𝑒𝑟𝑒 [𝑅 ≠ 1] Recurrence Relations Sequences • Always need to look back! Recurrence Relations Sequences Guess my rule! For these sequences I have done 2 maths functions! 3 7 15 31 x2 +1 2 3 63 127 x2 -1 5 9 17 33 Fibonacci’s Series This is a really famous number sequence which was discovered by an Italian mathematician a long time ago. It is called the Fibonacci sequence and can be seen in many natural things like pine cones and sunflowers!!! 1 1 2 3 5 8 13 21 etc… Can you see how it is made? What will the next number be? 34! See if you can find out something about Fibonacci! Who Was Fibonacci? ~ Born in Pisa, Italy in 1175 AD ~ Full name was Leonardo Pisano ~ Grew up with a North African education under the Moors ~ Traveled extensively around the Mediterranean coast ~ Met with many merchants and learned their systems of arithmetic ~ Realized the advantages of the Hindu-Arabic system Fibonacci’s Rabbits Problem: Suppose a newly-born pair of rabbits (one male, one female) are put in a field. Rabbits are able to mate at the age of one month so that at the end of its second month, a female can produce another pair of rabbits. Suppose that the rabbits never die and that the female always produces one new pair (one male, one female) every month from the second month on. How many pairs will there be in one year? Fibonacci’s Rabbits Continued ~ End of the first month = 1 pair ~ End of the second month = 2 pair ~ End of the third month = 3 pair ~ End of the fourth month = 5 pair ~ 5 pairs of rabbits produced in one year 1, 1, 2, 3, 5, 8, 13, 21, 34, … The Fibonacci Numbers in Nature ~ Fibonacci spiral found in both snail and sea shells Golden Ratio Euclid ca. 300 BC gave an equivalent definition of phi by defining it in terms of the so-called "extreme and mean ratios" on a line segment, i.e., such that Ф=AC/CB=AB/AC for the line segment AB illustrated above .Plugging in, (Ф+1)/Ф=Ф and clearing denominators gives Ф 2−Ф−1= 0 Ф =(1/2)*(1+sqrt(5)) =1.618033988749894848204586834365638117720... The Fibonacci Numbers in Nature ~ Sneezewort (Achillea ptarmica) shows the Fibonacci numbers The Fibonacci Numbers in Nature ~ Pinecones clearly show the Fibonacci spiral The Fibonacci Numbers in Nature Lilies and irises = 3 petals Buttercups and wild roses = 5 petals Corn marigolds = 13 petals Black-eyed Susan’s = 21 petals Summation of Sequences Uses of Fibonacci sequence Algorithms • The term algorithm itself derives from the 9th Century mathematician Muḥammad ibn Mūsā al'Khwārizmī. • "Algoritmi" was the translator's Latinization of AlKhwarizmi's name • ‘Algoritmi’ converted to English 'algorism', then to Greek algorithmus the corresponding English term 'algorithm' is first attested in the 17th century • An algorithm is a finite sequence of precise instructions for performing a computation or for solving a problem. EXAMPLE • Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers. 1. Set the temporary maximum equal to the first integer in the sequence. (The temporary maximum will be the largest integer examined at any stage of the procedure.) 2. Compare the next integer in the sequence to the temporary maximum, and if it is larger than the temporary maximum, set the temporary maximum equal to this integer. 3. Repeat the previous step if there are more integers in the sequence. 4. Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence. PROPERTIES OF ALGORITHMS • • • • • • • 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. Correctness: An algorithm should produce the correct output values for each set of input values. Definiteness: The steps of an algorithm must be defined precisely. Effectiveness: It must be possible to perform each step of an algorithm exactly and in a finite amount of time. Finiteness: An algorithm should produce the desired output after a finite (but perhaps large) number of steps for any input in the set. Generality: The procedure should be applicable for all problems of the desired form, not just for a particular set of input values. ALGORITHM 1 • Swap two variables without using third variables. – Procedure swap(a, b: integers) – a= a+b; – b= a-b; – a= a-b; – return a, b; ALGORITHM 2 • Finding the large number. – Procedure large(a, b: integers) – if a <b then large := b – return large ALGORITHM 3 • 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 – return location {location is the subscript of the term that equals x, or is 0 if x is not found} The Growth of Functions • Big-O notation (Best case) • Big-Omega notation (Worst case) • Big-Theta notation (Average case) TERMS • Searching algorithm: the problem of locating an element in a list • Linear search algorithm: a procedure for searching a list element by element • Binary search algorithm: a procedure for searching an ordered list by successively splitting the list in half • Sorting: the reordering of the elements of a list into prescribed order Complexity • Time complexity: the amount of time required for an algorithm to solve a problem • Space complexity: the amount of space in computer memory required for an algorithm to solve a problem • Worst-case time complexity: the greatest amount of time required for an algorithm to solve a problem of a given size • Average-case time complexity: the average amount of time required for an algorithm to solve a problem of a given size Algorithmic paradigm • A general approach for constructing algorithms based on a particular concept • Brute force: the algorithmic paradigm based on constructing algorithms for solving problems in a naïve manner from the statement of the problem and definitions. • Greedy algorithm: an algorithm that makes the best choice at each step according to some specified condition. Complexity of an algorithm Objectives • Algorithm • Performance of an algorithm • Complexity of an algorithm • Time Complexity • Space Complexity • Asymptotic notation • Some examples Algorithm • An algorithm is a sequence of unambiguous instructions used for solving a problem, which can be implemented (as a program) on a computer. Example of algorithm • Problem Statement : Find the largest number in the given list of numbers? Input : A list of positive integer numbers. (List must contain at least one number). Output : The largest number in the given list of positive integer numbers. Consider the given list of numbers as 'L' (input), and the largest number as 'max' (Output). • Algorithm Step 1: Define a variable 'max' and initialize with '0'. Step 2: Compare first number (say 'x') in the list 'L' with 'max', if 'x' is larger than 'max', set 'max' to 'x'. Step 3: Repeat step 2 for all numbers in the list 'L'. Step 4: Display the value of 'max' as a result. Performance of an algorithm • Performance of an algorithm is a process of making evaluative judgment about algorithms. • Performance of an algorithm means predicting the resources which are required to an algorithm to perform its task. • That means when we have multiple algorithms to solve a problem, we need to select a suitable algorithm to solve that problem. We compare all algorithms with each other which are solving same problem, to select best algorithm. To compare algorithms, we use a set of parameters or set of elements like memory required by that algorithm, execution speed of that algorithm, easy to understand, easy to implement, etc., Performance and complexity of an algorithm • Performance analysis of an algorithm is the process of calculating space required by that algorithm and time required by that algorithm. • Performance analysis of an algorithm is performed by using the following measures... • Space required to complete the task of that algorithm (Space Complexity). It includes program space and data space • Time required to complete the task of that algorithm (Time Complexity) Space Complexity • Total amount of computer memory required by an algorithm to complete its execution is called as space complexity of that algorithm. • Generally, when a program is under execution it uses the computer memory for THREE reasons. They are as follows... • Instruction Space: It is the amount of memory used to store compiled version of instructions. • Environmental Stack: It is the amount of memory used to store information of partially executed functions at the time of function call. • Data Space: It is the amount of memory used to store all the variables and constants. Example of Space Complexity • Consider the following piece of code... int square(int a) { return a*a; } • In above piece of code, it requires 2 bytes of memory to store variable 'a' and another 2 bytes of memory is used for return value. That means, totally it requires 4 bytes of memory to complete its execution. And this 4 bytes of memory is fixed for any input value of 'a'. This space complexity is said to be Constant Space Complexity. • If any algorithm requires a fixed amount of space for all input values then that space complexity is said to be Constant Space Complexity Time Complexity • The time complexity of an algorithm is the total amount of time required by an algorithm to complete its execution. • Generally, running time of an algorithm depends upon the following... • Whether it is running on Single processor machine or Multi processor machine. • Whether it is a 32 bit machine or 64 bit machine • Read and Write speed of the machine. • The time it takes to perform Arithmetic operations, logical operations, return value and assignment operations etc., • Input data Examples of Time Complexity • Consider the following piece of code... int sum(int a, int b) { return a+b; } • In above sample code, it requires 1 unit of time to calculate a+b and 1 unit of time to return the value. That means, totally it takes 2 units of time to complete its execution. And it does not change based on the input values of a and b. That means for all input values, it requires same amount of time i.e. 2 units. • If any program requires fixed amount of time for all input values then its time complexity is said to be Constant Time Complexity. Examples of Time Complexity Examples of Time Complexity • In above calculation Cost is the amount of computer time required for a single operation in each line. Repetition is the amount of computer time required by each operation for all its repetitions. Total is the amount of computer time required by each operation to execute. So above code requires '4n+4' Units of computer time to complete the task. Here the exact time is not fixed. And it changes based on the n value. If we increase the n value then the time required also increases linearly. • Totally it takes '4n+4' units of time to complete its execution and it is Linear Time Complexity. Asymptotic notation • Asymptotic notation of an algorithm is a mathematical representation of its complexity • For example, consider the following time complexities of two algorithms... 1. 2. Algorihtm 1 : 5n2 + 2n + 1 Algorihtm 2 : 10n2 + 8n + 3 • Generally, when we analyze an algorithm, we consider the time complexity for larger values of input data (i.e. 'n' value). In above two time complexities, for larger value of 'n' the term in algorithm 1 '2n + 1' has least significance than the term '5n2', and the term in algorithm 2 '8n + 3' has least significance than the term '10n2'. Here for larger value of 'n' the value of most significant terms ( 5n2 and 10n2 ) is very larger than the value of least significant terms ( 2n + 1 and 8n + 3 ). So for larger value of 'n' we ignore the least significant terms to represent overall time required by an algorithm. In asymptotic notation, we use only the most significant terms to represent the time complexity of an algorithm. Majorly, we use THREE types of Asymptotic Notations and those are as follows... 1. Big - Oh (O) 2. Big - Omega (Ω) 3. Big - Theta (Θ) Asymptotic notation(Big O) • Big - Oh notation is used to define the upper bound of an algorithm in terms of Time Complexity. • Consider function f(n) the time complexity of an algorithm and g(n) is the most significant term. If f(n) <= C g(n) for all n >= n0, C > 0 and n0 >= 1. Then we can represent f(n) as O(g(n)). • Consider the following graph drawn for the values of f(n) and C g(n) for input (n) value on X-Axis and time required is on Y-Axis. In below graph after a particular input value n0, always C g(n) is greater than f(n) which indicates the algorithm's upper bound. Example of Asymptotic notation(Big O) • Consider the following f(n) and g(n)... f(n) = 3n + 2 g(n) = n If we want to represent f(n) as O(g(n)) then it must satisfy f(n) <= C x g(n) for all values of C > 0 and n0>= 1 f(n) <= C g(n) ⇒3n + 2 <= C n Above condition is always TRUE for all values of C = 4 and n >= 2. By using Big - Oh notation we can represent the time complexity as follows... 3n + 2 = O(n) Asymptotic notation(Big Omega) • Big - Omega notation is used to define the lower bound of an algorithm in terms of Time Complexity. That means Big - Omega notation always indicates the minimum time required by an algorithm for all input values. That means Big - Omega notation describes the best case of an algorithm time complexity. Big - Omega Notation can be defined as follows... • Consider function f(n) the time complexity of an algorithm and g(n) is the most significant term. If f(n) >= C x g(n) for all n >= n0, C > 0 and n0 >= 1. Then we can represent f(n) as Ω(g(n)). Example of Asymptotic notation(Big Omega) • Consider the following f(n) and g(n)... f(n) = 3n + 2 g(n) = n If we want to represent f(n) as Ω(g(n)) then it must satisfy f(n) >= C g(n) for all values of C > 0 and n0>= 1 • f(n) >= C g(n) ⇒3n + 2 >= C n Above condition is always TRUE for all values of C = 1 and n >= 1. By using Big - Omega notation we can represent the time complexity as follows... 3n + 2 = Ω(n) Asymptotic notation(Big Theta) • Big - Theta notation is used to define the average bound of an algorithm in terms of Time Complexity. That means Big - Theta notation always indicates the average time required by an algorithm for all input values. That means Big - Theta notation describes the average case of an algorithm time complexity. Big - Theta Notation can be defined as follows... • Consider function f(n) the time complexity of an algorithm and g(n) is the most significant term. If C1 g(n) <= f(n) >= C2 g(n) for all n >= n0, C1, C2 > 0 and n0 >= 1. Then we can represent f(n) as Θ(g(n)). Big-O Complexity Table n g(n) log(n) n n log(n) n^2 n^3 2^n 5 3 5 15 25 125 32 10 4 10 40 100 10^3 10^3 100 7 100 700 10^4 10^6 10^30 1000 10 10^3 10^4 10^6 10^9 10^300 Big-O Complexity Chart Example of Asymptotic notation(Big Theta) • Consider the following f(n) and g(n)... f(n) = 3n + 2 g(n) = n If we want to represent f(n) as Θ(g(n)) then it must satisfy C1 g(n) <= f(n) >= C2 g(n) for all values of C1, C2 > 0 and n0>= 1 • C1 g(n) <= f(n) >= ⇒C2 g(n) C1 n <= 3n + 2 >= C2 n Above condition is always TRUE for all values of C1 = 1, C2 = 4 and n >= 1. By using Big - Theta notation we can represent the time complexity as follows... 3n + 2 = Θ(n) Some examples 1) O(1): Time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain loop, recursion and call to any other non-constant time function. // set of non-recursive and non-loop statements For example swap() /printf() functions have O(1) time complexity. A loop or recursion that runs a constant number of times is also considered as O(1). For example the following loop is O(1). 2) O(n): Time Complexity of a loop is considered as O(n) if the loop variables is incremented / decremented by a constant amount. For example following functions have O(n) time complexity. // Here c is a positive integer constant for (int i = 1; i <= n; i += c) { // some O(1) expressions } for (int i = n; i > 0; i -= c) { // some O(1) expressions } Some examples 3) O(n^c): Time complexity of nested loops is equal to the number of times the innermost statement is executed. For example the following sample loops have O(n2) time complexity for (int i = 1; i <=n; i += c) { for (int j = 1; j <=n; j += c) { // some O(1) expressions } } for (int i = n; i > 0; i -= c) { for (int j = i+1; j <=n; j += c) { // some O(1) expressions } For example Selection sort and Insertion Sort have O(n^2) time complexity. Some examples 4) O(Log(n)) Time Complexity of a loop is considered as O(Log(n)) if the loop variables is divided / multiplied by a constant amount. for (int i = 1; i <=n; i *= c) { // some O(1) expressions } for (int i = n; i > 0; i /= c) { // some O(1) expressions } For example Binary Search(refer iterative implementation) has O(Log(n)) time complexity. 5) O(Log(Log(n))) Time Complexity of a loop is considered as O(Log(Log(n))) if the loop variables is reduced / increased exponentially by a constant amount. // Here c is a constant greater than 1 for (int i = 2; i <=n; i = pow(i, c)) { // some O(1) expressions } //Here fun is sqrt or cuberoot or any other constant root for (int i = n; i > 0; i = fun(i)) { // some O(1) expressions } Some examples 6) Linear search Algorithm: 1. [Initialize] Set K:=1 and LOC:=0 2. Repeat Step 3 and 4 while LOC:=0 and K<=N 3. If ITEM=DATA[K], then Set LOC:=K 4. Set K:=K+1.[Increment Counter.][End of step 2 loop.] 5. [Successful?] If LOC:=0, then: Write : ITEM is not in the array DATA Else: Write : LOC is the location of ITEM. [End of if structure.] 7. Exit Counting 1 Every extraordinary feet began in ordinary circumstances. I will start my journey of success from where I am now. References Discrete Mathematics and its Applications by Kenneth H. Rosen • Chapter 6 : Counting Discrete Mathematics by Seymour Lipschutz, Marc Lipso • Chapter 6 : Counting Counting (PPT) by Zeph Grunschlag 2 Why? To solve different types of problem • Counting is used to determine the complexity of algorithms. • Counting is also required to determine whether there are enough telephone numbers or Internet protocol addresses to meet demand. • Recently, it has played a key role in mathematical biology, especially in sequencing DNA. • Furthermore, counting techniques are used extensively when probabilities of events are computed. 3 Basic Counting Principles: THE SUM RULE The Sum Rule If a task can be done either in one of n1 ways or in one of n2 ways, where none of the set of n1 ways is the same as any of the set of n2 ways, then there are n1 + n2 ways to do the task. Sum Rule: If no two events can occur at the same time, then one of the events can occur in: n1+n2+n3+···ways. Sum Rule Principle: Suppose A and B are disjoint sets. Then n(A∪B) = n(A) + n(B) 4 Basic Counting Principles: THE SUM RULE The Sum Rule If a task can be done either in one of n1 ways or in one of n2 ways, where none of the set of n1 ways is the same as any of the set of n2 ways, then there are n1 + n2 ways to do the task. Sum Rule: If no two events can occur at the same time, then one of the events can occur in: n1+n2+n3+···ways. Sum Rule Principle: Suppose A and B are disjoint sets. Then n(A∪B) = n(A) + n(B) 5 Basic Counting Principles: THE SUM RULE The Sum Rule If a task can be done either in one of n1 ways or in one of n2 ways, where none of the set of n1 ways is the same as any of the set of n2 ways, then there are n1 + n2 ways to do the task. Sum Rule: If no two events can occur at the same time, then one of the events can occur in: n1+n2+n3+···ways. Sum Rule Principle: Suppose A and B are disjoint sets. Then n(A∪B) = n(A) + n(B) 6 The Sum Rule : Example Problem : A student can choose a computer project from one of three lists. The three lists contain 23, 15, and 19 possible projects, respectively. No project is on more than one list. How many possible projects are there to choose from? Solution: The student can choose a project by selecting a project from the first list, the second list, or the third list. Because no project is on more than one list, by the sum rule there are 23+15+19=57 ways to choose a project. 7 The Sum Rule : Example Problem : Suppose a college has 3 different history courses, 4 different literature courses, and 2 different sociology courses. What is the number of ways a student can choose just one of the courses? Solution: 8 The Sum Rule : Example Problem : Solution: 9 Basic Counting Principles: THE PRODUCT RULE The 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 for each of these ways of doing the first task, there are n2 ways to do the second task, then there are n1.n2 ways to do the procedure. Product Rule: If the events occur one after the other, then all the events can occur in the order indicated in: n1·n2·n3·...ways. Product Rule Principle: Let A × B be the Cartesian product of sets A and B. Then n(A×B) = n(A) · n(B) 10 Basic Counting Principles: THE PRODUCT RULE The 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 for each of these ways of doing the first task, there are n2 ways to do the second task, then there are n1.n2 ways to do the procedure. Product Rule: If the events occur one after the other, then all the events can occur in the order indicated in: n1·n2·n3·...ways. Product Rule Principle: Let A × B be the Cartesian product of sets A and B. Then n(A×B) = n(A) · n(B) 11 Basic Counting Principles: THE PRODUCT RULE The 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 for each of these ways of doing the first task, there are n2 ways to do the second task, then there are n1.n2 ways to do the procedure. Product Rule: If the events occur one after the other, then all the events can occur in the order indicated in: n1·n2·n3·...ways. Product Rule Principle: Let A × B be the Cartesian product of sets A and B. Then n(A×B) = n(A) · n(B) 12 The Product Rule : Example Problem : How many different license plates can be made if each plate contains a sequence of three uppercase English letters followed by three digits (and no sequences of letters are prohibited)? Solution: There are 26 choices for each of the three uppercase English letters and ten choices for each of the three digits. Hence, by the product rule there are a total of 26·26·26·10·10·10 = 17,576,000 possible license plates. 13 The Product Rule : Example Problem : Suppose a college has 3 different history courses, 4 different literature courses, and 2 different sociology courses. What is the number of ways a student can choose just one of each kind of courses? Solution: 14 The Product Rule : Example Problem : How many functions are there from a set with m elements to a set with n elements? Solution: A function corresponds to a choice of one of the n elements in the codomain for each of the m elements in the domain. Hence, by the product rule there are n·n·n ··· ·n = nm functions from a set with m elements to one with n elements. For example, there are 53 = 125 different functions from a set with three elements to a set with five elements. 15 The Product Rule : Example Problem : 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 a port consists of two tasks, first picking a microcomputer and then picking a port on this microcomputer. Because there are 32 ways to choose the microcomputer and 24 ways to choose the port no matter which microcomputer has been selected, the product rule shows that there are 32·24=768 ports.: 16 The Product Rule : Example Problem : A new company with just two employees, Aowal and Ajoy, rents a floor of a building with 12 offices. How many ways are there to assign different offices to these two employees? Solution: The procedure of assigning offices to these two employees consists of assigning an office to Koni, which can be done in 12 ways, then assigning an office to Nadira different from the office assigned to Koni, which can be done in 11 ways. By the product rule, there are 12·11 = 132 ways to assign offices to these two employees. 17 The Product Rule : Example Problem : How many different bit strings of length seven are there? Solution: Each of the seven bits can be chosen in two ways, because each bit is either 0 or 1. Therefore, the product rule shows there are a total of 2^7=128 different bit strings of length seven. 18 The Product Rule : Example Problem : Solution: 19 The Subtraction Rule (Inclusion–Exclusion for Two Sets) Theorem If a task can be done in either n1 ways or n2 ways, then the number of ways to do the task is n1+ n2 minus the number of ways to do the task that are common to the two different ways. 20 The Subtraction Rule : Example Problem : A computer company receives 350 applications from computer graduates for a job planning a line of new Web servers. Suppose that 220 of these applicants majored in computer science, 147 majored in business, and 51 majored both in computer science and in business. How many of these applicants majored neither in computer science nor in business? Solution: Let A1 be the set of students who majored in computer science and A2 the set of students who majored in business. Then A1∪A2 is the set of students who majored in computer science or business (or both), and A1∩A2 is the set of students who majored both in computer science and in business. By the subtraction rule the number of students who majored either in computer science or in business (or both) equals |A1∪A2|=|A1|+|A2|−|A1∩A2| = 220 + 147 − 51 = 316. We conclude that 350 − 316 = 34 of the applicants majored neither in computer science nor in business. 21 The Subtraction Rule : Example Problem : How many bit strings of length eight either start with a 1 bit or end with the two bits 00? Solution: 22 The Division Rule Theorem There are n/d ways to do a task if it can be done using a procedure that can be carried out in n ways, and for every way w, exactly do f then ways correspond to way w. 23 The Division Rule: Example Problem : How many different ways are there to seat four people around a circular table, where two seating's are considered the same when each person has the same left neighbor and the same right neighbor? Solution: n!/n= (n-1)! Here, n=4 Ways to seat four people around a circular table= (4-1)!=3!=6 24 Tree Diagram • How many bit strings of length four do not have two consecutive 1s? 25 Tree Diagram • Suppose that “I Love New Jersey” T-shirts come in five different sizes: S, M, L, XL, and XXL. Further suppose that each size comes in four colors, white, red, green, and black, except for XL, which comes only in red, green, and black, and XXL, which comes only in green and black. How many different shirts does a souvenir shop have to stock to have at least one of each available size and color of the T-shirt? 26 Tree Diagram • Suppose that “I Love New Jersey” T-shirts come in five different sizes: S, M, L, XL, and XXL. Further suppose that each size comes in four colors, white, red, green, and black, except for XL, which comes only in red, green, and black, and XXL, which comes only in green and black. How many different shirts does a souvenir shop have to stock to have at least one of each available size and color of the T-shirt? Ans.: 15 27 How do you Learn? Problem A history class contains 8 male students and 6 female students. Find the number of ways that the class can elect: (a) 1 class representative; Ans.: 14 (b) 2 class representatives, 1 male and 1 female; Ans.: 8*6=48 (c) 1 president and 1 vice president. Ans.: 14*13=182 28 The Pigeonhole Principle 29 The Pigeonhole Principle Theorem If k is a positive integer and k+1 or more objects are placed into k boxes, then there is at least one box containing two or more of the objects. 30 The Pigeonhole Principle Theorem If k is a positive integer and k+1 or more objects are placed into k boxes, then there is at least one box containing two or more of the objects. 31 The Pigeonhole Principle Example • Suppose a department contains 13 professors, then two of the professors (pigeons) were born in the same month (pigeonholes) • Among any group of 367 people, there must be at least two with the same birthday, because there are only 366 possible birthdays. 32 The Pigeonhole Principle Example • Suppose a department contains 13 professors, then two of the professors (pigeons) were born in the same month (pigeonholes) • Among any group of 367 people, there must be at least two with the same birthday, because there are only 366 possible birthdays. 33 The Pigeonhole Principle Example • Among any group of 367 people, there must be at least two with the same birthday, because there are only 366 possible birthdays. • How many students must be in a class to guarantee that at least two students receive the same score on the final exam, if the exam is graded on a scale from 0 to 100 points? • Solution: There are 101 possible scores on the final. The pigeonhole principle shows that among any 102 students there must be at least 2 students with the same score. 34 The Generalized Pigeonhole Principle Theorem If N objects are placed into k boxes, then there is at least one box containing at least [N/k] objects. If k pigeonholes are occupied by [nk+1] or more pigeons, where k is a positive integer, then at least one pigeonhole is occupied by [n+1] or more pigeons 35 The Generalized Pigeonhole Principle Example • Among 100 people there are at least 100/12=9 who were born in the same month. • What is the minimum number of students required in a discrete mathematics class to be sure that at least six 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 six students receive the same grade is the smallest integer N such thatN/5=6. The smallest such integer is N=5·5+1=26. If you have only 25 students, it is possible for there to be five who have received each grade so that no six students have received the same grade. Thus, 26 is the minimum number of students needed to ensure that at least six students will receive the same grade. 36 The Generalized Pigeonhole Principle Example • What is the minimum number of students required in a discrete mathematics class to be sure that at least six 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 six students receive the same grade is the smallest integer N such that N/5=6. The smallest such integer is N=5·5+1=26. If you have only 25 students, it is possible for there to be five who have received each grade so that no six students have received the same grade. Thus, 26 is the minimum number of students needed to ensure that at least six students will receive the same grade. 37 Problem!!! • In how many ways can we arrange five students in a line for a picture? • In how many ways can we select three students from a group of five students to stand in line for a picture? 38 Permutations Theorem Any arrangement of a set of n objects in a given order is called a permutation of the object (taken all at a time). 39 Permutations Theorem Any arrangement of a set of n objects in a given order is called a permutation of the object (taken all at a time). In how many ways can we arrange three letter A, B, C without repetition? 40 Permutations Theorem Any arrangement of a set of n objects in a given order is called a permutation of the object (taken all at a time). In how many ways can we arrange three letter A, B, C without repetition? Corollary There are n! permutations of n objects (taken all at a time). 41 r-permutation Theorem Any arrangement of any r ≤ n of n objects in a given order is called an “r-permutation” or “a permutation of then objects taken r at a time.” 42 r-permutation Theorem Any arrangement of any r ≤ n of n objects in a given order is called an “r-permutation” or “a permutation of then objects taken r at a time.” The number of permutations of n objects taken r at a time will be denoted by 43 r-permutation • In how many ways can we select three students from a group of five students to stand in line for a picture? • In how many ways can we select four students from a group of five students to stand in line for a picture 44 r-permutation Theorem 45 P (n, r) = n(n−1)(n−2)···(n−r+1) Proof We will use the product rule to prove that this formula is correct. The first element of the permutation can be chosen in n ways because there are n elements in the set. There are n−1 ways to choose the second element of the permutation, because there aren−1 elements left in the set after using the element picked for the first position. Similarly, there are n−2 ways to choose the third element, and so on, until there are exactly n−(r−1)=n−r+1 ways to choose the rth element. Consequently, by the product rule, there are n(n−1)(n−2)···(n−r+1) r-permutations of the set. 46 r-permutation Theorem • Note that P(n,0)=1 whenever n is a nonnegative integer • because there is exactly one way to order zero elements. • That is, there is exactly one list with no elements in it, namely the empty list. 47 r-permutation COROLLARY 48 r-permutation : Example Problem: 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? Solve: Because it matters which person wins which prize, the number of ways to pick the three 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. Consequently, the answer is P(100,3) = 100·99·98 = 970,200. 49 r-permutation : Example Problem: Suppose that there are eight runners in a race. The winner receives a gold medal, the second place finisher receives a silver medal, and the third-place finisher receives a bronze medal. How many different ways are there to award these medals, if all possible outcomes of the race can occur and there are no ties? Solve: The number of different ways to award the medals is the number of 3permutations of a set with eight elements. Hence, there are P(8,3) = 8·7·6 = 336 possible ways to award the medals. 50 r-permutation : Example Problem: How many permutations of the letters ABCDEFGH contain the string ABC? Solve: Because the letters ABC must occur as a block, we can find the answer by finding the number of permutations of six objects, namely, the block ABC and the individual letters D, E, F, G, and H. Because these six objects can occur in any order, there are 6!=720 permutations of the letters ABCDEFGH in which ABC occurs as a block. 51 Permutations with Repetitions • Frequently we want to know the number of permutations of a multi set, that is, a set of objects some of which are alike. We will let P(n; n1,n2, ..., nr) • denote the number of permutations of n objects of which n1 are alike, n2 are alike, ..., nr are alike. • The general formula follows: 52 Permutations with Repetitions Theorem 53 Permutations with Repetitions: Example 54 Permutations with Repetitions: Example • Find the number of seven-letter words that can be formed using the letters of the word “BENZENE.” 55 Again Problem!!! • How many different committees of three students can be formed from a group of four students? 56 r-combination Theorem Let S be a set with n elements. A combination of these n elements taken r at a time is any selection of r of the elements where order does not count. Such a selection is called an r-combination; it is simply a subset of S with r elements. The number of such combinations will be denoted by 57 r-combination : Example Problem: Find the number of combinations of 4 objects A,B,C,D, taken 3 at a time. Solve: 58 r-combination Theorem 59 r-combination • How many ways are there to select five players from a 10-member tennis team to make a trip to a match at another school? • A group of 30 people have been trained as astronauts to go on the first mission to Mars. How many ways are there to select a crew of six people to go on this mission (assuming that all crew members have the same job)? 60 The Binomial Theorem Theorem 61 The Binomial Theorem Problem: What is the expansion of (x+y)4 Solve: 62 The Binomial Theorem What is the coefficient of x12y13 in the expansion of (x+y)25? Solve: 63 Pascal’s Identity and Triangle Theorem 64 Pascal’s Identity and Triangle 65 Graphs The truth prevails in the end. I will always stand for the truth Use of Graphs • Car navigation system • Efficient database • Build a bot to retrieve info off WWW • Representing computational models • Many other applications. This course we focus more on the properties of abstract graphs rather on algorithms. Graphs • Graphs is special data structure, which we can represent through two sets• Set of Vertices / Nodes - V • Set of Edges - E Dhaka1 Rajshahi Dilly DHK 2 • Let, • V = {1, 2, 3, 4} • E = {(1,2), (2,3), (1,4)} Bangkok 3 Khulna NY 4CTG Graphs –Intuitive Notion • A graph is a bunch of vertices (or nodes) represented by circles are connected by edges, represented by line segments which Graphs Definition A graph G = (V , E) consists of V, a nonempty set of vertices (or nodes) and E, a set of edges (possibly empty). Each edge has either one or two vertices associated with it, called its end points. An edge is said to connect its endpoints. Graphs Example {1,2} 1 2 {2,3} {1,3} {2,4} {3,4} 3 4 {1,4} SET OF VERTICES V = { 1, 2, 3, 4 } SET OF EDGES E = { (1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (1, 4) } L23 6 Simple Graphs Example Dhaka Dilly SET OF VERTICES SET OF EDGES Bangkok NY Infinite and Finite Graphs Infinite Graphs A graph with an infinite vertex set or an infinite number of edges is called an infinite graph. Finite Graphs A graph with a finite vertex set and a finite edge set is called a finite graph. Simple Graphs Definition A graph in which each edge connects two different vertices and where no two edges connect the same pair of vertices is called a simple graph. Dhaka Dilly Bangkok NY Multigraphs Definition Graphs that may have multiple edges connecting the same vertices are called multigraphs. Dhaka Dilly Bangkok NY Pseudographs Definition Edges that connect a vertex to itself are called loops. A graph with loop (self-loop) is called pseudograph. Dhaka Dilly Bangkok NY Undirected Graphs Dhaka Dilly Bangkok NY Directed Graphs • • • • Dhaka to Rajshahi Dhaka to Chapai Rajshahi to Dhaka No Chapai to Dhaka, but Chapai to Rajshahi Dhaka Rajshahi Chapai A Directed Graph Definition A directed graph (or digraph) (V, E) consists of a nonempty set of vertices V and a set of directed edges (or arcs) E. Each directed edge is associated with an ordered pair of vertices. The directed edge associated with the ordered pair (u, v) is said to start at u and end at v. The edge (a,b) is also denoted by a b • a is called the source of the edge while • b is called the target of the edge. Directed Graphs • Simple Directed Graphs • Multiple Directed Graphs • Mixed Graphs Types of Graphs Undirected Graphs: Adjacent Definition Vertices are adjacent if they are the endpoints of the same edge or they are connected by the same edge. Adjacent of 1 : 2 and 4 Adjacent of 2 : 1 and 3 Adjacent of 3 : 2 1 2 3 4 Undirected Graphs Terminology Adjacent Vertices (Neighbors) Definition 1. Two vertices, u and v in an undirected graph G are called adjacent (or neighbors) in G, if {u, v} is an edge of G. An edge e connecting u and v is called incident with vertices u and v, or is said to connect u and v. The vertices u and v are called endpoints of edge {u, v}. 18 Undirected Graphs Terminology e1 e2 1 e4 e5 e3 3 A: L23 2 e6 4 1 is adjacent to 2 and 3 2 is adjacent to 1 and 3 3 is adjacent to 1 and 2 4 is not adjacent to any vertex 19 Undirected Graphs Terminology A vertex is incident with an edge (and the edge is incident with the vertex) if it is the endpoint of the edge. e1 e2 1 2 e4 e5 e3 3 e6 4 Q: Which edges are incident to 1? How about incident to 2, 3, and 4? L23 20 Undirected Graphs Terminology e1 e2 1 e4 e5 e3 3 A: L23 2 e6 4 e1, e2, e3, e6 are incident with 1 2 is incident with e1, e2, e4, e5, e6 3 is incident with e3, e4, e5 4 is not incident with any edge 21 Undirected Graphs: Degree Definition The degree of a vertex in an undirected graph is the number of edges incident with it, except that a loop at a vertex contributes twice to the degree of that vertex. The degree of the vertex v is denoted by deg(v). deg (1) : 2 deg (3) : 1 deg (4) : 3 1 2 3 4 Directed Graphs : Adjacent Definition Adjacent of 1 : 2 and 4 Adjacent of 2 : 1 and 3 Adjacent of 3 : 2 1 2 3 4 Directed Graphs : Degree 1 2 3 4 In degree : Out degree: Directed Graphs : Degree Definition Oriented Degree when Edges Directed The in-degree of a vertex (deg-) counts the number of edges that stick in to the vertex. The out-degree (deg+) counts the number sticking out. 2 1 3 Q: What are in-degrees and out-degrees of all the vertices? L23 26 Oriented Degree when Edges Directed A: L23 deg-(1) = 0 deg-(2) = 3 deg-(3) = 4 deg+(1) = 2 deg+(2) = 3 deg+(3) = 2 2 1 3 27 Some Special Simple Graphs : Complete Graphs - Kn Complete Graphs - Kn A complete graph on n vertices, denoted by Kn, is a simple graph that contains exactly one edge between each pair of distinct vertices. No. of Edges = n(n-1) / 2 Graph Patterns Complete Graphs - Kn Complete Graphs – Kn , when n = 1, 2, 3, 4, 5 K1 L23 K2 K3 K4 K5 29 Some Special Simple Graphs : Cycles - Cn Cycles - Cn A cycle Cn, n ≥ 3, consists of n vertices v1, v2, ….., vn and edges {v1, v2}, {v2, v3}, ..., {vn−1, vn}, and {vn, v1}. That is, vertex i is connected to i +1 mod n and to i -1 mod n vertex. No. of Edges = n Graph Patterns Cycles - Cn Q: What type of graph are C1 and C2 ? C1 C2 C3 C4 C5 A: Pseudographs L23 31 Some Special Simple Graphs : Wheel Graph Wheel Graph We obtain a wheel Wn when we add an additional vertex to a cycle Cn, for n ≥ 3, and connect this new vertex to each of then vertices in Cn, by new edges. Graph Patterns Wheels - Wn Wn ; n = 3, 4, 5 W3 W4 W5 . L23 33 Some Special Simple Graphs : Cubes - Qn Cubes - Qn The n-cube Qn is defined recursively. Q0 is just a vertex. Qn+1 is gotten by taking 2 copies of Qn and joining each vertex v of Qn with its copy v’ : L23 Q0 Q1 Q2 Q3 Q4 (hypercube) 34 Some Special Simple Graphs Cubes - Qn The n-cube Qn is defined recursively. Q0 is just a vertex. Qn+1 is gotten by taking 2 copies of Qn and joining each vertex v of Qn with its copy v’ : Q0 L23 Q1 Q2 Q3 Q4 (hypercube) 35 Some Special Simple Graphs Cubes - Qn The n-cube Qn is defined recursively. Q0 is just a vertex. Qn+1 is gotten by taking 2 copies of Qn and joining each vertex v of Qn with its copy v’ : L23 Q0 Q1 Q2 Q3 Q4 (hypercube) 36 Bipartite Graphs Definition Is Bipartite Graphs? Bipartite Graphs Another definition L23 39 EG: C4 is a bichromatic: And so is bipartite, if we redraw it: L23 40 EG: C4 is a bichromatic: And so is bipartite, if we redraw it: L23 41 EG: C4 is a bichromatic: And so is bipartite, if we redraw it: L23 42 EG: C4 is a bichromatic: And so is bipartite, if we redraw it: L23 43 EG: C4 is a bichromatic: And so is bipartite, if we redraw it: L23 44 Bipartite Graphs EG: C4 is a bichromatic: And so is bipartite, if we redraw it: L23 45 EG: C4 is a bichromatic: And so is bipartite, if we redraw it: L23 46 EG: C4 is a bichromatic: And so is bipartite, if we redraw it: L23 47 Bipartite Graphs EG: C4 is a bichromatic: And so is bipartite, if we redraw it: L23 48 EG: C4 is a bichromatic: And so is bipartite, if we redraw it: L23 49 EG: C4 is a bichromatic: And so is bipartite, if we redraw it: L23 50 EG: C4 is a bichromatic: And so is bipartite, if we redraw it: Q: For which n is Cn bipartite? L23 51 Is Bipartite Graphs? Bipartite Graphs A: Cn is bipartite when n is even. For even n color all odd numbers red and all even numbers green so that vertices are only adjacent to opposite color. If n is odd, Cn is not bipartite. If it were, color 0 red. So 1 must be green, and 2 must be red. This way, all even numbers must be red, including vertex n1. But n-1 connects to 0 . L23 53 Graph Patterns Complete Bipartite - Km,n When all possible edges exist in a simple bipartite graph with m red vertices and n green vertices, the graph is called complete bipartite and the notation Km,n is used. EG: K2,3 L23 K4,5 54 Degree of a vertex Definition 1. The degree of a vertex in an undirected graph is the number of edges incident with it, except that a loop at a vertex contributes twice to the degree of that vertex. c d b a g f deg( d ) = 1 e 55 Degree Degree of a vertexof a vertex Definition 1. The degree of a vertex in an undirected graph is the number of edges incident with it, except that a loop at a vertex contributes twice to the degree of that vertex. c d b deg( e ) = 0 a g f e 56 Degree Degree of a vertexof a vertex Definition 1. The degree of a vertex in an undirected graph is the number of edges incident with it, except that a loop at a vertex contributes twice to the degree of that vertex. c deg( b ) = 6 d b a g f e 57 Degree Degree of a vertexof a vertex Find the degree of all the other vertices. deg( a ) deg( c ) deg( f ) deg( g ) c deg( b ) = 6 d b deg( d ) = 1 deg( e ) = 0 a g f e 58 Degree Degree of a vertexof a vertex Find the degree of all the other vertices. deg( a ) = 2 deg( c ) = 4 deg( f ) = 3 c deg( b ) = 6 d b deg( g ) = 4 deg( d ) = 1 deg( e ) = 0 a g f e 59 Degree Degree of a vertexof a vertex Find the degree of all the other vertices. deg( a ) = 2 deg( c ) = 4 deg( f ) = 3 deg( g ) = 4 TOTAL of degrees = 2 + 4 + 3 + 4 + 6 + 1 + 0 = 20 c deg( b ) = 6 d b deg( d ) = 1 deg( e ) = 0 a g f e 60 Degree Degree of a vertexof a vertex Find the degree of all the other vertices. deg( a ) = 2 deg( c ) = 4 deg( f ) = 3 deg( g ) = 4 TOTAL of degrees = 2 + 4 + 3 + 4 + 6 + 1 + 0 = 20 TOTAL NUMBER OF EDGES = 10 c deg( b ) = 6 d b deg( d ) = 1 deg( e ) = 0 a g f e 61 Handshaking Theorem Theorem 1. Let G = (V, E) be an undirected graph G with e edges. Then  deg( v ) = 2 e vV “The sum of the degrees over all the vertices equals twice the number of edges.” NOTE: This applies even if multiple edges and loops are present. 62 Adjacency Matrix A simple graph G = (V, E) with n vertices can be represented by its adjacency matrix, A, where entry aij in row i and column j is 1 if { vi, vj } is an edge in G, 0 otherwise. aij = 63 Finding the adjacency matrix This graph has 6 vertices c a, b, c, d, e, f. We can d b arrange them in that order. f a e W5 64 Finding theadjacency adjacency Finding the matrixmatrix TO c FROM d b b c d e f 0 1 0 0 1 1 b f a a a e c d W5 e f There are edges from a to b, from a to e, and from a to f 65 Finding theadjacency adjacency Finding the matrixmatrix TO c a FROM d b f a e b c d a 0 1 0 0 b 1 0 1 e f 1 1 0 0 1 c d W5 e f There are edges from b to a, from b to c, and from b to f 66 Finding theadjacency adjacency Finding the matrixmatrix TO c a FROM d b f a e b c d e a 0 1 0 0 b 1 0 1 0 0 1 c 0 1 0 1 0 1 f 1 1 d W5 e f There are edges from c to b, from c to d, and from c to f 67 Finding theadjacency adjacency Finding the matrixmatrix TO c a FROM d b f a e b c d e a 0 1 0 0 b 1 0 1 0 0 1 c 0 1 0 1 0 1 f 1 1 d W5 e f COMPLETE THE ADJACENCY MATRIX . . . 68 Finding theadjacency adjacency Finding the matrixmatrix TO c a b c d e f a 0 1 0 0 1 1 b 1 0 1 0 0 1 c 0 1 0 1 0 1 d 0 0 1 0 1 e 1 0 0 1 0 1 f 1 1 1 1 1 0 FROM d b f a e W5 1 Notice that this matrix is symmetric. That is aij = aji Why? 69 Tree • A tree is a connected undirected graph with no simple circuits. • A tree cannot have a simple circuit, • A tree cannot contain multiple edges or loops. • Any tree must be a simple graph. • An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices. Which of the graphs shown in Figure 2 are trees? – Solution: G1 and G2 are trees, because both are connected graphs with no simple circuits. G3 is not a tree because e, b, a, d, e is a simple circuit in this graph. Finally, G4 is not a tree because it is not connected. Tree Terminology • Root: node without parent (A) • Siblings: nodes share the same parent • Internal node: node with at least one child (A, B, C, F) • External node (leaf ): node without children (E, I, J, K, G, H, D) • Ancestors of a node: parent, grandparent, grand-grandparent, etc. • Descendant of a node: child, grandchild, grand-grandchild, etc. • Depth of a node: number of ancestors • Height of a tree: maximum depth of any node (3) • Degree of a node: the number of its children • Degree of a tree: the maximum number of its node. • Subtree: tree consisting of a node and its descendants. A B E F I D C J G K H Rooted Trees • A rooted tree is a tree in which one vertex has been designated as the root and every edge is directed away from the root. Difference between tree and Rooted Tree • A rooted tree comes with one of its vertices specially designated to be the "root" node, such that there's an implicit notion of "towards the root" and "away from the root" for each edge. • In a free tree there's no designated root vertex. m-ary tree • A rooted tree is called an m-ary tree if every internal vetex has no more than m children. • The tree is called a full m-ary tree if every internal vertex has exactly m children. An m-ary tree with m=2 is called a binary tree. full binary tree full 3-ary tree full 5-ary tree not full 3-ary tree Properties of Trees • A tree with n vertices has n-1 edges. • A full m-ary tree with i internal vertices contains n = mi + 1 vertices Level & Height • The level of a vertex v in a rooted tree is the length of the unique path from the root to this vertex. • The level of the root is defined to be zero. The height of a rooted tree is the maximum of the levels of vertices. level 0 1 2 3 4 height = 4 Balance Tree: A rooted m-ary tree of height h is balanced if all leaves are at levels h or h-1. Example Which of the rooted trees shown below are balanced? Sol. T1, T3 Theorem:There are at most mh leaves in an m-ary tree of height h. • A complete m-ary tree is a full m-ary tree, where every leaf is at the same level. Applications of Trees Binary Search Trees Goal: Implement a searching algorithm that finds items efficiently when the items are totally ordered. Binary Search Tree: Binary tree + each child of a vertex is designed as a right or left child, and each vertex v is labeled with a key label(v), which is one of the items. Note: label(v) > label(w) if w is in the left subtree of v and label(v) < label(w) if w is in the right subtree of v Example Form a binary search tree for the words mathematics, physics, geography, zoology, meteorology, geology, psychology, and chemistry (using alphabetical order). Sol. Decision Trees • A rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for each possible outcome of the decision, is called a decision tree. Example A decision tree that orders the elements of the list a, b, c. Sol. Prefix Codes • Using bit strings to encode the letter of the English alphabet. Example: encode e:0 a : 10 t : 110 n : 1110 s : 1111 decode 11111011100 s a n e Tree traversal • Pre-order(PLR) • In-order(LPR) • Post-order(LRP) Spanning Trees • Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G Sol. Remove an edge from any circuit. (repeat until no circuit exists) Four spanning trees of G: Example Use depth-first search to find a spanning tree for the graph. Sol. (arbitrarily start with the vertex f) Breadth-First Search (BFS) Example 5 Use breadth-first search to find a spanning tree for the graph. Sol. (arbitrarily start with the vertex e) Minimum spanning tree • minimum spanning tree of G: a spanning tree of G with smallest sum of weights of its edges. Using Prim’s algorithm to find a minimum spanning tree of G. Algebraic Structures and Coding Theory: Groups, Rings, and Fields 1 Algebraic structures To study a phenomenon or process of the real world, we construct a suitable mathematical model to represent it and study the properties of the model to understand the phenomenon this mathematical structures are called Algebras or Algebraic structures. Components of algebra: I. An underlying set S(sometimes it is called the carrier of the algebra) II. Operations defined on this set. III. Special elements of the underlying set possessing specific properties. These are called constants of the algebra. Semi groups Let S be a set and let * be a binary operation on S. The operation * 1.is commutative over S, if a*b=b*a 2.is associative over S, if a*(b*c)=(a*b)*c, for a, b, c ∈S Semi group: Let A be an algebra with an underlying set T and * a binary operation on T.(T,*) is called a semi group if the following two conditions are satisfied 1.T is closed with respect to * 2.* is an associative operation. Sub Semi groups and monoid If (S,*) is a semi group and (T,*) is also a semi group and T is belong to S then (T,*) is sub semi group of (S,*) semi group. Monoid: Let (T,*) be an algebraic system, where * is a binary operation on T.(T,*) is called monoid if the following conditions are satisfied. 1.T is closed with respect to *. 2.* is an associative operation. 3.There exists an identity element e ∈T for the operation *. i.e., for any x ∈T, e*x=x*e=x Group Let (T,*) be an algebraic system, where * is a binary operation on T.(T,*) is called group if the following conditions are satisfied. 1.T is closed with respect to *. 2.* is an associative operation. 3.There exists an identity element e ∈T for the operation *. 4.Each element x ∈T has an inverse element x-1 ∈T with respect to *. i.e., for any x ∈T, x* x-1 = x-1 *x=e(identity element) Not a Group Order of the group If G is group where G=<{a,b,c},*> Then order of G is denoted by |G| Where |G|=3(No. of elements in the given set) Group of order |G|=1 is: G=<{e},*> Because every group must have one identity element i.e: {e} * e The composition table is: e e Order of the group Group of order 2 is |G|=2 is: G=<{e,a},*> a=any random element Because every group must have one identity element i.e: {e} * e a The composition table is: e e a a*e=a a a e e*a=a a*a=e/a Here a*a=e because every elements in the set should have inverse elements so here the inverse element of e is e(identity). So the identity of a should be a or e but here we know a*e=a so e is not inverse of a so a is inverse of itself. Order of the group Group of order 3 is |G|=3 is: G=<{e,a,b},*> a, b are random elements. And e is identity element(Because every group must have one identity element i.e: {e}) * e a b The composition table is: e e a b a*a=b/e a a b e b b e a a*b=e/b here a*b=b is not Possible because a is not identity So, a*b=e So, a*a=b 6 – Our first group • We begin with a set of six numbers 6 = { 0, 1, 2, 3, 4, 5 } • We define an operation on the set which we call addition and denote by + • This is similar to, but not really the same as, the usual operation of addition • To see this, let's add 1 to each element of 6 10 Add 1 to each element of 6 0+1=1 1+1=2 2+1=3 3+1=4 4+1=5 5+1=? • What is 5 + 1? • Usually it's 6, but there is no 6 in our set 11 • What to do? Getting nothing from something • Let's be bold and define 5+1=0 • This might seem unreasonable, but consider what time it is 3 hours after 11am: 11 + 3 = 2 • Out of context this seems unreasonable too, but when doing "clock arithmetic" it makes sense • Remember we said + is not exactly 12 addition Add 2 to each element of 6 0+2=2 1+2=3 2+2=4 3+2=5 4+2=0 5+2=1 • Now we seem to have two strange results, but keep thinking about clock arithmetic 13 Addition table for 6   0 1 2 3 4 5 1 2 3 4 5 0 2 3 4 5 0 1 3 4 5 0 1 2 4 5 0 1 2 3 5 0 1 2 3 4 Pick an element x in the left column and an element y in the top row 14 Where the row and column meet is x + y Addition made easy • Given any two elements x and y of 6 the addition table shows us how to add them together to get the sum x + y • There's another way to do it: Just add x plus y in the usual way as if they were ordinary whole numbers to get a sum z If z < 6, then z is the sum If z ≥ 6, then z – 6 is the sum 15 Examples of addition 3+2=5 3 + 3 = 6, so 6 - 6 = 0 is the sum 3 + 4 = 7, so 7 - 6 = 1 is the sum 4+1=5 4 + 4 = 8, so 8 - 6 = 2 is the sum 5 + 5 = 10, so 10 - 6 = 4 is the sum Note how similar this is to clock arithmetic 16 0 is an identity   0+0=0 1+0=1 2+0=2 3+0=3 4+0=4 5+0=5 So x + 0 = x for every x We say 0 is an identity element for + 17 Every element has an inverse  0+0=0 1+5=0 2+4=0 3+3=0 4+2=0 5+1=0 Every element x has an inverse element x' such that 18 x + x' = 0 Be associative • How to compute 5 + 4 + 2 ? (5 + 4) + 2 = (3) + 2 = 5 5 + (4 + 2) = 5 + (0) = 5 • For any three elements x, y, z of 6 we have (x + y) + z = x + (y + z)‫‏‬ • Another way to say this: Addition is associative 19 Can we prove + is associative? • There are 6∙6∙6 = 216 different triples of numbers from 6 and + appears 4 times in (x + y) + z = x + (y + z)‫‏‬  So by using the addition table 4∙216 = 864 times, we can prove that + is associative  This‫‏‬is‫‏‬a‫‏‬proof‫‏‬by‫“‏‬main‫‏‬strength‫‏‬and‫‏‬ awkwardness”  There are better ways 20 Where are we?    We have a set of six elements 6 = { 0, 1, 2, 3, 4, 5 } We have an operation + defined on the set which takes any two elements of the set and combines them to produce another element of the set The operation is associative, so for any three elements x, y, and z we have (x + y) + z = x + (y + z)‫‏‬ 21 Still in Kansas? • An identity element 0 exists so that for any element x we have x+0=x • Every element x has an inverse element x' so that x + x' = 0 • In short, we have a group 22 Groups • Note‫‏‬that‫‏‬the‫‏‬word‫“‏‬group”‫‏‬is‫‏‬being‫‏‬used‫‏‬ here in a highly technical sense • A group is a non-empty set with a binary operation defined on it such that the operation is associative, an identity element exists, and every element has an inverse • The group is one of the most fundamental structures in mathematics • Groups sprout like weeds in modern math 23 Examples of groups • Let n = { 0, 1, 2, ..., n-1 } for any integer n > 0, and define + in analogy with + for 6 • n is a group • n is called the cyclic group of n elements • Note that for every positive integer n there exists at least one group with n elements • So we don't have to worry about running out of groups 24 Some groups go on forever ... • Now consider  = { ..., -2, -1, 0, 1, 2, ... }, the set of all integers, and let + be the usual addition of integers • Is <,+> a group? • • • • Is + a binary operation on ? Is + associative? Which element is the identity? For x an integer, what is its inverse? 25  is indeed a group • • • • + on integers is a binary operation It is associative 0 is the identity element For any integer x, its inverse is -x •  is an infinite group, the first such we have seen 26 Is <,*> a group? • Now consider  and the operation *, the usual multiplication of integers • Is <,*> a group? • • • • Is * a binary operation on ? Is * associative? Which element is the identity? For x an integer, what is its inverse? 27 <,*> is NOT a group • • • • • * on integers is a binary operation It is associative 1 is the identity element For any integer x, its inverse is 1/x Whoops! 1/x is not an integer (except when x = -1 or x = +1), so the vast majority of integers have no multiplicative inverses 28 Is <+,*> a group? • Let + denote the set of all positive real numbers, and let * be the usual multiplication of real numbers • Is <+,*> a group? • • • • Is * a binary operation on +? Is * associative? Which element is the identity? For x a positive real, what is its inverse? 29 <+,*> is a group • • • • * on real numbers is a binary operation It is associative 1 is the identity element For any real x, its inverse is 1/x • <+,*> is an infinite group, the second such we have seen • [It's way more infinite than <,+>, but that's the subject of another talk.] 30 Time for some geometry • All the groups we have seen so far are arithmetic in nature • It's time to consider a geometrical example • We start with a regular hexagon • hexa- means six (Greek)‫‏‬ • -gon means angled (Greek)‫‏‬ • regular means all sides are equal and all angles are equal 31 A regular hexagon 2 1 3 0 4 5 32 Transformations of the hexagon • We are going to consider transformations of the hexagon which map vertices to vertices • We will restrict our transformations to counterclockwise rotations by multiples of 60º • Any two rotations which map vertex 0 to the same vertex v are considered identical 33 CCW rotations of the hexagon • There are six possible rotations: R0 – rotate the hexagon CCW by 0º R1 – rotate the hexagon CCW by 60º R2 – rotate the hexagon CCW by 120º R3 – rotate the hexagon CCW by 180º R4 – rotate the hexagon CCW by 240º R5 – rotate the hexagon CCW by 300º 34 H - A set of rotations • Let's define a set whose members are the six rotations of the hexagon: H = { R0, R1, R2, R3, R4, R5 } • We define an operation on the set called composition and denote it by * • The composition Ri * Rj of two rotations is simply the rotation obtained by first doing rotation Ri and then doing rotation Rj 35 Examples of composition R 1 * R2 = R 3 R 2 * R2 = R 4 R 3 * R4 = R 1 R5 * R4 = R3 R 3 * R3 = R 0 R2 * R5 = R1 36 R0 is an identity rotation   R0 * R 0 = R 0 R1 * R 0 = R 1 R2 * R 0 = R 2 R3 * R 0 = R 3 R4 * R 0 = R 4 R5 * R 0 = R 5 So x * R0 = x for every x R0 is an identity element for * 37 Every rotation has an inverse  R0 * R 0 = R 0 R1 * R 5 = R 0 R2 * R 4 = R 0 R3 * R 3 = R 0 R4 * R 2 = R 0 R5 * R 1 = R 0 Every element x has an inverse element x' x * x' = R0 38 Composition is associative • How to determine R5 * R4 * R2 ? (R5 * R4) * R2 = (R3) * R2 = R5 R5 * (R4 * R2) = R5 * (R0) = R5 • For any three elements x, y, z of H we have (x * y) * z = x * (y * z)‫‏‬ • So composition is associative (but we haven't proved this)‫‏‬ 39 So, what have we got?    We have a set of six elements H = { R0, R1, R2, R3, R4, R5 } We have an operation * defined on the set which takes any two elements of the set and combines them to produce another element of the set The operation is associative, so for any three elements x, y, and z we have (x * y) * z = x * (y * z)‫‏‬ 40 H is a group! • An identity element R0 exists so that for any element x we have x * R0 = x • Every element x has an inverse element x' so that x * x' = R0 • In short, H is a group! 41 Composition table for H R0 R1 R2 R3 R4 R5 R1 R2 R3 R4 R5 R0 R2 R3 R4 R5 R0 R1 R3 R4 R5 R0 R1 R2 R4 R5 R0 R1 R2 R3 R5 R0 R1 R2 R3 R4 42 Is H starting to look familiar? • Consider this mapping: 0‫ ↔‏‬R0 1‫ ↔‏‬R1 2‫ ↔‏‬R2 3‫ ↔‏‬R3 4‫ ↔‏‬R4 5‫ ↔‏‬R5 43 Some correspondences 1+2=3 2+2=4 3+4=1 5+4=3 3+3=0 2+5=1 ↔ R1 * R 2 = R3 ↔ R2 * R 2 = R4 ↔ R3 * R 4 = R1 ↔ R 5 * R 4 = R3 ↔ R3 * R 3 = R0 ↔ R 2 * R 5 = R1 44 It's repeated all over again • Given a true statement about 6 replace each digit i by Ri and replace + by * and we have a true statement about H • And vice versa • We say that 6 and H are isomorphic <6,+>  <H,*>  iso- means same (Greek)‫‏‬  -morph means shape or form (Greek)‫‏‬ 45 <6,+> is isomorphic to <H,*> • When two mathematical objects are isomorphic, they have the same structure • When a statement is proved true about one structure, the equivalent statement is true about the other structure • Sometimes one structure is easier to work with than another • Addition in 6 may be easier than rotating cardboard hexagons 46 More geometry • Let's consider another geometrical example • This time we'll work with a regular triangle, also called an equilateral triangle • tri- means three (Latin)‫‏‬ • regular means all sides are equal and all angles are equal, so each angle is 60º 47 An equilateral triangle B A C 48 Transformations of the triangle • We're going to consider transformations of the triangle which map vertices to vertices • One kind of transformation is a counterclockwise rotation by a multiple of 120º • Another kind of transformation is a flip around an axis through a vertex, perpendicular to the side opposite 49 Rotations and flips There are six such transformations: R0 – rotate CCW by 0º R1 – rotate CCW by 120º R2 – rotate CCW by 240º F0 – flip around the axis defined by A F1 – flip around the axis defined by B F2 – flip around the axis defined by C 50 Permutations, anyone? Another view of the transformations: R0 ≡ ABC R1 ≡ CAB R2 ≡ BCA F0 ≡ ACB F1 ≡ CBA F2 ≡‫ ‏‬BAC 51 S3 – Triangle transformations • Let's define a set whose members are the six transformations of the triangle: S3 = { R0, R1, R2, F0, F1, F2 } • We define an operation on the set called composition and denote it by * • The composition t*s of two transformations is simply the transformation obtained by first doing transformation t followed by transformation s 52 Examples of composition R 1 * R2 = R 0 R 2 * R2 = R 1 F0 * R1 = F2 F2 * F1 = R1 R1 * F2 = F1 F2 * R1 = F0 53 R0 is an identity   R0 * R 0 = R 0 R1 * R 0 = R 1 R2 * R 0 = R 2 F0 * R 0 = F 0 F1 * R 0 = F 1 F2 * R 0 = F 2 So x * R0 = x for every x R0 is an identity element for * 54 Every element has an inverse  R0 * R 0 = R 0 R1 * R 2 = R 0 R2 * R 1 = R 0 F0 * F 0 = R 0 F1 * F 1 = R 0 F2 * F 2 = R 0 Every element x has an inverse element x' x * x' = R0 55 Composition is associative • How to determine F2 * R2 * F0 ? (F2 * R2) * F0 = (F0) * F0 = R0 F2 * (R2 * F0) = F2 * (F2) = R0 • For any three elements x, y, z of H we have (x * y) * z = x * (y * z)‫‏‬ • So composition is associative (but we haven't proved this)‫‏‬ 56 We've been here before    We have a set of six elements S3 = { R0, R1, R2, F0, F1, F2 } We have an operation * defined on the set which takes any two elements of the set and combines them to produce another element of the set The operation is associative, so for any three elements x, y, and z we have (x * y) * z = x * (y * z)‫‏‬ 57 S3 is a group • An identity element R0 exists so that for any element x we have x * R0 = x • Every element x has an inverse element x' so that x * x' = R0 • In short, S3 is a group! 58 Composition table for S3 R0 R1 R2 F0 F1 F2 R1 R2 R0 F2 F0 F1 R 2 F0 R 0 F1 R 1 F2 F1 R0 F2 R1 F0 R2 F1 F2 F0 R2 R0 R1 F2 F0 F1 R1 R2 R0 59 Something a bit odd You may have noticed something of interest in earlier examples of composition: R1 * F2 = F0 F2 * R1 = F1 What is a bit strange about this? What is going on here? 60 S3 is noncommutative • Many operations in mathematics are commutative, i.e., the order of the operands does not matter: 15 + 6 = 6 + 15 = 21 15 * 6 = 6 * 15 = 90 • But composition on S3 is noncommutative: R1 * F2 ≠‫ ‏‬F2 * R1  Be careful not to assume commutativity 61 S3 is nonabelian • When a group is commutative, it is said to be abelian • When a group is noncommutative, it is said to be nonabelian • The term abelian is derived from the name of the Norwegian mathematician, Niels Henrik Abel (1802-1829), who did significant mathematics before dying at a young age of tuberculosis 62 S3 is nonabelian 63 Is S3 really a new group? • We have now seen three groups of order six: 6, H, and S3 • We saw that 6 and H are isomorphic, so they are essentially the same • What about S3? • Is S3 isomorphic to 6 or H? • If S3 is isomorphic to one of 6 or H, it's isomorphic to the other also 64 Let's try a correspondence If S3 is isomorphic to 6 then there is a correspondence like this one 0 ↔ R0 1 ↔ R1 2 ↔ R2 3 ↔ F0 4 ↔ F1 5 ↔ F2 65 It doesn't work out like we want • All true statements in one group map to true statements in the other group • We know 1 + 2 = 3 in 6 • The corresponding statement in S3 is R1 + R 2 = F0 • But this is false because R1 + R2 = R 0 And R0 corresponds to 0 in 6, not to 3 66 Are we done yet? • Does this mean S3 is not isomorphic to 6? • No, not at all; we tried one correspondence between elements of S3 and 6 and it didn't work out, but there are a bunch more possible correspondences to try • How many? • There are 6! = 6∙5∙4∙3∙2∙1 = 720 ways to order six things, so we only have 719 to go 67 Flips are their own inverses    Let's not use main strength and awkwardness Elements of S3 which are their own inverses: R0 * R 0 = R 0 F0 * F 0 = R 0 F1 * F 1 = R 0 F2 * F 2 = R 0 Elements of 6 which are their own inverses: 0+0=0 68 S3  6 • So S3 has four elements which are their own inverses, and 6 has only one such element • Thus there is no possible correspondence between elements of the two groups which will preserve true statements • S3 and 6 are NOT isomorphic 69 What about multiplication? • A group has only one operation, usually called addition (but, as we have seen, it is not necessarily the usual addition we are used to)‫‏‬ • In many cases of interest there are two operations: addition and multiplication • Let's return to our favorite structure, 6, and see if we can define multiplication for it 70 Multiplication made easy • Denote multiplication by * • For x and y in , we define x * y like this: • Multiply x times y in the usual way as if they were ordinary whole numbers to get a product z If z < 6, then z is the product If z ≥ 6, then subtract 6 from z repeatedly until a number < 6 results; it is the product 71 Examples of multiplication 2*2=4 2 * 3 = 6, so 6 - 6 = 0 is the product 3 * 3 = 9, so 9 - 6 = 3 is the product 3 * 4 = 12, so 12 - 6 - 6 = 0 is the product 1*5=5 4 * 4 = 16, so 16 - 6 - 6 = 4 is the product 5 * 5 = 25, so 25 - 6 - 6 - 6 - 6 = 1 is the product 72 Multiplication table for 6 *| 0| 1| 2| 3| 4| 5| 0 0 0 0 0 0 0 1 0 1 2 3 4 5 2 0 2 4 0 2 4 3 0 3 0 3 0 3 4 0 4 2 0 4 2 5 0 5 4 3 2 1 73 1 is a unity   0*1=0 1*1=1 2*1=2 3*1=3 4*1=4 5*1=5 So x * 1 = x for every x We say 1 is a unity for * 74 Keep on associating • How to compute 5 * 4 * 2 ? (5 * 4) * 2 = (2) * 2 = 4 5 * (4 * 2) = 5 * (2) = 4 • For any three elements x, y, z of 6 we have (x * y) * z = x * (y * z)‫‏‬ • Another way to say this: Multiplication is associative (we have not proved this)‫‏‬ 75 Better commute too • How do 5 * 4 and 4 * 5 relate? 5*4=2 4*5=2 • For any two elements x, y of 6 we have x*y=y*x • Another way to say this: Multiplication is commutative (we have not proved this)‫‏‬ 76 The distributive law • How to compute 5 * (4 + 3) ? 5 * (4 + 3) = (5 * 4) + (5 * 3)‫‏‬ =2+3=5 • For any three elements x, y, z of 6 we have x * (y + z) = (x * y) + (x * z)‫‏‬ • Another way to say this: Multiplication distributes over addition (we have not proved this)‫‏‬ 77 Definition of a ring • A ring <R,+,*> is a non-empty set R together with two operations + and *, called addition and multiplication, such that • <R,+> is an abelian group • Multiplication is associative • Multiplication distributes over addition • A commutative ring with unity is a ring in which multiplication is commutative and there exists a unity element 78 <6,+,*> is a commutative ring with unity • We have seen that <6,+> is an abelian group (remember abelian means + is commutative)‫‏‬ • * is associative • * distributes over + • * is commutative • 1 is a unity • <6,+,*> is a commutative ring with unity 79 Examples of Rings • Let n = { 0, 1, 2, ..., n-1 } for any integer n > 0, and define + and * in analogy with + and * for 6 • <n,+,*> is a commutative ring with unity • So there are an infinite number of such rings • There are also infinite rings, e.g.,  with the usual addition and multiplication 80 Matrices • Let Mn be the set of all nn matrices of real numbers • Here are some typical elements of M2: 1 4 3 2‫‏‬ 0 2 2 1  We can define addition and multiplication of these elements in such a way that M2 is a ring with unity 81 More about matrices • The identity element for addition is 0 0‫‏‬ 0 0  The unity element for multiplication is 1 0 0 1  Matrix multiplication is NOT commutative 82 What about division? • In a group we can add (not necessarily the kind of addition we use in daily life)‫‏‬ • We can also subtract, because every element has an inverse, and adding the inverse of x is the same as subtracting x • In a ring, we can multiply • But can we divide? 83 Be careful! • Looking back at the multiplication table for 6, we see that 2*3=0 • Let's divide both sides by 3 to get 2=0 • NO! NO! NO! NO! NO! NO! NO! NO! • What's the problem? 84 A ring divided ... • In order to divide by an element x, we must have an element x' such that x * x' = 1, i.e., we need a multiplicative inverse for x • Then, to divide by x, we simply multiply by the multiplicative inverse x' • Looking back at the multiplication table for 6, we see that only 1 and 5 have multiplicative inverses • So division in 6 is just not going to work 85 If 6 doesn't work, try 5 • Let's look at <5,+,*>, a commutative ring with unity consisting of 5 elements 0, 1, 2, 3, and 4 • We can add and multiply in this ring much like we did in 6: When a value is greater than or equal to 5, we subtract 5 repeatedly until we get 0, 1, 2, 3, or 4 • Let's look at the multiplication table for 5 86 Multiplication table for 5 *| 0 1 2 3 4 0| 0 0 0 0 0 1| 0 1 2 3 4 2| 0 2 4 1 3 3| 0 3 1 4 2 4| 0 4 3 2 1 How is this different from the multiplication table for 6? 87 Unity at last • In every row of the table except the first, we see that the multiplicative unity 1 appears • This means that every element except 0 has a multiplicative inverse: 1 * 1 = 1 2 * 3 = 1 3 * 2 = 1 4 * 4 = 1 88 We can divide in 5 To divide by 1: To divide by 2: To divide by 3: To divide by 4: Multiply by 1 Multiply by 3 Multiply by 2 Multiply by 4 To divide by 0: Multiply by ??? 89 • Don't divide by zero • Don't ever, ever, ever divide by zero • Division is only defined when the divisor is non-zero • So dividing by zero is meaningless • [But can't we define x/0 =  ?] • [Yes, but that opens up another can of worms which we are not dealing with today] 90 What's your field? • Given a non-zero element x of a ring, if there exists an element x-1 such that x * x-1 = 1, then x-1 is the multiplicative inverse of x • A field is a commutative ring with unity in which every non-zero element has a multiplicative inverse • In a field we can not only add, subtract, and multiply, we can also divide 91 What's your field? 92 What have we seen? • Groups – structures in which we can add and subtract • Rings – structures in which we can add, subtract, and multiply • Fields – structures in which we can add, subtract, multiply, and divide • Some are finite, some are infinite 93 END Boolean Algebra Boolean Function Boolean algebra provides the operations and the rules for working with the set {0, 1}. Electronic and optical switches can be studied using this set and the rules of Boolean algebra. The three operations in Boolean algebra that we will use most are complementation, the Boolean sum, and the Boolean product. The complement of an element, denoted with a bar, is defined by 0 = 1 and 1 = 0. The Boolean sum, denoted by + or by OR, has the following values: 1 + 1 = 1, 1 + 0 = 1, 0 + 1 = 1, 0 + 0 = 0. The Boolean product, denoted by · or by AND, has the following values: 1 · 1 = 1, 1 · 0 = 0, 0 · 1 = 0, 0 · 0 = 0. When there is no danger of confusion, the symbol · can be deleted, just as in writing algebraic products. Unless parentheses are used, the rules of precedence for Boolean operators are: first, all complements are computed, followed by all Boolean products, followed by all Boolean sums. This is illustrated in Example 1. EXAMPLE 1 Find the value of 1 · 0 + (0 + 1). Solution: Using the definitions of complementation, the Boolean sum, and the Boolean product, it follows that 1 · 0 + (0 + 1) = 0 + 1 =0+0 =0 Boolean Function The complement, Boolean sum, and Boolean product correspond to the logical operators,¬, ∨, and ∧, respectively, where 0 corresponds to F (false) and 1 corresponds to T (true). Equalities in Boolean algebra can be directly translated into equivalences of compound propositions. Conversely, equivalences of compound propositions can be translated into equalities in Boolean algebra. We will see later in this section why these translations yield valid logical equivalences and identities in Boolean algebra. Example 2 illustrates the translation from Boolean algebra to propositional logic. EXAMPLE 2 Translate 1 · 0 + (0 + 1) = 0, the equality found in Example 1, into a logical equivalence. Solution: We obtain a logical equivalence when we translate each 1 into a T, each 0 into an F, each Boolean sum into a disjunction, each Boolean product into a conjunction, and each complementation into a negation. We obtain (T ∧ F) ∨ ¬(T ∨ F) ≡ F. Example 3 illustrates the translation from propositional logic to Boolean algebra. EXAMPLE 3 Translate the logical equivalence (T ∧ T) ∨ ¬F ≡ T into an identity in Boolean algebra. Solution: We obtain an identity in Boolean algebra when we translate each T into a 1, each F into a 0, each disjunction into a Boolean sum, each conjunction into a Boolean product, and each negation into a complementation. We obtain (1 · 1) + 0 = 1. Boolean Expressions and Boolean Functions Let B = {0, 1}. Then Bn = {(x1, x2, . . . , xn) | xi ∈ B for 1 ≤ i ≤ n} is the set of all possible n-tuples of 0s and 1s. The variable x is called a Boolean variable if it assumes values only from B, that is, if its only possible values are 0 and 1. A function from Bn to B is called a Boolean function of degree n. EXAMPLE 4 The function F (x, y) = xy from the set of ordered pairs of Boolean variables to the set {0, 1} is a Boolean function of degree 2 with F(1, 1) = 0, F(1, 0) = 1, F(0, 1) = 0, and F(0, 0) = 0. We display these values of F in Table 1. TABLE 1 Boolean functions can be represented using expressions made upfrom variables and Boolean operations. The Boolean expressions in the variables x1, x2, . . . , xn are defined recursively as 0, 1, x1, x2, . . . , xn are Boolean expressions; if E1 and E2 are Boolean expressions, then E1, (E1E2), and (E1 + E2) are Boolean expressions. Each Boolean expression represents a Boolean function. The values of this function are obtained by substituting 0 and 1 for the variables in the expression. In Section 12.2 we will show that every Boolean function can be represented by a Boolean expression. x y F(x, y) 1 1 0 0 1 0 1 0 0 1 0 0 Boolean Expressions and Boolean Functions EXAMPLE 5 Find the values of the Boolean function represented by F (x, y, z) = xy + z. Solution: The values of this function are displayed in Table 2.  Note that we can represent a Boolean function graphically by distinguishing the vertices of the n-cube that correspond to the n-tuples of bits where the function has value 1. TABLE 2 x y z xy z F (x, y, z) = xy + z 1 1 1 1 0 0 0 0 1 1 0 0 1 1 0 0 1 0 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 Identities of Boolean Algebra There are many identities in Boolean algebra. The most important of these are displayed in Table 5. These identities are particularly useful in simplifying the design of circuits. Each of the identities in Table 5 can be proved using a table. We will prove one of the distributive laws in this way in Example 8. The proofs of the remaining properties are left as exercises for the reader Topics • • • • • • 4.1 Divisibility and Modular Arithmetic 4.2 Integer Representations and Algorithms 4.3 Primes and Greatest Common Divisors 4.4 Solving Congruences 4.5 Applications of Congruences 4.6 Cryptography • Note: From Discrete Mathematics and Its Applications – Kenneth H. Rosen (Seventh Edition) Divisibility and Modular Arithmetic • Division of an integer by a positive integer produces a quotient and a remainder. • 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, or equivalently, if b/a is an integer. When a divides b we say that a is a factor or divisor of b, and that b is a multiple of a. The notation a/b denotes that a divides b. We write a b when a does not divide b. • EXAMPLE: Determine whether 3 | 7 and whether 3 | 12. • Solution: We see that 3 7, because 7/3 is not an integer. On the other hand, 3 | 12 because12/3 = 4. • THEOREM 1: – Let a,b, and c be integers, where a = 0. Then (i ) if a | b and a | c, then a | (b + c); (ii ) if a | b, then a | bc for all integers c; (iii ) if a | b and b | c, then a | c. • THEOREM 2: – THE DIVISION ALGORITHM: Let a be an integer and d a positive integer. Then there are unique integers q and r, with 0 ≤ r<d, such that a = dq + r. – In the equality given in the division algorithm, d is called the divisor, a is called the dividend, q is called the quotient, and r is called the remainder. This notation is used to express the quotient and remainder: q = a div d, r = a mod d. • EXAMPLE: What are the quotient and remainder when 101 is divided by 11? • Solution: We have 101 = 11 · 9 + 2. Hence, the quotient when 101 is divided by 11 is 9 = 101 div 11, and the remainder is 2 = 101 mod 11. • EXAMPLE: What are the quotient and remainder when −11 is divided by 3? • Solution: We have −11 = 3(−4) + 1.Hence, the quotient when −11 is divided by 3 is −4 =−11 div 3, and the remainder is 1 =−11mod 3. • Note that the remainder cannot be negative. Consequently, the remainder is not −2, even though −11 = 3(−3) − 2, because r =−2 does not satisfy 0 ≤ r<3. • Note that the integer a is divisible by the integer d if and only if the remainder is zero when a is divided by d. Modular Arithmetic • If a and b are integers and m is a positive integer, then a is congruent to b modulo m if m divides a − b. We use the notation a ≡ b (mod m) to indicate that a is congruent to b modulo m. We say that a ≡ b (mod m) is a congruence and that m is its modulus (plural moduli). If a and b are not congruent modulo m, we write a ≡ b (mod m). • Although both notations a ≡ b (mod m) and a mod m = b include “mod,” they represent fundamentally different concepts. The first represents a relation on the set of integers, whereas the second represents a function. THEOREM 3 • 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. • EXAMPLE Determine whether 17 is congruent to 5modulo6 and whether 24 and 14 are congruent modulo 6. • Solution: Because 6 divides 17 − 5 = 12, we see that 17 ≡ 5 (mod 6). However, because 24 − 14 = 10 is not divisible by 6, we see that 24 ≡ 14 (mod 6). THEOREM 4 • Let m be a positive integer. The integers a and b are congruent modulo m if and only if there is an integer k such that a = b + km. Proof: If a ≡ b(mod m), by the definition of congruence, we know that m | (a − b). This means that there is an integer k such that a − b = km, so that a = b + km. Conversely, if there is an integer k such that a = b + km, then km = a − b. Hence, m divides a − b, so that a ≡ b(mod m).  The set of all integers congruent to an integer a modulo m is called the congruence class of a modulo m. THEOREM 5 • Let m be a positive integer. If a ≡ b(mod m) and c ≡ d(mod m), then a + c ≡ b + d(mod m) and ac ≡ bd (mod m). • Proof:We use a direct proof. Because a ≡ b(mod m) and c ≡ d(mod m), by Theorem 4 there are integers s and t with b = a + sm and d = c + tm. Hence, b + d = (a + sm) + (c + tm) = (a + c) + m(s + t) and bd = (a + sm)(c + tm) = ac + m(at + cs + stm). Hence, a + c ≡ b + d(mod m) and ac ≡ bd (mod m). EXAMPLE • Because 7 ≡ 2 (mod 5) and 11 ≡ 1 (mod 5), it follows from Theorem 5 that 18 = 7 + 11 ≡ 2 + 1 = 3 (mod 5)and that 77 = 7 · 11 ≡ 2 · 1 = 2 (mod 5). Arithmetic Modulo m • We can define arithmetic operations on Zm, the set of nonnegative integers less than m, that is, the set {0, 1,...,m − 1}. In particular, we define addition of these integers, denoted by +m by a +m b = (a + b) mod m, where the addition on the right-hand side of this equation is the ordinary addition of integers, and we define multiplication of these integers, denoted by ·m by a ·m b = (a · b) mod m, where the multiplication on the right-hand side of this equation is the ordinary multiplication of integers. The operations +m and ·m are called addition and multiplication modulo m and when we use these operations, we are said to be doing arithmetic modulo m. EXAMPLE • Use the definition of addition and multiplication in Zm to find 7 +119 and 7 · 11 9. • Solution: Using the definition of addition modulo 11, we find that 7 +119 = (7 + 9) mod 11 = 16 mod 11 = 5, and 7 · 11 9= (7 · 9)mod 11 = 63 mod 11 = 8. Hence 7 +119 = 5 and 7 · 11 9= 8. Properties of the operations +m and ·m • Closure If a and b belong to Zm , then a +m b and a · m b belong to Zm . • Associativity If a, b, and c belong to Zm , then (a +m b) +m c = a +m (b +m c) and (a · m b) · m c = a · m (b · m c) • Commutativity If a and b belong to Zm , then a +m b = b +m a and a · m b = b · m a. • Identity elements The elements 0 and 1 are identity elements for addition and multiplication modulo m, respectively. That is, if a belongs to Zm , then a +m 0 = 0 +m a = a and a · m 1 =1 · m a = a. • Additive inverses If a = 0 belongs to Zm , then m − a is an additive inverse of a modulo m and 0 is its own additive inverse. That is a +m (m − a) = 0 and 0 +m 0 = 0. • Distributivity If a, b, and c belong to Zm , then a · m (b +m c) = (a · m b) +m (a · m c) and (a +m b) · m c = (a · m c) +m (b · m c). 4.2 Integer Representations and Algorithms Representations of Integers • THEOREM 1 Let b be an integer greater than 1. Then if n is a positive integer, it can be expressed uniquely in the form n = akbk+ ak−1 bk−1+···+ a1b + a0, where k is a nonnegative integer, a0, a1,..., ak are nonnegative integers less than b, and ak ≠ 0. BINARY EXPANSIONS • What is the decimal expansion of the integer that has (1 0101 1111)2 as its binary expansion? • Solution:We have (1 0101 1111)2 = 1 · 28 + 0 · 27 + 1 · 26 + 0 · 25 + 1 · 24 + 1 · 23 + 1 · 22 + 1 · 21 + 1 · 20 = 351. OCTAL EXPANSIONS • What is the decimal expansion of the number with octal expansion (7016)8 ? • Solution: Using the definition of a base b expansion with b = 8 tells us that (7016)8 = 7 · 83 + 0 · 82 + 1 · 8 + 6 = 3598. HEXADECIMAL EXPANSIONS • Sixteen different digits are required for hexadecimal expansions. Usually, the hexadecimal digits used are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F, where the letters A through F represent the digits corresponding to the numbers 10 through 15 (in decimal notation). • What is the decimal expansion of the number with hexadecimal expansion (2AE0B)16 ? • Solution: Using the definition of a base b expansion with b = 16 tells us that (2AE0B)16 = 2 · 164 + 10 · 163 + 14 · 162 + 0 · 16 + 11 = 175627. • Each hexadecimal digit can be represented using four bits. For instance, we see that (1110 0101)2 = (E5)16 because (1110)2 = (E)16 and (0101)2 = (5)16. Bytes, which are bit strings of length eight, can be represented by two hexadecimal digits. BASE CONVERSION • We will now describe an algorithm for constructing the base b expansion of an integer n. First, divide n by b to obtain a quotient and remainder, that is, n = bq0 + a0, 0 ≤ a0 <b. • The remainder, a0, is the rightmost digit in the base b expansion of n. Next, divide q0 by b to obtain q0 = bq1 + a1, 0 ≤ a1 <b. Decimal to octal • Find the octal expansion of (12345)10 • Solution: First, divide 12345 by 8 to obtain 12345 = 8 · 1543 + 1. • Successively dividing quotients by 8 gives 1543 = 8 · 192 + 7, 192 = 8 · 24 + 0, 24 = 8 · 3 + 0, 3 = 8 · 0 + 3. • The successive remainders that we have found, 1, 7, 0, 0, and 3, are the digits from the right to the left of 12345 in base 8. Hence, (12345)10=(30071)8 Decimal to Hexadecimal • Find the hexadecimal expansion of (177130)10. • Solution: First divide 177130 by 16 to obtain 177130 = 16 · 11070 + 10. • Successively dividing quotients by 16 gives 11070 = 16 · 691 + 14, 691 = 16 · 43 + 3, 43 = 16 · 2 + 11, 2 = 16 · 0 + 2. • The successive remainders that we have found, 10, 14, 3, 11, 2, give us the digits from the right to the left of 177130 in the hexadecimal (base 16) expansion of (177130)10. It follows that (177130)10= (2B3EA)16 • (Recall that the integers 10, 11, and 14 correspond to the hexadecimal digits A, B, and E, respectively.) Decimal to Binary • Find the binary expansion of (241)10 • Solution: First divide 241 by 2 to obtain 241 = 2 · 120 + 1. • Successively dividing quotients by 2 gives 120 = 2 · 60 + 0, 60 = 2 · 30 + 0, 30 = 2 · 15 + 0, 15 = 2 · 7 + 1, 7 = 2 · 3 + 1, 3 = 2 · 1 + 1, 1 = 2 · 0 + 1. • The successive remainders that we have found, 1, 0, 0, 0, 1, 1, 1, 1, are the digits from the right to the left in the binary (base 2) expansion of (241)10. Hence, (241)10= (1111 0001)2 ALGORITHM 1 Constructing Base b Expansions • procedure base b expansion(n, b: positive integers with b> 1) q := n k := 0 while q ≠ 0 ak := q mod b q := q div b k := k + 1 return (ak-1,..., a1, a0) {(ak-1,..., a1, a0) is the base b expansion of n} Find yourself! • Find the octal and hexadecimal expansions of (11 1110 1011 1100)2 and the binary expansions of (765)8 and (A8D)16 ADDITION ALGORITHM • procedure add(a, b: positive integers) {the binary expansions of a and b are (an−1an−2 ...a1a0)2 and (bn−1bn−2 ...b1b0)2, respectively} c := 0 for j := 0 to n − 1 d := └ (aj + bj + c)/2 ┘ // └ ┘represent lower bound sj := aj + bj + c − 2d c := d sn := c return (s0,s1,...,sn) {the binary expansion of the sum is (snsn−1 ...s0)2} Addition of Integers • Add a = (1110)2 and b = (1011)2 • Solution: Following the procedure specified in the algorithm, first note that a0 + b0 = 0 + 1 = 0 · 2 + 1, so that c0 = 0 and s0 = 1. Then, because a1 + b1 + c0 = 1 + 1 + 0 = 1 · 2 + 0, it follows that c1 = 1 and s1 = 0. Continuing, a2 + b2 + c1 = 1 + 0 + 1 = 1 · 2 + 0, so that c2 = 1 and s2 = 0. Finally, because a3 + b3 + c2 = 1 + 1 + 1 = 1 · 2 + 1, Follows that c3 = 1 and s3 = 1.Thismeans that s4 = c3 = 1. Therefore, s = a + b = (1 1001)2 Multiplication Algorithm • procedure multiply(a, b: positive integers) {the binary expansions of a and b are (an−1an−2 ...a1a0)2 and (bn−1bn−2 ...b1b0)2, respectively} for j := 0 to n − 1 if bj = 1 then cj := a shifted j places else cj := 0 {c0,c1,...,cn−1 are the partial products} p := 0 for j := 0 to n − 1 p := p + cj return p {p is the value of ab} Example • Find the product of a = (110)2 and b = (101)2. • Solution: First note that ab0 · 20 = (110)2 · 1 · 20 = (110)2, ab1 · 21 = (110)2 · 0 · 21 = (0000)2, and ab2 · 22= (110)2 · 1 · 22 = (11000)2. To find the product, add (110)2, (0000)2, and (11000)2. Carrying out these additions (using adition Algorithm, including initial zero bits when necessary) shows that ab = (1 1110)2 Algorithm for DIV and MOD • procedure division algorithm(a: integer, d: positive integer) q := 0 r := |a| while r ≥ d r := r − d q := q + 1 if a< 0 and r> 0 then r := d − r q := −(q + 1) return (q, r) {q = a div d is the quotient, r = a mod d is the remainder} 4.3 Primes and Greatest Common Divisors(GCD) Primes • Every integer greater than 1 is divisible by at least two integers, because a positive integer is divisible by 1 and by itself. Positive integers that have exactly two different positive integer factors are called primes. • An integer p greater than 1 is called prime if the only positive factors of pare 1 and p. • A positive integer that is greater than 1 and is not prime is called composite. • The integer 7 is prime because its only positive factors are 1 and 7, whereas the integer 9 is composite because it is divisible by 3. Primes • Largest prime found in 2018 is : 2 74,207,281-1 in short form M74207281 Primes • 1 could be divided by 1 and itself. However, it does not satisfy the unique factorization theorem. 1 is an empty product. A prime number must have exactly 2 factors which are unique. But, 1 has only 1 factor which is 1. As per, Fundamental Theorem of Arithmetic or Unique Factorization Theorem, 'Every positive whole number can be written as a unique product of primes.' Euclid's elements book shows 'Any composite number is measured by some prime number.‘ • Let's consider 1 as prime. Then we can write say,15 as 5x3, or 5x3x1, or 5x3x1x1,... Therefore, if 1 is prime, it does not allow for any composite positive integer to be written as a unique product of primes. So, if the Unique Factorization Theorem has to be valid, 1 shouldn't be a prime number. So, 1 is neither a prime nor a composite number. THEOREM 1 THE FUNDAMENTAL THEOREM OF ARITHMETIC • Every integer greater than 1 can be written uniquely as a prime or as the product of two or more primes where the prime factors are written in order of non decreasing size. • The prime factorizations of 100, 641, 999, and 1024 are given by • • • • 100=2·2·5·5=22.52 641=641 999=3·3·3·37=33.37, 1024=2·2·2·2·2·2·2·2·2·2=210 THEOREM 2 • If n is a composite integer, then n has a prime divisor less than or equal to √n. • Proof: If n is composite, by the definition of a composite integer, we know that it has a factor a with 1<a<n. Hence, by the definition of a factor of a positive integer, we have n=a.b, Where b is a positive integer greater than 1. We will show that a≤√n or b≤√n. If a>√n and b>√n, then a.b >√n·√n=n, which is a contradiction. Consequently, a≤√n or b≤√n. Because both a and b are divisors of n, we see that n has a positive divisor not exceeding √n. This divisor is either prime or, by the fundamental theorem of arithmetic, has a prime divisor less than itself. In either case, n has a prime divisor less than or equal to √n. Show that 101 is prime. • Solution: The only primes not exceeding √101 are 2, 3, 5, and 7. Because 101 is not divisible by 2, 3, 5, or 7 (the quotient of 101 and each of these integers is not an integer), it follows that 101 is prime. • Because every integer has a prime factorization, it would be useful to have a procedure for finding this prime factorization. Consider the problem of finding the prime factorization of n. Begin by dividing n by successive primes, starting with the smallest prime, 2. If n has a prime factor, then by Theorem 3 a prime factor p not exceeding √n will be found. So, if no prime. factor not exceeding √n is found, then n is prime. Otherwise, if a prime factor p is found, continue by factoring n/p. Note that n/p has no prime factors less than p. Again, if n/p has no prime factor greater than or equal to p and not exceeding its square root, then it is prime. Otherwise, if it has a prime factor q, continue by factoring n/(pq). This procedure is continued until the factorization has been reduced to a prime. Find the prime factorization of 7007. • Solution: To find the prime factorization of 7007, first perform divisions of 7007 by successive primes, beginning with 2. None of the primes 2, 3, and 5 divides 7007. However, 7 divides 7007, with 7007/7=1001. Next, divide 1001 by successive primes, beginning with 7. It is immediately seen that 7 also divides 1001, because 1001/7=143. Continue by dividing 143 by successive primes, beginning with 7. Although 7 does not divide 143, 11 does divide 143, and 143/11=13.Because 13 is prime, the procedure is completed. It follows that 7007=7·1001=7·7·143=7·7·11·13. Consequently, the prime factorization of 7007 is 7·7·11·13=72·11·13 Twin primes • Twin primes are pairs of primes that differ by 2, such as 3 and 5, 5 and 7, 11 and 13, 17 and 19, and 4967 and 4969. The twin prime conjecture asserts that there are infinitely many twin primes. The strongest result proved concerning twin primes is that there are infinitely many pairs p and p+2, where p is prime and p+2 is prime or the product of two primes. Greatest Common Divisors • The largest integer that divides both of two integers is called the greatest common divisor of these integers. • 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. The greatest common divisor of a and b is denoted by gcd(a, b). • The greatest common divisor of two integers, not both zero, exists because the set of common divisors of these integers is nonempty and finite. One way to find the greatest common divisor of two integers is to find all the positive common divisors of both integers and then take the largest divisor. What is the greatest common divisor of 24 and 36? • Solution: The positive common divisors of 24 and 36 are 1, 2, 3, 4, 6, and 12. Hence, gcd(24,36)=12. • What is the greatest common divisor of 17 and 22? • Solution: The integers 17 and 22 have no positive common divisors other than 1, so that gcd(17,22)=1. The integers a and b are relatively prime if their greatest common divisor is 1 • By previous Example it follows that the integers 17 and 22 are relatively prime, because gcd(17,22)=1. • The integers a1,a2,...,an are pairwise relatively prime if gcd(ai,aj)=1 whenever 1≤i<j≤n. • Determine whether the integers 10, 17, and 21 are pairwise relatively prime and whether the integers 10, 19, and 24 are pairwise relatively prime. • Solution: Because gcd(10,17)=1,gcd(10,21)=1, and gcd(17,21)=1, we conclude that 10, 17, and 21 are pairwise relatively prime. Because gcd(10,24)=2>1, we see that 10, 19, and 24 are not pairwise relatively prime. Least Common Multiple(LCM) • The least common multiple of the positive integers a and b is the smallest positive integer that is divisible by both a and b. The least common multiple of a and b is denoted by lcm(a, b). • lcm(a, b)=p1 max(a1,b1)P2max(a2,b2)···pnmax(an, bn) • What is the least common multiple of 233572and 2 43 3? Solution: We have lcm(233572,2433)=2max(3,4)3max(5,3)7max(2,0)=243572 THEOREM 5 • Let a and b be positive integers. Then ab=gcd(a, b)·lcm(a, b). 15*5=75 gcd(15,5)=5 lcm(15,5)=15 So , 5*15=gcd(15,5)*lcm(15,5) The Euclidean Algorithm • The Euclidean algorithm, we will show how it is used to find gcd(91,287). First, divide 287, the larger of the two integers, by 91, the smaller, to obtain 287=91·3+14. Any divisor of 91 and 287 must also be a divisor of 287−91·3=14. Also, any divisor of 91 and 14 must also be a divisor of 287=91·3+14. Hence, the greatest common divisor of 91 and 287 is the same as the greatest common divisor of 91 and 14. This means that the problem of finding gcd(91,287)has been reduced to the problem of finding gcd(91,14).Next, divide 91 by 14 to obtain 91=14·6+7. Because any common divisor of 91 and 14 also divides 91−14·6=7 and any common divisor of 14 and 7 divides 91, it follows that gcd(91,14)=gcd(14,7). Continue by dividing 14 by 7, to obtain14=7·2. Because 7 divides 14, it follows that gcd(14, 7)=7. Furthermore, because gcd(287, 91)=gcd(91, 14)=gcd(14, 7)=7, the original problem has been solved. Discrete Probability 1/17/2019 DISCRETE PROBABILITY 1 Introduction Combinatory and probability theory share common origins. The theory of probability was first developed more than 300 years ago, when certain gambling games were analyzed. Although probability theory was originally invented to study gambling, it now plays an essential role in a wide variety of disciplines. Probability: How likely something is to happen. When a coin is tossed, there are two possible outcomes: heads (H) or tails (T) We say that the probability of the coin landing H is ½ And the probability of the coin landing T is ½ 1/17/2019 DISCRETE PROBABILITY 2 Applications Probability theory is extensively applied in the study of genetics. Probability still remains an extremely popular part of mathematics because of its applicability to gambling. In computer science, probability theory plays an important role in the study of the complexity of algorithms. Probabilistic algorithms can be used to solve many problems that cannot be easily or practically solved by deterministic algorithms. In combinatorics, probability theory can even be used to show that objects with certain properties exist. Probability theory can help us answer questions that involve uncertainty, such as determining whether we should reject an incoming mail message as spam based on the words that appear in the message. 1/17/2019 DISCRETE PROBABILITY 3 Some Basic words Experiment or Trial: an action where the result is uncertain. Example: Toss a fair coin. Sample Space: all the possible outcomes of an experiment. Example: Toss a fair coin may result head or tail. Sample space={H,T} Sample Point: just one of the possible outcomes. Sample point={H} Event: a single result of an experiment. Event={T} 1/17/2019 DISCRETE PROBABILITY 4 Finite Probability If S is a finite nonempty sample space of equally likely outcomes, and E is an event, that is, a subset of S, then the probability of E is p(E) =|E|/|S| According to Laplace’s definition, the probability of an event is between 0 and 1. To see this, note that if E is an event from a finite sample space S, then 0 ≤ |E| ≤ |S|, because E ⊆ S. Thus, 0 ≤ p(E) = |E|/|S| ≤ 1. 1/17/2019 DISCRETE PROBABILITY 5 A box contains four blue balls and five red balls. What is the probability that a ball chosen at random from the box is blue? To calculate the probability, note that there are nine possible outcomes, and four of these possible outcomes produce a blue ball. Hence, the probability that a blue ball is chosen is 4/9. # of possible out comes produce blue ball |B|=4 p( )=_____________________________ # of Total possible outcomes |T|=4+5=9 So, Probability of choosing a blue ball , P(B)=|B|/|T|=4/9 1/17/2019 DISCRETE PROBABILITY 6 What is the probability getting a one when a die is rolled? There are a total of 6 equally likely possible outcomes when one die is rolled. Possible outcomes={1,2,3,4,5,6} # of total possible outcome=6 And # of possible out come of one is =1 Hence, the probability that a one comes up when a fair die is rolled is 1/6 . 1/17/2019 DISCRETE PROBABILITY 7 What is the probability that when two dice are rolled, the sum of the numbers on the two dice is 7? There are a total of 36 equally likely possible outcomes when two dice are rolled. (The product rule can be used to see this; because each die has six possible outcomes, the total number of outcomes when two dice are rolled is 6*2 = 36.) There are six successful outcomes, namely, (1, 6), (2, 5), (3, 4), (4, 3), (5, 2), and (6, 1), where the values of the first and second dice are represented by an ordered pair. Hence, the probability that a seven comes up when two fair dice are rolled is 6/36 = 1/6. 1/17/2019 DISCRETE PROBABILITY 8 Some Tips The probability of an event can only be between 0 and 1 and can also be written as a percentage. The probability of event A is often written as P(A). If P(A)>P(B), then event A has a higher chance of occurring than event B. If P(A)=P(B), then events A and B are equally likely to occur. 1/17/2019 DISCRETE PROBABILITY 9 Some exercises 1. What’s the probability of rolling a one or a six? 2. What’s the probability of rolling an even number (i.e., rolling a two, four or a six)? 3. Greg has an MP3 player called the Jumble. The Jumble randomly selects a song for the user to listen to. Greg's Jumble has 6 classical songs, 7 rock songs, and 9 rap songs on it. What is P(not rap song)? 4. You randomly draw a marble out of a bag that contains 20 total marbles. 12 of the marbles in the bag are blue. What is P(draw a blue marble)? 5. You roll a fair 6-sided die. What is P(roll a 2)? 6. Joel has an MP3 player called the Jumble. The Jumble randomly selects a song for the user to listen to. Joel's Jumble has 2 classical songs, 13 rock songs, and 5 rap songs on it. What is P(rock song or rap song)? 7. A die is thrown once. What is the probability that the score is a factor of 6? 1/17/2019 DISCRETE PROBABILITY 10 Some exercises 6. Each of the letters of the word MISSISSIPPI are written on separate pieces of paper that are then folded, put in a hat, and mixed thoroughly. One piece of paper is chosen (without looking) from the hat. What is the probability it is an ‘I’? 7. A card is chosen at random from a deck of 52 playing cards. There are 4 Queens and 4 Kings in a deck of playing cards. What is the probability the card chosen is a Queen or a King? 8. A fair coin is tossed three times. What is the probability of obtaining one Head and two Tails? (A fair coin is one that is not loaded, so there is an equal chance of it landing Heads up or Tails up.) 9. A committee of three is chosen from five councilors - Adams, Burke, Cobb, Dilby and Evans. What is the probability Burke is on the committee? 10. A special die is made in the shape of an octahedron: The die has 8 equal faces marked with the numbers 1 to 8. If the die is thrown once, what is the probability that the face that lands uppermost has a prime number? 1/17/2019 DISCRETE PROBABILITY 11 Probability: Complement Complement of an Event: All outcomes that are NOT the event. When the event is Heads, the complement is Tails. When the event is {Monday, Wednesday} the complement is {Tuesday, Thursday, Friday, Saturday, Sunday}. When the event is {Hearts} the complement is {Spades, Clubs, Diamonds, Jokers}. So the Complement of an event is all the other outcomes (not the ones we want). And together the Event and its Complement make all possible outcomes. 1/17/2019 DISCRETE PROBABILITY 12 Probability: Complement Example: Rolling a "5" or "6" Event A is {5, 6} Number of ways it can happen: 2 Total number of outcomes: 6 P(A) = 2/6 = 1/3 The Complement of Event A is {1, 2, 3, 4} Number of ways it can happen: 4 Total number of outcomes: 6 P(A') = 4 /6 = 2 /3 Let us add them: P(A) + P(A') = 1 /3 + 2/ 3 = 3 /3 = 1 Yep, that makes 1 It makes sense, right? Event A plus all outcomes that are not Event A make up all possible outcomes. 1/17/2019 DISCRETE PROBABILITY 13 Why is the Complement useful? Example: Throw two dice. What is the probability the two scores are different? Different scores are like getting a 2 and 3, or a 6 and 1. It is quite a long list: A = { (1,2), (1,3), (1,4), (1,5), (1,6),(2,1), (2,3), (2,4), ... etc ! } But the complement (which is when the two scores are the same) is only 6 outcomes: A' = { (1,1), (2,2), (3,3), (4,4), (5,5), (6,6) } And its probability is: P(A') = 6/36 = 1/6 Knowing that P(A) and P(A') together make 1, we can calculate: P(A) = 1 − P(A') = 1 − 1/6 = 5/6 So in this case (and many others) it's easier to work out P(A') first, then find P(A) 1/17/2019 DISCRETE PROBABILITY 14 Independent Events Independent Events are not affected by previous events. This is an important idea! A coin does not "know" it came up heads before. And each toss of a coin is a perfect isolated thing. Example: You toss a coin and it comes up "Heads" three times ... what is the chance that the next toss will also be a "Head"? The chance is simply ½ (or 0.5) just like ANY toss of the coin. What it did in the past will not affect the current toss! 1/17/2019 DISCRETE PROBABILITY 15 Independent Events Example: Probability of 3 Heads in a Row For each toss of a coin a "Head" has a probability of 0.5. And so the chance of getting 3 Heads in a row is 0.125 1/17/2019 DISCRETE PROBABILITY 16 So, for Independent Events: P(A and B) = P(A) × P(B) Probability of A and B equals the probability of A times the probability of B your boss (to be fair) randomly assigns everyone an extra 2 hours work on weekend evenings between 4 and midnight. What are the chances you get Saturday between 6 and 8? Day: there are two days on the weekend, so P(Saturday) = 0.5 Time: you want the 2 hours of 6-to-8, out of the 8 hours of 4-to-midnight): P(Your Time) = 2/8 = 0.25 And: P(Saturday and Your Time) = 0.5 × 0.25 = 0.125 = P(Saturday) × P(Your Time) Or a 12.5% Chance (Note: we could ALSO have worked out that you wanted 2 hours out of a total possible 16 hours, which is 2/16 = 0.125. Both methods work here.) 1/17/2019 DISCRETE PROBABILITY 17 Conditional Probability Events can also be "dependent" ... which means they can be affected by previous events ... 2 blue and 3 red marbles are in a bag. What are the chances of getting a blue marble? The chance is 2 in 5 But after taking one out the chances change! So the next time: probability marbles if we got a red marble before, then the chance of a blue marble next is 2 in 4 probability marbles if we got a blue marble before, then the chance of a blue marble next is 1 in 4 1/17/2019 DISCRETE PROBABILITY 18 Conditional Probability This is because we are removing marbles from the bag. So the next event depends on what happened in the previous event, and is called dependent. Replacement Note: if we replace the marbles in the bag each time, then the chances do not change and the events are independent: With Replacement: the events are Independent (the chances don't change) Without Replacement: the events are Dependent (the chances changes) 1/17/2019 DISCRETE PROBABILITY 19 Tree Diagram A Tree Diagram: is a wonderful way to picture what is going on, so let's build one for our marbles example. There is a 2/5 chance of pulling out a Blue marble, and a 3/5 chance for Red: We can go one step further and see what happens when we pick a second marble: 1/17/2019 DISCRETE PROBABILITY 20 Tree Diagram If a blue marble was selected first there is now a 1/4 chance of getting a blue marble and a 3/4 chance of getting a red marble. If a red marble was selected first there is now a 2/4 chance of getting a blue marble and a 2/4 chance of getting a red marble. Now we can answer questions like "What are the chances of drawing 2 blue marbles?" Answer: it is a 2/5 chance followed by a 1/4 chance: Did you see how we multiplied the chances? And got 1/10 as a result. The chances of drawing 2 blue marbles is 1/10 1/17/2019 DISCRETE PROBABILITY 21 Notation We love notation in mathematics! It means we can then use the power of algebra to play around with the ideas. So here is the notation for probability: P(A) means "Probability Of Event A" In our marbles example Event A is "get a Blue Marble first" with a probability of 2/5: P(A) = 2/5 And Event B is "get a Blue Marble second" ... but for that we have 2 choices: If we got a Blue Marble first the chance is now 1/4 If we got a Red Marble first the chance is now 2/4 So we have to say which one we want, and use the symbol "|" to mean "given": P(B|A) means "Event B given Event A" In other words, event A has already happened, now what is the chance of event B? P(B|A) is also called the "Conditional Probability" of B given A. And in our case: P(B|A) = ¼ So the probability of getting 2 blue marbles is: 1/17/2019 DISCRETE PROBABILITY 22 Notation And we write it as Probability of event A and event B equals the probability of event A times the probability of event B given event A" 1/17/2019 DISCRETE PROBABILITY 23 Example: Drawing 2 Kings from a Dec Event A is drawing a King first, and Event B is drawing a King second. For the first card the chance of drawing a King is 4 out of 52 (there are 4 Kings in a deck of 52 cards): P(A) = 4/52 But after removing a King from the deck the probability of the 2nd card drawn is lesslikely to be a King (only 3 of the 51 cards left are Kings): P(B|A) = 3/51 And so: P(A and B) = P(A) x P(B|A) = (4/52) x (3/51) = 12/2652 = 1/221 So the chance of getting 2 Kings is 1 in 221, or about 0.5% 1/17/2019 DISCRETE PROBABILITY 24 Finding Hidden Data P(B|A) = P(A and B) / P(A) "The probability of event B given event A equals the probability of event A and event B divided by the probability of event A Example: Ice Cream 70% of your friends like Chocolate, and 35% like Chocolate AND like Strawberry. What percent of those who like Chocolate also like Strawberry? P(Strawberry | Chocolate) = P(Chocolate and Strawberry) / P(Chocolate) 0.35 / 0.7 = 50% 50% of your friends who like Chocolate also like Strawberry 1/17/2019 DISCRETE PROBABILITY 25 Mutually Exclusive Events can't happen at the same time. Examples: Turning left and turning right are Mutually Exclusive (you can't do both at the same time) Tossing a coin: Heads and Tails are Mutually Exclusive Cards: Kings and Aces are Mutually Exclusive What is not Mutually Exclusive: Turning left and scratching your head can happen at the same time. Kings and Hearts, because we can have a King of Hearts! 1/17/2019 DISCRETE PROBABILITY 26 Example :Mutually Exclusive Events Example: there are 4 Kings in a deck of 52 cards. What is the probability of picking a King? Number of ways it can happen: 4 (there are 4 Kings) Total number of outcomes: 52 (there are 52 cards in total) So the probability = 4/52 = 1/13 1/17/2019 DISCRETE PROBABILITY 27 Mutually Exclusive Events When two events (call them "A" and "B") are Mutually Exclusive it is impossible for them to happen together: P(A and B) = 0 "The probability of A and B together equals 0 (impossible)" Example: King AND Queen A card cannot be a King AND a Queen at the same time! The probability of a King and a Queen is 0 (Impossible) But the probability of A or B is the sum of the individual probabilities: P(A or B) = P(A) + P(B) "The probability of A or B equals the probability of A plus the probability of B" Example: King OR Queen In a Deck of 52 Cards: the probability of a King is 1/13, so P(King)=1/13 the probability of a Queen is also 1/13, so P(Queen)=1/13 When we combine those two Events: The probability of a King or a Queen is (1/13) + (1/13) = 2/13 Which is written like this: P(King or Queen) = (1/13) + (1/13) = 2/13 1/17/2019 DISCRETE PROBABILITY 28 Not Mutually Exclusive Now let's see what happens when events are not Mutually Exclusive. Example: Hearts and Kings Hearts and Kings together is only the King of Hearts: But Hearts or Kings is: all the Hearts (13 of them) all the Kings (4 of them) But that counts the King of Hearts twice! So we correct our answer, by subtracting the extra "and" part: 1/17/2019 DISCRETE PROBABILITY 29 Not Mutually Exclusive 16 Cards = 13 Hearts + 4 Kings − the 1 extra King of Hearts Count them to make sure this works! As a formula this is: P(A or B) = P(A) + P(B) − P(A and B) "The probability of A or B equals the probability of A plus the probability of B minus the probability of A and B“ Here is the same formula, but using ∪ and ∩: P(A ∪ B) = P(A) + P(B) − P(A ∩ B) 1/17/2019 DISCRETE PROBABILITY 30 Example: Not Mutually Exclusive 16 people study French, 21 study Spanish and there are 30 altogether. Work out the probabilities! This is definitely a case of not Mutually Exclusive (you can study French AND Spanish). Let's say b is how many study both languages: people studying French Only must be 16-b people studying Spanish Only must be 21-b And we get: And we know there are 30 people, so: (16−b) + b + (21−b) = 30 37 − b = 30 b=7 And we can put in the correct numbers: P(French or Spanish) = 30/30 = 1 P(French and Spanish) = 7/30 Lastly, let's check with our formula: P(A or B) = P(A) + P(B) − P(A and B) Put the values in: 30/30 = 16/30 + 21/30 − 7/30 Yes, it works! So we know all this now: P(French) = 16/30 P(Spanish) = 21/30 P(French Only) = 9/30 P(Spanish Only) = 14/30 1/17/2019 DISCRETE PROBABILITY 31 Summary Mutually Exclusive A and B together is impossible: P(A and B) = 0 A or B is the sum of A and B: P(A or B) = P(A) + P(B) Not Mutually Exclusive A or B is the sum of A and B minus A and B: P(A or B) = P(A) + P(B) − P(A and B) Symbols And: ∩ (the "IntersecZon" symbol) Or: ∪ (the "Union" symbol) 1/17/2019 DISCRETE PROBABILITY 32 Addition rule for probability (basic) 1. One hundred students were surveyed about their preference between dogs and cats. The following two-way table displays data for the sample of students who responded to the survey. 1/17/2019 Preference Male Female Total Prefers dogs 36 20 56 Prefers cats 10 26 36 No Preference 2 6 8 TOTAL 48 52 100 DISCRETE PROBABILITY 33 Preference Male Female Total Prefers dogs 36 20 56 Prefers cats 10 26 36 No Preference 2 6 8 TOTAL 48 52 100 1. Find the probability that a randomly selected student prefers dogs. 2. In this sample, are the events "prefers dogs" and "prefers cats" mutually exclusive? 3. Find the probability that a randomly selected student prefers dogs or prefers cats. 4. In this sample, are the events "prefers dogs" and "female" mutually exclusive? 5. Find the probability that a randomly selected student prefers dogs or is female. (i.e: P(prefers dogs or is female)=P(prefers dogs)+P(is female)−P(prefers dogs and is female) 1/17/2019 DISCRETE PROBABILITY 34 Modeling Computation 1 Modeling Computation Given a task: Can it be performed by a computer? We learned earlier the some tasks are unsolvable. For the tasks that can be performed by a computer, how can they be carried out? We learned earlier the concept of an algorithm. – A description of a computational procedure. How can we model the computer itself, and what it is doing when it carries out an algorithm? Models of Computation – we want to model the abstract process of computation itself. We’ll cover three types of structures used in modeling computation: Grammars • • Used to generate sentences of a language and to determine if a given sentence is in a language Formal languages, generated by grammars, provide models for programming languages (Java, C, etc) as well as natural language --important for constructing compilers Finite-state machines (FSM) • FSM are characterized by a set of states, an input alphabet, and transitions that assigns a next state to a pair of state and an input. We’ll study FSM with and without output. They are used in language recognition (equivalent to certain grammar)but also for other tasks such as controlling vending machines Turing Machine – they are an abstraction of a computer; used to compute number theoretic functions 3 Early Models of Computation Recursive Function Theory – Kleene, Church, Turing, Post, 1930’s (before computers!!) Turing Machines – Turing, 1940’s (defined: computable) RAM Machines – von Neumann, 1940’s (“real computer”) Cellular Automata – von Neumann, 1950’s (Wolfram 2005; physics of our world?) Finite-state machines, pushdown automata – various people, 1950’s VLSI models – 1970s ( integrated circuits made of thousands of transistors form a single chip) Parallel RAMs, etc. – 1980’s Computers as Transition Functions A computer (or really any physical system) can be modeled as having, at any given time, a specific state sS from some (finite or infinite) state space S. Also, at any time, the computer receives an input symbol iI and produces an output symbol oO. – Where I and O are sets of symbols. • Each “symbol” can encode an arbitrary amount of data. A computer can then be modeled as simply being a transition function T:S×I → S×O. – Given the old state, and the input, this tells us what the computer’s new state and its output will be a moment later. Every model of computing we’ll discuss can be viewed as just being some special case of this general picture. Language Recognition Problem Let a language L be any set of some arbitrary objects s which will be dubbed “sentences.” – “legal” or “grammatically correct” sentences of the language. Let the language recognition problem for L be: – Given a sentence s, is it a legal sentence of the language L? • That is, is sL? Surprisingly, this simple problem is as general as our very notion of computation itself! Hmm… Ex: addition ‘language’ “num1-num2-(num1+num2)” Outlines Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing Machines Languages & Grammars Phrase-Structure Grammars Types of Phrase-Structure Grammars Derivation Trees Backus-Naur Form Intro to Languages English grammar tells us if a given combination of words is a valid sentence. The syntax of a sentence concerns its form while the semantics concerns its meaning. e.g. the mouse wrote a poem From a syntax point of view this is a valid sentence. From a semantics point of view not so fast…perhaps in Disney land Natural languages (English, French, Portguese, etc) have very complex rules of syntax and not necessarily well-defined. 9 Formal Language Formal language – is specified by well-defined set of rules of syntax We describe the sentences of a formal language using a grammar. Two key questions: 1 - Is a combination of words a valid sentence in a formal language? 2 – How can we generate the valid sentences of a formal language? Formal languages provide models for both natural languages and programming languages. 10 Grammars A formal grammar G is any compact, precise mathematical definition of a language L. – As opposed to just a raw listing of all of the language’s legal sentences, or just examples of them. A grammar implies an algorithm that would generate all legal sentences of the language. – Often, it takes the form of a set of recursive definitions. A popular way to specify a grammar recursively is to specify it as a phrase-structure grammar. Grammars (Semi-formal) Example: A grammar that generates a subset of the English language sentence  noun _ phrase noun _ phrase  article predicate  verb predicate noun 12 article  a article  the noun  boy noun  dog verb  runs verb  sleeps 13 A derivation of “the boy sleeps”: sentence  noun _ phrase predicate  noun _ phrase verb  article verb  the noun noun verb  the boy verb  the boy sleeps 14 A derivation of “a dog runs”: sentence  noun _ phrase predicate  noun _ phrase verb  article noun verb  a noun verb  a dog verb  a dog runs 15 Language of the grammar: L = { “a boy runs”, “a boy sleeps”, “the boy runs”, “the boy sleeps”, “a dog runs”, “a dog sleeps”, “the dog runs”, “the dog sleeps” } 16 Notation noun  boy noun  dog Variable or Non-terminal Production rule Terminal Symbols of the vocabulary Symbols of the vocabulary 17 Basic Terminology ► A vocabulary/alphabet, V is a finite nonempty set of elements called symbols. • Example: V = {a, b, c, A, B, C, S} ► A word/sentence over V is a string of finite length of elements of V. • Example: Aba ► The empty/null string, λ is the string with no symbols. ► V* is the set of all words over V. • Example: V* = {Aba, BBa, bAA, cab …} ► A language over V is a subset of V*. • We can give some criteria for a word to be in a language. Phrase-Structure Grammars A phrase-structure grammar (abbr. PSG) G = (V,T,S,P) is a 4-tuple, in which: – V is a vocabulary (set of symbols) • The “template vocabulary” of the language. – T  V is a set of symbols called terminals • Actual symbols of the language. • Also, N :≡ V − T is a set of special “symbols” called nonterminals. (Representing concepts like “noun”) – SN is a special nonterminal, the start symbol. • in our example the start symbol was “sentence”. – P is a set of productions (to be defined). • Rules for substituting one sentence fragment for another • Every production rule must contain at least one nonterminal on its left side. Phrase-structure Grammar ► EXAMPLE:  Let G = (V, T, S, P),  where V = {a, b, A, B, S}  T = {a, b},  S is a start symbol  P = {S → ABa, A → BB, B → ab, A → Bb}. G is a Phrase-Structure Grammar. What sentences can be generated with this grammar? Derivation Definition Let G=(V,T,S,P) be a phrase-structure grammar. Let w0=lz0r (the concatenation of l, z0, and r) w1=lz1r be strings over V. If z0  z1 is a production of G we say that w1 is directly derivable from w0 and we write wo => w1. If w0, w1, …., wn are strings over V such that w0 =>w1,w1=>w2,…, wn-1 => wn, then we say that wn is derivable from w0, and write w0=>*wn. The sequence of steps used to obtain wn from wo is called a derivation. Language Let G(V,T,S,P) be a phrase-structure grammar. The language generated by G (or the language of G) denoted by L(G) , is the set of all strings of terminals that are derivable from the starting state S. L(G)= {w  T* | S =>*w} 24 Language L(G) ► EXAMPLE: Let G = (V, T, S, P), where V = {a, b, A, S}, T = {a, b}, S is a start symbol and P = {S → aA, S → b, A → aa}. The language of this grammar is given by L (G) = {b, aaa}; 1. we can derive aA from using S → aA, and then derive aaa using A → aa. 2. We can also derive b using S → b. Another example Grammar: G=(V,T,S,P) T={a,b} V={a,b,S} Derivation of sentence P= S  aSb S  ab : S  aSb  ab S  aSb S  26 S  aSb S  Grammar: : aabb S  aSb  aaSbb  aabb Derivation of sentence S  aSb S  27 Other derivations: S  aSb  aaSbb  aaaSbbb  aaabbb S  aSb  aaSbb  aaaSbbb  aaaaSbbbb  aaaabbbb So, what’s the language of the grammar with the productions? S  aSb S  28 Language of the grammar with the productions: S  aSb S  n n L  {a b : n  0} 29 PSG Example – English Fragment We have G = (V, T, S, P), where: V = {(sentence), (noun phrase), (verb phrase), (article), (adjective), (noun), (verb), (adverb), a, the, large, hungry, rabbit, mathematician, eats, hops, quickly, wildly} T = {a, the, large, hungry, rabbit, mathematician, eats, hops, quickly, wildly} S = (sentence) P = (see next slide) Productions for our Language P = { (sentence) → (noun phrase) (verb phrase), (noun phrase) → (article) (adjective) (noun), (noun phrase) → (article) (noun), (verb phrase) → (verb) (adverb), (verb phrase) → (verb), (article) → a, (article) → the, (adjective) → large, (adjective) → hungry, (noun) → rabbit, (noun) → mathematician, (verb) → eats, (verb) → hops, (adverb) → quickly, (adverb) → wildly } A Sample Sentence Derivation (sentence) (noun phrase) (verb phrase) (article) (adj.) (noun) (verb phrase) (art.) (adj.) (noun) (verb) (adverb) the (adj.) (noun) (verb) (adverb) the large (noun) (verb) (adverb) the large rabbit (verb) (adverb) the large rabbit hops (adverb) the large rabbit hops quickly On each step, we apply a production to a fragment of the previous sentence template to get a new sentence template. Finally, we end up with a sequence of terminals (real words), that is, a sentence of our language L. Another Example V T Let G = ({a, b, A, B, S}, {a, b}, S, P {S → ABa, A → BB, B → ab, AB → b}). One possible derivation in this grammar is: S  ABa  Aaba  BBaba  Bababa  abababa. Defining the PSG Types Type 0: Phase-structure grammars – no restrictions on the production rules Type 1: Context-Sensitive PSG: – All after fragments are either longer than the corresponding before fragments, or empty: if b → a, then |b| < |a|  a = λ . Type 2: Context-Free PSG: – All before fragments have length 1 and are nonterminals: if b → a, then |b| = 1 (b  N). Type 3: Regular PSGs: – All before fragments have length 1 and nonterminals – All after fragments are either single terminals, or a pair of a terminal followed by a nonterminal. if b → a, then a  T  a  TN. Types of Grammars Chomsky hierarchy of languages Venn Diagram of Grammar Types: Type 0 – Phrase-structure Grammars Type 1 – Context-Sensitive Type 2 – Context-Free Type 3 – Regular Classifying grammars Given a grammar, we need to be able to find the smallest class in which it belongs. This can be determined by answering three questions: Are the left hand sides of all of the productions single non-terminals? If yes, does each of the productions create at most one non-terminal and is it on the right? Yes – regular No – context-free If not, can any of the rules reduce the length of a string of terminals and non-terminals? Yes – unrestricted No – context-sensitive Definition: Context-Free Grammars Grammar Vocabulary G  (V ,T , S , P) Terminal symbols Start variable Productions of the form: A x Non-Terminal String of variables and terminals Derivation Tree of A Context-free Grammar ► Represents the language using an ordered rooted tree. ► Root represents the starting symbol. ► Internal vertices represent the nonterminal symbol that arise in the production. ► Leaves represent the terminal symbols. ► If the production A → w arise in the derivation, where w is a word, the vertex that represents A has as children vertices that represent each symbol in w, in order from left to right. Language Generated by a Grammar Example: Let G = ({S,A,a,b},{a,b}, S, {S → aA, S → b, A → aa}). What is L(G)? Easy: We can just draw a tree of all possible derivations. – We have: S  aA  aaa. – and S  b. S aA b Answer: L = {aaa, b}. aaa Example of a derivation tree or parse tree or sentence diagram. Example: Derivation Tree ► Let G be a context-free grammar with the productions P = {S →aAB, A →Bba, B →bB, B →c}. The word w = acbabc can be derived from S as follows: S ⇒ aAB →a(Bba)B ⇒ acbaB ⇒ acba(bB) ⇒ acbabc Thus, the derivation tree is given as follows: S a A B c b B a b B c Backus-Naur Form sentence :: noun phrase verb phrase noun phrase :: article [adjective] noun verb phrase :: verb [adverb] article :: a | the Square brackets [] mean “optional” adjective :: large | hungry noun :: rabbit | mathematician Vertical bars verb :: eats | hops mean “alternatives” adverb :: quickly | wildly Generating Infinite Languages A simple PSG can easily generate an infinite language. Example: S → 11S, S → 0 (T = {0,1}). The derivations are: – – – – S0 S  11S  110 S  11S  1111S  11110 and so on… L = {(11)*0} – the set of all strings consisting of some number of concatenations of 11 with itself, followed by 0. Another example Construct a PSG that generates the language L = {0n1n | nN}. – 0 and 1 here represent symbols being concatenated n times, not integers being raised to the nth power. Solution strategy: Each step of the derivation should preserve the invariant that the number of 0’s = the number of 1’s in the template so far, and all 0’s come before all 1’s. Solution: S → 0S1, S → λ. Context-Sensitive Languages The language { anbncn | n  1} is context-sensitive but not context free. A grammar for this language is given by: S  aSBC | aBC CB  BC aB  ab bB  bb Terminal bC  bc and non-terminal cC  cc A derivation from this grammar is:S  aSBC  aaBCBC (using S  aBC)  aabCBC (using aB  ab)  aabBCC (using CB  BC)  aabbCC (using bB  bb)  aabbcC (using bC  bc)  aabbcc (using cC  cc) which derives a2b2c2.