Tower of Hanoi Final

download Tower of Hanoi Final

of 32

Transcript of Tower of Hanoi Final

  • 8/8/2019 Tower of Hanoi Final

    1/32

    The Towers of HanoiThe Towers of Hanoi

    oror

    ApocalypseApocalypse WhenWhen??

  • 8/8/2019 Tower of Hanoi Final

    2/32

  • 8/8/2019 Tower of Hanoi Final

    3/32

    A Legend (A Legend (CtdCtd))

    The priests were to transfer the disks from the first needleThe priests were to transfer the disks from the first needleto the second needle, using the third as necessary.to the second needle, using the third as necessary.

    But they couldBut they could only moveonly move one disk at a timeone disk at a time, and could, and couldnever put a larger disk on top of a smaller onenever put a larger disk on top of a smaller one..

    When they completed this task,When they completed this task, the world would endthe world would end!!

  • 8/8/2019 Tower of Hanoi Final

    4/32

  • 8/8/2019 Tower of Hanoi Final

    5/32

    ExampleExample

    For simplicity, suppose there were just 3 disks, and wellFor simplicity, suppose there were just 3 disks, and wellrefer to the three needles as A, B, and C...refer to the three needles as A, B, and C...

    We then move the top disk from A to C.We then move the top disk from A to C.

  • 8/8/2019 Tower of Hanoi Final

    6/32

    Example (Example (CtdCtd))

    For simplicity, suppose there were just 3 disks, and wellFor simplicity, suppose there were just 3 disks, and wellrefer to the three needles as A, B, and C...refer to the three needles as A, B, and C...

    We then move the top disk from B to C.We then move the top disk from B to C.

  • 8/8/2019 Tower of Hanoi Final

    7/32

    Example (Example (CtdCtd))

    For simplicity, suppose there were just 3 disks, and wellFor simplicity, suppose there were just 3 disks, and wellrefer to the three needles as A, B, and C...refer to the three needles as A, B, and C...

    We then move the top disk from A to B.We then move the top disk from A to B.

  • 8/8/2019 Tower of Hanoi Final

    8/32

    Example (Example (CtdCtd))

    For simplicity, suppose there were just 3 disks, and wellFor simplicity, suppose there were just 3 disks, and wellrefer to the three needles as A, B, and C...refer to the three needles as A, B, and C...

    We then move the top disk from C to A.We then move the top disk from C to A.

  • 8/8/2019 Tower of Hanoi Final

    9/32

    Example (Example (CtdCtd))

    For simplicity, suppose there were just 3 disks, and wellFor simplicity, suppose there were just 3 disks, and wellrefer to the three needles as A, B, and C...refer to the three needles as A, B, and C...

    We then move the top disk from C to B.We then move the top disk from C to B.

  • 8/8/2019 Tower of Hanoi Final

    10/32

    Example (Example (CtdCtd))

    For simplicity, suppose there were just 3 disks, and wellFor simplicity, suppose there were just 3 disks, and wellrefer to the three needles as A, B, and C...refer to the three needles as A, B, and C...

    We then move the top disk from A to B.We then move the top disk from A to B.

  • 8/8/2019 Tower of Hanoi Final

    11/32

    Example (Example (CtdCtd))

    For simplicity, suppose there were just 3 disks, and wellFor simplicity, suppose there were just 3 disks, and wellrefer to the three needles as A, B, and C...refer to the three needles as A, B, and C...

    and were done!and were done!

    The problem gets more difficult as the number of disksThe problem gets more difficult as the number of disksincreases...increases...

  • 8/8/2019 Tower of Hanoi Final

    12/32

    Our ProblemOur Problem

    Todays problem is to write a program that generates theTodays problem is to write a program that generates theinstructions for the priests to follow in moving the disks.instructions for the priests to follow in moving the disks.

    While quite difficult to solve iteratively, this problem hasWhile quite difficult to solve iteratively, this problem hasa simple and eleganta simple and elegant recursiverecursive solution.solution.

  • 8/8/2019 Tower of Hanoi Final

    13/32

    AnalysisAnalysis

    For flexibility, lets allow the user to enter the number ofFor flexibility, lets allow the user to enter the number ofdisks for which they wish a set of instructions:disks for which they wish a set of instructions:

    /* hanoi.cpp

    * ...*/

    void Move(int n, char src, char dest, char aux);

    int main(){

    cout numDisks;

    Move(numDisks, A, B, C);}

  • 8/8/2019 Tower of Hanoi Final

    14/32

    Analysis (Ctd)Analysis (Ctd)

    Our task, then is to write function Move() that does allOur task, then is to write function Move() that does allthe work:the work:

    /* hanoi.cpp

    * ...*/

    void Move(int n, char src, char dest, char aux);

    int main(){

    cout numDisks;

    Move(numDisks, A, B, C);}

  • 8/8/2019 Tower of Hanoi Final

    15/32

    DesignDesign

    Basis: What is an instance of the problem that is trivial?Basis: What is an instance of the problem that is trivial?

    p n == 1n == 1

    Since this base case could occur when the disk is on anySince this base case could occur when the disk is on anyneedle, we simply output the instruction to move theneedle, we simply output the instruction to move thetop disk fromtop disk from srcsrc toto destdest..

  • 8/8/2019 Tower of Hanoi Final

    16/32

  • 8/8/2019 Tower of Hanoi Final

    17/32

    Design (Design (CtdCtd))

    Induction Step: n > 1Induction Step: n > 1

    pHow can recursion help us out?How can recursion help us out?

    a.a. RecursivelyRecursively move nmove n--1 disks from1 disks from srcsrc toto auxaux..

  • 8/8/2019 Tower of Hanoi Final

    18/32

    Design (Design (CtdCtd))

    Induction Step: n > 1Induction Step: n > 1

    pHow can recursion help us out?How can recursion help us out?

    b. Move the one remaining disk fromb. Move the one remaining disk from srcsrc toto destdest..

  • 8/8/2019 Tower of Hanoi Final

    19/32

    Design (Design (CtdCtd))

    Induction Step: n > 1Induction Step: n > 1

    pHow can recursion help us out?How can recursion help us out?

    c.c. RecursivelyRecursively move nmove n--1 disks from1 disks from auxaux toto destdest......

  • 8/8/2019 Tower of Hanoi Final

    20/32

    Design (Design (CtdCtd))

    Induction Step: n > 1Induction Step: n > 1

    pHow can recursion help us out?How can recursion help us out?

    d. Were done!d. Were done!

  • 8/8/2019 Tower of Hanoi Final

    21/32

    AlgorithmAlgorithm

    We can combine these steps into the following algorithm:We can combine these steps into the following algorithm:

    0.0.ReceiveReceive n, src, dest, auxn, src, dest, aux..

    1.1.IfIf nn > 1:> 1:

    a. Move(a. Move(nn--1, src, aux,dest1, src, aux,dest););b. Move(1,b. Move(1, src,dest, auxsrc,dest, aux););

    c. Move(c. Move(nn--1, aux,dest, src1, aux,dest, src););

    ElseElse

    Display Move the top disk from ,Display Move the top disk from , srcsrc, to ,, to ,destdest..

    End if.End if.

  • 8/8/2019 Tower of Hanoi Final

    22/32

    CodingCoding// ...

    void Move(int n, char src, char dest, char aux){if (n > 1){

    Move(n-1, src, aux, dest);Move(1, src, dest, aux);Move(n-1, aux, dest, src);}elsecout

  • 8/8/2019 Tower of Hanoi Final

    23/32

    TestingTestingThe Hanoi Towers

    Enter how many disks: 1Move the top disk from A to B

  • 8/8/2019 Tower of Hanoi Final

    24/32

    Testing (Testing (CtdCtd))The Hanoi Towers

    Enter how many disks: 2Move the top disk from A to CMove the top disk from A to BMove the top disk from C to B

  • 8/8/2019 Tower of Hanoi Final

    25/32

    Testing (Testing (CtdCtd))

    The Hanoi Towers

    Enter how many disks: 3Move the top disk from A to BMove the top disk from A to CMove the top disk from B to CMove the top disk from A to BMove the top disk from C to AMove the top disk from C to BMove the top disk from A to B

  • 8/8/2019 Tower of Hanoi Final

    26/32

    Testing (Testing (CtdCtd))

    The Hanoi Towers

    Enter how many disks: 4

    move a disk from needle A to needle B

    move a disk from needle C to needle B

    move a disk from needle A to needle C

    move a disk from needle B to needle Amove a disk from needle B to needle C

    move a disk from needle A to needle C

    move a disk from needle A to needle B

    move a disk from needle C to needle B

    move a disk from needle C to needle A

    move a disk from needle B to needle Amove a disk from needle C to needle B

    move a disk from needle A to needle C

    move a disk from needle A to needle B

    move a disk from needle C to needle B

  • 8/8/2019 Tower of Hanoi Final

    27/32

    AnalysisAnalysis

    Lets see how many moves it takes to solve this problem,Lets see how many moves it takes to solve this problem,as a function ofas a function of nn, the number of disks to be moved., the number of disks to be moved.

    nn Number of diskNumber of disk--moves requiredmoves required

    11 11

    22 33

    33 77

    44 1515

    55 3131

    ......

    ii 22ii--11

    6464 226464

    --1 (a big number)1 (a big number)

  • 8/8/2019 Tower of Hanoi Final

    28/32

    Analysis (Analysis (CtdCtd))

    How big?How big?

    Suppose that our computer andsuperSuppose that our computer andsuper--printer canprinter cangenerate and print 1,048,576 (2generate and print 1,048,576 (22020) instructions/second.) instructions/second.

    How long will ittake toHow long will ittake to printprint the priests instructions?the priests instructions?

    There are 2There are 26464 instructions to print.instructions to print.

    Then it will take 2Then it will take 26464/2/22020 = 2= 24444 secondsseconds to printthem.to printthem.

    1 minute == 60 seconds.1 minute == 60 seconds.

    Lets take 64 = 2Lets take 64 = 266 as an approximation of 60.as an approximation of 60.

    Then it will takeThen it will take $224444/ 2/ 266 = 2= 23838 minutesminutes to printthem.to printthem.

  • 8/8/2019 Tower of Hanoi Final

    29/32

    Analysis (Analysis (CtdCtd))

    Hmm. 2Hmm. 23838 minutes is hardto grasp. Lets keep going...minutes is hardto grasp. Lets keep going...

    1 hour == 60 minutes.1 hour == 60 minutes.

    Lets take 64 = 2Lets take 64 = 266 as an approximation of 60.as an approximation of 60.

    Then it will takeThen it will take $223838/ 2/ 266 = 2= 23232 hourshours to printthem.to printthem.

    1 day == 24 hours.1 day == 24 hours.

    Lets take 32 = 2Lets take 32 = 255 as an approximation of 24.as an approximation of 24.

    Then it will takeThen it will take $223232/ 2/ 255 = 2= 22727 daysdays to printthem.to printthem.

  • 8/8/2019 Tower of Hanoi Final

    30/32

    Analysis (Analysis (CtdCtd))

    Hmm. 2Hmm. 22727 days is hardto grasp. Lets keep going...days is hardto grasp. Lets keep going...

    1 year == 365 days.1 year == 365 days.

    Lets take 512 = 2Lets take 512 = 299 as an approximation of 365.as an approximation of 365.

    Then it will takeThen it will take $222727/ 2/ 299 = 2= 21818 yearsyears to printthem.to printthem.

    1 century == 100 years.1 century == 100 years.

    Lets take 128 = 2Lets take 128 = 277 as an approximation of 100.as an approximation of 100.

    Then it will takeThen it will take $221818/ 2/ 277 = 2= 21111 centuriescenturies to printthem.to printthem.

  • 8/8/2019 Tower of Hanoi Final

    31/32

    Analysis (Analysis (CtdCtd))

    Hmm. 2Hmm. 21111 centuries is hardto grasp. Lets keep going...centuries is hardto grasp. Lets keep going...

    1 millenium == 10 centuries.1 millenium == 10 centuries.

    Lets take 16 = 2Lets take 16 = 244 as an approximation of 10.as an approximation of 10.

    Then it will takeThen it will take $221111/ 2/ 244 = 2= 277 == 128 millenia128 millenia

    justtojustto printprint the priests instructions (assuming our computerthe priests instructions (assuming our computerdoesnt crash, in which case we have to start all over again).doesnt crash, in which case we have to start all over again).

    How fast can the priests actuallyHow fast can the priests actually movemove the disks?the disks?Ill leave itto you to calculate the data ofthe apocalypse...Ill leave itto you to calculate the data ofthe apocalypse...

  • 8/8/2019 Tower of Hanoi Final

    32/32

    SummarySummary

    Recursion is a valuable tool that allows some problems toRecursion is a valuable tool that allows some problems tobe solved in an elegant and efficient manner.be solved in an elegant and efficient manner.

    Functions can sometimes require more than one recursiveFunctions can sometimes require more than one recursive

    call in order to accomplish their task.call in order to accomplish their task.

    There are problems for which we can design a solution,There are problems for which we can design a solution,but the nature of the problem makes solving itbut the nature of the problem makes solving iteffectively uncomputableeffectively uncomputable..