Skip to content

Commit fbde45f

Browse files
clang formatting.
1 parent bd04a7d commit fbde45f

2 files changed

Lines changed: 30 additions & 51 deletions

File tree

src/main/java/com/thealgorithms/randomized/KargerMinCut.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.thealgorithms.randomized;
22

3-
import java.util.*;
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Random;
8+
import java.util.Set;
49

510
/**
611
* Implementation of Karger's Minimum Cut algorithm.
@@ -21,11 +26,12 @@
2126
* <li>Count the edges between the two remaining nodes to determine the cut size.</li>
2227
* <li>Repeat the process multiple times to improve the likelihood of finding the true minimum cut.</li>
2328
* </ol>
24-
*
29+
* <p>
2530
* See more: <a href="https://en.wikipedia.org/wiki/Karger%27s_algorithm">Karger's algorithm</a>
31+
*
2632
* @author MuhammadEzzatHBK
2733
*/
28-
public class KargerMinCut {
34+
public final class KargerMinCut {
2935

3036
/**
3137
* Output of the Karger algorithm.
@@ -34,9 +40,11 @@ public class KargerMinCut {
3440
* @param second The second set of nodes in the cut.
3541
* @param minCut The size of the minimum cut.
3642
*/
37-
public record KargerOutput(Set<Integer> first, Set<Integer> second, int minCut) {}
43+
public record KargerOutput(Set<Integer> first, Set<Integer> second, int minCut) {
44+
}
3845

39-
private KargerMinCut() {}
46+
private KargerMinCut() {
47+
}
4048

4149
public static KargerOutput findMinCut(Collection<Integer> nodeSet, List<int[]> edges) {
4250
return findMinCut(nodeSet, edges, 100);
@@ -50,8 +58,7 @@ public static KargerOutput findMinCut(Collection<Integer> nodeSet, List<int[]> e
5058
* @param iterations: Iterations to run the algorithms for, more iterations = more accuracy
5159
* @return A KargerOutput object containing the two sets of nodes and the size of the minimum cut.
5260
*/
53-
public static KargerOutput findMinCut(Collection<Integer> nodeSet,
54-
List<int[]> edges, int iterations) {
61+
public static KargerOutput findMinCut(Collection<Integer> nodeSet, List<int[]> edges, int iterations) {
5562
Graph graph = new Graph(nodeSet, edges);
5663
KargerOutput minCut = new KargerOutput(new HashSet<>(), new HashSet<>(), Integer.MAX_VALUE);
5764
KargerOutput output;
@@ -104,7 +111,6 @@ public void union(int u, int v) {
104111
}
105112
}
106113

107-
108114
public boolean inSameSet(int u, int v) {
109115
return find(u) == find(v);
110116
}
@@ -114,7 +120,7 @@ public boolean inSameSet(int u, int v) {
114120
But it helps us provide more useful output.
115121
*/
116122
public Set<Integer> getAnySet() {
117-
int aRoot = find(0); //Get one of the two roots
123+
int aRoot = find(0); // Get one of the two roots
118124

119125
Set<Integer> set = new HashSet<>();
120126
for (int i = 0; i < parent.length; i++) {
@@ -125,10 +131,8 @@ public Set<Integer> getAnySet() {
125131

126132
return set;
127133
}
128-
129134
}
130135

131-
132136
private static class Graph {
133137
private final List<Integer> nodes;
134138
private final List<int[]> edges;
@@ -137,7 +141,7 @@ public Graph(Collection<Integer> nodeSet, List<int[]> edges) {
137141
this.nodes = new ArrayList<>(nodeSet);
138142
this.edges = new ArrayList<>();
139143
for (int[] e : edges) {
140-
this.edges.add(new int[]{e[0], e[1]});
144+
this.edges.add(new int[] {e[0], e[1]});
141145
}
142146
}
143147

src/test/java/com/thealgorithms/randomized/KargerMinCutTest.java

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package com.thealgorithms.randomized;
22

3-
import org.junit.jupiter.api.Test;
4-
5-
import java.util.*;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
65

7-
import static org.junit.jupiter.api.Assertions.*;
6+
import java.util.Arrays;
7+
import java.util.Collection;
8+
import java.util.List;
9+
import org.junit.jupiter.api.Test;
810

911
public class KargerMinCutTest {
1012

1113
@Test
1214
public void testSimpleGraph() {
1315
// Graph: 0 -- 1
1416
Collection<Integer> nodes = Arrays.asList(0, 1);
15-
List<int[]> edges = List.of(new int[]{0, 1});
17+
List<int[]> edges = List.of(new int[] {0, 1});
1618

1719
KargerMinCut.KargerOutput result = KargerMinCut.findMinCut(nodes, edges);
1820

@@ -25,11 +27,7 @@ public void testSimpleGraph() {
2527
public void testTriangleGraph() {
2628
// Graph: 0 -- 1 -- 2 -- 0
2729
Collection<Integer> nodes = Arrays.asList(0, 1, 2);
28-
List<int[]> edges = List.of(
29-
new int[]{0, 1},
30-
new int[]{1, 2},
31-
new int[]{2, 0}
32-
);
30+
List<int[]> edges = List.of(new int[] {0, 1}, new int[] {1, 2}, new int[] {2, 0});
3331

3432
KargerMinCut.KargerOutput result = KargerMinCut.findMinCut(nodes, edges);
3533

@@ -42,12 +40,7 @@ public void testSquareGraph() {
4240
// | |
4341
// 3 -- 2
4442
Collection<Integer> nodes = Arrays.asList(0, 1, 2, 3);
45-
List<int[]> edges = List.of(
46-
new int[]{0, 1},
47-
new int[]{1, 2},
48-
new int[]{2, 3},
49-
new int[]{3, 0}
50-
);
43+
List<int[]> edges = List.of(new int[] {0, 1}, new int[] {1, 2}, new int[] {2, 3}, new int[] {3, 0});
5144

5245
KargerMinCut.KargerOutput result = KargerMinCut.findMinCut(nodes, edges);
5346

@@ -58,10 +51,7 @@ public void testSquareGraph() {
5851
public void testDisconnectedGraph() {
5952
// Graph: 0 -- 1 2 -- 3
6053
Collection<Integer> nodes = Arrays.asList(0, 1, 2, 3);
61-
List<int[]> edges = List.of(
62-
new int[]{0, 1},
63-
new int[]{2, 3}
64-
);
54+
List<int[]> edges = List.of(new int[] {0, 1}, new int[] {2, 3});
6555

6656
KargerMinCut.KargerOutput result = KargerMinCut.findMinCut(nodes, edges);
6757

@@ -72,14 +62,7 @@ public void testDisconnectedGraph() {
7262
public void testCompleteGraph() {
7363
// Complete Graph: 0 -- 1 -- 2 -- 3 (all nodes connected to each other)
7464
Collection<Integer> nodes = Arrays.asList(0, 1, 2, 3);
75-
List<int[]> edges = List.of(
76-
new int[]{0, 1},
77-
new int[]{0, 2},
78-
new int[]{0, 3},
79-
new int[]{1, 2},
80-
new int[]{1, 3},
81-
new int[]{2, 3}
82-
);
65+
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});
8366

8467
KargerMinCut.KargerOutput result = KargerMinCut.findMinCut(nodes, edges);
8568

@@ -117,17 +100,9 @@ public void testComplexGraph() {
117100
// Nodes: 0, 1, 2, 3, 4, 5, 6, 7, 8
118101
// Edges: Fully connected graph with additional edges for complexity
119102
Collection<Integer> nodes = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8);
120-
List<int[]> edges = List.of(
121-
new int[]{0, 1}, new int[]{0, 2}, new int[]{0, 3}, new int[]{0, 4}, new int[]{0, 5},
122-
new int[]{1, 2}, new int[]{1, 3}, new int[]{1, 4}, new int[]{1, 5}, new int[]{1, 6},
123-
new int[]{2, 3}, new int[]{2, 4}, new int[]{2, 5}, new int[]{2, 6}, new int[]{2, 7},
124-
new int[]{3, 4}, new int[]{3, 5}, new int[]{3, 6}, new int[]{3, 7}, new int[]{3, 8},
125-
new int[]{4, 5}, new int[]{4, 6}, new int[]{4, 7}, new int[]{4, 8},
126-
new int[]{5, 6}, new int[]{5, 7}, new int[]{5, 8},
127-
new int[]{6, 7}, new int[]{6, 8},
128-
new int[]{7, 8},
129-
new int[]{0, 6}, new int[]{1, 7}, new int[]{2, 8}
130-
);
103+
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},
104+
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},
105+
new int[] {0, 6}, new int[] {1, 7}, new int[] {2, 8});
131106

132107
KargerMinCut.KargerOutput result = KargerMinCut.findMinCut(nodes, edges);
133108

0 commit comments

Comments
 (0)