DS-WK-11-Lec-21-22 Asg-10
DS-WK-11-Lec-21-22 Asg-10
DS-WK-11-Lec-21-22 Asg-10
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
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(1) fibonacci(0)
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
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
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
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.