



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Introduction to Algorithm Analysis and Design The Idea of an Algorithm We begin our formal lectures with a definition of the term algorithm. The definition of algorithm , taken from the textbook, is only partial. An algorithm is a sequence of unambiguous instructions for solving a problem. The full definition must include the provision that the algorithm terminate for any valid input. So we have the following definition of algorithm, taken from an another text. Definition: An algorithm is a finite set of instructions which, if followed, will accomplish a particular task. In addition every algorithm must satisfy the following criteria: i) input : there are zero or more quantities which are externally supplied; ii) output : at least one quantity is produced; iii) definiteness : each instruction must be clear and unambiguous; iv) finiteness : if we trace out the instructions of the algorithm, then for all valid cases the algorithm will terminate after a finite number of steps; v) effectiveness : every instruction must be sufficiently basic that it can in principle be carried out by a person using only a pencil and paper. It is not enough that each operation be definite as in (iii), but it must be feasible. [Hor76] The effectiveness criterion might be restated as it being possible to map each step in the algorithm to a simple instruction in the base computer language. This violates the idea that algorithms exist independently of programming languages but corresponds to the basic practice that algorithms are quite often expressed using a programming language. The first algorithm for study is Euclid’s algorithm for computing the greatest common divisor of two positive integers. This algorithm has a very simple expression apart from any programming language. For integers M and N (assume M ≥ N), gcd(M, N) = gcd (N, M mod N) The book computes gcd(60,24) = 12. Let’s compute a more interesting example. gcd(225, 63) = gcd(63, 36) as 225 = 63 3 + 36, so 225 mod 63 = 36 = gcd(36, 27) as 63 = 36 1 + 27, so 63 mod 36 = 27 = gcd(27, 9) as 36 = 27 1 + 9, so 36 mod 27 = 9 = gcd(9, 0) as 27 = 9 3, so 27 mod 9 = 0 = 9 as gcd(N, 0) = N for any N ≥ 0.
We now discuss an iterative description of the algorithm. Algorithm Euclid ( m , n ) // Computes the greatest common divisor of integers // m and n , using Euclid’s algorithm. // Input: Two nonnegative integers m and n , not both // zero. It is not necessary that m ≥ n. // Output: The greatest common divisor of m and n. while n 0 do r m mod n m n n r end while return m We make a few random comments before commenting on the algorithm. The first is the applicability of the criterion of placing the larger number first, so that we compute the greatest common divisor gcd(M, N) with M ≥ N. The rule is gcd(M, N) = gcd (N, M mod N). Suppose that M < N; then M mod N = M and we have gcd(M, N) = gcd (N, M) and we then compute the greatest common divisor with the larger number placed first. Thus, the requirement M ≥ N is not necessary, but only an artifact of executing the algorithm by hand. The second point is that, as a software engineer, I am uncomfortable writing code that might crash. The following represents a better programming implementation. Algorithm Euclid ( m , n ) // Computes the greatest common divisor of integers // m and n , using Euclid’s algorithm. // Input: Two nonnegative integers m and n. // Output: The greatest common divisor of m and n. // First take care of the case for either argument = 0 if ( ( n == 0) or ( m == 0) ) return 0 m = | m | // Force both numbers to be positive n = | n | // as required by the algorithm while n 0 do r m mod n m n n r end while return m
Let us apply the iterative version of Euclid’s algorithm to compute the same number. The main body of the loop is show below. while n 0 do // enter with m = 146 and n = 142 r m mod n m n n r end while return m Lets follow the execution while n 0 do // n = 142 r m mod n // r = 146 mod 142 = 4 m n // m = 142 n r // n = 4 end while while n 0 do // n = 4 r m mod n // r = 142 mod 4 = 2 m n // m = 4 n r // n = 2 end while while n 0 do // n = 2 r m mod n // r = 4 mod 2 = 0 m n // m = 2 n r // n = 0 end while return m // return 2 as n == 0. A third algorithm is presented, based on the prime factors of the two numbers M and N. Consider the first problem we did: computation of the greatest common divisor of the two integers 225 and 63. The prime factorization of these two numbers is 225 = 9 25 = 3^2 52 , and 63 = 7 9 = 3^2 7. We then look for the factors common to both of the integers; here the largest common factor is 3^2 = 9, which is the greatest common divisor of 225 and 63. While this is a great way to generate examples for the greatest common divisor problems, it is not suitable for implementation as an algorithm. The main difficulty is the difficulty in obtaining a list of prime numbers. The algorithm also has some efficiency problems.
Other Considerations We consider algorithms to be procedural solutions to problems. There are a number of other considerations when trying to design a specific algorithm. One consideration is whether or not one wants to obtain an exact solution to every instance of the problem. Some problems, such as the Traveling Salesman Problem, admit easy solutions when those solutions need be only approximate. Pseudocode is the most common device used to specify an algorithm. Most often this pseudocode is a variant of a programming language with some of the grammatical constraints removed. The rule for writing instructions in pseudocode is that every instruction be unambiguous and convertible to a finite number of instructions in the machine’s assembly language. For the study of algorithms, we find that there are a number of problem types that should be considered. These problems are