These are the solutions to the exercises found in the section, Exercises . The solutions provided here are in Ruby, but you can find the solutions in JavaScript and Python in the code download. [17]
-
If a user is browsing “nails,” the website will recommend “nail polish,” “needles,” “pins,” and “hammer.”
-
The order of depth-first search would be A-B-E-J-F-O-C-G-K-D-H-L-M-I-N-P, as seen in the following image:
-
The order of breadth-first search would be A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P, as seen in the following image:
-
Following is an implementation of breadth-first search:
<table><tbody><tr><td> <span> </span></td><td> <strong>def</strong> <strong>bfs</strong> (starting_vertex, search_value, visited_vertices={})</td></tr><tr><td> <span> </span></td><td>queue = Queue. <strong>new</strong> </td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td>visited_vertices[starting_vertex. <strong>value</strong> ] = <strong>true</strong> </td></tr><tr><td> <span> </span></td><td>queue. <strong>enqueue</strong> (starting_vertex)</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>while</strong> queue. <strong>read</strong> </td></tr><tr><td> <span> </span></td><td>current_vertex = queue. <strong>dequeue</strong> </td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>return</strong> current_vertex <strong>if</strong> current_vertex. <strong>value</strong> == search_value</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td>current_vertex. <strong>adjacent_vertices</strong> . <strong>each</strong> <strong>do</strong> |adjacent_vertex|</td></tr><tr><td> <span> </span></td><td> <strong>if</strong> !visited_vertices[adjacent_vertex. <strong>value</strong> ]</td></tr><tr><td> <span> </span></td><td>visited_vertices[adjacent_vertex. <strong>value</strong> ] = <strong>true</strong> </td></tr><tr><td> <span> </span></td><td>queue. <strong>enqueue</strong> (adjacent_vertex)</td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>return</strong> <strong>nil</strong> </td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr></tbody></table>
-
To find the shortest path in an unweighted graph, we are going to use breadth-first search. The main feature of breadth-first search is that it stays as close to the starting vertex as long as possible. This feature will serve as the key to finding the shortest path.
Let’s apply this to our social-networking example. Because breadth-first search stays close to Idris as long as possible, we’ll end up finding Lina first through the shortest possible path. Only later on in the search would we end up finding Lina through longer paths. In fact, we can even stop the search as soon we as find Lina. (Our implementation that follows doesn’t end early, but you can modify it to do so.)
As we visit each vertex for the first time, then, we know that the current vertex is always part of the shortest path from the starting vertex to the vertex we’re visiting. (Remember, in BFS, the current vertex and the vertex we’re visiting aren’t necessarily the same.)
For example, when we visit Lina for the first time, Kamil will be the current vertex. This is because in BFS, we’ll get to Lisa first through Kamil before we get to her through Sasha. When we do visit Lina (through Kamil), we can store in a table that the shortest path from Idris to Lina will be through Kamil. This table is similar to the cheapest_previous_stopover_city_table from Dijkstra’s algorithm.
In fact, whenever we visit any vertex, the shortest path from Idris to that vertex will be through the current vertex. We’ll store all of this data in the table called previous_vertex_table .
Finally, we can then use this data to work backward from Lina to Idris to build the precise shortest path between the two of them.
Here is our implementation:
<table><tbody><tr><td> <span> </span></td><td> <strong>def</strong> <strong>find_shortest_path</strong> (first_vertex, second_vertex, visited_vertices={})</td></tr><tr><td> <span> </span></td><td>queue = Queue. <strong>new</strong> </td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <em># As in Dijkstra's algorithm, we keep track in a table of each vertex</em> </td></tr><tr><td> <span> </span></td><td> <em># immediately preceding vertex.</em> </td></tr><tr><td> <span> </span></td><td>previous_vertex_table = {}</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <em># We employ breadth-first search:</em> </td></tr><tr><td> <span> </span></td><td>visited_vertices[first_vertex. <strong>value</strong> ] = <strong>true</strong> </td></tr><tr><td> <span> </span></td><td>queue. <strong>enqueue</strong> (first_vertex)</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>while</strong> queue. <strong>read</strong> </td></tr><tr><td> <span> </span></td><td>current_vertex = queue. <strong>dequeue</strong> </td></tr><tr><td> <span> </span></td><td>current_vertex. <strong>adjacent_vertices</strong> . <strong>each</strong> <strong>do</strong> |adjacent_vertex|</td></tr><tr><td> <span> </span></td><td> <strong>if</strong> !visited_vertices[adjacent_vertex. <strong>value</strong> ]</td></tr><tr><td> <span> </span></td><td>visited_vertices[adjacent_vertex. <strong>value</strong> ] = <strong>true</strong> </td></tr><tr><td> <span> </span></td><td>queue. <strong>enqueue</strong> (adjacent_vertex)</td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <em># We store in the previous_vertex table the adjacent_vertex</em> </td></tr><tr><td> <span> </span></td><td> <em># as the key, and the current_vertex as the value. This</em> </td></tr><tr><td> <span> </span></td><td> <em># indicates that the current_vertex is the immediately</em> </td></tr><tr><td> <span> </span></td><td> <em># preceding vertex that leads to the adjacent_vertex.</em> </td></tr><tr><td> <span> </span></td><td>previous_vertex_table[adjacent_vertex. <strong>value</strong> ] =</td></tr><tr><td> <span> </span></td><td>current_vertex. <strong>value</strong> </td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <em># As in Dijkstra's algorithm, we work backwards through the</em> </td></tr><tr><td> <span> </span></td><td> <em># previous_vertex_table to build the shortest path;</em> </td></tr><tr><td> <span> </span></td><td>shortest_path = []</td></tr><tr><td> <span> </span></td><td>current_vertex_value = second_vertex. <strong>value</strong> </td></tr><tr><td> <span> </span></td><td></td></tr><tr><td> <span> </span></td><td> <strong>while</strong> current_vertex_value != first_vertex. <strong>value</strong> </td></tr><tr><td> <span> </span></td><td>shortest_path << current_vertex_value</td></tr><tr><td> <span> </span></td><td>current_vertex_value = previous_vertex_table[current_vertex_value]</td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr><tr><td> <span> </span></td><td>shortest_path << first_vertex. <strong>value</strong> </td></tr><tr><td> <span> </span></td><td> <strong>return</strong> shortest_path. <strong>reverse</strong> </td></tr><tr><td> <span> </span></td><td> <strong>end</strong> </td></tr></tbody></table>