← back to discrete math

Graph Theory

Oscar Levin · CC BY-SA 4.0 · Discrete Mathematics: An Open Introduction, Ch. 4

A graph is a set of vertices and a set of edges connecting pairs of them. That is the entire definition. From it you get trees, planarity, coloring, and the oldest theorem in graph theory: Euler's criterion for traversing every edge exactly once.

A B C D Konigsberg: all 4 vertices have odd degree. No Euler circuit.

Graphs and degree

A graph G = (V, E) is a pair: a set of vertices and a set of edges. The degree of a vertex is the number of edges incident to it. The handshake lemma says the sum of all degrees equals 2|E|, because every edge contributes to two degrees.

Scheme

Paths, cycles, and connectivity

A path is a sequence of vertices where each consecutive pair is connected by an edge. A cycle is a path that starts and ends at the same vertex. A graph is connected if there is a path between every pair of vertices.

Scheme

Trees

A tree is a connected graph with no cycles. Equivalently, it is a connected graph on n vertices with exactly n-1 edges. Trees are minimal connected graphs: remove any edge and the graph disconnects.

Scheme

Euler and Hamilton paths

An Euler path traverses every edge exactly once. Euler proved it exists if and only if exactly 0 or 2 vertices have odd degree. A Hamilton path visits every vertex exactly once. No efficient test is known for Hamilton paths; this is one of the NP-complete problems.

Scheme

Planar graphs and coloring

A graph is planar if it can be drawn without edge crossings. Euler's formula for connected planar graphs: V - E + F = 2, where F is the number of faces. The four color theorem says every planar graph can be vertex-colored with at most 4 colors so that no two adjacent vertices share a color.

Scheme

Notation reference

Math Scheme Python Meaning
G = (V, E)adj listdictGraph
deg(v)(degree v edges)degree(v)Vertex degree
V - E + F = 2(+ (- v e) f)v - e + fEuler's formula (planar)
K_ncomplete graphcomplete graphComplete graph on n vertices
chi(G)greedy-colorgreedy colorChromatic number
Neighbors

Other chapter pages

Foundations (Wikipedia)

Translation notes

Graphs are represented as adjacency lists in both Scheme and Python. The greedy coloring algorithm is not guaranteed to find the optimal coloring (the chromatic number is NP-hard to compute). Euler's criterion for Euler paths is a theorem, not a heuristic: the "if and only if" is exact. Hamilton paths have no such clean characterization, which is why the problem is NP-complete.