Depth-first and breadth-first algorithm