Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
knapsack_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
37# include <iostream>
38# include <iomanip>
39
40# include <Knapsack.H>
41# include <print_rule.H>
42
43using namespace Aleph;
44
46
47namespace
48{
58 void print_item_table(const Array<Item> & items, const Array<const char *> & names)
59 {
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
67 << std::setw(4) << i
68 << std::setw(20) << names[i]
69 << std::setw(10) << items[i].weight
70 << std::setw(10) << items[i].value << "\n";
71 }
72
84 void print_selection_summary(const Array<Item> & items,
86 const Array<size_t> & selected)
87 {
89 for (size_t i = 0; i < items.size(); ++i)
90 freq(i) = 0;
91
92 int total_w = 0;
93 int total_v = 0;
94 for (size_t i = 0; i < selected.size(); ++i)
95 {
96 ++freq(selected[i]);
97 total_w += items[selected[i]].weight;
98 total_v += items[selected[i]].value;
99 }
100
101 std::cout << "Chosen items:\n";
102 for (size_t i = 0; i < items.size(); ++i)
103 if (freq[i] > 0)
104 std::cout << " " << freq[i] << " x " << names[i]
105 << " (w=" << items[i].weight
106 << ", v=" << items[i].value << ")\n";
107
108 if (selected.is_empty())
109 std::cout << " [empty]\n";
110
111 std::cout << "Total weight: " << total_w << "\n";
112 std::cout << "Total value : " << total_v << "\n";
113 }
114}
115
126int main()
127{
128 std::cout << "\n=== Knapsack Toolkit ===\n\n";
129
130 // Example 1: 0/1 knapsack
131 {
132 std::cout << "Scenario A: Expedition backpack (0/1)\n";
133 print_rule();
134
135 Array<Item> items = {{4, 6}, {3, 5}, {2, 3}, {5, 7}, {1, 2}};
136 Array<const char *> names = {"Tent", "Sleeping bag", "Food",
137 "Water flask", "First aid kit"};
138 const int capacity = 10;
139
140 print_item_table(items, names);
141 std::cout << "Capacity: " << capacity << "\n";
142
143 const auto r = knapsack_01(items, capacity);
144 std::cout << "Optimal value: " << r.optimal_value << "\n";
145 print_selection_summary(items, names, r.selected_items);
146 print_rule();
147 std::cout << "\n";
148 }
149
150 // Example 2: value-only shortcut
151 {
152 std::cout << "Scenario B: Value-only query (space optimized)\n";
153 print_rule();
154 Array<Item> items = {{4, 6}, {3, 5}, {2, 3}, {5, 7}, {1, 2}};
155 std::cout << "Capacity: 10\n";
156 std::cout << "Optimal value only: " << knapsack_01_value(items, 10) << "\n";
157 print_rule();
158 std::cout << "\n";
159 }
160
161 // Example 3: unbounded knapsack
162 {
163 std::cout << "Scenario C: Infinite stock (unbounded)\n";
164 print_rule();
165 Array<Item> items = {{1, 1}, {3, 4}, {4, 5}};
166 Array<const char *> names = {"Bronze coin", "Silver coin", "Gold coin"};
167 const int capacity = 7;
168
169 print_item_table(items, names);
170 std::cout << "Capacity: " << capacity << "\n";
171
172 const auto r = knapsack_unbounded(items, capacity);
173 std::cout << "Optimal value: " << r.optimal_value << "\n";
174 print_selection_summary(items, names, r.selected_items);
175 print_rule();
176 std::cout << "\n";
177 }
178
179 // Example 4: bounded knapsack
180 {
181 std::cout << "Scenario D: Warehouse with limited stock (bounded)\n";
182 print_rule();
183 Array<Item> items = {{2, 3}, {3, 5}, {4, 7}};
184 Array<size_t> counts = {3, 2, 1};
185 Array<const char *> names = {"Widget A", "Widget B", "Widget C"};
186 const int capacity = 10;
187
188 print_item_table(items, names);
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";
193
194 const auto r = knapsack_bounded(items, counts, capacity);
195 std::cout << "Optimal value: " << r.optimal_value << "\n";
196 print_selection_summary(items, names, r.selected_items);
197 print_rule();
198 }
199
200 std::cout << "\nDone.\n";
201 return 0;
202}
Classical knapsack problem variants (0/1, unbounded, bounded).
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
static Array create(size_t n)
Create an array with n logical elements.
Definition tpl_array.H:194
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:351
constexpr bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_array.H:348
int main()
Runs a console demonstration of multiple knapsack problem variants.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
V knapsack_01_value(const Array< Knapsack_Item< W, V > > &items, W capacity)
0/1 Knapsack — value only (space-optimized).
Definition Knapsack.H:199
Knapsack_Result< V > knapsack_01(const Array< Knapsack_Item< W, V > > &items, W capacity)
0/1 Knapsack with item reconstruction.
Definition Knapsack.H:131
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.
Definition print_rule.H:39
Knapsack_Result< V > knapsack_bounded(const Array< Knapsack_Item< W, V > > &items, const Array< size_t > &counts, W capacity)
Bounded Knapsack with reconstruction.
Definition Knapsack.H:317
Knapsack_Result< V > knapsack_unbounded(const Array< Knapsack_Item< W, V > > &items, W capacity)
Unbounded Knapsack with reconstruction.
Definition Knapsack.H:247
An item for knapsack problems.
Definition Knapsack.H:66
gsl_rng * r