Algorithms Chapter 3 3 1 Introduction p An















- Slides: 15
Algorithms Chapter 3
3. 1 Introduction p An algorithm is a finite set of instructions with the following characteristics: v Precision: steps are precisely stated v Uniqueness: Results of each step of execution are uniquely defined. They depend only on inputs and results of preceding steps v Finiteness: the algorithm stops after finitely many steps
More characteristics of algorithms v Input: the algorithm receives input v Output: the algorithm produces output v 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 p 2. If b > x then x: = b p 3. If c > x then x: = c A trace is a check of the algorithm for specific values of a, b and c p
3. 2 Notation for algorithms Pseudocode: Instructions given in a generic language similar to a computer language such as C++ or Pascal. n Else n Procedure n Return n If-then, action n While loop n If-then-else n For loop n begin n End
3. 3 The Euclidean algorithm Divisors: q 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. q A common divisor of two integers m and n is a positive integer k such that k|m and k|n.
Euclidean algorithm q 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). q 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. q Thus, gcd(120, 80) = gcd(80, 40) q
3. 4 Recursive algorithms q A recursive procedure is a procedure that invokes itself q 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-2)!, etc. q A recursive algorithm is an algorithm that contains a recursive procedure q
Fibonacci sequence Leonardo Fibonacci (Pisa, Italy, ca. 1170 -1250) q Fibonacci sequence f 1, f 2, … defined recursively as follows: f 1 = 1 f 2 = 2 fn = fn-1 + fn-2 for n > 3 q First terms of the sequence are: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, … q
3. 5 Complexity of algorithms p Complexity: the amount of time and/or space needed to execute the algorithm. p 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 n Worst-case time = maximum time needed to execute the algorithm for inputs of size n n Average-case time = average time needed n
Order of an algorithm Let f and g be functions with domain Z+ = {1, 2, 3, …} q f(n) = O(g(n)): f(n) is of order at most g(n) q q f(n) = (g(n)): f(n) is of order at least g(n) q q if there exists a positive constant C 1 such that |f(n)| < C 1|g(n)| for all but finitely many 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 > fn+1 and b > fn+1, where {fn} 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 (2 m/3)
3. 7 The RSA public-key cryptosystem q Cryptosystems: systems for secure communications q q Used by government, industry, investigation agencies, etc. Sender encrypts a message Receiver decrypts the message RSA (Rivest, Shamir, Adleman) system q q Messages are represented as numbers Based on the fact that no efficient algorithm exists for factoring large digit integers in polynomial time O(nk).