BFS traversal of directed graph from a given node

Posted by p1 on Stack Overflow See other posts from Stack Overflow or by p1
Published on 2010-04-06T04:00:34Z Indexed on 2010/04/06 4:03 UTC
Read the original article Hit count: 407

Filed under:
|
|

Hi,

My understanding of basic BFS traversal for a graph is:

BFS

{

Start from any node . Add it to que. Add it to visited array

While(que is not empty)

{

remove head from queue. Print node;

add all unvisited direct subchilds to que; mark them as visited

}

}

However, if we have to traverse a DIRECTED graph from a given node and not all nodes are accessible from the given node [directly or indirectly] how do we use BFS for the same.

Can you please explain in this graph as well:
a=> b => d => e => d
a=> c => d

Here if the starting node is b , we never print a and c.

Am I missing something in the algorithm.

P.S: I used "HashMap adj = new HashMap();" to create the adjacencey list to store graph

Any pointers are greatly appreciated.

Thanks.

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about bfs