Docsity

Introduction to Algorithm Analysis and Design - Notes | CPSC 5115G, Study notes of Computer Science

Toggle sidebar
bg1
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.
Page 1 of 6 Chapter 1 Last Revised On August 25, 2004
pf3
pf4
pf5

Partial preview of the text

Download Introduction to Algorithm Analysis and Design - Notes | CPSC 5115G and more Study notes Computer Science in PDF only on Docsity!

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 rm mod n mn nr 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 rm mod n mn nr 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 rm mod n mn nr end while return m Lets follow the execution while n  0 do // n = 142 rm mod n // r = 146 mod 142 = 4 mn // m = 142 nr // n = 4 end while while n  0 do // n = 4 rm mod n // r = 142 mod 4 = 2 mn // m = 4 nr // n = 2 end while while n  0 do // n = 2 rm mod n // r = 4 mod 2 = 0 mn // m = 2 nr // 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

  1. Sorting a list that supports a defined comparison operator a) Sorting by comparison. b) Sorting when another mechanism is allowed.
  2. Searching a list that supports a defined equality operator
  3. String processing
  4. Graph traversal problems (also called graph search problems)
  5. Combinatorial problems – how to generate certain combinatorial objects, such as all permutations of the set {1, 2, …., N}.
  6. Geometric problems
  7. Numerical problems. Is A Program An Algorithm? One can argue that every program specifies an algorithm, in that a program must be a finite set of steps, each defined in a specific programming language, which produces a specific result. Under this definition, the only way that a program can fail to be an algorithm is if the program enters an endless loop. By definition, all algorithms must terminate and produce an answer, so that a program or any other sequence of instructions that fails to terminate cannot be considered an algorithm. For the purpose of this course, we arbitrarily say that a program is not an algorithm. The reason for this statement is that this course will focus on what the algorithm is to do, assuming that all inputs are valid and that no errors occur in the processing. Both of these assumptions are very dangerous in the implementation of programs. However, each of the assumptions is valid in the study of algorithms as it allows us to focus on the other issues of importance. The specific requirement for this course is that homework answers contain only a description of the algorithm assigned and not include such extra, but important, features such as user input of the data, output of the data, or error checking (unless specified in the assignment). One reason for this requirement is that it greatly facilitates the grading.