偽代碼
// 構(gòu)件圖
const graph = {};
graph['you'] = ['alice', 'bob', 'claire'];
graph['alice'] = ['peggy'];
graph['claire'] = ['thom', 'jonny'];
graph['peggy'] = [];
graph['thom'] = [];
graph['jonny'] = [];
function BFS () {
let person = null;
const queue = [];
queue.push(...graph['you']);
while(graph.length > 0) {
person = graph.shift();
if (isSeller(person)) {
console.log(`${person} is seller`);
return true;
} else {
queue.push(...graph[person])
}
}
return false;
}