Euclidean Algorithm - Discrete Mathematics - Lecture Slides, Slides of Discrete Mathematics

During the study of discrete mathematics, I found this course very informative and applicable.The main points in these lecture slides are:Euclidean Algorithm, Recursive Algorithms, Recursive Step, Non-Negative Integer, Procedure Factorial, Nonzero Real Number, Recursive Version, Binary Search Algorithm, Proving Recursive Algorithm, Inductive Step

Typology: Slides

2012/2013

Uploaded on 04/27/2013

aslesha
aslesha 🇮🇳

4.4

(14)

160 documents

1 / 42

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE115/ENGR160 Discrete Mathematics
04/05/12
1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a

Partial preview of the text

Download Euclidean Algorithm - Discrete Mathematics - Lecture Slides and more Slides Discrete Mathematics in PDF only on Docsity!

CSE115/ENGR160 Discrete Mathematics 04/05/

5.4 Recursive algorithms

  • An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input
  • Give a recursive algorithm for computing n! where n is a non-negative integer
  • We can compute n!=n∙(n-1)! Where n is a positive integer, and that 0!=1 for a particular integer
  • We use the recursive step n times

Recursive algorithm for an

  • procedure power(a: nonzero real number, n: non-negative integer) if n=0 then power(a,n):= else power(a,n):=a ∙ power(a, n-1)

Example

  • Formulate a recursive algorithm for computing bn^ mod m, where b, n and m are integers with m≥2, n≥0, 1≤b<m
  • Let m be a positive integer and let a and b be integers, then (a+b) mod m=((a mod m) + (b mod m)) mod m ab mod m = ((a mod m)(b mod m)) mod m
  • Thus, b n^ mod m = (b (b n-1^ mod m)) mod m, and b 0 mod m = 1

Example

  • procedure mpower(b, n, m: integers with m≥2, n≥0) if n=0 then mpower(b, n, m)= else if n is even then mpower(b, n, m)=mpower(b, n/2, m)^2 mod m else mpower(b, n, m)=mpower(b, ⌞n/2⌟, m)^2 mod m ∙ b mod m) mod m {mpower(b, n, m)=bn^ mod m}
  • Trace the above algorithm with b=2, n=5, and m=3 (2^5 mod 3)
  • Since n is odd, we have mpower(2, 5, 3)=(mpower(2,2,3)^2 mod 3 ∙ 2 mod 3) mod 3
  • Next, mpower(2,2,3)=mpower(2,1,3)^2 mod 3, and mpower(2,1,3)= (mpower(2,0,3)^2 mod 3 ∙ 2 mod 3) mod 3, and mpower(2,0,3)=
  • So, mpower(2,1,3)=(1^2 mod 3 ∙ 2 mod 3) mod 3 = 2
  • So, power(2, 2, 3)=2 2 mod 3 =1 and finally mpower(2, 5, 3)=(1 2 mod 3 ∙ 2 mod 3) mod 3 =2 7

Example

  • Give a recursive algorithm for gcd(a, b) with a < b
  • A recursive version of Euclidean algorithm: b=aq+r, gcd(b,a)=gcd(a,r)
  • procedure gcd(a, b: non-negative integers with a < b) if a=0 then gcd(a, b):=b else gcd(a, b):=gcd(b mod a, a)
  • Let a=5 and b=8. gcd(5, 8)=gcd(8 mod 5, 5)=gcd(3, 5)
  • Next, gcd(3, 5)=gcd(5 mod 3, 3)=gcd(2, 3), then gcd(2, 3)=gcd (3 mod 2, 2)=gcd(1, 2). Finally gcd(1,2)=gcd(2 mod 1, 2) = gcd(0, 1)=1. Thus, gcd(5,8)=1 (^) Docsity.com 8

Proving recursive algorithm

  • Prove that the recursive algorithm power(a, n) is correct
  • procedure power(a: nonzero real number, n: non-negative integer) if n=0 then power(a,n):= else power(a,n):=a ∙ power(a, n-1)
  • Basis step: If n=0, power(a,0)=1. This is correct as a 0 =

Proving recursive algorithm

  • Show the recursive algorithm for a n
  • Basis step: n=0, power(a,0)=
  • Inductive step: The inductive hypothesis is power(a,k)=a k^ for a≠0 and non-negative k. To complete the proof, we need to show power(a,k+1)= a k+
  • As k+1 is a positive integer, the algorithms sets power(a, k+1)=a∙power(a, k)=a∙a k^ = a k+
  • This completes the inductive step

Recursion and iteration

  • fibonacci(4)

Iterative algorithm for Fibonacci

Number

  • procedure iterative_fibonacci(n: nonzero integer) if n=0 then y:= else begin x:= y:= for i:=1 to n- z:=x+y x:=y y:=z end end {y is the n-th Fibonacci number}
  • Example: Sort the list 8, 2, 4, 6, 9, 7, 10, 1, 5, 3
  • Merge sort: split the list into individual elements by successively splitting lists into two
  • Sorting is done by successively merging pairs of lists

16

Merge sort

Merge sort

  • In general, a merge sort proceeds by iteratively splitting lists into two sbulists represented by a balanced binary tree
  • We can also describe the merge sort recursively
  • procedure mergesort(L=a 1 , …, a (^) n ) if n>1 then m:= ⌞n/2⌟ L 1 =a 1 , a 2 , …, a (^) m L 2 =a (^) m+1 , …, a (^) n L=merge(mergesort(L 1 ), mergesort(L 2 )) {L is a sorted list in non-decreasing order}

Merge sort

  • procedure merge(L1, L 2 : sorted lists) L:=empty set while L1and L 2 are both non-empty begin remove smaller of first element of L 1 and L 2 from the list it is in and put it at the right end of L if removal of this element makes one list empty then remove all elements from the other list and append them to L end {L is a sorted list in non-decreasing order}

Merge sort

  • Two sorted lists with m elements and n elements can be merged into a sorted list using no more than m+n-1 comparisons