Infosys interview question

graph question based on traversals like bfs and dfs