directed-graph-typed 1.39.5 → 1.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/avl-tree.js +13 -14
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -27
- package/dist/data-structures/binary-tree/binary-tree.js +25 -46
- package/dist/data-structures/binary-tree/bst.d.ts +7 -7
- package/dist/data-structures/binary-tree/bst.js +16 -16
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -5
- package/dist/data-structures/binary-tree/rb-tree.js +5 -11
- package/dist/data-structures/binary-tree/segment-tree.d.ts +14 -30
- package/dist/data-structures/binary-tree/segment-tree.js +20 -68
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +7 -7
- package/dist/data-structures/binary-tree/tree-multiset.js +24 -24
- package/dist/data-structures/graph/abstract-graph.d.ts +16 -35
- package/dist/data-structures/graph/abstract-graph.js +18 -57
- package/dist/data-structures/graph/directed-graph.d.ts +16 -22
- package/dist/data-structures/graph/directed-graph.js +17 -35
- package/dist/data-structures/graph/map-graph.d.ts +13 -19
- package/dist/data-structures/graph/map-graph.js +15 -33
- package/dist/data-structures/graph/undirected-graph.d.ts +12 -19
- package/dist/data-structures/graph/undirected-graph.js +15 -28
- package/dist/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-map.js +0 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-set.js +0 -3
- package/dist/data-structures/hash/hash-map.d.ts +8 -14
- package/dist/data-structures/hash/hash-map.js +4 -22
- package/dist/data-structures/hash/hash-table.d.ts +10 -13
- package/dist/data-structures/hash/hash-table.js +8 -17
- package/dist/data-structures/heap/heap.d.ts +12 -6
- package/dist/data-structures/heap/heap.js +40 -22
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +34 -42
- package/dist/data-structures/linked-list/doubly-linked-list.js +67 -91
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +26 -32
- package/dist/data-structures/linked-list/singly-linked-list.js +64 -82
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +29 -10
- package/dist/data-structures/linked-list/skip-linked-list.js +62 -17
- package/dist/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +4 -4
- package/dist/data-structures/queue/deque.d.ts +8 -12
- package/dist/data-structures/queue/deque.js +31 -43
- package/dist/data-structures/queue/queue.d.ts +20 -5
- package/dist/data-structures/queue/queue.js +35 -18
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -7
- package/dist/data-structures/tree/tree.d.ts +3 -9
- package/dist/data-structures/tree/tree.js +3 -21
- package/dist/data-structures/trie/trie.d.ts +6 -12
- package/dist/data-structures/trie/trie.js +6 -24
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/interfaces/graph.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +15 -17
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
- package/src/data-structures/binary-tree/binary-tree.ts +35 -60
- package/src/data-structures/binary-tree/bst.ts +21 -22
- package/src/data-structures/binary-tree/rb-tree.ts +19 -27
- package/src/data-structures/binary-tree/segment-tree.ts +25 -92
- package/src/data-structures/binary-tree/tree-multiset.ts +26 -27
- package/src/data-structures/graph/abstract-graph.ts +42 -88
- package/src/data-structures/graph/directed-graph.ts +29 -55
- package/src/data-structures/graph/map-graph.ts +20 -45
- package/src/data-structures/graph/undirected-graph.ts +24 -41
- package/src/data-structures/hash/coordinate-map.ts +0 -4
- package/src/data-structures/hash/coordinate-set.ts +0 -4
- package/src/data-structures/hash/hash-map.ts +13 -37
- package/src/data-structures/hash/hash-table.ts +15 -27
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +58 -30
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +81 -115
- package/src/data-structures/linked-list/singly-linked-list.ts +76 -101
- package/src/data-structures/linked-list/skip-linked-list.ts +73 -25
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +38 -53
- package/src/data-structures/queue/queue.ts +38 -20
- package/src/data-structures/stack/stack.ts +13 -9
- package/src/data-structures/tree/tree.ts +7 -33
- package/src/data-structures/trie/trie.ts +14 -40
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/interfaces/graph.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -12,75 +12,42 @@ import {IGraph} from '../../interfaces';
|
|
|
12
12
|
import {Queue} from '../queue';
|
|
13
13
|
|
|
14
14
|
export abstract class AbstractVertex<V = any> {
|
|
15
|
+
key: VertexKey;
|
|
16
|
+
value: V | undefined;
|
|
17
|
+
|
|
15
18
|
/**
|
|
16
19
|
* The function is a protected constructor that takes an key and an optional value as parameters.
|
|
17
20
|
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
18
21
|
* used to uniquely identify the vertex object.
|
|
19
|
-
* @param {V} [
|
|
22
|
+
* @param {V} [value] - The parameter "value" is an optional parameter of type V. It is used to assign a value to the
|
|
20
23
|
* vertex. If no value is provided, it will be set to undefined.
|
|
21
24
|
*/
|
|
22
|
-
protected constructor(key: VertexKey,
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
private _key: VertexKey;
|
|
28
|
-
|
|
29
|
-
get key(): VertexKey {
|
|
30
|
-
return this._key;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
set key(v: VertexKey) {
|
|
34
|
-
this._key = v;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
private _val: V | undefined;
|
|
38
|
-
|
|
39
|
-
get val(): V | undefined {
|
|
40
|
-
return this._val;
|
|
25
|
+
protected constructor(key: VertexKey, value?: V) {
|
|
26
|
+
this.key = key;
|
|
27
|
+
this.value = value;
|
|
41
28
|
}
|
|
42
29
|
|
|
43
|
-
set val(value: V | undefined) {
|
|
44
|
-
this._val = value;
|
|
45
|
-
}
|
|
46
30
|
}
|
|
47
31
|
|
|
48
|
-
export abstract class AbstractEdge<
|
|
32
|
+
export abstract class AbstractEdge<E = any> {
|
|
33
|
+
value: E | undefined;
|
|
34
|
+
weight: number;
|
|
35
|
+
|
|
49
36
|
/**
|
|
50
37
|
* The above function is a protected constructor that initializes the weight, value, and hash code properties of an
|
|
51
38
|
* object.
|
|
52
39
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
|
|
53
40
|
* a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
|
|
54
41
|
* will be assigned.
|
|
55
|
-
* @param {VO} [
|
|
42
|
+
* @param {VO} [value] - The `value` parameter is of type `VO`, which means it can be any type. It is an optional parameter,
|
|
56
43
|
* meaning it can be omitted when creating an instance of the class.
|
|
57
44
|
*/
|
|
58
|
-
protected constructor(weight?: number,
|
|
59
|
-
this.
|
|
60
|
-
this.
|
|
45
|
+
protected constructor(weight?: number, value?: E) {
|
|
46
|
+
this.weight = weight !== undefined ? weight : 1;
|
|
47
|
+
this.value = value;
|
|
61
48
|
this._hashCode = uuidV4();
|
|
62
49
|
}
|
|
63
50
|
|
|
64
|
-
private _val: VO | undefined;
|
|
65
|
-
|
|
66
|
-
get val(): VO | undefined {
|
|
67
|
-
return this._val;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
set val(value: VO | undefined) {
|
|
71
|
-
this._val = value;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
private _weight: number;
|
|
75
|
-
|
|
76
|
-
get weight(): number {
|
|
77
|
-
return this._weight;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
set weight(v: number) {
|
|
81
|
-
this._weight = v;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
51
|
protected _hashCode: string;
|
|
85
52
|
|
|
86
53
|
get hashCode(): string {
|
|
@@ -91,15 +58,6 @@ export abstract class AbstractEdge<VO = any> {
|
|
|
91
58
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
92
59
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
93
60
|
*/
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* The function sets the value of the _hashCode property to the provided string.
|
|
97
|
-
* @param {string} v - The parameter "v" is of type string and represents the value that will be assigned to the
|
|
98
|
-
* "_hashCode" property.
|
|
99
|
-
*/
|
|
100
|
-
protected _setHashCode(v: string) {
|
|
101
|
-
this._hashCode = v;
|
|
102
|
-
}
|
|
103
61
|
}
|
|
104
62
|
|
|
105
63
|
export abstract class AbstractGraph<
|
|
@@ -107,9 +65,8 @@ export abstract class AbstractGraph<
|
|
|
107
65
|
E = any,
|
|
108
66
|
VO extends AbstractVertex<V> = AbstractVertex<V>,
|
|
109
67
|
EO extends AbstractEdge<E> = AbstractEdge<E>
|
|
110
|
-
> implements IGraph<V, E, VO, EO>
|
|
111
|
-
|
|
112
|
-
private _vertices: Map<VertexKey, VO> = new Map<VertexKey, VO>();
|
|
68
|
+
> implements IGraph<V, E, VO, EO> {
|
|
69
|
+
protected _vertices: Map<VertexKey, VO> = new Map<VertexKey, VO>();
|
|
113
70
|
|
|
114
71
|
get vertices(): Map<VertexKey, VO> {
|
|
115
72
|
return this._vertices;
|
|
@@ -119,9 +76,9 @@ export abstract class AbstractGraph<
|
|
|
119
76
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
120
77
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
121
78
|
* @param key
|
|
122
|
-
* @param
|
|
79
|
+
* @param value
|
|
123
80
|
*/
|
|
124
|
-
abstract createVertex(key: VertexKey,
|
|
81
|
+
abstract createVertex(key: VertexKey, value?: V): VO;
|
|
125
82
|
|
|
126
83
|
/**
|
|
127
84
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
@@ -129,9 +86,9 @@ export abstract class AbstractGraph<
|
|
|
129
86
|
* @param srcOrV1
|
|
130
87
|
* @param destOrV2
|
|
131
88
|
* @param weight
|
|
132
|
-
* @param
|
|
89
|
+
* @param value
|
|
133
90
|
*/
|
|
134
|
-
abstract createEdge(srcOrV1: VertexKey
|
|
91
|
+
abstract createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
|
|
135
92
|
|
|
136
93
|
abstract deleteEdge(edge: EO): EO | null;
|
|
137
94
|
|
|
@@ -170,13 +127,13 @@ export abstract class AbstractGraph<
|
|
|
170
127
|
|
|
171
128
|
addVertex(vertex: VO): boolean;
|
|
172
129
|
|
|
173
|
-
addVertex(key: VertexKey,
|
|
130
|
+
addVertex(key: VertexKey, value?: V): boolean;
|
|
174
131
|
|
|
175
|
-
addVertex(keyOrVertex: VertexKey | VO,
|
|
132
|
+
addVertex(keyOrVertex: VertexKey | VO, value?: V): boolean {
|
|
176
133
|
if (keyOrVertex instanceof AbstractVertex) {
|
|
177
134
|
return this._addVertexOnly(keyOrVertex);
|
|
178
135
|
} else {
|
|
179
|
-
const newVertex = this.createVertex(keyOrVertex,
|
|
136
|
+
const newVertex = this.createVertex(keyOrVertex, value);
|
|
180
137
|
return this._addVertexOnly(newVertex);
|
|
181
138
|
}
|
|
182
139
|
}
|
|
@@ -222,9 +179,9 @@ export abstract class AbstractGraph<
|
|
|
222
179
|
|
|
223
180
|
addEdge(edge: EO): boolean;
|
|
224
181
|
|
|
225
|
-
addEdge(src: VO | VertexKey, dest: VO | VertexKey, weight?: number,
|
|
182
|
+
addEdge(src: VO | VertexKey, dest: VO | VertexKey, weight?: number, value?: E): boolean;
|
|
226
183
|
|
|
227
|
-
addEdge(srcOrEdge: VO | VertexKey | EO, dest?: VO | VertexKey, weight?: number,
|
|
184
|
+
addEdge(srcOrEdge: VO | VertexKey | EO, dest?: VO | VertexKey, weight?: number, value?: E): boolean {
|
|
228
185
|
if (srcOrEdge instanceof AbstractEdge) {
|
|
229
186
|
return this._addEdgeOnly(srcOrEdge);
|
|
230
187
|
} else {
|
|
@@ -232,7 +189,7 @@ export abstract class AbstractGraph<
|
|
|
232
189
|
if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest))) return false;
|
|
233
190
|
if (srcOrEdge instanceof AbstractVertex) srcOrEdge = srcOrEdge.key;
|
|
234
191
|
if (dest instanceof AbstractVertex) dest = dest.key;
|
|
235
|
-
const newEdge = this.createEdge(srcOrEdge, dest, weight,
|
|
192
|
+
const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
|
|
236
193
|
return this._addEdgeOnly(newEdge);
|
|
237
194
|
} else {
|
|
238
195
|
throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
|
|
@@ -493,10 +450,10 @@ export abstract class AbstractGraph<
|
|
|
493
450
|
const getMinOfNoSeen = () => {
|
|
494
451
|
let min = Infinity;
|
|
495
452
|
let minV: VO | null = null;
|
|
496
|
-
for (const [key,
|
|
453
|
+
for (const [key, value] of distMap) {
|
|
497
454
|
if (!seen.has(key)) {
|
|
498
|
-
if (
|
|
499
|
-
min =
|
|
455
|
+
if (value < min) {
|
|
456
|
+
min = value;
|
|
500
457
|
minV = key;
|
|
501
458
|
}
|
|
502
459
|
}
|
|
@@ -556,14 +513,14 @@ export abstract class AbstractGraph<
|
|
|
556
513
|
}
|
|
557
514
|
|
|
558
515
|
getMinDist &&
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
}
|
|
516
|
+
distMap.forEach((d, v) => {
|
|
517
|
+
if (v !== srcVertex) {
|
|
518
|
+
if (d < minDist) {
|
|
519
|
+
minDist = d;
|
|
520
|
+
if (genPaths) minDest = v;
|
|
565
521
|
}
|
|
566
|
-
}
|
|
522
|
+
}
|
|
523
|
+
});
|
|
567
524
|
|
|
568
525
|
genPaths && getPaths(minDest);
|
|
569
526
|
|
|
@@ -625,8 +582,8 @@ export abstract class AbstractGraph<
|
|
|
625
582
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
626
583
|
}
|
|
627
584
|
|
|
628
|
-
const heap = new PriorityQueue<{key: number;
|
|
629
|
-
heap.add({key: 0,
|
|
585
|
+
const heap = new PriorityQueue<{ key: number; value: VO }>({comparator: (a, b) => a.key - b.key});
|
|
586
|
+
heap.add({key: 0, value: srcVertex});
|
|
630
587
|
|
|
631
588
|
distMap.set(srcVertex, 0);
|
|
632
589
|
preMap.set(srcVertex, null);
|
|
@@ -656,7 +613,7 @@ export abstract class AbstractGraph<
|
|
|
656
613
|
while (heap.size > 0) {
|
|
657
614
|
const curHeapNode = heap.poll();
|
|
658
615
|
const dist = curHeapNode?.key;
|
|
659
|
-
const cur = curHeapNode?.
|
|
616
|
+
const cur = curHeapNode?.value;
|
|
660
617
|
if (dist !== undefined) {
|
|
661
618
|
if (cur) {
|
|
662
619
|
seen.add(cur);
|
|
@@ -677,7 +634,7 @@ export abstract class AbstractGraph<
|
|
|
677
634
|
const distSrcToNeighbor = distMap.get(neighbor);
|
|
678
635
|
if (distSrcToNeighbor) {
|
|
679
636
|
if (dist + weight < distSrcToNeighbor) {
|
|
680
|
-
heap.add({key: dist + weight,
|
|
637
|
+
heap.add({key: dist + weight, value: neighbor});
|
|
681
638
|
preMap.set(neighbor, cur);
|
|
682
639
|
distMap.set(neighbor, dist + weight);
|
|
683
640
|
}
|
|
@@ -854,7 +811,7 @@ export abstract class AbstractGraph<
|
|
|
854
811
|
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
855
812
|
* path between vertices in the
|
|
856
813
|
*/
|
|
857
|
-
floyd(): {costs: number[][]; predecessor: (VO | null)[][]} {
|
|
814
|
+
floyd(): { costs: number[][]; predecessor: (VO | null)[][] } {
|
|
858
815
|
const idAndVertices = [...this._vertices];
|
|
859
816
|
const n = idAndVertices.length;
|
|
860
817
|
|
|
@@ -1040,7 +997,4 @@ export abstract class AbstractGraph<
|
|
|
1040
997
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
1041
998
|
}
|
|
1042
999
|
|
|
1043
|
-
protected _setVertices(value: Map<VertexKey, VO>) {
|
|
1044
|
-
this._vertices = value;
|
|
1045
|
-
}
|
|
1046
1000
|
}
|
|
@@ -15,15 +15,18 @@ export class DirectedVertex<V = any> extends AbstractVertex<V> {
|
|
|
15
15
|
* The constructor function initializes a vertex with an optional value.
|
|
16
16
|
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
17
17
|
* used to uniquely identify the vertex within a graph or data structure.
|
|
18
|
-
* @param {V} [
|
|
18
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It is used to initialize the value of the
|
|
19
19
|
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
|
20
20
|
*/
|
|
21
|
-
constructor(key: VertexKey,
|
|
22
|
-
super(key,
|
|
21
|
+
constructor(key: VertexKey, value?: V) {
|
|
22
|
+
super(key, value);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
27
|
+
src: VertexKey;
|
|
28
|
+
dest: VertexKey;
|
|
29
|
+
|
|
27
30
|
/**
|
|
28
31
|
* The constructor function initializes the source and destination vertices of an edge, along with an optional weight
|
|
29
32
|
* and value.
|
|
@@ -32,45 +35,24 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
|
32
35
|
* @param {VertexKey} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
|
|
33
36
|
* `VertexKey`, which is likely a unique identifier for a vertex in a graph.
|
|
34
37
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
35
|
-
* @param {E} [
|
|
38
|
+
* @param {E} [value] - The `value` parameter is an optional parameter of type `E`. It represents the value associated with
|
|
36
39
|
* the edge.
|
|
37
40
|
*/
|
|
38
|
-
constructor(src: VertexKey, dest: VertexKey, weight?: number,
|
|
39
|
-
super(weight,
|
|
40
|
-
this.
|
|
41
|
-
this.
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
private _src: VertexKey;
|
|
45
|
-
|
|
46
|
-
get src(): VertexKey {
|
|
47
|
-
return this._src;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
set src(v: VertexKey) {
|
|
51
|
-
this._src = v;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
private _dest: VertexKey;
|
|
55
|
-
|
|
56
|
-
get dest(): VertexKey {
|
|
57
|
-
return this._dest;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
set dest(v: VertexKey) {
|
|
61
|
-
this._dest = v;
|
|
41
|
+
constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E) {
|
|
42
|
+
super(weight, value);
|
|
43
|
+
this.src = src;
|
|
44
|
+
this.dest = dest;
|
|
62
45
|
}
|
|
63
46
|
}
|
|
64
47
|
|
|
65
48
|
export class DirectedGraph<
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
49
|
+
V = any,
|
|
50
|
+
E = any,
|
|
51
|
+
VO extends DirectedVertex<V> = DirectedVertex<V>,
|
|
52
|
+
EO extends DirectedEdge<E> = DirectedEdge<E>
|
|
53
|
+
>
|
|
71
54
|
extends AbstractGraph<V, E, VO, EO>
|
|
72
|
-
implements IGraph<V, E, VO, EO>
|
|
73
|
-
{
|
|
55
|
+
implements IGraph<V, E, VO, EO> {
|
|
74
56
|
/**
|
|
75
57
|
* The constructor function initializes an instance of a class.
|
|
76
58
|
*/
|
|
@@ -78,13 +60,13 @@ export class DirectedGraph<
|
|
|
78
60
|
super();
|
|
79
61
|
}
|
|
80
62
|
|
|
81
|
-
|
|
63
|
+
protected _outEdgeMap: Map<VO, EO[]> = new Map<VO, EO[]>();
|
|
82
64
|
|
|
83
65
|
get outEdgeMap(): Map<VO, EO[]> {
|
|
84
66
|
return this._outEdgeMap;
|
|
85
67
|
}
|
|
86
68
|
|
|
87
|
-
|
|
69
|
+
protected _inEdgeMap: Map<VO, EO[]> = new Map<VO, EO[]>();
|
|
88
70
|
|
|
89
71
|
get inEdgeMap(): Map<VO, EO[]> {
|
|
90
72
|
return this._inEdgeMap;
|
|
@@ -99,13 +81,13 @@ export class DirectedGraph<
|
|
|
99
81
|
* The function creates a new vertex with an optional value and returns it.
|
|
100
82
|
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is of type `VertexKey`, which
|
|
101
83
|
* could be a number or a string depending on how you want to identify your vertices.
|
|
102
|
-
* @param [
|
|
103
|
-
* it will be assigned to the '
|
|
84
|
+
* @param [value] - The 'value' parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
|
85
|
+
* it will be assigned to the 'value' property of the vertex. If no value is provided, the 'value' property will be
|
|
104
86
|
* assigned the same value as the 'key' parameter
|
|
105
87
|
* @returns a new instance of a DirectedVertex object, casted as type VO.
|
|
106
88
|
*/
|
|
107
|
-
createVertex(key: VertexKey,
|
|
108
|
-
return new DirectedVertex(key,
|
|
89
|
+
createVertex(key: VertexKey, value?: V): VO {
|
|
90
|
+
return new DirectedVertex(key, value ?? key) as VO;
|
|
109
91
|
}
|
|
110
92
|
|
|
111
93
|
/**
|
|
@@ -119,23 +101,23 @@ export class DirectedGraph<
|
|
|
119
101
|
* @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
|
|
120
102
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge. If no
|
|
121
103
|
* weight is provided, it defaults to 1.
|
|
122
|
-
* @param [
|
|
104
|
+
* @param [value] - The 'value' parameter is an optional value that can be assigned to the edge. It can be of any type and
|
|
123
105
|
* is used to store additional information or data associated with the edge.
|
|
124
106
|
* @returns a new instance of a DirectedEdge object, casted as type EO.
|
|
125
107
|
*/
|
|
126
|
-
createEdge(src: VertexKey, dest: VertexKey, weight?: number,
|
|
127
|
-
return new DirectedEdge(src, dest, weight ?? 1,
|
|
108
|
+
createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
|
|
109
|
+
return new DirectedEdge(src, dest, weight ?? 1, value) as EO;
|
|
128
110
|
}
|
|
129
111
|
|
|
130
112
|
/**
|
|
131
113
|
* The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
|
|
132
|
-
* @param {VO |
|
|
133
|
-
* @param {VO |
|
|
114
|
+
* @param {VO | VertexKey | null} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
|
|
115
|
+
* @param {VO | VertexKey | null} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
|
|
134
116
|
* destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `null` if the
|
|
135
117
|
* destination is not specified.
|
|
136
118
|
* @returns the first edge found between the source and destination vertices, or null if no such edge is found.
|
|
137
119
|
*/
|
|
138
|
-
getEdge(srcOrKey: VO |
|
|
120
|
+
getEdge(srcOrKey: VO | VertexKey | null, destOrKey: VO | VertexKey | null): EO | null {
|
|
139
121
|
let edges: EO[] = [];
|
|
140
122
|
|
|
141
123
|
if (srcOrKey !== null && destOrKey !== null) {
|
|
@@ -464,12 +446,4 @@ export class DirectedGraph<
|
|
|
464
446
|
return false;
|
|
465
447
|
}
|
|
466
448
|
}
|
|
467
|
-
|
|
468
|
-
protected _setOutEdgeMap(value: Map<VO, EO[]>) {
|
|
469
|
-
this._outEdgeMap = value;
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
protected _setInEdgeMap(value: Map<VO, EO[]>) {
|
|
473
|
-
this._inEdgeMap = value;
|
|
474
|
-
}
|
|
475
449
|
}
|
|
@@ -2,6 +2,9 @@ import {MapGraphCoordinate, VertexKey} from '../../types';
|
|
|
2
2
|
import {DirectedEdge, DirectedGraph, DirectedVertex} from './directed-graph';
|
|
3
3
|
|
|
4
4
|
export class MapVertex<V = any> extends DirectedVertex<V> {
|
|
5
|
+
lat: number;
|
|
6
|
+
long: number;
|
|
7
|
+
|
|
5
8
|
/**
|
|
6
9
|
* The constructor function initializes an object with an key, latitude, longitude, and an optional value.
|
|
7
10
|
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex.
|
|
@@ -11,33 +14,13 @@ export class MapVertex<V = any> extends DirectedVertex<V> {
|
|
|
11
14
|
* @param {number} long - The "long" parameter represents the longitude of a location. Longitude is a geographic
|
|
12
15
|
* coordinate that specifies the east-west position of a point on the Earth's surface. It is measured in degrees, with
|
|
13
16
|
* values ranging from -180 to 180.
|
|
14
|
-
* @param {V} [
|
|
17
|
+
* @param {V} [value] - The "value" parameter is an optional value of type V. It is not required to be provided when
|
|
15
18
|
* creating an instance of the class.
|
|
16
19
|
*/
|
|
17
|
-
constructor(key: VertexKey,
|
|
18
|
-
super(key,
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
private _lat: number;
|
|
24
|
-
|
|
25
|
-
get lat(): number {
|
|
26
|
-
return this._lat;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
set lat(value: number) {
|
|
30
|
-
this._lat = value;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
private _long: number;
|
|
34
|
-
|
|
35
|
-
get long(): number {
|
|
36
|
-
return this._long;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
set long(value: number) {
|
|
40
|
-
this._long = value;
|
|
20
|
+
constructor(key: VertexKey, value: V, lat: number, long: number) {
|
|
21
|
+
super(key, value);
|
|
22
|
+
this.lat = lat;
|
|
23
|
+
this.long = long;
|
|
41
24
|
}
|
|
42
25
|
}
|
|
43
26
|
|
|
@@ -49,11 +32,11 @@ export class MapEdge<E = any> extends DirectedEdge<E> {
|
|
|
49
32
|
* a graph.
|
|
50
33
|
* @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for an edge.
|
|
51
34
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
52
|
-
* @param {E} [
|
|
35
|
+
* @param {E} [value] - The "value" parameter is an optional parameter of type E. It is used to store additional
|
|
53
36
|
* information or data associated with the edge.
|
|
54
37
|
*/
|
|
55
|
-
constructor(src: VertexKey, dest: VertexKey, weight?: number,
|
|
56
|
-
super(src, dest, weight,
|
|
38
|
+
constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E) {
|
|
39
|
+
super(src, dest, weight, value);
|
|
57
40
|
}
|
|
58
41
|
}
|
|
59
42
|
|
|
@@ -78,39 +61,31 @@ export class MapGraph<
|
|
|
78
61
|
this._bottomRight = bottomRight;
|
|
79
62
|
}
|
|
80
63
|
|
|
81
|
-
|
|
64
|
+
protected _origin: MapGraphCoordinate = [0, 0];
|
|
82
65
|
|
|
83
66
|
get origin(): MapGraphCoordinate {
|
|
84
67
|
return this._origin;
|
|
85
68
|
}
|
|
86
69
|
|
|
87
|
-
|
|
88
|
-
this._origin = value;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
private _bottomRight: MapGraphCoordinate | undefined;
|
|
70
|
+
protected _bottomRight: MapGraphCoordinate | undefined;
|
|
92
71
|
|
|
93
72
|
get bottomRight(): MapGraphCoordinate | undefined {
|
|
94
73
|
return this._bottomRight;
|
|
95
74
|
}
|
|
96
75
|
|
|
97
|
-
set bottomRight(value: MapGraphCoordinate | undefined) {
|
|
98
|
-
this._bottomRight = value;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
76
|
/**
|
|
102
77
|
* The function creates a new vertex with the given key, value, latitude, and longitude.
|
|
103
78
|
* @param {VertexKey} key - The key parameter is the unique identifier for the vertex. It is of type VertexKey, which could
|
|
104
79
|
* be a string or a number depending on how you define it in your code.
|
|
105
|
-
* @param [
|
|
106
|
-
* is of type `V`, which means it should be of the same type as the `
|
|
80
|
+
* @param [value] - The `value` parameter is an optional value that can be assigned to the `value` property of the vertex. It
|
|
81
|
+
* is of type `V`, which means it should be of the same type as the `value` property of the vertex class `VO`.
|
|
107
82
|
* @param {number} lat - The `lat` parameter represents the latitude of the vertex. It is a number that specifies the
|
|
108
83
|
* position of the vertex on the Earth's surface in the north-south direction.
|
|
109
84
|
* @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
|
|
110
85
|
* @returns The method is returning a new instance of the `MapVertex` class, casted as type `VO`.
|
|
111
86
|
*/
|
|
112
|
-
override createVertex(key: VertexKey,
|
|
113
|
-
return new MapVertex(key,
|
|
87
|
+
override createVertex(key: VertexKey, value?: V, lat: number = this.origin[0], long: number = this.origin[1]): VO {
|
|
88
|
+
return new MapVertex(key, value, lat, long) as VO;
|
|
114
89
|
}
|
|
115
90
|
|
|
116
91
|
/**
|
|
@@ -121,11 +96,11 @@ export class MapGraph<
|
|
|
121
96
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. It
|
|
122
97
|
* is used to assign a numerical value to the edge, which can be used in algorithms such as shortest path algorithms.
|
|
123
98
|
* If the weight is not provided, it can be set to a default value or left undefined.
|
|
124
|
-
* @param [
|
|
99
|
+
* @param [value] - The `value` parameter is an optional value that can be assigned to the edge. It can be of any type,
|
|
125
100
|
* depending on the specific implementation of the `MapEdge` class.
|
|
126
101
|
* @returns a new instance of the `MapEdge` class, cast as type `EO`.
|
|
127
102
|
*/
|
|
128
|
-
override createEdge(src: VertexKey, dest: VertexKey, weight?: number,
|
|
129
|
-
return new MapEdge(src, dest, weight,
|
|
103
|
+
override createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
|
|
104
|
+
return new MapEdge(src, dest, weight, value) as EO;
|
|
130
105
|
}
|
|
131
106
|
}
|
|
@@ -15,15 +15,17 @@ export class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|
|
15
15
|
* The constructor function initializes a vertex with an optional value.
|
|
16
16
|
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
17
17
|
* used to uniquely identify the vertex within a graph or network.
|
|
18
|
-
* @param {V} [
|
|
18
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It is used to initialize the value of the
|
|
19
19
|
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
|
20
20
|
*/
|
|
21
|
-
constructor(key: VertexKey,
|
|
22
|
-
super(key,
|
|
21
|
+
constructor(key: VertexKey, value?: V) {
|
|
22
|
+
super(key, value);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
27
|
+
vertices: [VertexKey, VertexKey];
|
|
28
|
+
|
|
27
29
|
/**
|
|
28
30
|
* The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
29
31
|
* value.
|
|
@@ -31,34 +33,23 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
|
31
33
|
* @param {VertexKey} v2 - The parameter `v2` is a `VertexKey`, which represents the identifier of the second vertex in a
|
|
32
34
|
* graph edge.
|
|
33
35
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
34
|
-
* @param {E} [
|
|
36
|
+
* @param {E} [value] - The "value" parameter is an optional parameter of type E. It is used to store a value associated
|
|
35
37
|
* with the edge.
|
|
36
38
|
*/
|
|
37
|
-
constructor(v1: VertexKey, v2: VertexKey, weight?: number,
|
|
38
|
-
super(weight,
|
|
39
|
-
this.
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
private _vertices: [VertexKey, VertexKey];
|
|
43
|
-
|
|
44
|
-
get vertices() {
|
|
45
|
-
return this._vertices;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
set vertices(v: [VertexKey, VertexKey]) {
|
|
49
|
-
this._vertices = v;
|
|
39
|
+
constructor(v1: VertexKey, v2: VertexKey, weight?: number, value?: E) {
|
|
40
|
+
super(weight, value);
|
|
41
|
+
this.vertices = [v1, v2];
|
|
50
42
|
}
|
|
51
43
|
}
|
|
52
44
|
|
|
53
45
|
export class UndirectedGraph<
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
46
|
+
V = any,
|
|
47
|
+
E = any,
|
|
48
|
+
VO extends UndirectedVertex<V> = UndirectedVertex<V>,
|
|
49
|
+
EO extends UndirectedEdge<E> = UndirectedEdge<E>
|
|
50
|
+
>
|
|
59
51
|
extends AbstractGraph<V, E, VO, EO>
|
|
60
|
-
implements IGraph<V, E, VO, EO>
|
|
61
|
-
{
|
|
52
|
+
implements IGraph<V, E, VO, EO> {
|
|
62
53
|
/**
|
|
63
54
|
* The constructor initializes a new Map object to store edges.
|
|
64
55
|
*/
|
|
@@ -77,13 +68,13 @@ export class UndirectedGraph<
|
|
|
77
68
|
* The function creates a new vertex with an optional value and returns it.
|
|
78
69
|
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
|
|
79
70
|
* vertex from another in the graph.
|
|
80
|
-
* @param [
|
|
71
|
+
* @param [value] - The `value` parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
|
81
72
|
* it will be used as the value of the vertex. If no value is provided, the `key` parameter will be used as the value of
|
|
82
73
|
* the vertex.
|
|
83
74
|
* @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `VO`.
|
|
84
75
|
*/
|
|
85
|
-
override createVertex(key: VertexKey,
|
|
86
|
-
return new UndirectedVertex(key,
|
|
76
|
+
override createVertex(key: VertexKey, value?: VO['value']): VO {
|
|
77
|
+
return new UndirectedVertex(key, value ?? key) as VO;
|
|
87
78
|
}
|
|
88
79
|
|
|
89
80
|
/**
|
|
@@ -92,23 +83,23 @@ export class UndirectedGraph<
|
|
|
92
83
|
* @param {VertexKey} v2 - The parameter `v2` represents the second vertex of the edge.
|
|
93
84
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
94
85
|
* no weight is provided, it defaults to 1.
|
|
95
|
-
* @param [
|
|
86
|
+
* @param [value] - The `value` parameter is an optional value that can be assigned to the edge. It can be of any type and
|
|
96
87
|
* is used to store additional information or data associated with the edge.
|
|
97
88
|
* @returns a new instance of the `UndirectedEdge` class, which is casted as type `EO`.
|
|
98
89
|
*/
|
|
99
|
-
override createEdge(v1: VertexKey, v2: VertexKey, weight?: number,
|
|
100
|
-
return new UndirectedEdge(v1, v2, weight ?? 1,
|
|
90
|
+
override createEdge(v1: VertexKey, v2: VertexKey, weight?: number, value?: EO['value']): EO {
|
|
91
|
+
return new UndirectedEdge(v1, v2, weight ?? 1, value) as EO;
|
|
101
92
|
}
|
|
102
93
|
|
|
103
94
|
/**
|
|
104
95
|
* The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
|
|
105
|
-
* @param {VO |
|
|
96
|
+
* @param {VO | VertexKey | null} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
106
97
|
* object), `null`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
107
|
-
* @param {VO |
|
|
98
|
+
* @param {VO | VertexKey | null} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
108
99
|
* object), `null`, or `VertexKey` (vertex ID).
|
|
109
100
|
* @returns an edge (EO) or null.
|
|
110
101
|
*/
|
|
111
|
-
getEdge(v1: VO |
|
|
102
|
+
getEdge(v1: VO | VertexKey | null, v2: VO | VertexKey | null): EO | null {
|
|
112
103
|
let edges: EO[] | undefined = [];
|
|
113
104
|
|
|
114
105
|
if (v1 !== null && v2 !== null) {
|
|
@@ -265,12 +256,4 @@ export class UndirectedGraph<
|
|
|
265
256
|
}
|
|
266
257
|
return true;
|
|
267
258
|
}
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* The function sets the edges of a graph.
|
|
271
|
-
* @param v - A map where the keys are of type VO and the values are arrays of type EO.
|
|
272
|
-
*/
|
|
273
|
-
protected _setEdges(v: Map<VO, EO[]>) {
|
|
274
|
-
this._edges = v;
|
|
275
|
-
}
|
|
276
259
|
}
|