Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Dynamic Programming
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Cole Oie
Dynamic Programming
Commits
820423ad
Commit
820423ad
authored
1 year ago
by
Brady James Garvin
Browse files
Options
Downloads
Patches
Plain Diff
Recorded work from Friday.
parent
3ce5542c
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
dynamic-programming/notes.md
+25
-25
25 additions, 25 deletions
dynamic-programming/notes.md
dynamic-programming/src/features/knapsack/solver.js
+12
-3
12 additions, 3 deletions
dynamic-programming/src/features/knapsack/solver.js
with
37 additions
and
28 deletions
dynamic-programming/notes.md
+
25
−
25
View file @
820423ad
...
@@ -50,35 +50,35 @@ Problem: Subject to an integer weight limit, what choice of items (allowing repe
...
@@ -50,35 +50,35 @@ Problem: Subject to an integer weight limit, what choice of items (allowing repe
## DAG
## DAG
*
Edges (actions):
*
Edges (actions):
*
…
or
*
Take one of the items
or
*
…
*
Take one kg of nothing.
*
Vertices (situations):
*
Vertices (situations):
*
…
*
How much weight have we put into the backpack?
*
Edge weights:
*
Edge weights:
*
…
*
The value of the item being taken
*
Topological order:
*
Topological order:
*
…
*
0 → 1 → …
`weightLimit`
*
Goal:
*
Goal:
*
…
*
Compute a longest path from 0 to
`weightLimit`
## Backpointer Class
## Backpointer Class
*
Information for the final result:
*
Information for the final result:
*
…
*
What item did we take?
*
Information to go back:
*
Information to go back:
*
…
*
What was the weight of the item just taken? (redundant with the item taken)
*
Information to compare quality:
*
Information to compare quality:
*
…
*
What is the
*total*
value of all of the items taken so far?
## Choosing a Backpointer
## Choosing a Backpointer
*
Exhaustive search:
*
Exhaustive search:
*
Generate
…
.
*
Generate
items from the list or else 1 kg of nothing, but make sure that we don't try to go back to a negative total weight
.
*
Check that
…
.
*
Check that
the item maximizes total value (the previous total value plus the value of the item used to go back)
.
## Example
## Example
*
Item Z
…
.
*
Item Z
weighs 1 kg and is worth $0
.
*
Item A weighs 3 kg and is worth $10.
*
Item A weighs 3 kg and is worth $10.
*
Item B weighs 4 kg and is worth $14.
*
Item B weighs 4 kg and is worth $14.
...
@@ -87,20 +87,20 @@ Problem: Subject to an integer weight limit, what choice of items (allowing repe
...
@@ -87,20 +87,20 @@ Problem: Subject to an integer weight limit, what choice of items (allowing repe
Weight (kg) Item Total Value (Back to Weight)
Weight (kg) Item Total Value (Back to Weight)
----------- ------ ----------- ----------------
----------- ------ ----------- ----------------
0 [none] $0 ⊥
0 [none] $0 ⊥
1 Item
…
…
…
1 Item
Z
0
0
2 Item
…
…
…
2 Item
Z
0
1
3 Item
…
…
…
3 Item
A
10
0
4 Item
…
…
…
4 Item
B
14
0
5 Item
…
…
…
5 Item
Z
14
4
6 Item
…
…
…
6 Item
A
20
3
7 Item
…
…
…
7 Item
A
24
4
8 Item
…
…
…
8 Item
B
28
4
9 Item
…
…
…
9 Item
A
30
6
10 Item
…
…
…
10 Item
A
34
7
Reversed Path
Reversed Path
----
----
10 ← (Item
…
) ←
…
10 ← (Item
A
) ←
7 ← (Item A) ← 4 ← (Item B) ← 0
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
...
...
This diff is collapsed.
Click to expand it.
dynamic-programming/src/features/knapsack/solver.js
+
12
−
3
View file @
820423ad
import
{
Item
}
from
'
./items.js
'
;
import
{
Item
}
from
'
./items.js
'
;
class
Backpointer
{
class
Backpointer
{
constructor
()
{
constructor
(
item
,
totalValue
)
{
// TODO: stub
this
.
item
=
item
;
this
.
totalValue
=
totalValue
;
}
}
}
}
...
@@ -13,5 +14,13 @@ function chooseBackpointer(items, backpointers) {
...
@@ -13,5 +14,13 @@ function chooseBackpointer(items, backpointers) {
export
function
chooseItems
(
items
,
weightLimit
)
{
export
function
chooseItems
(
items
,
weightLimit
)
{
console
.
assert
(
Number
.
isInteger
(
weightLimit
),
`Tried to choose items with a noninteger weight limit
${
weightLimit
}
.`
);
console
.
assert
(
Number
.
isInteger
(
weightLimit
),
`Tried to choose items with a noninteger weight limit
${
weightLimit
}
.`
);
return
[];
// TODO: stub
const
backpointers
=
[
new
Backpointer
(
undefined
,
0
)];
while
(
backpointers
.
length
<=
weightLimit
)
{
backpointers
.
push
(
chooseBackpointer
(
items
,
backpointers
));
}
const
reversedPath
=
[];
for
(
let
weight
=
weightLimit
;
weight
>
0
;
weight
-=
backpointers
[
weight
].
item
.
weight
)
{
reversedPath
.
push
(
…
);
}
return
reversedPath
.
reverse
();
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment