Dynamic programming doesn't give correct answer

僤鯓⒐⒋嵵緔 提交于 2019-12-06 16:42:16
Andrea Corbellini

I think this is what you want:

def Opt(n):
    if len(n) == 1:
        return 0
    else:
        return sum(n) + min(Opt(n[:i]) + Opt(n[i:])
                            for i in range(1, len(n)))

Example:

>>> Opt([1])
0
>>> Opt([1, 2])
3
>>> Opt([2, 3])
5
>>> Opt([1, 2, 3])
9
>>> Opt([1, 2, 3, 4])
19

Dynamic programming is about dividing the "big problem" into small subproblems.

So, first of all, you should identify how the big problem is related to the subproblems. You do this by writing a recurrence relation. In this case:

Opt(nums) = sum(nums) + min(...)

You also need a starting point:

Opt(nums) = 0 iff len(nums) == 1

As you can see, once you have wrote the recurrence relation, transforming it into Python code is often straightforward.

It's important to understand that each subproblem is self-contained, and should not need external input. Your use of global variables was not only producing the wrong result, but was against the spirit of dynamic programming.

Your use of trees for expressing Opt() is nice. What you forgot to do is writing the relationship between each node and its children. If you did, I'm almost sure that you would have found the correct solution yourself.

We are not finished yet (thanks Stefan Pochmann for noticing). In order to build a true dynamic programming solution, you also need to avoid solving the same problem more than once. Currently, running Opt([1,2,3,4]) results in calling Opt([1,2]) more than once. One way to prevent that is by using memoization:

cache = {}

def Opt(n):
    # tuple objects are hashable and can be put in the cache.
    n = tuple(n)

    if n in cache:
        return cache[n]

    if len(n) == 1:
        result = 0
    else:
        result = sum(n) + min(Opt(n[:i]) + Opt(n[i:])
                              for i in range(1, len(n)))

    cache[n] = result
    return result

By the way, remember to handle the case where n is empty (i.e. len(n) == 0).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!