Skip to content

Commit ecf4c37

Browse files
authored
Add ProportionalFairScheduling algorithm (#5812)
1 parent 7a539bc commit ecf4c37

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@
538538
* [MultiAgentScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MultiAgentScheduling.java)
539539
* [NonPreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java)
540540
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
541+
* [ProportionalFairScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/ProportionalFairScheduling.java)
541542
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
542543
* [SelfAdjustingScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SelfAdjustingScheduling.java)
543544
* [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
@@ -1136,6 +1137,7 @@
11361137
* [MultiAgentSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MultiAgentSchedulingTest.java)
11371138
* [NonPreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java)
11381139
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
1140+
* [ProportionalFairSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/ProportionalFairSchedulingTest.java)
11391141
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
11401142
* [SelfAdjustingSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SelfAdjustingSchedulingTest.java)
11411143
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* ProportionalFairScheduling allocates resources to processes based on their
8+
* proportional weight or importance. It aims to balance fairness with
9+
* priority, ensuring that higher-weight processes receive a larger share of resources.
10+
*
11+
* Use Case: Network bandwidth allocation in cellular networks (4G/5G),
12+
* where devices receive a proportional share of bandwidth.
13+
*
14+
* @author Hardvan
15+
*/
16+
public final class ProportionalFairScheduling {
17+
18+
static class Process {
19+
String name;
20+
int weight;
21+
int allocatedResources;
22+
23+
Process(String name, int weight) {
24+
this.name = name;
25+
this.weight = weight;
26+
this.allocatedResources = 0;
27+
}
28+
}
29+
30+
private final List<Process> processes;
31+
32+
public ProportionalFairScheduling() {
33+
processes = new ArrayList<>();
34+
}
35+
36+
public void addProcess(String name, int weight) {
37+
processes.add(new Process(name, weight));
38+
}
39+
40+
public void allocateResources(int totalResources) {
41+
int totalWeight = processes.stream().mapToInt(p -> p.weight).sum();
42+
for (Process process : processes) {
43+
process.allocatedResources = (int) ((double) process.weight / totalWeight * totalResources);
44+
}
45+
}
46+
47+
public List<String> getAllocatedResources() {
48+
List<String> allocation = new ArrayList<>();
49+
for (Process process : processes) {
50+
allocation.add(process.name + ": " + process.allocatedResources);
51+
}
52+
return allocation;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.List;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class ProportionalFairSchedulingTest {
10+
11+
private ProportionalFairScheduling scheduler;
12+
13+
@BeforeEach
14+
public void setup() {
15+
scheduler = new ProportionalFairScheduling();
16+
}
17+
18+
@Test
19+
public void testAllocateResourcesSingleProcess() {
20+
scheduler.addProcess("Process1", 5);
21+
scheduler.allocateResources(100);
22+
List<String> expected = List.of("Process1: 100");
23+
assertEquals(expected, scheduler.getAllocatedResources());
24+
}
25+
26+
@Test
27+
public void testAllocateResourcesMultipleProcesses() {
28+
scheduler.addProcess("Process1", 2);
29+
scheduler.addProcess("Process2", 3);
30+
scheduler.addProcess("Process3", 5);
31+
scheduler.allocateResources(100);
32+
List<String> expected = List.of("Process1: 20", "Process2: 30", "Process3: 50");
33+
assertEquals(expected, scheduler.getAllocatedResources());
34+
}
35+
36+
@Test
37+
public void testAllocateResourcesZeroWeightProcess() {
38+
scheduler.addProcess("Process1", 0);
39+
scheduler.addProcess("Process2", 5);
40+
scheduler.allocateResources(100);
41+
List<String> expected = List.of("Process1: 0", "Process2: 100");
42+
assertEquals(expected, scheduler.getAllocatedResources());
43+
}
44+
45+
@Test
46+
public void testAllocateResourcesEqualWeights() {
47+
scheduler.addProcess("Process1", 1);
48+
scheduler.addProcess("Process2", 1);
49+
scheduler.allocateResources(100);
50+
List<String> expected = List.of("Process1: 50", "Process2: 50");
51+
assertEquals(expected, scheduler.getAllocatedResources());
52+
}
53+
}

0 commit comments

Comments
 (0)