data-structure-typed 1.19.5 → 1.19.6
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/README.md +7 -11
- package/dist/data-structures/graph/abstract-graph.d.ts +20 -20
- package/dist/data-structures/graph/abstract-graph.js +19 -19
- package/dist/data-structures/heap/heap.d.ts +13 -21
- package/dist/data-structures/heap/heap.js +18 -26
- package/dist/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +1 -2
- package/jest.config.js +1 -1
- package/package.json +4 -3
- package/src/data-structures/graph/abstract-graph.ts +27 -26
- package/src/data-structures/heap/heap.ts +30 -30
- package/src/data-structures/priority-queue/priority-queue.ts +2 -2
package/README.md
CHANGED
|
@@ -1,31 +1,27 @@
|
|
|
1
1
|
# What
|
|
2
2
|
|
|
3
3
|
## Brief
|
|
4
|
-
Javascript & TypeScript Data Structure
|
|
4
|
+
Javascript & TypeScript Data Structure collections.
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
## Algorithms
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
DFS, DFSIterative, BFS, morris, Bellman-Ford Algorithm, Dijkstra's Algorithm, Floyd-Warshall Algorithm, Tarjan's Algorithm
|
|
8
|
+
DFS, DFSIterative, BFS, morris, Bellman-Ford Algorithm, Dijkstra's Algorithm, Floyd-Warshall Algorithm, Tarjan's Algorithm. Listed only a few out, you can discover more in API docs
|
|
11
9
|
|
|
12
10
|
## Code design
|
|
13
11
|
By strictly adhering to object-oriented design (BinaryTree -> BST -> AVLTree -> TreeMultiset), you can seamlessly inherit the existing data structures to implement the customized ones you need. Object-oriented design stands as the optimal approach to data structure design.
|
|
14
12
|
|
|
15
13
|
# How
|
|
16
|
-
|
|
14
|
+
### npm
|
|
15
|
+
```bash
|
|
16
|
+
npm install data-structure-typed
|
|
17
|
+
```
|
|
17
18
|
## install
|
|
18
19
|
### yarn
|
|
19
|
-
|
|
20
20
|
```bash
|
|
21
21
|
yarn add data-structure-typed
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
-
### npm
|
|
25
24
|
|
|
26
|
-
```bash
|
|
27
|
-
npm install data-structure-typed
|
|
28
|
-
```
|
|
29
25
|
|
|
30
26
|
### Binary Search Tree (BST) snippet
|
|
31
27
|
|
|
@@ -72,7 +72,6 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
72
72
|
abstract edgesOf(vertexOrId: V | VertexId): E[];
|
|
73
73
|
abstract getNeighbors(vertexOrId: V | VertexId): V[];
|
|
74
74
|
abstract getEndsOfEdge(edge: E): [V, V] | null;
|
|
75
|
-
protected abstract _addEdgeOnly(edge: E): boolean;
|
|
76
75
|
/**
|
|
77
76
|
* The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
|
|
78
77
|
* @param {VertexId} vertexId - The `vertexId` parameter is the identifier of the vertex that you want to retrieve from
|
|
@@ -220,25 +219,6 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
220
219
|
* @returns The function `dijkstra` returns an object of type `DijkstraResult<V>`.
|
|
221
220
|
*/
|
|
222
221
|
dijkstra(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<V>;
|
|
223
|
-
/**
|
|
224
|
-
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
225
|
-
* /
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
229
|
-
* 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.
|
|
230
|
-
*/
|
|
231
|
-
/**
|
|
232
|
-
* BellmanFord time:O(VE) space:O(V)
|
|
233
|
-
* one to rest pairs
|
|
234
|
-
* 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 edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
235
|
-
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
236
|
-
*/
|
|
237
|
-
/**
|
|
238
|
-
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
239
|
-
* all pairs
|
|
240
|
-
* 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 edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
241
|
-
*/
|
|
242
222
|
/**
|
|
243
223
|
* BellmanFord time:O(VE) space:O(V)
|
|
244
224
|
* one to rest pairs
|
|
@@ -268,6 +248,25 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
268
248
|
min: number;
|
|
269
249
|
minPath: V[];
|
|
270
250
|
};
|
|
251
|
+
/**
|
|
252
|
+
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
253
|
+
* /
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
257
|
+
* 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.
|
|
258
|
+
*/
|
|
259
|
+
/**
|
|
260
|
+
* BellmanFord time:O(VE) space:O(V)
|
|
261
|
+
* one to rest pairs
|
|
262
|
+
* 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 edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
263
|
+
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
264
|
+
*/
|
|
265
|
+
/**
|
|
266
|
+
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
267
|
+
* all pairs
|
|
268
|
+
* 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 edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
269
|
+
*/
|
|
271
270
|
/**
|
|
272
271
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
273
272
|
* all pairs
|
|
@@ -325,6 +324,7 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
325
324
|
SCCs: Map<number, V[]>;
|
|
326
325
|
cycles: Map<number, V[]>;
|
|
327
326
|
};
|
|
327
|
+
protected abstract _addEdgeOnly(edge: E): boolean;
|
|
328
328
|
protected _addVertexOnly(newVertex: V): boolean;
|
|
329
329
|
protected _getVertex(vertexOrId: VertexId | V): V | null;
|
|
330
330
|
protected _getVertexId(vertexOrId: V | VertexId): VertexId;
|
|
@@ -600,25 +600,6 @@ class AbstractGraph {
|
|
|
600
600
|
}
|
|
601
601
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
602
602
|
}
|
|
603
|
-
/**
|
|
604
|
-
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
605
|
-
* /
|
|
606
|
-
|
|
607
|
-
/**
|
|
608
|
-
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
609
|
-
* 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.
|
|
610
|
-
*/
|
|
611
|
-
/**
|
|
612
|
-
* BellmanFord time:O(VE) space:O(V)
|
|
613
|
-
* one to rest pairs
|
|
614
|
-
* 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 edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
615
|
-
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
616
|
-
*/
|
|
617
|
-
/**
|
|
618
|
-
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
619
|
-
* all pairs
|
|
620
|
-
* 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 edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
621
|
-
*/
|
|
622
603
|
/**
|
|
623
604
|
* BellmanFord time:O(VE) space:O(V)
|
|
624
605
|
* one to rest pairs
|
|
@@ -725,6 +706,25 @@ class AbstractGraph {
|
|
|
725
706
|
}
|
|
726
707
|
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
727
708
|
}
|
|
709
|
+
/**
|
|
710
|
+
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
711
|
+
* /
|
|
712
|
+
|
|
713
|
+
/**
|
|
714
|
+
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
715
|
+
* 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.
|
|
716
|
+
*/
|
|
717
|
+
/**
|
|
718
|
+
* BellmanFord time:O(VE) space:O(V)
|
|
719
|
+
* one to rest pairs
|
|
720
|
+
* 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 edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
721
|
+
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
722
|
+
*/
|
|
723
|
+
/**
|
|
724
|
+
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
725
|
+
* all pairs
|
|
726
|
+
* 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 edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
727
|
+
*/
|
|
728
728
|
/**
|
|
729
729
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
730
730
|
* all pairs
|
|
@@ -44,16 +44,12 @@ export declare abstract class Heap<T = number> {
|
|
|
44
44
|
* @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
|
|
45
45
|
*/
|
|
46
46
|
isEmpty(): boolean;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
* The `peekLast` function returns the last item in the heap.
|
|
54
|
-
* @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
|
|
55
|
-
*/
|
|
56
|
-
peekLast(): HeapItem<T> | null;
|
|
47
|
+
peek(isItem?: undefined): T | undefined;
|
|
48
|
+
peek(isItem: false): T | undefined;
|
|
49
|
+
peek(isItem: true): HeapItem<T> | null;
|
|
50
|
+
peekLast(isItem?: undefined): T | undefined;
|
|
51
|
+
peekLast(isItem: false): T | undefined;
|
|
52
|
+
peekLast(isItem: true): HeapItem<T> | null;
|
|
57
53
|
/**
|
|
58
54
|
* The `add` function adds an val to a priority queue with an optional priority value.
|
|
59
55
|
* @param {T} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
|
|
@@ -64,23 +60,19 @@ export declare abstract class Heap<T = number> {
|
|
|
64
60
|
* @returns The `add` method returns the instance of the `Heap` class.
|
|
65
61
|
* @throws {Error} if priority is not a valid number
|
|
66
62
|
*/
|
|
67
|
-
add(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
*/
|
|
72
|
-
poll(): HeapItem<T> | null;
|
|
63
|
+
add(priority: number, val?: T): Heap<T>;
|
|
64
|
+
poll(isItem?: undefined): T | undefined;
|
|
65
|
+
poll(isItem: false): T | undefined;
|
|
66
|
+
poll(isItem: true): HeapItem<T> | null;
|
|
73
67
|
/**
|
|
74
68
|
* The function checks if a given node or value exists in the priority queue.
|
|
75
69
|
* @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
|
|
76
70
|
* @returns a boolean value.
|
|
77
71
|
*/
|
|
78
72
|
has(node: T | HeapItem<T>): boolean;
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
*/
|
|
83
|
-
toArray(): HeapItem<T>[];
|
|
73
|
+
toArray(isItem?: undefined): (T | undefined)[];
|
|
74
|
+
toArray(isItem: false): (T | undefined)[];
|
|
75
|
+
toArray(isItem: true): (HeapItem<T> | null)[];
|
|
84
76
|
/**
|
|
85
77
|
* The clear function clears the priority queue.
|
|
86
78
|
*/
|
|
@@ -69,15 +69,19 @@ class Heap {
|
|
|
69
69
|
* The `peek` function returns the top item in the priority queue without removing it.
|
|
70
70
|
* @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
|
|
71
71
|
*/
|
|
72
|
-
peek() {
|
|
73
|
-
|
|
72
|
+
peek(isItem) {
|
|
73
|
+
isItem = isItem !== null && isItem !== void 0 ? isItem : false;
|
|
74
|
+
const peeked = this._pq.peek();
|
|
75
|
+
return isItem ? peeked : peeked === null || peeked === void 0 ? void 0 : peeked.val;
|
|
74
76
|
}
|
|
75
77
|
/**
|
|
76
78
|
* The `peekLast` function returns the last item in the heap.
|
|
77
79
|
* @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
|
|
78
80
|
*/
|
|
79
|
-
peekLast() {
|
|
80
|
-
|
|
81
|
+
peekLast(isItem) {
|
|
82
|
+
isItem = isItem !== null && isItem !== void 0 ? isItem : false;
|
|
83
|
+
const leafItem = this._pq.leaf();
|
|
84
|
+
return isItem ? leafItem : leafItem === null || leafItem === void 0 ? void 0 : leafItem.val;
|
|
81
85
|
}
|
|
82
86
|
/**
|
|
83
87
|
* The `add` function adds an val to a priority queue with an optional priority value.
|
|
@@ -89,36 +93,22 @@ class Heap {
|
|
|
89
93
|
* @returns The `add` method returns the instance of the `Heap` class.
|
|
90
94
|
* @throws {Error} if priority is not a valid number
|
|
91
95
|
*/
|
|
92
|
-
add(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
if (priority === undefined) {
|
|
98
|
-
throw new Error('.add expects a numeric priority');
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
if (priority && Number.isNaN(+priority)) {
|
|
102
|
-
throw new Error('.add expects a numeric priority');
|
|
103
|
-
}
|
|
104
|
-
if (Number.isNaN(+priority) && Number.isNaN(this._priorityExtractor(val))) {
|
|
105
|
-
throw new Error('.add expects a numeric priority '
|
|
106
|
-
+ 'or a constructor callback that returns a number');
|
|
107
|
-
}
|
|
108
|
-
const _priority = !Number.isNaN(+priority) ? priority : this._priorityExtractor(val);
|
|
109
|
-
this._pq.add(new HeapItem(_priority, val));
|
|
96
|
+
add(priority, val) {
|
|
97
|
+
val = (val === undefined) ? priority : val;
|
|
98
|
+
this._pq.add(new HeapItem(priority, val));
|
|
110
99
|
return this;
|
|
111
100
|
}
|
|
112
101
|
/**
|
|
113
102
|
* The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an val with the highest priority in the queue
|
|
114
103
|
* @returns either a HeapItem<T> object or null.
|
|
115
104
|
*/
|
|
116
|
-
poll() {
|
|
105
|
+
poll(isItem) {
|
|
106
|
+
isItem = isItem !== null && isItem !== void 0 ? isItem : false;
|
|
117
107
|
const top = this._pq.poll();
|
|
118
108
|
if (!top) {
|
|
119
109
|
return null;
|
|
120
110
|
}
|
|
121
|
-
return top;
|
|
111
|
+
return isItem ? top : top.val;
|
|
122
112
|
}
|
|
123
113
|
/**
|
|
124
114
|
* The function checks if a given node or value exists in the priority queue.
|
|
@@ -139,8 +129,10 @@ class Heap {
|
|
|
139
129
|
* The `toArray` function returns an array of `HeapItem<T>` objects.
|
|
140
130
|
* @returns An array of HeapItem<T> objects.Returns a sorted list of vals
|
|
141
131
|
*/
|
|
142
|
-
toArray() {
|
|
143
|
-
|
|
132
|
+
toArray(isItem) {
|
|
133
|
+
isItem = isItem !== null && isItem !== void 0 ? isItem : false;
|
|
134
|
+
const itemArray = this._pq.toArray();
|
|
135
|
+
return isItem ? itemArray : itemArray.map(item => item.val);
|
|
144
136
|
}
|
|
145
137
|
/**
|
|
146
138
|
* The clear function clears the priority queue.
|
|
@@ -95,7 +95,7 @@ export declare class PriorityQueue<T = number> {
|
|
|
95
95
|
*/
|
|
96
96
|
isValid(): boolean;
|
|
97
97
|
/**
|
|
98
|
-
*
|
|
98
|
+
* O(n log n), In scenarios with smaller data sizes, heap sort is generally expected to be slower than QuickSort or MergeSort.
|
|
99
99
|
*/
|
|
100
100
|
/**
|
|
101
101
|
* The function sorts the elements in a data structure and returns them in an array.
|
|
@@ -157,7 +157,7 @@ class PriorityQueue {
|
|
|
157
157
|
return true;
|
|
158
158
|
}
|
|
159
159
|
/**
|
|
160
|
-
*
|
|
160
|
+
* O(n log n), In scenarios with smaller data sizes, heap sort is generally expected to be slower than QuickSort or MergeSort.
|
|
161
161
|
*/
|
|
162
162
|
/**
|
|
163
163
|
* The function sorts the elements in a data structure and returns them in an array.
|
|
@@ -165,7 +165,6 @@ class PriorityQueue {
|
|
|
165
165
|
* @returns The `sort()` method is returning an array of type `T[]`.
|
|
166
166
|
*/
|
|
167
167
|
sort() {
|
|
168
|
-
// TODO Plan to support sorting of duplicate elements.
|
|
169
168
|
const visitedNode = [];
|
|
170
169
|
while (this.size !== 0) {
|
|
171
170
|
const top = this.poll();
|
package/jest.config.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.19.
|
|
3
|
+
"version": "1.19.6",
|
|
4
4
|
"description": "Javascript & TypeScript Data Structure Library, meticulously crafted to empower developers with a versatile set of essential data structures. Our library includes a wide range of data structures, such as Binary Tree, AVL Tree, Binary Search Tree (BST), Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Singly Linked List, Hash, CoordinateSet, CoordinateMap, Heap, Doubly Linked List, Priority Queue, Max Priority Queue, Min Priority Queue, Queue, ObjectDeque, ArrayDeque, Stack, and Trie. Each data structure is thoughtfully designed and implemented using TypeScript to provide efficient, reliable, and easy-to-use solutions for your programming needs. Whether you're optimizing algorithms, managing data, or enhancing performance, our TypeScript Data Structure Library is your go-to resource. Elevate your coding experience with these fundamental building blocks for software development.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -65,8 +65,9 @@
|
|
|
65
65
|
"typescript": "^4.9.5"
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"avl-tree-typed": "^1.19.
|
|
69
|
-
"bst-typed": "^1.19.
|
|
68
|
+
"avl-tree-typed": "^1.19.6",
|
|
69
|
+
"bst-typed": "^1.19.6",
|
|
70
|
+
"heap-typed": "^1.19.6",
|
|
70
71
|
"zod": "^3.22.2"
|
|
71
72
|
}
|
|
72
73
|
}
|
|
@@ -129,6 +129,7 @@ export abstract class AbstractGraph<V extends AbstractVertex<any>, E extends Abs
|
|
|
129
129
|
abstract createEdge(srcOrV1: VertexId | string, destOrV2: VertexId | string, weight?: number, val?: E): E;
|
|
130
130
|
|
|
131
131
|
abstract removeEdge(edge: E): E | null;
|
|
132
|
+
|
|
132
133
|
abstract getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
|
|
133
134
|
|
|
134
135
|
abstract degreeOf(vertexOrId: V | VertexId): number;
|
|
@@ -141,8 +142,6 @@ export abstract class AbstractGraph<V extends AbstractVertex<any>, E extends Abs
|
|
|
141
142
|
|
|
142
143
|
abstract getEndsOfEdge(edge: E): [V, V] | null;
|
|
143
144
|
|
|
144
|
-
protected abstract _addEdgeOnly(edge: E): boolean;
|
|
145
|
-
|
|
146
145
|
/**
|
|
147
146
|
* The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
|
|
148
147
|
* @param {VertexId} vertexId - The `vertexId` parameter is the identifier of the vertex that you want to retrieve from
|
|
@@ -165,7 +164,9 @@ export abstract class AbstractGraph<V extends AbstractVertex<any>, E extends Abs
|
|
|
165
164
|
}
|
|
166
165
|
|
|
167
166
|
addVertex(vertex: V): boolean
|
|
167
|
+
|
|
168
168
|
addVertex(id: VertexId, val?: V['val']): boolean
|
|
169
|
+
|
|
169
170
|
addVertex(idOrVertex: VertexId | V, val?: V['val']): boolean {
|
|
170
171
|
if (idOrVertex instanceof AbstractVertex) {
|
|
171
172
|
return this._addVertexOnly(idOrVertex);
|
|
@@ -256,7 +257,6 @@ export abstract class AbstractGraph<V extends AbstractVertex<any>, E extends Abs
|
|
|
256
257
|
}
|
|
257
258
|
}
|
|
258
259
|
|
|
259
|
-
|
|
260
260
|
/**
|
|
261
261
|
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
|
|
262
262
|
* @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
|
|
@@ -691,29 +691,6 @@ export abstract class AbstractGraph<V extends AbstractVertex<any>, E extends Abs
|
|
|
691
691
|
return {distMap, preMap, seen, paths, minDist, minPath};
|
|
692
692
|
}
|
|
693
693
|
|
|
694
|
-
/**
|
|
695
|
-
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
696
|
-
* /
|
|
697
|
-
|
|
698
|
-
/**
|
|
699
|
-
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
700
|
-
* 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.
|
|
701
|
-
*/
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
/**
|
|
705
|
-
* BellmanFord time:O(VE) space:O(V)
|
|
706
|
-
* one to rest pairs
|
|
707
|
-
* 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 edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
708
|
-
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
709
|
-
*/
|
|
710
|
-
|
|
711
|
-
/**
|
|
712
|
-
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
713
|
-
* all pairs
|
|
714
|
-
* 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 edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
715
|
-
*/
|
|
716
|
-
|
|
717
694
|
/**
|
|
718
695
|
* BellmanFord time:O(VE) space:O(V)
|
|
719
696
|
* one to rest pairs
|
|
@@ -823,6 +800,29 @@ export abstract class AbstractGraph<V extends AbstractVertex<any>, E extends Abs
|
|
|
823
800
|
return {hasNegativeCycle, distMap, preMap, paths, min, minPath};
|
|
824
801
|
}
|
|
825
802
|
|
|
803
|
+
/**
|
|
804
|
+
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
805
|
+
* /
|
|
806
|
+
|
|
807
|
+
/**
|
|
808
|
+
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
809
|
+
* 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.
|
|
810
|
+
*/
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
/**
|
|
814
|
+
* BellmanFord time:O(VE) space:O(V)
|
|
815
|
+
* one to rest pairs
|
|
816
|
+
* 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 edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
817
|
+
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
818
|
+
*/
|
|
819
|
+
|
|
820
|
+
/**
|
|
821
|
+
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
822
|
+
* all pairs
|
|
823
|
+
* 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 edges, and it can simultaneously compute shortest paths between any two nodes.
|
|
824
|
+
*/
|
|
825
|
+
|
|
826
826
|
/**
|
|
827
827
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
828
828
|
* all pairs
|
|
@@ -1007,6 +1007,7 @@ export abstract class AbstractGraph<V extends AbstractVertex<any>, E extends Abs
|
|
|
1007
1007
|
return {dfnMap, lowMap, bridges, articulationPoints, SCCs, cycles};
|
|
1008
1008
|
}
|
|
1009
1009
|
|
|
1010
|
+
protected abstract _addEdgeOnly(edge: E): boolean;
|
|
1010
1011
|
|
|
1011
1012
|
protected _addVertexOnly(newVertex: V): boolean {
|
|
1012
1013
|
if (this.hasVertex(newVertex)) {
|
|
@@ -88,20 +88,30 @@ export abstract class Heap<T = number> {
|
|
|
88
88
|
return this._pq.size < 1;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
peek(isItem?: undefined): T | undefined;
|
|
92
|
+
peek(isItem: false): T | undefined;
|
|
93
|
+
peek(isItem: true): HeapItem<T> | null;
|
|
91
94
|
/**
|
|
92
95
|
* The `peek` function returns the top item in the priority queue without removing it.
|
|
93
96
|
* @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
|
|
94
97
|
*/
|
|
95
|
-
peek(): HeapItem<T> | null {
|
|
96
|
-
|
|
98
|
+
peek(isItem?: boolean): HeapItem<T> | null | T | undefined {
|
|
99
|
+
isItem = isItem ?? false;
|
|
100
|
+
const peeked = this._pq.peek();
|
|
101
|
+
return isItem ? peeked : peeked?.val;
|
|
97
102
|
}
|
|
98
103
|
|
|
104
|
+
peekLast(isItem?: undefined): T | undefined;
|
|
105
|
+
peekLast(isItem: false): T | undefined;
|
|
106
|
+
peekLast(isItem: true): HeapItem<T> | null;
|
|
99
107
|
/**
|
|
100
108
|
* The `peekLast` function returns the last item in the heap.
|
|
101
109
|
* @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
|
|
102
110
|
*/
|
|
103
|
-
peekLast(): HeapItem<T> | null {
|
|
104
|
-
|
|
111
|
+
peekLast(isItem?: boolean): HeapItem<T> | null | T | undefined {
|
|
112
|
+
isItem = isItem ?? false;
|
|
113
|
+
const leafItem = this._pq.leaf();
|
|
114
|
+
return isItem ? leafItem : leafItem?.val;
|
|
105
115
|
}
|
|
106
116
|
|
|
107
117
|
/**
|
|
@@ -114,41 +124,26 @@ export abstract class Heap<T = number> {
|
|
|
114
124
|
* @returns The `add` method returns the instance of the `Heap` class.
|
|
115
125
|
* @throws {Error} if priority is not a valid number
|
|
116
126
|
*/
|
|
117
|
-
add(
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
} else {
|
|
121
|
-
if (priority === undefined) {
|
|
122
|
-
throw new Error('.add expects a numeric priority');
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (priority && Number.isNaN(+priority)) {
|
|
127
|
-
throw new Error('.add expects a numeric priority');
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (Number.isNaN(+priority) && Number.isNaN(this._priorityExtractor(val))) {
|
|
131
|
-
throw new Error(
|
|
132
|
-
'.add expects a numeric priority '
|
|
133
|
-
+ 'or a constructor callback that returns a number'
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const _priority = !Number.isNaN(+priority) ? priority : this._priorityExtractor(val);
|
|
138
|
-
this._pq.add(new HeapItem<T>(_priority, val));
|
|
127
|
+
add(priority: number, val?: T,): Heap<T> {
|
|
128
|
+
val = (val === undefined) ? priority as T : val;
|
|
129
|
+
this._pq.add(new HeapItem<T>(priority, val));
|
|
139
130
|
return this;
|
|
140
131
|
}
|
|
141
132
|
|
|
133
|
+
poll(isItem?: undefined): T | undefined;
|
|
134
|
+
poll(isItem: false): T | undefined;
|
|
135
|
+
poll(isItem: true): HeapItem<T> | null;
|
|
142
136
|
/**
|
|
143
137
|
* The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an val with the highest priority in the queue
|
|
144
138
|
* @returns either a HeapItem<T> object or null.
|
|
145
139
|
*/
|
|
146
|
-
poll(): HeapItem<T> | null {
|
|
140
|
+
poll(isItem?: boolean): HeapItem<T> | null | T | undefined {
|
|
141
|
+
isItem = isItem ?? false;
|
|
147
142
|
const top = this._pq.poll();
|
|
148
143
|
if (!top) {
|
|
149
144
|
return null;
|
|
150
145
|
}
|
|
151
|
-
return top;
|
|
146
|
+
return isItem ? top : top.val;
|
|
152
147
|
}
|
|
153
148
|
|
|
154
149
|
/**
|
|
@@ -166,12 +161,17 @@ export abstract class Heap<T = number> {
|
|
|
166
161
|
}
|
|
167
162
|
}
|
|
168
163
|
|
|
164
|
+
toArray(isItem?: undefined): (T | undefined)[];
|
|
165
|
+
toArray(isItem: false): (T | undefined)[];
|
|
166
|
+
toArray(isItem: true): (HeapItem<T> | null)[];
|
|
169
167
|
/**
|
|
170
168
|
* The `toArray` function returns an array of `HeapItem<T>` objects.
|
|
171
169
|
* @returns An array of HeapItem<T> objects.Returns a sorted list of vals
|
|
172
170
|
*/
|
|
173
|
-
toArray(): HeapItem<T>[] {
|
|
174
|
-
|
|
171
|
+
toArray(isItem?: boolean): (HeapItem<T> | null | T | undefined)[] {
|
|
172
|
+
isItem = isItem ?? false;
|
|
173
|
+
const itemArray = this._pq.toArray();
|
|
174
|
+
return isItem ? itemArray : itemArray.map(item => item.val);
|
|
175
175
|
}
|
|
176
176
|
|
|
177
177
|
/**
|
|
@@ -174,15 +174,15 @@ export class PriorityQueue<T = number> {
|
|
|
174
174
|
}
|
|
175
175
|
|
|
176
176
|
/**
|
|
177
|
-
*
|
|
177
|
+
* O(n log n), In scenarios with smaller data sizes, heap sort is generally expected to be slower than QuickSort or MergeSort.
|
|
178
178
|
*/
|
|
179
|
+
|
|
179
180
|
/**
|
|
180
181
|
* The function sorts the elements in a data structure and returns them in an array.
|
|
181
182
|
* Plan to support sorting of duplicate elements.
|
|
182
183
|
* @returns The `sort()` method is returning an array of type `T[]`.
|
|
183
184
|
*/
|
|
184
185
|
sort(): T[] {
|
|
185
|
-
// TODO Plan to support sorting of duplicate elements.
|
|
186
186
|
const visitedNode: T[] = [];
|
|
187
187
|
while (this.size !== 0) {
|
|
188
188
|
const top = this.poll();
|