Purpose
This note collects the definitions and small proofs that everything else about graphs builds on: terminology, the degree sum formula, bounds on edge count, and the tradeoff between the two standard graph representations.
Undirected Graphs
An undirected graph is a set of vertices and a set of edges.
Terminology
- Connected: a graph is connected if there is a path between every pair of vertices.
- Isolated vertex: a vertex with no edges.
- Planar graph: a graph you can draw on a plane such that no two edges cross.
- Degree of a vertex: the number of edges that touch .
- Connected components: the maximal connected subsets of vertices. They partition .
- Path: a sequence of distinct vertices where each vertex is connected to the next by an edge. The length of a path is its number of edges.
- Cycle: a path of length at least 3 that starts and ends at the same vertex.
- Tree: a connected graph with no cycles.
Degree Sum
Claim: in any undirected graph, the number of edges is half the sum of all vertex degrees.
Proof: the sum counts each edge exactly twice, once from each endpoint.
Odd Degree Vertices
Claim: in any undirected graph, the number of odd-degree vertices is even.
Proof: the sum of all degrees is even (it equals ). The even-degree vertices contribute an even amount to the sum, so the odd-degree vertices must also contribute an even amount. A sum of an odd number of odd numbers is odd, so the count of odd-degree vertices is even.
Degree 1 Vertices
Claim: if is acyclic, then has a vertex of degree at most 1.
Proof: by contradiction. Assume . Start walking from any vertex, and at each vertex leave along an edge other than the one you arrived on, which exists since . The graph has finitely many vertices, so some vertex eventually repeats, and the walk between the two visits forms a cycle. This contradicts being acyclic.
Number of Edges
Let be a graph with vertices and edges.
Claim:
Proof: each vertex can be connected to at most other vertices, and each edge is shared by two vertices, so the total number of edges is at most .
Too Many Edges Force a Cycle
Claim: if an undirected graph on vertices has at least edges, then it contains a cycle.
Proof: an acyclic undirected graph is a forest, so each connected component is a tree. If the components have vertex counts , then the total number of edges is
So every acyclic graph on vertices has at most edges. The contrapositive says any graph with at least edges must contain a cycle.
Sparsity
A graph is called sparse if , and dense otherwise. Sparse graphs are common in practice: social networks, the web graph, and planar graphs are all sparse. For a sparse graph, an algorithm behaves like , even though in the worst case.
Storing Graphs
Adjacency Matrix
A matrix where if there is an edge between and , and otherwise.
- Pro: time to check whether an edge exists between two vertices.
- Con: space regardless of how many edges exist.
- Con: time to enumerate the neighbors of a vertex.
Good for dense graphs.
Adjacency List
A list of lists, where each vertex has a list of its neighbors.
- Pro: enumerating the neighbors of takes time.
- Pro: space.
- Con: checking whether edge exists takes time.
Good for sparse graphs.
from typing import List, Tuple
def build_adjacency_list(n: int, edges: List[Tuple[int, int]]) -> List[List[int]]:
adj = [[] for _ in range(n)]
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
return adj
def build_adjacency_matrix(n: int, edges: List[Tuple[int, int]]) -> List[List[int]]:
adj = [[0] * n for _ in range(n)]
for u, v in edges:
adj[u][v] = 1
adj[v][u] = 1
return adj