0% found this document useful (0 votes)
25 views5 pages

Lecture 31

The document describes the Towers of Hanoi puzzle, where disks of decreasing sizes must be moved from one peg to another according to rules, with the goal of moving all disks to the third peg. It explains that the puzzle can be solved recursively by breaking it down into smaller subproblems of moving n-1 disks, then the largest disk, and then moving the remaining n-1 disks. Pseudocode for a recursive method to solve the puzzle is provided, along with analysis of the number of moves needed and method calls based on the number of disks n.

Uploaded by

jahanzeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

Lecture 31

The document describes the Towers of Hanoi puzzle, where disks of decreasing sizes must be moved from one peg to another according to rules, with the goal of moving all disks to the third peg. It explains that the puzzle can be solved recursively by breaking it down into smaller subproblems of moving n-1 disks, then the largest disk, and then moving the remaining n-1 disks. Pseudocode for a recursive method to solve the puzzle is provided, along with analysis of the number of moves needed and method calls based on the number of disks n.

Uploaded by

jahanzeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Towers of Hanoi

Three pegs, one with n disks of


decreasing diameter; two other pegs are
empty
Task: move all disks to the third peg
under the following constraints

Can move only the topmost disk from one peg


to another in one step
Cannot place a smaller disk below a larger
one

An example where recursion is much


easier to formulate than a loop-based
solution
1

Towers of Hanoi
We want to write a recursive method
shift (n, source, target, using) which
moves n disks from peg source to
target with the help of peg using for
intermediate transfers
The first step is to formulate the
algorithm

Observation: shift (n, source, target, using)


shift (n-1, source, using, target) followed
by transferring the largest disk from peg
source to peg target and then calling shift
(n-1, using, target, source)
2
Stopping condition: n = 1

Tower of Hanoi

class hanoi{
static int counter = 0;
public static void shift(int n, char source, char
target, char using){
counter = counter + 1;
if (n==1) System.out.println(source+" -> "+target);
else if (n > 1) {
shift(n-1,source,using,target);
System.out.println(source+" -> "+target);
shift(n-1,using,target,source);
}
} // How many moves needed? 2n-1

Tower of Hanoi

public static void main (String args[])


{
int n = 3;
shift(n,'a','c','b');
System.out.println(counter);
}
}

Towers of Hanoi
Total number of method calls
Let Tn be the number of method calls to solve
for n disks
Tn = 2Tn-1 + 1 for n > 1; T1 = 1

You might also like