Skip to content

Commit c40eb8d

Browse files
authored
Add KCenters algorithm (#5806)
1 parent e0ab1d3 commit c40eb8d

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@
325325
* [FractionalKnapsack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/FractionalKnapsack.java)
326326
* [GaleShapley](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/GaleShapley.java)
327327
* [JobSequencing](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/JobSequencing.java)
328+
* [KCenters](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/KCenters.java)
328329
* [MergeIntervals](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MergeIntervals.java)
329330
* [MinimizingLateness](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MinimizingLateness.java)
330331
* [MinimumWaitingTime](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTime.java)
@@ -954,6 +955,7 @@
954955
* [FractionalKnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/FractionalKnapsackTest.java)
955956
* [GaleShapleyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/GaleShapleyTest.java)
956957
* [JobSequencingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/JobSequencingTest.java)
958+
* [KCentersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/KCentersTest.java)
957959
* [MergeIntervalsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MergeIntervalsTest.java)
958960
* [MinimizingLatenessTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MinimizingLatenessTest.java)
959961
* [MinimumWaitingTimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTimeTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Given a set of points and a number k.
7+
* The goal is to minimize the maximum distance between any point and its nearest center.
8+
* Each point is assigned to the nearest center.
9+
* The distance between two points is the Euclidean distance.
10+
* The problem is NP-hard.
11+
*
12+
* @author Hardvan
13+
*/
14+
public final class KCenters {
15+
private KCenters() {
16+
}
17+
18+
/**
19+
* Finds the maximum distance to the nearest center given k centers.
20+
* Steps:
21+
* 1. Initialize an array {@code selected} of size n and an array {@code maxDist} of size n.
22+
* 2. Set the first node as selected and update the maxDist array.
23+
* 3. For each center, find the farthest node from the selected centers.
24+
* 4. Update the maxDist array.
25+
* 5. Return the maximum distance to the nearest center.
26+
*
27+
* @param distances matrix representing distances between nodes
28+
* @param k the number of centers
29+
* @return the maximum distance to the nearest center
30+
*/
31+
public static int findKCenters(int[][] distances, int k) {
32+
int n = distances.length;
33+
boolean[] selected = new boolean[n];
34+
int[] maxDist = new int[n];
35+
36+
Arrays.fill(maxDist, Integer.MAX_VALUE);
37+
38+
selected[0] = true;
39+
for (int i = 1; i < n; i++) {
40+
maxDist[i] = Math.min(maxDist[i], distances[0][i]);
41+
}
42+
43+
for (int centers = 1; centers < k; centers++) {
44+
int farthest = -1;
45+
for (int i = 0; i < n; i++) {
46+
if (!selected[i] && (farthest == -1 || maxDist[i] > maxDist[farthest])) {
47+
farthest = i;
48+
}
49+
}
50+
selected[farthest] = true;
51+
for (int i = 0; i < n; i++) {
52+
maxDist[i] = Math.min(maxDist[i], distances[farthest][i]);
53+
}
54+
}
55+
56+
int result = 0;
57+
for (int dist : maxDist) {
58+
result = Math.max(result, dist);
59+
}
60+
return result;
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class KCentersTest {
8+
9+
@Test
10+
public void testFindKCenters() {
11+
int[][] distances = {{0, 2, 3, 4}, {2, 0, 5, 1}, {3, 5, 0, 7}, {4, 1, 7, 0}};
12+
assertEquals(4, KCenters.findKCenters(distances, 2));
13+
assertEquals(2, KCenters.findKCenters(distances, 4));
14+
}
15+
}

0 commit comments

Comments
 (0)