data-structure-typed 1.12.21 → 1.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +278 -179
- package/dist/data-structures/binary-tree/binary-tree.d.ts +46 -1
- package/dist/data-structures/binary-tree/binary-tree.js +67 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +29 -0
- package/dist/data-structures/binary-tree/segment-tree.js +45 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +40 -5
- package/dist/data-structures/graph/abstract-graph.js +47 -7
- package/dist/data-structures/graph/directed-graph.d.ts +8 -0
- package/dist/data-structures/graph/directed-graph.js +14 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +10 -0
- package/dist/data-structures/graph/undirected-graph.js +23 -1
- package/dist/data-structures/hash/coordinate-map.d.ts +7 -1
- package/dist/data-structures/hash/coordinate-map.js +16 -0
- package/dist/data-structures/hash/coordinate-set.d.ts +7 -1
- package/dist/data-structures/hash/coordinate-set.js +16 -0
- package/dist/data-structures/heap/heap.d.ts +16 -0
- package/dist/data-structures/heap/heap.js +38 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +30 -7
- package/dist/data-structures/linked-list/doubly-linked-list.js +71 -4
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +262 -328
- package/dist/data-structures/linked-list/singly-linked-list.js +258 -273
- package/dist/data-structures/priority-queue/priority-queue.d.ts +7 -1
- package/dist/data-structures/priority-queue/priority-queue.js +18 -2
- package/dist/data-structures/queue/deque.d.ts +18 -7
- package/dist/data-structures/queue/deque.js +50 -3
- package/dist/data-structures/types/abstract-graph.d.ts +2 -2
- package/dist/utils/types/utils.d.ts +0 -49
- package/dist/utils/types/utils.js +14 -52
- package/dist/utils/utils.d.ts +1 -97
- package/dist/utils/utils.js +197 -546
- package/package.json +4 -3
- package/src/data-structures/binary-tree/aa-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +84 -14
- package/src/data-structures/binary-tree/segment-tree.ts +45 -13
- package/src/data-structures/graph/abstract-graph.ts +58 -15
- package/src/data-structures/graph/directed-graph.ts +14 -5
- package/src/data-structures/graph/undirected-graph.ts +23 -6
- package/src/data-structures/hash/coordinate-map.ts +13 -1
- package/src/data-structures/hash/coordinate-set.ts +13 -1
- package/src/data-structures/heap/heap.ts +31 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +68 -11
- package/src/data-structures/linked-list/singly-linked-list.ts +312 -334
- package/src/data-structures/priority-queue/priority-queue.ts +15 -2
- package/src/data-structures/queue/deque.ts +38 -8
- package/src/data-structures/types/abstract-graph.ts +3 -3
- package/src/utils/types/utils.ts +165 -167
- package/src/utils/utils.ts +209 -480
- package/tests/unit/data-structures/graph/directed-graph.test.ts +431 -8
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.15.1",
|
|
4
4
|
"description": "Explore our comprehensive Javascript Data Structure / 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": {
|
|
7
7
|
"build": "rm -rf dist && npx tsc",
|
|
8
8
|
"test": "jest",
|
|
9
9
|
"build:docs": "typedoc --out docs ./src",
|
|
10
|
-
"deps:check": "dependency-cruiser src"
|
|
10
|
+
"deps:check": "dependency-cruiser src",
|
|
11
|
+
"build:publish": "npm run test && npm run build && npm run build:docs && npm run publish"
|
|
11
12
|
},
|
|
12
13
|
"repository": {
|
|
13
14
|
"type": "git",
|
|
@@ -54,7 +55,7 @@
|
|
|
54
55
|
"jest": "^29.6.2",
|
|
55
56
|
"ts-jest": "^29.1.1",
|
|
56
57
|
"typedoc": "^0.24.8",
|
|
57
|
-
"typescript": "^4.
|
|
58
|
+
"typescript": "^4.9.5"
|
|
58
59
|
},
|
|
59
60
|
"dependencies": {
|
|
60
61
|
"lodash": "^4.17.21"
|
|
@@ -30,38 +30,50 @@ export enum LoopType { iterative = 1, recursive = 2}
|
|
|
30
30
|
|
|
31
31
|
export class BinaryTreeNode<T> {
|
|
32
32
|
|
|
33
|
-
constructor(id: BinaryTreeNodeId, val: T, count?: number) {
|
|
34
|
-
this._id = id;
|
|
35
|
-
this._val = val;
|
|
36
|
-
this._count = count ?? 1;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
33
|
protected _id: BinaryTreeNodeId;
|
|
40
|
-
|
|
41
34
|
get id(): BinaryTreeNodeId {
|
|
42
35
|
return this._id;
|
|
43
36
|
}
|
|
44
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
40
|
+
*/
|
|
41
|
+
getId(): BinaryTreeNodeId {
|
|
42
|
+
return this._id;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
45
|
set id(v: BinaryTreeNodeId) {
|
|
46
46
|
this._id = v;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
protected _val: T;
|
|
50
|
-
|
|
51
50
|
get val(): T {
|
|
52
51
|
return this._val;
|
|
53
52
|
}
|
|
54
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
56
|
+
*/
|
|
57
|
+
getVal(): T {
|
|
58
|
+
return this._val;
|
|
59
|
+
}
|
|
60
|
+
|
|
55
61
|
set val(v: T) {
|
|
56
62
|
this._val = v;
|
|
57
63
|
}
|
|
58
64
|
|
|
59
65
|
protected _left?: BinaryTreeNode<T> | null;
|
|
60
|
-
|
|
61
66
|
get left(): BinaryTreeNode<T> | null | undefined {
|
|
62
67
|
return this._left;
|
|
63
68
|
}
|
|
64
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
72
|
+
*/
|
|
73
|
+
getLeft(): BinaryTreeNode<T> | null | undefined {
|
|
74
|
+
return this._left;
|
|
75
|
+
}
|
|
76
|
+
|
|
65
77
|
set left(v: BinaryTreeNode<T> | null | undefined) {
|
|
66
78
|
if (v) {
|
|
67
79
|
v.parent = this;
|
|
@@ -71,11 +83,17 @@ export class BinaryTreeNode<T> {
|
|
|
71
83
|
}
|
|
72
84
|
|
|
73
85
|
protected _right?: BinaryTreeNode<T> | null;
|
|
74
|
-
|
|
75
86
|
get right(): BinaryTreeNode<T> | null | undefined {
|
|
76
87
|
return this._right;
|
|
77
88
|
}
|
|
78
89
|
|
|
90
|
+
/**
|
|
91
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
92
|
+
*/
|
|
93
|
+
getRight(): BinaryTreeNode<T> | null | undefined {
|
|
94
|
+
return this._right;
|
|
95
|
+
}
|
|
96
|
+
|
|
79
97
|
set right(v: BinaryTreeNode<T> | null | undefined) {
|
|
80
98
|
if (v) {
|
|
81
99
|
v.parent = this;
|
|
@@ -85,45 +103,75 @@ export class BinaryTreeNode<T> {
|
|
|
85
103
|
}
|
|
86
104
|
|
|
87
105
|
protected _parent: BinaryTreeNode<T> | null | undefined;
|
|
88
|
-
|
|
89
106
|
get parent(): BinaryTreeNode<T> | null | undefined {
|
|
90
107
|
return this._parent;
|
|
91
108
|
}
|
|
92
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
112
|
+
*/
|
|
113
|
+
getParent(): BinaryTreeNode<T> | null | undefined {
|
|
114
|
+
return this._parent;
|
|
115
|
+
}
|
|
116
|
+
|
|
93
117
|
set parent(v: BinaryTreeNode<T> | null | undefined) {
|
|
94
118
|
this._parent = v;
|
|
95
119
|
}
|
|
96
120
|
|
|
97
121
|
protected _familyPosition: FamilyPosition = FamilyPosition.root;
|
|
98
|
-
|
|
99
122
|
get familyPosition(): FamilyPosition {
|
|
100
123
|
return this._familyPosition;
|
|
101
124
|
}
|
|
102
125
|
|
|
126
|
+
/**
|
|
127
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
128
|
+
*/
|
|
129
|
+
getFamilyPosition(): FamilyPosition {
|
|
130
|
+
return this._familyPosition;
|
|
131
|
+
}
|
|
132
|
+
|
|
103
133
|
set familyPosition(v: FamilyPosition) {
|
|
104
134
|
this._familyPosition = v;
|
|
105
135
|
}
|
|
106
136
|
|
|
107
137
|
protected _count = 1;
|
|
108
|
-
|
|
109
138
|
get count(): number {
|
|
110
139
|
return this._count;
|
|
111
140
|
}
|
|
112
141
|
|
|
142
|
+
/**
|
|
143
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
144
|
+
*/
|
|
145
|
+
getCount(): number {
|
|
146
|
+
return this._count;
|
|
147
|
+
}
|
|
148
|
+
|
|
113
149
|
set count(v: number) {
|
|
114
150
|
this._count = v;
|
|
115
151
|
}
|
|
116
152
|
|
|
117
153
|
protected _height = 0;
|
|
118
|
-
|
|
119
154
|
get height(): number {
|
|
120
155
|
return this._height;
|
|
121
156
|
}
|
|
122
157
|
|
|
158
|
+
/**
|
|
159
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
160
|
+
*/
|
|
161
|
+
getHeight(): number {
|
|
162
|
+
return this._height;
|
|
163
|
+
}
|
|
164
|
+
|
|
123
165
|
set height(v: number) {
|
|
124
166
|
this._height = v;
|
|
125
167
|
}
|
|
126
168
|
|
|
169
|
+
constructor(id: BinaryTreeNodeId, val: T, count?: number) {
|
|
170
|
+
this._id = id;
|
|
171
|
+
this._val = val;
|
|
172
|
+
this._count = count ?? 1;
|
|
173
|
+
}
|
|
174
|
+
|
|
127
175
|
swapLocation(swapNode: BinaryTreeNode<T>): BinaryTreeNode<T> {
|
|
128
176
|
const {val, count, height} = swapNode;
|
|
129
177
|
const tempNode = new BinaryTreeNode<T>(swapNode.id, val);
|
|
@@ -187,6 +235,14 @@ export class BinaryTree<T> {
|
|
|
187
235
|
return this._root;
|
|
188
236
|
}
|
|
189
237
|
|
|
238
|
+
/**
|
|
239
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Getters (using the same name as the property) while utilizing separate method names for Setters.
|
|
240
|
+
* @returns The method is returning either a BinaryTreeNode object of type T or null.
|
|
241
|
+
*/
|
|
242
|
+
getRoot(): BinaryTreeNode<T> | null {
|
|
243
|
+
return this._root;
|
|
244
|
+
}
|
|
245
|
+
|
|
190
246
|
protected set root(v: BinaryTreeNode<T> | null) {
|
|
191
247
|
if (v) {
|
|
192
248
|
v.parent = null;
|
|
@@ -201,6 +257,13 @@ export class BinaryTree<T> {
|
|
|
201
257
|
return this._size;
|
|
202
258
|
}
|
|
203
259
|
|
|
260
|
+
/**
|
|
261
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
262
|
+
*/
|
|
263
|
+
getSize(): number {
|
|
264
|
+
return this._size;
|
|
265
|
+
}
|
|
266
|
+
|
|
204
267
|
protected set size(v: number) {
|
|
205
268
|
this._size = v;
|
|
206
269
|
}
|
|
@@ -211,6 +274,13 @@ export class BinaryTree<T> {
|
|
|
211
274
|
return this._count;
|
|
212
275
|
}
|
|
213
276
|
|
|
277
|
+
/**
|
|
278
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
279
|
+
*/
|
|
280
|
+
getCount(): number {
|
|
281
|
+
return this._count;
|
|
282
|
+
}
|
|
283
|
+
|
|
214
284
|
protected set count(v: number) {
|
|
215
285
|
this._count = v;
|
|
216
286
|
}
|
|
@@ -17,61 +17,85 @@ export class SegmentTreeNode {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
protected _start = 0;
|
|
20
|
-
|
|
21
20
|
get start(): number {
|
|
22
21
|
return this._start;
|
|
23
22
|
}
|
|
24
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
25
|
+
*/
|
|
26
|
+
getStart(): number {
|
|
27
|
+
return this._start;
|
|
28
|
+
}
|
|
25
29
|
set start(v: number) {
|
|
26
30
|
this._start = v;
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
protected _end = 0;
|
|
30
|
-
|
|
31
34
|
get end(): number {
|
|
32
35
|
return this._end;
|
|
33
36
|
}
|
|
34
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
39
|
+
*/
|
|
40
|
+
getEnd(): number {
|
|
41
|
+
return this._end;
|
|
42
|
+
}
|
|
35
43
|
set end(v: number) {
|
|
36
44
|
this._end = v;
|
|
37
45
|
}
|
|
38
46
|
|
|
39
47
|
protected _val: SegmentTreeNodeVal | null = null;
|
|
40
|
-
|
|
41
48
|
get val(): SegmentTreeNodeVal | null {
|
|
42
49
|
return this._val;
|
|
43
50
|
}
|
|
44
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
53
|
+
*/
|
|
54
|
+
getVal(): SegmentTreeNodeVal | null {
|
|
55
|
+
return this._val;
|
|
56
|
+
}
|
|
45
57
|
set val(v: SegmentTreeNodeVal | null) {
|
|
46
58
|
this._val = v;
|
|
47
59
|
}
|
|
48
60
|
|
|
49
61
|
protected _sum = 0;
|
|
50
|
-
|
|
51
62
|
get sum(): number {
|
|
52
63
|
return this._sum;
|
|
53
64
|
}
|
|
54
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
67
|
+
*/
|
|
68
|
+
getSum(): number {
|
|
69
|
+
return this._sum;
|
|
70
|
+
}
|
|
55
71
|
set sum(v: number) {
|
|
56
72
|
this._sum = v;
|
|
57
73
|
}
|
|
58
74
|
|
|
59
75
|
protected _left: SegmentTreeNode | null = null;
|
|
60
|
-
|
|
61
76
|
get left(): SegmentTreeNode | null {
|
|
62
77
|
return this._left;
|
|
63
78
|
}
|
|
64
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
81
|
+
*/
|
|
82
|
+
getLeft(): SegmentTreeNode | null {
|
|
83
|
+
return this._left;
|
|
84
|
+
}
|
|
65
85
|
set left(v: SegmentTreeNode | null) {
|
|
66
86
|
this._left = v;
|
|
67
87
|
}
|
|
68
88
|
|
|
69
89
|
protected _right: SegmentTreeNode | null = null;
|
|
70
|
-
|
|
71
90
|
get right(): SegmentTreeNode | null {
|
|
72
91
|
return this._right;
|
|
73
92
|
}
|
|
74
|
-
|
|
93
|
+
/**
|
|
94
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
95
|
+
*/
|
|
96
|
+
getRight(): SegmentTreeNode | null {
|
|
97
|
+
return this._right;
|
|
98
|
+
}
|
|
75
99
|
set right(v: SegmentTreeNode | null) {
|
|
76
100
|
this._right = v;
|
|
77
101
|
}
|
|
@@ -101,10 +125,18 @@ export class SegmentTree {
|
|
|
101
125
|
}
|
|
102
126
|
|
|
103
127
|
protected _root: SegmentTreeNode | null;
|
|
104
|
-
|
|
105
128
|
get root(): SegmentTreeNode | null {
|
|
106
129
|
return this._root;
|
|
107
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
133
|
+
*/
|
|
134
|
+
getRoot(): SegmentTreeNode | null {
|
|
135
|
+
return this._root;
|
|
136
|
+
}
|
|
137
|
+
set root(v: SegmentTreeNode | null) {
|
|
138
|
+
this._root = v;
|
|
139
|
+
}
|
|
108
140
|
|
|
109
141
|
/**
|
|
110
142
|
* The function builds a segment tree by recursively dividing the given range into smaller segments and creating nodes
|
|
@@ -10,19 +10,25 @@ import {PriorityQueue} from '../priority-queue';
|
|
|
10
10
|
import type {DijkstraResult, IGraph, VertexId} from '../types';
|
|
11
11
|
|
|
12
12
|
export class AbstractVertex {
|
|
13
|
-
constructor(id: VertexId) {
|
|
14
|
-
this._id = id;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
13
|
protected _id: VertexId;
|
|
14
|
+
get id(): VertexId {
|
|
15
|
+
return this._id;
|
|
16
|
+
}
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
20
|
+
*/
|
|
21
|
+
getId(): VertexId {
|
|
20
22
|
return this._id;
|
|
21
23
|
}
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
set id(v: VertexId) {
|
|
24
26
|
this._id = v;
|
|
25
27
|
}
|
|
28
|
+
|
|
29
|
+
constructor(id: VertexId) {
|
|
30
|
+
this._id = id;
|
|
31
|
+
}
|
|
26
32
|
}
|
|
27
33
|
|
|
28
34
|
export abstract class AbstractEdge {
|
|
@@ -41,21 +47,33 @@ export abstract class AbstractEdge {
|
|
|
41
47
|
}
|
|
42
48
|
|
|
43
49
|
private _weight: number;
|
|
44
|
-
|
|
45
50
|
get weight(): number {
|
|
46
51
|
return this._weight;
|
|
47
52
|
}
|
|
48
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
56
|
+
*/
|
|
57
|
+
getWeight(): number {
|
|
58
|
+
return this._weight;
|
|
59
|
+
}
|
|
60
|
+
|
|
49
61
|
set weight(v: number) {
|
|
50
62
|
this._weight = v;
|
|
51
63
|
}
|
|
52
64
|
|
|
53
65
|
private _hashCode: string;
|
|
54
|
-
|
|
55
66
|
get hashCode(): string {
|
|
56
67
|
return this._hashCode;
|
|
57
68
|
}
|
|
58
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
72
|
+
*/
|
|
73
|
+
getHashCode(): string {
|
|
74
|
+
return this._hashCode;
|
|
75
|
+
}
|
|
76
|
+
|
|
59
77
|
set hashCode(v: string) {
|
|
60
78
|
this._hashCode = v;
|
|
61
79
|
}
|
|
@@ -97,9 +115,9 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
97
115
|
* The function checks if a vertex exists in a graph.
|
|
98
116
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
|
|
99
117
|
* (`VertexId`).
|
|
100
|
-
* @returns The method `
|
|
118
|
+
* @returns The method `hasVertex` returns a boolean value.
|
|
101
119
|
*/
|
|
102
|
-
|
|
120
|
+
hasVertex(vertexOrId: V | VertexId): boolean {
|
|
103
121
|
return this._vertices.has(this.getVertexId(vertexOrId));
|
|
104
122
|
}
|
|
105
123
|
|
|
@@ -120,7 +138,7 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
120
138
|
* false. Otherwise, it will add the newVertex to the graph and return true.
|
|
121
139
|
*/
|
|
122
140
|
addVertex(newVertex: V): boolean {
|
|
123
|
-
if (this.
|
|
141
|
+
if (this.hasVertex(newVertex)) {
|
|
124
142
|
return false;
|
|
125
143
|
}
|
|
126
144
|
this._vertices.set(newVertex.id, newVertex);
|
|
@@ -165,10 +183,10 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
165
183
|
* a vertex in a graph, while V represents the type of the vertex itself.
|
|
166
184
|
* @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in an edge. It can be either a `VertexId`
|
|
167
185
|
* or a `V` type.
|
|
168
|
-
* @returns The function `
|
|
186
|
+
* @returns The function `hasEdge` returns a boolean value. It returns `true` if there is an edge between the
|
|
169
187
|
* vertices `v1` and `v2`, and `false` otherwise.
|
|
170
188
|
*/
|
|
171
|
-
|
|
189
|
+
hasEdge(v1: VertexId | V, v2: VertexId | V): boolean {
|
|
172
190
|
const edge = this.getEdge(v1, v2);
|
|
173
191
|
return !!edge;
|
|
174
192
|
}
|
|
@@ -499,8 +517,20 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
499
517
|
return {distMap, preMap, seen, paths, minDist, minPath};
|
|
500
518
|
}
|
|
501
519
|
|
|
520
|
+
/**
|
|
521
|
+
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
|
|
522
|
+
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
|
|
523
|
+
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
524
|
+
*/
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
528
|
+
* 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.
|
|
529
|
+
*/
|
|
530
|
+
|
|
502
531
|
/**
|
|
503
532
|
* Dijkstra algorithm time: O(logVE) space: O(V + E)
|
|
533
|
+
* 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.
|
|
504
534
|
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
505
535
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
506
536
|
* @param {V | VertexId} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
@@ -625,10 +655,17 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
625
655
|
|
|
626
656
|
abstract getEndsOfEdge(edge: E): [V, V] | null;
|
|
627
657
|
|
|
658
|
+
/**
|
|
659
|
+
* BellmanFord time:O(VE) space:O(V)
|
|
660
|
+
* one to rest pairs
|
|
661
|
+
* 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.
|
|
662
|
+
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
663
|
+
*/
|
|
628
664
|
|
|
629
665
|
/**
|
|
630
666
|
* BellmanFord time:O(VE) space:O(V)
|
|
631
667
|
* one to rest pairs
|
|
668
|
+
* 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.
|
|
632
669
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
633
670
|
* all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
634
671
|
* @param {V | VertexId} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
@@ -732,6 +769,13 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
732
769
|
/**
|
|
733
770
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
734
771
|
* all pairs
|
|
772
|
+
* 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.
|
|
773
|
+
*/
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
777
|
+
* all pairs
|
|
778
|
+
* 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.
|
|
735
779
|
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
|
|
736
780
|
* graph.
|
|
737
781
|
* @returns The function `floyd()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
@@ -903,8 +947,7 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
|
|
|
903
947
|
}
|
|
904
948
|
|
|
905
949
|
|
|
906
|
-
// unionFind() {
|
|
907
|
-
// }
|
|
950
|
+
// unionFind() {}
|
|
908
951
|
|
|
909
952
|
/**--- end find cycles --- */
|
|
910
953
|
|
|
@@ -40,17 +40,26 @@ export class DirectedEdge extends AbstractEdge {
|
|
|
40
40
|
get src(): VertexId {
|
|
41
41
|
return this._src;
|
|
42
42
|
}
|
|
43
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
45
|
+
*/
|
|
46
|
+
getSrc(): VertexId {
|
|
47
|
+
return this._src;
|
|
48
|
+
}
|
|
44
49
|
set src(v: VertexId) {
|
|
45
50
|
this._src = v;
|
|
46
51
|
}
|
|
47
52
|
|
|
48
|
-
|
|
49
53
|
private _dest: VertexId;
|
|
50
54
|
get dest(): VertexId {
|
|
51
55
|
return this._dest;
|
|
52
56
|
}
|
|
53
|
-
|
|
57
|
+
/**
|
|
58
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
59
|
+
*/
|
|
60
|
+
getDest(): VertexId {
|
|
61
|
+
return this._dest;
|
|
62
|
+
}
|
|
54
63
|
set dest(v: VertexId) {
|
|
55
64
|
this._dest = v;
|
|
56
65
|
}
|
|
@@ -101,7 +110,7 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
|
|
|
101
110
|
* graph, and `false` if either the source or destination vertices of the edge are not present in the graph.
|
|
102
111
|
*/
|
|
103
112
|
addEdge(edge: E): boolean {
|
|
104
|
-
if (!(this.
|
|
113
|
+
if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
|
|
105
114
|
return false;
|
|
106
115
|
}
|
|
107
116
|
|
|
@@ -401,7 +410,7 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
|
|
|
401
410
|
* returns null.
|
|
402
411
|
*/
|
|
403
412
|
getEndsOfEdge(edge: E): [V, V] | null {
|
|
404
|
-
if (!this.
|
|
413
|
+
if (!this.hasEdge(edge.src, edge.dest)) {
|
|
405
414
|
return null;
|
|
406
415
|
}
|
|
407
416
|
const v1 = this.getVertex(edge.src);
|
|
@@ -35,21 +35,38 @@ export class UndirectedEdge extends AbstractEdge {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
private _vertices: [VertexId, VertexId];
|
|
38
|
-
|
|
39
|
-
public get vertices() {
|
|
38
|
+
get vertices() {
|
|
40
39
|
return this._vertices;
|
|
41
40
|
}
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
43
|
+
*/
|
|
44
|
+
getVertices() {
|
|
45
|
+
return this._vertices;
|
|
46
|
+
}
|
|
47
|
+
set vertices(v: [VertexId, VertexId]) {
|
|
44
48
|
this._vertices = v;
|
|
45
49
|
}
|
|
46
50
|
}
|
|
47
51
|
|
|
48
52
|
export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdge> extends AbstractGraph<V, E> {
|
|
49
|
-
protected _edges: Map<V, E[]
|
|
53
|
+
protected _edges: Map<V, E[]>;
|
|
54
|
+
get edges(): Map<V, E[]> {
|
|
55
|
+
return this._edges;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
59
|
+
*/
|
|
60
|
+
getEdges(): Map<V, E[]> {
|
|
61
|
+
return this._edges;
|
|
62
|
+
}
|
|
63
|
+
protected set edges(v: Map<V, E[]>) {
|
|
64
|
+
this._edges = v;
|
|
65
|
+
}
|
|
50
66
|
|
|
51
67
|
constructor() {
|
|
52
68
|
super();
|
|
69
|
+
this._edges = new Map<V, E[]>();
|
|
53
70
|
}
|
|
54
71
|
|
|
55
72
|
/**
|
|
@@ -223,7 +240,7 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
|
|
|
223
240
|
* `null`.
|
|
224
241
|
*/
|
|
225
242
|
getEndsOfEdge(edge: E): [V, V] | null {
|
|
226
|
-
if (!this.
|
|
243
|
+
if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
|
|
227
244
|
return null;
|
|
228
245
|
}
|
|
229
246
|
const v1 = this.getVertex(edge.vertices[0]);
|
|
@@ -6,7 +6,19 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
export class CoordinateMap<V> extends Map<any, V> {
|
|
9
|
-
|
|
9
|
+
protected _joint: string = '_';
|
|
10
|
+
get joint(): string {
|
|
11
|
+
return this._joint;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
15
|
+
*/
|
|
16
|
+
getJoint(): string {
|
|
17
|
+
return this._joint;
|
|
18
|
+
}
|
|
19
|
+
protected set joint(v: string) {
|
|
20
|
+
this._joint = v;
|
|
21
|
+
}
|
|
10
22
|
|
|
11
23
|
constructor(joint?: string) {
|
|
12
24
|
super();
|
|
@@ -6,7 +6,19 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
export class CoordinateSet extends Set {
|
|
9
|
-
|
|
9
|
+
protected _joint: string = '_';
|
|
10
|
+
get joint(): string {
|
|
11
|
+
return this._joint;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
15
|
+
*/
|
|
16
|
+
getJoint(): string {
|
|
17
|
+
return this._joint;
|
|
18
|
+
}
|
|
19
|
+
protected set joint(v: string) {
|
|
20
|
+
this._joint = v;
|
|
21
|
+
}
|
|
10
22
|
|
|
11
23
|
constructor(joint?: string) {
|
|
12
24
|
super();
|