deque-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.
- package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
- package/dist/data-structures/base/iterable-element-base.js +149 -107
- package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/data-structures/base/linear-base.d.ts +250 -192
- package/dist/data-structures/base/linear-base.js +137 -274
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/data-structures/binary-tree/avl-tree.js +208 -195
- package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/data-structures/binary-tree/binary-tree.js +612 -879
- package/dist/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/data-structures/binary-tree/bst.js +505 -481
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
- package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/data-structures/binary-tree/tree-counter.js +172 -203
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/data-structures/graph/abstract-graph.js +267 -237
- package/dist/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/data-structures/graph/directed-graph.js +146 -233
- package/dist/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/data-structures/graph/map-graph.js +56 -59
- package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/data-structures/graph/undirected-graph.js +129 -149
- package/dist/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/data-structures/hash/hash-map.js +270 -457
- package/dist/data-structures/heap/heap.d.ts +214 -289
- package/dist/data-structures/heap/heap.js +340 -349
- package/dist/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/data-structures/heap/max-heap.js +11 -66
- package/dist/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/data-structures/heap/min-heap.js +11 -66
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/data-structures/queue/deque.d.ts +227 -254
- package/dist/data-structures/queue/deque.js +309 -348
- package/dist/data-structures/queue/queue.d.ts +180 -201
- package/dist/data-structures/queue/queue.js +265 -248
- package/dist/data-structures/stack/stack.d.ts +124 -102
- package/dist/data-structures/stack/stack.js +181 -125
- package/dist/data-structures/trie/trie.d.ts +164 -165
- package/dist/data-structures/trie/trie.js +189 -172
- package/dist/interfaces/binary-tree.d.ts +56 -6
- package/dist/interfaces/graph.d.ts +16 -0
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/types/utils/utils.d.ts +6 -6
- package/dist/utils/utils.d.ts +110 -49
- package/dist/utils/utils.js +148 -73
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +238 -115
- package/src/data-structures/base/iterable-entry-base.ts +96 -120
- package/src/data-structures/base/linear-base.ts +271 -277
- package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
- package/src/data-structures/binary-tree/avl-tree.ts +239 -206
- package/src/data-structures/binary-tree/binary-tree.ts +681 -905
- package/src/data-structures/binary-tree/bst.ts +568 -570
- package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
- package/src/data-structures/binary-tree/tree-counter.ts +199 -218
- package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
- package/src/data-structures/graph/abstract-graph.ts +339 -264
- package/src/data-structures/graph/directed-graph.ts +146 -236
- package/src/data-structures/graph/map-graph.ts +63 -60
- package/src/data-structures/graph/undirected-graph.ts +129 -152
- package/src/data-structures/hash/hash-map.ts +274 -496
- package/src/data-structures/heap/heap.ts +389 -402
- package/src/data-structures/heap/max-heap.ts +12 -76
- package/src/data-structures/heap/min-heap.ts +13 -76
- package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
- package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
- package/src/data-structures/priority-queue/priority-queue.ts +3 -92
- package/src/data-structures/queue/deque.ts +381 -357
- package/src/data-structures/queue/queue.ts +310 -264
- package/src/data-structures/stack/stack.ts +217 -131
- package/src/data-structures/trie/trie.ts +240 -175
- package/src/interfaces/binary-tree.ts +240 -6
- package/src/interfaces/graph.ts +37 -0
- package/src/types/data-structures/base/base.ts +5 -5
- package/src/types/data-structures/graph/abstract-graph.ts +5 -0
- package/src/types/utils/utils.ts +9 -5
- 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
|
-
|
|
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
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* @param
|
|
49
|
-
* @
|
|
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
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* @param
|
|
56
|
-
* @param
|
|
57
|
-
* @param
|
|
58
|
-
* @
|
|
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
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
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
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
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
|
-
*
|
|
94
|
-
*
|
|
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
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
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
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
* @
|
|
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
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* @
|
|
128
|
-
*
|
|
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
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
* @
|
|
143
|
-
*
|
|
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
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
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
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
* @
|
|
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
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
* @
|
|
183
|
-
*
|
|
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
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
* @
|
|
203
|
-
*
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
* @param
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
* @returns
|
|
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
|
-
*
|
|
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
|
-
*
|
|
296
|
-
*
|
|
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
|
-
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
305
|
-
* @
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
*
|
|
310
|
-
*
|
|
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
|
}
|