Docs: Update benchmark results

This commit is contained in:
github-actions[bot]
2025-10-13 12:58:11 +00:00
parent 4d3146e916
commit 1d4e1d84ac
4 changed files with 278 additions and 5 deletions

View File

@@ -0,0 +1,82 @@
async function findShortestPath(graph, start, end) {
// Dynamically import the priority queue library from CDN
const { default: PriorityQueue } = await import(
'https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/+esm'
);
// Initialize distances object with Infinity for all nodes
const distances = {};
for (const node in graph) {
distances[node] = Infinity;
}
distances[start] = 0;
// Initialize visited set
const visited = new Set();
// Create a min-priority queue (lower distance = higher priority)
const pq = new PriorityQueue({
comparator: (a, b) => a.distance - b.distance
});
// Add start node to priority queue
pq.queue({ node: start, distance: 0 });
while (pq.length > 0) {
const { node: currentNode, distance: currentDistance } = pq.dequeue();
// Skip if already visited
if (visited.has(currentNode)) {
continue;
}
// Mark as visited
visited.add(currentNode);
// If we reached the end node, return the distance
if (currentNode === end) {
return currentDistance;
}
// Skip if current distance is outdated
if (currentDistance > distances[currentNode]) {
continue;
}
// Check all neighbors
const neighbors = graph[currentNode] || {};
for (const neighbor in neighbors) {
if (visited.has(neighbor)) {
continue;
}
const weight = neighbors[neighbor];
const newDistance = currentDistance + weight;
// If we found a shorter path, update it
if (newDistance < distances[neighbor]) {
distances[neighbor] = newDistance;
pq.queue({ node: neighbor, distance: newDistance });
}
}
}
// If we get here, no path exists
return Infinity;
}
// Example usage:
(async () => {
const graph = {
'A': { 'B': 1, 'C': 4 },
'B': { 'A': 1, 'C': 2, 'D': 5 },
'C': { 'A': 4, 'B': 2, 'D': 1 },
'D': { 'B': 5, 'C': 1 }
};
const shortestPath = await findShortestPath(graph, 'A', 'D');
console.log('Shortest path from A to D:', shortestPath); // Output: 4
const noPath = await findShortestPath(graph, 'A', 'E');
console.log('Path from A to E (non-existent):', noPath); // Output: Infinity
})();

View File

