doubly-linked-list-typed 2.0.4 → 2.1.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 (102) hide show
  1. package/README.md +14 -14
  2. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  3. package/dist/data-structures/base/iterable-element-base.js +149 -107
  4. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  5. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  6. package/dist/data-structures/base/linear-base.d.ts +250 -192
  7. package/dist/data-structures/base/linear-base.js +137 -274
  8. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  9. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  11. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  12. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  13. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  14. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  15. package/dist/data-structures/binary-tree/binary-tree.js +612 -879
  16. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  17. package/dist/data-structures/binary-tree/bst.js +505 -481
  18. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  19. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  20. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  21. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  22. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  23. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  24. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  25. package/dist/data-structures/graph/abstract-graph.js +267 -237
  26. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  27. package/dist/data-structures/graph/directed-graph.js +146 -233
  28. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  29. package/dist/data-structures/graph/map-graph.js +56 -59
  30. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  31. package/dist/data-structures/graph/undirected-graph.js +129 -149
  32. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  33. package/dist/data-structures/hash/hash-map.js +270 -457
  34. package/dist/data-structures/heap/heap.d.ts +214 -289
  35. package/dist/data-structures/heap/heap.js +340 -349
  36. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  37. package/dist/data-structures/heap/max-heap.js +11 -66
  38. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  39. package/dist/data-structures/heap/min-heap.js +11 -66
  40. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  41. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  42. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  43. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  44. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  45. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  46. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  47. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  48. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  49. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  50. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  51. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  52. package/dist/data-structures/queue/deque.d.ts +227 -254
  53. package/dist/data-structures/queue/deque.js +309 -348
  54. package/dist/data-structures/queue/queue.d.ts +180 -201
  55. package/dist/data-structures/queue/queue.js +265 -248
  56. package/dist/data-structures/stack/stack.d.ts +124 -102
  57. package/dist/data-structures/stack/stack.js +181 -125
  58. package/dist/data-structures/trie/trie.d.ts +164 -165
  59. package/dist/data-structures/trie/trie.js +189 -172
  60. package/dist/interfaces/binary-tree.d.ts +56 -6
  61. package/dist/interfaces/graph.d.ts +16 -0
  62. package/dist/types/data-structures/base/base.d.ts +1 -1
  63. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  64. package/dist/types/utils/utils.d.ts +6 -6
  65. package/dist/utils/utils.d.ts +110 -49
  66. package/dist/utils/utils.js +148 -73
  67. package/package.json +2 -2
  68. package/src/data-structures/base/iterable-element-base.ts +238 -115
  69. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  70. package/src/data-structures/base/linear-base.ts +271 -277
  71. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  72. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  73. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  74. package/src/data-structures/binary-tree/binary-tree.ts +681 -905
  75. package/src/data-structures/binary-tree/bst.ts +568 -570
  76. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  77. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  78. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  79. package/src/data-structures/graph/abstract-graph.ts +339 -264
  80. package/src/data-structures/graph/directed-graph.ts +146 -236
  81. package/src/data-structures/graph/map-graph.ts +63 -60
  82. package/src/data-structures/graph/undirected-graph.ts +129 -152
  83. package/src/data-structures/hash/hash-map.ts +274 -496
  84. package/src/data-structures/heap/heap.ts +389 -402
  85. package/src/data-structures/heap/max-heap.ts +12 -76
  86. package/src/data-structures/heap/min-heap.ts +13 -76
  87. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  88. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  89. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  90. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  91. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  92. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  93. package/src/data-structures/queue/deque.ts +381 -357
  94. package/src/data-structures/queue/queue.ts +310 -264
  95. package/src/data-structures/stack/stack.ts +217 -131
  96. package/src/data-structures/trie/trie.ts +240 -175
  97. package/src/interfaces/binary-tree.ts +240 -6
  98. package/src/interfaces/graph.ts +37 -0
  99. package/src/types/data-structures/base/base.ts +5 -5
  100. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  101. package/src/types/utils/utils.ts +9 -5
  102. package/src/utils/utils.ts +152 -86
