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

CCE133 – Discrete Structures

Lecture Notes
Department of Computer Engineering
College of Computer Science
133CCE – Discrete Structures
LECTURE DETAILS
1 History and overview, tools, standards, constraints
2 Sets, Relations, Functions
3 Prepositional logic, First-order logic and Reasoning
4 Proof techniques
5 Basics of counting
6 Discrete Probability
7 Graphs, Search Techniques
8 Trees
9 Iteration and recursion
Text Book :
Kenneth H. Rosen, Discrete Mathematics and its applications, McGraw
Hill.7ed,2012
Ref Book :
1. Edgar G. Goodaire & Michael M. Parmenter, Discrete Mathematical Structure with
Graph Theory, Prentice Hall, 3rd ed, 2005
2. Dr.Purnima, P.Patwardhan, Discrete Structures, Technical Publications, 2009
3. Internet sources
133CCE - Course Learning Outcomes:
1. Define the concepts of Sets, relations, functions,
probability, Iteration and recursion
2. State the concepts of discrete structures, standards,
tools and constraints
3. Formulate various propositions, compound propositions
and proof techniques
4. Analyze the various searching methods of graphs and
binary tree
5. Compose and Calculate permutations, combinations,
mathematical logic and Tautology
6. Demonstrate an ability to work in team for solving
discrete structures problems
7. Communicate in written about Tautology, contradictions
and its implications
CCE133 – Discrete Structures

Lecture Notes

Lecture 1
History and Overview
Tools, Standards, Constraints
Data
Data is a collection of facts, such as numbers, words
Data can be qualitative or quantitative
Qualitative data is descriptive information (it describes something)
Quantitative data, is numerical information (numbers).

Quantitative data can be Discrete or Continuous:


Discrete data can only take certain values (like whole numbers)
Continuous data can take any value (within a range)
Put simply: Discrete data is counted, Continuous data is measured
Mathematics categories
1) Continuous Mathematics deals with continuous data, continuous
functions, differential and integral calculus etc
Example of continuous math – Given a fixed surface area, what are
the dimensions of a cylinder that maximizes volume?
2) Discrete Mathematics − deals with data whose values are
separated (such as integers: Number line has gaps)
Example of Discrete Math – Given a fixed set of characters, and a
length, how many different passwords can you construct?
How many edges in graph with n vertices?

- Discrete structures are structures that are used in describing


discrete mathematics.
- Discrete mathematics is math that makes use of discrete
structures
- It is more common today to use the term “discrete mathematics”
and “discrete structures” interchangeable
Why study discrete mathematics in computer science?
- All information is stored in computers is in bits [0 or 1] (discrete)
-When determining how much time an algorithm needs to run, you
count the number of operations it needs to perform (discrete),
Finding good algorithms for sorting, Analysing algorithms for
correctness and efficiency
-Logic is used heavily in computer architecture design (logic gates).
Design efficient computer systems
- A large part of graph theory is looking at connections between
nodes and computer networks. Designing high-speed networks and
message routing paths
-Sets theory, relations and functions are used in design databases
-Computational geometry has been an important part of
the computer graphics incorporated into modern video
games and computer-aided design tools.
- it is the mathematics underlying many computer science courses
Some courses of computer science using
from discrete mathematics
Concepts and notations from discrete mathematics are
useful in studying and describing objects and problems in
many courses of computer science, such as
- Digital logic,
- computer algorithms and data structures,
- programming languages and software development,
- Cryptography and network security
- operations research,
- data base,
- computer graphics,
- computer networks . . . . .
History of discrete mathematics
- The history of discrete mathematics has involved a number of challenging
problems which have focused attention within areas of the field.
- The history of graph theory may be specifically traced to 1735, when the Swiss
mathematician Euler solved the Königsberg bridge problem
- In graph theory, much research was motivated by attempts to prove the four
color theorem, first stated in 1852, but not proved until 1976 (by Kenneth Appel
and Wolfgang Haken, using substantial computer assistance)
- In logic, the David Hilbert's list of open problems presented in 1900 was to prove
that the axioms of arithmetic are consistent
-The need to break German codes in World War II led to advances
in cryptography and theoretical computer science, with the first programmable
digital electronic computer being developed at England's Bletchley Park with the
guidance of Alan Turing and his seminal work, On Computable Numbers. At the
same time, military requirements motivated advances in operations research.
The Cold War meant that cryptography remained important, with fundamental
advances such as public-key cryptography being developed in the following
decades.
- Operations research remained important as a tool in business and project
management, with the critical path method being developed in the 1950s.
Overview of some topics in discrete mathematics
Set theory - Set theory is the branch of mathematics that
studies sets, relations have applications in several areas. The relational
algebra of set theory is used in databases

Boolean Algebra - algebra that deals with binary variables and logic
operations. It is used in logic gates and programming, computer system
design

Logic - Logic is the study of the principles of valid reasoning


and inference, as well as of consistency, soundness, and completeness,
truth table verification. Logic is useful in artificial intelligence

Proof techniques - The study of mathematical proof is particularly


important in logic, and has applications to automated theorem
proving and formal verification of software
Counting - Combinatorics studies the way in which discrete structures
can be combined or arranged. Enumerative combinatorics
concentrates on counting the number of certain combinatorial
objects - e.g. finding "largest", "smallest", or "optimal" objects.
Combinatorics is used frequently in computer science to obtain
formulas and estimates in the analysis of algorithms.
Graphs and trees - Graph theory, the study of graphs and networks .
In computer science, they can represent computer networks of
communication, data organization, computational devices, the flow of
computation, etc.
Iteration and recursion
-Recursion is the process a procedure goes through when one of the
steps of the procedure involves invoking the procedure itself
- Iteration - act of repeating a process or function.
-In the context of computer science, iteration & recursion is a
standard building block of algorithms and programming
Constraints
- In Project work/company people build product (hardware/software),
follow some process model like SDLC (software development life cycle)
which contain different stages – plan, analysis, design, implement, test
- Constraint is a limiting factor which limit freedom of action
- In product design architecture and implementation, constraints come
in two basic flavors technical and business.
- Technical constraints are fixed technical design decisions that
absolutely cannot be changed.
Ex- Programming language, Operating system or platforms supported,
Use of a specific library or framework
- Business constraints are unchangeable business decisions that in
some way restrict the software architecture design.
Ex- Schedule, Budget, Team composition and makeup,
- In most cases, constraints are outside of the control of engineer.
- Constraints can be viewed as requirements from an entity outside the
engineering domain.
- Design Engineers must consider a multitude of technical, economic,
social, environmental, and political constraints when they design products
and processes
- In project, all relevant constraints should be addressed and documented

Main constrains
For all types of engineering projects the main constrains are
1- Functionality
2- Efficiency & Quality
3- Reliability
4- Cost & Time
5- Legal/Ethical
6- Maintainability
7- Environmental Friendly
8- Above all Safe to operate or to use
Tools
In Project work/company people build product (hardware/software),
follow some process model like SDLC (software development life
cycle) which contain different stages – plan, analysis, design,
implement, test
- CASE tools are set of software application programs, which are used
to automate SDLC activities
- CASE stands for Computer Aided Software Engineering
- CASE tools are used by software project managers, analysts &
engineers to develop software/hardware system
- It means, development and maintenance of software projects with
help of various automated software tools
- Use of CASE tools accelerates the development of project to
produce desired result and helps to uncover flaws before moving
ahead with next stage in software development
- In projects, all the tools used should properly documented
Components of CASE Tools

Upper Case Tools - used in planning,


analysis and design stages of SDLC
Lower Case Tools - used in
implementation, testing and
maintenance
Integrated Case Tools - help in all the
stages of SDLC, from Requirement
gathering to Testing & documentation

Case Tools Types - Diagram tools, Process Modeling Tools, Project


Management Tools, Documentation Tools, Analysis Tools, Design Tools,
Configuration Management Tools, Change Control Tools, Programming Tools,
Prototyping Tools, Web Development Tools, Quality Assurance Tools,
Maintenance Tools,
Standards: Introduction
- Standards form the fundamental building blocks for product
development by establishing consistent protocols that can be
universally understood and adopted
- In projects we need to use standards to build a product
- Use of Standards can be in design or implementation stage,
hardware or software etc ( Search standards in IEEE, ISO ....etc )
- In projects, we need to address all standards followed to build the
product and properly documented
- International Standards make things work. They give worldclass
specifications for products, services and systems, to ensure quality,
safety and efficiency
- In summary standards fuel the development and implementation
of technologies that influence and transform the way we live, work
and communicate
Standard Definition:
Standards are published documents that establish specifications and
procedures designed to maximize the reliability of the materials,
products, methods, and/or services people use every day (IEEE)

Benefits of use of Standards


- Standards ensure that products and services are safe, reliable and
of good quality
- simplifies product development, and speeds time-to-market
- support consumer safety and public health
- maximize product functionality, interoperability and compatibility
- Standards also make it easier to understand and compare
competing products
- It is only through the application of standards that the credibility of
new products and new markets can be verified
IEEE Standards
As the world’s leading standards developer, IEEE is also a leading source of
information and resources on standards, their applications, and their impact on
designing new products, processes, and services.
The Institute of Electrical and Electronics Engineers Standards Association (IEEE-SA) is
an organization within IEEE that develops global standards in a broad range of
industries, including: power and energy, biomedical and health care, information
technology and robotics, telecommunication and home automation, transportation,
nanotechnology, information assurance, and many more.
In 2005, IEEE had close to 900 active standards, with 500 standards under
development. In projects, relevant standards should be searched in web & used and
properly documented
Some notable IEEE Standards committees and formats
IEEE 610 Standard Glossary of Software Engineering Terminology
IEEE 802 LAN/MAN IEEE 802.11 wireless networking wifi
IEEE 802.15.2 bluetooth IEEE 829 software test documentation
IEEE 830 software requirements specification
IEEE 1016 software design description
IEEE 1074 Software Development Life Cycle
IEEE 1233 system requirements specification
International Standards Organization (ISO)
- members from 161 countries and 3368 technical bodies to take care of
standard development
- An ISO standard is developed by a panel of experts, within a technical
committee
- ISO has formed joint committees with the International Electrotechnical
Commission (IEC) to develop standards and terminology in the areas of
electrical and electronic related technologies
- ISO has developed over 21000 International Standards and all are
included in the ISO Standards catalogue covering almost all aspects of
technology and business
ICS (International Classification for Standards) and
TC (technical committees)
ICS : field 31-Electronics; 29-Electrical engineering; 35-Information
technology
ISO/IEC JTC-1: field: Information technology
- In projects, relevant standards should be searched in web & used and
properly documented
CCE133 – Discrete Structures

Lecture Notes

Lecture 2
Sets, Relations and Functions
Set:
- Collection of objects (called elements or members)
- Sets are usually denoted by capital letters A, B, C, X, Y, Z, etc.
- elements of set can be - a, b, 1, 2, 1/2, 2.5, dog, apple,a1,a2 etc.

A={a,b,c}
aA “a belongs to A” a is an element of A
dA “d does not belongs to A” [d is not an element of A]
After we define a set, the set is a single mathematical object, and it
can be an element of another set
S = {{1,2}, {1,3}, {1,4}, {2,3}, {2,4}, {3,4}}
There are two methods of representing a set :
(i)Roster or tabular form
(ii)Set-builder form.
Roster form
- All elements of a set are listed, elements are separated by commas and are
enclosed within braces { }
- In roster form, the order in which elements are listed is immaterial.
Ex - set of all even positive integers less than 7 is described in roster form as {2, 4, 6}
set of odd natural numbers is represented by {1, 3, 5, . . .}
(The dots tell us that list of odd numbers continue infinite)
Set of vowels alphabets - {a, e, i, o, u}

