From 3997923608d12831712dd5950453e72dcd47c9a2 Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Thu, 22 Dec 2022 00:09:26 +0100 Subject: [PATCH] feat(adjacency): update BFS distance array to Float32Array --- packages/adjacency/src/bfs.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/adjacency/src/bfs.ts b/packages/adjacency/src/bfs.ts index a46a1aa207..4352b459cf 100644 --- a/packages/adjacency/src/bfs.ts +++ b/packages/adjacency/src/bfs.ts @@ -2,17 +2,29 @@ import { BitField } from "@thi.ng/bitfield/bitfield"; import { DCons } from "@thi.ng/dcons/dcons"; import type { CostFn, IGraph } from "./api.js"; +/** + * Breadth-First / shortest path search between `src` and `dest` in `graph`, + * with optional `cost` function. By default all edges have an uniform cost, + * i.e. the overall path cost is topological distance. + * + * @remarks + * Also see {@link bfs} for ad hoc queries. + * + * Reference: + * - https://en.wikipedia.org/wiki/Breadth-first_search + * - https://algs4.cs.princeton.edu/40graphs/ + */ export class BFS { graph: IGraph; marked: BitField; edges: Uint32Array; - dist: Uint32Array; + dist: Float32Array; constructor(graph: IGraph, src: number, cost: CostFn = () => 1) { this.graph = graph; const numV = graph.numVertices(); this.edges = new Uint32Array(numV); - this.dist = new Uint32Array(numV); + this.dist = new Float32Array(numV); this.marked = new BitField(numV); this.search(src, cost); }