Repository: TheAlgorithms/Java
Pull request #5657 (2024-10-09 05:34:47)

Add `EDFScheduling` 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/5657?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.10345%` with `2 lines` in your changes missing coverage. Please review.
> Project coverage is 58.65%. Comparing base [(`b54cc21`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b54cc21ade112e20f429964dc86f8543a5605d98?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`d460172`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/d4601726948125f5da854ff366d9d0a6bea16108?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/5657?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...va/com/thealgorithms/scheduling/EDFScheduling.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5657?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fscheduling%2FEDFScheduling.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9zY2hlZHVsaW5nL0VERlNjaGVkdWxpbmcuamF2YQ==) | 93.10% | [1 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5657?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    #5657      +/-   ##
============================================
+ Coverage     58.59%   58.65%   +0.05%     
- Complexity     3749     3751       +2     
============================================
  Files           554      555       +1     
  Lines         15922    15951      +29     
  Branches       3030     3032       +2     
============================================
+ Hits           9330     9356      +26     
- Misses         6195     6196       +1     
- Partials        397      399       +2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5657?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 a1a53cb2..46affca4 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -36,6 +36,8 @@
             * [OnesComplement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java)
             * [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java)
             * [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java)
+            * [SingleElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleElement.java)
+            * [SwapAdjacentBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java)
             * [TwosComplement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/TwosComplement.java)
           * ciphers
             * a5
@@ -476,6 +478,7 @@
           * Recursion
             * [GenerateSubsets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java)
           * scheduling
+            * [EDFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/EDFScheduling.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)
@@ -649,6 +652,8 @@
             * [OnesComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/OnesComplementTest.java)
             * [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java)
             * [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java)
+            * [SingleElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleElementTest.java)
+            * [SwapAdjacentBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java)
             * [TwosComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/TwosComplementTest.java)
           * ciphers
             * a5
@@ -975,6 +980,7 @@
           * Recursion
             * [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java)
           * scheduling
+            * [EDFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/EDFSchedulingTest.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/EDFScheduling.java b/src/main/java/com/thealgorithms/scheduling/EDFScheduling.java
new file mode 100644
index 00000000..5ba79cdb
--- /dev/null
+++ b/src/main/java/com/thealgorithms/scheduling/EDFScheduling.java
@@ -0,0 +1,99 @@
+package com.thealgorithms.scheduling;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+
+/**
+ * The Earliest Deadline First (EDF) Scheduling class implements a dynamic scheduling algorithm.
+ * It assigns the CPU to processes with the earliest deadlines, ensuring that deadlines are met if possible.
+ * This scheduling algorithm is ideal for real-time systems where meeting deadlines is critical.
+ */
+public final class EDFScheduling {
+    private EDFScheduling() {
+    }
+
+    private List<Process> processes;
+
+    /**
+     * Constructs an EDFScheduling object with a list of processes.
+     *
+     * @param processes List of processes to be scheduled.
+     */
+    public EDFScheduling(final List<Process> processes) {
+        this.processes = processes;
+    }
+
+    /**
+     * Schedules the processes using Earliest Deadline First (EDF) scheduling.
+     * Processes are sorted by their deadlines, and the method simulates their execution.
+     * It calculates the waiting time and turnaround time for each process.
+     *
+     * @return List of processes after they have been executed in order of earliest deadline first.
+     */
+    public List<Process> scheduleProcesses() {
+        processes.sort(Comparator.comparingInt(Process::getDeadline));
+
+        int currentTime = 0;
+        List<Process> executedProcesses = new ArrayList<>();
+
+        for (Process process : processes) {
+            process.setWaitingTime(currentTime);
+            currentTime += process.getBurstTime();
+            process.setTurnAroundTime(process.getWaitingTime() + process.getBurstTime());
+
+            if (currentTime > process.getDeadline()) {
+                System.out.println("Warning: Process " + process.getProcessId() + " missed its deadline.");
+            }
+
+            executedProcesses.add(process);
+        }
+
+        return executedProcesses;
+    }
+
+    /**
+     * The Process class represents a process with an ID, burst time, deadline, waiting time, and turnaround time.
+     */
+    public static class Process {
+        private String processId;
+        private int burstTime;
+        private int deadline;
+        private int waitingTime;
+        private int turnAroundTime;
+
+        public Process(String processId, int burstTime, int deadline) {
+            this.processId = processId;
+            this.burstTime = burstTime;
+            this.deadline = deadline;
+        }
+
+        public String getProcessId() {
+            return processId;
+        }
+
+        public int getBurstTime() {
+            return burstTime;
+        }
+
+        public int getDeadline() {
+            return deadline;
+        }
+
+        public int getWaitingTime() {
+            return waitingTime;
+        }
+
+        public void setWaitingTime(int waitingTime) {
+            this.waitingTime = waitingTime;
+        }
+
+        public int getTurnAroundTime() {
+            return turnAroundTime;
+        }
+
+        public void setTurnAroundTime(int turnAroundTime) {
+            this.turnAroundTime = turnAroundTime;
+        }
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/scheduling/EDFSchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/EDFSchedulingTest.java
new file mode 100644
index 00000000..8ce290cb
--- /dev/null
+++ b/src/test/java/com/thealgorithms/scheduling/EDFSchedulingTest.java
@@ -0,0 +1,69 @@
+package com.thealgorithms.scheduling;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class EDFSchedulingTest {
+
+    private List<EDFScheduling.Process> processes;
+
+    @BeforeEach
+    public void setup() {
+        processes = createProcesses();
+    }
+
+    @Test
+    public void testEDFScheduling() {
+        EDFScheduling edfScheduling = new EDFScheduling(processes);
+        List<EDFScheduling.Process> executedProcesses = edfScheduling.scheduleProcesses();
+
+        // Assert the correct number of processes
+        assertEquals(3, executedProcesses.size());
+
+        // Assert that processes are executed in order of earliest deadline first
+        EDFScheduling.Process process1 = executedProcesses.get(0);
+        assertEquals("P2", process1.getProcessId());
+        assertEquals(0, process1.getWaitingTime());
+        assertEquals(3, process1.getTurnAroundTime());
+
+        EDFScheduling.Process process2 = executedProcesses.get(1);
+        assertEquals("P1", process2.getProcessId());
+        assertEquals(3, process2.getWaitingTime());
+        assertEquals(10, process2.getTurnAroundTime());
+
+        EDFScheduling.Process process3 = executedProcesses.get(2);
+        assertEquals("P3", process3.getProcessId());
+        assertEquals(10, process3.getWaitingTime());
+        assertEquals(18, process3.getTurnAroundTime());
+    }
+
+    @Test
+    public void testProcessMissedDeadline() {
+        // Modify the deadline of a process to ensure it will miss its deadline
+        processes.get(1).setTurnAroundTime(5); // Set P1's deadline to 5 (which it will miss)
+
+        EDFScheduling edfScheduling = new EDFScheduling(processes);
+        edfScheduling.scheduleProcesses();
+
+        // Check if the process with ID "P1" missed its deadline
+        assertEquals("P1", processes.get(1).getProcessId());
+    }
+
+    private List<EDFScheduling.Process> createProcesses() {
+        // Process ID, Burst Time, Deadline
+        EDFScheduling.Process process1 = new EDFScheduling.Process("P1", 7, 10); // 7 burst time, 10 deadline
+        EDFScheduling.Process process2 = new EDFScheduling.Process("P2", 3, 5); // 3 burst time, 5 deadline
+        EDFScheduling.Process process3 = new EDFScheduling.Process("P3", 8, 18); // 8 burst time, 18 deadline
+
+        List<EDFScheduling.Process> processes = new ArrayList<>();
+        processes.add(process1);
+        processes.add(process2);
+        processes.add(process3);
+
+        return processes;
+    }
+}