data-structure-typed 1.12.21 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +278 -179
  2. package/dist/data-structures/binary-tree/binary-tree.d.ts +46 -1
  3. package/dist/data-structures/binary-tree/binary-tree.js +67 -0
  4. package/dist/data-structures/binary-tree/segment-tree.d.ts +29 -0
  5. package/dist/data-structures/binary-tree/segment-tree.js +45 -0
  6. package/dist/data-structures/graph/abstract-graph.d.ts +40 -5
  7. package/dist/data-structures/graph/abstract-graph.js +47 -7
  8. package/dist/data-structures/graph/directed-graph.d.ts +8 -0
  9. package/dist/data-structures/graph/directed-graph.js +14 -2
  10. package/dist/data-structures/graph/undirected-graph.d.ts +10 -0
  11. package/dist/data-structures/graph/undirected-graph.js +23 -1
  12. package/dist/data-structures/hash/coordinate-map.d.ts +7 -1
  13. package/dist/data-structures/hash/coordinate-map.js +16 -0
  14. package/dist/data-structures/hash/coordinate-set.d.ts +7 -1
  15. package/dist/data-structures/hash/coordinate-set.js +16 -0
  16. package/dist/data-structures/heap/heap.d.ts +16 -0
  17. package/dist/data-structures/heap/heap.js +38 -0
  18. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +30 -7
  19. package/dist/data-structures/linked-list/doubly-linked-list.js +71 -4
  20. package/dist/data-structures/linked-list/singly-linked-list.d.ts +262 -328
  21. package/dist/data-structures/linked-list/singly-linked-list.js +258 -273
  22. package/dist/data-structures/priority-queue/priority-queue.d.ts +7 -1
  23. package/dist/data-structures/priority-queue/priority-queue.js +18 -2
  24. package/dist/data-structures/queue/deque.d.ts +18 -7
  25. package/dist/data-structures/queue/deque.js +50 -3
  26. package/dist/data-structures/types/abstract-graph.d.ts +2 -2
  27. package/package.json +2 -2
  28. package/src/data-structures/binary-tree/aa-tree.ts +1 -1
  29. package/src/data-structures/binary-tree/binary-tree.ts +84 -14
  30. package/src/data-structures/binary-tree/segment-tree.ts +45 -13
  31. package/src/data-structures/graph/abstract-graph.ts +58 -15
  32. package/src/data-structures/graph/directed-graph.ts +14 -5
  33. package/src/data-structures/graph/undirected-graph.ts +23 -6
  34. package/src/data-structures/hash/coordinate-map.ts +13 -1
  35. package/src/data-structures/hash/coordinate-set.ts +13 -1
  36. package/src/data-structures/heap/heap.ts +31 -0
  37. package/src/data-structures/linked-list/doubly-linked-list.ts +68 -11
  38. package/src/data-structures/linked-list/singly-linked-list.ts +312 -334
  39. package/src/data-structures/priority-queue/priority-queue.ts +15 -2
  40. package/src/data-structures/queue/deque.ts +38 -8
  41. package/src/data-structures/types/abstract-graph.ts +3 -3
  42. package/tests/unit/data-structures/graph/directed-graph.test.ts +431 -8
@@ -7,7 +7,13 @@
7
7
  */
8
8
  import type { PriorityQueueComparator, PriorityQueueDFSOrderPattern, PriorityQueueOptions } from '../types';
