Rekursif
Pertemuan : 6 - 7 Disusun oleh : Danang Junaedi
Jurusan Teknik Informatika – Universitas Widyatama
Jurusan Teknik Informatika VI-VII - 2 Universitas Widyatama
Jurusan Teknik Informatika VI-VII - 2 Universitas Widyatama
Pemrograman II (Terstruktur II)
Pemrograman II (Terstruktur II)
Tujuan Instruksional
Review Fungsi & Prosedur
Introduction of Recursion
Definition
Recursion Method
Contoh Kasus
Recursion vs Iteration
Kasus
Tugas Presentasi
Overview
Pemrograman II (Terstruktur II)
Pemrograman II (Terstruktur II)
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
Jurusan Teknik Informatika VI-VII - 4 Universitas Widyatama
Jurusan Teknik Informatika VI-VII - 4 Universitas Widyatama
Pemrograman II (Terstruktur II)
Pemrograman II (Terstruktur II)
Review Fungsi dan Prosedur
Fungsi
tipe_fungsi nama_Fungsi(argumen1, argumen2,....) definisi Fungsi
{ xxxx xxxx tubuh Fungsi
xxxx }
contoh : double Absolut(double X) {
if (X,0) X=-X; return(X); }
Prosedur
void nama_Prosedur(argumen1, argumen2,....) definisi Prosedur
{ xxxx
xxxx tubuh Prosedur
xxxx }
contoh : void Tampil(char Nama[15], int Kali) {
int I;
for(I=0;I<Kali;I++) printf(Nama); }
Jurusan Teknik Informatika VI-VII - 5 Universitas Widyatama
Jurusan Teknik Informatika VI-VII - 5 Universitas Widyatama
Pemrograman II (Terstruktur II)
Pemrograman II (Terstruktur II)
Introduction of Recursion
So far, we have seen methods that call other methods.
For example, the main() method calls the square() method.
Recursive Method:
A recursive method is a method that calls itself.
main()
square()
compute()
Jurusan Teknik Informatika VI-VII - 6 Universitas Widyatama
Jurusan Teknik Informatika VI-VII - 6 Universitas Widyatama
Pemrograman II (Terstruktur II)
Pemrograman II (Terstruktur II)
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
Jurusan Teknik Informatika VI-VII - 7 Universitas Widyatama
Jurusan Teknik Informatika VI-VII - 7 Universitas Widyatama
Pemrograman II (Terstruktur II)
Pemrograman II (Terstruktur II)
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.
A termination part that stops the recursion.
This is called the base case.
The base case should have a simple or trivial solution.
One or more recursive calls.
This is called the recursive case.
The recursive case calls the same method but with simpler or
smaller arguments.
Jurusan Teknik Informatika VI-VII - 8 Universitas Widyatama
Jurusan Teknik Informatika VI-VII - 8 Universitas Widyatama
Pemrograman II (Terstruktur II)
Pemrograman II (Terstruktur II)
Example
(1)
Fungsi Faktorial
C++
Secara Iterasi
atau n! = n * (n - 1) * (n - 2) * ... * 1Secara Rekursif
atau n! = n * (n - 1)! dan 0! = 1 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);} }
Pemrograman II (Terstruktur II)
Pemrograman II (Terstruktur II)
Example
(2)
Ilustration
a. Process of recursive calls
b. Values returned from each recursive calls
5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 5! = 5 * 24 = 120 returned 4! = 4 * 6 = 24 returned 3! = 3 * 2 = 6 returned 2! = 2 * 1 = 2 returned 1 returnedJurusan Teknik Informatika VI-VII - 10 Universitas Widyatama
Jurusan Teknik Informatika VI-VII - 10 Universitas Widyatama
Pemrograman II (Terstruktur II)
Pemrograman II (Terstruktur II)
Recursion vs Iteration
Iteration
Uses repetition structures (for, whileor do…while) Repetition through explicitly use of repetition structure
Terminates when loop-continuation condition fails
Controls repetition by using a counter
Recursion
Uses selection structures (if, if…elseor 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
Jurusan Teknik Informatika VI-VII - 11 Universitas Widyatama
Jurusan Teknik Informatika VI-VII - 11 Universitas Widyatama
Pemrograman II (Terstruktur II)
Pemrograman II (Terstruktur II)
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) + ……)
Binary Search secara rekursif
Jurusan Teknik Informatika VI-VII - 12 Universitas Widyatama
Jurusan Teknik Informatika VI-VII - 12 Universitas Widyatama
Pemrograman II (Terstruktur II)
Pemrograman II (Terstruktur II)
Tugas
Buat Laporan berdasarkan kasus yang sudah kelompok anda pilih
dengan susunan
Bab I Pendahuluan (Latar Belakang, Tujuan, Batasan Masalah) Bab II Dasar Teori
Bab III Analisa & Perancangan
Bab IV Implementasi (Listing Program dan Hasil Running Program)
Bab V Penutup (Kesimpulan & Saran)
Referensi
Laporan (Tidak perlu dijilid)
Bahan presentasi dan program dikumpulkan dalam bentuk file
(maksimum 4 hari sebelum presentasi dilaksanakan)
Jurusan Teknik Informatika VI-VII - 13 Universitas Widyatama
Jurusan Teknik Informatika VI-VII - 13 Universitas Widyatama
Pemrograman II (Terstruktur II)
Pemrograman II (Terstruktur II)