-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.js
68 lines (56 loc) · 1.77 KB
/
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
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
export class Graph {
nodes = new Array();
successors = new Array();
edges = new Array();
addNode(node) {
this.nodes.push(node);
this.successors.push([]);
}
addEdge(i, j, edge) {
edge.start = i;
edge.end = j;
edge.A = this.nodes[i];
edge.B = this.nodes[j];
if (edge.capacity == undefined)
edge.capacity = 1;
if (edge.flow == undefined)
edge.flow = 0;
this.successors[i].push(edge);
this.edges.push(edge);
}
edge(i, j) {
for (const edge of this.edges)
if (edge.start == i && edge.end == j)
return edge;
}
edgesFrom(i) {
return this.successors[i];
}
node(i) { return this.nodes[i] }
get nbNodes() { return this.nodes.length };
draw(context, style) {
context.clearRect(0, 0, 1000, 1000)
for (let i = 0; i < this.successors.length; i++) {
for (let edge of this.successors[i]) {
const A = this.nodes[edge.start];
const B = this.nodes[edge.end];
style.drawEdge(context, A, B, edge, this);
}
}
for (const node of this.nodes) {
style.drawNode(context, node, this);
}
if (style.drawEdgeOver)
for (let i = 0; i < this.successors.length; i++) {
for (let edge of this.successors[i]) {
const A = this.nodes[edge.start];
const B = this.nodes[edge.end];
style.drawEdgeOver(context, A, B, edge, this);
}
}
if (style.drawNodeOver)
for (const node of this.nodes) {
style.drawNodeOver(context, node, this);
}
}
}