9
9
  export declare class PriorityQueue<T = number> {
10
- protected nodes: T[];
10
+ protected _nodes: T[];
11
+ get nodes(): T[];
12
+ /**
13
+ * 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.
14
+ */
15
+ getNodes(): T[];
16
+ protected set nodes(value: T[]);
11
17
  /**
12
18
  * The constructor initializes a priority queue with the given options, including an array of nodes and a comparator
13
19
  * function.
@@ -44,7 +44,7 @@ var PriorityQueue = /** @class */ (function () {
44
44
  * @param options - The `options` parameter is an object that contains the following properties:
45
45
  */
46
46
  function PriorityQueue(options) {
47
- this.nodes = [];
47
+ this._nodes = [];
48
48
  this._comparator = function (a, b) {
49
49
  var aKey = a, bKey = b;
50
50
  return aKey - bKey;
@@ -53,10 +53,26 @@ var PriorityQueue = /** @class */ (function () {
53
53
  this._comparator = comparator;
54
54
  if (nodes && nodes instanceof Array && nodes.length > 0) {
55
55
  // TODO support distinct
56
- this.nodes = Array.isArray(nodes) ? __spreadArray([], __read(nodes), false) : [];
56
+ this._nodes = Array.isArray(nodes) ? __spreadArray([], __read(nodes), false) : [];
57
57
  isFix && this._fix();
58
58
  }
59
59
  }
60
+ Object.defineProperty(PriorityQueue.prototype, "nodes", {
61
+ get: function () {
62
+ return this._nodes;
63
+ },
64
+ set: function (value) {
65
+ this._nodes = value;
66
+ },
67
+ enumerable: false,
68
+ configurable: true
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
+ PriorityQueue.prototype.getNodes = function () {
74
+ return this._nodes;
75
+ };
60
76
  Object.defineProperty(PriorityQueue.prototype, "size", {
61
77
  get: function () {
62
78
  return this.nodes.length;
@@ -9,15 +9,26 @@ import { DoublyLinkedList } from '../linked-list';
9
9
  export declare class Deque<T> extends DoublyLinkedList<T> {
10
10
  }
11
11
  export declare class ObjectDeque<T> {
12
- protected _nodes: {
13
- [key: number]: T;
12
+ private _nodes;
13
+ get nodes(): {
14
+ [p: number]: T;
14
15
  };
15
- protected _capacity: number;
16
- protected _first: number;
17
- protected _last: number;
18
- protected _size: number;
16
+ protected set nodes(value: {
17
+ [p: number]: T;
18
+ });
19
+ private _capacity;
20
+ get capacity(): number;
21
+ set capacity(value: number);
22
+ private _first;
23
+ get first(): number;
24
+ set first(value: number);
25
+ private _last;
26
+ get last(): number;
27
+ set last(value: number);
28
+ private _size;
29
+ get size(): number;
30
+ protected set size(value: number);
19
31
  constructor(capacity?: number);
20
- size(): number;
21
32
  addFirst(value: T): void;
22
33
  addLast(value: T): void;
23
34
  pollFirst(): T | undefined;
@@ -47,9 +47,56 @@ var ObjectDeque = /** @class */ (function () {
47
47
  if (capacity !== undefined)
48
48
  this._capacity = capacity;
49
49
  }
50
- ObjectDeque.prototype.size = function () {
51
- return this._size;
52
- };
50
+ Object.defineProperty(ObjectDeque.prototype, "nodes", {
51
+ get: function () {
52
+ return this._nodes;
53
+ },
54
+ set: function (value) {
55
+ this._nodes = value;
56
+ },
57
+ enumerable: false,
58
+ configurable: true
59
+ });
60
+ Object.defineProperty(ObjectDeque.prototype, "capacity", {
61
+ get: function () {
62
+ return this._capacity;
63
+ },
64
+ set: function (value) {
65
+ this._capacity = value;
66
+ },
67
+ enumerable: false,
68
+ configurable: true
69
+ });
70
+ Object.defineProperty(ObjectDeque.prototype, "first", {
71
+ get: function () {
72
+ return this._first;
73
+ },
74
+ set: function (value) {
75
+ this._first = value;
76
+ },
77
+ enumerable: false,
78
+ configurable: true
79
+ });
80
+ Object.defineProperty(ObjectDeque.prototype, "last", {
81
+ get: function () {
82
+ return this._last;
83
+ },
84
+ set: function (value) {
85
+ this._last = value;
86
+ },
87
+ enumerable: false,
88
+ configurable: true
89
+ });
90
+ Object.defineProperty(ObjectDeque.prototype, "size", {
91
+ get: function () {
92
+ return this._size;
93
+ },
94
+ set: function (value) {
95
+ this._size = value;
96
+ },
97
+ enumerable: false,
98
+ configurable: true
99
+ });
53
100
  ObjectDeque.prototype.addFirst = function (value) {
54
101
  if (this._size === 0) {
55
102
  var mid = Math.floor(this._capacity / 2);
@@ -8,7 +8,7 @@ export type DijkstraResult<V> = {
8
8
  minPath: V[];
9
9
  } | null;
10
10
  export interface IGraph<V, E> {
11
- containsVertex(vertexOrId: V | VertexId): boolean;
11
+ hasVertex(vertexOrId: V | VertexId): boolean;
12
12
  getVertex(vertexOrId: VertexId | V): V | null;
13
13
  getVertexId(vertexOrId: V | VertexId): VertexId;
14
14
  vertexSet(): Map<VertexId, V>;
@@ -17,7 +17,7 @@ export interface IGraph<V, E> {
17
17
  removeAllVertices(vertices: V[] | VertexId[]): boolean;
18
18
  degreeOf(vertexOrId: V | VertexId): number;
19
19
  edgesOf(vertexOrId: V | VertexId): E[];
20
- containsEdge(src: V | VertexId, dest: V | VertexId): boolean;
20
+ hasEdge(src: V | VertexId, dest: V | VertexId): boolean;
21
21
  getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
22
22
  edgeSet(): E[];
23
23
  addEdge(edge: E): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.12.21",
3
+ "version": "1.15.0",
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": {
@@ -54,7 +54,7 @@
54
54
  "jest": "^29.6.2",
55
55
  "ts-jest": "^29.1.1",
56
56
  "typedoc": "^0.24.8",
57
- "typescript": "^4.6.2"
57
+ "typescript": "^4.9.5"
58
58
  },
59
59
  "dependencies": {
60
60
  "lodash": "^4.17.21"
@@ -1,3 +1,3 @@
1
1
  export class AaTree {
2
2
 
3
- }
3
+ }
@@ -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
- public get id(): VertexId {
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
- public set id(v: VertexId) {
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 `containsVertex` returns a boolean value.
118
+ * @returns The method `hasVertex` returns a boolean value.
101
119
  */
102
- containsVertex(vertexOrId: V | VertexId): boolean {
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.containsVertex(newVertex)) {
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 `containsEdge` returns a boolean value. It returns `true` if there is an edge between the
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
- containsEdge(v1: VertexId | V, v2: VertexId | V): boolean {
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