-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortestRoute.js
73 lines (63 loc) · 2.38 KB
/
shortestRoute.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Given information about active users on the network, find the shortest route for a message from one user (the sender)
// to another (the recipient). Return an array of users that make up this route.
// There might be a few shortest delivery routes, all with the same length. For now, let's just return any shortest route.
// Your network information takes the form of an object where keys are usernames and values are arrays of other users nearby:
// var network = {
// 'Min' : ['William', 'Jayden', 'Omar'],
// 'William' : ['Min', 'Noam'],
// 'Jayden' : ['Min', 'Amelia', 'Ren', 'Noam'],
// 'Ren' : ['Jayden', 'Omar'],
// 'Amelia' : ['Jayden', 'Adam', 'Miguel'],
// 'Adam' : ['Amelia', 'Miguel', 'Sofia', 'Lucas'],
// 'Miguel' : ['Amelia', 'Adam', 'Liam', 'Nathan'],
// 'Noam' : ['Nathan', 'Jayden', 'William'],
// 'Omar' : ['Ren', 'Min', 'Scott'],
// ...
// };
// For the network above, a message from Jayden to Adam should have this route:
// ['Jayden', 'Amelia', 'Adam']
function createPath(path, start, end) {
let route = [];
let current = end;
while (current) {
route.unshift(current);
current = path[current];
}
return route;
}
function shortestRoute(network, start, end) {
let queue = [start];
let path = { [start]: null };
while (queue.length) {
let node = queue.shift();
if (node === end) {
return createPath(path, start, end);
}
network[node].forEach((user) => {
if (!path.hasOwnProperty(user)) {
path[user] = node;
queue.push(user);
}
});
}
}
var network = {
'Min' : ['William', 'Jayden', 'Omar'],
'William' : ['Min', 'Noam'],
'Jayden' : ['Min', 'Amelia', 'Ren', 'Noam'],
'Ren' : ['Jayden', 'Omar'],
'Amelia' : ['Jayden', 'Adam', 'Miguel'],
'Adam' : ['Amelia', 'Miguel', 'Sofia', 'Lucas'],
'Miguel' : ['Amelia', 'Adam', 'Liam', 'Nathan'],
'Noam' : ['Nathan', 'Jayden', 'William'],
'Omar' : ['Ren', 'Min', 'Scott'],
};
function assertEquals(a, b, desc) {
if (JSON.stringify(a) === JSON.stringify(b)) {
console.log(`${desc} ... PASS`);
} else {
console.log(`${desc} ... FAIL: ${a} !== ${b}`);
}
}
desc = 'Short path';
assertEquals(shortestRoute(network, 'Jayden', 'Adam'), ['Jayden', 'Amelia', 'Adam'], desc);