CS CS 466 466 In Introduct ctio ion t to B Bio ioin - - PowerPoint PPT Presentation

โ–ถ
cs cs 466 466 in introduct ctio ion t to b bio ioin
SMART_READER_LITE
LIVE PREVIEW

CS CS 466 466 In Introduct ctio ion t to B Bio ioin - - PowerPoint PPT Presentation

CS CS 466 466 In Introduct ctio ion t to B Bio ioin informatics ics Lecture 2 Part 1 Mohammed El-Kebir January 28, 2020 Outline 1. Change problem 2. Review of running time analysis 3. Edit distance 4. Review elementary graph


slide-1
SLIDE 1

CS CS 466 466 In Introduct ctio ion t to B Bio ioin informatics ics

Lecture 2 Part 1

Mohammed El-Kebir January 28, 2020

slide-2
SLIDE 2

Outline

  • 1. Change problem
  • 2. Review of running time analysis
  • 3. Edit distance
  • 4. Review elementary graph theory
  • 5. Manhattan Tourist problem
  • 6. Longest/shortest paths in DAGs

Reading:

  • Jones and Pevzner. Chapters 2.7-2.9 and 6.1-6.4
  • Lecture notes

2

slide-3
SLIDE 3

The Change Problem

3

  • Suppose we have ๐‘œ = 3 coins:
  • What is the minimum number of coins needed to make change for ๐‘ = 9

cents?

  • Answer: ๐‘’', โ€ฆ , ๐‘’* = (1, 0, 2) thus 1 + 0 + 2 = 3 coins.

1 cent 7 cent 3 cent

๐ = ( ) , ,

Change Problem: Given amount ๐‘ โˆˆ โ„• โˆ– {0} and coins ๐ = ๐‘‘', โ€ฆ , ๐‘‘* โˆˆ โ„•* s.t. ๐‘‘* = 1 and ๐‘‘8 โ‰ฅ ๐‘‘8:' for all ๐‘— โˆˆ ๐‘œ โˆ’ 1 = {1, โ€ฆ , ๐‘œ โˆ’ 1}, find ๐ž = ๐‘’', โ€ฆ , ๐‘’* โˆˆ โ„•* s.t. (i) ๐‘ = โˆ‘8?'

*

๐‘‘8๐‘’8 and (ii) โˆ‘8?'

*

๐‘’8 is minimum

slide-4
SLIDE 4

The Change Problem โ€“ Four Algorithms

4

