@ac-kit/algo 0.5.0 → 0.7.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.
Files changed (191) hide show
  1. package/CHANGELOG.md +128 -0
  2. package/README.md +60 -18
  3. package/dist/combinatorics/cartesian-product.d.ts +46 -0
  4. package/dist/combinatorics/cartesian-product.d.ts.map +1 -0
  5. package/dist/combinatorics/cartesian-product.js +79 -0
  6. package/dist/combinatorics/cartesian-product.js.map +1 -0
  7. package/dist/combinatorics/permutations.d.ts +41 -0
  8. package/dist/combinatorics/permutations.d.ts.map +1 -0
  9. package/dist/combinatorics/permutations.js +70 -0
  10. package/dist/combinatorics/permutations.js.map +1 -0
  11. package/dist/combinatorics/power-set.d.ts +40 -0
  12. package/dist/combinatorics/power-set.d.ts.map +1 -0
  13. package/dist/combinatorics/power-set.js +67 -0
  14. package/dist/combinatorics/power-set.js.map +1 -0
  15. package/dist/graph/bfs.d.ts +66 -0
  16. package/dist/graph/bfs.d.ts.map +1 -0
  17. package/dist/graph/bfs.js +89 -0
  18. package/dist/graph/bfs.js.map +1 -0
  19. package/dist/graph/connected-components.d.ts +35 -0
  20. package/dist/graph/connected-components.d.ts.map +1 -0
  21. package/dist/graph/connected-components.js +75 -0
  22. package/dist/graph/connected-components.js.map +1 -0
  23. package/dist/graph/find-cycle.d.ts +5 -3
  24. package/dist/graph/find-cycle.d.ts.map +1 -1
  25. package/dist/graph/find-cycle.js +23 -3
  26. package/dist/graph/find-cycle.js.map +1 -1
  27. package/dist/graph/floyd-warshall.d.ts +67 -0
  28. package/dist/graph/floyd-warshall.d.ts.map +1 -0
  29. package/dist/graph/floyd-warshall.js +106 -0
  30. package/dist/graph/floyd-warshall.js.map +1 -0
  31. package/dist/graph/graph-cycle-error.d.ts +16 -0
  32. package/dist/graph/graph-cycle-error.d.ts.map +1 -0
  33. package/dist/graph/graph-cycle-error.js +20 -0
  34. package/dist/graph/graph-cycle-error.js.map +1 -0
  35. package/dist/graph/hierarchy-from-parents.js.map +1 -1
  36. package/dist/graph/lowest-common-ancestor.d.ts +32 -0
  37. package/dist/graph/lowest-common-ancestor.d.ts.map +1 -0
  38. package/dist/graph/lowest-common-ancestor.js +56 -0
  39. package/dist/graph/lowest-common-ancestor.js.map +1 -0
  40. package/dist/graph/topological-sort.d.ts +51 -0
  41. package/dist/graph/topological-sort.d.ts.map +1 -0
  42. package/dist/graph/topological-sort.js +124 -0
  43. package/dist/graph/topological-sort.js.map +1 -0
  44. package/dist/group/_nest-by.d.ts +19 -0
  45. package/dist/group/_nest-by.d.ts.map +1 -0
  46. package/dist/group/_nest-by.js +85 -0
  47. package/dist/group/_nest-by.js.map +1 -0
  48. package/dist/group/count-by.d.ts +31 -0
  49. package/dist/group/count-by.d.ts.map +1 -0
  50. package/dist/group/count-by.js +35 -0
  51. package/dist/group/count-by.js.map +1 -0
  52. package/dist/group/flat-group-by.d.ts +28 -0
  53. package/dist/group/flat-group-by.d.ts.map +1 -0
  54. package/dist/group/flat-group-by.js +30 -0
  55. package/dist/group/flat-group-by.js.map +1 -0
  56. package/dist/group/group-adjacent-by.d.ts +31 -0
  57. package/dist/group/group-adjacent-by.d.ts.map +1 -0
  58. package/dist/group/group-adjacent-by.js +50 -0
  59. package/dist/group/group-adjacent-by.js.map +1 -0
  60. package/dist/group/group-sort.d.ts +38 -0
  61. package/dist/group/group-sort.d.ts.map +1 -0
  62. package/dist/group/group-sort.js +49 -0
  63. package/dist/group/group-sort.js.map +1 -0
  64. package/dist/group/index-by.d.ts +33 -0
  65. package/dist/group/index-by.d.ts.map +1 -0
  66. package/dist/group/index-by.js +42 -0
  67. package/dist/group/index-by.js.map +1 -0
  68. package/dist/group/nested-map.d.ts +12 -0
  69. package/dist/group/nested-map.d.ts.map +1 -0
  70. package/dist/group/nested-map.js +2 -0
  71. package/dist/group/nested-map.js.map +1 -0
  72. package/dist/group/rollup-by.d.ts +62 -0
  73. package/dist/group/rollup-by.d.ts.map +1 -0
  74. package/dist/group/rollup-by.js +72 -0
  75. package/dist/group/rollup-by.js.map +1 -0
  76. package/dist/group/run-length-encode.d.ts +41 -0
  77. package/dist/group/run-length-encode.d.ts.map +1 -0
  78. package/dist/group/run-length-encode.js +66 -0
  79. package/dist/group/run-length-encode.js.map +1 -0
  80. package/dist/index.d.ts +39 -1
  81. package/dist/index.d.ts.map +1 -1
  82. package/dist/index.js +39 -1
  83. package/dist/index.js.map +1 -1
  84. package/dist/ordered/_fractional-key.d.ts +38 -0
  85. package/dist/ordered/_fractional-key.d.ts.map +1 -0
  86. package/dist/ordered/_fractional-key.js +251 -0
  87. package/dist/ordered/_fractional-key.js.map +1 -0
  88. package/dist/ordered/bisect-right-numbers-array.d.ts +20 -0
  89. package/dist/ordered/bisect-right-numbers-array.d.ts.map +1 -0
  90. package/dist/ordered/bisect-right-numbers-array.js +42 -0
  91. package/dist/ordered/bisect-right-numbers-array.js.map +1 -0
  92. package/dist/ordered/bisect-right-numbers.d.ts +19 -0
  93. package/dist/ordered/bisect-right-numbers.d.ts.map +1 -0
  94. package/dist/ordered/bisect-right-numbers.js +32 -0
  95. package/dist/ordered/bisect-right-numbers.js.map +1 -0
  96. package/dist/ordered/create-bisector.js.map +1 -1
  97. package/dist/ordered/fractional-key-alphabet.d.ts +41 -0
  98. package/dist/ordered/fractional-key-alphabet.d.ts.map +1 -0
  99. package/dist/ordered/fractional-key-alphabet.js +67 -0
  100. package/dist/ordered/fractional-key-alphabet.js.map +1 -0
  101. package/dist/ordered/fractional-key-error.d.ts +15 -0
  102. package/dist/ordered/fractional-key-error.d.ts.map +1 -0
  103. package/dist/ordered/fractional-key-error.js +20 -0
  104. package/dist/ordered/fractional-key-error.js.map +1 -0
  105. package/dist/ordered/fractional-key.d.ts +82 -0
  106. package/dist/ordered/fractional-key.d.ts.map +1 -0
  107. package/dist/ordered/fractional-key.js +199 -0
  108. package/dist/ordered/fractional-key.js.map +1 -0
  109. package/dist/ordered/is-sorted.d.ts +33 -0
  110. package/dist/ordered/is-sorted.d.ts.map +1 -0
  111. package/dist/ordered/is-sorted.js +39 -0
  112. package/dist/ordered/is-sorted.js.map +1 -0
  113. package/dist/sample/sample-without-replacement.d.ts +26 -0
  114. package/dist/sample/sample-without-replacement.d.ts.map +1 -0
  115. package/dist/sample/sample-without-replacement.js +41 -0
  116. package/dist/sample/sample-without-replacement.js.map +1 -0
  117. package/dist/sample/shuffle.d.ts +2 -2
  118. package/dist/sample/shuffle.d.ts.map +1 -1
  119. package/dist/sample/shuffle.js +3 -3
  120. package/dist/sample/shuffle.js.map +1 -1
  121. package/dist/sample/weighted-choice-index.d.ts +29 -0
  122. package/dist/sample/weighted-choice-index.d.ts.map +1 -0
  123. package/dist/sample/weighted-choice-index.js +62 -0
  124. package/dist/sample/weighted-choice-index.js.map +1 -0
  125. package/dist/sample/weighted-choice.d.ts +28 -0
  126. package/dist/sample/weighted-choice.d.ts.map +1 -0
  127. package/dist/sample/weighted-choice.js +31 -0
  128. package/dist/sample/weighted-choice.js.map +1 -0
  129. package/dist/select/minmax.d.ts +52 -0
  130. package/dist/select/minmax.d.ts.map +1 -0
  131. package/dist/select/minmax.js +109 -0
  132. package/dist/select/minmax.js.map +1 -0
  133. package/dist/select/{quickselect.d.ts → quickselect-in-place.d.ts} +2 -2
  134. package/dist/select/quickselect-in-place.d.ts.map +1 -0
  135. package/dist/select/{quickselect.js → quickselect-in-place.js} +3 -3
  136. package/dist/select/quickselect-in-place.js.map +1 -0
  137. package/dist/sequence/_matching-runs.d.ts +16 -0
  138. package/dist/sequence/_matching-runs.d.ts.map +1 -0
  139. package/dist/sequence/_matching-runs.js +178 -0
  140. package/dist/sequence/_matching-runs.js.map +1 -0
  141. package/dist/sequence/common-prefix-length.d.ts +26 -0
  142. package/dist/sequence/common-prefix-length.d.ts.map +1 -0
  143. package/dist/sequence/common-prefix-length.js +34 -0
  144. package/dist/sequence/common-prefix-length.js.map +1 -0
  145. package/dist/sequence/diff-sequences.d.ts +59 -0
  146. package/dist/sequence/diff-sequences.d.ts.map +1 -0
  147. package/dist/sequence/diff-sequences.js +67 -0
  148. package/dist/sequence/diff-sequences.js.map +1 -0
  149. package/dist/sequence/levenshtein-distance.d.ts +43 -0
  150. package/dist/sequence/levenshtein-distance.d.ts.map +1 -0
  151. package/dist/sequence/levenshtein-distance.js +102 -0
  152. package/dist/sequence/levenshtein-distance.js.map +1 -0
  153. package/dist/sequence/longest-common-subsequence.d.ts +28 -0
  154. package/dist/sequence/longest-common-subsequence.d.ts.map +1 -0
  155. package/dist/sequence/longest-common-subsequence.js +39 -0
  156. package/dist/sequence/longest-common-subsequence.js.map +1 -0
  157. package/dist/sequence/longest-increasing-subsequence.d.ts +28 -0
  158. package/dist/sequence/longest-increasing-subsequence.d.ts.map +1 -0
  159. package/dist/sequence/longest-increasing-subsequence.js +47 -0
  160. package/dist/sequence/longest-increasing-subsequence.js.map +1 -0
  161. package/dist/sort/_sort-primitives.d.ts +6 -0
  162. package/dist/sort/_sort-primitives.d.ts.map +1 -0
  163. package/dist/sort/_sort-primitives.js +18 -0
  164. package/dist/sort/_sort-primitives.js.map +1 -0
  165. package/dist/sort/counting-sort.d.ts +40 -0
  166. package/dist/sort/counting-sort.d.ts.map +1 -0
  167. package/dist/sort/counting-sort.js +51 -0
  168. package/dist/sort/counting-sort.js.map +1 -0
  169. package/dist/sort/merge-sort.d.ts +26 -0
  170. package/dist/sort/merge-sort.d.ts.map +1 -0
  171. package/dist/sort/merge-sort.js +73 -0
  172. package/dist/sort/merge-sort.js.map +1 -0
  173. package/dist/sort/quick-sort-in-place.d.ts +35 -0
  174. package/dist/sort/quick-sort-in-place.d.ts.map +1 -0
  175. package/dist/sort/quick-sort-in-place.js +98 -0
  176. package/dist/sort/quick-sort-in-place.js.map +1 -0
  177. package/dist/sort/radix-index-sorter.d.ts +57 -0
  178. package/dist/sort/radix-index-sorter.d.ts.map +1 -0
  179. package/dist/sort/radix-index-sorter.js +204 -0
  180. package/dist/sort/radix-index-sorter.js.map +1 -0
  181. package/dist/sort/rank.d.ts +64 -0
  182. package/dist/sort/rank.d.ts.map +1 -0
  183. package/dist/sort/rank.js +107 -0
  184. package/dist/sort/rank.js.map +1 -0
  185. package/dist/sort/sort-range.d.ts +48 -0
  186. package/dist/sort/sort-range.d.ts.map +1 -0
  187. package/dist/sort/sort-range.js +267 -0
  188. package/dist/sort/sort-range.js.map +1 -0
  189. package/package.json +6 -5
  190. package/dist/select/quickselect.d.ts.map +0 -1
  191. package/dist/select/quickselect.js.map +0 -1
