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
CSCE 310
Dynamic Programming
Commits
4ed19b3f
Commit
4ed19b3f
authored
1 year ago
by
Brady James Garvin
Browse files
Options
Downloads
Patches
Plain Diff
Recorded work from Wednesday.
parent
820423ad
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
+5
-5
5 additions, 5 deletions
dynamic-programming/notes.md
dynamic-programming/src/features/knapsack/solver.js
+23
-2
23 additions, 2 deletions
dynamic-programming/src/features/knapsack/solver.js
with
28 additions
and
7 deletions
dynamic-programming/notes.md
+
5
−
5
View file @
4ed19b3f
...
...
@@ -111,15 +111,15 @@ Problem: Wrap a paragraph to a line length, minimizing the sum of the squares of
## DAG
*
Edges (actions):
*
…
*
Place a line on the page
*
Vertices (situations):
*
…
*
The number of words placed so far
*
Edge weights:
*
…
*
The penalty for the line just placed
*
Topological order:
*
…
*
Increasing order: 0 → 1 → … →
`wordCount`
*
Goal:
*
…
*
Find a shortest path from 0 to
`wordCount`
## Backpointer Class
...
...
This diff is collapsed.
Click to expand it.
dynamic-programming/src/features/knapsack/solver.js
+
23
−
2
View file @
4ed19b3f
import
{
Item
}
from
'
./items.js
'
;
const
KILOGRAM_OF_NOTHING
=
new
Item
(
1
,
0
);
class
Backpointer
{
constructor
(
item
,
totalValue
)
{
this
.
item
=
item
;
...
...
@@ -9,7 +11,23 @@ class Backpointer {
function
chooseBackpointer
(
items
,
backpointers
)
{
const
currentWeight
=
backpointers
.
length
;
// TODO: stub
let
best
=
new
Backpointer
(
KILOGRAM_OF_NOTHING
,
backpointers
[
currentWeight
-
1
].
totalValue
,
);
for
(
const
item
of
items
)
{
const
previousWeight
=
currentWeight
-
item
.
weight
;
if
(
previousWeight
>=
0
)
{
const
candidate
=
new
Backpointer
(
item
,
item
.
value
+
backpointers
[
previousWeight
].
totalValue
,
);
if
(
candidate
.
totalValue
>
best
.
totalValue
)
{
best
=
candidate
;
}
}
}
return
best
;
}
export
function
chooseItems
(
items
,
weightLimit
)
{
...
...
@@ -20,7 +38,10 @@ export function chooseItems(items, weightLimit) {
}
const
reversedPath
=
[];
for
(
let
weight
=
weightLimit
;
weight
>
0
;
weight
-=
backpointers
[
weight
].
item
.
weight
)
{
reversedPath
.
push
(
…
);
const
item
=
backpointers
[
weight
].
item
;
if
(
item
!==
KILOGRAM_OF_NOTHING
)
{
reversedPath
.
push
(
item
);
}
}
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