-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.js
196 lines (166 loc) · 4.49 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Generated by CoffeeScript 1.6.3
/*
This module provides a layer of indirection between implementations of
(directed) graphs and client code.
At the minimum, a graph instance needs to implement three methods,
each returning an array of vertices:
vertices : () -> ... # The vertices of the graph
predecessors: (v) -> ... # The direct predecessors of vertex v
successors : (v) -> ... # The direct successors of vertex v
Further methods will be used where present. For example, the function call
`equals(G, H)` will defer to `G.equals(H)` if the graph `G` has an attribute
`"equals"`.
*/
var adjacent, edges, equals, isEdge, isInternal, isIsolated, isSink, isSource, isVertex, predecessors, successors, vertices;
vertices = function(G) {
/* The vertices of the given graph as an array.*/
return G.vertices();
};
predecessors = function(G, v) {
/* The immediate predecessors of vertex `v` in graph `G` as an array.*/
return G.predecessors(v);
};
successors = function(G, v) {
/* The immediate successors of vertex `v` in graph `G` as an array.*/
return G.successors(v);
};
isVertex = function(G, v) {
/* Tests whether `v` is a vertex of the graph `G`.*/
if (G.isVertex != null) {
return G.isVertex(v);
} else {
return vertices(G).indexOf(v) >= 0;
}
};
isEdge = function(G, v, w) {
/* Tests whether `[v, w]` is a directed edge of the graph `G`.*/
if (G.isEdge != null) {
return G.isEdge(v, w);
} else {
return successors(G, v).indexOf(w) >= 0;
}
};
isSource = function(G, v) {
/* Tests whether `v` is a vertex in `G` with no predecessors.*/
if (G.isSource != null) {
return G.isSource(v);
} else {
return predecessors(G, v).length === 0;
}
};
isSink = function(G, v) {
/* Tests whether `v` is a vertex in `G` with no successors.*/
if (G.isSink != null) {
return G.isSink(v);
} else {
return successors(G, v).length === 0;
}
};
isInternal = function(G, v) {
/*
Tests whether `v` is a vertex in `G` with both predecessors and successors.
*/
if (G.isInternal != null) {
return G.isInternal(v);
} else {
return isVertex(G, v) && !(isSource(G, v) || isSink(G, v));
}
};
isIsolated = function(G, v) {
/*
Tests whether `v` is a vertex in `G` neither predecessors nor successors.
*/
if (G.isIsolated != null) {
return G.isIsolated(v);
} else {
return isVertex(G, v) && isSource(G, v) && isSink(G, v);
}
};
edges = function(G) {
/* The directed edges of the graph `G` as an array of pairs.*/
var a, v, w, _i, _j, _len, _len1, _ref, _ref1;
if (G.edges != null) {
return G.edges();
} else {
a = [];
_ref = vertices(G);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
v = _ref[_i];
_ref1 = successors(G, v);
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
w = _ref1[_j];
a.push([v, w]);
}
}
return a;
}
};
adjacent = function(G, v) {
/* All the vertices adjacent to `v` in the graph `G` as an array.*/
var a, w, _i, _len, _ref;
if (G.adjacent != null) {
return G.adjacent(v);
} else {
a = predecessors(G, v);
_ref = successors(G, v);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
w = _ref[_i];
if (!(a.indexOf(w) >= 0)) {
a.push(w);
}
}
return a;
}
};
equals = function(G, H) {
/*
Tests whether the graphs `G` and `H` are equal in the sense that they have
the same sets of vertices and directed edges.
*/
var eq, v, _i, _len, _ref;
if (G.equals != null) {
return G.equals(H);
} else {
eq = function(a, b) {
var i, _i, _ref;
a = a.slice(0).sort();
b = b.slice(0).sort();
if (a.length !== b.length) {
return false;
} else {
for (i = _i = 0, _ref = a.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
};
if (!eq(vertices(G), vertices(H))) {
return false;
} else {
_ref = vertices(G);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
v = _ref[_i];
if (!eq(successors(G, v), successors(H, v))) {
return false;
}
}
return true;
}
}
};
module.exports = {
vertices: vertices,
predecessors: predecessors,
successors: successors,
isVertex: isVertex,
isSource: isSource,
isSink: isSink,
isInternal: isInternal,
isIsolated: isIsolated,
isEdge: isEdge,
edges: edges,
adjacent: adjacent,
equals: equals
};