1 / 14

Rekursif- studi kasus

Rekursif- studi kasus. Kasus 1- pangkat. Kasus 1- pangkat (2). Function pangkat (x, y) Hasil ← 1 If y = 0 then pangkat ( x,y ) ←1 Else Hasil ← x * pangkat (x, y-1) Return. Kasus : Faktorial. 5! = 5 x 4 x 3 x 2 x 1 = 5 x 4! 4! = 4 x 3 x 2 x 1 = 4 x 3! 3! = 3 x 2 x 1 = 3 x 2!

brock
Télécharger la présentation

Rekursif- studi kasus

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. Rekursif- studi kasus

  2. Kasus 1- pangkat

  3. Kasus 1- pangkat (2) Function pangkat (x, y) Hasil ← 1 If y = 0 then pangkat(x,y) ←1 Else Hasil ← x * pangkat(x, y-1) Return

  4. Kasus : Faktorial • 5! = 5 x 4 x 3 x 2 x 1 = 5 x 4! • 4! = 4 x 3 x 2 x 1 = 4 x 3! • 3! = 3 x 2 x 1 = 3 x 2! • 2! = 2 x 1 = 2 x 1! • 1! = 1 • 0!=1

  5. Kasus : Faktorial 5! = 5 x 4! 4! = 4 x 3! 3! = 3 x 2! 2! = 2 x 1! 1! = 1 x 0! 0! = 1 5! = 5 x 4 x 3 x 2 x 1x1

  6. Kasus : Faktorial faktorial(5)  hasilnya : 120 5 * faktorial(4)  5 * 24 = 120 4 * faktorial(3)  4 * 6 = 24 3 * faktorial(2)  3 * 2 = 6 2 * faktorial(1)  2 * 1 = 2 1* faktorial(0)  1 * 1 = 2 1

  7. Kasus 2- faktorial Function faktorial (n) Hasil ← 1 If (n=0) then hasil ← 1 Else For j ← 1 to n do Hasil ← hasil * j Endfor Endif Return hasil Function faktorial(n) If (n=0) then faktorial (n) ← 1 Else faktorial(n) ← n * faktorial (n-1) endif Return

  8. Mekanisme Function faktorial(n) If (n=0) then faktorial (n) ← 1 Else faktorial(n) ← n * faktorial (n-1) Return Misal n = 4 Faktorial(0) 1 Faktorial(1) 1 * faktorial(1-1) 1 * 1 Faktorial(2) 2 * faktorial(2-1) 2 * (1 * 1) 3 * faktorial(3-1) Faktorial(3) 3 * (2 * (1 * 1)) Faktorial (4) 4 * faktorial(4-1) 4 * (3 * (2 * (1 * 1)))

  9. Studi kasus 3-fibbonaci • 1 1 2 3 5 8 13 21 34 55 89 … • dapat dicari dari dua bilangan sebelumnya yang terdekat dengan bilangan N, yaitu • bilangan ke-(N-1) dan bilangan ke-(N-2), sehingga dapat dirumuskan sebagai • Fibbonacci(1) = 1 (1) • Fibbonacci(2) = 1 (2) • Fibbonacci(N) = Fibbonacci(N-1) + Fibbonacci(N-2) (3) • Dengan persamaan (1) dan (2) adalah basis dan persamaan (3) adalah rekurensnya

  10. Fibbonacci-rekursif • if ((N=1) or (N=2)) then • return 1 {Basis} • else • return(FIBO(N-1)+ FIBO(N-2)) {Rekurens}

  11. Contoh analisisalgoritmarekursif • Misal:

  12. Menghitung kompleksitas waktu

  13. We must have n – 1 of the “+ 2” terms because there was one at the start and we did n – 2 substitutions: • So, the closed form of the equation is:

More Related