forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Corrected the directory of Fractional Knapsack algorithm (TheAlgorith…
…ms#7086) * Moved fractional knapsack from 'dynamic_programming' to 'greedy_methods' * Updated DIRECTORY.md
- Loading branch information
1 parent
6d20e2b
commit 7f6e0b6
Showing
3 changed files
with
55 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
106 changes: 53 additions & 53 deletions
106
dynamic_programming/fractional_knapsack_2.py → greedy_methods/fractional_knapsack_2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,53 @@ | ||
# https://en.wikipedia.org/wiki/Continuous_knapsack_problem | ||
# https://www.guru99.com/fractional-knapsack-problem-greedy.html | ||
# https://medium.com/walkinthecode/greedy-algorithm-fractional-knapsack-problem-9aba1daecc93 | ||
|
||
from __future__ import annotations | ||
|
||
|
||
def fractional_knapsack( | ||
value: list[int], weight: list[int], capacity: int | ||
) -> tuple[float, list[float]]: | ||
""" | ||
>>> value = [1, 3, 5, 7, 9] | ||
>>> weight = [0.9, 0.7, 0.5, 0.3, 0.1] | ||
>>> fractional_knapsack(value, weight, 5) | ||
(25, [1, 1, 1, 1, 1]) | ||
>>> fractional_knapsack(value, weight, 15) | ||
(25, [1, 1, 1, 1, 1]) | ||
>>> fractional_knapsack(value, weight, 25) | ||
(25, [1, 1, 1, 1, 1]) | ||
>>> fractional_knapsack(value, weight, 26) | ||
(25, [1, 1, 1, 1, 1]) | ||
>>> fractional_knapsack(value, weight, -1) | ||
(-90.0, [0, 0, 0, 0, -10.0]) | ||
>>> fractional_knapsack([1, 3, 5, 7], weight, 30) | ||
(16, [1, 1, 1, 1]) | ||
>>> fractional_knapsack(value, [0.9, 0.7, 0.5, 0.3, 0.1], 30) | ||
(25, [1, 1, 1, 1, 1]) | ||
>>> fractional_knapsack([], [], 30) | ||
(0, []) | ||
""" | ||
index = list(range(len(value))) | ||
ratio = [v / w for v, w in zip(value, weight)] | ||
index.sort(key=lambda i: ratio[i], reverse=True) | ||
|
||
max_value: float = 0 | ||
fractions: list[float] = [0] * len(value) | ||
for i in index: | ||
if weight[i] <= capacity: | ||
fractions[i] = 1 | ||
max_value += value[i] | ||
capacity -= weight[i] | ||
else: | ||
fractions[i] = capacity / weight[i] | ||
max_value += value[i] * capacity / weight[i] | ||
break | ||
|
||
return max_value, fractions | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
# https://en.wikipedia.org/wiki/Continuous_knapsack_problem | ||
# https://www.guru99.com/fractional-knapsack-problem-greedy.html | ||
# https://medium.com/walkinthecode/greedy-algorithm-fractional-knapsack-problem-9aba1daecc93 | ||
|
||
from __future__ import annotations | ||
|
||
|
||
def fractional_knapsack( | ||
value: list[int], weight: list[int], capacity: int | ||
) -> tuple[float, list[float]]: | ||
""" | ||
>>> value = [1, 3, 5, 7, 9] | ||
>>> weight = [0.9, 0.7, 0.5, 0.3, 0.1] | ||
>>> fractional_knapsack(value, weight, 5) | ||
(25, [1, 1, 1, 1, 1]) | ||
>>> fractional_knapsack(value, weight, 15) | ||
(25, [1, 1, 1, 1, 1]) | ||
>>> fractional_knapsack(value, weight, 25) | ||
(25, [1, 1, 1, 1, 1]) | ||
>>> fractional_knapsack(value, weight, 26) | ||
(25, [1, 1, 1, 1, 1]) | ||
>>> fractional_knapsack(value, weight, -1) | ||
(-90.0, [0, 0, 0, 0, -10.0]) | ||
>>> fractional_knapsack([1, 3, 5, 7], weight, 30) | ||
(16, [1, 1, 1, 1]) | ||
>>> fractional_knapsack(value, [0.9, 0.7, 0.5, 0.3, 0.1], 30) | ||
(25, [1, 1, 1, 1, 1]) | ||
>>> fractional_knapsack([], [], 30) | ||
(0, []) | ||
""" | ||
index = list(range(len(value))) | ||
ratio = [v / w for v, w in zip(value, weight)] | ||
index.sort(key=lambda i: ratio[i], reverse=True) | ||
|
||
max_value: float = 0 | ||
fractions: list[float] = [0] * len(value) | ||
for i in index: | ||
if weight[i] <= capacity: | ||
fractions[i] = 1 | ||
max_value += value[i] | ||
capacity -= weight[i] | ||
else: | ||
fractions[i] = capacity / weight[i] | ||
max_value += value[i] * capacity / weight[i] | ||
break | ||
|
||
return max_value, fractions | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |