CCE133 Lecture Notes2020
CCE133 Lecture Notes2020
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).
Boolean Algebra - algebra that deals with binary variables and logic
operations. It is used in logic gates and programming, computer system
design
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
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}
aA “a belongs to A” a is an element of A
dA “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.
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)
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
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
U, W, R – predicate; X-variable
(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… )
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
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.
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 262626 = 17576 different license plates. (Rule of Product)
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 127 = 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
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).
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 : 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
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
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.
Lecture Notes
Lecture 6
Discrete Probability
In computer science, probability theory plays an important role in
the study of the complexity of algorithms
Def: Probability
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
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).
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)
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
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
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
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
• 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.
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
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.
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
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
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