Professional Documents
Culture Documents
Overview
Tujuan Instruksional Review Fungsi & Prosedur Introduction of Recursion Definition Recursion Method Contoh Kasus Recursion vs Iteration Kasus Tugas Presentasi
Rekursif
Pertemuan : 6-7 Dosen Pembina : Danang Junaedi
Tujuan Instruksional
Menjelaskan pengertian dan manfaat rekursif, serta cara penulisannya dalam program Menjelaskan kelebihan dan kekurangan rekursif dalam pemrograman Menjelaskan penggunaan rekursif dalam program Menggunakan rekursif dalam program
4/9/2012
Introduction of Recursion
So far, we have seen methods that call other methods.
Definition
A recursive function is a function that calls itself either directly or indirectly through another function. The function launches (calls) a fresh copy of itself to work on the smaller problem this is referred to as recursive call and is also called as recursion step. The recursion step often includes the keyword return The function recognizes the base case and returns a result to the previous copy of the function and a sequence of returns ensues all the way up the line until the original function call eventually returns the final result to main
Recursion Method
The important thing to remember when creating a recursive function is to give an 'end-condition'. We don't want the function to keep calling itself forever, now, do we? Somehow, it should know when to stop. There are many ways of doing this. One of the simplest is by means of an 'if condition' statement A recursive method generally has two parts.
Example (1)
Fungsi Faktorial Secara Iterasi C++
int Fakt(int n) { Fak=1; if (n > 1) { for(i=1;i<=n;i++) { Fak=Fak*I;} } return Fak; } int Fakt(int n) { if (n == 1 || n == 0) { return 1;} else {return n * Fakt(n-1);} }
This is called the base case. The base case should have a simple or trivial solution.
One or more recursive calls. atau n! = n * (n - 1) * (n - 2) * ... * 1 Secara Rekursif
This is called the recursive case. The recursive case calls the same method but with simpler or smaller arguments.
4/9/2012
Example (2)
Ilustration
5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1
a. Process of recursive calls
Recursion vs Iteration
5!
5! = 5 * 24 = 120 returned
5 * 4!
4! = 4 * 6 = 24 returned
4 * 3!
3! = 3 * 2 = 6 returned
3 * 2!
2! = 2 * 1 = 2 returned
2 * 1!
1 returned
1
b. Values returned from each recursive calls
Iteration Uses repetition structures (for, while or dowhile) Repetition through explicitly use of repetition structure Terminates when loop-continuation condition fails Controls repetition by using a counter Recursion Uses selection structures (if, ifelse or switch) Repetition through repeated method calls Terminates when base case is satisfied Controls repetition by dividing problem into simpler one More overhead than iteration More memory intensive than iteration Can also be solved iteratively Often can be implemented with only a few lines of code
Studi Kasus
Towers of Hanoi game
(Game invented by French mathematician Edouard Lucas in 1883)
Rules for the Towers of Hanoi game 1. Move one disk at a time. Each disk you move must be a topmost disk. 2. No disk may rest on top of a disk smaller than itself. 3. You can store disks on the second pole temporarily, as long as you observe the previous two rules. Fibonacci (F(n)=F(n-1) + F(n-2) + )