|
| 1 | +import GraphVertex from '../../../../data-structures/graph/GraphVertex'; |
| 2 | +import GraphEdge from '../../../../data-structures/graph/GraphEdge'; |
| 3 | +import Graph from '../../../../data-structures/graph/Graph'; |
| 4 | +import dijkstra from '../dijkstra'; |
| 5 | + |
| 6 | +describe('dijkstra', () => { |
| 7 | + it('should find minimum paths to all vertices', () => { |
| 8 | + const vertexA = new GraphVertex('A'); |
| 9 | + const vertexB = new GraphVertex('B'); |
| 10 | + const vertexC = new GraphVertex('C'); |
| 11 | + const vertexD = new GraphVertex('D'); |
| 12 | + const vertexE = new GraphVertex('E'); |
| 13 | + const vertexF = new GraphVertex('F'); |
| 14 | + const vertexG = new GraphVertex('G'); |
| 15 | + const vertexH = new GraphVertex('H'); |
| 16 | + |
| 17 | + const edgeAB = new GraphEdge(vertexA, vertexB, 4); |
| 18 | + const edgeAE = new GraphEdge(vertexA, vertexE, 7); |
| 19 | + const edgeAC = new GraphEdge(vertexA, vertexC, 3); |
| 20 | + const edgeBC = new GraphEdge(vertexB, vertexC, 6); |
| 21 | + const edgeBD = new GraphEdge(vertexB, vertexD, 5); |
| 22 | + const edgeEC = new GraphEdge(vertexE, vertexC, 8); |
| 23 | + const edgeED = new GraphEdge(vertexE, vertexD, 2); |
| 24 | + const edgeDC = new GraphEdge(vertexD, vertexC, 11); |
| 25 | + const edgeDG = new GraphEdge(vertexD, vertexG, 10); |
| 26 | + const edgeDF = new GraphEdge(vertexD, vertexF, 2); |
| 27 | + const edgeFG = new GraphEdge(vertexF, vertexG, 3); |
| 28 | + const edgeEG = new GraphEdge(vertexE, vertexG, 5); |
| 29 | + |
| 30 | + const graph = new Graph(); |
| 31 | + graph |
| 32 | + .addVertex(vertexH) |
| 33 | + .addEdge(edgeAB) |
| 34 | + .addEdge(edgeAE) |
| 35 | + .addEdge(edgeAC) |
| 36 | + .addEdge(edgeBC) |
| 37 | + .addEdge(edgeBD) |
| 38 | + .addEdge(edgeEC) |
| 39 | + .addEdge(edgeED) |
| 40 | + .addEdge(edgeDC) |
| 41 | + .addEdge(edgeDG) |
| 42 | + .addEdge(edgeDF) |
| 43 | + .addEdge(edgeFG) |
| 44 | + .addEdge(edgeEG); |
| 45 | + |
| 46 | + const distances = dijkstra(graph, vertexA); |
| 47 | + |
| 48 | + expect(distances).toEqual({ |
| 49 | + H: Infinity, |
| 50 | + A: 0, |
| 51 | + B: 4, |
| 52 | + E: 7, |
| 53 | + C: 3, |
| 54 | + D: 9, |
| 55 | + G: 12, |
| 56 | + F: 11, |
| 57 | + }); |
| 58 | + }); |
| 59 | +}); |
0 commit comments