Hi all, welcome back to another post of my brand new series on Graph Theory named Graph Theory: Go Hero. I undoubtedly recommend the complete series, if you are planning to get started with or want to have a quick refresher. We’re going to see how we can use Breadth First Search (BFS) to solve a shortest path problem. I have already done an another post on BFS, earlier. So, let’s dive into deep.
I hope you have an idea about what is Breadth First Search (BFS) and how it works because we would be using the BFS concepts intensively.
Many problems in Graph Theory could be represented using grids because interestingly grids are a form of **_implicit graph. _**We can determine the neighbors of our current location by searching within the grid. A type of problem where we find the shortest path in a grid is solving a maze, like below.
Another example could be routing through obstacles (like trees, rivers, rocks etc) to get to a location.
A common approach to solve graph problems is to first convert the structure into some representational formats like adjacency matrix or list. Basically, these are data structures which store the neighborhood information within the graph. Let’s see a more intuitive version of it.
Consider we have an imaginary graph.
No, this is not a graph. Look at figure 1, but that’s what I was talking about. Imagine that every cell in figure 1 has neighbors to it’s left, right, bottom and up. For more clarity, cell 0 has two neighbors, 1 and 2. In the same way, cell 4 also has two neighbors 2 and 3. We can review these cells as the vertices in a graph where **_rows * columns _**would be the total number of vertices. Figure 2 is the adjacency list representing our imaginary graph, now you can relate it with the first figure, right? The last figure depicts the adjacency matrix of the same graph. Every cell (i, j) of adjacency matrix is filled with 1s where nodes i and j have an edge in between them. We used just 1s and 0s here because we have no information about the cost from vertex i to j. If we had that, we could have used that information, as well.
Once we have an adjacency list/matrix representation of a graph, we can run multiple graph algorithms on top it to solve different use cases like finding the shortest path and connected components.
This is probably a problem statement we have encountered in many interviews and programming competitions and it goes as follows.
Suppose you are trapped in a 2D dungeon and you have to find the easiest way out. Hold on, we have some obstacles too. The dungeon is composed of unit cubes which may or may not be filled with rocks. It would take exactly one minute to move either east, west, south or north. You can’t move diagonally as the maze is tightly packed with solid rocks.
The dungeon has a size of R x C where R is number of rows and c is number of columns. We have to start at cell ‘S’ and we have an exit at cell ‘E’. The number (#) symbol depicts the roadblocks in the route and period (.) shows an open route.
#data-science #engineering #graph-theory #data analysis