data-structure-typed 0.9.16 → 1.3.1

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 (264) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +665 -172
  3. package/dist/bundle.js +2 -0
  4. package/dist/bundle.js.LICENSE.txt +13 -0
  5. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  6. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
  7. package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
  8. package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
  9. package/dist/data-structures/binary-tree/avl-tree.js +142 -116
  10. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
  14. package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
  15. package/dist/data-structures/binary-tree/bst.d.ts +117 -23
  16. package/dist/data-structures/binary-tree/bst.js +233 -240
  17. package/dist/data-structures/binary-tree/index.d.ts +1 -0
  18. package/dist/data-structures/binary-tree/index.js +1 -0
  19. package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
  20. package/dist/data-structures/binary-tree/rb-tree.js +40 -5
  21. package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
  22. package/dist/data-structures/binary-tree/segment-tree.js +126 -93
  23. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  24. package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
  25. package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
  26. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  27. package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
  28. package/dist/data-structures/graph/abstract-graph.js +610 -572
  29. package/dist/data-structures/graph/directed-graph.d.ts +173 -16
  30. package/dist/data-structures/graph/directed-graph.js +345 -313
  31. package/dist/data-structures/graph/index.d.ts +1 -0
  32. package/dist/data-structures/graph/index.js +1 -0
  33. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  34. package/dist/data-structures/graph/map-graph.js +111 -0
  35. package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
  36. package/dist/data-structures/graph/undirected-graph.js +203 -178
  37. package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
  38. package/dist/data-structures/hash/coordinate-map.js +59 -36
  39. package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
  40. package/dist/data-structures/hash/coordinate-set.js +49 -33
  41. package/dist/data-structures/hash/hash-table.d.ts +2 -1
  42. package/dist/data-structures/hash/hash-table.js +4 -0
  43. package/dist/data-structures/hash/pair.d.ts +2 -1
  44. package/dist/data-structures/hash/pair.js +4 -0
  45. package/dist/data-structures/hash/tree-map.d.ts +2 -1
  46. package/dist/data-structures/hash/tree-map.js +4 -0
  47. package/dist/data-structures/hash/tree-set.d.ts +2 -1
  48. package/dist/data-structures/hash/tree-set.js +4 -0
  49. package/dist/data-structures/heap/heap.d.ts +62 -45
  50. package/dist/data-structures/heap/heap.js +124 -86
  51. package/dist/data-structures/heap/max-heap.d.ts +13 -5
  52. package/dist/data-structures/heap/max-heap.js +18 -28
  53. package/dist/data-structures/heap/min-heap.d.ts +14 -5
  54. package/dist/data-structures/heap/min-heap.js +19 -28
  55. package/dist/data-structures/index.d.ts +1 -1
  56. package/dist/data-structures/index.js +1 -1
  57. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
  58. package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
  59. package/dist/data-structures/linked-list/index.d.ts +1 -0
  60. package/dist/data-structures/linked-list/index.js +1 -0
  61. package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
  62. package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
  63. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
  64. package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
  65. package/dist/data-structures/matrix/matrix.d.ts +12 -0
  66. package/dist/data-structures/matrix/matrix.js +21 -8
  67. package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
  68. package/dist/data-structures/matrix/matrix2d.js +146 -80
  69. package/dist/data-structures/matrix/navigator.d.ts +36 -1
  70. package/dist/data-structures/matrix/navigator.js +46 -37
  71. package/dist/data-structures/matrix/vector2d.d.ts +142 -15
  72. package/dist/data-structures/matrix/vector2d.js +215 -109
  73. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
  74. package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
  75. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
  76. package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
  77. package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
  78. package/dist/data-structures/priority-queue/priority-queue.js +244 -143
  79. package/dist/data-structures/queue/deque.d.ts +141 -13
  80. package/dist/data-structures/queue/deque.js +200 -82
  81. package/dist/data-structures/queue/queue.d.ts +65 -38
  82. package/dist/data-structures/queue/queue.js +110 -66
  83. package/dist/data-structures/stack/stack.d.ts +27 -32
  84. package/dist/data-structures/stack/stack.js +47 -53
  85. package/dist/data-structures/tree/index.d.ts +1 -0
  86. package/dist/data-structures/tree/index.js +17 -0
  87. package/dist/data-structures/tree/tree.d.ts +14 -0
  88. package/dist/data-structures/tree/tree.js +60 -0
  89. package/dist/data-structures/trie/trie.d.ts +33 -10
  90. package/dist/data-structures/trie/trie.js +123 -208
  91. package/dist/index.d.ts +3 -0
  92. package/dist/index.js +3 -0
  93. package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
  94. package/dist/interfaces/abstract-graph.d.ts +17 -0
  95. package/dist/interfaces/avl-tree.d.ts +9 -0
  96. package/dist/interfaces/binary-tree.d.ts +6 -0
  97. package/dist/interfaces/bst.d.ts +17 -0
  98. package/dist/interfaces/directed-graph.d.ts +12 -0
  99. package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
  100. package/dist/interfaces/index.js +31 -0
  101. package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
  102. package/dist/interfaces/rb-tree.d.ts +8 -0
  103. package/dist/interfaces/segment-tree.d.ts +1 -0
  104. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  105. package/dist/interfaces/singly-linked-list.js +2 -0
  106. package/dist/interfaces/tree-multiset.d.ts +7 -0
  107. package/dist/interfaces/tree-multiset.js +2 -0
  108. package/dist/interfaces/undirected-graph.d.ts +5 -0
  109. package/dist/interfaces/undirected-graph.js +2 -0
  110. package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
  111. package/dist/types/data-structures/abstract-binary-tree.js +25 -0
  112. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  113. package/dist/types/data-structures/abstract-graph.js +2 -0
  114. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  115. package/dist/types/data-structures/avl-tree.js +2 -0
  116. package/dist/types/data-structures/binary-tree.d.ts +4 -0
  117. package/dist/types/data-structures/binary-tree.js +2 -0
  118. package/dist/types/data-structures/bst.d.ts +13 -0
  119. package/dist/types/data-structures/bst.js +9 -0
  120. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  121. package/dist/types/data-structures/directed-graph.js +9 -0
  122. package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
  123. package/dist/types/data-structures/doubly-linked-list.js +2 -0
  124. package/dist/types/data-structures/heap.d.ts +3 -0
  125. package/dist/types/data-structures/heap.js +2 -0
  126. package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
  127. package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
  128. package/dist/types/data-structures/map-graph.d.ts +1 -0
  129. package/dist/types/data-structures/map-graph.js +2 -0
  130. package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
  131. package/dist/types/data-structures/navigator.js +2 -0
  132. package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
  133. package/dist/types/data-structures/priority-queue.js +2 -0
  134. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  135. package/dist/types/data-structures/rb-tree.js +8 -0
  136. package/dist/types/data-structures/segment-tree.js +2 -0
  137. package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
  138. package/dist/types/data-structures/singly-linked-list.js +2 -0
  139. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  140. package/dist/types/data-structures/tree-multiset.js +2 -0
  141. package/dist/types/helpers.d.ts +1 -0
  142. package/dist/types/helpers.js +2 -0
  143. package/dist/types/index.d.ts +3 -0
  144. package/dist/types/index.js +19 -0
  145. package/dist/types/utils/index.d.ts +2 -0
  146. package/dist/types/utils/index.js +18 -0
  147. package/dist/types/utils/utils.d.ts +7 -0
  148. package/dist/types/utils/utils.js +2 -0
  149. package/dist/types/utils/validate-type.d.ts +19 -0
  150. package/dist/types/utils/validate-type.js +2 -0
  151. package/dist/utils/utils.d.ts +17 -103
  152. package/dist/utils/utils.js +40 -625
  153. package/package.json +134 -23
  154. package/.idea/data-structure-typed.iml +0 -12
  155. package/.idea/modules.xml +0 -8
  156. package/.idea/vcs.xml +0 -6
  157. package/dist/data-structures/trampoline.d.ts +0 -16
  158. package/dist/data-structures/trampoline.js +0 -130
  159. package/dist/data-structures/types/abstract-graph.d.ts +0 -29
  160. package/dist/data-structures/types/avl-tree.d.ts +0 -5
  161. package/dist/data-structures/types/binary-tree.d.ts +0 -16
  162. package/dist/data-structures/types/bst.d.ts +0 -7
  163. package/dist/data-structures/types/directed-graph.d.ts +0 -10
  164. package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
  165. package/dist/data-structures/types/heap.d.ts +0 -7
  166. package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
  167. package/dist/data-structures/types/tree-multiset.d.ts +0 -5
  168. package/dist/data-structures/types/utils.d.ts +0 -52
  169. package/dist/data-structures/types/utils.js +0 -54
  170. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  171. package/src/data-structures/binary-tree/avl-tree.ts +0 -227
  172. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  173. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
  174. package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
  175. package/src/data-structures/binary-tree/bst.ts +0 -395
  176. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  177. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  178. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  179. package/src/data-structures/binary-tree/index.ts +0 -11
  180. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  181. package/src/data-structures/binary-tree/segment-tree.ts +0 -172
  182. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  183. package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
  184. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  185. package/src/data-structures/diagrams/README.md +0 -7
  186. package/src/data-structures/graph/abstract-graph.ts +0 -753
  187. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  188. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  189. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  190. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  191. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  192. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  193. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  194. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  195. package/src/data-structures/graph/diagrams/mst.png +0 -0
  196. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  197. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  198. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  199. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  200. package/src/data-structures/graph/directed-graph.ts +0 -306
  201. package/src/data-structures/graph/index.ts +0 -3
  202. package/src/data-structures/graph/undirected-graph.ts +0 -155
  203. package/src/data-structures/hash/coordinate-map.ts +0 -24
  204. package/src/data-structures/hash/coordinate-set.ts +0 -20
  205. package/src/data-structures/hash/index.ts +0 -6
  206. package/src/data-structures/heap/heap.ts +0 -127
  207. package/src/data-structures/heap/index.ts +0 -3
  208. package/src/data-structures/heap/max-heap.ts +0 -23
  209. package/src/data-structures/heap/min-heap.ts +0 -25
  210. package/src/data-structures/index.ts +0 -12
  211. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
  212. package/src/data-structures/linked-list/index.ts +0 -2
  213. package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
  214. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  215. package/src/data-structures/matrix/index.ts +0 -4
  216. package/src/data-structures/matrix/matrix.ts +0 -13
  217. package/src/data-structures/matrix/matrix2d.ts +0 -125
  218. package/src/data-structures/matrix/navigator.ts +0 -87
  219. package/src/data-structures/matrix/vector2d.ts +0 -189
  220. package/src/data-structures/priority-queue/index.ts +0 -3
  221. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
  222. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
  223. package/src/data-structures/priority-queue/priority-queue.ts +0 -200
  224. package/src/data-structures/queue/deque.ts +0 -139
  225. package/src/data-structures/queue/index.ts +0 -2
  226. package/src/data-structures/queue/queue.ts +0 -122
  227. package/src/data-structures/stack/index.ts +0 -1
  228. package/src/data-structures/stack/stack.ts +0 -103
  229. package/src/data-structures/trampoline.ts +0 -51
  230. package/src/data-structures/trie/index.ts +0 -1
  231. package/src/data-structures/trie/trie.ts +0 -203
  232. package/src/data-structures/types/abstract-graph.ts +0 -51
  233. package/src/data-structures/types/avl-tree.ts +0 -6
  234. package/src/data-structures/types/binary-tree.ts +0 -15
  235. package/src/data-structures/types/bst.ts +0 -5
  236. package/src/data-structures/types/directed-graph.ts +0 -18
  237. package/src/data-structures/types/doubly-linked-list.ts +0 -1
  238. package/src/data-structures/types/heap.ts +0 -8
  239. package/src/data-structures/types/navigator.ts +0 -12
  240. package/src/data-structures/types/priority-queue.ts +0 -9
  241. package/src/data-structures/types/segment-tree.ts +0 -1
  242. package/src/data-structures/types/singly-linked-list.ts +0 -15
  243. package/src/data-structures/types/tree-multiset.ts +0 -3
  244. package/src/data-structures/types/utils.ts +0 -173
  245. package/src/index.ts +0 -1
  246. package/src/utils/index.ts +0 -1
  247. package/src/utils/utils.ts +0 -505
  248. package/tsconfig.json +0 -56
  249. /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
  250. /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
  251. /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
  252. /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
  253. /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
  254. /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
  255. /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
  256. /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
  257. /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
  258. /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
  259. /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
  260. /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
  261. /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
  262. /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
  263. /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
  264. /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
