CH 01

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Chapter 1:

Foundations: Sets, Logic, and Algorithms

Discrete Mathematical Structures:


Theory and Applications

Learning Objectives

‰Learn about sets

‰Explore various operations on sets

‰Become familiar with Venn diagrams

‰Learn how to represent sets in computer


memory

‰Learn about proposition logic

Discrete Mathematical Structures: Theory and Applications 2

Learning Objectives

‰Learn how to use logical connectives to combine


statements

‰Become familiar with quantifiers and predicates

‰Learn various proof techniques

‰Explore what an algorithm is

Discrete Mathematical Structures: Theory and Applications 3


Sets
‰Definition: Well-defined collection of distinct
objects
‰Members or Elements: objects in the set
‰Membership Test: Let Vowels be the set of all
vowels.
‰“a belongs to the set of Vowels” is written as: a ∈
Vowels
‰“j does not belong to the set of Vowels: j ∉ Vowels
‰A well-defined set can always tell which elements
are its members.

Discrete Mathematical Structures: Theory and Applications 4

How to Define a Set: Method 1


‰Roster Method: Description of a set by
enumerating the elements, enclosed with
braces
‰Examples:
‰Vowels = {a,e,i,o,u}
‰Primary colors = {red, blue, yellow}
‰N = { 1, 2, …, n, … }

Discrete Mathematical Structures: Theory and Applications 5

How to Define a Set: Method 2

‰Set-builder method

‰A = { x | x ∈ S, P(x) } or A = { x ∈ S | P(x) }

‰ A is the set of all elements x of S, such that x


satisfies the property P

‰Example:

‰If X = {2,4,6,8,10}, then in set-builder notation, X


can be described as

X = {n ∈ Z | n is even and 2 ≤ n ≤ 10}

Discrete Mathematical Structures: Theory and Applications 6


Sets of Numbers
‰Standard Symbols which denote sets of numbers
‰N : The set of all natural numbers
‰Z : The set of all integers
‰Q : The set of all rational numbers
‰R : The set of all real numbers
‰C : The set of all complex numbers

Discrete Mathematical Structures: Theory and Applications 7

Subsets
‰Set X is a subset of set Y if every member of X is
also a member of Y.

‰“X is a subset of Y” is written as X ⊆ Y

‰“X is not a subset of Y” is written as X Y

‰Example:

‰ X = {a,e,i,o,u}, Y = {a, i, u} and z = {i, n, o, u}

‰Y ⊆ X, since every element of Y is an element of X

‰Y Z, since a ∈ Y, but a ∉ Z

Discrete Mathematical Structures: Theory and Applications 8

Subsets vs. Supersets


‰Superset
‰ If X ⊆ Y, then “Y is a super set of X” or “Y
contains X”, written Y ⊇ X
‰Proper Subset
‰ X is a proper subset of Y if X ⊆ Y and
there exists at least one element in Y that is
not in X. This is written X ⊂ Y.
‰Example:
‰ X = {a,e,i,o,u}, Y = {a,e,i,o,u,y}
‰X ⊂ Y , since y ∈ Y, but y ∉ X

Discrete Mathematical Structures: Theory and Applications 9


Set Equality and Empty Set
‰Set Equality
‰X and Y are said to be equal if every element of X
is an element of Y and every element of Y is an
element of X, i.e. X ⊆ Y and Y ⊆ X
‰Examples:
‰{1,2,3} = {2,3,1}
‰X = {red, blue, yellow} and Y = {c | c is a primary
color} Therefore, X=Y
‰Empty (Null) Set
‰A Set is Empty (Null) if it contains no elements.
‰The Empty Set is written as ∅
‰The Empty Set is a subset of every set
Discrete Mathematical Structures: Theory and Applications 10

Finite and Infinite Sets

‰ X is a set. If there exists a nonnegative integer n


such that X has n elements, then X is called a finite
set with n elements.
‰ If a set is not finite, then it is an infinite set.
‰ Examples:
‰ Y = {1,2,3} is a finite set
‰ P = {red, blue, yellow} is a finite set
‰ E , the set of all even integers, is an infinite set
‰ ∅ , the empty set, is a finite set with 0 elements

Discrete Mathematical Structures: Theory and Applications 11

