data-structure-typed 0.8.18 → 0.9.16

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 (213) hide show
  1. package/.idea/modules.xml +1 -1
  2. package/README.md +197 -2
  3. package/dist/data-structures/binary-tree/aa-tree.js +5 -2
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -5
  5. package/dist/data-structures/binary-tree/avl-tree.js +93 -46
  6. package/dist/data-structures/binary-tree/b-tree.js +5 -2
  7. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -1
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.js +14 -13
  9. package/dist/data-structures/binary-tree/binary-tree.d.ts +20 -32
  10. package/dist/data-structures/binary-tree/binary-tree.js +480 -370
  11. package/dist/data-structures/binary-tree/bst.d.ts +4 -8
  12. package/dist/data-structures/binary-tree/bst.js +152 -107
  13. package/dist/data-structures/binary-tree/rb-tree.js +5 -2
  14. package/dist/data-structures/binary-tree/segment-tree.d.ts +3 -3
  15. package/dist/data-structures/binary-tree/segment-tree.js +91 -61
  16. package/dist/data-structures/binary-tree/splay-tree.js +5 -2
  17. package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -5
  18. package/dist/data-structures/binary-tree/tree-multiset.js +31 -11
  19. package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
  20. package/dist/data-structures/graph/abstract-graph.d.ts +5 -33
  21. package/dist/data-structures/graph/abstract-graph.js +546 -311
  22. package/dist/data-structures/graph/directed-graph.d.ts +5 -13
  23. package/dist/data-structures/graph/directed-graph.js +250 -128
  24. package/dist/data-structures/graph/undirected-graph.d.ts +4 -3
  25. package/dist/data-structures/graph/undirected-graph.js +166 -81
  26. package/dist/data-structures/hash/coordinate-map.d.ts +1 -1
  27. package/dist/data-structures/hash/coordinate-map.js +38 -20
  28. package/dist/data-structures/hash/coordinate-set.js +33 -15
  29. package/dist/data-structures/hash/index.d.ts +5 -0
  30. package/dist/data-structures/hash/index.js +5 -0
  31. package/dist/data-structures/heap/heap.d.ts +2 -8
  32. package/dist/data-structures/heap/heap.js +36 -31
  33. package/dist/data-structures/heap/max-heap.d.ts +3 -2
  34. package/dist/data-structures/heap/max-heap.js +27 -9
  35. package/dist/data-structures/heap/min-heap.d.ts +3 -2
  36. package/dist/data-structures/heap/min-heap.js +27 -9
  37. package/dist/data-structures/index.d.ts +1 -0
  38. package/dist/data-structures/index.js +1 -0
  39. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -1
  40. package/dist/data-structures/linked-list/doubly-linked-list.js +56 -53
  41. package/dist/data-structures/linked-list/singly-linked-list.d.ts +8 -12
  42. package/dist/data-structures/linked-list/singly-linked-list.js +308 -174
  43. package/dist/data-structures/matrix/matrix.js +8 -7
  44. package/dist/data-structures/matrix/matrix2d.d.ts +5 -5
  45. package/dist/data-structures/matrix/matrix2d.js +80 -63
  46. package/dist/data-structures/matrix/navigator.d.ts +2 -16
  47. package/dist/data-structures/matrix/navigator.js +37 -18
  48. package/dist/data-structures/matrix/vector2d.d.ts +18 -18
  49. package/dist/data-structures/matrix/vector2d.js +117 -94
  50. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -2
  51. package/dist/data-structures/priority-queue/max-priority-queue.js +25 -8
  52. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -2
  53. package/dist/data-structures/priority-queue/min-priority-queue.js +25 -8
  54. package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -21
  55. package/dist/data-structures/priority-queue/priority-queue.js +159 -116
  56. package/dist/data-structures/queue/deque.js +82 -56
  57. package/dist/data-structures/queue/queue.d.ts +9 -10
  58. package/dist/data-structures/queue/queue.js +34 -34
  59. package/dist/data-structures/stack/stack.d.ts +9 -10
  60. package/dist/data-structures/stack/stack.js +31 -31
  61. package/dist/data-structures/trampoline.d.ts +14 -23
  62. package/dist/data-structures/trampoline.js +103 -25
  63. package/dist/data-structures/trie/trie.d.ts +13 -3
  64. package/dist/data-structures/trie/trie.js +234 -80
  65. package/dist/data-structures/types/abstract-graph.d.ts +29 -0
  66. package/dist/data-structures/types/abstract-graph.js +2 -0
  67. package/dist/data-structures/types/avl-tree.d.ts +5 -0
  68. package/dist/data-structures/types/avl-tree.js +2 -0
  69. package/dist/data-structures/types/binary-tree.d.ts +16 -0
  70. package/dist/data-structures/types/binary-tree.js +2 -0
  71. package/dist/data-structures/types/bst.d.ts +7 -0
  72. package/dist/data-structures/types/bst.js +2 -0
  73. package/dist/data-structures/types/directed-graph.d.ts +10 -0
  74. package/dist/data-structures/types/directed-graph.js +2 -0
  75. package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
  76. package/dist/data-structures/types/doubly-linked-list.js +2 -0
  77. package/dist/data-structures/types/heap.d.ts +7 -0
  78. package/dist/data-structures/types/heap.js +2 -0
  79. package/dist/data-structures/types/index.d.ts +13 -0
  80. package/dist/data-structures/types/index.js +29 -0
  81. package/dist/data-structures/types/navigator.d.ts +14 -0
  82. package/dist/data-structures/types/navigator.js +2 -0
  83. package/dist/data-structures/types/priority-queue.d.ts +7 -0
  84. package/dist/data-structures/types/priority-queue.js +2 -0
  85. package/dist/data-structures/types/segment-tree.d.ts +1 -0
  86. package/dist/data-structures/types/segment-tree.js +2 -0
  87. package/dist/data-structures/types/singly-linked-list.d.ts +5 -0
  88. package/dist/data-structures/types/singly-linked-list.js +2 -0
  89. package/dist/data-structures/types/tree-multiset.d.ts +5 -0
  90. package/dist/data-structures/types/tree-multiset.js +2 -0
  91. package/dist/{types → data-structures/types}/utils.d.ts +7 -1
  92. package/dist/{types → data-structures/types}/utils.js +20 -19
  93. package/dist/{utils.d.ts → utils/utils.d.ts} +6 -23
  94. package/dist/utils/utils.js +651 -0
  95. package/package.json +20 -42
  96. package/src/data-structures/binary-tree/avl-tree.ts +1 -6
  97. package/src/data-structures/binary-tree/binary-indexed-tree.ts +4 -4
  98. package/src/data-structures/binary-tree/binary-tree.ts +184 -139
  99. package/src/data-structures/binary-tree/bst.ts +15 -24
  100. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  101. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  102. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  103. package/src/data-structures/binary-tree/segment-tree.ts +20 -12
  104. package/src/data-structures/binary-tree/tree-multiset.ts +1 -4
  105. package/src/data-structures/diagrams/README.md +7 -0
  106. package/src/data-structures/graph/abstract-graph.ts +58 -94
  107. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  108. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  109. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  110. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  111. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  112. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  113. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  114. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  115. package/src/data-structures/graph/diagrams/mst.png +0 -0
  116. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  117. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  118. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  119. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  120. package/src/data-structures/graph/directed-graph.ts +12 -28
  121. package/src/data-structures/graph/undirected-graph.ts +11 -10
  122. package/src/data-structures/hash/coordinate-map.ts +1 -1
  123. package/src/data-structures/hash/index.ts +5 -0
  124. package/src/data-structures/heap/heap.ts +2 -11
  125. package/src/data-structures/heap/max-heap.ts +3 -2
  126. package/src/data-structures/heap/min-heap.ts +3 -2
  127. package/src/data-structures/index.ts +1 -0
  128. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -9
  129. package/src/data-structures/linked-list/singly-linked-list.ts +18 -32
  130. package/src/data-structures/matrix/matrix2d.ts +11 -11
  131. package/src/data-structures/matrix/navigator.ts +2 -14
  132. package/src/data-structures/matrix/vector2d.ts +52 -52
  133. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  134. package/src/data-structures/priority-queue/min-priority-queue.ts +4 -3
  135. package/src/data-structures/priority-queue/priority-queue.ts +70 -78
  136. package/src/data-structures/queue/deque.ts +2 -2
  137. package/src/data-structures/queue/queue.ts +12 -13
  138. package/src/data-structures/stack/stack.ts +12 -13
  139. package/src/data-structures/trampoline.ts +31 -71
  140. package/src/data-structures/trie/trie.ts +61 -11
  141. package/src/data-structures/types/abstract-graph.ts +51 -0
  142. package/src/data-structures/types/avl-tree.ts +6 -0
  143. package/src/data-structures/types/binary-tree.ts +15 -0
  144. package/src/data-structures/types/bst.ts +5 -0
  145. package/src/data-structures/types/directed-graph.ts +18 -0
  146. package/src/data-structures/types/doubly-linked-list.ts +1 -0
  147. package/src/data-structures/types/heap.ts +8 -0
  148. package/src/data-structures/types/index.ts +13 -0
  149. package/src/data-structures/types/navigator.ts +12 -0
  150. package/src/data-structures/types/priority-queue.ts +9 -0
  151. package/src/data-structures/types/segment-tree.ts +1 -0
  152. package/src/data-structures/types/singly-linked-list.ts +15 -0
  153. package/src/data-structures/types/tree-multiset.ts +3 -0
  154. package/src/{types → data-structures/types}/utils.ts +20 -5
  155. package/src/utils/index.ts +1 -0
  156. package/src/{utils.ts → utils/utils.ts} +32 -132
  157. package/tsconfig.json +9 -6
  158. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
  159. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
  160. package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
  161. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
  162. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
  163. package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
  164. package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
  165. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
  166. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
  167. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
  168. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
  169. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
  170. package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
  171. package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
  172. package/dist/types/data-structures/graph/index.d.ts +0 -3
  173. package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
  174. package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
  175. package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
  176. package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
  177. package/dist/types/data-structures/hash/index.d.ts +0 -1
  178. package/dist/types/data-structures/hash/pair.d.ts +0 -1
  179. package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
  180. package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
  181. package/dist/types/data-structures/heap/heap.d.ts +0 -72
  182. package/dist/types/data-structures/heap/index.d.ts +0 -3
  183. package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
  184. package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
  185. package/dist/types/data-structures/index.d.ts +0 -9
  186. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
  187. package/dist/types/data-structures/linked-list/index.d.ts +0 -2
  188. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
  189. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
  190. package/dist/types/data-structures/matrix/index.d.ts +0 -3
  191. package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
  192. package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
  193. package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
  194. package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
  195. package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
  196. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
  197. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
  198. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
  199. package/dist/types/data-structures/queue/deque.d.ts +0 -37
  200. package/dist/types/data-structures/queue/index.d.ts +0 -1
  201. package/dist/types/data-structures/queue/queue.d.ts +0 -76
  202. package/dist/types/data-structures/stack/index.d.ts +0 -1
  203. package/dist/types/data-structures/stack/stack.d.ts +0 -69
  204. package/dist/types/data-structures/trampoline.d.ts +0 -25
  205. package/dist/types/data-structures/trie/index.d.ts +0 -1
  206. package/dist/types/data-structures/trie/trie.d.ts +0 -28
  207. package/dist/types/types/index.d.ts +0 -1
  208. package/dist/types/types/utils.d.ts +0 -46
  209. package/dist/utils.js +0 -569
  210. package/src/types/index.ts +0 -1
  211. package/src/types/patches/index.d.ts +0 -0
  212. /package/dist/{types → utils}/index.d.ts +0 -0
  213. /package/dist/{types → utils}/index.js +0 -0
