Architecture Verification: Turning a Diagram into a Test
How a two-step graph check can verify an architecture automatically, what to do when it fails, and how volatility numbers can suggest layer boundaries.
You have drawn a dependency diagram on a whiteboard. Somebody asked, "Is that actually what the code does?", and nobody could say for sure. Chapter 3 of the book answers by treating the diagram as what it really is, a directed graph, and borrowing the handful of ideas from graph theory that let you check it with a program instead of an opinion.
The vocabulary takes a minute to learn. A component is any unit you care about: a package, a class, a file, even a single function. A dependency is a directed edge from one component to another, meaning "the first needs the second". Put every component and every edge together and you have the system's dependency graph.
Edges come in three flavours, which the book distinguishes. A compile-time dependency means one component refers to types or functions defined in another. A runtime dependency means it calls or instantiates something from the other. A logical dependency is the sneaky kind: one component simply needs the other for correct behaviour, with no import statement to show it. In practice, tools see the first kind easily, the second with effort and the third hardly at all, which is worth bearing in mind when someone says "we have no dependencies on that".
Here is a tiny graph for an imaginary ride-sharing app, written as an adjacency list:
RideScreen -> RideService
RideService -> FareCalc, GeoClient, RideTypes
FareCalc -> RideTypes
GeoClient -> RideTypes
Everything else in the chapter is a question you can ask about a picture like this one.
Reachability. One component reaches another if you can follow edges from the first to the second. The book defines it so that a component reaches itself only through a genuine loop, which makes "does anything reach itself?" a direct test for cycles.
The directed acyclic graph (DAG). A DAG is a directed graph with no cycles. The ride-sharing graph above is one. Add an edge from RideTypes back to RideService and it stops being one, which tells you something is wrong with the design.
Topological ordering. This is a way of lining up the components so that every edge points one way. The book states a theorem that a graph is acyclic exactly when such an ordering exists. For architects the useful reading is that a valid build order exists if and only if there are no cycles. Because edges here mean "depends on", the order you actually build in runs the other way, dependencies first.
Strongly connected components. A group of components that can all reach each other forms one such component. Every graph splits cleanly into them, and any group with more than one member is a cycle you will need to untangle. This is how you find where the tangles are, not just whether one exists.
Where do the edges come from? The book's answer is static analysis: read every source file, note the component it defines, and record each import as an edge. Dependencies exist at several granularities, and it recommends using several. Look at modules for high-level architecture, classes for detailed design and functions when you are testing or refactoring. The cost grows with the number of files and the average number of imports per file, so extraction stays cheap even for large repositories.
The book also notes how big real graphs get: components in the thousands to the hundreds of thousands, with dependencies up to the millions. That scale explains a requirement that runs through the chapter, which is that algorithms must run in time proportional to components plus dependencies. Everything it recommends meets that bar. It also lists three ways to hold a graph in memory, a matrix, an adjacency list or a plain list of edges, and observes that dense graphs favour the matrix and sparse ones the list. Software graphs are usually sparse.
You do not need to write the extractor yourself. The chapter surveys existing tools: Madge for JavaScript and TypeScript, JDepend and ArchUnit for Java, import-linter for Python, plus commercial products such as Structure101, NDepend and SonarQube. For LVD specifically it points forward to lvd-lint, which checks dependencies with layers in mind; see lvd-lint: enforcing layer rules automatically.
None of this requires advanced mathematics. It requires seeing your system as nodes and arrows, then noticing that a handful of well-understood graph questions, including loops, build order and tangles, have fast, mechanical answers. The next articles put these ideas to work: detecting cycles and turning a diagram into a test. If a term is unfamiliar, the glossary has short definitions.