Set-builder form
- All the elements of a set possess a single common property which is
not possessed by any element outside the set.

Ex- set of all even positive integers less than 7 - { x| x  N, x<7 }


set of odd natural numbers - { x| x  N, x is odd }
set of vowel alphabets - {x | x is a vowel in English alphabet}
Well known sets

Size of Set
size of a set S, denoted by |S|, is defined as number of elements contained in S
S = {2, 3, 5, 7, 11, 13, 17, 19}, then |S|=8.
S = {{1,2}, {1,3}, {1,4}, {2,3}, {2,4}, {3,4}}, then |S|=6
pow(A)

If A has n elements, then pow(A) has 2n elements


Size of set
Inverse function
CCE133 – Discrete Structures

Lecture Notes

Lecture 3
Prepositional logic,
First-order logic and Reasoning
proposition - a statement which is true or false
Ex: 2+3=7 False (preposition)
“Man is Mortal", it returns truth value “TRUE” (preposition)
"12 + 9 = 3 – 2", it returns truth value “FALSE (preposition)
"A is less than 2". (not preposition) It is because unless we give a
specific value of A, we cannot say whether the statement is true or
false

Knowledge is information (which can be expressed in the


form of propositions) from the environment
Knowledge representation is symbols used to represent
propositions
Knowledge Base is a set of sentences, each of them being
expressed in a language called the knowledge
representation language (logic)
Logic describes what is true and not how to solve problems
Facts about the world are represented as sentences in logic
• Language: has two aspects: syntax and semantics
• Method of reasoning
Syntax specifies the symbols in the language and how they can be
combined to form sentences
Semantics gives meanings to the atomic symbols of the logic
Semantic specifies how you assign a truth value to a sentence based on
its meaning in the world.

Reasoning (Inferencing)
• It is the mechanism for deriving new sentences from old ones.
• It consists of the rules for determining a subset of logical
expressions, called theorems of the logic.

There are a number of logical systems with different syntax and semantics
1. Propositional logic
2. First order predicate logic
Propositional Logic
Propositional logic is the simplest logical system.
In propositional logic the user defines a set of propositional symbols,
like P and Q.
User defines the semantics of each of these symbols. For example:
P means “Sunday is a holiday”
Q means “Today is Sunday”
Example
Precedence levels of logical operators ¬, ∧, ∨, →, ↔
Ex: What is the value expression when p=T, q=F and r=F
p∧q∨r =T∧F∨F=F∨F=F
p∨q→r =T∨F→F=T→F=F
Convert sentence into prepositional logic statement
Generally AND ( ^ )will come when and, but, since comes in sentence
OR ( v )will come when or / either comes in sentence
NOT (¬ )will come when not does not comes in sentence
IMPLY (→ )will come when following comes in sentence

Equivalence (↔ )will come when if and only if comes in sentence


Convert sentence into prepositional logic statement (contd..)
1) It is hot and sunny
A - It is hot B - It is sunny A^B
2) If it is humid then it will rain (or) It will rain if it is humid (or) It will rain only if it is
humid (or) It will rain when it is humid (or) it will rain whenever it is humid
A - It is humid B - It will rain A → B
3) Ali is boy boy(ali)
4) Cat chases mice or bird but not at same time
A - Cat chases mice B - Cat chases bird (A v B ) ^ ¬ (A ^ B )
5) Ali is math teacher but not computer teacher
A - Ali is math teacher B - Ali is computer teacher A ^ ¬ B
6) Ali is healthy and wealthy but not wise
A - Ali is healthy B - Ali is wealthy C – Ali is wise (A ^ B ) ^ ¬ C
7) If Ali brings new house then he either sell old house or took loan
A - Ali bring new house B - Ali sell old house C- Ali took loan A → (B v C)

P) If R: It is raining U: Joe takes his umbrella W: Joe gets wet


“IF it rains, THEN Joe takes his umbrella” R⟹U
“IF Joe takes an umbrella, THEN he does not get wet” U⟹ ¬W
“IF it does not rain, THEN Joe does not get wet” ¬R⟹ ¬W
Example
Example: Consider the following propositional language
P: Ahmad is happy Q: Ahmad paints a picture R: Ali is happy
Formulate the following sentences:
1. If Ahmad is happy AND paints a picture THEN Ali is not happy.
2. Ahmad is happy ONLY IF he paints a picture

Convert prepositional logic statement into sentence


Ex: Let p and q be the propositions
p : I bought a lottery ticket
q : I won million dollar jackpot
Express each of these propositions as an English sentence
(a) p ∨ q (b) p^q (c) p^ ¬ q (d) ¬ p ^ ¬ q (e) p → q (f)!p
Either I bought a lottery ticket or I won million dollar jackpot
I bought a lottery ticket and I won million dollar jackpot
I bought a lottery ticket and I do not won million dollar jackpot
I do not bought a lottery ticket and I do not won million dollar jackpot
If I bought a lottery ticket, then I won million dollar jackpot
I did not bought a lottery ticket
Predicate Logic (First order logic)
- Propositional logic deals with facts (T or F) ,
- Propositional logic cannot adequately express the meaning of all
statements in mathematics and in natural language
- Predicate Logic deals with facts / objects / relations
- Predicate Logic deals with predicates, which are propositions
containing variables
Ex: Statement “x is greater than 3” has two parts.
The first part, variable x, is subject (object) of statement
The second part—predicate, “is greater than 3”—refers to a property that the
subject of the statement can have
Ex - “x is student” -- student(x) student – predicate x – subject (object)

A predicate is an expression of one or more variables defined on some specific


domain.
A predicate with variables can be made a proposition by either assigning a value
to variable or by quantifying variable
Ex: x>5 (if x=1 false, if x=10 true)
Capital(X,saudi) (if Riyadh –true; if abha – false)
Predicates are functions of zero or more variables that return
Boolean values

U, W, R – predicate; X-variable

Jane is the mother of Mary - mother(Jane, Mary)


Mother – predicate ; Jane, Mary - constants
Atomic Formula is a predicate with zero or more arguments.
For example, 𝑈(𝑥) is an atomic formula with predicate 𝑈 and one
argument occupied by the variable 𝑥.
In general, an argument is either a variable or a constant.
Constant – fix integer or real, or character or strings. For example ‘Ali’,
‘Omer’, ‘Jane’, ‘Mary’
Variables - symbols capable of taking on any constant as value. (x)

Ground Atomic Formula


It is an atomic formula where all of its arguments are constants.
Non-ground atomic formula can have constants or variables as
formula arguments, but at least one argument must be a variable.
Note that any proposition, being an atomic formula with no
arguments, has “all arguments constant,” and is therefore a ground
atomic formula
Quantifiers
- Quantification expresses the extent to which a predicate is true over a range of
elements.
- In English, the words all, some, many, none, and few are used in quantifications.

two types of quantification:


(i) universal quantification, which tells us that a predicate is true for every (all)
element under consideration
∀x P(x) is read as for all value of x, P(x) is true
Ex: “Fruit is sweet" can be transformed into the propositional form ∀x P(x) where
P(x) is predicate which denotes x is sweet and universe of discourse is all fruit
Generally for universal ( ∀ ) statements IMPLY will come ( )

(ii) existential quantification, which tells us that there is one or more element under
consideration for which the predicate is true
∃x P(x) is read as for some values of x, P(x) is true
Ex: "Some students are good" can be transformed into the propositional form ∃x
P(x) where P(x) is predicate which denotes x is good and universe of discourse is
some students
Generally for existential ( ∃ ) statements AND will come ( ^ )
Examples of First order logic
1) john likes cricket or football
likes(john, cricket) v likes(john, football)
2) john lives in a house and the colour of the house is green
lives(john,house) ^ colour(house,green)
3)if car belongs to john then it is green
belongs(car,john) → colour(car,green)
4)john did not write JAVA
¬ write(john,JAVA)
5)all elephants are grey
x [elephants(x) → colour(x, grey)]
6)all computer students are brilliant
x [computer students(x) → brilliant(x)]
7)john likes all kinds of foods
x [food(x) → likes(john,x)]
8)everyone who is both strong and intelligent will succeed in his carrier
x [person(x) ^ strong(x) ^ intelligent(x)→ succeeds(x,carrier)]
9)there is person who wrote computer chess
X[person(x) ^ wrote(x, computer chess)]
Examples of First order logic (cont…)
10) all courses in CEdepartment are easy
x [courses(x) → CEdepartment(x, easy)]
11)chicken is food food(chicken)
12)bill eats peanuts and is still alive
eats(bill,peanuts)^alive(bill)
13)sue eats everything that bill eat
x : eats(bill,x) → eats(sue,x)
14)The last meeting of the club was at toms house
last meeting(club, toms house)
15)ali is not married ¬ married(ali)
16)Every country has exactly one ruler
x :  y: country(x) ^ruler(y)
17)A grand parent is parent of ones parent
 x :  y :  z parent(z,y)^parent(y,x) → grand parent(z,x)
18)Every one is loyal to some one
x :  y: loyal to(x,y)
19)All pompieans were romans
x : pompieans(x) → romans(x)
Examples of Predicate logic (cont…)
20) Bill takes either analysis or geometry
takes(bill, analysis) v takes(bill, geometry)
21) Bill takes analysis if and only if he does not take geometry
takes(bill, analysis) ↔ ¬ takes(bill, geometry)
22) Some students loves bill
x[student(x) ^ loves(x, bill)]
23) All students loves bill
x [student(x) → loves(x, bill)]
24) All students are smart
x [student(x) → smart(x)]
25) All graduating students are happy
x [GRstudent(x) → happy(x)]
26) All happy people smile
x [happyPeople(x) → smile(x)]
27) Someone is graduating
x[graduating (x)]
28) Someone is smiling
x[smiling (x)]
Inference (Reasoning)
Modus ponens (MP): This rule states that if a conditional statement
(‘if p then q ’) is accepted, and the antecedent (p) holds, then the
consequent (q) may be inferred
Example
Which rule of inference is used in each argument below:
1. Alice is a Math major. Therefore, Alice is either a Math major or a CSI major.
2. Jerry is a Math major and a CSI major. Therefore, Jerry is a Math major.
3. If it is rainy, then the pool will be closed. It is rainy. Therefore, the pool is closed.
4. If it snows today, the university will close. The university is not closed today.
Therefore,
it did not snow today.
5. If I go swimming, then I will stay in the sun too long. If I stay in the sun too long,
then I
will sunburn. Therefore, if I go swimming, then I will sunburn.
6. I go swimming or eat an ice cream. I did not go swimming. Therefore, I eat an ice
cream.
1. Addition
2. Simplification
3. Modus ponens
4. Modus Tollens
5. Hypothetical syllogism
6. Disjunctive syllogism
Formal Proof
A formal proof of a conclusion 𝑄 given hypotheses 𝑃1, 𝑃1, . . . , 𝑃𝑛 is a sequence of
steps, each of which applies some inference rule to hypotheses or previously
proven statements (antecedents) to yield a new true statement (the consequent).
Formal proof demonstrates that if the premises are true, then the conclusion is
true.
Formal proof is based simply on symbol manipulation (no need of thinking, just
apply rules)
CCE133 – Discrete Structures

Lecture Notes

Lecture 4
Proof Techniques
Introduction to Proofs
• To understand written mathematics, one must
understand what makes up a correct mathematical
argument, that is, a proof.
• This requires an understanding of the techniques used
to build proofs.
• The methods we will study for building proofs are also
used throughout computer science, such as the rules
computers used to reason, the techniques used to verify
that programs are correct, etc.
• Many theorems in mathematics are implications, p q.
The techniques of proving implications give rise to
different methods of proofs
What is a theorem?
Ans : A statement that can be shown true. Sometimes called facts.
What is a Proof?
Ans : Demonstration that a theorem is true.