@@ -1,88 +1,149 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
4
- const utils_1 = require("../../utils");
5
- const priority_queue_1 = require("../priority-queue");
6
- class AbstractVertex {
7
- get id() {
8
- return this._id;
2
+ var __values = (this && this.__values) || function(o) {
3
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
+ if (m) return m.call(o);
5
+ if (o && typeof o.length === "number") return {
6
+ next: function () {
7
+ if (o && i >= o.length) o = void 0;
8
+ return { value: o && o[i++], done: !o };
9
+ }
10
+ };
11
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12
+ };
13
+ var __read = (this && this.__read) || function (o, n) {
14
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
15
+ if (!m) return o;
16
+ var i = m.call(o), r, ar = [], e;
17
+ try {
18
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
9
19
  }
10
- set id(v) {
11
- this._id = v;
20
+ catch (error) { e = { error: error }; }
21
+ finally {
22
+ try {
23
+ if (r && !r.done && (m = i["return"])) m.call(i);
24
+ }
25
+ finally { if (e) throw e.error; }
26
+ }
27
+ return ar;
28
+ };
29
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
30
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
31
+ if (ar || !(i in from)) {
32
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
33
+ ar[i] = from[i];
34
+ }
12
35
  }
13
- constructor(id) {
36
+ return to.concat(ar || Array.prototype.slice.call(from));
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
40
+ var utils_1 = require("../../utils");
41
+ var priority_queue_1 = require("../priority-queue");
42
+ var AbstractVertex = /** @class */ (function () {
43
+ function AbstractVertex(id) {
14
44
  this._id = id;
15
45
  }
16
- }
46
+ Object.defineProperty(AbstractVertex.prototype, "id", {
47
+ get: function () {
48
+ return this._id;
49
+ },
50
+ set: function (v) {
51
+ this._id = v;
52
+ },
53
+ enumerable: false,
54
+ configurable: true
55
+ });
56
+ return AbstractVertex;
57
+ }());
17
58
  exports.AbstractVertex = AbstractVertex;
18
- class AbstractEdge {
19
- get weight() {
20
- return this._weight;
21
- }
22
- set weight(v) {
23
- this._weight = v;
24
- }
25
- get hashCode() {
26
- return this._hashCode;
27
- }
28
- set hashCode(v) {
29
- this._hashCode = v;
30
- }
31
- constructor(weight) {
59
+ var AbstractEdge = /** @class */ (function () {
60
+ function AbstractEdge(weight) {
32
61
  if (weight === undefined)
33
62
  weight = AbstractEdge.DEFAULT_EDGE_WEIGHT;
34
63
  this._weight = weight;
35
64
  this._hashCode = (0, utils_1.uuidV4)();
36
65
  }
37
- }
66
+ Object.defineProperty(AbstractEdge.prototype, "weight", {
67
+ get: function () {
68
+ return this._weight;
69
+ },
70
+ set: function (v) {
71
+ this._weight = v;
72
+ },
73
+ enumerable: false,
74
+ configurable: true
75
+ });
76
+ Object.defineProperty(AbstractEdge.prototype, "hashCode", {
77
+ get: function () {
78
+ return this._hashCode;
79
+ },
80
+ set: function (v) {
81
+ this._hashCode = v;
82
+ },
83
+ enumerable: false,
84
+ configurable: true
85
+ });
86
+ AbstractEdge.DEFAULT_EDGE_WEIGHT = 1;
87
+ return AbstractEdge;
88
+ }());
38
89
  exports.AbstractEdge = AbstractEdge;
39
- AbstractEdge.DEFAULT_EDGE_WEIGHT = 1;
40
90
  // Connected Component === Largest Connected Sub-Graph
41
- class AbstractGraph {
42
- constructor() {
91
+ var AbstractGraph = /** @class */ (function () {
92
+ function AbstractGraph() {
43
93
  this._vertices = new Map();
44
94
  // unionFind() {
45
95
  // }
46
96
  /**--- end find cycles --- */
47
97
  // Minimum Spanning Tree
48
98
  }
49
- getVertex(vertexOrId) {
50
- const vertexId = this.getVertexId(vertexOrId);
99
+ AbstractGraph.prototype.getVertex = function (vertexOrId) {
100
+ var vertexId = this.getVertexId(vertexOrId);
51
101
  return this._vertices.get(vertexId) || null;
52
- }
53
- getVertexId(vertexOrId) {
102
+ };
103
+ AbstractGraph.prototype.getVertexId = function (vertexOrId) {
54
104
  return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
55
- }
56
- containsVertex(vertexOrId) {
105
+ };
106
+ AbstractGraph.prototype.containsVertex = function (vertexOrId) {
57
107
  return this._vertices.has(this.getVertexId(vertexOrId));
58
- }
59
- vertexSet() {
108
+ };
109
+ AbstractGraph.prototype.vertexSet = function () {
60
110
  return this._vertices;
61
- }
62
- addVertex(newVertex) {
111
+ };
112
+ AbstractGraph.prototype.addVertex = function (newVertex) {
63
113
  if (this.containsVertex(newVertex)) {
64
114
  return false;
65
115
  }
66
116
  this._vertices.set(newVertex.id, newVertex);
67
117
  return true;
68
- }
69
- removeVertex(vertexOrId) {
70
- const vertexId = this.getVertexId(vertexOrId);
118
+ };
119
+ AbstractGraph.prototype.removeVertex = function (vertexOrId) {
120
+ var vertexId = this.getVertexId(vertexOrId);
71
121
  return this._vertices.delete(vertexId);
72
- }
73
- removeAllVertices(vertices) {
74
- const removed = [];
75
- for (const v of vertices) {
76
- removed.push(this.removeVertex(v));
122
+ };
123
+ AbstractGraph.prototype.removeAllVertices = function (vertices) {
124
+ var e_1, _a;
125
+ var removed = [];
126
+ try {
127
+ for (var vertices_1 = __values(vertices), vertices_1_1 = vertices_1.next(); !vertices_1_1.done; vertices_1_1 = vertices_1.next()) {
128
+ var v = vertices_1_1.value;
129
+ removed.push(this.removeVertex(v));
130
+ }
131
+ }
132
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
133
+ finally {
134
+ try {
135
+ if (vertices_1_1 && !vertices_1_1.done && (_a = vertices_1.return)) _a.call(vertices_1);
136
+ }
137
+ finally { if (e_1) throw e_1.error; }
77
138
  }
78
139
  return removed.length > 0;
79
- }
80
- containsEdge(v1, v2) {
81
- const edge = this.getEdge(v1, v2);
140
+ };
141
+ AbstractGraph.prototype.containsEdge = function (v1, v2) {
142
+ var edge = this.getEdge(v1, v2);
82
143
  return !!edge;
83
- }
84
- setEdgeWeight(srcOrId, destOrId, weight) {
85
- const edge = this.getEdge(srcOrId, destOrId);
144
+ };
145
+ AbstractGraph.prototype.setEdgeWeight = function (srcOrId, destOrId, weight) {
146
+ var edge = this.getEdge(srcOrId, destOrId);
86
147
  if (edge) {
87
148
  edge.weight = weight;
88
149
  return true;
@@ -90,76 +151,112 @@ class AbstractGraph {
90
151
  else {
91
152
  return false;
92
153
  }
93
- }
94
- getAllPathsBetween(v1, v2) {
95
- const paths = [];
96
- const vertex1 = this.getVertex(v1);
97
- const vertex2 = this.getVertex(v2);
154
+ };
155
+ AbstractGraph.prototype.getAllPathsBetween = function (v1, v2) {
156
+ var _this = this;
157
+ var paths = [];
158
+ var vertex1 = this.getVertex(v1);
159
+ var vertex2 = this.getVertex(v2);
98
160
  if (!(vertex1 && vertex2)) {
99
161
  return [];
100
162
  }
101
- const dfs = (cur, dest, visiting, path) => {
163
+ var dfs = function (cur, dest, visiting, path) {
164
+ var e_2, _a;
102
165
  visiting.set(cur, true);
103
166
  if (cur === dest) {
104
- paths.push([vertex1, ...path]);
167
+ paths.push(__spreadArray([vertex1], __read(path), false));
105
168
  }
106
- const neighbors = this.getNeighbors(cur);
107
- for (const neighbor of neighbors) {
169
+ var neighbors = _this.getNeighbors(cur);
170
+ var _loop_1 = function (neighbor) {
108
171
  if (!visiting.get(neighbor)) {
109
172
  path.push(neighbor);
110
173
  dfs(neighbor, dest, visiting, path);
111
- (0, utils_1.arrayRemove)(path, vertex => vertex === neighbor);
174
+ (0, utils_1.arrayRemove)(path, function (vertex) { return vertex === neighbor; });
112
175
  }
176
+ };
177
+ try {
178
+ for (var neighbors_1 = __values(neighbors), neighbors_1_1 = neighbors_1.next(); !neighbors_1_1.done; neighbors_1_1 = neighbors_1.next()) {
179
+ var neighbor = neighbors_1_1.value;
180
+ _loop_1(neighbor);
181
+ }
182
+ }
183
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
184
+ finally {
185
+ try {
186
+ if (neighbors_1_1 && !neighbors_1_1.done && (_a = neighbors_1.return)) _a.call(neighbors_1);
187
+ }
188
+ finally { if (e_2) throw e_2.error; }
113
189
  }
114
190
  visiting.set(cur, false);
115
191
  };
116
192
  dfs(vertex1, vertex2, new Map(), []);
117
193
  return paths;
118
- }
119
- getPathSumWeight(path) {
194
+ };
195
+ AbstractGraph.prototype.getPathSumWeight = function (path) {
120
196
  var _a;
121
- let sum = 0;
122
- for (let i = 0; i < path.length; i++) {
197
+ var sum = 0;
198
+ for (var i = 0; i < path.length; i++) {
123
199
  sum += ((_a = this.getEdge(path[i], path[i + 1])) === null || _a === void 0 ? void 0 : _a.weight) || 0;
124
200
  }
125
201
  return sum;
126
- }
127
- getMinCostBetween(v1, v2, isWeight) {
202
+ };
203
+ AbstractGraph.prototype.getMinCostBetween = function (v1, v2, isWeight) {
204
+ var e_3, _a, e_4, _b;
128
205
  if (isWeight === undefined)
129
206
  isWeight = false;
130
207
  if (isWeight) {
131
- const allPaths = this.getAllPathsBetween(v1, v2);
132
- let min = Infinity;
133
- for (const path of allPaths) {
134
- min = Math.min(this.getPathSumWeight(path), min);
208
+ var allPaths = this.getAllPathsBetween(v1, v2);
209
+ var min = Infinity;
210
+ try {
211
+ for (var allPaths_1 = __values(allPaths), allPaths_1_1 = allPaths_1.next(); !allPaths_1_1.done; allPaths_1_1 = allPaths_1.next()) {
212
+ var path = allPaths_1_1.value;
213
+ min = Math.min(this.getPathSumWeight(path), min);
214
+ }
215
+ }
216
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
217
+ finally {
218
+ try {
219
+ if (allPaths_1_1 && !allPaths_1_1.done && (_a = allPaths_1.return)) _a.call(allPaths_1);
220
+ }
221
+ finally { if (e_3) throw e_3.error; }
135
222
  }
136
223
  return min;
137
224
  }
138
225
  else {
139
226
  // BFS
140
- const vertex2 = this.getVertex(v2);
141
- const vertex1 = this.getVertex(v1);
227
+ var vertex2 = this.getVertex(v2);
228
+ var vertex1 = this.getVertex(v1);
142
229
  if (!(vertex1 && vertex2)) {
143
230
  return null;
144
231
  }
145
- const visited = new Map();
146
- const queue = [vertex1];
232
+ var visited = new Map();
233
+ var queue = [vertex1];
147
234
  visited.set(vertex1, true);
148
- let cost = 0;
235
+ var cost = 0;
149
236
  while (queue.length > 0) {
150
- for (let i = 0; i < queue.length; i++) {
151
- const cur = queue.shift();
237
+ for (var i = 0; i < queue.length; i++) {
238
+ var cur = queue.shift();
152
239
  if (cur === vertex2) {
153
240
  return cost;
154
241
  }
155
242
  // TODO consider optimizing to AbstractGraph
156
243
  if (cur !== undefined) {
157
- const neighbors = this.getNeighbors(cur);
158
- for (const neighbor of neighbors) {
159
- if (!visited.has(neighbor)) {
160
- visited.set(neighbor, true);
161
- queue.push(neighbor);
244
+ var neighbors = this.getNeighbors(cur);
245
+ try {
246
+ for (var neighbors_2 = (e_4 = void 0, __values(neighbors)), neighbors_2_1 = neighbors_2.next(); !neighbors_2_1.done; neighbors_2_1 = neighbors_2.next()) {
247
+ var neighbor = neighbors_2_1.value;
248
+ if (!visited.has(neighbor)) {
249
+ visited.set(neighbor, true);
250
+ queue.push(neighbor);
251
+ }
252
+ }
253
+ }
254
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
255
+ finally {
256
+ try {
257
+ if (neighbors_2_1 && !neighbors_2_1.done && (_b = neighbors_2.return)) _b.call(neighbors_2);
162
258
  }
259
+ finally { if (e_4) throw e_4.error; }
163
260
  }
164
261
  }
165
262
  }
@@ -167,53 +264,79 @@ class AbstractGraph {
167
264
  }
168
265
  return null;
169
266
  }
170
- }
171
- getMinPathBetween(v1, v2, isWeight) {
267
+ };
268
+ AbstractGraph.prototype.getMinPathBetween = function (v1, v2, isWeight) {
269
+ var e_5, _a;
270
+ var _this = this;
172
271
  if (isWeight === undefined)
173
272
  isWeight = false;
174
273
  if (isWeight) {
175
- const allPaths = this.getAllPathsBetween(v1, v2);
176
- let min = Infinity;
177
- let minIndex = -1;
178
- let index = 0;
179
- for (const path of allPaths) {
180
- const pathSumWeight = this.getPathSumWeight(path);
181
- if (pathSumWeight < min) {
182
- min = pathSumWeight;
183
- minIndex = index;
184
- }
185
- index++;
274
+ var allPaths = this.getAllPathsBetween(v1, v2);
275
+ var min = Infinity;
276
+ var minIndex = -1;
277
+ var index = 0;
278
+ try {
279
+ for (var allPaths_2 = __values(allPaths), allPaths_2_1 = allPaths_2.next(); !allPaths_2_1.done; allPaths_2_1 = allPaths_2.next()) {
280
+ var path = allPaths_2_1.value;
281
+ var pathSumWeight = this.getPathSumWeight(path);
282
+ if (pathSumWeight < min) {
283
+ min = pathSumWeight;
284
+ minIndex = index;
285
+ }
286
+ index++;
287
+ }
288
+ }
289
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
290
+ finally {
291
+ try {
292
+ if (allPaths_2_1 && !allPaths_2_1.done && (_a = allPaths_2.return)) _a.call(allPaths_2);
293
+ }
294
+ finally { if (e_5) throw e_5.error; }
186
295
  }
187
296
  return allPaths[minIndex] || null;
188
297
  }
189
298
  else {
190
299
  // BFS
191
- let minPath = [];
192
- const vertex1 = this.getVertex(v1);
193
- const vertex2 = this.getVertex(v2);
194
- if (!(vertex1 && vertex2)) {
300
+ var minPath_1 = [];
301
+ var vertex1_1 = this.getVertex(v1);
302
+ var vertex2 = this.getVertex(v2);
303
+ if (!(vertex1_1 && vertex2)) {
195
304
  return [];
196
305
  }
197
- const dfs = (cur, dest, visiting, path) => {
306
+ var dfs_1 = function (cur, dest, visiting, path) {
307
+ var e_6, _a;
198
308
  visiting.set(cur, true);
199
309
  if (cur === dest) {
200
- minPath = [vertex1, ...path];
310
+ minPath_1 = __spreadArray([vertex1_1], __read(path), false);
201
311
  return;
202
312
  }
203
- const neighbors = this.getNeighbors(cur);
204
- for (const neighbor of neighbors) {
313
+ var neighbors = _this.getNeighbors(cur);
314
+ var _loop_2 = function (neighbor) {
205
315
  if (!visiting.get(neighbor)) {
206
316
  path.push(neighbor);
207
- dfs(neighbor, dest, visiting, path);
208
- (0, utils_1.arrayRemove)(path, vertex => vertex === neighbor);
317
+ dfs_1(neighbor, dest, visiting, path);
318
+ (0, utils_1.arrayRemove)(path, function (vertex) { return vertex === neighbor; });
209
319
  }
320
+ };
321
+ try {
322
+ for (var neighbors_3 = __values(neighbors), neighbors_3_1 = neighbors_3.next(); !neighbors_3_1.done; neighbors_3_1 = neighbors_3.next()) {
323
+ var neighbor = neighbors_3_1.value;
324
+ _loop_2(neighbor);
325
+ }
326
+ }
327
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
328
+ finally {
329
+ try {
330
+ if (neighbors_3_1 && !neighbors_3_1.done && (_a = neighbors_3.return)) _a.call(neighbors_3);
331
+ }
332
+ finally { if (e_6) throw e_6.error; }
210
333
  }
211
334
  visiting.set(cur, false);
212
335
  };
213
- dfs(vertex1, vertex2, new Map(), []);
214
- return minPath;
336
+ dfs_1(vertex1_1, vertex2, new Map(), []);
337
+ return minPath_1;
215
338
  }
216
- }
339
+ };
217
340
  /**
218
341
  * Dijkstra algorithm time: O(VE) space: O(V + E)
219
342
  * @param src
@@ -221,60 +344,98 @@ class AbstractGraph {
221
344
  * @param getMinDist
222
345
  * @param genPaths
223
346
  */
224
- dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
347
+ AbstractGraph.prototype.dijkstraWithoutHeap = function (src, dest, getMinDist, genPaths) {
348
+ var e_7, _a, e_8, _b;
225
349
  if (getMinDist === undefined)
226
350
  getMinDist = false;
227
351
  if (genPaths === undefined)
228
352
  genPaths = false;
229
353
  if (dest === undefined)
230
354
  dest = null;
231
- let minDist = Infinity;
232
- let minDest = null;
233
- let minPath = [];
234
- const paths = [];
235
- const vertices = this._vertices;
236
- const distMap = new Map();
237
- const seen = new Set();
238
- const preMap = new Map(); // predecessor
239
- const srcVertex = this.getVertex(src);
240
- const destVertex = dest ? this.getVertex(dest) : null;
355
+ var minDist = Infinity;
356
+ var minDest = null;
357
+ var minPath = [];
358
+ var paths = [];
359
+ var vertices = this._vertices;
360
+ var distMap = new Map();
361
+ var seen = new Set();
362
+ var preMap = new Map(); // predecessor
363
+ var srcVertex = this.getVertex(src);
364
+ var destVertex = dest ? this.getVertex(dest) : null;
241
365
  if (!srcVertex) {
242
366
  return null;
243
367
  }
244
- for (const vertex of vertices) {
245
- distMap.set(vertex[1], Infinity);
368
+ try {
369
+ for (var vertices_2 = __values(vertices), vertices_2_1 = vertices_2.next(); !vertices_2_1.done; vertices_2_1 = vertices_2.next()) {
370
+ var vertex = vertices_2_1.value;
371
+ var vertexOrId = vertex[1];
372
+ if (vertexOrId instanceof AbstractVertex)
373
+ distMap.set(vertexOrId, Infinity);
374
+ }
375
+ }
376
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
377
+ finally {
378
+ try {
379
+ if (vertices_2_1 && !vertices_2_1.done && (_a = vertices_2.return)) _a.call(vertices_2);
380
+ }
381
+ finally { if (e_7) throw e_7.error; }
246
382
  }
247
383
  distMap.set(srcVertex, 0);
248
384
  preMap.set(srcVertex, null);
249
- const getMinOfNoSeen = () => {
250
- let min = Infinity;
251
- let minV = null;
252
- for (const [key, val] of distMap) {
253
- if (!seen.has(key)) {
254
- if (val < min) {
255
- min = val;
256
- minV = key;
385
+ var getMinOfNoSeen = function () {
386
+ var e_9, _a;
387
+ var min = Infinity;
388
+ var minV = null;
389
+ try {
390
+ for (var distMap_1 = __values(distMap), distMap_1_1 = distMap_1.next(); !distMap_1_1.done; distMap_1_1 = distMap_1.next()) {
391
+ var _b = __read(distMap_1_1.value, 2), key = _b[0], val = _b[1];
392
+ if (!seen.has(key)) {
393
+ if (val < min) {
394
+ min = val;
395
+ minV = key;
396
+ }
257
397
  }
258
398
  }
259
399
  }
400
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
401
+ finally {
402
+ try {
403
+ if (distMap_1_1 && !distMap_1_1.done && (_a = distMap_1.return)) _a.call(distMap_1);
404
+ }
405
+ finally { if (e_9) throw e_9.error; }
406
+ }
260
407
  return minV;
261
408
  };
262
- const getPaths = (minV) => {
263
- for (const vertex of vertices) {
264
- const path = [vertex[1]];
265
- let parent = preMap.get(vertex[1]);
266
- while (parent) {
267
- path.push(parent);
268
- parent = preMap.get(parent);
269
- }
270
- const reversed = path.reverse();
271
- if (vertex[1] === minV)
272
- minPath = reversed;
273
- paths.push(reversed);
409
+ var getPaths = function (minV) {
410
+ var e_10, _a;
411
+ try {
412
+ for (var vertices_3 = __values(vertices), vertices_3_1 = vertices_3.next(); !vertices_3_1.done; vertices_3_1 = vertices_3.next()) {
413
+ var vertex = vertices_3_1.value;
414
+ var vertexOrId = vertex[1];
415
+ if (vertexOrId instanceof AbstractVertex) {
416
+ var path = [vertexOrId];
417
+ var parent = preMap.get(vertexOrId);
418
+ while (parent) {
419
+ path.push(parent);
420
+ parent = preMap.get(parent);
421
+ }
422
+ var reversed = path.reverse();
423
+ if (vertex[1] === minV)
424
+ minPath = reversed;
425
+ paths.push(reversed);
426
+ }
427
+ }
428
+ }
429
+ catch (e_10_1) { e_10 = { error: e_10_1 }; }
430
+ finally {
431
+ try {
432
+ if (vertices_3_1 && !vertices_3_1.done && (_a = vertices_3.return)) _a.call(vertices_3);
433
+ }
434
+ finally { if (e_10) throw e_10.error; }
274
435
  }
275
436
  };
276
- for (let i = 1; i < vertices.size; i++) {
277
- const cur = getMinOfNoSeen();
437
+ for (var i = 1; i < vertices.size; i++) {
438
+ var cur = getMinOfNoSeen();
278
439
  if (cur) {
279
440
  seen.add(cur);
280
441
  if (destVertex && destVertex === cur) {
@@ -284,28 +445,38 @@ class AbstractGraph {
284
445
  if (genPaths) {
285
446
  getPaths(destVertex);
286
447
  }
287
- return { distMap, preMap, seen, paths, minDist, minPath };
288
- }
289
- const neighbors = this.getNeighbors(cur);
290
- for (const neighbor of neighbors) {
291
- if (!seen.has(neighbor)) {
292
- const edge = this.getEdge(cur, neighbor);
293
- if (edge) {
294
- const curFromMap = distMap.get(cur);
295
- const neighborFromMap = distMap.get(neighbor);
296
- // TODO after no-non-null-assertion not ensure the logic
297
- if (curFromMap !== undefined && neighborFromMap !== undefined) {
298
- if (edge.weight + curFromMap < neighborFromMap) {
299
- distMap.set(neighbor, edge.weight + curFromMap);
300
- preMap.set(neighbor, cur);
448
+ return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
449
+ }
450
+ var neighbors = this.getNeighbors(cur);
451
+ try {
452
+ for (var neighbors_4 = (e_8 = void 0, __values(neighbors)), neighbors_4_1 = neighbors_4.next(); !neighbors_4_1.done; neighbors_4_1 = neighbors_4.next()) {
453
+ var neighbor = neighbors_4_1.value;
454
+ if (!seen.has(neighbor)) {
455
+ var edge = this.getEdge(cur, neighbor);
456
+ if (edge) {
457
+ var curFromMap = distMap.get(cur);
458
+ var neighborFromMap = distMap.get(neighbor);
459
+ // TODO after no-non-null-assertion not ensure the logic
460
+ if (curFromMap !== undefined && neighborFromMap !== undefined) {
461
+ if (edge.weight + curFromMap < neighborFromMap) {
462
+ distMap.set(neighbor, edge.weight + curFromMap);
463
+ preMap.set(neighbor, cur);
464
+ }
301
465
  }
302
466
  }
303
467
  }
304
468
  }
305
469
  }
470
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
471
+ finally {
472
+ try {
473
+ if (neighbors_4_1 && !neighbors_4_1.done && (_b = neighbors_4.return)) _b.call(neighbors_4);
474
+ }
475
+ finally { if (e_8) throw e_8.error; }
476
+ }
306
477
  }
307
478
  }
308
- getMinDist && distMap.forEach((d, v) => {
479
+ getMinDist && distMap.forEach(function (d, v) {
309
480
  if (v !== srcVertex) {
310
481
  if (d < minDist) {
311
482
  minDist = d;
@@ -315,8 +486,8 @@ class AbstractGraph {
315
486
  }
316
487
  });
317
488
  genPaths && getPaths(minDest);
318
- return { distMap, preMap, seen, paths, minDist, minPath };
319
- }
489
+ return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
490
+ };
320
491
  /**
321
492
  * Dijkstra algorithm time: O(logVE) space: O(V + E)
322
493
  * @param src
@@ -324,52 +495,79 @@ class AbstractGraph {
324
495
  * @param getMinDist
325
496
  * @param genPaths
326
497
  */
327
- dijkstra(src, dest, getMinDist, genPaths) {
328
- var _a;
498
+ AbstractGraph.prototype.dijkstra = function (src, dest, getMinDist, genPaths) {
499
+ var e_11, _a, e_12, _b;
500
+ var _c;
329
501
  if (getMinDist === undefined)
330
502
  getMinDist = false;
331
503
  if (genPaths === undefined)
332
504
  genPaths = false;
333
505
  if (dest === undefined)
334
506
  dest = null;
335
- let minDist = Infinity;
336
- let minDest = null;
337
- let minPath = [];
338
- const paths = [];
339
- const vertices = this._vertices;
340
- const distMap = new Map();
341
- const seen = new Set();
342
- const preMap = new Map(); // predecessor
343
- const srcVertex = this.getVertex(src);
344
- const destVertex = dest ? this.getVertex(dest) : null;
507
+ var minDist = Infinity;
508
+ var minDest = null;
509
+ var minPath = [];
510
+ var paths = [];
511
+ var vertices = this._vertices;
512
+ var distMap = new Map();
513
+ var seen = new Set();
514
+ var preMap = new Map(); // predecessor
515
+ var srcVertex = this.getVertex(src);
516
+ var destVertex = dest ? this.getVertex(dest) : null;
345
517
  if (!srcVertex) {
346
518
  return null;
347
519
  }
348
- for (const vertex of vertices) {
349
- distMap.set(vertex[1], Infinity);
520
+ try {
521
+ for (var vertices_4 = __values(vertices), vertices_4_1 = vertices_4.next(); !vertices_4_1.done; vertices_4_1 = vertices_4.next()) {
522
+ var vertex = vertices_4_1.value;
523
+ var vertexOrId = vertex[1];
524
+ if (vertexOrId instanceof AbstractVertex)
525
+ distMap.set(vertexOrId, Infinity);
526
+ }
527
+ }
528
+ catch (e_11_1) { e_11 = { error: e_11_1 }; }
529
+ finally {
530
+ try {
531
+ if (vertices_4_1 && !vertices_4_1.done && (_a = vertices_4.return)) _a.call(vertices_4);
532
+ }
533
+ finally { if (e_11) throw e_11.error; }
350
534
  }
351
- const heap = new priority_queue_1.PriorityQueue({ comparator: (a, b) => a.id - b.id });
535
+ var heap = new priority_queue_1.PriorityQueue({ comparator: function (a, b) { return a.id - b.id; } });
352
536
  heap.offer({ id: 0, val: srcVertex });
353
537
  distMap.set(srcVertex, 0);
354
538
  preMap.set(srcVertex, null);
355
- const getPaths = (minV) => {
356
- for (const vertex of vertices) {
357
- const path = [vertex[1]];
358
- let parent = preMap.get(vertex[1]);
359
- while (parent) {
360
- path.push(parent);
361
- parent = preMap.get(parent);
362
- }
363
- const reversed = path.reverse();
364
- if (vertex[1] === minV)
365
- minPath = reversed;
366
- paths.push(reversed);
539
+ var getPaths = function (minV) {
540
+ var e_13, _a;
541
+ try {
542
+ for (var vertices_5 = __values(vertices), vertices_5_1 = vertices_5.next(); !vertices_5_1.done; vertices_5_1 = vertices_5.next()) {
543
+ var vertex = vertices_5_1.value;
544
+ var vertexOrId = vertex[1];
545
+ if (vertexOrId instanceof AbstractVertex) {
546
+ var path = [vertexOrId];
547
+ var parent = preMap.get(vertexOrId);
548
+ while (parent) {
549
+ path.push(parent);
550
+ parent = preMap.get(parent);
551
+ }
552
+ var reversed = path.reverse();
553
+ if (vertex[1] === minV)
554
+ minPath = reversed;
555
+ paths.push(reversed);
556
+ }
557
+ }
558
+ }
559
+ catch (e_13_1) { e_13 = { error: e_13_1 }; }
560
+ finally {
561
+ try {
562
+ if (vertices_5_1 && !vertices_5_1.done && (_a = vertices_5.return)) _a.call(vertices_5);
563
+ }
564
+ finally { if (e_13) throw e_13.error; }
367
565
  }
368
566
  };
369
567
  while (heap.size > 0) {
370
- const curHeapNode = heap.poll();
371
- const dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.id;
372
- const cur = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.val;
568
+ var curHeapNode = heap.poll();
569
+ var dist = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.id;
570
+ var cur = curHeapNode === null || curHeapNode === void 0 ? void 0 : curHeapNode.val;
373
571
  if (dist !== undefined) {
374
572
  if (cur) {
375
573
  seen.add(cur);
@@ -380,29 +578,39 @@ class AbstractGraph {
380
578
  if (genPaths) {
381
579
  getPaths(destVertex);
382
580
  }
383
- return { distMap, preMap, seen, paths, minDist, minPath };
581
+ return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
384
582
  }
385
- const neighbors = this.getNeighbors(cur);
386
- for (const neighbor of neighbors) {
387
- if (!seen.has(neighbor)) {
388
- const weight = (_a = this.getEdge(cur, neighbor)) === null || _a === void 0 ? void 0 : _a.weight;
389
- if (typeof weight === 'number') {
390
- const distSrcToNeighbor = distMap.get(neighbor);
391
- if (distSrcToNeighbor) {
392
- if (dist + weight < distSrcToNeighbor) {
393
- heap.offer({ id: dist + weight, val: neighbor });
394
- preMap.set(neighbor, cur);
395
- distMap.set(neighbor, dist + weight);
583
+ var neighbors = this.getNeighbors(cur);
584
+ try {
585
+ for (var neighbors_5 = (e_12 = void 0, __values(neighbors)), neighbors_5_1 = neighbors_5.next(); !neighbors_5_1.done; neighbors_5_1 = neighbors_5.next()) {
586
+ var neighbor = neighbors_5_1.value;
587
+ if (!seen.has(neighbor)) {
588
+ var weight = (_c = this.getEdge(cur, neighbor)) === null || _c === void 0 ? void 0 : _c.weight;
589
+ if (typeof weight === 'number') {
590
+ var distSrcToNeighbor = distMap.get(neighbor);
591
+ if (distSrcToNeighbor) {
592
+ if (dist + weight < distSrcToNeighbor) {
593
+ heap.offer({ id: dist + weight, val: neighbor });
594
+ preMap.set(neighbor, cur);
595
+ distMap.set(neighbor, dist + weight);
596
+ }
396
597
  }
397
598
  }
398
599
  }
399
600
  }
400
601
  }
602
+ catch (e_12_1) { e_12 = { error: e_12_1 }; }
603
+ finally {
604
+ try {
605
+ if (neighbors_5_1 && !neighbors_5_1.done && (_b = neighbors_5.return)) _b.call(neighbors_5);
606
+ }
607
+ finally { if (e_12) throw e_12.error; }
608
+ }
401
609
  }
402
610
  }
403
611
  }
404
612
  if (getMinDist) {
405
- distMap.forEach((d, v) => {
613
+ distMap.forEach(function (d, v) {
406
614
  if (v !== srcVertex) {
407
615
  if (d < minDist) {
408
616
  minDist = d;
@@ -415,8 +623,8 @@ class AbstractGraph {
415
623
  if (genPaths) {
416
624
  getPaths(minDest);
417
625
  }
418
- return { distMap, preMap, seen, paths, minDist, minPath };
419
- }
626
+ return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
627
+ };
420
628
  /**
421
629
  * BellmanFord time:O(VE) space:O(V)
422
630
  * one to rest pairs
@@ -425,39 +633,40 @@ class AbstractGraph {
425
633
  * @param getMin
426
634
  * @param genPath
427
635
  */
428
- bellmanFord(src, scanNegativeCycle, getMin, genPath) {
636
+ AbstractGraph.prototype.bellmanFord = function (src, scanNegativeCycle, getMin, genPath) {
637
+ var e_14, _a;
429
638
  if (getMin === undefined)
430
639
  getMin = false;
431
640
  if (genPath === undefined)
432
641
  genPath = false;
433
- const srcVertex = this.getVertex(src);
434
- const paths = [];
435
- const distMap = new Map();
436
- const preMap = new Map(); // predecessor
437
- let min = Infinity;
438
- let minPath = [];
642
+ var srcVertex = this.getVertex(src);
643
+ var paths = [];
644
+ var distMap = new Map();
645
+ var preMap = new Map(); // predecessor
646
+ var min = Infinity;
647
+ var minPath = [];
439
648
  // TODO
440
- let hasNegativeCycle = undefined;
649
+ var hasNegativeCycle;
441
650
  if (scanNegativeCycle)
442
651
  hasNegativeCycle = false;
443
652
  if (!srcVertex)
444
- return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
445
- const vertices = this._vertices;
446
- const numOfVertices = vertices.size;
447
- const edges = this.edgeSet();
448
- const numOfEdges = edges.length;
449
- this._vertices.forEach(vertex => {
653
+ return { hasNegativeCycle: hasNegativeCycle, distMap: distMap, preMap: preMap, paths: paths, min: min, minPath: minPath };
654
+ var vertices = this._vertices;
655
+ var numOfVertices = vertices.size;
656
+ var edges = this.edgeSet();
657
+ var numOfEdges = edges.length;
658
+ this._vertices.forEach(function (vertex) {
450
659
  distMap.set(vertex, Infinity);
451
660
  });
452
661
  distMap.set(srcVertex, 0);
453
- for (let i = 1; i < numOfVertices; ++i) {
454
- for (let j = 0; j < numOfEdges; ++j) {
455
- const ends = this.getEndsOfEdge(edges[j]);
662
+ for (var i = 1; i < numOfVertices; ++i) {
663
+ for (var j = 0; j < numOfEdges; ++j) {
664
+ var ends = this.getEndsOfEdge(edges[j]);
456
665
  if (ends) {
457
- const [s, d] = ends;
458
- const weight = edges[j].weight;
459
- const sWeight = distMap.get(s);
460
- const dWeight = distMap.get(d);
666
+ var _b = __read(ends, 2), s = _b[0], d = _b[1];
667
+ var weight = edges[j].weight;
668
+ var sWeight = distMap.get(s);
669
+ var dWeight = distMap.get(d);
461
670
  if (sWeight !== undefined && dWeight !== undefined) {
462
671
  if (distMap.get(s) !== Infinity && sWeight + weight < dWeight) {
463
672
  distMap.set(d, sWeight + weight);
@@ -467,9 +676,9 @@ class AbstractGraph {
467
676
  }
468
677
  }
469
678
  }
470
- let minDest = null;
679
+ var minDest = null;
471
680
  if (getMin) {
472
- distMap.forEach((d, v) => {
681
+ distMap.forEach(function (d, v) {
473
682
  if (v !== srcVertex) {
474
683
  if (d < min) {
475
684
  min = d;
@@ -480,59 +689,72 @@ class AbstractGraph {
480
689
  });
481
690
  }
482
691
  if (genPath) {
483
- for (const vertex of vertices) {
484
- const path = [vertex[1]];
485
- let parent = preMap.get(vertex[1]);
486
- while (parent !== undefined) {
487
- path.push(parent);
488
- parent = preMap.get(parent);
692
+ try {
693
+ for (var vertices_6 = __values(vertices), vertices_6_1 = vertices_6.next(); !vertices_6_1.done; vertices_6_1 = vertices_6.next()) {
694
+ var vertex = vertices_6_1.value;
695
+ var vertexOrId = vertex[1];
696
+ if (vertexOrId instanceof AbstractVertex) {
697
+ var path = [vertexOrId];
698
+ var parent = preMap.get(vertexOrId);
699
+ while (parent !== undefined) {
700
+ path.push(parent);
701
+ parent = preMap.get(parent);
702
+ }
703
+ var reversed = path.reverse();
704
+ if (vertex[1] === minDest)
705
+ minPath = reversed;
706
+ paths.push(reversed);
707
+ }
489
708
  }
490
- const reversed = path.reverse();
491
- if (vertex[1] === minDest)
492
- minPath = reversed;
493
- paths.push(reversed);
709
+ }
710
+ catch (e_14_1) { e_14 = { error: e_14_1 }; }
711
+ finally {
712
+ try {
713
+ if (vertices_6_1 && !vertices_6_1.done && (_a = vertices_6.return)) _a.call(vertices_6);
714
+ }
715
+ finally { if (e_14) throw e_14.error; }
494
716
  }
495
717
  }
496
- for (let j = 0; j < numOfEdges; ++j) {
497
- const ends = this.getEndsOfEdge(edges[j]);
718
+ for (var j = 0; j < numOfEdges; ++j) {
719
+ var ends = this.getEndsOfEdge(edges[j]);
498
720
  if (ends) {
499
- const [s] = ends;
500
- const weight = edges[j].weight;
501
- const sWeight = distMap.get(s);
721
+ var _c = __read(ends, 1), s = _c[0];
722
+ var weight = edges[j].weight;
723
+ var sWeight = distMap.get(s);
502
724
  if (sWeight) {
503
725
  if (sWeight !== Infinity && sWeight + weight < sWeight)
504
726
  hasNegativeCycle = true;
505
727
  }
506
728
  }
507
729
  }
508
- return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
509
- }
730
+ return { hasNegativeCycle: hasNegativeCycle, distMap: distMap, preMap: preMap, paths: paths, min: min, minPath: minPath };
731
+ };
510
732
  /**
511
733
  * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
512
734
  * all pairs
513
735
  */
514
- floyd() {
736
+ AbstractGraph.prototype.floyd = function () {
515
737
  var _a;
516
- const idAndVertices = [...this._vertices];
517
- const n = idAndVertices.length;
518
- const costs = [];
519
- const predecessor = [];
738
+ var idAndVertices = __spreadArray([], __read(this._vertices), false);
739
+ var n = idAndVertices.length;
740
+ var costs = [];
741
+ var predecessor = [];
520
742
  // successors
521
- for (let i = 0; i < n; i++) {
743
+ for (var i = 0; i < n; i++) {
522
744
  costs[i] = [];
523
745
  predecessor[i] = [];
524
- for (let j = 0; j < n; j++) {
746
+ for (var j = 0; j < n; j++) {
525
747
  predecessor[i][j] = null;
526
748
  }
527
749
  }
528
- for (let i = 0; i < n; i++) {
529
- for (let j = 0; j < n; j++) {
750
+ for (var i = 0; i < n; i++) {
751
+ for (var j = 0; j < n; j++) {
530
752
  costs[i][j] = ((_a = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])) === null || _a === void 0 ? void 0 : _a.weight) || Infinity;
531
753
  }
532
754
  }
533
- for (let k = 0; k < n; k++) {
534
- for (let i = 0; i < n; i++) {
535
- for (let j = 0; j < n; j++) {
755
+ for (var k = 0; k < n; k++) {
756
+ for (var i = 0; i < n; i++) {
757
+ for (var j = 0; j < n; j++) {
536
758
  if (costs[i][j] > costs[i][k] + costs[k][j]) {
537
759
  costs[i][j] = costs[i][k] + costs[k][j];
538
760
  predecessor[i][j] = idAndVertices[k][1];
@@ -540,8 +762,8 @@ class AbstractGraph {
540
762
  }
541
763
  }
542
764
  }
543
- return { costs, predecessor };
544
- }
765
+ return { costs: costs, predecessor: predecessor };
766
+ };
545
767
  /**--- start find cycles --- */
546
768
  /**
547
769
  * Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
@@ -550,11 +772,12 @@ class AbstractGraph {
550
772
  * Tarjan solve the bi-connected components of undirected graphs;
551
773
  * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
552
774
  */
553
- tarjan(needArticulationPoints, needBridges, needSCCs, needCycles) {
775
+ AbstractGraph.prototype.tarjan = function (needArticulationPoints, needBridges, needSCCs, needCycles) {
554
776
  // !! in undirected graph we will not let child visit parent when DFS
555
777
  // !! articulation point(in DFS search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
556
778
  // !! bridge: low(child) > dfn(cur)
557
- const defaultConfig = false;
779
+ var _this = this;
780
+ var defaultConfig = false;
558
781
  if (needArticulationPoints === undefined)
559
782
  needArticulationPoints = defaultConfig;
560
783
  if (needBridges === undefined)
@@ -563,60 +786,71 @@ class AbstractGraph {
563
786
  needSCCs = defaultConfig;
564
787
  if (needCycles === undefined)
565
788
  needCycles = defaultConfig;
566
- const dfnMap = new Map();
567
- const lowMap = new Map();
568
- const vertices = this._vertices;
569
- vertices.forEach(v => {
789
+ var dfnMap = new Map();
790
+ var lowMap = new Map();
791
+ var vertices = this._vertices;
792
+ vertices.forEach(function (v) {
570
793
  dfnMap.set(v, -1);
571
794
  lowMap.set(v, Infinity);
572
795
  });
573
- const [root] = vertices.values();
574
- const articulationPoints = [];
575
- const bridges = [];
576
- let dfn = 0;
577
- const dfs = (cur, parent) => {
796
+ var _a = __read(vertices.values(), 1), root = _a[0];
797
+ var articulationPoints = [];
798
+ var bridges = [];
799
+ var dfn = 0;
800
+ var dfs = function (cur, parent) {
801
+ var e_15, _a;
578
802
  dfn++;
579
803
  dfnMap.set(cur, dfn);
580
804
  lowMap.set(cur, dfn);
581
- const neighbors = this.getNeighbors(cur);
582
- let childCount = 0; // child in DFS tree not child in graph
583
- for (const neighbor of neighbors) {
584
- if (neighbor !== parent) {
585
- if (dfnMap.get(neighbor) === -1) {
586
- childCount++;
587
- dfs(neighbor, cur);
588
- }
589
- const childLow = lowMap.get(neighbor);
590
- const curLow = lowMap.get(cur);
591
- // TODO after no-non-null-assertion not ensure the logic
592
- if (curLow !== undefined && childLow !== undefined) {
593
- lowMap.set(cur, Math.min(curLow, childLow));
594
- }
595
- const curFromMap = dfnMap.get(cur);
596
- if (childLow !== undefined && curFromMap !== undefined) {
597
- if (needArticulationPoints) {
598
- if ((cur === root && childCount >= 2) || ((cur !== root) && (childLow >= curFromMap))) {
599
- // todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
600
- articulationPoints.push(cur);
601
- }
805
+ var neighbors = _this.getNeighbors(cur);
806
+ var childCount = 0; // child in DFS tree not child in graph
807
+ try {
808
+ for (var neighbors_6 = __values(neighbors), neighbors_6_1 = neighbors_6.next(); !neighbors_6_1.done; neighbors_6_1 = neighbors_6.next()) {
809
+ var neighbor = neighbors_6_1.value;
810
+ if (neighbor !== parent) {
811
+ if (dfnMap.get(neighbor) === -1) {
812
+ childCount++;
813
+ dfs(neighbor, cur);
814
+ }
815
+ var childLow = lowMap.get(neighbor);
816
+ var curLow = lowMap.get(cur);
817
+ // TODO after no-non-null-assertion not ensure the logic
818
+ if (curLow !== undefined && childLow !== undefined) {
819
+ lowMap.set(cur, Math.min(curLow, childLow));
602
820
  }
603
- if (needBridges) {
604
- if (childLow > curFromMap) {
605
- const edgeCurToNeighbor = this.getEdge(cur, neighbor);
606
- if (edgeCurToNeighbor) {
607
- bridges.push(edgeCurToNeighbor);
821
+ var curFromMap = dfnMap.get(cur);
822
+ if (childLow !== undefined && curFromMap !== undefined) {
823
+ if (needArticulationPoints) {
824
+ if ((cur === root && childCount >= 2) || ((cur !== root) && (childLow >= curFromMap))) {
825
+ // todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
826
+ articulationPoints.push(cur);
827
+ }
828
+ }
829
+ if (needBridges) {
830
+ if (childLow > curFromMap) {
831
+ var edgeCurToNeighbor = _this.getEdge(cur, neighbor);
832
+ if (edgeCurToNeighbor) {
833
+ bridges.push(edgeCurToNeighbor);
834
+ }
608
835
  }
609
836
  }
610
837
  }
611
838
  }
612
839
  }
613
840
  }
841
+ catch (e_15_1) { e_15 = { error: e_15_1 }; }
842
+ finally {
843
+ try {
844
+ if (neighbors_6_1 && !neighbors_6_1.done && (_a = neighbors_6.return)) _a.call(neighbors_6);
845
+ }
846
+ finally { if (e_15) throw e_15.error; }
847
+ }
614
848
  };
615
849
  dfs(root, null);
616
- let SCCs = new Map();
617
- const getSCCs = () => {
618
- const SCCs = new Map();
619
- lowMap.forEach((low, vertex) => {
850
+ var SCCs = new Map();
851
+ var getSCCs = function () {
852
+ var SCCs = new Map();
853
+ lowMap.forEach(function (low, vertex) {
620
854
  var _a;
621
855
  if (!SCCs.has(low)) {
622
856
  SCCs.set(low, [vertex]);
@@ -630,19 +864,20 @@ class AbstractGraph {
630
864
  if (needSCCs) {
631
865
  SCCs = getSCCs();
632
866
  }
633
- const cycles = new Map();
867
+ var cycles = new Map();
634
868
  if (needCycles) {
635
- let SCCs = new Map();
636
- if (SCCs.size < 1) {
637
- SCCs = getSCCs();
869
+ var SCCs_1 = new Map();
870
+ if (SCCs_1.size < 1) {
871
+ SCCs_1 = getSCCs();
638
872
  }
639
- SCCs.forEach((SCC, low) => {
873
+ SCCs_1.forEach(function (SCC, low) {
640
874
  if (SCC.length > 1) {
641
875
  cycles.set(low, SCC);
642
876
  }
643
877
  });
644
878
  }
645
- return { dfnMap, lowMap, bridges, articulationPoints, SCCs, cycles };
646
- }
647
- }
879
+ return { dfnMap: dfnMap, lowMap: lowMap, bridges: bridges, articulationPoints: articulationPoints, SCCs: SCCs, cycles: cycles };
880
+ };
881
+ return AbstractGraph;
882
+ }());
648
883
  exports.AbstractGraph = AbstractGraph;