3
3
#so i kept it under recursion
4
4
#its about dynamic programming
5
5
#its kinda tricky to understand
6
+ #if you are familiar with convex optimization or lagrangian
7
+ #its better to use em instead of this
6
8
7
9
8
- #knapsack problem is to maxmize the value
10
+ #knapsack problem is to maximize the value
9
11
#while having a weight constraint
10
12
#each value has a different weight
11
13
#the knapsack has a maximum capacity which is the constraint
12
14
13
15
14
- #to solve the problem, we have to use recursive thinking
15
- #lets create a list of capacity
16
- #before reaching the maximum capacity
16
+ #to solve the problem,we have to use recursive thinking
17
+ #lets create a list from 1 to the maximum capacity
17
18
#for each capacity in the list
18
- #we try to reach its own optimization
19
- #say we have c as constrait
19
+ #we try to reach the optimal allocation of weight at the given capacity
20
+ #say we have c as the maximum capacity
20
21
#we remove the last item i
21
22
#we get weight c-w[i]
22
- #we wanna make sure c-w[i] is still optmized
23
- #by optimized, we mean for the same weight we can achieve the highest value
24
- #if n -w[i] is not optmized
23
+ #we wanna make sure our allocation at c-w[i] is still the optimal
24
+ #by optimal, we mean for the same weight we can achieve the highest value
25
+ #if c -w[i] is not the optimal
25
26
#we can find another combo with the same weight but higher value
26
- #we add back the item i into the knapsack then
27
- #the total value we get would be larger than the previous so-called optmization
28
- #it will contradict the definition of optimization
29
- #hence, for each capacity,we keep removing items
30
- #until we reach base case 0, and it always stays optimized
27
+ #we add the item i back into the knapsack then
28
+ #the new total value we get would be larger than the previous so-called optimal
29
+ #it will contradict the definition of optimal
30
+ #hence,for each capacity,we keep removing items
31
+ #until we reach base case 0,and it always stays the optimal at the given capacity
31
32
32
33
33
- #to get final optmization on the constraint
34
+ #to get the optimal status
34
35
#we shall do a traversal on all items
35
36
#we create a matrix called l with (number of items) * (maximum capacity)
36
- #for each capacity level, we try to add a new item
37
- #if the item weight is larger than the current capacity level
38
- #the knapsack stays the previous status without item i which is l[i-1][j]
39
- #if the item weight is smaller than the current capacity level
40
- #we try to see whether adding item i would be the optimization
37
+ #for each capacity level,we try to add a new item
38
+ #if adding new item causes the overall weight larger than the current capacity level
39
+ #the knapsack reverts to the previous status without item i which is l[i-1][j]
40
+ #if adding new item doesnt cause the overall weight bigger than the current capacity level
41
+ #we try to see whether adding item i would be the new optimal case
41
42
#so we compare the previous status with the status after adding item i
42
43
#the status after adding item i shall be l[i-1][j-w[i-1]]+v[i-1]
43
44
#we use j-w[i-1] cuz adding item i would reduce the capacity we have
44
45
#we have to use the current constraint level j to minus item i weight
45
46
#note that we use w[i-1] and v[i-1] instead of w[i] and v[i] to represent item i
46
47
#that is becuz weight and value list index at 0
47
- #when we refer to the first item i, we intend to say item 1
48
- #but computers refer to item 0
49
- #so i-1 actually refers to the current item i
50
48
#the alternative way is to insert 0 at index 0 for both v and w
51
49
#we would be able to use v[i] instead of v[i-1]
52
50
53
51
54
- def f (v ,w ,c ):
55
- l = []
56
- #as mentioned before
57
- #v indexes from 0 which is why we use len(v)+1
58
- #so that we get the real item i in our concept instead of item i-1 in computers concept
59
- #the same applies to c
60
- #in this section, we create a list consists of lists, which is a matrix
61
- #its actually a matrix of (number of items+1 )* (c+1)
62
- for i in range (len (v )+ 1 ):
63
- l .append ([0 ]* (c + 1 ))
64
-
52
+ def knapsack (v ,w ,c ):
65
53
66
- #now we begin our traversal on all elements in matrix
67
- #i starts from 1 cuz we would be using i-1 to imply item i in our concept
68
- #so we dont go outta index for the case of item 0
69
- #cuz there is no item -1
70
- for i in range (1 ,len (v )+ 1 ):
71
- for j in range (c + 1 ):
72
- #this is the part to check if adding item i would exceed the current constraint level j
73
- #if it does, we go to the previous status
74
- #if not, we shall find out whether adding item i would be optimized
75
- if w [i - 1 ]> j :
76
- l [i ][j ]= l [i - 1 ][j ]
77
- else :
78
- #we use max funcion to see if adding item i would be optimized
79
- l [i ][j ]= max (l [i - 1 ][j ],l [i - 1 ][j - w [i - 1 ]]+ v [i - 1 ])
80
- return l
54
+ #as mentioned before
55
+ #v indexes from 0 which is why we use len(v)+1
56
+ #the same applies to c
57
+ #in this section,we create a nested list with size of (number of items+1)*(c+1)
58
+ l = [[0 ]* (c + 1 ) for _ in range (len (v )+ 1 )]
81
59
82
- print (f ([0 ,60 ,100 ,120 ],[0 ,10 ,20 ,30 ],50 ))
60
+ #now we begin our traversal on all elements in matrix
61
+ #i starts from 1 cuz we would be using i-1 to imply item i
62
+ for i in range (1 ,len (v )+ 1 ):
63
+ for j in range (c + 1 ):
64
+
65
+ #this is the part to check if adding item i would exceed the current capacity j
66
+ #if it does,we go to the previous status
67
+ #if not,we shall find out whether adding item i would be the new optimal
68
+ if w [i - 1 ]> j :
69
+
70
+ l [i ][j ]= l [i - 1 ][j ]
71
+
72
+ else :
73
+
74
+ #we use max funcion to see if adding item i would be the new optimal
75
+ l [i ][j ]= max (l [i - 1 ][j ],l [i - 1 ][j - w [i - 1 ]]+ v [i - 1 ])
76
+
77
+ return l [len (v )][c ]
78
+
79
+ print (knapsack ([0 ,40 ,60 ,50 ,120 ],[0 ,10 ,15 ,20 ,30 ],50 ))
0 commit comments