Some Terminology
• propositions - Less important theorems
• axioms - statement that is assumed to be true
• lemma - A less important theorem that is useful to prove a theorem
• corollary - A theorem that can be proven directly from a theorem
that has been proved
• Conjecture - A statement that is being proposed to be a true
statement
• trivial proof - A proof that p → q is true based on the fact that q is
true
Some Basic Definitions
Rational Numbers
- A number is rational if we can write it as a fraction where top number of the
fraction and bottom number are both whole numbers with no common factors
- Every whole number, including negative numbers and zero, is a rational number.
This is because every whole number ‘n’ can be written in the form n/1
Examples of Rational Numbers - 8 (8/1) , 3/4, 1.5 (3/2), 0.333... (1/3)
Recurring decimals such as 0.26262626…, all integers and all finite decimals, such
as 0.241, are also rational numbers

Irrational Numbers
- An irrational number is any number that is not rational.
- It is a number that cannot be written as a fraction of two integers
- An irrational number has endless non-repeating digits to the right of the decimal
point
Examples of irrational number - √2 (1.414….) , π (3.141592…), e (2.718… )

Real number – set of all rational and irrational numbers


Even numbers
- A number n is even if there exist a number k, such that n = 2k where k is integer
i.e An even number is any number that can be divided by 2 (last digit is 0,2,4,6,or 8)
examples of even numbers – 6, 58, 44884, 998632, 10000000
Odd numbers
- A number n is odd if there exist a number k, such that n = 2k + 1 where k is integer
i.e An odd number n divided by 2, always get a quotient k with a remainder of 1.
Having a remainder of 1 means that n cannot in fact be divided by 2
(last digit is 1, 3, 5, 7 or 9)
examples of odd numbers – 7, 59, 44885, 998631
Basic operations with even and odd numbers
Addition Rule Subtraction Rule
even + even = even 4 + 2 = 6 even − even = even 8 − 4 = 4
even + odd = odd 6 + 3 = 9 even − odd = odd 8−5=3
odd + odd = even 13 + 13 = 26 odd − odd = even 9−5=4
Multiplication Rule
even × even = even 2 × 6 = 12
even × odd = even 8 × 3 = 24
odd × odd = odd 3 × 5 = 15
Methods of Proving Theorems
Direct Proofs
First assume p is true
Then show q must be true (using axioms, definitions)
So the combination of p is true and q is false never occurs
Thus p → q is true
Example - Prove that the sum of two odd integers is even.
Proof: Let m and n be two odd integers.
Then by definition of odd numbers m = 2k + 1 for some k Z
n = 2l + 1 for some l  Z
Now m + n = (2k + 1) + (2l + 1)
= 2k + 2l + 2 = 2 (k + l + 1) = 2r where r = (k + l + 1) Z
Hence m + n is even. Hence Proved

Example - The product of two odd numbers is odd


Proof x = 2m+1, y = 2n+1
xy = (2m+1)(2n+1) = 4mn + 2m + 2n + 1 = 2(2mn+m+n) + 1  odd
Example - If m and n are perfect square, then m+n+2√(mn) is a
perfect square
Proof: m = a2 and n = b2 for some integers a and b
Then m + n + 2√(mn) = a2 + b2 + 2ab
= (a + b)2 is perfect square Hence proved

Example - Give a direct proof that if m and n are both perfect


squares, then nm is also a perfect square.
Proof: let r and s are two integers, then m=r² and n=s²,
Now m . n= r² s² = (r s)² = t² where t = r s Hence proved

Example - Use a direct proof to show that the product of two


rational numbers is rational.
Proof: First number x = a/b, b ≠0 Second number y =c/d, d≠0
x*y = a*c/b*d since b ≠0 and d≠0 then b*d≠0.
Let a*c = l and b*d = m so, x = l/m
Hence x is rational number. Hence Proved
Proof by Contraposition
A proof that p → q is true that proceeds by showing that p must be
false when q is false. p → q ≡ !q → !p
( Prove contrapositive, i.e. prove “not Q implies not P” )
It is an indirect proofs
Example - Prove that if n is an integer and 3n+2 is odd, then n is odd
Proof: Assume that n is even (negation).
So, n can be expressed as 2k for some integer k; n = 2k
Therefore, 3n + 2 = 3(2k) + 2 = 6k + 2 = 2(3k + 1) = 2 x  even
So 3n + 2 is even. Hence proved

Example - If r is irrational, then √r is irrational


Proof: We shall prove contrapositive – “if √r is rational, then r is rational”
Since √r is rational, √r = a/b for some integers a,b
r = a2/b2. Since a,b are integers, a2,b2 are integers
Therefore, r is rational
Example - Prove that If n2 is even, then n is even
Proof: Assume that n is odd (negation).
So, n can be expressed as 2k+1 for some integer k; n = 2k+1
Therefore, n2 = (2k+1)2 = (2k)2 + 2(2k) + 1 = 2(2k2 + 2k) + 1
So 2x + 1 is odd. Hence proved

Proof by Contradiction
A proof that p is true based on the truth of the conditional statement
!p → q, where q is contradiction
Example - Give a proof by contradiction of theorem “If 3n + 2 is odd, then n
is odd.”
Let p= 3n + 2 is odd q= n is odd
To construct a proof by contradiction, assume that both p and ¬q are true
That is, assume that 3n + 2 is odd and that n is not odd
Because n is not odd (means even), n = 2k
3n + 2 = 3(2k) + 2 = 6k + 2 = 2(3k + 1) = 2t is even = ¬p
both p and ¬p are true, we have contradiction
This completes proof by contradiction, proving if 3n + 2 is odd, then n is odd
Example - Prove √2 is irrational using contradiction
Suppose √2 was rational
Choose m, n integers without common factors (always true)
√2 = m /n √2 n=m 2n2=m2 (a is even if and only if a2 is even)
so m is even
m=2l √2 n=2l 2 n2=4l2 n2=2l2 so n is even
m and n are both even, thus have a common factor 2, a contradiction!

Example - Show that if n is an integer and n³+5 is odd , then n is even using
a proof by contradiction
Let p= n³+5 is odd and q= n is even
assume that both p and ¬q are true
So n is odd n=2k-1 for some integer k
n³+5 = (2k-1)³+ 5 = (4k²+4k+1) (2k-1) + 5 = 8k³- 4k²+ 8k²-4k + 2k – 1 + 5 =
8k³+4k²- 2k + 4 =2(4k³+2k-k+1) =2t is even (where t= 4k³+2k²- k + 1 ) = ¬p
both p and ¬p are true, we have contradiction
We conclude that for every integer n, if n³ +5 is odd, then n is even. Hence
Proved
Proof by Cases
- Sometimes easiest way to prove a theorem is to split it into
several cases.
- the original conditional statement p → q with a hypothesis p
made up of a disjunction of propositions p1, p2, . . . , pn can be
proved by proving each of the n conditional statements pi → q,
i = 1, 2, . . . , n, individually.

Exhaustive Proof
- Some theorems can be proved by examining a relatively small
number of examples. Such proofs are called exhaustive proofs, or
proofs by exhaustion because these proofs proceed by exhausting
all possibilities.
- An exhaustive proof is a special type of proof by cases where each
case involves checking a single example
Example - Prove that (n + 1)3 ≥ 3n if n is a positive integer with n ≤ 4
We use a proof by exhaustion.
n = 1, (n + 1)3 = (1 + 1)3 = (2)3 = 8 and 3n = 31 = 3
n = 2, (n + 1)3 = (2 + 1)3 = (3)3 = 27 and 3n = 32 = 9
n = 3, (n + 1)3 = (3 + 1)3 = (4)3 = 64 and 3n = 33 = 27
n = 4, (n + 1)3 = (4 + 1)3 = (5)3 = 125 and 3n = 34 = 81
In each of these four cases, we see that (n + 1)3 ≥ 3n for n ≤ 4

Example - Prove that if n is an integer, then n2 ≥ n


Solution: We can prove that n2 ≥ n for every integer by considering three cases,
when n = 0, when n ≥ 1, and when n ≤ −1
Case (i): When n = 0, because 02 = 0, we see that 02 ≥ 0. It follows that n2 ≥ n is true
Case (ii): When n ≥ 1, multiply both sides by n, we get n · n ≥ n · 1
This implies that n2 ≥ n
Case (iii): In this case n ≤ −1, square both sides, However, n2 ≥ 0.
It follows that n2 ≥ n.
Because the inequality n2 ≥ n holds in all three cases,
we can conclude that if n is an integer, then n2 ≥ n.
Example - Let n be an integer. Prove that 9n2 + 3n - 2 is even
Case 1: n is even
The product of an even integer and any integer is even. Since n is
even, 9n2 and 3n are even too.
Thus 9n2 + 3n -2 is even because it is sum of three even integers.
Case 2: n is odd
The product of odd integers is odd. In this case, since n is odd, 9n2 and
3n are also odd. The sum of two odd integers is even.
Thus 9n2 + 3n is even.
sum of even integers is even, so 9n2 + 3n -2 = (9n2 + 3n) + (-2) is even
CCE133 – Discrete Structures

Lecture Notes

Lecture 5
Basics of Counting
Basic Counting Principles
Counting problems are of the following kind:
“How many different 8-letter passwords are there?”
“How many possible ways are there to pick 11 soccer players out of a
20-player team?”
For solving these problems, mathematical theory of counting are used.

- Counting mainly encompasses fundamental counting rule, the


permutation rule, and the combination rule
- We can phrase many counting problems in terms of ordered or
unordered arrangements of the objects of a set with or without
repetitions. These arrangements, called permutations and
combinations, are used in many counting problems
Importance of counting
- Counting subject was studied long, when combinatorial questions
arose in study of gambling games. Enumeration, the counting of
objects with certain properties, is an important part of combinatorics
- 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.
- counting has played a key role in mathematical biology, especially in
sequencing DNA.
- counting techniques are used extensively when probabilities of
events are computed
- Another problem in combinatorics involves generating all the
arrangements of a specified kind. This is often important in computer
simulations.
The Rules of Sum and Product
The Rule of Sum and Rule of Product are used to decompose difficult
counting problems into simple problems.

The Rule of Sum:


If we have tasks T1, T2, …, Tm that can be done in n1, n2, …, nm ways,
respectively, and no two of these tasks can be done at the same time,
then there are n1 + n2 + … + nm ways to do one of these tasks.
If we consider two tasks A and B which are disjoint (i.e. A ∩ B = Ø),
then mathematically |A ∪ B| = |A| + |B|

The Rule of Product:


