|
{16, 14, 10, 8, 7, 9, 3, 2, 4, 1}; {14, 8, 10, 4, 7, 9, 3, 2, 1, 16}; {10, 8, 9, 4, 7, 1, 3, 2, 14, 16}; { 9, 8, 3, 4, 7, 1, 2, 10, 14, 16}; { 8, 7, 3, 4, 2, 1, 9, 10, 14, 16}; { 7, 4, 3, 1, 2, 8, 9, 10, 14, 16}; { 4, 2, 3, 1, 7, 8, 9, 10, 14, 16}; { 3, 2, 1, 4, 7, 8, 9, 10, 14, 16}; { 2, 1, 3, 4, 7, 8, 9, 10, 14, 16}; { 1, 2, 3, 4, 7, 8, 9, 10, 14, 16};
{16, 14, 10, 8, 7, 9, 3, 2, 4, 1}; // initial {14, 8, 10, 4, 7, 9, 3, 2, 1}; // after extracting max (16) and restoring heap {15, 14, 10, 4, 8, 9, 3, 2, 1, 7}; // after inserting 15 and restoring heap {15, 14, 10, 4,12, 9, 3, 2, 1, 7, 12}; // after inserting 12 and restoring heap {14, 12, 10, 4, 8, 9, 3, 2, 1, 7}; // after extracting max (15) and restoring heap
m11 = 0 | m22 = 0 | m33 = 0 | m44 = 0 | m55 = 0 | m66 = 0 |
m12 = 150 | m23 = 360 | m34 = 180 | m45 = 3000 | m56 = 1500 | |
m13 = 330 | m24 = 330 | m35 = 930 | m46 = 1860 | ||
m14 = 405 | m25 = 2430 | m36 = 1770 | |||
m15 = 1655 | m26 = 1950 | ||||
m15 = 2010 |
Here are the calculations:
m12 = 15*10*3 = 150; m23 = 10*3*12 = 360; m34 = 3*12*5 = 180; m45 = 12*5*50; m56 = 5*50*6 = 1500Above, the minimum values are given in bold at each level, and these are the entries in the earlier table. The final bold line for m16 shows that the final multiplication involved m12 and m36,that is,
m13 = min{ m12 + m33 + 5*3*12 = 150 + 0 + 180 = 330, m11 + m23 + 5*10*12 = 0 + 360 + 600 = 960} m24 = min{ m23 + m44 + 10*12*5 = 360 + 0 + 600 = 960, m22 + m34 + 10*3*5 = 0 + 180 + 150 = 330} m35 = min{ m34 + m55 + 3*5*50 = 180 + 0 + 750 = 930, m33 + m45 + 3*12*50 = 0 + 3000 + 1800 = 4800} m46 = min{ m45 + m66 + 12*50*6 = 3000 + 0 + 3600 = 6600, m44 + m56 + 12*5*6 = 0 + 1500 + 360 = 1860}
m14 = min{ m13 + m44 + 5*12*5 = 330 + 0 + 300 = 630, m12 + m34 + 5*3*5 = 150 + 180 + 75 = 405, m11 + m24 + 5*10*5 = 0 + 330 + 250 = 580} m25 = min{ m24 + m55 + 10*5*50 = 330 + 0 + 2500 = 2830, m23 + m45 + 10*12*50 = 360 + 3200 + 6000 = 9560, m22 + m35 + 10*3*50 = 0 + 930 + 1500 = 2430} m36 = min{ m35 + m66 + 3*50*6 = 930 + 0 + 900 = 1830, m34 + m56 + 3*5*6 = 180 + 1500 + 90 = 1770, m33 + m46 + 3*12*6 = 0 + 1860 + 216 = 2076}
m15 = min{ m14 + m55 + 5*5*50 = 405 + 0 + 1250 = 1655, m13 + m45 + 5*12*50 = 330 + 3000 + 3000 = 6330, m12 + m35 + 5*3*50 = 150 + 930 + 750 = 1830, m11 + m25 + 5*10*50 = 0 + 2430 + 2500 = 2930} m26 = min{ m25 + m66 + 10*50*6 = 2430 + 0 + 3000 = 5430, m24 + m56 + 10*5*6 = 330 + 1500 + 300 = 2130, m23 + m46 + 10*12*6 = 360 + 1860 + 720 = 2940, m22 + m36 + 10*3*6 = 0 + 1770 + 180 = 1950}
m16 = min{ m15 + m66 + 5*50*6 = 1635 + 0 + 1500 = 3155, m14 + m56 + 5*5*6 = 405 + 1500 + 150 = 2055, m13 + m46 + 5*12*6 = 330 + 1860 + 360 = 2550, m12 + m36 + 5*3*6 = 150 + 1770 + 90 = 2010, m11 + m26 + 5*10*6 = 0 + 1950 + 300 = 2250}
As a check, here is the output of my C program that calculates this stuff (the output is arranged slightly differently):
% chain 5 10 3 12 5 50 6 0 The array p: p[0] = 5, p[1] = 10, p[2] = 3, p[3] = 12, p[4] = 5, p[5] = 50, p[6] = 6, The array m: i= 1 i= 2 i= 3 i= 4 i= 5 i= 6 j= 6: 2010 1950 1770 1860 1500 0 j= 5: 1655 2430 930 3000 0 j= 4: 405 330 180 0 j= 3: 330 360 0 j= 2: 150 0 j= 1: 0 The array s: i= 1 i= 2 i= 3 i= 4 i= 5 j= 6: 2 2 4 4 5 j= 5: 4 2 4 4 j= 4: 2 2 3 j= 3: 2 2 j= 2: 1 T1=A1*A2 T2=A3*A4 T3=A5*A6 T4=T2*T3 T5=T1*T4 Final result is in T5 ((A1)*(A2))*(((A3)*(A4))*((A5)*(A6))) ((A1*A2)*((A3*A4)*(A5*A6)))
C(n, 0) = 1, for all n C(n, n) = 1, for all n C(n, k) = C(n-1, k-1) + C(n-1, k), for n > k and k > 0
This example is on pages 277-278 of your text, but you are not
to use the book's algorithm on page 278 (no credit for this)
but you must use memoization as illustrated in the program
Fibonacci numbers.
As in this example, you should calculate without memoization,
counting the number of recursive calls, and compute with
memoization, counting the number of insertions into an array.
In this case you will need to use a 2-dimensional array.
Try this for n = 5, k = 3;
n = 10, k = 6;
n = 20, k = 10;
n = 25, k = 14;
n = 30, k = 15.
[Partial answer: C(20, 10) = 184756.]
Answer:
A program using memoization to calculate C is here:
Comb.c. Here is a log of output (edited):
% cc -o Comb Comb.c % Comb 5 3 ------------+-------+-------+--------------+--------------+----------+ Array used | n | k | C(n,k) | recursive | inserts | or not | | | | calls | | ------------+-------+-------+--------------+--------------+----------+ No array: | 5 | 3 | 10 | 19 | | Use array: | 5 | 3 | 10 | 13 | 18 | ------------+-------+-------+--------------+--------------+----------+ 10 6 ------------+-------+-------+--------------+--------------+----------+ No array: | 10 | 6 | 210 | 419 | | Use array: | 10 | 6 | 210 | 49 | 72 | ------------+-------+-------+--------------+--------------+----------+ 20 10 ------------+-------+-------+--------------+--------------+----------+ No array: | 20 | 10 | 184756 | 369511 | | Use array: | 20 | 10 | 184756 | 201 | 300 | ------------+-------+-------+--------------+--------------+----------+ 25 14 ------------+-------+-------+--------------+--------------+----------+ No array: | 25 | 14 | 4457400 | 8914799 | | Use array: | 25 | 14 | 4457400 | 309 | 462 | ------------+-------+-------+--------------+--------------+----------+ 30 15 ------------+-------+-------+--------------+--------------+----------+ No array: | 30 | 15 | 155117520 | 310235039 | | Use array: | 30 | 15 | 155117520 | 451 | 675 | ------------+-------+-------+--------------+--------------+----------+
X(1) = 1 X(n) = Sum (X(i)*X(n-i)), where the sum goes for i from 1 to n-1
It turns out that the formula for X(n) is:
X(n + 1) = (1/(n+1)) C(2n, n),
where this is the same C in the previous problem.
Use the results of the previous problem to find
X(11) and X(16).
[Partial answer: X(11) = 16796.]
Answer:
Consider the initial values for X(n):
X(1) = 1 X(2) = 1 X(3) = x(1)*X(2) + X(2)*X(1) = 1*1 + 1*1 = 2 X(4) = x(1)*X(3) + X(2)*X(2) + X(3)*X(1) = 1*2 + 1*1 + 2*1 = 5 X(5) = x(1)*X(4) + X(2)*X(3) + X(3)*X(2) + X(4)*X(1) = 1*5 + 1*2 + 2*1 + 5*1 = 14 X(6) = x(1)*X(5) + X(2)*X(4) + X(3)*X(3) + X(4)*X(2) + X(5)*X(1) = 1*14 + 1*5 + 2*2 + 5*1 + 14*1 = 42This shows why the summation formula for X(n) is correct, since on needs to locate the point of the last multiplication in the sequence, and that leads to two smaller values of X. Finally, here are values for X(n), where 1 <= n <= 17
Parenthesize 1 terms: 1 (C(1,1) = 1) Parenthesize 2 terms: 1 (C(2,1) = 2) Parenthesize 3 terms: 2 (C(4,2) = 6) Parenthesize 4 terms: 5 (C(6,3) = 20) Parenthesize 5 terms: 14 (C(8,4) = 70) Parenthesize 6 terms: 42 (C(10,5) = 252) Parenthesize 7 terms: 132 (C(12,6) = 924) Parenthesize 8 terms: 429 (C(14,7) = 3432) Parenthesize 9 terms: 1430 (C(16,8) = 12870) Parenthesize 10 terms: 4862 (C(18,9) = 48620) Parenthesize 11 terms: 16796 (C(20,10) = 184756) Parenthesize 12 terms: 58786 (C(22,11) = 705432) Parenthesize 13 terms: 208012 (C(24,12) = 2704156) Parenthesize 14 terms: 742900 (C(26,13) = 10400600) Parenthesize 15 terms: 2674440 (C(28,14) = 40116600) Parenthesize 16 terms: 9694845 (C(30,15) = 155117520) Parenthesize 17 terms: 35357670 (C(32,16) = 601080390)