Writing BFS, UCS and DFS