If we have a procedure consisting of sequential tasks T1, T2, …, Tm that
can be done in n1, n2, …, nm ways, respectively, then there are n1 x n2 x
… x nm ways to carry out the procedure.
Mathematically, if a task B arrives after a task A, then |A×B| = |A|×|B|
Example: Babu has 3 pants and 2 shirts. How many different pairs of
a pant and a shirt, can he dress up with?
pant can be chosen in 3 ways, shirt can be chosen in 2 ways.
For every choice of a pant, there are 2 choices of a shirt.
Therefore, there are 3 × 2 = 6 pairs of a pant and a shirt.
Example: Sabnam has 2 school bags, 3 tiffin boxes and 2 water
bottles. In how many ways can she carry these items
there are 2 × 3 = 6 pairs of school bag and a tiffin box.
For each of these pairs a water bottle can be chosen in 2 ways.
Hence, there are 6 × 2 = 12 total different ways
Let there be ‘n’ different elements.
There are n number of ways to fill up first place.
After filling first place, (n-1) number of elements is left.
Hence, there are (n-1) ways to fill up the second place.
After filling the first and second place, (n-2) number of
elements is left.
Hence, there are (n-2) ways to fill up the third place.
We can now generalize
number of ways to fill = n(n–1)(n–2) -----
If the repetition of the letters was allowed, how many words can be
formed?
One can easily understand that each of the 4 vacant places can be filled in
succession in 4 different ways.
Hence, the required number of words = 4 × 4 × 4 × 4 = 256
Example: How many 2 digit even numbers can be formed from the
digits 1, 2, 3, 4, 5 if the digits can be repeated?
unit’s place should be even number (0,2,4,6,8)
here we have two options 2 and 4 and this can be done in 2 ways;
ten’s place can be filled by any of the 5 digits in 5 ways
number of two digits even numbers is 2 × 5, i.e., 10
Example: Find the number of different signals that can be generated
by arranging at least 2 flags in order (one below the other) on a
vertical staff, if five different flags are available.
A signal can consist of either 2 flags, 3 flags, 4 flags or 5 flags
2 flag signals = 5 × 4 = 20
3 flag signals = 5 × 4 x 3 = 60
4 flag signals = 5 × 4 x3 x2 = 120
5 flag signals = 5 × 4 x3 x2 x1= 120
the total required no of signals = 20 + 60 + 120 + 120 = 320
Example: The department will award a free computer to either a CS
student or a CS professor. How many different choices are there, if there
are 530 students and 15 professors?
There are 530 + 15 = 545 choices (Rule of Sum)

Example: How many different license plates are there that containing
exactly three English letters ?
There are 26 possibilities to pick the first letter, then 26 possibilities for the
second one, and 26 for the last one.
So there are 262626 = 17576 different license plates. (Rule of Product)

Example: A boy lives at X and wants to go to School at Z. From his home X


he has to first reach Y and then Y to Z. He may go X to Y by either 3 bus
routes or 2 train routes. From there, he can either choose 4 bus routes or 5
train routes to reach Z. How many ways are there to go from X to Z?
From X to Y, he can go in 3+2=5 ways (Rule of Sum)
Thereafter, he can go Y to Z in 4+5 = 9 ways (Rule of Sum)
Hence from X to Z he can go in 5×9 =45 ways (Rule of Product)
The Inclusion-Exclusion principle
The Inclusion-exclusion principle computes the cardinal number of the union
of multiple non-disjoint sets.
For two sets A and B, the principle states: |A ∪B| = |A| + |B| – |A∩B|

Example: How many integers from 1 to 50 are multiples of 2 or 3 but not both
From 1 to 50, there are 50/2=25 numbers which are multiples of 2
There are 50/3=16 numbers which are multiples of 3
There are 50/6=8 numbers which are multiples of both 2 and 3
So, |A|=25, |B|=16 and |A∩B|= 8 |A∪B|=|A|+|B|–|A∩B|=25+16–8= 33

Example: In a group of 50 students 24 like cold drinks and 36 like hot drinks
and each student likes at least one of the two drinks. How many like both
coffee and tea?
Let X be the set of students who like cold drinks and Y be the set of people
who like hot drinks.
So, | X ∪ Y | = 50, |X| = 24, |Y| = 36
|X∩Y| = |X| + |Y| – |X∪Y| = 24 + 36 – 50 = 60 – 50 = 10
Hence, there are 10 students who like both tea and coffee
Example: How many bit strings of length 8 either start with a 1 or end with 00
Construct a string of length 8 that starts with a 1
There is one way to pick the first bit (1),
two ways to pick the second bit (0 or 1),
two ways to pick the third bit (0 or 1) . . . .
Product rule: Task 1 can be done in 127 = 128 ways
Construct a string of length 8 that ends with 00
There are two ways to pick the first bit (0 or 1),
two ways to pick the second bit (0 or 1) …
one way to pick the seventh and eighth bit (0)
Product rule: Task 2 can be done in 26 = 64 ways
Construct a string of length 8 start with 1 and end with 00
There is one way to pick the first bit (1),
two ways for the second, …, sixth bit (0 or 1),
one way for the seventh, eighth bit (0).
Product rule: In 25 = 32 cases, Tasks 1 and 2 are carried out at the same time.
number of ways to do either task = 128 + 64 – 32 = 160
Permutations
- A permutation is an arrangement of some elements in which order matters.
- In other words a Permutation is an ordered Combination of elements

Example: From a set S ={x, y, z} by taking two at a time,


all permutations are: xy, yx, xz, zx, yz, zy (xy ≠ yx order matter)

Example: We have to form a permutation of three digit numbers from a set of


numbers S= {1, 2, 3}.
Different three digit numbers will be formed when we arrange the digits.
The permutation will be = 123, 132, 213, 231, 312, 321 (123 ≠ 321 order matter)

Factorial
𝑛! = 1.2.3. … . . (𝑛 − 1). 𝑛
we have 0!=1 n! = n × (n – 1) !

Number of Permutations
The number of permutations of ‘n’ different things taken ‘r’ at a time is denoted by

p(n,r) =
Example: determine the number of 3-letter words, with or without
meaning, which can be formed out of the letters of the word
NUMBER,
where the repetition of the letters is not allowed, we need to count
the arrangements NUM, NMU, MUN, NUB, ..., etc.
Here, we are counting the permutations of 6 different letters taken
3 at a time.
The required number of words = 6 × 5 × 4 = 120 (by using
multiplication principle).

If the repetition of the letters was allowed, the required number of


words would
be 6 × 6 × 6 = 216.
Example: Suppose we have to find the number of ways of
rearranging the letters of the word ROOT
In this case, the letters of the word are not all different. There are 2 Os,
which are of the same kind. Let us treat, temporarily, the 2 Os as different,
say, O1 and O2.
number of permutations of 4-different letters, in this case, taken all at a
time is 4!
Consider one of these permutations say, RO1O2T. Corresponding to this
permutation, we have 2 ! permutations RO1O2T and RO2O1T which will be
exactly same permutation if O1 and O2 are not treated as different, i.e., if
O1 and O2 are the same O at both places.
Example: find the number of ways of rearranging the letters of the word
INSTITUTE.
In this case there are 9 letters, in which I appears 2 times and T appears 3
times.

Example: How many 4-digit PIN numbers can be formed by using the
digits 1 to 9 if repetition of digits is not allowed?
Here order matters for example 1234 and 1324 are two different PIN
numbers.
Therefore, there will be as many 4 digit numbers as there are
permutations of 9 different digits taken 4 at a time.
Example: How many numbers lying between 100 and 1000 can be
formed with digits 0, 1, 2, 3, 4, 5, if repetition of digits is not allowed?
Every number between 100 and 1000 is a 3-digit number.
We, first, have to count permutations of 6 digits taken 3 at a time.
This number would be 6P3
But, these permutations will include those also where 0 is at the 100’s
place.
For example, 092, 042, . . ., etc are actually 2-digit numbers
To get the number of such numbers, we fix 0 at the 100’s place and
rearrange the remaining 5 digits taking 2 at a time. This number is 5P2
Example: Find number of different 8-letter arrangements that can be made
from the letters of the word DAUGHTER so that
(i) all vowels occur together (ii) all vowels do not occur together.
(i) There are 8 different letters in the word DAUGHTER, in which there
are 3 vowels, namely, A, U and E.
Since the vowels have to occur together, we can for the time being, assume them as a
single object (AUE). This single object together with 5 remaining letters (objects) will
be counted as 6 objects.
Then we count permutations of these 6 objects taken all at a time.
This number would be 6P6 = 6!
Corresponding to each of these permutations, we shall have 3! permutations of the
three vowels A, U, E taken all at a time .
Hence, by multiplication principle, required number of permutations = 6 ! × 3 ! =4320
(ii) If we have to count those permutations in which all vowels are never
together,
we first find all possible arrangements of 8 letters taken all at a time, which can be
done in 8! ways.
Then, we subtract from this number, the number of permutations in which the vowels
are always together.
Therefore, the required number = 8 ! – 6 ! × 3 ! = 6 ! (7×8 – 6) = 2 × 6 ! (28 – 3)
= 50 × 6 ! = 50 × 720 = 36000
Example: In how many ways can 4 red, 3 yellow and 2 green discs be
arranged in a row if the discs of the same colour are
indistinguishable ?
Total number of discs are 4 + 3 + 2 = 9.
Out of 9 discs, 4 are of the first kind (red), 3 are of the second kind
(yellow) and 2 are of the third kind (green).

Example : Find the number of arrangements of the letters of the


word INDEPENDENCE. In how many of these arrangements,
(i) do the words start with P
(ii) do all the vowels always occur together
(iii) do the vowels never occur together
(iv) do the words begin with I and end in P?
Example : From a bunch of 6 different cards, how many ways we can
permute it?
As we are taking 6 cards at a time from a deck of 6 cards, the permutation
will be 6P6 = 6! = 720

Example : In how many ways can the letters of the word 'READER' be
arranged?
There are 6 letters word (2 E, 1 A, 1D and 2R.) in the word 'READER'
The permutation will be = 6! / [(2!) (1!)(1!)(2!)] = 180

Example : In how ways can the letters of the word 'ORANGE' be arranged
so that the consonants occupy only the even positions?
There are 3 vowels and 3 consonants in the word 'ORANGE'
Number of ways of arranging consonants among themselves
= 3P3 = 3! = 6 ways
remaining 3 vacant places will be filled up by 3 vowels in 3P3 = 3! = 6 ways
Hence, the total number of permutation is 6×6=36
Combinations
- A combination is selection/ choices of some given elements in which
order does not matter
Example: Let us now assume that there is a group of 3 tennis players X, Y, Z.
A team consisting of 2 players is to be formed. In how many ways can we
do so?
Is the team of X and Y different from the team of Y and X ?
Here, order is not important.
In fact, there are only 3 possible ways in which team could be constructed
These are XY, YZ and ZX . Here, each selection is called a combination of 3
different objects taken 2 at a time. In a combination, the order is not
important

The number of all combinations of n things, taken r at a time


is: c(n,r) =
Example: Suppose we have 4 different objects A, B, C and D. Taking 2 at a
time, if we have to make combinations, these will be AB, AC, AD, BC, BD, CD.
Here, AB and BA are same combination as order does not alter the
combination. This is why we have not included BA, CA, DA, CB, DB and DC in
this list. There are as many as 6 combinations
of 4 different objects taken 2 at a time, i.e., 4C2 = 6.

Example: Find the number of subsets of the set {1, 2, 3, 4, 5, 6} having 3


elements.
The cardinality of the set is 6 and we have to choose 3 elements from set.
Here, the ordering does not matter.
Hence, the number of subsets will be 6C3=20

Example: There are 6 men and 5 women in a room. In how many ways we
can choose 3 men and 2 women from the room?
The number of ways to choose 3 men from 6 men is 6C3
the number of ways to choose 2 women from 5 women is 5C2
Hence, the total number of ways is: 6C3 ×5C2=20×10=200
Example : How many ways can you choose 3 distinct groups of 3 students
from total 9 students?
Let us number the groups as 1, 2 and 3
For choosing 3 students for 1st group, number of ways: 9C3
number of ways for choosing 3 students for 2nd group after choosing 1st group: 6C3
number of ways for choosing 3 students for 3rd group after choosing 1st and 2nd
group: 3C3
Hence, the total number of ways = 9C3 ×6C3 × 3C3 = 84×20×1 =1680

Example: A committee of 3 persons is to be constituted from a group of 2 men and


3 women. In how many ways can this be done? How many of these committees
would consist of 1 man and 2 women?
Here, order does not matter. Therefore, we need to count combinations.
There will be as many committees as there are combinations of 5 different persons
taken 3 at a time

