Skip to content

Commit

Permalink
refactor: Represent the optional parameters in the dijkstraWithoutHea…
Browse files Browse the repository at this point in the history
…p and dijkstra methods using default arguments. Use the Heap data structure instead of PriorityQueue.
  • Loading branch information
zrwusa committed Dec 11, 2023
1 parent 1fc4791 commit 9d4228c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
23 changes: 8 additions & 15 deletions src/data-structures/graph/abstract-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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[] = [];
Expand All @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions test/unit/data-structures/graph/undirected-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 9d4228c

Please sign in to comment.