forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKargerMinCutTest.java
More file actions
114 lines (87 loc) · 4.48 KB
/
KargerMinCutTest.java
File metadata and controls
114 lines (87 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.thealgorithms.randomized;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.junit.jupiter.api.Test;
public class KargerMinCutTest {
@Test
public void testSimpleGraph() {
// Graph: 0 -- 1
Collection<Integer> nodes = Arrays.asList(0, 1);
List<int[]> edges = List.of(new int[] {0, 1});
KargerMinCut.KargerOutput result = KargerMinCut.findMinCut(nodes, edges);
assertEquals(1, result.minCut());
assertTrue(result.first().contains(0) || result.first().contains(1));
assertTrue(result.second().contains(0) || result.second().contains(1));
}
@Test
public void testTriangleGraph() {
// Graph: 0 -- 1 -- 2 -- 0
Collection<Integer> nodes = Arrays.asList(0, 1, 2);
List<int[]> edges = List.of(new int[] {0, 1}, new int[] {1, 2}, new int[] {2, 0});
KargerMinCut.KargerOutput result = KargerMinCut.findMinCut(nodes, edges);
assertEquals(2, result.minCut());
}
@Test
public void testSquareGraph() {
// Graph: 0 -- 1
// | |
// 3 -- 2
Collection<Integer> nodes = Arrays.asList(0, 1, 2, 3);
List<int[]> edges = List.of(new int[] {0, 1}, new int[] {1, 2}, new int[] {2, 3}, new int[] {3, 0});
KargerMinCut.KargerOutput result = KargerMinCut.findMinCut(nodes, edges);
assertEquals(2, result.minCut());
}
@Test
public void testDisconnectedGraph() {
// Graph: 0 -- 1 2 -- 3
Collection<Integer> nodes = Arrays.asList(0, 1, 2, 3);
List<int[]> edges = List.of(new int[] {0, 1}, new int[] {2, 3});
KargerMinCut.KargerOutput result = KargerMinCut.findMinCut(nodes, edges);
assertEquals(0, result.minCut());
}
@Test
public void testCompleteGraph() {
// Complete Graph: 0 -- 1 -- 2 -- 3 (all nodes connected to each other)
Collection<Integer> nodes = Arrays.asList(0, 1, 2, 3);
List<int[]> edges = List.of(new int[] {0, 1}, new int[] {0, 2}, new int[] {0, 3}, new int[] {1, 2}, new int[] {1, 3}, new int[] {2, 3});
KargerMinCut.KargerOutput result = KargerMinCut.findMinCut(nodes, edges);
assertEquals(3, result.minCut());
}
@Test
public void testSingleNodeGraph() {
// Graph: Single node with no edges
Collection<Integer> nodes = List.of(0);
List<int[]> edges = List.of();
KargerMinCut.KargerOutput result = KargerMinCut.findMinCut(nodes, edges);
assertEquals(0, result.minCut());
assertTrue(result.first().contains(0));
assertTrue(result.second().isEmpty());
}
@Test
public void testTwoNodesNoEdge() {
// Graph: 0 1 (no edges)
Collection<Integer> nodes = Arrays.asList(0, 1);
List<int[]> edges = List.of();
KargerMinCut.KargerOutput result = KargerMinCut.findMinCut(nodes, edges);
assertEquals(0, result.minCut());
assertTrue(result.first().contains(0) || result.first().contains(1));
assertTrue(result.second().contains(0) || result.second().contains(1));
}
@Test
public void testComplexGraph() {
// Nodes: 0, 1, 2, 3, 4, 5, 6, 7, 8
// Edges: Fully connected graph with additional edges for complexity
Collection<Integer> nodes = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8);
List<int[]> edges = List.of(new int[] {0, 1}, new int[] {0, 2}, new int[] {0, 3}, new int[] {0, 4}, new int[] {0, 5}, new int[] {1, 2}, new int[] {1, 3}, new int[] {1, 4}, new int[] {1, 5}, new int[] {1, 6}, new int[] {2, 3}, new int[] {2, 4}, new int[] {2, 5}, new int[] {2, 6},
new int[] {2, 7}, new int[] {3, 4}, new int[] {3, 5}, new int[] {3, 6}, new int[] {3, 7}, new int[] {3, 8}, new int[] {4, 5}, new int[] {4, 6}, new int[] {4, 7}, new int[] {4, 8}, new int[] {5, 6}, new int[] {5, 7}, new int[] {5, 8}, new int[] {6, 7}, new int[] {6, 8}, new int[] {7, 8},
new int[] {0, 6}, new int[] {1, 7}, new int[] {2, 8});
KargerMinCut.KargerOutput result = KargerMinCut.findMinCut(nodes, edges);
// The exact minimum cut value depends on the randomization, but it should be consistent
// for this graph structure. For a fully connected graph, the minimum cut is typically
// determined by the smallest number of edges connecting two partitions.
assertTrue(result.minCut() > 0);
}
}