Introduction to Graph Theory
Graphs and their vocabulary (directed, undirected, weighted, simple), degrees and the handshaking lemma, walks, paths, cycles and connectivity, special graphs (complete, bipartite, trees and forests, DAGs), adjacency matrix and adjacency list representations with their costs, counting walks with matrix powers, Euler and Hamiltonian paths, planarity and Euler's formula, and colouring.
Graphs
A graph is a set of vertices (nodes) and edges connecting pairs of vertices. Graphs model networks of every kind: roads, social links, web pages, dependencies, state spaces in AI search.
| Kind | Edges are |
|---|---|
| Undirected | unordered pairs |
| Directed (digraph) | ordered pairs , "arcs" |
| Weighted | labelled with a cost/length |
| Simple | no self-loops, no parallel edges |
| Multigraph | parallel edges allowed |
A simple undirected graph has at most edges; a simple digraph at most . Dense graphs have ; sparse ones have .
Degrees
- Undirected: = number of incident edges. Handshaking lemma: , so the number of odd-degree vertices is even.
- Directed: in-degree and out-degree, with .
- -regular: every vertex has degree (then ).
Walks, paths, cycles, connectivity
| Term | Definition |
|---|---|
| Walk | sequence of vertices joined by edges (repeats allowed) |
| Trail | walk with no repeated edge |
| Path | walk with no repeated vertex |
| Cycle | closed path (length ≥ 3 in simple graphs) |
| Connected | a path between every pair of vertices |
| Connected component | maximal connected subgraph |
| Strongly connected (digraph) | directed path both ways between every pair |
| Distance | length of a shortest path |
| Diameter | largest distance between two vertices |
A graph with vertices and components has at least edges.
Special graphs
| Graph | Key facts |
|---|---|
| Complete | edges, every vertex degree |
| Bipartite | vertices split into two sides, edges only across; ⇔ no odd cycle ⇔ 2-colourable; has edges |
| Tree | connected, acyclic, edges, a unique path between any two vertices; adding any edge creates exactly one cycle |
| Forest | acyclic; edges for trees |
| DAG | directed, no directed cycle; has a topological order |
| Cycle , path | and edges |
A spanning tree of a connected graph is a subgraph that is a tree containing all vertices ( edges). By Cayley's formula, has spanning trees.
Representations
# vertices 0..3, undirected edges 0-1, 0-2, 1-2, 2-3
adj_list = {0: [1, 2], 1: [0, 2], 2: [0, 1, 3], 3: [2]}
adj_matrix = [
[0, 1, 1, 0],
[1, 0, 1, 0],
[1, 1, 0, 1],
[0, 0, 1, 0],
]
| Operation | Adjacency matrix | Adjacency list |
|---|---|---|
| Space | ||
| Is an edge? | ||
| Iterate over neighbours of | ||
| Visit all edges | ||
| Best for | dense graphs, matrix algebra | sparse graphs, traversals |
For undirected graphs the adjacency matrix is symmetric; for weighted graphs store (∞ or None for no edge).
Counting walks: is the number of walks of length exactly from to . counts the triangles of an undirected graph.
Euler and Hamiltonian
| Euler (every edge once) | Hamiltonian (every vertex once) | |
|---|---|---|
| Circuit/cycle exists | connected (ignoring isolated vertices) and all degrees even | no simple test — NP-complete |
| Path exists | exactly 0 or 2 vertices of odd degree | NP-complete |
| Algorithm | Hierholzer, | exhaustive search / DP |
Planarity and colouring
- A graph is planar if it can be drawn without crossing edges. For connected planar graphs, Euler's formula holds, and if , ( if there are no triangles). Hence () and () are not planar.
- A proper vertex colouring gives adjacent vertices different colours; the minimum number of colours is the chromatic number . ; iff bipartite (with at least one edge); every planar graph is 4-colourable. Colouring models scheduling and register allocation.
Quick revision
Practice questions
GATE-style questions: 4 one-mark and 6 two-mark. Wrong MCQs cost ⅓ of their marks in the real exam; MSQ and NAT have no negative marking. Attempt each before opening the solution.
An undirected graph has 5 vertices with degrees 3, 3, 2, 2, 2. The number of edges is ______.
The number of edges in the complete graph is ______.
A tree with vertices has exactly
Which statements about finite simple undirected graphs are true?
One or more options are correct.
A connected planar graph has 10 vertices and 15 edges. The number of faces in any planar drawing (including the outer face) is ______.
The number of distinct simple undirected graphs on 4 labelled vertices is ______.
Which of these graphs has an Euler circuit (a closed walk using every edge exactly once)?
For the 4-cycle with vertices 1–2–3–4–1 and adjacency matrix , the entry is ______.
A forest (acyclic undirected graph) has 10 vertices and 7 edges. The number of connected components (trees) is ______.
The space needed by an adjacency-list representation of a graph with vertices and edges is