optimization - Algorithm for optimizing computation of an arithmetic expression -
suppose have expression formed integer variables , arithmetic operations: addition, subtraction , multiplication. know each multiplication takes m seconds, , each addition/substraction takes seconds. there algorithm compute expression in efficient way arbitrary assignment variables? (assume can store in memory 1 number).
example:
m=10
a=1
expression: a*a+a*b+b*b.
initially, has 3 multiplications , 2 additions, total time 3*m+2*a=32
however, can build equivalent expression (a+b)*(a+b)-a*b has 2 multiplications , 3 additions, total computation time 2*m+3*a=23.
Comments
Post a Comment