Skip to main content

BFS

📄️ Guidelines

First of all you should remember that you can look at matrix/grid and trees just as a specilization of graphs. Tree is a connected acyclic undirected graph (connected == there is path between any pair of nodes, acyclic = no cycle in the graph, undirected = the direction of relation is not important e.g. child is connected to parent just like the parent is connected to the child in tree). For the BFS purposes matrix/grid can be looked as 4-directionally adjacent graph, any matrix cell is a node in graph and it's connected to the 4 cells above, below, left and right. So if you get a tree or matrix/grid shortest path or level traversal problem remember to think about them as graph and apply the BFS.