Skip to content

feat: Add KCenters new algorithm with Junit tests #5806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -951,6 +952,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)
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/com/thealgorithms/greedyalgorithms/KCenters.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}