// Cloned by Tristan Everitt on 29 Sep 2022 from World "Breadth-first search (Kevin Bacon)" by "Coding Train" project
// Please leave this clone trail here.
// Breadth-first search
// Port from:
// https://github.com/nature-of-code/NOC-S17-2-Intelligence-Learning/tree/master/week1-graphs/P2_six_degrees_kevin_bacon
// https://www.youtube.com/watch?v=piBq7VD0ZSo
// Hard to visualise graph
AB.headerRHS();
AB.newDiv("selectdiv");
$("#selectdiv").css({
"margin": "30",
});
$("#selectdiv").html(" Pick an actor: <span id='actorlist'></span> ");
// Daniel Shiffman
// Nature of Code: Intelligence and Learning
// https://github.com/shiffman/NOC-S17-2-Intelligence-Learning
// Graph object
function Graph() {
// store nodes by key and also in an array
this.graph = {};
this.nodes = [];
// Start and end
this.start = null;
this.end = null;
}
// Set the start
Graph.prototype.setStart = function (node) {
this.start = node;
}
// Set the end
Graph.prototype.setEnd = function (node) {
this.end = node;
}
// Add a node
Graph.prototype.addNode = function (label) {
let n = new Node(label);
// Add to both the graph object and the nodes array
this.graph[label] = n;
this.nodes.push(n);
return n;
}
// Clear all searched and parent values
Graph.prototype.clear = function () {
for (const element of this.nodes) {
element.searched = false;
element.parent = null;
}
}
// Daniel Shiffman
// Nature of Code: Intelligence and Learning
// https://github.com/shiffman/NOC-S17-2-Intelligence-Learning
// Node object
function Node(label) {
// Has a label
this.label = label;
// zero or more edges
this.edges = [];
// No parent and not searched by default
this.parent = null;
this.searched = false;
this.actor = false;
this.film = false;
}
// Connect one or more neighbors
Node.prototype.connect = function (neighbour) // one Node (movie) has a connection to another Node (person)
{
// This is a fancy way of having a function
// that can accept a variable number of arguments
for (const element of arguments) {
this.edges.push(element); // increase the size of the array
// Connect both ways
element.edges.push(this);
}
}
// Daniel Shiffman
// Nature of Code: Intelligence and Learning
// https://github.com/shiffman/NOC-S17-2-Intelligence-Learning
// The data
let data;
// The graph
let graph;
// A lookup table for actors
// (redundant, the graph could handle this)
let actors;
// Dropdown menu for actors
let actorlist;
function preload() {
data = loadJSON('/uploads/tristan/exercise3.9-breadth-first-bacon.json');
}
function setup() {
noCanvas();
// Create the graph
graph = new Graph();
// A separate lookup table for actors
actors = {};
// For all movies
for (const movie of data.movies) {
// Add the movie to the graph
let movieNode = graph.addNode(movie.title); // Node movie
movieNode.film = true;
// Go through all the cast list
for (const cast of movie.cast) {
let actorNode;
// Does the actor exist already?
if (actors[cast]) {
actorNode = actors[cast];
}
// If not add a new node
else {
actorNode = graph.addNode(cast); // Node person
actors[cast] = actorNode;
}
actorNode.actor = true;
// Connect movie and actor
movieNode.connect(actorNode); // movie connects to person
}
}
// Create dropdown
actorlist = createSelect(); // https://p5js.org/reference/#/p5/createSelect
actorlist.parent('actorlist');
// Add all the actors
for (const actor of Object.keys(actors)) {
actorlist.option(actor);
}
// Set up an event
actorlist.changed(findbacon);
}
function findbacon() {
// Clear everyone from having been searched
graph.clear();
// Start and end
let start = actors[actorlist.value()];
let end = actors['Kevin Bacon'];
graph.setStart(start);
graph.setEnd(end);
// Run the search!
bfs();
}
function reachedDestination(node) {
console.log("found goal node: " + node.label);
let path = [];
// Figure out the path
path.push(node);
let next = node.parent;
while (next) {
path.push(next);
next = next.parent;
}
return path;
}
function bfs() {
// Create a queue ad path
let queue = [];
let path = [];
// Get started
queue.push(graph.start);
while (queue.length > 0) // inside the loop we keep adding things to queue
{
let node = queue.shift(); // remove first item
console.log("======");
console.log("start of search of " + node.label);
// Are we done?
if (node === graph.end) {
path = reachedDestination(node);
break;
} else if (node.searched) {
// TE Bug-fix
console.log("previously search so skip: " + node.label);
} else {
console.log("checking neighbours of " + node.label);
// Check all neighbors
// console.log ("checking " + next.length + " neighbours");
for (const neighbour of node.edges) {
// Any neighbour not already searched add to queue
if (!neighbour.searched) {
if (neighbour === graph.end) {
// TE Bug-fix
// Update the parent
neighbour.parent = node;
path = reachedDestination(neighbour);
queue = [];
break
} else {
console.log("adding unsearched neighbour " + neighbour.label + " to queue");
queue.push(neighbour);
// Update the parent
neighbour.parent = node;
}
}
}
// Mark node as searched
node.searched = true;
console.log("end of search of " + node.label);
// console.log ("======" );
}
} // end of while loop
// now output path
console.log('finished search for path');
// path = bacon + movie + x + movie + x ... + movie + x
let s = '<h1> Path length ' + path.length + '</h1>';
if (path.length > 0) {
let pIdx = 1;
for (let i = path.length - 1; i >= 0; i--) {
const node = path[i];
const first = i === path.length - 1;
s += (first ? '' : '<br/>') + pIdx + ') ';
const label = '<span style="font-weight: bold;background-color:' + (node.actor ? 'aqua' : 'burlywood') + ';">' + node.label + '</span>';
if (node.actor) {
s += i === path.length - 1 ? ('Actor ' + label) : (' along with actor ' + label);
} else {
s += '>>> who played in the film ' + label;
}
pIdx++;
}
} else {
s += 'Actor <b>' + graph.start.label + '</b> has no path to actor <b>' + graph.end.label + '</b>';
}
AB.newDiv("theoutput");
$("#theoutput").css({
"margin": "30",
"padding": "30",
"display": "inline-block",
"border": "1px solid black"
});
$("#theoutput").html(s);
}