-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.js
29 lines (27 loc) · 919 Bytes
/
graph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var graph = {
you: ['alice', 'bob', 'claire'],
alice: ['peggy'],
bob: ['anuj', 'peggy'],
claire: ['thom', 'jonny']
};
function findSomebody(graph, sb) {
var queue = graph.you;
var searched = {};
while(queue.length) {
var person = queue[0];
console.log(person);
if(!searched[person]) {
searched[person] = null;
if (person === sb) {
console.log(`find ${sb}!!!`)
return;
} else {
queue = queue.concat(graph[person]);
queue.splice(0, 1);
}
}
}
console.log(`no ${sb} is found`);
return -1;
}
findSomebody(graph, 'peggy');