Add Python doctest to interval-scheduling.py (#162)
* Add Python doctest to interval-scheduling.py * URL to a description of the algorithm used * Remove a stale commentpull/173/head
parent
bd860a57a4
commit
7a9ae18fca
|
@ -5,29 +5,37 @@ The Strategy: At each step choose the job with earliest finish time
|
||||||
Algorithm Type: Greedy
|
Algorithm Type: Greedy
|
||||||
Time Complexity: O(n*log(n))
|
Time Complexity: O(n*log(n))
|
||||||
"""
|
"""
|
||||||
|
jobs = [
|
||||||
|
(0, 2, 8), # (job_id, start_time, finish_time)
|
||||||
|
(1, 6, 10),
|
||||||
|
(2, 1, 3),
|
||||||
|
(3, 4, 7),
|
||||||
|
(4, 3, 6),
|
||||||
|
(5, 1, 2),
|
||||||
|
(6, 8, 10),
|
||||||
|
(7, 10, 15),
|
||||||
|
(8, 12, 16),
|
||||||
|
(9, 14, 16)
|
||||||
|
]
|
||||||
|
|
||||||
def get_opt_schedule(jobs): # Returns the job_id's in the optimal schedule
|
|
||||||
|
def get_opt_schedule(jobs):
|
||||||
|
"""
|
||||||
|
Return the job_id's in the optimal jobs
|
||||||
|
https://en.wikipedia.org/wiki/Interval_scheduling#Greedy_polynomial_solution
|
||||||
|
|
||||||
|
>>> get_opt_schedule(jobs)
|
||||||
|
[5, 4, 1, 7]
|
||||||
|
"""
|
||||||
opt_schedule = []
|
opt_schedule = []
|
||||||
sorted_jobs = sorted(jobs, key=lambda j: j[2])
|
sorted_jobs = sorted(jobs, key=lambda j: j[2])
|
||||||
n = len(sorted_jobs)
|
number_of_jobs = len(sorted_jobs)
|
||||||
opt_schedule.append(sorted_jobs[0][0])
|
opt_schedule.append(sorted_jobs[0][0])
|
||||||
for i in range(1, n):
|
for i in range(1, number_of_jobs):
|
||||||
if sorted_jobs[i][1] >= jobs[opt_schedule[-1]][2]:
|
if sorted_jobs[i][1] >= jobs[opt_schedule[-1]][2]:
|
||||||
opt_schedule.append(sorted_jobs[i][0])
|
opt_schedule.append(sorted_jobs[i][0])
|
||||||
return opt_schedule
|
return opt_schedule
|
||||||
|
|
||||||
jobs = [
|
|
||||||
[0, 2, 8], # [job_id, start_time, finish_time]
|
|
||||||
[1, 6, 10],
|
|
||||||
[2, 1, 3],
|
|
||||||
[3, 4, 7],
|
|
||||||
[4, 3, 6],
|
|
||||||
[5, 1, 2],
|
|
||||||
[6, 8, 10],
|
|
||||||
[7, 10, 15],
|
|
||||||
[8, 12, 16],
|
|
||||||
[9, 14, 16]
|
|
||||||
]
|
|
||||||
|
|
||||||
opt_schedule = get_opt_schedule(jobs)
|
if __name__ == "__main__":
|
||||||
print(opt_schedule)
|
print(get_opt_schedule(jobs))
|
||||||
|
|
Loading…
Reference in New Issue