Now, 1 man can be selected from 2 men in 2C1 ways and 2 women can be
selected from 3 women in 3C2 ways. Therefore, the required number of committees
Example: What is the number of ways of choosing 4 cards from a pack of 52
playing cards? In how many of these
(i) four cards are of the same suit, (ii) four cards belong to four different suits,
(iii) are face cards, (iv) two are red cards and two are black cards,
(v) cards are of the same colour?
Pigeonhole Principle
if there are more pigeons than pigeonholes, then there must be at least one
pigeonhole with at least two pigeons in it
If (k + 1) or more objects are placed into k boxes, then there is at least one box
containing two or more of the objects.
If N objects are placed into k boxes, then there is at least one box containing
at least N/k of the objects.
Example:
1. Ten men are in a room and they are taking part in handshakes. If each
person shakes hands at least once and no man shakes the same man’s hand
more than once then two men took part in the same number of handshakes.
2. There must be at least two people in a class of 30 whose names start with
the same alphabet.
Example: Among any group of 367 people, there must be at least two with
the same birthday, because there are only 366 possible birthdays.
Example: In any group of 27 English words, there must be at least two that
begin with same letter, because there are 26 letters in the English alphabet
Example: 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?
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.

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
|N/5|=6
The smallest such integer is N = 5 · 5 + 1 = 26
CCE133 – Discrete Structures

Lecture Notes

Lecture 6
Discrete Probability
In computer science, probability theory plays an important role in
the study of the complexity of algorithms

Experiment - procedure that yields one of a given set of possible


outcomes
Sample space (S) - set of possible outcomes of experiment
Event (E)- subset of sample space

Def: Probability

The probability of an event can never be negative or more


than one, so probability will be { 0.0 - 1.0 }
EXAMPLE : A bag contain four blue balls and five red balls. What is
the probability that a ball chosen at random from the bag is blue?
Expt: choosing a ball from bag
Sample space: 9 [total nine possible outcomes - 4 blue ball +5 red ball ]
Event: 4 [possible outcomes produce blue ball ]
Hence, the probability that a blue ball is chosen is 4/9

EXAMPLE : A coin is successively tossed three times. Find the


probability of getting exactly one head or two heads
Expt: coin is tossed three times
Sample space: 8 [total nine possible outcomes – 2x2x2 ]
[ HHH, HHT, HTH, HTT, THH, THT, TTH, TTT ]
Event: 6 [possible outcomes to produce one head or two heads
HTT, THT, TTH, HHT, THH, HTH ]
Hence, the probability that a blue ball is chosen is 6/8 = 3/4
EXAMPLE: What is the probability that when two dice are rolled, the
sum of the numbers on the two dice is 7?
One die has six possible outcomes,
total number of outcomes when two dice are rolled is 6 x 6 = 36
(product rule)
successful outcomes are (1, 6), (2, 5), (3, 4), (4, 3), (5, 2), and (6, 1) = 7
Hence, the probability that a seven comes up when two fair dice are
rolled is 6/36 = 1/6

EXAMPLE: In a lottery, players win a large prize when they pick four
digits that match, in the correct order, four digits selected by a random
mechanical process. A smaller prize is won if only three digits are
matched. What is the probability that a player wins the large prize?
What is the probability that a player wins the small prize?
There is only one way to choose all four digits correctly.
By the product rule, there are 10x10x10x10 = 10,000 ways to choose
four digits.
Hence, the probability that a player wins the large prize is 1/10,000 =
0.0001
To count the number of successes with the first digit incorrect, note
that there are nine possible choices for the first digit (all but the one
correct digit), and one choice for each of the other digits, namely, the
correct digits for these slots. Hence, there are nine ways to choose
four digits where the first digit is incorrect, but the last three are
correct.
Similarly, there are nine ways to choose four digits where the second
digit is incorrect, nine with the third digit incorrect, and nine with the
fourth digit incorrect. Hence, there is a total of 36 ways to choose four
digits with exactly three of the four digits correct.
Thus, the probability that a player wins the smaller prize is 36/10,000
= 9/2500 = 0.0036
Example: What is the probability that the numbers 11, 4, 17, 39, and
23 are drawn in that order from a bag containing 50 balls labeled
with the numbers 1, 2, . . . , 50 if (a) the ball selected is not returned
to the bag before the next ball is selected and (b) the ball selected is
returned to the bag before the next ball is selected?
(a) By the product rule, there are 50 X 49 X 48 X 47 X 46 =
254,251,200 ways to select the balls because each time a ball is
drawn there is one fewer ball to choose from.
Consequently, the probability that 11, 4, 17, 39, and 23 are drawn in
that order is 1/254,251,200.
This is an example of sampling without replacement.
(b) By the product rule, there are 50X50X50X50X50 = 312,500,000
ways to select the balls because there are 50 possible balls to choose
from each time a ball is drawn.
Consequently, the probability that 11, 4, 17, 39, and 23 are drawn in
that order is 1/312,500,000.
This is an example of sampling with replacement.
EXAMPLE

EXAMPLE
EXAMPLE

EXAMPLE
EXAMPLE
Probabilities of Complements and Unions of Events

Example: A sequence of 10 bits is randomly generated. What is the


probability that at least one of these bits is 0?
Example: What is the probability that a positive integer selected at
random from the set of positive integers not exceeding 100 is
divisible by either 2 or 5?
Example: What is the probability that a card selected at
random from a standard deck of 52 cards is an ace?
There are 52 equally likely cards to be selected, and 4 of
them are aces.
Therefore the probability is 4/52 = 1/13 = 7.73

Example: What is the probability that when a coin is flipped


six times in a row, it lands heads up every time
There are 2x2x2x2x2x2 = 64 possible outcomes,
represented by all the sequences of length 6 of H's and T's.
Only one of those sequences, HHHHHH, represents the
event under consideration,
so the probability is 1/64 = 0.016
Example: What is the probability that a positive integer not
exceeding 100 selected at random is divisible by 3
We need to count the number of positive integers not exceeding
100 that are divisible by 3
= |100/3| = 33
so the probability is 33/100 = 0.33

Example: What is the probability that a positive integer not


exceeding 100 selected at random is divisible by 5 or 7
not exceeding 100 that are divisible by 5 = |100/5| = 20
not exceeding 100 that are divisible by 7 = |100/7| = 14
we have counted the numbers 35 and 70 twice, since they are
divisible by both 5 and 7 (i.e., divisible by 35 ). Therefore there are
20 + 14 - 2 = 32 such numbers.
so the probability is 32/100 = 8/25 = 0.32
P) A box contains 5 green, 4 yellow and 3 white balls. Three balls are drawn at
random. (a) What is the probability that they are green colour
(b) What is the probability that they are not of same colour

P) There are 10 boys and 5 girls in a class. Three students are selected at random.
What is the probability that one girl and two boys are selected

P) Two cards are drawn from a pack of 52 cards. (a) what is the probability that one
is club and other is diamond (b) what is the probability that both cards are kings
Conditional Probability – It is the probability of occurring of event E
when another event F is known to have already happened

Example: A bit string of length four is generated at random so that each of the 16
bit strings of length four is equally likely. What is the probability that it contains at
least two consecutive 0s, given that its first bit is a 0? (We assume that 0 bits and 1
bits are equally likely.)
Example: What is the conditional probability that a family with two
children has two boys, given they have at least one boy? Assume that
each of the possibilities BB, BG, GB, and GG is equally
likely, where B represents a boy and G represents a girl. (Note that
BG represents a family with an older boy and a younger girl while GB
represents a family with an older girl and a younger boy.)
Example : A family has two children. What is the probability that
both the children are boys given that at least one of them is a boy ?
Example : Ten cards numbered 1 to 10 are placed in a box, mixed up
thoroughly and then one card is drawn randomly. If it is known that
the number on the drawn card is more than 3, what is the probability
that it is an even number
Example: Bag I contains 3 red and 4 black balls while another Bag II contains 5 red
and 6 black balls. One ball is drawn at random from one of the bags and it is found
to be red. Find the probability that it was drawn from Bag II
Example: Given three identical boxes I, II and III, each containing two coins. In box I,
both coins are gold coins, in box II, both are silver coins and in the box III, there is
one gold and one silver coin. A person chooses a box at random and takes out a
coin. If the coin is of gold, what is the probability that the other coin in the box is
also of gold?
Example: In a factory which manufactures bolts, machines A, B and C manufacture
respectively 25%, 35% and 40% of the bolts. Of their outputs, 5, 4 and 2 percent
are respectively defective bolts. A bolt is drawn at random from the product and is
found to be defective. What is the probability that it is manufactured by the
machine B?
Random Variables and probability distributions
A random variable is a real valued function whose domain is the sample space of a
random experiment.
The probability distribution of a random variable X is the system of numbers

Example: Suppose that a coin is tossed twice so that the sample space is S =
{HH,HT,TH,TT}. Let X represent the number of heads that can come up. So with
each sample point we can associate a number for X so for HH the value of X is 2 as
in HH there are 2 heads, for HT the value of X is 1 as there is only one head in HT.
similarly for TH, X=1 and for TT value of X = 0 as there are no heads in TT
Example: A coin is biased so that the head is 3 times as likely to occur as tail. If the
coin is tossed twice, find the probability distribution of number of tails.
Expected Value (Mean) and Variance of a Random variable
Example: Suppose that a coin is tossed twice so that the sample space is S =
{HH,HT,TH,TT}
Let X represent the number of heads that can come up
The probability distribution of a random variable X (see before example)
Example: In a meeting, 70% of the members favour and 30% oppose a
certain proposal. A member is selected at random and we take X = 0 if he
opposed, and X = 1 if he is in favour. Find E(X) and Var(X).

It is given that P(X = 0) = 30% = 30/100 = 0.3


P(X=1)=70%= 70/100 = 0.7
Therefore, the probability distribution is as follows.
Example: The mean of the numbers obtained on throwing a dice having written
1 on three faces, 2 on two faces and 5 on one face is (A) 1 (B) 2 (C) 5 (D) 8/3

Let X be the random variable representing a number on the dice.


The total number of observations is six.
∴ P(X=1)= 3/6 = 1/2
P(X=2)= 2/6 = 1/3
P(X=5)= 1/6
∴ The probability distribution is as follows.
CCE133 – Discrete Structures

Lecture Notes

Lecture 7
Graphs and Search Techniques
Graphs
Definition: A graph consists of a non-empty set of vertices or nodes V
and a set of edges E
Graph is denoted as G = (V, E)
Example: Let us consider, a Graph is G = (V, E)
where V = {a, b, c, d}
E = { ab,ac,bc,cd}
Nodes - vertices or end points in graph (a,b,c,d)
Edge – line joining two nodes or endpoints in graph (ab, ac,bc,cd)
Importance of graph theory
- We can determine whether two computers are connected by a
communications link using graph models of computer networks.
- Graphs with weights assigned to their edges can be used to solve
problems such as finding the shortest path between two cities in a
transportation network.
Loop- an edge that joins a vertex to itself
(a join to a)
multiple edges - if there is more than one edge
joining two vertices ( CD)

Directed and Undirected Graph


- A graph is directed graph if edge set is made of ordered vertex pair
In Fig below, Edges are ab (not ba), bc (not cb), ca (not ac)
- A graph is undirected if edge set is made of unordered vertex pair
In Fig below, Edges are ab, bc, ac (here edges ab & ba are same, ac &
ca are same, bc & cb are same)
Example: determine whether the graph shown has directed or
undirected edges, whether it has multiple edges, and whether it has
one or more loops
G – undirected graph
One loop (b), No multiple edges

