Skip to main content

Module dynamic_programming

Module dynamic_programming 

Source
Expand description

Dynamic programming: classic optimisation problems solved by tabulation, and solvers for Markov decision processes.

The first group (knapsack, coin change, longest common / increasing subsequence, edit distance, matrix-chain ordering) each return the optimal value together with a reconstruction of one optimal solution. The second group, FiniteMdp, solves sequential decision problems by backward induction (finite horizon) or value iteration (discounted, infinite horizon).

See the book entry Dynamic programming.

Structs§

FiniteMdp
A finite Markov decision process: in state s, taking action a yields rewards[s][a] and moves to state s' with the probabilities in transitions[s][a] (a list of (s', prob)). Every state must offer at least one action; the probabilities of each action must sum to 1.
MdpSolution
The value function and a greedy policy.

Enums§

EditOp
One step of an edit script turning sequence a into b.

Functions§

coin_change_min_coins
The fewest coins (unlimited supply of each denomination) summing to amount, as the count of each denomination; None if the amount cannot be made. Zero-valued denominations are ignored.
coin_change_ways
The number of distinct multisets of coins summing to amount (order ignored), saturating at u64::MAX.
edit_distance
Levenshtein distance between a and b (unit cost for insert, delete and substitute) and one optimal edit script.
knapsack_01
0/1 knapsack: chooses items (each at most once) maximising total value within capacity. Returns (best value, chosen item indices in increasing order). O(n · capacity) time and memory.
longest_common_subsequence
A longest common subsequence of a and b as pairs of matched indices (i in a, j in b).
longest_increasing_subsequence
A longest strictly increasing subsequence, as indices into values, in O(n log n).
matrix_chain_order
The cheapest way to multiply a chain of matrices where matrix k is dims[k] × dims[k+1]: (scalar multiplications, parenthesisation such as "((A0A1)A2)"). Needs at least one matrix (dims.len() >= 2).