diff --git a/README b/README index f9060bf..e3b655a 100644 --- a/README +++ b/README @@ -28,19 +28,19 @@ The list below shows the pass/fail status and execution time for each model on e **google/gemini-2.5-pro** -- 1_dijkstra: ❌ Fail (0.213s) +- 1_dijkstra: ❌ Fail (0.036s) - 2_convex_hull: ⚪ Not Run - 3_lis: ⚪ Not Run - 4_determinant: ⚪ Not Run **anthropic/claude-sonnet-4.5** -- 1_dijkstra: ❌ Fail (0.189s) +- 1_dijkstra: ❌ Fail (0.032s) - 2_convex_hull: ⚪ Not Run - 3_lis: ⚪ Not Run - 4_determinant: ⚪ Not Run **openai/gpt-5-codex** -- 1_dijkstra: ❌ Fail (0.245s) +- 1_dijkstra: ❌ Fail (0.031s) - 2_convex_hull: ⚪ Not Run - 3_lis: ⚪ Not Run - 4_determinant: ⚪ Not Run diff --git a/tests/1_dijkstra/outputs/anthropic_claude-sonnet-4.5_2025-10-13T13-07-02.635Z.js b/tests/1_dijkstra/outputs/anthropic_claude-sonnet-4.5_2025-10-13T13-07-02.635Z.js new file mode 100644 index 0000000..4b79882 --- /dev/null +++ b/tests/1_dijkstra/outputs/anthropic_claude-sonnet-4.5_2025-10-13T13-07-02.635Z.js @@ -0,0 +1,87 @@ +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 map - all nodes start at Infinity except start node + const distances = {}; + for (let node in graph) { + distances[node] = Infinity; + } + distances[start] = 0; + + // Create a min-heap priority queue (lower distance = higher priority) + const pq = new PriorityQueue({ + comparator: (a, b) => a.distance - b.distance + }); + + // Track visited nodes to avoid reprocessing + const visited = new Set(); + + // Start with the initial node + 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; + } + + // Check if current node exists in graph + if (!graph[currentNode]) { + continue; + } + + // Explore neighbors + for (let neighbor in graph[currentNode]) { + if (!visited.has(neighbor)) { + const weight = graph[currentNode][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 to the end node + return Infinity; +} + +// Example usage: +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 } +}; + +findShortestPath(graph, 'A', 'D').then(distance => { + console.log(`Shortest path from A to D: ${distance}`); // Output: 4 (A -> B -> C -> D) +}); + +// Example with no path +const disconnectedGraph = { + 'A': { 'B': 1 }, + 'B': { 'A': 1 }, + 'C': { 'D': 1 }, + 'D': { 'C': 1 } +}; + +findShortestPath(disconnectedGraph, 'A', 'D').then(distance => { + console.log(`Shortest path from A to D: ${distance}`); // Output: Infinity +}); \ No newline at end of file diff --git a/tests/1_dijkstra/outputs/google_gemini-2.5-pro_2025-10-13T13-06-49.377Z.js b/tests/1_dijkstra/outputs/google_gemini-2.5-pro_2025-10-13T13-06-49.377Z.js new file mode 100644 index 0000000..14c07ff --- /dev/null +++ b/tests/1_dijkstra/outputs/google_gemini-2.5-pro_2025-10-13T13-06-49.377Z.js @@ -0,0 +1,117 @@ +/** + * Finds the shortest path in a weighted, undirected graph using Dijkstra's algorithm. + * This function dynamically imports a Priority Queue library from a CDN. + * + * @param {object} graph - The graph structure, e.g., { 'A': { 'B': 1, 'C': 4 }, 'B': { ... } }. + * @param {string} startNode - The starting node for the path. + * @param {string} endNode - The destination node for the path. + * @returns {Promise} A promise that resolves to the total weight of the shortest path, or Infinity if no path exists. + */ +async function findShortestPath(graph, startNode, endNode) { + // 1. Dynamically import the priority queue library from a CDN. + // The 'js-priority-queue' library is a UMD module, so the main export + // is often on the 'default' property when imported this way. + let PriorityQueue; + try { + const PriorityQueueModule = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/priority-queue.min.js'); + PriorityQueue = PriorityQueueModule.default; + } catch (error) { + console.error("Failed to load the Priority Queue library:", error); + // Cannot proceed without the library, so we throw an error. + throw new Error("Could not load a required dependency."); + } + + + // 2. Initialize data structures for Dijkstra's algorithm. + const distances = {}; // Stores the shortest known distance from the startNode to every other node. + const previous = {}; // Stores the preceding node in the shortest path. + + // A min-priority queue to store nodes to visit, prioritized by distance. + const pq = new PriorityQueue({ comparator: (a, b) => a.priority - b.priority }); + + // 3. Set initial state. + Object.keys(graph).forEach(node => { + if (node === startNode) { + distances[node] = 0; + pq.queue({ value: node, priority: 0 }); // Add the start node to the queue with priority 0. + } else { + distances[node] = Infinity; + } + previous[node] = null; + }); + + // 4. Main loop of the algorithm. + while (pq.length > 0) { + // Get the node with the smallest distance from the queue. + const { value: currentNode } = pq.dequeue(); + + // If we've reached the end node, we have found the shortest path. + if (currentNode === endNode) { + // If a path exists, the distance will be a finite number. + if (distances[endNode] !== Infinity) { + return distances[endNode]; + } + // This break is for the case where endNode is unreachable but was queued + // with an infinite priority (which shouldn't happen with this logic, but is safe). + break; + } + + // Explore neighbors of the current node. + if (graph[currentNode]) { + for (const neighbor in graph[currentNode]) { + const weight = graph[currentNode][neighbor]; + const newPathWeight = distances[currentNode] + weight; + + // If we've found a shorter path to this neighbor... + if (newPathWeight < distances[neighbor]) { + // ...update its distance, record the path, and add it to the queue. + distances[neighbor] = newPathWeight; + previous[neighbor] = currentNode; + pq.queue({ value: neighbor, priority: newPathWeight }); + } + } + } + } + + // 5. If the loop finishes and we haven't returned, the endNode was not reachable. + return Infinity; +} + + +// --- Example Usage --- + +// Define our weighted, undirected graph. +const graph = { + 'A': { 'B': 1, 'C': 4 }, + 'B': { 'A': 1, 'C': 2, 'D': 5 }, + 'C': { 'A': 4, 'B': 2, 'E': 3 }, + 'D': { 'B': 5, 'E': 1 }, + 'E': { 'C': 3, 'D': 1 }, + 'F': { 'G': 1 }, // An unconnected component + 'G': { 'F': 1 } +}; + +// We use an async IIFE (Immediately Invoked Function Expression) to run our async function. +(async () => { + try { + console.log("Finding shortest path from A to E..."); + const pathWeight1 = await findShortestPath(graph, 'A', 'E'); + // Expected Path: A -> B -> C -> E, Weight: 1 + 2 + 3 = 6 + console.log(`Shortest path weight from A to E is: ${pathWeight1}`); // Output: 6 + + console.log("\nFinding shortest path from A to D..."); + const pathWeight2 = await findShortestPath(graph, 'A', 'D'); + // Expected Path: A -> B -> D, Weight: 1 + 5 = 6 + console.log(`Shortest path weight from A to D is: ${pathWeight2}`); // Output: 6 + + console.log("\nFinding shortest path from A to A..."); + const pathWeight3 = await findShortestPath(graph, 'A', 'A'); + console.log(`Shortest path weight from A to A is: ${pathWeight3}`); // Output: 0 + + console.log("\nFinding shortest path from A to F (unreachable)..."); + const pathWeight4 = await findShortestPath(graph, 'A', 'F'); + console.log(`Shortest path weight from A to F is: ${pathWeight4}`); // Output: Infinity + } catch(err) { + console.error("An error occurred during pathfinding:", err); + } +})(); \ No newline at end of file diff --git a/tests/1_dijkstra/outputs/openai_gpt-5-codex_2025-10-13T13-07-35.085Z.js b/tests/1_dijkstra/outputs/openai_gpt-5-codex_2025-10-13T13-07-35.085Z.js new file mode 100644 index 0000000..bc940a3 --- /dev/null +++ b/tests/1_dijkstra/outputs/openai_gpt-5-codex_2025-10-13T13-07-35.085Z.js @@ -0,0 +1,70 @@ +/** + * Finds the total weight of the shortest path between two nodes in a weighted, undirected graph. + * Uses Dijkstra's algorithm with a priority queue loaded dynamically from a CDN. + * + * @param {Object>} graph - Adjacency list representing the graph. + * @param {string} start - Starting node id. + * @param {string} end - Target node id. + * @returns {Promise} - Total weight of the shortest path, or Infinity if no path exists. + */ +const findShortestPath = async (graph, start, end) => { + // Dynamically import the priority queue implementation from jsDelivr + const { default: PriorityQueue } = await import( + 'https://cdn.jsdelivr.net/npm/js-priority-queue@1.0.0?module' + ); + + if (!graph || !graph.hasOwnProperty(start) || !graph.hasOwnProperty(end)) { + return Infinity; + } + if (start === end) { + return 0; + } + + const distances = {}; + const visited = new Set(); + + for (const node of Object.keys(graph)) { + distances[node] = Infinity; + } + distances[start] = 0; + + const pq = new PriorityQueue({ + comparator: (a, b) => a.distance - b.distance, + }); + + pq.queue({ node: start, distance: 0 }); + + while (pq.length > 0) { + const { node, distance } = pq.dequeue(); + + if (visited.has(node)) continue; + visited.add(node); + + if (node === end) { + return distance; + } + + for (const [neighbor, weight] of Object.entries(graph[node] || {})) { + if (visited.has(neighbor)) continue; + + const newDist = distance + weight; + if (newDist < distances[neighbor]) { + distances[neighbor] = newDist; + pq.queue({ node: neighbor, distance: newDist }); + } + } + } + + 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 } +// }; +// console.log(await findShortestPath(graph, 'A', 'D')); // Output: 4 +// })(); \ No newline at end of file