H - directed graph
No loop, two multiple edges (ab, bd)
Adjacent nodes and degree of undirected graph
Two vertices u and v in an undirected graph G are called adjacent (or
neighbors) in G if u and v are endpoints of an edge e.
Such an edge e is called incident with vertices u and v and
e is said to connect u and v
a, b are adjacent nodes (connected by edge ab)
a, c are adjacent nodes

Degree of Vertex
The degree of a vertex V of a undirected graph G (denoted by deg (V)) is the
number of edges incident with vertex V.
In above graph; deg(a)=2, deg(b)=2, deg(c)=3, deg(d)=1, deg(e)=0
pendant vertex – vertex with degree one; (d)
isolated vertex - vertex of degree zero; (e)
An isolated vertex is not adjacent to any vertex
A vertex having loop has additional degree 2 for the loop
Degree of Graph
The degree of a graph is the largest vertex degree of that graph.
For above graph the degree of the graph is 3.
The Handshaking Lemma:
In a graph, sum of all degrees of all vertices is equal to twice
the number of edges

Example: How many edges are there in a graph with 10


vertices each of degree six?
Because the sum of the degrees of the vertices is 6 ・ 10 =
60, it follows that 2m = 60
where m is the number of edges. Therefore, m = 30
Example: (a) List number of vertices (nodes) and edges of graph G
(b) What are degrees of vertex (c) what are neighborhoods (adjacent)
of vertices in graphs G (d) what is degree of graph
(e) Show sum of degs=2edges
(f) list pendent vertex
(g) list isolated vertex

Vertices = {a,b,c,d,e,f,g,h} = 8
Edges = {aa,ab, af, bc,be,bf, cd,cd,ce,cf,ef,eg} = 12
deg(a)=deg(b)=deg(f)= deg(e)= 4,deg(c)=5,deg(d)=2,deg(g)=1,deg(h)=0
degree of graph = MAX deg = 5
The neighborhoods (adj)of vertices are N(a) = {a,b, f },
N(b) = {a, c, e, f }, N(c) = {b, d, e, f }, N(d) = {c}, N(e) = {b, c, f },
N(f ) = {a, b, c, e}, N(g) ={e} N(h) = ∅
sum of degs=4x4+5+2+1=24 2edges =2x12=24
pendent vertex =vertex with deg(1) =g
isolated vertex =vertex with deg(0) =h
Adjacent nodes and degree of directed graph
When (u, v) is an edge of graph G with directed edges,
u is said to be adjacent to v and v is said to be adjacent from u.
The vertex u is called initial vertex of (u, v),
v is called terminal or end vertex of (u, v).
The initial vertex (w) and terminal vertex (w) of a loop
are same
In a graph with directed edges
in-degree of a vertex v, deg−(v), is number of edges with v as their
terminal vertex. [deg−(u)= 1,deg−(v)=1, deg−(w)=2 ]
out-degree of v, deg+(v), is number of edges with v as their initial
vertex. [deg+(u)= 1,deg+(v)=1, deg+(w)=2 ]
(Note that a loop at a vertex contributes 1 to both the in-degree
and the out-degree of this vertex)
sum of in-degrees = sum of out-degrees = number of edges
(above graph, 4)
Example: (a) List the number of vertices and edges of graph G
(b) Find in-degree and out-degree of each vertex in the graph G
with directed edges
(c) Show sum of in-degree =
sum of out-degree = edges

Vertices = {a,b,c,d,e,f} = 6
Edges = {aa, ab, ac, ae, bd, cb,cc, dc,de, ea,ed,ee} = 12
in-degrees , deg−(a)= 2, deg−(b)= 2, deg−(c)= 3, deg−(d)= 2,
deg−(e)= 3, deg−(f )= 0
out-degrees, deg+(a)= 4, deg+(b)= 1, deg+(c)= 2, deg+(d)= 2,
deg+(e)= 3, deg+(f )= 0
Sum of in-degree = sum of out-degree =12 = edges
Null Graph
A null graph has no edges. null graph of n vertices is denoted by Nn

Connected and Disconnected Graph


graph is connected if any two vertices of graph are connected by path
graph is disconnected if at least two vertices of graph are not
connected by path.
Regular Graph
A graph is regular if all vertices of graph
have the same degree.
In a regular graph G of degree r, the
degree of each vertex of G is r.

Complete Graph
A graph is called complete graph if every
two vertices pair are joined by exactly one
edge. The complete graph with n vertices
is denoted by Kn

Cycle Graph
If a graph consists of a single cycle, it is
called cycle graph. The cycle graph with n
vertices is denoted by Cn
Bipartite Graph
If the vertex-set of a graph G can be split into two disjoint sets, 𝑉1
and 𝑉2, in such a way that each edge in the graph joins a vertex in 𝑉1
to a vertex in 𝑉2, and there are no edges in G that connect two
vertices in 𝑉1 or two vertices in 𝑉2, then the graph G is called a
bipartite graph

Example: Is graphs C, bipartite or not

C is bipartite, because its vertex set can be partitioned into the two
sets V1 = {v1, v3, v5} and V2 = {v2, v4, v6}, and every edge of C
connects a vertex in V1 and a vertex in V2
GRAPH UNION
The union of two simple graphs G1 = (V1,E1) and G2 = (V2,E2) is the
simple graph with vertex set V1 ∪ V2 and edge set E1 ∪ E2
The union of G1 and G2 is denoted by G1 ∪ G2

Example: Find union of graphs G1 and G2 shown in Fig

The vertex set of the union G1 ∪ G2 is union of two vertex sets,


namely, {a, b, c, d, e, f }. The edge set of the union is the union of
the two edge sets. The union is displayed in Fig
Representation of Graphs
There are mainly two ways to represent a graph:
Adjacency List
Adjacency Matrix
Incidence matrices
Adjacency List
adjacency lists specify vertices that are adjacent to each vertex of
the graph.
Example: Use adjacency lists to describe simple graph given in Fig
Example: Represent directed graph shown in Fig
list all vertices that are terminal vertices of edges starting at each
vertex of graph
Adjacency Matrices
Suppose that G = (V ,E) is a simple graph where |V| = n.
Suppose that the vertices of G are listed arbitrarily as v1, v2, . . . , vn.
The adjacency matrix A (or AG) of simple graph or directed graph G,
with respect to this listing of the vertices, is the n x n zero–one matrix

Adjacency matrices can also be used to represent undirected graphs with


loops and with multiple edges.
A loop at vertex vi is represented by a 1 at the (i, i)th position of adjacency
matrix. When multiple edges connecting same pair of vertices vi and vj, or
multiple loops at same vertex, are present, the adjacency matrix is no
longer a zero–one matrix, because the (i, j )th entry of this matrix equals the
number of edges that are associated to {vi , vj }.
All undirected graphs, including simple graph (has no loops or multi edges),
multigraphs and pseudographs, have symmetric adjacency matrices Aij = Aji
Example: Use an adjacency matrix to represent graph shown in Fig

Example: Use an adjacency matrix to represent pseudograph shown in Fig


The adjacency matrix for a directed graph does not have to be
symmetric
Adjacency matrices can also be used to represent directed
multigraphs. Again, such matrices are not zero–one matrices when
there are multiple edges in same direction connecting two vertices.
Example: Use an adjacency matrix to represent the directed graph
shown in Fig
Incidence Matrices
Let G = (V ,E) be an undirected graph.
Suppose that v1, v2, . . . , vn are the vertices and
e1, e2, . . . , em are the edges of G.
Then the incidence matrix with respect to this ordering of V and E is
the n × m matrix M = [mij ], where

Multiple edges are represented in the incidence matrix using columns


with identical entries, because these edges are incident with the
same pair of vertices. Loops are represented using a column with
exactly one entry equal to 1, corresponding to the vertex that is
incident with this loop
Example: Represent graph shown in Fig with an incidence matrix.

Example: Represent pseudograph shown in Fig using an incidence


matrix
Euler Graphs (circuit) and Euler Path
travel along edges of a graph starting at a vertex and returning to same
vertex by traversing each edge of graph exactly once
Euler path – A simple path that uses every edge of a graph exactly
once.
Euler circuit - A simple circuit in a graph G is a simple circuit containing
every edge of G.

NECESSARY AND SUFFICIENT CONDITIONS FOR EULER CIRCUITS AND


PATHS
- A connected multigraph with at least two vertices has an Euler circuit
if and only if each of its vertices has even degree.
A connected graph G is Eulerian if and only if its edge set can be
decomposed into cycles
- A connected multigraph has an Euler path but not an Euler circuit if
and only if it has exactly two vertices of odd degree
Example: Which of the undirected graphs in Figure 3 have an Euler
circuit? Of those that do not, which have an Euler path?

The graph G1 has an Euler circuit, for example, a, e, c, d, e, b, a.


Neither of the graphs G2 or G3 has an Euler circuit.
However, G3 has an Euler path, namely, a, c, d, e, b, d, a, b.
G2 does not have an Euler path
Example: Which of the directed graphs in Fig have an Euler circuit? Of
those that do not, which have an Euler path

The graph H2 has an Euler circuit, for example, a, g, c, b, g, e, d, f, a


Neither H1 nor H3 has an Euler circuit
H3 has an Euler path, namely, c, a, b, c, d, b, but H1 does not
Example: Which graphs shown in Fig have an Euler path

G1 contains exactly two vertices of odd degree, namely, b and d.


Hence, it has an Euler path that must have b and d as its endpoints.
One such Euler path is d, a, b, c, d, b. Similarly,G2 has exactly two
vertices of odd degree, namely, b and d. So it has an Euler path that
must have b and d as endpoints. One such Euler path is b, a, g, f, e,
d, c, g, b, c, f, d. G3 has no Euler path because it has six vertices of
odd degree
Hamiltonian Graphs (circuit) and Hamiltonian path
travel along edges of a graph starting at a vertex and returning to it
while visiting each vertex of graph exactly once

Hamiltonian path - A simple path in a graph G that passes through


every vertex exactly once

Hamilton circuit- A simple circuit in a graph G that passes through


every vertex exactly once

CONDITIONS FORTHE EXISTENCE OF HAMILTON CIRCUITS


- a graph with a vertex of degree one cannot have a Hamilton circuit,
because in a Hamilton circuit, each vertex is incident with two edges in
the circuit
- if a vertex in the graph has degree two, then both edges that are
incident with this vertex must be part of any Hamilton circuit
Example: Which of simple graphs in Fig have a Hamilton circuit or, if
not, a Hamilton path

G1 has a Hamilton circuit: a, b, c, d, e, a. There is no Hamilton circuit


in G2 (this can be seen by noting that any circuit containing every
vertex must contain the edge {a, b} twice), but G2 does have a
Hamilton path, namely, a, b, c, d. G3 has neither a Hamilton circuit
nor a Hamilton path, because any path containing all vertices must
contain one of the edges {a, b}, {e, f }, and {c, d} more than once
Example: Show that neither graph displayed in Fig has a Hamilton
circuit

There is no Hamilton circuit in G because G has a vertex of degree


one, namely, e. Now consider H. Because the degrees of the vertices
a, b, d, and e are all two, every edge incident with these vertices must
be part of any Hamilton circuit. It is now easy to see that no
Hamilton circuit can exist in H, for any Hamilton circuit would have to
contain four edges incident with c, which is impossible
Search Techniques
Search Strategies
• Once we have formulated the problem, we have to decide how to
find the solutions to those problems. This could be done by
searching the state space.

• When searching for a solution, you have start at the initial state of
the search tree/graph and expand each state and visit connected
states until the goal state has been reached.

• There are many ways to determine which state to expand in order


to get to the goal state, which are known as ‘Search strategies’.

• These strategies can be mainly divided in to two groups known as