Cardinality of Sets
‰Let S be a finite set with n distinct elements,
where n ≥ 0. Then |S| = n , where the
cardinality (i.e., the size) of S is n
‰Example:
‰If P = {red, blue, yellow}, then |P| = 3
‰Singleton
‰ A set with only one element is a singleton
‰Example:
‰H = { 4 }, |H| = 1, H is a singleton

Discrete Mathematical Structures: Theory and Applications 12


Power Sets

‰Power Set
‰For any set X ,the power set of X ,written P(X),is
the set of all subsets of X
‰Example:
‰If X = {red, blue, yellow}, then P(X) = { ∅ , {red},
{blue}, {yellow}, {red,blue}, {red, yellow}, {blue,
yellow}, {red, blue, yellow} }

‰Theorem: If |X| = n, then |P(X)| = 2n

Discrete Mathematical Structures: Theory and Applications 13

Barber’s Paradox

‰In a small village, a barber claims that “I


shave anyone who does not shave himself,
and none else.”
‰Let A = the set of men who do not shave
themselves.
‰Let B = the set of men that the barber
shaves.
‰A = B?

Discrete Mathematical Structures: Theory and Applications 14

Russell’s Paradox (1901)

‰Let M be the set of all sets that do not


contain themselves as members. That is,
M = { A | A ∉ A }.
‰Question: Does M contains itself?

Discrete Mathematical Structures: Theory and Applications 15


Cantor’s Paradox (189?)

‰The cardinality of a set is smaller than the


cardinality of its power set.
‰The set of all sets is its own power set.
‰Therefore, the cardinality of the set of all
sets is smaller than itself.

Discrete Mathematical Structures: Theory and Applications 16

Universal Sets and Venn Diagrams

Universal Set: A set of all ‰Venn Diagrams


objects, i.e., a superset of ‰Abstract visualization
all sets, in a given of a Universal set, U
problem. as a rectangle, with all
subsets of U shown as
circles.
‰Shaded portion
represents the
corresponding set
‰Example:
‰In Figure 1, Set X,
shaded, is a subset of
the Universal set, U
Discrete Mathematical Structures: Theory and Applications 17

Union of Sets

Example:
If X = {1,2,3,4,5} and Y = {5,6,7,8,9}, then
X∪Y = {1,2,3,4,5,6,7,8,9}
Discrete Mathematical Structures: Theory and Applications 18
Intersection of Sets

Example:
If X = {1,2,3,4,5} and Y = {5,6,7,8,9}, then X ∩ Y = {5}

Discrete Mathematical Structures: Theory and Applications 19

Disjoint Sets

Example:
If X = {1,2,3,4,} and Y = {6,7,8,9}, then X ∩ Y = ∅

Discrete Mathematical Structures: Theory and Applications 20

Properties of Set Operations

Discrete Mathematical Structures: Theory and Applications 21


Properties of Set Operations

Discrete Mathematical Structures: Theory and Applications 22

Operations over Multiple Sets


‰ The union and intersection
of three,four,or even
infinitely many sets can be
considered
‰ For a finite collection of n
sets, X1, X2, … Xn where n
≥2:

Discrete Mathematical Structures: Theory and Applications 23

Mutually Disjoint Sets

A family of sets A = { A1, A2, …, An } are said to be


mutually disjoint, or pairwise disjoint, if for any Ai,
Aj in A, Ai ∩ Aj = ∅

Example:

‰If A = {a,b,c}, B = {x, y, z} and C = {1,2,3}


then A∩B = ∅ and B∩C = ∅ and A∩C= ∅.
Therefore, A,B,C are pairwise disjoint

Discrete Mathematical Structures: Theory and Applications 24


Difference of Sets

• Example:
If X = {a,b,c,d} and Y =
{c,d,e,f}, then X – Y =
{a,b} and Y – X = {e,f}

Discrete Mathematical Structures: Theory and Applications 25

Complement of Sets

Example:
If U = {a,b,c,d,e,f} and
X = {c,d,e,f}, then X’ =
{a,b}

Discrete Mathematical Structures: Theory and Applications 26

Illustration of Difference Sets

Discrete Mathematical Structures: Theory and Applications 27


Properties of Difference

Discrete Mathematical Structures: Theory and Applications 28

Discrete Mathematical Structures: Theory and Applications 29

