Repository: TheAlgorithms/Java
Pull request #5807 (2024-10-14 10:15:19)

feat: Add `BandwidthAllocation` new algorithm with Junit tests
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5807?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `93.33333%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 64.82%. Comparing base [(`9c76b30`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/9c76b30271535123ea30c0b39a46ccee6b302c6c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`ceaa91e`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/ceaa91eeaddebe65fe45cbd4862af667ec0ab642?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5807?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...gorithms/greedyalgorithms/BandwidthAllocation.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5807?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fgreedyalgorithms%2FBandwidthAllocation.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9ncmVlZHlhbGdvcml0aG1zL0JhbmR3aWR0aEFsbG9jYXRpb24uamF2YQ==) | 93.33% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5807?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5807      +/-   ##
============================================
+ Coverage     64.81%   64.82%   +0.01%     
- Complexity     4299     4301       +2     
============================================
  Files           599      600       +1     
  Lines         16764    16779      +15     
  Branches       3219     3222       +3     
============================================
+ Hits          10865    10877      +12     
  Misses         5455     5455              
- Partials        444      447       +3     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5807?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 10922d76..b7707a25 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -320,11 +320,14 @@
             * [BresenhamLine](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/BresenhamLine.java)
             * [ConvexHull](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/ConvexHull.java)
             * [GrahamScan](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/GrahamScan.java)
+            * [MidpointCircle](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/MidpointCircle.java)
+            * [MidpointEllipse](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/MidpointEllipse.java)
             * [Point](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/Point.java)
           * graph
             * [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)
@@ -360,6 +363,7 @@
             * [Average](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Average.java)
             * [BinaryPow](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/BinaryPow.java)
             * [BinomialCoefficient](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/BinomialCoefficient.java)
+            * [CatalanNumbers](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/CatalanNumbers.java)
             * [Ceil](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Ceil.java)
             * [ChineseRemainderTheorem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/ChineseRemainderTheorem.java)
             * [CircularConvolutionFFT](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/CircularConvolutionFFT.java)
@@ -967,10 +971,13 @@
             * [BresenhamLineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/BresenhamLineTest.java)
             * [ConvexHullTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/ConvexHullTest.java)
             * [GrahamScanTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java)
+            * [MidpointCircleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/MidpointCircleTest.java)
+            * [MidpointEllipseTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/MidpointEllipseTest.java)
           * graph
             * [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)
@@ -1003,6 +1010,7 @@
             * [AverageTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/AverageTest.java)
             * [BinaryPowTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/BinaryPowTest.java)
             * [BinomialCoefficientTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/BinomialCoefficientTest.java)
+            * [CatalanNumbersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CatalanNumbersTest.java)
             * [CeilTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CeilTest.java)
             * [ChineseRemainderTheoremTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ChineseRemainderTheoremTest.java)
             * [CollatzConjectureTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CollatzConjectureTest.java)
diff --git a/src/main/java/com/thealgorithms/greedyalgorithms/BandwidthAllocation.java b/src/main/java/com/thealgorithms/greedyalgorithms/BandwidthAllocation.java
new file mode 100644
index 00000000..602cdcd5
--- /dev/null
+++ b/src/main/java/com/thealgorithms/greedyalgorithms/BandwidthAllocation.java
@@ -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;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/greedyalgorithms/BandwidthAllocationTest.java b/src/test/java/com/thealgorithms/greedyalgorithms/BandwidthAllocationTest.java
new file mode 100644
index 00000000..452f7858
--- /dev/null
+++ b/src/test/java/com/thealgorithms/greedyalgorithms/BandwidthAllocationTest.java
@@ -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));
+    }
+}