mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Corrected the directory of Fractional Knapsack algorithm (#7086)
* Moved fractional knapsack from 'dynamic_programming' to 'greedy_methods' * Updated DIRECTORY.md
This commit is contained in:
parent
6d20e2b750
commit
7f6e0b656f
|
@ -279,8 +279,6 @@
|
||||||
* [Fast Fibonacci](dynamic_programming/fast_fibonacci.py)
|
* [Fast Fibonacci](dynamic_programming/fast_fibonacci.py)
|
||||||
* [Fibonacci](dynamic_programming/fibonacci.py)
|
* [Fibonacci](dynamic_programming/fibonacci.py)
|
||||||
* [Floyd Warshall](dynamic_programming/floyd_warshall.py)
|
* [Floyd Warshall](dynamic_programming/floyd_warshall.py)
|
||||||
* [Fractional Knapsack](dynamic_programming/fractional_knapsack.py)
|
|
||||||
* [Fractional Knapsack 2](dynamic_programming/fractional_knapsack_2.py)
|
|
||||||
* [Integer Partition](dynamic_programming/integer_partition.py)
|
* [Integer Partition](dynamic_programming/integer_partition.py)
|
||||||
* [Iterating Through Submasks](dynamic_programming/iterating_through_submasks.py)
|
* [Iterating Through Submasks](dynamic_programming/iterating_through_submasks.py)
|
||||||
* [Knapsack](dynamic_programming/knapsack.py)
|
* [Knapsack](dynamic_programming/knapsack.py)
|
||||||
|
@ -396,6 +394,8 @@
|
||||||
* [Test Min Spanning Tree Prim](graphs/tests/test_min_spanning_tree_prim.py)
|
* [Test Min Spanning Tree Prim](graphs/tests/test_min_spanning_tree_prim.py)
|
||||||
|
|
||||||
## Greedy Methods
|
## Greedy Methods
|
||||||
|
* [Fractional Knapsack](greedy_methods/fractional_knapsack.py)
|
||||||
|
* [Fractional Knapsack 2](greedy_methods/fractional_knapsack_2.py)
|
||||||
* [Optimal Merge Pattern](greedy_methods/optimal_merge_pattern.py)
|
* [Optimal Merge Pattern](greedy_methods/optimal_merge_pattern.py)
|
||||||
|
|
||||||
## Hashes
|
## Hashes
|
||||||
|
|
|
@ -1,53 +1,53 @@
|
||||||
# https://en.wikipedia.org/wiki/Continuous_knapsack_problem
|
# https://en.wikipedia.org/wiki/Continuous_knapsack_problem
|
||||||
# https://www.guru99.com/fractional-knapsack-problem-greedy.html
|
# https://www.guru99.com/fractional-knapsack-problem-greedy.html
|
||||||
# https://medium.com/walkinthecode/greedy-algorithm-fractional-knapsack-problem-9aba1daecc93
|
# https://medium.com/walkinthecode/greedy-algorithm-fractional-knapsack-problem-9aba1daecc93
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
def fractional_knapsack(
|
def fractional_knapsack(
|
||||||
value: list[int], weight: list[int], capacity: int
|
value: list[int], weight: list[int], capacity: int
|
||||||
) -> tuple[float, list[float]]:
|
) -> tuple[float, list[float]]:
|
||||||
"""
|
"""
|
||||||
>>> value = [1, 3, 5, 7, 9]
|
>>> value = [1, 3, 5, 7, 9]
|
||||||
>>> weight = [0.9, 0.7, 0.5, 0.3, 0.1]
|
>>> weight = [0.9, 0.7, 0.5, 0.3, 0.1]
|
||||||
>>> fractional_knapsack(value, weight, 5)
|
>>> fractional_knapsack(value, weight, 5)
|
||||||
(25, [1, 1, 1, 1, 1])
|
(25, [1, 1, 1, 1, 1])
|
||||||
>>> fractional_knapsack(value, weight, 15)
|
>>> fractional_knapsack(value, weight, 15)
|
||||||
(25, [1, 1, 1, 1, 1])
|
(25, [1, 1, 1, 1, 1])
|
||||||
>>> fractional_knapsack(value, weight, 25)
|
>>> fractional_knapsack(value, weight, 25)
|
||||||
(25, [1, 1, 1, 1, 1])
|
(25, [1, 1, 1, 1, 1])
|
||||||
>>> fractional_knapsack(value, weight, 26)
|
>>> fractional_knapsack(value, weight, 26)
|
||||||
(25, [1, 1, 1, 1, 1])
|
(25, [1, 1, 1, 1, 1])
|
||||||
>>> fractional_knapsack(value, weight, -1)
|
>>> fractional_knapsack(value, weight, -1)
|
||||||
(-90.0, [0, 0, 0, 0, -10.0])
|
(-90.0, [0, 0, 0, 0, -10.0])
|
||||||
>>> fractional_knapsack([1, 3, 5, 7], weight, 30)
|
>>> fractional_knapsack([1, 3, 5, 7], weight, 30)
|
||||||
(16, [1, 1, 1, 1])
|
(16, [1, 1, 1, 1])
|
||||||
>>> fractional_knapsack(value, [0.9, 0.7, 0.5, 0.3, 0.1], 30)
|
>>> fractional_knapsack(value, [0.9, 0.7, 0.5, 0.3, 0.1], 30)
|
||||||
(25, [1, 1, 1, 1, 1])
|
(25, [1, 1, 1, 1, 1])
|
||||||
>>> fractional_knapsack([], [], 30)
|
>>> fractional_knapsack([], [], 30)
|
||||||
(0, [])
|
(0, [])
|
||||||
"""
|
"""
|
||||||
index = list(range(len(value)))
|
index = list(range(len(value)))
|
||||||
ratio = [v / w for v, w in zip(value, weight)]
|
ratio = [v / w for v, w in zip(value, weight)]
|
||||||
index.sort(key=lambda i: ratio[i], reverse=True)
|
index.sort(key=lambda i: ratio[i], reverse=True)
|
||||||
|
|
||||||
max_value: float = 0
|
max_value: float = 0
|
||||||
fractions: list[float] = [0] * len(value)
|
fractions: list[float] = [0] * len(value)
|
||||||
for i in index:
|
for i in index:
|
||||||
if weight[i] <= capacity:
|
if weight[i] <= capacity:
|
||||||
fractions[i] = 1
|
fractions[i] = 1
|
||||||
max_value += value[i]
|
max_value += value[i]
|
||||||
capacity -= weight[i]
|
capacity -= weight[i]
|
||||||
else:
|
else:
|
||||||
fractions[i] = capacity / weight[i]
|
fractions[i] = capacity / weight[i]
|
||||||
max_value += value[i] * capacity / weight[i]
|
max_value += value[i] * capacity / weight[i]
|
||||||
break
|
break
|
||||||
|
|
||||||
return max_value, fractions
|
return max_value, fractions
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
Loading…
Reference in New Issue
Block a user