0% found this document useful (0 votes)
13 views28 pages

Lecture 13-14

Uploaded by

umar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views28 pages

Lecture 13-14

Uploaded by

umar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Recursive

Algorithms,
Inference Rules
Discrete Structure
MUHAMMAD UMAR NASIR
LECTURER FCS RIPHAH INTERNATIONAL UNIVERSITY LAHORE
Recursive Algorithm

 An algorithm is called recursive if it solves a problem by reducing


it to an instance of the same problem with smaller input.
Recursive Algorithm for
Computing n!
 procedure factorial(n: nonnegative integer)
 if n = 0 then return 1
 else return n · factorial(n − 1)
 {output is n!}
Recursive Algorithm for
Computing n!
Find factorial of 4

factorial(4)

4≠0 4.6=24

4.factorial(3)

3≠0 3.2=6

3.factorial(2)

2≠0 2.1=2

2.factorial(1) 1.1=1

1≠0

1.factorial(0)

0=0 so return 1
A Recursive Algorithm for
Computing an
 procedure power(a: nonzero real number, n: nonnegative
integer)
 if n = 0 then return 1
 else return a · power(a, n − 1)
 {output is an}
A Recursive Algorithm for
Computing an
Calculate 24 using recursive algorithm for computing an

power(2,4)

4≠0 2.8=16

2.power(2,3)

3≠0 2.4=8

2.power(2,2)

2≠0 2.2=4

2.power(2,1) 2.1=2

1≠0

2.power(2,0)

0=0 so return 1
GCD:

 GCD (Greatest Common Divisor) of two or more integers (at least


one of which is not zero), is the largest positive integer that
divides the numbers without a remainder. For example, the GCD
of 8 and 12 is 4.
A Recursive Algorithm for
Computing gcd(a, b)
 procedure gcd(a, b: nonnegative integers with a < b)
 if a = 0 then return b
 else return gcd(b mod a, a)
 {output is gcd(a, b)}
A Recursive Algorithm for
Computing gcd(a, b)
Find GCD of 5 and 8 using recursive algorithm

gcd(5,8)

gcd(8 mod 5, 5)

= gcd(3,5) 1

gcd(5 mod 3,3)

=gcd(2,3) 1

gcd(3 mod 2, 2) As a=0


so retun
=gcd(1,2) 1

gcd(2 mod 1, 1)

=gcd(0,1)

So gcd of 5 and 8 is 1
A Recursive Algorithm for
Computing gcd(a, b)
Find GCD of 4 and 12 using recursive algorithm

gcd(4,12)

gcd(12 mod 4, 4) As a=0 so


return 4
= gcd(0,4)

So GCD of 4 and 12 is 4
A Recursive Algorithm for
Computing gcd(a, b)
Find GCD of 8 and 12 using recursive algorithm

gcd(8,12)

gcd(12 mod 8,
8)
4
= gcd(4,8)

gcd(8 mod 4,4) As a=0


so return
=gcd(0,4) 4

So GCd of 8 and 12 is 4
Fibonacci Numbers:

 In mathematics, the Fibonacci numbers or Fibonacci sequence are the


numbers in the following integer sequence
 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
 By definition, the first two numbers in the Fibonacci sequence are 0 and 1
and each subsequent number is the sum of the previous two

 A Recursive Algorithm for Fibonacci Numbers


 procedure fibonacci(n: nonnegative integer)
 if n = 0 then return 0
 else if n = 1 then return 1
 else return fibonacci(n − 1) + fibonacci(n − 2)
 {output is fibonacci(n)}
Fibonacci Numbers:
Following figure shows how f4 is evaluated recursively
Fibonacci Numbers:

fibonacci(4)

1 fibonacci(3) 1 fibonacci(2)
1 0

1 fibonacci(2) fibonacci(1) fibonacci(1) fibonacci(0)


0

fibonacci(1) fibonacci(0)

The 4th term in Fibonacci series is 3


Term 0 1 2 3 4

Series 0 1 1 2 3
Recursive Linear Search
Algorithm:
 procedure search(i, j, x: i, j, x integers, 1 ≤ i ≤ j ≤ n)
 if ai = x then
 return i
 else if i = j then
 return 0
 else
 return search(i + 1, j, x)
 {output is the location of x in a1, a2, . . . , an if it appears;
otherwise it is 0}
Recursive Linear Search
Algorithm:
Search 7 from the list 4 , 6 , 2 , 7 , 1 , 3 using Recursive Linear search Algorithm
Search(1,6, 4,6,2,7,1,3
7)

a1≠7 and 1≠6

