CEMC Banner

Grade 7/8 Math Circles
Graph Theory and Search Algorithms — Solutions

Exercise Solutions

Exercise 1

What are the vertices and edges in the graph below? What is the neighbourhood of \(B\)?

There are 7 vertices labelled A, B, K, L, T, Y, Z. A line segment is drawn between circles A and Y, B and L, B and T, B and K, K and Z, Y and Z.

Exercise 1 Solution

The vertices in the graph are \(A,B,K,L,T,Y,Z\). The edges in the graph are \(\{A,Y\},\{B,K\},\{B,L\},\{B,T\},\{K,Z\}, \{Y,Z\}\). The neighbourhood of \(B\) is \(K,L,T\).

Exercise 2

What is the queue for each of the five graphs above?

Exercise 2 Solution

The queue for the first graph is \(Q=H,C,E,G\). The queue for the second graph is \(Q=C,E,G\). The queue for the third graph is \(Q=E,G\). The queue for the fourth graph is \(Q=G\). Finally, the queue for the fifth graph is empty.

Exercise 3

For the same graph as above, use BFS to do the following searches. Add neighbours to the queue in alphabetical order for both searches and list the order that you looked at the vertices.

  1. Search for vertex \(H\) starting from vertex \(B\).

  2. Search for vertex \(E\) starting from vertex \(F\).

Exercise 3 Solution

  1. The order that the vertices were looked at should be \(B,A,C,D,F,H\).

  2. The order that the vertices were looked at should be \(F,A,H,B,D,C,E\).

Exercise 4

For the same graph as above, use DFS to do the following searches. Choose neighbours in alphabetical order for both searches and list the order that you looked at the vertices.

  1. Search for vertex \(H\) starting from vertex \(B\).

  2. Search for vertex \(E\) starting from vertex \(F\).

How do the ordered lists using DFS in this exercise compare to the ordered lists using BFS in Exercise 3? Which search method is quicker for each search?

Exercise 4 Solution

  1. The order that the vertices were looked at should be \(B,A,D,E,C,D,G,A,F,H\).

  2. The order that the vertices were looked at should be \(F,A,B,C,E\).

For search (a), BFS is faster. For search (b), DFS is faster. The answer for other comparisons may vary. A few things that could have been said are:

