|
| 1 | +package com.thealgorithms.datastructures.graphs; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import com.thealgorithms.datastructures.graphs.WelshPowell.Graph; |
| 8 | +import java.util.Arrays; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +class WelshPowellTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + void testSimpleGraph() { |
| 15 | + final var graph = WelshPowell.makeGraph(4, new int[][] {{0, 1}, {1, 2}, {2, 3}}); |
| 16 | + int[] colors = WelshPowell.findColoring(graph); |
| 17 | + assertTrue(isColoringValid(graph, colors)); |
| 18 | + assertEquals(2, countDistinctColors(colors)); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + void testDisconnectedGraph() { |
| 23 | + final var graph = WelshPowell.makeGraph(3, new int[][] {}); // No edges |
| 24 | + int[] colors = WelshPowell.findColoring(graph); |
| 25 | + assertTrue(isColoringValid(graph, colors)); |
| 26 | + assertEquals(1, countDistinctColors(colors)); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void testCompleteGraph() { |
| 31 | + final var graph = WelshPowell.makeGraph(3, new int[][] {{0, 1}, {1, 2}, {2, 0}}); |
| 32 | + int[] colors = WelshPowell.findColoring(graph); |
| 33 | + assertTrue(isColoringValid(graph, colors)); |
| 34 | + assertEquals(3, countDistinctColors(colors)); |
| 35 | + } |
| 36 | + |
| 37 | + // The following test originates from the following website : https://www.geeksforgeeks.org/welsh-powell-graph-colouring-algorithm/ |
| 38 | + @Test |
| 39 | + void testComplexGraph() { |
| 40 | + int[][] edges = { |
| 41 | + {0, 7}, // A-H |
| 42 | + {0, 1}, // A-B |
| 43 | + {1, 3}, // B-D |
| 44 | + {2, 3}, // C-D |
| 45 | + {3, 8}, // D-I |
| 46 | + {3, 10}, // D-K |
| 47 | + {4, 10}, // E-K |
| 48 | + {4, 5}, // E-F |
| 49 | + {5, 6}, // F-G |
| 50 | + {6, 10}, // G-K |
| 51 | + {6, 7}, // G-H |
| 52 | + {7, 8}, // H-I |
| 53 | + {7, 9}, // H-J |
| 54 | + {7, 10}, // H-K |
| 55 | + {8, 9}, // I-J |
| 56 | + {9, 10}, // J-K |
| 57 | + }; |
| 58 | + |
| 59 | + final var graph = WelshPowell.makeGraph(11, edges); // 11 vertices from A (0) to K (10) |
| 60 | + int[] colors = WelshPowell.findColoring(graph); |
| 61 | + |
| 62 | + assertTrue(isColoringValid(graph, colors), "The coloring should be valid with no adjacent vertices sharing the same color."); |
| 63 | + assertEquals(3, countDistinctColors(colors), "The chromatic number of the graph should be 3."); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void testNegativeVertices() { |
| 68 | + assertThrows(IllegalArgumentException.class, () -> { WelshPowell.makeGraph(-1, new int[][] {}); }, "Number of vertices cannot be negative"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void testSelfLoop() { |
| 73 | + assertThrows(IllegalArgumentException.class, () -> { WelshPowell.makeGraph(3, new int[][] {{0, 0}}); }, "Self-loops are not allowed"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testInvalidVertex() { |
| 78 | + assertThrows(IllegalArgumentException.class, () -> { WelshPowell.makeGraph(3, new int[][] {{0, 3}}); }, "Vertex out of bounds"); |
| 79 | + assertThrows(IllegalArgumentException.class, () -> { WelshPowell.makeGraph(3, new int[][] {{0, -1}}); }, "Vertex out of bounds"); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void testInvalidEdgeArray() { |
| 84 | + assertThrows(IllegalArgumentException.class, () -> { WelshPowell.makeGraph(3, new int[][] {{0}}); }, "Edge array must have exactly two elements"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void testWithPreColoredVertex() { |
| 89 | + // Create a linear graph with 4 vertices and edges connecting them in sequence |
| 90 | + final var graph = WelshPowell.makeGraph(4, new int[][] {{0, 1}, {1, 2}, {2, 3}}); |
| 91 | + |
| 92 | + // Apply the Welsh-Powell coloring algorithm to the graph |
| 93 | + int[] colors = WelshPowell.findColoring(graph); |
| 94 | + |
| 95 | + // Validate that the coloring is correct (no two adjacent vertices have the same color) |
| 96 | + assertTrue(isColoringValid(graph, colors)); |
| 97 | + |
| 98 | + // Check if the algorithm has used at least 2 colors (expected for a linear graph) |
| 99 | + assertTrue(countDistinctColors(colors) >= 2); |
| 100 | + |
| 101 | + // Verify that all vertices have been assigned a color |
| 102 | + for (int color : colors) { |
| 103 | + assertTrue(color >= 0); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private boolean isColoringValid(Graph graph, int[] colors) { |
| 108 | + if (Arrays.stream(colors).anyMatch(n -> n < 0)) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + for (int i = 0; i < graph.getNumVertices(); i++) { |
| 112 | + for (int neighbor : graph.getAdjacencyList(i)) { |
| 113 | + if (i != neighbor && colors[i] == colors[neighbor]) { |
| 114 | + return false; // Adjacent vertices have the same color |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + return true; // No adjacent vertices share the same color |
| 119 | + } |
| 120 | + |
| 121 | + private int countDistinctColors(int[] colors) { |
| 122 | + return (int) Arrays.stream(colors).distinct().count(); |
| 123 | + } |
| 124 | +} |
0 commit comments