Part IV · Cost, Information, and the Limits of Grammar
Chapter 12
Cost Profiles, Preprocessing, and Information
mixed The information lower bound and break-even analysis are elementary results proved here; the experiments are reproducible measurements.
- Account for the total cost of a workload: build, queries, updates and output, in a single formula.
- Compute break-even query volumes, including under invalidating updates.
- Prove that any representation answering reachability on all digraphs without consulting the graph needs at least bits.
- Distinguish retrieving an answer from discovering it, and both from producing a witness.
- Read the experimental evidence that no strategy dominates under updates.
12.1 Total cost
A resolution scheme (Definition 7.4) has a cost profile . For a workload of queries and updates the cost that matters is
plus the cost of writing the outputs, and subject to the space fitting in memory. Every “fast query” claim must be read against (12.1). A query answered in after preprocessing is a statement about the second term only.
Suppose updates invalidate a precomputed index, splitting the query stream into epochs with queries, and the index is rebuilt (cost ) at the first query of each non-empty epoch. Against a baseline without preprocessing (query cost ) and with indexed query cost , the index is cheaper iff
a sufficient condition is for every non-empty epoch.
Sum the costs epoch by epoch: the index pays in each non-empty epoch and the baseline .
∎The formula is trivial; its consequences are not. In the experiments on random graphs with (CGT-OBS-004), the break-even query volume for a precomputed transitive closure against breadth-first search ranged from about queries on dense graphs (where a giant strongly connected component lets the closure share rows) to about on sparse ones (where random queries are mostly negative and search stops early). The same index is an obvious win or an obvious loss depending on a structural property of the input and on the query distribution.
from cgtsdk import Cost, break_even
from cgtsdk.algorithms import bfs, transitive_closure
from cgtsdk.lab import random_digraph
import random
rng = random.Random(1)
for deg in (0.8, 3.0):
adj = random_digraph(1024, deg, seed=2)
build = Cost(); rows = transitive_closure(adj, build)
q0 = Cost()
for _ in range(300):
bfs(adj, rng.randrange(1024), q0) # search without an index (full BFS from a random source)
q1 = 1 # one bit test per indexed query
Qstar = break_even(build.total, q1, q0.total / 300)
print(f"avg out-degree {deg}: closure build {build.total} steps, BFS {q0.total/300:.1f} steps/query -> break-even after {Qstar:.0f} queries")
avg out-degree 0.8: closure build 14742 steps, BFS 10.0 steps/query -> break-even after 1637 queries
avg out-degree 3.0: closure build 8208 steps, BFS 4565.1 steps/query -> break-even after 2 queries
(Here each BFS runs to completion rather than stopping at the target, so the dense case is even more favourable to the index than in the ledger’s experiment.)
Text alternative: enter a build cost, per-query costs with and without the index, and the number of queries between invalidating updates; the widget computes and says whether the index pays within an epoch. Its defaults are the counted values of the ledger’s dynamic-workload experiment.
12.2 The information a grammar must contain
The strongest form of the founding conjecture would be a small “grammar” from which every reachability question about a graph can be read off, without searching the graph. Information theory forbids it.
Let a scheme map each structure in a class to a bit string from which every query of a family can be answered without access to . If the structures of induce distinct answer functions, then some encoding has length at least , and fewer than answer functions have encodings shorter than bits.
Structures with different answer functions need different encodings. There are strings of length at most , so ; and there are fewer than strings shorter than .
∎Any scheme that answers all reachability queries “does reach ?” on all digraphs with vertex set , without consulting the graph, uses at least bits on some digraph; and on all but a fraction smaller than of the bipartite graphs below it uses at least bits.
Split into of size and of size . For each let have edge set . Every path in has length at most one, so reaches iff . The graphs have pairwise different answer functions; apply Lemma 12.2 with .
∎No grammar, index or labeling of bits answers reachability on all digraphs without reading the graph; the transitive closure ( bits) is optimal for this task up to a factor of . Sub-quadratic schemes must either restrict the class of graphs or read the graph at query time — that is, search.
The asymptotic version for acyclic graphs follows from the enumeration of partial orders by Kleitman and Rothschild [Kleitman & Rothschild 1975] ( of their number is ); the bipartite family above gives the exact, non-asymptotic bound in one line. The result is elementary and almost certainly folklore. Its role here is to rule out precisely the reading of the founding conjecture that says a sufficiently expressive grammar could make reachability a constant-time lookup in general. The experiments agree: a general-purpose compressor could not shrink bipartite-family closures below bits (281,960 bits against the bound 262,144 at , CGT-OBS-005).
12.2.1 Restricted classes can be cheap
The lower bound applies to all digraphs. Restricted classes have less information, and for them the grammar of the class can be the optimal index.
For rooted forests on , assigning each vertex its DFS entry and exit times answers “ is an ancestor of ” by , in , with bits after preprocessing [Agrawal et al. 1989]. This is optimal up to a constant factor: there are labelled rooted forests, each with a distinct ancestor relation.
The nesting property of DFS intervals gives correctness. Cayley’s formula counts rooted forests on as trees on vertices. The ancestor relation determines each vertex’s parent as its deepest proper ancestor, so distinct forests have distinct answer functions; the counting lemma gives bits.
∎This is mechanism M-RESTRICT in its clearest form: the nesting structure that defines forests is itself a compact index, and the advantage comes from the restriction of the class, not from having a grammar. 2-hop labelings extend the idea to graphs whose reachability has small covers [Cohen et al. 2003].
12.3 Retrieval, discovery, witnesses
Three different things are often all called “answering a query”:
- Retrieval: reading an answer that was computed earlier (one bit of a closure).
- Discovery: computing the answer from the structure (a search).
- Witness production: exhibiting why — the actual path, of length .
Retrieval can be ; discovery cannot be cheaper than reading enough of the structure to decide; witness production costs at least the output size. A closure answers “is there a path?” by retrieval but still needs search-like work to produce the path: walking from along successors that still reach costs the sum of their out-degrees (CGT-NEG-011). Storing next-hop pointers for all pairs makes witness production output-linear, at bits — whether that is optimal is an open question (CGT-OPEN-009).
12.4 When the structure changes
Precomputation assumes the structure holds still. The ledger’s dynamic-workload experiment (CGT-EXP-005) ran nine streams of 4,000 operations on a digraph with , comparing BFS per query, a lazily rebuilt closure, an incrementally maintained closure (insertions handled by OR-ing rows, deletions forcing a rebuild [Italiano 1986]), and memoized BFS. Four different strategies won:
| Workload | BFS | lazy rebuild | incremental | memoized BFS | cheapest |
|---|---|---|---|---|---|
| queries only, uniform | 62,223 | 21,641 | 21,641 | 66,121 | rebuild / incremental |
| queries only, repetitive | 30,216 | 21,641 | 21,641 | 4,077 | memo |
| 90% queries, 10% insertions, uniform | 242,024 | 6,775,863 | 602,748 | 245,653 | BFS |
| 90% queries, 10% insertions, adversarial | 1,780,567 | 7,546,028 | 661,186 | 1,770,413 | incremental |
| 50% queries, mixed updates, uniform | 26,624 | 17,535,647 | 12,550,168 | 28,608 | BFS |
(Counted steps; full table in the ledger.) With 10% insertions, epochs contain about ten queries while , so the lazily rebuilt index costs 28 times as much as plain search — the break-even proposition at work. No strategy dominates; the winner is decided by the query–update ratio, the share of deletions, and the repetitiveness of queries (CGT-OBS-009).
Sharing suffers from updates in the same way: adversarial relabellings inflate a minimal DAG of a full binary tree to nodes (Chapter 5, CGT-THM-005).
12.5 An accounting of the mechanisms
| Mechanism | What it reduces | What it costs | What defeats it |
|---|---|---|---|
| M-ARITH | query time, space for addresses | requires dense numerations | sparse or deep shapes (Ch. 5); non-uniform shapes (Ch. 13) |
| M-PRE | query time | build time, space information content | updates; the counting lemma |
| M-SHARE | space | navigation overhead | updates; repetition of the wrong kind |
| M-RESTRICT | everything, on the class | applicability | inputs outside the class |
| M-PRUNE | search space | up to a factor more work | permissive languages (Ch. 8) |
Derive a version of Proposition 12.1 for an index that supports incremental updates at cost each instead of rebuilds. When is incremental maintenance better than lazy rebuilding?
Solution to Exercise 12.1
With updates, incremental maintenance costs ; lazy rebuilding costs . Incremental wins when — roughly when an update costs less than a rebuild times the fraction of updates followed by a query. Deletions that force rebuilds make effectively .
Use the counting lemma to give a lower bound on the space of any scheme answering “is adjacent to ?” on all undirected graphs with vertices without consulting the graph. Compare it with the adjacency matrix.
The counting lemma says nothing about schemes that read the graph at query time. Give a scheme with extra bits that answers reachability on DAGs faster than BFS for some queries, and say which mechanism it uses.
- Total cost plus output; an index pays only after queries per epoch (CGT-PROP-005).
- Retrieval-only reachability on all digraphs needs bits (CGT-THM-002); no grammar evades the counting lemma. Restricted classes such as forests have compact optimal indexes.
- Retrieval, discovery and witness production are different costs.
- Under updates no strategy dominates; the measured winners vary with the workload.
- Each mechanism has a cost and a class of inputs that defeats it.
References cited in this chapter
- Agrawal et al. 1989 R. Agrawal, A. Borgida, H. V. Jagadish (1989). Efficient management of transitive relationships in large data and knowledge bases. Proc. ACM SIGMOD 1989, pp. 253–262. doi:10.1145/67544.66950.
- Cohen et al. 2003 E. Cohen, E. Halperin, H. Kaplan, U. Zwick (2003). Reachability and distance queries via 2-hop labels. SIAM Journal on Computing 32(5):1338–1355. doi:10.1137/S0097539702403098.
- Italiano 1986 G. F. Italiano (1986). Amortized efficiency of a path retrieval data structure. Theoretical Computer Science 48:273–281.
- Kleitman & Rothschild 1975 D. J. Kleitman, B. L. Rothschild (1975). Asymptotic enumeration of partial orders on a finite set. Transactions of the AMS 205:205–220.