Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 25 additions & 4 deletions src/bmssp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,42 @@ 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) {
// To clean the state before calculation
this.initializeShortestPaths();

// validate startNode
if (!this.nodeIDs.has(startNode)) {
throw new Error("Start node not found in the graph");
}

// Placeholder logic for shortest path calculation
// This should be replaced with an actual implementation of BMSSP algorithm
this.shortestPaths.set(startNode, 0);
}
}

Expand Down
25 changes: 20 additions & 5 deletions test/main.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, test, expect } from "@jest/globals";
import { BMSSP } from "../index.mjs";
import fs from "fs";

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

// Have an initialized BMSSP instance for tests
const myBMSSP = new BMSSP(roadNetCA);

describe("BMSSP constructor", () => {
test("initializes the graph correctly", () => {
const myBMSSP = new BMSSP(roadNetCA);
expect(myBMSSP.graph).toEqual(roadNetCA);
});
});

describe("BMSSP nodeIDs", () => {
test("stores unique node IDs correctly", () => {
const myBMSSP = new BMSSP(roadNetCA);
const uniqueNodeIDs = new Set();
roadNetCA.forEach((edge) => {
uniqueNodeIDs.add(edge[0]);
Expand All @@ -40,11 +42,24 @@ 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 startNode = [...myBMSSP.nodeIDs][0];
myBMSSP.calculateShortestPaths(startNode);
expect(myBMSSP.shortestPaths.get(startNode)).toBe(0);
});
test("throws an error if the start node is not in the graph", () => {
const invalidStartNode = -1; // Assuming -1 is not a valid node ID in the graph
expect(() => {
myBMSSP.calculateShortestPaths(invalidStartNode);
}).toThrow("Start node not found in the graph");
});
});