|
| 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 | +/** |
| 8 | + * Unit tests for the StoerWagner global minimum cut algorithm. |
| 9 | + * |
| 10 | + * These tests verify correctness of the implementation across |
| 11 | + * several graph configurations: simple, complete, disconnected, |
| 12 | + * and small edge cases. |
| 13 | + */ |
| 14 | +public class StoerWagnerTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + public void testSimpleGraph() { |
| 18 | + int[][] graph = {{0, 3, 2, 0}, {3, 0, 1, 4}, {2, 1, 0, 5}, {0, 4, 5, 0}}; |
| 19 | + StoerWagner algo = new StoerWagner(); |
| 20 | + assertEquals(5, algo.findMinCut(graph)); // Correct minimum cut = 5 |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testTriangleGraph() { |
| 25 | + int[][] graph = {{0, 2, 3}, {2, 0, 4}, {3, 4, 0}}; |
| 26 | + StoerWagner algo = new StoerWagner(); |
| 27 | + assertEquals(5, algo.findMinCut(graph)); // min cut = 5 |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testDisconnectedGraph() { |
| 32 | + int[][] graph = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; |
| 33 | + StoerWagner algo = new StoerWagner(); |
| 34 | + assertEquals(0, algo.findMinCut(graph)); // Disconnected graph => cut = 0 |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testCompleteGraph() { |
| 39 | + int[][] graph = {{0, 1, 1, 1}, {1, 0, 1, 1}, {1, 1, 0, 1}, {1, 1, 1, 0}}; |
| 40 | + StoerWagner algo = new StoerWagner(); |
| 41 | + assertEquals(3, algo.findMinCut(graph)); // Each vertex connected to all others |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testSingleVertex() { |
| 46 | + int[][] graph = {{0}}; |
| 47 | + StoerWagner algo = new StoerWagner(); |
| 48 | + assertEquals(0, algo.findMinCut(graph)); // Only one vertex |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testTwoVertices() { |
| 53 | + int[][] graph = {{0, 7}, {7, 0}}; |
| 54 | + StoerWagner algo = new StoerWagner(); |
| 55 | + assertEquals(7, algo.findMinCut(graph)); // Only one edge, cut weight = 7 |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testSquareGraphWithDiagonal() { |
| 60 | + int[][] graph = {{0, 2, 0, 2}, {2, 0, 3, 0}, {0, 3, 0, 4}, {2, 0, 4, 0}}; |
| 61 | + StoerWagner algo = new StoerWagner(); |
| 62 | + assertEquals(4, algo.findMinCut(graph)); // verified manually |
| 63 | + } |
| 64 | +} |
0 commit comments