Solution (source code)

= Solution

With an <adjacency list>, initialization and the outer loop cost $O(|V|)$. Each <vertex> is visited once, and the adjacency scans inspect each <edge of a graph> twice, once from each endpoint. The total worst-case time is therefore \b[$O(|V|+|E|)$], with $O(|V|)$ labels and recursion-stack space. An <adjacency matrix> representation instead incurs $O(|V|^2)$ scanning time.