Ordered Pairs and Cartesian Products


‰Ordered Pair
‰X and Y are sets. If x ∈ X and y ∈ Y, then an ordered
pair is written (x,y)
‰Order of elements is important. (x,y) is not necessarily
equal to (y,x)
‰Cartesian Product
‰ The Cartesian product of two sets X and Y , written X
×Y ,is the set
‰ X ×Y ={ (x, y) |x ∈ X , y ∈ Y}
‰For any set X, X × ∅ = ∅ = ∅ × X
‰ Example:
‰ X = {a,b}, Y = {c,d}
‰X × Y = {(a,c), (a,d), (b,c), (b,d)}
‰Y × X = {(c,a), (d,a), (c,b), (d,b)}

Discrete Mathematical Structures: Theory and Applications 30


Computer Representation of Sets

‰ A set may be stored in a computer in an array as an


unordered list
‰Problem: Difficult to perform operations on the set.
‰ Solution: use Bit Strings
‰A Bit String is a sequence of 0s and 1s
‰Length of a Bit String is the number of digits in the
string
‰Elements appear in order in the bit string
‰A 0 indicates an element is absent, a 1 indicates that the
element is present

Discrete Mathematical Structures: Theory and Applications 31

Mathematical Logic

‰Definition: Methods of reasoning, provides rules


and techniques to determine whether an
argument is valid
‰Theorem: a statement that can be shown to be
true (under certain conditions)
‰Example: If x is an even integer, then x + 1 is an
odd integer
‰This statement is true under the condition that x is an
integer is true

Discrete Mathematical Structures: Theory and Applications 32

Propositional Logic
‰A statement, or a proposition, is a declarative
sentence that is either true or false, but not both
‰Lowercase letters denote propositions
‰Examples:
‰p: 2 is an even number (true)
‰q: 3 is an odd number (true)
‰r: A is a consonant (false)
‰The following are not propositions:
‰p: My cat is beautiful
‰q: Are you in charge?

Discrete Mathematical Structures: Theory and Applications 33


Truth Values and Truth Assignments

‰One of the truth values “true” or “false”


assigned to a statement or propositional
variable
‰True is abbreviated to T or 1
‰False is abbreviated to F or 0
‰A truth assignment is a mapping from
statements (or propositional variables) to
truth values (such that every statement is
assigned a truth value).

Discrete Mathematical Structures: Theory and Applications 34

Negation
‰ The negation of p, written ∼p, is the statement
obtained by negating statement p
‰Truth values of p and ∼p are opposite
‰Symbol ~ is called “not” ~p is read as as “not p”
‰Example:
‰p: A is a consonant
‰~p: it is the case that A is not a consonant
‰Truth Table
‰Each row represents
a truth assignment for
propositions.

Discrete Mathematical Structures: Theory and Applications 35

Conjunction (logical AND)

‰The conjunction of p and q,


written p ∧ q , is the
statement formed by
joining statements p and q
using the word “and”
‰The statement p∧q is true
if both p and q are true;
otherwise p∧q is false
‰Truth Table:

Discrete Mathematical Structures: Theory and Applications 36


Disjunction
‰Let p and q be statements. The disjunction
of p and q, written p ∨ q , is the statement
formed by joining statements p and q using
the word “or”
‰The statement p∨q is true if at least one of the
statements p and q is true; otherwise p∨q is
false

‰The symbol ∨ is read “or”

Discrete Mathematical Structures: Theory and Applications 37

Truth Table for Disjunction

‰Truth Table for


Disjunction:

Discrete Mathematical Structures: Theory and Applications 38

Implication
‰Let p and q be statements.The statement “if p
then q” is called an implication or condition.
‰The implication “if p then q” is written p → q
‰p → q is read:
‰“If p, then q”
‰“p is sufficient for q”
‰q if p
‰q whenever p
‰p implies q

Discrete Mathematical Structures: Theory and Applications 39


Truth Table for Implication
‰Truth Table for Implication:

‰p is called the hypothesis, q is called the conclusion

Discrete Mathematical Structures: Theory and Applications 40

Converse, Inverse and Contrapositive

‰ Let p: Today is Sunday and q: I will wash the car.


