Professional Documents
Culture Documents
• Optimizing Fn. Last FT - Strategy: Sort jobs in reverse order, assign next
job on the earliest available processor
• (j3, 6), (j1, 3), (j2, 5), (j4, 10), (j6, 14), (j5, 11), (j8, 18), (j7, 15), (j9, 20): 3
proc
• Reverse sort-
• (j9, 20), (j8, 18), (j7, 15), (j6, 14), (j5, 11), (j4, 10), (j3, 6), (j2, 5), (j1, 3)
• Proc 1: j9 - 20, j4 - 30, j1 - 33.
• Proc 2: j8 - 18, j5 - 29, j3 - 35,
• Proc 3: j7 - 15, j6 - 29, j2 - 34, Last FT = 35.
• // sort: O(n log n)
• // place: naïve: (nM), with heap over processors: O(n log m)
• Optimal: Proc1: j2, j5, j8; Proc 2: j6, j9; Proc 3: j1, j3, j4, j7. Last FT = 34.
• Greedy alg is not optimal algorithm here, but the relative err <= [1/3 -
1/3m], for m processors.
• An NP-complete problem, greedy alg is polynomial O(n logn) for n jobs
(from sorting, assignment is additional O(n), choice of next proc. In each
cycle O(log m) using heap, total O(n logn + n logm), for n>>m the first
term dominates).
HUFFMAN CODES
• Lemma 2: The #of objects left out after M bins are filled (i.e., the
ones that go into the extra bins, M+1-th bin onwards) are at most
M. [This is a static picture after First Fit finished working]
Proof of Lemma 2.
• On the contrary, suppose there are M or more objects left.
• [Note that each of them are <1/3 because they were picked up
after si from the Lemma 1 proof.]
• Note, j=1N sj M, since M is optimum #bins, where N
is #items.
• Say, each bin Bj of the first M bins has items of total
weight Wj in each bin, and xk represent the items in
the extra bins ((M+1)-th bin onwards): x1, …, xM, …
OFFLINE BIN-PACKING
• INTEGER MULTIPLICATION:
– Divide the bits/digits into two sets,
– X = XL*10(size/2) + XR, and
– Y = YL*10(size/2) + YR
• MATRIX MULTIPLICATION:
– Naïve multiplication: O(N3)
– Divide two square matrices into 4 parts each of size N/2, and
– recursively multiply (N/2 x N/2) matrices,
– and add resulting (N/2 x N/2) matrices,
– then put them back to their respective places.
• Eight recursive calls, O(N2) overhead for additions.
T(N) = 8T(N/2) + O(N2).
• Solution [case a >bk]: T(N) = O(N^ logba) = O(N3)
• Strassen’s algorithm;
– rewrite multiplication formula reducing recursive calls to 7 from 8.
– T(N) = 7T(n/2) + O(N2)
– Solution: T(N) = O(Nlog27) = O(N2.81)
DYNAMIC PROGRAMMING STRATEGY
SpaceSaving-fibonacci(n)
if (n<=1) return 1;
int last=1, last2last=1, result=1;
for i=2 through n do
result = last + last2last;
last2last = last;
last=result
end for;
return result.
J| 0 1 2 3 4 5 6 7 8
O -- -- -- -- -- -- -- -- --
1| 0 0 1 1 1 1 1 1 1
2| 0 0 1 2 2 3 3 3 3
3| 0 0 1 2 2 3 3 4 5
• Start the calculation at the lowest level with two matrices, AB, BC,
CD etc. Then calculate for triplets, ABC, BCD etc. And so on..
Matrix-chain Recursive algorithm
Recursive Algorithm M(left, right)
if left > right return 0
else
return min{M(left, i) + M(i+1, right) + rowleft .coli .colright,
for lefti<right};
end algorithm.
j= 1 2 3 4
i
1 0 15 35(2) 69(2)
2 0 12 42
3 0 24 Triplets
4 0 Pairs
3 - - (4) (4)
4 - (3) (4)
5 ---
6 ---
fk
C(k+1, right)
C(left, k-1)
Optimal Binary Search Tree (Continued)
• We will start from one element sub-tree and finish with n element
full tree.
Optimal Binary Search Tree (Continued)
j= 1 2 3 4 5
i
1 7 24(2) 34 55 67
2 0 10 20 41 51
3 0 5 18 26
4 0 8 16
5 0 4
• This algorithm: O(n3). However, O(n2) is feasible.
All Pairs Shortest Path