Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/bmssp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,36 @@ class BMSSP {
this.graph = [];
// Set to store unique node IDs
this.nodeIDs = new Set();
// Array to store shortest paths
this.shortestPaths = [];
// Map to store shortest paths
this.shortestPaths = new Map();

for (let edge of inputGraph) {
// Create a deep copy of each edge array
this.graph.push([...edge]);

// Add node IDs to the set
this.nodeIDs.add(edge[0]);
this.nodeIDs.add(edge[1]);
}

// Initialize shortest paths with Infinity on each nodeID (after all nodes are collected)
// Initialize shortest paths map
this.initializeShortestPaths();
}

// Method to initialize the shortest paths map
initializeShortestPaths() {
for (let nodeId of this.nodeIDs) {
this.shortestPaths.push([nodeId, Infinity]);
this.shortestPaths.set(nodeId, Infinity);
}
}

// Method to calculate shortest paths (placeholder implementation)
calculateShortestPaths(startNode) {
// Placeholder logic for shortest path calculation
// This should be replaced with an actual implementation of BMSSP algorithm
this.initializeShortestPaths();
this.shortestPaths.set(startNode, 0);
}
}

export { BMSSP };
13 changes: 11 additions & 2 deletions test/main.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,19 @@ describe("BMSSP nodeIDs", () => {
describe("BMSSP shortestPaths", () => {
test("initializes shortest paths with Infinity", () => {
const myBMSSP = new BMSSP(roadNetCA);
const expectedShortestPaths = [];
const expectedShortestPaths = new Map();
myBMSSP.nodeIDs.forEach((nodeId) => {
expectedShortestPaths.push([nodeId, Infinity]);
expectedShortestPaths.set(nodeId, Infinity);
});
expect(myBMSSP.shortestPaths).toEqual(expectedShortestPaths);
});
});

describe("BMSSP initialize calculateShortestPaths", () => {
test("sets the distance to the start node to 0", () => {
const myBMSSP = new BMSSP(roadNetCA);
const startNode = [...myBMSSP.nodeIDs][0];
myBMSSP.calculateShortestPaths(startNode);
expect(myBMSSP.shortestPaths.get(startNode)).toBe(0);
});
});