The conjunction p → q is the statement:
‰p → q : If today is Sunday, then I will wash the car
‰ The converse of this implication is written q → p
‰If I wash the car, then today is Sunday
‰ The inverse of this implication is ~p → ~q
‰If today is not Sunday, then I will not wash the car
‰ The contrapositive of this implication is ~q → ~p
‰If I do not wash the car, then today is not Sunday

Discrete Mathematical Structures: Theory and Applications 41

Biconditional or Equivalence
‰Let p and q be statements. The statement “p
if and only if q” is called the biimplication or
biconditional of p and q
‰The biconditional “p if and only if q” is written
p↔q
‰p ↔ q is read:
‰“p if and only if q”
‰“p is necessary and sufficient for q”
‰“q if and only if p”
‰“p is equivalent to q”

Discrete Mathematical Structures: Theory and Applications 42


Truth Table for Biconditional
‰Truth Table for the Biconditional:

Discrete Mathematical Structures: Theory and Applications 43

Statement Formulas
‰ Symbols p ,q ,r ,...,called propositional or
statement variables
‰ Symbols ~, ∧, ∨, →,and ↔ are called logical
connectives
‰ Definition of Statement Formulas:
1) A statement variable is a statement formula
2) If A and B are statement formulas, then the
expressions (~A ), (A ∧ B) , (A ∨ B ), (A →
B ) and (A ↔ B ) are statement formulas
‰ Expressions are statement formulas that are
constructed only by using 1) and 2) above
Discrete Mathematical Structures: Theory and Applications 44

Precedence of Logical Connectives

‰Precedence of logical connectives is:

‰~ highest

‰∧ second highest

‰∨ third highest

‰→ fourth highest

‰↔ fifth highest

Discrete Mathematical Structures: Theory and Applications 45


Mathematical Logic
‰Example:
‰Let A be the statement formula (~(p ∨q )) → (q ∧p )
‰Truth Table for A is:

Discrete Mathematical Structures: Theory and Applications 46

Tautology and Contradiction

‰Tautology
‰A statement formula A is said to be a tautology if
the truth value of A is T for any assignment of the
truth values T and F to the statement variables
occurring in A
‰Contradiction
‰A statement formula A is said to be a
contradiction if the truth value of A is F for any
assignment of the truth values T and F to the
statement variables occurring in A

Discrete Mathematical Structures: Theory and Applications 47

Logical Implication and Logical


Equivalence
‰Logically Implies
‰A statement formula A is said to logically imply a
statement formula B if the statement formula A →
B is a tautology. If A logically implies B, then
symbolically we write A → B
‰Logically Equivalent
‰A statement formula A is said to be logically
equivalent to a statement formula B if the
statement formula A ↔ B is a tautology. If A is
logically equivalent to B , then symbolically we
write A ≡ B (or A ⇔ B)
Discrete Mathematical Structures: Theory and Applications 48
Properties of Logical Connectives

Discrete Mathematical Structures: Theory and Applications 49

Example of Proof

‰ Proof of (~p ∧q ) → (~(q →p ))

Discrete Mathematical Structures: Theory and Applications 50

Validity of Proofs
‰Proof: a proof of a theorem consists of a finite
sequence of logically valid statements ending in
a conclusion A1 , A2 , A3 , ..., An −1 , An
‰The final statement, An , is the conclusion, and
the statements A1 , A2 , A3 , ..., An −1 are the
premises of the argument.
‰A proof is logically valid if the statement formula,
i.e., A1 , A2 , A3 , ..., An−1 → An is a tautology.

Discrete Mathematical Structures: Theory and Applications 51


Validity of Arguments
‰Valid Argument Forms
‰Modus Ponens (Method of Affirming)

‰Modus Tollens (Method of Denying)

Discrete Mathematical Structures: Theory and Applications 52

Validity of Arguments
‰ Valid Argument Forms
‰ Hypothetical Syllogism

‰ Dilemma

Discrete Mathematical Structures: Theory and Applications 53

Quantifiers and First Order Logic

‰Predicate or Propositional Function


‰Let x be a variable and D be a set; P(x) is a
sentence
‰Then P(x) is called a predicate function with
respect to the set D if for each value of x in D,
P(x) is a statement; i.e., P(x) is true or false
‰Moreover, D is called the domain of the
discourse and x is called the free variable

Discrete Mathematical Structures: Theory and Applications 54


