forked from adinloh/Algorithms-design-and-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07a) Greedy jobs scheduling.py
44 lines (29 loc) · 1.1 KB
/
07a) Greedy jobs 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
'''
Algorithms - design and analysis (Stanford), Part II.
Programming Assignment 1-1:
Greedy jobs scheduling
@author: Mikhail Dubov
'''
def greedy_minimal_weighted_sum(jobs, greedy_criterion, ties_criterion):
# correct, since .sort() is stable
jobs.sort(key = ties_criterion)
jobs.sort(key = greedy_criterion)
timeline = 0
sum = 0
for w, l in jobs:
timeline += l
sum += w * timeline
return sum
def main():
f = open('jobs.txt')
n = int(f.readline())
jobs = []
for line in f:
w, l = [int(x) for x in line.split()]
jobs.append((w, l))
greedy_criterion_1 = lambda (w, l): -(w - l) # not always correct
greedy_criterion_2 = lambda (w, l): -float(w)/l # always correct
ties_criterion = lambda (w, l): -w # matters for criterion 1 only
print 'Greedy algorithm #1: %i' % greedy_minimal_weighted_sum(jobs, greedy_criterion_1, ties_criterion)
print 'Greedy algorithm #2: %i' % greedy_minimal_weighted_sum(jobs, greedy_criterion_2, ties_criterion)
main()