Create your own
Lesson illustration

Dependency Graphs and Valid Evaluation Order

Hello. In the previous lesson, you used and to describe repeated work compactly. Those expressions can be part of a larger system model: one quantity is computed, then used by another calculation, which may feed several later outputs.

This lesson makes those relationships visible. You will turn a set of equations into a dependency graph, identify a valid evaluation order, and carry out a forward pass: computing values only after the values they require are available. This is a core habit for reading ML equations, analyzing agent harnesses, and later understanding how neural networks are evaluated.


Equations are small dependency specifications

Consider these two equations:

The first equation says that depends directly on and . The second says that depends directly on and .

A dependency graph makes those statements visual:

  • each quantity is a node;
  • a directed edge points from a required quantity to the quantity that uses it;
  • quantities with no defining equation in the current model are source nodes. They are inputs, fixed constants, or chosen parameters;
  • a quantity you care about at the end is an output node.

So , , and are sources here. You cannot compute until you have computed , because appears on the right-hand side of the equation for .

This is the same structural idea as a prerequisite map. A course can only be taken after its prerequisites; a computed quantity can only be evaluated after the quantities it requires are known.

The left side depicts course prerequisites as a directed acyclic graph: an edge means one course must be completed before another. The right side shows that the same dependency structure can be written as a valid linear order in which prerequisites come earlier.

A graph that has no circular dependencies is called a directed acyclic graph, usually abbreviated DAG.

“Directed” means the dependency has a direction. “Acyclic” means you cannot begin at one node, follow dependencies, and eventually return to that same node. A DAG can always be evaluated in at least one valid order.


From an equation list to a graph

There are two closely related ways to draw a graph of a computation:

  1. A quantity dependency graph uses variables such as , , and as nodes.
  2. A finer-grained computational graph can use operations such as addition and multiplication as nodes.

For system analysis, begin with the first form. It is concise and maps naturally onto a set of named workflow measurements. Later, when we differentiate functions, the more detailed operation-level form will become useful.

Use this four-part procedure whenever you encounter several equations.

1. List the quantities defined by equations

The variable on the left side of an equation is a computed quantity.

For example, in

the model defines . It does not define or in that equation.

2. Identify every required quantity

Look at the right-hand side. Here, both and are required to compute .

A useful way to record this is a dependency table:

Computed quantityDirect requirements

“Direct” matters. The final value also depends indirectly on and , because it requires , which requires them.

3. Mark source quantities

Any symbol that is needed but never defined within this equation list must already be known.

In the example, , , and are sources. In an AI workflow, a source may be:

  • an observed measurement, such as prompt-token count;
  • a configuration parameter, such as a model price;
  • a fixed constant, such as a budget limit;
  • an input supplied by an earlier system boundary.

Do not add a node for the literal number in an equation like . It is built into the operation. But do represent a named parameter such as if its value can be chosen or changed.

4. Draw dependencies from requirements to result

For each equation, connect every required right-hand-side quantity to the left-hand-side quantity. Label computed nodes with their defining equation when that makes the graph easier to read.

The graph is now a compact answer to two practical questions:

  • What does this output depend on?
  • What can I compute now?

Computation Graph (C1W2L07)

Watch “Computation Graph (C1W2L07)” from DeepLearningAI for a compact visual example of breaking one expression into intermediate quantities, then evaluating those quantities in dependency order.

Watch the decomposition to see why a single expression is split into small named calculations. Then watch the graph construction, focusing on which values are inputs and which are intermediate results. Finish with the forward pass, where numerical values are substituted only when the required earlier values are available.


A worked AI-workflow dependency graph

Suppose you are comparing an agent configuration that evaluates candidate responses. You define a simple score that penalizes both dollar cost and latency:

The meanings are:

SymbolMeaning
number of candidates evaluated
average tokens per candidate
total generated tokens
price per token
variable token cost
fixed workflow cost
total cost
fixed setup latency
average latency per candidate
total latency
baseline value of the configuration
score penalty per dollar
score penalty per second
final utility score

Before calculating anything, construct the dependency table:

Computed quantityEquationDirect requirements

The variables have no definitions in the equation set, so they are source nodes. The quantities are computed nodes.

Here is the corresponding dependency graph. Notice that it displays a branching workflow: the candidate count contributes both to token use and to latency.

This graph shows both direct and indirect dependencies. For example:

  • directly requires and .
  • indirectly requires , because requires .
  • indirectly requires , , and , through the cost branch.
  • also indirectly requires through the latency branch.

