Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Algorithm for sequencing a collection of dependent equations

35 views
Skip to first unread message

Malcolm Greene

unread,
Jul 22, 2016, 12:01:28 PM7/22/16
to
We're working on a DSL (domain specific language) that we translate into
a list of tokenized expressions. My challenge is to figure out how to
sequence evaluation of these expressions so that we evaluate these
expressions in the proper order given that expressions have dependencies
on other expressions.

We've done the work to determine the full list of tokens associated with
each expression (after referencing other expressions) and we've detected
expressions that result in loops.

Here's an example of expressions and their full list of dependencies:

a = b + b + b + c + c > b, c, d, e, s, t, x
b = c + d + e > c, d, e, s, t, x
c = s + 3 > s, x
d = t + 1 > t
e = t + 2 > t
s = x + 100 > x
t = 10 > None
x = 1 > None
y = 2 > None

I'm looking for an algorithm/data structure that will me to start with
the least dependent expression (t, x, y) and move through the list of
expressions in dependency order ending with the expression with the most
dependencies.

I imagine that spreadsheets have to perform a similar type of analysis
to figure out how to recalculate their cells.

Suggestions on algorithms and/or data structures (some form of graph?)
to support the above goals?

Thank you,
Malcolm

Tim Golden

unread,
Jul 22, 2016, 12:36:03 PM7/22/16
to
On 22/07/2016 17:01, Malcolm Greene wrote:
> We're working on a DSL (domain specific language) that we translate into
> a list of tokenized expressions. My challenge is to figure out how to
> sequence evaluation of these expressions so that we evaluate these
> expressions in the proper order given that expressions have dependencies
> on other expressions.
>
> We've done the work to determine the full list of tokens associated with
> each expression (after referencing other expressions) and we've detected
> expressions that result in loops.

[... snip ...]

> I'm looking for an algorithm/data structure that will me to start with
> the least dependent expression (t, x, y) and move through the list of
> expressions in dependency order ending with the expression with the most
> dependencies.
>
> I imagine that spreadsheets have to perform a similar type of analysis
> to figure out how to recalculate their cells.
>
> Suggestions on algorithms and/or data structures (some form of graph?)
> to support the above goals?

I think that what you're looking for is a topological sort

TJG

Malcolm Greene

unread,
Jul 22, 2016, 12:45:54 PM7/22/16
to
Hi Tim,

> I think that what you're looking for is a topological sort

BINGO! That's *exactly* what I was searching for.

Thank you very much,
Malcolm

Robin Becker

unread,
Jul 25, 2016, 7:03:15 AM7/25/16
to
On 22/07/2016 17:01, Malcolm Greene wrote:
..........
> Here's an example of expressions and their full list of dependencies:
>
> a = b + b + b + c + c > b, c, d, e, s, t, x
> b = c + d + e > c, d, e, s, t, x
> c = s + 3 > s, x
> d = t + 1 > t
> e = t + 2 > t
> s = x + 100 > x
> t = 10 > None
> x = 1 > None
> y = 2 > None
>
> I'm looking for an algorithm/data structure that will me to start with
> the least dependent expression (t, x, y) and move through the list of
> expressions in dependency order ending with the expression with the most
> dependencies.
>
> I imagine that spreadsheets have to perform a similar type of analysis
> to figure out how to recalculate their cells.
>
> Suggestions on algorithms and/or data structures (some form of graph?)
> to support the above goals?
>
> Thank you,
> Malcolm
>
assuming you have no loops then the topological sort is what you need. If you
have mutual dependencies then you need to find a set of variables that cuts all
the loops and solve for those simultaneously. Unfortunately for a directed graph
structure I think the minimal cutset problem is NP complete so good luck with that.
--
Robin Becker

0 new messages