-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathsubsection2.xml
More file actions
781 lines (756 loc) · 25.9 KB
/
subsection2.xml
File metadata and controls
781 lines (756 loc) · 25.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
<SUBSECTION>
<NAME>Tree Recursion</NAME>
<LABEL NAME="sec:tree-recursion"/>
<INDEX>tree-recursive process<OPEN/></INDEX>
<INDEX>process<SUBINDEX>tree-recursive<OPEN/></SUBINDEX></INDEX>
<INDEX>recursive process<SUBINDEX>tree<OPEN/></SUBINDEX></INDEX>
<TEXT>
Another common pattern of computation is called <EM>tree recursion</EM>.
As an example, consider computing the sequence of
<INDEX>Fibonacci numbers</INDEX>
Fibonacci numbers,
in which each number is the sum of the preceding two:
<LATEX>
\[\begin{array}{l}
0, 1, 1, 2, 3, 5, 8, 13, 21, \ldots
\end{array}\]
</LATEX>
In general, the Fibonacci numbers can be defined by the rule
<LATEX>
\[\begin{array}{lll}
\textrm{Fib}(n) & = & \left\{ \begin{array}{ll}
0 & \mbox{if $n=0$}\\
1 & \mbox{if $n=1$}\\
\textrm{Fib}(n-1)+\textrm{Fib}(n-2) & \mbox{otherwise}
\end{array}
\right.
\end{array}\]
</LATEX>
We can immediately translate this definition into a recursive
<SPLITINLINE>
<SCHEME>procedure</SCHEME>
<JAVASCRIPT>function</JAVASCRIPT>
</SPLITINLINE>
for computing Fibonacci numbers:
<SNIPPET PAGE="37">
<INDEX><DECLARATION>fib</DECLARATION><SUBINDEX>tree-recursive version</SUBINDEX></INDEX>
<NAME>fib_definition</NAME>
<EXAMPLE>fib_example</EXAMPLE>
<SCHEME>
(define (fib n)
(cond ((= n 0) 0)
((= n 1) 1)
(else (+ (fib (- n 1))
(fib (- n 2))))))
</SCHEME>
<JAVASCRIPT>
function fib(n) {
return n === 0
? 0
: n === 1
? 1
: fib(n - 1) + fib(n - 2);
}
</JAVASCRIPT>
</SNIPPET>
<SNIPPET PAGE="37" HIDE="yes">
<NAME>fib_example</NAME>
<REQUIRES>fib_definition</REQUIRES>
<EXPECTED>8</EXPECTED>
<SCHEME>
(fib 6)
</SCHEME>
<JAVASCRIPT>
fib(6);
</JAVASCRIPT>
</SNIPPET>
<SPLIT>
<SCHEME>
<FIGURE>
<FIGURE src="img_original/ch1-Z-G-13.svg"></FIGURE>
<LABEL NAME="fig:fib-tree_scheme"/>
<CAPTION>The tree-recursive process generated in computing
<SCHEMEINLINE>(fib 5)</SCHEMEINLINE>.
</CAPTION>
</FIGURE>
</SCHEME>
<JAVASCRIPT>
<FIGURE>
<FIGURE src="img_javascript/ch1-Z-G-13.svg"></FIGURE>
<LABEL NAME="fig:fib-tree"/>
<CAPTION>The tree-recursive process generated in computing
<JAVASCRIPTINLINE>fib(5)</JAVASCRIPTINLINE>.
</CAPTION>
</FIGURE>
</JAVASCRIPT>
</SPLIT>
</TEXT>
<TEXT>
Consider the pattern of this computation. To compute
<SPLITINLINE>
<SCHEME><SCHEMEINLINE>(fib 5)</SCHEMEINLINE>,</SCHEME>
<JAVASCRIPT><JAVASCRIPTINLINE>fib(5)</JAVASCRIPTINLINE>,</JAVASCRIPT>
</SPLITINLINE>
we compute
<SPLITINLINE>
<SCHEME><SCHEMEINLINE>(fib 4)</SCHEMEINLINE></SCHEME>
<JAVASCRIPT><JAVASCRIPTINLINE>fib(4)</JAVASCRIPTINLINE></JAVASCRIPT>
</SPLITINLINE>
and
<SPLITINLINE>
<SCHEME><SCHEMEINLINE>(fib 3)</SCHEMEINLINE>.</SCHEME>
<JAVASCRIPT><JAVASCRIPTINLINE>fib(3)</JAVASCRIPTINLINE>.</JAVASCRIPT>
</SPLITINLINE>
To compute
<SPLITINLINE>
<SCHEME><SCHEMEINLINE>(fib 4)</SCHEMEINLINE>,</SCHEME>
<JAVASCRIPT><JAVASCRIPTINLINE>fib(4)</JAVASCRIPTINLINE>,</JAVASCRIPT>
</SPLITINLINE>
we compute
<SPLITINLINE>
<SCHEME><SCHEMEINLINE>(fib 3)</SCHEMEINLINE></SCHEME>
<JAVASCRIPT><JAVASCRIPTINLINE>fib(3)</JAVASCRIPTINLINE></JAVASCRIPT>
</SPLITINLINE>
and
<SPLITINLINE>
<SCHEME><SCHEMEINLINE>(fib 2)</SCHEMEINLINE>.</SCHEME>
<JAVASCRIPT><JAVASCRIPTINLINE>fib(2)</JAVASCRIPTINLINE>.</JAVASCRIPT>
</SPLITINLINE>
In general, the evolved process looks like a tree, as shown in
<SPLITINLINE>
<SCHEME>
figure<SPACE/><REF NAME="fig:fib-tree_scheme"/>.
</SCHEME>
<JAVASCRIPT>
figure<SPACE/><REF NAME="fig:fib-tree"/>.
</JAVASCRIPT>
</SPLITINLINE>
Notice that the branches split into
two at each level (except at the bottom); this reflects the fact that the
<SCHEMEINLINE>fib</SCHEMEINLINE>
<SPLITINLINE>
<SCHEME>procedure</SCHEME>
<JAVASCRIPT>function</JAVASCRIPT>
</SPLITINLINE>
calls itself twice each time it is invoked.
</TEXT>
<TEXT>
This
<SPLITINLINE>
<SCHEME>procedure</SCHEME>
<JAVASCRIPT>function</JAVASCRIPT>
</SPLITINLINE>
is instructive as a prototypical tree recursion, but it is a terrible way to
compute Fibonacci numbers because it does so much redundant computation.
Notice in
<SPLITINLINE>
<SCHEME>
figure<SPACE/><REF NAME="fig:fib-tree_scheme"/>
</SCHEME>
<JAVASCRIPT>
figure<SPACE/><REF NAME="fig:fib-tree"/>
</JAVASCRIPT>
</SPLITINLINE>
that the entire
computation of
<SPLITINLINE>
<SCHEME>
<SCHEMEINLINE>(fib 3)</SCHEMEINLINE><EMDASH/>almost
half the work<EMDASH/>is
</SCHEME>
<JAVASCRIPT>
<JAVASCRIPTINLINE>fib(3)</JAVASCRIPTINLINE><EMDASH/>almost
half the work<EMDASH/>is
</JAVASCRIPT>
</SPLITINLINE>
duplicated. In fact, it is not hard to show that the number of times the
<SPLITINLINE>
<SCHEME>procedure</SCHEME>
<JAVASCRIPT>function</JAVASCRIPT>
</SPLITINLINE>
will compute
<SPLITINLINE>
<SCHEME><SCHEMEINLINE>(fib 1)</SCHEMEINLINE></SCHEME>
<JAVASCRIPT><JAVASCRIPTINLINE>fib(1)</JAVASCRIPTINLINE></JAVASCRIPT>
</SPLITINLINE>
or
<SPLITINLINE>
<SCHEME>
<SCHEMEINLINE>(fib 0)</SCHEMEINLINE>
</SCHEME>
<JAVASCRIPT>
<JAVASCRIPTINLINE>fib(0)</JAVASCRIPTINLINE>
</JAVASCRIPT>
</SPLITINLINE>
(the number of leaves in the above tree, in general) is precisely
<LATEXINLINE>$\textrm{Fib}(n+1)$</LATEXINLINE>. To get an idea of how
bad this is, one can show that the value of
<LATEXINLINE>$\textrm{Fib}(n)$</LATEXINLINE>
<INDEX>exponential growth<SUBINDEX><ORDER>tree-recursive</ORDER>of tree-recursive Fibonacci-number computation</SUBINDEX></INDEX>
grows exponentially with <LATEXINLINE>$n$</LATEXINLINE>. More precisely
(see exercise<SPACE/><REF NAME="ex:fib-proof"/>),
<LATEXINLINE>$\textrm{Fib}(n)$</LATEXINLINE> is the closest integer to
<LATEXINLINE>$\phi^{n} /\sqrt{5}$</LATEXINLINE>, where
<LATEX>
\[\begin{array}{lllll}
\phi&=&(1+\sqrt{5})/2 & \approx & 1.6180
\end{array}\]
</LATEX>
is the
<INDEX>golden ratio</INDEX>
<EM>golden ratio</EM>, which satisfies the equation
<LATEX>
\[\begin{array}{lll}
\phi^{2} &=&\phi + 1
\end{array}\]
</LATEX>
Thus, the process uses a number of steps that grows exponentially with the
input. On the other hand, the space required grows only linearly with the
input, because we need keep track only of which nodes are above us in the
tree at any point in the computation. In general, the number of steps
required by a tree-recursive process will be proportional to the number of
nodes in the tree, while the space required will be proportional to the
maximum depth of the tree.
</TEXT>
<TEXT>
We can also formulate an iterative process for computing the Fibonacci
numbers. The idea is to use a pair of integers <LATEXINLINE>$a$</LATEXINLINE>
and <LATEXINLINE>$b$</LATEXINLINE>, initialized to
<LATEXINLINE>$\textrm{Fib}(1)=1$</LATEXINLINE> and
<LATEXINLINE>$\textrm{Fib}(0)=0$</LATEXINLINE>, and to repeatedly apply the
simultaneous transformations
<LATEX>
\[\begin{array}{lll}
a & \leftarrow & a+b \\
b & \leftarrow & a
\end{array}\]
</LATEX>
It is not hard to show that, after applying this transformation
<LATEXINLINE>$n$</LATEXINLINE> times, <LATEXINLINE>$a$</LATEXINLINE> and
<LATEXINLINE>$b$</LATEXINLINE> will be equal, respectively, to
<LATEXINLINE>$\textrm{Fib}(n+1)$</LATEXINLINE> and
<LATEXINLINE>$\textrm{Fib}(n)$</LATEXINLINE>. Thus, we can compute
Fibonacci numbers iteratively using the
<SPLITINLINE>
<SCHEME>procedure</SCHEME>
<JAVASCRIPT>function</JAVASCRIPT>
</SPLITINLINE>
<SNIPPET PAGE="39">
<INDEX><DECLARATION>fib</DECLARATION><SUBINDEX>linear iterative version</SUBINDEX></INDEX>
<EXAMPLE>fib_example</EXAMPLE>
<EXPECTED>8</EXPECTED>
<SCHEME>
(define (fib n)
(fib-iter 1 0 n))
(define (fib-iter a b count)
(if (= count 0)
b
(fib-iter (+ a b) a (- count 1))))
</SCHEME>
<JAVASCRIPT>
function fib(n) {
return fib_iter(1, 0, n);
}
function fib_iter(a, b, count) {
return count === 0
? b
: fib_iter(a + b, a, count - 1);
}
</JAVASCRIPT>
</SNIPPET>
This second method for computing <LATEXINLINE>$\textrm{Fib}(n)$</LATEXINLINE>
is a linear iteration. The difference in number of steps required by the two
methods<EMDASH/>one linear in <LATEXINLINE>$n$</LATEXINLINE>, one growing as
fast as <LATEXINLINE>$\textrm{Fib}(n)$</LATEXINLINE> itself<EMDASH/>is
enormous, even for small inputs.
</TEXT>
<TEXT>
One should not conclude from this that tree-recursive processes are useless.
When we consider processes that operate on hierarchically structured data
rather than numbers, we will find that tree recursion is a natural and
powerful tool.<FOOTNOTE>An example of this was hinted at in
section<SPACE/><REF NAME="sec:evaluating-combinations"/>: The interpreter
itself evaluates expressions using a tree-recursive process.</FOOTNOTE> But
even in numerical operations, tree-recursive processes can be useful in
helping us to understand and design programs. For instance, although the
first
<SCHEMEINLINE>fib</SCHEMEINLINE>
<SPLITINLINE>
<SCHEME>procedure</SCHEME>
<JAVASCRIPT>function</JAVASCRIPT>
</SPLITINLINE>
is much less efficient than the second one, it is more straightforward,
being little more than a translation into
<SPLITINLINE>
<SCHEME>
Lisp
</SCHEME>
<JAVASCRIPT>
JavaScript
</JAVASCRIPT>
</SPLITINLINE>
of the definition of the Fibonacci sequence. To formulate the iterative
algorithm required noticing that the computation could be recast as an
iteration with three state variables.
</TEXT>
<SUBHEADING>
<NAME>Example: Counting change</NAME>
</SUBHEADING>
<INDEX>counting change<OPEN/></INDEX>
<TEXT>
It takes only a bit of cleverness to come up with the iterative Fibonacci
algorithm. In contrast, consider the following problem:
How many different ways can we make change of
<SPLITINLINE>
<SCHEME>
<LATEXINLINE>\$1.00</LATEXINLINE>,
</SCHEME>
<JAVASCRIPT>
<DOLLAR/>1.00 (100 cents),
</JAVASCRIPT>
</SPLITINLINE>
given half-dollars, quarters, dimes, nickels, and pennies
(50 cents, 25 cents, 10 cents, 5 cents, and 1 cent, respectively)?
More generally, can
we write a
<SPLITINLINE>
<SCHEME>procedure</SCHEME>
<JAVASCRIPT>function</JAVASCRIPT>
</SPLITINLINE>
to compute the number of ways to change any given amount of money?
</TEXT>
<TEXT>
This problem has a simple solution as a recursive
<SPLITINLINE>
<SCHEME>procedure.</SCHEME>
<JAVASCRIPT>function.</JAVASCRIPT>
</SPLITINLINE>
Suppose we think of the types of coins available as arranged in some order.
Then the following relation holds:
<BLOCKQUOTE>
The number of ways to change amount <LATEXINLINE>$a$</LATEXINLINE> using
<LATEXINLINE>$n$</LATEXINLINE> kinds of coins equals
<UL>
<LI>
the number of ways to change amount <LATEXINLINE>$a$</LATEXINLINE>
using all but the first kind of coin, plus
</LI>
<LI>
the number of ways to change amount <LATEXINLINE>$a-d$</LATEXINLINE>
using all <LATEXINLINE>$n$</LATEXINLINE> kinds of coins, where
<LATEXINLINE>$d$</LATEXINLINE> is the denomination of the first kind
of coin.
</LI>
</UL>
</BLOCKQUOTE>
</TEXT>
<TEXT>
To see why this is true, observe that the ways to make change can be divided
into two groups: those that do not use any of the first kind of coin, and
those that do. Therefore, the total number of ways to make change for some
amount is equal to the number of ways to make change for the amount without
using any of the first kind of coin, plus the number of ways to make change
assuming that we do use the first kind of coin. But the latter number is
equal to the number of ways to make change for the amount that remains after
using a coin of the first kind.
</TEXT>
<TEXT>
Thus, we can recursively reduce the problem of changing a given amount to
problems of changing smaller amounts or using fewer kinds of coins. Consider
this reduction rule carefully, and convince yourself that we can use it to
describe an algorithm if we specify the following degenerate
cases:<FOOTNOTE>For example, work through in detail how the reduction rule
applies to the problem of making change for 10 cents using pennies and
nickels.</FOOTNOTE>
<UL>
<LI>
If <LATEXINLINE>$a$</LATEXINLINE> is exactly 0, we should count that
as 1 way to make change.
</LI>
<LI>
If <LATEXINLINE>$a$</LATEXINLINE> is less than 0, we should count
that as 0 ways to make change.
</LI>
<LI> If <LATEXINLINE>$n$</LATEXINLINE> is 0, we should count that
as 0 ways to make change.
</LI>
</UL>
We can easily translate this description into a recursive
<SPLITINLINE>
<SCHEME>procedure:</SCHEME>
<JAVASCRIPT>function:</JAVASCRIPT>
</SPLITINLINE>
<SNIPPET PAGE="40-41">
<INDEX><DECLARATION>count_change</DECLARATION></INDEX>
<NAME>count_change_definition</NAME>
<EXAMPLE>count_change_example</EXAMPLE>
<SCHEME>
(define (count-change amount)
(cc amount 5))
(define (cc amount kinds-of-coins)
(cond ((= amount 0) 1)
((or (< amount 0)
(= kinds-of-coins 0)) 0)
(else (+ (cc amount
(- kinds-of-coins 1))
(cc (- amount
(first-denomination
kinds-of-coins))
kinds-of-coins)))))
(define (first-denomination kinds-of-coins)
(cond ((= kinds-of-coins 1) 1)
((= kinds-of-coins 2) 5)
((= kinds-of-coins 3) 10)
((= kinds-of-coins 4) 25)
((= kinds-of-coins 5) 50)))
</SCHEME>
<JAVASCRIPT>
function count_change(amount) {
return cc(amount, 5);
}
<SHORT_SPACE/>
function cc(amount, kinds_of_coins) {
return amount === 0
? 1
: amount < 0 || kinds_of_coins === 0
? 0
: cc(amount, kinds_of_coins - 1)
+
cc(amount - first_denomination(kinds_of_coins),
kinds_of_coins);
}
<SHORT_SPACE_AND_ALLOW_BREAK/>
function first_denomination(kinds_of_coins) {
return kinds_of_coins === 1 ? 1
: kinds_of_coins === 2 ? 5
: kinds_of_coins === 3 ? 10
: kinds_of_coins === 4 ? 25
: kinds_of_coins === 5 ? 50
: 0;
}
</JAVASCRIPT>
</SNIPPET>
(The
<SPLITINLINE>
<SCHEME>
<SCHEMEINLINE>first-denomination</SCHEMEINLINE> procedure
</SCHEME>
<JAVASCRIPT>
<JAVASCRIPTINLINE>first_denomination</JAVASCRIPTINLINE> function
</JAVASCRIPT>
</SPLITINLINE>
takes as input the number of kinds of coins available and returns the
denomination of the first kind. Here we are thinking of the coins as
arranged in order from largest to smallest, but any order would do as well.)
We can now answer our original question about changing a dollar:
<SNIPPET PAGE="41">
<NAME>count_change_example</NAME>
<REQUIRES>count_change_definition</REQUIRES>
<EXPECTED>292</EXPECTED>
<SCHEME>
(count-change 100)
</SCHEME>
<SCHEMEOUTPUT>
292
</SCHEMEOUTPUT>
<JAVASCRIPT>
count_change(100);
</JAVASCRIPT>
<JAVASCRIPT_OUTPUT>
292
</JAVASCRIPT_OUTPUT>
</SNIPPET>
</TEXT>
<TEXT>
<SPLITINLINE>
<SCHEME><SCHEMEINLINE>Count-change</SCHEMEINLINE></SCHEME>
<JAVASCRIPT>
The function <JAVASCRIPTINLINE>count_change</JAVASCRIPTINLINE>
</JAVASCRIPT>
</SPLITINLINE>
generates a tree-recursive process with redundancies similar to those in
our first implementation of <SCHEMEINLINE>fib</SCHEMEINLINE>.
<SPLITINLINE>
<SCHEME>
(It will take
quite a while for that
<SPLITINLINE>
<SCHEME>292</SCHEME>
<JAVASCRIPT>293</JAVASCRIPT>
</SPLITINLINE>
to be computed.)
</SCHEME>
</SPLITINLINE>
On the other hand, it is not
obvious how to design a better algorithm for computing the result, and we
leave this problem as a challenge. The observation that a
<INDEX>efficiency<SUBINDEX><ORDER>tree</ORDER>of tree-recursive process</SUBINDEX></INDEX>
tree-recursive process may be highly inefficient but often easy to specify
and understand has led people to propose that one could get the best of both
worlds by designing a <QUOTE>smart compiler</QUOTE> that could transform
tree-recursive
<SPLITINLINE>
<SCHEME>procedures</SCHEME>
<JAVASCRIPT>functions</JAVASCRIPT>
</SPLITINLINE>
into more efficient
<SPLITINLINE>
<SCHEME>procedures</SCHEME>
<JAVASCRIPT>functions</JAVASCRIPT>
</SPLITINLINE>
that compute the same result.<FOOTNOTE>One approach to coping with redundant
computations is to arrange matters so that we automatically construct a
table of values as they are computed. Each time we are asked to apply the
<SPLITINLINE>
<SCHEME>procedure</SCHEME>
<JAVASCRIPT>function</JAVASCRIPT>
</SPLITINLINE>
to some argument, we first look to see if the value is already stored in the
table, in which case we avoid performing the redundant computation. This
strategy, known as
<INDEX>tabulation</INDEX>
<EM>tabulation</EM> or
<INDEX>memoization</INDEX>
<EM>memoization</EM>, can be implemented in a
straightforward way. Tabulation can sometimes be used to transform processes
that require an exponential number of steps
<SPLITINLINE>
<SCHEME>
(such as <SCHEMEINLINE>count-change</SCHEMEINLINE>)
</SCHEME>
<JAVASCRIPT>
(such as <JAVASCRIPTINLINE>count_change</JAVASCRIPTINLINE>)
</JAVASCRIPT>
</SPLITINLINE>
into processes whose space and time requirements grow linearly with the
input. See exercise<SPACE/><REF NAME="ex:memoization"/>.</FOOTNOTE>
<INDEX>tree-recursive process<CLOSE/></INDEX>
<INDEX>process<SUBINDEX>tree-recursive<CLOSE/></SUBINDEX></INDEX>
<INDEX>recursive process<SUBINDEX>tree<CLOSE/></SUBINDEX></INDEX>
<INDEX>counting change<CLOSE/></INDEX>
</TEXT>
<EXERCISE>
A function <LATEXINLINE>$f$</LATEXINLINE> is defined by the
<SPLITINLINE>
<SCHEME>
rule that
</SCHEME>
<JAVASCRIPT>
rules
</JAVASCRIPT>
</SPLITINLINE>
<LATEXINLINE>$f(n)=n$</LATEXINLINE> if <LATEXINLINE>$n < 3$</LATEXINLINE>
and <LATEXINLINE>$f(n)={f(n-1)}+2f(n-2)+3f(n-3)$</LATEXINLINE> if
<LATEXINLINE>$n\ge 3$</LATEXINLINE>. Write a
<SPLITINLINE>
<SCHEME>procedure</SCHEME>
<JAVASCRIPT>JavaScript function</JAVASCRIPT>
</SPLITINLINE>
that computes <LATEXINLINE>$f$</LATEXINLINE> by means of a recursive process.
Write a
<SPLITINLINE>
<SCHEME>procedure</SCHEME>
<JAVASCRIPT>function</JAVASCRIPT>
</SPLITINLINE>
that computes <LATEXINLINE>$f$</LATEXINLINE> by means of an iterative
process.
<SOLUTION>
<SNIPPET>
<EXAMPLE>example_1.12_1</EXAMPLE>
<EXPECTED>25</EXPECTED>
<JAVASCRIPT>
// iterative function
function f_iterative(n) {
return n < 3
? n
: f_iterative_impl(2, 1, 0, n - 2);
}
function f_iterative_impl(a, b, c, count) {
return count === 0
? a
: f_iterative_impl(a + 2 * b + 3 * c, a, b, count - 1);
}
</JAVASCRIPT>
</SNIPPET>
<SNIPPET>
<EXAMPLE>example_1.12_2</EXAMPLE>
<EXPECTED>25</EXPECTED>
<JAVASCRIPT>
//recursive function
function f_recursive(n) {
return n < 3
? n
: f_recursive(n - 1) +
2 * f_recursive(n - 2) +
3 * f_recursive(n - 3);
}
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
<SNIPPET HIDE="yes">
<NAME>example_1.12_1</NAME>
<JAVASCRIPT>
f_iterative(5);
</JAVASCRIPT>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>example_1.12_2</NAME>
<JAVASCRIPT>
f_recursive(5);
</JAVASCRIPT>
</SNIPPET>
<LABEL NAME="ex:1_11"/>
</EXERCISE>
<EXERCISE>
The following pattern of numbers is called
<INDEX>Pascal<APOS/>s triangle</INDEX>
<EM>Pascal<APOS/>s triangle</EM>.
<LATEX>
\[
{
\begin{array}{rrrrcrrrr}
& & & & 1 & & & & \\
& & &1 & &1 & & & \\
& &1 & & 2 & &1 & & \\
&1 & &3 & &3 & &1 & \\
1 & & 4 & & 6 & & 4 & & 1 \\
& & & & \ldots & & & &
\end{array}}
\]
</LATEX>
The numbers at the edge of the triangle are all 1, and each number inside
the triangle is the sum of the two numbers above it.<FOOTNOTE>The elements
of Pascal<APOS/>s triangle are called the <EM>binomial coefficients</EM>,
because the <LATEXINLINE>$n$</LATEXINLINE>th row consists of
<INDEX>binomial coefficients</INDEX>
the coefficients of the terms in the expansion of
<LATEXINLINE>$(x+y)^n$</LATEXINLINE>. This pattern for computing the
coefficients
appeared in
<INDEX><ORDER>Pascal</ORDER>Pascal, Blaise</INDEX>
Blaise Pascal<APOS/>s 1653 seminal work on probability theory,
<EM>Trait<EACUTE_LOWER/> du triangle arithm<EACUTE_LOWER/>tique</EM>.
According to
<INDEX>Edwards, Anthony William Fairbank</INDEX>
Edwards (2019), the same pattern appears
in the works of
the eleventh-century Persian mathematician
<INDEX>Al-Karaji</INDEX>
Al-Karaji,
in the works of the twelfth-century Hindu mathematician
<INDEX>Bhaskara</INDEX>
Bhaskara, and
in the works of the
thirteenth-century Chinese mathematician
<INDEX>Yang Hui</INDEX>
Yang Hui.
</FOOTNOTE>
Write a
<SPLITINLINE>
<SCHEME>procedure</SCHEME>
<JAVASCRIPT>function</JAVASCRIPT>
</SPLITINLINE>
that computes elements of Pascal<APOS/>s triangle by means of a recursive
process.
<SOLUTION>
Assuming that we count the rows of the triangle from the top starting
at 1 and the numbers in each row using indices from left to right
starting at 1, the following function computes the number in the
triangle in a given row at a given index.
<SNIPPET>
<NAME>pascal_triangle</NAME>
<EXAMPLE>example_1.13</EXAMPLE>
<JAVASCRIPT>
function pascal_triangle(row, index) {
return index > row
? false
: index === 1 || index===row
? 1
: pascal_triangle(row - 1, index - 1)
+
pascal_triangle(row - 1, index);
}
</JAVASCRIPT>
<SCHEME>
</SCHEME>
</SNIPPET>
</SOLUTION>
<SNIPPET HIDE="yes">
<NAME>example_1.13</NAME>
<REQUIRES>pascal_triangle</REQUIRES>
<EXPECTED>4</EXPECTED>
<JAVASCRIPT>
pascal_triangle(5, 4);
</JAVASCRIPT>
<SCHEME>
</SCHEME>
</SNIPPET>
<LABEL NAME="ex:1_12"/>
</EXERCISE>
<EXERCISE>
Prove that <LATEXINLINE>$\textrm{Fib}(n)$</LATEXINLINE> is the closest
integer to <LATEXINLINE>$\phi^n/\sqrt{5}$</LATEXINLINE>, where
<LATEXINLINE>$\phi= (1+\sqrt{5})/2$</LATEXINLINE>.
<SPLIT>
<SCHEME>
Hint: Let
<LATEXINLINE>$\psi= (1-\sqrt{5})/2$</LATEXINLINE>. Use induction and the
definition of the Fibonacci numbers (see
section<SPACE/><REF NAME="sec:tree-recursion"/>) to prove that
<LATEXINLINE>$\textrm{Fib}(n)=(\phi^n-\psi^n)/\sqrt{5}$</LATEXINLINE>.
</SCHEME>
<JAVASCRIPT>
Hint: Use induction and the
definition of the Fibonacci numbers to prove that
<LATEXINLINE>$\textrm{Fib}(n)=(\phi^n-\psi^n)/\sqrt{5}$</LATEXINLINE>,
where
<LATEXINLINE>$\psi= (1-\sqrt{5})/2$</LATEXINLINE>.
</JAVASCRIPT>
</SPLIT>
<LABEL NAME="ex:fib-proof"/>
<SOLUTION>
First, we show that
<LATEXINLINE>$\textrm{Fib}(n) =
\dfrac{\phi^n-\psi^n}{\sqrt{5}}$</LATEXINLINE>,
where
<LATEXINLINE>$\psi = \dfrac{1-\sqrt{5}}{2}$</LATEXINLINE>
using strong induction.
<BR/>
<LATEXINLINE>$\textrm{Fib}(0) = 0$</LATEXINLINE>
and
<LATEXINLINE>$\dfrac{\phi^0-\psi^0}{\sqrt{5}} = 0$</LATEXINLINE>
<BR/>
<LATEXINLINE>$\textrm{Fib}(1) = 1$</LATEXINLINE>
and
<LATEXINLINE>$\dfrac{\phi^1-\psi^1}{\sqrt{5}} =
\dfrac{\dfrac{1}{2}\left(1+\sqrt{5} - 1 + \sqrt{5}\right)}{\sqrt{5}} = 1$
</LATEXINLINE>
<BR/>
So the statement is true for <LATEXINLINE>$n=0,1$</LATEXINLINE>.
Given <LATEXINLINE>$n \geq 1$</LATEXINLINE>, assume the proposition
to be true for <LATEXINLINE>$0, 1, \dots , n$</LATEXINLINE>.
<BR/>
<LATEXINLINE>$\textrm{Fib}(n+1) =
\textrm{Fib}(n) + \textrm{Fib}(n-1) =
\dfrac{\phi^n-\psi^n + \phi^{n-1} - \psi^{n-1}}{\sqrt{5}}$</LATEXINLINE>
<BR/>
<LATEXINLINE>$= \dfrac{\phi^{n-1}(\phi + 1) -
\psi^{n-1}(\psi + 1)}{\sqrt{5}}$
</LATEXINLINE>
<BR/>
<LATEXINLINE>$=\dfrac{\phi^{n-1}(\phi^2) - \psi^{n-1}(\psi^2)}{\sqrt{5}}
= \dfrac{\phi^{n+1} - \psi^{n+1}}{\sqrt{5}}$</LATEXINLINE>,
so the statement is true.
<BR/>
Notice that since <LATEXINLINE>$|\psi| < 1$</LATEXINLINE> and
<LATEXINLINE>$\sqrt{5} > 2$</LATEXINLINE>, one has
<LATEXINLINE>$\left|\dfrac{\psi^n}{\sqrt{5}}\right| <
\dfrac{1}{2}$</LATEXINLINE>
<BR/>
Then the integer closest to
<LATEXINLINE>$\textrm{Fib}(n) + \dfrac{\psi^n}{\sqrt{5}} =
\dfrac{\phi^n}{\sqrt{5}}$</LATEXINLINE> is
<LATEXINLINE>$\textrm{Fib}(n)$</LATEXINLINE>.
</SOLUTION>
</EXERCISE>
</SUBSECTION>