Docsity

Understanding Algorithms: Termination and Recursion - Prof. R. Metoyer, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
Chapter 2: Algorithms 1
Chapter 2: Algorithms
A computer must be given a series of commands, termed a program, in order to make it
perform a useful task. Because programs can be large, they are divided into smaller units,
termed (depending upon your programming language), functions, procedures, methods,
subroutines, subprograms, classes, or packages. In programs that you have written you
have undoubtedly used some of these features.
A function, for example, is used to
package a task to be performed. Let us
use as an example a function to
compute the square root of a floating-
point number. Among other benefits,
the function is providing an
abstraction. By this we mean that from
the outside, from the point of view of
the person calling the function, they
only need to know the name of the
action to be performed. On the other hand, inside the function the programmer must have
written the exact series of instructions needed to perform the task. Since to use the
function you do not need to know the internal details, we say that the function is a form
of encapsulation.
The difference between the information necessary to use a function and the information
necessary to write a function, the fact that you can use a function without knowing how it
is implemented, is one of the most important tools used to manage complex systems. In
your own programming experience you have undoubtedly used libraries of functions, for
performing tasks such as input and output, or manipulating graphical interfaces. You can
use these functions with only a minimal knowledge of how they go about their task. This
makes developing programs infinitely easier than if every programmer had to write all of
these actions from scratch.
There is another level of abstraction that is equally important. Let us again illustrate this
using the function to compute a square root. In order to make this function available for
your use, at some point in the past there must have been a programmer who developed
the code that you invoke when you call the square root function. Although this
programmer may never have written this particular function, they did not start entirely
from scratch. Fortunately for this programmer, there are well known techniques that can
be used to solve this problem. In this case, the technique was most likely something
termed Newton’s Method. This technique, discovered by Issac Newton (1643-1727),
finds a square root by first making a guess, then using the first guess to find a better
guess, and so on until the correct answer is located.
Newton, of course, lived well before the advent of computers, and so never wrote his
technique as a computer function. Instead, he described an algorithm. An algorithm is a
description of how a specific problem can be solved, written at a level of detail that can
double sqrt (double val) {
/* return square root of argument */
/* works only for positive values */
assert (val >= 0);
double guess = val / 2.0;
…
return guess;
}
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Understanding Algorithms: Termination and Recursion - Prof. R. Metoyer and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Chapter 2: Algorithms

A computer must be given a series of commands, termed a program , in order to make it perform a useful task. Because programs can be large, they are divided into smaller units, termed (depending upon your programming language), functions, procedures, methods, subroutines, subprograms, classes, or packages. In programs that you have written you have undoubtedly used some of these features.

A function, for example, is used to package a task to be performed. Let us use as an example a function to compute the square root of a floating- point number. Among other benefits, the function is providing an abstraction. By this we mean that from the outside, from the point of view of the person calling the function, they only need to know the name of the action to be performed. On the other hand, inside the function the programmer must have written the exact series of instructions needed to perform the task. Since to use the function you do not need to know the internal details, we say that the function is a form of encapsulation.

The difference between the information necessary to use a function and the information necessary to write a function, the fact that you can use a function without knowing how it is implemented, is one of the most important tools used to manage complex systems. In your own programming experience you have undoubtedly used libraries of functions, for performing tasks such as input and output, or manipulating graphical interfaces. You can use these functions with only a minimal knowledge of how they go about their task. This makes developing programs infinitely easier than if every programmer had to write all of these actions from scratch.

There is another level of abstraction that is equally important. Let us again illustrate this using the function to compute a square root. In order to make this function available for your use, at some point in the past there must have been a programmer who developed the code that you invoke when you call the square root function. Although this programmer may never have written this particular function, they did not start entirely from scratch. Fortunately for this programmer, there are well known techniques that can be used to solve this problem. In this case, the technique was most likely something termed Newton’s Method. This technique, discovered by Issac Newton (1643-1727), finds a square root by first making a guess, then using the first guess to find a better guess, and so on until the correct answer is located.

Newton, of course, lived well before the advent of computers, and so never wrote his technique as a computer function. Instead, he described an algorithm. An algorithm is a description of how a specific problem can be solved, written at a level of detail that can

