dynamic programming [email protected]@gmail.com, [email protected]@rknec.edu tutorial...

36
Dynamic Programming [email protected] , [email protected] www.mbchandak.com Tutorial &Practice on Longest Common Sub-sequence Optimal Binary Search Tree

Upload: morgan-dickerson

Post on 31-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Dynamic [email protected], [email protected]

www.mbchandak.com

Tutorial &Practice on Longest Common Sub-sequence

Optimal Binary Search Tree

Page 2: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Characteristics of Dynamic Programming• Developed by Richard Bellman in 1950.• To provide accurate solution with the help of series of decisions. • Define a small part of problem and find out the optimal solution to

small part.• Enlarge the small part to next version and again find optimal

solution.• Continue till problem is solved.• Find the complete solution by collecting the solution of optimal

problems in bottom up manner.

Page 3: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Characteristics• Types of problems and solution strategies.• 1. Problem can be solved in stages.• 2. Each problem has number of states• 3. The decision at a stage updates the state at the stage into the

state for next stage.• 4. Given the current state, the optimal decision for the remaining

stages is independent of decisions made in previous states.• 5. There is recursive relationship between the value of decision at a

stage and the value of optimum decisions at previous stages.

Page 4: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Characteristics• How memory requirement is reduce:• How recursion overheads are converted into advantage• - By storing the results of previous n-2 computations in computation

of n-1 stage and will be used for computation of “n” stage.• Not very fast, but very accurate• It belongs to smart recursion class, in which recursion results and

decisions are used for next level computation. Hence not only results but decisions are also generated.• Final collection of results: Bottom Up Approach.

Page 5: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Longest Common Subsequence• Given two strings of characters, LCS is a method to

find a longest subsequence either continuous or non-continuous and is common in both the strings. The subsequence generation starts from comparison of first character of both the given strings.• The two strings may or may not be of equal length.• Let “A” and “B” be two strings of length “m” and “n”

respectively, and let “i” and “j” be two pointers to handle the two strings. • Let matrix c[m,n] be the storage matrix to store the results

of comparison related with two strings. The C[m,n] matrix will be handled using pointer “i” and “j”.

Page 6: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

If i=0 and j=0, then c[i,j] = 0If a[1..i] and b[1..j] is compared and a[i] is not

equal to b[j]. Then the comparison will be either In betweena[1..i-1] and b[1..j] or a[1..i] and b[1..j-1]

Thenc[i,j] = max[c[i-1,j], c[I,j-1]]If a[1..i] and b[1..j] is compared and a[i] is equal

to b[j].Then c[i,j] = c[i-1,j-1] + 1

Page 7: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

ExampleSTRING A = G D V E G T A B = G V C E K S T

i/j 0 1 2 3 4 5 6 7G V C E K S T

0 0/H 0/H 0/H 0/H 0/H 0/H 0/H 0/H1 G2 D3 V4 E5 G6 T7 A

H = Halt

Case:String A = No CharacterString B = All CharactersResult = 0 and Halt State

Page 8: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

ExampleSTRING A = G D V E G T A B = G V C E K S T

i/j 0 1 2 3 4 5 6 7G V C E K S T

0 0/H1 G 0/H2 D 0/H3 V 0/H4 E 0/H5 G 0/H6 T 0/H7 A 0/H

H = Halt

Case:String A = All CharactersString B = No CharacterResult = 0 and Halt State

Page 9: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

ExampleSTRING A = G D V E G T A B = G V C E K S T

i/j 0 1 2 3 4 5 6 7G V C E K S T

0 0/H 0/H 0/H 0/H 0/H 0/H 0/H 0/H1 G 0/H

1/D

H = HaltD = DiagonalS = Side

Case:String A = GString B = GResult = MatchUse diagonal value from the cell and add 1 to diagonal value0+1

i/j 0 1 2 3 4 5 6 7G V C E K S T

0 0/H 0/H 0/H 0/H 0/H 0/H 0/H 0/H1 G 0/H

1/D 1/S

Case:String A = GString B = VResult = No MatchUse Side or Upper cell value and select maximum valueIf both values are same select UPPER cell value

1/S 1/S 1/S 1/S 1/S

Page 10: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

ExampleSTRING A = G D V E G T A B = G V C E K S T

i/j 0 1 2 3 4 5 6 7G V C E K S T

0 0/H 0/H 0/H

0/H 0/H 0/H 0/H 0/H

