|
| 1 | +package com.thealgorithms.graph; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +class EdmondsTest { |
| 11 | + |
| 12 | + @Test |
| 13 | + void testSimpleGraphNoCycle() { |
| 14 | + int n = 4; |
| 15 | + int root = 0; |
| 16 | + List<Edmonds.Edge> edges = new ArrayList<>(); |
| 17 | + edges.add(new Edmonds.Edge(0, 1, 10)); |
| 18 | + edges.add(new Edmonds.Edge(0, 2, 1)); |
| 19 | + edges.add(new Edmonds.Edge(2, 1, 2)); |
| 20 | + edges.add(new Edmonds.Edge(2, 3, 5)); |
| 21 | + |
| 22 | + // Expected arborescence edges: (0,2), (2,1), (2,3) |
| 23 | + // Weights: 1 + 2 + 5 = 8 |
| 24 | + long result = Edmonds.findMinimumSpanningArborescence(n, edges, root); |
| 25 | + assertEquals(8, result); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void testGraphWithOneCycle() { |
| 30 | + int n = 4; |
| 31 | + int root = 0; |
| 32 | + List<Edmonds.Edge> edges = new ArrayList<>(); |
| 33 | + edges.add(new Edmonds.Edge(0, 1, 10)); |
| 34 | + edges.add(new Edmonds.Edge(2, 1, 4)); |
| 35 | + edges.add(new Edmonds.Edge(1, 2, 5)); |
| 36 | + edges.add(new Edmonds.Edge(2, 3, 6)); |
| 37 | + |
| 38 | + // Min edges: (2,1, w=4), (1,2, w=5), (2,3, w=6) |
| 39 | + // Cycle: 1 -> 2 -> 1, cost = 4 + 5 = 9 |
| 40 | + // Contract {1,2} to C. |
| 41 | + // New edge (0,C) with w = 10 - min_in(1) = 10 - 4 = 6 |
| 42 | + // New edge (C,3) with w = 6 |
| 43 | + // Contracted MSA cost = 6 + 6 = 12 |
| 44 | + // Total cost = cycle_cost + contracted_msa_cost = 9 + 12 = 21 |
| 45 | + long result = Edmonds.findMinimumSpanningArborescence(n, edges, root); |
| 46 | + assertEquals(21, result); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void testComplexGraphWithCycle() { |
| 51 | + int n = 6; |
| 52 | + int root = 0; |
| 53 | + List<Edmonds.Edge> edges = new ArrayList<>(); |
| 54 | + edges.add(new Edmonds.Edge(0, 1, 10)); |
| 55 | + edges.add(new Edmonds.Edge(0, 2, 20)); |
| 56 | + edges.add(new Edmonds.Edge(1, 2, 5)); |
| 57 | + edges.add(new Edmonds.Edge(2, 3, 10)); |
| 58 | + edges.add(new Edmonds.Edge(3, 1, 3)); |
| 59 | + edges.add(new Edmonds.Edge(1, 4, 7)); |
| 60 | + edges.add(new Edmonds.Edge(3, 4, 2)); |
| 61 | + edges.add(new Edmonds.Edge(4, 5, 5)); |
| 62 | + |
| 63 | + // Min edges: (3,1,3), (1,2,5), (2,3,10), (3,4,2), (4,5,5) |
| 64 | + // Cycle: 1->2->3->1, cost = 5+10+3=18 |
| 65 | + // Contract {1,2,3} to C. |
| 66 | + // Edge (0,1,10) -> (0,C), w = 10-3=7 |
| 67 | + // Edge (0,2,20) -> (0,C), w = 20-5=15. Min is 7. |
| 68 | + // Edge (1,4,7) -> (C,4,7) |
| 69 | + // Edge (3,4,2) -> (C,4,2). Min is 2. |
| 70 | + // Edge (4,5,5) -> (4,5,5) |
| 71 | + // Contracted MSA: (0,C,7), (C,4,2), (4,5,5). Cost = 7+2+5=14 |
| 72 | + // Total cost = 18 + 14 = 32 |
| 73 | + long result = Edmonds.findMinimumSpanningArborescence(n, edges, root); |
| 74 | + assertEquals(32, result); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void testUnreachableNode() { |
| 79 | + int n = 4; |
| 80 | + int root = 0; |
| 81 | + List<Edmonds.Edge> edges = new ArrayList<>(); |
| 82 | + edges.add(new Edmonds.Edge(0, 1, 10)); |
| 83 | + edges.add(new Edmonds.Edge(2, 3, 5)); // Node 2 and 3 are unreachable from root 0 |
| 84 | + |
| 85 | + long result = Edmonds.findMinimumSpanningArborescence(n, edges, root); |
| 86 | + assertEquals(-1, result); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void testNoEdgesToNonRootNodes() { |
| 91 | + int n = 3; |
| 92 | + int root = 0; |
| 93 | + List<Edmonds.Edge> edges = new ArrayList<>(); |
| 94 | + edges.add(new Edmonds.Edge(0, 1, 10)); // Node 2 is unreachable |
| 95 | + |
| 96 | + long result = Edmonds.findMinimumSpanningArborescence(n, edges, root); |
| 97 | + assertEquals(-1, result); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void testSingleNode() { |
| 102 | + int n = 1; |
| 103 | + int root = 0; |
| 104 | + List<Edmonds.Edge> edges = new ArrayList<>(); |
| 105 | + |
| 106 | + long result = Edmonds.findMinimumSpanningArborescence(n, edges, root); |
| 107 | + assertEquals(0, result); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void testInvalidInputThrowsException() { |
| 112 | + List<Edmonds.Edge> edges = new ArrayList<>(); |
| 113 | + |
| 114 | + assertThrows(IllegalArgumentException.class, () -> Edmonds.findMinimumSpanningArborescence(0, edges, 0)); |
| 115 | + assertThrows(IllegalArgumentException.class, () -> Edmonds.findMinimumSpanningArborescence(5, edges, -1)); |
| 116 | + assertThrows(IllegalArgumentException.class, () -> Edmonds.findMinimumSpanningArborescence(5, edges, 5)); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void testCoverageForEdgeSelectionLogic() { |
| 121 | + int n = 3; |
| 122 | + int root = 0; |
| 123 | + List<Edmonds.Edge> edges = new ArrayList<>(); |
| 124 | + |
| 125 | + // This will cover the `edge.weight < minWeightEdge[edge.to]` being false. |
| 126 | + edges.add(new Edmonds.Edge(0, 1, 10)); |
| 127 | + edges.add(new Edmonds.Edge(2, 1, 20)); |
| 128 | + |
| 129 | + // This will cover the `edge.to != root` being false. |
| 130 | + edges.add(new Edmonds.Edge(1, 0, 100)); |
| 131 | + |
| 132 | + // A regular edge to make the graph complete |
| 133 | + edges.add(new Edmonds.Edge(0, 2, 5)); |
| 134 | + |
| 135 | + // Expected MSA: (0,1, w=10) and (0,2, w=5). Total weight = 15. |
| 136 | + long result = Edmonds.findMinimumSpanningArborescence(n, edges, root); |
| 137 | + assertEquals(15, result); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void testCoverageForContractedSelfLoop() { |
| 142 | + int n = 4; |
| 143 | + int root = 0; |
| 144 | + List<Edmonds.Edge> edges = new ArrayList<>(); |
| 145 | + |
| 146 | + // Connect root to the cycle components |
| 147 | + edges.add(new Edmonds.Edge(0, 1, 20)); |
| 148 | + |
| 149 | + // Create a cycle 1 -> 2 -> 1 |
| 150 | + edges.add(new Edmonds.Edge(1, 2, 5)); |
| 151 | + edges.add(new Edmonds.Edge(2, 1, 5)); |
| 152 | + |
| 153 | + // This is the CRITICAL edge for coverage: |
| 154 | + // It connects two nodes (1 and 2) that are part of the SAME cycle. |
| 155 | + // After contracting cycle {1, 2} into a supernode C, this edge becomes (C, C), |
| 156 | + // which means newU == newV. This will trigger the `false` branch of the `if`. |
| 157 | + edges.add(new Edmonds.Edge(1, 1, 100)); // Also a self-loop on a cycle node. |
| 158 | + |
| 159 | + // Add another edge to ensure node 3 is reachable |
| 160 | + edges.add(new Edmonds.Edge(1, 3, 10)); |
| 161 | + |
| 162 | + // Cycle {1,2} has cost 5+5=10. |
| 163 | + // Contract {1,2} to supernode C. |
| 164 | + // Edge (0,1,20) becomes (0,C, w=20-5=15). |
| 165 | + // Edge (1,3,10) becomes (C,3, w=10). |
| 166 | + // Edge (1,1,100) is discarded because newU == newV. |
| 167 | + // Cost of contracted graph = 15 + 10 = 25. |
| 168 | + // Total cost = cycle cost + contracted cost = 10 + 25 = 35. |
| 169 | + long result = Edmonds.findMinimumSpanningArborescence(n, edges, root); |
| 170 | + assertEquals(35, result); |
| 171 | + } |
| 172 | +} |
0 commit comments