Skip to content

Commit 1f851c6

Browse files
committed
feat: Add shortest paths placeholder function
1 parent b25900e commit 1f851c6

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/bmssp.mjs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,36 @@ class BMSSP {
44
this.graph = [];
55
// Set to store unique node IDs
66
this.nodeIDs = new Set();
7-
// Array to store shortest paths
8-
this.shortestPaths = [];
7+
// Map to store shortest paths
8+
this.shortestPaths = new Map();
99

1010
for (let edge of inputGraph) {
1111
// Create a deep copy of each edge array
1212
this.graph.push([...edge]);
13+
1314
// Add node IDs to the set
1415
this.nodeIDs.add(edge[0]);
1516
this.nodeIDs.add(edge[1]);
1617
}
1718

18-
// Initialize shortest paths with Infinity on each nodeID (after all nodes are collected)
19+
// Initialize shortest paths map
20+
this.initializeShortestPaths();
21+
}
22+
23+
// Method to initialize the shortest paths map
24+
initializeShortestPaths() {
1925
for (let nodeId of this.nodeIDs) {
20-
this.shortestPaths.push([nodeId, Infinity]);
26+
this.shortestPaths.set(nodeId, Infinity);
2127
}
2228
}
29+
30+
// Method to calculate shortest paths (placeholder implementation)
31+
calculateShortestPaths(startNode) {
32+
// Placeholder logic for shortest path calculation
33+
// This should be replaced with an actual implementation of BMSSP algorithm
34+
this.initializeShortestPaths();
35+
this.shortestPaths.set(startNode, 0);
36+
}
2337
}
2438

2539
export { BMSSP };

test/main.test.mjs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,19 @@ describe("BMSSP nodeIDs", () => {
4141
describe("BMSSP shortestPaths", () => {
4242
test("initializes shortest paths with Infinity", () => {
4343
const myBMSSP = new BMSSP(roadNetCA);
44-
const expectedShortestPaths = [];
44+
const expectedShortestPaths = new Map();
4545
myBMSSP.nodeIDs.forEach((nodeId) => {
46-
expectedShortestPaths.push([nodeId, Infinity]);
46+
expectedShortestPaths.set(nodeId, Infinity);
4747
});
4848
expect(myBMSSP.shortestPaths).toEqual(expectedShortestPaths);
4949
});
5050
});
51+
52+
describe("BMSSP initialize calculateShortestPaths", () => {
53+
test("sets the distance to the start node to 0", () => {
54+
const myBMSSP = new BMSSP(roadNetCA);
55+
const startNode = [...myBMSSP.nodeIDs][0];
56+
myBMSSP.calculateShortestPaths(startNode);
57+
expect(myBMSSP.shortestPaths.get(startNode)).toBe(0);
58+
});
59+
});

0 commit comments

Comments
 (0)