-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathGraph.js
More file actions
37 lines (30 loc) · 820 Bytes
/
Graph.js
File metadata and controls
37 lines (30 loc) · 820 Bytes
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
import Vertex from "./Vertex";
import Edge from "./Edge";
class Graph {
nodes;
links;
constructor(nodes, links) {
this.nodes = [];
this.links = [];
nodes.forEach((n) => this.nodes.push(new Vertex(n)));
links.forEach((l) => this.links.push(new Edge(l)));
}
addNode(newNode) {
this.nodes.push(new Vertex(newNode));
}
addEdge(newEdge) {
this.links.push(new Edge(newEdge));
}
deleteNode(delNode) {
this.nodes = this.nodes.filter((n) => n.vertex.id != delNode.id);
this.links = this.links.filter(
(l) => l.edge.source.id != delNode.id && l.edge.target.id != delNode.id
);
}
deleteEdge(delEdge) {
this.links = this.links.filter(
(l) => l.edge.source != delEdge.source || l.edge.target != delEdge.target
);
}
}
export { Graph as default };