|
| 1 | +package com.thealgorithms.datastructures.graphs; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +/** |
| 10 | + * Unit tests for {@link JohnsonsAlgorithm} class. This class |
| 11 | + * contains test cases to verify the correct implementation of |
| 12 | + * various methods used in Johnson's Algorithm such as shortest path |
| 13 | + * calculations, graph reweighting, and more. |
| 14 | + */ |
| 15 | +class JohnsonsAlgorithmTest { |
| 16 | + |
| 17 | + // Constant representing infinity |
| 18 | + private static final double INF = Double.POSITIVE_INFINITY; |
| 19 | + |
| 20 | + /** |
| 21 | + * Tests the Johnson's Algorithm with a simple graph without negative edges. |
| 22 | + * Verifies that the algorithm returns the correct shortest path distances. |
| 23 | + */ |
| 24 | + @Test |
| 25 | + void testSimpleGraph() { |
| 26 | + // Test case for a simple graph without negative edges |
| 27 | + double[][] graph = {{0, 4, INF, INF}, {INF, 0, 1, INF}, {INF, INF, 0, 2}, {INF, INF, INF, 0}}; |
| 28 | + |
| 29 | + double[][] result = JohnsonsAlgorithm.johnsonAlgorithm(graph); |
| 30 | + |
| 31 | + double[][] expected = {{0, 4, 5, 7}, {INF, 0, 1, 3}, {INF, INF, 0, 2}, {INF, INF, INF, 0}}; |
| 32 | + |
| 33 | + assertArrayEquals(expected, result); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Tests Johnson's Algorithm on a graph with negative edges but no |
| 38 | + * negative weight cycles. Verifies the algorithm handles negative |
| 39 | + * edge weights correctly. |
| 40 | + */ |
| 41 | + @Test |
| 42 | + void testGraphWithNegativeEdges() { |
| 43 | + // Graph with negative edges but no negative weight cycles |
| 44 | + double[][] graph = {{0, -1, 4}, {INF, 0, 3}, {INF, INF, 0}}; |
| 45 | + |
| 46 | + double[][] result = JohnsonsAlgorithm.johnsonAlgorithm(graph); |
| 47 | + |
| 48 | + double[][] expected = {{0, INF, 4}, {INF, 0, 3}, {INF, INF, 0}}; |
| 49 | + |
| 50 | + assertArrayEquals(expected, result); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Tests the behavior of Johnson's Algorithm on a graph with a negative |
| 55 | + * weight cycle. Expects an IllegalArgumentException to be thrown |
| 56 | + * due to the presence of the cycle. |
| 57 | + */ |
| 58 | + @Test |
| 59 | + void testNegativeWeightCycle() { |
| 60 | + // Graph with a negative weight cycle |
| 61 | + double[][] graph = {{0, 1, INF}, {INF, 0, -1}, {-1, INF, 0}}; |
| 62 | + |
| 63 | + // Johnson's algorithm should throw an exception when a negative cycle is detected |
| 64 | + assertThrows(IllegalArgumentException.class, () -> { JohnsonsAlgorithm.johnsonAlgorithm(graph); }); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Tests Dijkstra's algorithm as a part of Johnson's algorithm implementation |
| 69 | + * on a small graph. Verifies that the shortest path is correctly calculated. |
| 70 | + */ |
| 71 | + @Test |
| 72 | + void testDijkstra() { |
| 73 | + // Testing Dijkstra's algorithm with a small graph |
| 74 | + double[][] graph = {{0, 1, 2}, {INF, 0, 3}, {INF, INF, 0}}; |
| 75 | + |
| 76 | + double[] modifiedWeights = {0, 0, 0}; // No reweighting in this simple case |
| 77 | + |
| 78 | + double[] result = JohnsonsAlgorithm.dijkstra(graph, 0, modifiedWeights); |
| 79 | + double[] expected = {0, 1, 2}; |
| 80 | + |
| 81 | + assertArrayEquals(expected, result); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Tests the conversion of an adjacency matrix to an edge list. |
| 86 | + * Verifies that the conversion process generates the correct edge list. |
| 87 | + */ |
| 88 | + @Test |
| 89 | + void testEdgeListConversion() { |
| 90 | + // Test the conversion of adjacency matrix to edge list |
| 91 | + double[][] graph = {{0, 5, INF}, {INF, 0, 2}, {INF, INF, 0}}; |
| 92 | + |
| 93 | + // Running convertToEdgeList |
| 94 | + double[][] edges = JohnsonsAlgorithm.convertToEdgeList(graph); |
| 95 | + |
| 96 | + // Expected edge list: (0 -> 1, weight 5), (1 -> 2, weight 2) |
| 97 | + double[][] expected = {{0, 1, 5}, {1, 2, 2}}; |
| 98 | + |
| 99 | + // Verify the edge list matches the expected values |
| 100 | + assertArrayEquals(expected, edges); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Tests the reweighting of a graph as a part of Johnson's Algorithm. |
| 105 | + * Verifies that the reweighted graph produces correct results. |
| 106 | + */ |
| 107 | + @Test |
| 108 | + void testReweightGraph() { |
| 109 | + // Test reweighting of the graph |
| 110 | + double[][] graph = {{0, 2, 9}, {INF, 0, 1}, {INF, INF, 0}}; |
| 111 | + double[] modifiedWeights = {1, 2, 3}; // Arbitrary weight function |
| 112 | + |
| 113 | + double[][] reweightedGraph = JohnsonsAlgorithm.reweightGraph(graph, modifiedWeights); |
| 114 | + |
| 115 | + // Expected reweighted graph: |
| 116 | + double[][] expected = {{0, 1, 7}, {INF, 0, 0}, {INF, INF, 0}}; |
| 117 | + |
| 118 | + assertArrayEquals(expected, reweightedGraph); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Tests the minDistance method used in Dijkstra's algorithm to find |
| 123 | + * the vertex with the minimum distance that has not yet been visited. |
| 124 | + */ |
| 125 | + @Test |
| 126 | + void testMinDistance() { |
| 127 | + // Test minDistance method |
| 128 | + double[] dist = {INF, 3, 1, INF}; |
| 129 | + boolean[] visited = {false, false, false, false}; |
| 130 | + |
| 131 | + int minIndex = JohnsonsAlgorithm.minDistance(dist, visited); |
| 132 | + |
| 133 | + // The vertex with minimum distance is vertex 2 with a distance of 1 |
| 134 | + assertEquals(2, minIndex); |
| 135 | + } |
| 136 | +} |
0 commit comments