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 102<br />

fib n+1 = fib n + fib n-1 for n > 0<br />

<strong>and</strong> fib 1 = 1, fib 0 = 0. A direct, naive transcription leads to the recursive program<br />

PROCEDURE Fib (n: INTEGER): INTEGER;<br />

VAR res: INTEGER;<br />

BEGIN<br />

IF n = 0 THEN res := 0<br />

ELSIF n = 1 THEN res := 1<br />

ELSE res := Fib(n-1) + Fib(n-2)<br />

END;<br />

RETURN res<br />

END Fib<br />

Computation of fib n by a call Fib(n) causes this function procedure to be activated recursively. How<br />

often? We notice that each call with n > 1 leads to 2 further calls, i.e., the total number of calls grows<br />

exponentially (see Fig. 3.2). Such a program is clearly impractical.<br />

5<br />

4<br />

3<br />

3<br />

2<br />

2<br />

1<br />

2<br />

1<br />

1<br />

0<br />

1<br />

0<br />

1<br />

0<br />

Fig. 3.2. The 15 activations of Fib(5)<br />

But fortunately the Fibonacci numbers can be computed by an iterative scheme that avoids the<br />

recomputation of the same values by use of auxiliary variables such that x = fib i <strong>and</strong> y = fib i-1 .<br />

i := 1; x := 1; y := 0;<br />

WHILE i < n DO z := x; x := x + y; y := z; i := i + 1 END<br />

Note: The assignments to x, y, z may be expressed by two assignments only without a need for the<br />

auxiliary variable z: x := x + y; y := x - y.<br />

Thus, the lesson to be drawn is to avoid the use of recursion when there is an obvious solution by<br />

iteration. This, however, should not lead to shying away from recursion at any price. There are many good<br />

applications of recursion, as the following paragraphs <strong>and</strong> chapters will demonstrate. The fact that<br />

implementations of recursive procedures on essentially non-recursive machines exist proves that for<br />

practical purposes every recursive program can be transformed into a purely iterative one. This, however,<br />

involves the explicit h<strong>and</strong>ling of a recursion stack, <strong>and</strong> these operations will often obscure the essence of a<br />

program to such an extent that it becomes most difficult to comprehend. The lesson is that algorithms which<br />

by their nature are recursive rather than iterative should be formulated as recursive procedures. In order to<br />

appreciate this point, the reader is referred to the algorithms for QuickSort <strong>and</strong> NonRecursiveQuickSort<br />

in Sect. 2.3.3 for a comparison.<br />

The remaining part of this chapter is devoted to the development of some recursive programs in<br />

situations in which recursion is justifiably appropriate. Also Chap. 4 makes extensive use of recursion in<br />

cases in which the underlying data structures let the choice of recursive solutions appear obvious <strong>and</strong><br />

natural.

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

Saved successfully!

Ooh no, something went wrong!