1 G 0/H 1/D 1/S 1/S 1/S 1/S 1/S 1/S

2 D 0/H 1/U

H = HaltD = DiagonalS = Side

Case:String A = DString B = GResult = No Match

Since the value in Upper cell is greater than Side cellUse UPPER Cell value

Page 11: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

ExampleSTRING A = G D V E G T A B = G V C E K S T

i/j 0 1 2 3 4 5 6 7G V C E K S T

0 0/H 0/H

0/H 0/H 0/H 0/H 0/H 0/H

1 G 0/H 1/D 1/S 1/S 1/S 1/S 1/S 1/S

2 D 0/H 1/U

H = HaltD = DiagonalS = Side

Case:String A = DString B = VResult = No Match

Since the value in Upper cell is same as Side cellUse UPPER Cell value

1/U1/U 1/U 1/U 1/U 1/U

Page 12: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

ExampleSTRING A = G D V E G T A B = G V C E K S Ti/j 0 1 2 3 4 5 6 7

G V C E K S T0 0/H 0/H 0/H 0/H 0/H 0/H 0/H 0/H1 G 0/H 1/D 1/S 1/S 1/S 1/S 1/S 1/S2 D 0/H 1/U 1/U 1/U 1/U 1/U 1/U 1/U3 V 0/H 1/U 2/D 2/S 2/S 2/S 2/S 2/S4 E 0/H 1/U 2/U 2/U 3/D 3/S 3/S 3/S5 G 0/H 1/D 2/U 2/U 3/U 3/U 3/U 3/U6 T 0/H 1/U 2/U 2/U 3/U 3/U 3/U 4/D7 A 0/H 1/U 2/U 2/U 3/U 3/U 3/U 4/U

Page 13: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

ExampleSTRING A = G D V E G T A B = G V C E K S Ti/j 0 1 2 3 4 5 6 7

G V C E K S T0 0/H 0/H 0/H 0/H 0/H 0/H 0/H 0/H1 G 0/H 1/D 1/S 1/S 1/S 1/S 1/S 1/S2 D 0/H 1/U 1/U 1/U 1/U 1/U 1/U 1/U3 V 0/H 1/U 2/D 2/S 2/S 2/S 2/S 2/S4 E 0/H 1/U 2/U 2/U 3/D 3/S 3/S 3/S5 G 0/H 1/D 2/U 2/U 3/U 3/U 3/U 3/U6 T 0/H 1/U 2/U 2/U 3/U 3/U 3/U 4/D7 A 0/H 1/U 2/U 2/U 3/U 3/U 3/U 4/U

Page 14: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Example:2STRING A = X M J Y A U Z B = M Z J A W X U i/j 0 1X 2M 3J 4Y 5A 6U 7Z0 0/H 0/H 0/H 0/H 0/H 0/H 0/H 0/H1M 0/H 1/U 1/D 1/S 1/S 1/S 1/S 1/S2Z 0/H 1/U 1/U 1/U 1/U 1/U 1/U 2/D3J 0/H 1/U 1/U 2/D 2/S 2/S 2/S 2/S4A 0/H 1/U 1/U 2/U 2/U 3/D 3/S 3/S5W 0/H 1/U 1/U 2/U 2/U 3/U 3/U 3/U6X 0/H 1/D 1/U 2/U 2/U 3/U 3/U 3/U7U 0/H 1/U 1/U 2/U 2/U 3/U 4/D 4/S

Page 15: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Algorithm:• Assumptions: Let the two strings be present in

array “a” size “m” and array “b” size “n” handled using pointers “i“ and “j”.

• The resultant array will be c[m,n] : one instance c[i,j]. The array “c” will be structure: c[i,j].val and c[i,j].dir = “u”, “s”, “d” and “h”

