Algorithms: Dynamic Programming

Algorithms: Dynamic Programming

Explore algorithms: dynamic programming code snippets and tutorials

Algorithms: Dynamic Programming

0-1 Knapsack in PHP

Knapsack chooses items with maximum total value under a weight limit, using each item at most once.

php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
declare(strict_types=1);
// 0-1 Knapsack in PHP
//
// PURPOSE
// Knapsack chooses items with maximum total value under a weight limit, using each item at
// most once. The one-dimensional table stores the best value for each capacity.
//
// METHOD
// Process items one at a time and capacities downward. Compare skipping the item with adding
// its value to the previous best at the remaining capacity. Descending capacities prevent
// reusing the current item.
//
// COMPLEXITY
// O(n * capacity) time and O(capacity) auxiliary space; pseudo-polynomial.
//
// CONTRACT
// Weights are positive, values nonnegative, capacity >= 0. Both arrays have n entries.
// Returns best value, not the chosen item set.
// Public APIs infer array lengths except in C, which accepts pointer/length pairs. Vertex
// counts and matrix dimensions remain explicit. Arrays must contain signed 32-bit integers.
// Sorted order, correct tree topology, and caller-owned data-structure invariants are
// preconditions; checking them on every query would change query complexity.
// Scalar and shape checks are included. Value-range checks are included where needed to
// prevent invalid indexing or nontermination. Allocation sizes are capped at 4,000,000
// workspace entries. Arithmetic outside Python raises an error on 32-bit overflow; Python
// uses arbitrary precision. Native allocation failures propagate. In-place outputs may be
// partly updated after arithmetic failure. These are single-threaded, in-memory
// implementations.
//
// LANGUAGE DETAILS
// PHP 8.1+ on a 64-bit runtime: packed arrays, strict scalar types, read-only inputs by
// value using copy-on-write, references only for outputs, and checked 32-bit arithmetic.
// Invalid input and overflow throw exceptions. PHP arrays have higher overhead than native
// packed buffers in C/Java.
//
// EXAMPLE
// Expected return: 9.
// Run: php example.php
// Tests for this exact source artifact run before publication. The accompanying source
// archive includes unit cases, runtime results, and source hashes.

function alg_require(bool $valid): void { if (!$valid) throw new InvalidArgumentException("invalid algorithm input"); }
function alg_checked(int|float $v): int { if ($v < -2147483648 || $v > 2147483647 || floor($v) != $v) throw new OverflowException("32-bit result overflow"); return (int)$v; }
function alg_add(int $a,int $b): int { return alg_checked($a+$b); }
function alg_sub(int $a,int $b): int { return alg_checked($a-$b); }
function alg_mul(int $a,int $b): int { return alg_checked($a*$b); }
function alg_size(int $n): int { alg_require($n>=0 && $n<=4000000); return $n; }

function knapsack(array $weights, array $values, int $capacity): int {
    alg_require(array_is_list($weights));
    alg_require(array_is_list($values));
    $n = count($weights);
    alg_require((($n >= 0) && ($n <= 1000000)));
    alg_require((count($weights) == $n));
    alg_require((count($values) == $n));
    alg_require((($capacity >= 0) && ($capacity <= 1000000)));
    for ($alg_i = 0; $alg_i < count($weights); $alg_i++) {
        $alg_value = $weights[$alg_i];
        alg_require(($alg_value > 0));
    }
    for ($alg_i = 0; $alg_i < count($values); $alg_i++) {
        $alg_value = $values[$alg_i];
        alg_require(($alg_value >= 0));
    }
    $dp = array_fill(0, alg_size(alg_add($capacity, 1)), 0);
    $i = 0;
    $c = 0;
    while (($i < $n)) {
        $c = $capacity;
        while (($c >= $weights[$i])) {
            $dp[$c] = max($dp[$c], alg_add($dp[alg_sub($c, $weights[$i])], $values[$i]));
            $c = alg_sub($c, 1);
        }
        $i = alg_add($i, 1);
    }
    return $dp[$capacity];
}

$weights = [2, 3, 4];
$values = [4, 5, 7];
$capacity = 5;
$result = knapsack($weights, $values, $capacity);
echo $result, PHP_EOL;
Algorithms: Dynamic Programming

