refactor: Represent the optional parameters in the dijkstraWithoutHeap and dijkstra methods using default arguments. Use the Heap data structure instead of PriorityQueue.

This commit is contained in:
Revone 2023-12-11 09:41:24 +08:00
parent 1fc47918d2
commit 9d4228c9fe
4 changed files with 27 additions and 17 deletions

View file

@ -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

View file

@ -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",

View file

@ -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<VO> {
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<VO> {
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);

View file

@ -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<string>();
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);
});