Algorithm 求多方程求值阶的算法
我的文本内容中充满了这样的表达:Algorithm 求多方程求值阶的算法,algorithm,Algorithm,我的文本内容中充满了这样的表达: a = 1 d = b + c b = a * c c = 2 - a ... a = 1 d = ? (skip) b = ? (skip) c = 2 - a = 1 ... d = ? (skip) b = a * c = 1 ... d = b + c = 2 ... 这些表达式可以随机顺序书写。我可以提取这些表达式并计算它们,但我需要找到最佳算法以避免循环计算,如: a = 1 d = b + c b = a * c c = 2 - a ...
a = 1
d = b + c
b = a * c
c = 2 - a
...
a = 1
d = ? (skip)
b = ? (skip)
c = 2 - a = 1
...
d = ? (skip)
b = a * c = 1
...
d = b + c = 2
...
这些表达式可以随机顺序书写。我可以提取这些表达式并计算它们,但我需要找到最佳算法以避免循环计算,如:
a = 1
d = b + c
b = a * c
c = 2 - a
...
a = 1
d = ? (skip)
b = ? (skip)
c = 2 - a = 1
...
d = ? (skip)
b = a * c = 1
...
d = b + c = 2
...
是否有方法通过相关参数对方程进行“排序”,以避免额外的计算过程,如:
a = 1
c = 2 - a = 1
b = a * c = 1
d = b + c = 2
...
?对于每个表达式,在图形中创建一个节点,其中传入的边是它所依赖的其他表达式。然后使用拓扑排序来确定求值顺序 例如: 表达方式:
a = 1
d = b + c
b = a * c
c = 2 - a
图形节点:
a()
d(b,c)
b(a,c)
c(a)
拓扑排序:
begin
process a
process a's dependencies (none)
output a
mark a as done
process d
process d's dependencies
process b
process b's dependencies
process a (already done)
process c
process c's dependencies
process a (already done)
output c
mark c as done
output b
mark b as done
process c (already done)
output d
mark d as done
process b (already done)
process c (already done)
end
结果:
a c b d
我想总有一个解决办法,你永远不会遇到像[a=b;b=a]或[a=1;a=2]?@Kwariz你是绝对正确的。可能有你提到的情况。甚至交叉引用也可能是错误的。但是我简化了这个问题只是为了基本的算法。谢谢你的更正!它实际上是某种
process\u variable()
函数的递归调用。非常感谢。