10 recursion problem solving[1]
TRANSCRIPT
-
8/8/2019 10 Recursion Problem Solving[1]
1/24
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Recursive Problem Solving
2140101 Computer Programming for International Engineers
-
8/8/2019 10 Recursion Problem Solving[1]
2/24
22140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Objectives
Students should:
Be able to explain the concept of recursive
definition. Be able to use recursion in Java to solve
problems.
-
8/8/2019 10 Recursion Problem Solving[1]
3/24
32140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Recursive Problem Solving
How to solve problem recursively ? Break a problem into identical but smaller,
or simpler problems.
Solve smaller problems to obtain asolution to the original one.
-
8/8/2019 10 Recursion Problem Solving[1]
4/24
42140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Example: Summation
Alternative Solution:
public static int s(int n){
int sum = 0;
for(int i=0;i
-
8/8/2019 10 Recursion Problem Solving[1]
5/24
52140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Example: Summation
Write a method finding the sum of integers from 0 to n
=
=+=
00
,...3,2,1)1()(
n
nnnSnS
Let S(n) be the sum of integers from 0 to n.
Mathematically, we can write:
Java Implementation:
public static int s(int n){
if(n==0) return 0;return s(n-1)+n;
} Recursive Approach
-
8/8/2019 10 Recursion Problem Solving[1]
6/24
62140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Example: Summation
=
=+=
00
,...3,2,1)1()(
n
nnnSnS
public static int s(int n){
if(n==0) return 0;return s(n-1)+n;
}
Solving S(n) is broken into solving S(n-1), which is asimpler (but somewhat identical) problem.
A
case
B
case
B
case
A
case
-
8/8/2019 10 Recursion Problem Solving[1]
7/24
72140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Example: Summation
In finding S(2), method invocation can bedepicted as:
public static int s(int n){
if(n==0) return 0;
return s(n-1)+n;
}S(2)
-
8/8/2019 10 Recursion Problem Solving[1]
8/24
82140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Example: Factorial
Write a method finding n!
=
==
01
,...3,2,11...)1(!
n
nnnn
Mathematically, we can write:
Java Implementation:
public static int factorial(int n){
int s = 1;
for(int i=1;i
-
8/8/2019 10 Recursion Problem Solving[1]
9/24
92140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Example: Factorial
Write a method finding n!
=
==
01
,...3,2,1)!1(!
n
nnnn
Alternatively, we can write:
Java Implementation:
Recursive Approach
public static intfactorial(int n){
if(n==0) return 1;return factorial(n-1)*n;
}
-
8/8/2019 10 Recursion Problem Solving[1]
10/24
102140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
factorial(4)
Example: Factorial
factorial(4)
factorial(3)
factorial(2)
2
6
24
(factorial(2)*3)
(factorial(3)*4)
factorial(1)
factorial(0)
1
1
(factorial(1)*2)
(factorial(0)*1)
-
8/8/2019 10 Recursion Problem Solving[1]
11/24
112140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
public static int s(int n){if(n==0) return 0;
return s(n-1)+n;
}
public static int factorial(int n){
if(n==0) return 1;
return factorial(n-1)*n;
}
Recursive Method Design
A recursive method must have two parts.
Base cases : determine the case where the recursive
method invocation terminates Recursive cases : recursive calls itself, but with
simpler parameters
Base cases
Recursive cases
-
8/8/2019 10 Recursion Problem Solving[1]
12/24
122140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Example: Fibonacci
Example: Fibonacci Numbers (0, 1, 1, 2, 3, 5, 8, 13,21, 34, 55, 89, )
The Fibonacci numbers form a sequence of integerdefined recursively by:
-
8/8/2019 10 Recursion Problem Solving[1]
13/24
132140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Recursive Method Design
public class FiboDemo
{
public static void main(String[] args)
{
final int n = 20;
for(int i=0;i
-
8/8/2019 10 Recursion Problem Solving[1]
14/24
142140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Recursive Method Design
fibo(4)
-
8/8/2019 10 Recursion Problem Solving[1]
15/24
152140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Costs of Recursion
A recursive method accomplishes its task bysuccessively calling itself.
Therefore, there are many invocations of methodinvolved. Each time a recursive call is made, a certain
amount of memory must be allocated.
For a recursive method that makes very deeprecursions, a large amount of memory isrequired.
Does this mean we should avoidrecursive algorithms ?
-
8/8/2019 10 Recursion Problem Solving[1]
16/24
162140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Example: Fibonacci Numbers Revisited
import java.io.*;
public class FiboDemo
{
public static void main(String[] args) throws IOException
{ BufferedReader stdin =new BufferedReader(newInputStreamReader(System.in));
System.out.print("Enter n:");
int n = Integer.parseInt(stdin.readLine());
System.out.println("---Using fibo()------------");
System.out.println("F("+n+")="+fibo(n));
System.out.println("---Using fiboNew()---------");System.out.println("F("+n+")="+fiboNew(n));
}
public static int fibo(int n){
System.out.println("fibo("+n+") is called.");
if(n
-
8/8/2019 10 Recursion Problem Solving[1]
17/24
172140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Example: Fibonacci Numbers Revisited
// The same fibo() as the previous example
public static int fiboNew(int n){
int [] remember = new int[n+1];
for(int i=0;i
-
8/8/2019 10 Recursion Problem Solving[1]
18/24
182140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Example: Fibonacci Numbers Revisited
From the picture, we can
see that finding the 6thFibonacci number usingfibo() requires more than
three times as manymethod invocations as itis required in the case of
usingfiboNew().
-
8/8/2019 10 Recursion Problem Solving[1]
19/24
192140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Example: The Towers of Hanoi
Sometimes, the easiest and the least error-
prone ways to write programs for solving
some problems are recursivemethods.
Sometimes, an iterative approach is much
more difficult than the recursive ones.
See example Towers of Hanoi
-
8/8/2019 10 Recursion Problem Solving[1]
20/24
202140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Towers of Hanoi
Peg A Peg B Peg C
Goal: Move all disks on Peg A to PegB.
Rules:1. Only one disk can be moved at a time, and this disk must be top
disk on a tower.2. A larger disk cannot be placed on the top of a smaller disk.
Using minimum number of moves
-
8/8/2019 10 Recursion Problem Solving[1]
21/24
212140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Towers of HanoiSee Towers of Hanoi Flash Demo distributed with this courseware.
-
8/8/2019 10 Recursion Problem Solving[1]
22/24
222140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Example: The Towers of Hanoi
import java.io.*;
public class TowerOfHanoiDemo
{public static void main(String[] args)
throws IOException{
BufferedReader stdin =
new BufferedReader(newInputStreamReader(System.in));
System.out.print("Enter number of disks:");
int n = Integer.parseInt(stdin.readLine());
move(n,"A","B","C");
}
// continue on the next page
-
8/8/2019 10 Recursion Problem Solving[1]
23/24
232140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Example: The Towers of Hanoi
public static void move(int n,
String orgPole,String destPole,String otherPole){
String step;
if(n
-
8/8/2019 10 Recursion Problem Solving[1]
24/24
242140101 Computer Programming for International EngineersOPEN COURSEWARE MATERIALSCHULALONGKORNUNIVERSITY
Department of Computer Engineering, Faculty of Engineering, Chulalongkorn University
Example: The Towers of Hanoi
Try solving it using an iterative approach.