Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2015/functional-programming-design-and-analysis-of-algorithms/7/c/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2015 Functional programming, design and analysis of algorithms 7 c Solution by
Codex 0 2026-10-31
Augment depth-first search with a two-valued colour:Each tree edge receives opposite endpoint colours. If an inspected edge has equal colours, the preceding parity argument produces an odd cycle, so the graph is not bipartite. If the procedure succeeds, every edge has differently coloured endpoints, and the two colour classes are a bipartition. The outer loop handles disconnected graphs and isolated vertices.
paint(v, c):
colour[v] = c
for w in adjacency[v]:
if colour[w] is unset:
if not paint(w, 1-c):
return false
else if colour[w] == c:
return false
return true
for v in V:
if colour[v] is unset:
if not paint(v, 0):
return false
return trueEach vertex is coloured once and each edge incidence is checked once, giving time and extra space with adjacency lists. No separate cycle enumeration is required.
New to topics? Read the docs here!