60 std::cout << std::left
61 << std::setw(4) <<
"#"
62 << std::setw(20) <<
"Item"
63 << std::setw(10) <<
"Weight"
64 << std::setw(10) <<
"Value" <<
"\n";
65 for (
size_t i = 0; i < items.
size(); ++i)
66 std::cout << std::left
68 << std::setw(20) <<
names[i]
69 << std::setw(10) << items[i].weight
70 << std::setw(10) << items[i].value <<
"\n";
89 for (
size_t i = 0; i < items.
size(); ++i)
94 for (
size_t i = 0; i < selected.
size(); ++i)
97 total_w += items[selected[i]].weight;
98 total_v += items[selected[i]].value;
101 std::cout <<
"Chosen items:\n";
102 for (
size_t i = 0; i < items.
size(); ++i)
104 std::cout <<
" " << freq[i] <<
" x " <<
names[i]
105 <<
" (w=" << items[i].weight
106 <<
", v=" << items[i].value <<
")\n";
109 std::cout <<
" [empty]\n";
111 std::cout <<
"Total weight: " <<
total_w <<
"\n";
112 std::cout <<
"Total value : " <<
total_v <<
"\n";
128 std::cout <<
"\n=== Knapsack Toolkit ===\n\n";
132 std::cout <<
"Scenario A: Expedition backpack (0/1)\n";
135 Array<Item> items = {{4, 6}, {3, 5}, {2, 3}, {5, 7}, {1, 2}};
137 "Water flask",
"First aid kit"};
138 const int capacity = 10;
141 std::cout <<
"Capacity: " << capacity <<
"\n";
144 std::cout <<
"Optimal value: " <<
r.optimal_value <<
"\n";
152 std::cout <<
"Scenario B: Value-only query (space optimized)\n";
154 Array<Item> items = {{4, 6}, {3, 5}, {2, 3}, {5, 7}, {1, 2}};
155 std::cout <<
"Capacity: 10\n";
163 std::cout <<
"Scenario C: Infinite stock (unbounded)\n";
167 const int capacity = 7;
170 std::cout <<
"Capacity: " << capacity <<
"\n";
173 std::cout <<
"Optimal value: " <<
r.optimal_value <<
"\n";
181 std::cout <<
"Scenario D: Warehouse with limited stock (bounded)\n";
186 const int capacity = 10;
189 std::cout <<
"Stock limits:\n";
190 for (
size_t i = 0; i < items.
size(); ++i)
191 std::cout <<
" " <<
names[i] <<
" -> " <<
counts[i] <<
"\n";
192 std::cout <<
"Capacity: " << capacity <<
"\n";
195 std::cout <<
"Optimal value: " <<
r.optimal_value <<
"\n";
200 std::cout <<
"\nDone.\n";
Classical knapsack problem variants (0/1, unbounded, bounded).
Simple dynamic array with automatic resizing and functional operations.
static Array create(size_t n)
Create an array with n logical elements.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
int main()
Runs a console demonstration of multiple knapsack problem variants.
Main namespace for Aleph-w library functions.
V knapsack_01_value(const Array< Knapsack_Item< W, V > > &items, W capacity)
0/1 Knapsack — value only (space-optimized).
Knapsack_Result< V > knapsack_01(const Array< Knapsack_Item< W, V > > &items, W capacity)
0/1 Knapsack with item reconstruction.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
void print_rule()
Prints a horizontal rule for example output separation.
Knapsack_Result< V > knapsack_bounded(const Array< Knapsack_Item< W, V > > &items, const Array< size_t > &counts, W capacity)
Bounded Knapsack with reconstruction.
Knapsack_Result< V > knapsack_unbounded(const Array< Knapsack_Item< W, V > > &items, W capacity)
Unbounded Knapsack with reconstruction.
An item for knapsack problems.