AlgoWiki

Knuth's optimization

    Applicability

    Knuth's optimization applies when the dynamic programming recurrence is approximately of the form

    dp[i][j]=mini<k<j{dp[i][k]+dp[k][j]}+C[i][j],\mathrm{dp}[i][j] = \min_{i<k<j} \left\{\mathrm{dp}[i][k] + \mathrm{dp}[k][j] \right\} + C[i][j],

    where A[i][j1]A[i][j]A[i+1][j]A[i][j-1] \leq A[i][j] \leq A[i+1][j]. Here A[i][j]A[i][j] is the smallest index i<k<ji < k^\star < j that minimizes dp[i][k]+dp[k][j]\mathrm{dp}[i][k^\star] + \mathrm{dp}[k^\star][j], i.e.

    A[i][j]=argmini<k<j{dp[i][k]+dp[k][j]}.A[i][j] = \mathrm{argmin}_{i<k<j} \left\{\mathrm{dp}[i][k] + \mathrm{dp}[k][j] \right\}.

    A sufficient (but not necessary) condition for A[i][j1]A[i][j]A[i+1][j]A[i][j-1] \leq A[i][j] \leq A[i+1][j] to hold is that C[a][c]+C[b][d]C[a][d]+C[b][c]C[a][c] + C[b][d] \leq C[a][d] + C[b][c] and C[b][c]C[a][d]C[b][c] \leq C[a][d] where abcda \leq b \leq c \leq d

    The naive way of computing this recurrence with dynamic programming takes O(n3)O(n^3) time, but only takes O(n2)O(n^2) time with Knuth's optimization.

    Problems

    See also