mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 16:27:33 +08:00
Add KCenters
algorithm (#5806)
This commit is contained in:
@ -325,6 +325,7 @@
|
||||
* [FractionalKnapsack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/FractionalKnapsack.java)
|
||||
* [GaleShapley](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/GaleShapley.java)
|
||||
* [JobSequencing](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/JobSequencing.java)
|
||||
* [KCenters](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/KCenters.java)
|
||||
* [MergeIntervals](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MergeIntervals.java)
|
||||
* [MinimizingLateness](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MinimizingLateness.java)
|
||||
* [MinimumWaitingTime](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTime.java)
|
||||
@ -954,6 +955,7 @@
|
||||
* [FractionalKnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/FractionalKnapsackTest.java)
|
||||
* [GaleShapleyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/GaleShapleyTest.java)
|
||||
* [JobSequencingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/JobSequencingTest.java)
|
||||
* [KCentersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/KCentersTest.java)
|
||||
* [MergeIntervalsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MergeIntervalsTest.java)
|
||||
* [MinimizingLatenessTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MinimizingLatenessTest.java)
|
||||
* [MinimumWaitingTimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MinimumWaitingTimeTest.java)
|
||||
|
@ -0,0 +1,62 @@
|
||||
package com.thealgorithms.greedyalgorithms;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Given a set of points and a number k.
|
||||
* The goal is to minimize the maximum distance between any point and its nearest center.
|
||||
* Each point is assigned to the nearest center.
|
||||
* The distance between two points is the Euclidean distance.
|
||||
* The problem is NP-hard.
|
||||
*
|
||||
* @author Hardvan
|
||||
*/
|
||||
public final class KCenters {
|
||||
private KCenters() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the maximum distance to the nearest center given k centers.
|
||||
* Steps:
|
||||
* 1. Initialize an array {@code selected} of size n and an array {@code maxDist} of size n.
|
||||
* 2. Set the first node as selected and update the maxDist array.
|
||||
* 3. For each center, find the farthest node from the selected centers.
|
||||
* 4. Update the maxDist array.
|
||||
* 5. Return the maximum distance to the nearest center.
|
||||
*
|
||||
* @param distances matrix representing distances between nodes
|
||||
* @param k the number of centers
|
||||
* @return the maximum distance to the nearest center
|
||||
*/
|
||||
public static int findKCenters(int[][] distances, int k) {
|
||||
int n = distances.length;
|
||||
boolean[] selected = new boolean[n];
|
||||
int[] maxDist = new int[n];
|
||||
|
||||
Arrays.fill(maxDist, Integer.MAX_VALUE);
|
||||
|
||||
selected[0] = true;
|
||||
for (int i = 1; i < n; i++) {
|
||||
maxDist[i] = Math.min(maxDist[i], distances[0][i]);
|
||||
}
|
||||
|
||||
for (int centers = 1; centers < k; centers++) {
|
||||
int farthest = -1;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (!selected[i] && (farthest == -1 || maxDist[i] > maxDist[farthest])) {
|
||||
farthest = i;
|
||||
}
|
||||
}
|
||||
selected[farthest] = true;
|
||||
for (int i = 0; i < n; i++) {
|
||||
maxDist[i] = Math.min(maxDist[i], distances[farthest][i]);
|
||||
}
|
||||
}
|
||||
|
||||
int result = 0;
|
||||
for (int dist : maxDist) {
|
||||
result = Math.max(result, dist);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.thealgorithms.greedyalgorithms;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class KCentersTest {
|
||||
|
||||
@Test
|
||||
public void testFindKCenters() {
|
||||
int[][] distances = {{0, 2, 3, 4}, {2, 0, 5, 1}, {3, 5, 0, 7}, {4, 1, 7, 0}};
|
||||
assertEquals(4, KCenters.findKCenters(distances, 2));
|
||||
assertEquals(2, KCenters.findKCenters(distances, 4));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user