double sqrt (double val) { /* return square root of argument / / works only for positive values */ assert (val >= 0); double guess = val / 2.0; … return guess; }

be followed by the reader. Terms that have a related meaning include process, routine, technique, procedure, pattern, and recipe.

It is important that you understand the distinction between an algorithm and a function. A function is a set of instructions written in a particular programming language. A function will often embody an algorithm, but the essence of the algorithm transcends the function, transcends even the programming language. Newton described, for example, the way to find square roots, and the details of the algorithm will remain the same no matter what programming language is used to write the code.

Properties of Algorithms

Once you have an algorithm, to solve a problem is simply a matter of executing each instruction of the algorithm in turn. If this process is to be successful there are several characteristics an algorithm must possess. Among these are:

input preconditions. The most common form of algorithm is a transformation that takes a set of input values and performs some manipulations to yield a set of output values. (For example, taking a positive number as input, and returning a square root). An algorithm must make clear the number and type of input values, and the essential initial conditions those input values must possess to achieve successful operation. For example, Newtons method works only if the input number is larger than zero.

precise specification of each instruction. Each step of an algorithm must be well defined. There should be no ambiguity about the actions to be carried out at any point. Algorithms presented in an informal descriptive form are sometimes ill-defined for this reason, due to the ambiguities in English and other natural languages.

correctness. An algorithm is expected to solve a problem. For any putative algorithm, we must demonstrate that, in fact, the algorithm will solve the problem. Often this will take the form of an argument, mathematical or logical in nature, to the effect that if the input conditions are satisfied and the steps of the algorithm executed then the desired outcome will be produced.

termination, time to execute. It must be clear that for any input values the algorithm is guaranteed to terminate after a finite number of steps. We postpone until later a more precise definition of the informal term “steps.'” It is usually not necessary to know the exact number of steps an algorithm will require, but it will be convenient to provide an upper bound and argue that the algorithm will always terminate in fewer steps than the upper bound. Usually this upper bound will be given as a function of some values in the input. For example, if the input consists of two integer values n and m, we might be able to say that a particular algorithm will always terminate in fewer than n + m steps.

description of the result or effect. Finally, it must be clear exactly what the algorithm is intended to accomplish. Most often this can be expressed as the production of a result value having certain properties. Less frequently algorithms are executed for a side effect ,

Instruction Precision

Algorithms must be specified in a form that can be followed to produce the desired outcome. In this book we will generally present algorithms in a high level pseudo-code, a form that can easily be translated into a working program in a given programming language. Using a pseudo-code form allows us to emphasize the important properties of the algorithm, and downplay incidental details that, while they may be necessary for a working program, do nothing to assist in the understanding of the procedure.

Time to execute

Traditionally the discussion of execution time is divided into two separate questions. The first question is showing that the algorithm will terminate for all legal input values. Once this question has been settled, the next question is to provide a more accurate characterization of the amount of time it will take to execute. This second question will be considered in more detail in a later section. Here, we will address the first.

The basic tool used to prove that an algorithm terminates is to find a property or value that satisfies the following three characteristics:

  1. The property or value can be placed into a correspondence with integer values.
  2. The property is nonnegative.
  3. The property or value decreases steadily as the algorithm executes.

The majority of the time this value is obvious, and is left unstated. For instance, if an algorithm operates by examining each element of an array in turn, we can use the remaining size of the array (that is, the unexplored elements) as the property we seek. It is only in rare occasions that an explicit argument must be provided.

An example in which termination may not be immediately obvious occurs in Euclid's Greatest Common Divisor algorithm, first developed in third century B.C. Greece. Donald Knuth, a computer scientist who has done a great deal of research into the history of algorithms, has claimed that this is the earliest-known nontrivial completely specified mathematical algorithm. The algorithm is intended to compute, given two positive integer values n and m, the largest positive integer that evenly divides both values. The algorithm can be written as shown at right.

Because there is no definite limit on the number of iterations, termination is not obvious. But we can note that either n or m

int gcd (int n, int m) { /* compute the greatest common divisor of two positive integer values */ while (m != n) { if (n > m) n = n - m; else m = m - n; } return n; }

becomes smaller on each iteration, and so the sum n+m satisfies our three conditions, and hence can be used to demonstrate that the function will halt.

