Foundations to Frontier
Tests
A. Mathematical Foundations › A11 Graph Theory and Discrete Math

Trees, DAGs and Topological Sort

Trees are the minimal connected graphs: n − 1 edges, no cycles, one path between any two vertices. Directed acyclic graphs (DAGs) encode dependencies: build systems, task schedules, neural network computation graphs, Bayesian networks. This topic covers tree characterizations, minimum spanning trees by Kruskal and Prim (and why greed works), counting spanning trees with Kirchhoff's matrix-tree theorem, topological sorting with cycle detection, and longest paths in DAGs. In the research exercise you run the critical path method on a spacecraft integration-and-test campaign and see why speeding up one task may barely shorten the schedule.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Branches without loops, and things that must come first

A family tree branches out but never loops back. That’s a tree: everything is connected, with exactly one route between any two members. A recipe is different: you must chop before you fry and boil water before adding pasta. Its steps form a DAG (directed acyclic graph), arrows with no circles. Sorting the steps so every “must come first” is respected is called topological sorting.

Building a minimum spanning tree (Kruskal)
components 1 · tree edges 13/13 · tree weight 2.034 · done: minimum spanning tree
Kruskal sorts all edges by length and adds each one unless it would close a cycle, which a union-find structure detects in near-constant time. By the cut property, the cheapest edge crossing any split of the vertices is always safe to add, so the greedy result is optimal. A tree on n points always has exactly n − 1 edges and one unique path between any two points: the cheapest way to network a set of rovers, sensors or ground stations.
Why engineers care

Spanning trees give the cheapest way to connect sensors, rovers or ground stations. DAGs schedule spacecraft assembly and test campaigns, order the operations in a neural network’s backward pass, describe Bayesian networks, and drive build systems and mission command sequences. A dependency cycle in a schedule means the plan is impossible, so it must be detected.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A tree with 10 vertices has how many edges?
  2. 2. A DAG cannot contain…
  3. 3. Topological order means…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Equivalent definitions of a tree

For a graph on n vertices, the following are equivalent:

  1. connected and acyclic;
  2. connected with n − 1 edges;
  3. acyclic with n − 1 edges;
  4. exactly one path between every pair;
  5. minimally connected (removing any edge disconnects it).

A forest is a disjoint union of trees. A spanning tree of G is a tree using all of G’s vertices.

DAGs

A directed graph is acyclic iff it has a topological order: a numbering in which every arc goes from a lower to a higher number. Kahn’s algorithm repeatedly removes vertices with in-degree 0. If vertices remain, there’s a cycle.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Adding any edge to a tree creates…
  2. 2. Kahn's algorithm starts with vertices of…
  3. 3. If Kahn's algorithm can't output all vertices, the graph…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

Minimum spanning trees

  • Kruskal: sort the edges and add each unless it closes a cycle (union-find). O(mlog⁡m)O(m\log m).
  • Prim: grow one tree, always adding the cheapest edge leaving it (heap). O(mlog⁡n)O(m\log n).

Both are correct because of the cut property: for any partition of the vertices, the lightest crossing edge belongs to some MST. MSTs also give a 2-approximation for metric TSP and the basis of single-linkage clustering.

Counting spanning trees (Kirchhoff)

τ(G)=det⁡(L with any row and column removed)=1n∏k≥2λk\tau(G) = \det(L\text{ with any row and column removed}) = \tfrac1n\prod_{k\ge2}\lambda_k

For KnK_n, this gives Cayley’s formula nn−2n^{n-2}.

Longest paths and scheduling in DAGs

In a DAG, longest (and shortest) paths come from a single pass in topological order: EF(v)=max⁡u→vEF(u)+dv\mathrm{EF}(v) = \max_{u\to v}\mathrm{EF}(u) + d_v. The critical path method adds a backward pass for the latest start times. Slack = latest start − earliest start, and tasks with zero slack form the critical path.