@@ -5,252 +5,220 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { DijkstraResult, EntryCallback, VertexKey } from '../../types';
8
+ import type { DijkstraResult, EntryCallback, GraphOptions, VertexKey } from '../../types';
9
9
  import { IterableEntryBase } from '../base';
10
10
  import { IGraph } from '../../interfaces';
11
11
  export declare abstract class AbstractVertex<V = any> {
12
12
  key: VertexKey;
13
13
  value: V | undefined;
14
- /**
15
- * The function is a protected constructor that takes an key and an optional value as parameters.
16
- * @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
17
- * used to uniquely identify the vertex object.
18
- * @param {V} [value] - The parameter "value" is an optional parameter of type V. It is used to assign a value to the
19
- * vertex. If no value is provided, it will be set to undefined.
20
- */
21
14
  protected constructor(key: VertexKey, value?: V);
22
15
  }
23
16
  export declare abstract class AbstractEdge<E = any> {
24
17
  value: E | undefined;
25
18
  weight: number;
26
- /**
27
- * The above function is a protected constructor that initializes the weight, value, and hash code properties of an
28
- * object.
29
- * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
30
- * a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
31
- * will be assigned.
32
- * @param {VO} [value] - The `value` parameter is of type `VO`, which means it can be any type. It is an optional parameter,
33
- * meaning it can be omitted when creating an instance of the class.
34
- */
35
19
  protected constructor(weight?: number, value?: E);
36
20
  protected _hashCode: string;
37
21
  get hashCode(): string;
38
22
  }
