Skip to content

Commit 8901d6f

Browse files
Vinay SanwalVinay Sanwal
Vinay Sanwal
authored and
Vinay Sanwal
committed
Interval Scheduling Algorithm
1 parent dba8eec commit 8901d6f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
interval scheduling is a class of problems. The programs take a number of tasks into account. Every task is represented by an interval that indicates the amount of time it should take a machine to complete it. If there is no overlap between any two intervals on the system or resource, a subset of intervals is compatible.
3+
4+
The goal of the interval scheduling maximization problem is to identify the largest compatible set or a collection of intervals with the least possible overlap. The idea is to optimize throughput by completing as many tasks as you can.
5+
6+
"""
7+
8+
def interval_scheduling(stimes, ftimes):
9+
index = list(range(len(stimes)))
10+
# sort according to finish times
11+
index.sort(key=lambda i: ftimes[i])
12+
13+
maximal_set = set()
14+
prev_finish_time = 0
15+
for i in index:
16+
if stimes[i] >= prev_finish_time:
17+
maximal_set.add(i)
18+
prev_finish_time = ftimes[i]
19+
20+
return maximal_set
21+
22+
23+
n = int(input('Enter number of activities: '))
24+
stimes = input('Enter the start time of the {} activities in order: '
25+
.format(n)).split()
26+
stimes = [int(st) for st in stimes]
27+
ftimes = input('Enter the finish times of the {} activities in order: '
28+
.format(n)).split()
29+
ftimes = [int(ft) for ft in ftimes]
30+
31+
ans = interval_scheduling(stimes, ftimes)
32+
print('A maximum-size subset of activities that are mutually compatible is', ans)

0 commit comments

Comments
 (0)