graph-dynamic 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/README.md +102 -0
- package/dist/graph-alg.min.js +1 -0
- package/dist/graph-settings.min.js +1 -0
- package/dist/graphlib.core.js +1396 -0
- package/dist/graphlib.core.min.js +304 -0
- package/dist/graphlib.js +1396 -0
- package/dist/graphlib.min.js +304 -0
- package/index.d.ts +621 -0
- package/index.js +38 -0
- package/lib/alg/components.js +25 -0
- package/lib/alg/dfs.js +67 -0
- package/lib/alg/dijkstra-all.js +10 -0
- package/lib/alg/dijkstra.js +53 -0
- package/lib/alg/find-cycles.js +9 -0
- package/lib/alg/floyd-warshall.js +48 -0
- package/lib/alg/index.js +13 -0
- package/lib/alg/is-acyclic.js +15 -0
- package/lib/alg/postorder.js +7 -0
- package/lib/alg/preorder.js +7 -0
- package/lib/alg/prim.js +51 -0
- package/lib/alg/tarjan.js +45 -0
- package/lib/alg/topsort.js +36 -0
- package/lib/data/priority-queue.js +150 -0
- package/lib/graph.js +703 -0
- package/lib/index.js +5 -0
- package/lib/json.js +80 -0
- package/lib/version.js +1 -0
- package/package.json +50 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
// Type definitions for graph-dynamic 2.1.1
|
|
2
|
+
// Project: https://github.com/Graph-Node-Org/graph-dynamic
|
|
3
|
+
// Definitions by: Dan Vanderkam <http://danvk.org/>, Dan Mironenko <wolfson@bracketedrebels.com>
|
|
4
|
+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
|
5
|
+
|
|
6
|
+
declare module "Graph-Node-Org/graph-dynamic" {
|
|
7
|
+
export interface GraphOptions {
|
|
8
|
+
directed?: boolean; // default: true.
|
|
9
|
+
multigraph?: boolean; // default: false.
|
|
10
|
+
compound?: boolean; // default: false.
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Edge {
|
|
14
|
+
v: string;
|
|
15
|
+
w: string;
|
|
16
|
+
/** The name that uniquely identifies a multi-edge. */
|
|
17
|
+
name?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class Graph {
|
|
21
|
+
constructor(options?: GraphOptions);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Sets the default node label. This label will be assigned as default label
|
|
25
|
+
* in case if no label was specified while setting a node.
|
|
26
|
+
* Complexity: O(1).
|
|
27
|
+
*
|
|
28
|
+
* @argument label - default node label.
|
|
29
|
+
* @returns the graph, allowing this to be chained with other functions.
|
|
30
|
+
*/
|
|
31
|
+
setDefaultNodeLabel(label: any): Graph;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Sets the default node label factory function. This function will be invoked
|
|
35
|
+
* each time when setting a node with no label specified and returned value
|
|
36
|
+
* will be used as a label for node.
|
|
37
|
+
* Complexity: O(1).
|
|
38
|
+
*
|
|
39
|
+
* @argument labelFn - default node label factory function.
|
|
40
|
+
* @returns the graph, allowing this to be chained with other functions.
|
|
41
|
+
*/
|
|
42
|
+
setDefaultNodeLabel(labelFn: (v: string) => any): Graph;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Creates or updates the value for the node v in the graph. If label is supplied
|
|
46
|
+
* it is set as the value for the node. If label is not supplied and the node was
|
|
47
|
+
* created by this call then the default node label will be assigned.
|
|
48
|
+
* Complexity: O(1).
|
|
49
|
+
*
|
|
50
|
+
* @argument name - node name.
|
|
51
|
+
* @argument label - value to set for node.
|
|
52
|
+
* @returns the graph, allowing this to be chained with other functions.
|
|
53
|
+
*/
|
|
54
|
+
setNode(name: string, label?: any): Graph;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Invokes setNode method for each node in names list.
|
|
58
|
+
* Complexity: O(|names|).
|
|
59
|
+
*
|
|
60
|
+
* @argument names - list of nodes names to be set.
|
|
61
|
+
* @argument label - value to set for each node in list.
|
|
62
|
+
* @returns the graph, allowing this to be chained with other functions.
|
|
63
|
+
*/
|
|
64
|
+
setNodes(names: string[], label?: any): Graph;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Sets node p as a parent for node v if it is defined, or removes the
|
|
68
|
+
* parent for v if p is undefined. Method throws an exception in case of
|
|
69
|
+
* invoking it in context of noncompound graph.
|
|
70
|
+
* Average-case complexity: O(1).
|
|
71
|
+
*
|
|
72
|
+
* @argument v - node to be child for p.
|
|
73
|
+
* @argument p - node to be parent for v.
|
|
74
|
+
* @returns the graph, allowing this to be chained with other functions.
|
|
75
|
+
*/
|
|
76
|
+
setParent(v: string, p?: string): Graph;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Gets parent node for node v.
|
|
80
|
+
* Complexity: O(1).
|
|
81
|
+
*
|
|
82
|
+
* @argument v - node to get parent of.
|
|
83
|
+
* @returns parent node name or void if v has no parent.
|
|
84
|
+
*/
|
|
85
|
+
parent(v: string): string | void;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Gets list of direct children of node v.
|
|
89
|
+
* Complexity: O(1).
|
|
90
|
+
*
|
|
91
|
+
* @argument v - node to get children of.
|
|
92
|
+
* @returns children nodes names list.
|
|
93
|
+
*/
|
|
94
|
+
children(v: string): string[];
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Creates new graph with nodes filtered via filter. Edges incident to rejected node
|
|
98
|
+
* are also removed. In case of compound graph, if parent is rejected by filter,
|
|
99
|
+
* than all its children are rejected too.
|
|
100
|
+
* Average-case complexity: O(|E|+|V|).
|
|
101
|
+
*
|
|
102
|
+
* @argument filter - filtration function detecting whether the node should stay or not.
|
|
103
|
+
* @returns new graph made from current and nodes filtered.
|
|
104
|
+
*/
|
|
105
|
+
filterNodes(filter: (v: string) => boolean): Graph;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Sets the default edge label. This label will be assigned as default label
|
|
109
|
+
* in case if no label was specified while setting an edge.
|
|
110
|
+
* Complexity: O(1).
|
|
111
|
+
*
|
|
112
|
+
* @argument label - default edge label.
|
|
113
|
+
* @returns the graph, allowing this to be chained with other functions.
|
|
114
|
+
*/
|
|
115
|
+
setDefaultEdgeLabel(label: any): Graph;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Sets the default edge label factory function. This function will be invoked
|
|
119
|
+
* each time when setting an edge with no label specified and returned value
|
|
120
|
+
* will be used as a label for edge.
|
|
121
|
+
* Complexity: O(1).
|
|
122
|
+
*
|
|
123
|
+
* @argument labelFn - default edge label factory function.
|
|
124
|
+
* @returns the graph, allowing this to be chained with other functions.
|
|
125
|
+
*/
|
|
126
|
+
setDefaultEdgeLabel(labelFn: (v: string) => any): Graph;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Establish an edges path over the nodes in nodes list. If some edge is already
|
|
130
|
+
* exists, it will update its label, otherwise it will create an edge between pair
|
|
131
|
+
* of nodes with label provided or default label if no label provided.
|
|
132
|
+
* Complexity: O(|nodes|).
|
|
133
|
+
*
|
|
134
|
+
* @argument nodes - list of nodes to be connected in series.
|
|
135
|
+
* @argument label - value to set for each edge between pairs of nodes.
|
|
136
|
+
* @returns the graph, allowing this to be chained with other functions.
|
|
137
|
+
*/
|
|
138
|
+
setPath(nodes: string[], label?: any): Graph;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Detects whether graph has a node with specified name or not.
|
|
142
|
+
|
|
143
|
+
*
|
|
144
|
+
* @argument name - name of the node.
|
|
145
|
+
* @returns true if graph has node with specified name, false - otherwise.
|
|
146
|
+
*/
|
|
147
|
+
hasNode(name: string): boolean;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Remove the node with the name from the graph or do nothing if the node is not in
|
|
151
|
+
* the graph. If the node was removed this function also removes any incident
|
|
152
|
+
* edges.
|
|
153
|
+
* Complexity: O(1).
|
|
154
|
+
*
|
|
155
|
+
* @argument name - name of the node.
|
|
156
|
+
* @returns the graph, allowing this to be chained with other functions.
|
|
157
|
+
*/
|
|
158
|
+
removeNode(name: string): Graph;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Gets all nodes of the graph. Note, the in case of compound graph subnodes are
|
|
162
|
+
* not included in list.
|
|
163
|
+
* Complexity: O(1).
|
|
164
|
+
*
|
|
165
|
+
* @returns list of graph nodes.
|
|
166
|
+
*/
|
|
167
|
+
nodes(): string[];
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Gets the label of node with specified name.
|
|
171
|
+
* Complexity: O(|V|).
|
|
172
|
+
*
|
|
173
|
+
* @returns label value of the node.
|
|
174
|
+
*/
|
|
175
|
+
node(name: string): any;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Creates or updates the label for the edge (v, w) with the optionally supplied
|
|
179
|
+
* name. If label is supplied it is set as the value for the edge. If label is not
|
|
180
|
+
* supplied and the edge was created by this call then the default edge label will
|
|
181
|
+
* be assigned. The name parameter is only useful with multigraphs.
|
|
182
|
+
* Complexity: O(1).
|
|
183
|
+
*
|
|
184
|
+
* @argument v - edge source node.
|
|
185
|
+
* @argument w - edge sink node.
|
|
186
|
+
* @argument label - value to associate with the edge.
|
|
187
|
+
* @argument name - unique name of the edge in order to identify it in multigraph.
|
|
188
|
+
* @returns the graph, allowing this to be chained with other functions.
|
|
189
|
+
*/
|
|
190
|
+
setEdge(v: string, w: string, label?: any, name?: string): Graph;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Creates or updates the label for the specified edge. If label is supplied it is
|
|
194
|
+
* set as the value for the edge. If label is not supplied and the edge was created
|
|
195
|
+
* by this call then the default edge label will be assigned. The name parameter is
|
|
196
|
+
* only useful with multigraphs.
|
|
197
|
+
* Complexity: O(1).
|
|
198
|
+
*
|
|
199
|
+
* @argument edge - edge descriptor.
|
|
200
|
+
* @argument label - value to associate with the edge.
|
|
201
|
+
* @returns the graph, allowing this to be chained with other functions.
|
|
202
|
+
*/
|
|
203
|
+
setEdge(edge: Edge, label?: any): Graph;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Gets edges of the graph. In case of compound graph subgraphs are not considered.
|
|
207
|
+
* Complexity: O(|E|).
|
|
208
|
+
*
|
|
209
|
+
* @return graph edges list.
|
|
210
|
+
*/
|
|
211
|
+
edges(): Edge[];
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Gets the label for the specified edge.
|
|
215
|
+
* Complexity: O(1).
|
|
216
|
+
*
|
|
217
|
+
* @argument v - edge source node.
|
|
218
|
+
* @argument w - edge sink node.
|
|
219
|
+
* @argument name - name of the edge (actual for multigraph).
|
|
220
|
+
* @returns value associated with specified edge.
|
|
221
|
+
*/
|
|
222
|
+
edge(v: string, w: string, name?: string): any;
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Gets the label for the specified edge.
|
|
226
|
+
* Complexity: O(1).
|
|
227
|
+
*
|
|
228
|
+
* @argument edge - edge descriptor.
|
|
229
|
+
* @returns value associated with specified edge.
|
|
230
|
+
*/
|
|
231
|
+
edge(e: Edge): any;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Gets the label for the specified edge and converts it to an object.
|
|
235
|
+
* Complexity: O(1).
|
|
236
|
+
*
|
|
237
|
+
* @argument v - edge source node.
|
|
238
|
+
* @argument w - edge sink node.
|
|
239
|
+
* @argument name - name of the edge (actual for multigraph).
|
|
240
|
+
* @returns value associated with specified edge.
|
|
241
|
+
*/
|
|
242
|
+
edgeAsObj(v: string, w: string, name?: string): Object;
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Gets the label for the specified edge and converts it to an object.
|
|
246
|
+
* Complexity: O(1).
|
|
247
|
+
*
|
|
248
|
+
* @argument edge - edge descriptor.
|
|
249
|
+
* @returns value associated with specified edge.
|
|
250
|
+
*/
|
|
251
|
+
edgeAsObj(e: Edge): Object;
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Detects whether the graph contains specified edge or not. No subgraphs are considered.
|
|
255
|
+
* Complexity: O(1).
|
|
256
|
+
*
|
|
257
|
+
* @argument v - edge source node.
|
|
258
|
+
* @argument w - edge sink node.
|
|
259
|
+
* @argument name - name of the edge (actual for multigraph).
|
|
260
|
+
* @returns whether the graph contains the specified edge or not.
|
|
261
|
+
*/
|
|
262
|
+
hasEdge(v: string, w: string, name?: string): boolean;
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Detects whether the graph contains specified edge or not. No subgraphs are considered.
|
|
266
|
+
* Complexity: O(1).
|
|
267
|
+
*
|
|
268
|
+
* @argument edge - edge descriptor.
|
|
269
|
+
* @returns whether the graph contains the specified edge or not.
|
|
270
|
+
*/
|
|
271
|
+
hasEdge(edge: Edge): boolean;
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Removes the specified edge from the graph. No subgraphs are considered.
|
|
275
|
+
* Complexity: O(1).
|
|
276
|
+
*
|
|
277
|
+
* @argument edge - edge descriptor.
|
|
278
|
+
* @returns the graph, allowing this to be chained with other functions.
|
|
279
|
+
*/
|
|
280
|
+
removeEdge(edge: Edge): Graph;
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Removes the specified edge from the graph. No subgraphs are considered.
|
|
284
|
+
* Complexity: O(1).
|
|
285
|
+
*
|
|
286
|
+
* @argument v - edge source node.
|
|
287
|
+
* @argument w - edge sink node.
|
|
288
|
+
* @argument name - name of the edge (actual for multigraph).
|
|
289
|
+
* @returns the graph, allowing this to be chained with other functions.
|
|
290
|
+
*/
|
|
291
|
+
removeEdge(v: string, w: string, name?: string): Graph;
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Return all edges that point to the node v. Optionally filters those edges down to just those
|
|
295
|
+
* coming from node u. Behavior is undefined for undirected graphs - use nodeEdges instead.
|
|
296
|
+
* Complexity: O(|E|).
|
|
297
|
+
*
|
|
298
|
+
* @argument v - edge sink node.
|
|
299
|
+
* @argument w - edge source node.
|
|
300
|
+
* @returns edges descriptors list if v is in the graph, or undefined otherwise.
|
|
301
|
+
*/
|
|
302
|
+
inEdges(v: string, w?: string): void | Edge[];
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Return all edges that are pointed at by node v. Optionally filters those edges down to just
|
|
306
|
+
* those point to w. Behavior is undefined for undirected graphs - use nodeEdges instead.
|
|
307
|
+
* Complexity: O(|E|).
|
|
308
|
+
*
|
|
309
|
+
* @argument v - edge source node.
|
|
310
|
+
* @argument w - edge sink node.
|
|
311
|
+
* @returns edges descriptors list if v is in the graph, or undefined otherwise.
|
|
312
|
+
*/
|
|
313
|
+
outEdges(v: string, w?: string): void | Edge[];
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Returns all edges to or from node v regardless of direction. Optionally filters those edges
|
|
317
|
+
* down to just those between nodes v and w regardless of direction.
|
|
318
|
+
* Complexity: O(|E|).
|
|
319
|
+
*
|
|
320
|
+
* @argument v - edge adjacent node.
|
|
321
|
+
* @argument w - edge adjacent node.
|
|
322
|
+
* @returns edges descriptors list if v is in the graph, or undefined otherwise.
|
|
323
|
+
*/
|
|
324
|
+
nodeEdges(v: string, w?: string): void | Edge[];
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Return all nodes that are predecessors of the specified node or undefined if node v is not in
|
|
328
|
+
* the graph. Behavior is undefined for undirected graphs - use neighbors instead.
|
|
329
|
+
* Complexity: O(|V|).
|
|
330
|
+
*
|
|
331
|
+
* @argument v - node identifier.
|
|
332
|
+
* @returns node identifiers list or undefined if v is not in the graph.
|
|
333
|
+
*/
|
|
334
|
+
predecessors(v: string): void | string[];
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Return all nodes that are successors of the specified node or undefined if node v is not in
|
|
338
|
+
* the graph. Behavior is undefined for undirected graphs - use neighbors instead.
|
|
339
|
+
* Complexity: O(|V|).
|
|
340
|
+
*
|
|
341
|
+
* @argument v - node identifier.
|
|
342
|
+
* @returns node identifiers list or undefined if v is not in the graph.
|
|
343
|
+
*/
|
|
344
|
+
successors(v: string): void | string[];
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Return all nodes that are predecessors or successors of the specified node or undefined if
|
|
348
|
+
* node v is not in the graph.
|
|
349
|
+
* Complexity: O(|V|).
|
|
350
|
+
*
|
|
351
|
+
* @argument v - node identifier.
|
|
352
|
+
* @returns node identifiers list or undefined if v is not in the graph.
|
|
353
|
+
*/
|
|
354
|
+
|
|
355
|
+
neighbors(v: string): void | string[];
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Whether graph was created with 'directed' flag set to true or not.
|
|
359
|
+
*
|
|
360
|
+
* @returns whether the graph edges have an orientation.
|
|
361
|
+
*/
|
|
362
|
+
isDirected(): boolean;
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Whether graph was created with 'multigraph' flag set to true or not.
|
|
366
|
+
*
|
|
367
|
+
* @returns whether the pair of nodes of the graph can have multiple edges.
|
|
368
|
+
*/
|
|
369
|
+
isMultigraph(): boolean;
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Whether graph was created with 'compound' flag set to true or not.
|
|
373
|
+
*
|
|
374
|
+
* @returns whether a node of the graph can have subnodes.
|
|
375
|
+
*/
|
|
376
|
+
isCompound(): boolean;
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Sets the label of the graph.
|
|
380
|
+
*
|
|
381
|
+
* @argument label - label value.
|
|
382
|
+
* @returns the graph, allowing this to be chained with other functions.
|
|
383
|
+
*/
|
|
384
|
+
setGraph(label: any): Graph;
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Gets the graph label.
|
|
388
|
+
*
|
|
389
|
+
* @returns currently assigned label for the graph or undefined if no label assigned.
|
|
390
|
+
*/
|
|
391
|
+
graph(): any;
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Gets the number of nodes in the graph.
|
|
395
|
+
* Complexity: O(1).
|
|
396
|
+
*
|
|
397
|
+
* @returns nodes count.
|
|
398
|
+
*/
|
|
399
|
+
nodeCount(): number;
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Gets the number of edges in the graph.
|
|
403
|
+
* Complexity: O(1).
|
|
404
|
+
*
|
|
405
|
+
* @returns edges count.
|
|
406
|
+
*/
|
|
407
|
+
edgeCount(): number;
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Gets list of nodes without in-edges.
|
|
411
|
+
* Complexity: O(|V|).
|
|
412
|
+
*
|
|
413
|
+
* @returns the graph source nodes.
|
|
414
|
+
*/
|
|
415
|
+
sources(): string[];
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Gets list of nodes without out-edges.
|
|
419
|
+
* Complexity: O(|V|).
|
|
420
|
+
*
|
|
421
|
+
* @returns the graph source nodes.
|
|
422
|
+
*/
|
|
423
|
+
sinks(): string[];
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export namespace json {
|
|
427
|
+
/**
|
|
428
|
+
* Creates a JSON representation of the graph that can be serialized to a string with
|
|
429
|
+
* JSON.stringify. The graph can later be restored using json.read.
|
|
430
|
+
*
|
|
431
|
+
* @argument graph - target to create JSON representation of.
|
|
432
|
+
* @returns JSON serializable graph representation
|
|
433
|
+
*/
|
|
434
|
+
function write(graph: Graph): Object;
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Takes JSON as input and returns the graph representation.
|
|
438
|
+
*
|
|
439
|
+
* @example
|
|
440
|
+
* var g2 = graphlib.json.read(JSON.parse(str));
|
|
441
|
+
* g2.nodes();
|
|
442
|
+
* // ['a', 'b']
|
|
443
|
+
* g2.edges()
|
|
444
|
+
* // [ { v: 'a', w: 'b' } ]
|
|
445
|
+
*
|
|
446
|
+
* @argument json - JSON serializable graph representation
|
|
447
|
+
* @returns graph constructed acccording to specified representation
|
|
448
|
+
*/
|
|
449
|
+
function read(json: Object): Graph;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
export interface Path {
|
|
453
|
+
distance: number;
|
|
454
|
+
predecessor: string;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
export namespace alg {
|
|
458
|
+
/**
|
|
459
|
+
* Finds all connected components in a graph and returns an array of these components.
|
|
460
|
+
* Each component is itself an array that contains the ids of nodes in the component.
|
|
461
|
+
* Complexity: O(|V|).
|
|
462
|
+
*
|
|
463
|
+
* @argument graph - graph to find components in.
|
|
464
|
+
* @returns array of nodes list representing components
|
|
465
|
+
*/
|
|
466
|
+
function components(graph: Graph): string[][];
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* This function is an implementation of Dijkstra's algorithm which finds the shortest
|
|
470
|
+
* path from source to all other nodes in graph. This function returns a map of
|
|
471
|
+
* v -> { distance, predecessor }. The distance property holds the sum of the weights
|
|
472
|
+
* from source to v along the shortest path or Number.POSITIVE_INFINITY if there is no path
|
|
473
|
+
* from source. The predecessor property can be used to walk the individual elements of the
|
|
474
|
+
* path from source to v in reverse order.
|
|
475
|
+
* Complexity: O((|E| + |V|) * log |V|).
|
|
476
|
+
*
|
|
477
|
+
* @argument graph - graph where to search pathes.
|
|
478
|
+
* @argument source - node to start pathes from.
|
|
479
|
+
* @argument weightFn - function which takes edge e and returns the weight of it. If no weightFn
|
|
480
|
+
* is supplied then each edge is assumed to have a weight of 1. This function throws an
|
|
481
|
+
* Error if any of the traversed edges have a negative edge weight.
|
|
482
|
+
* @argument edgeFn - function which takes a node v and returns the ids of all edges incident to it
|
|
483
|
+
* for the purposes of shortest path traversal. By default this function uses the graph.outEdges.
|
|
484
|
+
* @returns shortest pathes map that starts from node source
|
|
485
|
+
*/
|
|
486
|
+
function dijkstra(
|
|
487
|
+
graph: Graph,
|
|
488
|
+
source: string,
|
|
489
|
+
weightFn?: (e: Edge) => number,
|
|
490
|
+
edgeFn?: (v: string) => Edge[],
|
|
491
|
+
): { [node: string]: Path };
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* This function finds the shortest path from each node to every other reachable node in
|
|
495
|
+
* the graph. It is similar to alg.dijkstra, but instead of returning a single-source
|
|
496
|
+
* array, it returns a mapping of source -> alg.dijksta(g, source, weightFn, edgeFn).
|
|
497
|
+
* Complexity: O(|V| * (|E| + |V|) * log |V|).
|
|
498
|
+
*
|
|
499
|
+
* @argument graph - graph where to search pathes.
|
|
500
|
+
* @argument weightFn - function which takes edge e and returns the weight of it. If no weightFn
|
|
501
|
+
* is supplied then each edge is assumed to have a weight of 1. This function throws an
|
|
502
|
+
* Error if any of the traversed edges have a negative edge weight.
|
|
503
|
+
* @argument edgeFn - function which takes a node v and returns the ids of all edges incident to it
|
|
504
|
+
* for the purposes of shortest path traversal. By default this function uses the graph.outEdges.
|
|
505
|
+
* @returns shortest pathes map.
|
|
506
|
+
*/
|
|
507
|
+
function dijkstraAll(
|
|
508
|
+
graph: Graph,
|
|
509
|
+
weightFn?: (e: Edge) => number,
|
|
510
|
+
edgeFn?: (v: string) => Edge[],
|
|
511
|
+
): { [source: string]: { [node: string]: Path } };
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Given a Graph, graph, this function returns all nodes that are part of a cycle. As there
|
|
515
|
+
* may be more than one cycle in a graph this function return an array of these cycles,
|
|
516
|
+
* where each cycle is itself represented by an array of ids for each node involved in
|
|
517
|
+
* that cycle. Method alg.isAcyclic is more efficient if you only need to determine whether a graph has a
|
|
518
|
+
* cycle or not.
|
|
519
|
+
* Complexity: O(|V| + |E|).
|
|
520
|
+
*
|
|
521
|
+
* @argument graph - graph where to search cycles.
|
|
522
|
+
* @returns cycles list.
|
|
523
|
+
*/
|
|
524
|
+
function findCycles(graph: Graph): string[][];
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Given a Graph, graph, this function returns true if the graph has no cycles and returns false if it
|
|
528
|
+
* does. This algorithm returns as soon as it detects the first cycle. You can use alg.findCycles
|
|
529
|
+
* to get the actual list of cycles in the graph.
|
|
530
|
+
*
|
|
531
|
+
* @argument graph - graph to detect whether it acyclic ot not.
|
|
532
|
+
* @returns whether graph contain cycles or not.
|
|
533
|
+
*/
|
|
534
|
+
function isAcyclic(graph: Graph): boolean;
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* This function is an implementation of the Floyd-Warshall algorithm, which finds the
|
|
538
|
+
* shortest path from each node to every other reachable node in the graph. It is similar
|
|
539
|
+
* to alg.dijkstraAll, but it handles negative edge weights and is more efficient for some types
|
|
540
|
+
* of graphs. This function returns a map of source -> { target -> { distance, predecessor }.
|
|
541
|
+
* The distance property holds the sum of the weights from source to target along the shortest
|
|
542
|
+
* path of Number.POSITIVE_INFINITY if there is no path from source. The predecessor property
|
|
543
|
+
* can be used to walk the individual elements of the path from source to target in reverse
|
|
544
|
+
* order.
|
|
545
|
+
* Complexity: O(|V|^3).
|
|
546
|
+
*
|
|
547
|
+
* @argument graph - graph where to search pathes.
|
|
548
|
+
* @argument weightFn - function which takes edge e and returns the weight of it. If no weightFn
|
|
549
|
+
* is supplied then each edge is assumed to have a weight of 1. This function throws an
|
|
550
|
+
* Error if any of the traversed edges have a negative edge weight.
|
|
551
|
+
* @argument edgeFn - function which takes a node v and returns the ids of all edges incident to it
|
|
552
|
+
* for the purposes of shortest path traversal. By default this function uses the graph.outEdges.
|
|
553
|
+
* @returns shortest pathes map.
|
|
554
|
+
*/
|
|
555
|
+
function floydWarshall(
|
|
556
|
+
graph: Graph,
|
|
557
|
+
weightFn?: (e: Edge) => number,
|
|
558
|
+
edgeFn?: (v: string) => Edge[],
|
|
559
|
+
): { [source: string]: { [node: string]: Path } };
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* Prim's algorithm takes a connected undirected graph and generates a minimum spanning tree. This
|
|
563
|
+
* function returns the minimum spanning tree as an undirected graph. This algorithm is derived
|
|
564
|
+
* from the description in "Introduction to Algorithms", Third Edition, Cormen, et al., Pg 634.
|
|
565
|
+
* Complexity: O(|E| * log |V|);
|
|
566
|
+
*
|
|
567
|
+
* @argument graph - graph to generate a minimum spanning tree of.
|
|
568
|
+
* @argument weightFn - function which takes edge e and returns the weight of it. It throws an Error if
|
|
569
|
+
* the graph is not connected.
|
|
570
|
+
* @returns minimum spanning tree of graph.
|
|
571
|
+
*/
|
|
572
|
+
function prim(graph: Graph, weightFn: (e: Edge) => number): Graph;
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* This function is an implementation of Tarjan's algorithm which finds all strongly connected
|
|
576
|
+
* components in the directed graph g. Each strongly connected component is composed of nodes that
|
|
577
|
+
* can reach all other nodes in the component via directed edges. A strongly connected component
|
|
578
|
+
* can consist of a single node if that node cannot both reach and be reached by any other
|
|
579
|
+
* specific node in the graph. Components of more than one node are guaranteed to have at least
|
|
580
|
+
* one cycle.
|
|
581
|
+
* Complexity: O(|V| + |E|).
|
|
582
|
+
*
|
|
583
|
+
* @argument graph - graph to find all strongly connected components of.
|
|
584
|
+
* @return an array of components. Each component is itself an array that contains
|
|
585
|
+
* the ids of all nodes in the component.
|
|
586
|
+
*/
|
|
587
|
+
function tarjan(graph: Graph): string[][];
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Given a Graph graph this function applies topological sorting to it.
|
|
591
|
+
* If the graph has a cycle it is impossible to generate such a list and CycleException is thrown.
|
|
592
|
+
* Complexity: O(|V| + |E|).
|
|
593
|
+
*
|
|
594
|
+
* @argument graph - graph to apply topological sorting to.
|
|
595
|
+
* @returns an array of nodes such that for each edge u -> v, u appears before v in the array.
|
|
596
|
+
*/
|
|
597
|
+
function topsort(graph: Graph): string[];
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* Performs pre-order depth first traversal on the input graph. If the graph is
|
|
601
|
+
* undirected then this algorithm will navigate using neighbors. If the graph
|
|
602
|
+
* is directed then this algorithm will navigate using successors.
|
|
603
|
+
*
|
|
604
|
+
* @argument graph - depth first traversal target.
|
|
605
|
+
* @argument vs - nodes list to traverse.
|
|
606
|
+
* @returns the nodes in the order they were visited as a list of their names.
|
|
607
|
+
*/
|
|
608
|
+
function preorder(graph: Graph, vs: string[]): string[];
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Performs post-order depth first traversal on the input graph. If the graph is
|
|
612
|
+
* undirected then this algorithm will navigate using neighbors. If the graph
|
|
613
|
+
* is directed then this algorithm will navigate using successors.
|
|
614
|
+
*
|
|
615
|
+
* @argument graph - depth first traversal target.
|
|
616
|
+
* @argument vs - nodes list to traverse.
|
|
617
|
+
* @returns the nodes in the order they were visited as a list of their names.
|
|
618
|
+
*/
|
|
619
|
+
function postorder(graph: Graph, vs: string[]): string[];
|
|
620
|
+
}
|
|
621
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2014, Chris Pettitt
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
*
|
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
|
6
|
+
* modification, are permitted provided that the following conditions are met:
|
|
7
|
+
*
|
|
8
|
+
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
* list of conditions and the following disclaimer.
|
|
10
|
+
*
|
|
11
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
* this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
* and/or other materials provided with the distribution.
|
|
14
|
+
*
|
|
15
|
+
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
16
|
+
* may be used to endorse or promote products derived from this software without
|
|
17
|
+
* specific prior written permission.
|
|
18
|
+
*
|
|
19
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
20
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
21
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
var lib = require("./lib");
|
|
32
|
+
|
|
33
|
+
module.exports = {
|
|
34
|
+
Graph: lib.Graph,
|
|
35
|
+
json: require("./lib/json"),
|
|
36
|
+
alg: require("./lib/alg"),
|
|
37
|
+
version: lib.version
|
|
38
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module.exports = components;
|
|
2
|
+
|
|
3
|
+
function components(g) {
|
|
4
|
+
var visited = {};
|
|
5
|
+
var cmpts = [];
|
|
6
|
+
var cmpt;
|
|
7
|
+
|
|
8
|
+
function dfs(v) {
|
|
9
|
+
if (Object.hasOwn(visited, v)) return;
|
|
10
|
+
visited[v] = true;
|
|
11
|
+
cmpt.push(v);
|
|
12
|
+
g.successors(v).forEach(dfs);
|
|
13
|
+
g.predecessors(v).forEach(dfs);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
g.nodes().forEach(function(v) {
|
|
17
|
+
cmpt = [];
|
|
18
|
+
dfs(v);
|
|
19
|
+
if (cmpt.length) {
|
|
20
|
+
cmpts.push(cmpt);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
return cmpts;
|
|
25
|
+
}
|