Graph Types & Master Taxonomy
Formal graph definitions (V, E), directed vs undirected, weighted, cyclic/acyclic, bipartite verification, and Handshaking Lemma.
Graphs model arbitrary non-linear relational topology without the hierarchical constraints of trees, capturing asymmetric dependencies, cyclic flows, and multidimensional networks. Master the mathematical invariants—from Euler's Handshaking Lemma and directed degree balances to the Odd Cycle Bipartiteness Theorem—that dictate every downstream graph algorithm.
1. Executive Summary & Learning Objectives
#Originating with Leonhard Euler's 1736 resolution of the Seven Bridges of Königsberg problem, Graph Theory formalizes pairwise relations between objects. A graph consists of a set of vertices connected by edges . Unlike trees, graphs permit arbitrary cycles, multiple disjoint components, and asymmetric traversals.
By the end of this chapter, you will be able to:
- Apply Euler's Handshaking Lemma to prove vertex degree parity invariants in undirected and directed networks.
- Differentiate directed, undirected, weighted, and unweighted graphs across mathematical formalisms and algorithmic constraints.
- Formulate the structural conditions of Directed Acyclic Graphs (DAGs) and their role as topological dependency engines.
- Prove Kőnig's Odd Cycle Theorem and verify graph bipartiteness via 2-color BFS/DFS in time.
- Classify special topological families—including Complete (), Planar (), Eulerian (), and Hamiltonian (NP-Complete) graphs.
2. Graph Anatomy & The Handshaking Lemma
#A graph is defined by:
- A finite vertex set .
- An edge set , where each edge connects a pair of vertices .
Structural Comparison: Trees vs. General Graphs
#| Architectural Dimension | Tree () | General Graph () |
|---|---|---|
| Root & Hierarchy | Strict single root; parent-child hierarchy | No root; arbitrary peer relationships |
| Path Uniqueness | Exactly one unique simple path between any two nodes | Zero, one, or exponentially many paths |
| Cycles & Loops | Strictly acyclic () | May contain self-loops, parallel edges, and cycles |
| Connectivity | Always fully connected in a single component | May consist of multiple disconnected components |
Theorem: Euler's Handshaking Lemma (1736)
#In any undirected graph , the sum of the degrees of all vertices equals exactly twice the number of edges:
Formal Proof
- Each individual edge has exactly two endpoints ( and ).
- When summing vertex degrees , every incident edge contributes to and to .
- Because every edge is counted exactly twice, the sum equals .
Corollary: The Odd Degree Parity Invariant
In any undirected graph, the count of vertices having an odd degree must be even.
Proof: Partition into vertices with even degrees () and odd degrees ():
The total is even, and is even. Thus, must be an even number. The sum of odd integers is even if and only if the number of terms is even. Hence, is even.
3. Directed vs. Undirected Graphs
#| Structural Property | Undirected Graph | Directed Graph (Digraph) |
|---|---|---|
| Edge Representation | Unordered pair | Ordered pair |
| Traversal Semantic | Bidirectional two-way corridor | Asymmetric one-way street () |
| Degree Metric | Single degree | Split: In-Degree and Out-Degree |
| Degree Sum Theorem | ||
| Maximum Edges | ||
| Real-World Models | Facebook friendships, bidirectional highway grids | Twitter/X followers, Web hyperlinks, financial wires |
4. Weighted Graphs & Cost Models
#In a weighted graph, every edge carries an assigned numerical weight :
| Weight Domain | Real-World Analog | Algorithmic Solvability for Shortest Path |
|---|---|---|
| Unweighted () | Hop counts, web link distance | Breadth-First Search (BFS) in |
| Non-Negative () | Physical distance, network latency, road tolls | Dijkstra's Algorithm with Min-Heap in |
| Negative Weights () | Financial arbitrage, energy delta, chemical bonds | Bellman-Ford Algorithm in (Dijkstra fails!) |
| Negative Weight Cycles | Infinite profit cycles, energy generation loops | Shortest path is mathematically undefined () |
5. Directed Acyclic Graphs (DAGs)
#A Directed Acyclic Graph (DAG) is a directed graph containing no directed cycles: following directed edges from any vertex never returns to .
Fundamental Theorems of DAGs
#| Theorem | Formal Assertion | Practical Implication |
|---|---|---|
| 1. Source and Sink Invariant | Every finite non-empty DAG has at least one Source () and at least one Sink (). | Guarantees well-defined start and end boundaries for traversals. |
| 2. Topological Ordering | A directed graph admits a Topological Sort if and only if it is a DAG. | Linearizes dependencies such that for all , precedes . |
| 3. Dynamic Programming Equivalence | Any problem possessing optimal substructure and overlapping subproblems maps to a DAG. | Shortest and longest paths on DAGs are solvable in time! |
Real-World Production DAG Architectures
#- Compilation Build Systems: Make, Bazel, and Gradle model compilation targets as DAG nodes to orchestrate parallel builds.
- Version Control Systems (Git): Commits form a directed acyclic graph where child commits point backward to parent commits.
- Deep Learning Computation Engines: PyTorch autograd and TensorFlow represent tensor operations as directed acyclic computation graphs.
6. Bipartite Graphs & Kőnig's Odd Cycle Theorem
#Definition: Bipartite Graph
#An undirected graph is Bipartite if its vertex set can be partitioned into two disjoint subsets and (, ) such that every edge in connects a vertex in to a vertex in . No edge connects two vertices within the same subset.
Theorem (Dénes Kőnig, 1936)
#A graph is bipartite if and only if it contains no odd-length cycles.
| Cycle Topology | Cycle Length | 2-Color Partition Feasibility | Bipartite Status |
|---|---|---|---|
| Square () | (Even) | Alternates: . Zero conflict. | Bipartite |
| Triangle () | (Odd) | Nodes: . Third node connects to both Red and Blue! Conflict! | Non-Bipartite |
| Pentagon () | (Odd) | 2-coloring forces adjacent nodes to share the same color. | Non-Bipartite |
7. Step-by-Step Dry Run State Trace: 2-Color BFS Bipartiteness Test
#Consider verifying whether graph with edges is bipartite.
export function isBipartite(n: number, adj: number[][]): boolean {
const color = new Array(n + 1).fill(0); // 0 = unvisited, 1 = Red, -1 = Blue
for (let start = 1; start <= n; start++) {
if (color[start] !== 0) continue;
const queue: number[] = [start];
color[start] = 1;
while (queue.length > 0) {
const u = queue.shift()!;
for (const v of adj[u]) {
if (color[v] === 0) {
color[v] = -color[u]; // Invert color for neighbor
queue.push(v);
} else if (color[v] === color[u]) {
return false; // Odd cycle detected!
}
}
}
}
return true;
}Execution Trace Table
#| Step | Queue State | Active Vertex | Neighbor | Current Color of | Invariant Check | Action Taken |
|---|---|---|---|---|---|---|
| 1 | [1] | (Color: Red) | 0 (Unvisited) | Uncolored | Set . Enqueue . | |
| 2 | [1] | (Color: Red) | 0 (Unvisited) | Uncolored | Set . Enqueue . | |
| 3 | [2, 4] | (Color: Blue) | 0 (Unvisited) | Uncolored | Set . Enqueue . | |
| 4 | [4, 3] | (Color: Blue) | -1 (Blue) | Color conflict! Both endpoints of edge are Blue! | ||
| Result | — | — | — | — | Odd-length 3-cycle detected: . Graph is NOT Bipartite. | Return false. |
8. Special Graph Classes & Topologies
#| Graph Class | Mathematical Invariants | Edge Density | Primary Application / Real-World Role |
|---|---|---|---|
| Complete Graph () | Every pair of vertices is linked: . | Dense () | Worst-case benchmarking, clique detection |
| Complete Bipartite () | Every vertex in links to all in : . | Medium | Two-sided matching, recommender systems |
| Planar Graph | Can be drawn on 2D plane with zero edge crossings: . | Strictly Sparse: | Printed circuit board (PCB) layout, road maps |
| Eulerian Graph | Connected and every vertex has even degree. | Arbitrary | DNA sequencing (de Bruijn graphs), street sweeping |
| Hamiltonian Graph | Contains a cycle visiting every vertex once. | Arbitrary | Traveling Salesperson Problem (NP-Complete) |
9. Common Traps, Edge Cases & Implementation Pitfalls
#- Self-Loops and Handshaking:
- A self-loop in an undirected graph contributes to , as both endpoints attach to the same vertex. Neglecting this invalidates the Handshaking Lemma.
- Disconnected Components in Traversal:
- Graph algorithms (BFS, DFS, Bipartiteness) must loop through all vertices as potential search roots. Running BFS from a single vertex will fail to visit disconnected components.
- Eulerian Path vs. Eulerian Circuit:
- An Eulerian Circuit requires all vertices to have even degrees. An Eulerian Path requires exactly two vertices to have odd degrees (start and finish).
10. References & Academic Attribution
#- Euler, L. (1736). Solutio problematis ad geometriam situs pertinentis. Commentarii Academiae Scientiarum Petropolitanae, 8, 128–140.
- Kőnig, D. (1936). Theorie der endlichen und unendlichen Graphen. Akademische Verlagsgesellschaft.
- Kuratowski, K. (1930). Sur le problème des courbes gauches en topologie. Fundamenta Mathematicae, 15(1), 271–283.
- Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2022). Introduction to Algorithms (4th ed.), Chapter 20. MIT Press.