Skip to content

Commit 7f6e0b6

Browse files
Corrected the directory of Fractional Knapsack algorithm (TheAlgorithms#7086)
* Moved fractional knapsack from 'dynamic_programming' to 'greedy_methods' * Updated DIRECTORY.md
1 parent 6d20e2b commit 7f6e0b6

File tree

3 files changed

+55
-55
lines changed

3 files changed

+55
-55
lines changed

DIRECTORY.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,6 @@
279279
* [Fast Fibonacci](dynamic_programming/fast_fibonacci.py)
280280
* [Fibonacci](dynamic_programming/fibonacci.py)
281281
* [Floyd Warshall](dynamic_programming/floyd_warshall.py)
282-
* [Fractional Knapsack](dynamic_programming/fractional_knapsack.py)
283-
* [Fractional Knapsack 2](dynamic_programming/fractional_knapsack_2.py)
284282
* [Integer Partition](dynamic_programming/integer_partition.py)
285283
* [Iterating Through Submasks](dynamic_programming/iterating_through_submasks.py)
286284
* [Knapsack](dynamic_programming/knapsack.py)
@@ -396,6 +394,8 @@
396394
* [Test Min Spanning Tree Prim](graphs/tests/test_min_spanning_tree_prim.py)
397395

398396
## Greedy Methods
397+
* [Fractional Knapsack](greedy_methods/fractional_knapsack.py)
398+
* [Fractional Knapsack 2](greedy_methods/fractional_knapsack_2.py)
399399
* [Optimal Merge Pattern](greedy_methods/optimal_merge_pattern.py)
400400

401401
## Hashes
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
# https://en.wikipedia.org/wiki/Continuous_knapsack_problem
2-
# https://www.guru99.com/fractional-knapsack-problem-greedy.html
3-
# https://medium.com/walkinthecode/greedy-algorithm-fractional-knapsack-problem-9aba1daecc93
4-
5-
from __future__ import annotations
6-
7-
8-
def fractional_knapsack(
9-
value: list[int], weight: list[int], capacity: int
10-
) -> tuple[float, list[float]]:
11-
"""
12-
>>> value = [1, 3, 5, 7, 9]
13-
>>> weight = [0.9, 0.7, 0.5, 0.3, 0.1]
14-
>>> fractional_knapsack(value, weight, 5)
15-
(25, [1, 1, 1, 1, 1])
16-
>>> fractional_knapsack(value, weight, 15)
17-
(25, [1, 1, 1, 1, 1])
18-
>>> fractional_knapsack(value, weight, 25)
19-
(25, [1, 1, 1, 1, 1])
20-
>>> fractional_knapsack(value, weight, 26)
21-
(25, [1, 1, 1, 1, 1])
22-
>>> fractional_knapsack(value, weight, -1)
23-
(-90.0, [0, 0, 0, 0, -10.0])
24-
>>> fractional_knapsack([1, 3, 5, 7], weight, 30)
25-
(16, [1, 1, 1, 1])
26-
>>> fractional_knapsack(value, [0.9, 0.7, 0.5, 0.3, 0.1], 30)
27-
(25, [1, 1, 1, 1, 1])
28-
>>> fractional_knapsack([], [], 30)
29-
(0, [])
30-
"""
31-
index = list(range(len(value)))
32-
ratio = [v / w for v, w in zip(value, weight)]
33-
index.sort(key=lambda i: ratio[i], reverse=True)
34-
35-
max_value: float = 0
36-
fractions: list[float] = [0] * len(value)
37-
for i in index:
38-
if weight[i] <= capacity:
39-
fractions[i] = 1
40-
max_value += value[i]
41-
capacity -= weight[i]
42-
else:
43-
fractions[i] = capacity / weight[i]
44-
max_value += value[i] * capacity / weight[i]
45-
break
46-
47-
return max_value, fractions
48-
49-
50-
if __name__ == "__main__":
51-
import doctest
52-
53-
doctest.testmod()
1+
# https://en.wikipedia.org/wiki/Continuous_knapsack_problem
2+
# https://www.guru99.com/fractional-knapsack-problem-greedy.html
3+
# https://medium.com/walkinthecode/greedy-algorithm-fractional-knapsack-problem-9aba1daecc93
4+
5+
from __future__ import annotations
6+
7+
8+
def fractional_knapsack(
9+
value: list[int], weight: list[int], capacity: int
10+
) -> tuple[float, list[float]]:
11+
"""
12+
>>> value = [1, 3, 5, 7, 9]
13+
>>> weight = [0.9, 0.7, 0.5, 0.3, 0.1]
14+
>>> fractional_knapsack(value, weight, 5)
15+
(25, [1, 1, 1, 1, 1])
16+
>>> fractional_knapsack(value, weight, 15)
17+
(25, [1, 1, 1, 1, 1])
18+
>>> fractional_knapsack(value, weight, 25)
19+
(25, [1, 1, 1, 1, 1])
20+
>>> fractional_knapsack(value, weight, 26)
21+
(25, [1, 1, 1, 1, 1])
22+
>>> fractional_knapsack(value, weight, -1)
23+
(-90.0, [0, 0, 0, 0, -10.0])
24+
>>> fractional_knapsack([1, 3, 5, 7], weight, 30)
25+
(16, [1, 1, 1, 1])
26+
>>> fractional_knapsack(value, [0.9, 0.7, 0.5, 0.3, 0.1], 30)
27+
(25, [1, 1, 1, 1, 1])
28+
>>> fractional_knapsack([], [], 30)
29+
(0, [])
30+
"""
31+
index = list(range(len(value)))
32+
ratio = [v / w for v, w in zip(value, weight)]
33+
index.sort(key=lambda i: ratio[i], reverse=True)
34+
35+
max_value: float = 0
36+
fractions: list[float] = [0] * len(value)
37+
for i in index:
38+
if weight[i] <= capacity:
39+
fractions[i] = 1
40+
max_value += value[i]
41+
capacity -= weight[i]
42+
else:
43+
fractions[i] = capacity / weight[i]
44+
max_value += value[i] * capacity / weight[i]
45+
break
46+
47+
return max_value, fractions
48+
49+
50+
if __name__ == "__main__":
51+
import doctest
52+
53+
doctest.testmod()

0 commit comments

Comments
 (0)