23
+ /**
24
+ * Abstract graph over vertices and edges.
25
+ * @template V - Vertex value type.
26
+ * @template E - Edge value type.
27
+ * @template VO - Concrete vertex subclass (extends AbstractVertex<V>).
28
+ * @template EO - Concrete edge subclass (extends AbstractEdge<E>).
29
+ * @remarks Time O(1), Space O(1)
30
+ * @example examples will be generated by unit test
31
+ */
39
32
  export declare abstract class AbstractGraph<V = any, E = any, VO extends AbstractVertex<V> = AbstractVertex<V>, EO extends AbstractEdge<E> = AbstractEdge<E>> extends IterableEntryBase<VertexKey, V | undefined> implements IGraph<V, E, VO, EO> {
40
- constructor();
33
+ /**
34
+ * Construct a graph with runtime defaults.
35
+ * @param options - `GraphOptions<V>` in `options.graph` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
36
+ * @remarks Time O(1), Space O(1)
37
+ */
38
+ constructor(options?: Partial<Record<string, unknown>>);
39
+ protected _options: GraphOptions<V>;
40
+ get options(): Readonly<GraphOptions<V>>;
41
41
  protected _vertexMap: Map<VertexKey, VO>;
42
42
  get vertexMap(): Map<VertexKey, VO>;
43
43
  set vertexMap(v: Map<VertexKey, VO>);
44
44
  get size(): number;
45
45
  /**
46
- * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
47
- * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
48
- * @param key
49
- * @param value
46
+ * Create a new vertex instance (implementation specific).
47
+ * @param key - Vertex identifier.
48
+ * @param value - Optional payload.
49
+ * @returns Concrete vertex instance.
50
+ * @remarks Time O(1), Space O(1)
50
51
  */
51
52
  abstract createVertex(key: VertexKey, value?: V): VO;
52
53
  /**
53
- * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
54
- * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
55
- * @param srcOrV1
56
- * @param destOrV2
57
- * @param weight
58
- * @param value
54
+ * Create a new edge instance (implementation specific).
55
+ * @param srcOrV1 - Source/endpoint A key.
56
+ * @param destOrV2 - Destination/endpoint B key.
57
+ * @param weight - Edge weight (defaults may apply).
58
+ * @param value - Edge payload.
59
+ * @returns Concrete edge instance.
60
+ * @remarks Time O(1), Space O(1)
59
61
  */
60
62
  abstract createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
63
+ /**
64
+ * Delete an edge by instance.
65
+ * @param edge - Edge instance.
66
+ * @returns Removed edge or `undefined`.
67
+ * @remarks Time O(1) avg, Space O(1)
68
+ */
61
69
  abstract deleteEdge(edge: EO): EO | undefined;
70
+ /**
71
+ * Get an edge between two vertices if present.
72
+ * @param srcOrKey - Source/endpoint A vertex or key.
73
+ * @param destOrKey - Destination/endpoint B vertex or key.
74
+ * @returns Edge instance or `undefined`.
75
+ * @remarks Time O(1) avg, Space O(1)
76
+ */
62
77
  abstract getEdge(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | undefined;
78
+ /**
79
+ * Degree of a vertex in this graph model.
80
+ * @param vertexOrKey - Vertex or key.
81
+ * @returns Non-negative integer degree.
82
+ * @remarks Time O(1) avg, Space O(1)
83
+ */
63
84
  abstract degreeOf(vertexOrKey: VO | VertexKey): number;
85
+ /**
86
+ * All edges in the graph (unique, order not guaranteed).
87
+ * @returns Array of edges.
88
+ * @remarks Time O(E), Space O(E)
89
+ */
64
90
  abstract edgeSet(): EO[];
91
+ /**
92
+ * Incident edges of a vertex.
93
+ * @param vertexOrKey - Vertex or key.
94
+ * @returns Array of incident edges.
95
+ * @remarks Time O(deg), Space O(deg)
96
+ */
65
97
  abstract edgesOf(vertexOrKey: VO | VertexKey): EO[];
98
+ /**
99
+ * One-step neighbors of a vertex.
100
+ * @param vertexOrKey - Vertex or key.
101
+ * @returns Array of neighbor vertices.
102
+ * @remarks Time O(deg), Space O(deg)
103
+ */
66
104
  abstract getNeighbors(vertexOrKey: VO | VertexKey): VO[];
105
+ /**
106
+ * Resolve endpoints of an edge to vertex instances.
107
+ * @param edge - Edge instance.
108
+ * @returns `[v1, v2]` or `undefined` if missing.
109
+ * @remarks Time O(1), Space O(1)
110
+ */
67
111
  abstract getEndsOfEdge(edge: EO): [VO, VO] | undefined;
68
112
  /**
69
- * Time Complexity: O(1) - Constant time for Map lookup.
70
- * Space Complexity: O(1) - Constant space, as it creates only a few variables.
71
- *
72
- * The function "getVertex" returns the vertex with the specified ID or undefined if it doesn't exist.
73
- * @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
74
- * the `_vertexMap` map.
75
- * @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertexMap`
76
- * map. If the vertex does not exist, it returns `undefined`.
113
+ * Get vertex instance by key.
114
+ * @param vertexKey - Vertex key.
115
+ * @returns Vertex instance or `undefined`.
116
+ * @remarks Time O(1), Space O(1)
77
117
  */
78
118
  getVertex(vertexKey: VertexKey): VO | undefined;
79
119
  /**
80
- * Time Complexity: O(1) - Constant time for Map lookup.
81
- * Space Complexity: O(1) - Constant space, as it creates only a few variables.
82
- *
83
- * The function checks if a vertex exists in a graph.
84
- * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
85
- * (`VertexKey`).
86
- * @returns a boolean value.
120
+ * Whether a vertex exists.
121
+ * @param vertexOrKey - Vertex or key.
122
+ * @returns `true` if present, otherwise `false`.
123
+ * @remarks Time O(1) avg, Space O(1)
87
124
  */
88
125
  hasVertex(vertexOrKey: VO | VertexKey): boolean;
89
126
  addVertex(vertex: VO): boolean;
90
127
  addVertex(key: VertexKey, value?: V): boolean;
128
+ /**
129
+ * Type guard: check if a value is a valid vertex key.
130
+ * @param potentialKey - Value to test.
131
+ * @returns `true` if string/number; else `false`.
132
+ * @remarks Time O(1), Space O(1)
133
+ */
91
134
  isVertexKey(potentialKey: any): potentialKey is VertexKey;
92
135
  /**
93
- * Time Complexity: O(1) - Constant time for Map operations.
94
- * Space Complexity: O(1) - Constant space, as it creates only a few variables.
136
+ * Delete a vertex and its incident edges.
137
+ * @param vertexOrKey - Vertex or key.
138
+ * @returns `true` if removed; otherwise `false`.
139
+ * @remarks Time O(deg), Space O(1)
95
140
  */
96
141
  abstract deleteVertex(vertexOrKey: VO | VertexKey): boolean;
97
142
  /**
98
- * Time Complexity: O(K), where K is the number of vertexMap to be removed.
99
- * Space Complexity: O(1) - Constant space, as it creates only a few variables.
100
- *
101
- * The function removes all vertexMap from a graph and returns a boolean indicating if any vertexMap were removed.
102
- * @param {VO[] | VertexKey[]} vertexMap - The `vertexMap` parameter can be either an array of vertexMap (`VO[]`) or an array
103
- * of vertex IDs (`VertexKey[]`).
104
- * @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertexMap
105
- * were removed.
143
+ * Delete multiple vertices.
144
+ * @param vertexMap - Array of vertices or keys.
145
+ * @returns `true` if any vertex was removed.
146
+ * @remarks Time O(sum(deg)), Space O(1)
106
147
  */
107
148
  removeManyVertices(vertexMap: VO[] | VertexKey[]): boolean;
108
149
  /**
109
- * Time Complexity: O(1) - Depends on the implementation in the concrete class.
110
- * Space Complexity: O(1) - Depends on the implementation in the concrete class.
111
- *
112
- * The function checks if there is an edge between two vertexMap and returns a boolean value indicating the result.
113
- * @param {VertexKey | VO} v1 - The parameter v1 can be either a VertexKey or a VO. A VertexKey represents the unique
114
- * identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
115
- * @param {VertexKey | VO} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
116
- * `VertexKey` or a `VO` type, which represents the type of the vertex.
117
- * @returns A boolean value is being returned.
150
+ * Whether an edge exists between two vertices.
151
+ * @param v1 - Endpoint A vertex or key.
152
+ * @param v2 - Endpoint B vertex or key.
153
+ * @returns `true` if present; otherwise `false`.
154
+ * @remarks Time O(1) avg, Space O(1)
118
155
  */
119
156
  hasEdge(v1: VertexKey | VO, v2: VertexKey | VO): boolean;
120
157
  addEdge(edge: EO): boolean;
121
158
  addEdge(src: VO | VertexKey, dest: VO | VertexKey, weight?: number, value?: E): boolean;
122
159
  /**
123
- * Time Complexity: O(1) - Constant time for Map and Edge operations.
124
- * Space Complexity: O(1) - Constant space, as it creates only a few variables.
125
- *
126
- * The function sets the weight of an edge between two vertexMap in a graph.
127
- * @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
128
- * the source vertex of the edge.
129
- * @param {VertexKey | VO} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
130
- * either a `VertexKey` or a vertex object `VO`.
131
- * @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
132
- * and the destination vertex (destOrKey).
133
- * @returns a boolean value. If the edge exists between the source and destination vertexMap, the function will update
134
- * the weight of the edge and return true. If the edge does not exist, the function will return false.
160
+ * Set the weight of an existing edge.
161
+ * @param srcOrKey - Source vertex or key.
162
+ * @param destOrKey - Destination vertex or key.
163
+ * @param weight - New weight.
164
+ * @returns `true` if updated; otherwise `false`.
165
+ * @remarks Time O(1) avg, Space O(1)
135
166
  */
136
167
  setEdgeWeight(srcOrKey: VertexKey | VO, destOrKey: VertexKey | VO, weight: number): boolean;
137
168
  /**
138
- * Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
139
- * Space Complexity: O(P) - Linear space, where P is the number of paths found.
140
- *
141
- * The function `getAllPathsBetween` finds all paths between two vertexMap in a graph using depth-first search.
142
- * @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
143
- * It is the starting vertex for finding paths.
144
- * @param {VO | VertexKey} v2 - The parameter `v2` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
145
- * @param limit - The count of limitation of result array.
146
- * @returns The function `getAllPathsBetween` returns an array of arrays of vertexMap (`VO[][]`).
169
+ * Enumerate simple paths up to a limit.
170
+ * @param v1 - Source vertex or key.
171
+ * @param v2 - Destination vertex or key.
172
+ * @param limit - Maximum number of paths to collect.
173
+ * @returns Array of paths (each path is an array of vertices).
174
+ * @remarks Time O(paths) worst-case exponential, Space O(V + paths)
147
175
  */
148
176
  getAllPathsBetween(v1: VO | VertexKey, v2: VO | VertexKey, limit?: number): VO[][];
149
177
  /**
150
- * Time Complexity: O(L), where L is the length of the path.
151
- * Space Complexity: O(1) - Constant space.
152
- *
153
- * The function calculates the sum of weights along a given path.
154
- * @param {VO[]} path - An array of vertexMap (VO) representing a path in a graph.
155
- * @returns The function `getPathSumWeight` returns the sum of the weights of the edgeMap in the given path.
178
+ * Sum the weights along a vertex path.
179
+ * @param path - Sequence of vertices.
180
+ * @returns Path weight sum (0 if empty or edge missing).
181
+ * @remarks Time O(L), Space O(1) where L is path length
156
182
  */
157
183
  getPathSumWeight(path: VO[]): number;
158
184
  /**
159
- * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
160
- * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
161
- *
162
- * The function `getMinCostBetween` calculates the minimum cost between two vertexMap in a graph, either based on edge
163
- * weights or using a breadth-first search algorithm.
164
- * @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
165
- * @param {VO | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
166
- * you want to find the minimum cost or weight from the source vertex `v1`.
167
- * @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edgeMap have weights.
168
- * If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
169
- * the edgeMap. If isWeight is set to false or not provided, the function will calculate the
170
- * @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertexMap (`v1`
171
- * and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
172
- * vertexMap. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
173
- * minimum number of
185
+ * Minimum hops/weight between two vertices.
186
+ * @param v1 - Source vertex or key.
187
+ * @param v2 - Destination vertex or key.
188
+ * @param isWeight - If `true`, compare by path weight; otherwise by hop count.
189
+ * @returns Minimum cost or `undefined` if missing/unreachable.
190
+ * @remarks Time O((V + E) log V) weighted / O(V + E) unweighted, Space O(V + E)
174
191
  */
175
192
  getMinCostBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean): number | undefined;
176
193
  /**
177
- * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
178
- * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
179
- *
180
- * The function `getMinPathBetween` returns the minimum path between two vertexMap in a graph, either based on weight or
181
- * using a breadth-first search algorithm.
182
- * @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
183
- * object (`VO`) or a vertex ID (`VertexKey`).
184
- * @param {VO | VertexKey} v2 - VO | VertexKey - The second vertex or vertex ID between which we want to find the minimum
185
- * path.
186
- * @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edgeMap in finding the
187
- * minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
188
- * to false, the function will use breadth-first search (BFS) to find the minimum path.
189
- * @param isDFS - If set to true, it enforces the use of getAllPathsBetween to first obtain all possible paths,
190
- * followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
191
- * so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
192
- * @returns The function `getMinPathBetween` returns an array of vertexMap (`VO[]`) representing the minimum path between
193
- * two vertexMap (`v1` and `v2`). If there is no path between the vertexMap, it returns `undefined`.
194
+ * Minimum path (as vertex sequence) between two vertices.
195
+ * @param v1 - Source vertex or key.
196
+ * @param v2 - Destination vertex or key.
197
+ * @param isWeight - If `true`, compare by path weight; otherwise by hop count.
198
+ * @param isDFS - For weighted mode only: if `true`, brute-force all paths; if `false`, use Dijkstra.
199
+ * @returns Vertex sequence, or `undefined`/empty when unreachable depending on branch.
200
+ * @remarks Time O((V + E) log V) weighted / O(V + E) unweighted, Space O(V + E)
194
201
  */
195
202
  getMinPathBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean, isDFS?: boolean): VO[] | undefined;
