Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2016/functional-programming-design-and-analysis-of-algorithms/6/e/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2016 Functional programming, design and analysis of algorithms 6 e Solution by
Codex 0 2026-10-31
Compute any topological sort using a standard algorithm, then check its consecutive entries in the adjacency matrix:If all checks pass, the returned ordering is a Hamiltonian path. If a check fails but a Hamiltonian path existed, part (d) would force the computed topological sort to equal the unique path order, whose consecutive edges all exist. This contradiction proves the negative answer correct.
DAGHamiltonianPath(A):
order = TopologicalSort(A)
for j = 1,...,n-1:
if not A[order[j],order[j+1]]:
return "no Hamiltonian path"
return orderA standard topological sort scans all matrix rows and therefore takes time with this input representation; the subsequent edge checks take . The answer is consequently time, with auxiliary storage beyond the input matrix. An bound for an adjacency list must not be substituted for the stipulated adjacency matrix bound.
New to topics? Read the docs here!