X = < 7, 3, 2, 8, 9, 6, 5, 4, 2 >
Y = < 3, 8, 6, 5, 7, 7, 8, 9, 4 >
Set value (xi = yj) = 1
Using algorithm in page 317 from [CLR], we get:
X
|
7
|
3
|
2
|
8
|
9
|
6
|
5
|
4
|
2
|
|
Y
|
0
|
0
|
0
|
0
|
0
|
0
|
0
|
0
|
0
|
0
|
3
|
0
|
0
|
1
|
1
|
1
|
1
|
1
|
1
|
1
|
1
|
8
|
0
|
0
|
1
|
1
|
2
|
2
|
2
|
2
|
2
|
2
|
6
|
0
|
0
|
1
|
1
|
2
|
2
|
3
|
3
|
3
|
3
|
5
|
0
|
0
|
1
|
1
|
2
|
2
|
3
|
4
|
4
|
4
|
7
|
0
|
1
|
1
|
1
|
2
|
2
|
3
|
4
|
4
|
4
|
7
|
0
|
1
|
1
|
1
|
2
|
2
|
3
|
4
|
4
|
4
|
8
|
0
|
1
|
1
|
1
|
2
|
2
|
3
|
4
|
4
|
4
|
9
|
0
|
1
|
1
|
1
|
2
|
3
|
3
|
4
|
4
|
4
|
4
|
0
|
1
|
1
|
1
|
2
|
3
|
3
|
4
|
5
|
5
|
In this example, LCS of < 7, 3, 2, 8, 9, 6, 5> and < 3, 8, 6, 5, 7, 7, 8, 9> is 3, 8, 6, 5, which is solution of optimal substructures of X and Y.
So solving optimal subproblems ® total problem ® optimal solution.
Giving you the following LCS recursion version which does not use a table to show you the overlapping subproblems property in which the dynamic programming is the better solution.
LCS (X, Y, m, n)
Begin
If ( m = 0 or n = 0 ) then
Return
Else
If ( xm = yn)
then
Return max {LCS (X, Y, m-1, n-1) + 1,
LCS (X, Y, m, n-1),
LCS (X, Y, m-1, n),}
Else
Return max {LCS (X, Y, m, n-1),
LCS (X, Y, m-1, n),}
End
M1 *
M2 * M3
* M4 *
M5 = M
[10,20] * [20,100] * [100,2] * [2,50] * [50,10] = [10,10]
In order to multiply M1*M2, the number of column
of M1 must be as same as the number of row of M2:
first matrix column = second matrix row.
So total result matrix has 10 rows, 10 columns.
Matrix multiplication is associative: It doesn't matter how you parenthesize the matrixes, it'll give the right answer. Depending on how you parenthesize the matrixes, the number of operations that you are going to perform will be different. For example:
Using dynamic programming to perform M1* M2( [a,b] * [b,c] ), ignoring the number of addition performed, filling in each entry of the table need b number of multiplication, and there are ac entries in M1* M2, so the total number of multiplication of M1* M2 is abc.
So the number of multiplication of following is:
(( M1 *
M2 ) *
M3)
(M1 * (M2
* M3))
(([10,20] * [20,100]) * [100,2])
([10,20] * ([20,100] * [100,2]))
So figuring out the best order of multiplication can save a lot of time.
An example shown below is a polygon with 2 different triangulations:
The problem is to minimize the total length of all the lines you draw.
The most efficient solution is by using dynamic programming.
The moment you draw one line, you break the polygon into two pieces,
then you can simply solve two subproblems. The hard thing is to decide
where to draw the first line. Once you draw the first line, you can "recursively"
solve the subproblems.
This problem is very similar to previous problems we discussed, so
it's easy to write recursion solution, but it won't be efficient, and what
you are going to do is using dynamic programming to solve the problem.
In this problem, you just want the maximum number of scheduled seminars. You can also be asked for which of these seminars you are going to schedule in that day.
The objective is to schedule as many seminars as possible. Clearly, the obvious requirement is that two seminars scheduled cannot overlap, i.e., the seminars scheduled for output must be completely disjointed.
First to prove that seminar one was a good choice to include in the
optimal solution. If you pick the one that finish first, then you have
best choice of scheduling the maximum number of seminar after the first
one finish, as long as you pick the one finished first.
Second to show that if a seminar is an optimal solution, then clearly
we can schedule that seminar and remove everything overlaps it, and solve
the rest of the problem.
If the whole strategy is correct, it should be correct for whatever
it is leftover, and using induction on whatever is leftover to solve the
rest of the subproblems.
Proof by contradiction of claim 1:
Assume [s1, f1] is not in any optimal solution.
Consider an optimal solution T.
Assume [si , fi] is the interval that finishes
first in T.
We can claim that [s1, f1] must overlap [si
, fi], otherwise you can add [s1, f1]
to this solution, and get extra seminar to schedule, contradicting the
assumption that T is optimal.
We now claim that [s1, f1] cannot overlap any
interval in T other than [si , fi]. This is because
f1 £ fi and the
starting time of all other intervals are after fi.
Proving the second step is also using contradiction.
Let's see how it is true for seminar case:
Think about the subproblem that you pick the seminar that finished first,
and remove all the seminars that overlap it, for the rest of the subproblem,
you can get the optimal solution for whatever left of the subproblem. If
you attach to it the first seminar, then you have the optimal solution
for the whole problem.
In a sense, you solve that subproblem, and add to this one particular
seminar, what you get is the optimal solution for the whole problem.
One simple way to do the encoding is using only two bits for a character:
A
00
C
01
G
10
T
11
When the code is the same length, it's easy to decode the encoded message.
Let's assume you are told the frequency of the characters in the message.
In this case, it really doesn't make any sense to use above code. You can
do much better by using a shorter code for more frequent character and
longer code for less frequent character.
The question is how do I design these codes so that encoded message's
length is as less as possible and can also be decoded.
Following is a example:
Character
Frequency Code1
Code2 Code3
A
70%
00
0
0
C
20%
01
1
10
G
5%
10
11
110
T
5%
11
10
111
Using code2 is easy to encode the message and total length is shorter,
but decoding is difficult, or impossible.
Using code3 is the best of the three.
This problem can be solved using Greedy algorithms.
Knapsack's weight limit is B
Output: Combination of items
such that (1). Total weight £ B
(2). Total value is maximized.
The original knapsack problem is sometimes called the 0-1 knapsack problem since you could either pick the entire item or none at all.
The fact that greedy algorithm stops after finishing scheduling the nth seminar means no seminars starting after time fn. By the claim: fi£ f'i, we can conclude that [s'n+1, f'n+1] doesn't exist. Thus n ³ m.
Proof of claim by induction:
i = 1, f1£ f'1
is true, since [s1, f1] is the seminar which
finish the earliest.
Assume fi£ f'i,
it's obvious that fi+1£
f'i+1, since [si+1, fi+1] is the
one starting after time fi and finishing the earliest and [s'i+1,
f'i+1] starts after time fi.