min-heap-typed 1.50.1 → 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 +120 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/data-structures/binary-tree/avl-tree.js +101 -72
- 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 +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/data-structures/binary-tree/bst.js +68 -76
- package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/data-structures/binary-tree/rb-tree.js +152 -99
- 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 +72 -58
- package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/data-structures/graph/abstract-graph.js +3 -189
- package/dist/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/data-structures/graph/directed-graph.js +131 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/data-structures/graph/undirected-graph.js +151 -18
- package/dist/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/data-structures/hash/hash-map.js +347 -78
- package/dist/data-structures/heap/heap.d.ts +95 -25
- package/dist/data-structures/heap/heap.js +95 -26
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- 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 +139 -35
- package/dist/data-structures/queue/deque.js +200 -62
- package/dist/data-structures/queue/queue.d.ts +103 -49
- package/dist/data-structures/queue/queue.js +111 -49
- package/dist/data-structures/stack/stack.d.ts +51 -21
- package/dist/data-structures/stack/stack.js +58 -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 +135 -34
- package/dist/data-structures/trie/trie.js +153 -33
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- 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 +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -40,6 +40,7 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
40
40
|
constructor();
|
|
41
41
|
protected _vertexMap: Map<VertexKey, VO>;
|
|
42
42
|
get vertexMap(): Map<VertexKey, VO>;
|
|
43
|
+
set vertexMap(v: Map<VertexKey, VO>);
|
|
43
44
|
/**
|
|
44
45
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
45
46
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
@@ -361,84 +362,6 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
361
362
|
costs: number[][];
|
|
362
363
|
predecessor: (VO | undefined)[][];
|
|
363
364
|
};
|
|
364
|
-
/**
|
|
365
|
-
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
366
|
-
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
367
|
-
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
368
|
-
* Tarjan can find cycles in directed or undirected graph
|
|
369
|
-
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
370
|
-
* Tarjan solve the bi-connected components of undirected graphs;
|
|
371
|
-
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
372
|
-
* /
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
376
|
-
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
377
|
-
*
|
|
378
|
-
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
379
|
-
* Tarjan can find cycles in directed or undirected graph
|
|
380
|
-
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
381
|
-
* Tarjan solve the bi-connected components of undirected graphs;
|
|
382
|
-
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
383
|
-
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
|
|
384
|
-
* strongly connected components (SCCs), and cycles in a graph.
|
|
385
|
-
* @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
|
|
386
|
-
* articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
|
|
387
|
-
* number of connected components in the graph.
|
|
388
|
-
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
|
|
389
|
-
* (edgeMap whose removal would increase the number of connected components in the graph).
|
|
390
|
-
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
|
|
391
|
-
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
|
|
392
|
-
* SCCs will not be calculated or returned.
|
|
393
|
-
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
|
|
394
|
-
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
|
|
395
|
-
* are arrays of vertexMap that form cycles within the SCCs.
|
|
396
|
-
* @returns The function `tarjan` returns an object with the following properties:
|
|
397
|
-
*/
|
|
398
|
-
tarjan(needCutVertexes?: boolean, needBridges?: boolean, needSCCs?: boolean, needCycles?: boolean): {
|
|
399
|
-
dfnMap: Map<VO, number>;
|
|
400
|
-
lowMap: Map<VO, number>;
|
|
401
|
-
bridges: EO[];
|
|
402
|
-
cutVertexes: VO[];
|
|
403
|
-
SCCs: Map<number, VO[]>;
|
|
404
|
-
cycles: Map<number, VO[]>;
|
|
405
|
-
};
|
|
406
|
-
/**
|
|
407
|
-
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
408
|
-
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
409
|
-
*/
|
|
410
|
-
/**
|
|
411
|
-
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
412
|
-
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
413
|
-
*
|
|
414
|
-
* The function returns a map that associates each vertex object with its corresponding depth-first
|
|
415
|
-
* number.
|
|
416
|
-
* @returns A Map object with keys of type VO and values of type number.
|
|
417
|
-
*/
|
|
418
|
-
getDFNMap(): Map<VO, number>;
|
|
419
|
-
/**
|
|
420
|
-
* The function returns a Map object that contains the low values of each vertex in a Tarjan
|
|
421
|
-
* algorithm.
|
|
422
|
-
* @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
|
|
423
|
-
* type `number`.
|
|
424
|
-
*/
|
|
425
|
-
getLowMap(): Map<VO, number>;
|
|
426
|
-
/**
|
|
427
|
-
* The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
|
|
428
|
-
* @returns an array of VO objects, specifically the cut vertexes.
|
|
429
|
-
*/
|
|
430
|
-
getCutVertexes(): VO[];
|
|
431
|
-
/**
|
|
432
|
-
* The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
|
|
433
|
-
* algorithm.
|
|
434
|
-
* @returns a map where the keys are numbers and the values are arrays of VO objects.
|
|
435
|
-
*/
|
|
436
|
-
getSCCs(): Map<number, VO[]>;
|
|
437
|
-
/**
|
|
438
|
-
* The function "getBridges" returns an array of bridges using the Tarjan algorithm.
|
|
439
|
-
* @returns the bridges found using the Tarjan algorithm.
|
|
440
|
-
*/
|
|
441
|
-
getBridges(): EO[];
|
|
442
365
|
/**
|
|
443
366
|
* O(V+E+C)
|
|
444
367
|
* O(V+C)
|
|
@@ -47,6 +47,9 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
47
47
|
get vertexMap() {
|
|
48
48
|
return this._vertexMap;
|
|
49
49
|
}
|
|
50
|
+
set vertexMap(v) {
|
|
51
|
+
this._vertexMap = v;
|
|
52
|
+
}
|
|
50
53
|
/**
|
|
51
54
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
52
55
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
@@ -817,195 +820,6 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
817
820
|
}
|
|
818
821
|
return { costs, predecessor };
|
|
819
822
|
}
|
|
820
|
-
/**
|
|
821
|
-
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
822
|
-
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
823
|
-
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
824
|
-
* Tarjan can find cycles in directed or undirected graph
|
|
825
|
-
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
826
|
-
* Tarjan solve the bi-connected components of undirected graphs;
|
|
827
|
-
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
828
|
-
* /
|
|
829
|
-
|
|
830
|
-
/**
|
|
831
|
-
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
|
|
832
|
-
* Space Complexity: O(V) - Linear space (Tarjan's algorithm).
|
|
833
|
-
*
|
|
834
|
-
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
|
|
835
|
-
* Tarjan can find cycles in directed or undirected graph
|
|
836
|
-
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
|
|
837
|
-
* Tarjan solve the bi-connected components of undirected graphs;
|
|
838
|
-
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
|
|
839
|
-
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
|
|
840
|
-
* strongly connected components (SCCs), and cycles in a graph.
|
|
841
|
-
* @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
|
|
842
|
-
* articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
|
|
843
|
-
* number of connected components in the graph.
|
|
844
|
-
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
|
|
845
|
-
* (edgeMap whose removal would increase the number of connected components in the graph).
|
|
846
|
-
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
|
|
847
|
-
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
|
|
848
|
-
* SCCs will not be calculated or returned.
|
|
849
|
-
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
|
|
850
|
-
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
|
|
851
|
-
* are arrays of vertexMap that form cycles within the SCCs.
|
|
852
|
-
* @returns The function `tarjan` returns an object with the following properties:
|
|
853
|
-
*/
|
|
854
|
-
tarjan(needCutVertexes = false, needBridges = false, needSCCs = true, needCycles = false) {
|
|
855
|
-
// !! in undirected graph we will not let child visit parent when dfs
|
|
856
|
-
// !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
|
|
857
|
-
// !! bridge: low(child) > dfn(cur)
|
|
858
|
-
const defaultConfig = false;
|
|
859
|
-
if (needCutVertexes === undefined)
|
|
860
|
-
needCutVertexes = defaultConfig;
|
|
861
|
-
if (needBridges === undefined)
|
|
862
|
-
needBridges = defaultConfig;
|
|
863
|
-
if (needSCCs === undefined)
|
|
864
|
-
needSCCs = defaultConfig;
|
|
865
|
-
if (needCycles === undefined)
|
|
866
|
-
needCycles = defaultConfig;
|
|
867
|
-
const dfnMap = new Map();
|
|
868
|
-
const lowMap = new Map();
|
|
869
|
-
const vertexMap = this._vertexMap;
|
|
870
|
-
vertexMap.forEach(v => {
|
|
871
|
-
dfnMap.set(v, -1);
|
|
872
|
-
lowMap.set(v, Infinity);
|
|
873
|
-
});
|
|
874
|
-
const [root] = vertexMap.values();
|
|
875
|
-
const cutVertexes = [];
|
|
876
|
-
const bridges = [];
|
|
877
|
-
let dfn = 0;
|
|
878
|
-
const dfs = (cur, parent) => {
|
|
879
|
-
dfn++;
|
|
880
|
-
dfnMap.set(cur, dfn);
|
|
881
|
-
lowMap.set(cur, dfn);
|
|
882
|
-
const neighbors = this.getNeighbors(cur);
|
|
883
|
-
let childCount = 0; // child in dfs tree not child in graph
|
|
884
|
-
for (const neighbor of neighbors) {
|
|
885
|
-
if (neighbor !== parent) {
|
|
886
|
-
if (dfnMap.get(neighbor) === -1) {
|
|
887
|
-
childCount++;
|
|
888
|
-
dfs(neighbor, cur);
|
|
889
|
-
}
|
|
890
|
-
const childLow = lowMap.get(neighbor);
|
|
891
|
-
const curLow = lowMap.get(cur);
|
|
892
|
-
// TODO after no-non-undefined-assertion not ensure the logic
|
|
893
|
-
if (curLow !== undefined && childLow !== undefined) {
|
|
894
|
-
lowMap.set(cur, Math.min(curLow, childLow));
|
|
895
|
-
}
|
|
896
|
-
const curFromMap = dfnMap.get(cur);
|
|
897
|
-
if (childLow !== undefined && curFromMap !== undefined) {
|
|
898
|
-
if (needCutVertexes) {
|
|
899
|
-
if ((cur === root && childCount >= 2) || (cur !== root && childLow >= curFromMap)) {
|
|
900
|
-
// todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
|
|
901
|
-
cutVertexes.push(cur);
|
|
902
|
-
}
|
|
903
|
-
}
|
|
904
|
-
if (needBridges) {
|
|
905
|
-
if (childLow > curFromMap) {
|
|
906
|
-
const edgeCurToNeighbor = this.getEdge(cur, neighbor);
|
|
907
|
-
if (edgeCurToNeighbor) {
|
|
908
|
-
bridges.push(edgeCurToNeighbor);
|
|
909
|
-
}
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
|
-
};
|
|
916
|
-
dfs(root, undefined);
|
|
917
|
-
let SCCs = new Map();
|
|
918
|
-
const getSCCs = () => {
|
|
919
|
-
const SCCs = new Map();
|
|
920
|
-
lowMap.forEach((low, vertex) => {
|
|
921
|
-
var _a;
|
|
922
|
-
if (!SCCs.has(low)) {
|
|
923
|
-
SCCs.set(low, [vertex]);
|
|
924
|
-
}
|
|
925
|
-
else {
|
|
926
|
-
(_a = SCCs.get(low)) === null || _a === void 0 ? void 0 : _a.push(vertex);
|
|
927
|
-
}
|
|
928
|
-
});
|
|
929
|
-
return SCCs;
|
|
930
|
-
};
|
|
931
|
-
if (needSCCs) {
|
|
932
|
-
SCCs = getSCCs();
|
|
933
|
-
}
|
|
934
|
-
const cycles = new Map();
|
|
935
|
-
if (needCycles) {
|
|
936
|
-
const visitedMap = new Map();
|
|
937
|
-
const stack = [];
|
|
938
|
-
const findCyclesDFS = (cur, parent) => {
|
|
939
|
-
visitedMap.set(cur, true);
|
|
940
|
-
stack.push(cur);
|
|
941
|
-
const neighbors = this.getNeighbors(cur);
|
|
942
|
-
for (const neighbor of neighbors) {
|
|
943
|
-
if (!visitedMap.get(neighbor)) {
|
|
944
|
-
findCyclesDFS(neighbor, cur);
|
|
945
|
-
}
|
|
946
|
-
else if (stack.includes(neighbor) && neighbor !== parent) {
|
|
947
|
-
const cycleStartIndex = stack.indexOf(neighbor);
|
|
948
|
-
const cycle = stack.slice(cycleStartIndex);
|
|
949
|
-
const cycleLow = Math.min(...cycle.map(v => dfnMap.get(v) || Infinity));
|
|
950
|
-
cycles.set(cycleLow, cycle);
|
|
951
|
-
}
|
|
952
|
-
}
|
|
953
|
-
stack.pop();
|
|
954
|
-
};
|
|
955
|
-
vertexMap.forEach(v => {
|
|
956
|
-
if (!visitedMap.get(v)) {
|
|
957
|
-
findCyclesDFS(v, undefined);
|
|
958
|
-
}
|
|
959
|
-
});
|
|
960
|
-
}
|
|
961
|
-
return { dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles };
|
|
962
|
-
}
|
|
963
|
-
/**
|
|
964
|
-
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
965
|
-
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
966
|
-
*/
|
|
967
|
-
/**
|
|
968
|
-
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
|
|
969
|
-
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
|
|
970
|
-
*
|
|
971
|
-
* The function returns a map that associates each vertex object with its corresponding depth-first
|
|
972
|
-
* number.
|
|
973
|
-
* @returns A Map object with keys of type VO and values of type number.
|
|
974
|
-
*/
|
|
975
|
-
getDFNMap() {
|
|
976
|
-
return this.tarjan(false, false, false, false).dfnMap;
|
|
977
|
-
}
|
|
978
|
-
/**
|
|
979
|
-
* The function returns a Map object that contains the low values of each vertex in a Tarjan
|
|
980
|
-
* algorithm.
|
|
981
|
-
* @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
|
|
982
|
-
* type `number`.
|
|
983
|
-
*/
|
|
984
|
-
getLowMap() {
|
|
985
|
-
return this.tarjan(false, false, false, false).lowMap;
|
|
986
|
-
}
|
|
987
|
-
/**
|
|
988
|
-
* The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
|
|
989
|
-
* @returns an array of VO objects, specifically the cut vertexes.
|
|
990
|
-
*/
|
|
991
|
-
getCutVertexes() {
|
|
992
|
-
return this.tarjan(true, false, false, false).cutVertexes;
|
|
993
|
-
}
|
|
994
|
-
/**
|
|
995
|
-
* The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
|
|
996
|
-
* algorithm.
|
|
997
|
-
* @returns a map where the keys are numbers and the values are arrays of VO objects.
|
|
998
|
-
*/
|
|
999
|
-
getSCCs() {
|
|
1000
|
-
return this.tarjan(false, false, true, false).SCCs;
|
|
1001
|
-
}
|
|
1002
|
-
/**
|
|
1003
|
-
* The function "getBridges" returns an array of bridges using the Tarjan algorithm.
|
|
1004
|
-
* @returns the bridges found using the Tarjan algorithm.
|
|
1005
|
-
*/
|
|
1006
|
-
getBridges() {
|
|
1007
|
-
return this.tarjan(false, true, false, false).bridges;
|
|
1008
|
-
}
|
|
1009
823
|
/**
|
|
1010
824
|
* O(V+E+C)
|
|
1011
825
|
* O(V+C)
|
|
@@ -41,8 +41,10 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
41
41
|
constructor();
|
|
42
42
|
protected _outEdgeMap: Map<VO, EO[]>;
|
|
43
43
|
get outEdgeMap(): Map<VO, EO[]>;
|
|
44
|
+
set outEdgeMap(v: Map<VO, EO[]>);
|
|
44
45
|
protected _inEdgeMap: Map<VO, EO[]>;
|
|
45
46
|
get inEdgeMap(): Map<VO, EO[]>;
|
|
47
|
+
set inEdgeMap(v: Map<VO, EO[]>);
|
|
46
48
|
/**
|
|
47
49
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
48
50
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
@@ -327,6 +329,77 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
327
329
|
* graph. If the edge does not exist, it returns `undefined`.
|
|
328
330
|
*/
|
|
329
331
|
getEndsOfEdge(edge: EO): [VO, VO] | undefined;
|
|
332
|
+
/**
|
|
333
|
+
* The isEmpty function checks if the graph is empty.
|
|
334
|
+
*
|
|
335
|
+
* @return A boolean value
|
|
336
|
+
*/
|
|
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;
|
|
349
|
+
/**
|
|
350
|
+
* The clone function creates a new DirectedGraph object with the same vertices and edges as the original.
|
|
351
|
+
*
|
|
352
|
+
* @return A new instance of the directedgraph class
|
|
353
|
+
*/
|
|
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[]>;
|
|
330
403
|
/**
|
|
331
404
|
* Time Complexity: O(1)
|
|
332
405
|
* Space Complexity: O(1)
|
|
@@ -47,9 +47,15 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
47
47
|
get outEdgeMap() {
|
|
48
48
|
return this._outEdgeMap;
|
|
49
49
|
}
|
|
50
|
+
set outEdgeMap(v) {
|
|
51
|
+
this._outEdgeMap = v;
|
|
52
|
+
}
|
|
50
53
|
get inEdgeMap() {
|
|
51
54
|
return this._inEdgeMap;
|
|
52
55
|
}
|
|
56
|
+
set inEdgeMap(v) {
|
|
57
|
+
this._inEdgeMap = v;
|
|
58
|
+
}
|
|
53
59
|
/**
|
|
54
60
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
55
61
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
@@ -527,6 +533,131 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
527
533
|
return undefined;
|
|
528
534
|
}
|
|
529
535
|
}
|
|
536
|
+
/**
|
|
537
|
+
* The isEmpty function checks if the graph is empty.
|
|
538
|
+
*
|
|
539
|
+
* @return A boolean value
|
|
540
|
+
*/
|
|
541
|
+
isEmpty() {
|
|
542
|
+
return this.vertexMap.size === 0 && this.inEdgeMap.size === 0 && this.outEdgeMap.size === 0;
|
|
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
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* The clone function creates a new DirectedGraph object with the same vertices and edges as the original.
|
|
561
|
+
*
|
|
562
|
+
* @return A new instance of the directedgraph class
|
|
563
|
+
*/
|
|
564
|
+
clone() {
|
|
565
|
+
const cloned = new DirectedGraph();
|
|
566
|
+
cloned.vertexMap = new Map(this.vertexMap);
|
|
567
|
+
cloned.inEdgeMap = new Map(this.inEdgeMap);
|
|
568
|
+
cloned.outEdgeMap = new Map(this.outEdgeMap);
|
|
569
|
+
return cloned;
|
|
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
|
+
}
|
|
530
661
|
/**
|
|
531
662
|
* Time Complexity: O(1)
|
|
532
663
|
* Space Complexity: O(1)
|
|
@@ -70,4 +70,12 @@ export declare class MapGraph<V = any, E = any, VO extends MapVertex<V> = MapVer
|
|
|
70
70
|
* @returns a new instance of the `MapEdge` class, cast as type `EO`.
|
|
71
71
|
*/
|
|
72
72
|
createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO;
|
|
73
|
+
/**
|
|
74
|
+
* The override function is used to override the default behavior of a function.
|
|
75
|
+
* In this case, we are overriding the clone() function from Graph<V, E>.
|
|
76
|
+
* The clone() function returns a new graph that is an exact copy of the original graph.
|
|
77
|
+
*
|
|
78
|
+
* @return A mapgraph<v, e, vo, eo>
|
|
79
|
+
*/
|
|
80
|
+
clone(): MapGraph<V, E, VO, EO>;
|
|
73
81
|
}
|
|
@@ -89,5 +89,19 @@ class MapGraph extends directed_graph_1.DirectedGraph {
|
|
|
89
89
|
createEdge(src, dest, weight, value) {
|
|
90
90
|
return new MapEdge(src, dest, weight, value);
|
|
91
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* The override function is used to override the default behavior of a function.
|
|
94
|
+
* In this case, we are overriding the clone() function from Graph<V, E>.
|
|
95
|
+
* The clone() function returns a new graph that is an exact copy of the original graph.
|
|
96
|
+
*
|
|
97
|
+
* @return A mapgraph<v, e, vo, eo>
|
|
98
|
+
*/
|
|
99
|
+
clone() {
|
|
100
|
+
const cloned = new MapGraph(this.originCoord, this.bottomRight);
|
|
101
|
+
cloned.vertexMap = new Map(this.vertexMap);
|
|
102
|
+
cloned.inEdgeMap = new Map(this.inEdgeMap);
|
|
103
|
+
cloned.outEdgeMap = new Map(this.outEdgeMap);
|
|
104
|
+
return cloned;
|
|
105
|
+
}
|
|
92
106
|
}
|
|
93
107
|
exports.MapGraph = MapGraph;
|