Skip to content

Commit d35fe4d

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent a6fb347 commit d35fe4d

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

knapsack/knapsack_branch_and_bound.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ class Node:
2929
bound: float
3030

3131

32-
def calculate_bound(
33-
node: Node, capacity: int, items: List[Item]
34-
) -> float:
32+
def calculate_bound(node: Node, capacity: int, items: List[Item]) -> float:
3533
"""
3634
Calculate the upper bound of profit for a node using
3735
the fractional knapsack approach.
@@ -50,9 +48,7 @@ def calculate_bound(
5048

5149
if index < len(items):
5250
profit_bound += (
53-
(capacity - total_weight)
54-
* items[index].value
55-
/ items[index].weight
51+
(capacity - total_weight) * items[index].value / items[index].weight
5652
)
5753

5854
return profit_bound
@@ -101,9 +97,7 @@ def knapsack_branch_and_bound(
10197

10298
include_node.bound = calculate_bound(include_node, capacity, items)
10399
if include_node.bound > max_profit:
104-
heapq.heappush(
105-
priority_queue, (-include_node.bound, include_node)
106-
)
100+
heapq.heappush(priority_queue, (-include_node.bound, include_node))
107101

108102
# Exclude next item
109103
exclude_node = Node(
@@ -115,8 +109,6 @@ def knapsack_branch_and_bound(
115109

116110
exclude_node.bound = calculate_bound(exclude_node, capacity, items)
117111
if exclude_node.bound > max_profit:
118-
heapq.heappush(
119-
priority_queue, (-exclude_node.bound, exclude_node)
120-
)
112+
heapq.heappush(priority_queue, (-exclude_node.bound, exclude_node))
121113

122114
return max_profit

0 commit comments

Comments
 (0)