I learned bfs from my friend and from this blog shafaetsplanet . In shafaetsplanet the algorithm is explained very easily. There the bfs code was implemented using adjacent metrix. Here we will implement it using adj list.
from safaet's blog I will take few lines to show everyone how bfs works
01. A node can't be visited more than once.
02. The source node is always level 0.
03. All nodes directly connected to level 0 or source node are level 1.
04. All nodes directly connected to level 1 are level 2.And this is how level will keep on rising one by one.
05. Level of each node is the shortest path distance from its source node
Basic Graph:
Bfs using source node A :
from safaet's blog I will take few lines to show everyone how bfs works
01. A node can't be visited more than once.
02. The source node is always level 0.
03. All nodes directly connected to level 0 or source node are level 1.
04. All nodes directly connected to level 1 are level 2.And this is how level will keep on rising one by one.
05. Level of each node is the shortest path distance from its source node
Basic Graph:
Bfs using source node A :
Bfs using source node F:
No comments:
Post a Comment