25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 101<br />

Programs in which the use of algorithmic recursion is to be avoided can be characterized by a schema<br />

which exhibits the pattern of their composition. The equivalent schemata are shown below. Their<br />

characteristic is that there is only a single call of P either at the end (or the beginning) of the composition.<br />

P ≡ IF B THEN S; P END<br />

P ≡ S; IF B THEN P END<br />

These schemata are natural in those cases in which values are to be computed that are defined in terms of<br />

simple recurrence relations. Let us look at the well-known example of the factorial numbers f i = i!:<br />

i = 0, 1, 2, 3, 4, 5, ...<br />

f i = 1, 1, 2, 6, 24, 120, ...<br />

The first number is explicitly defined as f 0 = 1, whereas the subsequent numbers are defined recursively in<br />

terms of their predecessor:<br />

f i+1 = (i+1) * f i<br />

This recurrence relation suggests a recursive algorithm to compute the n-th factorial number. If we<br />

introduce the two variables I <strong>and</strong> F to denote the values i <strong>and</strong> f i at the i-th level of recursion, we find the<br />

computation necessary to proceed to the next numbers in the sequences to be<br />

I := I + 1; F := I * F<br />

<strong>and</strong>, substituting these two statements for S, we obtain the recursive program<br />

P<br />

≡ IF I < n THEN I := I + 1; F := I * F; P END<br />

I := 0; F := 1; P<br />

The first line is expressed in terms of our conventional programming notation as<br />

PROCEDURE P;<br />

BEGIN<br />

IF I < n THEN I := I + 1; F := I*F; P END<br />

END P<br />

A more frequently used, but essentially equivalent, form is the one given below. P is replaced by a function<br />

procedure F, i.e., a procedure with which a resulting value is explicitly associated, <strong>and</strong> which therefore may<br />

be used directly as a constituent of expressions. The variable F therefore becomes superfluous; <strong>and</strong> the role<br />

of I is taken over by the explicit procedure parameter.<br />

PROCEDURE F(I: INTEGER): INTEGER;<br />

BEGIN<br />

IF I > 0 THEN RETURN I * F(I - 1) ELSE RETURN 1 END<br />

END F<br />

It now is plain that in this example recursion can be replaced quite simply by iteration. This is expressed<br />

by the program<br />

I := 0; F := 1;<br />

WHILE I < n DO I := I + 1; F := I*F END<br />

In general, programs corresponding to the original schemata should be transcribed into one according to<br />

the following schema:<br />

P<br />

≡ [x := x0; WHILE B DO S END]<br />

There also exist more complicated recursive composition schemes that can <strong>and</strong> should be translated into<br />

an iterative form. An example is the computation of the Fibonacci numbers which are defined by the<br />

recurrence relation

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!