持续计算对象之间的级联关系的最佳方法(算法)是什么?

发布于 2024-09-28 04:16:49 字数 202 浏览 5 评论 0原文

例如,A+B=C C+D=E E+F=G,当对每个节点进行更改时,会重新计算关联的节点。 下图是我正在尝试做的事情的简单示例。

进一步澄清 每个对象的结构都是相同的。输入将是价格,因为每次价格变化都会对下游价格产生连锁效应。所以在上面的例子中,A+B=C 将变成 5+6=11。变化

不断发生(可能每秒),当每个值发生变化时,我需要得到通知(事件触发)。

For instance A+B=C C+D=E E+F=G as changes are made to each node the associated nodes are recalculated.
The image below is a simplistic example of what I am trying to do.

Further clarification
The structure for each object is identical. the inputs would be prices as each price changes it would have a cascading effect on the prices downstream. so in the example above A+B=C would become 5+6=11. etc.

Changes take place constantly (possibly every second), as each value is changed I would need to be notified (Event fired).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清旖 2024-10-05 04:16:49

只要你的图表没有改变,只有值,你就可以进行拓扑排序你的图表。然后从发生变化的值开始按拓扑排序顺序遍历图表。如果更改将是图表的稀疏部分,请按拓扑排序顺序为每个节点分配一个索引,并使用 优先级队列来决定下一步执行哪个节点。

As long as your graph isn't changing, only the values, you can do a topological sort of your graph. Then walk the graph in topological sort order starting at the value(s) which changed. If the changes are going to be a sparse part of the graph, assign each node an index in topological sort order and use a priority queue to decide which node to do next.

素年丶 2024-10-05 04:16:49

最简单的方法就是基于事件的方法。每个节点都有一个“onchanged”事件,任何使用该节点的东西都可以订阅该事件。节点更新自身后,它会引发该事件并让其他任何需要知道的事情发生。

如果您的依赖关系更复杂,那么您可能需要其他东西来管理更新以优化事物。例如,如果A影响B和C,并且C也影响B(例如B=A+C和C=A+1),则简单的方法可能会更新a,然后更新b,然后再次更新C,然后再次更新B。这可行,但显然比所需的更新多了一个。优化更新的确切方法将取决于依赖树的复杂程度。

The simplest way would jsut be an event based method. Each node has an "onchanged" event and anything that uses that node can subscribe to that event. After a node has updated itself then it raises that event and lets anything else that needs to know.

If your dependancies are more complex then you may need to have something else managing the updates to optimise things. eg if A effects B and C and C also effects B (eg B=A+C and C=A+1) then a simple method might update a, then b, then C then B again. This works but is obviously one mroe update than is required. The exact way of optimising the updates will depend on how complex your dependancy tree is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文