Lesson overview | Previous part | Lesson overview
Graph Basics: Part 11: Common Mistakes to Appendix C: Common Graph Families - Properties at a Glance
11. Common Mistakes
| # | Mistake | Why It's Wrong | Fix |
|---|---|---|---|
| 1 | Confusing "path" and "walk" | A walk allows vertex repetition; a path does not. Claiming a walk is a path overstates the constraint. | Use "walk" for general traversals, "path" only when no vertex is repeated. |
| 2 | Assuming all graphs are simple | Real-world graphs often have self-loops (user follows self) or parallel edges (multiple bus routes). GCN explicitly adds self-loops. | State "simple graph" explicitly when assuming no self-loops or parallel edges. |
| 3 | Forgetting that edges in undirected graphs are unordered | Writing for undirected edges suggests direction. but . | Use for undirected, for directed. |
| 4 | Claiming degree sequence determines the graph | Many non-isomorphic graphs share the same degree sequence. Example: and (perfect matching removed). | Degree sequence is a necessary but not sufficient invariant for isomorphism. |
| 5 | Assuming bipartite 2-colorable | These are actually equivalent! The common mistake is failing to recognise this equivalence and testing bipartiteness separately from 2-coloring. | Bipartite no odd cycle . Use BFS 2-coloring to test bipartiteness. |
| 6 | Confusing "connected" with "strongly connected" for digraphs | A weakly connected digraph may have vertex pairs with no directed path between them. Strong connectivity requires directed paths in both directions. | Always specify "weakly" or "strongly" for directed graphs. |
| 7 | Treating graph complement as "flipping edges" | The complement removes existing edges AND adds all missing edges. It's not "toggle each edge" unless you mean exactly that (which IS the complement). | has edge iff does NOT have edge . |
| 8 | Assuming GNNs can distinguish any two non-isomorphic graphs | Message-passing GNNs are bounded by 1-WL. They fail on regular graphs of the same degree and many other cases. | Use 1-WL as the upper bound. For tasks requiring more power, consider higher-order GNNs or graph transformers. |
| 9 | Counting edges in as | has edges, not . The adjacency matrix has entries but is symmetric with zero diagonal. | Use the formula for undirected, for directed complete graphs. |
| 10 | Forgetting the handshaking lemma when debugging graph code | If your computed degree sum is odd, the graph is invalid - odd degree sums are impossible. | Always verify $\sum \deg(v) = 2 |
12. Exercises
Exercise 1 * - Handshaking Lemma Verification
Construct three specific graphs (your choice of vertex/edge sets) and verify the handshaking lemma for each. (a) A graph with 5 vertices and 7 edges. (b) A directed graph with 4 vertices - verify . (c) A bipartite graph - compute degrees of both sides.
Exercise 2 * - Degree Sequences and Graphicality
(a) Determine whether each sequence is graphic (can be realised as a simple graph): , , . (b) For each graphic sequence, construct a graph that realises it. (c) Implement the Erdos-Gallai test programmatically.
Exercise 3 * - Paths, Cycles, and Distance
Given the Petersen graph (look up its edge list): (a) Find all shortest paths from vertex 0 to vertex 5. (b) Compute the diameter and radius. (c) Verify the girth is 5 by finding a shortest cycle. (d) Show that the Petersen graph has no Hamiltonian cycle (explain why, or try exhaustive search).
Exercise 4 ** - Tree Equivalences
(a) Prove that if is connected and has exactly edges, then is acyclic. (b) Prove that in a tree, there is exactly one path between every pair of vertices. (c) Compute the number of spanning trees of using Kirchhoff's matrix tree theorem ( of any cofactor of ) and verify against Cayley's formula .
Exercise 5 ** - Bipartiteness Testing
(a) Implement a BFS-based algorithm to test whether a graph is bipartite. (b) If bipartite, output the bipartition . (c) If not bipartite, output an odd cycle as a certificate. (d) Test on: (bipartite), (not bipartite), Petersen (not bipartite), (bipartite).
Exercise 6 ** - Graph Distance and Eccentricity
(a) Compute the all-pairs shortest path distance matrix for a given graph using BFS. (b) From the distance matrix, compute eccentricity, diameter, radius, and center. (c) Verify: for any connected graph, .
Exercise 7 *** - Weisfeiler-Leman Test and GNN Expressiveness
(a) Implement the 1-WL color refinement algorithm. (b) Run it on two non-isomorphic 3-regular graphs on 6 vertices. Does 1-WL distinguish them? (c) Construct a pair of non-isomorphic graphs that 1-WL CANNOT distinguish (hint: use two regular graphs with the same degree sequence and spectrum). (d) Explain why this means a standard message-passing GNN cannot distinguish them either.
Exercise 8 *** - Real-World Graph Analysis
Model a real-world system as a graph and analyse it: (a) Choose a domain: social network (use Zachary's karate club), citation network, molecular graph, or small knowledge graph. (b) Compute: number of vertices/edges, degree distribution, diameter, connected components, clustering coefficient. (c) Test whether the graph is bipartite, planar, or has any special structure. (d) Discuss: what would a 2-layer GNN "see" on this graph? Which pairs of vertices can exchange information?
13. Why This Matters for AI (2026 Perspective)
| Concept | AI Impact |
|---|---|
| Graph definition | The foundational data structure for GNNs, knowledge graphs, and computation graphs in all major frameworks (PyTorch Geometric, DGL, JAX-based graph libraries) |
| Directed graphs | Computation graphs (autograd DAGs), causal graphs (structural causal models), citation networks, dependency parse trees |
| Weighted graphs | Attention weights in transformers define weighted digraphs; similarity graphs (-NN) use distance weights; Markov chains use transition probabilities |
| Degree and degree distribution | Determines GNN aggregation balance; power-law distributions cause over-squashing; degree-based normalisation () is standard in GCN |
| Paths and distance | GNN receptive field is bounded by path length; graph diameter determines minimum GNN depth; shortest-path features improve GNN expressiveness |
| Connectivity | Disconnected components are processed independently by message-passing GNNs; virtual nodes connect components artificially |
| Trees and DAGs | Decision trees (XGBoost), parse trees (NLP), computation graphs (autograd), Bayesian networks, causal DAGs |
| Bipartite graphs | User-item recommendation (matrix factorization), bipartite matching (assignment), entity-relation graphs |
| Graph isomorphism | GNN permutation invariance; graph-level classification requires isomorphism-invariant readout functions |
| Weisfeiler-Leman test | Provable upper bound on message-passing GNN expressiveness (Xu et al., 2019); drives research into more expressive architectures (Graph Transformers, subgraph GNNs) |
| Graph coloring | Scheduling, resource allocation, register allocation in compilers; CSP solving with neural methods |
| Hypergraphs | Higher-order attention (multi-head attention as learned hyperedges); set function learning (DeepSets); group interaction modeling |
| Planar graphs | Geographic ML, mesh-based physics simulations (weather prediction, fluid dynamics with GNNs) |
14. Conceptual Bridge
Looking Back
This section builds on the mathematical foundations established in earlier chapters:
- Sets and functions (Ch. 01) provide the language for defining vertex sets, edge sets, and the mappings (isomorphisms, colourings) between them
- Matrix operations (Ch. 02) connect through the adjacency matrix - the handshaking lemma is , and walk counting uses matrix powers
- Eigenvalues (Ch. 03) will become central in the next section - the spectrum of and encodes deep structural information about the graph
Looking Forward
The vocabulary and theory developed here is the foundation for the rest of the Graph Theory chapter:
- 02 Graph Representations takes the abstract objects defined here (adjacency, degree, connectivity) and asks: how do we store them in memory?
- 03 Graph Algorithms provides efficient algorithms for computing the properties defined here: BFS for distance, DFS for connectivity, Dijkstra for weighted shortest paths
- 04 Spectral Graph Theory analyses the eigenvalues of , , and - connecting the combinatorial properties (connectivity, bipartiteness, clustering) to algebraic properties (spectrum)
- 05 Graph Neural Networks builds learnable functions on graphs, with the message-passing paradigm directly implementing walk-based aggregation on the structures defined here
- 06 Random Graphs asks: what happens when edges are drawn randomly? The degree sequences, connectivity thresholds, and component structure we defined here become probabilistic phenomena
The Big Picture
GRAPH BASICS IN THE CURRICULUM
========================================================================
Ch.01 Sets & Logic -----------> Vertex sets, edge sets, mappings
|
Ch.02 Linear Algebra ---------> Adjacency matrix, degree matrix
|
v
+---------------------------------------------------------+
| 01 GRAPH BASICS |
| Definitions * Degree * Paths * Connectivity * Trees |
| Bipartite * Coloring * Isomorphism * WL Test |
+-------+-----------+-----------+-----------+-------------+
| | | |
v v v v
02 03 04 05
Represent. Algorithms Spectral GNNs
(storage) (BFS, DFS) (eigenvals) (learning)
|
v
06 Random Graphs
(probabilistic)
========================================================================
15. Quick Computational Reference
The table below collects the key formulas and algorithms from this section for fast lookup during implementation.
15.1 Degree and Edge Counting
| Quantity | Formula | Python (NetworkX) |
|---|---|---|
| Total edges | G.number_of_edges() | |
| Max degree | max(d for _,d in G.degree()) | |
| Min degree | min(d for _,d in G.degree()) | |
| Average degree | 2*G.number_of_edges()/G.number_of_nodes() | |
| Degree sequence | Sorted list of degrees | sorted([d for _,d in G.degree()], reverse=True) |
15.2 Adjacency Matrix Operations
| Operation | Matrix expression | Interpretation |
|---|---|---|
| Degree of | Row sum | |
| Number of walks of length | Matrix power | |
| Number of triangles | Trace of cube | |
| Graph Laplacian | ||
| Normalised Laplacian | Eigenvalues in |
15.3 Graph Properties - Decision Table
| Property | Algorithm | Time complexity |
|---|---|---|
| Connectivity | BFS/DFS from any vertex | |
| Strong connectivity | Kosaraju or Tarjan | |
| Bipartiteness | BFS 2-colouring | |
| Euler circuit | Check all degrees even + connected | |
| Spanning tree | BFS/DFS tree | |
| Minimum spanning tree | Kruskal or Prim | |
| Shortest path (unweighted) | BFS | |
| Shortest path (weighted) | Dijkstra (non-negative) | |
| Topological sort (DAG) | Kahn's algorithm | |
| Planarity test | Boyer-Myrvold |
15.4 Graph Invariant Checklist
When you need to test whether two graphs might be isomorphic, check these invariants in order from cheapest to most expensive:
- equal? (O(1))
- equal? (O(1))
- Degree sequences equal? (O(n log n))
- Degree distribution equal? (O(n))
- Number of triangles equal? (O(n^3) or O(n^{2.37}) with fast matrix multiply)
- Characteristic polynomial of equal? (O(n^3))
- Chromatic polynomial equal? (#P-hard in general)
- Run VF2 or Nauty for exact isomorphism check.
If any invariant differs, the graphs are definitively not isomorphic. If all agree, they are likely isomorphic but not guaranteed (cospectral non-isomorphic graphs exist).
Implementation note. In practice, NetworkX provides
nx.is_isomorphic(G1, G2)using VF2, andnx.graph_atlas_g()for enumeration. For large graphs ($n > 10^4$), use approximate fingerprinting (degree sequence + triangle count + eigenvalue moments) before exact checks.
16. Section Summary
This section covered the full vocabulary of graph theory needed to read modern GNN papers and implement graph algorithms from scratch.
Core objects defined:
- A graph is a pair of a vertex set and an edge set; variants include directed, weighted, simple, multi, and hypergraphs.
- The degree of a vertex counts its incident edges; the handshaking lemma is the universal sanity check.
- Walks, trails, paths, and cycles form a hierarchy of traversal objects; Eulerian and Hamiltonian structures live at opposite ends of algorithmic tractability.
Core structural theorems:
- Connectivity is characterised by BFS/DFS reachability; bridges and articulation points are the fragile points; Menger's theorem equates disjoint paths with cuts.
- Trees admit six equivalent definitions and are the minimum connected structures; spanning trees compress any connected graph.
- Bipartite graphs are exactly those with no odd cycles, equivalent to 2-colourability, and support Konig's matching theorem.
- Planar graphs satisfy Euler's formula and are characterised by Kuratowski's forbidden minors.
Core AI connections:
- The Weisfeiler-Leman test (1-WL) is the provable upper bound on the expressiveness of all message-passing GNNs, showing that structural indistinguishability in WL implies indistinguishability by GNNs.
- Graph motifs (triangles, stars, paths) are the structural units that standard GNNs cannot count, motivating expressiveness research.
- Graph isomorphism, automorphisms, and invariants formalise what it means for a graph function to be permutation-invariant - the foundational requirement for graph-level prediction.
The theory notebook works through all matrix computations, proofs, and visualisations in executable Python. The exercises notebook provides graded practice from basic degree calculations to proving WL indistinguishability of specific graph pairs.
The single most important takeaway: graphs are simultaneously combinatorial objects (studied via degree, paths, cycles), algebraic objects (studied via matrices and polynomials), and computational objects (studied via algorithms and complexity). GNNs sit at the intersection of all three perspectives - they are learned functions that respect the algebraic symmetries of graphs while computing efficiently on the combinatorial structure. Every concept in this section reappears in that context.
17. Further Reading
Foundational Texts
- Diestel, R. Graph Theory (5th ed., 2017) - The standard graduate reference. Free PDF at diestel-graph-theory.com. Chapters 1-2 cover the material in this section with full proofs.
- West, D. Introduction to Graph Theory (2nd ed., 2001) - Excellent undergraduate text with many exercises. Chapters 1 (Fundamental Concepts) and 2 (Trees and Distance) map directly to 01.
- Bondy, J. A. and Murty, U. S. R. Graph Theory (2008, Springer GTM) - Comprehensive coverage including extremal graph theory and Ramsey theory.
For the AI/ML Connection
- Hamilton, W. L. Graph Representation Learning (2020, Synthesis Lectures) - The definitive ML-focused introduction. Chapter 2 covers graph basics from an ML perspective. Free draft at cs.mcgill.ca/~wlh/grl_book/.
- Xu, K. et al. "How Powerful are Graph Neural Networks?" ICLR 2019 - Establishes the 1-WL upper bound on message-passing GNNs. Essential reading before implementing GNNs.
- Bronstein, M. et al. "Geometric Deep Learning: Grids, Groups, Graphs, Geodesics, and Gauges" arXiv 2021 - Places graphs in the broader context of geometric priors in ML (symmetry, equivariance).
Historical Papers
- Euler, L. "Solutio problematis ad geometriam situs pertinentis" (1736) - The Konigsberg bridges paper: the first graph theory result.
- Erdos, P. and Renyi, A. "On Random Graphs I" Publicationes Mathematicae (1959) - Founding paper of random graph theory; introduces the model studied in 06.
- Appel, K. and Haken, W. "Every planar map is four colorable" Illinois J. Math. (1977) - The first major computer-assisted proof in mathematics.
<- Back to Graph Theory | Next: Graph Representations ->
Appendix A: Graph Theory Notation Reference
A quick-reference table for the notation used throughout this section and the rest of the chapter.
A.1 Graph Notation
| Symbol | Meaning |
|---|---|
| Graph with vertex set and edge set | |
| Order (number of vertices) | |
| Size (number of edges) | |
| Vertices and are adjacent | |
| Neighbourhood of : all vertices adjacent to | |
| Closed neighbourhood: | |
| Degree of vertex in an undirected graph | |
| Out-degree of in a digraph | |
| In-degree of in a digraph | |
| Minimum degree: | |
| Maximum degree: | |
| Graph distance (shortest path length) from to | |
| Diameter: | |
| Radius: | |
| Chromatic number | |
| Vertex connectivity | |
| Edge connectivity | |
| Induced subgraph on vertex subset | |
| Graph with vertex and its incident edges removed | |
| Graph with edge removed | |
| Graph with edge contracted | |
| Complement of | |
| Line graph of | |
| Automorphism group of |
A.2 Special Graph Families
| Symbol | Name | Definition |
|---|---|---|
| Complete graph | vertices, all edges | |
| Complete bipartite | vertices, edges in bipartite form | |
| Cycle | vertices in a single cycle | |
| Path | vertices in a single path | |
| Empty graph | vertices, no edges | |
| Star | One center vertex connected to leaves | |
| Wheel | plus one central vertex connected to all | |
| Hypercube | vertices, -bit binary strings as vertices |
A.3 Graph Matrices
| Matrix | Symbol | Definition | Size |
|---|---|---|---|
| Adjacency | if | ||
| Degree | diagonal | ||
| Laplacian | |||
| Normalised Laplacian | |||
| Incidence | if (undirected) | ||
| Distance |
Appendix B: Key Theorems Summary
| Theorem | Statement | Significance |
|---|---|---|
| Handshaking Lemma | Fundamental constraint; useful for validation | |
| Euler's Theorem | Eulerian circuit all degrees even | First theorem of graph theory (1736) |
| Odd-Cycle Theorem | Bipartite no odd cycles | Connects structure to coloring |
| Tree Equivalences | 6 equivalent definitions of a tree | Foundational for spanning trees |
| Cayley's Formula | spanning trees in | Counts labeled trees |
| Matrix Tree Theorem | Spanning trees = any cofactor of | Connects combinatorics to linear algebra |
| Euler's Planar Formula | Topological constraint on planar graphs | |
| Kuratowski's Theorem | Planar no or subdivision | Characterises planarity |
| Four Color Theorem | for planar | Computer-assisted proof (1976/2008) |
| Brooks' Theorem | (non-complete, non-odd-cycle) | Upper bound on chromatic number |
| Konig's Theorem | Max matching = min vertex cover (bipartite) | Foundation of bipartite combinatorics |
| Menger's Theorem | Max disjoint paths = min cut | Max-flow min-cut for graphs |
| WL-GNN Equivalence | MPNN power 1-WL test (Xu et al., 2019) | Expressiveness bound for GNNs |
| Robertson-Seymour | Every minor-closed property has finite forbidden set | Deepest result in structural graph theory |
Appendix C: Common Graph Families - Properties at a Glance
| Graph | Regular? | Bipartite? | Planar? | Diameter | |||
|---|---|---|---|---|---|---|---|
| -reg | |||||||
| -reg | Yes | ||||||
| -reg | even | Yes | |||||
| No | Yes | Yes | |||||
| Tree (any) | No (gen.) | Yes | Yes | ||||
| Petersen | -reg | No | No | ||||
| (hypercube) | -reg | Yes |