Quantifiers and First Order Logic
‰Predicate Function

‰Example:
‰ Q(x,y) : x > y, where the Domain is the set of integers
‰ Q is a 2-place predicate
‰ Q is T for Q(4,3) and Q is F for Q (3,4)

Discrete Mathematical Structures: Theory and Applications 55

Universal Quantifiers

‰Let P(x) be a predicate and let D be the domain of


the discourse. The universal quantification of P(x)
is the statement:
‰For all x, P(x) or
‰For every x, P(x)
‰The symbol ∀ is read as “for all and every”
‰ ∀x P ( x)
‰ Two-place predicate: ∀x∀y P ( x, y )

Discrete Mathematical Structures: Theory and Applications 56

Existential Quantifiers

‰Let P(x) be a predicate and let D be the domain of the


discourse. The existential quantification of P(x) is the
statement:
‰There exists x, P(x)
‰The symbol ∃ is read as “there exists”
‰ ∃x P ( x )
‰ Bound Variable
‰The variable appearing in: ∀x P( x) or ∃x P ( x )

Discrete Mathematical Structures: Theory and Applications 57


Examples of Quantifiers

‰ Everybody loves somebody.

‰ Everybody is loved by at least one.

‰ There is one that everybody loves.

‰ He loves everybody.

‰ Brothers are siblings.

‰ One’s mother is one’s female parent.

‰ A first cousin is a child of a parent’s sibling.

Discrete Mathematical Structures: Theory and Applications 58

Examples of Quantifiers

‰All apples are red.


‰ ∀x Apple(x) /\ Red(x)
‰ ∀x Apple(x) → Red(x)
‰ Implication is the usual connective for ∀
‰One person in UI is very smart.
‰ ∃x At(x, UI) /\ Smart(x)
‰ ∃x At(x, UI) → Smart(x)
‰ Conjunction is the usual connective for ∃

Discrete Mathematical Structures: Theory and Applications 59

Logic versus Set

‰What’s the relationship between P(x) and the


set SP = { x | P(x) }?
‰The domain D for x is the Universal Set.
‰Theorem:
‰P(a) is true iff a ∈ SP.
‰ ∀ x P(x) is true iff SP = D.
‰ ∃ x P(x) is true iff SP is not ∅.

Discrete Mathematical Structures: Theory and Applications 60


Logic versus Set

‰What’s relationship between logical connectives


and set operations?
‰Let SP = { x | P(x) } and SQ = { x | Q(x) }.
‰Theorem:
‰ P(a) \/ Q(a) is true iff a ∈ SP ∪ SQ.
‰ P(a) /\ Q(a) is true iff a ∈ SP ∩ SQ.
‰ ∀ x P(x) \/ Q(x) is true iff SP ∪ SQ = D.
‰ ∀ x P(x) /\ Q(x) is true iff SP ∩ SQ = D.
‰ ∃ x P(x) \/ Q(x) is true iff SP ∪ SQ is not ∅..
‰ ∃ x P(x) /\ Q(x) is true iff SP ∩ SQ is not ∅.

Discrete Mathematical Structures: Theory and Applications 61

Negation of Predicates

‰ ~ ∀x P( x) ≡ ∃x ~ P( x)
‰ Example:
‰ If P(x) is the statement “x has won a race” where the
domain of discourse is all runners, then the universal
quantification of P(x) is ∀x P ( x) , i.e., every runner has
won a race. The negation of this statement is “it is not the
case that every runner has won a race. Therefore there
exists at least one runner who has not won a race.
Therefore: ∃x ~ P ( x)
and so, ~ ∀x P ( x) ≡ ∃x ~ P ( x)

Discrete Mathematical Structures: Theory and Applications 62

Negation of Predicates

~ ∀x P( x) ≡ ∃x ~ P( x)
~ ∃x P( x) ≡ ∀x ~ P( x)

Discrete Mathematical Structures: Theory and Applications 63


Formulas in First Order Logic

‰All statement formulas are considered formulas


‰Each n, n =1,2,...,n-place predicate P(x1 , x2 , ... , xn ) containing
the variables x1 , x2 , ... , xn is a formula.
‰If A and B are formulas, then the expressions ~A, (A∧B),
(A∨B) , A →B and A↔B are statement formulas, where
~, ∧, ∨, → and ↔ are logical connectives
‰If A is a formula and x is a variable, then ∀x A(x) and
∃x A(x) are formulas
‰All formulas constructed using only above rules are
considered formulas in predicate or first order logic

