There is a bug:
The search adds new nodes to the queue if they are not searched.
But they might be on the queue already, just not searched.
A node can be added multiple times to the queue.
To see this:
Search for "Michael Cyril Creighton".
Go to bottom of console.
It adds the entire block of "neighbours of Flatliners" twice.
Exercise: Edit the code to fix this. Tips:
The node is added to the queue, which is an array.
Use an "if" statement and array includes
to decide if to add the node or not.
neighbor is the object.
neighbor.label is the name.
graph.end is the goal object.
graph.end.label is the goal name.
When you are done, the entire block of "neighbours of Flatliners" will only be added once.
We are inside a "for" loop
which is inside an "if" statement
inside a "while" loop.
It needs the same solution of refactoring the code.
Exercise - Edit JSON
What if no path found?
Make it display "no path found" nicely.
Will need an actor for which no path is found.
So do this:
Download JSON.
Edit it to add fictional movie with two fictional actors.
Upload JSON.
Change code to point to that JSON.
See what happens when you select the fictional actor (no path to Kevin Bacon).
Exercise - Simple visualisation
Cosmetic code fix: Highlight the movie titles different to the actor names.
Exercise - Better visualisation
In contrast to the binary tree, this is not an ordered tree or ordered graph.
So search will take much longer. (We cannot quickly eliminate entire branches as we could with the binary tree search.)
Also in contrast to the binary tree, we do not draw the graph here.
In fact, the graph is complex and hard to visualise.
This illustrates the point that we do not have to draw the graph to search it.
Exercise: Can you edit the code to help visualise the graph?
Not easy. Consider thousands of movies and actors.