-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
42 lines (38 loc) · 1.04 KB
/
graph.js
File metadata and controls
42 lines (38 loc) · 1.04 KB
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
module.exports = class Graph {
constructor() {
this.nodes = new Map();
}
addNode(node) {
if (!this.nodes.has(node)) {
this.nodes.set(node, []);
}
}
addEdge(source, destination, length, edgeId) {
this.nodes.get(source).push({id: destination, length: length, edgeId: edgeId});
}
getNodes() {
let nodes = []
for (let [node] of this.nodes) {
if (!nodes.some(el => el.id === node.id)) {
nodes.push(node);
}
}
return nodes;
}
getEdges() {
let edges = [];
for (let [node, neighbors] of this.nodes) {
let edge = null;
for (let i = 0; i < neighbors.length; i++) {
edge = {
id: neighbors[i].edgeId,
length: neighbors[i].length,
source: node.id,
target: neighbors[i].id.id
};
edges.push(edge);
}
}
return edges;
}
}