data-structure-typed 1.50.2 → 1.50.3
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/CHANGELOG.md +1 -1
- package/README.md +17 -15
- package/README_zh-CN.md +1 -1
- package/SPECIFICATION.md +56 -0
- package/SPONSOR-zh-CN.md +90 -0
- package/SPONSOR.md +54 -0
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +149 -155
- package/dist/cjs/data-structures/base/iterable-base.d.ts +6 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +29 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +33 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +46 -13
- package/dist/cjs/data-structures/binary-tree/bst.js +46 -13
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +73 -15
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +35 -2
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +38 -0
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +0 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js +0 -189
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +59 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +105 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +60 -7
- package/dist/cjs/data-structures/graph/undirected-graph.js +126 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +143 -23
- package/dist/cjs/data-structures/hash/hash-map.js +196 -62
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +29 -19
- package/dist/cjs/data-structures/heap/heap.js +29 -20
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +83 -25
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +26 -3
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +34 -3
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/cjs/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/cjs/data-structures/matrix/matrix.js +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +95 -21
- package/dist/cjs/data-structures/queue/deque.js +100 -16
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +65 -45
- package/dist/cjs/data-structures/queue/queue.js +65 -45
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +36 -22
- package/dist/cjs/data-structures/stack/stack.js +36 -22
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/cjs/data-structures/tree/tree.js +77 -11
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +100 -36
- package/dist/cjs/data-structures/trie/trie.js +115 -36
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +6 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +29 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +34 -2
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +46 -13
- package/dist/mjs/data-structures/binary-tree/bst.js +46 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +74 -16
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +35 -2
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +38 -1
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +0 -78
- package/dist/mjs/data-structures/graph/abstract-graph.js +0 -188
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +59 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +105 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +60 -7
- package/dist/mjs/data-structures/graph/undirected-graph.js +127 -19
- package/dist/mjs/data-structures/hash/hash-map.d.ts +143 -23
- package/dist/mjs/data-structures/hash/hash-map.js +204 -70
- package/dist/mjs/data-structures/heap/heap.d.ts +29 -19
- package/dist/mjs/data-structures/heap/heap.js +29 -20
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +86 -28
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +26 -3
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +36 -5
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/mjs/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/mjs/data-structures/matrix/matrix.js +1 -1
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/mjs/data-structures/queue/deque.d.ts +95 -21
- package/dist/mjs/data-structures/queue/deque.js +105 -21
- package/dist/mjs/data-structures/queue/queue.d.ts +65 -45
- package/dist/mjs/data-structures/queue/queue.js +65 -45
- package/dist/mjs/data-structures/stack/stack.d.ts +36 -22
- package/dist/mjs/data-structures/stack/stack.js +36 -22
- package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/mjs/data-structures/tree/tree.js +80 -14
- package/dist/mjs/data-structures/trie/trie.d.ts +100 -36
- package/dist/mjs/data-structures/trie/trie.js +118 -39
- package/dist/umd/data-structure-typed.js +1445 -595
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +59 -58
- package/src/data-structures/base/iterable-base.ts +12 -0
- 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 +46 -13
- package/src/data-structures/binary-tree/rb-tree.ts +79 -18
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +42 -3
- 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/test/performance/data-structures/graph/directed-graph.test.ts +1 -1
- package/test/unit/data-structures/graph/abstract-graph.test.ts +3 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +189 -253
- package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
- package/test/unit/data-structures/queue/queue.test.ts +2 -0
|
@@ -538,6 +538,21 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
538
538
|
isEmpty() {
|
|
539
539
|
return this.vertexMap.size === 0 && this.inEdgeMap.size === 0 && this.outEdgeMap.size === 0;
|
|
540
540
|
}
|
|
541
|
+
/**
|
|
542
|
+
* Time Complexity: O(1)
|
|
543
|
+
* Space Complexity: O(1)
|
|
544
|
+
*/
|
|
545
|
+
/**
|
|
546
|
+
* Time Complexity: O(1)
|
|
547
|
+
* Space Complexity: O(1)
|
|
548
|
+
*
|
|
549
|
+
* The clear function resets the vertex map, in-edge map, and out-edge map.
|
|
550
|
+
*/
|
|
551
|
+
clear() {
|
|
552
|
+
this._vertexMap = new Map();
|
|
553
|
+
this._inEdgeMap = new Map();
|
|
554
|
+
this._outEdgeMap = new Map();
|
|
555
|
+
}
|
|
541
556
|
/**
|
|
542
557
|
* The clone function creates a new DirectedGraph object with the same vertices and edges as the original.
|
|
543
558
|
*
|
|
@@ -550,6 +565,96 @@ export class DirectedGraph extends AbstractGraph {
|
|
|
550
565
|
cloned.outEdgeMap = new Map(this.outEdgeMap);
|
|
551
566
|
return cloned;
|
|
552
567
|
}
|
|
568
|
+
/**
|
|
569
|
+
* Time Complexity: O(V + E)
|
|
570
|
+
* Space Complexity: O(V)
|
|
571
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
572
|
+
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
573
|
+
*/
|
|
574
|
+
/**
|
|
575
|
+
* Time Complexity: O(V + E)
|
|
576
|
+
* Space Complexity: O(V)
|
|
577
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
578
|
+
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
579
|
+
*
|
|
580
|
+
* The function `tarjan` implements the Tarjan's algorithm to find strongly connected components in a
|
|
581
|
+
* graph.
|
|
582
|
+
* @returns The function `tarjan()` returns an object with three properties: `dfnMap`, `lowMap`, and
|
|
583
|
+
* `SCCs`.
|
|
584
|
+
*/
|
|
585
|
+
tarjan() {
|
|
586
|
+
const dfnMap = new Map();
|
|
587
|
+
const lowMap = new Map();
|
|
588
|
+
const SCCs = new Map();
|
|
589
|
+
let time = 0;
|
|
590
|
+
const stack = [];
|
|
591
|
+
const inStack = new Set();
|
|
592
|
+
const dfs = (vertex) => {
|
|
593
|
+
dfnMap.set(vertex, time);
|
|
594
|
+
lowMap.set(vertex, time);
|
|
595
|
+
time++;
|
|
596
|
+
stack.push(vertex);
|
|
597
|
+
inStack.add(vertex);
|
|
598
|
+
const neighbors = this.getNeighbors(vertex);
|
|
599
|
+
for (const neighbor of neighbors) {
|
|
600
|
+
if (!dfnMap.has(neighbor)) {
|
|
601
|
+
dfs(neighbor);
|
|
602
|
+
lowMap.set(vertex, Math.min(lowMap.get(vertex), lowMap.get(neighbor)));
|
|
603
|
+
}
|
|
604
|
+
else if (inStack.has(neighbor)) {
|
|
605
|
+
lowMap.set(vertex, Math.min(lowMap.get(vertex), dfnMap.get(neighbor)));
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
if (dfnMap.get(vertex) === lowMap.get(vertex)) {
|
|
609
|
+
const SCC = [];
|
|
610
|
+
let poppedVertex;
|
|
611
|
+
do {
|
|
612
|
+
poppedVertex = stack.pop();
|
|
613
|
+
inStack.delete(poppedVertex);
|
|
614
|
+
SCC.push(poppedVertex);
|
|
615
|
+
} while (poppedVertex !== vertex);
|
|
616
|
+
SCCs.set(SCCs.size, SCC);
|
|
617
|
+
}
|
|
618
|
+
};
|
|
619
|
+
for (const vertex of this.vertexMap.values()) {
|
|
620
|
+
if (!dfnMap.has(vertex)) {
|
|
621
|
+
dfs(vertex);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
return { dfnMap, lowMap, SCCs };
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
628
|
+
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
629
|
+
*/
|
|
630
|
+
/**
|
|
631
|
+
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
632
|
+
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
633
|
+
*
|
|
634
|
+
* The function returns a map that associates each vertex object with its corresponding depth-first
|
|
635
|
+
* number.
|
|
636
|
+
* @returns A Map object with keys of type VO and values of type number.
|
|
637
|
+
*/
|
|
638
|
+
getDFNMap() {
|
|
639
|
+
return this.tarjan().dfnMap;
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* The function returns a Map object that contains the low values of each vertex in a Tarjan
|
|
643
|
+
* algorithm.
|
|
644
|
+
* @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
|
|
645
|
+
* type `number`.
|
|
646
|
+
*/
|
|
647
|
+
getLowMap() {
|
|
648
|
+
return this.tarjan().lowMap;
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
|
|
652
|
+
* algorithm.
|
|
653
|
+
* @returns a map where the keys are numbers and the values are arrays of VO objects.
|
|
654
|
+
*/
|
|
655
|
+
getSCCs() {
|
|
656
|
+
return this.tarjan().SCCs;
|
|
657
|
+
}
|
|
553
658
|
/**
|
|
554
659
|
* Time Complexity: O(1)
|
|
555
660
|
* 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)
|
|
@@ -13,7 +13,7 @@ export class UndirectedVertex extends AbstractVertex {
|
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
export class UndirectedEdge extends AbstractEdge {
|
|
16
|
-
|
|
16
|
+
endpoints;
|
|
17
17
|
/**
|
|
18
18
|
* The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
|
|
19
19
|
* value.
|
|
@@ -26,7 +26,7 @@ export class UndirectedEdge extends AbstractEdge {
|
|
|
26
26
|
*/
|
|
27
27
|
constructor(v1, v2, weight, value) {
|
|
28
28
|
super(weight, value);
|
|
29
|
-
this.
|
|
29
|
+
this.endpoints = [v1, v2];
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
export class UndirectedGraph extends AbstractGraph {
|
|
@@ -77,7 +77,7 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
77
77
|
* Time Complexity: O(|E|), where |E| is the number of edgeMap incident to the given vertex.
|
|
78
78
|
* Space Complexity: O(1)
|
|
79
79
|
*
|
|
80
|
-
* The function `getEdge` returns the first edge that connects two
|
|
80
|
+
* The function `getEdge` returns the first edge that connects two endpoints, or undefined if no such edge exists.
|
|
81
81
|
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
82
82
|
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
83
83
|
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
@@ -90,7 +90,7 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
90
90
|
const vertex1 = this._getVertex(v1);
|
|
91
91
|
const vertex2 = this._getVertex(v2);
|
|
92
92
|
if (vertex1 && vertex2) {
|
|
93
|
-
edgeMap = this._edgeMap.get(vertex1)?.filter(e => e.
|
|
93
|
+
edgeMap = this._edgeMap.get(vertex1)?.filter(e => e.endpoints.includes(vertex2.key));
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
return edgeMap ? edgeMap[0] || undefined : undefined;
|
|
@@ -107,7 +107,7 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
107
107
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
108
108
|
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
109
109
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
110
|
-
* @returns the removed edge (EO) if it exists, or undefined if either of the
|
|
110
|
+
* @returns the removed edge (EO) if it exists, or undefined if either of the endpoints (VO) does not exist.
|
|
111
111
|
*/
|
|
112
112
|
deleteEdgeBetween(v1, v2) {
|
|
113
113
|
const vertex1 = this._getVertex(v1);
|
|
@@ -118,11 +118,11 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
118
118
|
const v1Edges = this._edgeMap.get(vertex1);
|
|
119
119
|
let removed = undefined;
|
|
120
120
|
if (v1Edges) {
|
|
121
|
-
removed = arrayRemove(v1Edges, (e) => e.
|
|
121
|
+
removed = arrayRemove(v1Edges, (e) => e.endpoints.includes(vertex2.key))[0] || undefined;
|
|
122
122
|
}
|
|
123
123
|
const v2Edges = this._edgeMap.get(vertex2);
|
|
124
124
|
if (v2Edges) {
|
|
125
|
-
arrayRemove(v2Edges, (e) => e.
|
|
125
|
+
arrayRemove(v2Edges, (e) => e.endpoints.includes(vertex1.key));
|
|
126
126
|
}
|
|
127
127
|
return removed;
|
|
128
128
|
}
|
|
@@ -134,7 +134,7 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
134
134
|
* Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
|
|
135
135
|
* Space Complexity: O(1)
|
|
136
136
|
*
|
|
137
|
-
* The function `deleteEdge` deletes an edge between two
|
|
137
|
+
* The function `deleteEdge` deletes an edge between two endpoints in a graph.
|
|
138
138
|
* @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be
|
|
139
139
|
* either an edge object or a vertex key.
|
|
140
140
|
* @param {VertexKey} [otherSideVertexKey] - The parameter `otherSideVertexKey` is an optional
|
|
@@ -155,8 +155,8 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
155
155
|
}
|
|
156
156
|
}
|
|
157
157
|
else {
|
|
158
|
-
oneSide = this._getVertex(edgeOrOneSideVertexKey.
|
|
159
|
-
otherSide = this._getVertex(edgeOrOneSideVertexKey.
|
|
158
|
+
oneSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[0]);
|
|
159
|
+
otherSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[1]);
|
|
160
160
|
}
|
|
161
161
|
if (oneSide && otherSide) {
|
|
162
162
|
return this.deleteEdgeBetween(oneSide, otherSide);
|
|
@@ -195,7 +195,7 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
195
195
|
const neighborEdges = this._edgeMap.get(neighbor);
|
|
196
196
|
if (neighborEdges) {
|
|
197
197
|
const restEdges = neighborEdges.filter(edge => {
|
|
198
|
-
return !edge.
|
|
198
|
+
return !edge.endpoints.includes(vertexKey);
|
|
199
199
|
});
|
|
200
200
|
this._edgeMap.set(neighbor, restEdges);
|
|
201
201
|
}
|
|
@@ -277,7 +277,7 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
277
277
|
* Time Complexity: O(|V| + |E|), where |V| is the number of vertexMap and |E| is the number of edgeMap.
|
|
278
278
|
* Space Complexity: O(|E|)
|
|
279
279
|
*
|
|
280
|
-
* The function "getNeighbors" returns an array of neighboring
|
|
280
|
+
* The function "getNeighbors" returns an array of neighboring endpoints for a given vertex or vertex ID.
|
|
281
281
|
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
282
282
|
* (`VertexKey`).
|
|
283
283
|
* @returns an array of vertexMap (VO[]).
|
|
@@ -288,7 +288,7 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
288
288
|
if (vertex) {
|
|
289
289
|
const neighborEdges = this.edgesOf(vertex);
|
|
290
290
|
for (const edge of neighborEdges) {
|
|
291
|
-
const neighbor = this._getVertex(edge.
|
|
291
|
+
const neighbor = this._getVertex(edge.endpoints.filter(e => e !== vertex.key)[0]);
|
|
292
292
|
if (neighbor) {
|
|
293
293
|
neighbors.push(neighbor);
|
|
294
294
|
}
|
|
@@ -304,18 +304,18 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
304
304
|
* Time Complexity: O(1)
|
|
305
305
|
* Space Complexity: O(1)
|
|
306
306
|
*
|
|
307
|
-
* The function "getEndsOfEdge" returns the
|
|
307
|
+
* The function "getEndsOfEdge" returns the endpoints at the ends of an edge if the edge exists in the graph, otherwise
|
|
308
308
|
* it returns undefined.
|
|
309
309
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
310
|
-
* @returns The function `getEndsOfEdge` returns an array containing two
|
|
310
|
+
* @returns The function `getEndsOfEdge` returns an array containing two endpoints `[VO, VO]` if the edge exists in the
|
|
311
311
|
* graph. If the edge does not exist, it returns `undefined`.
|
|
312
312
|
*/
|
|
313
313
|
getEndsOfEdge(edge) {
|
|
314
|
-
if (!this.hasEdge(edge.
|
|
314
|
+
if (!this.hasEdge(edge.endpoints[0], edge.endpoints[1])) {
|
|
315
315
|
return undefined;
|
|
316
316
|
}
|
|
317
|
-
const v1 = this._getVertex(edge.
|
|
318
|
-
const v2 = this._getVertex(edge.
|
|
317
|
+
const v1 = this._getVertex(edge.endpoints[0]);
|
|
318
|
+
const v2 = this._getVertex(edge.endpoints[1]);
|
|
319
319
|
if (v1 && v2) {
|
|
320
320
|
return [v1, v2];
|
|
321
321
|
}
|
|
@@ -330,6 +330,20 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
330
330
|
isEmpty() {
|
|
331
331
|
return this.vertexMap.size === 0 && this.edgeMap.size === 0;
|
|
332
332
|
}
|
|
333
|
+
/**
|
|
334
|
+
* Time Complexity: O(1)
|
|
335
|
+
* Space Complexity: O(1)
|
|
336
|
+
*/
|
|
337
|
+
/**
|
|
338
|
+
* Time Complexity: O(1)
|
|
339
|
+
* Space Complexity: O(1)
|
|
340
|
+
*
|
|
341
|
+
* The clear function resets the vertex and edge maps to empty maps.
|
|
342
|
+
*/
|
|
343
|
+
clear() {
|
|
344
|
+
this._vertexMap = new Map();
|
|
345
|
+
this._edgeMap = new Map();
|
|
346
|
+
}
|
|
333
347
|
/**
|
|
334
348
|
* The clone function creates a new UndirectedGraph object and copies the
|
|
335
349
|
* vertexMap and edgeMap from this graph to the new one. This is done by
|
|
@@ -349,6 +363,100 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
349
363
|
* Time Complexity: O(1)
|
|
350
364
|
* Space Complexity: O(1)
|
|
351
365
|
*/
|
|
366
|
+
/**
|
|
367
|
+
* Time Complexity: O(V + E)
|
|
368
|
+
* Space Complexity: O(V)
|
|
369
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
370
|
+
* 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
|
|
371
|
+
*
|
|
372
|
+
* The function `tarjan` implements the Tarjan's algorithm to find bridges and cut vertices in a
|
|
373
|
+
* graph.
|
|
374
|
+
* @returns The function `tarjan()` returns an object with the following properties:
|
|
375
|
+
*/
|
|
376
|
+
tarjan() {
|
|
377
|
+
const dfnMap = new Map();
|
|
378
|
+
const lowMap = new Map();
|
|
379
|
+
const bridges = [];
|
|
380
|
+
const cutVertices = [];
|
|
381
|
+
let time = 0;
|
|
382
|
+
const dfs = (vertex, parent) => {
|
|
383
|
+
dfnMap.set(vertex, time);
|
|
384
|
+
lowMap.set(vertex, time);
|
|
385
|
+
time++;
|
|
386
|
+
const neighbors = this.getNeighbors(vertex);
|
|
387
|
+
let childCount = 0;
|
|
388
|
+
for (const neighbor of neighbors) {
|
|
389
|
+
if (!dfnMap.has(neighbor)) {
|
|
390
|
+
childCount++;
|
|
391
|
+
dfs(neighbor, vertex);
|
|
392
|
+
lowMap.set(vertex, Math.min(lowMap.get(vertex), lowMap.get(neighbor)));
|
|
393
|
+
if (lowMap.get(neighbor) > dfnMap.get(vertex)) {
|
|
394
|
+
// Found a bridge
|
|
395
|
+
const edge = this.getEdge(vertex, neighbor);
|
|
396
|
+
if (edge) {
|
|
397
|
+
bridges.push(edge);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
if (parent !== undefined && lowMap.get(neighbor) >= dfnMap.get(vertex)) {
|
|
401
|
+
// Found an articulation point
|
|
402
|
+
cutVertices.push(vertex);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
else if (neighbor !== parent) {
|
|
406
|
+
lowMap.set(vertex, Math.min(lowMap.get(vertex), dfnMap.get(neighbor)));
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
if (parent === undefined && childCount > 1) {
|
|
410
|
+
// Special case for root in DFS tree
|
|
411
|
+
cutVertices.push(vertex);
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
for (const vertex of this.vertexMap.values()) {
|
|
415
|
+
if (!dfnMap.has(vertex)) {
|
|
416
|
+
dfs(vertex, undefined);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return {
|
|
420
|
+
dfnMap,
|
|
421
|
+
lowMap,
|
|
422
|
+
bridges,
|
|
423
|
+
cutVertices
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Time Complexity: O(V + E)
|
|
428
|
+
* Space Complexity: O(V)
|
|
429
|
+
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
430
|
+
* 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time
|
|
431
|
+
*/
|
|
432
|
+
/**
|
|
433
|
+
* The function "getBridges" returns an array of bridges in a graph using the Tarjan's algorithm.
|
|
434
|
+
* @returns The function `getBridges()` is returning the bridges found using the Tarjan's algorithm.
|
|
435
|
+
*/
|
|
436
|
+
getBridges() {
|
|
437
|
+
return this.tarjan().bridges;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* The function "getCutVertices" returns an array of cut vertices using the Tarjan's algorithm.
|
|
441
|
+
* @returns the cut vertices found using the Tarjan's algorithm.
|
|
442
|
+
*/
|
|
443
|
+
getCutVertices() {
|
|
444
|
+
return this.tarjan().cutVertices;
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* The function returns the dfnMap property of the result of the tarjan() function.
|
|
448
|
+
* @returns the `dfnMap` property of the result of calling the `tarjan()` function.
|
|
449
|
+
*/
|
|
450
|
+
getDFNMap() {
|
|
451
|
+
return this.tarjan().dfnMap;
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* The function returns the lowMap property of the result of the tarjan() function.
|
|
455
|
+
* @returns the lowMap property of the result of calling the tarjan() function.
|
|
456
|
+
*/
|
|
457
|
+
getLowMap() {
|
|
458
|
+
return this.tarjan().lowMap;
|
|
459
|
+
}
|
|
352
460
|
/**
|
|
353
461
|
* Time Complexity: O(1)
|
|
354
462
|
* Space Complexity: O(1)
|
|
@@ -358,7 +466,7 @@ export class UndirectedGraph extends AbstractGraph {
|
|
|
358
466
|
* @returns a boolean value.
|
|
359
467
|
*/
|
|
360
468
|
_addEdge(edge) {
|
|
361
|
-
for (const end of edge.
|
|
469
|
+
for (const end of edge.endpoints) {
|
|
362
470
|
const endVertex = this._getVertex(end);
|
|
363
471
|
if (endVertex === undefined)
|
|
364
472
|
return false;
|