Ict202 4 Recursion
Ict202 4 Recursion
Mulungushi University
School of Science, Engineering and
Technology
ICT202 Data Structures and Algorithms
Lecture 3
1. Introduction
2. Recursion Concepts
3. Example Using Recursion: Factorials
4. Example Using Recursion: Fibonacci Series
5. Recursion and the Method Call Stack
6. Recursion vs. Iteration
7. Towers of Hanoi
8. Dynamic Programming
9. Recursive Backtracking
N N −1
i = N + i
i =1 i =1
N −2
= N + N −1 + i
i =1
N −3
= N + N − 1 + N − 2 + i
i =1
= N + N −1 + N − 2 + + 2 +1
result = 2 + sum(1)
sum(3)
sum
sum(2) result = 1
sum
sum(1)
sum
• Base case(s).
– Values of the input variables for which we perform no
recursive calls are called base cases (there should be at
least one base case).
– Every possible chain of recursive calls must eventually
reach a base case.
• Recursive calls.
– Calls to the current method.
– Each recursive call should be defined so that it makes
progress towards a base case.
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
Fibonacci of 0 is: 0
Fibonacci of 1 is: 1
Fibonacci of 2 is: 1
Fibonacci of 3 is: 2
Fibonacci of 4 is: 3
Fibonacci of 5 is: 5
Fibonacci of 6 is: 8
Fibonacci of 7 is: 13
Fibonacci of 8 is: 21
Fibonacci of 9 is: 34
Fibonacci of 10 is: 55
Towers of Hanoi
• Classic problem – Priests in Far East are attempting to
move a stack of disks from one peg to another. One disk
must be moved at a time, at no time may a larger disk be
placed above a smaller disk
Towers of Hanoi
• Recursive solution:
– Move n – 1 disks from peg 1 to peg 2, using peg
3 as temporary holding area
– Move the last disk (the largest) from peg 1 to peg
3
– Move the n – 1 disks from peg 2 to peg 3, using
peg 1 as a temporary holding area
• Base case: When only one disk needs to be moved –
no temporary holding area needed, disk is simply
moved
//------------------------------------------------------------------
// Sets up the puzzle with the specified number of disks.
//------------------------------------------------------------------
public TowersOfHanoi (int disks)
{
totalDisks = disks;
}
(more…)
//------------------------------------------------------------------
// Prints instructions to move one disk from the specified start
// tower to the specified end tower.
//------------------------------------------------------------------
private void moveOneDisk (int start, int end)
{
System.out.println ("Move one disk from " + start + " to " +
end);
}
}
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
m1 m2 m3
m1 m2 m3
m1 m2 m3
Dynamic Programming
Recursive Backtracking
Exercises
• Write a recursive definition of xy (x raised to the power y),
where x and y are integers and y > 0.
• Write a recursive definition of i * j (integer multiplication),
where i > 0. Define the multiplication process in terms of integer
addition. For example, 4 * 7 is equal to 7 added to itself 4 times.
• Write a recursive definition of the Fibonacci numbers. The
Fibonacci numbers are a sequence of integers, each of which is
the sum of the previous two numbers. The first two numbers in
the sequence are 0 and 1. Explain why you would not normally
use recursion to solve this problem.
Exercises
• Modify the method that calculates the sum of the integers
between 1 and N shown in this chapter. Have the new version
match the following recursive definition: The sum of 1 to N is
the sum of 1 to (N/2) plus the sum of (N/2 + 1) to N. Trace your
solution using an N of 7.
• Write a recursive method that returns the value of N! (N
factorial) using the definition given in this chapter. Explain why
you would not normally use recursion to solve this problem.
• Write a recursive method to reverse a string. Explain why you
would not normally use recursion to solve this problem.
End
System.out.println (labyrinth);
System.out.println (labyrinth);
}
}
© 1992-2007 Pearson Education, Inc. All rights reserved.
Maze.java
//********************************************************************
// Maze.java Java Foundations
//
// Represents a maze of characters. The goal is to get from the
// top left corner to the bottom right, following a path of 1's.
//********************************************************************
(more…)
return done;
}
//------------------------------------------------------------------
// Determines if a specific location is valid.
//------------------------------------------------------------------
private boolean valid (int row, int column)
{
boolean result = false;
return result;
}
(more…)
return result;
}
}