Exercise Solutions
Exercise 1
What are the vertices and edges in the graph below? What is the
neighbourhood of ?
Exercise 1 Solution
The vertices in the graph are . The edges in the graph are
. The neighbourhood of is .
Exercise 2
What is the queue for each of the five graphs above?
Exercise 2 Solution
The queue for the first graph is . The queue for the second graph
is . The queue for the third
graph is . The queue for the
fourth graph is . 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.
Search for vertex starting
from vertex .
Search for vertex starting
from vertex .
Exercise 3 Solution
The order that the vertices were looked at should be .
The order that the vertices were looked at should be .
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.
Search for vertex starting
from vertex .
Search for vertex starting
from vertex .
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
The order that the vertices were looked at should be .
The order that the vertices were looked at should be .
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:
The BFS and DFS for the searches start with the same two letters
( and ).
Vertex appears in the DFS
for (a) but not the BFS for (a). This could be done with other vertices
too, like vertex appears in BFS
for (b) but not DFS for (b).
Problem Set
Solutions
What are the vertices and edges in the graph below? Include
colour labels for the vertices.
Solution: The vertices are . The edges are .
List the neighbourhoods of all the vertices in the graph in
question 1.
Solution: The neighbourhood for vertex is . The neighbourhood for vertex is . The neighbourhood for vertex is . The neighbourhood for vertex is . The neighbourhood for vertex is . The neighbourhood for vertex
is . Finally, the neighbourhood for
vertex is .
Do BFS on the graph below until all the vertices are coloured
black. Start at vertex 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?
Solution: The order that the vertices were looked at should
be .
Do DFS on the graph in question 3 until all the vertices are
coloured black. Start at vertex
and pick neighbours alphabetically. What order did you look at the
vertices?
Solution: The order that the vertices were looked at should
be .
Search for vertex starting
from vertex using BFS for the
following graph, picking neighbours alphabetically. What order did you
look at the vertices?
Solution: The order that the vertices were looked at should
be .
For the graph in question 5, would it be faster to find vertex
from vertex 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 and the ordered list for
DFS should be .
Therefore, BFS is faster because there is no need to backtrack. Picking
neighbours from right-to-left, the ordered list for BFS should be and the ordered list for DFS
should be . Therefore, DFS
is faster because we didn’t have to look at .
Search through the whole graph below three times using BFS. Pick
neighbours alphabetically and use the starting points , , and . 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?
Search through the whole graph below three times using DFS. Pick
neighbours alphabetically and use the starting points , , and . 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?
Solution:
With the starting point ,
the order that the vertices were looked at should be . With the starting point
, the order that the vertices were
looked at should be . With the starting point
, the order that the vertices were
looked at should be . The lists of vertices
are all the same length, 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.
With the starting point ,
the order that the vertices were looked at should be . With the starting
point , the order that the
vertices were looked at should be . With the starting
point , the order that the
vertices were looked at should be . The lists of
vertices are not all the same length, two of them have items and one has 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.
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 ? What
about for the staring points and
?
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 ? What
about for the staring points and
?
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 ? What about for the staring
points and ?
Solution:
From starting point , the
vertex can be found faster using
BFS. From starting point , the
vertices , , , and can be found faster using BFS. Lastly,
from starting point , the vertices
and can be found faster using BFS.
From starting point , no
vertices can be found faster using DFS. From starting point , the vertices and can be found faster using DFS. Lastly,
from starting point , the vertices
, , , and can be found faster using DFS.
From starting point , the
vertices and are found at the same time and in the
same order between using BFS or DFS. From starting point , the vertices and are found at the same time and in the
same order between using BFS or DFS. Lastly, from starting point , the vertices and are found at the same time and in the
same order between using BFS or DFS.