Repository: TheAlgorithms/Java
Pull request #5815 (2024-10-14 13:29:22)

feat: Add `FairShareScheduling` 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/5815?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 `96.15385%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 65.54%. 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 [(`476d41b`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/476d41b166371fe12c72bfe86eb5f2834d21d7f0?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/5815?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [.../thealgorithms/scheduling/FairShareScheduling.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5815?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fscheduling%2FFairShareScheduling.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9zY2hlZHVsaW5nL0ZhaXJTaGFyZVNjaGVkdWxpbmcuamF2YQ==) | 96.15% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5815?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    #5815      +/-   ##
============================================
+ Coverage     65.49%   65.54%   +0.04%     
- Complexity     4339     4345       +6     
============================================
  Files           599      600       +1     
  Lines         16742    16768      +26     
  Branches       3212     3215       +3     
============================================
+ Hits          10965    10990      +25     
  Misses         5332     5332              
- Partials        445      446       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5815?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 34ded2ce..774a1860 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -530,6 +530,7 @@
               * [ScanScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/diskscheduling/ScanScheduling.java)
               * [SSFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/diskscheduling/SSFScheduling.java)
             * [EDFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/EDFScheduling.java)
+            * [FairShareScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FairShareScheduling.java)
             * [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java)
             * [HighestResponseRatioNextScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java)
             * [JobSchedulingWithDeadline](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java)
@@ -1129,6 +1130,7 @@
               * [ScanSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/ScanSchedulingTest.java)
               * [SSFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/SSFSchedulingTest.java)
             * [EDFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/EDFSchedulingTest.java)
+            * [FairShareSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FairShareSchedulingTest.java)
             * [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
             * [HighestResponseRatioNextSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java)
             * [JobSchedulingWithDeadlineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java)
diff --git a/src/main/java/com/thealgorithms/scheduling/FairShareScheduling.java b/src/main/java/com/thealgorithms/scheduling/FairShareScheduling.java
new file mode 100644
index 00000000..776fc59c
--- /dev/null
+++ b/src/main/java/com/thealgorithms/scheduling/FairShareScheduling.java
@@ -0,0 +1,65 @@
+package com.thealgorithms.scheduling;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * FairShareScheduling allocates CPU resources equally among users or groups
+ * instead of individual tasks. Each group gets a proportional share,
+ * preventing resource hogging by a single user's processes.
+ *
+ * Use Case: Multi-user systems where users submit multiple tasks simultaneously,
+ * such as cloud environments.
+ *
+ * @author Hardvan
+ */
+public final class FairShareScheduling {
+
+    static class User {
+        String name;
+        int allocatedResources;
+        int totalWeight;
+
+        User(String name) {
+            this.name = name;
+            this.allocatedResources = 0;
+            this.totalWeight = 0;
+        }
+
+        void addWeight(int weight) {
+            this.totalWeight += weight;
+        }
+    }
+
+    private final Map<String, User> users; // username -> User
+
+    public FairShareScheduling() {
+        users = new HashMap<>();
+    }
+
+    public void addUser(String userName) {
+        users.putIfAbsent(userName, new User(userName));
+    }
+
+    public void addTask(String userName, int weight) {
+        User user = users.get(userName);
+        if (user != null) {
+            user.addWeight(weight);
+        }
+    }
+
+    public void allocateResources(int totalResources) {
+        int totalWeights = users.values().stream().mapToInt(user -> user.totalWeight).sum();
+        for (User user : users.values()) {
+            user.allocatedResources = (int) ((double) user.totalWeight / totalWeights * totalResources);
+        }
+    }
+
+    public Map<String, Integer> getAllocatedResources() {
+        Map<String, Integer> allocation = new HashMap<>();
+        for (User user : users.values()) {
+            allocation.put(user.name, user.allocatedResources);
+        }
+        return allocation;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/scheduling/FairShareSchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/FairShareSchedulingTest.java
new file mode 100644
index 00000000..7aa6e061
--- /dev/null
+++ b/src/test/java/com/thealgorithms/scheduling/FairShareSchedulingTest.java
@@ -0,0 +1,60 @@
+package com.thealgorithms.scheduling;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.Map;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class FairShareSchedulingTest {
+
+    private FairShareScheduling scheduler;
+
+    @BeforeEach
+    public void setup() {
+        scheduler = new FairShareScheduling();
+    }
+
+    @Test
+    public void testAllocateResourcesSingleUser() {
+        scheduler.addUser("User1");
+        scheduler.addTask("User1", 5);
+        scheduler.allocateResources(100);
+        Map<String, Integer> expected = Map.of("User1", 100);
+        assertEquals(expected, scheduler.getAllocatedResources());
+    }
+
+    @Test
+    public void testAllocateResourcesMultipleUsers() {
+        scheduler.addUser("User1");
+        scheduler.addUser("User2");
+        scheduler.addUser("User3");
+        scheduler.addTask("User1", 2);
+        scheduler.addTask("User2", 3);
+        scheduler.addTask("User3", 5);
+        scheduler.allocateResources(100);
+        Map<String, Integer> expected = Map.of("User1", 20, "User2", 30, "User3", 50);
+        assertEquals(expected, scheduler.getAllocatedResources());
+    }
+
+    @Test
+    public void testAllocateResourcesZeroWeightUser() {
+        scheduler.addUser("User1");
+        scheduler.addUser("User2");
+        scheduler.addTask("User2", 5);
+        scheduler.allocateResources(100);
+        Map<String, Integer> expected = Map.of("User1", 0, "User2", 100);
+        assertEquals(expected, scheduler.getAllocatedResources());
+    }
+
+    @Test
+    public void testAllocateResourcesEqualWeights() {
+        scheduler.addUser("User1");
+        scheduler.addUser("User2");
+        scheduler.addTask("User1", 1);
+        scheduler.addTask("User2", 1);
+        scheduler.allocateResources(100);
+        Map<String, Integer> expected = Map.of("User1", 50, "User2", 50);
+        assertEquals(expected, scheduler.getAllocatedResources());
+    }
+}