Topological order: the algorithm that decides what to compute first
In a node graph nobody decides by hand what to compute first. The wires decide, through a rule called topological sort — and that rule is why moving a node never changes a number.
Kahn's algorithm in three steps
The classic method was described by Kahn in 1962. First, collect every node with no incoming wire. Second, take one, append it to the compute list, and delete the wires leaving it. Third, collect the nodes that just lost their last incoming wire and repeat until no node is left.
The result is an order in which every wire points forwards. No node needs a value that appears later in the list, so one pass down the list defines every value.
FlowSim processes nodes of the same stage in their original order, which makes the result deterministic: the same graph always yields the same order.
| Node | Incoming wires | Order |
|---|---|---|
| Starting capital (slider) | 0 | 1 |
| Monthly contribution (slider) | 0 | 2 |
| Compounded balance (series) | 3 — start, add, rate | 6 |
| Total paid in (multiply) | 2 | 7 |
| Principal (add) | 2 | 8 |
| After-tax value (add) | 2 — principal, after-tax gain | 11 |
| Output chart | 1 | 14 |
Why the order does not change the answer
Several valid orders can exist, which feels unsettling at first. If A and B are independent, computing A first or B first is fine. But independent means exactly that neither uses the other's value, so the result is identical either way.
That is why dragging nodes, adding nodes, or deleting unrelated ones leaves computed values unchanged. If a value moves, a wire changed or a cycle appeared.
What a cycle does
Kahn's algorithm ends in success if every node makes it into the list, and in failure if some are left. The leftovers are waiting on each other and can never start.
FlowSim reports exactly that: the count of nodes that did not make the list, and a blank value for each of them. In the node-value table the order number is replaced by "in cycle — not computed".
Finding a cycle and avoiding one are different jobs. Checking before a wire is created means the cycle never exists.
Checking by hand
For a busy graph, write the incoming-wire count beside each node on paper and erase nodes with a count of zero as you go. What you erased is the compute order; whatever cannot be erased is the cycle.
The node-value table in FlowSim is that order: the first row is computed first, the last row last. It is the quickest way to see which value depends on which.
queue = [nodes with in-degree 0]
while queue:
n = queue.pop(0)
order.append(n)
for m in n.outgoing:
in_degree[m] -= 1
if in_degree[m] == 0:
queue.append(m)
# order shorter than nodes → a cycle exists
Where else it is used
Topological order is not only for calculation graphs. Build tools use it to decide which file to compile first, spreadsheets to decide which cell to evaluate, package managers to decide which dependency to install.
The shared rule is: respect the dependency where one exists, otherwise any order will do. Let code follow that rule and nobody has to memorise the order.
Questions
Can there be several valid orders?
Yes. Independent nodes can be swapped. FlowSim uses the on-screen order among them so the result is always the same.
Does it slow the app down?
It is linear in the number of nodes. With dozens of nodes, re-sorting on every slider move is imperceptible.
Sources
Every calculation here can be reproduced with a template in the simulator.
Open the simulator