Part I · What Is a Computational Structure?
Chapter 6
Graphs and Paths
mixed Standard material; the relational reading of paths is classical (Kleene algebra, Tarjan's path expressions) and is restated here as the semantic basis of the book.
- Compare adjacency-matrix and adjacency-list representations by the cost of their basic operations.
- Read a labelled graph as a family of relations, and words of labels as compositions of relations.
- Prove that the meaning of a word of moves is the composition of the meanings of its letters, and that languages of words denote unions.
- Distinguish walks from simple paths, and explain why that semantic choice moves a problem from polynomial time to NP-hardness.
- Relate paths-as-grammar to Tarjan’s path expressions and to semiring path problems.
6.1 Graphs and their representations
A directed graph is a structure with one binary relation . A labelled graph has one relation per label in a finite alphabet ; a weighted graph attaches a number to each edge. Throughout, and .
Two representations dominate:
| Operation | Adjacency matrix ( bits) | Adjacency lists ( words) |
|---|---|---|
| test whether | : one bit at address | , or expected with hashing |
| list the out-neighbours of | ||
| breadth-first search from | ||
| space | bits | words |
The matrix applies the array’s address formula to pairs of vertices (row-major numeration of , Chapter 2), so edge tests are arithmetic; but it pays bits whether or not the graph is sparse, and every traversal touches whole rows. Lists pay only for edges that exist. Neither is better in general — the familiar shape of every comparison in this book.
6.2 Labelled graphs as relations
Read each label as a move whose meaning is the relation : from vertex , the move may lead to any with . A word of labels then means “follow an edge, then an edge, …”, and its meaning is a composition of relations.
Given relations for each , extend to words and languages by
where is relational composition (first , then ). For a vertex , write .
is a monoid homomorphism from to . For languages, , , and , the reflexive–transitive closure of .
The extension to words is by composition, and relational composition is associative with identity , so for all words by induction on . Composition distributes over arbitrary unions on both sides, which gives the identities for concatenation and union of languages, and hence for .
∎The proposition turns the recursive slogan “a path from to is an edge from to some followed by a path from to ” into an identity:
Paths really are grammatical objects: the set of walks is the language (or for labelled graphs), and its meaning is the transitive closure. This is the relational reading underlying Kleene algebra, and Tarjan’s path expressions make it algorithmic: a regular expression over the edges that represents all paths from a source to each vertex, from which shortest paths, path counts and dataflow facts can be computed by interpreting the expression in a suitable algebra [Tarjan 1981a; Tarjan 1981b]. Tarjan showed that constructing path expressions is, in a precise sense, the most general path problem.
Proposition 6.2 is a statement about semantics. It says the recursive description of paths is correct; it says nothing about how cheaply a path can be found. Chapter 10 shows that evaluating the recursive description is exactly the work of computing a closure, and that two grammars with the same meaning can differ asymptotically in cost (CGT-THM-004).
The SDK implements this relational semantics directly: the state of GraphStructure is a set of current vertices, and a label move maps the set to its image.
from cgtsdk.structures import GraphStructure
edges = [(0, "a", 1), (0, "a", 2), (1, "b", 3), (2, "b", 3), (3, "a", 0), (2, "c", 4)]
g = GraphStructure(5, edges).grammar()
r = g.execute("at(0) a here b here a here c")
print(r.values, "| defined:", r.defined, "| undefined at call", r.undefined_at, "-", r.reason)
[None, None, [1, 2], None, [3], None, [0]] | defined: False | undefined at call 7 - ⟦c⟧ of the current set is empty
From , the move reaches , then reaches , then returns to ; the final has no edge from , so the expression is undefined at that call — the semantic layer again, with its reason.
6.3 Walks, simple paths, and a change of meaning
A walk may repeat vertices; a simple path may not. The distinction looks minor. It is not.
Let be a regular language over the labels, given by an automaton with states. Deciding whether some walk from to has its label word in takes time. Deciding whether some simple path from to has its label word in is NP-complete for some fixed regular languages [Mendelzon & Wood 1995], and remains NP-hard on very restricted graph classes [Barrett et al. 2000].
The first half is the product construction of Chapter 8 (CGT-THM-001); the second is a theorem of Mendelzon and Wood. Nothing about the syntax of the query changed — the same regular expression, the same graph. What changed is the semantics: which sequences of edges count as a path. That one change moves the problem across the boundary between polynomial time and NP-completeness (assuming P ≠ NP). It is the clearest example in this book of why the semantic layer must be stated precisely before any claim about cost is made.
6.4 Path problems as algebra
Many path problems differ only in how the paths in are aggregated:
| Problem | Value of a path | Combine alternatives | Algebra |
|---|---|---|---|
| reachability | true | or | Boolean semiring |
| shortest path | sum of weights | min | tropical semiring |
| widest path | min of capacities | max | |
| number of paths (DAG) | 1 | + |
Interpreting the path grammar in each algebra gives each problem; this is the unifying idea behind Tarjan’s path algebra and the closed-semiring treatment of shortest paths in textbooks [Cormen et al. 2022]. For CGT the point is precise: the grammar of paths () is common to all of them; the semantics is chosen by the algebra; the execution cost depends on the algebra’s properties (Dijkstra’s algorithm needs non-negative weights; Bellman–Ford does not). Chapter 10 implements each.
For the graph of the SDK example above, compute and by hand, using Proposition 6.2.
Solution to Exercise 6.1
and , so . For : ; ; . Hence .
Show that relational composition does not distribute over intersection: give relations with . What does this imply for a “language” built with intersection?
Explain why the walk/simple-path dichotomy does not arise for the language (plain reachability): show that if a walk from to exists, a simple path exists.
In the counting semiring, may be infinite on graphs with cycles. Which property of DAGs makes path counting well defined, and how is it computed in time?
- Adjacency matrices apply the address formula to vertex pairs; adjacency lists pay only for existing edges. Neither dominates.
- Labels are moves whose meanings are relations; words mean compositions and languages mean unions (CGT-PROP-001). “A path is an edge followed by a path” is the identity .
- This is a semantic fact, not a cost bound.
- Changing the semantics from walks to simple paths makes regular path queries NP-complete: the syntax is unchanged, the meaning is not.
- Path problems share a grammar and differ by the algebra in which it is interpreted.
References cited in this chapter
- Barrett et al. 2000 C. Barrett, R. Jacob, M. Marathe (2000). Formal-language-constrained path problems. SIAM Journal on Computing 30(3):809–837. doi:10.1137/S0097539798337716.
- Cormen et al. 2022 T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein (2022). Introduction to Algorithms, 4th edition. MIT Press (ISBN 978-0-262-04630-5).
- Mendelzon & Wood 1995 A. O. Mendelzon, P. T. Wood (1995). Finding regular simple paths in graph databases. SIAM Journal on Computing 24(6):1235–1258.
- Tarjan 1981a R. E. Tarjan (1981). A unified approach to path problems. Journal of the ACM 28(3):577–593.
- Tarjan 1981b R. E. Tarjan (1981). Fast algorithms for solving path problems. Journal of the ACM 28(3):594–614. doi:10.1145/322261.322273.