Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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 @@
* [StronglyConnectedComponentOptimized](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/graph/StronglyConnectedComponentOptimized.java)
* greedyalgorithms
* [ActivitySelection](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/ActivitySelection.java)
* [BandwidthAllocation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/BandwidthAllocation.java)
* [BinaryAddition](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java)
* [CoinChange](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/CoinChange.java)
* [DigitSeparation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java)
Expand Down Expand Up @@ -971,6 +972,7 @@
* [StronglyConnectedComponentOptimizedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/graph/StronglyConnectedComponentOptimizedTest.java)
* greedyalgorithms
* [ActivitySelectionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/ActivitySelectionTest.java)
* [BandwidthAllocationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/BandwidthAllocationTest.java)
* [BinaryAdditionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java)
* [CoinChangeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/CoinChangeTest.java)
* [DigitSeparationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.thealgorithms.greedyalgorithms;

import java.util.Arrays;

/**
* Class to solve the Bandwidth Allocation Problem.
* The goal is to maximize the value gained by allocating bandwidth to users.
* Example:
* Bandwidth = 10
* Users = [3, 5, 7]
* Values = [10, 20, 30]
* The maximum value achievable is 40 by allocating 3 units to user 0 and 7 units to user 2.
*
* @author Hardvan
*/
public final class BandwidthAllocation {
private BandwidthAllocation() {
}

/**
* Allocates bandwidth to maximize value.
* Steps:
* 1. Calculate the ratio of value/demand for each user.
* 2. Sort the users in descending order of the ratio.
* 3. Allocate bandwidth to users in order of the sorted list.
* 4. If the bandwidth is not enough to allocate the full demand of a user, allocate a fraction of the demand.
* 5. Return the maximum value achievable.
*
* @param bandwidth total available bandwidth to allocate
* @param users array of user demands
* @param values array of values associated with each user's demand
* @return the maximum value achievable
*/
public static int maxValue(int bandwidth, int[] users, int[] values) {
int n = users.length;
double[][] ratio = new double[n][2]; // {index, ratio}

for (int i = 0; i < n; i++) {
ratio[i][0] = i;
ratio[i][1] = (double) values[i] / users[i];
}

Arrays.sort(ratio, (a, b) -> Double.compare(b[1], a[1]));

int maxValue = 0;
for (int i = 0; i < n; i++) {
int index = (int) ratio[i][0];
if (bandwidth >= users[index]) {
maxValue += values[index];
bandwidth -= users[index];
} else {
maxValue += (int) (ratio[i][1] * bandwidth);
break;
}
}
return maxValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.thealgorithms.greedyalgorithms;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class BandwidthAllocationTest {

@ParameterizedTest
@MethodSource("bandwidthProvider")
public void testMaxValue(int capacity, int[] bandwidths, int[] values, int expected) {
assertEquals(expected, BandwidthAllocation.maxValue(capacity, bandwidths, values));
}

private static Stream<Arguments> bandwidthProvider() {
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),
Arguments.of(25, new int[] {10, 15, 20}, new int[] {10, 30, 50}, 60));
}
}