Repository: TheAlgorithms/Java
Pull request #5819 (2024-10-14 13:40:54)

feat: Add `GangScheduling` 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/5819?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 `95.23810%` with `1 line` in your changes missing coverage. Please review.
> 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 [(`7380e47`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/7380e47a0c5421f8dc8100174d822570390226d0?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/5819?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...a/com/thealgorithms/scheduling/GangScheduling.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5819?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fscheduling%2FGangScheduling.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9zY2hlZHVsaW5nL0dhbmdTY2hlZHVsaW5nLmphdmE=) | 95.23% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5819?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    #5819      +/-   ##
============================================
+ Coverage     65.49%   65.52%   +0.03%     
- Complexity     4339     4345       +6     
============================================
  Files           599      600       +1     
  Lines         16742    16763      +21     
  Branches       3212     3214       +2     
============================================
+ Hits          10965    10984      +19     
- Misses         5332     5333       +1     
- Partials        445      446       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5819?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 476308a3..ad893d45 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -533,6 +533,7 @@
             * [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)
+            * [GangScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/GangScheduling.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)
             * [LotteryScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/LotteryScheduling.java)
@@ -1134,6 +1135,7 @@
             * [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)
+            * [GangSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/GangSchedulingTest.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)
             * [LotterySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/LotterySchedulingTest.java)
diff --git a/src/main/java/com/thealgorithms/scheduling/GangScheduling.java b/src/main/java/com/thealgorithms/scheduling/GangScheduling.java
new file mode 100644
index 00000000..ac1ce8dd
--- /dev/null
+++ b/src/main/java/com/thealgorithms/scheduling/GangScheduling.java
@@ -0,0 +1,61 @@
+package com.thealgorithms.scheduling;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * GangScheduling groups related tasks (gangs) to run simultaneously on multiple processors.
+ * All tasks in a gang are executed together or not at all.
+ *
+ * Use Case: Parallel computing environments where multiple threads of a program
+ * need to run concurrently for optimal performance.
+ *
+ * @author Hardvan
+ */
+public final class GangScheduling {
+
+    static class Gang {
+        String name;
+        List<String> tasks;
+
+        Gang(String name) {
+            this.name = name;
+            this.tasks = new ArrayList<>();
+        }
+
+        void addTask(String task) {
+            tasks.add(task);
+        }
+
+        List<String> getTasks() {
+            return tasks;
+        }
+    }
+
+    private final Map<String, Gang> gangs;
+
+    public GangScheduling() {
+        gangs = new HashMap<>();
+    }
+
+    public void addGang(String gangName) {
+        gangs.putIfAbsent(gangName, new Gang(gangName));
+    }
+
+    public void addTaskToGang(String gangName, String task) {
+        Gang gang = gangs.get(gangName);
+        if (gang != null) {
+            gang.addTask(task);
+        }
+    }
+
+    public Map<String, List<String>> getGangSchedules() {
+        Map<String, List<String>> schedules = new HashMap<>();
+        for (Gang gang : gangs.values()) {
+            schedules.put(gang.name, gang.getTasks());
+        }
+        return schedules;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/scheduling/GangSchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/GangSchedulingTest.java
new file mode 100644
index 00000000..2e1bb4cd
--- /dev/null
+++ b/src/test/java/com/thealgorithms/scheduling/GangSchedulingTest.java
@@ -0,0 +1,52 @@
+package com.thealgorithms.scheduling;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.List;
+import java.util.Map;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class GangSchedulingTest {
+
+    private GangScheduling scheduler;
+
+    @BeforeEach
+    public void setup() {
+        scheduler = new GangScheduling();
+    }
+
+    @Test
+    public void testAddGangAndTask() {
+        scheduler.addGang("Gang1");
+        scheduler.addTaskToGang("Gang1", "Task1");
+        Map<String, List<String>> expected = Map.of("Gang1", List.of("Task1"));
+        assertEquals(expected, scheduler.getGangSchedules());
+    }
+
+    @Test
+    public void testMultipleGangs() {
+        scheduler.addGang("Gang1");
+        scheduler.addGang("Gang2");
+        scheduler.addTaskToGang("Gang1", "Task1");
+        scheduler.addTaskToGang("Gang2", "Task2");
+        Map<String, List<String>> expected = Map.of("Gang1", List.of("Task1"), "Gang2", List.of("Task2"));
+        assertEquals(expected, scheduler.getGangSchedules());
+    }
+
+    @Test
+    public void testGangWithMultipleTasks() {
+        scheduler.addGang("Gang1");
+        scheduler.addTaskToGang("Gang1", "Task1");
+        scheduler.addTaskToGang("Gang1", "Task2");
+        Map<String, List<String>> expected = Map.of("Gang1", List.of("Task1", "Task2"));
+        assertEquals(expected, scheduler.getGangSchedules());
+    }
+
+    @Test
+    public void testEmptyGangSchedule() {
+        scheduler.addGang("Gang1");
+        Map<String, List<String>> expected = Map.of("Gang1", List.of());
+        assertEquals(expected, scheduler.getGangSchedules());
+    }
+}