@@ -0,0 +1,120 @@
/**
* Finds the shortest path in a weighted, undirected graph using Dijkstra's algorithm.
*
* This function dynamically imports the 'js-priority-queue' library to manage nodes to visit.
*
* @param {object} graph - The graph structure. e.g., { 'A': { 'B': 1, 'C': 4 }, 'B': { ... }, ... }
* @param {string} startNode - The starting node.
* @param {string} endNode - The ending node.
* @returns {Promise<number>} The total weight of the shortest path, or Infinity if no path exists.
*/
async function findShortestPath(graph, startNode, endNode) {
let PriorityQueue;
try {
// Dynamically import the PriorityQueue library from a CDN.
// The { default: ... } syntax is used to get the default export from the UMD module.
const PQLib = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/priority-queue.min.js');
PriorityQueue = PQLib.default;
} catch (e) {
console.error("Failed to load the Priority Queue library from the CDN.", e);
// Can't proceed without the library.
throw new Error("Could not load a required dependency.");
}
// 1. Initialize distances, priority queue, and previous nodes map
const distances = {};
const priorityQueue = new PriorityQueue({
// The comparator ensures the queue is a min-priority queue based on distance.
comparator: (a, b) => a.distance - b.distance
});
// Set initial distances for all nodes
for (const node in graph) {
if (node === startNode) {
distances[node] = 0;
priorityQueue.queue({ node: node, distance: 0 });
} else {
distances[node] = Infinity;
}
}
// If the start or end node is not in the graph, no path can exist.
if (distances[startNode] === undefined || distances[endNode] === undefined) {
return Infinity;
}
// 2. Main loop of Dijkstra's algorithm
while (priorityQueue.length > 0) {
// Get the node with the smallest distance from the queue
const { node: currentNode, distance: currentDistance } = priorityQueue.dequeue();
// If we've already found a shorter path to the current node, skip it.
if (currentDistance > distances[currentNode]) {
continue;
}
// If we've reached the end node, we have found the shortest path.
if (currentNode === endNode) {
// The value in 'distances' is the shortest path weight.
return distances[endNode];
}
const neighbors = graph[currentNode];
if (!neighbors) continue; // Skip if node has no outgoing edges
// 3. Relaxation step: Check all neighbors of the current node
for (const neighbor in neighbors) {
const weight = neighbors[neighbor];
const distanceThroughCurrent = currentDistance + weight;
// If we found a shorter path to the neighbor, update it.
if (distanceThroughCurrent < distances[neighbor]) {
distances[neighbor] = distanceThroughCurrent;
// Enqueue the neighbor with its new, shorter distance.
priorityQueue.queue({ node: neighbor, distance: distanceThroughCurrent });
}
}
}
// 4. If the loop finishes and we haven't returned, the end node is unreachable.
return distances[endNode]; // This will be Infinity
}
// --- Example Usage ---
// Define the graph structure
const graph = {
'A': { 'B': 2, 'C': 5 },
'B': { 'A': 2, 'C': 6, 'D': 1 },
'C': { 'A': 5, 'B': 6, 'D': 6, 'E': 8 },
'D': { 'B': 1, 'C': 6, 'E': 2 },
'E': { 'C': 8, 'D': 2 },
'F': { 'G': 1 }, // A disconnected part of the graph
'G': { 'F': 1 }
};
// We wrap the calls in an async IIFE (Immediately Invoked Function Expression)
// because we can only use 'await' inside an async function.
(async () => {
console.log("Finding shortest path from A to E...");
const pathAtoE = await findShortestPath(graph, 'A', 'E');
console.log(`Shortest path from A to E has a weight of: ${pathAtoE}`); // Expected: 5 (A -> B -> D -> E)
console.log("\nFinding shortest path from A to C...");
const pathAtoC = await findShortestPath(graph, 'A', 'C');
console.log(`Shortest path from A to C has a weight of: ${pathAtoC}`); // Expected: 5 (A -> C)
console.log("\nFinding shortest path from A to A...");
const pathAtoA = await findShortestPath(graph, 'A', 'A');
console.log(`Shortest path from A to A has a weight of: ${pathAtoA}`); // Expected: 0
console.log("\nFinding shortest path from A to F (unreachable)...");
const pathAtoF = await findShortestPath(graph, 'A', 'F');
console.log(`Shortest path from A to F has a weight of: ${pathAtoF}`); // Expected: Infinity
console.log("\nFinding shortest path from A to Z (non-existent node)...");
const pathAtoZ = await findShortestPath(graph, 'A', 'Z');
console.log(`Shortest path from A to Z has a weight of: ${pathAtoZ}`); // Expected: Infinity
})();

View File

@@ -0,0 +1,71 @@
// Lazy-load the PriorityQueue module once and cache the Promise
let priorityQueueModulePromise;
/**
* Asynchronously finds the total weight of the shortest path between two nodes
* in a weighted, undirected graph using Dijkstra's algorithm.
*
* @param {Object<string, Object<string, number>>} graph - Adjacency list representation of the graph.
* @param {string} start - The starting node.
* @param {string} end - The destination node.
* @returns {Promise<number>} Total weight of the shortest path or Infinity if no path exists.
*/
export async function findShortestPath(graph, start, end) {
if (!graph || graph[start] === undefined || graph[end] === undefined) {
return Infinity;
}
if (start === end) {
return 0;
}
// Dynamically import js-priority-queue from a CDN (via jsDelivr +esm build)
if (!priorityQueueModulePromise) {
priorityQueueModulePromise = import(
'https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/+esm'
);
}
const { default: PriorityQueue } = await priorityQueueModulePromise;
const distances = Object.create(null);
const visited = new Set();
// Initialize distances
for (const node of Object.keys(graph)) {
distances[node] = Infinity;
}
distances[start] = 0;
const queue = new PriorityQueue({
comparator: (a, b) => a.distance - b.distance,
});
queue.queue({ node: start, distance: 0 });
while (queue.length > 0) {
const { node: currentNode, distance: currentDistance } = queue.dequeue();
if (visited.has(currentNode)) {
continue;
}
visited.add(currentNode);
if (currentNode === end) {
return currentDistance;
}
const neighbors = graph[currentNode] || {};
for (const [neighbor, weight] of Object.entries(neighbors)) {
if (weight < 0) {
throw new Error('Dijkstra\'s algorithm requires non-negative edge weights.');
}
const newDistance = currentDistance + weight;
if (newDistance < distances[neighbor]) {
distances[neighbor] = newDistance;
queue.queue({ node: neighbor, distance: newDistance });
}
}
}
return Infinity;
}