Algorithm lcs (a,b: c){ for i = 0 to m do for j = 0 to n do if a[i]=0 or b[j]=0{ c[i,j].val = 0; c[i,j].dir=‘h’}else

{ if (a[i] ≠ b[j]) c[i,j].val = max [c[i-1, j].val, c[i,j-1].val] if(c[i-1, j].val >= c[i,j-1].val) c[i,j].dir = ‘u’ else c[i,j].dir=‘s’ else c[i,j].val = c[i-1,j-1].val + 1 c[i,j].dir = ‘d’}

Page 16: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Algorithm:• Printing LCS: Assumptions: Let “A”

be the given array. • Let “C” be the array containing

the details of LCS

Algorithm print_lcs (a,c,i,j){ if (i=0 or j=0) return; if(c[i,j] = ‘d’ {

print_lcs(a,c,i-1,j-1) print(a[i]) } else { if(c[i,j] = ‘u’ print_lcs(a,c,i-1,j) else print_lcs(a,c,i,j-1) }

Page 17: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Exercises

• Question 1:• String 1 : E X P O N E N T I A L• String 2 : P O L Y N O M I A L

• String 1 : SUBSEQUENCE• String 2 : CONSEQUENCES

Page 18: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Optimal Binary Search Tree (OBST)• Drawback of BST: The first value in the sequence is always used as

ROOT node.• If the data is present in sorted order, BST will be in the form of linked

list. This will increase search time and complexity equation will be not valid.• For example if dictionary word are to be stored in BST, it will be in the

form of linked list:Apple, Bat, Cat, Dog, Elephant, Fish, Gun, Horse, Ink, Jug, King, Lion

Since all the start characters are Greater than “A”, all the values will be on the Right side of the Apple.

Page 19: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Example of word list

Page 20: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Optimal Binary Search Tree• Need: Mechanism to decide out of the available values,

which value should be used as ROOT, so that the tree is balanced and COST of searching is minimum.• Requirement: For selection, the probability of data in the

domain text is required.• There are two values: Successful probability value and

Unsuccessful probability value.• Application: OBST is applied to generate the tree for the set

of values (keys) with defined successful and unsuccessful search probabilities.

Page 21: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Example:•Creation of matrices:• “w” = probability matrix• “e” = evolution matrix• “r” = root matrix

Dimensions:n = Total number of keys given in the search spaceMatrix “w” = [1..n+1, 0..n]Matrix “e” = [1..n+1, 0..n]For example number of keys are K1, K2, K3, K4 then n=4 and matrix “w” and “e” will have dimensions as: [1..5, 0..4]

Page 22: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Optimal Binary Search Tree

i 0 1 2 3 4pi 0.12 0.10 0.09 0.20qi 0.10 0.08 0.05 0.11 0.15

sum 0.10 0.20 0.15 0.20 0.35

Page 23: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

W 0 1 2 3 4

1 0.10 0.30 0.45 0.65 1.0

2 # 0.08 0.23 0.43 0.78

3 # # 0.05 0.25 0.60

4 # # # 0.11 0.46

5 # # # # 0.15

E 0 1 2 3 4

1 0.10 0.48/1 0.91/1 1.54/2 2.63/3

2 # 0.08 0.36/2 0.90/3 1.83/4

3 # # 0.05 0.41/3 1.16/4

4 # # # 0.11 0.72/4

5 # # # # 0.15

i 0 1 2 3 4

pi 0.12 0.10 0.09 0.20

qi 0.10 0.08 0.05 0.11 0.15

sum 0.10 0.20 0.15 0.20 0.35

Page 24: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Example 2: OBSTi 0 1 2 3 4 5

pi 0.05 0.20 0.05 0.10 0.15qi 0.10 0.10 0.05 0.05 0.05 0.10

sum 0.10 0.15 0.25 0.10 0.15 0.25

Page 25: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Solution: Example 2: AnimationW 0 1 2 3 4 5

1 0.10

2 0.10

3 0.05

4 0.05

5 0.05

6 0.10

e 0 1 2 3 4 5

1 0.10

2 0.10

3 0.05

4 0.05

5 0.05

6 0.10

i 0 1 2 3 4 5pi 0.05 0.20 0.05 0.10 0.15qi 0.10 0.10 0.05 0.05 0.05 0.10

sum 0.10 0.15 0.25 0.10 0.15 0.25

Page 26: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Empty matrix structure and formulaw 0 1 2 3 4 5

1

2 0.10

3 0.05 0.15

4 0.05 0.20

5 0.05 0.30

6 0.10

e 0 1 2 3 4 5

1 0.10

2 0.10

3 0.05

4 0.05

5 0.05

6 0.10

i 0 1 2 3 4 5

pi 0.05 0.20 0.05 0.10 0.15

qi 0.10 0.10 0.05 0.05 0.05 0.10

sum 0.10 0.10 0.15 0.250.15

0.10

0.10+0.15=0.25

0.25

0.25

0.10+0.25=0.35

0.35

Page 27: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Empty matrix structure and formulaw 0 1 2 3 4 5

1 0.25

2 0.10 0.35

3 0.05 0.15 0.30

4 0.05 0.20 0.45

5 0.05 0.30

6 0.10

e 0 1 2 3 4 5

1 0.10

2 0.10

3 0.05

4 0.05

5 0.05

6 0.10

i 0 1 2 3 4 5

pi 0.05 0.20 0.05 0.10 0.15

qi 0.10 0.10 0.05 0.05 0.05 0.10

sum 0.10 0.15 0.15 0.25

0.10

0.25

0.25+0.25=0.50

0.50

0.10

0.35+0.10=0.45

0.45

Page 28: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Empty matrix structure and formulaW 0 1 2 3 4 5

1 0.25 0.60 0.75 1.00

2 0.10 0.35 0.60 0.85

3 0.05 0.15 0.30 0.55

4 0.05 0.20 0.45

5 0.05 0.30

6 0.10

e 0 1 2 3 4 5

1 0.10

2 0.10

3 0.05

4 0.05

5 0.05

6 0.10

i 0 1 2 3 4 5

pi 0.05 0.20 0.05 0.10 0.15

qi 0.10 0.10 0.05 0.05 0.05 0.10

sum 0.10 0.15 0.25 0.10 0.15 0.25

0.10 0.50

0.45

Page 29: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Empty matrix structure and formulae 0 1 2 2 4 5

1 0.25 0.60 0.75 1.00

2 0.10 0.35 0.60 0.85

3 0.05 0.15 0.30 0.55

4 0.05 0.20 0.45

5 0.05 0.30

6 0.10

e 0 1 2 3 4 5

1 0.10 0.45/1

2 0.10

3 0.05

4 0.05

5 0.05

6 0.10

i 0 1 2 3 4 5

pi 0.05 0.20 0.05 0.10 0.15

qi 0.10 0.10 0.05 0.05 0.05 0.10

sum 0.10 0.15 0.25 0.10 0.15 0.25

0.10 0.50

0.45

e 0 1 2 3 4 5

1 0.10 0.45/1

2 0.10 0.50/2

3 0.05 0.25/3

4 0.05 0.30/4

5 0.05 0.45/5

6 0.10e[1,1] = e[1,0]+e[2,1]+w[1,1] 0.10+0.10+0.250.45e[2,2] = e[2,1]+2[3,2]+w[2,2] 0.10+0.05+0.350.50

Page 30: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Empty matrix structure and formulae 0 1 2 3 4 5

1 0.25 0.60 0.75 1.00

2 0.10 0.35 0.60 0.85

3 0.05 0.15 0.30 0.55

4 0.05 0.20 0.45

5 0.05 0.30

6 0.10

e 0 1 2 3 4 5

1 0.10 0.45/1 1.00/2

2 0.10 0.50/2

3 0.05 0.25/3

4 0.05 0.30/4

5 0.05 0.45/5

6 0.10

0.10 0.50

0.45

e 0 1 2 3 4 5

1 0.10 0.45/1 1.00/2

2 0.80/2

3 0.05

4 0.30/4

5 0.05 0.45/5

6 0.10

e[1,2] = Two options for root between values 1 and 2 are r=1 and r=2e[1,2] will be minimum of e[1,0]+e[2,2]+w[2,2] 0.10+0.50+0.50 =1.10 [r=1]e[1,1]+e[3,2]+w[2,2] 0.45+0.05+0.50 =1.00 [r=2]

0.10

0.25/3

First0.80

0.50

0.05

0.45

Second1.00

Final0.80/2

Page 31: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Empty matrix structure and formulae 0 1 2 3 4 5

1 0.25 0.60 0.75 1.00

2 0.10 0.35 0.60 0.85

3 0.05 0.15 0.30 0.55

4 0.05 0.20 0.45

5 0.05 0.30

6 0.10

0.10 0.50

0.45

e 0 1 2 3 4 5

1 0.10 0.45/1 1.00/2

2 0.80/2

3 0.05 0.25/3 0.60/4

4 0.05 0.30/4 0.85/5

5 0.05 0.45/5

6 0.10

0.10 0.50

0.45

Page 32: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Empty matrix structure and formulae 0 1 2 3 4 5

1 0.25 0.60 0.75 1.00

2 0.10 0.35 0.60 0.85

3 0.05 0.15 0.30 0.55

4 0.05 0.20 0.45

5 0.05 0.30

6 0.10

0.10 0.50

0.45

e 0 1 2 3 4 5

1 0.10 0.45/1 1.00/2

1.30/2

2 0.80/2

3 0.05 0.25/3 0.60/4

4 0.05 0.30/4 0.85/5

5 0.05 0.45/5

6 0.10

0.10 0.50

0.45

e 0 1 2 3 4 5

1 0.10 0.45/1 1.00/2

1.30/2

2 0.10 0.50 0.80/2 1.30/2

3 0.05 0.25/3 0.60/4

4 0.05 0.30/4 0.85/5

5 0.05 0.45/5

6 0.10

0 1 2 3 4 5

1 0.10 0.45/1 1.00/2

1.30/2

2 0.10 0.50 0.80/2 1.30/2

3 0.05 0.25/3 0.60/4 1.25/4,5

4 0.05 0.30/4 0.85/5

5 0.05 0.45/5

6 0.10

Page 33: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Empty matrix structure and formulae 0 1 2 3 4 5

1 0.25 0.60 0.75 1.00

2 0.10 0.35 0.60 0.85

3 0.05 0.15 0.30 0.55

4 0.05 0.20 0.45

5 0.05 0.30

6 0.10

0.10 0.50

0.450.45

e 0 1 2 3 4 5

1 0.10 0.45/1 1.00/2

1.30/2

1.80/2

2 0.10 0.50 0.80/2 1.30/2

3 0.05 0.25/3 0.60/4 1.25/4,5

4 0.05 0.30/4 0.85/5

5 0.05 0.45/5

6 0.10

e 0 1 2 3 4 5

1 0.10 0.45/1 1.00/2

1.30/2

1.80/2

2 0.10 0.50 0.80/2 1.30/2

2.25/5

3 0.05 0.25/3 0.60/4 1.25/4,5

4 0.05 0.30/4 0.85/5

5 0.05 0.45/5

6 0.10

Page 34: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Empty matrix structure and formula0 1 2 3 4 5

1 0.25 0.60 0.75 1.00

2 0.10 0.35 0.60 0.85

3 0.05 0.15 0.30 0.55

4 0.05 0.20 0.45

5 0.05 0.30

6 0.10

0.10 0.50

0.450.45

0 1 2 3 4 5

1 0.10 0.45/1 1.00/2 1.30/2 1.80/2 2.70/2

2 0.10 0.50 0.80/2 1.30/2 2.25/5

3 0.05 0.25/3 0.60/4 1.25/4,5

4 0.05 0.30/4 0.85/5

5 0.05 0.45/5

6 0.10

k2

K3,K4,K5K1

Refer the cell35 from theMatrix andFind root InformationTake next rootas 4 or 5Consider : 4

Cost of TreeAnd main root

Page 35: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Empty matrix structure and formula0 1 2 3 4 5

1 0.25 0.60 0.75 1.00

2 0.10 0.35 0.60 0.85

3 0.05 0.15 0.30 0.55

4 0.05 0.20 0.45

5 0.05 0.30

6 0.10

0.10 0.50

0.450.45

0 1 2 3 4 5

1 0.10 0.45/1 1.00/2 1.30/2 1.80/2 2.70/2

2 0.10 0.50 0.80/2 1.30/2 2.25/5

3 0.05 0.25/3 0.60/4 1.25/4,5

4 0.05 0.30/4 0.85/5

5 0.05 0.45/5

6 0.10

k2

K3,K4,K5K1

k3k1

k3 k5

Page 36: Dynamic Programming chandakmb@gmail.comchandakmb@gmail.com, hodcs@rknec.eduhodcs@rknec.edu  Tutorial &Practice on Longest Common Sub-sequence

Empty matrix structure and formula

0 1 2 3 4 51 0.10 0.45/

11.00/2

1.30/2

1.80/2

2.70/2

2 0.10 0.50 0.80/2

1.30/2

2.25/5

3 0.05 0.25/3

0.60/4

1.25/4,5

4 0.05 0.30/4

0.85/5

5 0.05 0.45/5

6 0.10

k2

K3,K4,K5K1

k3k1

k3 k5

i 0 1 2 3 4 5

pi 0.05 0.20 0.05 0.10 0.15

qi 0.10 0.10 0.05 0.05 0.05 0.10

sum 0.10 0.15 0.25 0.10 0.15 0.25

do d1

d2 d3 d4 d5

Verification: 1x0.20 = 0.202x(0.05+0.05) = 0.203x(0.10+0.10) + 3x(0.05+0.15)=1.204x(0.05+0.05+0.05+0.10) = 1.00TOTAL = 2.70

L1

L2

L3

L4

pi

qi