jeffgerickson / algorithms

Bug-tracking for Jeff's algorithms book, notes, etc.
7.92k stars 1.02k forks source link

[Oops.] May be there is a error in Figure 1.4 #170

Open voook opened 5 years ago

voook commented 5 years ago

Please verify that the error is present in the most recent revision before reporting.

Chapter number or note title: [title] in Figure 1.4 at 1st edition

Page number: [page] P26 or Figure 1.4 at 1st edition

Error description: [error]

in Figure 1.4

Hanoi(n, src, dst, tmp):
    if n > 0
        Hanoi(n − 1, src, tmp, dst) 〈〈 Recurse! 〉〉
        move disk n from src to dst           <<--------- may be error,I'm not certain
        Hanoi(n − 1, tmp, dst, src) 〈〈 Recurse! 〉〉

Suggested fix (if any): [fix]

suggested change to:

Hanoi(n, src, dst, tmp):
    if n > 0
        Hanoi(n − 1, src, tmp, dst) 〈〈 Recurse! 〉〉
        directly move disk from src to dst               // only this line changed
        Hanoi(n − 1, tmp, dst, src) 〈〈 Recurse! 〉〉

explanation:

 directly move disk from src to dst       <<--------- now n should be 1

thought base on:

Page 22

1.2 Simplify and Delegate

Recursion is a particularly powerful kind of reduction, which can be described
loosely as follows:

    • If the given instance of the problem can be solved directly, solve it directly.
    • Otherwise, reduce it to one or more simpler instances of the same problem.