By the end of this lesson, you will be able to create routing tables to navigate between any two vertices of a graph.
Hi everyone. In today’s lesson, we will see how to build routing tables to navigate a graph, using the spanning trees obtained from graph traversal algorithms, such as BFS or DFS. These routing tables will help the artificial intelligence that you will develop in this course to move between two positions in the maze.
From spanning trees to routing
In the previous lesson you have learnt how to build spanning trees. Once we have such a spanning tree, we need a routing algorithm to be able to navigate between any two vertices in the graph.
Routing from a starting point to a destination is interpreted backwards. We already know what the starting point is, so we need to begin with the destination to find a path. So, a routing algorithm provides a path from the destination to the starting point. To better understand this, let’s look at an example we’ve already seen in the previous lesson.
Let’s consider the path between and . We can see the spanning tree obtained using a DFS on the left, and the spanning tree obtained using a BFS on the right:
Routing from to any other vertex can be easy when we use trees. But to do this, we first need a few more definitions.
A rooted tree is a tree in which we designate a particular vertex as the root, which is also sometimes known as the origin. In this case, we will consider as the root, because it’s our starting position. Therefore, the edges of this rooted tree have a natural orientation towards the root.
In a rooted tree, the parent of a vertex is the vertex connected to it on the path from the root. For example, the parent of is in the tree on the left. Every vertex except the root has a single parent. A child of a vertex is a vertex of which is the parent. So, is the child of in the tree on the left.
If we apply these concepts on the whole trees, we obtain the following rooted trees, where the orientation of the edges indicates the parents of the vertices. For example, in both graphs, has no parent, the parent of is , and the children of are and .
Routing tables are useful data structures that can be used to reconstruct a path. As we mentioned earlier, routing is considered backwards, from the destination back to the starting position.
A routing table has just one row, and as many columns as there are vertices in the graph. Each column corresponds to one vertex, and the associated value in the table is its parent in the spanning tree rooted in .
Let us now see how this table is built step by step using a spanning tree.
Let’s start with one obtained from a DFS.
We start from an empty table. is the root, so it doesn’t have a parent. We simply note a « dot » in the column.
only has one child vertex, . So we add in the column corresponding to .
only has one child, , so we add in the column of .
Now let’s simply apply the same principle to vertices and , which are the only children of and , respectively.
Finally, has two children, and . So, let’s add in the columns corresponding to and .
So that’s it, the routing table is complete.
To read this table, you can simply start from the desired destination and read the corresponding entry, which refers to the parent vertex of the destination. For example, if we are looking for the path from to , we first read the entry corresponding to , which is . By definition, the parent of the destination in turn corresponds to the previous vertex in the path from to the destination vertex. By reading the entries corresponding to each predecessor, you’ll eventually reach the starting position . So in our example, the next step is to identify the entry corresponding to , which is . By continuing like this, we obtain the path .
As a second example, let’s also build the routing table from the spanning tree obtained from a BFS.
Here’s the spanning tree. Again, we start from an empty table.
has two children vertices, and , so we add in the corresponding columns.
The only child of is , and the only child of is .
Then, has two children, and . The routing table is complete!
If we read the path from to in the table we obtain . We can see that this path is shorter than the one obtained using a DFS, which was . This is an important difference between DFS and BFS. BFS is a more cautious approach than DFS, as BFS gradually increases the distance from the starting position. Using BFS, we can be absolutely certain that we’re going to find the shortest path between two vertices.
That’s it for today. Thank you for your attention. You should remember that it is not sufficient to build the spanning tree to navigate between vertices in the graph, but that you also need a routing algorithm.