Professional Documents
Culture Documents
Ass#5
Ass#5
d j mi ,k mk 1, j }
ik j
Rod_Cut (p, n)
r[0] = 0
for j = 1 to n
q=-∞
for i = 1 to j
r[j] = q
return r[n]
F[0] = 0
n 0 1 2 3 4 5 6 7 8 9
F 0
n 0 1 2 3 4 5 6 7 8 9
F 0 1
n 0 1 2 3 4 5 6 7 8 9
F 0 1 2
n 0 1 2 3 4 5 6 7 8 9
F 0 1 2 1
n 0 1 2 3 4 5 6 7 8 9
F 0 1 2 1 2
n 0 1 2 3 4 5 6 7 8 9
F 0 1 2 1 2 1
n 0 1 2 3 4 5 6 7 8 9
F 0 1 2 1 2 1 2
n 0 1 2 3 4 5 6 7 8 9
F 0 1 2 1 2 1 2 3
n 0 1 2 3 4 5 6 7 8 9
F 0 1 2 1 2 1 2 3 2
n 0 1 2 3 4 5 6 7 8 9
F 0 1 2 1 2 1 2 3 2 3
Item3 The maximal value of a feasible subset is V [5, 6] = 65. The optimal subset is {item 3, item 5}.
b) 65 optimal solutions
c)The instance has a unique optimal subset in view of the following general property: An instance of
the knapsack problem has a unique optimal solution if and only if the algorithm for obtaining an
optimal subset, which retraces backward the computation of V [n, W], encounters no equality between
V [i − 1, j] and vi + V [i − 1, j − wi] during its operation.
a) Fib(n)
let fib[0..n] be a new array
fib[0] = fib[1] = 1
for i = 2 to n
fib[i] = fib[i - 1] + fib[i - 2]
return fib[n]
b) There are n + 1 vertices in the subproblem graph, i.e., v0, v1,…, vn.
For v0, v1, each has 0 leaving edge.
For v2, v3, …, vn, each has 2 leaving edges.
The Merge-Sort procedure does not have overlapping subproblems. In other word, all node of the
recursion tree are distinct (do not reuse the results of recursive call). That's why DP does not improve
the efficiency of The Merge-Sort procedure.
Yi 0 1 0 1 1 0 1 1 0
Xi 0 0 0 0 0 0 0 0 0 0
1 0 0 1 1 1 1 1 1 1 1
0 0 1 1 2 2 2 2 2 2 2
0 0 1 1 2 2 2 3 3 3 3
1 0 1 2 2 3 3 3 4 4 4
0 0 1 2 3 3 3 4 4 4 5
1 0 1 2 3 4 4 4 5 5 5
0 0 1 2 3 4 4 5 5 5 6
1 0 1 2 3 4 5 5 6 6 6
LCS= < 100110 >
a.
b.
-:0.15 C: 0.2 A: 0.4
0.25
0 1
B: 0.1 D: 0.15
c. 0.
0.25 3 A: 0.4
5
0 1 0 1
B: 0.1 D: 0.15 -:0.15 C: 0.2
d.
A: 0.4
0.60
0 1
0.25 0.35
0 1 0 1
B: 0.1 D: 0.15 -:0.15 C: 0.2
e.
1.
00
0 1
A: 0.4
0.6
0
0 1
0.2 0.3
5 5
0 1 0 1
B: 0.1 D: 0.15 -:0.15 C: 0.2
Constructing the Huffman Code:
B: 100 D: 101 -:110 C: 111 A: 0