Repository: TheAlgorithms/Java
Pull request #5812 (2024-10-14 13:20:41)

feat: Add `ProportionalFairScheduling` 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/5812?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 65.52%. Comparing base [(`bcf4034`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/bcf4034ce58ed965d1d885e1b0bf85a920f57e2d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`9c818db`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/9c818db101f397b51f917df13e571e1d9690dec7?dropdown=coverage&el=desc&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    #5812      +/-   ##
============================================
+ Coverage     65.49%   65.52%   +0.03%     
- Complexity     4339     4345       +6     
============================================
  Files           599      600       +1     
  Lines         16742    16762      +20     
  Branches       3212     3214       +2     
============================================
+ Hits          10965    10984      +19     
- Misses         5332     5333       +1     
  Partials        445      445              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5812?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 f6e7ebe4..34ded2ce 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -538,6 +538,7 @@
             * [MultiAgentScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MultiAgentScheduling.java)
             * [NonPreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java)
             * [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
+            * [ProportionalFairScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/ProportionalFairScheduling.java)
             * [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
             * [SelfAdjustingScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SelfAdjustingScheduling.java)
             * [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
@@ -1136,6 +1137,7 @@
             * [MultiAgentSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MultiAgentSchedulingTest.java)
             * [NonPreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java)
             * [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
+            * [ProportionalFairSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/ProportionalFairSchedulingTest.java)
             * [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
             * [SelfAdjustingSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SelfAdjustingSchedulingTest.java)
             * [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)
diff --git a/src/main/java/com/thealgorithms/scheduling/ProportionalFairScheduling.java b/src/main/java/com/thealgorithms/scheduling/ProportionalFairScheduling.java
new file mode 100644
index 00000000..fd78dc57
--- /dev/null
+++ b/src/main/java/com/thealgorithms/scheduling/ProportionalFairScheduling.java
@@ -0,0 +1,54 @@
+package com.thealgorithms.scheduling;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ProportionalFairScheduling allocates resources to processes based on their
+ * proportional weight or importance. It aims to balance fairness with
+ * priority, ensuring that higher-weight processes receive a larger share of resources.
+ *
+ * Use Case: Network bandwidth allocation in cellular networks (4G/5G),
+ * where devices receive a proportional share of bandwidth.
+ *
+ * @author Hardvan
+ */
+public final class ProportionalFairScheduling {
+
+    static class Process {
+        String name;
+        int weight;
+        int allocatedResources;
+
+        Process(String name, int weight) {
+            this.name = name;
+            this.weight = weight;
+            this.allocatedResources = 0;
+        }
+    }
+
+    private final List<Process> processes;
+
+    public ProportionalFairScheduling() {
+        processes = new ArrayList<>();
+    }
+
+    public void addProcess(String name, int weight) {
+        processes.add(new Process(name, weight));
+    }
+
+    public void allocateResources(int totalResources) {
+        int totalWeight = processes.stream().mapToInt(p -> p.weight).sum();
+        for (Process process : processes) {
+            process.allocatedResources = (int) ((double) process.weight / totalWeight * totalResources);
+        }
+    }
+
+    public List<String> getAllocatedResources() {
+        List<String> allocation = new ArrayList<>();
+        for (Process process : processes) {
+            allocation.add(process.name + ": " + process.allocatedResources);
+        }
+        return allocation;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/scheduling/ProportionalFairSchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/ProportionalFairSchedulingTest.java
new file mode 100644
index 00000000..0d379ee9
--- /dev/null
+++ b/src/test/java/com/thealgorithms/scheduling/ProportionalFairSchedulingTest.java
@@ -0,0 +1,53 @@
+package com.thealgorithms.scheduling;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.List;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class ProportionalFairSchedulingTest {
+
+    private ProportionalFairScheduling scheduler;
+
+    @BeforeEach
+    public void setup() {
+        scheduler = new ProportionalFairScheduling();
+    }
+
+    @Test
+    public void testAllocateResourcesSingleProcess() {
+        scheduler.addProcess("Process1", 5);
+        scheduler.allocateResources(100);
+        List<String> expected = List.of("Process1: 100");
+        assertEquals(expected, scheduler.getAllocatedResources());
+    }
+
+    @Test
+    public void testAllocateResourcesMultipleProcesses() {
+        scheduler.addProcess("Process1", 2);
+        scheduler.addProcess("Process2", 3);
+        scheduler.addProcess("Process3", 5);
+        scheduler.allocateResources(100);
+        List<String> expected = List.of("Process1: 20", "Process2: 30", "Process3: 50");
+        assertEquals(expected, scheduler.getAllocatedResources());
+    }
+
+    @Test
+    public void testAllocateResourcesZeroWeightProcess() {
+        scheduler.addProcess("Process1", 0);
+        scheduler.addProcess("Process2", 5);
+        scheduler.allocateResources(100);
+        List<String> expected = List.of("Process1: 0", "Process2: 100");
+        assertEquals(expected, scheduler.getAllocatedResources());
+    }
+
+    @Test
+    public void testAllocateResourcesEqualWeights() {
+        scheduler.addProcess("Process1", 1);
+        scheduler.addProcess("Process2", 1);
+        scheduler.allocateResources(100);
+        List<String> expected = List.of("Process1: 50", "Process2: 50");
+        assertEquals(expected, scheduler.getAllocatedResources());
+    }
+}