DS-WK-11-Lec-21-22 Asg-10

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Discrete Structures (CS-335)

Mrs. Maryam Amin email id: [email protected] Whatsapp# 0346-5410878


Mrs. Sadia Rashid email id: [email protected] Whatsapp# 0334-4166270

(Week 11) Lecture 21

Objectives: Learning objectives of this lecture are

 Understand Recursive Greatest Common Divisor


 Understand Recursive Algorithm of Fibonacci Numbers
Text Book & Resources: Discrete Mathematics and its Applications (7th Edition) by Kenneth H.
Rosen

Greatest Common Divisor:


Greatest Common Divisor (GCD) 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)}

Example:
Find GCD of 5 and 8 using recursive algorithm.
Solution:

gcd(5,8)
1
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
=gcd(1,2) retun 1
gcd(2 mod 1, 1)
=gcd(0,1)

1
Discrete Structures (CS-335)
Mrs. Maryam Amin email id: [email protected] Whatsapp# 0346-5410878
Mrs. Sadia Rashid email id: [email protected] Whatsapp# 0334-4166270
So gcd of 5 and 8 is 1

Example:
Find GCD of 4 and 12 using recursive algorithm.
Solution:

gcd(4,12)
4
gcd(12 mod 4, 4) As a=0 so
= gcd(0,4) return 4
So GCD of 4 and 12 is 4
Example:
Find GCD of 8 and 12 using recursive algorithm.
Solution:

gcd(8,12)
4
gcd(12 mod 8, 8)
= gcd(4,8) 4
gcd(8 mod 4,4) As a=0 so
=gcd(0,4) return 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)}

2
Discrete Structures (CS-335)
Mrs. Maryam Amin email id: [email protected] Whatsapp# 0346-5410878
Mrs. Sadia Rashid email id: [email protected] Whatsapp# 0334-4166270

Example:
Find fourth Fibonacci number by using recursive algorithm.
Solution:
Following figure shows how f4 is evaluated recursively

2 fibonacci(4)
1

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

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


1 0

fibonacci(1) fibonacci(0)

The 4th term in Fibonacci series is 3


Term 0 1 2 3 4
Serie 0 1 1 2 3
s

3
Discrete Structures (CS-335)
Mrs. Maryam Amin email id: [email protected] Whatsapp# 0346-5410878
Mrs. Sadia Rashid email id: [email protected] Whatsapp# 0334-4166270

(Week 11) Lecture 22

Objectives: Learning objectives of this lecture are

 Understand Recursive Algorithm of Linear Search


 Know Recursive Algorithm of Binary Search
Text Book & Resources: Discrete Mathematics and its Applications (7th Edition) by Kenneth H.
Rosen, 6th Edition

Recursive Linear Search:


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 of 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}
Example:
Search 7 from the list 4 , 6 , 2 , 7 , 1 , 3 using Recursive Linear search Algorithm
Solution:
Search(1,6, 7) 4,6,2,7,1,3
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
return 4 at location 4

4
Discrete Structures (CS-335)
Mrs. Maryam Amin email id: [email protected] Whatsapp# 0346-5410878
Mrs. Sadia Rashid email id: [email protected] Whatsapp# 0334-4166270
Element found at location 4

Example:
Search 5 from the list 4 , 6 , 2 , 7 , 1 , 3 using Recursive Linear search Algorithm
Solution:
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
Element not found

Recursive Algorithm of Binary Search:


procedure search(i, j, x: i, j, x integers, 1 ≤ i ≤ j ≤ n)
m := ⌊(i+ j)/ 2 ⌋
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}

Example:
Search 7 from the list 2 , 5 , 6 , 7 , 8 , 11 using Recursive Binary search Algorithm

5
Discrete Structures (CS-335)
Mrs. Maryam Amin email id: [email protected] Whatsapp# 0346-5410878
Mrs. Sadia Rashid email id: [email protected] Whatsapp# 0334-4166270

Solution:
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
return 4 found at
location 4
Element found at location 4

Example:
Search 5 from the list 2, 4, 6 , 7 , 8 , 11 using Recursive Binary search Algorithm
Solution:
Search(1,6, 5) 2,4,6,7,8,11
m=3
a3≠5 and
5<a3 and 1<3

Search(1,2,5) 2,4,6,7,8,11
m=2
a2≠5 and
5>a2 and 2>2
return 0
2,4,6,7,8,11
Element not
found

Element not found

6
Discrete Structures (CS-335)
Mrs. Maryam Amin email id: [email protected] Whatsapp# 0346-5410878
Mrs. Sadia Rashid email id: [email protected] Whatsapp# 0334-4166270

Assignment # 10
Dear students read the given lectures carefully as per the course objectives mentioned on the top
and carryout the assignment as per following instructions

1. Submission Date: Sunday 10-May-2020 at 11:59PM; This will also count as your
Attendance for this week.

2. You must prepare handwritten Assignment and send it to respective course teacher (after
scanning it) for assessment by email only.

1. Find GCD of 17 and 21 in a table format by using recursive algorithm.


2. Find 6th Fibonacci number in table format by using recursive algorithm.
3. Search 10 from the list 9 , 16 , 7 , 12 , 10 , 32 by using Recursive Linear search Algorithm.
4. Search 11 from the list 2 , 5 , 6 , 7 , 8 , 11 by using Recursive Binary search Algorithm.

You might also like