Algorithms For Dummies


PART 5   Challenging Difficult Problems



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

 

   


  PART 5 

 Challenging Difficult Problems

    elif n == 1:

        return 1

    else:

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

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

        return fib(n-1,tab

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

The code prints the splits generated by each recursion level. The following output 

shows what happens when you call 

fib()

 with an input value of 



7

:

fib(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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

13

The output shows 20 splits. Some numbers appear more than once as part of the 



splits. It seems like an ideal case for applying dynamic programming. The follow-

ing code adds a dictionary, called 

memo

, which stores previous results. After the 



recursion splits a number, it checks whether the result already appears in the 

dictionary before starting the next recursive branch. If it finds the result, the code 

uses the precomputed result, as shown here:

memo = dict()

def fib_mem(n, tab=0):



CHAPTER 16


Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   500   501   502   503   504   505   506   507   ...   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