Improved depth-first search and added breadth-first search