Skip to content

Commit 5715faa

Browse files
pharshil1902cclauss
authored andcommitted
Added Shortest Job First Algorithm (TheAlgorithms#1957)
* Added Shortest Job First Algorithm It is in IPYNB format but the dataframes are really looking good. Please, take a look. * Delete Shortest_Job_First_Algorithm.ipynb * Added Shortest Job First Algorithm * Update Shortest_Job_First Algorithm.py * Update Shortest_Job_First Algorithm.py * Update Shortest_Job_first Algorithm * Added Shortest_Job_First Algorithm * Added Shortest Job First Algorithm * Update shortest_job_first_algorithm.py * Format code with psf/black Co-authored-by: Christian Clauss <[email protected]>
1 parent 6341eb8 commit 5715faa

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed
+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
"""
2+
Shortest job remainig first
3+
Please note arrival time and burst
4+
Please use spaces to separate times entered.
5+
"""
6+
7+
import pandas as pd
8+
from typing import List
9+
10+
11+
def calculate_waitingtime(
12+
arrival_time: List[int], burst_time: List[int], no_of_processes: int
13+
) -> List[int]:
14+
15+
"""
16+
Calculate the waiting time of each processes
17+
Return: list of waiting times.
18+
>>> calculate_waitingtime([1,2,3,4],[3,3,5,1],4)
19+
[0, 3, 5, 0]
20+
>>> calculate_waitingtime([1,2,3],[2,5,1],3)
21+
[0, 2, 0]
22+
>>> calculate_waitingtime([2,3],[5,1],2)
23+
[1, 0]
24+
"""
25+
remaining_time = [0] * no_of_processes
26+
waiting_time = [0] * no_of_processes
27+
# Copy the burst time into remaining_time[]
28+
for i in range(no_of_processes):
29+
remaining_time[i] = burst_time[i]
30+
31+
complete = 0
32+
increment_time = 0
33+
minm = 999999999
34+
short = 0
35+
check = False
36+
37+
# Process until all processes are completed
38+
while complete != no_of_processes:
39+
for j in range(no_of_processes):
40+
if arrival_time[j] <= increment_time:
41+
if remaining_time[j] > 0:
42+
if remaining_time[j] < minm:
43+
minm = remaining_time[j]
44+
short = j
45+
check = True
46+
47+
if not check:
48+
increment_time += 1
49+
continue
50+
remaining_time[short] -= 1
51+
52+
minm = remaining_time[short]
53+
if minm == 0:
54+
minm = 999999999
55+
56+
if remaining_time[short] == 0:
57+
complete += 1
58+
check = False
59+
60+
# Find finish time of current process
61+
finish_time = increment_time + 1
62+
63+
# Calculate waiting time
64+
finar = finish_time - arrival_time[short]
65+
waiting_time[short] = finar - burst_time[short]
66+
67+
if waiting_time[short] < 0:
68+
waiting_time[short] = 0
69+
70+
# Increment time
71+
increment_time += 1
72+
return waiting_time
73+
74+
75+
def calculate_turnaroundtime(
76+
burst_time: List[int], no_of_processes: int, waiting_time: List[int]
77+
) -> List[int]:
78+
"""
79+
Calculate the turn around time of each Processes
80+
Return: list of turn around times.
81+
>>> calculate_turnaroundtime([3,3,5,1], 4, [0,3,5,0])
82+
[3, 6, 10, 1]
83+
>>> calculate_turnaroundtime([3,3], 2, [0,3])
84+
[3, 6]
85+
>>> calculate_turnaroundtime([8,10,1], 3, [1,0,3])
86+
[9, 10, 4]
87+
"""
88+
turn_around_time = [0] * no_of_processes
89+
for i in range(no_of_processes):
90+
turn_around_time[i] = burst_time[i] + waiting_time[i]
91+
return turn_around_time
92+
93+
94+
def calculate_average_times(
95+
waiting_time: List[int], turn_around_time: List[int], no_of_processes: int
96+
):
97+
"""
98+
This function calculates the average of the waiting & turnaround times
99+
Prints: Average Waiting time & Average Turn Around Time
100+
>>> calculate_average_times([0,3,5,0],[3,6,10,1],4)
101+
Average waiting time = 2.00000
102+
Average turn around time = 5.0
103+
>>> calculate_average_times([2,3],[3,6],2)
104+
Average waiting time = 2.50000
105+
Average turn around time = 4.5
106+
>>> calculate_average_times([10,4,3],[2,7,6],3)
107+
Average waiting time = 5.66667
108+
Average turn around time = 5.0
109+
"""
110+
total_waiting_time = 0
111+
total_turn_around_time = 0
112+
for i in range(no_of_processes):
113+
total_waiting_time = total_waiting_time + waiting_time[i]
114+
total_turn_around_time = total_turn_around_time + turn_around_time[i]
115+
print("Average waiting time = %.5f" % (total_waiting_time / no_of_processes))
116+
print("Average turn around time =", total_turn_around_time / no_of_processes)
117+
118+
119+
if __name__ == "__main__":
120+
print("Enter how many process you want to analyze")
121+
no_of_processes = int(input())
122+
burst_time = [0] * no_of_processes
123+
arrival_time = [0] * no_of_processes
124+
processes = list(range(1, no_of_processes + 1))
125+
126+
for i in range(no_of_processes):
127+
print("Enter the arrival time and brust time for process:--" + str(i + 1))
128+
arrival_time[i], burst_time[i] = map(int, input().split())
129+
waiting_time = calculate_waitingtime(arrival_time, burst_time, no_of_processes)
130+
bt = burst_time
131+
n = no_of_processes
132+
wt = waiting_time
133+
turn_around_time = calculate_turnaroundtime(bt, n, wt)
134+
calculate_average_times(waiting_time, turn_around_time, no_of_processes)
135+
processes = list(range(1, no_of_processes + 1))
136+
fcfs = pd.DataFrame(
137+
list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)),
138+
columns=[
139+
"Process",
140+
"BurstTime",
141+
"ArrivalTime",
142+
"WaitingTime",
143+
"TurnAroundTime",
144+
],
145+
)
146+
147+
# Printing the dataFrame
148+
pd.set_option("display.max_rows", fcfs.shape[0] + 1)
149+
print(fcfs)

0 commit comments

Comments
 (0)