GreedyChange(๐‘, ๐‘‘', โ€ฆ , ๐‘‘*) 1. for ๐‘— รŸ 1 to ๐‘œ 2. ๐‘’8 รŸ 3. ๐‘ รŸ ๐‘ โˆ’ ๐‘’8๐‘‘8

bM/cic

ExhaustiveChange(๐‘, ๐‘‘', โ€ฆ , ๐‘‘*) 1. for 2. if 3. return

(d1, . . . , dn) 2 [bM/c1c] โ‡ฅ . . . โ‡ฅ [bM/cnc] Pn

i=1 cidi = M

(d1, . . . , dn)

RecursiveChange(๐‘, ๐‘‘', โ€ฆ , ๐‘‘*)

  • 1. if ๐‘ = 0

2. return 0

  • 3. bestNumCoins รŸ โˆž
  • 4. for ๐‘— รŸ 1 to ๐‘œ

5. if ๐‘ โ‰ฅ ๐‘‘8 6. numCoins รŸ RecursiveChange(๐‘ โˆ’ ๐‘‘8, ๐‘‘', โ€ฆ , ๐‘‘*) 7. if numCoins + 1 < bestNumCoins 8. bestNumCoins รŸ numCoins + 1

  • 9. return bestNumCoins

DPChange(๐‘, ๐‘‘', โ€ฆ , ๐‘‘*)

  • 1. for ๐‘› รŸ 1 to ๐‘

2. minNumCoins[๐‘›] รŸ โˆž

  • 3. for ๐‘— รŸ 1 to ๐‘œ

4. minNumCoins[๐‘‘8] รŸ 1

  • 5. for ๐‘› รŸ 1 to ๐‘

6. for ๐‘— รŸ 1 to ๐‘œ 7. if ๐‘› > ๐‘‘8 8. minNumCoins[๐‘›] รŸ min(1 + minNumCoins[๐‘› โˆ’ ๐‘‘8], minNumCoins[๐‘›])

  • 9. return minNumCoins[M]
slide-5
SLIDE 5

Four Different Algorithms

5

Technique Correct? Efficient? Greedy algorithm [GreedyChange] no yes Exhaustive enumeration [ExhaustiveChange] yes no Recursive algorithm [RecursiveChange] yes no Dynamic programming [DPChange] yes yes

Question: How to assess efficiency?

slide-6
SLIDE 6

Running Time Analysis

  • The running time of an algorithm ๐ต for problem ฮ  is the maximum number
  • f steps that ๐ต will take on any instance of size ๐‘œ = |๐‘Œ|
  • Asymptotic running time ignores constant factors using Big O notation

6

f(n) g(n)

๐‘”(๐‘œ) is ๐‘ƒ(๐‘• ๐‘œ ) provided there exists ๐‘‘ > 0 and ๐‘œJ โ‰ฅ 0 such that ๐‘” ๐‘œ โ‰ค ๐‘‘ ๐‘•(๐‘œ) for all ๐‘œ โ‰ฅ ๐‘œJ

slide-7
SLIDE 7

Running Time Analysis โ€“ Example

7

๐‘” ๐‘œ = 10000 + 500๐‘œM ๐‘• ๐‘œ = ๐‘œN/2

๐‘”(๐‘œ) is ๐‘ƒ(๐‘• ๐‘œ ) provided there exists ๐‘‘ > 0 and ๐‘œJ โ‰ฅ 0 such that ๐‘” ๐‘œ โ‰ค ๐‘‘ ๐‘•(๐‘œ) for all ๐‘œ โ‰ฅ ๐‘œJ Pick ๐‘‘ = 1000 and ๐‘œJ = 3. Then, ๐‘”(๐‘œ) โ‰ค ๐‘‘๐‘•(๐‘œ) for all ๐‘œ โ‰ฅ ๐‘œJ.

๐‘” ๐‘œ 1000 ๐‘• ๐‘œ

slide-8
SLIDE 8

The Change Problem โ€“ Running Time Analysis

8

GreedyChange(๐‘, ๐‘‘', โ€ฆ , ๐‘‘*) 1. for ๐‘— รŸ 1 to ๐‘œ 2. ๐‘’8 รŸ 3. ๐‘ รŸ ๐‘ โˆ’ ๐‘’8๐‘‘8

bM/cic

DPChange(๐‘, ๐‘‘', โ€ฆ , ๐‘‘*)

  • 1. for ๐‘› รŸ 1 to ๐‘

2. minNumCoins[๐‘›] รŸ โˆž

  • 3. for ๐‘— รŸ 1 to ๐‘œ

4. minNumCoins[๐‘‘8] รŸ 1

  • 5. for ๐‘› รŸ 1 to ๐‘

6. for ๐‘— รŸ 1 to ๐‘œ 7. if ๐‘› > ๐‘‘8 8. minNumCoins[๐‘›] รŸ min(1 + minNumCoins[๐‘› โˆ’ ๐‘‘8], minNumCoins[๐‘›])

  • 9. return minNumCoins[M]

Number of operations:

  • Line 2: 3 = ๐‘ƒ(1)
  • Line 3: 3 = ๐‘ƒ(1)
  • Total: 6๐‘œ = ๐‘ƒ(๐‘œ)

Number of operations:

  • Lines 1-2: ๐‘ƒ(๐‘)
  • Lines 3-4: ๐‘ƒ(๐‘œ)
  • Lines 5-8: ๐‘ƒ(๐‘๐‘œ)
  • Total: ๐‘ƒ(๐‘) + ๐‘ƒ(๐‘œ) + ๐‘ƒ(๐‘๐‘œ) =

๐‘ƒ(๐‘๐‘œ)

slide-9
SLIDE 9

Running Time Analysis โ€“ Guidelines

9

  • ๐‘ƒ(๐‘œQ) โŠ‚ ๐‘ƒ(๐‘œS) for any positive constants ๐‘ < ๐‘
  • For any constants ๐‘, ๐‘ > 0 and ๐‘‘ > 1,

๐‘ƒ(๐‘) โŠ‚ ๐‘ƒ(log ๐‘œ) โŠ‚ ๐‘ƒ(๐‘œS) โŠ‚ ๐‘ƒ(๐‘‘*)

  • We can multiply to learn about other functions. For any constants ๐‘, ๐‘ > 0 and ๐‘‘ > 1,

๐‘ƒ ๐‘๐‘œ = ๐‘ƒ(๐‘œ) โŠ‚ ๐‘ƒ(๐‘œ log ๐‘œ) โŠ‚ ๐‘ƒ ๐‘œ ๐‘œS = ๐‘ƒ(๐‘œS:') โŠ‚ ๐‘ƒ(๐‘œ๐‘‘*)

  • Base of the logarithm is a constant and can be ignored. For any constants ๐‘, ๐‘ > 1,

๐‘ƒ logQ ๐‘œ = ๐‘ƒ(logS ๐‘œ/ logS ๐‘) = ๐‘ƒ(1/(logS ๐‘) logS ๐‘œ) = ๐‘ƒ(logS ๐‘œ)

slide-10
SLIDE 10

Running Time Analysis โ€“ Guidelines

10

Big Oh Name ๐‘ƒ(1) Constant ๐‘ƒ(log ๐‘œ) Logarithmic ๐‘ƒ(๐‘œ) Linear ๐‘ƒ(๐‘œZ) Quadratic ๐‘ƒ ๐‘œ[ = ๐‘ƒ(poly ๐‘œ ) Polynomial ๐‘ƒ(2^_`a(*)) Exponential

  • ๐‘ƒ(๐‘œQ) โŠ‚ ๐‘ƒ(๐‘œS) for any positive constants ๐‘ < ๐‘
  • For any constants ๐‘, ๐‘ > 0 and ๐‘‘ > 1,

๐‘ƒ(๐‘) โŠ‚ ๐‘ƒ(log ๐‘œ) โŠ‚ ๐‘ƒ(๐‘œS) โŠ‚ ๐‘ƒ(๐‘‘*)

  • We can multiply to learn about other functions. For any constants ๐‘, ๐‘ > 0 and ๐‘‘ > 1,

๐‘ƒ ๐‘๐‘œ = ๐‘ƒ(๐‘œ) โŠ‚ ๐‘ƒ(๐‘œ log ๐‘œ) โŠ‚ ๐‘ƒ ๐‘œ ๐‘œS = ๐‘ƒ(๐‘œS:') โŠ‚ ๐‘ƒ(๐‘œ๐‘‘*)

  • Base of the logarithm is a constant and can be ignored. For any constants ๐‘, ๐‘ > 1,

๐‘ƒ logQ ๐‘œ = ๐‘ƒ(logS ๐‘œ/ logS ๐‘) = ๐‘ƒ(1/(logS ๐‘) logS ๐‘œ) = ๐‘ƒ(logS ๐‘œ)

slide-11
SLIDE 11

Running Time Analysis โ€“ More Examples

11

Question: What is ๐‘ƒ

* b

?

slide-12
SLIDE 12

Running Time Analysis โ€“ More Examples

  • For constant ๐‘™ > 0 it holds that *

b = O(๐‘œb)

  • Recall that ๐‘œ! = โˆ8?'

*

๐‘—

12

Question: What is ๐‘ƒ ๐‘œ! ? Question: What is ๐‘ƒ

* b

?

slide-13
SLIDE 13

Running Time Analysis โ€“ More Examples

  • For constant ๐‘™ > 0 it holds that *

b = O(๐‘œb)

  • Recall that ๐‘œ! = โˆ8?'

*

๐‘—

13

Stirlingโ€™s approximation: ๐‘œ! โ‰ˆ 2๐œŒ๐‘œ

* i *

= 2๐œŒ

* ij^ * ๐‘œ* = ๐‘ƒ ๐‘œ* = ๐‘ƒ(2* `_k *)

(*) : ๐‘œ / exp ๐‘œ < 1 for all ๐‘œ > 0

(*)

Question: What is ๐‘ƒ ๐‘œ! ? Question: What is ๐‘ƒ

* b

? Question: Is ๐‘œ* = ๐‘ƒ ๐‘œ! ?

slide-14
SLIDE 14

Running Time Analysis โ€“ More Examples

  • For constant ๐‘™ > 0 it holds that *

b = O(๐‘œb)

  • Recall that ๐‘œ! = โˆ8?'

*

๐‘—

14

Stirlingโ€™s approximation: ๐‘œ! โ‰ˆ 2๐œŒ๐‘œ

* i *

= 2๐œŒ

* ij^ * ๐‘œ* = ๐‘ƒ ๐‘œ* = ๐‘ƒ(2* `_k *)

(*) : ๐‘œ / exp ๐‘œ < 1 for all ๐‘œ > 0

(*)

Question: What is ๐‘ƒ ๐‘œ! ? Question: What is ๐‘ƒ

* b

? Question: Is ๐‘œ* = ๐‘ƒ ๐‘œ! ? Question: What is ๐‘ƒ log(๐‘œ!) ?

slide-15
SLIDE 15

Course Topic #1: Sequence Alignment

15

Question: How do we align sequences to identify similarities/differences?

โ€œThus, although the FOXP2 protein is extremely conserved among mammals, it acquired two amino-acid changes on the human lineage, at least one of which may have functional consequences. This is an intriguing finding, because FOXP2 is the first gene known to be involved in the development of speech and language.โ€

Nature (2002)

slide-16
SLIDE 16

Alignment

16

An alignment between two strings v (of m characters) and w (of n characters) is a two row matrix where the first row contains the characters of v in order, the second row contains the characters of w in order, and spaces may be interspersed throughout each. v: KITTEN

(m = 6)

w: SITTING

(n = 7)

Input Output

K

  • I

T T E N

  • S

I

  • T

T I N G v: w: Question: Is this a good alignment? Answer: Count the number of insertion, deletions, substitutions.

slide-17
SLIDE 17

Alignment

17

An alignment between two strings v (of m characters) and w (of n characters) is a two row matrix where the first row contains the characters of v in order, the second row contains the characters of w in order, and spaces may be interspersed throughout each. v: KITTEN

(m = 6)

w: SITTING

(n = 7)

Input Output

K

  • I

T T E N

  • S

I

  • T

T I N G v: w: Question: Is this a good alignment? Answer: Count the number of insertion, deletions, substitutions.

slide-18
SLIDE 18

Edit Distance [Levenshtein, 1966]

18

Edit Distance problem: Given strings ๐ฐ โˆˆ ฮฃp and ๐ฑ โˆˆ ฮฃ*, compute the minimum number ๐‘’(๐ฐ, ๐ฑ) of elementary operations to transform ๐ฐ into ๐ฑ. Elementary operations: insertion, deletions and substitutions of single characters

๐‘’ ๐๐›๐ฎ, ๐๐›๐ฌ = 1 ๐‘’ ๐๐›๐ฎ, ๐›๐ฎ๐Ÿ = 2 ๐‘’ ๐๐›๐ฎ, ๐›๐ฌ๐Ÿ = 3

slide-19
SLIDE 19

Computing Edit Distance

19

v: ATGTTAT... w: AGCGTAC... Edit Distance problem: Given strings ๐ฐ โˆˆ ฮฃp and ๐ฑ โˆˆ ฮฃ*, compute the minimum number ๐‘’(๐ฐ, ๐ฑ) of elementary operations to transform ๐ฐ into ๐ฑ.

match mismatch

A T

  • G

T T T A G C G T

  • C

๐ฐ8: ๐ฑ

v:

Optimal substructure: Edit distance obtained from edit distance of prefix of string. ๐‘— ๐‘˜ ๐‘˜ โˆ’ 1 ๐‘— โˆ’ 1

prefix of ๐ฐ of length ๐‘— prefix of ๐ฑ of length ๐‘˜ insertion deletion

slide-20
SLIDE 20

Computing Edit Distance โ€“ Optimal Substructure

20

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

Insertion: ๐‘’ ๐‘—, ๐‘˜ = ๐‘’ ๐‘—, ๐‘˜ โˆ’ 1 + 1 Extend by a character in ๐ฑ Match: ๐‘’ ๐‘—, ๐‘˜ = ๐‘’ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 Extend by a character in ๐ฐ and ๐ฑ โ€ฆ ๐ฐ8 โ€ฆ

  • Deletion: ๐‘’ ๐‘—, ๐‘˜ = ๐‘’ ๐‘— โˆ’ 1, ๐‘˜ + 1

Extend by a character in ๐ฐ Mismatch: ๐‘’ ๐‘—, ๐‘˜ = ๐‘’ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 + 1 Extend by a character in ๐ฐ and ๐ฑ

๐‘’[๐‘—, ๐‘˜] is the edit distance of ๐ฐ8 and ๐ฑ

v,

where ๐ฐ8 is prefix of ๐ฐ of length ๐‘— and ๐ฑ

v is prefix of ๐ฑ of length ๐‘˜

slide-21
SLIDE 21

Computing Edit Distance โ€“ Recurrence

21

๐‘’[๐‘—, ๐‘˜] is the edit distance of ๐ฐ8 and ๐ฑ

v,

where ๐ฐ8 is prefix of ๐ฐ of length ๐‘— and ๐ฑ

v is prefix of ๐ฑ of length ๐‘˜

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃณ d[i 1, j] + 1, d[i, j 1] + 1, d[i 1, j 1] + 1, if vi 6= wj, d[i 1, j 1], if vi = wj.

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

slide-22
SLIDE 22

Computing Edit Distance โ€“ Recurrence

22

๐‘’[๐‘—, ๐‘˜] is the edit distance of ๐ฐ8 and ๐ฑ

v,

where ๐ฐ8 is prefix of ๐ฐ of length ๐‘— and ๐ฑ

v is prefix of ๐ฑ of length ๐‘˜

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

slide-23
SLIDE 23

Computing Edit Distance โ€“ Dynamic Programming

23

1 2 3 4 1 2 3 4

W A T C G A T G T V

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

match mismatch insertion deletion

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

slide-24
SLIDE 24

Computing Edit Distance โ€“ Dynamic Programming

24

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

1 2 3 4 1 2 3 4

W A T C G A T G T V

๐‘—, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 ๐‘—, ๐‘˜ โˆ’ 1

match mismatch insertion deletion 1 1 0 or 1

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

slide-25
SLIDE 25

Computing Edit Distance โ€“ Dynamic Programming

25

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

1 2 3 4 1 2 3 4 1 1 2 2 3 3 4 4

W A T C G A T G T V

๐‘—, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 ๐‘—, ๐‘˜ โˆ’ 1

1 1 0 or 1 match mismatch insertion deletion

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

slide-26
SLIDE 26

Computing Edit Distance โ€“ Dynamic Programming

26

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

1 2 3 4 1 2 3 4 1 1 ? 2 2 3 3 4 4

W A T C G A T G T V

๐‘—, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 ๐‘—, ๐‘˜ โˆ’ 1

1 1 0 or 1 match mismatch insertion deletion

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

slide-27
SLIDE 27

Computing Edit Distance โ€“ Dynamic Programming

27

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

1 2 3 4 1 2 3 4 1 1 2 2 3 3 4 4

W A T C G A T G T V

๐‘—, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 ๐‘—, ๐‘˜ โˆ’ 1

1 1 0 or 1 match mismatch insertion deletion

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

slide-28
SLIDE 28

Computing Edit Distance โ€“ Dynamic Programming

28

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

1 2 3 4 1 2 3 4 1 1 2 2 ? 3 3 4 4

W A T C G A T G T V

๐‘—, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 ๐‘—, ๐‘˜ โˆ’ 1

1 1 0 or 1 match mismatch insertion deletion

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

slide-29
SLIDE 29

Computing Edit Distance โ€“ Dynamic Programming

29

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

1 2 3 4 1 2 3 4 1 1 2 2 1 3 3 4 4

W A T C G A T G T V

๐‘—, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 ๐‘—, ๐‘˜ โˆ’ 1

1 1 0 or 1 match mismatch insertion deletion

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

slide-30
SLIDE 30

Computing Edit Distance โ€“ Dynamic Programming

30

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

1 2 3 4 1 2 3 4 1 1 2 2 3 3 4 4

W A T C G A T G T V

๐‘—, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 ๐‘—, ๐‘˜ โˆ’ 1

1 1 0 or 1 match mismatch insertion deletion

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

slide-31
SLIDE 31

Computing Edit Distance โ€“ Dynamic Programming

31

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

1 2 3 4 1 2 3 4 1 1 2 2 3 3 4 4

W A T C G A T G T V

๐‘—, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 ๐‘—, ๐‘˜ โˆ’ 1

1 1 0 or 1 match mismatch insertion deletion

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

slide-32
SLIDE 32

Computing Edit Distance โ€“ Dynamic Programming

32

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

1 2 3 4 1 2 3 4 1 1 2 2 3 3 4 4

W A T C G A T G T V

๐‘—, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 ๐‘—, ๐‘˜ โˆ’ 1

1 1 0 or 1 match mismatch insertion deletion

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

slide-33
SLIDE 33

Computing Edit Distance โ€“ Dynamic Programming

33

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

1 2 3 4 1 2 3 4 1 1 1 2 3 2 2 1 1 2 3 3 2 1 1 1 4 4 3 2 2 2

W A T C G A T G T V

๐‘—, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 ๐‘—, ๐‘˜ โˆ’ 1

1 1 0 or 1 match mismatch insertion deletion

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

slide-34
SLIDE 34

Computing Edit Distance โ€“ Dynamic Programming

34

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

1 2 3 4 1 2 3 4 1 1 1 2 3 2 2 1 1 2 3 3 2 1 1 1 4 4 3 2 2 2

W A T C G A T G T V

๐‘—, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 ๐‘—, ๐‘˜ โˆ’ 1

1 1 0 or 1 match mismatch insertion deletion

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

slide-35
SLIDE 35

Computing Edit Distance โ€“ Dynamic Programming

35

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

1 2 3 4 1 2 3 4 1 1 1 2 3 2 2 1 1 2 3 3 2 1 1 1 4 4 3 2 2 2

W A T C G A T G T V

match mismatch insertion deletion

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

A T

  • G

T A T C G

  • A

T G T A T C G

slide-36
SLIDE 36

Computing Edit Distance โ€“ Running Time

36

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

1 2 3 4 1 2 3 4 1 1 1 2 3 2 2 1 1 2 3 3 2 1 1 1 4 4 3 2 2 2

W A T C G A T G T V

match mismatch insertion deletion

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

๐‘—, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 ๐‘—, ๐‘˜ โˆ’ 1

1 1 0 or 1

For each ๐‘› + 1 ร— (๐‘œ + 1) entry:

  • 3 addition operations
  • 1 comparison operation
  • 1 minimum operation

Running time: ๐‘ƒ ๐‘›๐‘œ time

slide-37
SLIDE 37

Computing Edit Distance โ€“ Running Time

37

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

1 2 3 4 1 2 3 4 1 1 1 2 3 2 2 1 1 2 3 3 2 1 1 1 4 4 3 2 2 2

W A T C G A T G T V

match mismatch insertion deletion

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

๐‘—, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 ๐‘—, ๐‘˜ โˆ’ 1

1 1 0 or 1

For each ๐‘› + 1 ร— (๐‘œ + 1) entry:

  • 3 addition operations
  • 1 comparison operation
  • 1 minimum operation

Running time: ๐‘ƒ ๐‘›๐‘œ time

slide-38
SLIDE 38

Computing Edit Distance โ€“ Your turn!

38

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, d[i 1, j] + 1, if i > 0, d[i, j 1] + 1, if j > 0, d[i 1, j 1] + 1, if i > 0, j > 0 and vi 6= wj, d[i 1, j 1], if i > 0, j > 0 and vi = wj.

1 2 3 1 2 3

W C A R C A T V

match mismatch insertion deletion

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • โ€ฆ

๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

๐‘—, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ ๐‘— โˆ’ 1, ๐‘˜ โˆ’ 1 ๐‘—, ๐‘˜ โˆ’ 1

1 1 0 or 1

1 2 3 1 2 3

W A T E C A T V

1 2 3 1 2 3

W A R E C A T V

๐‘’ ๐๐›๐ฎ, ๐๐›๐ฌ = ๐‘’ ๐๐›๐ฎ, ๐›๐ฎ๐Ÿ = ๐‘’ ๐๐›๐ฎ, ๐›๐ฌ๐Ÿ =

slide-39
SLIDE 39

Change Problem and Edit distance

40

1 2 3 4 1 2 3 4 1 1 1 2 3 2 2 1 1 2 3 3 2 1 1 1 4 4 3 2 2 2

W A T C G A T G T V

Value 1 2 3 4 5 6 7 Min # coins 1 2 1 2 1 2 3

Make M cents using minimum number of 1, 3 and 5 cent coins.

  • Both have optimal substructure and can be solved using dynamic programming
  • These are examples of a more general problem!
slide-40
SLIDE 40

Review of Graph Theory

  • Graph ๐ป = (๐‘Š, ๐น)
  • Vertices ๐‘Š = {๐‘ค', โ€ฆ , ๐‘ค*}
  • Edges ๐น = {(๐‘ค8, ๐‘คv), โ€ฆ }

41

Champaign-Urbana Chicago Indianapolis

  • St. Louis

Bloomington

slide-41
SLIDE 41

Review of Graph Theory

  • Directed Graph ๐ป = (๐‘Š, ๐น)
  • Vertices ๐‘Š = {๐‘ค', โ€ฆ , ๐‘ค*}
  • Directed edges ๐น = {(๐‘ค8, ๐‘คv), โ€ฆ }

42

Champaign-Urbana Chicago Indianapolis

  • St. Louis

Bloomington

slide-42
SLIDE 42

Review of Graph Theory

  • Directed Graph ๐ป = (๐‘Š, ๐น)
  • Vertices ๐‘Š = {๐‘ค', โ€ฆ , ๐‘ค*}
  • Directed edges ๐น = {(๐‘ค8, ๐‘คv), โ€ฆ }
  • Path is a sequence of vertices and edges

that connect them

43

Champaign-Urbana Chicago Indianapolis

  • St. Louis

Bloomington

slide-43
SLIDE 43

Review of Graph Theory

  • Directed Graph ๐ป = (๐‘Š, ๐น)
  • Vertices ๐‘Š = {๐‘ค', โ€ฆ , ๐‘ค*}
  • Directed edges ๐น = {(๐‘ค8, ๐‘คv), โ€ฆ }
  • Path is a sequence of vertices and edges

that connect them

  • Edges can be weighted

44

Champaign-Urbana Chicago Indianapolis

  • St. Louis

Bloomington 130 50 140 170 150 180

slide-44
SLIDE 44

Manhattan Tourist Problem

45

End

* * * * * * * * * * *

Begin

*

A tourist in Manhattan wants to visit the maximum number of attractions (*) by traveling on a path (only eastward and southward) from start to end

slide-45
SLIDE 45

Manhattan Tourist Problem

46

End

1 1 1 2 5 1 1 2 1 1 3

Begin

1

A tourist in Manhattan wants to visit the maximum number of attractions (*) by traveling on a path (only eastward and southward) from start to end May be more than 1 attraction on a street. Add weights!

slide-46
SLIDE 46

Manhattan Tourist Problem

47

3 2 4 7 3 3 3 1 3 2 4 4 5 6 4 6 5 5 8 2 2 5

1 2 3 1 2 3

i coordinate

13

begin end

4

3 2 4 1 2 4 3 3 1 1 2 2 2 4

19 9 5 15 23 20 3

4

j coordinate

Manhattan Tourist Problem (MTP): Given a weighted, directed grid graph G with two vertices โ€œbeginโ€ and โ€œendโ€, find the maximum weight path in G from โ€œbeginโ€ to โ€œendโ€.

slide-47
SLIDE 47

Manhattan Tourist Problem โ€“ Exhaustive Algorithm

48

3 2 4 7 3 3 3 1 3 2 4 4 5 6 4 6 5 5 8 2 2 5

1 2 3 1 2 3

i coordinate

13

begin end

4

3 2 4 1 2 4 3 3 1 1 2 2 2 4

19 9 5 15 23 20 3

4

j coordinate

Check all paths Question: How many paths?

slide-48
SLIDE 48

Manhattan Tourist Problem โ€“ Greedy Algorithm

49

1 2 5 2 1 5 2 3 4 5 3 3 5 10 3 5 5 1 2

promising start, but leads to bad choices! begin end

18 22

better path!

slide-49
SLIDE 49

Manhattan Tourist Problem โ€“ Optimal Substructure

50

1 2 5 2 1 5 2 3 4 5 3 3 5 10 3 5 5 1 2

best score to this point begin end

18 20

best score to this point

22

best score to end

slide-50
SLIDE 50

Manhattan Tourist Problem โ€“ Optimal Substructure

51

๐‘ก[๐‘—, ๐‘˜] is the best score for path to coordinate (๐‘—, ๐‘˜)

  • ๐‘ฅ[ ๐‘— โˆ’ 1, ๐‘˜ , (๐‘—, ๐‘˜)] weight of street between ๐‘— โˆ’ 1, ๐‘˜ and ๐‘—, ๐‘˜
  • ๐‘ฅ[ ๐‘—, ๐‘˜ โˆ’ 1 , (๐‘—, ๐‘˜)] weight of street between ๐‘—, ๐‘˜ โˆ’ 1 and ๐‘—, ๐‘˜

1 2 5 2 1 5 2 3 4 5 3 3 5 10 3 5 5 1 2

best score to this point begin end

18 20

best score to this point

22

best score to end

Question: What is the recurrence?

slide-51
SLIDE 51

Manhattan Tourist Problem โ€“ Optimal Substructure

52 1 2 5 2 1 5 2 3 4 5 3 3 5 10 3 5 5 1 2

best score to this point begin end

18 20

best score to this point

22

best score to end

๐‘ก[๐‘—, ๐‘˜] is the best score for path to coordinate (๐‘—, ๐‘˜)

s[i, j] = max ๏ฃฑ ๏ฃด ๏ฃฒ ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, s[i โˆ’ 1, j] + w[(i โˆ’ 1, j), (i, j)] if i > 0, s[i, j โˆ’ 1] + w[(i, j โˆ’ 1), (i, j)] if j > 0.

  • ๐‘ฅ[ ๐‘— โˆ’ 1, ๐‘˜ , (๐‘—, ๐‘˜)] weight of street between ๐‘— โˆ’ 1, ๐‘˜ and ๐‘—, ๐‘˜
  • ๐‘ฅ[ ๐‘—, ๐‘˜ โˆ’ 1 , (๐‘—, ๐‘˜)] weight of street between ๐‘—, ๐‘˜ โˆ’ 1 and ๐‘—, ๐‘˜
slide-52
SLIDE 52

MTP โ€“ Solving Recurrence using Dynamic Programming

53

๐‘ก[๐‘—, ๐‘˜] is the best score for path to coordinate (๐‘—, ๐‘˜)

s[i, j] = max ๏ฃฑ ๏ฃด ๏ฃฒ ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, s[i โˆ’ 1, j] + w[(i โˆ’ 1, j), (i, j)] if i > 0, s[i, j โˆ’ 1] + w[(i, j โˆ’ 1), (i, j)] if j > 0.

  • ๐‘ฅ[ ๐‘— โˆ’ 1, ๐‘˜ , (๐‘—, ๐‘˜)] weight of street between

๐‘— โˆ’ 1, ๐‘˜ and ๐‘—, ๐‘˜

  • ๐‘ฅ[ ๐‘—, ๐‘˜ โˆ’ 1 , (๐‘—, ๐‘˜)] weight of street between

๐‘—, ๐‘˜ โˆ’ 1 and ๐‘—, ๐‘˜

source

slide-53
SLIDE 53

MTP โ€“ Solving Recurrence using Dynamic Programming

54

๐‘ก[๐‘—, ๐‘˜] is the best score for path to coordinate (๐‘—, ๐‘˜)

s[i, j] = max ๏ฃฑ ๏ฃด ๏ฃฒ ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, s[i โˆ’ 1, j] + w[(i โˆ’ 1, j), (i, j)] if i > 0, s[i, j โˆ’ 1] + w[(i, j โˆ’ 1), (i, j)] if j > 0.

  • ๐‘ฅ[ ๐‘— โˆ’ 1, ๐‘˜ , (๐‘—, ๐‘˜)] weight of street between

๐‘— โˆ’ 1, ๐‘˜ and ๐‘—, ๐‘˜

  • ๐‘ฅ[ ๐‘—, ๐‘˜ โˆ’ 1 , (๐‘—, ๐‘˜)] weight of street between

๐‘—, ๐‘˜ โˆ’ 1 and ๐‘—, ๐‘˜

1 5 1 1

i source 1 5 j

slide-54
SLIDE 54

MTP โ€“ Solving Recurrence using Dynamic Programming

55

๐‘ก[๐‘—, ๐‘˜] is the best score for path to coordinate (๐‘—, ๐‘˜)

s[i, j] = max ๏ฃฑ ๏ฃด ๏ฃฒ ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, s[i โˆ’ 1, j] + w[(i โˆ’ 1, j), (i, j)] if i > 0, s[i, j โˆ’ 1] + w[(i, j โˆ’ 1), (i, j)] if j > 0.

  • ๐‘ฅ[ ๐‘— โˆ’ 1, ๐‘˜ , (๐‘—, ๐‘˜)] weight of street between

๐‘— โˆ’ 1, ๐‘˜ and ๐‘—, ๐‘˜

  • ๐‘ฅ[ ๐‘—, ๐‘˜ โˆ’ 1 , (๐‘—, ๐‘˜)] weight of street between

๐‘—, ๐‘˜ โˆ’ 1 and ๐‘—, ๐‘˜

1 2 2 5 3 3 1 2 1 2

i source 1 3 5 8 7 j

slide-55
SLIDE 55

MTP โ€“ Solving Recurrence using Dynamic Programming

56

๐‘ก[๐‘—, ๐‘˜] is the best score for path to coordinate (๐‘—, ๐‘˜)

s[i, j] = max ๏ฃฑ ๏ฃด ๏ฃฒ ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, s[i โˆ’ 1, j] + w[(i โˆ’ 1, j), (i, j)] if i > 0, s[i, j โˆ’ 1] + w[(i, j โˆ’ 1), (i, j)] if j > 0.

  • ๐‘ฅ[ ๐‘— โˆ’ 1, ๐‘˜ , (๐‘—, ๐‘˜)] weight of street between

๐‘— โˆ’ 1, ๐‘˜ and ๐‘—, ๐‘˜

  • ๐‘ฅ[ ๐‘—, ๐‘˜ โˆ’ 1 , (๐‘—, ๐‘˜)] weight of street between

๐‘—, ๐‘˜ โˆ’ 1 and ๐‘—, ๐‘˜

1 2 2 5 3 3 1 2 1 2

i source 1 3 5 8 7 j

5 1 2 5 10 3 3

8 8 12 13

slide-56
SLIDE 56

MTP โ€“ Solving Recurrence using Dynamic Programming

57

๐‘ก[๐‘—, ๐‘˜] is the best score for path to coordinate (๐‘—, ๐‘˜)

s[i, j] = max ๏ฃฑ ๏ฃด ๏ฃฒ ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, s[i โˆ’ 1, j] + w[(i โˆ’ 1, j), (i, j)] if i > 0, s[i, j โˆ’ 1] + w[(i, j โˆ’ 1), (i, j)] if j > 0.

  • ๐‘ฅ[ ๐‘— โˆ’ 1, ๐‘˜ , (๐‘—, ๐‘˜)] weight of street between

๐‘— โˆ’ 1, ๐‘˜ and ๐‘—, ๐‘˜

  • ๐‘ฅ[ ๐‘—, ๐‘˜ โˆ’ 1 , (๐‘—, ๐‘˜)] weight of street between

๐‘—, ๐‘˜ โˆ’ 1 and ๐‘—, ๐‘˜

1 2 2 5 3 3 1 2 1 2

i source 1 3 5 8 7 j

5 1 2 5 10 3 3

8 8 12 13

5 3 3 5

18 16 12

slide-57
SLIDE 57

MTP โ€“ Solving Recurrence using Dynamic Programming

58

๐‘ก[๐‘—, ๐‘˜] is the best score for path to coordinate (๐‘—, ๐‘˜)

s[i, j] = max ๏ฃฑ ๏ฃด ๏ฃฒ ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, s[i โˆ’ 1, j] + w[(i โˆ’ 1, j), (i, j)] if i > 0, s[i, j โˆ’ 1] + w[(i, j โˆ’ 1), (i, j)] if j > 0.

  • ๐‘ฅ[ ๐‘— โˆ’ 1, ๐‘˜ , (๐‘—, ๐‘˜)] weight of street between

๐‘— โˆ’ 1, ๐‘˜ and ๐‘—, ๐‘˜

  • ๐‘ฅ[ ๐‘—, ๐‘˜ โˆ’ 1 , (๐‘—, ๐‘˜)] weight of street between

๐‘—, ๐‘˜ โˆ’ 1 and ๐‘—, ๐‘˜

1 2 2 5 3 3 1 2 1 2

i source 1 3 5 8 7 j

5 1 2 5 10 3 3

8 8 12 13

5 3 3 5

18 16 12

4 5 1

20 21

slide-58
SLIDE 58

MTP โ€“ Solving Recurrence using Dynamic Programming

59

๐‘ก[๐‘—, ๐‘˜] is the best score for path to coordinate (๐‘—, ๐‘˜)

s[i, j] = max ๏ฃฑ ๏ฃด ๏ฃฒ ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, s[i โˆ’ 1, j] + w[(i โˆ’ 1, j), (i, j)] if i > 0, s[i, j โˆ’ 1] + w[(i, j โˆ’ 1), (i, j)] if j > 0.

  • ๐‘ฅ[ ๐‘— โˆ’ 1, ๐‘˜ , (๐‘—, ๐‘˜)] weight of street between

๐‘— โˆ’ 1, ๐‘˜ and ๐‘—, ๐‘˜

  • ๐‘ฅ[ ๐‘—, ๐‘˜ โˆ’ 1 , (๐‘—, ๐‘˜)] weight of street between

๐‘—, ๐‘˜ โˆ’ 1 and ๐‘—, ๐‘˜

1 2 2 5 3 3 1 2 1 2

i source 1 3 5 8 7 j

5 1 2 5 10 3 3

8 8 12 13

5 3 3 5

18 16 12

4 5 1

20 21

2

22

slide-59
SLIDE 59

MTP โ€“ Solving Recurrence using Dynamic Programming

60

๐‘ก[๐‘—, ๐‘˜] is the best score for path to coordinate (๐‘—, ๐‘˜)

s[i, j] = max ๏ฃฑ ๏ฃด ๏ฃฒ ๏ฃด ๏ฃณ 0, if i = 0 and j = 0, s[i โˆ’ 1, j] + w[(i โˆ’ 1, j), (i, j)] if i > 0, s[i, j โˆ’ 1] + w[(i, j โˆ’ 1), (i, j)] if j > 0.

1 2 5 2 1 5 2 3 4 5 3 3 5 10 3 5 5 1 1 2 3 1 2 3

i source 1 3 8 5 8 8 7 12 13 18 16 12 20 21 j

2

22 S3,3 = 22

Let ๐‘› be the number of rows and ๐‘œ be the number of columns. Running time: ๐‘ƒ(๐‘›๐‘œ) Question: Implementation?

  • ๐‘ฅ[ ๐‘— โˆ’ 1, ๐‘˜ , (๐‘—, ๐‘˜)] weight of street between

๐‘— โˆ’ 1, ๐‘˜ and ๐‘—, ๐‘˜

  • ๐‘ฅ[ ๐‘—, ๐‘˜ โˆ’ 1 , (๐‘—, ๐‘˜)] weight of street between

๐‘—, ๐‘˜ โˆ’ 1 and ๐‘—, ๐‘˜

slide-60
SLIDE 60

Manhattan Is Not a Perfect Grid

61

What about diagonals?

B A3 A1 A2

s[B] = max ๏ฃฑ ๏ฃด ๏ฃฒ ๏ฃด ๏ฃณ s[A1] + w[A1, B], s[A2] + w[A2, B], s[A3] + w[A3, B].

slide-61
SLIDE 61

Manhattan Is Not a Perfect Grid, Itโ€™s a Directed Graph

62

s[0, 0] = 0 s[i, j] = max

(i0,j0) 2 pred(i,j){s[i0, j0] + w[(i0, j0), (i, j)]}

๐‘—, ๐‘˜ pred ๐‘—, ๐‘˜

๐ป = (๐‘Š, ๐น) is a directed acyclic graph (DAG) with nonnegative edges weights ๐‘ฅ โˆถ ๐น โ†’ โ„: Each edge is evaluated

  • nce: ๐‘ƒ( ๐น ) time
slide-62
SLIDE 62

Dynamic Programming as a Graph Problem

63

End

* * * * * * * * * * *

Begin

*

Manhattan Tourist Problem: Every path in directed graph is a possible tourist path. Find maximum weight path. Running time: ๐‘ƒ ๐‘›๐‘œ = ๐‘ƒ( ๐น ) Change Problem: Make M cents using minimum number of coins ๐ = 1, 3, 5 . Every path in directed graph is a possible

  • change. Find shortest path.

Running time: ๐‘ƒ ๐‘๐‘œ = ๐‘ƒ( ๐น )

slide-63
SLIDE 63

What About the Edit Distance Problem?

64

1 2 3 4 1 2 3 4

W A T C G A T G T V

Edit Distance problem: Given strings ๐ฐ โˆˆ ฮฃp and ๐ฑ โˆˆ ฮฃ*, compute the minimum number ๐‘’(๐ฐ, ๐ฑ) of elementary operations to transform ๐ฐ into ๐ฑ.

match mismatch insertion deletion

  • ๐ฑ

v

๐ฐ8

  • ๐ฐ8

๐ฑ

v

๐ฐ8 ๐ฑ

v

slide-64
SLIDE 64

What About the Edit Distance Problem?

65

1 2 3 4 O O O O O 1 O O O O O 2 O O O O O 3 O O O O O 4 O O O O O

W A T C G A T G T V

Edit Distance problem: Given strings ๐ฐ โˆˆ ฮฃp and ๐ฑ โˆˆ ฮฃ*, compute the minimum number ๐‘’(๐ฐ, ๐ฑ) of elementary operations to transform ๐ฐ into ๐ฑ.

match mismatch insertion deletion

  • ๐ฑ

v

๐ฐ8

  • ๐ฐ8

๐ฑ

v

๐ฐ8 ๐ฑ

v

Edit graph is a weighed, directed grid graph ๐ป = (๐‘Š, ๐น) with source vertex (0, 0) and target vertex (๐‘›, ๐‘œ). Each edge (๐‘—, ๐‘˜) has weight [๐‘—, ๐‘˜] corresponding to edit cost: deletion (1), insertion (1), mismatch (1) and match (0).

slide-65
SLIDE 65

What About the Edit Distance Problem?

66

1 2 3 4 O O O O O 1 O O O O O 2 O O O O O 3 O O O O O 4 O O O O O

W A T C G A T G T V

match mismatch insertion deletion

  • ๐ฑ

v

๐ฐ8

  • ๐ฐ8

๐ฑ

v

๐ฐ8 ๐ฑ

v

Edit graph is a weighed, directed grid graph ๐ป = (๐‘Š, ๐น) with source vertex (0, 0) and target vertex (๐‘›, ๐‘œ). Each edge (๐‘—, ๐‘˜) has weight [๐‘—, ๐‘˜] corresponding to edit cost: deletion (1), insertion (1), mismatch (1) and match (0). Alignment is a path from (0, 0) to (๐‘›, ๐‘œ)

slide-66
SLIDE 66

What About the Edit Distance Problem?

67

1 2 3 4 O O O O O 1 O O O O O 2 O O O O O 3 O O O O O 4 O O O O O

W A T C G A T G T V

match mismatch insertion deletion

  • ๐ฑ

v

๐ฐ8

  • ๐ฐ8

๐ฑ

v

๐ฐ8 ๐ฑ

v

Edit Distance problem: Given edit graph ๐ป = (๐‘Š, ๐น), with edge weights c โˆถ ๐น โ†’ 0,1 . Find shortest path from (0, 0) to (๐‘›, ๐‘œ).

Edit graph is a weighed, directed grid graph ๐ป = (๐‘Š, ๐น) with source vertex (0, 0) and target vertex (๐‘›, ๐‘œ). Each edge (๐‘—, ๐‘˜) has weight [๐‘—, ๐‘˜] corresponding to edit cost: deletion (1), insertion (1), mismatch (1) and match (0). Alignment is a path from (0, 0) to (๐‘›, ๐‘œ)

slide-67
SLIDE 67

Shortest Path vs Longest Path

  • Change graph, edit graph and the MTP grid are directed graphs G.
  • Change problem and Edit Distance problem are minimization problems.
  • Find shortest path in G from source to sink.
  • Manhattan Tourist problem is a maximization problem.
  • Find longest path in G from source to sink.

68

slide-68
SLIDE 68

Shortest Path vs Longest Path

  • Shortest path in directed graphs can be found efficiently (Dijkstra, Bellman-

Ford, Floyd-Warshall algorithms)

  • Longest path in direct graphs cannot be found efficiently (NP-hard).
  • Change graph, edit graph and MTP grid graph are directed acylic graphs

(DAGs).

  • No directed cycles.
  • Longest path problem in a DAG can

solved efficiently by dynamic programming

69

Question: Whatโ€™s the relation between absence of directed cycles and optimal substructure?

directed cycle

slide-69
SLIDE 69

Weighted Edit Distance

70

๐‘’[๐‘—, ๐‘˜] is the edit distance of ๐ฐ8 and ๐ฑ

v,

where ๐ฐ8 is prefix of ๐ฐ of length ๐‘— and ๐ฑ

v is prefix of ๐ฑ of length ๐‘˜

d[i, j] = min ๏ฃฑ ๏ฃด ๏ฃด ๏ฃด ๏ฃฒ ๏ฃด ๏ฃด ๏ฃด ๏ฃณ d[i 1, j] + 1, d[i, j 1] + 1, d[i 1, j 1] + 1, if vi 6= wj, d[i 1, j 1], if vi = wj.

โ€ฆ

  • โ€ฆ

๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ ๐ฑ

v

โ€ฆ ๐ฐ8 โ€ฆ

  • deletion

insertion mismatch

Replace +1 with different penalties for different types of edits.

slide-70
SLIDE 70

Summary

  • 1. Change problem
  • 2. Review of running time analysis
  • 3. Edit distance
  • 4. Review elementary graph theory
  • 5. Manhattan Tourist problem
  • 6. Longest/shortest paths in DAGs

Reading:

  • Jones and Pevzner. Chapters 2.7-2.9 and 6.1-6.4
  • Lecture notes

71

slide-71
SLIDE 71

Sources

  • CS 362 by Layla Oesper (Carleton College)
  • CS 1810 by Ben Raphael (Brown/Princeton University)
  • An Introduction to Bioinformatics Algorithms book (Jones and Pevzner)
  • http://bioalgorithms.info/

72