1 / 34

Design and A n alysis of Algorithm Recursive Algorithm Analysis

Design and A n alysis of Algorithm Recursive Algorithm Analysis. Aryo Pinandito, ST, M.MT - PTIIK UB. Apa itu fungsi rekursif ?. Fungsi yang memanggil dirinya sendiri Sebuah fungsi f juga merupakan fungsi rekursif jika memanggil fungsi lain g dan di dalam g terdapat pemanggilan f.

ray
Download Presentation

Design and A n alysis of Algorithm Recursive Algorithm Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design and Analysis of AlgorithmRecursive Algorithm Analysis Aryo Pinandito, ST, M.MT - PTIIK UB

  2. Apaitufungsirekursif? • Fungsi yang memanggildirinyasendiri • Sebuahfungsi f jugamerupakanfungsirekursifjikamemanggilfungsi lain g dan di dalam g terdapatpemanggilan f. start f end start f g

  3. SifatPenyelesaianMasalahdenganRekursif • Permasalahan yang dapatdiselesaikanolehfungsirekursifmemilikisifat: • Memilikikasussederhana yang dapatlangsungdiselesaikan (base case). Contoh 0! = 1. • Kasus yang kompleksdapatdiuraikanmenjadikasus yang identikdenganukuran yang lebihkecil (recursive cases). Contoh: n! = n * (n-1)! • Denganmenerapkankarakteristik 2 berulang-ulang, recursive cases akanmendekatidansampaipada base case. Contoh: n!  (n-1)!  (n-2)!  . . . 1!, 0!.

  4. FungsiRekursif: Tree

  5. Format fungsi rekursif if current case is base case then solve it else redefine the problem using recursion case

  6. Format fungsi rekursif • Cabangifberisibase case, sedangkanbagianelse-nyaberisirecursive case • Agar rekursidapatberhentiinput pada recursive cases harusmendekati base case di setiappemanggilanfungsirekursif

  7. Latihan • BuatlahfungsirekursifuntukmenghitungnilaiXn. • Buatpohonrekursifuntuk45.

  8. Jawaban • Algoritma: pangkat(X, n) • algoritmauntukmenghitungnilaiXnsecararekursif • input : integer positif X dan n • output : nilaiXn if n == 1 return X else return (X * pangkat(X, n-1)) • Pohonrekursif?

  9. Latihan • Buatlahfungsirekursifuntukmenghitungbilanganfibonaccike n. • Buatpohonrekursifuntukfibonacci(4).

  10. Jawaban • Algorithm fibonacci(n) • // algoritmauntukmenghitungbilanganfibonaccike n • // secararekursif • // input : n • // output : bilanganfibonaccike n if n = 0 or n = 1 return n else return (fib(n - 1) + fib(n - 2)) • Pohonrekursif?

  11. Analisa Efisiensi Algoritma Rekursif • Algorithm pangkat(X, n) • //algoritmauntukmenghitungnilaiXnsecararekursif • //input : integer positif X dan n • //output : nilaiXn if n == 1 return X else return (X * pangkat(X, n-1)) • Analisisefisiensiwaktualgoritmarekursif!

  12. Langkah-langkah Umum Analisis Efisiensi Waktu Algoritma Rekursif • Tentukan metrik untuk ukuran input • Identifikasi basic operation algoritma • Tentukan apakah untuk ukuran input yang sama banyaknya eksekusi basic operation bisa berbeda • Tentukan persamaan rekursi yang menunjukkan berapa kali basic operation dieksekusi • Cari rumus langsung yang menunjukkan banyaknya basic operation dieksekusi

  13. 1: MetrikUntukUkuran Input • Sesuatupada input yang jikamembesar, makabanyaknyapemanggilanfungsirekursifbertambah • Padakasusiniadalahnilai n. Jika n membesar, makabanyaknyakomputasiataupemanggilanfungsirekursibertambah • Untukmemahaminyacobagambarpohonrekursifnya. • Efisiensidinyatakansebagaifungsidari n

  14. 2: Basic operation • Padaalgoritmarekursifmerupakansalahsatuoperasipadakondisiseleksi base case ataubagian recursive case • Basic operationnyadipilih = yang dilakukan 1 kali setiap kali fungsirekursifdipanggil if n == 1

  15. 3: Case • Apakahada best case, average case dan worst case? • Untukinput n tertentumisal 5, recursion treenyaselalusama. Banyaknyakomputasi / pemanggilanfungsirekursiftetap. • Tidakada best case, average case dan worst case Padafungsirekrusif.

  16. 4: PersamaanRekursifBanyaknyaEksekusi Basic Operation • Jikaalgoritmapangkatdieksekusidengan input (X, n) maka basic operation dieksekusisatu kali. Namunpadasaateksekusi, algoritmatersebutjugamemanggildirinyasendiridengan input (X, n-1). • Hal inimenyebabkansecara internal basic operation dieksekusilagi. • Berapa kali banyaknya basic operation dieksekusiuntuk input n? if n == 1

  17. 4: PersamaanRekursifBanyaknyaEksekusi Basic Operation • Jika C(n) menyatakanbanyaknya basic operation dieksekusiuntuk input berukuran n dan C(n - 1) menyatakanbanyaknya basic operation dieksekusiuntuk input berukuran n-1, • Hubungan C(n) dan C(n - 1) dinyatakandengan C(n) = C(n-1) + 1 untukn>1 (recursive case) C(1) = 1, base case

  18. 4: PersamaanRekursifBanyaknyaEksekusi Basic Operation • Untukmengetahuikelasefisiensiwaktunyakitaharusmenemukanpersamaanlangsung (non recursive) dari C(n)

  19. Perhitunganefisiensiwaktu • Menggunakanmetode backward substitution, caripoladari C(n) : C(n) = C(n - 1) + 1 C(n) = (C(n - 2) + 1) + 1 = C(n) = C(n - 2) + 2 C(n) = (C(n - 3) + 1) + 2 = C(n) = C(n - 3) + 3 dst • Polaataubentukumum yang didapatkanadalah C(n) = C(n - i) + i.

  20. Perhitunganefisiensiwaktu • Nilai initial condition C(1) disubtitusikanke C(n - i) padabentukumum C(n). C(n) = C(n - i) + i C(n) = C(1) + i C(n) = i + 1

  21. Perhitunganefisiensiwaktu • Subtitusitersebutmensyaratkan C(n - i) = C(1) atau n – i = 1 i = n – 1 • Nilaii = n – 1 disubtitusikankebentukumumC(n) = i + 1 sehingga: C(n) = n – 1 + 1 C(n) = n C(n) merupakananggotakelasefisiensi n • Apaartinya?

  22. Example 2: Tower of Hanoi (1) • n disks on different sizes and three pegs • Initially, all disks are on the first peg in order of size. The largest on the bottom and the smallest on top • The goal: move all disks to the third peg, using the second one as an auxiliary • Move only one disk at a time • It is forbidden to place a larger disk on top of a smaller one CS3024-FAZ

  23. Example 2: Tower of Hanoi (2)

  24. Tower of Hanoi: Recursive Solution (1)

  25. ToH: Recursive Solution (2) • To move n>1 disks from peg 1 to peg 3 (with peg 2 as an auxiliary): • Move recursively n-1 disk(s) from peg 1 to peg 2 (with peg 3 as an auxiliary) • Move the largest disk from peg 1 to peg 3 • Move recursively n-1 disk(s) from peg 2 to peg 3 (with peg 1 as an auxiliary)

  26. Recursive Pseudocode FUNCTION MoveTower(disk, source, dest, spare): IF disk == 0, THEN: move disk from source to dest ELSE: MoveTower(disk - 1, source, spare, dest) move disk from source to dest MoveTower(disk - 1, spare, dest, source) END IF

  27. ToH: Analysis (1) • Input's size = the number of disks = n • Basic operation = moving one disk • The number of moves M(n) depends on n only: M(n) = M(n-1) + 1 + M(n-1) ; for n>1 • Recurrence relation: • M(n) = 2M(n-1) + 1 ; for n>1 • M(1) = 1 initial condition

  28. ToH: Analysis (2) • Backward substitution: M(n) = 2M(n-1) + 1 = 2[2M(n-2)+1]+1=22M(n-2)+2+1 = 22 [2M(n-3)+1]+2+1=23M(n-3)+22+2+1 = 24M(n-4)+23+22+2+1 • The pattern, after i substitution: M(n) = 2iM(n-i) + 2i-1 + 2i-2 +..+ 2 + 1 = 2iM(n-i) + 2i - 1

  29. ToH: Analysis (3) • Initial condition, n=1  i=n-1: M(n) = 2iM(n-i) + 2i - 1 = 2(n-1)M(n-(n-1)) + 2(n-1) -1 = 2(n-1)M(1) + 2(n-1) - 1 = 2(n-1) + 2(n-1) - 1 = 2n - 1 • Exponential algorithm! • This is the most efficient algorithm • It is the problem's intrinsic difficulty that makes it so computationally difficult

  30. Exercises (2) Algorithm S(n) //input: a positive integer n if n = 1 return 1 else return S(n-1)+n*n*n • What does this algorithm compute? • Set up and solve a recurrence relation for the number of times the basic operation is executed • How does this algorithm compare with the straightforward non recursive algorithm for computing this function?

  31. Exercises (3) • Write an algorithm to find the n-th Fibonacci number recursively by using its definition. Is it more efficient than the iterative algorithm to find the same number? CS3024-FAZ

  32. Exercises (4) • In the original version of the Tower of Hanoi puzzle (Edouard Lucas, 1890), the world will end after 64 disks have been moved from a mythical Tower of Brahma. • Estimate the number of years it will take if priests could move one disk per minute (they do not eat, sleep, or die) • How many moves are made by the i-th largest disk (1≤i ≤n) in this algorithm?

  33. Questions?

  34. 감사합니다 Grazias Kiitos Gratias Danke ﺷﻜﺮﺍﹰ 谢谢 TerimaKasih Merci Thank You धन्यवाद ありがとうございます

More Related