Replaced bfs() usage by iterating over the forwardnodes

In a few places, we want to iterate over all nodes. The order of nodes is not important here.
I used iterating over forwardnodes.
It is faster than the breadth-first-search.