## Video transcripts

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 the BFS or the 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. Tracing a path backwards is more intuitive than it might seem. Imagine that you, the blue circle on the map, have to drive to a friend’s house, which is the orange circle on the map. You could start by deciding if you should turn left or right at the next intersection. Or, you might think of the best route backwards, by first identifying what street your friend’s house is on. Then, which other road accessed this street, etc.

This second way of thinking is how the routing algorithm will work. To better understand this, let’s look at an example we’ve already seen in the previous lesson that you had with Vincent. 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 away from 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

We can now build routing tables by considering the parent of each vertex in the path from . 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. 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. 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.

### Concluding words

That’s it for today. Thank you for your attention! I’ve really enjoyed talking about routing with you.

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.

See you soon for more algorithmic adventures!

## Quiz

Routing helps us...
In a rooted tree...
In a routing table, we can find...