Skip to content

Commit 978f989

Browse files
SirivasvCopilot
andauthored
feat: Add shortest paths placeholder function (#39)
Co-authored-by: Copilot <[email protected]>
1 parent b25900e commit 978f989

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

src/bmssp.mjs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,42 @@ 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);
27+
}
28+
}
29+
30+
// Method to calculate shortest paths (placeholder implementation)
31+
calculateShortestPaths(startNode) {
32+
// To clean the state before calculation
33+
this.initializeShortestPaths();
34+
35+
// validate startNode
36+
if (!this.nodeIDs.has(startNode)) {
37+
throw new Error("Start node not found in the graph");
2138
}
39+
40+
// Placeholder logic for shortest path calculation
41+
// This should be replaced with an actual implementation of BMSSP algorithm
42+
this.shortestPaths.set(startNode, 0);
2243
}
2344
}
2445

test/main.test.mjs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, test, expect } from "@jest/globals";
22
import { BMSSP } from "../index.mjs";
33
import fs from "fs";
44

5+
// Load the roadNet-CA.txt graph and parse it into an array of edges
56
let roadNetCA = (() => {
67
let graph = [];
78
const filePath = new URL("./roadNet-CA.txt", import.meta.url).pathname;
@@ -19,16 +20,17 @@ let roadNetCA = (() => {
1920
return graph;
2021
})();
2122

23+
// Have an initialized BMSSP instance for tests
24+
const myBMSSP = new BMSSP(roadNetCA);
25+
2226
describe("BMSSP constructor", () => {
2327
test("initializes the graph correctly", () => {
24-
const myBMSSP = new BMSSP(roadNetCA);
2528
expect(myBMSSP.graph).toEqual(roadNetCA);
2629
});
2730
});
2831

2932
describe("BMSSP nodeIDs", () => {
3033
test("stores unique node IDs correctly", () => {
31-
const myBMSSP = new BMSSP(roadNetCA);
3234
const uniqueNodeIDs = new Set();
3335
roadNetCA.forEach((edge) => {
3436
uniqueNodeIDs.add(edge[0]);
@@ -40,11 +42,24 @@ describe("BMSSP nodeIDs", () => {
4042

4143
describe("BMSSP shortestPaths", () => {
4244
test("initializes shortest paths with Infinity", () => {
43-
const myBMSSP = new BMSSP(roadNetCA);
44-
const expectedShortestPaths = [];
45+
const expectedShortestPaths = new Map();
4546
myBMSSP.nodeIDs.forEach((nodeId) => {
46-
expectedShortestPaths.push([nodeId, Infinity]);
47+
expectedShortestPaths.set(nodeId, Infinity);
4748
});
4849
expect(myBMSSP.shortestPaths).toEqual(expectedShortestPaths);
4950
});
5051
});
52+
53+
describe("BMSSP initialize calculateShortestPaths", () => {
54+
test("sets the distance to the start node to 0", () => {
55+
const startNode = [...myBMSSP.nodeIDs][0];
56+
myBMSSP.calculateShortestPaths(startNode);
57+
expect(myBMSSP.shortestPaths.get(startNode)).toBe(0);
58+
});
59+
test("throws an error if the start node is not in the graph", () => {
60+
const invalidStartNode = -1; // Assuming -1 is not a valid node ID in the graph
61+
expect(() => {
62+
myBMSSP.calculateShortestPaths(invalidStartNode);
63+
}).toThrow("Start node not found in the graph");
64+
});
65+
});

0 commit comments

Comments
 (0)