Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

DIG1G3 Implementasi Struktur Data

Presentasi serupa


Presentasi berjudul: "DIG1G3 Implementasi Struktur Data"— Transcript presentasi:

1 DIG1G3 Implementasi Struktur Data
Fungsi Rekursif *Cahyana

2 Pengertian Fungsi rekursi adalah fungsi yang didalam function body-nya ada statement yang memanggil dirinya sendiri. Fungsi rekursif, sangat berguna dalam pemecahan masalah jika masalah tadi dapat didefinisikan secara rekursif pula. Contoh : Faktorial (n) atau n! didefinisikan sebagai berikut : n! = 1, untuk n = 0; n! = n * (n-1)!, untuk n > 0 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1* 0! 0! = 1 Bila ditelusur mundur : 4! = 1*2*3*4 = 24

3 Perhitungan 5 faktorial
5! (5 * 4!) (5 * (4 *3!)) (5 * (4 * (3 * 2!))) (5 * (4 * (3 * (2 * 1!)))) (5 * (4 * (3 * (2 * (1 * 0!))))) (5 * (4 * (3 * (2 * (1 * 1))))) (5 * (4 * (3 * (2 * 1)))) (5 * (4 * (3 * 2))) (5 * (4 * 6 )) (5 * 24) 120

4 Komponen Fungsi rekursif mempunyai dua komponen yaitu:
Base case: mengembalikan nilai tanpa melakukan pemanggilan rekursi berikutnya. Reduction step: menghubungkan fungsi di suatu nilai input ke fungsi yang dievaluasi di nilai input yang lain. Sekuen nilai input harus konvergen ke base case. Fungsi faktorial Base case : n = 0 Reduction step: f(n) = n * f(n-1)

5 Fungsi Iteratif vs Rekursif
Faktorial - Rekursif long faktor (int n) { if(n==0) return (1); else return(n * faktor(n-1)); } Faktorial - Iteratif long faktor(int n) { long i, fak = 1; for(i=1; i<=n; i++) fak *= i; return (fak); }

6 Kekurangan Rekursif (?)
Meskipun penulisan program dengan cara rekursif bisa lebih pendek, namun procedure atau function rekursif memerlukan : Memori yang lebih banyak, karena perlu tambahan untuk mengaktifkan Stack. Waktu lebih lama, karena perlu menjejaki setiap pemanggilan rekursif melalui stack.

7 Kapan Menggunakan Rekursif ?
Queue/rmb 30/10/'06 Kapan Menggunakan Rekursif ? Secara umum, gunakan penyelesaian secara rekursif, hanya jika : Penyelesaian sulit dilaksanakan secara iteratif Efisiensi dengan cara rekursif sudah memadai Efisiensi bukan masalah dibandingkan dengan kejelasan logika program Tidak mempertimbangkan faktor penghematan memori dan kecepatan eksekusi program Pertimbangan antara aspek kecepatan dan penghematan menggunakan iteratif, dibanding perancangan logika yang baik menggunakan rekursif

8 Contoh:Bilangan Fibonacci
Queue/rmb 30/10/'06 Contoh:Bilangan Fibonacci Urutan bilangan 0, 1, 1, 2, 3, 5, 8, 13 … disebut bilangan fibonacci. Hubungan antara satu angka dengan angka berikutnya didefinisikan secara rekursi sebagai berikut : Fib(N) = N jika N = 0 atau 1 Fib(N) = Fib(N-2) + Fib(N-1) jika N >= 2

9 Bilangan Fibonacci int Fib(int n) { int f; if(n==0) f = 0;
Queue/rmb 30/10/'06 Bilangan Fibonacci int Fib(int n) { int f; if(n==0) f = 0; else if(n==1) f = 1; else f = Fib(n-2) + Fib(n-1); return f; } Fungsi fib() di-samping ditulis secara rekursi dan disebut sebagai slow_Fib() tulislah fast_Fib() menggunakan iterasi.

10 Bilangan Fibonacci Contoh : Skema fibonacci jika N=4 FIB (4) FIB (3)
30/10/'06 Bilangan Fibonacci Contoh : Skema fibonacci jika N=4 FIB (4) FIB (3) FIB (2) FIB (1) FIB (0)

11 Bilangan Fibonacci Contoh : Skema fibonacci jika N=4 FIB (4) FIB (3)
30/10/'06 Bilangan Fibonacci Contoh : Skema fibonacci jika N=4 FIB (4) FIB (3) FIB (2) FIB (1) FIB (0)

12 Function Parameter Declaration
Classic Function Parameter declaration Contoh: Function Parameter Declaration int fungsi1(a) int a; { a++; return a; } int fungsi2(b) int b; b = b * b; return b; #include <stdio.h> int main() { int x; x=fungsi1(3); printf("x=%d\n",x); x=fungsi2(13); return(0); }

13 Function Parameter Declaration
Modern Function Parameter declaration Contoh: Function Parameter Declaration int fungsi1(int a) { a++; return a; } int fungsi2(int b) b = b * b; return b; #include <stdio.h> int main() { int x; x=fungsi1(3); printf("x=%d\n",x); x=fungsi2(13); return(0); }

14 Queue/rmb 30/10/'06 Latihan void listNumbers(int start, int end) that outputs the numbers from start to end to console. Write one version that outputs the numbers in ascending order, and another that outputs them in descending order. String repeat(String s, int n) that creates and returns a new string that is made by concatenating n copies of the parameter string s. For example, calling this method with the parameters “Hello” and 3 would return the string “HelloHelloHello”. If n equals zero, the method should return the empty string. int min(int[] a, int start, int end) that returns the smallest element between the indices start and end in the parameter array a.

15 Queue/rmb 30/10/'06 Latihan int mul(int a, int b) that computes the product of two integers a and b. The only arithmetic operation that you are allowed to use in this problem is addition +. double power(double a, int b) that calculates the power ab. You are allowed to use the multiplication operator *. int sumOfDigits(int n) that computes and returns the sum of digits of the positive integer n. For example, when called with the parameter 12345, this method would return 15. int reverseDigits(int n) that returns the positive integer that you get when you reverse the digits of parameter n. For xample, when called with the parameter 12345, this method would return o use the multiplication operator *.

16 Queue/rmb 30/10/'06 References Slide T0016 / Algoritma dan Pemrograman, “Pertemuan Fungsi”, Universitas Bina Nusantara, 2007 Thomas A. Standish,“Data Structures, Algorithms & Software Principles in C” Addison- Wesley, 1995 p. 253 ff (Chapter 7) Tjokorda Agung Budi Wirayuda, “PI1043 Struktur Data: Queue Data Structure”.


Download ppt "DIG1G3 Implementasi Struktur Data"

Presentasi serupa


Iklan oleh Google