0-1 Knapsack in C#

Knapsack chooses items with maximum total value under a weight limit, using each item at most once.

csharp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
// 0-1 Knapsack in C#
//
// PURPOSE
// Knapsack chooses items with maximum total value under a weight limit, using each item at
// most once. The one-dimensional table stores the best value for each capacity.
//
// METHOD
// Process items one at a time and capacities downward. Compare skipping the item with adding
// its value to the previous best at the remaining capacity. Descending capacities prevent
// reusing the current item.
//
// COMPLEXITY
// O(n * capacity) time and O(capacity) auxiliary space; pseudo-polynomial.
//
// CONTRACT
// Weights are positive, values nonnegative, capacity >= 0. Both arrays have n entries.
// Returns best value, not the chosen item set.
// Public APIs infer array lengths except in C, which accepts pointer/length pairs. Vertex
// counts and matrix dimensions remain explicit. Arrays must contain signed 32-bit integers.
// Sorted order, correct tree topology, and caller-owned data-structure invariants are
// preconditions; checking them on every query would change query complexity.
// Scalar and shape checks are included. Value-range checks are included where needed to
// prevent invalid indexing or nontermination. Allocation sizes are capped at 4,000,000
// workspace entries. Arithmetic outside Python raises an error on 32-bit overflow; Python
// uses arbitrary precision. Native allocation failures propagate. In-place outputs may be
// partly updated after arithmetic failure. These are single-threaded, in-memory
// implementations.
//
// LANGUAGE DETAILS
// C# / .NET 10: ReadOnlySpan<int> inputs, Span<int> outputs, primitive workspace arrays,
// Array.BinarySearch for LIS, and checked integer arithmetic. Invalid arguments and overflow
// throw exceptions.
//
// EXAMPLE
// Expected return: 9.
// Run: Use Program.cs in a net10.0 console project; dotnet run.
// Tests for this exact source artifact run before publication. The accompanying source
// archive includes unit cases, runtime results, and source hashes.

class Program {
    static void alg_require(bool valid) { if (!valid) throw new ArgumentException("invalid algorithm input"); }
    static int alg_size(int n) { alg_require(n>=0 && n<=4000000); return n; }

    static int knapsack(ReadOnlySpan<int> weights, ReadOnlySpan<int> values, int capacity) {
        checked {
            int n = weights.Length;
            alg_require(((n >= 0) && (n <= 1000000)));
            alg_require((weights.Length == n));
            alg_require((values.Length == n));
            alg_require(((capacity >= 0) && (capacity <= 1000000)));
            for (int alg_i = 0; alg_i < weights.Length; alg_i++) {
                int alg_value = weights[alg_i];
                alg_require((alg_value > 0));
            }
            for (int alg_i = 0; alg_i < values.Length; alg_i++) {
                int alg_value = values[alg_i];
                alg_require((alg_value >= 0));
            }
            int[] dp = new int[alg_size((capacity + 1))];
            int i = 0;
            int c = 0;
            while ((i < n)) {
                c = capacity;
                while ((c >= weights[i])) {
                    dp[c] = Math.Max(dp[c], (dp[(c - weights[i])] + values[i]));
                    c = (c - 1);
                }
                i = (i + 1);
            }
            return dp[capacity];
        }
    }

    static void Main() {
        int[] weights = new int[] {2, 3, 4};
        int[] values = new int[] {4, 5, 7};
        int capacity = 5;
        int result = knapsack(weights, values, capacity);
        Console.WriteLine(result);
    }
}
Algorithms: Dynamic Programming

0-1 Knapsack in C++

