简体   繁体   English

动态编程自上而下的方法-矩阵中的最低成本(Python)

[英]Dynamic programming Topdown approach - minimum cost in a matrix (in python)

I have to implement some functions in python to find the minimum cost in a matrix.We must go down or right and to an adjacent number so at each step we have only two possible choices. 我必须在python中实现一些函数才能找到矩阵中的最小成本。我们必须向下或向右移至相邻的数字,因此在每一步中只有两个可能的选择。

i wrote the fisrt version (naive one) which works: 我写了第一个版本的fisrt(天真):

def minimal_trajectorry_helper(matrix, m, n):
    if ( (n > len(matrix) -1 ) or (m > len(matrix) -1)):
        return 1000000000

    elif ((m == len(matrix) - 1) and (n == len(matrix) - 1)):
        return matrix[m][n]

    else:
         result = matrix[m][n] + min(minimal_trajectorry_helper(matrix, m+1, n),
                            minimal_trajectorry_helper(matrix, m, n+1) )
         return result

But when i want to optimize it using memoization i can't find the right answer. 但是,当我想使用记忆优化它时,我找不到正确的答案。 I tried different ways but i wasn't able to do it correctly. 我尝试了不同的方法,但无法正确执行。 Here is what i wrote: 这是我写的:

def dptd_minimal_trajectorry_helper(matrix, m, n, track):
    if ( (n > len(matrix) -1 ) or (m > len(matrix) -1)):
        return 1000000000

    elif ((m == len(matrix) - 1) and (n == len(matrix) - 1)):
        return matrix[m][n]

    elif (track[m][n] != -1):
        return track[m][n] 

    else:
        result = matrix[m][n] + min(dptd_minimal_trajectorry_helper(matrix, m+1, n, track),
                            dptd_minimal_trajectorry_helper(matrix, m, n+1, track) 
        track[m][n] = result
        return result

Here is an example : 这是一个例子:

     [2,3,1,1,6]
     [1,4,4,1,4]
     [7,1,2,2,5]
     [2,1,3,8,3]
     [2,4,3,2,1]

The naive version gives me the right anwser which is 18 -> 2,1,4,1,1,3,3,2,1 But the second one gives me 12 天真的版本给了我正确的答案,即18-> 2,1,4,1,1,3,3,2,1但是第二个给了我12

Thanks in advance for any help :) 在此先感谢您的帮助:)

EDIT : I call the naive version like minimal_trajectorry_helper(matrix, 0, 0) and the optimized one like dptd_minimal_trajectorry_helper(matrix, m, n, track) where track is initialized by : track = [[-1]*5]*5 编辑:我称其为朴素的版本,如minimal_trajectorry_helper(matrix,0,0)和已优化的版本,如dptd_minimal_trajectorry_helper(matrix,m,n,track) ,其中轨道是通过以下方式初始化的: track = [[-1] * 5] * 5

The problem here is the way that you have intialised the variable track. 这里的问题是您初始化可变轨道的方式。 Observe the following: 请注意以下几点:

track = [[-1]*5]*5
track[2][3]=4
print(track)

Result 结果

[-1, -1, -1, 4, -1]
[-1, -1, -1, 4, -1]
[-1, -1, -1, 4, -1]
[-1, -1, -1, 4, -1]
[-1, -1, -1, 4, -1]

Why has the happened? 为什么会这样呢? when you do [a]*n you are creating n references to the same object a . 当您执行[a]*n您正在创建对同一对象a n个引用。 In the case of a list (which is mutable), when you change it, (eg in the line track[m][n]=result ) that change will be reflected in all of the references to that list. 对于列表(可变的),当您对其进行更改时(例如,在行track[m][n]=result ),该更改将反映在对该列表的所有引用中。

How to fix it 如何修复

Use a list comprehnsion instead, this will create seperate copys of the 'inner list' eg 相反,使用列表推导,这将创建“内部列表”的单独副本,例如

track = [[-1]*5 for i in range(5)]

I have tried this out with the code above, and that seems to fix the problem. 我已经使用上面的代码进行了尝试,似乎可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM