BFS-Shortest Path

Getting wrong answers in test cases.
Here is my code:

@Puneet_Sharma,

https://ide.codingblocks.com/s/219708, here is the corrected code.

You don’t need to put strings, you can put integers in your hashmap and queues.
And you can return an array from your bfs function.

You need to return an array from the bfs function. In the bfs function, create a distance array of size number of nodes +1. And initially fill the Array with -1 as values. Now make a Hashmap for visited nodes and a queue for nodes that you need to visit.

Now to this queue add the start node and put the value of start index in the distance array as 0.

Now all you need to do is start a while loop such that the size of queue is >0. Now pop the current node and add it to our hashmap of visited node. And start a for loop for the current node and visit all the neighbors while updating the distance array.

hi @Puneet_Sharma
Do you still have doubt in the above explanation?

No, thanks. :slightly_smiling_face:

@Puneet_Sharma
please mark your doubt as resolved in my doubts section and rate me as well.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.