Discrete Mathematical Structures: Theory and Applications 64

Counterexample and Disproof


‰An argument has the form ∀x (P(x ) → Q(x )), where the
domain of discourse is D
‰To show that this implication is not true in the domain D, it
must be shown that there exists some x in D such that
(P(x ) → Q(x )) is not true
‰This means that there exists some x in D such that P(x) is true
but Q(x) is not true. Such an x is called a counterexample of
the above implication
‰To show that ∀x (P(x) → Q(x)) is false by finding an x in D
such that P(x) → Q(x) is false is called the disproof of the
given statement by counterexample

Discrete Mathematical Structures: Theory and Applications 65

Proof Techniques

‰Theorem

‰Statement that can be shown to be true (under certain


conditions)

‰Typically Stated in one of three ways

‰As Facts

‰As Implications

‰As Biimplications

Discrete Mathematical Structures: Theory and Applications 66


Proof Techniques
‰Direct Proof or Proof by Direct Method
‰Proof of those theorems that can be expressed in
the form ∀x (P(x) → Q(x)), D is the domain of
discourse
‰Select a particular, but arbitrarily chosen, member
a of the domain D
‰Show that the statement P(a) → Q(a) is true.
(Assume that P(a) is true, show that Q(a) is true)

Discrete Mathematical Structures: Theory and Applications 67

Proof Techniques

‰Indirect Proof
‰The implication p → q is equivalent to the
implication (∼q → ∼p)
‰Therefore, in order to show that p → q is true,
one can also show that the implication
(∼q → ∼p) is true
‰To show that (∼q → ∼p) is true, assume that
the negation of q is true and prove that the
negation of p is true

Discrete Mathematical Structures: Theory and Applications 68

Proof Techniques
‰Proof by Contradiction
‰Assume that the conclusion is not true and then
arrive at a contradiction
‰Example: Prove that there are infinitely many prime
numbers
‰Proof:
‰Assume there are not infinitely many prime numbers,
therefore they are listable, i.e. p1,p2,…,pn
‰Consider the number q = p1p2…pn+1. q is not
divisible by any of the listed primes
‰Therefore, q is a prime. However, it was not listed.
‰Contradiction! Therefore, there are infinitely many
primes
Discrete Mathematical Structures: Theory and Applications 69
Proof Techniques

Discrete Mathematical Structures: Theory and Applications 70

Proof Techniques
‰Proof of Biimplications
‰To prove a theorem of the form ∀x (P(x) ↔
Q(x )), where D is the domain of the discourse,
consider an arbitrary but fixed element a from D.
For this a, prove that the biimplication P(a) ↔
Q(a) is true
‰The biimplication p ↔ q is equivalent to
(p → q) ∧ (q → p)
‰Prove that the implications p → q and q → p are
true
‰Assume that p is true and show that q is true
‰Assume that q is true and show that p is true
Discrete Mathematical Structures: Theory and Applications 71

Proof Techniques

‰Proof of Equivalent Statements


‰Consider the theorem that says that statements
p,q and r are equivalent
‰Show that p → q, q → r and r → p
‰Assume p and prove q. Then assume q and prove
r Finally, assume r and prove p
‰Or, prove that p if and only if q, and then q if and
only if r
‰Other methods are possible

Discrete Mathematical Structures: Theory and Applications 72


Algorithms
‰ Definition: step-by-step problem-solving process in which a
solution is arrived at in a finite amount of time
‰ All algorithms have the following properties:
‰Input : For example, a set of numbers to find the sum of the
numbers
‰Output : For example, the sum of the numbers
‰Precision : Each step of the algorithm is precisely defined
‰Uniqueness : Results of each step are unique and depend on the
input and results of previous step
‰Finiteness : Algorithm must terminate after executing a finite
number of steps
‰Generality : Algorithm is general in that it applies to a set of
inputs
Discrete Mathematical Structures: Theory and Applications 73

