forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_scheduling.py
68 lines (56 loc) · 1.83 KB
/
job_scheduling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Python program for weighted job scheduling using Dynamic
Programming and Binary Search
"""
class Job:
"""
Class to represent a job
"""
def __init__(self, start, finish, profit):
self.start = start
self.finish = finish
self.profit = profit
def binary_search(job, start_index):
"""
A Binary Search based function to find the latest job
(before current job) that doesn't conflict with current
job. "index" is index of the current job. This function
returns -1 if all jobs before index conflict with it.
The array jobs[] is sorted in increasing order of finish
time.
"""
left = 0
right = start_index - 1
# Perform binary Search iteratively
while left <= right:
mid = (left + right) // 2
if job[mid].finish <= job[start_index].start:
if job[mid + 1].finish <= job[start_index].start:
left = mid + 1
else:
return mid
else:
right = mid - 1
return -1
def schedule(job):
"""
The main function that returns the maximum possible
profit from given array of jobs
"""
# Sort jobs according to finish time
job = sorted(job, key = lambda j: j.finish)
# Create an array to store solutions of subproblems. table[i]
# stores the profit for jobs till arr[i] (including arr[i])
length = len(job)
table = [0 for _ in range(length)]
table[0] = job[0].profit
# Fill entries in table[] using recursive property
for i in range(1, length):
# Find profit including the current job
incl_prof = job[i].profit
pos = binary_search(job, i)
if pos != -1:
incl_prof += table[pos]
# Store maximum of including and excluding
table[i] = max(incl_prof, table[i - 1])
return table[length-1]