Skip to content
Snippets Groups Projects
Commit 8f5c5a54 authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Designed an exhaustive search for the third example problem.

parent c92b4f35
Branches
Tags
No related merge requests found
......@@ -113,22 +113,24 @@ set of items not exceeding the weight limit.
Signature: knapsack(I: set<item>, w: set<item> → number, W: number, v: set<item> → number) → K: set<item>
Happy path:
Precondition:
Postcondition:
Postcondition:
Precondition: |options(I, w, W)| > 0
Postcondition: K ∈ options(I, w, W)
Postcondition: ∀X∈options(I, w, W). v(K) ≥ v(X)
Sad path:
Precondition:
Postcondition:
Precondition: |options(I, w, W)| = 0
Postcondition: K = ⊥
### JavaScript
```
function knapsack(items, getWeight, weightLimit, getValue) {
// …
let result = undefined;
for (const option of options(items, getWeight, weightLimit)) {
// …
if (result === undefined || getValue(option) >= getValue(result)) {
result = option;
}
}
// …
return result;
}
```
......@@ -144,13 +146,19 @@ of items not exceeding the weight limit.
Signature: options(I: set<item>, w: set<item> → number, W: number) → O: set<set<item>>
Precondition: [none]
Postcondition:
Postcondition:
Postcondition: O ⊆ 𝒫(I)
Postcondition: ∀X∈𝒫(I). X∈O ↔ w(X) ≤ W
#### JavaScript
```
function options(items, getWeight, weightLimit) {
// …
const results = new Set();
for (const candidate of powerset(items)) {
if (getWeight(candidate) <= weightLimit) {
results.add(candidate);
}
}
return results;
}
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment