|
1 | | -import { describe, expect, it } from 'vitest'; |
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
2 | 2 | import { Graph } from '../../Graph.js'; |
3 | 3 | import { serializeGraph } from '../../utils/serializeGraph.js'; |
4 | 4 | import { shortestPath } from './shortestPath.js'; |
5 | 5 | import { shortestPaths } from './shortestPaths.js'; |
| 6 | +import { addWeightFunction } from './getPath.js'; |
| 7 | +import { NextWeightFnParams } from '../../types.js'; |
6 | 8 |
|
7 | 9 | describe("Dijkstra's Shortest Path Algorithm", function () { |
8 | 10 | it('Should compute shortest path on a single edge.', function () { |
@@ -104,3 +106,98 @@ describe("Dijkstra's Shortest Path Algorithm", function () { |
104 | 106 | expect(postSerializedGraph.links).toContainEqual({ source: 'f', target: 'c' }); |
105 | 107 | }); |
106 | 108 | }); |
| 109 | + |
| 110 | +describe('addWeightFunction', () => { |
| 111 | + it('should return edgeWeight if currentPathWeight is undefined', () => { |
| 112 | + const graph = new Graph(); |
| 113 | + const params = { |
| 114 | + edgeWeight: 5, currentPathWeight: undefined, hop: 1, |
| 115 | + graph: graph, path: { d: new Map(), p: new Map(), q: new Set() }, |
| 116 | + previousNode: 'a', currentNode: 'b' |
| 117 | + }; |
| 118 | + expect(addWeightFunction(params)).toBe(5); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should return the sum of edgeWeight and currentPathWeight', () => { |
| 122 | + const graph = new Graph() |
| 123 | + const params = { edgeWeight: 5, currentPathWeight: 10, hop: 1, |
| 124 | + graph: graph, path: { d: new Map(), p: new Map(), q: new Set() }, |
| 125 | + previousNode: 'a', currentNode: 'b' |
| 126 | + }; |
| 127 | + expect(addWeightFunction(params)).toBe(15); |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +describe('shortestPath with custom weight functions', () => { |
| 132 | + it('should compute shortest path with default weight function (sum of weights)', () => { |
| 133 | + const graph = new Graph().addEdge('a', 'b', 1).addEdge('b', 'c', 2); |
| 134 | + expect(shortestPath(graph, 'a', 'c')).toEqual({ |
| 135 | + nodes: ['a', 'b', 'c'], |
| 136 | + weight: 3, |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should compute shortest path with a custom weight function', () => { |
| 141 | + const customWeightFn = ({ edgeWeight, currentPathWeight, hop }: NextWeightFnParams) => { |
| 142 | + if (currentPathWeight === undefined) { |
| 143 | + return edgeWeight; |
| 144 | + } |
| 145 | + return currentPathWeight + edgeWeight ** hop; |
| 146 | + }; |
| 147 | + |
| 148 | + const graph = new Graph().addEdge('a', 'b', 2).addEdge('b', 'c', 3); |
| 149 | + expect(shortestPath(graph, 'a', 'c', customWeightFn)).toEqual({ |
| 150 | + nodes: ['a', 'b', 'c'], |
| 151 | + weight: 7, |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should pass correct parameters to custom weight function for a path with 3 nodes', () => { |
| 156 | + const customWeightFn = vi.fn(({ edgeWeight, currentPathWeight, hop }: NextWeightFnParams) => { |
| 157 | + if (currentPathWeight === undefined) { |
| 158 | + return edgeWeight; |
| 159 | + } |
| 160 | + return currentPathWeight + edgeWeight ** hop; |
| 161 | + }); |
| 162 | + |
| 163 | + const graph = new Graph().addEdge('a', 'b', 1).addEdge('b', 'c', 2); |
| 164 | + shortestPath(graph, 'a', 'c', customWeightFn); |
| 165 | + |
| 166 | + expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 2, currentPathWeight: undefined, hop: 1, |
| 167 | + graph: graph, currentNode: 'b', previousNode: 'c', |
| 168 | + path: { |
| 169 | + d: new Map([['a', 0], ['b', 1], ['c', 3]]), |
| 170 | + p: new Map([['b', 'a'], ['c', 'b']]), |
| 171 | + q: new Set(), |
| 172 | + }, |
| 173 | + }); |
| 174 | + expect(customWeightFn).toHaveBeenCalledWith({ edgeWeight: 1, currentPathWeight: 2, hop: 2, |
| 175 | + graph: graph, currentNode: 'a', previousNode: 'b', |
| 176 | + path: { |
| 177 | + d: new Map([['a', 0], ['b', 1], ['c', 3]]), |
| 178 | + p: new Map([['b', 'a'], ['c', 'b']]), |
| 179 | + q: new Set(), |
| 180 | + } |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should compute shortest path with a custom weight function in a graph with multiple paths', () => { |
| 185 | + const customWeightFn = ({ edgeWeight, currentPathWeight }: NextWeightFnParams) => { |
| 186 | + if (currentPathWeight === undefined) { |
| 187 | + return edgeWeight; |
| 188 | + } |
| 189 | + return edgeWeight + currentPathWeight; |
| 190 | + }; |
| 191 | + |
| 192 | + const graph = new Graph() |
| 193 | + .addEdge('a', 'b', 1) |
| 194 | + .addEdge('b', 'c', 2) |
| 195 | + .addEdge('a', 'd', 1) |
| 196 | + .addEdge('d', 'c', 1); |
| 197 | + |
| 198 | + expect(shortestPath(graph, 'a', 'c', customWeightFn)).toEqual({ |
| 199 | + nodes: ['a', 'd', 'c'], |
| 200 | + weight: 2, |
| 201 | + }); |
| 202 | + }); |
| 203 | +}); |
0 commit comments