@@ -0,0 +1,66 @@
1
+ /** Continue walking the graph, queueing the children of this node. */
2
+ export declare const BfsWalkContinue: unique symbol;
3
+ /** Do not queue the children of this node, but continue walking the graph. */
4
+ export declare const BfsWalkSkipChildren: unique symbol;
5
+ /** Stop walking the graph immediately, returning `true` from {@link bfs}. */
6
+ export declare const BfsWalkStop: unique symbol;
7
+ /** The return value of {@link BfsTraits.onEnter} to control the walk. */
8
+ export type BfsWalkControl = typeof BfsWalkContinue | typeof BfsWalkSkipChildren | typeof BfsWalkStop;
9
+ export type BfsTraits<TNode> = {
10
+ /**
11
+ * Returns the successors of a node, or `undefined`/empty for a leaf. Called
12
+ * at most once per entered node, and not for a node whose `onEnter` skips its
13
+ * children or stops the walk.
14
+ */
15
+ childrenOf: (node: TNode) => Iterable<TNode> | undefined;
16
+ /**
17
+ * Called on entering a node, level after level. `depth` is the number of
18
+ * edges from the root — with {@link BfsTraits.seen}, the length of a shortest
19
+ * path to the node in edge count — and `parent` the node it was reached from
20
+ * (`undefined` for the root), so recording the parents gives those shortest
21
+ * paths. Returning `undefined` is treated as {@link BfsWalkContinue}.
22
+ */
23
+ onEnter?: (node: TNode, depth: number, parent: TNode | undefined) => BfsWalkControl | undefined;
24
+ /**
25
+ * Marks the nodes already reached, for a graph in which a node is reachable
26
+ * twice (a cycle or a shared descendant). A node is marked when it is first
27
+ * queued, so it is entered once, from the shallowest level. Omit for a tree,
28
+ * where no node is reachable twice and the bookkeeping would be wasted.
29
+ */
30
+ seen?: Set<TNode>;
31
+ };
32
+ /**
33
+ * Walks a graph or tree breadth-first: the root, then every node one edge away,
34
+ * then every node two edges away, and so on — level order. The counterpart of
35
+ * `dfs` with the same `childrenOf` shape, and the search that finds a shortest
36
+ * path in edge count when every edge costs the same (use `aStar` when edges
37
+ * have costs).
38
+ *
39
+ * Iterative, level by level, so a deep graph cannot overflow the stack. Nodes
40
+ * are compared by identity in {@link BfsTraits.seen} (`Set` semantics).
41
+ *
42
+ * Time complexity: O(V + E) for the nodes and edges reached. Memory: O(w) for
43
+ * the widest level `w`, plus `seen`. Without {@link BfsTraits.seen}, a cyclic
44
+ * graph does not terminate — pass a `Set` whenever a cycle is possible.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * // Shortest path in edge count, from the parents of a breadth-first walk.
49
+ * const parents = new Map<string, string | undefined>();
50
+ * bfs("a", {
51
+ * childrenOf: (node) => graph.outNeighbors(node),
52
+ * seen: new Set(),
53
+ * onEnter: (node, _depth, parent) => {
54
+ * parents.set(node, parent);
55
+ * return node === "goal" ? BfsWalkStop : undefined;
56
+ * },
57
+ * });
58
+ * ```;
59
+ *
60
+ * @param root - The node to start walking from.
61
+ * @param traits - How to traverse, and what to do at each node.
62
+ * @returns `true` if the walk was stopped early via {@link BfsWalkStop},
63
+ * `false` if it ran to completion (or `root` was already in `seen`).
64
+ */
65
+ export declare function bfs<TNode>(root: TNode, traits: BfsTraits<TNode>): boolean;
66
+ //# sourceMappingURL=bfs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bfs.d.ts","sourceRoot":"","sources":["../../src/graph/bfs.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,eAAO,MAAM,eAAe,eAA4B,CAAC;AACzD,8EAA8E;AAC9E,eAAO,MAAM,mBAAmB,eAAgC,CAAC;AACjE,6EAA6E;AAC7E,eAAO,MAAM,WAAW,eAAwB,CAAC;AAEjD,yEAAyE;AACzE,MAAM,MAAM,cAAc,GACvB,OAAO,eAAe,GACtB,OAAO,mBAAmB,GAC1B,OAAO,WAAW,CAAC;AAEtB,MAAM,MAAM,SAAS,CAAC,KAAK,IAAI;IAC9B;;;;OAIG;IACH,UAAU,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,QAAQ,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC;IAEzD;;;;;;OAMG;IACH,OAAO,CAAC,EAAE,CACT,IAAI,EAAE,KAAK,EACX,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,KAAK,GAAG,SAAS,KACrB,cAAc,GAAG,SAAS,CAAC;IAEhC;;;;;OAKG;IACH,IAAI,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC;CAClB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,GAAG,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,KAAK,CAAC,GAAG,OAAO,CAuDzE"}
@@ -0,0 +1,89 @@
1
+ /** Continue walking the graph, queueing the children of this node. */
2
+ export const BfsWalkContinue = Symbol("BfsWalkContinue");
3
+ /** Do not queue the children of this node, but continue walking the graph. */
4
+ export const BfsWalkSkipChildren = Symbol("BfsWalkSkipChildren");
5
+ /** Stop walking the graph immediately, returning `true` from {@link bfs}. */
6
+ export const BfsWalkStop = Symbol("BfsWalkStop");
7
+ /**
8
+ * Walks a graph or tree breadth-first: the root, then every node one edge away,
9
+ * then every node two edges away, and so on — level order. The counterpart of
10
+ * `dfs` with the same `childrenOf` shape, and the search that finds a shortest
11
+ * path in edge count when every edge costs the same (use `aStar` when edges
12
+ * have costs).
13
+ *
14
+ * Iterative, level by level, so a deep graph cannot overflow the stack. Nodes
15
+ * are compared by identity in {@link BfsTraits.seen} (`Set` semantics).
16
+ *
17
+ * Time complexity: O(V + E) for the nodes and edges reached. Memory: O(w) for
18
+ * the widest level `w`, plus `seen`. Without {@link BfsTraits.seen}, a cyclic
19
+ * graph does not terminate — pass a `Set` whenever a cycle is possible.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * // Shortest path in edge count, from the parents of a breadth-first walk.
24
+ * const parents = new Map<string, string | undefined>();
25
+ * bfs("a", {
26
+ * childrenOf: (node) => graph.outNeighbors(node),
27
+ * seen: new Set(),
28
+ * onEnter: (node, _depth, parent) => {
29
+ * parents.set(node, parent);
30
+ * return node === "goal" ? BfsWalkStop : undefined;
31
+ * },
32
+ * });
33
+ * ```;
34
+ *
35
+ * @param root - The node to start walking from.
36
+ * @param traits - How to traverse, and what to do at each node.
37
+ * @returns `true` if the walk was stopped early via {@link BfsWalkStop},
38
+ * `false` if it ran to completion (or `root` was already in `seen`).
39
+ */
40
+ export function bfs(root, traits) {
41
+ const { childrenOf, onEnter, seen } = traits;
42
+ if (seen !== undefined) {
43
+ if (seen.has(root)) {
44
+ return false;
45
+ }
46
+ seen.add(root);
47
+ }
48
+ let level = [root];
49
+ let parents = [undefined];
50
+ let nextLevel = [];
51
+ let nextParents = [];
52
+ for (let depth = 0; level.length > 0; depth++) {
53
+ for (let index = 0; index < level.length; index++) {
54
+ const node = level[index];
55
+ const control = onEnter?.(node, depth, parents[index]) ?? BfsWalkContinue;
56
+ if (control === BfsWalkStop) {
57
+ return true;
58
+ }
59
+ if (control === BfsWalkSkipChildren) {
60
+ continue;
61
+ }
62
+ const children = childrenOf(node);
63
+ if (children === undefined) {
64
+ continue;
65
+ }
66
+ for (const child of children) {
67
+ if (seen !== undefined) {
68
+ if (seen.has(child)) {
69
+ continue;
70
+ }
71
+ seen.add(child);
72
+ }
73
+ nextLevel.push(child);
74
+ nextParents.push(node);
75
+ }
76
+ }
77
+ // Swap the two level buffers so neither is reallocated per level.
78
+ const doneLevel = level;
79
+ const doneParents = parents;
80
+ level = nextLevel;
81
+ parents = nextParents;
82
+ nextLevel = doneLevel;
83
+ nextParents = doneParents;
84
+ nextLevel.length = 0;
85
+ nextParents.length = 0;
86
+ }
87
+ return false;
88
+ }
89
+ //# sourceMappingURL=bfs.js.map
@@ -0,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AC,IAAI,eAAe,CAAC;YAE1E,IAAI,OAAO,KAAK,WAAW,EAAE,CAAC;gBAC7B,OAAO,IAAI,CAAC;YACb,CAAC;YACD,IAAI,OAAO,KAAK,mBAAmB,EAAE,CAAC;gBACrC,SAAS;YACV,CAAC;YAED,MAAM,QAAQ,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;YAClC,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;gBAC5B,SAAS;YACV,CAAC;YACD,KAAK,MAAM,KAAK,IAAI,QAAQ,EAAE,CAAC;gBAC9B,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;oBACxB,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;wBACrB,SAAS;oBACV,CAAC;oBACD,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;gBACjB,CAAC;gBACD,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;gBACtB,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACxB,CAAC;QACF,CAAC;QAED,kEAAkE;QAClE,MAAM,SAAS,GAAG,KAAK,CAAC;QACxB,MAAM,WAAW,GAAG,OAAO,CAAC;QAC5B,KAAK,GAAG,SAAS,CAAC;QAClB,OAAO,GAAG,WAAW,CAAC;QACtB,SAAS,GAAG,SAAS,CAAC;QACtB,WAAW,GAAG,WAAW,CAAC;QAC1B,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC;QACrB,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC;IACxB,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC"}
@@ -0,0 +1,35 @@
1
+ import type { DefinedValue } from "@ac-kit/core";
2
+ /**
3
+ * Partitions an undirected graph into its connected components: the largest
4
+ * sets of nodes in which every node can reach every other.
5
+ *
6
+ * Each edge counts in both directions however it is listed, so a neighbour list
7
+ * need only name an edge from one of its two ends; handing in a directed
8
+ * graph's out-neighbours therefore gives its weakly connected components.
9
+ * Union–find over the edges (`StaticDisjointSet` from `@ac-kit/data`), so the
10
+ * result does not depend on the order edges are read in, and no deep graph can
11
+ * overflow the stack.
12
+ *
13
+ * The components come in the order of their first node, and the nodes of each
14
+ * in the order they first appear: `nodes` in order, then any node only a
15
+ * neighbour list names, where it is first named. Nodes are compared by identity
16
+ * (`Map` semantics, so `NaN` equals `NaN`); `neighboursOf` is called once per
17
+ * node, its iterable read once.
18
+ *
19
+ * Time complexity: O((V + E) · α(V)), effectively linear. Memory: O(V).
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * const graph = new Graph<string>();
24
+ * // …
25
+ * connectedComponents(graph.nodes(), (node) => graph.neighbors(node));
26
+ * ```;
27
+ *
28
+ * @param nodes - The nodes of the graph; a repeated one counts once. A node
29
+ * left out is still found when a neighbour list names it.
30
+ * @param neighboursOf - The neighbours of a node, or `undefined` for none.
31
+ * @returns The components, each a fresh array of its nodes.
32
+ * @throws Whatever `neighboursOf` or an iterator throws.
33
+ */
34
+ export declare function connectedComponents<TNode extends DefinedValue>(nodes: Iterable<TNode>, neighboursOf: (node: TNode) => Iterable<TNode> | undefined): TNode[][];
35
+ //# sourceMappingURL=connected-components.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connected-components.d.ts","sourceRoot":"","sources":["../../src/graph/connected-components.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAGjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,SAAS,YAAY,EAC7D,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,EACtB,YAAY,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,QAAQ,CAAC,KAAK,CAAC,GAAG,SAAS,GACxD,KAAK,EAAE,EAAE,CA0CX"}
@@ -0,0 +1,75 @@
1
+ import { StaticDisjointSet } from "@ac-kit/data";
2
+ /**
3
+ * Partitions an undirected graph into its connected components: the largest
4
+ * sets of nodes in which every node can reach every other.
5
+ *
6
+ * Each edge counts in both directions however it is listed, so a neighbour list
7
+ * need only name an edge from one of its two ends; handing in a directed
8
+ * graph's out-neighbours therefore gives its weakly connected components.
9
+ * Union–find over the edges (`StaticDisjointSet` from `@ac-kit/data`), so the
10
+ * result does not depend on the order edges are read in, and no deep graph can
11
+ * overflow the stack.
12
+ *
13
+ * The components come in the order of their first node, and the nodes of each
14
+ * in the order they first appear: `nodes` in order, then any node only a
15
+ * neighbour list names, where it is first named. Nodes are compared by identity
16
+ * (`Map` semantics, so `NaN` equals `NaN`); `neighboursOf` is called once per
17
+ * node, its iterable read once.
18
+ *
19
+ * Time complexity: O((V + E) · α(V)), effectively linear. Memory: O(V).
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * const graph = new Graph<string>();
24
+ * // …
25
+ * connectedComponents(graph.nodes(), (node) => graph.neighbors(node));
26
+ * ```;
27
+ *
28
+ * @param nodes - The nodes of the graph; a repeated one counts once. A node
29
+ * left out is still found when a neighbour list names it.
30
+ * @param neighboursOf - The neighbours of a node, or `undefined` for none.
31
+ * @returns The components, each a fresh array of its nodes.
32
+ * @throws Whatever `neighboursOf` or an iterator throws.
33
+ */
34
+ export function connectedComponents(nodes, neighboursOf) {
35
+ const sets = new StaticDisjointSet();
36
+ const order = [];
37
+ const reach = (node) => {
38
+ if (!sets.has(node)) {
39
+ sets.makeSet(node);
40
+ order.push(node);
41
+ }
42
+ };
43
+ for (const node of nodes) {
44
+ reach(node);
45
+ }
46
+ // `order` grows while it is walked, so a node only a neighbour list names is
47
+ // expanded in turn.
48
+ for (let index = 0; index < order.length; index++) {
49
+ const node = order[index];
50
+ const neighbours = neighboursOf(node);
51
+ if (neighbours === undefined) {
52
+ continue;
53
+ }
54
+ for (const neighbour of neighbours) {
55
+ reach(neighbour);
56
+ sets.union(node, neighbour);
57
+ }
58
+ }
59
+ const components = new Map();
60
+ const result = [];
61
+ for (const node of order) {
62
+ const root = sets.find(node);
63
+ const component = components.get(root);
64
+ if (component === undefined) {
65
+ const created = [node];
66
+ components.set(root, created);
67
+ result.push(created);
68
+ }
69
+ else {
70
+ component.push(node);
71
+ }
72
+ }
73
+ return result;
74
+ }
75
+ //# sourceMappingURL=connected-components.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connected-components.js","sourceRoot":"","sources":["../../src/graph/connected-components.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,UAAU,mBAAmB,CAClC,KAAsB,EACtB,YAA0D;IAE1D,MAAM,IAAI,GAAG,IAAI,iBAAiB,EAAS,CAAC;IAC5C,MAAM,KAAK,GAAY,EAAE,CAAC;IAE1B,MAAM,KAAK,GAAG,CAAC,IAAW,EAAQ,EAAE;QACnC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YACrB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;YACnB,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAClB,CAAC;IACF,CAAC,CAAC;IAEF,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QAC1B,KAAK,CAAC,IAAI,CAAC,CAAC;IACb,CAAC;IACD,6EAA6E;IAC7E,oBAAoB;IACpB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACnD,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAE,CAAC;QAC3B,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;QACtC,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;YAC9B,SAAS;QACV,CAAC;QACD,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;YACpC,KAAK,CAAC,SAAS,CAAC,CAAC;YACjB,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QAC7B,CAAC;IACF,CAAC;IAED,MAAM,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC7C,MAAM,MAAM,GAAc,EAAE,CAAC;IAC7B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QAC1B,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAE,CAAC;QAC9B,MAAM,SAAS,GAAG,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACvC,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;YAC7B,MAAM,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC;YACvB,UAAU,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;YAC9B,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACtB,CAAC;aAAM,CAAC;YACP,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACtB,CAAC;IACF,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC"}
@@ -1,8 +1,9 @@
1
1
  import type { WorkBudget } from "@ac-kit/core";
