queue-typed 1.47.7 → 1.47.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/data-structures/graph/abstract-graph.js +30 -30
- package/dist/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/data-structures/graph/directed-graph.js +28 -28
- package/dist/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/data-structures/graph/undirected-graph.js +18 -18
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/data-structures/queue/queue.d.ts +13 -13
- package/dist/data-structures/queue/queue.js +13 -13
- package/dist/data-structures/stack/stack.d.ts +6 -6
- package/dist/data-structures/stack/stack.js +7 -7
- package/dist/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/segment-tree.ts +10 -10
- package/src/data-structures/graph/abstract-graph.ts +46 -46
- package/src/data-structures/graph/directed-graph.ts +40 -40
- package/src/data-structures/graph/undirected-graph.ts +26 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
- package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
- package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
- package/src/data-structures/queue/queue.ts +13 -13
- package/src/data-structures/stack/stack.ts +9 -9
- package/src/types/data-structures/graph/abstract-graph.ts +2 -2
|
@@ -16,8 +16,8 @@ class LinkedListQueue extends linked_list_1.SinglyLinkedList {
|
|
|
16
16
|
this.push(value);
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns
|
|
20
|
-
* @returns The method is returning the element at the front of the queue, or
|
|
19
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
20
|
+
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
21
21
|
*/
|
|
22
22
|
dequeue() {
|
|
23
23
|
return this.shift();
|
|
@@ -100,7 +100,7 @@ class Queue {
|
|
|
100
100
|
*
|
|
101
101
|
* The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
102
102
|
* necessary to optimize performance.
|
|
103
|
-
* @returns The function `shift()` returns either the first element in the queue or `
|
|
103
|
+
* @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
|
|
104
104
|
*/
|
|
105
105
|
shift() {
|
|
106
106
|
if (this.size === 0)
|
|
@@ -123,9 +123,9 @@ class Queue {
|
|
|
123
123
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
124
124
|
* Space Complexity: O(1) - no additional space is used.
|
|
125
125
|
*
|
|
126
|
-
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `
|
|
126
|
+
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
127
127
|
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
128
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `
|
|
128
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
129
129
|
*/
|
|
130
130
|
getFirst() {
|
|
131
131
|
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
@@ -138,9 +138,9 @@ class Queue {
|
|
|
138
138
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
139
139
|
* Space Complexity: O(1) - no additional space is used.
|
|
140
140
|
*
|
|
141
|
-
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `
|
|
141
|
+
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
142
142
|
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
143
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `
|
|
143
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
144
144
|
*/
|
|
145
145
|
peek() {
|
|
146
146
|
return this.getFirst();
|
|
@@ -153,9 +153,9 @@ class Queue {
|
|
|
153
153
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
154
154
|
* Space Complexity: O(1) - no additional space is used.
|
|
155
155
|
*
|
|
156
|
-
* The `getLast` function returns the last element in an array-like data structure, or
|
|
156
|
+
* The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
157
157
|
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
158
|
-
* array is empty, it returns `
|
|
158
|
+
* array is empty, it returns `undefined`.
|
|
159
159
|
*/
|
|
160
160
|
getLast() {
|
|
161
161
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
@@ -168,9 +168,9 @@ class Queue {
|
|
|
168
168
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
169
169
|
* Space Complexity: O(1) - no additional space is used.
|
|
170
170
|
*
|
|
171
|
-
* The `peekLast` function returns the last element in an array-like data structure, or
|
|
171
|
+
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
172
172
|
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
173
|
-
* array is empty, it returns `
|
|
173
|
+
* array is empty, it returns `undefined`.
|
|
174
174
|
*/
|
|
175
175
|
peekLast() {
|
|
176
176
|
return this.getLast();
|
|
@@ -197,8 +197,8 @@ class Queue {
|
|
|
197
197
|
* Time Complexity: O(n) - same as shift().
|
|
198
198
|
* Space Complexity: O(1) - same as shift().
|
|
199
199
|
*
|
|
200
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns
|
|
201
|
-
* @returns The method is returning a value of type E or
|
|
200
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
201
|
+
* @returns The method is returning a value of type E or undefined.
|
|
202
202
|
*/
|
|
203
203
|
dequeue() {
|
|
204
204
|
return this.shift();
|
|
@@ -45,10 +45,10 @@ export declare class Stack<E = any> {
|
|
|
45
45
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
46
46
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
47
47
|
*
|
|
48
|
-
* The `peek` function returns the last element of an array, or
|
|
49
|
-
* @returns The `peek()` function returns the last element of the `_elements` array, or `
|
|
48
|
+
* The `peek` function returns the last element of an array, or undefined if the array is empty.
|
|
49
|
+
* @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
|
|
50
50
|
*/
|
|
51
|
-
peek(): E |
|
|
51
|
+
peek(): E | undefined;
|
|
52
52
|
/**
|
|
53
53
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
54
54
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
@@ -70,11 +70,11 @@ export declare class Stack<E = any> {
|
|
|
70
70
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
71
71
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
72
72
|
*
|
|
73
|
-
* The `pop` function removes and returns the last element from an array, or returns
|
|
73
|
+
* The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
|
|
74
74
|
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
75
|
-
* array is empty, it returns `
|
|
75
|
+
* array is empty, it returns `undefined`.
|
|
76
76
|
*/
|
|
77
|
-
pop(): E |
|
|
77
|
+
pop(): E | undefined;
|
|
78
78
|
/**
|
|
79
79
|
* Time Complexity: O(n)
|
|
80
80
|
* Space Complexity: O(n)
|
|
@@ -62,12 +62,12 @@ class Stack {
|
|
|
62
62
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
63
63
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
64
64
|
*
|
|
65
|
-
* The `peek` function returns the last element of an array, or
|
|
66
|
-
* @returns The `peek()` function returns the last element of the `_elements` array, or `
|
|
65
|
+
* The `peek` function returns the last element of an array, or undefined if the array is empty.
|
|
66
|
+
* @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
|
|
67
67
|
*/
|
|
68
68
|
peek() {
|
|
69
69
|
if (this.isEmpty())
|
|
70
|
-
return
|
|
70
|
+
return undefined;
|
|
71
71
|
return this.elements[this.elements.length - 1];
|
|
72
72
|
}
|
|
73
73
|
/**
|
|
@@ -94,14 +94,14 @@ class Stack {
|
|
|
94
94
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
95
95
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
96
96
|
*
|
|
97
|
-
* The `pop` function removes and returns the last element from an array, or returns
|
|
97
|
+
* The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
|
|
98
98
|
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
99
|
-
* array is empty, it returns `
|
|
99
|
+
* array is empty, it returns `undefined`.
|
|
100
100
|
*/
|
|
101
101
|
pop() {
|
|
102
102
|
if (this.isEmpty())
|
|
103
|
-
return
|
|
104
|
-
return this.elements.pop() ||
|
|
103
|
+
return undefined;
|
|
104
|
+
return this.elements.pop() || undefined;
|
|
105
105
|
}
|
|
106
106
|
/**
|
|
107
107
|
* Time Complexity: O(n)
|
|
@@ -2,9 +2,9 @@ export type VertexKey = string | number;
|
|
|
2
2
|
export type DijkstraResult<V> = {
|
|
3
3
|
distMap: Map<V, number>;
|
|
4
4
|
distPaths?: Map<V, V[]>;
|
|
5
|
-
preMap: Map<V, V |
|
|
5
|
+
preMap: Map<V, V | undefined>;
|
|
6
6
|
seen: Set<V>;
|
|
7
7
|
paths: V[][];
|
|
8
8
|
minDist: number;
|
|
9
9
|
minPath: V[];
|
|
10
|
-
} |
|
|
10
|
+
} | undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "queue-typed",
|
|
3
|
-
"version": "1.47.
|
|
3
|
+
"version": "1.47.8",
|
|
4
4
|
"description": "Queue, ArrayQueue. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -115,6 +115,6 @@
|
|
|
115
115
|
"typescript": "^4.9.5"
|
|
116
116
|
},
|
|
117
117
|
"dependencies": {
|
|
118
|
-
"data-structure-typed": "^1.47.
|
|
118
|
+
"data-structure-typed": "^1.47.8"
|
|
119
119
|
}
|
|
120
120
|
}
|
|
@@ -11,16 +11,16 @@ import type { SegmentTreeNodeVal } from '../../types';
|
|
|
11
11
|
export class SegmentTreeNode {
|
|
12
12
|
start = 0;
|
|
13
13
|
end = 0;
|
|
14
|
-
value: SegmentTreeNodeVal |
|
|
14
|
+
value: SegmentTreeNodeVal | undefined = undefined;
|
|
15
15
|
sum = 0;
|
|
16
|
-
left: SegmentTreeNode |
|
|
17
|
-
right: SegmentTreeNode |
|
|
16
|
+
left: SegmentTreeNode | undefined = undefined;
|
|
17
|
+
right: SegmentTreeNode | undefined = undefined;
|
|
18
18
|
|
|
19
|
-
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal |
|
|
19
|
+
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined) {
|
|
20
20
|
this.start = start;
|
|
21
21
|
this.end = end;
|
|
22
22
|
this.sum = sum;
|
|
23
|
-
this.value = value ||
|
|
23
|
+
this.value = value || undefined;
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -44,7 +44,7 @@ export class SegmentTree {
|
|
|
44
44
|
if (values.length > 0) {
|
|
45
45
|
this._root = this.build(start, end);
|
|
46
46
|
} else {
|
|
47
|
-
this._root =
|
|
47
|
+
this._root = undefined;
|
|
48
48
|
this._values = [];
|
|
49
49
|
}
|
|
50
50
|
}
|
|
@@ -67,9 +67,9 @@ export class SegmentTree {
|
|
|
67
67
|
return this._end;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
protected _root: SegmentTreeNode |
|
|
70
|
+
protected _root: SegmentTreeNode | undefined;
|
|
71
71
|
|
|
72
|
-
get root(): SegmentTreeNode |
|
|
72
|
+
get root(): SegmentTreeNode | undefined {
|
|
73
73
|
return this._root;
|
|
74
74
|
}
|
|
75
75
|
|
|
@@ -109,7 +109,7 @@ export class SegmentTree {
|
|
|
109
109
|
* @returns The function does not return anything.
|
|
110
110
|
*/
|
|
111
111
|
updateNode(index: number, sum: number, value?: SegmentTreeNodeVal) {
|
|
112
|
-
const root = this.root ||
|
|
112
|
+
const root = this.root || undefined;
|
|
113
113
|
if (!root) {
|
|
114
114
|
return;
|
|
115
115
|
}
|
|
@@ -145,7 +145,7 @@ export class SegmentTree {
|
|
|
145
145
|
* @returns The function `querySumByRange` returns a number.
|
|
146
146
|
*/
|
|
147
147
|
querySumByRange(indexA: number, indexB: number): number {
|
|
148
|
-
const root = this.root ||
|
|
148
|
+
const root = this.root || undefined;
|
|
149
149
|
if (!root) {
|
|
150
150
|
return 0;
|
|
151
151
|
}
|
|
@@ -89,9 +89,9 @@ export abstract class AbstractGraph<
|
|
|
89
89
|
*/
|
|
90
90
|
abstract createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
|
|
91
91
|
|
|
92
|
-
abstract deleteEdge(edge: EO): EO |
|
|
92
|
+
abstract deleteEdge(edge: EO): EO | undefined;
|
|
93
93
|
|
|
94
|
-
abstract getEdge(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO |
|
|
94
|
+
abstract getEdge(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | undefined;
|
|
95
95
|
|
|
96
96
|
abstract degreeOf(vertexOrKey: VO | VertexKey): number;
|
|
97
97
|
|
|
@@ -101,7 +101,7 @@ export abstract class AbstractGraph<
|
|
|
101
101
|
|
|
102
102
|
abstract getNeighbors(vertexOrKey: VO | VertexKey): VO[];
|
|
103
103
|
|
|
104
|
-
abstract getEndsOfEdge(edge: EO): [VO, VO] |
|
|
104
|
+
abstract getEndsOfEdge(edge: EO): [VO, VO] | undefined;
|
|
105
105
|
|
|
106
106
|
/**
|
|
107
107
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
@@ -112,14 +112,14 @@ export abstract class AbstractGraph<
|
|
|
112
112
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
113
113
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
114
114
|
*
|
|
115
|
-
* The function "getVertex" returns the vertex with the specified ID or
|
|
115
|
+
* The function "getVertex" returns the vertex with the specified ID or undefined if it doesn't exist.
|
|
116
116
|
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
117
117
|
* the `_vertices` map.
|
|
118
118
|
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
|
|
119
|
-
* map. If the vertex does not exist, it returns `
|
|
119
|
+
* map. If the vertex does not exist, it returns `undefined`.
|
|
120
120
|
*/
|
|
121
|
-
getVertex(vertexKey: VertexKey): VO |
|
|
122
|
-
return this._vertices.get(vertexKey) ||
|
|
121
|
+
getVertex(vertexKey: VertexKey): VO | undefined {
|
|
122
|
+
return this._vertices.get(vertexKey) || undefined;
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
/**
|
|
@@ -365,7 +365,7 @@ export abstract class AbstractGraph<
|
|
|
365
365
|
* vertices. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
366
366
|
* minimum number of
|
|
367
367
|
*/
|
|
368
|
-
getMinCostBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean): number |
|
|
368
|
+
getMinCostBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean): number | undefined {
|
|
369
369
|
if (isWeight === undefined) isWeight = false;
|
|
370
370
|
|
|
371
371
|
if (isWeight) {
|
|
@@ -380,7 +380,7 @@ export abstract class AbstractGraph<
|
|
|
380
380
|
const vertex2 = this._getVertex(v2);
|
|
381
381
|
const vertex1 = this._getVertex(v1);
|
|
382
382
|
if (!(vertex1 && vertex2)) {
|
|
383
|
-
return
|
|
383
|
+
return undefined;
|
|
384
384
|
}
|
|
385
385
|
|
|
386
386
|
const visited: Map<VO, boolean> = new Map();
|
|
@@ -406,7 +406,7 @@ export abstract class AbstractGraph<
|
|
|
406
406
|
}
|
|
407
407
|
cost++;
|
|
408
408
|
}
|
|
409
|
-
return
|
|
409
|
+
return undefined;
|
|
410
410
|
}
|
|
411
411
|
}
|
|
412
412
|
|
|
@@ -432,9 +432,9 @@ export abstract class AbstractGraph<
|
|
|
432
432
|
* followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
|
|
433
433
|
* so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
|
|
434
434
|
* @returns The function `getMinPathBetween` returns an array of vertices (`VO[]`) representing the minimum path between
|
|
435
|
-
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `
|
|
435
|
+
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `undefined`.
|
|
436
436
|
*/
|
|
437
|
-
getMinPathBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean, isDFS = false): VO[] |
|
|
437
|
+
getMinPathBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean, isDFS = false): VO[] | undefined {
|
|
438
438
|
if (isWeight === undefined) isWeight = false;
|
|
439
439
|
|
|
440
440
|
if (isWeight) {
|
|
@@ -451,7 +451,7 @@ export abstract class AbstractGraph<
|
|
|
451
451
|
}
|
|
452
452
|
index++;
|
|
453
453
|
}
|
|
454
|
-
return allPaths[minIndex] ||
|
|
454
|
+
return allPaths[minIndex] || undefined;
|
|
455
455
|
} else {
|
|
456
456
|
return this.dijkstra(v1, v2, true, true)?.minPath ?? [];
|
|
457
457
|
}
|
|
@@ -503,9 +503,9 @@ export abstract class AbstractGraph<
|
|
|
503
503
|
* a graph without using a heap data structure.
|
|
504
504
|
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
505
505
|
* vertex object or a vertex ID.
|
|
506
|
-
* @param {VO | VertexKey |
|
|
506
|
+
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
507
507
|
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
508
|
-
* identifier. If no destination is provided, the value is set to `
|
|
508
|
+
* identifier. If no destination is provided, the value is set to `undefined`.
|
|
509
509
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
510
510
|
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
511
511
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
@@ -516,29 +516,29 @@ export abstract class AbstractGraph<
|
|
|
516
516
|
*/
|
|
517
517
|
dijkstraWithoutHeap(
|
|
518
518
|
src: VO | VertexKey,
|
|
519
|
-
dest?: VO | VertexKey |
|
|
519
|
+
dest?: VO | VertexKey | undefined,
|
|
520
520
|
getMinDist?: boolean,
|
|
521
521
|
genPaths?: boolean
|
|
522
522
|
): DijkstraResult<VO> {
|
|
523
523
|
if (getMinDist === undefined) getMinDist = false;
|
|
524
524
|
if (genPaths === undefined) genPaths = false;
|
|
525
525
|
|
|
526
|
-
if (dest === undefined) dest =
|
|
526
|
+
if (dest === undefined) dest = undefined;
|
|
527
527
|
let minDist = Infinity;
|
|
528
|
-
let minDest: VO |
|
|
528
|
+
let minDest: VO | undefined = undefined;
|
|
529
529
|
let minPath: VO[] = [];
|
|
530
530
|
const paths: VO[][] = [];
|
|
531
531
|
|
|
532
532
|
const vertices = this._vertices;
|
|
533
533
|
const distMap: Map<VO, number> = new Map();
|
|
534
534
|
const seen: Set<VO> = new Set();
|
|
535
|
-
const preMap: Map<VO, VO |
|
|
535
|
+
const preMap: Map<VO, VO | undefined> = new Map(); // predecessor
|
|
536
536
|
const srcVertex = this._getVertex(src);
|
|
537
537
|
|
|
538
|
-
const destVertex = dest ? this._getVertex(dest) :
|
|
538
|
+
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
539
539
|
|
|
540
540
|
if (!srcVertex) {
|
|
541
|
-
return
|
|
541
|
+
return undefined;
|
|
542
542
|
}
|
|
543
543
|
|
|
544
544
|
for (const vertex of vertices) {
|
|
@@ -546,11 +546,11 @@ export abstract class AbstractGraph<
|
|
|
546
546
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
547
547
|
}
|
|
548
548
|
distMap.set(srcVertex, 0);
|
|
549
|
-
preMap.set(srcVertex,
|
|
549
|
+
preMap.set(srcVertex, undefined);
|
|
550
550
|
|
|
551
551
|
const getMinOfNoSeen = () => {
|
|
552
552
|
let min = Infinity;
|
|
553
|
-
let minV: VO |
|
|
553
|
+
let minV: VO | undefined = undefined;
|
|
554
554
|
for (const [key, value] of distMap) {
|
|
555
555
|
if (!seen.has(key)) {
|
|
556
556
|
if (value < min) {
|
|
@@ -562,7 +562,7 @@ export abstract class AbstractGraph<
|
|
|
562
562
|
return minV;
|
|
563
563
|
};
|
|
564
564
|
|
|
565
|
-
const getPaths = (minV: VO |
|
|
565
|
+
const getPaths = (minV: VO | undefined) => {
|
|
566
566
|
for (const vertex of vertices) {
|
|
567
567
|
const vertexOrKey = vertex[1];
|
|
568
568
|
|
|
@@ -600,7 +600,7 @@ export abstract class AbstractGraph<
|
|
|
600
600
|
if (edge) {
|
|
601
601
|
const curFromMap = distMap.get(cur);
|
|
602
602
|
const neighborFromMap = distMap.get(neighbor);
|
|
603
|
-
// TODO after no-non-
|
|
603
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
604
604
|
if (curFromMap !== undefined && neighborFromMap !== undefined) {
|
|
605
605
|
if (edge.weight + curFromMap < neighborFromMap) {
|
|
606
606
|
distMap.set(neighbor, edge.weight + curFromMap);
|
|
@@ -651,7 +651,7 @@ export abstract class AbstractGraph<
|
|
|
651
651
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
652
652
|
* @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
653
653
|
* start. It can be either a vertex object or a vertex ID.
|
|
654
|
-
* @param {VO | VertexKey |
|
|
654
|
+
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
655
655
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
656
656
|
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
657
657
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -664,27 +664,27 @@ export abstract class AbstractGraph<
|
|
|
664
664
|
*/
|
|
665
665
|
dijkstra(
|
|
666
666
|
src: VO | VertexKey,
|
|
667
|
-
dest?: VO | VertexKey |
|
|
667
|
+
dest?: VO | VertexKey | undefined,
|
|
668
668
|
getMinDist?: boolean,
|
|
669
669
|
genPaths?: boolean
|
|
670
670
|
): DijkstraResult<VO> {
|
|
671
671
|
if (getMinDist === undefined) getMinDist = false;
|
|
672
672
|
if (genPaths === undefined) genPaths = false;
|
|
673
673
|
|
|
674
|
-
if (dest === undefined) dest =
|
|
674
|
+
if (dest === undefined) dest = undefined;
|
|
675
675
|
let minDist = Infinity;
|
|
676
|
-
let minDest: VO |
|
|
676
|
+
let minDest: VO | undefined = undefined;
|
|
677
677
|
let minPath: VO[] = [];
|
|
678
678
|
const paths: VO[][] = [];
|
|
679
679
|
const vertices = this._vertices;
|
|
680
680
|
const distMap: Map<VO, number> = new Map();
|
|
681
681
|
const seen: Set<VO> = new Set();
|
|
682
|
-
const preMap: Map<VO, VO |
|
|
682
|
+
const preMap: Map<VO, VO | undefined> = new Map(); // predecessor
|
|
683
683
|
|
|
684
684
|
const srcVertex = this._getVertex(src);
|
|
685
|
-
const destVertex = dest ? this._getVertex(dest) :
|
|
685
|
+
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
686
686
|
|
|
687
|
-
if (!srcVertex) return
|
|
687
|
+
if (!srcVertex) return undefined;
|
|
688
688
|
|
|
689
689
|
for (const vertex of vertices) {
|
|
690
690
|
const vertexOrKey = vertex[1];
|
|
@@ -695,14 +695,14 @@ export abstract class AbstractGraph<
|
|
|
695
695
|
heap.add({ key: 0, value: srcVertex });
|
|
696
696
|
|
|
697
697
|
distMap.set(srcVertex, 0);
|
|
698
|
-
preMap.set(srcVertex,
|
|
698
|
+
preMap.set(srcVertex, undefined);
|
|
699
699
|
|
|
700
700
|
/**
|
|
701
701
|
* The function `getPaths` retrieves all paths from vertices to a specified minimum vertex.
|
|
702
|
-
* @param {VO |
|
|
703
|
-
*
|
|
702
|
+
* @param {VO | undefined} minV - The parameter `minV` is of type `VO | undefined`. It represents the minimum vertex value or
|
|
703
|
+
* undefined.
|
|
704
704
|
*/
|
|
705
|
-
const getPaths = (minV: VO |
|
|
705
|
+
const getPaths = (minV: VO | undefined) => {
|
|
706
706
|
for (const vertex of vertices) {
|
|
707
707
|
const vertexOrKey = vertex[1];
|
|
708
708
|
if (vertexOrKey instanceof AbstractVertex) {
|
|
@@ -841,7 +841,7 @@ export abstract class AbstractGraph<
|
|
|
841
841
|
}
|
|
842
842
|
}
|
|
843
843
|
|
|
844
|
-
let minDest: VO |
|
|
844
|
+
let minDest: VO | undefined = undefined;
|
|
845
845
|
if (getMin) {
|
|
846
846
|
distMap.forEach((d, v) => {
|
|
847
847
|
if (v !== srcVertex) {
|
|
@@ -920,22 +920,22 @@ export abstract class AbstractGraph<
|
|
|
920
920
|
* graph.
|
|
921
921
|
* @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
922
922
|
* property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
|
|
923
|
-
* `predecessor` property is a 2D array of vertices (or `
|
|
923
|
+
* `predecessor` property is a 2D array of vertices (or `undefined`) representing the predecessor vertices in the shortest
|
|
924
924
|
* path between vertices in the
|
|
925
925
|
*/
|
|
926
|
-
floydWarshall(): { costs: number[][]; predecessor: (VO |
|
|
926
|
+
floydWarshall(): { costs: number[][]; predecessor: (VO | undefined)[][] } {
|
|
927
927
|
const idAndVertices = [...this._vertices];
|
|
928
928
|
const n = idAndVertices.length;
|
|
929
929
|
|
|
930
930
|
const costs: number[][] = [];
|
|
931
|
-
const predecessor: (VO |
|
|
931
|
+
const predecessor: (VO | undefined)[][] = [];
|
|
932
932
|
// successors
|
|
933
933
|
|
|
934
934
|
for (let i = 0; i < n; i++) {
|
|
935
935
|
costs[i] = [];
|
|
936
936
|
predecessor[i] = [];
|
|
937
937
|
for (let j = 0; j < n; j++) {
|
|
938
|
-
predecessor[i][j] =
|
|
938
|
+
predecessor[i][j] = undefined;
|
|
939
939
|
}
|
|
940
940
|
}
|
|
941
941
|
|
|
@@ -1021,7 +1021,7 @@ export abstract class AbstractGraph<
|
|
|
1021
1021
|
const cutVertexes: VO[] = [];
|
|
1022
1022
|
const bridges: EO[] = [];
|
|
1023
1023
|
let dfn = 0;
|
|
1024
|
-
const dfs = (cur: VO, parent: VO |
|
|
1024
|
+
const dfs = (cur: VO, parent: VO | undefined) => {
|
|
1025
1025
|
dfn++;
|
|
1026
1026
|
dfnMap.set(cur, dfn);
|
|
1027
1027
|
lowMap.set(cur, dfn);
|
|
@@ -1036,7 +1036,7 @@ export abstract class AbstractGraph<
|
|
|
1036
1036
|
}
|
|
1037
1037
|
const childLow = lowMap.get(neighbor);
|
|
1038
1038
|
const curLow = lowMap.get(cur);
|
|
1039
|
-
// TODO after no-non-
|
|
1039
|
+
// TODO after no-non-undefined-assertion not ensure the logic
|
|
1040
1040
|
if (curLow !== undefined && childLow !== undefined) {
|
|
1041
1041
|
lowMap.set(cur, Math.min(curLow, childLow));
|
|
1042
1042
|
}
|
|
@@ -1062,7 +1062,7 @@ export abstract class AbstractGraph<
|
|
|
1062
1062
|
}
|
|
1063
1063
|
};
|
|
1064
1064
|
|
|
1065
|
-
dfs(root,
|
|
1065
|
+
dfs(root, undefined);
|
|
1066
1066
|
|
|
1067
1067
|
let SCCs: Map<number, VO[]> = new Map();
|
|
1068
1068
|
|
|
@@ -1170,9 +1170,9 @@ export abstract class AbstractGraph<
|
|
|
1170
1170
|
return true;
|
|
1171
1171
|
}
|
|
1172
1172
|
|
|
1173
|
-
protected _getVertex(vertexOrKey: VertexKey | VO): VO |
|
|
1173
|
+
protected _getVertex(vertexOrKey: VertexKey | VO): VO | undefined {
|
|
1174
1174
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
1175
|
-
return this._vertices.get(vertexKey) ||
|
|
1175
|
+
return this._vertices.get(vertexKey) || undefined;
|
|
1176
1176
|
}
|
|
1177
1177
|
|
|
1178
1178
|
protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey {
|