Algorithms For Dummies


Relying on Dynamic Programming



Download 7,18 Mb.
Pdf ko'rish
bet505/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   501   502   503   504   505   506   507   508   ...   651
Bog'liq
Algorithms

  Relying on Dynamic Programming 

     305


    if n==0:

        return 0

    elif n == 1:

        return 1

    else:

        if (n-1, n-2) not in memo:

            print ("lvl %i, Summing fib(%i) and fib(%i)" %

                   (tab, n-1, n-2))

            memo[(n-1,n-2)] = fib_mem(n-1,tab

+1

                          ) 



+ fib_mem(n-2,tab+1)

        return memo[(n-1,n-2)]

Using memoization, the recursive function doesn’t compute 20 additions but 

rather uses just six, the essential ones used as building blocks to solve the initial 

requirement for computing a certain number in the sequence:

fib_mem(7)

lvl 0, Summing fib(6) and fib(5)

lvl 1, Summing fib(5) and fib(4)

lvl 2, Summing fib(4) and fib(3)

lvl 3, Summing fib(3) and fib(2)

lvl 4, Summing fib(2) and fib(1)

lvl 5, Summing fib(1) and fib(0)

13

Looking inside the 



memo

 dictionary, you can find the sequence of sums that define 

the Fibonacci sequence starting from 

1

:



memo

{(1, 0): 1, (2, 1): 2, (3, 2): 3, (4, 3): 5, (5, 4): 8,

 (6, 5): 13}


Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   501   502   503   504   505   506   507   508   ...   651




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2025
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish