Dynami mic Programmi mming Jeevani Goone*llake University - - PowerPoint PPT Presentation

dynami mic programmi mming
SMART_READER_LITE
LIVE PREVIEW

Dynami mic Programmi mming Jeevani Goone*llake University - - PowerPoint PPT Presentation

Dynami mic Programmi mming Jeevani Goone*llake University of Colombo School of Compu*ng Sri Lanka Jeevani Goone*llake (University of Colombo - Sri Lanka)


slide-1
SLIDE 1

Dynami mic ¡ ¡Programmi mming

Jeevani ¡Goone*llake ¡ University ¡of ¡Colombo ¡School ¡of ¡Compu*ng ¡ Sri ¡Lanka ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-2
SLIDE 2

Dynami mic ¡ ¡Programmi mming

  • Dynamic ¡ Programming, ¡ like ¡ the ¡ divide-­‑and-­‑conquer ¡

approach ¡ method, ¡ solves ¡ problems ¡ by ¡ combining ¡ the ¡ solu*ons ¡to ¡sub-­‑problems. ¡

  • In ¡Divide ¡and ¡Conquer, ¡the ¡sub-­‑problems ¡are ¡independent ¡
  • f ¡each ¡other ¡
  • In ¡contrast, ¡dynamic ¡programming ¡is ¡applicable ¡when ¡the ¡

sub ¡ problems ¡ are ¡ overlapping. ¡ That ¡ is ¡ when ¡ solu*on ¡ of ¡

  • ne ¡sub-­‑problem ¡may ¡be ¡required ¡to ¡solve ¡another ¡sub-­‑
  • problem. ¡

¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-3
SLIDE 3

Dynami mic ¡ ¡Programmi mming

Dynamic ¡Programming ¡(shortened ¡as ¡DP) ¡ ¡

  • ¡A ¡programming ¡technique ¡ ¡
  • ¡Can ¡drama*cally ¡reduce ¡the ¡run*me ¡of ¡some ¡algorithms ¡

(but ¡ not ¡ all ¡ problem ¡ has ¡ DP ¡ characteris*cs) ¡ from ¡ ¡ exponen*al ¡to ¡polynomial. ¡ ¡

¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-4
SLIDE 4

Dynami mic ¡ ¡Programmi mming ¡ ¡(DP)

  • Dynamic ¡ Programming ¡ algorithm ¡ solves ¡ every ¡ sub ¡ sub-­‑

problem ¡just ¡once ¡and ¡saves ¡its ¡answer ¡in ¡a ¡table. ¡ ¡ ¡

  • This ¡ avoids ¡ the ¡ work ¡ of ¡ re-­‑compu*ng ¡ the ¡ answer ¡ every ¡

*me ¡ ¡the ¡sub ¡sub-­‑problem ¡is ¡encountered. ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-5
SLIDE 5

Eleme ments ¡ ¡of ¡ ¡DP

  • Op#mal ¡ substructure: ¡ Dynamic ¡ Programming ¡ typically ¡

applied ¡to ¡op#miza#on ¡problems. ¡ ¡ ¡

  • In ¡such ¡problems ¡there ¡can ¡be ¡many ¡possible ¡solu*ons. ¡ ¡
  • Each ¡solu*on ¡has ¡a ¡value ¡and ¡we ¡wish ¡to ¡find ¡a ¡solu*on ¡

with ¡the ¡op*mal ¡value. ¡ ¡ ¡ ¡ ¡ ¡In ¡DP ¡we ¡build ¡an ¡op*mal ¡solu*on ¡to ¡the ¡problem ¡from ¡

  • p*mal ¡ ¡ ¡solu*ons ¡to ¡sub-­‑problems. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-6
SLIDE 6

Eleme ments ¡ ¡of ¡ ¡DP

  • Overlapping ¡ sub-­‑problems: ¡ A ¡ recursive ¡ algorithm ¡ for ¡ the ¡