Knapsack chooses items with maximum total value under a weight limit, using each item at most once.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <stdexcept>
#include <cstdint>
#include <limits>
// 0-1 Knapsack in C++
//
// PURPOSE
// Knapsack chooses items with maximum total value under a weight limit, using each item at
// most once. The one-dimensional table stores the best value for each capacity.
//
// METHOD
// Process items one at a time and capacities downward. Compare skipping the item with adding
// its value to the previous best at the remaining capacity. Descending capacities prevent
// reusing the current item.
//
// COMPLEXITY
// O(n * capacity) time and O(capacity) auxiliary space; pseudo-polynomial.
//
// CONTRACT
// Weights are positive, values nonnegative, capacity >= 0. Both arrays have n entries.
// Returns best value, not the chosen item set.
// Public APIs infer array lengths except in C, which accepts pointer/length pairs. Vertex
// counts and matrix dimensions remain explicit. Arrays must contain signed 32-bit integers.
// Sorted order, correct tree topology, and caller-owned data-structure invariants are
// preconditions; checking them on every query would change query complexity.
// Scalar and shape checks are included. Value-range checks are included where needed to
// prevent invalid indexing or nontermination. Allocation sizes are capped at 4,000,000
// workspace entries. Arithmetic outside Python raises an error on 32-bit overflow; Python
// uses arbitrary precision. Native allocation failures propagate. In-place outputs may be
// partly updated after arithmetic failure. These are single-threaded, in-memory
// implementations.
//
// LANGUAGE DETAILS
// C++17: const vector references for inputs, mutable references for outputs, RAII workspace,
// standard lower_bound for LIS. Invalid inputs throw invalid_argument; overflow throws
// overflow_error; allocation failure propagates bad_alloc.
//
// EXAMPLE
// Expected return: 9.
// Run: g++ -std=c++17 -O2 example.cpp -o example && ./example
// Tests for this exact source artifact run before publication. The accompanying source
// archive includes unit cases, runtime results, and source hashes.

static int alg_min(int a, int b) { return a < b ? a : b; }
static int alg_max(int a, int b) { return a > b ? a : b; }
static void alg_require(bool valid) { if (!valid) throw std::invalid_argument("invalid algorithm input"); }
static int alg_checked(int64_t v) { if (v < INT32_MIN || v > INT32_MAX) throw std::overflow_error("32-bit result overflow"); return (int)v; }
static int alg_add(int a,int b) { return alg_checked((int64_t)a+b); }
static int alg_sub(int a,int b) { return alg_checked((int64_t)a-b); }
static int alg_mul(int a,int b) { return alg_checked((int64_t)a*b); }
static size_t alg_size(int n) { alg_require(n>=0 && n<=4000000); return (size_t)n; }

int knapsack(const std::vector<int>& weights, const std::vector<int>& values, int capacity) {
    alg_require(weights.size() <= 1000000);
    alg_require(values.size() <= 1000000);
    int n = (int)weights.size();
    alg_require(((n >= 0) && (n <= 1000000)));
    alg_require(((int)weights.size() == n));
    alg_require(((int)values.size() == n));
    alg_require(((capacity >= 0) && (capacity <= 1000000)));
    for (int alg_i = 0; alg_i < (int)weights.size(); alg_i++) {
        int alg_value = weights[alg_i];
        alg_require((alg_value > 0));
    }
    for (int alg_i = 0; alg_i < (int)values.size(); alg_i++) {
        int alg_value = values[alg_i];
        alg_require((alg_value >= 0));
    }
    std::vector<int> dp(alg_size(alg_add(capacity, 1)), 0);
    int i = 0;
    int c = 0;
    while ((i < n)) {
        c = capacity;
        while ((c >= weights[i])) {
            dp[c] = alg_max(dp[c], alg_add(dp[alg_sub(c, weights[i])], values[i]));
            c = alg_sub(c, 1);
        }
        i = alg_add(i, 1);
    }
    return dp[capacity];
}

int main(void) {
    std::vector<int> weights = {2, 3, 4};
    std::vector<int> values = {4, 5, 7};
    int capacity = 5;
    int result = knapsack(weights, values, capacity);
    printf("%d\n", result);
    return 0;
}
Algorithms: Dynamic Programming

0-1 Knapsack in C

Knapsack chooses items with maximum total value under a weight limit, using each item at most once.

