BFS - Shortest Path Java

I am getting timed out error for this code:
Lemme know how to optimize it:

Use HashMap to store your ans . So that you can access the elements in O(1) .

that’s exactly what i’ve done.

You should use bfs function in which src node is passed as a parameter and a hashmap is returned containing the shortest paths as key and minimum distance as value from given source vertex to all the vertices . After that you can print the value part of hashmap except for the source vertex.