Sub topic 1
Representing Graphs with Adjacency Lists
Prerequisites
This chapter assumes you know basic graph terms (vertex, edge, path, cycle), Big-O notation, and how to read Python. Breadth-first search (BFS) comes up in later chapters.
Representing Graphs with Adjacency Lists
An adjacency list stores, for each vertex, the list of its neighbours. In an undirected graph each edge (u, v) is stored twice: v goes in u's list and u goes in v's list. The structure takes O(V + E) space and lets you visit a vertex's neighbours in time proportional to its degree.
def build_adj(n, edges):
adj = [[] for _ in range(n)]
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
return adj
def is_proper(adj, colour):
"""True if no edge joins two vertices of the same colour."""
return all(colour[u] != colour[v] for u in range(len(adj)) for v in adj[u])
square = build_adj(4, [(0, 1), (1, 2), (2, 3), (3, 0)])
print(square) # [[1, 3], [0, 2], [1, 3], [2, 0]]
print(is_proper(square, [0, 1, 0, 1])) # True
print(is_proper(square, [0, 0, 1, 1])) # FalseA common mistake is to add only one direction of each edge. A search started from the other endpoint will then miss that edge.
What Makes a Graph Bipartite
A graph is bipartite if its vertices can be split into two sets L and R so that every edge has one endpoint in L and the other in R. No edge stays inside a single set. Some familiar examples:
- Even-length cycles such as the 4-cycle above
- Trees
- Grids
- "Workers and jobs" graphs, where edges only link a worker to a job
Two-Colouring: Bipartiteness as a Colouring Problem
A proper two-colouring gives each vertex colour 0 or 1 so that the two endpoints of every edge get different colours. Colour 0 corresponds to set L and colour 1 to set R. So a graph is bipartite exactly when it has a proper two-colouring. The is_proper function above checks a proposed colouring in O(V + E) time.
The Odd Cycle Theorem
Theorem: a graph is bipartite if and only if it contains no cycle of odd length.
No odd cycle is necessary. Colours must alternate as you walk along a cycle. After an odd number of steps you are back at the start vertex, but you have landed on the opposite colour. So the start vertex would need both colours, which is impossible.
No odd cycle is sufficient. In each connected component, pick a root and colour every vertex by the parity of its shortest-path distance from that root. Suppose an edge joined two vertices of the same parity. Then the two shortest paths plus that edge would form a closed walk of odd length. Every odd closed walk contains an odd cycle, which contradicts the assumption. So every edge joins different colours.
The proof of sufficiency is also an algorithm: colour vertices by BFS distance parity. The next chapter turns this into code.
Exercise
Is the 5-cycle with edges (0,1),(1,2),(2,3),(3,4),(4,0) bipartite? Justify using the theorem.
Show answer
No. The graph is itself a cycle of length 5, which is odd. If you alternate colours around it, vertex 0 and vertex 4 both need colour 0, but they are joined by an edge.
Exercise
Why does every tree have a proper two-colouring?
Show answer
A tree has no cycles, so it has no odd cycle, and the theorem says it is bipartite. To build the colouring, colour each vertex by the parity of its depth from any root.