c
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <errno.h>
// 0-1 Knapsack in C
//
// PURPOSE
// Knapsack chooses items with maximum total value under a weight limit, using each item at
// most once. The one-dimensional table stores the best value for each capacity.
//
// METHOD
// Process items one at a time and capacities downward. Compare skipping the item with adding
// its value to the previous best at the remaining capacity. Descending capacities prevent
// reusing the current item.
//
// COMPLEXITY
// O(n * capacity) time and O(capacity) auxiliary space; pseudo-polynomial.
//
// CONTRACT
// Weights are positive, values nonnegative, capacity >= 0. Both arrays have n entries.
// Returns best value, not the chosen item set.
// Public APIs infer array lengths except in C, which accepts pointer/length pairs. Vertex
// counts and matrix dimensions remain explicit. Arrays must contain signed 32-bit integers.
// Sorted order, correct tree topology, and caller-owned data-structure invariants are
// preconditions; checking them on every query would change query complexity.
// Scalar and shape checks are included. Value-range checks are included where needed to
// prevent invalid indexing or nontermination. Allocation sizes are capped at 4,000,000
// workspace entries. Arithmetic outside Python raises an error on 32-bit overflow; Python
// uses arbitrary precision. Native allocation failures propagate. In-place outputs may be
// partly updated after arithmetic failure. These are single-threaded, in-memory
// implementations.
//
// LANGUAGE DETAILS
// C99: checked calloc/free workspace, const input pointers, explicit size_t lengths. Errors
// return INT_MIN with errno=EINVAL, ERANGE, or ENOMEM. Check errno before using a result.
// Input/output arrays must not alias. No variable-length stack arrays.
//
// EXAMPLE
// Expected return: 9.
// Run: gcc -std=c99 -O2 example.c -o example && ./example
// Tests for this exact source artifact run before publication. The accompanying source
// archive includes unit cases, runtime results, and source hashes.

static int alg_min(int a, int b) { return a < b ? a : b; }
static int alg_max(int a, int b) { return a > b ? a : b; }
static int alg_checked(int64_t v, int *error) {
    if (v < INT_MIN || v > INT_MAX) { *error = 1; return 0; }
    return (int)v;
}
static int alg_add(int a, int b, int *e) { return alg_checked((int64_t)a+b,e); }
static int alg_sub(int a, int b, int *e) { return alg_checked((int64_t)a-b,e); }
static int alg_mul(int a, int b, int *e) { return alg_checked((int64_t)a*b,e); }

int knapsack(const int *weights, size_t weights_length, const int *values, size_t values_length, int capacity) {
    int alg_error = 0;
    int alg_result = INT_MIN;
    errno = 0;
    int *dp = NULL;
    if (weights_length > 1000000 || (weights_length && !weights)) { errno = EINVAL; alg_result = INT_MIN; goto alg_cleanup; }
    if (values_length > 1000000 || (values_length && !values)) { errno = EINVAL; alg_result = INT_MIN; goto alg_cleanup; }
    int n = (int)weights_length;
    if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    if (!(((n >= 0) && (n <= 1000000)))) { errno = EINVAL; alg_result = INT_MIN; goto alg_cleanup; }
    if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    if (!(((int)weights_length == n))) { errno = EINVAL; alg_result = INT_MIN; goto alg_cleanup; }
    if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    if (!(((int)values_length == n))) { errno = EINVAL; alg_result = INT_MIN; goto alg_cleanup; }
    if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    if (!(((capacity >= 0) && (capacity <= 1000000)))) { errno = EINVAL; alg_result = INT_MIN; goto alg_cleanup; }
    if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    for (int alg_i = 0; alg_i < (int)weights_length; alg_i++) {
        int alg_value = weights[alg_i];
        if (!((alg_value > 0))) { errno = EINVAL; alg_result = INT_MIN; goto alg_cleanup; }
        if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    }
    for (int alg_i = 0; alg_i < (int)values_length; alg_i++) {
        int alg_value = values[alg_i];
        if (!((alg_value >= 0))) { errno = EINVAL; alg_result = INT_MIN; goto alg_cleanup; }
        if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    }
    {
        int alg_length = alg_add(capacity, 1, &alg_error);
        if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        if (alg_length < 0 || alg_length > 4000000) { errno = EINVAL; alg_result = INT_MIN; goto alg_cleanup; }
        dp = calloc((size_t)(alg_length ? alg_length : 1), sizeof(*dp));
        if (!dp) { errno = ENOMEM; alg_result = INT_MIN; goto alg_cleanup; }
    }
    int i = 0;
    if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    int c = 0;
    if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    while (1) {
        int alg_condition = (i < n);
        if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        if (!alg_condition) break;
        c = capacity;
        if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        while (1) {
            int alg_condition = (c >= weights[i]);
            if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
            if (!alg_condition) break;
            dp[c] = alg_max(dp[c], alg_add(dp[alg_sub(c, weights[i], &alg_error)], values[i], &alg_error));
            if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
            c = alg_sub(c, 1, &alg_error);
            if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
        }
        i = alg_add(i, 1, &alg_error);
        if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    }
    alg_result = dp[capacity];
    if (alg_error) { errno = ERANGE; alg_result = INT_MIN; goto alg_cleanup; }
    goto alg_cleanup;
alg_cleanup:
    free(dp);
    return alg_result;
}

