-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplement-graph.mjs
49 lines (40 loc) · 903 Bytes
/
implement-graph.mjs
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
export class Graph {
constructor() {
this.nodes = new Map();
}
addNode(value) {
this.nodes.set(value, []);
}
addEdge(origin, destination) {
this.nodes.has(origin)
? this.nodes.get(origin).push(destination)
: this.nodes.set(origin, [destination]);
this.nodes.has(destination)
? this.nodes.get(destination).push(origin)
: this.nodes.set(destination, [origin]);
}
getEdges(origin) {
return this.nodes.get(origin);
}
}
export const routes = [
['PHX', 'LAX'],
['PHX', 'JFK'],
['JFK', 'OKC'],
['JFK', 'HEL'],
['JFK', 'LOS'],
['MEX', 'LAX'],
['MEX', 'BKK'],
['MEX', 'LIM'],
['MEX', 'EZE'],
['LIM', 'BKK'],
];
export function createGraph(edges = routes) {
const graph = new Graph();
edges.forEach((route) => {
graph.addEdge(...route);
});
return graph;
}
const graph = createGraph(routes);
console.log(graph);