‘Uninformed search’ and ‘Informed search’
Breadth First Search (BFS)
- BFS expands all the nodes at a given depth in the search tree before
any nodes at the next level are expanded
- Que is used in implementation of BFS (FIFO strategy)

BFS-tree Algorithm (Start Node)


Mark all the nodes as NOT visited
Mark the starting node as visited and enqueue it to QUEUE
Make the starting node as the ROOT of BFS-tree
while QUEUE is not empty:
Dequeue the first node F from QUEUE
Expand node F
For each unvisited child of F
Add the child to its parent in the BFS-tree
Mark the child as visited and enqueue it to QUEUE.
End
end
Example What is the BFS tree of the following graph if the starting
node is A?
BFS Search Algorithm (Start Node, Goal Node)
Mark all the nodes as NOT visited
Mark the starting node as visited and enqueue it to QUEUE
Make the starting node as the root of BFS-tree
IF the starting node is a goal node, EXIT on success
while QUEUE is not empty:
Dequeue the first node F from QUEUE
Expand node F
For each unvisited child of F
Add the child to its parent in the BFS-tree
Mark the child as visited and enqueue it to QUEUE.
IF the child is a goal node, EXIT on success
End
end
The SEARCH RESULT is the path from the ROOT to the GOAL in the BFS-tree

Example What is the BFS tree of the following graph if the starting node is A and
goal node is G?
Depth First Search (DFS)
- DFS always expands the deepest node in the current frontier of the
search tree.
- Stack is used in implementation of DFS (LIFO strategy)
DFS-tree Algorithm (Start Node)
Mark all the nodes as NOT visited
Mark the starting node as visited and PUSH it to STACK
Make the starting node as the ROOT of DFS-tree
while STACK is not empty:
Expand the top node F from STACK
IF all children of the top node F are visited THEN POP it from STACK
ELSE
Select one unvisited child of F, mark it as visited and PUSH it
to STACK.
Add selected child to its parent (top node) in the DFS-tree.
END
End
Example What is the DFS tree of the following graph if the starting
node is A?
DFS Search Algorithm (Start Node, Goal Node)
Mark all the nodes as NOT visited
Mark the starting node as visited and PUSH it to STACK
Make the starting node as the ROOT of DFS-tree
while STACK is not empty:
IF the top node F is a goal node, THEN EXIT on success
Expand the top node F from STACK
IF all children of the top node F are visited THEN POP it from STACK
ELSE
Select one unvisited child of F, mark it as visited and PUSH it to STACK.
Add the selected child to its parent (top node) in the DFS-tree.
END
end
The SEARCH RESULT is the path from the ROOT to the GOAL in the DFS-tree.

Example What is the DFS search output of the following graph if the start node is A and
goal node is G?
Example: Find path traversed using BFS and DFS traversal, if start
node is 0, Draw BFS/DFS tree
Uniform Cost Search (UCS)
Uniform-cost search expands node 𝑛 with the lowest path cost 𝑔(𝑛).
UCS uses a priority queue ordered by 𝑔.
The goal test is applied to a node when it is selected for expansion
rather than when it is first generated.
A test is added if a better path is found to a node currently on the
priority queue.
UCS does not care about the number of steps a path has, but only
about their total cost.
UCS will get stuck in an infinite loop if there is a path with an infinite
sequence of zero-cost actions—for example, a sequence of NoOp
actions.
When all step costs are the same, UCS is similar to BFS, except that
the latter stops as soon as it generates a goal, whereas UCS examines
all the nodes at the goal’s depth to see if one has a lower cost; thus
UCS does strictly more work by expanding nodes at depth 𝑑
unnecessarily.
UCS-tree Algorithm (Start Node)
Mark all the nodes as NOT visited
Enqueue the starting node to PQUEUE
Make the starting node as the ROOT of UCS-tree with zero cost
do
IF PQUEUE is empty, THEN return FAILURE
Dequeue the first node F from PQUEUE
Mark F as visited
Expand node F
For each child of F
cost(child) = cost(parent) + cost(parent-child)
IF child is NOT visited OR NOT available in PQUEUE THEN
Enqueue the child with its cost
Add the child to its parent in the UCS-tree
ELSE IF child is in PQUEUE with higher cost THEN
Replace it in PQUEUE with its new cost
Remove it from the UCS-tree
Add it to its new parent in the UCS-tree
end
Example: Find short path traversed using UCS, if start node is A, Draw UCS tree

Example What is the UCS tree if the start state is Frankfurt?


Greedy Search
The idea is to expand the node with the smallest estimated cost to reach the goal
(the node which appears to be closest to the goal).
It is called “greedy” because at each step it tries to get as close to the goal as it
can.
The heuristic function for greedy search is 𝑓(𝑛) = 𝑕(𝑛) where 𝑕(𝑛) estimates the
remaining distance to a goal.
Greedy search is simple and finds solution quickly.
The time and space complexity for the tree version of greedy search is 𝑂(𝑏𝑚).
However, with a good heuristic function, the complexity can be reduced
substantially.
Greedy algorithm is neither complete nor optimal. It is susceptible to false start
Example The distance between two nodes are given in the graph, Find the shortest
path from B to G using greedy algorithm?

1) Start node B
2) See A,C,E and distance 6,5,7; Choose less distance node – C
3) Go to C, See D,F and distance 6,7; Choose less distance node – D
REPEAT STEPS UNTIL GOAL
4) Go to D, See A,H and distance 7,4; Choose less distance node – H
5) Go to H, See F,G and distance 4,3; Choose less distance node – G (GOAL)
SO STOP
PATH MAY BE SHORT OR NOT
PATH = B-C-D-H-G = 5+6+4+3=18
Example The distance between any two cities and direct distances from each city to
Bucharest are given below.
Find the shortest path from Barcelona to Bucharest using greedy algorithm?
For this problem, the direct distances from a city to
Bucharest (table 1) will be used as the heuristic
function.
The complete path is:
Barcelona – Rome – Palermo – Athens – Bucharest
The path cost is:
𝑃𝑐𝑜𝑠𝑡 = 1471 + 1043 + 907 + 1300 = 4,721𝐾𝑚
A* Search
The A* algorithm combines the UCS and the Greedy search to
determinate the ordering.

The evaluation function 𝑓 is given by:


𝑓(𝑛) = 𝑕(𝑛) + 𝑔(𝑛)
𝑓(𝑛) estimates total cost of path through 𝑛 to goal.
𝑕(𝑛) is a heuristic function that estimates the cost from 𝑛 to goal
𝑔(𝑛) is the cost so far to reach 𝑛.

A* search is complete, optimal and speedy


The queue will be sorted based on estimates of full path costs (not
just the cost so far, and not just the estimated remaining cost, but
the two together).
A* Search Algorithm (Start Node, Goal Node)
Add the starting node to visited array
cost(start) =h(start)
Enqueue the starting node to PQUEUE with its cost
Make the starting node as the ROOT of A*-tree
do
Dequeue the first node F from PQUEUE
IF F is a goal node, EXIT on success
Expand node F
For each unvisited child of F
g(child) = g(parent) + g(child-parent)
cost(child) =h(child) + g(child)
Enqueue the child with its cost to PQUEUE
Add the child to its parent (F) in the A*-tree
Add the child to visited array
End
end
The SEARCH RESULT is the path from the ROOT to the GOAL in the A*-tree
Example Find the shortest path from X to Y using A* algorithm?

1) Start node X
2) Find f(n) from X to Y through a and also through d; choose short dist (a or d) ; so node a
3) Go to a, Find f(n) from X to Y through a&b ; choose short dist (remaining d,b) ; so node b
REPEAT STEPS UNTIL GOAL
4) Go to b, Find f(n) from X to Y through a&b &c; choose short dist (remaining d,c) ; so node d
5) Go to d, Find f(n) from X to Y through d&e; choose short dist (remaining e,c) ; so node e
6) Go to e, only one route, goal node Y, so go to Y
SHORT PATH = X-d-e-Y = 2+3+2=7
Example Find the shortest path from S to E using A* algorithm?
Example Find the shortest path from Barcelona to Bucharest using A* algorithm?
The distance between any two cities and direct distances from each city to
Bucharest are given (previous example)
Assume the following:
𝒉(𝒏): direct distance to Bucharest from the node 𝑛.
𝒈(𝒏): cost of the path so far (i.e. the sum of all costs in the path from Barcelona
to the current node).
Example: Find shortest path from initial node S to goal node G using A* algorithm?
Shortest Path using Dijkstra’s Algorithm
Dijkstra’s algorithm finds the length of a shortest path between two vertices in a
connected simple undirected weighted graph.
1. It maintains a list of unvisited vertices.
2. It chooses a vertex (source) with cost zero and assigns a maximum possible
cost (i.e. infinity) to every other vertex.
3. In every subsequent step of the algorithm it tries to improve (minimize) the
cost for each vertex.
- For each unvisited vertex of the current vertex calculate the new cost from
the vertex, choose minimum
4. When all the neighbors of the current node are considered, it marks the
current node as visited and is removed from the unvisited list.
5. Select a vertex from the list of unvisited nodes (which has smallest cost) and
repeat step 3
6. At the end there will be no possibilities to improve it further and then the
algorithm ends
Example: What is the length of a shortest
path between a and z in the weighted
graph shown in Fig using Dijkstra’s
Algorithm
CCE133 – Discrete Structures

Lecture Notes

Lecture 8
Trees
Trees
Def – Tree is a connected graph that contains no simple circuits (loop)
- A tree cannot contain multiple edges or loops
- An undirected graph is a tree if and only if there is a unique simple
path between any two of its vertices.
- A tree with N number of vertices contains (N-1) number of edges.
Importance of trees
- Trees are used to construct efficient algorithms for locating items in a
list.
- Trees can be used in algorithms, such as Huffman coding, that
construct efficient codes saving costs in data transmission and storage.
- Trees can be used to study games such as checkers and chess and can
help determine winning strategies for playing these games.
- The operating system on a computer organizes files into directories
and subdirectories in trees
EXAMPLE: Which of graphs shown in Fig are trees? If tree how many
edges are there
G1 is tree, because
both are connected
graphs with no
simple circuits.
No of vertices=6
Edges = (N-1)=6-5=5
G2 is not a tree
because e, b, a, d, e
is a simple circuit in
this graph.
G3 is not tree
because it is not
connected
Rooted tree and parts
Rooted tree - tree in which one vertex has been designated as root
Root − node at the top of tree
Parent − Any node except root node has one edge upward to a node.
Child − node below a given node connected by its edge downward
Siblings - Vertices with the same parent
Ancestors of a vertex (other than root) - vertices in path from root to vertex,
excluding vertex itself and including the root (that is, its parent, its parent’s
parent, and so on, until the root is reached).
Descendants of a vertex v - vertices that have v as an ancestor.
Leaf - A vertex which has no children. (terminal vertex or external vertex)
(degree=1)
Internal vertices - Vertices that have children. (degree >1 & atleast 2)
Subtree - represents descendants of a node and edges
Level of a vertex - number of edges along unique path between vertex and
root
Height of rooted tree - maximum level to any vertex of tree
Example: (a) what is the level of v8 (b) what is the level of v0 (c) what is the
height of the tree (d) what are the children of v10 (e) what are the siblings of v1
(f) what are the decedents of v12 (g) Which vertex is the root? (h) Which vertices
are internal? (i) Which vertices are leaves (external or terminal)? (j) Which vertex
is the parent of v10? (k) Which vertices are ancestors of v17? (l) draw subtree for
v12

