Counting and Sampling Traces in Regular Languages

Alexis de Colnet, Kuldeep S. Meel, Umang Mathur

TL;DR

This paper tackles the challenge of counting and sampling "traces"—equivalence classes of program executions that differ only by the order of independent concurrent actions. The authors prove that while counting words in a DFA is easy, counting traces is #P-hard; they then provide the first fully polynomial-time randomized approximation schemes (FPRAS) and almost uniform samplers (FPAUS) to solve these problems efficiently for any regular language.

What Problem Does This Paper Address?

In concurrent programming, two different sequences of events (words) might represent the exact same behavior if the only difference is the interleaving of independent operations (e.g., two threads updating local variables). These equivalence classes are known as Mazurkiewicz traces.

For tasks like model checking and randomized testing (fuzzing), we don't want to count every possible interleaving—that leads to "state space explosion." Instead, we want to count or sample the unique traces. If a model checker knows there are only 1,000 unique traces rather than 1,000,000 interleavings, it can allocate resources much more effectively. Similarly, a fuzzer needs to sample traces uniformly to ensure it explores diverse program behaviors rather than getting stuck testing the same logic in different orders.

The technical gap is that while we have efficient tools for counting strings in a regular language, counting traces is significantly more complex because a single trace can have a varying number of linearizations, and the "trace closure" of a regular language isn't always regular.

Methods

The authors approach the problem through three main lenses:

  1. Complexity Analysis: They prove that exact trace counting is #P-hard, even for Deterministic Finite Automata (DFAs). This is a striking result because counting words in a DFA is a classic polynomial-time problem.
  2. Approximate Counting (FPRAS): To bypass the #P-hardness, they develop a sampling-based approximation algorithm. They build on a "dependence-analysis" framework but introduce a novel concept of Canonical Runs. Since a trace can be accepted by an automaton through many different paths, they define a unique "canonical" path for every trace to ensure they don't overcount.
  3. Almost Uniform Sampling (FPAUS): They reduce the sampling problem to the counting problem. A key component here is the Prefix-Validator Automaton, a DFA that tracks whether a partially generated string can still be extended into a "lexicographical normal form" (the unique "first" string in a trace's alphabetical order). This ensures the sampler picks traces with equal probability.

Datasets

As a theoretical computer science paper, it does not use empirical datasets or machine learning benchmarks. The authors use a simple imperative program and its corresponding Control Flow Automaton (Fig. 1) as a synthetic example to illustrate how program behaviors map to regular languages and traces.

Key Insights and Ideas

  • The "P to #P" Jump: The most significant insight is that the transition from words to traces breaks the efficiency of DFAs. In a DFA, each word has one path. In trace theory, one trace (behavior) maps to many words, and those words might be scattered across the automaton in ways that make simple dynamic programming impossible.
  • Non-Local Effects: Independence relations create "non-local" effects. Swapping two symbols at the beginning of a long execution might change which swaps are possible at the very end. The authors' "canonical run" approach manages this by imposing a strict global order on how transitions are viewed.
  • Normal Forms are Key: By focusing on lexicographical normal forms (the "dictionary-first" representative of a trace), the authors can treat the set of all unique behaviors as a regular language itself, even if the original program's language isn't "trace-closed."

Key Results

  • Hardness: Proved that counting traces is #P-complete for NFAs and #P-hard for DFAs.
  • Approximation: Developed the first FPRAS for trace counting. The algorithm provides an $(\epsilon, \delta)$-approximation (meaning the result is within a factor of $1 \pm \epsilon$ with probability $1-\delta$) in time polynomial in the automaton size, string length, and accuracy parameters.
  • Sampling: Developed the first FPAUS that generates traces approximately uniformly.
  • Width Parameter: The complexity of their algorithms scales with the "width" of the concurrent alphabet (the size of the largest set of mutually independent actions), providing a fine-grained look at how concurrency affects performance.

Limitations and Future Work

  • Runtime Complexity: While polynomial, the current algorithms are still computationally expensive for very large systems. The authors suggest that optimizing the constant factors and exponents is a primary next step.
  • Sampling Overhead: The current sampling method requires $O(n)$ calls to the counting routine (where $n$ is the execution length). Finding a way to sample without repeated counting would drastically speed up trace-aware fuzzing.
  • Beyond Regular Languages: The work is limited to behaviors that can be described by finite automata. Extending these techniques to context-free languages (to model recursive function calls) remains an open challenge.

Summary updated

BibTeX
@article{colnet2026counting,
  author = {Alexis de Colnet and Kuldeep S. Meel and Umang Mathur},
  title = {{Counting and Sampling Traces in Regular Languages}},
  journal = {Proc. ACM Program. Lang.},
  year = {2026},
  doi = {10.1145/3776723},
}
Figure