problem ¡ solves ¡ the ¡ same ¡ sub-­‑problems ¡ over ¡ and ¡ over, ¡ rather ¡ than ¡always ¡genera*ng ¡new ¡sub-­‑problems. ¡ ¡

  • DP ¡algorithms ¡take ¡advantage ¡of ¡overlapping ¡sub-­‑problems ¡by ¡

¡-­‑ ¡ ¡solving ¡each ¡sub-­‑problem ¡once ¡and ¡ ¡

  • ­‑ storing ¡the ¡solu*on ¡in ¡a ¡table ¡where ¡it ¡can ¡be ¡looked ¡ ¡

¡ ¡ ¡up ¡when ¡needed, ¡using ¡constant ¡*me ¡per ¡look ¡up. ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-7
SLIDE 7

Dynami mic ¡ ¡Programmi mming

¡ ¡ ¡ ¡ ¡Applica*ons: ¡ ¡ ¡

  • Compu*ng ¡combina*ons ¡ ¡
  • Assembly-­‑line ¡scheduling ¡
  • Matrix-­‑chain ¡mul*plica*on ¡
  • Longest ¡Common ¡Subsequence ¡
  • …………………………………. ¡

¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-8
SLIDE 8

Fi Fibonacci ¡ ¡Numb mbers

  • Consider ¡the ¡algorithm ¡for ¡calcula*ng ¡Fibonacci ¡numbers: ¡ ¡

¡

