|
| 1 | +import {dijkstra, reconstructPath} from './dijkstra'; |
| 2 | + |
| 3 | +describe('Dijkstra Algorithm', () => { |
| 4 | + test('should find the shortest paths in a simple graph', () => { |
| 5 | + const graph = [ |
| 6 | + [ |
| 7 | + {to: 1, weight: 1}, |
| 8 | + {to: 2, weight: 4}, |
| 9 | + ], |
| 10 | + [ |
| 11 | + {to: 2, weight: 2}, |
| 12 | + {to: 3, weight: 5}, |
| 13 | + ], |
| 14 | + [{to: 3, weight: 1}], |
| 15 | + [], |
| 16 | + ]; |
| 17 | + const {distances, predecessors} = dijkstra(graph, 0); |
| 18 | + expect(distances).toEqual([0, 1, 3, 4]); |
| 19 | + expect(predecessors).toEqual([-1, 0, 1, 2]); |
| 20 | + }); |
| 21 | + test('should handle unreachable vertices', () => { |
| 22 | + const graph = [[{to: 1, weight: 1}], [{to: 2, weight: 2}], [], []]; |
| 23 | + const {distances, predecessors} = dijkstra(graph, 0); |
| 24 | + expect(distances).toEqual([0, 1, 3, Infinity]); |
| 25 | + expect(predecessors).toEqual([-1, 0, 1, -1]); |
| 26 | + }); |
| 27 | + test('should throw error for negative edge weights', () => { |
| 28 | + const graph = [ |
| 29 | + [ |
| 30 | + {to: 1, weight: 1}, |
| 31 | + {to: 2, weight: -2}, |
| 32 | + ], |
| 33 | + [], |
| 34 | + [], |
| 35 | + ]; |
| 36 | + expect(() => dijkstra(graph, 0)).toThrow( |
| 37 | + "Dijkstra's algorithm does not work with negative edge weights", |
| 38 | + ); |
| 39 | + }); |
| 40 | + test('should handle a graph with a single vertex', () => { |
| 41 | + const graph = [[]]; |
| 42 | + const {distances, predecessors} = dijkstra(graph, 0); |
| 43 | + expect(distances).toEqual([0]); |
| 44 | + expect(predecessors).toEqual([-1]); |
| 45 | + }); |
| 46 | + test('should handle a graph with multiple paths to the same vertex', () => { |
| 47 | + const graph = [ |
| 48 | + [ |
| 49 | + {to: 1, weight: 1}, |
| 50 | + {to: 2, weight: 2}, |
| 51 | + ], |
| 52 | + [{to: 3, weight: 4}], |
| 53 | + [{to: 3, weight: 3}], |
| 54 | + [], |
| 55 | + ]; |
| 56 | + const {distances, predecessors} = dijkstra(graph, 0); |
| 57 | + expect(distances[3]).toBe(5); |
| 58 | + expect(predecessors[3]).toBe(1); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe('reconstructPath', () => { |
| 63 | + test('should reconstruct the correct path', () => { |
| 64 | + const predecessors = [-1, 0, 1, 2]; |
| 65 | + const path = reconstructPath(0, 3, predecessors); |
| 66 | + expect(path).toEqual([0, 1, 2, 3]); |
| 67 | + }); |
| 68 | + test('should handle direct paths', () => { |
| 69 | + const predecessors = [-1, 0, 0, 0]; |
| 70 | + const path = reconstructPath(0, 1, predecessors); |
| 71 | + expect(path).toEqual([0, 1]); |
| 72 | + }); |
| 73 | + test('should return null for unreachable vertices', () => { |
| 74 | + const predecessors = [-1, 0, 1, -1]; |
| 75 | + const path = reconstructPath(0, 3, predecessors); |
| 76 | + expect(path).toBeNull(); |
| 77 | + }); |
| 78 | + test('should handle path from vertex to itself', () => { |
| 79 | + const predecessors = [-1, 0, 1, 2]; |
| 80 | + const path = reconstructPath(0, 0, predecessors); |
| 81 | + expect(path).toEqual([0]); |
| 82 | + }); |
| 83 | + test('should handle invalid target vertex', () => { |
| 84 | + const predecessors = [-1, 0, 1, 2]; |
| 85 | + const path = reconstructPath(0, 4, predecessors); |
| 86 | + expect(path).toBeNull(); |
| 87 | + }); |
| 88 | +}); |
0 commit comments