🐍 Exercise 1: spanning trees and topological order
Implement Kruskal (union-find) and Prim (heap) and check they agree on the MST of 30 random points. Implement Kahn's topological sort with cycle detection, and count spanning trees with Kirchhoff's theorem, checking Cayley's formula.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Why can Kruskal be greedy and still optimal?
  2. 2. Spanning trees of K₄:
  3. 3. Tasks with zero slack are…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Cut property

Let e be the lightest edge across a cut (S,Sˉ)(S, \bar S), and T an MST without e. Adding e to T creates a cycle, which must cross the cut through another edge f with w(f)≥w(e)w(f)\ge w(e). Then T+e−fT + e - f is a spanning tree no heavier than T. ∎

Topological order exists iff acyclic

If there’s a cycle, its vertices can’t all precede each other. Conversely, a finite DAG has a vertex of in-degree 0 (otherwise following arcs backward would cycle). Remove it and recurse. ∎

Why n − 1 edges

Induction: a tree has a leaf (take the end of a longest path). Removing it leaves a tree with n − 1 vertices. ∎

Exercise 3.1: spanning trees of a 4-cycle via Kirchhoff

The cycle Laplacian’s eigenvalues are 0, 2, 2, 4. So τ=14⋅2⋅2⋅4=4\tau = \frac14\cdot2\cdot2\cdot4 = 4. Removing any one of the 4 edges gives a spanning path.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The cut-property proof exchanges the cut edge with…
  2. 2. Every finite DAG has a vertex with…
  3. 3. Spanning trees of the 4-cycle:

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Critical path of a spacecraft I&T campaign

A small spacecraft’s integration and test campaign has 12 tasks: structure, harness, avionics, propulsion, payload, bus integration, payload integration, functional test, thermal vacuum, vibration, review and shipping. The dependencies form a DAG. CPM gives an 88-day campaign with critical path structure → propulsion → bus → payload integration → functional test → thermal vacuum → review → ship. Avionics has 13 days of slack, payload 18 and vibration 12. Cutting thermal vacuum from 21 to 5 days saves only 12 days, because vibration (9 days) becomes critical. Project managers and space agencies use exactly this analysis for launch-readiness planning, and the same topological ordering underlies autonomous onboard schedulers.

🐍 Exercise 2 (research): critical path of an integration-and-test campaign
Topologically sort a spacecraft I&T task DAG (detecting circular dependencies). Compute earliest and latest starts, slack and the critical path, then show how shortening the bottleneck task shifts the critical path.

Trees and DAGs in AI, robotics and aerospace

  • Computation graphs: autodiff executes a DAG in topological order forward and in reverse for gradients (a8-5).
  • Planning and scheduling: CPM/PERT for missions; temporal networks in onboard planners (e.g. EUROPA, MEXEC).
  • Probabilistic models: Bayesian networks are DAGs; junction trees enable exact inference.
  • Networks and perception: MSTs for sensor-network backbones and minimal wiring; single-linkage clustering of point clouds; RRT motion planners grow trees.

References

  1. J. Kruskal (1956). “On the shortest spanning subtree of a graph and the traveling salesman problem.” Proc. AMS, 7.
  2. R. Prim (1957). “Shortest connection networks and some generalizations.” Bell System Technical Journal, 36(6).
  3. A. Kahn (1962). “Topological sorting of large networks.” Communications of the ACM, 5(11).
  4. G. Kirchhoff (1847). “Über die Auflösung der Gleichungen…” Annalen der Physik, 148 (matrix-tree theorem).
  5. J. Kelley, M. Walker (1959). “Critical-path planning and scheduling.” Proc. Eastern Joint Computer Conference.
  6. T. Cormen, C. Leiserson, R. Rivest, C. Stein (2022). Introduction to Algorithms, 4th ed. MIT Press. Ch. 20–21.
  7. NASA (2016). NASA Systems Engineering Handbook, NASA/SP-2016-6105 Rev 2.
  8. S. LaValle (1998). “Rapidly-exploring random trees: a new tool for path planning.” TR 98-11, Iowa State University.