Recursive Algorithms

Recursion is a very common technique used for both algorithms and the functions that result from implementing algorithms. The basic idea of recursion is to “reduce” a complex problem by “simplifying” it in some way, then invoking the algorithm on the simpler problem. This is performed repeated until one or more special cases, termed base

Zeno’s Paradox

The most nonintuitive of the three properties required for proving termination is the first: that the quantity or property being discussed can be placed into a correspondence with a diminishing list of integers. Why integer? Why not just numeric? Why diminishing? To illustrate the necessity of this requirement, consider the following “proof” that it is possible to share a single candy bar with all your friends, no matter how many friends you may have. Take a candy bar and cut it in half, giving one half to your best friend. Take the remaining half and cut it in half once more, giving a portion to your second best friend. Assuming you have a sufficiently sharp knife, you can continue in this manner, at each step producing ever-smaller pieces, for as long as you like. Thus, we have no guarantee of termination for this process. Certainly the sequence here (1/2, ¼, 1/8, 1/16 …) consists of all nonnegative terms, and is decreasing. But a correspondence with the integers would need to increase, not decrease; and any diminishing sequence would be numeric, but not integer.

In the 5th century B.C. the Greek philosopher Zeno used similar arguments to show that no matter how fast he runs, the famous hero Achilles cannot overtake a Tortoise, if the Tortoise is given a head start. Suppose, for example, that the Tortoise and Achilles start as below at time A. When, at time B, Achilles reaches the point from which the Tortoise started, the Tortoise will have proceeded some distance ahead of this point. When Achilles reaches this further point at time C, the Tortoise will have proceeded yet further ahead. At each point when Achilles reaches a point from which the Tortoise began, the Tortoise will have proceeded at least some distance further. Thus, it was impossible for Achilles to ever overtake and pass the Tortoise.

Achiles A -> B -> C -> D Tortoise A -> B -> C -> D

The paradox arises due to an infinite sequence of ever-smaller quantities, and the nonintuitive possibility that an infinite sum can nevertheless be bounded by a finite value. By restricting our arguments concerning termination to use only a decreasing set of integer values, we avoid problems such as those typified by Zeno's paradox.

printDigit, that handles only the values zero to nine. In trying to understand how a recursive function can work, it is important to remember that every time a procedure is invoked, new space is allocated for parameters and local variables. So each time the function printInteger is called, a new value of n is created. Previous values of n are left pending, stacked up so that they can be restored after the function returns. This is called the activation record stack. A portion of the activation record stack is shown at right, a snapshot showing the various values of n that are pending when the value 4 is printed at the end of the series of recursive calls. Once the latest version of printInteger finishes the caller, who just happens to be printInteger, will be restarted. But in the caller the value of variable n will have been restored to the value 49.

A classic example of a recursive algorithm is the puzzle known as the Towers of Hanoi. In this puzzle, three poles are labeled A, B and C. A number of disks of decreasing sizes are, initially, all on pole A.

A B C

The goal of the puzzle is to move all disks from pole A to pole B, without ever moving a disk onto another disk with a smaller size. The third pole can be used as a temporary during this process. At any point, only the topmost disk from any pole may be moved.

A B C

It is obvious that only two first steps are legal. These are moving the smallest disk from pole A to pole B, or moving the smallest disk from pole A to pole C. But which of these is the correct first move?

As is often the case with recursive algorithms, a greater insight is found by considering a point in the middle of solving the problem, rather that thinking about how to begin. For example, consider the point we would like to be one step before we move the largest disk. For this to be a legal move, we must have already moved all the disks except for the largest from pole A to pole C.

N = 4

N = 49

N = 497

N = 4973

A B C

Notice that in this picture we have moved all but the last disk to pole C. Once we move the largest disk, pole A will be empty, and we can use it as a temporary. This observation hints at how to express the solution to the problem as a recursive algorithm.

How to move N disks from pole A to pole B using pole C as a temporary: If N is 1, move disk from A to B. Otherwise, move (N – 1) disks from pole A to pole C using pole B as a temporary. Move the largest disk from pole A to pole B. Then move disks from pole C to pole B, using pole A as a temporary.