Search(2,6,7) 4,6,2,7,1,3

a2≠7 and 2≠6

Search(3,6,7) 4,6,2,7,1,3

a3≠7 and 3≠6

Search(4,6,7) 4,6,2,7,1,3

a4=7 so Element
found at
return 4 location 4
Recursive Linear Search
Algorithm:
Search 5 from the list 4 , 6 , 2 , 7 , 1 , 3 using Recursive Linear search Algorithm
Search(1,6,5) 4,6,2,7,1,3

a1≠5 and 1≠6

Search(2,6,5) 4,6,2,7,1,3

a2≠5 and 2≠6

Search(3,6,5) 4,6,2,7,1,3

a3≠5 and 3≠6

Search(4,6,5) 4,6,2,7,1,3

a4≠5 and 4≠6

Search(5,6,5) 4,6,2,7,1,3

a5≠5 and 5≠6

4,6,2,7,1,3 Search(6,6,5)

Element not a6≠5 and 6=6


found
So return 0
Recursive Binary Search Algorithm:

 procedure search(i, j, x: i, j, x integers, 1 ≤ i ≤ j ≤ n)


 m :=
 if x = am then
 return m
 else if (x < am and i <m) then
 return search(i ,m − 1, x)
 else if (x > am and j >m) then
 return search(m + 1, j, x)
 else return 0
 {output is location of x in a1, a2, . . . , an if it appears; otherwise it is 0}
Recursive Binary Search Algorithm:
Search 7 from the list 2 , 5 , 6 , 7 , 8 , 11 using Recursive Binary search Algorithm
Search(1,6, 7) 2,5,6,7,8,11

m=3

a3≠7 and

7>a3 and 6>3

Search(4,6,7) 2,5,6,7,8,11

m=5

a5≠7 and

7<a5 and 4<7

Search(4,4,7) 2,5,6,7,8,11

a4=7 So Element
fount at
return 4 location 4
Rules of Inference:

 Valid Arguments in Propositional Logic


 Rules of Inference
Valid Arguments in
Propositional Logic:
 An argument in propositional logic is a sequence of propositions. All
but the final proposition in the argument are called premises and
the final proposition is called the conclusion.
 An argument is valid if the truth of all its premises implies that the
conclusion is true.
 An argument form in propositional logic is a sequence of
compound propositions involving propositional variables.
Valid Arguments in
Propositional Logic:
((p →q) ∧ p) → q

premise1 premise2 conclusion

Consider the following argument involving


propositions.
"If you have a current password, then you can log
onto the network."
"You have a current password."
Therefore,
"You can log onto the network."
Then, the argument has the form
p→q
p
∴q
where ∴ is the symbol that denotes ''therefore.''
Valid Arguments in
Propositional Logic:
 Using this notation, the hypotheses are written in a column, followed
by a horizontal bar, followed by a line that begins with the therefore
symbol and ends with the conclusion.
 We know that when p and q are propositional variables, the
statement ((p →q) ∧ p) → q is a tautology .In particular, when both p
→ q and p are true, we know that q must also be true. We say this
form of argument is valid because whenever all its premises are
true, the conclusion must also be true.
Rules of inference:

 We can always use a truth table to show that an argument form is


valid. Instead of using truth tables, we can first establish the validity
of some relatively simple argument forms, called rules of
inference.
 These rules of inference can be used as building blocks to construct
more complicated valid argument forms.
Rules of inference:
Rules of inference:
 Example 1: State which rule of inference is the basis of the following
argument?
 "It is below freezing now. Therefore, it is either below freezing or
raining now."
 Solution: Let p be the proposition "It is below freezing now" and q the
proposition "It is raining
 now." Then this argument is of the form

∴ p ∨q
p

 This is an argument that uses the addition rule.


Rules of inference:
Example 2: State which rule of inference is the basis of the following
argument?
"It is below freezing and raining now. Therefore, it is below freezing
now."
Solution: Let p be the proposition "It is below freezing now," and let q
be the proposition "It is

p ∧qnow." This argument is of the form


raining

∴ p

This argument uses the simplification rule.


Rules of inference:
Example 3: State which rule of inference is used in the argument?
If it rains today, then we will not have a barbecue today. If we do not
have a barbecue today, then we will have a barbecue tomorrow.
Therefore, if it rains today, then we will have a barbecue tomorrow.
Solution: Let p be the proposition "It is raining today," let q be the
proposition "We will not have a barbecue today," and let r be the
proposition "We will have a barbecue tomorrow." Then this argument is
of the form
p→q
q→r
∴ p→r
Hence, this argument is a hypothetical syllogism.

You might also like