Rekursif Pertemuan : 6-7 Disusun oleh : Danang Junaedi Jurusan Teknik Informatika Universitas Widyatama Overview Tujuan Instruksional Review Fungsi & Prosedur Introduction of Recursion Definition Recursion Method Contoh Kasus Recursion vs Iteration Kasus Tugas Presentasi Jurusan Teknik Informatika VI-VII - 2 Universitas Widyatama 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 Jurusan Teknik Informatika VI-VII - 3 Universitas Widyatama
Review Fungsi dan Prosedur Fungsi tipe_fungsi nama_fungsi(argumen, argumen2,...) tubuh Fungsi contoh : double Absolut(double X) if (X,0) X=-X; return(x); Prosedur void nama_prosedur(argumen, argumen2,...) tubuh Prosedur contoh : void Tampil(char Nama[5], int Kali) int I; for(i=0;i<kali;i++) printf(nama); definisi Fungsi definisi Prosedur Jurusan Teknik Informatika VI-VII - 4 Universitas Widyatama Introduction of Recursion So far, we have seen methods that call other methods. For example, the main() method calls the square() method. main() Recursive Method: square() A recursive method is a method that calls itself. compute() Jurusan Teknik Informatika VI-VII - 5 Universitas Widyatama 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 - 6 Universitas Widyatama 2
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 - 7 Universitas Widyatama Example () Fungsi Faktorial Secara Iterasi atau n! = n * (n - ) * (n - 2) *... * Secara Rekursif atau n! = n * (n - )! dan 0! = C++ int Fakt(int n) Fak=; if (n > ) for(i=;i<=n;i++) Fak=Fak*I; return Fak; int Fakt(int n) if (n == n == 0) return ; else return n * Fakt(n-); Jurusan Teknik Informatika VI-VII - 8 Universitas Widyatama Ilustration 5! 5 * 4! 4 * 3! 3 * 2! 2 *! Example (2) 5! 5! = 5 * 24 = 20 returned 5 * 4! 4! = 4 * 6 = 24 returned 4 * 3! 3! = 3 * 2 = 6 returned 3 * 2! 2! = 2 * = 2 returned 2 *! returned a. Process of recursive calls b. Values returned from each recursive calls Jurusan Teknik Informatika VI-VII - 9 Universitas Widyatama 3
Iteration Recursion vs Iteration Uses repetition structures (for, while or 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 else 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 Jurusan Teknik Informatika VI-VII - 0 Universitas Widyatama Towers of Hanoi game Studi Kasus (Game invented by French mathematician Edouard Lucas in 883) Rules for the Towers of Hanoi game. 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-) + F(n-2) + ) Binary Search secara rekursif Jurusan Teknik Informatika VI-VII - Universitas Widyatama 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) Presentasi pada pertemuan ke-9 Jurusan Teknik Informatika VI-VII - 2 Universitas Widyatama 4
Untuk bahan renungan bersama. Jadilah Jagung, Jangan Jambu Monyet. Jagung membungkus bijinya yang banyak, sedangkan jambu monyet memamerkan bijinya yang cuma satu-satunya. Artinya : Jangan suka pamer 2. Jadilah Duren, jangan kedondong. Walaupun luarnya penuh kulit yang tajam, tetapi dalamnya lembut dan manis. hmmmm, beda dengan kedondong, luarnya mulus, rasanya agak asem dan di dalamnya ada biji yang berduri. Artinya : Don't Judge a Book by The Cover.. jangan menilai orang dari Luarnya saja. 3. Jadilah bengkoang. Walaupun hidup dalam kompos sampah, tetapi umbinya isinya putih bersih. Artinya : Jagalah hati jangan kau nodai. 4. Jadilah Tandan Pete, bukan Tandan Rambutan. Tandan pete membagi makanan sama rata ke biji petenya, semua seimbang, tidak seperti rambutan.. ada yang kecil ada yang gede. Artinya : Selalu adil dalam bersikap. 6. Jadilah Cabe. Makin tua makin pedas. Artinya : Makin tua makin bijaksana. 7. Jadilah Buah Manggis. Bisa ditebak isinya dari pantat buahnya. Artinya : Jangan Munafik 8. Jadilah Buah Nangka. Selain buahnya, nangka memberi getah kepada penjual atau yg memakannya. Artinya : Berikan kesan kepada semua orang (tentunya yg baik). Jurusan Teknik Informatika VI-VII - 3 Universitas Widyatama Pertemuan 8 : UTS Pertemuan 9 : Presentasi Pengelolaan File dan Rekursif Jurusan Teknik Informatika Universitas Widyatama 5