Chapter3_odd
Chapter3_odd
Section 3.1
n − 1 n − 1 (n − 1)! (n − 1)!
We are left with the case 0 < k < n: + = + =
k − 1 k (k − 1)!(n − k )! k!(n − k − 1)!
(n − 1)! (n − 1)! (n − 1)! n(n − 1)! n! n
k + (n − k ) = (k + n − k ) = = =
k!(n − k )! k!(n − k )! k!(n − k )! k!(n − k )! k!(n − k )! k
3) Implement both algorithms for the Binomial Coefficient problem (Algorithms 3.1 and 3.2)
on your system and study their performances using different problem instances.
//Recursive binomial
int bino_rec(int n, int k){
if ( k == 0 || k == n ) /* base case */
return 1;
else /* recursive step */
return bino_rec(n-1, k-1) + bino_rec(n-1, k);
}
int min;
int main(){
time_t begin, end;
int N, k;
cout << ("Enter positive integer N(<=100): ");
cin >> N;
cout << ("Enter positive integer k(<=100): ");
cin >> k;
begin = time(NULL);
cout << "Binomial recursive : " << bino_rec(N,k) << endl;
end = time(NULL);
cout << "\ttime = " << difftime(end, begin) << " s" << endl;
begin = time(NULL);
cout << "Fibonacci dynamic prog.: " << bino_dyn(N,k) << endl;
end = time(NULL);
cout << "\ttime = " << difftime(end, begin) << " s" << endl <<endl;
return 0;
}
Section 3.2
5) Use Floyd’s algorithm for the Shortest Paths problem 2 (Algorithm 3.4) to
construct the matrixD, which contains the lengths of the shortest paths, and
the matrixP, which contains the highest indices of the intermediate vertices
on the shortest paths, for the following graph. Show the actions step by step.
Constructing D:
D0:
0 4 INF INF INF 10 INF
3 0 INF 18 INF INF INF
INF 6 0 INF INF INF INF
INF 5 15 0 2 19 5
INF INF 12 1 0 INF INF
INF INF INF INF INF 0 10
INF INF INF 8 INF INF 0
D1:
0 4 INF INF INF 10 INF
3 0 INF 18 INF 13 INF
INF 6 0 INF INF INF INF
INF 5 15 0 2 19 5
INF INF 12 1 0 INF INF
INF INF INF INF INF 0 10
INF INF INF 8 INF INF 0
Chapter 3: Dynamic Programming Solutions
D2:
0 4 INF 22 INF 10 INF
3 0 INF 18 INF 13 INF
9 6 0 24 INF 19 INF
8 5 15 0 2 18 5
INF INF 12 1 0 INF INF
INF INF INF INF INF 0 10
INF INF INF 8 INF INF 0
D3:
0 4 INF 22 INF 10 INF
3 0 INF 18 INF 13 INF
9 6 0 24 INF 19 INF
8 5 15 0 2 18 5
21 18 12 1 0 31 INF
INF INF INF INF INF 0 10
INF INF INF 8 INF INF 0
D4:
0 4 37 22 24 10 27
3 0 33 18 20 13 23
9 6 0 24 26 19 29
8 5 15 0 2 18 5
9 6 12 1 0 19 6
INF INF INF INF INF 0 10
16 13 23 8 10 26 0
D5:
0 4 36 22 24 10 27
3 0 32 18 20 13 23
9 6 0 24 26 19 29
8 5 14 0 2 18 5
9 6 12 1 0 19 6
INF INF INF INF INF 0 10
16 13 22 8 10 26 0
D6:
0 4 36 22 24 10 20
3 0 32 18 20 13 23
9 6 0 24 26 19 29
8 5 14 0 2 18 5
9 6 12 1 0 19 6
INF INF INF INF INF 0 10
16 13 22 8 10 26 0
Chapter 3: Dynamic Programming Solutions
D7:
0 4 36 22 24 10 20
3 0 32 18 20 13 23
9 6 0 24 26 19 29
8 5 14 0 2 18 5
9 6 12 1 0 19 6
26 23 32 18 20 0 10
16 13 22 8 10 26 0
Constructing P:
P0:
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
P1:
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
P2:
0 0 0 2 0 0 0
0 0 0 0 0 0 0
2 0 0 2 0 2 0
2 0 0 0 0 2 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
P3:
0 0 0 2 0 0 0
0 0 0 0 0 0 0
2 0 0 2 0 2 0
2 0 0 0 0 2 0
3 3 0 0 0 3 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Chapter 3: Dynamic Programming Solutions
P4:
0 0 4 2 4 0 4
0 0 4 0 4 0 4
2 0 0 2 4 2 4
2 0 0 0 0 2 0
4 4 0 0 0 4 4
0 0 0 0 0 0 0
4 4 4 0 4 4 0
P5:
0 0 5 2 4 0 4
0 0 5 0 4 0 4
2 0 0 2 4 2 4
2 0 5 0 0 2 0
4 4 0 0 0 4 4
0 0 0 0 0 0 0
4 4 5 0 4 4 0
P6:
0 0 5 2 4 0 6
0 0 5 0 4 0 4
2 0 0 2 4 2 4
2 0 5 0 0 2 0
4 4 0 0 0 4 4
0 0 0 0 0 0 0
4 4 5 0 4 4 0
P7:
0 0 5 2 4 0 6
0 0 5 0 4 0 4
2 0 0 2 4 2 4
2 0 5 0 0 2 0
4 4 0 0 0 4 4
7 7 7 7 7 0 0
4 4 5 0 4 4 0
7) Analyze the Print Shortest Path algorithm (Algorithm 3.5) and show that it has a linear-
time complexity.
9) Can Floyd’s algorithm for the Shortest Paths problem 2 (Algorithm 3.4) be modified to give
just the shortest path from a given vertex to another specified vertex in a graph? Justify your
answer.
Working backwards from D(n)(i,j), we note that it only depends on 3 values from the previous
table D(n-1). Based on this it is possible to reduce the number of operations by only calculating
one value from D(n), three values from D(n-1), etc. As we regress towards D(0), however, the
number of values grows and ends up being n2 (the entire table), and the asymptotic
complexity remains (n3).
Section 3.3
11) Find an optimization problem in which the principle of optimality does not apply and
therefore the optimal solution cannot be obtained using dynamic programming. Justify your
answer.
The text example: Finding the longest path between two vertices A → C of a graph (without
loops) does not have optimal substructure: if the longest path is A → … → B → … → C, this
doesn’t mean that:
• the subpath B → … → C is longest; for instance, in the B→C problem we may find that the
longest path contains A, which would create a cycle in the original problem A → C, so it’s
not allowed there.
• the subpath A → … → B is longest; for instance, in the A→B problem we may find that the
longest path contains C.
Another example: Raising a number to an integer power n with the fewest number of
multiplications (a.k.a. the Addition Chain Exponentiation problem). When n is a power of 2,
the best way is successive squaring, e.g. x8 =((x∙x)2)2 requires only 3 multiplications. If n is not
a power of 2, however, there is no optimal substructure, because a subproblem is not always
solved optimally by itself; we can prefer a suboptimal solution to a subproblem because parts
of the solution can be reused to solve other subproblems. This problem was proved to be NP-
complete (see Ch.9).
Chapter 3: Dynamic Programming Solutions
Section 3.4
13) Find the optimal order, and its cost, for evaluating the product A1×A2× A3×A4×A5, where
A1 is (10×4)
A2 is (4×5)
A3 is (5×20)
A4 is (20×2)
A5 is (2×50)
Show the final matrices M and P produced by Algorithm 3.6.
M:
P:
1 1 1 4
2 2 4
3 4
4
15) Show that a divide-and-conquer algorithm based on Equality 3.5 has an exponential-time
complexity.
As in the analysis of the Minimum Multiplications algorithm, we take the basic operation to be
one evaluation of k. A problem of size n (obtaining the 1320 in example above) requires
evaluation of more than two problems of size n-1 (320 and 640 above), so we have the
inequality T(n) ≥ 2T(n-1) ≥ 2∙2T(n-2) ≥ … ≥ 2n.
n −1 n −1
By multiplying out each term inside the sum, we have: n d − d 2
. The first sum is solved
d =1 d =1
in Example A.1, and the second in Example A.2; we only have to replace n with n-1:
(n − 1)n (n − 1)n [2(n − 1) + 1] n(n − 1)( n + 1)
n − = .
2 6 6
19) Analyze Algorithm 3.7 and show that it has a linear-time complexity.
Every call to order prints one pair of parentheses, and according to Exercise 18, a problem of
size n needs only n-1 pairs, which is (n).
Chapter 3: Dynamic Programming Solutions
Section 3.5
21) How many different binary search trees can be constructed using six distinct keys?
j
23) Find an efficient way to compute p
m =i
m which is used in the Optimal Binary Search Tree
j
The partial sums can be stored in an n x n table B, whose position B[i][j] stores p
m =i
m . We
25) Analyze Algorithm 3.10, and show its time complexity using order notation.
We define the basic operation to be the access of an element of the array R; this is done once
per each call to the function tree, and each such call inserts in the tree one node. Since there
are n nodes total, the number of calls and that of basic operations is n, so the complexity is
linear, (n).
Chapter 3: Dynamic Programming Solutions
27) Show that a divide-and-conquer algorithm based on Equality 3.6 has an exponential time
complexity.
0 1 2 3 4 5 6
1 0 .05 .25 .35 .95 1.05 1.8
2 0 .15 .25 .8 .9 1.65
3 0 .05 .45 .55 1.3
4 0 .35 .45 1.2
5 0 .05 .45
6 0 .35
7 0
As in the analysis of the Minimum Multiplications algorithm, we take the basic operation to be
one evaluation of k. A problem of size n (obtaining the 1.8 in example above) requires
evaluation of more than two sub-problems of size n-1 (1.05 and 1.65 above), so we have the
inequality T(n) ≥ 2T(n-1) ≥ 2∙2T(n-2) ≥ … ≥ 2n.
Chapter 3: Dynamic Programming Solutions
Section 3.6
29) Write a more detailed version of the Dynamic Programming algorithm for the TSP
(Algorithm 3.11).
One design decision is how to store the subsets of vertices. This can be done, for example, with
an array of n linked lists, each of them holding the set of vertices (from zero – empty set – to
(n – 2) sets) and the respective optimal cost and j vertex associated.
Below is an example of matrix D for n = 5 (from Exercise 28). The “ground” symbol means a
null pointer. Only the line for v2 is shown:
The sets themselves can be stored as strings, arrays of boolean (used below), or even the data
structure described in Appendix C.
For homogeneity, the list node for the empty set can be made the same as the others
(although technically it does not need a j index or a next pointer):
struct NodeType{
bool set[n-1];
int min;
int jay;
struct NodeType* next;
};
Chapter 3: Dynamic Programming Solutions
When we generate the subsets A ⊆ V− {v1} containing k vertices, we need to store them in an
array:
bool arrayOfSubsets[1..maxSubsets][n-1]
where maxSubsets is the maximum over k of Binomial(n-2, k), so it can store the most subsets
that can be generated. Alternatively, the subsets can also be stored in a linked list.