We could express this algorithm as pseudo-code as shown. Here the arguments are used to represent the size of the stack and the names of the poles, and the instructions for solving the puzzle are printed.

We will see many recursive algorithms as we proceed through the text.

Study Questions

  1. What is abstraction? Explain how a function is a form of abstraction.
  2. What is an algorithm?
  3. What is the relationship between an algorithm and a function?
  4. Give an example input condition that cannot be specified using only a type.
  5. What are two different techniques used to specify the input conditions for an algorithm?
  6. What are some ways used to describe the outcome, or result, of executing an algorithm?
  7. In what way does the precision of instructions needed to convey an algorithm to another human being differ from that needed to convey an algorithm to a computer?

void solveHanoi (int n, char a, char b, char c) { if (n == 1) print (“move disk from pole ”, a, “ to pole ”, b); else { solveHanoi (n – 1, a, c, b); print (“move disk from pole “, a, “ to pole “, b); solveHanoi (n – 1, c, b, a); } }

picture

Write a recursive procedure comb (n, m) to compute this value.

  1. Rewrite the GCD algorithm as a recursive procedure.
  2. List the sequence of moved the Towers of Hanoi problem would make in moving a tower of four disks from the source pole to the destination pole.
  3. The Fibonacci sequence is another famous series that is defined recursively. The definition is as follows:

f 1 = 1 f 2 = 1 fn = fn-1 + fn-

Write in pseudo-code a function to compute the nth^ Fibonacci number.

  1. A palindrome is a word that reads the same both forward and backwards, such as rotor. An algorithm to determine whether or not a word is a palindrome can be expressed recursively. Simply strip off the first and last letters; if they are different, the word is not a palindrome. If they are, test the remaining string (after the first and last letters have been removed) to see if it is a palindrome. What is your base case for this procedure?
  2. Extend the procedure you wrote in the previous question so that it can handle (by ignoring them) spaces and punctuation. An example of this sort is “A man, a plan, a canal, panama!”
  3. Describe in pseudo-code, but do not implement, a recursive algorithm for finding the smallest number in an Array between two integer bounds (that is, a typical call such as smallest(a, 3, 7) would find the smallest value in the array a among those elements with indices between 3 and 7. What is the base case for your algorithm? How does the inductive case simplify the problem?
  4. Using the algorithm you developed in the preceding question describe in pseudo-code a recursive sorting algorithm that works by finding the smallest element, swapping it into the first position, then finding the smallest element in the remainder, swapping it into the next position, and so on. What is the base case for your algorithm?

Programming Projects

  1. Write a recursive procedure to print the octal (base-8) representation of an integer value.
  1. Write a program that takes an integer argument and prints the value spelled out as English words. For example, the input - 3472 would produce the output “negative three thousand four hundred seventy-two”. After removing any initial negative signs, this program is most easily handled in a recursive fashion. Numbers greater than millions can be printed by printing the number of millions (via a recursive call), then printing the work “million”, then printing the remainder. Similarly with thousands, hundreds, and most values larger than twenty. Base cases are zero, numbers between 1 and 20.
  2. Write a program to compute the fibonocci sequence recursively (see earlier exercise). Include a global variable fibCount that is incremented every time the function calls itself recursively. Using this, determine the number of calls for various values of N, such as n from 1 to 20. Can you determine a relationship between n and the resulting number of calls?
  3. Do the same sort of analysis as described in the preceding question, but this time for the towers of Hanoi.

On the Web

Wikipedia (http://en.wikipedia.org) has a detailed entry for Algorithm that provides a history of the word, and several examples illustrating the way algorithms can be presented. The GCD algorithm is described in an entry “Euclidian Algorithm”. Another interesting entry can be found on “recursive algorithms”. The entry on “Fibonacci Numbers” provides an interesting history of the sequence, as well as many of the mathematical relationships of these numbers. The entry of “Towers of Hanoi” includes an animated applet that demonstrates the solution for N equal to 4. The more mathematically inclined might want to explore the entries on “Fractals” and “Mathematical Induction”, and the relationship of these to recursion. Newtons method of computing square roots is described in an entry on “Methods of computing square roots”. Another example of recursive art in commercial advertising is the “Morton Salt girl”, who is spilling a container of salt, on which is displayed a picture of the Morton salt girl.