Applicability
Knuth's optimization applies when the dynamic programming recurrence is approximately of the form
dp[i][j]=i<k<jmin{dp[i][k]+dp[k][j]}+C[i][j],
where A[i][j−1]≤A[i][j]≤A[i+1][j]. Here A[i][j] is the smallest index i<k⋆<j that minimizes dp[i][k⋆]+dp[k⋆][j], i.e.
A[i][j]=argmini<k<j{dp[i][k]+dp[k][j]}.
A sufficient (but not necessary) condition for A[i][j−1]≤A[i][j]≤A[i+1][j] to hold is that C[a][c] + C[b][d]≤C[a][d] + C[b][c] and C[b][c]≤C[a][d] where a ≤ b ≤ c ≤ d
The naive way of computing this recurrence with dynamic programming takes O(n3) time, but only takes O(n2) time with Knuth's optimization.
Problems
See also
External links