2
2
  export type FindCycleOptions = {
3
3
  /**
4
- * Charged 1 before each node is entered and 1 for each successor read (each
5
- * edge scanned, repeated ones included), so a search over an untrusted graph
4
+ * Charged 1 for each root read (a repeated one included), 1 before each node
5
+ * is entered and 1 for each successor read (each edge scanned, repeated ones
6
+ * included), so a search over an untrusted graph or an endless roots iterable
6
7
  * can be capped. Whatever `spend` throws propagates unchanged.
7
8
  */
8
9
  budget?: WorkBudget;
@@ -15,7 +16,8 @@ export type FindCycleOptions = {
15
16
  * Nodes are compared by identity (`Map` semantics, so `NaN` equals `NaN`).
16
17
  * `childrenOf` is called once per node, and the iterable it returns is read
17
18
  * once. Whichever way the search ends, the iterators still open are closed with
18
- * `return()`; an error from that is dropped in favour of the one being thrown.
19
+ * `return()`, the successors of the deepest node first and the roots last; an
20
+ * error from that is dropped in favour of the one being thrown.
19
21
  *
20
22
  * Time complexity: O(V + E). Memory: O(V).
21
23
  *
@@ -1 +1 @@
1
- {"version":3,"file":"find-cycle.d.ts","sourceRoot":"","sources":["../../src/graph/find-cycle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,MAAM,MAAM,gBAAgB,GAAG;IAC9B;;;;OAIG;IACH,MAAM,CAAC,EAAE,UAAU,CAAC;CACpB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAC9B,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,EACtB,UAAU,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,QAAQ,CAAC,KAAK,CAAC,GAAG,SAAS,EACxD,OAAO,GAAE,gBAAqB,GAC5B,KAAK,EAAE,GAAG,SAAS,CAWrB"}
1
+ {"version":3,"file":"find-cycle.d.ts","sourceRoot":"","sources":["../../src/graph/find-cycle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,MAAM,MAAM,gBAAgB,GAAG;IAC9B;;;;;OAKG;IACH,MAAM,CAAC,EAAE,UAAU,CAAC;CACpB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAC9B,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,EACtB,UAAU,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,QAAQ,CAAC,KAAK,CAAC,GAAG,SAAS,EACxD,OAAO,GAAE,gBAAqB,GAC5B,KAAK,EAAE,GAAG,SAAS,CAWrB"}
@@ -6,7 +6,8 @@
6
6
  * Nodes are compared by identity (`Map` semantics, so `NaN` equals `NaN`).
7
7
  * `childrenOf` is called once per node, and the iterable it returns is read
8
8
  * once. Whichever way the search ends, the iterators still open are closed with
9
- * `return()`; an error from that is dropped in favour of the one being thrown.
9
+ * `return()`, the successors of the deepest node first and the roots last; an
10
+ * error from that is dropped in favour of the one being thrown.
10
11
  *
11
12
  * Time complexity: O(V + E). Memory: O(V).
12
13
  *
@@ -31,6 +32,8 @@ export function findCycle(roots, childrenOf, options = {}) {
31
32
  closeAll_(pending, true);
32
33
  return result;
33
34
  }
35
+ // `pending` holds the roots iterator first and then the successor iterators
36
+ // of the path, so closing innermost first closes the roots last.
34
37
  function search_(roots, childrenOf, budget, pending) {
35
38
  // Index on the current path while a node is open, -1 once it is finished.
36
39
  const pathIndices = new Map();
@@ -41,7 +44,25 @@ function search_(roots, childrenOf, budget, pending) {
41
44
  path.push(node);
42
45
  pending.push((childrenOf(node) ?? [])[Symbol.iterator]());
43
46
  };
44
- for (const root of roots) {
47
+ const rootIterator = roots[Symbol.iterator]();
48
+ const readRoot = rootIterator.next;
49
+ pending.push(rootIterator);
50
+ for (;;) {
51
+ let nextRoot;
52
+ try {
53
+ nextRoot = readRoot.call(rootIterator);
54
+ }
55
+ catch (error) {
56
+ // An iterator that threw from `next()` is finished: no `return()`.
57
+ pending.pop();
58
+ throw error;
59
+ }
60
+ if (nextRoot.done) {
61
+ pending.pop();
62
+ return undefined;
63
+ }
64
+ budget?.spend(1);
65
+ const root = nextRoot.value;
45
66
  if (pathIndices.has(root)) {
46
67
  continue;
47
68
  }
@@ -63,7 +84,6 @@ function search_(roots, childrenOf, budget, pending) {
63
84
  }
64
85
  }
65
86
  }
66
- return undefined;
67
87
  }
68
88
  // Closes innermost first; every iterator gets its `return()`, and only when
69
89
  // `report` is set does the first error from one propagate.
@@ -1 +1 @@
1
- {"version":3,"file":"find-cycle.js","sourceRoot":"","sources":["../../src/graph/find-cycle.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,SAAS,CACxB,KAAsB,EACtB,UAAwD,EACxD,OAAO,GAAqB,EAAE;IAE9B,MAAM,OAAO,GAAsB,EAAE,CAAC;IACtC,IAAI,MAA2B,C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AAC;IACrD,OAAO,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC3B,IAAI,CAAC;YACJ,OAAO,CAAC,GAAG,EAAG,CAAC,MAAM,EAAE,EAAE,CAAC;QAC3B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,OAAO,KAAK,EAAE,KAAK,EAAE,CAAC;QACvB,CAAC;IACF,CAAC;IACD,IAAI,MAAM,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QACrC,MAAM,OAAO,CAAC,KAAK,CAAC;IACrB,CAAC;AACF,CAAC"}
@@ -0,0 +1,67 @@
1
+ export type FloydWarshallTraits<TNode> = {
2
+ /** The successors of a node, or `undefined` for none; each must be in `nodes`. */
3
+ neighboursOf: (node: TNode) => Iterable<TNode> | undefined;
4
+ /**
5
+ * Cost of the edge from `from` to `to`, any number but `NaN`; negative costs
6
+ * are allowed as long as no cycle is negative. Defaults to `1`, which makes
7
+ * distance equal hop count. Called once per edge read; of parallel edges the
8
+ * cheapest counts.
9
+ */
10
+ costOf?: (from: TNode, to: TNode) => number;
11
+ };
12
+ /**
13
+ * Shortest distances between every pair of nodes, as flat `n × n` row-major
14
+ * tables indexed by position in {@link AllPairsShortestPaths.nodes}: the entry
15
+ * for the pair `(i, j)` is at `i · n + j`.
16
+ */
17
+ export type AllPairsShortestPaths<TNode> = {
18
+ /** The nodes, in the order given, which numbers the rows and columns. */
19
+ readonly nodes: readonly TNode[];
20
+ /** Cost of a shortest path from row to column; `Infinity` when unreachable. */
21
+ readonly distances: Float64Array;
22
+ /**
23
+ * Position of the node after the row's on a shortest path to the column: the
24
+ * column itself for a direct edge, the row itself on the diagonal, `-1` when
25
+ * the column is unreachable. Following it from `i` until `j` gives the path.
26
+ */
27
+ readonly nextHops: Int32Array;
28
+ };
29
+ /**
30
+ * Shortest paths between every pair of nodes of a dense directed graph — the
31
+ * question `aStar` answers one pair at a time, and the base of a diameter, an
32
+ * eccentricity or a transitive closure. For an undirected graph, list each edge
33
+ * from both ends.
34
+ *
35
+ * Floyd–Warshall: every node in turn is allowed as an intermediate stop, so it
36
+ * handles negative edge costs and reports a negative cycle rather than
37
+ * returning distances that do not exist. Three nested loops, no recursion and
38
+ * no allocation beyond the result.
39
+ *
40
+ * Nodes are compared by identity (`Map` semantics, so `NaN` equals `NaN`).
41
+ * `neighboursOf` is called once per node.
42
+ *
43
+ * Time complexity: O(V³ + E). Memory: O(V²), the result.
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * const { nodes, distances, nextHops } = floydWarshall(cities, {
48
+ * neighboursOf: (city) => roads.outNeighbors(city),
49
+ * costOf: (from, to) => roads.edgeLabel(from, to)!,
50
+ * });
51
+ * // The path from nodes[i] to nodes[j], when distances[i * n + j] is finite.
52
+ * const path = [nodes[i]];
53
+ * for (let at = i; at !== j; ) {
54
+ * at = nextHops[at * nodes.length + j];
55
+ * path.push(nodes[at]);
56
+ * }
57
+ * ```;
58
+ *
59
+ * @param nodes - The nodes of the graph, each once.
60
+ * @param traits - How to expand and weight a node.
61
+ * @returns The distance and next-hop tables.
62
+ * @throws {RangeError} When a node is repeated in `nodes`, a neighbour is not
63
+ * in `nodes`, a cost is `NaN`, or a cycle has a negative total cost.
64
+ * @throws Whatever `neighboursOf`, `costOf` or an iterator throws.
65
+ */
66
+ export declare function floydWarshall<TNode>(nodes: Iterable<TNode>, traits: FloydWarshallTraits<TNode>): AllPairsShortestPaths<TNode>;
67
+ //# sourceMappingURL=floyd-warshall.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"floyd-warshall.d.ts","sourceRoot":"","sources":["../../src/graph/floyd-warshall.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,mBAAmB,CAAC,KAAK,IAAI;IACxC,kFAAkF;IAClF,YAAY,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,QAAQ,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC;IAE3D;;;;;OAKG;IACH,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,KAAK,MAAM,CAAC;CAC5C,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,qBAAqB,CAAC,KAAK,IAAI;IAC1C,yEAAyE;IACzE,QAAQ,CAAC,KAAK,EAAE,SAAS,KAAK,EAAE,CAAC;IAEjC,+EAA+E;IAC/E,QAAQ,CAAC,SAAS,EAAE,YAAY,CAAC;IAEjC;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,UAAU,CAAC;CAC9B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAClC,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,EACtB,MAAM,EAAE,mBAAmB,CAAC,KAAK,CAAC,GAChC,qBAAqB,CAAC,KAAK,CAAC,CA+E9B"}
@@ -0,0 +1,106 @@
1
+ import { describeValue } from "@ac-kit/core";
2
+ /**
3
+ * Shortest paths between every pair of nodes of a dense directed graph — the
4
+ * question `aStar` answers one pair at a time, and the base of a diameter, an
5
+ * eccentricity or a transitive closure. For an undirected graph, list each edge
6
+ * from both ends.
7
+ *
8
+ * Floyd–Warshall: every node in turn is allowed as an intermediate stop, so it
9
+ * handles negative edge costs and reports a negative cycle rather than
10
+ * returning distances that do not exist. Three nested loops, no recursion and
11
+ * no allocation beyond the result.
12
+ *
13
+ * Nodes are compared by identity (`Map` semantics, so `NaN` equals `NaN`).
14
+ * `neighboursOf` is called once per node.
15
+ *
16
+ * Time complexity: O(V³ + E). Memory: O(V²), the result.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * const { nodes, distances, nextHops } = floydWarshall(cities, {
21
+ * neighboursOf: (city) => roads.outNeighbors(city),
22
+ * costOf: (from, to) => roads.edgeLabel(from, to)!,
23
+ * });
24
+ * // The path from nodes[i] to nodes[j], when distances[i * n + j] is finite.
25
+ * const path = [nodes[i]];
26
+ * for (let at = i; at !== j; ) {
27
+ * at = nextHops[at * nodes.length + j];
28
+ * path.push(nodes[at]);
29
+ * }
30
+ * ```;
31
+ *
32
+ * @param nodes - The nodes of the graph, each once.
33
+ * @param traits - How to expand and weight a node.
34
+ * @returns The distance and next-hop tables.
35
+ * @throws {RangeError} When a node is repeated in `nodes`, a neighbour is not
36
+ * in `nodes`, a cost is `NaN`, or a cycle has a negative total cost.
37
+ * @throws Whatever `neighboursOf`, `costOf` or an iterator throws.
38
+ */
39
+ export function floydWarshall(nodes, traits) {
40
+ const { neighboursOf, costOf } = traits;
41
+ const nodeList = [...nodes];
42
+ const n = nodeList.length;
43
+ const indices = new Map();
44
+ for (let index = 0; index < n; index++) {
45
+ const node = nodeList[index];
46
+ if (indices.has(node)) {
47
+ throw new RangeError(`floydWarshall: nodes[${index}] repeats nodes[${indices.get(node)}]`);
48
+ }
49
+ indices.set(node, index);
50
+ }
51
+ const distances = new Float64Array(n * n).fill(Infinity);
52
+ const nextHops = new Int32Array(n * n).fill(-1);
53
+ for (let row = 0; row < n; row++) {
54
+ distances[row * n + row] = 0;
55
+ nextHops[row * n + row] = row;
56
+ }
57
+ for (let row = 0; row < n; row++) {
58
+ const from = nodeList[row];
59
+ const neighbours = neighboursOf(from);
60
+ if (neighbours === undefined) {
61
+ continue;
62
+ }
63
+ for (const to of neighbours) {
64
+ const column = indices.get(to);
65
+ if (column === undefined) {
66
+ throw new RangeError(`floydWarshall: a neighbour of nodes[${row}], ${describeValue(to)}, is not in nodes`);
67
+ }
68
+ const cost = costOf === undefined ? 1 : costOf(from, to);
69
+ if (cost !== cost) {
70
+ throw new RangeError(`floydWarshall: the cost from nodes[${row}] to nodes[${column}] is NaN`);
71
+ }
72
+ const cell = row * n + column;
73
+ if (cost < distances[cell]) {
74
+ distances[cell] = cost;
75
+ nextHops[cell] = column;
76
+ }
77
+ }
78
+ }
79
+ for (let via = 0; via < n; via++) {
80
+ const viaRow = via * n;
81
+ for (let row = 0; row < n; row++) {
82
+ const rowStart = row * n;
83
+ const toVia = distances[rowStart + via];
84
+ if (toVia === Infinity) {
85
+ continue;
86
+ }
87
+ const hopToVia = nextHops[rowStart + via];
88
+ for (let column = 0; column < n; column++) {
89
+ // `Infinity + x` never compares below, and `-Infinity + Infinity`
90
+ // is `NaN`, which never does either.
91
+ const through = toVia + distances[viaRow + column];
92
+ if (through < distances[rowStart + column]) {
93
+ distances[rowStart + column] = through;
94
+ nextHops[rowStart + column] = hopToVia;
95
+ }
96
+ }
97
+ }
98
+ }
99
+ for (let row = 0; row < n; row++) {
100
+ if (distances[row * n + row] < 0) {
101
+ throw new RangeError(`floydWarshall: a cycle through nodes[${row}] has a negative cost`);
102
+ }
103
+ }
104
+ return { nodes: nodeList, distances, nextHops };
105
+ }
106
+ //# sourceMappingURL=floyd-warshall.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"floyd-warshall.js","sourceRoot":"","sources":["../../src/graph/floyd-warshall.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAmC7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,MAAM,UAAU,aAAa,CAC5B,KAAsB,EACtB,MAAkC;IAElC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;IACxC,MAAM,QAAQ,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC;IAC5B,MAAM,CAAC,GAAG,QAAQ,CAAC,MAAM,CAAC;IAC1B,MAAM,OAAO,GAAG,IAAI,GAAG,EAAiB,CAAC;IACzC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC;QACxC,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAE,CAAC;QAC9B,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,UAAU,CACnB,wBAAwB,KAAK,mBAAmB,OAAO,CAAC,GAAG,CAAC,IAAI,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,MAAM,CAAC,GAAG,QAAQ,CAAC;gBACxC,CAAC;YACF,CAAC;QACF,CAAC;IACF,CAAC;IAED,KAAK,IAAI,GAAG,GAAG,CAAC,EAAE,GAAG,GAAG,CAAC,EAAE,GAAG,EAAE,EAAE,CAAC;QAClC,IAAI,SAAS,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAE,GAAG,CAAC,EAAE,CAAC;YACnC,MAAM,IAAI,UAAU,CACnB,wCAAwC,GAAG,uBAAuB,CAClE,CAAC;QACH,CAAC;IACF,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC;AACjD,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Error thrown by a graph algorithm that needs an acyclic graph and was given a
3
+ * cycle, carrying one offending cycle so the message shown to a user can name
4
+ * it ("a imports b imports a").
5
+ *
6
+ * @template TNode The graph's node type.
7
+ */
8
+ export declare class GraphCycleError<TNode = unknown> extends Error {
9
+ /**
10
+ * The nodes of one cycle in edge order: each one has an edge to the next, and
11
+ * the last one to the first (a self-loop is a single node).
12
+ */
13
+ readonly cycle: readonly TNode[];
14
+ constructor(functionName: string, cycle: readonly TNode[]);
15
+ }
16
+ //# sourceMappingURL=graph-cycle-error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph-cycle-error.d.ts","sourceRoot":"","sources":["../../src/graph/graph-cycle-error.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,qBAAa,eAAe,CAAC,KAAK,GAAG,OAAO,CAAE,SAAQ,KAAK;IAC1D;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,SAAS,KAAK,EAAE,CAAC;IAEjC,YAAY,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,KAAK,EAAE,EAMxD;CACD"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Error thrown by a graph algorithm that needs an acyclic graph and was given a
3
+ * cycle, carrying one offending cycle so the message shown to a user can name
4
+ * it ("a imports b imports a").
5
+ *
6
+ * @template TNode The graph's node type.
7
+ */
8
+ export class GraphCycleError extends Error {
9
+ /**
10
+ * The nodes of one cycle in edge order: each one has an edge to the next, and
11
+ * the last one to the first (a self-loop is a single node).
12
+ */
13
+ cycle;
14
+ constructor(functionName, cycle) {
15
+ super(`${functionName}: the graph has a cycle through ${cycle.length} node${cycle.length === 1 ? "" : "s"}`);
16
+ this.name = "GraphCycleError";
17
+ this.cycle = cycle;
18
+ }
19
+ }
20
+ //# sourceMappingURL=graph-cycle-error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph-cycle-error.js","sourceRoot":"","sources":["../../src/graph/graph-cycle-error.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,OAAO,eAAiC,SAAQ,KAAK;IAC1D;;;OAGG;IACM,KAAK,CAAmB;IAEjC,YAAY,YAAoB,EAAE,KAAuB;QACxD,KAAK,CACJ,GAAG,YAAY,mCAAmC,KAAK,CAAC,MAAM,QAAQ,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CACrG,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;QAC9B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACpB,CAAC;CACD"}
@@ -1 +1 @@
1
- {"version":3,"file":"hierarchy-from-parents.js","sourceRoot":"","sources":["../../src/graph/hierarchy-from-parents.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAiB,MAAM,cAAc,CAAC;AAE5D,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AA+B1C;;;;;;;;;;GAUG;AACH,MAAM,UAAU,oBAAoB,CACnC,KAAuB,EACvB,MAAiC;IAEjC,MAAM,SAAS,GAAG,KAAK,CAAC,MAAM,CAAC;IAC/B,MAA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