That last point is easy to miss in a long paragraph of prose, but obvious in a graph. This is one reason dependency graphs are useful for harness design: they expose which configuration choices influence each final metric.


Finding a valid computation order

A topological order is a list of nodes in which every dependency occurs before the quantity that uses it.

It is not necessarily the only valid order. Independent branches can be evaluated in either order. In the example, the cost branch and latency branch do not depend on one another until the final utility calculation.

A reliable manual method is:

  1. Write down the source values as available.
  2. Find an equation whose every right-hand-side symbol is available.
  3. Compute its left-hand-side quantity.
  4. Mark that new quantity as available.
  5. Repeat until you have computed the desired output.

For the utility model, one valid evaluation schedule is:

  1. Compute from and .
  2. Compute from and .
  3. Compute from and .
  4. Compute from , , and .
  5. Compute from , , , , and .

You could compute before , or between and , and the result would be equally valid. The only requirement is that you never use a value before its prerequisites have been computed.

Forward-pass calculation

Assign the following input values:

Start with total tokens:

Now variable token cost is available:

Then total cost:

The latency branch is independent of the cost branch until the final step:

Finally, every requirement for is available:

The forward pass did not require an advanced algorithm. It required one discipline: calculate each node only when its direct dependencies are known.


Dependency graphs and topological sorting

The pancake recipe is a useful non-mathematical example because it separates what must precede what from an exact minute-by-minute schedule.

Problem Solving with Algorithms and Data Structures using Python: The Interactive Edition

Read the short opening of Section 7.17, “Topological Sorting,” from Runestone Academy. It introduces dependency graphs with a pancake recipe, then defines topological sorting as a way to turn a DAG into a valid order of action.

In Section 7.17, “Topological Sorting,” first read from the paragraph beginning “The difficult thing about making pancakes is knowing what to do first” through the sentence ending “a graph algorithm called the topological sort.” Use the recipe discussion to notice that several starting tasks are possible, but later tasks have requirements. Then read the following definition paragraph, from “A topological sort takes a directed acyclic graph” through “multiplying matrices.” Focus on the definition: every prerequisite must occur earlier in the returned ordering.

This graph represents pancake-making prerequisites: ingredients must be combined before batter can be poured, the griddle must be heated before pouring, and both cooking and syrup preparation must be complete before eating.

There are many valid orders for this pancake graph. For instance, heating syrup can happen early or late, as long as it finishes before eating. Similarly, in the AI-workflow model, can be calculated before or after , because neither depends on the other.

A dependency graph therefore does not necessarily say:

  • which task a real system starts first;
  • whether independent tasks should run serially or in parallel;
  • how long each task takes.

It says something narrower and essential: which values must be available before another value can be computed.


Detecting a problem: circular dependencies

A graph cannot be evaluated as a normal forward computation if it contains a cycle with no externally supplied starting value.

For example:

To calculate , you need . To calculate , you need . Neither is a source, so there is no first computation to perform.

This does not mean equations containing mutual relationships are always useless. It means they are not yet a complete feed-forward procedure. You may need:

  • an initial value;
  • an additional constraint;
  • a simultaneous-equation solution method;
  • or an index that makes time explicit, such as .

That last form is how iterative and recursive workflows are often made computable: begin from , then calculate one step at a time. You will study indexed sequences and recurrence relations in a later module.

For now, apply this quick graph check to any equation set:

CheckWhat it reveals
Does every required symbol have a source value or defining equation?Whether the model is complete
Does each computed quantity have a clear equation?Whether its operation is specified
Can you begin from source nodes and eventually reach the output?Whether a forward pass is possible
Does any quantity ultimately require itself?Whether there is a circular dependency
Are independent branches present?Whether multiple valid orders, and perhaps parallel execution, are possible

Wrap-up

A dependency graph turns equations into an operational model:

  • A node represents a quantity, and its incoming edges identify the quantities it directly requires.
  • Inputs, constants, and configuration parameters are source nodes; they are available before computation begins.
  • A topological order is any order in which every dependency is computed before the quantity that uses it.
  • A forward pass evaluates values by repeatedly choosing equations whose required inputs are already known.
  • Independent branches may be computed in different valid orders.
  • A circular dependency prevents an ordinary forward pass unless the model supplies an initial value or another way to resolve the loop.

Next, you will reproduce a hand calculation in short Python expressions. That will make the connection explicit between an equation, its dependency-respecting evaluation order, and executable code.

Can't find a good explanation? Sign up and we'll make it for you

Sign up