min-heap-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/dist/data-structures/base/iterable-base.d.ts +6 -0
- 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 +46 -13
- package/dist/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/data-structures/binary-tree/rb-tree.js +73 -15
- 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-multimap.d.ts +35 -2
- package/dist/data-structures/binary-tree/tree-multimap.js +38 -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/package.json +2 -2
- 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
|
@@ -820,195 +820,6 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
820
820
|
}
|
|
821
821
|
return { costs, predecessor };
|
|
822
822
|
}
|
|
823
|
-
/**
|
|
824
|
-
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
825
|
-
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
826
|
-
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
827
|
-
* Tarjan can find cycles in directed or undirected graph
|
|
828
|
-
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
829
|
-
* Tarjan solve the bi-connected components of undirected graphs;
|
|
830
|
-
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
831
|
-
* /
|
|
832
|
-
|
|
833
|
-
/**
|
|
834
|
-
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
835
|
-
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
836
|
-
*
|
|
837
|
-
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
838
|
-
* Tarjan can find cycles in directed or undirected graph
|
|
839
|
-
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
840
|
-
* Tarjan solve the bi-connected components of undirected graphs;
|
|
841
|
-
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
842
|
-
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
|
|
843
|
-
* strongly connected components (SCCs), and cycles in a graph.
|
|
844
|
-
* @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
|
|
845
|
-
* articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
|
|
846
|
-
* number of connected components in the graph.
|
|
847
|
-
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
|
|
848
|
-
* (edgeMap whose removal would increase the number of connected components in the graph).
|
|
849
|
-
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
|
|
850
|
-
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
|
|
851
|
-
* SCCs will not be calculated or returned.
|
|
852
|
-
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
|
|
853
|
-
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
|
|
854
|
-
* are arrays of vertexMap that form cycles within the SCCs.
|
|
855
|
-
* @returns The function `tarjan` returns an object with the following properties:
|
|
856
|
-
*/
|
|
857
|
-
tarjan(needCutVertexes = false, needBridges = false, needSCCs = true, needCycles = false) {
|
|
858
|
-
// !! in undirected graph we will not let child visit parent when dfs
|
|
859
|
-
// !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
|
|
860
|
-
// !! bridge: low(child) > dfn(cur)
|
|
861
|
-
const defaultConfig = false;
|
|
862
|
-
if (needCutVertexes === undefined)
|
|
863
|
-
needCutVertexes = defaultConfig;
|
|
864
|
-
if (needBridges === undefined)
|
|
865
|
-
needBridges = defaultConfig;
|
|
866
|
-
if (needSCCs === undefined)
|
|
867
|
-
needSCCs = defaultConfig;
|
|
868
|
-
if (needCycles === undefined)
|
|
869
|
-
needCycles = defaultConfig;
|
|
870
|
-
const dfnMap = new Map();
|
|
871
|
-
const lowMap = new Map();
|
|
872
|
-
const vertexMap = this._vertexMap;
|
|
873
|
-
vertexMap.forEach(v => {
|
|
874
|
-
dfnMap.set(v, -1);
|
|
875
|
-
lowMap.set(v, Infinity);
|
|
876
|
-
});
|
|
877
|
-
const [root] = vertexMap.values();
|
|
878
|
-
const cutVertexes = [];
|
|
879
|
-
const bridges = [];
|
|
880
|
-
let dfn = 0;
|
|
881
|
-
const dfs = (cur, parent) => {
|
|
882
|
-
dfn++;
|
|
883
|
-
dfnMap.set(cur, dfn);
|
|
884
|
-
lowMap.set(cur, dfn);
|
|
885
|
-
const neighbors = this.getNeighbors(cur);
|
|
886
|
-
let childCount = 0; // child in dfs tree not child in graph
|
|
887
|
-
for (const neighbor of neighbors) {
|
|
888
|
-
if (neighbor !== parent) {
|
|
889
|
-
if (dfnMap.get(neighbor) === -1) {
|
|
890
|
-
childCount++;
|
|
891
|
-
dfs(neighbor, cur);
|
|
892
|
-
}
|
|
893
|
-
const childLow = lowMap.get(neighbor);
|
|
894
|
-
const curLow = lowMap.get(cur);
|
|
895
|
-
// TODO after no-non-undefined-assertion not ensure the logic
|
|
896
|
-
if (curLow !== undefined && childLow !== undefined) {
|
|
897
|
-
lowMap.set(cur, Math.min(curLow, childLow));
|
|
898
|
-
}
|
|
899
|
-
const curFromMap = dfnMap.get(cur);
|
|
900
|
-
if (childLow !== undefined && curFromMap !== undefined) {
|
|
901
|
-
if (needCutVertexes) {
|
|
902
|
-
if ((cur === root && childCount >= 2) || (cur !== root && childLow >= curFromMap)) {
|
|
903
|
-
// todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
|
|
904
|
-
cutVertexes.push(cur);
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
if (needBridges) {
|
|
908
|
-
if (childLow > curFromMap) {
|
|
909
|
-
const edgeCurToNeighbor = this.getEdge(cur, neighbor);
|
|
910
|
-
if (edgeCurToNeighbor) {
|
|
911
|
-
bridges.push(edgeCurToNeighbor);
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
|
-
};
|
|
919
|
-
dfs(root, undefined);
|
|
920
|
-
let SCCs = new Map();
|
|
921
|
-
const getSCCs = () => {
|
|
922
|
-
const SCCs = new Map();
|
|
923
|
-
lowMap.forEach((low, vertex) => {
|
|
924
|
-
var _a;
|
|
925
|
-
if (!SCCs.has(low)) {
|
|
926
|
-
SCCs.set(low, [vertex]);
|
|
927
|
-
}
|
|
928
|
-
else {
|
|
929
|
-
(_a = SCCs.get(low)) === null || _a === void 0 ? void 0 : _a.push(vertex);
|
|
930
|
-
}
|
|
931
|
-
});
|
|
932
|
-
return SCCs;
|
|
933
|
-
};
|
|
934
|
-
if (needSCCs) {
|
|
935
|
-
SCCs = getSCCs();
|
|
936
|
-
}
|
|
937
|
-
const cycles = new Map();
|
|
938
|
-
if (needCycles) {
|
|
939
|
-
const visitedMap = new Map();
|
|
940
|
-
const stack = [];
|
|
941
|
-
const findCyclesDFS = (cur, parent) => {
|
|
942
|
-
visitedMap.set(cur, true);
|
|
943
|
-
stack.push(cur);
|
|
944
|
-
const neighbors = this.getNeighbors(cur);
|
|
945
|
-
for (const neighbor of neighbors) {
|
|
946
|
-
if (!visitedMap.get(neighbor)) {
|
|
947
|
-
findCyclesDFS(neighbor, cur);
|
|
948
|
-
}
|
|
949
|
-
else if (stack.includes(neighbor) && neighbor !== parent) {
|
|
950
|
-
const cycleStartIndex = stack.indexOf(neighbor);
|
|
951
|
-
const cycle = stack.slice(cycleStartIndex);
|
|
952
|
-
const cycleLow = Math.min(...cycle.map(v => dfnMap.get(v) || Infinity));
|
|
953
|
-
cycles.set(cycleLow, cycle);
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
stack.pop();
|
|
957
|
-
};
|
|
958
|
-
vertexMap.forEach(v => {
|
|
959
|
-
if (!visitedMap.get(v)) {
|
|
960
|
-
findCyclesDFS(v, undefined);
|
|
961
|
-
}
|
|
962
|
-
});
|
|
963
|
-
}
|
|
964
|
-
return { dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles };
|
|
965
|
-
}
|
|
966
|
-
/**
|
|
967
|
-
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
968
|
-
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
969
|
-
*/
|
|
970
|
-
/**
|
|
971
|
-
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
972
|
-
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
973
|
-
*
|
|
974
|
-
* The function returns a map that associates each vertex object with its corresponding depth-first
|
|
975
|
-
* number.
|
|
976
|
-
* @returns A Map object with keys of type VO and values of type number.
|
|
977
|
-
*/
|
|
978
|
-
getDFNMap() {
|
|
979
|
-
return this.tarjan(false, false, false, false).dfnMap;
|
|
980
|
-
}
|
|
981
|
-
/**
|
|
982
|
-
* The function returns a Map object that contains the low values of each vertex in a Tarjan
|
|
983
|
-
* algorithm.
|
|
984
|
-
* @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
|
|
985
|
-
* type `number`.
|
|
986
|
-
*/
|
|
987
|
-
getLowMap() {
|
|
988
|
-
return this.tarjan(false, false, false, false).lowMap;
|
|
989
|
-
}
|
|
990
|
-
/**
|
|
991
|
-
* The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
|
|
992
|
-
* @returns an array of VO objects, specifically the cut vertexes.
|
|
993
|
-
*/
|
|
994
|
-
getCutVertexes() {
|
|
995
|
-
return this.tarjan(true, false, false, false).cutVertexes;
|
|
996
|
-
}
|
|
997
|
-
/**
|
|
998
|
-
* The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
|
|
999
|
-
* algorithm.
|
|
1000
|
-
* @returns a map where the keys are numbers and the values are arrays of VO objects.
|
|
1001
|
-
*/
|
|
1002
|
-
getSCCs() {
|
|
1003
|
-
return this.tarjan(false, false, true, false).SCCs;
|
|
1004
|
-
}
|
|
1005
|
-
/**
|
|
1006
|
-
* The function "getBridges" returns an array of bridges using the Tarjan algorithm.
|
|
1007
|
-
* @returns the bridges found using the Tarjan algorithm.
|
|
1008
|
-
*/
|
|
1009
|
-
getBridges() {
|
|
1010
|
-
return this.tarjan(false, true, false, false).bridges;
|
|
1011
|
-
}
|
|
1012
823
|
/**
|
|
1013
824
|
* O(V+E+C)
|
|
1014
825
|
* O(V+C)
|
|
@@ -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)
|