tunning the performance using DFS and BFS