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

Initial commit.

parents
Branches
No related tags found
No related merge requests found
Showing
with 522 additions and 0 deletions
# Disable line-ending conversions for this repository.
* -text
# dependencies
/node_modules
# testing
/coverage
# production
/build
# environments
.env.local
.env.development.local
.env.test.local
.env.production.local
# logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# misc
*~
.DS_Store
[submodule "stylelint-config"]
path = stylelint-config
url = git@git.unl.edu:soft-core/soft-260/stylelint-config.git
[submodule "eslint-config"]
path = eslint-config
url = git@git.unl.edu:soft-core/soft-260/eslint-config.git
{
"folders": [
{
"path": "."
}
],
"settings": {
"files.eol": "LF",
"files.exclude": {
"**/node_modules": true
},
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true
}
}
# dependencies
/node_modules
# testing
/coverage
# production
/build
# environments
.env.local
.env.development.local
.env.test.local
.env.production.local
# logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# misc
*~
.DS_Store
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="77.9616pt" height="71.0002pt" viewBox="0 0 77.9616 71.0002" version="1.1">
<defs>
<g>
<symbol overflow="visible" id="glyph0-0">
<path style="stroke:none;" d=""/>
</symbol>
<symbol overflow="visible" id="glyph0-1">
<path style="stroke:none;" d="M 3.71875 -3.765625 C 3.53125 -4.140625 3.25 -4.40625 2.796875 -4.40625 C 1.640625 -4.40625 0.40625 -2.9375 0.40625 -1.484375 C 0.40625 -0.546875 0.953125 0.109375 1.71875 0.109375 C 1.921875 0.109375 2.421875 0.0625 3.015625 -0.640625 C 3.09375 -0.21875 3.453125 0.109375 3.921875 0.109375 C 4.28125 0.109375 4.5 -0.125 4.671875 -0.4375 C 4.828125 -0.796875 4.96875 -1.40625 4.96875 -1.421875 C 4.96875 -1.53125 4.875 -1.53125 4.84375 -1.53125 C 4.75 -1.53125 4.734375 -1.484375 4.703125 -1.34375 C 4.53125 -0.703125 4.359375 -0.109375 3.953125 -0.109375 C 3.671875 -0.109375 3.65625 -0.375 3.65625 -0.5625 C 3.65625 -0.78125 3.671875 -0.875 3.78125 -1.3125 C 3.890625 -1.71875 3.90625 -1.828125 4 -2.203125 L 4.359375 -3.59375 C 4.421875 -3.875 4.421875 -3.890625 4.421875 -3.9375 C 4.421875 -4.109375 4.3125 -4.203125 4.140625 -4.203125 C 3.890625 -4.203125 3.75 -3.984375 3.71875 -3.765625 Z M 3.078125 -1.1875 C 3.015625 -1 3.015625 -0.984375 2.875 -0.8125 C 2.4375 -0.265625 2.03125 -0.109375 1.75 -0.109375 C 1.25 -0.109375 1.109375 -0.65625 1.109375 -1.046875 C 1.109375 -1.546875 1.421875 -2.765625 1.65625 -3.234375 C 1.96875 -3.8125 2.40625 -4.1875 2.8125 -4.1875 C 3.453125 -4.1875 3.59375 -3.375 3.59375 -3.3125 C 3.59375 -3.25 3.578125 -3.1875 3.5625 -3.140625 Z M 3.078125 -1.1875 "/>
</symbol>
<symbol overflow="visible" id="glyph0-2">
<path style="stroke:none;" d="M 2.375 -6.8125 C 2.375 -6.8125 2.375 -6.921875 2.25 -6.921875 C 2.03125 -6.921875 1.296875 -6.84375 1.03125 -6.8125 C 0.953125 -6.8125 0.84375 -6.796875 0.84375 -6.625 C 0.84375 -6.5 0.9375 -6.5 1.09375 -6.5 C 1.5625 -6.5 1.578125 -6.4375 1.578125 -6.328125 C 1.578125 -6.265625 1.5 -5.921875 1.453125 -5.71875 L 0.625 -2.46875 C 0.515625 -1.96875 0.46875 -1.796875 0.46875 -1.453125 C 0.46875 -0.515625 1 0.109375 1.734375 0.109375 C 2.90625 0.109375 4.140625 -1.375 4.140625 -2.8125 C 4.140625 -3.71875 3.609375 -4.40625 2.8125 -4.40625 C 2.359375 -4.40625 1.9375 -4.109375 1.640625 -3.8125 Z M 1.453125 -3.046875 C 1.5 -3.265625 1.5 -3.28125 1.59375 -3.390625 C 2.078125 -4.03125 2.53125 -4.1875 2.796875 -4.1875 C 3.15625 -4.1875 3.421875 -3.890625 3.421875 -3.25 C 3.421875 -2.65625 3.09375 -1.515625 2.90625 -1.140625 C 2.578125 -0.46875 2.125 -0.109375 1.734375 -0.109375 C 1.390625 -0.109375 1.0625 -0.375 1.0625 -1.109375 C 1.0625 -1.3125 1.0625 -1.5 1.21875 -2.125 Z M 1.453125 -3.046875 "/>
</symbol>
<symbol overflow="visible" id="glyph0-3">
<path style="stroke:none;" d="M 3.953125 -3.78125 C 3.78125 -3.78125 3.65625 -3.78125 3.515625 -3.65625 C 3.34375 -3.5 3.328125 -3.328125 3.328125 -3.265625 C 3.328125 -3.015625 3.515625 -2.90625 3.703125 -2.90625 C 3.984375 -2.90625 4.25 -3.15625 4.25 -3.546875 C 4.25 -4.03125 3.78125 -4.40625 3.078125 -4.40625 C 1.734375 -4.40625 0.40625 -2.984375 0.40625 -1.578125 C 0.40625 -0.671875 0.984375 0.109375 2.03125 0.109375 C 3.453125 0.109375 4.28125 -0.953125 4.28125 -1.0625 C 4.28125 -1.125 4.234375 -1.203125 4.171875 -1.203125 C 4.109375 -1.203125 4.09375 -1.171875 4.03125 -1.09375 C 3.25 -0.109375 2.15625 -0.109375 2.046875 -0.109375 C 1.421875 -0.109375 1.140625 -0.59375 1.140625 -1.203125 C 1.140625 -1.609375 1.34375 -2.578125 1.6875 -3.1875 C 2 -3.765625 2.546875 -4.1875 3.09375 -4.1875 C 3.421875 -4.1875 3.8125 -4.0625 3.953125 -3.78125 Z M 3.953125 -3.78125 "/>
</symbol>
<symbol overflow="visible" id="glyph1-0">
<path style="stroke:none;" d=""/>
</symbol>
<symbol overflow="visible" id="glyph1-1">
<path style="stroke:none;" d="M 2.9375 -6.375 C 2.9375 -6.625 2.9375 -6.640625 2.703125 -6.640625 C 2.078125 -6 1.203125 -6 0.890625 -6 L 0.890625 -5.6875 C 1.09375 -5.6875 1.671875 -5.6875 2.1875 -5.953125 L 2.1875 -0.78125 C 2.1875 -0.421875 2.15625 -0.3125 1.265625 -0.3125 L 0.953125 -0.3125 L 0.953125 0 C 1.296875 -0.03125 2.15625 -0.03125 2.5625 -0.03125 C 2.953125 -0.03125 3.828125 -0.03125 4.171875 0 L 4.171875 -0.3125 L 3.859375 -0.3125 C 2.953125 -0.3125 2.9375 -0.421875 2.9375 -0.78125 Z M 2.9375 -6.375 "/>
</symbol>
<symbol overflow="visible" id="glyph1-2">
<path style="stroke:none;" d="M 2.890625 -3.515625 C 3.703125 -3.78125 4.28125 -4.46875 4.28125 -5.265625 C 4.28125 -6.078125 3.40625 -6.640625 2.453125 -6.640625 C 1.453125 -6.640625 0.6875 -6.046875 0.6875 -5.28125 C 0.6875 -4.953125 0.90625 -4.765625 1.203125 -4.765625 C 1.5 -4.765625 1.703125 -4.984375 1.703125 -5.28125 C 1.703125 -5.765625 1.234375 -5.765625 1.09375 -5.765625 C 1.390625 -6.265625 2.046875 -6.390625 2.40625 -6.390625 C 2.828125 -6.390625 3.375 -6.171875 3.375 -5.28125 C 3.375 -5.15625 3.34375 -4.578125 3.09375 -4.140625 C 2.796875 -3.65625 2.453125 -3.625 2.203125 -3.625 C 2.125 -3.609375 1.890625 -3.59375 1.8125 -3.59375 C 1.734375 -3.578125 1.671875 -3.5625 1.671875 -3.46875 C 1.671875 -3.359375 1.734375 -3.359375 1.90625 -3.359375 L 2.34375 -3.359375 C 3.15625 -3.359375 3.53125 -2.6875 3.53125 -1.703125 C 3.53125 -0.34375 2.84375 -0.0625 2.40625 -0.0625 C 1.96875 -0.0625 1.21875 -0.234375 0.875 -0.8125 C 1.21875 -0.765625 1.53125 -0.984375 1.53125 -1.359375 C 1.53125 -1.71875 1.265625 -1.921875 0.984375 -1.921875 C 0.734375 -1.921875 0.421875 -1.78125 0.421875 -1.34375 C 0.421875 -0.4375 1.34375 0.21875 2.4375 0.21875 C 3.65625 0.21875 4.5625 -0.6875 4.5625 -1.703125 C 4.5625 -2.515625 3.921875 -3.296875 2.890625 -3.515625 Z M 2.890625 -3.515625 "/>
</symbol>
</g>
<clipPath id="clip1">
<path d="M 0 0 L 77.960938 0 L 77.960938 71 L 0 71 Z M 0 0 "/>
</clipPath>
</defs>
<g id="surface1">
<g style="fill:rgb(0%,0%,0%);fill-opacity:1;">
<use xlink:href="#glyph0-1" x="62.32857" y="60.145"/>
</g>
<path style="fill:none;stroke-width:1;stroke-linecap:butt;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;" d="M 9.999267 0.0002 C 9.999267 2.652544 8.94458 5.195513 7.06958 7.070513 C 5.19458 8.945513 2.651611 10.0002 -0.0007325 10.0002 C -2.653076 10.0002 -5.196045 8.945513 -7.071045 7.070513 C -8.946045 5.195513 -10.000733 2.652544 -10.000733 0.0002 C -10.000733 -2.652144 -8.946045 -5.195112 -7.071045 -7.070112 C -5.196045 -8.945112 -2.653076 -9.9998 -0.0007325 -9.9998 C 2.651611 -9.9998 5.19458 -8.945112 7.06958 -7.070112 C 8.94458 -5.195112 9.999267 -2.652144 9.999267 0.0002 Z M 9.999267 0.0002 " transform="matrix(1,0,0,-1,64.96167,58.0002)"/>
<g style="fill:rgb(0%,0%,0%);fill-opacity:1;">
<use xlink:href="#glyph0-2" x="36.84307" y="16.4592"/>
</g>
<path style="fill:none;stroke-width:1;stroke-linecap:butt;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;" d="M -15.981201 45.0002 C -15.981201 47.652544 -17.035889 50.195513 -18.910889 52.070513 C -20.785889 53.945513 -23.328858 55.0002 -25.981201 55.0002 C -28.633545 55.0002 -31.176514 53.945513 -33.051514 52.070513 C -34.926514 50.195513 -35.981201 47.652544 -35.981201 45.0002 C -35.981201 42.347856 -34.926514 39.804888 -33.051514 37.929888 C -31.176514 36.054888 -28.633545 35.0002 -25.981201 35.0002 C -23.328858 35.0002 -20.785889 36.054888 -18.910889 37.929888 C -17.035889 39.804888 -15.981201 42.347856 -15.981201 45.0002 Z M -15.981201 45.0002 " transform="matrix(1,0,0,-1,64.96167,58.0002)"/>
<g style="fill:rgb(0%,0%,0%);fill-opacity:1;">
<use xlink:href="#glyph0-3" x="10.84427" y="60.145"/>
</g>
<path style="fill:none;stroke-width:1;stroke-linecap:butt;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;" d="M -41.96167 0.0002 C -41.96167 2.652544 -43.016358 5.195513 -44.891358 7.070513 C -46.766358 8.945513 -49.309326 10.0002 -51.96167 10.0002 C -54.614014 10.0002 -57.156983 8.945513 -59.031983 7.070513 C -60.906983 5.195513 -61.96167 2.652544 -61.96167 0.0002 C -61.96167 -2.652144 -60.906983 -5.195112 -59.031983 -7.070112 C -57.156983 -8.945112 -54.614014 -9.9998 -51.96167 -9.9998 C -49.309326 -9.9998 -46.766358 -8.945112 -44.891358 -7.070112 C -43.016358 -5.195112 -41.96167 -2.652144 -41.96167 0.0002 Z M -41.96167 0.0002 " transform="matrix(1,0,0,-1,64.96167,58.0002)"/>
<path style="fill:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;" d="M -5.000733 8.660356 L -20.981201 36.340044 " transform="matrix(1,0,0,-1,64.96167,58.0002)"/>
<path style="fill-rule:nonzero;fill:rgb(0%,0%,0%);fill-opacity:1;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;" d="M -17.80542 33.90645 L -20.981201 36.340044 L -20.457764 32.3752 Z M -17.80542 33.90645 " transform="matrix(1,0,0,-1,64.96167,58.0002)"/>
<g style="fill:rgb(0%,0%,0%);fill-opacity:1;">
<use xlink:href="#glyph1-1" x="54.07127" y="33.4001"/>
</g>
<path style="fill:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;" d="M -10.000733 0.0002 L -41.96167 0.0002 " transform="matrix(1,0,0,-1,64.96167,58.0002)"/>
<path style="fill-rule:nonzero;fill:rgb(0%,0%,0%);fill-opacity:1;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;" d="M -38.266358 1.53145 L -41.96167 0.0002 L -38.266358 -1.53105 Z M -38.266358 1.53145 " transform="matrix(1,0,0,-1,64.96167,58.0002)"/>
<g style="fill:rgb(0%,0%,0%);fill-opacity:1;">
<use xlink:href="#glyph1-2" x="36.49017" y="67.4206"/>
</g>
<path style="fill:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;" d="M -30.981201 36.340044 L -46.96167 8.660356 " transform="matrix(1,0,0,-1,64.96167,58.0002)"/>
<path style="fill-rule:nonzero;fill:rgb(0%,0%,0%);fill-opacity:1;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;" d="M -46.438233 12.6252 L -46.96167 8.660356 L -43.789795 11.09395 Z M -46.438233 12.6252 " transform="matrix(1,0,0,-1,64.96167,58.0002)"/>
<g style="fill:rgb(0%,0%,0%);fill-opacity:1;">
<use xlink:href="#glyph1-1" x="18.90917" y="33.4001"/>
</g>
<g clip-path="url(#clip1)" clip-rule="nonzero">
<path style="fill:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;" d="M -64.46167 -12.4998 L 12.499267 -12.4998 L 12.499267 57.5002 L -64.46167 57.5002 Z M -64.46167 -12.4998 " transform="matrix(1,0,0,-1,64.96167,58.0002)"/>
</g>
</g>
</svg>
Please clone the starter code linked from Canvas (with `git clone --recursive` so that you also clone the submodules), install its dependencies with `npm install`, and use "File" → "Open Workspace…" to open your clone in VSCode. Please do not run the app yet—it contains spoilers.
--------------------------------------------------------------------------------
# Graph Search vs. Dynamic Programming
* Idea behind *graph search* design:
* Frame the problem as a problem on a graph
* Explore vertices and edges using a worklist algorithm
* Compute a subproblem result for each vertex
* Postprocess to extract a final result
* Idea behind *dynamic programming* design:
* Frame the problem as a problem on a directed acyclic graph (DAG)
* Explore vertices in topological order
* Compute a subproblem result for each vertex
* Postprocess to extract a final result
--------------------------------------------------------------------------------
# Problem 1:
As long as you take no more than 10 kg, you may choose as many as you want of each of these items:
* Item A weighs 3 kg and is worth $10.
* Item B weighs 4 kg and is worth $14.
What choice of items gives you the maximum total value?
Example: The selection ⟅B, B⟆ weights 8 kg and has value $28. But there is a more valuable choice of items.
# Problem 2:
You are placing linebreaks in the following paragraph (where words are represented by 'x's):
> x x x x xxxx xxxx
so that every line is at most seven characters long, including spaces. The penalty for not filling a line is the number of unused spaces on that line squared. What wrapping gives the minimum possible total penalty?
Example: The wrapping
> x x x x
> xxxx
> xxxx
has a total penalty of `(7 - 7)² + (7 - 4)² + (7 - 4)² = 0 + 9 + 9 = 18`. But there is a wrapping with a lower total penalty.
--------------------------------------------------------------------------------
# Dynamic Programming for the Unbounded Knapsack Problem
Problem: Subject to an integer weight limit, what choice of items (allowing repeats) from a collection of items with positive integer weights gives the maximum total value?
## DAG
* Edges (actions):
* … or
*
* Vertices (situations):
*
* Edge weights:
* [not needed]
* Topological order:
*
* Goal:
*
## Backpointer Class
* Information for the final result:
*
* Information to go back:
*
* Information to compare quality:
*
## Choosing a Backpointer
* Exhaustive search:
* Generate ….
* Check that ….
## Example
* Item Z ….
* Item A weighs 3 kg and is worth $10.
* Item B weighs 4 kg and is worth $14.
Backpointer
-------------------
Weight (kg) Item Total Value (Back to Weight)
----------- ------ ----------- ----------------
0 [none] $0 ⊥
1 Item … … …
2 Item … … …
3 Item … … …
4 Item … … …
5 Item … … …
6 Item … … …
7 Item … … …
8 Item … … …
9 Item … … …
10 Item … … …
Reversed Path
----
10 ← (Item …) ← …
--------------------------------------------------------------------------------
# Dynamic Programming for the Line-Wrapping Problem
Problem: Wrap a paragraph to a line length, minimizing the sum of the squares of the lengths of unused space on each line.
## DAG
* Edges (actions):
*
* Vertices (situations):
*
* Edge weights:
*
* Topological order:
*
* Goal:
*
## Backpointer Class
* Information for the final result:
*
* Information to go back:
*
* Information to compare quality:
*
## Choosing a Backpointer
* Exhaustive search:
* Generate ….
* Check that ….
## Example
> x x x x xxxx xxxx
> 0 1 2 3 4 5 6
Backpointer
-----------------------------
Index Previous Index Total Penalty
----- -------------- -------------
0 ⊥ 0
1 … …
2 … …
3 … …
4 … …
5 … …
6 … …
Reversed Path
----
6 ← …
This diff is collapsed.
{
"name": "@unlsoft/dynamic-programming",
"version": "1.0.0",
"description": "Starter code for the class on dynamic programming.",
"private": true,
"license": "UNLICENSED",
"scripts": {
"lint:css": "stylelint \"**/*.css\" \"**/*.module.css\" \"!coverage/**\"",
"lint:js": "eslint --max-warnings 0 ./src",
"lint": "run-s --continue-on-error lint:**",
"test-once": "react-scripts test --watchAll=false --coverage",
"test": "react-scripts test --watchAll --coverage",
"start": "react-scripts start",
"build": "react-scripts build",
"eject": "react-scripts eject"
},
"homepage": ".",
"dependencies": {
"@reduxjs/toolkit": "^1.6.0",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^12.0.0",
"@testing-library/user-event": "^13.1.9",
"classnames": "^2.3.1",
"npm-run-all": "^4.1.5",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-redux": "^7.2.4",
"react-router-dom": "^5.2.0",
"react-scripts": "^4.0.3",
"workbox-background-sync": "^5.1.3",
"workbox-broadcast-update": "^5.1.3",
"workbox-cacheable-response": "^5.1.3",
"workbox-core": "^5.1.3",
"workbox-expiration": "^5.1.3",
"workbox-google-analytics": "^5.1.3",
"workbox-navigation-preload": "^5.1.3",
"workbox-precaching": "^5.1.3",
"workbox-range-requests": "^5.1.3",
"workbox-routing": "^5.1.3",
"workbox-strategies": "^5.1.3",
"workbox-streams": "^5.1.3"
},
"devDependencies": {
"@unlsoft/eslint-config": "file:../eslint-config",
"@unlsoft/stylelint-config": "file:../stylelint-config",
"eslint-plugin-jest-dom": "^3.9.0",
"stylelint": "^13.13.1"
},
"stylelint": {
"extends": "@unlsoft/stylelint-config"
},
"eslintConfig": {
"extends": [
"react-app",
"@unlsoft/eslint-config/react"
]
},
"jest": {
"clearMocks": true,
"collectCoverageFrom": [
"src/features/**/*.js"
],
"resetMocks": false,
"restoreMocks": false
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<meta
name="description"
content="Starter code for the class on dynamic programming."
/>
<meta name="theme-color" content="rgba(208 0 0 / 100%)" />
<link rel="icon" href="%PUBLIC_URL%/logo.svg" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo.svg" />
<title>Dynamic Programming in Class</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 152 152">
<rect x="0" y="0" width="152" height="152" fill="rgba(0 0 0 / 100%)" />
<path d="M147,1H90V42h10V75.673L53.532,2.393,52.648,1H2V42H12v66H2v41H62V108H52V74.336l46.467,73.271L99.351,149H150V108H140V42h10V1Z" stroke-width="3" stroke="rgba(255 255 255 / 100%)" fill="rgba(208 0 0 / 100%)">
</path>
</svg>
{
"short_name": "Dynamic Programming",
"name": "Dynamic Programming in Class",
"description": "Starter code for the class on dynamic programming.",
"icons": [
{
"src": "logo.svg",
"type": "image/svg+xml",
"sizes": "192x192 512x512",
"purpose": "any maskable"
}
],
"start_url": ".",
"display": "standalone",
"orientation": "portrait",
"theme_color": "rgba(208 0 0 / 100%)",
"background_color": "rgba(255 255 255 / 100%)"
}
import { Route } from 'react-router-dom';
import { Solution as KnapsackSolution } from './features/knapsack/solution.js';
import { Solution as WrappingSolution } from './features/wrapping/solution.js';
export function App() {
return (
<>
<Route exact path={'/'}>
<h1>Knapsack Problem</h1>
<KnapsackSolution />
<h1>Wrapping Problem</h1>
<WrappingSolution />
</Route>
</>
);
}
This diff is collapsed.
export class Item {
constructor(weight, value) {
console.assert(weight > 0, `Tried to create an item with a nonpositive weight ${weight}.`);
console.assert(Number.isInteger(weight), `Tried to create an item with a noninteger weight ${weight}.`);
this.weight = weight;
this.value = value;
}
}
/* eslint-disable no-magic-numbers */
import { Item } from './items.js';
import { chooseItems } from './solver.js';
import knapsackImage from './images/knapsack.svg';
import styles from './solution.module.css';
function formatSolution(solution) {
return solution.map((item) => `(weight: ${item.weight}, value: ${item.value})`).join(', ');
}
const ITEMS = [
new Item(3, 10),
new Item(4, 14),
];
const WEIGHT_LIMIT = 10;
export function Solution() {
const solution = chooseItems(ITEMS, WEIGHT_LIMIT);
return (
<div>
<figure className={styles.diagram} >
<img src={knapsackImage} alt="A weighted directed graph corresponding to the knapsack problem." />
</figure>
<label>
Chosen Knapsack Items: <output>{formatSolution(solution)}</output>
</label>
</div>
);
}
.diagram {
display: block;
box-sizing: border-box;
margin: auto;
width: 100%;
overflow-x: scroll;
}
import { Item } from './items.js';
class Backpointer {
constructor() {
// TODO: stub
}
}
function chooseBackpointer(items, backpointers) {
const currentWeight = backpointers.length;
// TODO: stub
}
export function chooseItems(items, weightLimit) {
console.assert(Number.isInteger(weightLimit), `Tried to choose items with a noninteger weight limit ${weightLimit}.`);
return []; // TODO: stub
}
This diff is collapsed.
/* eslint-disable no-magic-numbers */
import { wrap } from './solver.js';
import wrappingImage from './images/wrapping.svg';
import styles from './solution.module.css';
function formatSolution(solution) {
return solution.map((count) => `after ${count} word${count !== 1 ? 's' : ''}`).join(', ');
}
const WORD_LENGTHS = [1, 1, 1, 1, 4, 4];
const LINE_LENGTH = 7;
export function Solution() {
const solution = wrap(WORD_LENGTHS, LINE_LENGTH);
return (
<div>
<figure className={styles.diagram} >
<img src={wrappingImage} alt="A weighted directed graph corresponding to the line-wrapping problem." />
</figure>
<label>
Line Break Locations: <output>{formatSolution(solution)}</output>
</label>
</div>
);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment