The Mathematics of Flow
Learning objective: Read and write the formal notation; understand firing rules and state equations.
Chapter 1 introduced Petri nets informally — circles, bars, arrows, dots. That’s enough for intuition, but not enough for analysis. To prove that a system can’t deadlock, that a resource pool never loses a unit, or that a workflow always terminates, you need precise definitions. This chapter provides them.
The notation looks heavier than it is. Everything here follows from two ideas: a net is a bipartite graph with weights, and a marking is a vector of integers. The rest is matrix arithmetic.
The Formal Definition
A Petri net is a 5-tuple:
Where:
- is a finite set of places
- is a finite set of transitions
- is the flow relation — the set of arcs
- is the arc weight function
- is the initial marking
Two constraints keep things clean: (nothing is both a place and a transition) and (the net isn’t empty).
That is the whole definition. Places are where tokens live. Transitions are what moves them. Arcs say who connects to whom. Weights say how many tokens an arc carries. The initial marking says where tokens start.
Presets and Postsets
Every transition has inputs and outputs. The notation for these is compact and worth memorizing:
- — the preset of (input places)
- — the postset of (output places)
The same notation works for places:
- — transitions that feed into
- — transitions that feeds into
The bullet notation reads naturally: means “what comes before ,” and means “what comes after .”
Markings and the State Vector
A marking assigns a non-negative integer to each place — how many tokens it holds. Since a net has places, we can write the marking as a column vector:
This is the state vector. It captures everything about the system’s current state. Two markings are equal if and only if every place has the same token count in both. For the nets of this chapter there is no state outside the vector.
The initial marking is the starting state. For the traffic light from Chapter 1:
Every marking you can reach by firing transitions is a point in — non-negative integer coordinates, one per place. The set of all reachable markings, starting from , is the reachability set . For the traffic light, contains exactly three points: the token in Green, the token in Yellow, the token in Red.
Firing Rules and Reachability
A transition fires by consuming tokens from its input places and producing tokens in its output places. But it can only fire when it has enough tokens to consume. The enabling condition is:
Every input place must have at least as many tokens as the arc weight demands. If any input place is short, the transition is blocked.
When an enabled transition fires, the new marking is:
For every place : subtract the tokens consumed (arc from to ) and add the tokens produced (arc from to ). If isn’t connected to , both weights are zero and — unconnected places are unaffected.
A Worked Example
Consider a simple three-place chain:
[p1] -> t1 -> [p2] -> t2 -> [p3]
With initial marking (one token in ).
Step 1: Is enabled? Its only input is , and . Yes. Fire :
Step 2: Is enabled? Its only input is , and . Yes. Fire :
The token has moved from through to . The reachability set is . Three reachable markings, matching intuition — the token can be in exactly one of three places.
Firing Sequences
A firing sequence is an ordered list of transitions: . The sequence is valid from marking if each transition is enabled when it’s supposed to fire — is enabled at , is enabled at the marking after fires, and so on.
We write to mean “firing sequence is valid from and leads to .” The reachability set is then:
Nondeterminism
When multiple transitions are enabled simultaneously, either one could fire. The Petri net doesn’t prescribe which. This nondeterminism is deliberate: it models concurrency, where the order of independent events is unspecified.
In the traffic light, only one transition is ever enabled at a time, so there’s no nondeterminism. But in the intersection model from Chapter 1, when the shared token is available, both lights could claim it. The net structure constrains what’s possible; it doesn’t dictate the schedule.
The Incidence Matrix
Computing markings by hand gets tedious. The incidence matrix makes it mechanical.
The incidence matrix has one row per place and one column per transition. Each entry is the net effect of firing that transition on that place:
Positive means the transition produces tokens in that place. Negative means it consumes. Zero means no net effect, which includes a self-loop that consumes and produces the same number. Inhibitor and read arcs, which test a place without moving tokens, have no entry in at all; Appendix E explains what that costs.
go-pflow builds this matrix in IncidenceMatrix (reachability/invariants.go) with the same orientation, rows for places and columns for transitions, sorted by name, and it skips inhibitor arcs for the same reason.
Building the Matrix
For the three-place chain:
Reading column : firing removes one token from and adds one to . Reading column : firing removes one from and adds one to . Each column is a change vector — the delta applied to the marking when that transition fires.
The State Equation
With the incidence matrix, the firing rule becomes matrix multiplication:
Where is the firing count vector — how many times each transition fires. For a single firing of , :
Fire both transitions once () to go directly from to :
The state equation is linear: given any firing count vector, one matrix multiplication gives the resulting marking. The caveat is that it doesn’t check enabling. A firing count vector might describe a sequence that isn’t valid — some transition might need tokens that aren’t there yet. The state equation gives you a necessary condition for reachability, not a sufficient one.
A Richer Example
Consider a net where a transition has two inputs — the synchronization pattern:
[p1] --> t1 --> [p3]
[p2] --/
Transition needs a token from both and to fire. The incidence matrix:
With , firing gives . With , is not enabled — has no token. The synchronization is enforced by the structure.
P-Invariants and Conservation Laws
The incidence matrix also shows what cannot change, whatever sequence of transitions fires.
A P-invariant (place invariant) is a non-zero vector such that:
The vector assigns a weight to each place such that the weighted sum of tokens is preserved by every transition. If is a P-invariant, then for any reachable marking :
The proof is one line of the state equation. If , then , so
This is a conservation law: whatever fires, in whatever order, the weighted token count stays constant. The argument needs only the state equation, which holds for every valid firing sequence, so the caveat about enabling does not weaken it.
Finding P-Invariants
For the three-place chain, try :
It works. The invariant says:
The total number of tokens is always 1. go-pflow’s TestPInvariantThreePlaces (reachability/farkas_test.go) builds the same chain with five tokens in the first place, asserts that the only minimal invariant found is a + b + c == 5, and checks it on every reachable marking (not re-run here).
Chapter 1 argued informally that the traffic light keeps exactly one token. The same check makes that argument formal. With places Green, Yellow, Red as rows and transitions go_yellow, go_red, go_green as columns, the light’s incidence matrix and the all-ones vector give
so in every reachable marking. Each column sums to zero because each transition moves one token from one place to the next.
What Conservation Laws Tell You
P-invariants answer practical questions:
“Can tokens leak?” If is an invariant, the total token count is constant. The net is closed — nothing is created or destroyed. This is the Petri net equivalent of conservation of mass.
“Is this resource pool managed correctly?” If two places (available) and (busy) satisfy for some constant , then resources are always either available or busy, never duplicated or lost.
“Is the net bounded?” If every place appears in at least one P-invariant with a positive coefficient, then no place can accumulate unbounded tokens: for any invariant covering . The net is structurally bounded, bounded from every initial marking, with the bound depending on . The test is sufficient, not necessary; a net can be bounded without a covering invariant. go-pflow implements this test as StructuralBoundedness in reachability/invariants.go.
Partial Invariants
Not every net has a P-invariant covering all places. A net with a “source” transition (one that produces tokens from nothing) or a “sink” transition (one that consumes tokens into nothing) breaks total conservation. But subsets of places may still be conserved.
For example, in an open producer-consumer system, has no input place and has no output place:
t_produce -> [buffer] -> t_consume
The buffer’s row of is , and no non-zero makes , so this net has no P-invariant at all. go-pflow’s TestPInvariantNoneForUnbounded (reachability/farkas_test.go) checks the producer half of this net and expects an empty result.
Now add a capacity limit by looping a token back to a pool of empty slots, and let also record each delivery:
[empty_slots: 5] -> t_produce -> [buffer] -> t_consume -> [empty_slots]
\-> [delivered]
The incidence matrix, with rows empty_slots, buffer, delivered:
| empty_slots | ||
| buffer | ||
| delivered |
Solving by hand: the first column forces , and the second then forces . So , and in every reachable marking. The buffer can never exceed 5. The delivered place is in no invariant, and it grows by one on every consumption without limit: the conservation law covers part of the net and not the rest.
In Code
The go-pflow library computes incidence matrices and checks invariants directly:
analyzer := reachability.NewInvariantAnalyzer(net)
matrix, places, transitions := analyzer.IncidenceMatrix()
invariants := analyzer.FindPInvariants(initialMarking)
for _, inv := range invariants {
fmt.Printf("Conserved: %v = %d\n", inv.Coefficients, inv.Value)
}
// Check if total tokens are conserved
isConservative := analyzer.CheckConservation(initialMarking)
The IncidenceMatrix method builds the matrix from the net’s arc structure. FindPInvariants solves for semi-positive with the Farkas algorithm and returns the minimal-support invariants, each with its constant evaluated at the given marking; every semi-positive invariant is a non-negative combination of these, so an empty result means the net has no conservation law (provided the search did not hit its row limit, which PInvariantBasis reports). CheckConservation tests only whether the all-ones vector is an invariant, that is, whether the total token count is preserved. All three are in reachability/invariants.go; the tests in reachability/farkas_test.go cover weighted invariants, invariants over three places, and the mutual-exclusion net with its three separate laws.
Putting It Together
The mathematics of this chapter — the 5-tuple, the state equation, the incidence matrix, P-invariants — is the toolkit for structural analysis. Given a Petri net, you can:
- Write the incidence matrix from the arc structure
- Rule out markings with the state equation : a marking with no non-negative integer solution is unreachable, though a solution does not prove reachability
- Find conservation laws by solving
- Prove boundedness from positive P-invariants
- Check for deadlocks by finding reachable markings where no transition is enabled
Items 1 to 4 are static analysis: you reason about the structure without running the net, and the results hold for every firing sequence. Item 5 is different. Deadlock depends on which markings are reachable, so it is found by building the reachability graph; go-pflow’s Analyzer.BuildGraph reports the dead markings in Result.Deadlocks (reachability/analyzer.go, checked by TestGraphDeadlock in reachability/reachability_test.go).
That exploration is where discrete nets run into their limit. They track individual tokens and individual firings, and for large systems — thousands of tokens, complex topologies — the reachability set can be enormous. The next chapter trades exactness for scale by letting tokens be real numbers and transitions fire continuously, turning the state equation into a differential equation.