@@ -1,306 +0,0 @@
1
- import {arrayRemove} from '../../utils';
2
- import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
3
- import type {IDirectedGraph, TopologicalStatus, VertexId} from '../types';
4
-
5
- export class DirectedVertex extends AbstractVertex {
6
- constructor(id: VertexId) {
7
- super(id);
8
- }
9
- }
10
-
11
- export class DirectedEdge extends AbstractEdge {
12
- constructor(src: VertexId, dest: VertexId, weight?: number) {
13
- super(weight);
14
- this._src = src;
15
- this._dest = dest;
16
- }
17
-
18
- private _src: VertexId;
19
- get src(): VertexId {
20
- return this._src;
21
- }
22
-
23
- set src(v: VertexId) {
24
- this._src = v;
25
- }
26
-
27
-
28
- private _dest: VertexId;
29
- get dest(): VertexId {
30
- return this._dest;
31
- }
32
-
33
- set dest(v: VertexId) {
34
- this._dest = v;
35
- }
36
- }
37
-
38
- // Strongly connected, One direction connected, Weakly connected
39
- export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> extends AbstractGraph<V, E> implements IDirectedGraph<V, E> {
40
-
41
- protected _outEdgeMap: Map<V, E[]> = new Map<V, E[]>();
42
-
43
- protected _inEdgeMap: Map<V, E[]> = new Map<V, E[]>();
44
-
45
- constructor() {
46
- super();
47
- }
48
-
49
- getEdge(srcOrId: V | null | VertexId, destOrId: V | null | VertexId): E | null {
50
- let edges: E[] = [];
51
-
52
- if (srcOrId !== null && destOrId !== null) {
53
- const src: V | null = this.getVertex(srcOrId);
54
- const dest: V | null = this.getVertex(destOrId);
55
-
56
- if (src && dest) {
57
- const srcOutEdges = this._outEdgeMap.get(src);
58
- if (srcOutEdges) {
59
- edges = srcOutEdges.filter(edge => edge.dest === dest.id);
60
- }
61
- }
62
- }
63
-
64
- return edges[0] || null;
65
- }
66
-
67
- addEdge(edge: E): boolean {
68
- if (!(this.containsVertex(edge.src) && this.containsVertex(edge.dest))) {
69
- return false;
70
- }
71
-
72
- const srcVertex = this.getVertex(edge.src);
73
- const destVertex = this.getVertex(edge.dest);
74
-
75
- // TODO after no-non-null-assertion not ensure the logic
76
- if (srcVertex && destVertex) {
77
- const srcOutEdges = this._outEdgeMap.get(srcVertex);
78
- if (srcOutEdges) {
79
- srcOutEdges.push(edge);
80
- } else {
81
- this._outEdgeMap.set(srcVertex, [edge]);
82
- }
83
-
84
- const destInEdges = this._inEdgeMap.get(destVertex);
85
- if (destInEdges) {
86
- destInEdges.push(edge);
87
- } else {
88
- this._inEdgeMap.set(destVertex, [edge]);
89
- }
90
- return true;
91
- } else {
92
- return false;
93
- }
94
- }
95
-
96
- removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null {
97
-
98
- const src: V | null = this.getVertex(srcOrId);
99
- const dest: V | null = this.getVertex(destOrId);
100
- let removed: E | null = null;
101
- if (!src || !dest) {
102
- return null;
103
- }
104
-
105
- const srcOutEdges = this._outEdgeMap.get(src);
106
- if (srcOutEdges) {
107
- arrayRemove<E>(srcOutEdges, (edge: DirectedEdge) => edge.dest === dest.id);
108
- }
109
-
110
- const destInEdges = this._inEdgeMap.get(dest);
111
- if (destInEdges) {
112
- removed = arrayRemove<E>(destInEdges, (edge: DirectedEdge) => edge.src === src.id)[0] || null;
113
- }
114
- return removed;
115
- }
116
-
117
- removeEdge(edge: E): E | null {
118
- let removed: E | null = null;
119
- const src = this.getVertex(edge.src);
120
- const dest = this.getVertex(edge.dest);
121
- if (src && dest) {
122
- const srcOutEdges = this._outEdgeMap.get(src);
123
- if (srcOutEdges && srcOutEdges.length > 0) {
124
- arrayRemove(srcOutEdges, (edge: DirectedEdge) => edge.src === src.id);
125
- }
126
-
127
- const destInEdges = this._inEdgeMap.get(dest);
128
- if (destInEdges && destInEdges.length > 0) {
129
- removed = arrayRemove(destInEdges, (edge: DirectedEdge) => edge.dest === dest.id)[0];
130
- }
131
-
132
- }
133
-
134
- return removed;
135
- }
136
-
137
- removeAllEdges(src: VertexId | V, dest: VertexId | V): E[] {
138
- return [];
139
- }
140
-
141
- incomingEdgesOf(vertexOrId: V | VertexId): E[] {
142
- const target = this.getVertex(vertexOrId);
143
- if (target) {
144
- return this._inEdgeMap.get(target) || [];
145
- }
146
- return [];
147
- }
148
-
149
- outgoingEdgesOf(vertexOrId: V | VertexId): E[] {
150
- const target = this.getVertex(vertexOrId);
151
- if (target) {
152
- return this._outEdgeMap.get(target) || [];
153
- }
154
- return [];
155
- }
156
-
157
- degreeOf(vertexOrId: VertexId | V): number {
158
- return this.outDegreeOf(vertexOrId) + this.inDegreeOf(vertexOrId);
159
- }
160
-
161
- inDegreeOf(vertexOrId: VertexId | V): number {
162
- return this.incomingEdgesOf(vertexOrId).length;
163
- }
164
-
165
- outDegreeOf(vertexOrId: VertexId | V): number {
166
- return this.outgoingEdgesOf(vertexOrId).length;
167
- }
168
-
169
- edgesOf(vertexOrId: VertexId | V): E[] {
170
- return [...this.outgoingEdgesOf(vertexOrId), ...this.incomingEdgesOf(vertexOrId)];
171
- }
172
-
173
- getEdgeSrc(e: E): V | null {
174
- return this.getVertex(e.src);
175
- }
176
-
177
- getEdgeDest(e: E): V | null {
178
- return this.getVertex(e.dest);
179
- }
180
-
181
- getDestinations(vertex: V | VertexId | null): V[] {
182
- if (vertex === null) {
183
- return [];
184
- }
185
- const destinations: V[] = [];
186
- const outgoingEdges = this.outgoingEdgesOf(vertex);
187
- for (const outEdge of outgoingEdges) {
188
- const child = this.getEdgeDest(outEdge);
189
- if (child) {
190
- destinations.push(child);
191
- }
192
- }
193
- return destinations;
194
- }
195
-
196
- /**--- start find cycles --- */
197
-
198
- /**
199
- * when stored with adjacency list time: O(V+E)
200
- * when stored with adjacency matrix time: O(V^2)
201
- */
202
- topologicalSort(): (V | VertexId)[] | null {
203
- // vector<vector<int>> g;
204
- // vector<int> color;
205
- // int last;
206
- // bool hasCycle;
207
- //
208
- // bool topo_sort() {
209
- // int n = g.size();
210
- // vector<int> degree(n, 0);
211
- // queue<int> q;
212
- // for (int i = 0; i < n; i++) {
213
- // degree[i] = g[i].size();
214
- // if (degree[i] <= 1) {
215
- // q.push(i);
216
- // }
217
- // }
218
- // int cnt = 0;
219
- // while (!q.empty()) {
220
- // cnt++;
221
- // int root = q.front();
222
- // q.pop();
223
- // for (auto child : g[root]) {
224
- // degree[child]--;
225
- // if (degree[child] == 1) {
226
- // q.push(child);
227
- // }
228
- // }
229
- // }
230
- // return (cnt != n);
231
- // }
232
- // When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
233
- // When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
234
- const statusMap: Map<V | VertexId, TopologicalStatus> = new Map<V, TopologicalStatus>();
235
- for (const entry of this._vertices) {
236
- statusMap.set(entry[1], 0);
237
- }
238
-
239
- const sorted: (V | VertexId)[] = [];
240
- let hasCycle = false;
241
- const dfs = (cur: V | VertexId) => {
242
- statusMap.set(cur, 1);
243
- const children = this.getDestinations(cur);
244
- for (const child of children) {
245
- const childStatus = statusMap.get(child);
246
- if (childStatus === 0) {
247
- dfs(child);
248
- } else if (childStatus === 1) {
249
- hasCycle = true;
250
- }
251
- }
252
- statusMap.set(cur, 2);
253
- sorted.push(cur);
254
- };
255
-
256
- for (const entry of this._vertices) {
257
- if (statusMap.get(entry[1]) === 0) {
258
- dfs(entry[1]);
259
- }
260
- }
261
-
262
- if (hasCycle) {
263
- return null;
264
- }
265
- return sorted.reverse();
266
- }
267
-
268
- /**--- end find cycles --- */
269
-
270
- edgeSet(): E[] {
271
- let edges: E[] = [];
272
- this._outEdgeMap.forEach(outEdges => {
273
- edges = [...edges, ...outEdges];
274
- });
275
- return edges;
276
- }
277
-
278
- getNeighbors(vertexOrId: V | VertexId): V[] {
279
- const neighbors: V[] = [];
280
- const vertex = this.getVertex(vertexOrId);
281
- if (vertex) {
282
- const outEdges = this.outgoingEdgesOf(vertex);
283
- for (const outEdge of outEdges) {
284
- const neighbor = this.getVertex(outEdge.dest);
285
- // TODO after no-non-null-assertion not ensure the logic
286
- if (neighbor) {
287
- neighbors.push(neighbor);
288
- }
289
- }
290
- }
291
- return neighbors;
292
- }
293
-
294
- getEndsOfEdge(edge: E): [V, V] | null {
295
- if (!this.containsEdge(edge.src, edge.dest)) {
296
- return null;
297
- }
298
- const v1 = this.getVertex(edge.src);
299
- const v2 = this.getVertex(edge.dest);
300
- if (v1 && v2) {
301
- return [v1, v2];
302
- } else {
303
- return null;
304
- }
305
- }
306
- }
@@ -1,3 +0,0 @@
1
- export * from './abstract-graph';
2
- export * from './directed-graph';
3
- export * from './undirected-graph';
@@ -1,155 +0,0 @@
1
- import {arrayRemove} from '../../utils';
2
- import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
3
- import type {VertexId} from '../types';
4
-
5
- export class UndirectedVertex extends AbstractVertex {
6
- constructor(id: VertexId) {
7
- super(id);
8
- }
9
- }
10
-
11
- export class UndirectedEdge extends AbstractEdge {
12
- constructor(v1: VertexId, v2: VertexId, weight?: number) {
13
- super(weight);
14
- this._vertices = [v1, v2];
15
- }
16
-
17
- private _vertices: [VertexId, VertexId];
18
-
19
- public get vertices() {
20
- return this._vertices;
21
- }
22
-
23
- public set vertices(v: [VertexId, VertexId]) {
24
- this._vertices = v;
25
- }
26
- }
27
-
28
- export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdge> extends AbstractGraph<V, E> {
29
- protected _edges: Map<V, E[]> = new Map();
30
-
31
- constructor() {
32
- super();
33
- }
34
-
35
- getEdge(v1: V | null | VertexId, v2: V | null | VertexId): E | null {
36
- let edges: E[] | undefined = [];
37
-
38
- if (v1 !== null && v2 !== null) {
39
- const vertex1: V | null = this.getVertex(v1);
40
- const vertex2: V | null = this.getVertex(v2);
41
-
42
- if (vertex1 && vertex2) {
43
- edges = this._edges.get(vertex1)?.filter(e => e.vertices.includes(vertex2.id));
44
- }
45
- }
46
-
47
- return edges ? edges[0] || null : null;
48
- }
49
-
50
- addEdge(edge: E): boolean {
51
- for (const end of edge.vertices) {
52
- const endVertex = this.getVertex(end);
53
- if (endVertex === null) return false;
54
- if (endVertex) {
55
- const edges = this._edges.get(endVertex);
56
- if (edges) {
57
- edges.push(edge);
58
- } else {
59
- this._edges.set(endVertex, [edge]);
60
- }
61
- }
62
- }
63
- return true;
64
- }
65
-
66
- removeEdgeBetween(v1: V | VertexId, v2: V | VertexId): E | null {
67
-
68
- const vertex1: V | null = this.getVertex(v1);
69
- const vertex2: V | null = this.getVertex(v2);
70
-
71
- if (!vertex1 || !vertex2) {
72
- return null;
73
- }
74
-
75
- const v1Edges = this._edges.get(vertex1);
76
- let removed: E | null = null;
77
- if (v1Edges) {
78
- removed = arrayRemove<E>(v1Edges, (e: UndirectedEdge) => e.vertices.includes(vertex2.id))[0] || null;
79
- }
80
- const v2Edges = this._edges.get(vertex2);
81
- if (v2Edges) {
82
- arrayRemove<E>(v2Edges, (e: UndirectedEdge) => e.vertices.includes(vertex1.id));
83
- }
84
- return removed;
85
- }
86
-
87
-
88
- removeEdge(edge: E): E | null {
89
- return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
90
- }
91
-
92
- degreeOf(vertexOrId: VertexId | V): number {
93
- const vertex = this.getVertex(vertexOrId);
94
- if (vertex) {
95
- return this._edges.get(vertex)?.length || 0;
96
- } else {
97
- return 0;
98
- }
99
- }
100
-
101
- edgesOf(vertexOrId: VertexId | V): E[] {
102
- const vertex = this.getVertex(vertexOrId);
103
- if (vertex) {
104
- return this._edges.get(vertex) || [];
105
- } else {
106
- return [];
107
- }
108
- }
109
-
110
- edgeSet(): E[] {
111
- const edgeSet: Set<E> = new Set();
112
- this._edges.forEach(edges => {
113
- edges.forEach(edge => {
114
- edgeSet.add(edge);
115
- });
116
- });
117
- return [...edgeSet];
118
- }
119
-
120
- getEdgesOf(vertexOrId: V | VertexId): E[] {
121
- const vertex = this.getVertex(vertexOrId);
122
- if (!vertex) {
123
- return [];
124
- }
125
- return this._edges.get(vertex) || [];
126
- }
127
-
128
- getNeighbors(vertexOrId: V | VertexId): V[] {
129
- const neighbors: V[] = [];
130
- const vertex = this.getVertex(vertexOrId);
131
- if (vertex) {
132
- const neighborEdges = this.getEdgesOf(vertex);
133
- for (const edge of neighborEdges) {
134
- const neighbor = this.getVertex(edge.vertices.filter(e => e !== vertex.id)[0]);
135
- if (neighbor) {
136
- neighbors.push(neighbor);
137
- }
138
- }
139
- }
140
- return neighbors;
141
- }
142
-
143
- getEndsOfEdge(edge: E): [V, V] | null {
144
- if (!this.containsEdge(edge.vertices[0], edge.vertices[1])) {
145
- return null;
146
- }
147
- const v1 = this.getVertex(edge.vertices[0]);
148
- const v2 = this.getVertex(edge.vertices[1]);
149
- if (v1 && v2) {
150
- return [v1, v2];
151
- } else {
152
- return null;
153
- }
154
- }
155
- }
@@ -1,24 +0,0 @@
1
- export class CoordinateMap<V> extends Map<any, V> {
2
- private readonly _joint: string = '_';
3
-
4
- constructor(joint?: string) {
5
- super();
6
- if (joint !== undefined) this._joint = joint;
7
- }
8
-
9
- override has(key: number[]) {
10
- return super.has(key.join(this._joint));
11
- }
12
-
13
- override set(key: number[], value: V) {
14
- return super.set(key.join(this._joint), value);
15
- }
16
-
17
- override get(key: number[]) {
18
- return super.get(key.join(this._joint));
19
- }
20
-
21
- override delete(key: number[]) {
22
- return super.delete(key.join(this._joint));
23
- }
24
- }
@@ -1,20 +0,0 @@
1
- export class CoordinateSet extends Set {
2
- private readonly _joint: string = '_';
3
-
4
- constructor(joint?: string) {
5
- super();
6
- if (joint !== undefined) this._joint = joint;
7
- }
8
-
9
- override has(value: number[]) {
10
- return super.has(value.join(this._joint));
11
- }
12
-
13
- override add(value: number[]) {
14
- return super.add(value.join(this._joint));
15
- }
16
-
17
- override delete(value: number[]) {
18
- return super.delete(value.join(this._joint));
19
- }
20
- }
@@ -1,6 +0,0 @@
1
- export * from './hash-table';
2
- export * from './coordinate-map';
3
- export * from './coordinate-set';
4
- export * from './pair';
5
- export * from './tree-map';
6
- export * from './tree-set';
@@ -1,127 +0,0 @@
1
- import {PriorityQueue} from '../priority-queue';
2
- import type {HeapItem, HeapOptions} from '../types';
3
-
4
- /**
5
- * @copyright 2021 Tyler Zeng <zrwusa@gmail.com>
6
- * @license MIT
7
- *
8
- * @abstract
9
- * @class Heap
10
- */
11
- export abstract class Heap<T> {
12
- protected abstract _pq: PriorityQueue<HeapItem<T>>;
13
- protected _priorityCb: (element: T) => number;
14
-
15
- /**
16
- * Creates a priority queue
17
- * @public
18
- * @params {object} [options]
19
- */
20
- protected constructor(options?: HeapOptions<T>) {
21
- if (options) {
22
- const {priority} = options;
23
- if (priority !== undefined && typeof priority !== 'function') {
24
- throw new Error('.constructor expects a valid priority function');
25
- }
26
- this._priorityCb = priority || ((el) => +el);
27
- } else {
28
- this._priorityCb = (el) => +el;
29
- }
30
- }
31
-
32
- /**
33
- * @public
34
- * @returns {number}
35
- */
36
- get size(): number {
37
- return this._pq.size;
38
- }
39
-
40
- /**
41
- * @public
42
- * @returns {boolean}
43
- */
44
- isEmpty(): boolean {
45
- return this._pq.size < 1;
46
- }
47
-
48
- /**
49
- * Returns an element with highest priority in the queue
50
- * @public
51
- * @returns {object}
52
- */
53
- peek(): HeapItem<T> | null {
54
- return this._pq.peek();
55
- }
56
-
57
- /**
58
- * Returns an element with lowest priority in the queue
59
- * @public
60
- * @returns {object}
61
- */
62
- peekLast(): HeapItem<T> | null {
63
- return this._pq.leaf();
64
- }
65
-
66
- /**
67
- * Adds an element to the queue
68
- * @public
69
- * @param {any} element
70
- * @param priority
71
- * @throws {Error} if priority is not a valid number
72
- */
73
- offer(element: T, priority?: number): Heap<T> {
74
- if (typeof element === 'number') {
75
- priority = element;
76
- } else {
77
- if (priority === undefined) {
78
- throw new Error('.offer expects a numeric priority');
79
- }
80
- }
81
-
82
- if (priority && Number.isNaN(+priority)) {
83
- throw new Error('.offer expects a numeric priority');
84
- }
85
-
86
- if (Number.isNaN(+priority) && Number.isNaN(this._priorityCb(element))) {
87
- throw new Error(
88
- '.offer expects a numeric priority '
89
- + 'or a constructor callback that returns a number'
90
- );
91
- }
92
-
93
- const _priority = !Number.isNaN(+priority) ? priority : this._priorityCb(element);
94
- this._pq.offer({priority: _priority, element});
95
- return this;
96
- }
97
-
98
- /**
99
- * Removes and returns an element with highest priority in the queue
100
- * @public
101
- * @returns {object}
102
- */
103
- poll(): HeapItem<T> | null {
104
- const top = this._pq.poll();
105
- if (!top) {
106
- return null;
107
- }
108
- return top;
109
- }
110
-
111
- /**
112
- * Returns a sorted list of elements
113
- * @public
114
- * @returns {array}
115
- */
116
- toArray(): HeapItem<T>[] {
117
- return this._pq.toArray();
118
- }
119
-
120
- /**
121
- * Clears the queue
122
- * @public
123
- */
124
- clear(): void {
125
- this._pq.clear();
126
- }
127
- }
@@ -1,3 +0,0 @@
1
- export * from './max-heap';
2
- export * from './min-heap';
3
- export * from './heap';