├── bun.lockb ├── README.md ├── package.json ├── src ├── utils │ └── index.js └── index.js ├── tsconfig.json └── .gitignore /bun.lockb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aliezzahn/knapsack-problem/HEAD/bun.lockb -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # knapsack-problem 2 | 3 | To install dependencies: 4 | 5 | ```bash 6 | bun install 7 | ``` 8 | 9 | To run: 10 | 11 | ```bash 12 | bun run index.ts 13 | ``` 14 | 15 | This project was created using `bun init` in bun v1.0.0. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "knapsack-problem", 3 | "module": "index.js", 4 | "type": "module", 5 | "devDependencies": { 6 | "bun-types": "latest" 7 | }, 8 | "peerDependencies": { 9 | "typescript": "^5.0.0" 10 | }, 11 | "scripts": { 12 | "start": "bun run src/index.js" 13 | }, 14 | "dependencies": { 15 | "chalk": "^5.3.0" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/utils/index.js: -------------------------------------------------------------------------------- 1 | export const createRangeArray = (start, stop, step) => 2 | Array.from( 3 | { length: (stop - start) / step + 1 }, 4 | (value, index) => start + index * step 5 | ); 6 | 7 | export const createEmptyMatrix = ( 8 | rowNumber = 1, 9 | columnNumber = 1, 10 | fillValue = 0 11 | ) => { 12 | return Array(rowNumber) 13 | .fill() 14 | .map(() => Array(columnNumber).fill(fillValue)); 15 | }; 16 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "lib": ["ESNext"], 4 | "module": "esnext", 5 | "target": "esnext", 6 | "moduleResolution": "bundler", 7 | "moduleDetection": "force", 8 | "allowImportingTsExtensions": true, 9 | "noEmit": true, 10 | "composite": true, 11 | "strict": true, 12 | "downlevelIteration": true, 13 | "skipLibCheck": true, 14 | "jsx": "preserve", 15 | "allowSyntheticDefaultImports": true, 16 | "forceConsistentCasingInFileNames": true, 17 | "allowJs": true, 18 | "types": [ 19 | "bun-types" // add Bun global 20 | ] 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | // 1 - Microwave - weight: 8, value: 50 2 | // 2 - Drone - weight: 2, value: 150 3 | // 3 - Monitor - weight: 6, value: 210 4 | // 4 - Kettle - weight: 1, value: 30 5 | // Total capacity of container can hold 10kgs 6 | 7 | import chalk from "chalk"; 8 | import { createEmptyMatrix, createRangeArray } from "./utils"; 9 | 10 | const log = console.log; 11 | 12 | function getMaxValueInContainer( 13 | itemsNumber, 14 | maxCapacity, 15 | weights, 16 | values, 17 | data 18 | ) { 19 | for (let itemNumber = 0; itemNumber <= ITEMS_NUMBER; itemNumber++) { 20 | for (let capacity = 0; capacity <= MAX_CAPACITY; capacity++) { 21 | if (itemNumber === 0 || capacity === 0) { 22 | data[itemNumber][capacity] = 0; 23 | } else if (WEIGHTS[itemNumber] <= capacity) { 24 | data[itemNumber][capacity] = Math.max( 25 | VALUES[itemNumber] + 26 | data[itemNumber - 1][capacity - WEIGHTS[itemNumber]], 27 | data[itemNumber - 1][capacity] 28 | ); 29 | } else { 30 | data[itemNumber][capacity] = data[itemNumber - 1][capacity]; 31 | } 32 | } 33 | } 34 | return data[itemsNumber][maxCapacity]; 35 | } 36 | 37 | function outputItemInclusionStatus( 38 | itemsNumber, 39 | maxCapacity, 40 | weights, 41 | values, 42 | data, 43 | itemsName 44 | ) { 45 | let i = itemsNumber; 46 | let j = maxCapacity; 47 | while (i > 0 && j > 0) { 48 | if (data[i][j] === data[i - 1][j]) { 49 | log( 50 | chalk.white.bgRed.bold( 51 | `EXCLUDED: Item Name: ${itemsName[i]}, Item Weight: ${weights[i]}, Item Value: ${values[i]}` 52 | ) 53 | ); 54 | } else { 55 | log( 56 | chalk.white.bgGreen.bold( 57 | `INCLUDED: Item Name: ${itemsName[i]}, Item Weight: ${weights[i]}, Item Value: ${values[i]}` 58 | ) 59 | ); 60 | j = j - weights[i]; 61 | } 62 | i--; 63 | } 64 | } 65 | 66 | const ITEMS_NAME = ["", "Microwave", "Drone", "Monitor", "Kettle"]; 67 | const WEIGHTS = [0, 8, 2, 6, 1]; 68 | const VALUES = [0, 50, 150, 210, 30]; 69 | const ITEMS_NUMBER = 4; 70 | const MAX_CAPACITY = 10; 71 | let DATA = createEmptyMatrix(ITEMS_NUMBER + 1, MAX_CAPACITY + 1, 0); 72 | 73 | // console.log(ITEMS_NAME); 74 | 75 | const result = getMaxValueInContainer( 76 | ITEMS_NUMBER, 77 | MAX_CAPACITY, 78 | WEIGHTS, 79 | VALUES, 80 | DATA 81 | ); 82 | 83 | outputItemInclusionStatus( 84 | ITEMS_NUMBER, 85 | MAX_CAPACITY, 86 | WEIGHTS, 87 | VALUES, 88 | DATA, 89 | ITEMS_NAME 90 | ); 91 | 92 | // console.log(`Max value of items included in container: ${result}`); 93 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore 2 | 3 | # Logs 4 | 5 | logs 6 | _.log 7 | npm-debug.log_ 8 | yarn-debug.log* 9 | yarn-error.log* 10 | lerna-debug.log* 11 | .pnpm-debug.log* 12 | 13 | # Diagnostic reports (https://nodejs.org/api/report.html) 14 | 15 | report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json 16 | 17 | # Runtime data 18 | 19 | pids 20 | _.pid 21 | _.seed 22 | \*.pid.lock 23 | 24 | # Directory for instrumented libs generated by jscoverage/JSCover 25 | 26 | lib-cov 27 | 28 | # Coverage directory used by tools like istanbul 29 | 30 | coverage 31 | \*.lcov 32 | 33 | # nyc test coverage 34 | 35 | .nyc_output 36 | 37 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 38 | 39 | .grunt 40 | 41 | # Bower dependency directory (https://bower.io/) 42 | 43 | bower_components 44 | 45 | # node-waf configuration 46 | 47 | .lock-wscript 48 | 49 | # Compiled binary addons (https://nodejs.org/api/addons.html) 50 | 51 | build/Release 52 | 53 | # Dependency directories 54 | 55 | node_modules/ 56 | jspm_packages/ 57 | 58 | # Snowpack dependency directory (https://snowpack.dev/) 59 | 60 | web_modules/ 61 | 62 | # TypeScript cache 63 | 64 | \*.tsbuildinfo 65 | 66 | # Optional npm cache directory 67 | 68 | .npm 69 | 70 | # Optional eslint cache 71 | 72 | .eslintcache 73 | 74 | # Optional stylelint cache 75 | 76 | .stylelintcache 77 | 78 | # Microbundle cache 79 | 80 | .rpt2_cache/ 81 | .rts2_cache_cjs/ 82 | .rts2_cache_es/ 83 | .rts2_cache_umd/ 84 | 85 | # Optional REPL history 86 | 87 | .node_repl_history 88 | 89 | # Output of 'npm pack' 90 | 91 | \*.tgz 92 | 93 | # Yarn Integrity file 94 | 95 | .yarn-integrity 96 | 97 | # dotenv environment variable files 98 | 99 | .env 100 | .env.development.local 101 | .env.test.local 102 | .env.production.local 103 | .env.local 104 | 105 | # parcel-bundler cache (https://parceljs.org/) 106 | 107 | .cache 108 | .parcel-cache 109 | 110 | # Next.js build output 111 | 112 | .next 113 | out 114 | 115 | # Nuxt.js build / generate output 116 | 117 | .nuxt 118 | dist 119 | 120 | # Gatsby files 121 | 122 | .cache/ 123 | 124 | # Comment in the public line in if your project uses Gatsby and not Next.js 125 | 126 | # https://nextjs.org/blog/next-9-1#public-directory-support 127 | 128 | # public 129 | 130 | # vuepress build output 131 | 132 | .vuepress/dist 133 | 134 | # vuepress v2.x temp and cache directory 135 | 136 | .temp 137 | .cache 138 | 139 | # Docusaurus cache and generated files 140 | 141 | .docusaurus 142 | 143 | # Serverless directories 144 | 145 | .serverless/ 146 | 147 | # FuseBox cache 148 | 149 | .fusebox/ 150 | 151 | # DynamoDB Local files 152 | 153 | .dynamodb/ 154 | 155 | # TernJS port file 156 | 157 | .tern-port 158 | 159 | # Stores VSCode versions used for testing VSCode extensions 160 | 161 | .vscode-test 162 | 163 | # yarn v2 164 | 165 | .yarn/cache 166 | .yarn/unplugged 167 | .yarn/build-state.yml 168 | .yarn/install-state.gz 169 | .pnp.\* 170 | --------------------------------------------------------------------------------