Algorithms
‰ Pseudocode Conventions
‰ The symbol := is called the assignment operator
‰Example: The statement x := a is read as “assign the value a
to x” or “x gets the value a” or “copy the value of a into x”
‰x := a is also known as an assignment statement
‰ Control Structures
‰One way-selection
‰if booleanExpression then statement
‰If booleanExpression evaluates to true, statement is
evaluated
‰Two way-selection
‰if booleanExpression then statement1
else statement2
‰If booleanExpression evaluates to true , statement1
executes,
‰otherwise statement2 executes
Discrete Mathematical Structures: Theory and Applications 74

Algorithms
‰ Pseudocode Conventions
‰ Control Structures
‰The while loop takes the form:
‰while booleanExpression do loopBody
‰The booleanExpression is evaluated. If it evaluates
to true, loopBody executes. Thereafter loopBody
continues to execute as long as booleanExpression
is true
‰The for loop takes the form:
‰for var := start to limit do loopBody
‰var is an integer variable. The variable var is set to
the value specified by start. If var limit, loopBody

executes. After executing the loopBody , var is
incremented by 1. The statement continues to
execute until var > limit
Discrete Mathematical Structures: Theory and Applications 75
Algorithms

‰Pseudocode Conventions

‰Control Structures

‰The do/while loop takes the form:

‰do loopBody while booleanExpression

‰The loopBody is executed first and then the


booleanExpression is evaluated. The loopBody
continues to execute as long as the
booleanExpression is true

Discrete Mathematical Structures: Theory and Applications 76

Algorithms

‰Pseudocode Conventions
‰Block of Statement
‰To consider a set of statements a single statement, the
statements are written between the words begin and
end
begin
statement1
statement2
...
statementn;
end

Discrete Mathematical Structures: Theory and Applications 77

Algorithms
‰Pseudocode Conventions
‰Return Statement
‰The return statement is used to return the value
computed by the algorithm and it takes the following
form:
return expression;
‰The value specified by expression is returned. In an
algorithm, the execution of a return statement also
terminates the algorithm
‰Read and Print Statements
‰read x;
‰Read the next value≤ and store it in the variable x
‰print x;
‰Output the value of x
Discrete Mathematical Structures: Theory and Applications 78
Algorithms
‰Pseudocode Conventions
‰Arrays (List)
‰A list is a set of elements of the same type
‰The length of the list is the number of elements in the list
‰L[1...n ]. L is an array of n components, indexed 1 to n . L[i ]
denotes the ith element of L
‰For data in tabular form, a two-dimensional array is used:
‰M[1...m,1...n ] M is a two-dimensional array of m rows
and n columns
‰The rows are indexed 1 to m and the columns are
indexed 1 to n
‰M[i,j] denotes the (i,j)th element of M, that is, the element
at the ith row and jth column position

Discrete Mathematical Structures: Theory and Applications 79

Algorithms
‰ Pseudocode Conventions
‰ Subprograms (Procedures)
‰In a programming language,an algorithm is implemented in
the form of a subprogram, a.k.a. a subroutine or a module
‰Two types of subprograms
‰Functions
‰Returns a unique value
‰Procedure
‰Other types of subprograms
‰body of the function or procedure is enclosed between the
words begin and end
‰the execution of a return statement in a function terminates
the function

Discrete Mathematical Structures: Theory and Applications 80

Algorithms

‰Pseudocode Conventions
‰ Comments
‰In describing the steps of an algorithm, comments are
included wherever necessary to clarify the steps
‰Two types of comments: single-line and multi-line
‰Single-line comments start anywhere in the line
with the pair of symbols //
‰Multi-line comments are enclosed between the pair
of symbols /* and */
‰Specifies what the algorithm does, as well as the input
and output

Discrete Mathematical Structures: Theory and Applications 81


Algorithms
‰Polynomial Operations
‰ Polynomial in one variable is

‰ ai are real (or complex) and n is a non-negative integer


‰ If p(x) = a0 then p(x) is a constant polynomial
‰ If p(x) is a nonzero constant polynomial, then the degree of p(x) is
defined to be 0
‰ If p(x) is not constant and an ≠ 0, then n is called the degree of p(x);
that is, the degree of a non-constant polynomial is defined to be the
exponent of the highest power of x
‰ The basic operations performed on polynomials are to add, subtract,
multiply, and divide polynomials, and to evaluate a polynomial at a
given point

Discrete Mathematical Structures: Theory and Applications 82

You might also like