196
203
  /**
197
- * Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
198
- * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
199
- *
200
- * The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertexMap in
201
- * a graph without using a heap data structure.
202
- * @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
203
- * vertex object or a vertex ID.
204
- * @param {VO | VertexKey | undefined} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
205
- * parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
206
- * identifier. If no destination is provided, the value is set to `undefined`.
207
- * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
208
- * distance from the source vertex to the destination vertex should be calculated and returned in the result. If
209
- * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
210
- * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
211
- * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
212
- * shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
213
- * @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
214
- */
215
- dijkstraWithoutHeap(src: VO | VertexKey, dest?: VO | VertexKey | undefined, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<VO>;
216
- /**
217
- * Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
218
- * Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
219
- *
220
- * Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
221
- * The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
222
- * optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
223
- * @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
224
- * start. It can be either a vertex object or a vertex ID.
225
- * @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
226
- * vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
227
- * will calculate the shortest paths to all other vertexMap from the source vertex.
228
- * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
229
- * distance from the source vertex to the destination vertex should be calculated and returned in the result. If
230
- * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
231
- * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
232
- * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
233
- * shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
234
- * @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
235
- */
236
- dijkstra(src: VO | VertexKey, dest?: VO | VertexKey | undefined, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<VO>;
237
- /**
238
- * Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
239
- * Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
240
- *
241
- * one to rest pairs
242
- * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edgeMap for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edgeMap, the Bellman-Ford algorithm is more flexible in some scenarios.
243
- * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
244
- * all other vertexMap in a graph, and optionally detects negative cycles and generates the minimum path.
245
- * @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
246
- * start calculating the shortest paths. It can be either a vertex object or a vertex ID.
247
- * @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
248
- * @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
249
- * calculate the minimum distance from the source vertex to all other vertexMap in the graph. If `getMin` is set to
250
- * `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
251
- * @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertexMap from the source
252
- * vertex.
253
- * @returns The function `bellmanFord` returns an object with the following properties:
204
+ * Dijkstra without heap (array-based selection).
205
+ * @param src - Source vertex or key.
206
+ * @param dest - Optional destination for early stop.
207
+ * @param getMinDist - If `true`, compute global minimum distance.
208
+ * @param genPaths - If `true`, also generate path arrays.
209
+ * @returns Result bag or `undefined` if source missing.
210
+ * @remarks Time O(V^2 + E), Space O(V + E)
211
+ */
212
+ dijkstraWithoutHeap(src: VO | VertexKey, dest?: VO | VertexKey | undefined, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<VO> | undefined;
213
+ dijkstra(src: VO | VertexKey, dest?: VO | VertexKey | undefined, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<VO> | undefined;
214
+ /**
215
+ * Bellman-Ford single-source shortest paths with option to scan negative cycles.
216
+ * @param src - Source vertex or key.
217
+ * @param scanNegativeCycle - If `true`, also detect negative cycles.
218
+ * @param getMin - If `true`, compute global minimum distance.
219
+ * @param genPath - If `true`, generate path arrays via predecessor map.
220
+ * @returns Result bag including distances, predecessors, and optional cycle flag.
221
+ * @remarks Time O(V * E), Space O(V + E)
254
222
  */
255
223
  bellmanFord(src: VO | VertexKey, scanNegativeCycle?: boolean, getMin?: boolean, genPath?: boolean): {
256
224
  hasNegativeCycle: boolean | undefined;
@@ -261,75 +229,112 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
261
229
  minPath: VO[];
262
230
  };
263
231
  /**
264
- * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
265
- */
266
- /**
267
- * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
268
- * Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
269
- */
270
- /**
271
- * BellmanFord time:O(VE) space:O(VO)
272
- * one to rest pairs
273
- * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edgeMap for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edgeMap, the Bellman-Ford algorithm is more flexible in some scenarios.
274
- * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
275
- */
276
- /**
277
- * Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
278
- * Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
279
- *
280
- * Not support graph with negative weight cycle
281
- * all pairs
282
- * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edgeMap, and it can simultaneously compute shortest paths between any two nodes.
283
- * The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertexMap in a
284
- * graph.
285
- * @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
286
- * property is a 2D array of numbers representing the shortest path costs between vertexMap in a graph. The
287
- * `predecessor` property is a 2D array of vertexMap (or `undefined`) representing the predecessor vertexMap in the shortest
288
- * path between vertexMap in the
232
+ * Floyd–Warshall all-pairs shortest paths.
233
+ * @returns `{ costs, predecessor }` matrices.
234
+ * @remarks Time O(V^3), Space O(V^2)
289
235
  */
290
236
  floydWarshall(): {
291
237
  costs: number[][];
292
238
  predecessor: (VO | undefined)[][];
293
239
  };
294
240
  /**
295
- * O(V+E+C)
296
- * O(V+C)
241
+ * Enumerate simple cycles (may be expensive).
242
+ * @param isInclude2Cycle - If `true`, include 2-cycles when graph semantics allow.
243
+ * @returns Array of cycles (each as array of vertex keys).
244
+ * @remarks Time exponential in worst-case, Space O(V + E)
297
245
  */
298
246
  getCycles(isInclude2Cycle?: boolean): VertexKey[][];
299
247
  /**
300
- * Time Complexity: O(n)
301
- * Space Complexity: O(n)
302
- *
303
- * The `filter` function iterates over key-value pairs in a data structure and returns an array of
304
- * pairs that satisfy a given predicate.
305
- * @param predicate - The `predicate` parameter is a callback function that takes four arguments:
306
- * `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included
307
- * in the filtered array. The callback function should return `true` if the element should be
308
- * included, and `
309
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
310
- * specify the value of `this` within the `predicate` function. It is used when you want to bind a
311
- * specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
312
- * @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]`
313
- * that satisfy the given predicate function.
314
- */
315
- filter(predicate: EntryCallback<VertexKey, V | undefined, boolean>, thisArg?: any): [VertexKey, V | undefined][];
316
- /**
317
- * Time Complexity: O(n)
318
- * Space Complexity: O(n)
319
- *
320
- * The `map` function iterates over the elements of a collection and applies a callback function to
321
- * each element, returning an array of the results.
322
- * @param callback - The callback parameter is a function that will be called for each element in the
323
- * map. It takes four arguments:
324
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
325
- * specify the value of `this` within the callback function. If `thisArg` is provided, it will be
326
- * used as the `this` value when calling the callback function. If `thisArg` is not provided, `
327
- * @returns The `map` function is returning an array of type `T[]`.
248
+ * Induced-subgraph filter: keep vertices where `predicate(key, value)` is true,
249
+ * and only keep edges whose endpoints both survive.
250
+ * @param predicate - `(key, value, index, self) => boolean`.
251
+ * @param thisArg - Optional `this` for callback.
252
+ * @returns A new graph of the same concrete class (`this` type).
253
+ * @remarks Time O(V + E), Space O(V + E)
254
+ */
255
+ filter(predicate: EntryCallback<VertexKey, V | undefined, boolean>, thisArg?: any): this;
256
+ /**
257
+ * Preserve the old behavior: return filtered entries as an array.
258
+ * @remarks Time O(V), Space O(V)
328
259
  */
260
+ filterEntries(predicate: EntryCallback<VertexKey, V | undefined, boolean>, thisArg?: any): [VertexKey, V | undefined][];
329
261
  map<T>(callback: EntryCallback<VertexKey, V | undefined, T>, thisArg?: any): T[];
262
+ /**
263
+ * Create a deep clone of the graph with the same species.
264
+ * @remarks Time O(V + E), Space O(V + E)
265
+ */
266
+ /**
267
+ * Create a deep clone of the graph with the same species.
268
+ * @returns A new graph of the same concrete class (`this` type).
269
+ * @remarks Time O(V + E), Space O(V + E)
270
+ */
271
+ clone(): this;
272
+ /**
273
+ * Internal iterator over `[key, value]` entries in insertion order.
274
+ * @returns Iterator of `[VertexKey, V | undefined]`.
275
+ * @remarks Time O(V), Space O(1)
276
+ */
330
277
  protected _getIterator(): IterableIterator<[VertexKey, V | undefined]>;
278
+ /**
279
+ * Capture configuration needed to reproduce the current graph.
280
+ * Currently the graph has no runtime options, so we return an empty object.
281
+ */
282
+ /**
283
+ * Capture configuration needed to reproduce the current graph.
284
+ * @returns Options bag (opaque to callers).
285
+ * @remarks Time O(1), Space O(1)
286
+ */
287
+ protected _snapshotOptions(): Record<string, unknown>;
288
+ /**
289
+ * Create an empty graph instance of the same concrete species (Directed/Undirected/etc).
290
+ * @remarks Time O(1), Space O(1)
291
+ */
292
+ /**
293
+ * Create an empty graph instance of the same concrete species.
294
+ * @param _options - Snapshot options from `_snapshotOptions()`.
295
+ * @returns A new empty graph instance of `this` type.
296
+ * @remarks Time O(1), Space O(1)
297
+ */
298
+ protected _createInstance(_options?: Partial<Record<string, unknown>>): this;
299
+ /**
300
+ * Create a same-species graph populated with the given entries.
301
+ * Also preserves edges between kept vertices from the source graph.
302
+ * @remarks Time O(V + E), Space O(V + E)
303
+ */
304
+ /**
305
+ * Create a same-species graph populated with entries; preserves edges among kept vertices.
306
+ * @param iter - Optional entries to seed the new graph.
307
+ * @param options - Snapshot options.
308
+ * @returns A new graph of `this` type.
309
+ * @remarks Time O(V + E), Space O(V + E)
310
+ */
311
+ protected _createLike(iter?: Iterable<[VertexKey, V | undefined]>, options?: Partial<Record<string, unknown>>): this;
312
+ /**
313
+ * Internal hook to attach an edge into adjacency structures.
314
+ * @param edge - Edge instance.
315
+ * @returns `true` if inserted; otherwise `false`.
316
+ * @remarks Time O(1) avg, Space O(1)
317
+ */
331
318
  protected abstract _addEdge(edge: EO): boolean;
319
+ /**
320
+ * Insert a pre-built vertex into the graph.
321
+ * @param newVertex - Concrete vertex instance.
322
+ * @returns `true` if inserted; `false` if key already exists.
323
+ * @remarks Time O(1) avg, Space O(1)
324
+ */
332
325
  protected _addVertex(newVertex: VO): boolean;
326
+ /**
327
+ * Resolve a vertex key or instance to the concrete vertex instance.
328
+ * @param vertexOrKey - Vertex key or existing vertex.
329
+ * @returns Vertex instance or `undefined`.
330
+ * @remarks Time O(1), Space O(1)
331
+ */
333
332
  protected _getVertex(vertexOrKey: VertexKey | VO): VO | undefined;
333
+ /**
334
+ * Resolve a vertex key from a key or vertex instance.
335
+ * @param vertexOrKey - Vertex key or existing vertex.
336
+ * @returns The vertex key.
337
+ * @remarks Time O(1), Space O(1)
338
+ */
334
339
  protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey;
335
340
  }