min-heap-typed 1.50.2 → 1.50.4
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/base/iterable-base.d.ts +6 -0
- package/dist/data-structures/binary-tree/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +43 -10
- package/dist/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +49 -11
- package/dist/data-structures/binary-tree/avl-tree.d.ts +29 -1
- package/dist/data-structures/binary-tree/avl-tree.js +33 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +46 -13
- package/dist/data-structures/binary-tree/bst.js +51 -20
- package/dist/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/data-structures/binary-tree/index.js +2 -1
- package/dist/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/data-structures/binary-tree/rb-tree.js +90 -24
- package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +200 -0
- package/dist/data-structures/binary-tree/tree-multi-map.js +399 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -78
- package/dist/data-structures/graph/abstract-graph.js +0 -189
- package/dist/data-structures/graph/directed-graph.d.ts +59 -0
- package/dist/data-structures/graph/directed-graph.js +105 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +60 -7
- package/dist/data-structures/graph/undirected-graph.js +126 -18
- package/dist/data-structures/hash/hash-map.d.ts +143 -23
- package/dist/data-structures/hash/hash-map.js +196 -62
- package/dist/data-structures/heap/heap.d.ts +29 -19
- package/dist/data-structures/heap/heap.js +29 -20
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
- package/dist/data-structures/linked-list/doubly-linked-list.js +83 -25
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +26 -3
- package/dist/data-structures/linked-list/singly-linked-list.js +34 -3
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix.js +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/data-structures/queue/deque.d.ts +95 -21
- package/dist/data-structures/queue/deque.js +100 -16
- package/dist/data-structures/queue/queue.d.ts +65 -45
- package/dist/data-structures/queue/queue.js +65 -45
- package/dist/data-structures/stack/stack.d.ts +36 -22
- package/dist/data-structures/stack/stack.js +36 -22
- package/dist/data-structures/tree/tree.d.ts +57 -3
- package/dist/data-structures/tree/tree.js +77 -11
- package/dist/data-structures/trie/trie.d.ts +100 -36
- package/dist/data-structures/trie/trie.js +115 -36
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/index.js +2 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +2 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +12 -0
- package/src/data-structures/binary-tree/{tree-multimap.ts → avl-tree-multi-map.ts} +59 -20
- package/src/data-structures/binary-tree/avl-tree.ts +37 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/data-structures/binary-tree/bst.ts +51 -19
- package/src/data-structures/binary-tree/index.ts +2 -1
- package/src/data-structures/binary-tree/rb-tree.ts +99 -28
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multi-map.ts +463 -0
- package/src/data-structures/graph/abstract-graph.ts +0 -211
- package/src/data-structures/graph/directed-graph.ts +122 -0
- package/src/data-structures/graph/undirected-graph.ts +143 -19
- package/src/data-structures/hash/hash-map.ts +228 -76
- package/src/data-structures/heap/heap.ts +31 -20
- package/src/data-structures/linked-list/doubly-linked-list.ts +96 -29
- package/src/data-structures/linked-list/singly-linked-list.ts +42 -6
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +118 -22
- package/src/data-structures/queue/queue.ts +68 -45
- package/src/data-structures/stack/stack.ts +39 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +131 -40
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +8 -0
- package/src/types/data-structures/binary-tree/index.ts +2 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +8 -0
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
- package/src/types/data-structures/binary-tree/tree-multimap.ts +0 -8
- /package/dist/types/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +0 -0
|
@@ -335,12 +335,71 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
335
335
|
* @return A boolean value
|
|
336
336
|
*/
|
|
337
337
|
isEmpty(): boolean;
|
|
338
|
+
/**
|
|
339
|
+
* Time Complexity: O(1)
|
|
340
|
+
* Space Complexity: O(1)
|
|
341
|
+
*/
|
|
342
|
+
/**
|
|
343
|
+
* Time Complexity: O(1)
|
|
344
|
+
* Space Complexity: O(1)
|
|
345
|
+
*
|
|
346
|
+
* The clear function resets the vertex map, in-edge map, and out-edge map.
|
|
347
|
+
*/
|
|
348
|
+
clear(): void;
|
|
338
349
|
/**
|
|
339
350
|
* The clone function creates a new DirectedGraph object with the same vertices and edges as the original.
|
|
340
351
|
*
|
|
341
352
|
* @return A new instance of the directedgraph class
|
|
342
353
|
*/
|
|
343
354
|
clone(): DirectedGraph<V, E, VO, EO>;
|
|
355
|
+
/**
|
|
356
|
+
* Time Complexity: O(V + E)
|
|
357
|
+
* Space Complexity: O(V)
|
|
358
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
359
|
+
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
360
|
+
*/
|
|
361
|
+
/**
|
|
362
|
+
* Time Complexity: O(V + E)
|
|
363
|
+
* Space Complexity: O(V)
|
|
364
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
365
|
+
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
366
|
+
*
|
|
367
|
+
* The function `tarjan` implements the Tarjan's algorithm to find strongly connected components in a
|
|
368
|
+
* graph.
|
|
369
|
+
* @returns The function `tarjan()` returns an object with three properties: `dfnMap`, `lowMap`, and
|
|
370
|
+
* `SCCs`.
|
|
371
|
+
*/
|
|
372
|
+
tarjan(): {
|
|
373
|
+
dfnMap: Map<VO, number>;
|
|
374
|
+
lowMap: Map<VO, number>;
|
|
375
|
+
SCCs: Map<number, VO[]>;
|
|
376
|
+
};
|
|
377
|
+
/**
|
|
378
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
379
|
+
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
380
|
+
*/
|
|
381
|
+
/**
|
|
382
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
383
|
+
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
384
|
+
*
|
|
385
|
+
* The function returns a map that associates each vertex object with its corresponding depth-first
|
|
386
|
+
* number.
|
|
387
|
+
* @returns A Map object with keys of type VO and values of type number.
|
|
388
|
+
*/
|
|
389
|
+
getDFNMap(): Map<VO, number>;
|
|
390
|
+
/**
|
|
391
|
+
* The function returns a Map object that contains the low values of each vertex in a Tarjan
|
|
392
|
+
* algorithm.
|
|
393
|
+
* @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
|
|
394
|
+
* type `number`.
|
|
395
|
+
*/
|
|
396
|
+
getLowMap(): Map<VO, number>;
|
|
397
|
+
/**
|
|
398
|
+
* The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
|
|
399
|
+
* algorithm.
|
|
400
|
+
* @returns a map where the keys are numbers and the values are arrays of VO objects.
|
|
401
|
+
*/
|
|
402
|
+
getSCCs(): Map<number, VO[]>;
|
|
344
403
|
/**
|
|
345
404
|
* Time Complexity: O(1)
|
|
346
405
|
* Space Complexity: O(1)
|
|
@@ -541,6 +541,21 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
541
541
|
isEmpty() {
|
|
542
542
|
return this.vertexMap.size === 0 && this.inEdgeMap.size === 0 && this.outEdgeMap.size === 0;
|
|
543
543
|
}
|
|
544
|
+
/**
|
|
545
|
+
* Time Complexity: O(1)
|
|
546
|
+
* Space Complexity: O(1)
|
|
547
|
+
*/
|
|
548
|
+
/**
|
|
549
|
+
* Time Complexity: O(1)
|
|
550
|
+
* Space Complexity: O(1)
|
|
551
|
+
*
|
|
552
|
+
* The clear function resets the vertex map, in-edge map, and out-edge map.
|
|
553
|
+
*/
|
|
554
|
+
clear() {
|
|
555
|
+
this._vertexMap = new Map();
|
|
556
|
+
this._inEdgeMap = new Map();
|
|
557
|
+
this._outEdgeMap = new Map();
|
|
558
|
+
}
|
|
544
559
|
/**
|
|
545
560
|
* The clone function creates a new DirectedGraph object with the same vertices and edges as the original.
|
|
546
561
|
*
|
|
@@ -553,6 +568,96 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
553
568
|
cloned.outEdgeMap = new Map(this.outEdgeMap);
|
|
554
569
|
return cloned;
|
|
555
570
|
}
|
|
571
|
+
/**
|
|
572
|
+
* Time Complexity: O(V + E)
|
|
573
|
+
* Space Complexity: O(V)
|
|
574
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
575
|
+
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
576
|
+
*/
|
|
577
|
+
/**
|
|
578
|
+
* Time Complexity: O(V + E)
|
|
579
|
+
* Space Complexity: O(V)
|
|
580
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
581
|
+
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
582
|
+
*
|
|
583
|
+
* The function `tarjan` implements the Tarjan's algorithm to find strongly connected components in a
|
|
584
|
+
* graph.
|
|
585
|
+
* @returns The function `tarjan()` returns an object with three properties: `dfnMap`, `lowMap`, and
|
|
586
|
+
* `SCCs`.
|
|
587
|
+
*/
|
|
588
|
+
tarjan() {
|
|
589
|
+
const dfnMap = new Map();
|
|
590
|
+
const lowMap = new Map();
|
|
591
|
+
const SCCs = new Map();
|
|
592
|
+
let time = 0;
|
|
593
|
+
const stack = [];
|
|
594
|
+
const inStack = new Set();
|
|
595
|
+
const dfs = (vertex) => {
|
|
596
|
+
dfnMap.set(vertex, time);
|
|
597
|
+
lowMap.set(vertex, time);
|
|
598
|
+
time++;
|
|
599
|
+
stack.push(vertex);
|
|
600
|
+
inStack.add(vertex);
|
|
601
|
+
const neighbors = this.getNeighbors(vertex);
|
|
602
|
+
for (const neighbor of neighbors) {
|
|
603
|
+
if (!dfnMap.has(neighbor)) {
|
|
604
|
+
dfs(neighbor);
|
|
605
|
+
lowMap.set(vertex, Math.min(lowMap.get(vertex), lowMap.get(neighbor)));
|
|
606
|
+
}
|
|
607
|
+
else if (inStack.has(neighbor)) {
|
|
608
|
+
lowMap.set(vertex, Math.min(lowMap.get(vertex), dfnMap.get(neighbor)));
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
if (dfnMap.get(vertex) === lowMap.get(vertex)) {
|
|
612
|
+
const SCC = [];
|
|
613
|
+
let poppedVertex;
|
|
614
|
+
do {
|
|
615
|
+
poppedVertex = stack.pop();
|
|
616
|
+
inStack.delete(poppedVertex);
|
|
617
|
+
SCC.push(poppedVertex);
|
|
618
|
+
} while (poppedVertex !== vertex);
|
|
619
|
+
SCCs.set(SCCs.size, SCC);
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
for (const vertex of this.vertexMap.values()) {
|
|
623
|
+
if (!dfnMap.has(vertex)) {
|
|
624
|
+
dfs(vertex);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
return { dfnMap, lowMap, SCCs };
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
631
|
+
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
632
|
+
*/
|
|
633
|
+
/**
|
|
634
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
635
|
+
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
636
|
+
*
|
|
637
|
+
* The function returns a map that associates each vertex object with its corresponding depth-first
|
|
638
|
+
* number.
|
|
639
|
+
* @returns A Map object with keys of type VO and values of type number.
|
|
640
|
+
*/
|
|
641
|
+
getDFNMap() {
|
|
642
|
+
return this.tarjan().dfnMap;
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* The function returns a Map object that contains the low values of each vertex in a Tarjan
|
|
646
|
+
* algorithm.
|
|
647
|
+
* @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
|
|
648
|
+
* type `number`.
|
|
649
|
+
*/
|
|
650
|
+
getLowMap() {
|
|
651
|
+
return this.tarjan().lowMap;
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
|
|
655
|
+
* algorithm.
|
|
656
|
+
* @returns a map where the keys are numbers and the values are arrays of VO objects.
|
|
657
|
+
*/
|
|
658
|
+
getSCCs() {
|
|
659
|
+
return this.tarjan().SCCs;
|
|
660
|
+
}
|
|
556
661
|
/**
|
|
557
662
|
* Time Complexity: O(1)
|
|
558
663
|
* Space Complexity: O(1)
|
|
@@ -19,7 +19,7 @@ export declare class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|
|
19
19
|
constructor(key: VertexKey, value?: V);
|
|
20
20
|
}
|
|
21
21
|
export declare class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
22
|
-
|
|
22
|
+
endpoints: [VertexKey, VertexKey];
|
|
23
23
|
/**
|
|
24
24
|
* The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
25
25
|
* value.
|
|
@@ -69,7 +69,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
69
69
|
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
70
70
|
* Space Complexity: O(1)
|
|
71
71
|
*
|
|
72
|
-
* The function `getEdge` returns the first edge that connects two
|
|
72
|
+
* The function `getEdge` returns the first edge that connects two endpoints, or undefined if no such edge exists.
|
|
73
73
|
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
74
74
|
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
75
75
|
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
@@ -89,7 +89,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
89
89
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
90
90
|
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
91
91
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
92
|
-
* @returns the removed edge (EO) if it exists, or undefined if either of the
|
|
92
|
+
* @returns the removed edge (EO) if it exists, or undefined if either of the endpoints (VO) does not exist.
|
|
93
93
|
*/
|
|
94
94
|
deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | undefined;
|
|
95
95
|
/**
|
|
@@ -100,7 +100,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
100
100
|
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
101
101
|
* Space Complexity: O(1)
|
|
102
102
|
*
|
|
103
|
-
* The function `deleteEdge` deletes an edge between two
|
|
103
|
+
* The function `deleteEdge` deletes an edge between two endpoints in a graph.
|
|
104
104
|
* @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
|
|
105
105
|
* either an edge object or a vertex key.
|
|
106
106
|
* @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
|
|
@@ -173,7 +173,7 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
173
173
|
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
174
174
|
* Space Complexity: O(|E|)
|
|
175
175
|
*
|
|
176
|
-
* The function "getNeighbors" returns an array of neighboring
|
|
176
|
+
* The function "getNeighbors" returns an array of neighboring endpoints for a given vertex or vertex ID.
|
|
177
177
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
178
178
|
* (`VertexKey`).
|
|
179
179
|
* @returns an array of vertexMap (VO[]).
|
|
@@ -187,10 +187,10 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
187
187
|
* Time Complexity: O(1)
|
|
188
188
|
* Space Complexity: O(1)
|
|
189
189
|
*
|
|
190
|
-
* The function "getEndsOfEdge" returns the
|
|
190
|
+
* The function "getEndsOfEdge" returns the endpoints at the ends of an edge if the edge exists in the graph, otherwise
|
|
191
191
|
* it returns undefined.
|
|
192
192
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
193
|
-
* @returns The function `getEndsOfEdge` returns an array containing two
|
|
193
|
+
* @returns The function `getEndsOfEdge` returns an array containing two endpoints `[VO, VO]` if the edge exists in the
|
|
194
194
|
* graph. If the edge does not exist, it returns `undefined`.
|
|
195
195
|
*/
|
|
196
196
|
getEndsOfEdge(edge: EO): [VO, VO] | undefined;
|
|
@@ -199,6 +199,17 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
199
199
|
* @return True if the graph is empty and false otherwise
|
|
200
200
|
*/
|
|
201
201
|
isEmpty(): boolean;
|
|
202
|
+
/**
|
|
203
|
+
* Time Complexity: O(1)
|
|
204
|
+
* Space Complexity: O(1)
|
|
205
|
+
*/
|
|
206
|
+
/**
|
|
207
|
+
* Time Complexity: O(1)
|
|
208
|
+
* Space Complexity: O(1)
|
|
209
|
+
*
|
|
210
|
+
* The clear function resets the vertex and edge maps to empty maps.
|
|
211
|
+
*/
|
|
212
|
+
clear(): void;
|
|
202
213
|
/**
|
|
203
214
|
* The clone function creates a new UndirectedGraph object and copies the
|
|
204
215
|
* vertexMap and edgeMap from this graph to the new one. This is done by
|
|
@@ -213,6 +224,48 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
213
224
|
* Time Complexity: O(1)
|
|
214
225
|
* Space Complexity: O(1)
|
|
215
226
|
*/
|
|
227
|
+
/**
|
|
228
|
+
* Time Complexity: O(V + E)
|
|
229
|
+
* Space Complexity: O(V)
|
|
230
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
231
|
+
* 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
|
|
232
|
+
*
|
|
233
|
+
* The function `tarjan` implements the Tarjan's algorithm to find bridges and cut vertices in a
|
|
234
|
+
* graph.
|
|
235
|
+
* @returns The function `tarjan()` returns an object with the following properties:
|
|
236
|
+
*/
|
|
237
|
+
tarjan(): {
|
|
238
|
+
dfnMap: Map<VO, number>;
|
|
239
|
+
lowMap: Map<VO, number>;
|
|
240
|
+
bridges: EO[];
|
|
241
|
+
cutVertices: VO[];
|
|
242
|
+
};
|
|
243
|
+
/**
|
|
244
|
+
* Time Complexity: O(V + E)
|
|
245
|
+
* Space Complexity: O(V)
|
|
246
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
247
|
+
* 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
|
|
248
|
+
*/
|
|
249
|
+
/**
|
|
250
|
+
* The function "getBridges" returns an array of bridges in a graph using the Tarjan's algorithm.
|
|
251
|
+
* @returns The function `getBridges()` is returning the bridges found using the Tarjan's algorithm.
|
|
252
|
+
*/
|
|
253
|
+
getBridges(): EO[];
|
|
254
|
+
/**
|
|
255
|
+
* The function "getCutVertices" returns an array of cut vertices using the Tarjan's algorithm.
|
|
256
|
+
* @returns the cut vertices found using the Tarjan's algorithm.
|
|
257
|
+
*/
|
|
258
|
+
getCutVertices(): VO[];
|
|
259
|
+
/**
|
|
260
|
+
* The function returns the dfnMap property of the result of the tarjan() function.
|
|
261
|
+
* @returns the `dfnMap` property of the result of calling the `tarjan()` function.
|
|
262
|
+
*/
|
|
263
|
+
getDFNMap(): Map<VO, number>;
|
|
264
|
+
/**
|
|
265
|
+
* The function returns the lowMap property of the result of the tarjan() function.
|
|
266
|
+
* @returns the lowMap property of the result of calling the tarjan() function.
|
|
267
|
+
*/
|
|
268
|
+
getLowMap(): Map<VO, number>;
|
|
216
269
|
/**
|
|
217
270
|
* Time Complexity: O(1)
|
|
218
271
|
* Space Complexity: O(1)
|
|
@@ -29,7 +29,7 @@ class UndirectedEdge extends abstract_graph_1.AbstractEdge {
|
|
|
29
29
|
*/
|
|
30
30
|
constructor(v1, v2, weight, value) {
|
|
31
31
|
super(weight, value);
|
|
32
|
-
this.
|
|
32
|
+
this.endpoints = [v1, v2];
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
exports.UndirectedEdge = UndirectedEdge;
|
|
@@ -80,7 +80,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
80
80
|
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
81
81
|
* Space Complexity: O(1)
|
|
82
82
|
*
|
|
83
|
-
* The function `getEdge` returns the first edge that connects two
|
|
83
|
+
* The function `getEdge` returns the first edge that connects two endpoints, or undefined if no such edge exists.
|
|
84
84
|
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
85
85
|
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
86
86
|
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
@@ -94,7 +94,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
94
94
|
const vertex1 = this._getVertex(v1);
|
|
95
95
|
const vertex2 = this._getVertex(v2);
|
|
96
96
|
if (vertex1 && vertex2) {
|
|
97
|
-
edgeMap = (_a = this._edgeMap.get(vertex1)) === null || _a === void 0 ? void 0 : _a.filter(e => e.
|
|
97
|
+
edgeMap = (_a = this._edgeMap.get(vertex1)) === null || _a === void 0 ? void 0 : _a.filter(e => e.endpoints.includes(vertex2.key));
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
return edgeMap ? edgeMap[0] || undefined : undefined;
|
|
@@ -111,7 +111,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
111
111
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
112
112
|
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
113
113
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
114
|
-
* @returns the removed edge (EO) if it exists, or undefined if either of the
|
|
114
|
+
* @returns the removed edge (EO) if it exists, or undefined if either of the endpoints (VO) does not exist.
|
|
115
115
|
*/
|
|
116
116
|
deleteEdgeBetween(v1, v2) {
|
|
117
117
|
const vertex1 = this._getVertex(v1);
|
|
@@ -122,11 +122,11 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
122
122
|
const v1Edges = this._edgeMap.get(vertex1);
|
|
123
123
|
let removed = undefined;
|
|
124
124
|
if (v1Edges) {
|
|
125
|
-
removed = (0, utils_1.arrayRemove)(v1Edges, (e) => e.
|
|
125
|
+
removed = (0, utils_1.arrayRemove)(v1Edges, (e) => e.endpoints.includes(vertex2.key))[0] || undefined;
|
|
126
126
|
}
|
|
127
127
|
const v2Edges = this._edgeMap.get(vertex2);
|
|
128
128
|
if (v2Edges) {
|
|
129
|
-
(0, utils_1.arrayRemove)(v2Edges, (e) => e.
|
|
129
|
+
(0, utils_1.arrayRemove)(v2Edges, (e) => e.endpoints.includes(vertex1.key));
|
|
130
130
|
}
|
|
131
131
|
return removed;
|
|
132
132
|
}
|
|
@@ -138,7 +138,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
138
138
|
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
139
139
|
* Space Complexity: O(1)
|
|
140
140
|
*
|
|
141
|
-
* The function `deleteEdge` deletes an edge between two
|
|
141
|
+
* The function `deleteEdge` deletes an edge between two endpoints in a graph.
|
|
142
142
|
* @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
|
|
143
143
|
* either an edge object or a vertex key.
|
|
144
144
|
* @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
|
|
@@ -159,8 +159,8 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
161
|
else {
|
|
162
|
-
oneSide = this._getVertex(edgeOrOneSideVertexKey.
|
|
163
|
-
otherSide = this._getVertex(edgeOrOneSideVertexKey.
|
|
162
|
+
oneSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[0]);
|
|
163
|
+
otherSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[1]);
|
|
164
164
|
}
|
|
165
165
|
if (oneSide && otherSide) {
|
|
166
166
|
return this.deleteEdgeBetween(oneSide, otherSide);
|
|
@@ -199,7 +199,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
199
199
|
const neighborEdges = this._edgeMap.get(neighbor);
|
|
200
200
|
if (neighborEdges) {
|
|
201
201
|
const restEdges = neighborEdges.filter(edge => {
|
|
202
|
-
return !edge.
|
|
202
|
+
return !edge.endpoints.includes(vertexKey);
|
|
203
203
|
});
|
|
204
204
|
this._edgeMap.set(neighbor, restEdges);
|
|
205
205
|
}
|
|
@@ -282,7 +282,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
282
282
|
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
283
283
|
* Space Complexity: O(|E|)
|
|
284
284
|
*
|
|
285
|
-
* The function "getNeighbors" returns an array of neighboring
|
|
285
|
+
* The function "getNeighbors" returns an array of neighboring endpoints for a given vertex or vertex ID.
|
|
286
286
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
287
287
|
* (`VertexKey`).
|
|
288
288
|
* @returns an array of vertexMap (VO[]).
|
|
@@ -293,7 +293,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
293
293
|
if (vertex) {
|
|
294
294
|
const neighborEdges = this.edgesOf(vertex);
|
|
295
295
|
for (const edge of neighborEdges) {
|
|
296
|
-
const neighbor = this._getVertex(edge.
|
|
296
|
+
const neighbor = this._getVertex(edge.endpoints.filter(e => e !== vertex.key)[0]);
|
|
297
297
|
if (neighbor) {
|
|
298
298
|
neighbors.push(neighbor);
|
|
299
299
|
}
|
|
@@ -309,18 +309,18 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
309
309
|
* Time Complexity: O(1)
|
|
310
310
|
* Space Complexity: O(1)
|
|
311
311
|
*
|
|
312
|
-
* The function "getEndsOfEdge" returns the
|
|
312
|
+
* The function "getEndsOfEdge" returns the endpoints at the ends of an edge if the edge exists in the graph, otherwise
|
|
313
313
|
* it returns undefined.
|
|
314
314
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
315
|
-
* @returns The function `getEndsOfEdge` returns an array containing two
|
|
315
|
+
* @returns The function `getEndsOfEdge` returns an array containing two endpoints `[VO, VO]` if the edge exists in the
|
|
316
316
|
* graph. If the edge does not exist, it returns `undefined`.
|
|
317
317
|
*/
|
|
318
318
|
getEndsOfEdge(edge) {
|
|
319
|
-
if (!this.hasEdge(edge.
|
|
319
|
+
if (!this.hasEdge(edge.endpoints[0], edge.endpoints[1])) {
|
|
320
320
|
return undefined;
|
|
321
321
|
}
|
|
322
|
-
const v1 = this._getVertex(edge.
|
|
323
|
-
const v2 = this._getVertex(edge.
|
|
322
|
+
const v1 = this._getVertex(edge.endpoints[0]);
|
|
323
|
+
const v2 = this._getVertex(edge.endpoints[1]);
|
|
324
324
|
if (v1 && v2) {
|
|
325
325
|
return [v1, v2];
|
|
326
326
|
}
|
|
@@ -335,6 +335,20 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
335
335
|
isEmpty() {
|
|
336
336
|
return this.vertexMap.size === 0 && this.edgeMap.size === 0;
|
|
337
337
|
}
|
|
338
|
+
/**
|
|
339
|
+
* Time Complexity: O(1)
|
|
340
|
+
* Space Complexity: O(1)
|
|
341
|
+
*/
|
|
342
|
+
/**
|
|
343
|
+
* Time Complexity: O(1)
|
|
344
|
+
* Space Complexity: O(1)
|
|
345
|
+
*
|
|
346
|
+
* The clear function resets the vertex and edge maps to empty maps.
|
|
347
|
+
*/
|
|
348
|
+
clear() {
|
|
349
|
+
this._vertexMap = new Map();
|
|
350
|
+
this._edgeMap = new Map();
|
|
351
|
+
}
|
|
338
352
|
/**
|
|
339
353
|
* The clone function creates a new UndirectedGraph object and copies the
|
|
340
354
|
* vertexMap and edgeMap from this graph to the new one. This is done by
|
|
@@ -354,6 +368,100 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
354
368
|
* Time Complexity: O(1)
|
|
355
369
|
* Space Complexity: O(1)
|
|
356
370
|
*/
|
|
371
|
+
/**
|
|
372
|
+
* Time Complexity: O(V + E)
|
|
373
|
+
* Space Complexity: O(V)
|
|
374
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
375
|
+
* 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
|
|
376
|
+
*
|
|
377
|
+
* The function `tarjan` implements the Tarjan's algorithm to find bridges and cut vertices in a
|
|
378
|
+
* graph.
|
|
379
|
+
* @returns The function `tarjan()` returns an object with the following properties:
|
|
380
|
+
*/
|
|
381
|
+
tarjan() {
|
|
382
|
+
const dfnMap = new Map();
|
|
383
|
+
const lowMap = new Map();
|
|
384
|
+
const bridges = [];
|
|
385
|
+
const cutVertices = [];
|
|
386
|
+
let time = 0;
|
|
387
|
+
const dfs = (vertex, parent) => {
|
|
388
|
+
dfnMap.set(vertex, time);
|
|
389
|
+
lowMap.set(vertex, time);
|
|
390
|
+
time++;
|
|
391
|
+
const neighbors = this.getNeighbors(vertex);
|
|
392
|
+
let childCount = 0;
|
|
393
|
+
for (const neighbor of neighbors) {
|
|
394
|
+
if (!dfnMap.has(neighbor)) {
|
|
395
|
+
childCount++;
|
|
396
|
+
dfs(neighbor, vertex);
|
|
397
|
+
lowMap.set(vertex, Math.min(lowMap.get(vertex), lowMap.get(neighbor)));
|
|
398
|
+
if (lowMap.get(neighbor) > dfnMap.get(vertex)) {
|
|
399
|
+
// Found a bridge
|
|
400
|
+
const edge = this.getEdge(vertex, neighbor);
|
|
401
|
+
if (edge) {
|
|
402
|
+
bridges.push(edge);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
if (parent !== undefined && lowMap.get(neighbor) >= dfnMap.get(vertex)) {
|
|
406
|
+
// Found an articulation point
|
|
407
|
+
cutVertices.push(vertex);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
else if (neighbor !== parent) {
|
|
411
|
+
lowMap.set(vertex, Math.min(lowMap.get(vertex), dfnMap.get(neighbor)));
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
if (parent === undefined && childCount > 1) {
|
|
415
|
+
// Special case for root in DFS tree
|
|
416
|
+
cutVertices.push(vertex);
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
for (const vertex of this.vertexMap.values()) {
|
|
420
|
+
if (!dfnMap.has(vertex)) {
|
|
421
|
+
dfs(vertex, undefined);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return {
|
|
425
|
+
dfnMap,
|
|
426
|
+
lowMap,
|
|
427
|
+
bridges,
|
|
428
|
+
cutVertices
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Time Complexity: O(V + E)
|
|
433
|
+
* Space Complexity: O(V)
|
|
434
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
435
|
+
* 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
|
|
436
|
+
*/
|
|
437
|
+
/**
|
|
438
|
+
* The function "getBridges" returns an array of bridges in a graph using the Tarjan's algorithm.
|
|
439
|
+
* @returns The function `getBridges()` is returning the bridges found using the Tarjan's algorithm.
|
|
440
|
+
*/
|
|
441
|
+
getBridges() {
|
|
442
|
+
return this.tarjan().bridges;
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* The function "getCutVertices" returns an array of cut vertices using the Tarjan's algorithm.
|
|
446
|
+
* @returns the cut vertices found using the Tarjan's algorithm.
|
|
447
|
+
*/
|
|
448
|
+
getCutVertices() {
|
|
449
|
+
return this.tarjan().cutVertices;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* The function returns the dfnMap property of the result of the tarjan() function.
|
|
453
|
+
* @returns the `dfnMap` property of the result of calling the `tarjan()` function.
|
|
454
|
+
*/
|
|
455
|
+
getDFNMap() {
|
|
456
|
+
return this.tarjan().dfnMap;
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* The function returns the lowMap property of the result of the tarjan() function.
|
|
460
|
+
* @returns the lowMap property of the result of calling the tarjan() function.
|
|
461
|
+
*/
|
|
462
|
+
getLowMap() {
|
|
463
|
+
return this.tarjan().lowMap;
|
|
464
|
+
}
|
|
357
465
|
/**
|
|
358
466
|
* Time Complexity: O(1)
|
|
359
467
|
* Space Complexity: O(1)
|
|
@@ -363,7 +471,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
363
471
|
* @returns a boolean value.
|
|
364
472
|
*/
|
|
365
473
|
_addEdge(edge) {
|
|
366
|
-
for (const end of edge.
|
|
474
|
+
for (const end of edge.endpoints) {
|
|
367
475
|
const endVertex = this._getVertex(end);
|
|
368
476
|
if (endVertex === undefined)
|
|
369
477
|
return false;
|