Problem Set Solutions

  1. What are the vertices and edges in the graph below? Include colour labels for the vertices.

    There are seven circles that are coloured using four colours. Two circles are pink and are labelled 1 and 3. Two circles are blue and are labelled D and 7. Two circles are purple and are labelled W and 6. One circles is yellow and is labelled M. A line segment is drawn between circles D adn 1, D and M, D and 6, D and W, D and 3, 1 and M, 1 and 7, 3 and 7.

    Solution: The vertices are \(\{1,\text{pink}\}, \{3, \text{pink}\}, \{6, \text{purple}\}, \{7, \text{blue}\}, \{D, \text{blue}\}, \{M, \text{yellow}\},\) \(\{W, \text{purple}\}\). The edges are \(\{1,7\},\{1,D\},\{1,M\},\{3,7\},\{3,D\},\{6,D\},\{D,M\}, \{D,W\}\).

  2. List the neighbourhoods of all the vertices in the graph in question 1.

    Solution: The neighbourhood for vertex \(1\) is \(7,D,M\). The neighbourhood for vertex \(3\) is \(7,D\). The neighbourhood for vertex \(6\) is \(D\). The neighbourhood for vertex \(7\) is \(1,3\). The neighbourhood for vertex \(D\) is \(1,3,6,M,W\). The neighbourhood for vertex \(M\) is \(1,D\). Finally, the neighbourhood for vertex \(W\) is \(D\).

  3. Do BFS on the graph below until all the vertices are coloured black. Start at vertex \(H\) and pick neighbours from right-to-left (in other words, visually from the right side of the graph to the left). What order did you look at the vertices?

    There are 8 vertices labelled A to H. From right to left, the vertices appear in the order F, D, G, B, H, C, A, E. An edge connects A and C, B and C, B and H, B and G, D and G, D and F, G and H, and E and H.

    Solution: The order that the vertices were looked at should be \(H,G,B,E,D,C,F,A\).

  4. Do DFS on the graph in question 3 until all the vertices are coloured black. Start at vertex \(E\) and pick neighbours alphabetically. What order did you look at the vertices?

    Solution: The order that the vertices were looked at should be \(E,H,B,C,A,B,G,D,F,H\).

  5. Search for vertex \(F\) starting from vertex \(A\) using BFS for the following graph, picking neighbours alphabetically. What order did you look at the vertices?

    There are seven vertices. The vertices are labelled from A, B, C, R, E, F, G. The edges connect A and C, B and C, C and E, E and D, E and F, E and G, F and G.

    Solution: The order that the vertices were looked at should be \(A,C,B,E,D,F\).

  6. For the graph in question 5, would it be faster to find vertex \(G\) from vertex \(B\) using BFS or DFS if you pick neighbours from left-to-right? What about from right-to-left?

    Solution: Picking neighbours from left-to-right, the ordered list for BFS should be \(B,C,A,E,D,F,G\) and the ordered list for DFS should be \(B,C,A,C,E,D,E,F,G\). Therefore, BFS is faster because there is no need to backtrack. Picking neighbours from right-to-left, the ordered list for BFS should be \(B,C,E,A,G\) and the ordered list for DFS should be \(B,C,E,G\). Therefore, DFS is faster because we didn’t have to look at \(A\).

    1. Search through the whole graph below three times using BFS. Pick neighbours alphabetically and use the starting points \(G\), \(A\), and \(E\). List the order that you look at the vertices for those three searches. What do you notice about the length of the lists of vertices?

    2. Search through the whole graph below three times using DFS. Pick neighbours alphabetically and use the starting points \(G\), \(A\), and \(E\). List the order that you look at the vertices for those three searches. What do you notice about the length of the lists of vertices?

      There are 8 vertices labelled from A to H. The edges connect A and B, A and C, B and D, C and E, C and F, D and G, E and F, and E and H.

    Solution:

    1. With the starting point \(G\), the order that the vertices were looked at should be \(G,D,B,A,C,E,F,H\). With the starting point \(A\), the order that the vertices were looked at should be \(A,B,C,D,E,F,G,H\). With the starting point \(E\), the order that the vertices were looked at should be \(E,C,F,H,A,B,D,G\). The lists of vertices are all the same length, \(8\) items. We can come to the conclusion that BFS will always give ordered lists of the same length because each vertex is only looked at once.

    2. With the starting point \(G\), the order that the vertices were looked at should be \(G,D,B,A,C,E,F,E,H,C\). With the starting point \(A\), the order that the vertices were looked at should be \(A,B,D,G,A,C,E,F,E,H,C\). With the starting point \(E\), the order that the vertices were looked at should be \(E,C,A,B,D,G,E,F,E,H\). The lists of vertices are not all the same length, two of them have \(10\) items and one has \(11\) items. We can come to the conclusion that DFS will give ordered lists of different length because more backtracking will add more items to the ordered list.

    1. Compare your lists from part (a) and part (b) in question 7 for each separate starting point. Which vertices can be found faster using BFS from the starting point \(G\)? What about for the staring points \(A\) and \(E\)?

    2. Compare your lists from part (a) and part (b) in question 7 for each separate starting point. Which vertices can be found faster using DFS from the starting point \(G\)? What about for the staring points \(A\) and \(E\)?

    3. Compare your lists from part (a) and part (b) in question 7 for each separate starting point. Are there any vertices found in the same order or at the same time between using BFS and DFS from the starting point \(G\)? What about for the staring points \(A\) and \(E\)?

    Solution:

    1. From starting point \(G\), the vertex \(H\) can be found faster using BFS. From starting point \(A\), the vertices \(C\), \(E\), \(F\), and \(H\) can be found faster using BFS. Lastly, from starting point \(E\), the vertices \(F\) and \(H\) can be found faster using BFS.

    2. From starting point \(G\), no vertices can be found faster using DFS. From starting point \(A\), the vertices \(D\) and \(G\) can be found faster using DFS. Lastly, from starting point \(E\), the vertices \(A\), \(B\), \(D\), and \(G\) can be found faster using DFS.

    3. From starting point \(G\), the vertices \(G,D,B,A,C,E,\) and \(F\) are found at the same time and in the same order between using BFS or DFS. From starting point \(A\), the vertices \(A\) and \(B\) are found at the same time and in the same order between using BFS or DFS. Lastly, from starting point \(E\), the vertices \(E\) and \(C\) are found at the same time and in the same order between using BFS or DFS.