[project] codebase reformated

This commit is contained in:
Revone 2023-11-06 19:44:01 +08:00
parent be38422de9
commit 8a518f95b7
10 changed files with 1189 additions and 92 deletions

View file

@ -1,6 +1,6 @@
{
"name": "data-structure-typed",
"version": "1.42.2",
"version": "1.42.3",
"description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
"main": "dist/cjs/src/index.js",
"module": "dist/mjs/src/index.js",

View file

@ -108,7 +108,8 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
* @template N - The type of the binary tree's nodes.
*/
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
implements IBinaryTree<V, N> {
implements IBinaryTree<V, N>
{
iterationType: IterationType = IterationType.ITERATIVE;
/**
@ -140,8 +141,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
return this._size;
}
protected defaultOneParamCallback = (node: N) => node.key;
/**
* Creates a new instance of BinaryTreeNode with the given key and value.
* @param {BTNKey} key - The key for the new node.
@ -390,7 +389,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
return -1;
}
const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
let maxHeight = 0;
while (stack.length > 0) {
@ -850,21 +849,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
beginRoot?: BTNKey | N | null,
iterationType?: IterationType,
includeNull?: false
): ReturnType<C>[]
): ReturnType<C>[];
subTreeTraverse<C extends BTNCallback<N>>(
callback?: C,
beginRoot?: BTNKey | N | null,
iterationType?: IterationType,
includeNull?: undefined
): ReturnType<C>[]
): ReturnType<C>[];
subTreeTraverse<C extends BTNCallback<N | null>>(
callback?: C,
beginRoot?: BTNKey | N | null,
iterationType?: IterationType,
includeNull?: true
): ReturnType<C>[]
): ReturnType<C>[];
/**
* The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
@ -908,7 +907,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
_traverse(beginRoot);
} else {
const stack: (N| null)[] = [beginRoot];
const stack: (N | null)[] = [beginRoot];
while (stack.length > 0) {
const cur = stack.pop();
@ -933,7 +932,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
beginRoot?: N | null,
iterationType?: IterationType,
includeNull?: false
): ReturnType<C>[]
): ReturnType<C>[];
dfs<C extends BTNCallback<N>>(
callback?: C,
@ -941,7 +940,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
beginRoot?: N | null,
iterationType?: IterationType,
includeNull?: undefined
): ReturnType<C>[]
): ReturnType<C>[];
dfs<C extends BTNCallback<N | null>>(
callback?: C,
@ -949,7 +948,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
beginRoot?: N | null,
iterationType?: IterationType,
includeNull?: true
): ReturnType<C>[]
): ReturnType<C>[];
/**
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
@ -1019,7 +1018,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
_traverse(beginRoot);
} else {
// 0: visit, 1: print
const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
while (stack.length > 0) {
const cur = stack.pop();
@ -1066,21 +1065,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
beginRoot?: N | null,
iterationType?: IterationType,
includeNull?: false
): ReturnType<C>[]
): ReturnType<C>[];
bfs<C extends BTNCallback<N>>(
callback?: C,
beginRoot?: N | null,
iterationType?: IterationType,
includeNull?: undefined
): ReturnType<C>[]
): ReturnType<C>[];
bfs<C extends BTNCallback<N | null>>(
callback?: C,
beginRoot?: N | null,
iterationType?: IterationType,
includeNull?: true
): ReturnType<C>[]
): ReturnType<C>[];
/**
* The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
@ -1123,7 +1122,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
if (current.right) queue.push(current.right);
}
traverse(level + 1);
};
@ -1144,7 +1142,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
if (current.left) queue.push(current.left);
if (current.right) queue.push(current.right);
}
}
}
}
@ -1152,25 +1149,25 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
}
listLevels<C extends BTNCallback<N>>(
callback?: C ,
beginRoot?: N | null ,
callback?: C,
beginRoot?: N | null,
iterationType?: IterationType,
includeNull?: false
): ReturnType<C>[][]
): ReturnType<C>[][];
listLevels<C extends BTNCallback<N>>(
callback?: C ,
beginRoot?: N | null ,
callback?: C,
beginRoot?: N | null,
iterationType?: IterationType,
includeNull?: undefined
): ReturnType<C>[][]
): ReturnType<C>[][];
listLevels<C extends BTNCallback<N | null>>(
callback?: C ,
beginRoot?: N | null ,
callback?: C,
beginRoot?: N | null,
iterationType?: IterationType,
includeNull?: true
): ReturnType<C>[][]
): ReturnType<C>[][];
/**
* The `listLevels` function takes a binary tree node and a callback function, and returns an array
@ -1204,7 +1201,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
if (includeNull) {
if (node && node.left !== undefined) _recursive(node.left, level + 1);
if (node && node.right !== undefined) _recursive(node.right, level + 1);
} else {
} else {
if (node && node.left) _recursive(node.left, level + 1);
if (node && node.right) _recursive(node.right, level + 1);
}
@ -1224,7 +1221,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
if (includeNull) {
if (node && node.right !== undefined) stack.push([node.right, level + 1]);
if (node && node.left !== undefined) stack.push([node.left, level + 1]);
} else {
} else {
if (node && node.right) stack.push([node.right, level + 1]);
if (node && node.left) stack.push([node.left, level + 1]);
}
@ -1273,8 +1270,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
return y;
}
// --- start additional methods ---
/**
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
* algorithm and returns an array of values obtained by applying a callback function to each node.
@ -1374,6 +1369,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
return ans;
}
// --- start additional methods ---
/**
* The above function is an iterator for a binary tree that can be used to traverse the tree in
* either an iterative or recursive manner.
@ -1383,7 +1380,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
* binary tree nodes in a specific order.
*/
* [Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
*[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
if (!node) {
return;
}
@ -1416,6 +1413,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
}
}
protected defaultOneParamCallback = (node: N) => node.key;
/**
* Swap the data of two nodes in the binary tree.
* @param {N} srcNode - The source node to swap.

View file

@ -64,7 +64,8 @@ export abstract class AbstractGraph<
E = any,
VO extends AbstractVertex<V> = AbstractVertex<V>,
EO extends AbstractEdge<E> = AbstractEdge<E>
> implements IGraph<V, E, VO, EO> {
> implements IGraph<V, E, VO, EO>
{
protected _vertices: Map<VertexKey, VO> = new Map<VertexKey, VO>();
get vertices(): Map<VertexKey, VO> {
@ -234,7 +235,7 @@ export abstract class AbstractGraph<
return [];
}
const stack: { vertex: VO, path: VO[] }[] = [];
const stack: {vertex: VO; path: VO[]}[] = [];
stack.push({vertex: vertex1, path: [vertex1]});
while (stack.length > 0) {
@ -256,7 +257,6 @@ export abstract class AbstractGraph<
return paths;
}
/**
* The function calculates the sum of weights along a given path.
* @param {VO[]} path - An array of vertices (VO) representing a path in a graph.
@ -366,7 +366,6 @@ export abstract class AbstractGraph<
} else {
return this.dijkstra(v1, v2, true, true)?.minPath ?? [];
}
} else {
// DFS
let minPath: VO[] = [];
@ -519,14 +518,14 @@ export abstract class AbstractGraph<
}
getMinDist &&
distMap.forEach((d, v) => {
if (v !== srcVertex) {
if (d < minDist) {
minDist = d;
if (genPaths) minDest = v;
distMap.forEach((d, v) => {
if (v !== srcVertex) {
if (d < minDist) {
minDist = d;
if (genPaths) minDest = v;
}
}
}
});
});
genPaths && getPaths(minDest);
@ -588,7 +587,7 @@ export abstract class AbstractGraph<
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
}
const heap = new PriorityQueue<{ key: number; value: VO }>({comparator: (a, b) => a.key - b.key});
const heap = new PriorityQueue<{key: number; value: VO}>({comparator: (a, b) => a.key - b.key});
heap.add({key: 0, value: srcVertex});
distMap.set(srcVertex, 0);
@ -812,7 +811,7 @@ export abstract class AbstractGraph<
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
* path between vertices in the
*/
floydWarshall(): { costs: number[][]; predecessor: (VO | null)[][] } {
floydWarshall(): {costs: number[][]; predecessor: (VO | null)[][]} {
const idAndVertices = [...this._vertices];
const n = idAndVertices.length;
@ -876,7 +875,12 @@ export abstract class AbstractGraph<
* are arrays of vertices that form cycles within the SCCs.
* @returns The function `tarjan` returns an object with the following properties:
*/
tarjan(needCutVertexes: boolean = false, needBridges: boolean = false, needSCCs: boolean = true, needCycles: boolean = false) {
tarjan(
needCutVertexes: boolean = false,
needBridges: boolean = false,
needSCCs: boolean = true,
needCycles: boolean = false
) {
// !! in undirected graph we will not let child visit parent when dfs
// !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
// !! bridge: low(child) > dfn(cur)

View file

@ -107,6 +107,7 @@ const composeReport = () => {
fs.writeFileSync(htmlFilePath, html);
console.log(`Performance ${BOLD}${GREEN}report${END} file generated`);
};
function replaceMarkdownContent(startMarker: string, endMarker: string, newText: string) {
const parentDirectory = path.resolve(__dirname, '../..'); // The path to the parent directory
const filePath = path.join(parentDirectory, 'README.md'); // Path to README.md file

View file

@ -174,10 +174,14 @@ describe('BinaryTree', () => {
it('should subTreeTraverse', () => {
tree.addMany([4, 2, 6, null, 1, 3, null, 5, null, 7]);
expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.ITERATIVE)).toEqual([6,3, 7]);
expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.RECURSIVE)).toEqual([6,3, 7]);
expect(tree.subTreeTraverse(node => node === null? null : node.key, tree.getNode(6), IterationType.ITERATIVE, true)).toEqual([6, 3, 7, null]);
expect(tree.subTreeTraverse(node => node === null? null : node.key, tree.getNode(6), IterationType.RECURSIVE, true)).toEqual([6, 3, 7, null]);
expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.ITERATIVE)).toEqual([6, 3, 7]);
expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.RECURSIVE)).toEqual([6, 3, 7]);
expect(
tree.subTreeTraverse(node => (node === null ? null : node.key), tree.getNode(6), IterationType.ITERATIVE, true)
).toEqual([6, 3, 7, null]);
expect(
tree.subTreeTraverse(node => (node === null ? null : node.key), tree.getNode(6), IterationType.RECURSIVE, true)
).toEqual([6, 3, 7, null]);
});
it('should clear the tree', () => {
@ -268,17 +272,33 @@ describe('BinaryTree traversals', () => {
const arr = [35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55];
tree.refill(arr);
expect(tree.bfs(node => node , tree.root, IterationType.ITERATIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55]);
expect(tree.bfs(node => node , tree.root, IterationType.RECURSIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55]);
expect(tree.bfs(node => node , tree.root, IterationType.ITERATIVE).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
expect(tree.bfs(node => node , tree.root, IterationType.RECURSIVE).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
expect(
tree.bfs(node => node, tree.root, IterationType.ITERATIVE, true).map(node => (node === null ? null : node.key))
).toEqual([35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55]);
expect(
tree.bfs(node => node, tree.root, IterationType.RECURSIVE, true).map(node => (node === null ? null : node.key))
).toEqual([35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55]);
expect(
tree.bfs(node => node, tree.root, IterationType.ITERATIVE).map(node => (node === null ? null : node.key))
).toEqual([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
expect(
tree.bfs(node => node, tree.root, IterationType.RECURSIVE).map(node => (node === null ? null : node.key))
).toEqual([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
expect(tree.dfs(node => node.key, 'pre')).toEqual([35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55]);
expect(tree.dfs(node => node.key, 'pre', tree.root, IterationType.RECURSIVE)).toEqual([
35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55
]);
expect(tree.dfs(node => node, 'pre', tree.root, IterationType.ITERATIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 15, null, 16, 29, 28, 30, 40, null, 50, 45, 55]);
expect(tree.dfs(node => node, 'pre', tree.root, IterationType.RECURSIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 15, null, 16, 29, 28, 30, 40, null, 50, 45, 55]);
expect(
tree
.dfs(node => node, 'pre', tree.root, IterationType.ITERATIVE, true)
.map(node => (node === null ? null : node.key))
).toEqual([35, 20, 15, null, 16, 29, 28, 30, 40, null, 50, 45, 55]);
expect(
tree
.dfs(node => node, 'pre', tree.root, IterationType.RECURSIVE, true)
.map(node => (node === null ? null : node.key))
).toEqual([35, 20, 15, null, 16, 29, 28, 30, 40, null, 50, 45, 55]);
expect(tree.dfs(node => node.key, 'in')).toEqual([15, 16, 20, 28, 29, 30, 35, 40, 45, 50, 55]);
expect(tree.dfs(node => node.key, 'post')).toEqual([16, 15, 28, 30, 29, 20, 45, 55, 50, 40, 35]);
@ -300,21 +320,20 @@ describe('BinaryTree traversals', () => {
[15, 29, 50],
[16, 28, 30, 45, 55]
]);
expect(tree.listLevels(node => node === null ? null :node.key, tree.root, IterationType.ITERATIVE, true)).toEqual([
expect(tree.listLevels(node => (node === null ? null : node.key), tree.root, IterationType.ITERATIVE, true)).toEqual([
[35],
[20, 40],
[15, 29, null,50],
[15, 29, null, 50],
[null, 16, 28, 30, 45, 55]
]);
expect(tree.listLevels(node => node === null ? null :node.key, tree.root, IterationType.RECURSIVE, true)).toEqual([
expect(tree.listLevels(node => (node === null ? null : node.key), tree.root, IterationType.RECURSIVE, true)).toEqual([
[35],
[20, 40],
[15, 29, null,50],
[15, 29, null, 50],
[null, 16, 28, 30, 45, 55]
]);
});
describe('BinaryTree', () => {
let tree: BinaryTree<string>;

View file

@ -838,13 +838,16 @@ describe('BST Performance test', function () {
expect(bst.lastKey()).toBe(9);
});
it('should subTreeTraverse, null should be ignored', () => {
const bst = new BST();
bst.addMany([4, 2, 6, null, 1, 3, null, 5, null, 7]);
expect(bst.subTreeTraverse(node => node.key, bst.getNode(6), IterationType.ITERATIVE)).toEqual([6,5, 7]);
expect(bst.subTreeTraverse(node => node.key, bst.getNode(6), IterationType.RECURSIVE)).toEqual([6,5, 7]);
expect(bst.subTreeTraverse(node => node === null ? null : node.key, bst.getNode(6), IterationType.ITERATIVE, true)).toEqual([6, 5, 7]);
expect(bst.subTreeTraverse(node => node === null ? null : node.key, bst.getNode(6), IterationType.RECURSIVE, true)).toEqual([6, 5, 7]);
expect(bst.subTreeTraverse(node => node.key, bst.getNode(6), IterationType.ITERATIVE)).toEqual([6, 5, 7]);
expect(bst.subTreeTraverse(node => node.key, bst.getNode(6), IterationType.RECURSIVE)).toEqual([6, 5, 7]);
expect(
bst.subTreeTraverse(node => (node === null ? null : node.key), bst.getNode(6), IterationType.ITERATIVE, true)
).toEqual([6, 5, 7]);
expect(
bst.subTreeTraverse(node => (node === null ? null : node.key), bst.getNode(6), IterationType.RECURSIVE, true)
).toEqual([6, 5, 7]);
});
});

View file

@ -563,7 +563,6 @@ describe('Inherit from DirectedGraph and perform operations test2.', () => {
});
});
describe('cycles, strongly connected components, bridges, articular points in DirectedGraph', () => {
const graph = new DirectedGraph();
graph.addVertex('A');
@ -589,10 +588,10 @@ describe('cycles, strongly connected components, bridges, articular points in Di
const cutVertexes = graph.getCutVertexes();
const dfnMap = graph.getDFNMap();
const lowMap = graph.getLowMap();
expect(cycles.size).toBe(1)
expect(scCs.size).toBe(5)
expect(bridges.length).toBe(4)
expect(cutVertexes.length).toBe(4)
expect(dfnMap.size).toBe(8)
expect(lowMap.size).toBe(8)
});
expect(cycles.size).toBe(1);
expect(scCs.size).toBe(5);
expect(bridges.length).toBe(4);
expect(cutVertexes.length).toBe(4);
expect(dfnMap.size).toBe(8);
expect(lowMap.size).toBe(8);
});

File diff suppressed because one or more lines are too long

View file

@ -1 +1,200 @@
[{"name":"Intersection_1"},{"name":"Intersection_2"},{"name":"Intersection_3"},{"name":"Intersection_4"},{"name":"Intersection_5"},{"name":"Intersection_6"},{"name":"Intersection_7"},{"name":"Intersection_8"},{"name":"Intersection_9"},{"name":"Intersection_10"},{"name":"Intersection_11"},{"name":"Intersection_12"},{"name":"Intersection_13"},{"name":"Intersection_14"},{"name":"Intersection_15"},{"name":"Intersection_16"},{"name":"Intersection_17"},{"name":"Intersection_18"},{"name":"Intersection_19"},{"name":"Intersection_20"},{"name":"Intersection_21"},{"name":"Intersection_22"},{"name":"Intersection_23"},{"name":"Intersection_24"},{"name":"Intersection_25"},{"name":"Intersection_26"},{"name":"Intersection_27"},{"name":"Intersection_28"},{"name":"Intersection_29"},{"name":"Intersection_30"},{"name":"Intersection_31"},{"name":"Intersection_32"},{"name":"Intersection_33"},{"name":"Intersection_34"},{"name":"Intersection_35"},{"name":"Intersection_36"},{"name":"Intersection_37"},{"name":"Intersection_38"},{"name":"Intersection_39"},{"name":"Intersection_40"},{"name":"Intersection_41"},{"name":"Intersection_42"},{"name":"Intersection_43"},{"name":"Intersection_44"},{"name":"Intersection_45"},{"name":"Intersection_46"},{"name":"Intersection_47"},{"name":"Intersection_48"},{"name":"Intersection_49"},{"name":"Intersection_50"},{"name":"Intersection_51"},{"name":"Intersection_52"},{"name":"Intersection_53"},{"name":"Intersection_54"},{"name":"Intersection_55"},{"name":"Intersection_56"},{"name":"Intersection_57"},{"name":"Intersection_58"},{"name":"Intersection_59"},{"name":"Intersection_60"},{"name":"Intersection_61"},{"name":"Intersection_62"},{"name":"Intersection_63"},{"name":"Intersection_64"},{"name":"Intersection_65"},{"name":"Intersection_66"}]
[
{
"name": "Intersection_1"
},
{
"name": "Intersection_2"
},
{
"name": "Intersection_3"
},
{
"name": "Intersection_4"
},
{
"name": "Intersection_5"
},
{
"name": "Intersection_6"
},
{
"name": "Intersection_7"
},
{
"name": "Intersection_8"
},
{
"name": "Intersection_9"
},
{
"name": "Intersection_10"
},
{
"name": "Intersection_11"
},
{
"name": "Intersection_12"
},
{
"name": "Intersection_13"
},
{
"name": "Intersection_14"
},
{
"name": "Intersection_15"
},
{
"name": "Intersection_16"
},
{
"name": "Intersection_17"
},
{
"name": "Intersection_18"
},
{
"name": "Intersection_19"
},
{
"name": "Intersection_20"
},
{
"name": "Intersection_21"
},
{
"name": "Intersection_22"
},
{
"name": "Intersection_23"
},
{
"name": "Intersection_24"
},
{
"name": "Intersection_25"
},
{
"name": "Intersection_26"
},
{
"name": "Intersection_27"
},
{
"name": "Intersection_28"
},
{
"name": "Intersection_29"
},
{
"name": "Intersection_30"
},
{
"name": "Intersection_31"
},
{
"name": "Intersection_32"
},
{
"name": "Intersection_33"
},
{
"name": "Intersection_34"
},
{
"name": "Intersection_35"
},
{
"name": "Intersection_36"
},
{
"name": "Intersection_37"
},
{
"name": "Intersection_38"
},
{
"name": "Intersection_39"
},
{
"name": "Intersection_40"
},
{
"name": "Intersection_41"
},
{
"name": "Intersection_42"
},
{
"name": "Intersection_43"
},
{
"name": "Intersection_44"
},
{
"name": "Intersection_45"
},
{
"name": "Intersection_46"
},
{
"name": "Intersection_47"
},
{
"name": "Intersection_48"
},
{
"name": "Intersection_49"
},
{
"name": "Intersection_50"
},
{
"name": "Intersection_51"
},
{
"name": "Intersection_52"
},
{
"name": "Intersection_53"
},
{
"name": "Intersection_54"
},
{
"name": "Intersection_55"
},
{
"name": "Intersection_56"
},
{
"name": "Intersection_57"
},
{
"name": "Intersection_58"
},
{
"name": "Intersection_59"
},
{
"name": "Intersection_60"
},
{
"name": "Intersection_61"
},
{
"name": "Intersection_62"
},
{
"name": "Intersection_63"
},
{
"name": "Intersection_64"
},
{
"name": "Intersection_65"
},
{
"name": "Intersection_66"
}
]

View file

@ -158,24 +158,23 @@ describe('UndirectedGraph', () => {
const [s, d] = e;
graph.addEdge(s.name, d.name, d.weight);
}
const allPaths = graph.getAllPathsBetween('Intersection_1','Intersection_5');
const allPaths = graph.getAllPathsBetween('Intersection_1', 'Intersection_5');
expect(allPaths.length).toBe(1000);
const minWeightedPathDFS = graph.getMinPathBetween('Intersection_1','Intersection_5', true, true);
const minWeightedPathDFS = graph.getMinPathBetween('Intersection_1', 'Intersection_5', true, true);
expect(minWeightedPathDFS?.[0]?.key).toBe('Intersection_1');
expect(minWeightedPathDFS?.[5]?.key).toBe('Intersection_42');
expect(minWeightedPathDFS?.[8]?.key).toBe('Intersection_18');
expect(minWeightedPathDFS?.[27]?.key).toBe('Intersection_6');
const minWeightedPath = graph.dijkstra('Intersection_1','Intersection_5', true, true);
const minWeightedPath = graph.dijkstra('Intersection_1', 'Intersection_5', true, true);
expect(minWeightedPath?.minPath?.[0]?.key).toBe('Intersection_1')
expect(minWeightedPath?.minPath?.[1]?.key).toBe('Intersection_2')
expect(minWeightedPath?.minPath?.[2]?.key).toBe('Intersection_3')
expect(minWeightedPath?.minPath?.[3]?.key).toBe('Intersection_4')
expect(minWeightedPath?.minPath?.[4]?.key).toBe('Intersection_5')
expect(minWeightedPath?.minPath?.[0]?.key).toBe('Intersection_1');
expect(minWeightedPath?.minPath?.[1]?.key).toBe('Intersection_2');
expect(minWeightedPath?.minPath?.[2]?.key).toBe('Intersection_3');
expect(minWeightedPath?.minPath?.[3]?.key).toBe('Intersection_4');
expect(minWeightedPath?.minPath?.[4]?.key).toBe('Intersection_5');
});
});
describe('cycles, strongly connected components, bridges, articular points in UndirectedGraph', () => {
const graph = new UndirectedGraph();
graph.addVertex('A');
@ -201,10 +200,10 @@ describe('cycles, strongly connected components, bridges, articular points in Un
const cutVertexes = graph.getCutVertexes();
const dfnMap = graph.getDFNMap();
const lowMap = graph.getLowMap();
expect(cycles.size).toBe(1)
expect(scCs.size).toBe(5)
expect(bridges.length).toBe(4)
expect(cutVertexes.length).toBe(4)
expect(dfnMap.size).toBe(8)
expect(lowMap.size).toBe(8)
});
expect(cycles.size).toBe(1);
expect(scCs.size).toBe(5);
expect(bridges.length).toBe(4);
expect(cutVertexes.length).toBe(4);
expect(dfnMap.size).toBe(8);
expect(lowMap.size).toBe(8);
});