I have implemented a breadth-first search and I'm more or less looking for suggestions on how I can improve on what I've done here.
How to use:
search(queue, "Musician");
The logs from the search:
Lansana is not a Musician. Lansana is a Software Engineer. Zack is not a Musician. Zack is a Software Architect. John is not a Musician. John is a Project Manager. Francisco is not a Musician. Francisco is a Game Developer. Ronaldinho is not a Musician. Ronaldinho is a Professional Soccer Player. Lauren is not a Musician. Lauren is a Mango Seller. Angelica is not a Musician. Angelica is a Professional Volleyball Player. Cody is not a Musician. Cody is a Network Programmer. Match found: Beyonce is a Musician!
I believe it works correctly because if you look at the order in which I enqueue people, and the order in which their friends
are enqueued, you'll notice that the logs match the exact order (which of course is crucial in this algorithm in order to get the shortest path).
class Queue {
constructor() {
this.count = 0;
this.data = {};
}
enqueue(key, val) {
if (!this.data.hasOwnProperty(key)) {
this.count++;
}
this.data[key] = val;
}
dequeue() {
let key = Object.keys(this.data)[0],
memo = this.data[key];
delete this.data[key];
this.count--;
return memo;
}
}
class Person {
constructor(args) {
this.name = args.name;
this.friends = args.friends || [];
this.profession = args.profession;
}
}
let Ronaldinho = new Person({
name: "Ronaldinho",
profession: "Professional Soccer Player"
}),
Lauren = new Person({
name: "Lauren",
profession: "Mango Seller"
}),
Angelica = new Person({
name: "Angelica",
profession: "Professional Volleyball Player"
}),
Cody = new Person({
name: "Cody",
profession: "Network Programmer"
}),
Beyonce = new Person({
name: "Beyonce",
profession: "Musician"
}),
Lansana = new Person({
name: "Lansana",
friends: [Ronaldinho, Lauren, Angelica],
profession: "Software Engineer"
}),
Zack = new Person({
name: "Zack",
friends: [Cody],
profession: "Software Architect"
}),
John = new Person({
name: "John",
friends: [Ronaldinho, Angelica],
profession: "Project Manager"
}),
Francisco = new Person({
name: "Francisco",
friends: [Beyonce],
profession: "Game Developer"
});
let queue = new Queue();
queue.enqueue("Lansana", Lansana);
queue.enqueue("Zack", Zack);
queue.enqueue("John", John);
queue.enqueue("Francisco", Francisco);
function search(queue, profession) {
let searched = [];
while (queue.count > 0) {
let person = queue.dequeue();
if (!searched[person.name]) {
if (hasProfession(person, profession)) {
console.log(`Match found: ${person.name} is a ${person.profession}!`);
return true;
} else {
console.log(`${person.name} is not a ${profession}. ${person.name} is a ${person.profession}.`);
for(let i = 0, len = person.friends.length; i < len; i++) {
queue.enqueue(person.friends[i].name, person.friends[i]);
}
}
searched.push(person.name);
}
}
return false;
}
function hasProfession(person, profession) {
return person.profession.toLowerCase() === profession.toLowerCase();
}