編集:CSEまたはマトリックスチェーン乗算を実行するには、受け入れられた回答に加えて、この回答の成長の順序が必要です
興味深いことに、圧縮アルゴリズムが必要な場合があります。圧縮アルゴリズムは、圧縮対象のサイズを縮小しようとします。それができる唯一の方法が置換である場合は、それを追跡して、アルゴリズムに必要なサブコンポーネントを取得できます。これは、入力が小さい場合でも良い結果が得られない場合があります。
操作のどのサブセットが可換であるかは、そのようなアルゴリズムを選択する際の重要な考慮事項になります。[編集:OPは、彼/彼女の状況では交換可能な操作はないと言っています]
キャッシングなどの影響を無視すれば、最適なソリューションを定義することもできます。
input: [some product of matrices to compute]
given that multiplying two NxN matrices is O(N^2.376)
given we can visualize the product as follows:
[[AxB][BxC][CxD][DxE]...]
we must for example perform O(max(A,B,C)^2.376) or so operations in order to combine
[AxB][BxC] -> [AxC]
The max(...) is an estimate based on how fast it is to multiply two square matrices;
a better estimate of cost(A,B,C) for multiplying an AxB * BxC matrix can be gotten
from actually looking at the algorithm, or running benchmarks if you don't know the
algorithm used.
However note that multiplying the same matrix with itself, i.e. calculating
a power, can be much more efficient, and we also need to take that into account.
At worst, it takes log_2(power) multiplies each of O(N^2.376), but this could be
made more efficient by diagonalizing the matrix first.
貪欲なアプローチが実行可能かどうかについての質問があります。各ステップで繰り返し部分文字列を圧縮する必要があるかどうか。これは当てはまらない場合があります。
aaaaabaab
compressing 'aa' results in ccabcb and compressing 'aab' is now impossible
ただし、部分文字列を圧縮するすべての順序を試しても、この問題に頻繁に遭遇することはないだろうという予感があります。
したがって、必要なもの (コスト) を書き留めて、考えられる問題を検討すると、これを実行できるブルート フォース アルゴリズムが既にあり、非常に少数の行列に対して実行されます。
# pseudocode
def compress(problem, substring)
x = new Problem(problem)
x.string.replaceall(substring, newsymbol)
x.subcomputations += Subcomputation(newsymbol=substring)
def bestCompression(problem)
candidateCompressions = [compress(problem,substring) for each substring in problem.string]
# etc., recursively return problem with minimum cost
# dynamic programming may help make this more efficient, but one must watch
# out for the note above, how it may be hard to be greedy
注: Asgeir による別の回答によると、これは Matrix Chain Multiplication 最適化問題として知られています。Nick Fortescue は、これはより一般的にはhttp://en.wikipedia.org/wiki/Common_subexpression_eliminationとしても知られていると述べています。したがって、一般的な CSE または Matrix-Chain-Multiplication アルゴリズム/ライブラリを文献から見つけて、コストを差し込むことができます。前述の桁数(どのソリューションを使用する場合でも必要になります)。上記の計算 (乗算、累乗など) のコストは、最先端のアルゴリズムを使用して効率的に実行されていることを前提としていることに注意してください。そうでない場合は、指数を操作の実行方法に対応する適切な値に置き換えます。