From 9d4228c9fe1b294317e7553bf02ddd5eabba8bdd Mon Sep 17 00:00:00 2001 From: Revone Date: Mon, 11 Dec 2023 09:41:24 +0800 Subject: [PATCH] refactor: Represent the optional parameters in the dijkstraWithoutHeap and dijkstra methods using default arguments. Use the Heap data structure instead of PriorityQueue. --- CHANGELOG.md | 2 +- package.json | 2 +- src/data-structures/graph/abstract-graph.ts | 23 +++++++------------ .../graph/undirected-graph.test.ts | 17 ++++++++++++++ 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 663747e..d5c23c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file. - [Semantic Versioning](https://semver.org/spec/v2.0.0.html) - [`auto-changelog`](https://github.com/CookPete/auto-changelog) -## [v1.48.9](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming) +## [v1.49.0](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming) ### Changes diff --git a/package.json b/package.json index 49f0b05..81218d4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "data-structure-typed", - "version": "1.48.9", + "version": "1.49.0", "description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python", "main": "dist/cjs/index.js", "module": "dist/mjs/index.js", diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts index 9305d49..959dd33 100644 --- a/src/data-structures/graph/abstract-graph.ts +++ b/src/data-structures/graph/abstract-graph.ts @@ -6,7 +6,7 @@ * @license MIT License */ import { uuidV4 } from '../../utils'; -import { PriorityQueue } from '../priority-queue'; +import { Heap } from '../heap'; import type { DijkstraResult, VertexKey } from '../../types'; import { EntryCallback } from "../../types"; import { IGraph } from '../../interfaces'; @@ -527,14 +527,10 @@ export abstract class AbstractGraph< */ dijkstraWithoutHeap( src: VO | VertexKey, - dest?: VO | VertexKey | undefined, - getMinDist?: boolean, - genPaths?: boolean + dest: VO | VertexKey | undefined = undefined, + getMinDist: boolean = false, + genPaths: boolean = false ): DijkstraResult { - if (getMinDist === undefined) getMinDist = false; - if (genPaths === undefined) genPaths = false; - - if (dest === undefined) dest = undefined; let minDist = Infinity; let minDest: VO | undefined = undefined; let minPath: VO[] = []; @@ -675,14 +671,11 @@ export abstract class AbstractGraph< */ dijkstra( src: VO | VertexKey, - dest?: VO | VertexKey | undefined, - getMinDist?: boolean, - genPaths?: boolean + dest: VO | VertexKey | undefined = undefined, + getMinDist: boolean = false, + genPaths: boolean = false ): DijkstraResult { - if (getMinDist === undefined) getMinDist = false; - if (genPaths === undefined) genPaths = false; - if (dest === undefined) dest = undefined; let minDist = Infinity; let minDest: VO | undefined = undefined; let minPath: VO[] = []; @@ -702,7 +695,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 Heap<{ key: number; value: VO }>([], { comparator: (a, b) => a.key - b.key }); heap.add({ key: 0, value: srcVertex }); distMap.set(srcVertex, 0); diff --git a/test/unit/data-structures/graph/undirected-graph.test.ts b/test/unit/data-structures/graph/undirected-graph.test.ts index d40e391..5292545 100644 --- a/test/unit/data-structures/graph/undirected-graph.test.ts +++ b/test/unit/data-structures/graph/undirected-graph.test.ts @@ -244,3 +244,20 @@ describe('cycles, strongly connected components, bridges, articular points in Un expect(dfnMap.size).toBe(8); expect(lowMap.size).toBe(8); }); + +it("Should return Infinity if dest is not found", () => { + + const graph = new UndirectedGraph(); + + for (let i = 0; i < 3; ++i) { + graph.addVertex(graph.createVertex(i, `${i}`)); + } + + graph.addEdge(0, 1, 1); + + const minCost02 = graph.getMinCostBetween(0, 2, true); + expect(minCost02).toBe(Infinity); + + const minCost01 = graph.getMinCostBetween(0, 1, true); + expect(minCost01).toBe(1); +}); \ No newline at end of file