¡int ¡fib( ¡int ¡n ¡) ¡{ ¡ ¡ ¡

¡ ¡if ¡( ¡n ¡< ¡2 ¡) ¡return ¡n; ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡fib(n-­‑1) ¡+ ¡fib(n-­‑2); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ What ¡about ¡the ¡running ¡4me ¡of ¡this ¡algorithm? ¡ ¡ ¡ ¡ ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-9
SLIDE 9

Fi Fibonacci ¡ ¡Numb mbers

  • Consider ¡the ¡algorithm ¡for ¡calcula*ng ¡Fibonacci ¡numbers: ¡ ¡

¡

¡int ¡fib( ¡int ¡n ¡) ¡{ ¡ ¡ ¡

¡ ¡if ¡( ¡n ¡< ¡2 ¡) ¡return ¡n; ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡fib(n-­‑1) ¡+ ¡fib(n-­‑2); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡O(fn) ¡= ¡O(1.6n) ¡ ¡ ¡

  • So ¡this ¡simple ¡func*on ¡will ¡take ¡exponen*al ¡*me! ¡WHY? ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-10
SLIDE 10

Fi Fibonacci ¡ ¡Numb mbers

fib(5) ¡ fib(4) ¡ fib(3) ¡ fib(3) ¡ fib(2) ¡ fib(2) ¡ fib(1) ¡ fib(2) ¡ fib(1) ¡ fib(1) ¡ fib(0) ¡ fib(1) ¡ fib(0) ¡ fib(1) ¡ fib(0) ¡

For ¡ example, ¡ the ¡ problem ¡ of ¡ compu*ng ¡ the ¡ Fibonacci ¡ sequence ¡ exhibits ¡overlapping ¡sub-­‑problems. ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-11
SLIDE 11

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Dynami

mic ¡ ¡Programmi mming ¡ ¡Techniques

¡? ¡ ¡? ¡

Iterative Dynamic Programming

(Filling a table “from one corner”)

Memoization

Filling a table recursively “by need”. Memoization of results of earlier calls avoids exponential blow-up. ¡

Obtained ¡from ¡Frank ¡Drew’s ¡slides ¡on ¡Dynamic ¡Programming ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-12
SLIDE 12

Dynami mic ¡ ¡Programmi mming

  • We ¡can ¡either ¡build ¡up ¡solu*ons ¡of ¡sub-­‑problems ¡from ¡ ¡

¡ ¡ ¡small ¡to ¡large ¡bo`om ¡up ¡-­‑ ¡controlled ¡brute ¡force) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡or ¡ ¡ ¡ ¡top ¡down, ¡memoized ¡algorithm ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡-­‑ ¡recursion ¡+ ¡memoiza*on ¡(i.e. ¡re-­‑use) ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-13
SLIDE 13

Memo moiza?on

  • A ¡varia*on ¡of ¡DP ¡
  • Maintains ¡a ¡top-­‑down ¡strategy. ¡
  • Idea ¡is ¡to ¡memoize ¡the ¡natural ¡but ¡inefficient, ¡recursive ¡
  • algorithm. ¡
  • As ¡ in ¡ ordinary ¡ dynamic ¡ programming ¡ a ¡ table ¡ with ¡ sub ¡

problems ¡ is ¡ maintained, ¡ but ¡ the ¡ control ¡ structure ¡ for ¡ filling ¡in ¡the ¡table ¡is ¡more ¡like ¡the ¡recursive ¡algorithm. ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-14
SLIDE 14

int fib(int n) { if (n==1 || n==2) return 1; else return fib(n-1) + fib(n-2); } int fib(int n) { memo[1] = memo[2] = 1; for (int i=3; i<=n; i++) memo[i] = memo[i-1] + memo[i-2]; return memo[n]; }

¡ ¡ ¡int fb(int n) {

if (n == 1 || n == 2) return 1; if (memo[n] != 0) return memo[n]; memo[n] = fb(n-1) + fb(n-2); return memo[n]; } ¡

Recursive ¡Algorithm ¡ ¡ ¡O(2n) ¡

BoIom ¡Up ¡Dynamic ¡Programming ¡Approach ¡ O(n) ¡ ¡ ¡ ¡Top ¡Down ¡memoiza#on ¡Approach ¡ O(n) ¡

Fibonacci ¡Sequence ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-15
SLIDE 15

Dynami mic ¡ ¡Programmi mming

  • Typically, ¡a ¡greedy ¡algorithm ¡is ¡used ¡to ¡solve ¡a ¡problem ¡

with ¡op*mal ¡substructure ¡wherever ¡such ¡an ¡algorithm ¡ can ¡be ¡found; ¡ ¡

  • Otherwise, ¡ if ¡ the ¡ problem ¡ exhibits ¡ overlapping ¡

subproblems ¡as ¡well, ¡dynamic ¡programming ¡is ¡used. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-16
SLIDE 16

Longest ¡ ¡Commo mmon ¡ ¡Subsequences ¡ ¡(LCS)

  • Given ¡ two ¡ sequences, ¡ find ¡ the ¡ length ¡ of ¡ longest ¡ subsequence ¡

present ¡in ¡both ¡of ¡them. ¡ ¡

  • A ¡ subsequence ¡ is ¡ a ¡ sequence ¡ that ¡ appears ¡ in ¡ the ¡ same ¡ rela*ve ¡
  • rder, ¡but ¡not ¡necessarily ¡con*guous. ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-17
SLIDE 17

Longest ¡ ¡Commo mmon ¡ ¡Subsequences ¡ ¡ ¡

  • Applica*ons ¡: ¡Bioinforma*cs ¡(DNA ¡string ¡comparison). ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Check ¡differences ¡between ¡two ¡files ¡ ¡

  • DNA ¡string: ¡a ¡sequence ¡of ¡symbols ¡A,C,G,T. ¡

¡S=ACCGGTCGAGCTTCGAAT ¡ ¡

  • Subsequence ¡(of ¡X): ¡is ¡X ¡with ¡some ¡symbols ¡leh ¡out. ¡ ¡

¡ ¡ ¡Z=CGTC ¡is ¡a ¡subsequence ¡of ¡X=ACGCTAC. ¡

¡ ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-18
SLIDE 18

Longest ¡ ¡Commo mmon ¡ ¡Subsequences

  • Common ¡subsequence ¡Z ¡(of ¡X ¡and ¡Y): ¡a ¡subsequence ¡of ¡X ¡and ¡

also ¡a ¡subsequence ¡of ¡Y. ¡

¡ ¡ ¡Z=CGA ¡is ¡a ¡common ¡subsequence ¡of ¡both ¡X=ACGCTAC ¡and ¡Y=CTGACA. ¡ ¡

  • Longest ¡Common ¡Subsequence ¡(LCS): ¡the ¡longest ¡one ¡of ¡common ¡
  • subsequences. ¡ ¡

¡ ¡Z' ¡=CGCA ¡is ¡the ¡LCS ¡of ¡the ¡above ¡X ¡and ¡Y. ¡ ¡

  • LCS ¡problem: ¡given ¡X=<x1, ¡x2,…, ¡xm> ¡and ¡Y=<y1, ¡y2,…, ¡yn>, ¡find ¡

their ¡LCS. ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-19
SLIDE 19

Longest ¡ ¡Commo mmon ¡ ¡Subsequences

  • Naive ¡solu#on: ¡List ¡all ¡possible ¡subsequences ¡of ¡X, ¡check ¡

whether ¡they ¡are ¡also ¡subsequences ¡of ¡Y, ¡keep ¡the ¡longer ¡

  • ne ¡each ¡*me. ¡ ¡
  • Each ¡subsequence ¡corresponds ¡to ¡a ¡subset ¡of ¡the ¡indices. ¡

A ¡ string ¡ of ¡ length ¡ n ¡ has ¡ 2n ¡ different ¡ possible ¡

  • subsequences. ¡ This ¡ solu*on ¡ is ¡ exponen*al ¡ in ¡ terms ¡ of ¡

*me ¡complexity. ¡ ¡ ¡

¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-20
SLIDE 20

Longest ¡ ¡Commo mmon ¡ ¡Subsequences

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡m=0, ¡or ¡n=0 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡c[m-­‑1,n-­‑1]+1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡m,n ¡> ¡0 ¡and ¡xm= ¡yn, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡max{c[m-­‑1,n], ¡c[m,n-­‑1]} ¡ ¡ ¡if ¡m,n ¡> ¡0 ¡and ¡xm ¡≠ ¡yn, ¡ ¡ This ¡gives ¡a ¡recursive ¡algorithm ¡to ¡solve ¡the ¡problem. ¡ ¡ c[m,n] = ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-21
SLIDE 21

¡ ¡LCS ¡ ¡-­‑

  • ­‑ ¡

¡Op?ma mal ¡ ¡Structure ¡ ¡Property

¡

  • ¡ ¡ ¡ ¡ ¡Let ¡ ¡X=<x1, ¡x2,…, ¡xm> ¡(= ¡Xm) ¡ ¡and ¡ ¡Y=<y1, ¡y2,…,yn> ¡(= ¡Yn) ¡ ¡and ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

¡ ¡ ¡ ¡Z=<z1, ¡z2,…, ¡zk> ¡(= ¡Zk) ¡be ¡any ¡LCS ¡of ¡X ¡and ¡Y, ¡ ¡ ¡ i) ¡if ¡ ¡xm= ¡yn, ¡then ¡zk= ¡xm= ¡yn, ¡and ¡Zk-­‑1 ¡is ¡the ¡LCS ¡of ¡Xm-­‑1 ¡and ¡Yn-­‑1. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

¡A ¡ ¡A ¡ X ¡ Y ¡ Xm ¡ Yn ¡ Xm-­‑1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Yn-­‑1 ¡ ¡A ¡ ¡A ¡ Last ¡characters ¡ match ¡ Add ¡to ¡LCS ¡ LCS ¡ Case ¡i) ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-22
SLIDE 22

LCS ¡ ¡-­‑

  • ­‑ ¡

¡Op?ma mal ¡ ¡Structure ¡ ¡Property

iii) ¡if ¡xm≠ ¡yn, ¡then ¡zk ¡≠ ¡yn ¡implies ¡Z ¡is ¡the ¡LCS ¡of ¡ ¡Xm ¡and ¡Yn-­‑1. ¡

¡A ¡ ¡B ¡ X ¡ Y ¡ Xm ¡ Yn ¡ Xm-­‑1 ¡ ¡A ¡ Last ¡characters ¡do ¡ not ¡match ¡ LCS ¡ ¡B ¡ Skip ¡Xm ¡ ¡ Y ¡

ii) ¡if ¡xm≠ ¡yn, ¡then ¡zk ¡≠ ¡xm ¡implies ¡Z ¡is ¡the ¡LCS ¡of ¡ ¡Xm-­‑1 ¡and ¡Yn. ¡

¡A ¡ ¡B ¡ X ¡ Y ¡ Xm ¡ Yn ¡ ¡B ¡ Last ¡characters ¡do ¡ not ¡match ¡ LCS ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Yn-­‑1 ¡ Skip ¡Yn ¡ ¡ Case ¡ii) ¡ Case ¡iii) ¡ ¡A ¡ X ¡ Max ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-23
SLIDE 23

LCS ¡ ¡– ¡ – ¡Overlapping ¡ ¡Substructure ¡ ¡Property ¡ ¡

  • A ¡recursive ¡solu*on ¡contains ¡a ¡“small” ¡number ¡of ¡dis*nct ¡sub-­‑

problems ¡repeated ¡many ¡*mes. ¡ ¡ ¡If ¡the ¡lengths ¡of ¡strings ¡X ¡= ¡3 ¡and ¡Y ¡= ¡4 ¡then ¡we ¡get ¡the ¡following ¡ recursion ¡tree. ¡

¡

¡ ¡3, ¡4 ¡ ¡2, ¡4 ¡ ¡3, ¡3 ¡ ¡1, ¡4 ¡ ¡2, ¡3 ¡ ¡3, ¡2 ¡ ¡2, ¡3 ¡ ¡0, ¡4 ¡ ¡1, ¡3 ¡ ¡1, ¡3 ¡ ¡2, ¡2 ¡ ¡2, ¡2 ¡ ¡3, ¡1 ¡ ¡1, ¡3 ¡ ¡2, ¡2 ¡

It ¡can ¡be ¡seen ¡from ¡the ¡recursion ¡tree ¡that ¡sub-­‑problems ¡are ¡repeatedly ¡solved. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-24
SLIDE 24

Longest ¡ ¡Commo mmon ¡ ¡Subsequences ¡ ¡-­‑

  • ­‑ ¡

¡Recursive

int ¡ ¡lcs( ¡String ¡X, ¡String ¡Y, ¡int ¡m, ¡int ¡n ¡) ¡ { ¡ ¡ ¡ ¡if ¡(m ¡== ¡0 ¡|| ¡n ¡== ¡0) ¡ ¡ ¡ ¡ ¡ ¡return ¡0; ¡ ¡ ¡ ¡if ¡(X[m] ¡== ¡Y[n]) ¡ ¡ ¡ ¡ ¡ ¡return ¡1 ¡+ ¡lcs(X, ¡Y, ¡m-­‑1, ¡n-­‑1); ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡return ¡max(lcs(X, ¡Y, ¡m, ¡n-­‑1), ¡lcs(X, ¡Y, ¡m-­‑1, ¡n)); ¡ } ¡ ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-25
SLIDE 25

Longest ¡ ¡Commo mmon ¡ ¡Subsequences ¡ ¡-­‑

  • ­‑ ¡

¡Memo moized

int ¡ ¡lcs( ¡String ¡X, ¡String ¡Y, ¡int ¡m, ¡int ¡n ¡) ¡ { ¡ ¡ ¡ ¡if ¡(m ¡== ¡0 ¡|| ¡n ¡== ¡0) ¡ ¡ ¡ ¡ ¡ ¡return ¡0; ¡ ¡ ¡ ¡if ¡(arr[n][m] ¡!= ¡NULL) ¡return ¡arr[n][m]; ¡ ¡ ¡ ¡if ¡(X[m] ¡== ¡Y[n]) ¡ ¡ ¡ ¡ ¡ ¡ ¡result ¡=1 ¡+ ¡lcs(X, ¡Y, ¡m-­‑1, ¡n-­‑1); ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡result ¡= ¡max(lcs(X, ¡Y, ¡m, ¡n-­‑1), ¡lcs(X, ¡Y, ¡m-­‑1, ¡n)); ¡ ¡ ¡ ¡ ¡arr[n][m] ¡= ¡result; ¡ ¡ ¡ } ¡

¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-26
SLIDE 26

Longest ¡ ¡Commo mmon ¡ ¡Subsequences

LCS(a, ¡b, ¡c) ¡{ ¡ ¡ ¡m ¡:= ¡length[a] ¡ ¡ ¡n ¡:= ¡length[b] ¡ ¡ ¡for ¡i ¡:= ¡0 ¡to ¡m ¡ ¡ ¡ ¡ ¡ ¡ ¡C[i,0] ¡= ¡0 ¡ ¡ ¡ ¡for ¡j ¡:= ¡0 ¡to ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡C[0,j] ¡= ¡0 ¡ ¡ ¡ ¡for ¡i ¡:= ¡1 ¡to ¡m ¡ ¡ ¡ ¡for ¡j ¡:= ¡1 ¡to ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡X[i] ¡= ¡Y[j] ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡C[i,j] ¡:= ¡C[i-­‑1,j-­‑1] ¡+ ¡1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡C[i,j] ¡:= ¡max(C[i,j-­‑1], ¡C[i-­‑1,j]) ¡ ¡ ¡return ¡C[m,n] ¡ ¡ ¡ ¡ ¡ ¡} ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-27
SLIDE 27

Longest ¡ ¡Commo mmon ¡ ¡Subsequences

  • The dynamic programming approach for solving the LCS problem:
  • Time complexity: O(mn)

L1,1 L2,1 L3,1 L1,2 L1,3 L2,2 Lm,n

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-28
SLIDE 28

Longest ¡ ¡Commo mmon ¡ ¡Subsequences

  • Find ¡the ¡longest ¡common ¡subsequence ¡for ¡

¡ ¡ ¡ ¡X ¡= ¡ ¡A ¡B ¡C ¡B ¡D ¡A ¡B ¡ ¡ ¡ ¡ ¡Y ¡ ¡= ¡B ¡D ¡C ¡A ¡B ¡A ¡

j ¡ 0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ i ¡ yi ¡ B ¡ D ¡ C ¡ A ¡ B ¡ A ¡ 0 ¡ xi ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 1 ¡ A ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 1 ¡ 1 ¡ 1 ¡ 2 ¡ B ¡ 0 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 2 ¡ 2 ¡ 3 ¡ C ¡ 0 ¡ 1 ¡ 1 ¡ 2 ¡ 2 ¡ 2 ¡ 2 ¡ 4 ¡ B ¡ 0 ¡ 1 ¡ 1 ¡ 2 ¡ 2 ¡ 3 ¡ 3 ¡ 5 ¡ D ¡ 0 ¡ 1 ¡ 2 ¡ 2 ¡ 2 ¡ 3 ¡ 3 ¡ 6 ¡ A ¡ 0 ¡ 1 ¡ 2 ¡ 2 ¡ 3 ¡ 3 ¡ 4 ¡ 7 ¡ B ¡ 0 ¡ 1 ¡ 2 ¡ 2 ¡ 3 ¡ 4 ¡ 4 ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-29
SLIDE 29

Longest ¡ ¡Commo mmon ¡ ¡Subsequences

  • Find ¡the ¡longest ¡common ¡subsequence ¡for ¡

¡ ¡ ¡ ¡X ¡= ¡ ¡A ¡B ¡C ¡B ¡D ¡A ¡B ¡ ¡ ¡ ¡ ¡Y ¡ ¡= ¡B ¡D ¡C ¡A ¡B ¡A ¡

j ¡ 0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ i ¡ yi ¡ B ¡ D ¡ C ¡ A ¡ B ¡ A ¡ 0 ¡ xi ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 1 ¡ A ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 1 ¡ 1 ¡ 1 ¡ 2 ¡ B ¡ 0 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 2 ¡ 2 ¡ 3 ¡ C ¡ 0 ¡ 1 ¡ 1 ¡ 2 ¡ 2 ¡ 2 ¡ 2 ¡ 4 ¡ B ¡ 0 ¡ 1 ¡ 1 ¡ 2 ¡ 2 ¡ 3 ¡ 3 ¡ 5 ¡ D ¡ 0 ¡ 1 ¡ 2 ¡ 2 ¡ 2 ¡ 3 ¡ 3 ¡ 6 ¡ A ¡ 0 ¡ 1 ¡ 2 ¡ 2 ¡ 3 ¡ 3 ¡ 4 ¡ 7 ¡ B ¡ 0 ¡ 1 ¡ 2 ¡ 2 ¡ 3 ¡ 4 ¡ 4 ¡ j ¡ 0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡ 5 ¡ 6 ¡ i ¡ yi ¡ B ¡ D ¡ C ¡ A ¡ B ¡ A ¡ 0 ¡ xi ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 1 ¡ A ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 1 ¡ 1 ¡ 1 ¡ 2 ¡ B ¡ 0 ¡ 1 ¡ ¡1 ¡ 1 ¡ 1 ¡ 2 ¡ 2 ¡ 3 ¡ C ¡ 0 ¡ 1 ¡ 1 ¡ 2 ¡ 2 ¡ 2 ¡ 2 ¡ 4 ¡ B ¡ 0 ¡ 1 ¡ 1 ¡ 2 ¡ 2 ¡ 3 ¡ 3 ¡ 5 ¡ D ¡ 0 ¡ 1 ¡ 2 ¡ 2 ¡ 2 ¡ 3 ¡ 3 ¡ 6 ¡ A ¡ 0 ¡ 1 ¡ 2 ¡ 2 ¡ 3 ¡ 3 ¡ 4 ¡ 7 ¡ B ¡ 0 ¡ 1 ¡ 2 ¡ 2 ¡ 3 ¡ 4 ¡ 4 ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-30
SLIDE 30

Longest ¡ ¡Commo mmon ¡ ¡Subsequences

LCS_print ¡(a,m,n,C) ¡{ ¡ ¡if ¡(C[m.n] ¡== ¡0) ¡ ¡ ¡ ¡ ¡ ¡return ¡ ¡ ¡ ¡if ¡(C[m,n] ¡== ¡C[m-­‑1,n]) ¡ ¡ ¡ ¡ ¡LCS_print ¡(a, ¡m-­‑1,n,C) ¡ ¡ ¡else ¡if ¡(C[m,n] ¡== ¡C[m,n-­‑1]) ¡ ¡ ¡ ¡ ¡LCS_print ¡(a, ¡m,n-­‑1,C) ¡ ¡ ¡else ¡ ¡{ ¡ ¡ ¡ ¡LCS_print(a, ¡m-­‑1, ¡n-­‑1, ¡C) ¡ ¡ ¡ ¡ ¡Print(a[m]) ¡ } ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-31
SLIDE 31

Longest ¡ ¡Commo mmon ¡ ¡Subsequences

Find the longest common subsequence for

A = b a c a d, B = a c c b a d c b

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