Private notes
0/8000

Notes stay private to your browser until account sync is configured.

Part 3
15 min read18 headingsSplit lesson page

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

#MistakeWhy It's WrongFix
1Confusing "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.
2Assuming all graphs are simpleReal-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.
3Forgetting that edges in undirected graphs are unorderedWriting (u,v)(u,v) for undirected edges suggests direction. {u,v}={v,u}\{u,v\} = \{v,u\} but (u,v)(v,u)(u,v) \neq (v,u).Use {u,v}\{u,v\} for undirected, (u,v)(u,v) for directed.
4Claiming degree sequence determines the graphMany non-isomorphic graphs share the same degree sequence. Example: C6C_6 and K3,3MK_{3,3} \setminus M (perfect matching removed).Degree sequence is a necessary but not sufficient invariant for isomorphism.
5Assuming bipartite     \iff 2-colorableThese are actually equivalent! The common mistake is failing to recognise this equivalence and testing bipartiteness separately from 2-coloring.Bipartite \Leftrightarrow no odd cycle \Leftrightarrow χ(G)2\chi(G) \leq 2. Use BFS 2-coloring to test bipartiteness.
6Confusing "connected" with "strongly connected" for digraphsA 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.
7Treating 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).Gˉ\bar{G} has edge {u,v}\{u,v\} iff GG does NOT have edge {u,v}\{u,v\}.
8Assuming GNNs can distinguish any two non-isomorphic graphsMessage-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.
9Counting edges in KnK_n as n2n^2KnK_n has (n2)=n(n1)/2\binom{n}{2} = n(n-1)/2 edges, not n2n^2. The adjacency matrix has n2n^2 entries but is symmetric with zero diagonal.Use the formula (n2)\binom{n}{2} for undirected, n(n1)n(n-1) for directed complete graphs.
10Forgetting the handshaking lemma when debugging graph codeIf 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 deg(v)=2E\sum \deg(v) = 2|E| for each. (a) A graph with 5 vertices and 7 edges. (b) A directed graph with 4 vertices - verify deg+=deg=E\sum \deg^+ = \sum \deg^- = |E|. (c) A bipartite graph K3,4K_{3,4} - 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): (4,3,3,2,2)(4, 3, 3, 2, 2), (3,3,3,1)(3, 3, 3, 1), (5,3,2,2,2,2)(5, 3, 2, 2, 2, 2). (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 GG is connected and has exactly n1n - 1 edges, then GG 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 K4K_4 using Kirchhoff's matrix tree theorem (det\det of any cofactor of LL) and verify against Cayley's formula 442=164^{4-2} = 16.

Exercise 5 ** - Bipartiteness Testing

(a) Implement a BFS-based algorithm to test whether a graph is bipartite. (b) If bipartite, output the bipartition (U,W)(U, W). (c) If not bipartite, output an odd cycle as a certificate. (d) Test on: C6C_6 (bipartite), C7C_7 (not bipartite), Petersen (not bipartite), K3,3K_{3,3} (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, rad(G)diam(G)2rad(G)\operatorname{rad}(G) \leq \operatorname{diam}(G) \leq 2 \cdot \operatorname{rad}(G).

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)

ConceptAI Impact
Graph definition G=(V,E)G = (V, E)The foundational data structure for GNNs, knowledge graphs, and computation graphs in all major frameworks (PyTorch Geometric, DGL, JAX-based graph libraries)
Directed graphsComputation graphs (autograd DAGs), causal graphs (structural causal models), citation networks, dependency parse trees
Weighted graphsAttention weights in transformers define weighted digraphs; similarity graphs (kk-NN) use distance weights; Markov chains use transition probabilities
Degree and degree distributionDetermines GNN aggregation balance; power-law distributions cause over-squashing; degree-based normalisation (D1/2AD1/2D^{-1/2}AD^{-1/2}) is standard in GCN
Paths and distanceGNN receptive field is bounded by path length; graph diameter determines minimum GNN depth; shortest-path features improve GNN expressiveness
ConnectivityDisconnected components are processed independently by message-passing GNNs; virtual nodes connect components artificially
Trees and DAGsDecision trees (XGBoost), parse trees (NLP), computation graphs (autograd), Bayesian networks, causal DAGs
Bipartite graphsUser-item recommendation (matrix factorization), bipartite matching (assignment), entity-relation graphs
Graph isomorphismGNN permutation invariance; graph-level classification requires isomorphism-invariant readout functions
Weisfeiler-Leman testProvable upper bound on message-passing GNN expressiveness (Xu et al., 2019); drives research into more expressive architectures (Graph Transformers, subgraph GNNs)
Graph coloringScheduling, resource allocation, register allocation in compilers; CSP solving with neural methods
HypergraphsHigher-order attention (multi-head attention as learned hyperedges); set function learning (DeepSets); group interaction modeling
Planar graphsGeographic 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 AA - the handshaking lemma is 1A1=2E\mathbf{1}^\top A \mathbf{1} = 2|E|, and walk counting uses matrix powers AkA^k
  • Eigenvalues (Ch. 03) will become central in the next section - the spectrum of AA and LL 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 AA, DD, and LL - 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

QuantityFormulaPython (NetworkX)
Total edgesm=12vdeg(v)m = \tfrac{1}{2}\sum_v \deg(v)G.number_of_edges()
Max degreeΔ(G)=maxvdeg(v)\Delta(G) = \max_v \deg(v)max(d for _,d in G.degree())
Min degreeδ(G)=minvdeg(v)\delta(G) = \min_v \deg(v)min(d for _,d in G.degree())
Average degreedˉ=2m/n\bar{d} = 2m/n2*G.number_of_edges()/G.number_of_nodes()
Degree sequenceSorted list of degreessorted([d for _,d in G.degree()], reverse=True)

15.2 Adjacency Matrix Operations

OperationMatrix expressionInterpretation
Degree of viv_i(A1)i(A\mathbf{1})_iRow sum
Number of walks of length kk(Ak)ij(A^k)_{ij}Matrix power
Number of triangles16tr(A3)\tfrac{1}{6}\operatorname{tr}(A^3)Trace of cube
Graph LaplacianL=DAL = D - AD=diag(A1)D = \operatorname{diag}(A\mathbf{1})
Normalised LaplacianLsym=ID1/2AD1/2L_{\text{sym}} = I - D^{-1/2}AD^{-1/2}Eigenvalues in [0,2][0, 2]

15.3 Graph Properties - Decision Table

PropertyAlgorithmTime complexity
ConnectivityBFS/DFS from any vertexO(n+m)O(n + m)
Strong connectivityKosaraju or TarjanO(n+m)O(n + m)
BipartitenessBFS 2-colouringO(n+m)O(n + m)
Euler circuitCheck all degrees even + connectedO(n+m)O(n + m)
Spanning treeBFS/DFS treeO(n+m)O(n + m)
Minimum spanning treeKruskal or PrimO(mlogn)O(m \log n)
Shortest path (unweighted)BFSO(n+m)O(n + m)
Shortest path (weighted)Dijkstra (non-negative)O((n+m)logn)O((n+m)\log n)
Topological sort (DAG)Kahn's algorithmO(n+m)O(n + m)
Planarity testBoyer-MyrvoldO(n)O(n)

15.4 Graph Invariant Checklist

When you need to test whether two graphs G1,G2G_1, G_2 might be isomorphic, check these invariants in order from cheapest to most expensive:

  1. V|V| equal? (O(1))
  2. E|E| equal? (O(1))
  3. Degree sequences equal? (O(n log n))
  4. Degree distribution equal? (O(n))
  5. Number of triangles equal? (O(n^3) or O(n^{2.37}) with fast matrix multiply)
  6. Characteristic polynomial of AA equal? (O(n^3))
  7. Chromatic polynomial equal? (#P-hard in general)
  8. 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, and nx.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 G=(V,E)G = (V, E) 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 deg(v)=2E\sum \deg(v) = 2|E| 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 nm+f=2n - m + f = 2 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 G(n,p)G(n,p) 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

SymbolMeaning
G=(V,E)G = (V, E)Graph with vertex set VV and edge set EE
n=Vn = \lvert V \rvertOrder (number of vertices)
m=Em = \lvert E \rvertSize (number of edges)
uvu \sim vVertices uu and vv are adjacent
N(v)\mathcal{N}(v)Neighbourhood of vv: all vertices adjacent to vv
N[v]\mathcal{N}[v]Closed neighbourhood: N(v){v}\mathcal{N}(v) \cup \{v\}
deg(v)\deg(v)Degree of vertex vv in an undirected graph
deg+(v)\deg^+(v)Out-degree of vv in a digraph
deg(v)\deg^-(v)In-degree of vv in a digraph
δ(G)\delta(G)Minimum degree: minvVdeg(v)\min_{v \in V}\deg(v)
Δ(G)\Delta(G)Maximum degree: maxvVdeg(v)\max_{v \in V}\deg(v)
d(u,v)d(u, v)Graph distance (shortest path length) from uu to vv
diam(G)\operatorname{diam}(G)Diameter: maxu,vd(u,v)\max_{u,v} d(u,v)
rad(G)\operatorname{rad}(G)Radius: minvmaxud(u,v)\min_v \max_u d(u,v)
χ(G)\chi(G)Chromatic number
κ(G)\kappa(G)Vertex connectivity
λ(G)\lambda(G)Edge connectivity
G[S]G[S]Induced subgraph on vertex subset SS
GvG - vGraph with vertex vv and its incident edges removed
GeG - eGraph with edge ee removed
G/eG / eGraph with edge ee contracted
Gˉ\bar{G}Complement of GG
L(G)L(G)Line graph of GG
Aut(G)\operatorname{Aut}(G)Automorphism group of GG

A.2 Special Graph Families

SymbolNameDefinition
KnK_nComplete graphnn vertices, all (n2)\binom{n}{2} edges
Km,nK_{m,n}Complete bipartitem+nm + n vertices, mnmn edges in bipartite form
CnC_nCyclenn vertices in a single cycle
PnP_nPathnn vertices in a single path
Kˉn\bar{K}_nEmpty graphnn vertices, no edges
SnS_nStarOne center vertex connected to n1n-1 leaves
WnW_nWheelCnC_n plus one central vertex connected to all
QnQ_nHypercube2n2^n vertices, nn-bit binary strings as vertices

A.3 Graph Matrices

MatrixSymbolDefinitionSize
AdjacencyAAAij=1A_{ij} = 1 if {i,j}E\{i,j\} \in En×nn \times n
DegreeDDD=diag(deg(v1),,deg(vn))D = \operatorname{diag}(\deg(v_1), \ldots, \deg(v_n))n×nn \times n diagonal
LaplacianLLL=DAL = D - An×nn \times n
Normalised LaplacianLsymL_{\text{sym}}ID1/2AD1/2I - D^{-1/2}AD^{-1/2}n×nn \times n
IncidenceBBBve=1B_{ve} = 1 if vev \in e (undirected)n×mn \times m
DistanceD\mathcal{D}Dij=d(vi,vj)\mathcal{D}_{ij} = d(v_i, v_j)n×nn \times n

Appendix B: Key Theorems Summary

TheoremStatementSignificance
Handshaking Lemmavdeg(v)=2E\sum_{v}\deg(v) = 2\lvert E \rvertFundamental constraint; useful for validation
Euler's TheoremEulerian circuit     \iff all degrees evenFirst theorem of graph theory (1736)
Odd-Cycle TheoremBipartite     \iff no odd cyclesConnects structure to coloring
Tree Equivalences6 equivalent definitions of a treeFoundational for spanning trees
Cayley's Formulann2n^{n-2} spanning trees in KnK_nCounts labeled trees
Matrix Tree TheoremSpanning trees = any cofactor of LLConnects combinatorics to linear algebra
Euler's Planar Formulanm+f=2n - m + f = 2Topological constraint on planar graphs
Kuratowski's TheoremPlanar     \iff no K5K_5 or K3,3K_{3,3} subdivisionCharacterises planarity
Four Color Theoremχ(G)4\chi(G) \leq 4 for planar GGComputer-assisted proof (1976/2008)
Brooks' Theoremχ(G)Δ(G)\chi(G) \leq \Delta(G) (non-complete, non-odd-cycle)Upper bound on chromatic number
Konig's TheoremMax matching = min vertex cover (bipartite)Foundation of bipartite combinatorics
Menger's TheoremMax disjoint paths = min cutMax-flow min-cut for graphs
WL-GNN EquivalenceMPNN power \leq 1-WL test (Xu et al., 2019)Expressiveness bound for GNNs
Robertson-SeymourEvery minor-closed property has finite forbidden setDeepest result in structural graph theory

Appendix C: Common Graph Families - Properties at a Glance

GraphnnmmRegular?Bipartite?Planar?χ\chiDiameter
KnK_nnnn(n1)/2n(n{-}1)/2(n1)(n{-}1)-regn2n \leq 2n4n \leq 4nn11
Kn,nK_{n,n}2n2nn2n^2nn-regYesn2n \leq 22222
CnC_nnnnn22-regnn evenYesn/2\lfloor n/2 \rfloorn/2\lfloor n/2 \rfloor
PnP_nnnn1n{-}1NoYesYes22n1n{-}1
Tree (any)nnn1n{-}1No (gen.)YesYes22n1\leq n{-}1
Petersen1010151533-regNoNo3322
QnQ_n (hypercube)2n2^nn2n1n2^{n-1}nn-regYesn3n \leq 322nn

Skill Check

Test this lesson

Answer 4 quick questions to lock in the lesson and feed your adaptive practice queue.

--
Score
0/4
Answered
Not attempted
Status
1

Which module does this lesson belong to?

2

Which section is covered in this lesson content?

3

Which term is most central to this lesson?

4

What is the best way to use this lesson for real learning?

Your answers save locally first, then sync when account storage is available.
Practice queue