정수가 아닌 값으로 배낭 DP 알고리즘에 필요한 시간과 공간을 줄이려고합니다. 해시 배열이있는 배낭 DP
http://en.wikipedia.org/wiki/Knapsack_problem#Meet-in-the-Middle_Algorithm
In particular, if the [elements] are nonnegative but not integers, we could
still use the dynamic programming algorithm by scaling and rounding (i.e. using
fixed-point arithmetic), but if the problem requires fractional digits of
precision to arrive at the correct answer, W will need to be scaled by 10^d,
and the DP algorithm will require O(W * 10^d) space and O(nW * 10^d) time.
민주당 배낭 알고리즘
결과 그것을 채우고, A [N x 폭] 행렬을 이용하지만 충전 만나지 일부 열은 - 그들이 개체 가중치의 조합과 일치하지 않는다. 이 방법을 사용하면 각 행에 0이 채워지고 시간과 공간이 낭비됩니다.행렬 대신 해시 배열을 사용하면 필요한 시간과 공간을 줄일 수 있습니다.
edit:
knapsack capacity = 2
items: [{weight:2,value:3} ]
[0 1 2]
[0 0 0]
2: [0 0 3]
^
Do we need this column?
Substitution with hash:
2: {0:0, 2:3}
파이썬에서 dict 삽입은 O (n) 최악의 경우와 O (1) "상각 된"선형 시간을가집니다.
내가 누락 된 항목이 있습니까?
배낭 DP 알고리즘에서 이러한 변형의 복잡성은 어떻게됩니까?
나는 따라 가지 않습니다. 어떻게 바꾸려고하는거야? 사전의 키/값은 무엇입니까? – amit
해시는 행렬의 "행"을 대체합니다. 키가 가중치이고 값은 부분 집합 합이됩니다 – lordkrandel
그리고 정확하게 저장하려고합니까? "행"에는 여전히 같은 수의 요소 (가중치)가 필요합니다. 당신이 그것에 관해 다른 생각을 가지고 있다면 - 우리는 그것을 기꺼이들을 것입니다. – amit