Source code online books for professionals by professionals



Download 4,67 Mb.
Pdf ko'rish
bet147/266
Sana31.12.2021
Hajmi4,67 Mb.
#213682
1   ...   143   144   145   146   147   148   149   150   ...   266
Bog'liq
2 5296731884800181221

Listing 8-8.  A Memoized Recursive Solution to the LCS Problem
def rec_lcs(a,b):                               # Longest common subsequence
    @memo                                       # L is memoized
    def L(i,j):                                 # Prefixes a[:i] and b[:j]
        if min(i,j) < 0: return 0               # One prefix is empty
        if a[i] == b[j]: return 1 + L(i-1,j-1)  # Match! Move diagonally
        return max(L(i-1,j), L(i,j-1))          # Chop off either a[i] or b[j]
    return L(len(a)-1,len(b)-1)                 # Run L on entire sequences
 
This recursive decomposition can easily be seen as a dynamic decision process (do we chop off an element from 
the first sequence, from the second, or from both?), which can be represented as a DAG (see Figure 
8-5
). We start in 
the node represented by the full sequences, and we try to find the longest path back to the node representing two 
empty prefixes. It’s important to be clear about what the “longest path” is here, though—that is, what the edge weights 
are. The only time we can extend the LCS (which is our goal) is when we chop off two identical elements, represented 
by the DAG edges that are diagonal when the nodes are placed in a grid, as in Figure 
8-5
. These edges, then, have a 
weight of one, while the other edges have a weight of zero.


Chapter 8 

 tangled dependenCies and MeMoization 
177
For the usual reasons, you may want to reverse the solution and make it iterative. Listing 8-9 gives you a version 
that saves memory by keeping only the current and the previous row of the DP matrix. (You could save a bit more, 
though; see Exercise 8-13.) Note that cur[i-1] corresponds to L(i-1,j) in the recursive version, while pre[i] and 
pre[i-1] correspond to L(i,j-1) and L(i-1,j-1), respectively.

Download 4,67 Mb.

Do'stlaringiz bilan baham:
1   ...   143   144   145   146   147   148   149   150   ...   266




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
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