












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













A certain string processing language allows the programmer to break a string into two pieces. Since this involves copying the old string, it costs n units of time to break a string of n characters into two pieces. Suppose a programmer wants to break a string into many pieces. The order in which the breaks are made can affect the total amount of time used. For example suppose we wish to break a 20 character string after characters 3,8, and 10:
Symbol Technology has developed a new design for bar codes, PDF-417 that has a capacity of several hundred bytes. What is the best way to encode text for this design?
They developed a complicated mode-switching data com- pression scheme. Docsity.com
LOWERCASE MIXED
OTHERCHARS
ALPHA latch (^) latchlatch
latch shift
latch shift
shift
latch
Latch commands permanently put you in a different mode. Shift commands temporarily put you in a different mode. Symbol used a greedy algorithm to encode a string, making local decisions only. But we realized that for any prefix, you want an optimal encoding which might leave you in every possible mode.
Suppose the job scanning through a shelf of books is to be split between k workers. To avoid the need to rearrange the books or separate them into piles, we can divide the shelf into k regions and assign each region to one worker. What is the fairest way to divide the shelf up? If each book is the same length, partition the books into equal- sized regions,
100 100 100 | 100 100 100 | 100 100 100
But what if the books are not the same length? This partition would yield
100 200 300 | 400 500 600 | 700 800 900 Docsity.com
Which part of the job would you volunteer to do? How can we find the fairest possible partition, i.e.
100 200 300 400 500 | 600 700 | 800 900
Consider a recursive, exhaustive search approach. Notice that the kth partition starts right after we placed the (k โ 1)st divider. Where can we place this last divider? Between the ith and (i + 1)st elements for some i, where 1 โค i โค n. What is the cost of this? The total cost will be the larger of two quantities, (1) the cost of the last partition โnj=i+1 sj and (2) the cost of the largest partition cost formed to the left of i. What is the size of this left partition? To partition the elements {s 1 ,... , si} as equally as possible. But this is a smaller instance of the same problem!
Define M [n, k] to be the minimum possible cost over all partitionings of {s 1 ,... , sn} into k ranges, where the cost of a partition is the largest sum of elements in one of its parts. Thus defined, this function can be evaluated:
M [n, k] =
n min i=1 max(M [i, k โ 1], โn j=i+
sj)
with the natural basis cases of
M [1, k] = s 1 , for all k > 0 and,
M [n, 1] =
โn i=
si
What is the running time?
To evaluate this efficiently, we must make sure we do the smaller cases before the larger cases that depend upon them.
Partition[S, k] (* compute prefix sums: p[k] = โki=1 si *) p[0] = 0 for i = 1 to n do p[i] = p[i โ 1] + si
(* initialize boundary conditions *) for i = 1 to n do M [i, 1] = p[i] for i = 1 to k do M [1, j] = s 1
(* evaluate main recurrence *) Docsity.com
for i = 2 to n do for j = 2 to k do M [i, j] = โ for x = 1 to i โ 1 do s = max(M [x, j โ 1], p[i] โ p[x]) if (M [i, j] > s) then M [i, j] = s D[i, j] = x
For the input { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } M k D k n 1 2 3 n 1 2 3 1 1 1 1 1 โ โ โ 1 2 1 1 1 โ 1 1 1 3 2 1 1 โ 1 2 1 4 2 2 1 โ 2 2 1 5 3 2 1 โ 2 3 1 6 3 2 1 โ 3 4 1 7 4 3 1 โ 3 4 1 8 4 3 1 โ 4 5 1 9 5 3 1 โ 4 6
Dynamic programming computes recurrences efficiently by storing partial results. Thus dynamic programming can only be efficient when there are not too many partial results to compute! There are n! permutations of an n-element set โ we cannot use dynamic programming to store the best solution for each subpermutation. There are 2 n^ subsets of an n-element set
To use dynamic programming, the problem must observe the principle of optimality , that whatever the initial state is, remaining decisions must be optimal with regard the state following from the first decision. Combinatorial problems may have this property but may use too much memory/time to be efficient.
Let T (i; j 1 , j 2 ,... , jk) be the cost of the optimal tour for i to 1 that goes thru each of the other cities once
T (i; i 1 , j 2 ,... , ji) = M in 1 โคmโคk C[i, jm] + T (jm; j 1 , j 2 ,... , jk)
T (i, j) = C(i, j) + C(j, 1)
Here there can be any subset of j 1 , j 2 ,... , jk instead of any subinterval - hence exponential.