|
| 1 | +import { johnson } from "../johnson"; |
| 2 | + |
| 3 | +describe("johnson", () => { |
| 4 | + |
| 5 | + const init_graph = (N: number): [number, number][][] => { |
| 6 | + let graph = Array(N); |
| 7 | + for (let i = 0; i < N; ++i) { |
| 8 | + graph[i] = []; |
| 9 | + } |
| 10 | + return graph; |
| 11 | + } |
| 12 | + |
| 13 | + const add_edge = (graph: [number, number][][], a: number, b: number, weight: number) => { |
| 14 | + graph[a].push([b, weight]); |
| 15 | + graph[b].push([a, weight]); |
| 16 | + } |
| 17 | + |
| 18 | + it("should return the correct value", () => { |
| 19 | + let graph = init_graph(9); |
| 20 | + add_edge(graph, 0, 1, 4); |
| 21 | + add_edge(graph, 0, 7, 8); |
| 22 | + add_edge(graph, 1, 2, 8); |
| 23 | + add_edge(graph, 1, 7, 11); |
| 24 | + add_edge(graph, 2, 3, 7); |
| 25 | + add_edge(graph, 2, 5, 4); |
| 26 | + add_edge(graph, 2, 8, 2); |
| 27 | + add_edge(graph, 3, 4, 9); |
| 28 | + add_edge(graph, 3, 5, 14); |
| 29 | + add_edge(graph, 4, 5, 10); |
| 30 | + add_edge(graph, 5, 6, 2); |
| 31 | + add_edge(graph, 6, 7, 1); |
| 32 | + add_edge(graph, 6, 8, 6); |
| 33 | + add_edge(graph, 7, 8, 7); |
| 34 | + |
| 35 | + let expected = [ |
| 36 | + [0, 4, 12, 19, 21, 11, 9, 8, 14], |
| 37 | + [4, 0, 8, 15, 22, 12, 12, 11, 10], |
| 38 | + [12, 8, 0, 7, 14, 4, 6, 7, 2], |
| 39 | + [19, 15, 7, 0, 9, 11, 13, 14, 9], |
| 40 | + [21, 22, 14, 9, 0, 10, 12, 13, 16], |
| 41 | + [11, 12, 4, 11, 10, 0, 2, 3, 6], |
| 42 | + [9, 12, 6, 13, 12, 2, 0, 1, 6], |
| 43 | + [8, 11, 7, 14, 13, 3, 1, 0, 7], |
| 44 | + [14, 10, 2, 9, 16, 6, 6, 7, 0] |
| 45 | + ] |
| 46 | + expect(johnson(graph)).toStrictEqual(expected); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should return the correct value for graph with negative weights", () => { |
| 50 | + let graph = init_graph(4); |
| 51 | + graph[0].push([1, -5]); |
| 52 | + graph[0].push([2, 2]); |
| 53 | + graph[0].push([3, 3]); |
| 54 | + graph[1].push([2, 4]); |
| 55 | + graph[2].push([3, 1]); |
| 56 | + |
| 57 | + let expected = [ |
| 58 | + [ 0, -5, -1, 0 ], |
| 59 | + [ Infinity, 0, 4, 5 ], |
| 60 | + [ Infinity, Infinity, 0, 1 ], |
| 61 | + [ Infinity, Infinity, Infinity, 0 ] |
| 62 | + ] |
| 63 | + expect(johnson(graph)).toStrictEqual(expected); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should return the undefined for two node graph with negative-weight cycle", () => { |
| 67 | + let graph = init_graph(2); |
| 68 | + add_edge(graph, 0, 1, -1); |
| 69 | + expect(johnson(graph)).toStrictEqual(undefined); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should return the undefined for three node graph with negative-weight cycle", () => { |
| 73 | + let graph = init_graph(3); |
| 74 | + graph[0].push([1, -1]); |
| 75 | + graph[0].push([2, 7]); |
| 76 | + graph[1].push([2, -5]); |
| 77 | + graph[2].push([0, 4]); |
| 78 | + expect(johnson(graph)).toStrictEqual(undefined); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should return the correct value for zero element graph", () => { |
| 82 | + expect(johnson([])).toStrictEqual([]); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should return the correct value for single element graph", () => { |
| 86 | + expect(johnson([[]])).toStrictEqual([[0]]); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should return the correct value for a linear graph", () => { |
| 90 | + let linear_graph = init_graph(4); |
| 91 | + add_edge(linear_graph, 0, 1, 1); |
| 92 | + add_edge(linear_graph, 1, 2, 2); |
| 93 | + add_edge(linear_graph, 2, 3, 3); |
| 94 | + |
| 95 | + let expected = [[0, 1, 3, 6 ], [1, 0, 2, 5], [3, 2, 0, 3], [6, 5, 3, 0]]; |
| 96 | + expect(johnson(linear_graph)).toStrictEqual(expected); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should return the correct value for a linear graph with unreachable node", () => { |
| 100 | + let linear_graph = init_graph(3); |
| 101 | + add_edge(linear_graph, 0, 1, 1); |
| 102 | + |
| 103 | + let expected = [[0, 1, Infinity], [1, 0, Infinity], [Infinity, Infinity, 0]]; |
| 104 | + expect(johnson(linear_graph)).toStrictEqual(expected); |
| 105 | + }); |
| 106 | +}) |
| 107 | + |
0 commit comments