|
| 1 | +package com.thealgorithms.graph; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +public class TravelingSalesmanTest { |
| 8 | + |
| 9 | + // Test Case 1: A simple distance matrix with 4 cities |
| 10 | + @Test |
| 11 | + public void testBruteForceSimple() { |
| 12 | + int[][] distanceMatrix = {{0, 10, 15, 20}, {10, 0, 35, 25}, {15, 35, 0, 30}, {20, 25, 30, 0}}; |
| 13 | + int expectedMinDistance = 80; |
| 14 | + int result = TravelingSalesman.bruteForce(distanceMatrix); |
| 15 | + assertEquals(expectedMinDistance, result); |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + public void testDynamicProgrammingSimple() { |
| 20 | + int[][] distanceMatrix = {{0, 10, 15, 20}, {10, 0, 35, 25}, {15, 35, 0, 30}, {20, 25, 30, 0}}; |
| 21 | + int expectedMinDistance = 80; |
| 22 | + int result = TravelingSalesman.dynamicProgramming(distanceMatrix); |
| 23 | + assertEquals(expectedMinDistance, result); |
| 24 | + } |
| 25 | + |
| 26 | + // Test Case 2: A distance matrix with 3 cities |
| 27 | + @Test |
| 28 | + public void testBruteForceThreeCities() { |
| 29 | + int[][] distanceMatrix = {{0, 10, 15}, {10, 0, 35}, {15, 35, 0}}; |
| 30 | + int expectedMinDistance = 60; |
| 31 | + int result = TravelingSalesman.bruteForce(distanceMatrix); |
| 32 | + assertEquals(expectedMinDistance, result); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testDynamicProgrammingThreeCities() { |
| 37 | + int[][] distanceMatrix = {{0, 10, 15}, {10, 0, 35}, {15, 35, 0}}; |
| 38 | + int expectedMinDistance = 60; |
| 39 | + int result = TravelingSalesman.dynamicProgramming(distanceMatrix); |
| 40 | + assertEquals(expectedMinDistance, result); |
| 41 | + } |
| 42 | + |
| 43 | + // Test Case 3: A distance matrix with 5 cities (larger input) |
| 44 | + @Test |
| 45 | + public void testBruteForceFiveCities() { |
| 46 | + int[][] distanceMatrix = {{0, 2, 9, 10, 1}, {2, 0, 6, 5, 8}, {9, 6, 0, 4, 3}, {10, 5, 4, 0, 7}, {1, 8, 3, 7, 0}}; |
| 47 | + int expectedMinDistance = 15; |
| 48 | + int result = TravelingSalesman.bruteForce(distanceMatrix); |
| 49 | + assertEquals(expectedMinDistance, result); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testDynamicProgrammingFiveCities() { |
| 54 | + int[][] distanceMatrix = {{0, 2, 9, 10, 1}, {2, 0, 6, 5, 8}, {9, 6, 0, 4, 3}, {10, 5, 4, 0, 7}, {1, 8, 3, 7, 0}}; |
| 55 | + int expectedMinDistance = 15; |
| 56 | + int result = TravelingSalesman.dynamicProgramming(distanceMatrix); |
| 57 | + assertEquals(expectedMinDistance, result); |
| 58 | + } |
| 59 | + |
| 60 | + // Test Case 4: A distance matrix with 2 cities (simple case) |
| 61 | + @Test |
| 62 | + public void testBruteForceTwoCities() { |
| 63 | + int[][] distanceMatrix = {{0, 1}, {1, 0}}; |
| 64 | + int expectedMinDistance = 2; |
| 65 | + int result = TravelingSalesman.bruteForce(distanceMatrix); |
| 66 | + assertEquals(expectedMinDistance, result); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testDynamicProgrammingTwoCities() { |
| 71 | + int[][] distanceMatrix = {{0, 1}, {1, 0}}; |
| 72 | + int expectedMinDistance = 2; |
| 73 | + int result = TravelingSalesman.dynamicProgramming(distanceMatrix); |
| 74 | + assertEquals(expectedMinDistance, result); |
| 75 | + } |
| 76 | + |
| 77 | + // Test Case 5: A distance matrix with identical distances |
| 78 | + @Test |
| 79 | + public void testBruteForceEqualDistances() { |
| 80 | + int[][] distanceMatrix = {{0, 10, 10, 10}, {10, 0, 10, 10}, {10, 10, 0, 10}, {10, 10, 10, 0}}; |
| 81 | + int expectedMinDistance = 40; |
| 82 | + int result = TravelingSalesman.bruteForce(distanceMatrix); |
| 83 | + assertEquals(expectedMinDistance, result); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testDynamicProgrammingEqualDistances() { |
| 88 | + int[][] distanceMatrix = {{0, 10, 10, 10}, {10, 0, 10, 10}, {10, 10, 0, 10}, {10, 10, 10, 0}}; |
| 89 | + int expectedMinDistance = 40; |
| 90 | + int result = TravelingSalesman.dynamicProgramming(distanceMatrix); |
| 91 | + assertEquals(expectedMinDistance, result); |
| 92 | + } |
| 93 | + |
| 94 | + // Test Case 6: A distance matrix with only one city |
| 95 | + @Test |
| 96 | + public void testBruteForceOneCity() { |
| 97 | + int[][] distanceMatrix = {{0}}; |
| 98 | + int expectedMinDistance = 0; |
| 99 | + int result = TravelingSalesman.bruteForce(distanceMatrix); |
| 100 | + assertEquals(expectedMinDistance, result); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testDynamicProgrammingOneCity() { |
| 105 | + int[][] distanceMatrix = {{0}}; |
| 106 | + int expectedMinDistance = 0; |
| 107 | + int result = TravelingSalesman.dynamicProgramming(distanceMatrix); |
| 108 | + assertEquals(expectedMinDistance, result); |
| 109 | + } |
| 110 | + |
| 111 | + // Test Case 7: Distance matrix with large numbers |
| 112 | + @Test |
| 113 | + public void testBruteForceLargeNumbers() { |
| 114 | + int[][] distanceMatrix = {{0, 1000000, 2000000}, {1000000, 0, 1500000}, {2000000, 1500000, 0}}; |
| 115 | + int expectedMinDistance = 4500000; |
| 116 | + int result = TravelingSalesman.bruteForce(distanceMatrix); |
| 117 | + assertEquals(expectedMinDistance, result); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testDynamicProgrammingLargeNumbers() { |
| 122 | + int[][] distanceMatrix = {{0, 1000000, 2000000}, {1000000, 0, 1500000}, {2000000, 1500000, 0}}; |
| 123 | + int expectedMinDistance = 4500000; |
| 124 | + int result = TravelingSalesman.dynamicProgramming(distanceMatrix); |
| 125 | + assertEquals(expectedMinDistance, result); |
| 126 | + } |
| 127 | +} |
0 commit comments