BFS OF A GRAPH (doubt in code)

AFTER VISITING EACH NODE WE HAVE TO PUSH NEIGHBOR TO THE QUEUE AND SEE THAT THEY ARE VISITED I HAVE DOUBT IN THE CODE

It won’t give an error, see what’s happening is. You take source node, add it into queue and mark it as visited.
Now your node has the first element if the queue that’s the source, and we pop it from queue and now it’s empty.
Your nbr will traverse on linked path of the source node, as it’s linked list implementation and will check for all left over nodes.

If you still have any doubt, you can ask it.

after the src(assume 0 as src and has 1 2 as neighbour) is pushed in the queue , the map has <0,true> now, in the for each loop we are checking for is 1 visited ? right, but 1 is still not initialized in the map ? that’s my doubt

You are talking about visited map? Right?

yes i am talking about visited map

The error you are talking about occurs when we are only talking about vector < T> v
But this error doesn’t occur when we take vector of vector that is
Vector < vector < T> > v
So in this case, it won’t throw an error.

but this this map of key–>integer value–>boolean

we are making vector of map of Boolean and integers,

is there any other way to make visited array such that this confusion occurs

because i was trying to submit it in submit this in this -->https://practice.geeksforgeeks.org/problems/bfs-traversal-of-graph/1 code–>https://ide.codingblocks.com/s/375277
its showing seg fault


Have edited your code, this is getting accepted .
Please don’t forget to mark it as resolved. and do give star rating.