(a) 3 (b) 0 (c) 5 (d) v14,v15,v16 (e) v2 (f) v17,v18,v19 (g) v0 (h) v1,v2,v3,v5,v6,v8,
v10,v12,v17 (i) v7,v4,v13,v9,v14,v15,v16,v11,v18,v19 (j) v6? (k) v12,v6,2,v0
m-ary tree
A rooted tree is called an m-ary tree if every internal vertex 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
EXAMPLE Are the rooted trees in Fig full m-ary trees for some
positive integer m?
T1 is a full binary tree because
each of its internal vertices has two
children. T2 is not a full m-ary tree
for any m because some of
its internal vertices have two
children and others have three
children
Properties of Trees
A tree with n vertices has n − 1 edges.
A tree with n vertices and n-1 edges has the total degree of 2(n-1)
A full m-ary tree with i internal vertices contains n = mi + 1 vertices
Binary tree
A binary tree is a rooted tree in which every internal vertex has at most 2 children. It
means it can have 2,1,0 children.
A full binary tree is a binary tree in which each internal vertex has exactly 2 children
or 0 children
In an ordered binary tree (usually called just a binary tree), if an internal vertex has
two
children, the first child is called the left child (generally less value than parent) and
the second child is called the right child (generally greater value than parent).
The tree rooted at the left child of a vertex is called the left subtree of this vertex,
and the tree rooted at the right child of a vertex is called the right subtree of the
vertex.
Path − Path refers to the sequence of nodes along the edges of a tree
Binary Search Tree Representation
EXAMPLE: What are the left and right children of d in the binary tree T
shown in Fig? What are the left and right subtrees of c?

The left child of d is f and the right child is g. the left and right subtrees
of c are shown in Fig
Tree Traversal
Traversal is a process to visit all nodes of a tree in a specific
order. Because, all nodes are connected via edges (links)
we always start from the root (head) node. That is, we
cannot randomly access a node in a tree. There are three
ways which we use to traverse a tree −
In-order Traversal
Pre-order Traversal
Post-order Traversal
Generally, we traverse a tree to search or locate a given
item or key in the tree or to print all the values it contains
In-order Traversal (Left-Root-Right)
In this method, left subtree is visited first, then root and later right
sub-tree. We should always remember that every node may
represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted
key values in an ascending order

We start from A, and following in-order traversal, we move to its


left subtree B. B is also traversed in-order. The process goes on
until all the nodes are visited. The output of inorder traversal of
this tree will be − D → B → E → A → F → C → G
EXAMPLE In which order does an inorder traversal visit the vertices
of the ordered rooted tree T in Fig?
Pre-order Traversal (Root-Left-Right)
In this method, root node is visited first, then left subtree and finally
right subtree

We start from A, and following pre-order traversal, we first


visit A itself and then move to its left subtree B. B is also traversed
pre-order. The process goes on until all the nodes are visited. The
output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
EXAMPLE In which order does a preorder traversal visit the vertices in
the ordered rooted tree T shown in Fig
Post-order Traversal (Left-Right-Root)
In this method, First we traverse left subtree, then right subtree and
finally root node

We start from A, and following pre-order traversal, we first visit the


left subtree B. B is also traversed post-order. The process goes on
until all the nodes are visited. The output of post-order traversal of
this tree will be − D → E → B → F → G → C → A
EXAMPLE In which order does a postorder traversal visit the vertices
of the ordered rooted tree T shown in Fig
Easy ways to list the vertices of an
ordered rooted tree in preorder, inorder,
and postorder.
To do this, first draw a curve around the
ordered rooted tree starting at the root,
moving along the edges, as shown in the
example in Fig.
preorder - list each vertex first time curve
passes it.
inorder - list a leaf first time curve passes
it and list each internal vertex second time
curve passes it.
postorder - list a vertex last time it is
passed on the way back up to its parent.
preorder - a, b, d, h, e, i, j , c, f , g, k
inorder - h, d, b, i, e, j , a, f , c, k, g
postorder - h, d, i, j , e, b, f , k, g, c, a
Another Easy ways to list the vertices of an ordered rooted tree in
preorder, inorder, and postorder.
Infix, Prefix, and Postfix Notation
Infix notation: (operand)(operator) (operand)
Ex: X + Y
A * ( B + C ) / D
Evaluation of expression
operators precedence and associativity,
first : brackets ( )
second : exponent ↑ (right to left) suppose 2↑3↑2 then 2↑32 = 2↑9 = 29 = 512
third : * / (left to right) suppose 4*2/2 = 8/2 = 4 (first * then / , exp left to right)
fourth : + - (left to right) suppose 2 –3 +1 = -1+1 = 0 (first – then + , exp left to right)

Example: What is the value of the infix expression


2 ↑ 3 / 4 + 2 * 3 – (3+2)
2 ↑ 3 / 4 + 2 * 3 – 5 (first bracket)
8 / 4 + 2 * 3 – 5 (second exponent)
2 + 2 * 3 – 5 (/ and * are of same order, take left to right, so third divide; fourth
multiply)
2 + 6 – 5 (+ and - are of same order, take left to right, so fifth add; sixth subtract)
8–5=3
Prefix notation (also known as "Polish notation"):
(operator) (operand) (operand)
Ex: +XY
/*A+BCD
The steps used to evaluate the expression by working right to left, and performing
operations using the operands on the right
EXAMPLE What is the value of the prefix expression + − ∗ 2 3 5/↑ 2 3 4?
Postfix notation (also known "Reverse Polish notation"):
(operand) (operand) (operator)
Ex: XY+
A B C + * D /
The steps used to evaluate the expression by starting at the left to right and
carrying out operations when two operands are followed by an operator
EXAMPLE What is the value of the postfix expression 7 2 3 ∗ − 4 ↑ 9 3/+?
Construction of expression tree
An ordered rooted tree (expression tree) can be used to represent s expressions,
where the internal vertices represent operations, and the leaves represent the
variables or numbers. Each operation operates on its left and right subtrees (in that
order).
Exp: X+Y

EXAMPLE What is ordered rooted tree that represents expression


((x + y)↑2) + ((x − 4)/3)
Infix, prefix and postfix form of expression from binary tree
Infix expression - An inorder traversal of binary tree (expression tree) represent infix
exp. To make expressions unambiguous it is necessary to include parentheses in
inorder traversal whenever we encounter an operation
Prefix expression - when we traverse its rooted tree in preorder.
Postfix expression - when we traverse its rooted tree in postorder.
An expression in prefix & postfix notation, is unambiguous, so no parentheses are
needed in such an expression.

EXAMPLE What is the prefix form for ((x + y) ↑ 2) + ((x − 4)/3)?


Construct expression tree (above example) and make preorder traversal

This produces + ↑ + x y 2 / − x 4 3
EXAMPLE What is the postfix form of the expression ((x + y) ↑ 2) + ((x − 4)/3)?
Construct expression tree (above example) and make postorder traversal

This produces the postfix expression: x y + 2 ↑ x 4 − 3 / +


CCE133 – Discrete Structures

Lecture Notes

Lecture 9
Iteration and Recursion
Recurrence Relations or Recursively Defined Functions
In mathematics, we can create recursive functions, which
depend on its previous values to create new ones. We often
call these recurrence relations.
We use two steps to define a function with the set of non
negative integers as its domain:
BASIS STEP: Specify the value of the function at zero
RECURSIVE STEP: Give a rule for finding its value at an
integer from its values at smaller integer
For example, we can have function f(x)=2f(x-1), with f(1)=1
If we calculate some of f values, we get 1, 2, 4, 8, 16, ...
- Iteration is act of repeating a process, either to generate an
unbounded sequence of outcomes, or with the aim of approaching a
desired goal, target or result.
- Each repetition of process is also called an "iteration", and the
results of one iteration are used as starting point for next iteration
- Iteration in mathematics may refer to process of iterating a
function i.e. applying a function repeatedly, using the output from
one iteration as the input to the next.
- In iteration, we start with value of function at one or more integers
(base cases), and successively apply recursive function to find values
of the function at successive larger integers. Such a procedure is
called iterative
- Recursion is the process a procedure goes through when one of
steps of the procedure involves invoking the procedure itself.
- A procedure that goes through recursion is said to be 'recursive'.
Example: Fibonacci numbers, f0, f1, f2, . . . , are defined by equations
f0 = 0, base case1
f1 = 1, base case2
fn = fn−1 + fn−2 for n = 2, 3, 4, . . .. Find f5 by iteration method
f0 = 0
f1 = 1
f2=f1+f0=1+0=1
f3=f2+f1=1+1=2
f4=f3+f2=2+1=3
f5=f4+f3=3+2=5

Example Suppose that f is defined recursively by f (0) = 3,


f (n + 1) = 2f (n) + 3 Find f (1), f (2), f (3), and f (4) by iteration method
From the recursive definition it follows that f(0)=3
f (1) = 2f (0) + 3 = 2 · 3 + 3 = 9
f (2) = 2f (1) + 3 = 2 · 9 + 3 = 21
f (3) = 2f (2) + 3 = 2 · 21 + 3 = 45
f (4) = 2f (3) + 3 = 2 · 45 + 3 = 93
Example: Fibonacci numbers, f0, f1, f2, . . . , are defined by equations
f0 = 0, base case1
f1 = 1, base case2
fn = fn−1 + fn−2 for n = 2, 3, 4, . . .. Find f5 by recursion method
f5=f4+f3
= [(f3+f2)] + [(f2+f1)]
= [(f2+f1)+(f1+f0)] + [(f1+f0)+f1]
= [((f1+f0)+f1)+(f1+f0)] + [(f1+f0)+f1]
= [((1+0)+1)+(1+0)] + [(1+0)+1]
= [(1+1)+(1)] + [(1)+1]
= [(2)+(1)] + [2]
= [3] + [2] = 5
Example: factorial function definition
0! = 1
n! = n (n-1)! Find 4! by recursion method
4! = 4 · 3!
We then use recursive step repeatedly to write
3! = 3 · 2!
2! = 2 · 1!
1! = 1 · 0!
Inserting value of 0! = 1, and working back through steps,
1! = 1 · 1 = 1
2! = 2 · 1! = 2 . 1 = 2
3! = 3 · 2! = 3 · 2 = 6
4! = 4 · 3! = 4 · 6 = 24
- Iteration in computing is the technique marking out of a
block of statements within a computer program for a
defined number of repetitions in a loop.
- The block of statements is said to be iterated; a computer
scientist might also refer to that block of statements
as an "iteration".
-Recursion -function calls itself on a smaller version of input
- while using recursion, programmer need to be careful to
define an exit condition from the function, otherwise it will
go into an infinite loop
- In the context of mathematics or computer science,
iteration (along with related technique of recursion) is a
standard building block of algorithms.
Example: From the pseudocode below
int fact (num)
f=1
for i from 1 to num
{
f=f*i
}
return f
What is the value of fact(4), write the iterative steps
num=4
initial f=1
iteration-1 i=1, f=1*1=1
iteration-2 i=2, f=1*2=2
iteration-3 i=3, f=2*3=6
iteration-4 i=4, f=6*4=24
The value 24 is returned for fact(4)
Example: From the pseudocode below
int fact (num)
if num<=1
return 1
else
return num*fact(num-1)
What is the value of fact(4), write the recursive steps

The value 24 is returned for fact(4)


P)
Iteration vs Recursion
- In algorithmic situations, recursion and iteration can be employed to
the same effect.
- The primary difference is that recursion can be employed as a
solution without prior knowledge as to how many times the action
will have to repeat, while a successful iteration requires that
foreknowledge.
- Often an iterative approach for the evaluation of a recursively
defined sequence requires much less computation than a procedure
using recursion
- The recursive algorithm require more computation than an iterative
one when a recursively defined function is evaluated.
- It is sometimes preferable to use a recursive procedure even if it is
less efficient than the iterative procedure.
- In particular, this is true when the recursive approach is easily
implemented and the iterative approach is not

You might also like