I have a problem at hand, at first sight it looks easy and it is, however I am looking for some other solution (maybe more easy one):
Expressions:
V0
V1
V2
V3
V4
SumA = V1 + V2
SumB = SumA + V3
SumC = SumB + SumA
SumD = SumC + V0
As we can see here, the “base” variables are V0, V1, V2, V3 and V4 (the value of each one of them is returned from DB queries)
The user ask the software to return the result of V1 and SumC.
Solution that I know:
Find all necessary variables: V1, SumC, SumB, SumA, V3, V2
For performance I just want to process the math of each variable JUST ONE TIME.
This means that I need to order the expressions from “base expressions” to “top variables”.
At this point I am only seeing a solution of the type “Tree (data structure)” > Get V1, V2 and V3
Then get SumA, after get SumB and only at last get SumC.
Is there any other way to solve this problem?
The final objective in this algorithm is to use with more complex variables and several “middle variables”. So, performance is critical, I can’t make the same math operation more than 1 time.
I am not sure I completely understand – but I think you are referring to common subexpression elimination, [or something similar to it] which is a very common compiler optimization.
One common way of doing this optimization is using a graph [which is actually a DAG] of the expressions in the program, and adding iteratively new expressions. The “sources” in your DAG are all initial variables [V0,V1,V2,V3,V4 in your example]. You can “know” which expression is redundant if you already calculated it – and avoid recalculating it.
These lecture notes seems to be a decent more detailed explanation [though I admit I did not read it all]