Skip to content

Commit cc46fe5

Browse files
committed
simplify public interface of the solver
1 parent 4925d48 commit cc46fe5

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
A pretty good implementation of Dijkstra's shortest-path algorithm.
44

5-
This implementation is designed to process large in-memory graphs. It will perform reasonably well even when the number of edges is in the millions. Using numbered indexes for nodes, rather than string labels or objects, was by design. This reduced the memory footprint needed for the graph and speeds up graph creation and lookups.
5+
This implementation is designed to process large in-memory graphs. It will
6+
perform reasonably well even when the number of edges is in the millions. Using
7+
numbered indexes for nodes, rather than string labels or objects, was by design.
8+
This reduced the memory footprint needed for the graph and speeds up graph
9+
creation and lookups.
610

711
This code was adapted to Typescript/Deno from
812
[A Walkthrough of Dijkstra's Algorithm (In JavaScript!)](https://medium.com/@adriennetjohnson/a-walkthrough-of-dijkstras-algorithm-in-javascript-e94b74192026)

dijkstra.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ interface IPath {
2929
*/
3030
export class DijkstraShortestPathSolver {
3131
private constructor(
32-
public readonly nodes: number,
33-
public readonly adjacencyList: IEdge[][],
32+
protected readonly adjacencyList: IEdge[][],
3433
) {
3534
}
3635

@@ -41,7 +40,6 @@ export class DijkstraShortestPathSolver {
4140
*/
4241
static init(nodes: number): DijkstraShortestPathSolver {
4342
return new DijkstraShortestPathSolver(
44-
nodes,
4543
new Array(nodes).fill(null).map((_v) => new Array(0)),
4644
);
4745
}
@@ -52,11 +50,17 @@ export class DijkstraShortestPathSolver {
5250
*/
5351
clone(): DijkstraShortestPathSolver {
5452
return new DijkstraShortestPathSolver(
55-
this.nodes,
5653
this.adjacencyList.map((a) => a.slice(0)),
5754
);
5855
}
5956

57+
/**
58+
* The number of nodes in the graph.
59+
*/
60+
protected get nodes(): number {
61+
return this.adjacencyList.length;
62+
}
63+
6064
/**
6165
* Add an edge (in one direction).
6266
* @param fromNode Starting node.

0 commit comments

Comments
 (0)