Skip to content

Commit 90ef219

Browse files
authored
Merge pull request #52 from sharadbhat/master
Added fractional knapsack and minor changes
2 parents e74ea15 + f6faf55 commit 90ef219

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

pygorithm/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"Songzhuozhuo 'souo'",
5757
"Emil 'Skeen' Madsen",
5858
"Ian 'IanDoarn' Doarn",
59-
"Timothy 'Tjstretchalot' Moore"
59+
"Timothy 'Tjstretchalot' Moore",
60+
"Sharad 'sharadbhat' Bhat"
6061
]
6162

6263
__all__ = [
@@ -69,5 +70,6 @@
6970
'sorting',
7071
'string',
7172
'pathfinding'
72-
'geometry'
73+
'geometry',
74+
'greedy_algorithm'
7375
]

pygorithm/dynamic_programming/binary_knapsack.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,25 @@
33
Created At: 25th August 2017
44
'''
55

6-
def knapsack(W, value, weight, n):
6+
import inspect
7+
8+
def knapsack(W, value, weight):
79
'''
810
:param W: maximum weight capacity
911
:param value: an array of values of items in the knapsack
1012
:param weight: an array of weights of items in the knapsack
11-
:param n: number of items in the knapsack
1213
'''
14+
if type(value) is not list:
15+
raise TypeError("binary knapsack only accepts lists, not {}".format(str(type(value))))
16+
if type(weight) is not list:
17+
raise TypeError("binary knapsack only accepts lists, not {}".format(str(type(weight))))
18+
19+
if len(value) != len(weight):
20+
raise ValueError("both the lists must be of same length")
21+
22+
# n = number of items
23+
n = len(value)
24+
1325
knap_sack = [[0 for x in range(W+1)] for x in range(n+1)]
1426

1527
for j in range(W + 1):
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Collection for greedy algorithms
3+
"""
4+
from . import fractional_knapsack
5+
6+
__all__ = [
7+
'fractional_knapsack'
8+
]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
Author: SHARAD BHAT
3+
Created On: 22nd August 2017
4+
"""
5+
6+
import inspect
7+
8+
def knapsack(W, item_values, item_weights):
9+
"""
10+
:param W: maximum weight capacity
11+
:param item_values: a list of values of items in the knapsack
12+
:param item_weights: a list of weights of items in the knapsack
13+
"""
14+
15+
if type(item_values) is not list:
16+
raise TypeError("fractional knapsack only accepts lists, not {}".format(str(type(item_values))))
17+
if type(item_weights) is not list:
18+
raise TypeError("fractional knapsack only accepts lists, not {}".format(str(type(item_weights))))
19+
20+
if len(item_values) != len(item_weights):
21+
raise ValueError("length of both lists must be same")
22+
23+
n = len(item_values)
24+
25+
fractional_weights = []
26+
for i in range(0, n):
27+
fractional_weights.append(item_values[i] / item_weights[i])
28+
29+
# Sorting items based on maximum fractional profit
30+
for i in range(0, n):
31+
maximum = i
32+
for j in range(i, n):
33+
if fractional_weights[maximum] < fractional_weights[j]:
34+
maximum = j
35+
36+
fractional_weights[i], fractional_weights[maximum] = fractional_weights[maximum], fractional_weights[i]
37+
item_values[i], item_values[maximum] = item_values[maximum], item_values[i]
38+
item_weights[i], item_weights[maximum] = item_weights[maximum], item_weights[i]
39+
40+
# Placing items in knapsack
41+
remaining_space = W
42+
profit = 0
43+
for i in range(0, n):
44+
if remaining_space > item_weights[i]:
45+
profit += item_values[i]
46+
remaining_space -= item_weights[i]
47+
else:
48+
profit += fractional_weights[i] * remaining_space
49+
remaining_space = 0
50+
break
51+
52+
return profit
53+
54+
55+
56+
def get_code():
57+
"""
58+
returns the code for the knapsack function
59+
"""
60+
return inspect.getsource(knapsack)

0 commit comments

Comments
 (0)