int main(void) {
    int weights[] = {2, 3, 4};
    int values[] = {4, 5, 7};
    int capacity = 5;
    int result = knapsack(weights, 3, values, 3, capacity);
    printf("%d\n", result);
    if (errno) { perror("algorithm"); return 1; }
    return 0;
}
Algorithms: Dynamic Programming

0-1 Knapsack in Python

Knapsack chooses items with maximum total value under a weight limit, using each item at most once.

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from bisect import bisect_left

# 0-1 Knapsack in Python
#
# PURPOSE
# Knapsack chooses items with maximum total value under a weight limit, using each item at
# most once. The one-dimensional table stores the best value for each capacity.
#
# METHOD
# Process items one at a time and capacities downward. Compare skipping the item with adding
# its value to the previous best at the remaining capacity. Descending capacities prevent
# reusing the current item.
#
# COMPLEXITY
# O(n * capacity) time and O(capacity) auxiliary space; pseudo-polynomial.
#
# CONTRACT
# Weights are positive, values nonnegative, capacity >= 0. Both arrays have n entries.
# Returns best value, not the chosen item set.
# Public APIs infer array lengths except in C, which accepts pointer/length pairs. Vertex
# counts and matrix dimensions remain explicit. Arrays must contain signed 32-bit integers.
# Sorted order, correct tree topology, and caller-owned data-structure invariants are
# preconditions; checking them on every query would change query complexity.
# Scalar and shape checks are included. Value-range checks are included where needed to
# prevent invalid indexing or nontermination. Allocation sizes are capped at 4,000,000
# workspace entries. Arithmetic outside Python raises an error on 32-bit overflow; Python
# uses arbitrary precision. Native allocation failures propagate. In-place outputs may be
# partly updated after arithmetic failure. These are single-threaded, in-memory
# implementations.
#
# LANGUAGE DETAILS
# Python 3.9+: automatic lengths, native lists, arbitrary-precision arithmetic, and
# bisect_left for LIS. Invalid scalar/shape inputs raise ValueError. Integer element types
# and sorted/tree invariants remain caller preconditions.
#
# EXAMPLE
# Expected return: 9.
# Run: python example.py
# Tests for this exact source artifact run before publication. The accompanying source
# archive includes unit cases, runtime results, and source hashes.

def alg_require(condition):
    if not condition:
        raise ValueError("invalid algorithm input; see contract")
def alg_size(n):
    alg_require(0 <= n <= 4000000)
    return n

def knapsack(weights: list[int], values: list[int], capacity: int) -> int:
    alg_require(isinstance(weights, (list, tuple)) and len(weights) <= 1000000)
    alg_require(isinstance(values, (list, tuple)) and len(values) <= 1000000)
    alg_require(type(capacity) is int and -2147483648 <= capacity <= 2147483647)
    n: int = len(weights)
    alg_require(n >= 0 and n <= 1000000)
    alg_require(len(weights) == n)
    alg_require(len(values) == n)
    alg_require(capacity >= 0 and capacity <= 1000000)
    for value in weights:
        alg_require(value > 0)
    for value in values:
        alg_require(value >= 0)
    dp: list[int] = [0] * alg_size(capacity + 1)
    i: int = 0
    c: int = 0
    while i < n:
        c = capacity
        while c >= weights[i]:
            dp[c] = max(dp[c], dp[c - weights[i]] + values[i])
            c -= 1
        i += 1
    return dp[capacity]

weights = [2, 3, 4]
values = [4, 5, 7]
capacity = 5
result = knapsack(weights, values, capacity)
print(result)