Skip to content

Commit 5f58192

Browse files
committed
feat: Add BandwidthAllocation new algorithm with Junit tests
1 parent 9c76b30 commit 5f58192

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Class to solve the Bandwidth Allocation Problem.
7+
* The goal is to maximize the value gained by allocating bandwidth to users.
8+
* Example:
9+
* Bandwidth = 10
10+
* Users = [3, 5, 7]
11+
* Values = [10, 20, 30]
12+
* The maximum value achievable is 40 by allocating 3 units to user 0 and 7 units to user 2.
13+
*
14+
* @author Hardvan
15+
*/
16+
public final class BandwidthAllocation {
17+
private BandwidthAllocation() {
18+
}
19+
20+
/**
21+
* Allocates bandwidth to maximize value.
22+
* Steps:
23+
* 1. Calculate the ratio of value/demand for each user.
24+
* 2. Sort the users in descending order of the ratio.
25+
* 3. Allocate bandwidth to users in order of the sorted list.
26+
* 4. If the bandwidth is not enough to allocate the full demand of a user, allocate a fraction of the demand.
27+
* 5. Return the maximum value achievable.
28+
*
29+
* @param bandwidth total available bandwidth
30+
* @param users array of user demands
31+
* @param values array of values associated with each user's demand
32+
* @return the maximum value achievable
33+
*/
34+
public static int maxValue(int bandwidth, int[] users, int[] values) {
35+
int n = users.length;
36+
double[][] ratio = new double[n][2]; // {index, ratio}
37+
38+
for (int i = 0; i < n; i++) {
39+
ratio[i][0] = i;
40+
ratio[i][1] = (double) values[i] / users[i];
41+
}
42+
43+
Arrays.sort(ratio, (a, b) -> Double.compare(b[1], a[1]));
44+
45+
int maxValue = 0;
46+
for (int i = 0; i < n; i++) {
47+
int index = (int) ratio[i][0];
48+
if (bandwidth >= users[index]) {
49+
maxValue += values[index];
50+
bandwidth -= users[index];
51+
} else {
52+
maxValue += (int) (ratio[i][1] * bandwidth);
53+
break;
54+
}
55+
}
56+
return maxValue;
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public class BandwidthAllocationTest {
11+
12+
@ParameterizedTest
13+
@MethodSource("bandwidthProvider")
14+
public void testMaxValue(int capacity, int[] bandwidths, int[] values, int expected) {
15+
assertEquals(expected, BandwidthAllocation.maxValue(capacity, bandwidths, values));
16+
}
17+
18+
private static Stream<Arguments> bandwidthProvider() {
19+
return Stream.of(Arguments.of(50, new int[] {20, 10, 30}, new int[] {40, 20, 30}, 80), Arguments.of(0, new int[] {5, 10}, new int[] {10, 20}, 0), Arguments.of(5, new int[] {5, 10}, new int[] {10, 20}, 10), Arguments.of(15, new int[] {10, 20}, new int[] {10, 25}, 18),
20+
Arguments.of(25, new int[] {10, 15, 20}, new int[] {10, 30, 50}, 60));
21+
}
22+
}

0 commit comments

Comments
 (0)