R. Johnsonbaugh, Discrete Mathematics 5 th edition, 2001 Chapter 3 Algorithms
3.1 Introduction An algorithm is a finite set of instructions with the following characteristics: Precision: steps are precisely stated Uniqueness: Results of each step of execution are uniquely defined. They depend only on inputs and results of preceding steps Finiteness: the algorithm stops after finitely many steps
More characteristics of algorithms Input: the algorithm receives input Output: the algorithm produces output Generality: the algorithm applies to various sets of inputs
Example: a simple algorithm Algorithm to find the largest of three numbers a, b, c: Assignment operator s := k means “copy the value of k into s” 1. x:= a 2. If b > x then x:= b 3. If c > x then x:= c A trace is a check of the algorithm for specific values of a, b and c
3.2 Notation for algorithms Pseudocode: Instructions given in a generic language similar to a computer language such as C++ or Pascal. Procedure If-then, action If-then-else begin Else Return While loop For loop End
3.3 The euclidean algorithm Divisors: Given an integer n, we say that k is a divisor of n or k divides n, notation: k|n, if k is a positive integer n = kq for some integer q called the quotient. A common divisor of two integers m and n is a positive integer k such that k|m and k|n.
Euclidean algorithm Given two integers m and n, the gcd(m,n) or greatest common divisor of m and n is a common divisor k > 1 such that k is the largest of all common divisors of m and n. The Euclidean algorithm finds the gcd(m, n). Theorem 3.3.6: If a is a nonnegative integer, b is a positive integer, and r = a mod b, then gcd(a,b) = gcd(b,r) Example: if a = 120, b = 80, then r = 40 = 120 mod 80. Thus, gcd(120,80) = gcd(80,40)
3.4 Recursive algorithms A recursive procedure is a procedure that invokes itself Example: given a positive integer n, factorial of n is defined as the product of n by all numbers less than n and greater than 0. Notation: n! = n(n-1)(n-2)…3.2.1 Observe that n! = n(n-1)! = n(n-1)(n-2)!, etc. A recursive algorithm is an algorithm that contains a recursive procedure
Fibonacci sequence Leonardo Fibonacci (Pisa, Italy, ca ) Fibonacci sequence f 1, f 2,… defined recursively as follows: f 1 = 1 f 2 = 2 f n = f n-1 + f n-2 for n > 3 First terms of the sequence are: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597,…
3.5 Complexity of algorithms Complexity: the amount of time and/or space needed to execute the algorithm. Complexity depends on many factors: data representation type, kind of computer, computer language used, etc.
Types of complexity Best-case time = minimum time needed to execute the algorithm for inputs of size n Worst-case time = maximum time needed to execute the algorithm for inputs of size n Average-case time = average time needed
Order of an algorithm Z Let f and g be functions with domain Z + = {1, 2, 3,…} f(n) = O(g(n)): f(n) is of order at most g(n) if there exists a positive constant C 1 such that |f(n)| < C 1 |g(n)| for all but finitely many n f(n) = (g(n)): f(n) is of order at least g(n) if there exists a positive constant C 2 such that |f(n)| > C 2 |g(n)| for all but finitely many n f(n) = (g(n)): f(n) is or order g(n) if it is O(g(n)) and (g(n)).
3.6 Analysis of the Euclidean algorithm Theorem 3.6.1: Suppose that the pair a, b with a > b requires n >1 modulus operations when input to the Euclidean algorithm. Then a > f n+1 and b > f n+1, where {f n } is the Fibonacci sequence.
Number of operations Theorem 3.6.2: If integers in the range 0 to m, m > 8, not both zero, are input to the Euclidean algorithm, then the number of modulus operations required is at most log 3/2 (2m/3)
3.7 The RSA public-key cryptosystem Cryptosystems: systems for secure communications Used by government, industry, investigation agencies, etc. Sender encrypts a message Receiver decripts the message RSA (Rivest, Shamir, Adleman) system Messages are represented as numbers Based on the fact that no efficient algorithm exists for factoring large digit integers in polynomial time O(n k ).