0% found this document useful (0 votes)
4 views

Chapter 06_Dynamic Programming

The document discusses dynamic programming as a problem-solving approach, emphasizing its application in breaking down complex problems into smaller components for efficient solutions. It specifically addresses the coin change problem, comparing different methods such as greedy, recursive, and dynamic programming techniques to find the minimum number of coins needed for a given amount. Additionally, it touches on the historical context of dynamic programming and its relevance in fields like genetics, particularly in relation to cystic fibrosis research.

Uploaded by

nigatudebebe3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter 06_Dynamic Programming

The document discusses dynamic programming as a problem-solving approach, emphasizing its application in breaking down complex problems into smaller components for efficient solutions. It specifically addresses the coin change problem, comparing different methods such as greedy, recursive, and dynamic programming techniques to find the minimum number of coins needed for a given amount. Additionally, it touches on the historical context of dynamic programming and its relevance in fields like genetics, particularly in relation to cystic fibrosis research.

Uploaded by

nigatudebebe3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 248

Center of Biomedical Engineering

Data Structures and Algorithms


(BMED – 3112)

Dec, 2024
Divide and Conquer vs Dynamic Programming
Dynamic Programming
Flowchart
Dynamic programming (DP)
• A way of thinking
• Unlike specific coding syntax or design patterns, dynamic
programming isn't a particular algorithm but a way of
thinking. Therefore, the technique takes many forms when it
comes to implementation.
• The main idea of dynamic programming is to consider a
significant problem and break it into smaller, individualized
components.
• When it comes to implementation, optimal techniques rely
on data storage and reuse to increase algorithm efficiency.
Type of data storage that are ready to implement

5
The Fibonacci sequence

• Recursion is a method of solving problems where you solve smaller


portions of the problem until you solve the original, larger problem.
• A method or function is recursive if it can call itself.

public func fibRec(_ n: Int) -> Int {


if n < 2 {
return n
} else {
return fibRec(n-1) + fibRec(n-2)
}
}

6
Dynamic Programming: Change
problem

1 Greedy Change

2 Recursive Change

3 Dynamic Programming
Coin Change Problem
• You are given an array of coins with varying denominations and
an integer sum representing the total amount of money; you must
return the fewest coins required to make up that sum; if that sum
cannot be constructed, return 0.
if the sum is not constructed

the algorithm is always ppich the largest denomination


Change problem
Find the minimum number of coins needed
to make change.
Formally

Change problem
Input: An integer money and positive
integers coin1,...,coind.
Output: The minimum number of coins with
denominations coin1,...,coind
that changes money.
Greedy Way
GreedyChange( money)
Change ← empty c o l l e c t i o n o f c o ins
while money > 0: an array representation
coin ← l a r g e s t denomination
tha t does not exceed
money add coin to Change
money ← money − coin
return Change
Changing Money

in the US

40 cents = 25 + 10 + 5
Greedy
Changing Money

in Tanzania

40 cents = 25 + 10 + 5 = 20 + 20
Greedy is not Optimal
Outline

1 Greedy Change

2 Recursive Change

3 Dynamic Programming
Recursive Change
Given the denominations 6, 5, and 1, what is the
minimum number of coins needed to change 9
cents?

MinNumCoins(9) =?

money 1 2 3 4 5 6 7 8 9 10
MinNumCoins ?
Recursive Change
Given the denominations 6, 5, and 1, what is the
minimum number of coins needed to change 9
cents?

⎪⎨ MinNumCoins (9 − 6) + 1
MinNumCoins (9) = min MinNumCoins (9 − 5) + 1
⎪⎩
MinNumCoins (9 − 1) + 1

money 1 2 3 4 5 6 7 8 9 10
MinNumCoins ?
Recursive Change
Given the denominations 6, 5, and 1, what is the
minimum number of coins needed to change 9
cents?

⎪⎨ MinNumCoins (3) + 1
MinNumCoins (9) = min MinNumCoins (4) + 1
⎪⎩
MinNumCoins (8) + 1

money 1 2 3 4 5 6 7 8 9 10
MinNumCoins ?
Recursive Change
Given the denominations 6, 5, and 1, what is the
minimum number of coins needed to change 9
cents?

⎪⎨ MinNumCoins (3) + 1
MinNumCoins (9) = min MinNumCoins (4) + 1
⎪⎩
MinNumCoins (8) + 1

money 1 2 3 4 5 6 7 8 9 10
MinNumCoins ? ? ? ?
Recurrence for Change Problem

MinNumCoins (money) =

MinNumCoins (money − coin )1+ 1
⎪⎨
MinNumCoins(money —coin2) + 1
min
···

MinNumCoins (money − coind) + 1

Recursive Change(money,coins)
i f money = 0:
return 0
MinNumCoins ← ∞
f o r i from 1 to |coins|:
i f money ≥ coini :
NumCoins ← RecursiveChange(money − coini,coins)
i f NumCoins + 1 < MinNumCoins :
MinNumCoins ← NumCoins + 1
return MinNumCoins
How Fast is RecursiveChange?
76
How Fast is RecursiveChange?
76

70 71 75
How Fast is RecursiveChange?
76

70 71 75

64 65 69 65 66 70 69 70 74
How Fast is RecursiveChange?
76

70 71 75

64 65 69 65 66 70 69 70 74

.
How Fast is RecursiveChange?
76

70 71 75

64 65 69 65 66 70 69 70 74

.
How Fast is RecursiveChange?
76

70 71 75

64 65 69 65 66 70 69 70 74
the optimal coin combination
.. for 70
cents is computed at least three times!
How Fast is RecursiveChange?
76

70 71 75

64 65 69 65 66 70 69 70 74
The optimal coin combination
.. for 70
cents is computed at least three times!
The optimal coin combination for 30
cents is computed trillions of times!
Hint
Wouldn’t it be nice to know all the
answers for changing money − coini by
the time we need to compute an optimal
way of changing money?
Hint
Wouldn’t it be nice to know all the
answers for changing money − coini by
the time we need to compute an optimal
way of changing money?
Instead of the time-consuming calls to

RecursiveChange(money−coini ,coins)

we would simply look up these values!


Outline

1 Greedy Change

2 Recursive Change

3 Dynamic Programming
Dynamic Programming
What is the minimum number of coins needed to
change 0 cents for denominations 6, 5, and 1?
money 0 1 2 3 4 5 6 7 8 9
MinNumCoins 0
Dynamic Programming
What is the minimum number of coins needed to
change 1 cent for denominations 6, 5, and 1?
money 0 1 2 3 4 5 6 7 8 9
MinNumCoins 0 1
Dynamic Programming
What is the minimum number of coins needed to
change 2 cents for denominations 6, 5, and 1?
money 0 1 2 3 4 5 6 7 8 9
MinNumCoins 0 1 2
Dynamic Programming
What is the minimum number of coins needed to
change 3 cents for denominations 6, 5, and 1?
money 0 1 2 3 4 5 6 7 8 9
MinNumCoins 0 1 2 3
Dynamic Programming
What is the minimum number of coins needed to
change 4 cents for denominations 6, 5, and 1?
money 0 1 2 3 4 5 6 7 8 9
MinNumCoins 0 1 2 3 4
Dynamic Programming
What is the minimum number of coins needed to
change 5 cents for denominations 6, 5, and 1?
money 0 1 2 3 4 5 6 7 8 9
MinNumCoins 0 1 2 3 4

{︃
MinNumCoins (0) + 1
min
MinNumCoins (4) + 1
Dynamic Programming
What is the minimum number of coins needed to
change 5 cents for denominations 6, 5, and 1?
money 0 1 2 3 4 5 6 7 8 9
MinNumCoins 0 1 2 3 4 1
Dynamic Programming
What is the minimum number of coins needed to
change 6 cents for denominations 6, 5, and 1?
money 0 1 2 3 4 5 6 7 8 9
MinNumCoins 0 1 2 3 4 1


⎪⎨ MinNumCoins (0) + 1
min MinNumCoins (1) + 1
⎪⎩
MinNumCoins (5) + 1
Dynamic Programming
What is the minimum number of coins needed to
change 6 cents for denominations 6, 5, and 1?
money 0 1 2 3 4 5 6 7 8 9
MinNumCoins 0 1 2 3 4 1 1
Dynamic Programming
What is the minimum number of coins needed to
change 7 cents for denominations 6, 5, and 1?
money 0 1 2 3 4 5 6 7 8 9
MinNumCoins 0 1 2 3 4 1 1
Dynamic Programming
What is the minimum number of coins needed to
change 7 cents for denominations 6, 5, and 1?
money 0 1 2 3 4 5 6 7 8 9
MinNumCoins 0 1 2 3 4 1 1 2
Dynamic Programming
What is the minimum number of coins needed to
change 8 cents for denominations 6, 5, and 1?
money 0 1 2 3 4 5 6 7 8 9
MinNumCoins 0 1 2 3 4 1 1 2
Dynamic Programming
What is the minimum number of coins needed to
change 8 cents for denominations 6, 5, and 1?
money 0 1 2 3 4 5 6 7 8 9
MinNumCoins 0 1 2 3 4 1 1 2 3
Dynamic Programming
What is the minimum number of coins needed to
change 9 cents for denominations 6, 5, and 1?
money 0 1 2 3 4 5 6 7 8 9
MinNumCoins 0 1 2 3 4 1 1 2 3
Dynamic Programming
What is the minimum number of coins needed to
change 9 cents for denominations 6, 5, and 1?
money 0 1 2 3 4 5 6 7 8 9
MinNumCoins 0 1 2 3 4 1 1 2 3 4
DP Change(money,coins)
MinNumCoins(0) ← 0
f o r m from 1 to money:
MinNumCoins(m) ← ∞
f o r i from 1 to |coins|:
i f m ≥ coini :
NumCoins ← MinNumCoins (m − coini ) + 1
i f NumCoins < MinNumCoins (m):
MinNumCoins(m) ← NumCoins
return MinNumCoins (money)
“Programming” in “Dynamic Programming”
Has Nothing to Do with Programming!
Richard Bellman developed this idea in
1950s working on an Air Force project.
At that time, his approach seemed
completely impractical.
He wanted to hide that he is really doing
math from the Secretary of Defense. Richard
Bellman
. . . What name could I choose? I was interested in planning
but planning, is not a good word for various reasons. I decided
therefore to use the word, “programming” and
I wanted to get across the idea that this was dynamic. It was
something not even a Congressman could object to. So I
used it as an umbrella for my activities.
Dynamic Programming:
String Comparison
Cystic Fibrosis
Cystic fibrosis (CF): An often
fatal disease which affects the
respiratory system and produces
an abnormally large amount of
mucus.
Mucus is a slimy material
that coats epithelial
surfaces and is secreted
into fluids such as saliva.
Approximately 1 in 25 Humans Carry a
Faulty CF Gene

When BOTH parent carry a faulty


gene, there is a 25% chance that their
child will have cystic fibrosis.
In the early 1980s biologists
hypothesized that CF is caused by
mutations in an unidentified gene.
Where Is the Cystic Fibrosis Gene?
In the late 1980s, biologists narrowed
the search for the CF gene to a million
nucleotide long region on chromosome
7. chromosome 7
However, this regions contained many
genes and it was unclear which of them
was responsible for CF.
Where Is the Cystic Fibrosis Gene?
In the late 1980s, biologists narrowed
the search for the CF gene to a million
nucleotide long region on chromosome
7. chromosome 7
However, this regions contained many
genes and it was unclear which of them
was responsible for CF.

Hint 1: Cystic fibrosis involves sweet secretion with abnor-


mally high sodium levels
Where Is the Cystic Fibrosis Gene?
In the late 1980s, biologists narrowed the
search for the CF gene to a million
nucleotide long region on chromosome
7. chromosome 7

However, this regions contained many genes


and it was unclear which of them
was responsible for CF.
Hint 1: Cystic fibrosis involves sweet secretion with abnor-
mally high sodium levels
Hint 2: By that time biologists already knew the sequences of
some genes responsible for secretion, e.g., ATP binding proteins
act as transport channels responsible for secretion.
Where Is the Cystic Fibrosis Gene?
In the late 1980s, biologists narrowed the
search for the CF gene to a million
nucleotide long region on chromosome
7. chromosome 7
However, this regions contained many genes
and it was unclear which of them
was responsible for CF.
Hint 1: Cystic fibrosis involves sweet secretion with abnor-
mally high sodium levels
Hint 2: By that time biologists already knew the sequences of
some genes responsible for secretion, e.g., ATP binding proteins
act as transport channels responsible for secretion.
Hint 3: Should we search for genes in this region that are similar
to known genes responsible for secretion?
Identifying the Cystic Fibrosis Gene

BINGO: One of the genes in this region


was similar to ATP binding proteins
that act as transport channels
responsible for secretion.

Hint 1: Cystic fibrosis involves sweet secretion with abnor- mally


high sodium levels
Hint 2: By that time biologists already knew the sequences of
some genes responsible for secretion, e.g., ATP binding proteins
act as transport channels responsible for secretion
Hint 3: Should we search for genes in this region that are similar
to known genes responsible for secretion?
Outline

1 The Alignment Game

2 Computing Edit Distance

3 Reconstructing an Optimal Alignment


The Alignment Game
A T G T T A T AA
T C G T C C

Alignment game: remove all symbols from two strings


in such a way that the number of points is maximized:
Remove the 1st symbol from both strings:
1 point if the symbols match,
0 points if they don’t match
Remove the 1st symbol from one of the strings: 0 points
The Alignment Game

A T G T T A T A
A T C G T C C
+1
The Alignment Game

A T G T T A T A
A T C G T C C
+1 +1
The Alignment Game

A T - G T T A T
AA T C G T C C
+1 +1
The Alignment Game

A T - G T T A T
AA T C G T C C
+1 +1 +1
The Alignment Game

A T - G T T A T
AA T C G T C C
+1 +1 +1 +1
The Alignment Game

A T - G T T A T
AA T C G T - C C
+1 +1 +1 +1
The Alignment Game

A T - G T T A T
AA T C G T - C C
+1 +1 +1 +1
The Alignment Game

A T - G T T A T
AA T C G T - C -
C
+1 +1 +1 +1
The Alignment Game

A T - G T T A T
AA T C G T - C -
C
+1 +1 +1 +1
The Alignment Game

A T - G T T A T
AA T C G T - C -
C =4
+1 +1 +1 +1
Sequence Alignment

A T - G T T A T A
A T C G T - C - C

Alignment of two strings is a two-row matrix:


1st row: symbols of the 1st string (in order)
interspersed by “–”
2nd row: symbols of the 2nd string (in order)
interspersed by “–”
Sequence alignment
matches

A T - G T T A T A
A T C G T - C - C
Sequence Alignment
matches mismatches

A T - G T T A T A
A T C G T - C - C
Sequence Alignment
matches insertions deletions mismatches

A T - G T T A T A
A T C G T - C - C
Alignment Score

A T - G T T A T A
A T C G T - C - C

Alignment score: premium for every match (+1) and


penalty for every mismatch (−� ), indel (−� ).
Alignment Score

A T - G T T A T A
A T C G T - C - C
+1 +1 -1 +1 +1 -1 +0 -1 +0 =1

Alignment score: premium for every match (+1) and


penalty for every mismatch (−� ), indel (−� ).

Example: � = 0 and � = 1
Alignment Score

#matches − � · #mismatches − � · #indels

Optimal alignment
Input: Two strings, mismatch penalty � ,
and indel penalty � .
Output: An alignment of the strings
maximizing the score.
Common Subsequence

A T - G T T A T A
A T C G T - C - C

Matches in an alignment of two strings


(ATGT) form their common subsequence
Longest common subsequence
Input: Two strings.
Output: A longest common subsequence of
these strings.
Longest common subsequence
Input: Two strings.
Output: A longest common subsequence of
these strings.

Maximizing the length of a common


subsequence corresponds to maximizing the
score of an alignment with � = � = 0.
Edit
distance
Input: Two strings.
Output: The minimum number of operations
(insertions, deletions, and
substitutions of symbols) to
transform one string into another.
Edit distance
Input: Two strings.
Output: The minimum number of operations
(insertions, deletions, and
substitutions of symbols) to
transform one string into another.

The minimum number of insertions, deletions


and mismatches in an alignment of two strings
(among all possible alignments).
Example

E D I —T I N G—
—D I S T A N C E
Exampl
e
mismatches
matches

E D I —T I N G—
—D I S T A N C E

deletion insertions
E D I — T I N G —
— D I S T A N C E
E D I — T I N G —
— D I S T A N C E

the total number of symbols in two strings=


E D I — T I N G —
— D I S T A N C E

the total number of symbols in two


strings=

+2·#matches
+2·#mismatches
+1·#insertions
+1·#deletions
E D I — T I N G —
— D I S T A N C E

the total number of symbols in two strings=

+2·#matches
+2·#matches −1·#insertions
+2·#mismatches −1·#deletions
+1·#insertions =+2·#mismatches
+1·#deletions +2·#insertions
+2·#deletions
E D I — T I N G —
— D I S T A N C E

the total number of symbols in two strings=

+2·#matches
2 · AlignmentScore
+2·#matches −1·#insertions
(� = 0, � =
+2·#mismatches −1·#deletions
1/2)
+1·#insertions =+2·#mismatches
+
+1·#deletions +2·#insertions
+2·#deletions 2 · EditDistance
E D I — T I N G —
— D I S T A N C E

the total number of symbols in two strings=

+2·#matches
minimizing edit distance
2 · AlignmentScore
+2·#matches —1·#insertions (� = 0, � = 1/2)
=
+2·#mismatches —1·#deletions
=+2·#mismatches
maximizing
+1·#insertions alignment score +
+1·#deletions +2·#insertions 2 · EditDistance
+2·#deletions
1Reconstructing an Optimal Alignment
Quick revisions
• Data
• Information
• Data Structure
• Algorithms
• Greedy algorithms
• Divide and conquer algorithms
• Dynamic Programming
• Change problem
• Greedy Change
• Recursive Change
• Dynamic Programming

89
Outline

1 The Alignment Game

2 Computing Edit Distance

3 Reconstructing an Optimal
Alignment
Edit Distance
How similar are two strings?

92
Approximate string matching

93
Alignment

• This is an example of alignment: a way of lining up


the characters of x and y.
• Could include mismatches, gaps or both.
• Vertical lines are drawn where opposite characters
match.

94
Hamming distance

95
Limitations of Hamming distance

96
Edit distance

97
Edit distance vs Hamming distance

98
Self study
• Differentiate the following string distance types
available to use in our applications.
• Levenshtein distance
• Damerau-Levenshtein distance
• Jaro Distance
• Jaro-Winkler Distance

•Finally, discuss different algorithms and metrics


to evaluate string similarity for each type of
distances.

It might be included in your mid term exam!


99
100
101
102
103
104
Cont…

105
A[1 . . . i ]
B[1 . . . j]

Given strings A[1 ...n] and B[1 ...m],


• What is an optimal alignment (an alignment that results in
minimum edit distance) of an i -prefix A[1 ...i ] of the first
string and a j-prefix B[1 ...j] of the second string?
A[1 . . . i ]
B[1 . . . j]

The last column of an optimal alignment is either


Insertion A[1 . . . i ] —
B[1 . . . j − 1] B[j]

A[1 . . . i ]
B[1 . . . j]

The last column of an optimal alignment is either


an insertion,
Insertion A[1 . . . i ] —
B[1 . . . j − 1] B[j]
Deletion A[1 . . . i − 1] A[i ]
A[1 . . . i ] B[1 . . . j] —
B[1 . . . j]

The last column of an optimal alignment is either


an insertion,
a deletion,
Insertion A[1 . . . i ] —
B[1 . . . j − 1] B[j]
Deletion A[1 . . . i − 1] A[i ]
A[1 . . . i ] B[1 . . . j] —
B[1 . . . j] A[1 . . . i − 1] A[i ]
A[i ] ̸= B[j]
B[1 . . . j − 1] B[j]
Mismatch

The last column of an optimal alignment is either


an insertion,
a deletion,
a mismatch,
Insertion A[1 . . . i ] —
B[1 . . . j − 1] B[j]
Deletion A[1 . . . i − 1] A[i ]
A[1 . . . i ] B[1 . . . j] —
B[1 . . . j] A[i ] ̸= B[j] A[1 . . . i − 1] A[i ]
Mismatch B[1 . . . j − 1] B[j]
A[i ] = B[j] A[1 . . . i − 1] A[i ]
Match B[1 . . . j − 1] B[j]

The last column of an optimal alignment is either


an insertion,
a deletion,
a mismatch,
or a match.
Insertion A[1 . . . i ] —
+1
B[1 . . . j − 1] B[j]
Deletion A[1 . . . i − 1] A[i ]
A[1 . . . i ] B[1 . . . j] — +1
B[1 . . . j] A[i ] ̸= B[j] A[1 . . . i − 1] A[i ]
+1
Mismatch B[1 . . . j − 1] B[j]
A[i ] = B[j] A[1 . . . i − 1] A[i ]
Match B[1 . . . j − 1] B[j]

The last column of an optimal alignment is either


an insertion,
a deletion,
a mismatch,
or a match.
What is left (after the removal of the last column) is an
optimal alignment of the corresponding two prefixes.
Insertion A[1 . . . i ] —
+1
B[1 . . . j − 1] B[j]
Deletion A[1 . . . i − 1] A[i ]
A[1 . . . i ] B[1 . . . j] — +1
B[1 . . . j] A[i ] ̸= B[j] A[1 . . . i − 1] A[i ]
+1
Mismatch B[1 . . . j − 1] B[j]
A[i ] = B[j] A[1 . . . i − 1] A[i ]
Match B[1 . . . j − 1] B[j]

Let D(i, j) be the edit distance of an i -prefix A[1 . . . i ]


and a j-prefix B[1 . . . j].
Insertion A[1 . . . i ] —
+1
B[1 . . . j − 1] B[j]
Deletion A[1 . . . i − 1] A[i ]
A[1 . . . i ] B[1 . . . j] — +1
B[1 . . . j] A[i ] ̸= B[j] A[1 . . . i − 1] A[i ]
+1
Mismatch B[1 . . . j − 1] B[j]
A[i ] = B[j] A[1 . . . i − 1] A[i ]
Match B[1 . . . j − 1] B[j]


⎪D(i , j − 1) + 1


D(i − 1, j) + 1
D(i, j) = min
D(i − 1, j − 1) + 1 if A[i ] ̸= B[j]

⎩ D(i − 1, j − 1) if A[i ] = B[j]
j
D I S T A N C E
0 1 2 3 4 5 6 7 8
0
E 1
D 2
I 3
i T 4
I 5
N 6
G 7
Comparing A[1...n] = EDITING and
B[1...m] = DISTANCE
j
D I S T A N C E
0 1 2 3 4 5 6 7 8
0
E 1
D 2
I 3
i T 4 D(i, j)

I 5
N 6
G 7
comparing A[1 . . . n] = EDITING
and B [1 . . . m] = DISTANCE
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0
E 1 1
D 2 2
I 3 3
T 4 4
I 5 5
N 6 6
G 7 7
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1
D 2 2
I 3 3
T 4 4
I 5 5
N 6 6
G 7 7
D
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 ?
D 2 2
I 3 3
T 4 4
I 5 5
N 6 6
G 7 7
D(1, 1) = min{D(1, 0) + 1, D(0, 1) + 1, D(0, 0) + 1}
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 ?
D 2 2
I 3 3
T 4 4
I 5 5
N 6 6
G 7 7
D(1, 1) = min{2, 2, 1}
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1
D 2 2
I 3 3
T 4 4
I 5 5
N 6 6
G 7 7
D
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1
D 2 2 ?
I 3 3
T 4 4
I 5 5
N 6 6
G 7 7
D(2, 1) = min{D(2, 0) + 1, D(1, 1) + 1, D(1, 0)}
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1
D 2 2 1
I 3 3
T 4 4
I 5 5
N 6 6
G 7 7
D
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1
D 2 2 1
II 3 3 ?
T 4 4
I 5 5
N 6 6
G 7 7
D(3, 1) = min{D(3, 0) + 1, D(2, 1) + 1, D(2, 0) + 1}
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1
D 2 2 1
I 3 3 2
T 4 4
I 5 5
N 6 6
G 7 7
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1
D 2 2 1
I 3 3 2
T 4 4 3
I 5 5 4
N 6 6 5
G 7 7 6
D II S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 ?
D 2 2 1
I 3 3 2
T 4 4 3
I 5 5 4
N 6 6 5
G 7 7 6
D(1, 2) = min{D(1, 1) + 1, D(0, 2) + 1, D(0, 1) + 1}
D II S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 ?
D 2 2 1
I 3 3 2
T 4 4 3
I 5 5 4
N 6 6 5
G 7 7 6
D(1, 1) = min{2, 3, 2}
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2
D 2 2 1
I 3 3 2
T 4 4 3
I 5 5 4
N 6 6 5
G 7 7 6
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2
D 2 2 1 2
I 3 3 2 1
T 4 4 3 2
I 5 5 4 3
N 6 6 5 4
G 7 7 6 5
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2 3 4 5 6 7 7
D 2 2 1 2 3 4 5 6 7 8
I 3 3 2 1 2 3 4 5 6 7
T 4 4 3 2 2 2 3 4 5 6
I 5 5 4 3 3 3 3 4 5 6
N 6 6 5 4 4 4 4 3 4 5
G 7 7 6 5 5 5 5 4 4 5
Edit Distance( A [1 ...n], B[1...m])
D(i, 0) ← i and D(0, j) ← j f o r a l l i , j
f o r j from 1 to m:
f o r i from 1 to n:
insertion ← D(i, j − 1) + 1
deletion ← D(i − 1, j) + 1
match ← D(i − 1, j − 1)
mismatch ← D(i − 1, j − 1) + 1
i f A[i ] = B [j]:
D(i, j) ← min(insertion, deletion, match)
else:
D(i, j) ← min(insertion, deletion, mismatch)
return D(n, m)
Outline

1 The Alignment Game

2 Computing Edit Distance

3 Reconstructing an Optimal Alignment


Optimal Alignment
We have computed the edit distance, but
how can we find an optimal alignment?
Optimal Alignment
We have computed the edit distance, but how
can we find an optimal alignment?
The backtracking pointers that we stored
will help us to reconstruct an optimal
alignment.
D I S T A N C E
0 1 2 3 4 5 6 7 8
0
E 1
D 2
I 3
T 4
I 5
N 6
G 7
D I S T A N C E
0 1 2 3 4 5 6 7 8
0
• Any path from (0,0) E 1
to (i ,j) spells an D 2
alignment of prefixes
I 3
A[1...i ] and
B[1...j] T 4
I 5
N 6
G 7
D I S T A N C E
0 1 2 3 4 5 6 7 8
0
any path from E 1
(0, 0) to (i , j) D 2
spells an align-
I 3
ment of prefixes
A[1 . . . i ] and T 4
B [1 . . . j] I 5
N 6
G 7

E
D
D I S T A N C E
0 1 2 3 4 5 6 7 8
0
any path from E 1
(0, 0) to (i , j) D 2
spells an align-
I 3
ment of prefixes
A[1 . . . i ] and T 4
B [1 . . . j] I 5
N 6
G 7

E −
D I
D I S T A N C E
0 1 2 3 4 5 6 7 8
0
any path from E 1
(0, 0) to (i , j) D 2
spells an align-
I 3
ment of prefixes
A[1 . . . i ] and T 4
B [1 . . . j] I 5
N 6
G 7

E ——
D I S
D I S T A N C E
0 1 2 3 4 5 6 7 8
0
any path from E 1
(0, 0) to (i , j) D 2
spells an align-
I 3
ment of prefixes
A[1 . . . i ] and T 4
B [1 . . . j] I 5
N 6
G 7

E —— D
D I S—
D I S T A N C E
0 1 2 3 4 5 6 7 8
0
any path from E 1
(0, 0) to (i , j) D 2
spells an align-
I 3
ment of prefixes
A[1 . . . i ] and T 4
B [1 . . . j] I 5
N 6
G 7

E —— D I
D I S ——
D I S T A N C E
0 1 2 3 4 5 6 7 8
0
any path from E 1
(0, 0) to (i , j) D 2
spells an align-
I 3
ment of prefixes
A[1 . . . i ] and T 4
B [1 . . . j] I 5
N 6
G 7

E —— D I T
D I S —— T
D I S T A N C E
0 1 2 3 4 5 6 7 8
0
any path from E 1
(0, 0) to (i , j) D 2
spells an align-
I 3
ment of prefixes
A[1 . . . i ] and T 4
B [1 . . . j] I 5
N 6
G 7

E —— D I T I N — G
D I S —— T A N C E
D I S T A N C E
0 1 2 3 4 5 6 7 8
0
E 1
the constructed
path corresponds D 2
to distance 8 and I 3
is not optimal T 4
(edit distance is 5)
I 5
N 6
G 7

E —— D I T I N — G
D I S —— T A N C E
D I S T A N C E
0 1 2 3 4 5 6 7 8
0
E 1
To construct
an optimal D 2
align- ment we I 3
will use the
T 4
backtracking
pointers I 5
N 6
G 7

E —— D I T I N — G
D I S —— T A N C E
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2 3 4 5 6 7 7
D 2 2 1 2 3 4 5 6 7 8
I 3 3 2 1 2 3 4 5 6 7
T 4 4 3 2 2 2 3 4 5 6
I 5 5 4 3 3 3 3 4 5 6
N 6 6 5 4 4 4 4 3 4 5
G 7 7 6 5 5 5 5 4 4 5
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2 3 4 5 6 7 7
D 2 2 1 2 3 4 5 6 7 8
the edit
I 3 3 2 1 2 3 4 5 6 7
distance is 5
T 4 4 3 2 2 2 3 4 5 6
I 5 5 4 3 3 3 3 4 5 6
N 6 6 5 4 4 4 4 3 4 5
G 7 7 6 5 5 5 5 4 4 5
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2 3 4 5 6 7 7 we arrived to the
D 2 2 1 2 3 4 5 6 7 8 bottom right cell
by moving along
I 3 3 2 1 2 3 4 5 6 7
the backtracking
T 4 4 3 2 2 2 3 4 5 6 pointers shown
I 5 5 4 3 3 3 3 4 5 6 below.
N 6 6 5 4 4 4 4 3 4 5
G
G 7 7 6 5 5 5 5 4 4 5
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8 There exists an op-
E 1 1 1 2 3 4 5 6 7 7 timal alignment
D 2 2 1 2 3 4 5 6 7 8 whose last column is a
mismatch and an
I 3 3 2 1 2 3 4 5 6 7
optimal alignment
T 4 4 3 2 2 2 3 4 5 6 whose last column is
I 5 5 4 3 3 3 3 4 5 6 an insertion.
N 6 6 5 4 4 4 4 3 4 5
G
G 7 7 6 5 5 5 5 4 4 5
D I S T A N C
C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2 3 4 5 6 7 7
D 2 2 1 2 3 4 5 6 7 8
let’s consider
I 3 3 2 1 2 3 4 5 6 7
a mismatch
T 4 4 3 2 2 2 3 4 5 6
I 5 5 4 3 3 3 3 4 5 6
N 6 6 5 4 4 4 4 3 4 5
G 7 7 6 5 5 5 5 4 4 5

G
E
D I S T A N C
C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2 3 4 5 6 7 7
D 2 2 1 2 3 4 5 6 7 8
we continue in
I 3 3 2 1 2 3 4 5 6 7
a similar
T 4 4 3 2 2 2 3 4 5 6 fashion
I 5 5 4 3 3 3 3 4 5 6
N 6 6 5 4 4 4 4 3 4 5
G 7 7 6 5 5 5 5 4 4 5

G
E
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2 3 4 5 6 7 7
D 2 2 1 2 3 4 5 6 7 8
I 3 3 2 1 2 3 4 5 6 7
T 4 4 3 2 2 2 3 4 5 6
I 5 5 4 3 3 3 3 4 5 6
N 6 6 5 4 4 4 4 3 4 5
G 7 7 6 5 5 5 5 4 4 5

—G
C E
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2 3 4 5 6 7 7
D 2 2 1 2 3 4 5 6 7 8
I 3 3 2 1 2 3 4 5 6 7
T 4 4 3 2 2 2 3 4 5 6
II 5 5 4 3 3 3 3 4 5 6
N 6 6 5 4 4 4 4 3 4 5
G 7 7 6 5 5 5 5 4 4 5

N—G
N C E
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2 3 4 5 6 7 7
D 2 2 1 2 3 4 5 6 7 8
I 3 3 2 1 2 3 4 5 6 7
T 4 4 3 2 2 2 3 4 5 6
I 5 5 4 3 3 3 3 4 5 6
N 6 6 5 4 4 4 4 3 4 5
G 7 7 6 5 5 5 5 4 4 5

I N—G
A N C E
D I S
S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2 3 4 5 6 7 7
D 2 2 1 2 3 4 5 6 7 8
II 3 3 2 1 2 3 4 5 6 7
T 4 4 3 2 2 2 3 4 5 6
I 5 5 4 3 3 3 3 4 5 6
N 6 6 5 4 4 4 4 3 4 5
G 7 7 6 5 5 5 5 4 4 5

T I N—G
T A N C E
D II S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2 3 4 5 6 7 7
D 2 2 1 2 3 4 5 6 7 8
II 3 3 2 1 2 3 4 5 6 7
T 4 4 3 2 2 2 3 4 5 6
I 5 5 4 3 3 3 3 4 5 6
N 6 6 5 4 4 4 4 3 4 5
G 7 7 6 5 5 5 5 4 4 5

—T I N—G
S T A N C E
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2 3 4 5 6 7 7
D 2 2 1 2 3 4 5 6 7 8
I 3 3 2 1 2 3 4 5 6 7
T 4 4 3 2 2 2 3 4 5 6
I 5 5 4 3 3 3 3 4 5 6
N 6 6 5 4 4 4 4 3 4 5
G 7 7 6 5 5 5 5 4 4 5

I —T I N—G
I S T A N C E
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2 3 4 5 6 7 7
D 2 2 1 2 3 4 5 6 7 8
I 3 3 2 1 2 3 4 5 6 7
T 4 4 3 2 2 2 3 4 5 6
I 5 5 4 3 3 3 3 4 5 6
N 6 6 5 4 4 4 4 3 4 5
G 7 7 6 5 5 5 5 4 4 5

D I —T I N—G
D I S T A N C E
D I S T A N C E
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 6 7 8
E 1 1 1 2 3 4 5 6 7 7
D 2 2 1 2 3 4 5 6 7 8
I 3 3 2 1 2 3 4 5 6 7
T 4 4 3 2 2 2 3 4 5 6
I 5 5 4 3 3 3 3 4 5 6
N 6 6 5 4 4 4 4 3 4 5
G 7 7 6 5 5 5 5 4 4 5

E D I —T I N—G Constructing A[i,


—D I S T A N C E j] and B[i, j].
Output Alignment( i, j )
i f i = 0 and j = 0:
return
i f backtrack (i , j) = ↓ :
Output Alignment(i − 1, j)
p r i n t A[i ]

e l s e i f backtrack (i , j) = → :
Output Alignment(i, j − 1)
print —
B [j]
else:
Output Alignment(i − 1, j − 1)
p r i n t A[i ]
B [j]
Output Alignment(i, j)
i f i = 0 and j = 0:
return
i f i > 0 and D(i, j) = D(i − 1, j) + 1:
Output Alignment(i − 1, j)
p r i n t A[i ]

e l s e i f j > 0 and D(i, j) = D(i, j − 1) + 1:
Output Alignment(i, j − 1)
print —
B [j]
else:
Output Alignment(i − 1, j − 1)
p r i n t A[i ]
B [j]
Thank you!!

Next Session:
1 Knapsack
Outline

1 Problem Overview

2 Knapsack with Repetitions

3 Knapsack without Repetitions

4 Final Remarks
TV commercial placement

Select a set of TV commercials (each


commercial has duration and cost) so that
the total revenue is maximal while the total
length does not exceed the length of the
available time slot.
Optimizing data center performance
Purchase computers for a data center to
achieve the maximal performance under
limited budget.
Knapsack Problem
(knapsack is another word for backpack)

Goal
Maximize
value ($)
while limiting
total
weight (kg)
Some applications of Knapsack
problem
1) Home Energy Management
2) Cognitive Radio Networks
3) Resource management in software
4) Large-scale multi-period precedence constrained knapsack
problem: A mining application
5) Relay selection in secure cooperative wireless communication
6) Power allocation management
7) Selecting adverts garden city radio
8) Solve the production planning problem
9) Cognitive radio networks
10) 5G mobile edge computing
Cont…
11) Selection of renovation actions
12) Solve the production planning problem
13) Sensor Selection in Distributed Multiple-Radar
14) Architectures for Localization
15) Appliance Scheduling Optimization for Demand Response
16) Adaptive Variable Density Sampling
17) Secure Cooperative Wireless Communication
18) Optimizing Power Allocation to Electrical Appliances
19) Computation Offloading in Wireless Multi-Hop Networks
20) Formulation and Solution Method of Tour Conducting
21) Plastic Bags Waste Management Using the Knapsack Model
22) Workflow mapping using CUDA
23) Optimization of Content Delivery Networks
24) Network Selection for Mobile Nodes

169
Problem Variations

knapsack
Problem Variations
fractional
knapsack

knapsack

discrete
knapsack
Problem Variations
fractional can take fractions
knapsack of items

knapsack

discrete each item is either


knapsack taken or not
Problem Variations
fractional
knapsack
with unlimited
repetitions quantities
knapsack

discrete without one of each


knapsack repetitions item
Problem Variations
greedy algorithm
fractional
knapsack
with unlimited
repetitions quantities
knapsack

discrete without one of each


knapsack repetitions item
Problem Variations
greedy algorithm
fractional
knapsack
with unlimited
repetitions
greedy quantities
does not work
knapsack for discrete knapsack!
will design a dynamic
discrete without one
programming solution
of each
knapsack repetitions item
Example
$30 $14 $16 $9
6 3 4 2

10
knapsack
Example
$30 $14 $16 $9
6 3 4 2
$30 $16
w/o repeats 6 4 total: $46
Example
$30 $14 $16 $9
6 3 4 2
$30 $16
w/o repeats 6 4 total: $46
$30 $9 $9
w repeats 6 2 2 total: $48
Exampl
e $30 $14 $16 $9
6 3 4 2
$30 $16
w/o repeats 6 4 total: $46
$30 $9 $9
w repeats 6 2 2 total: $48
$30 $14 $4.5
fractional 6 3 1 total: $48.5
Why does greedy fail for the
discrete knapsack?
Example
$30 $14 $16 $9
6 3 4 2
Why does greedy fail for the
discrete knapsack?
Example
$30 $14 $16 $9
6 3 4 2
5 42 4 41
3 2
Why does greedy fail for the
discrete knapsack?
Example
$30 $14 $16 $9
6 3 4 2
5 42 4 41
3 2

$30
6
Why does greedy fail for the discrete knapsack?

Example
$30 $14 $16 $9
6 3 4 2
5 42 4 41
3 2

$30 $14
6 3
Why does greedy fail for the discrete knapsack?

Example
$30 $14 $16 $9
6 3 4 2
5 42 4 41
3 2

taking an element of maximum


$30 $14
value per unit of weight is not safe!
6 3
Outline

1 Problem Overview

2 Knapsack with Repetitions

3 Knapsack without Repetitions

4 Final Remarks
With repetitions: Without repeti-
unlimited quantities tions: one of
each item
With repetitions: Without repeti-
unlimited quantities tions: one of
each item
Knapsack with repetitions problem
Input: Weights w1,...,wn and values v1,...,
vn of n items; total weight W (vi’s,
wi’s, and W are non-negative
integers).
Output: The maximum value of items whose
weight does not exceed W . Each item
can be used any number of times.
Subproblems

Consider an optimal solution and an


item in it:
wi W
Subproblems

Consider an optimal solution and an


item in it:
wi W
If we take this item out then we get an
optimal solution for a knapsack of total
weight W − wi.
Subproblems

Let value(w ) be the maximum value of


knapsack of weight w.
Subproblems

Let value(w ) be the maximum value of


knapsack of weight w.

value(w ) = max {value(w − wi) + v i }


i : w i ≤w
Knapsack(W )
value(0) ← 0
fo r w from 1 to W :
value(w ) ← 0
fo r i from 1 to n:
i f wi ≤ w:
val ← value(w − wi) + vi
i f val > value(w ) :
value(w ) ← val
return value(W )
Example: W = 10

$30 $14 $16 $9


6 3 4 2

0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0

Hint, find
maximum of (w2 + w3 + … + wi) + v1, (w1 +w3 + … +
wi) + v2, ….., (w1+w2+…+w(i-1)) + wi.
Example: W = 10

$30 $14 $16 $9


6 3 4 2

0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0
Example: W = 10

$30 $14 $16 $9


6 3 4 2

0 1 2 3 4 5 6 7 8 9 10
0 0 9 0 0 0 0 0 0 0 0
Example: W = 10

$30 $14 $16 $9


6 3 4 2

0 1 2 3 4 5 6 7 8 9 10
0 0 9 0 0 0 0 0 0 0 0
Example: W = 10

$30 $14 $16 $9


6 3 4 2

0 1 2 3 4 5 6 7 8 9 10
0 0 9 14 0 0 0 0 0 0 0
Example: W = 10

$30 $14 $16 $9


6 3 4 2

0 1 2 3 4 5 6 7 8 9 10
0 0 9 14 0 0 0 0 0 0 0
Example: W = 10

$30 $14 $16 $9


6 3 4 2

0 1 2 3 4 5 6 7 8 9 10
0 0 9 14 18 0 0 0 0 0 0
Example: W = 10

$30 $14 $16 $9


6 3 4 2

0 1 2 3 4 5 6 7 8 9 10
0 0 9 14 18 23 30 32 39 44 48
Outline
1 Problem Overview

2 Knapsack with Repetitions

3 Knapsack without Repetitions

4 Final Remarks
With repetitions: Without repeti-
unlimited quantities tions: one of
each item
With repetitions: Without repeti-
unlimited tions: one of each
quantities item
Knapsack without repetitions problem
Input: Weights w1,...,wn and values v1,...,vn
of n items; total weight W (vi’s, wi’s,
and W are non-negative integers).

Output: The maximum value of items whose


weight does not exceed W . Each
item can be used at most once.
Same Subproblems?

wn W
Same Subproblems?

wn W

W − wn
Same Subproblems?

wn W

wn W − wn
Same Subproblems?

wn W

wn W − wn
Subproblems
If the n-th item is taken into an optimal
solution:
wn W
then what is left is an optimal solution
for a knapsack of total weight W − wn
using items 1, 2, . . . , n − 1.
Subproblems
If the n-th item is taken into an optimal
solution:
wn W
then what is left is an optimal solution
for a knapsack of total weight W − wn
using items 1, 2, . . . , n − 1.
If the n-th item is not used, then the
whole knapsack must be filled in
optimally with items 1, 2, . . . , n − 1.
Subproblems
For 0 ≤ w ≤ W and 0 ≤ i ≤ n, value(w, i )
is the maximum value achievable using a
knapsack of weight w and items 1, . . . , i .
Subproblems

For 0 ≤ w ≤ W and 0 ≤ i ≤ n, value(w, i )


is the maximum value achievable using a
knapsack of weight w and items 1, . . . , i .
The i -th item is either used or not:
value(w, i ) is equal to

max{value(w−wi, i−1)+vi, value(w, i−1)}


Knapsack(W )
i n i t i a l i z e a l l value(0, j) ← 0
i n i t i a l i z e a l l value(w, 0) ← 0
f o r i from 1 to n:
f o r w from 1 to W :
value(w, i ) ← value(w, i − 1)
i f wi ≤ w :
val ← value(w − wi, i − 1) + vi
i f value(w, i ) < val
value(w, i ) ← val
return value(W , n)
Example: reconstructing a solution
$30 $14 $16 $9
6 3 4 2

0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 3030303030
2 0 0 0 1414143030304444
3 0 0 0 1416163030304446
4 0 0 9 1416233030394446

max{value(w−wi, i−1)+vi, value(w, i−1)}


Example: reconstructing a
solution
$30 $14 $16 $9
6 3 4 2

0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 3030303030
2 0 0 0 1414143030304444
3 0 0 0 1416163030304446
4 0 0 9 1416233030394446
Example: reconstructing a
solution
$30 $14 $16 $9
6 3 4 2

0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 3030303030
2 0 0 0 1414143030304444
3 0 0 0 1416163030304446
4 0 0 9 1416233030394446

1 2 3 4
Optimal solution: 1 0 1 0
Outline

1 Problem Overview

2 Knapsack with Repetitions

3 Knapsack without Repetitions

4 Final Remarks
Memoization
• Memoization is a term describing an optimization technique where you
cache previously computed results, and return the cached result when
the same computation is needed again.
• Dynamic programming is a technique for solving problems of recursive
nature, iteratively and is applicable when the computations of the
subproblems overlap.

• When you solve a dynamic programming problem using tabulation you


solve the problem "bottom up", i.e., by solving all related sub-problems
first, typically by filling up an n-dimensional table. Based on the results
in the table, the solution to the "top" / original problem is then
computed.
• If you use memoization to solve the problem you do it by maintaining a
map of already solved sub problems. You do it "top down" in the sense
that you solve the "top" problem first (which typically recurses down to
solve the sub-problems).
Memoization
Knapsack(w )
i f w i s i n hash t a b l e :
return value(w )
value(w ) ← 0
f o r i from 1 to n:
i f wi ≤ w :
val ← Knapsack(w − wi ) + vi
i f val > value(w ) :
value(w ) ← val
i n s e r t value(w ) i n t o hash ta b l e with key
w
return value(w )
What is Faster?
If all subproblems must be solved then an iterative
algorithm is usually faster since it has no
recursion overhead.

There are cases however when one does not need


to solve all sub problems: assume that W and all
wi’s are multiples of 100; then value(w) is not
needed if w is not divisible by 100.
Running Time
The running time O(nW ) is not polynomial
since the input size is proportional to log W , but
not W .
In other words, the running time is O(n2logW ).

E.g., for
later, we’ll learn why
W = 71 345 970 345 617 824 751
solving this problem
(twenty digits only!) the algorithm needs roughly 1020
in polynomial time costs $1M!
basic operations.
Dynamic Programming: Placing Parentheses
• A good problem to solve by Dynamic
Programming is Placing Parentheses problem.
Outline

1 Problem Overview

2 Subproblems

3 Algorithm

4 Reconstructing a Solution
How to place parentheses in an expression

1+ 2− 3× 4− 5

to maximize its value?


How to place parentheses in an expression

1+ 2− 3× 4− 5

to maximize its value?


Example
((((1 + 2) − 3) × 4) − 5) = −5
How to place parentheses in an expression

1+ 2− 3× 4− 5

to maximize its value?


Example
((((1 + 2) − 3) × 4) − 5) = −5
((1 + 2) − ((3 × 4) − 5)) = −4
Answer

((1 + 2) − (3 × (4 − 5))) = 6
Another example
What about

5 − 8 + 7 × 4 − 8 + 9?
Another example
What about

5 − 8 + 7 × 4 − 8 + 9?

Soon
We’ll design an efficient dynamic
programming algorithm to find the answer.
Outline

1 Problem Overview

2 Subproblems

3 Algorithm

4 Reconstructing a Solution
Placing parentheses
Input: A sequence of digits d1, . . . , dn and
a sequence of operations
op1, . . . , opn−1 ∈ { +, −, × }.
Output: An order of applying these
operations that maximizes the
value of the expression

d1 op1 d2 op2 ···opn−1 dn .


Intuition

Assume that the last operation in an


optimal parenthesizing of
5 − 8 + 7 × 4 − 8 + 9 is ×:

(5 − 8 + 7) × (4 − 8 + 9) .
Intuition

Assume that the last operation in an


optimal parenthesizing of
5 − 8 + 7 × 4 − 8 + 9 is ×:

(5 − 8 + 7) × (4 − 8 + 9) .

It would help to know optimal values for


subexpressions 5 − 8 + 7 and 4 − 8 + 9.
However
We need to keep track for both the minimal
and the maximal values of subexpressions!
Example: (5 − 8 + 7) × (4 − 8 + 9)

min(5 − 8 + 7) = (5 − (8 + 7)) = −10


max(5 − 8 + 7) = ((5 − 8) + 7) = 4
min(4 − 8 + 9) = (4 − (8 + 9)) = −13
max(4 − 8 + 9) = ((4 − 8) + 9) = 5
Example: (5 − 8 + 7) × (4 − 8 + 9)

min(5 − 8 + 7) = (5 − (8 + 7)) = −10


max(5 − 8 + 7) = ((5 − 8) + 7) = 4
min(4 − 8 + 9) = (4 − (8 + 9)) = −13
max(4 − 8 + 9) = ((4 − 8) + 9) = 5

max((5 − 8 + 7) × (4 − 8 + 9)) = 130


Subproblems
Let Ei,j be the subexpression

di opi ···opj −1 dj
Subproblems
Let Ei,j be the subexpression

di opi ···opj −1 dj

Subproblems:

M (i, j) = maximum value of E i ,j


m(i, j) = minimum value of E i ,j
Recurrence Relation
• Generally, to find maximum of an subexpression E(i, j), we
follow the recurrences:
M(i, k ) opk M(k + 1, j)
M(i, k ) opk m(k + 1, j)
max
M (i , j ) = i ≤k≤j −1 m(i , k) opk M(k + 1, j)
⎩⎪
m(i, k ) opk m(k + 1, j)
M(i, k ) opk M(k + 1, j)
M(i, k ) opk m(k + 1, j)
min
m(i , j ) = i ≤k≤j −1 m(i , k) opk M(k + 1, j)


m(i, k ) opk m(k + 1, j)
Outline

1 Problem Overview

2 Subproblems

3 Algorithm

4 Reconstructing a Solution
MinAndMax(i, j)
min ← +∞
max ← −∞
to j − 1:
for k from i a ← M (i, k)
opk M (k + 1, j)
b ← M (i, k) opk c ← m(i, k)
m(k + 1, j)
opk
M (k + 1, j)
d ← m(i, k) m(k + 1, j) opk
min ← min(min, a, b, c, d)
max ← max(max, a, b, c, d)
return (min, max)
Order of Subproblems

When computing M (i, j), the values of


M (i, k) and M (k + 1, j) should be
already computed.
Solve all subproblems in order of
increasing (j − i).
Possible Order

1 n
1

n
Parentheses(d1 op1 d2 op2 . . . dn)
for i from 1 to n:
m(i, i) ← di , M (i, i) ← di
for s from 1 to n − 1:
for i from 1 to n − s:
j← i+s
m(i, j), M (i, j) ← MinAndMax(i, j)
return M (1, n)
Example: 5 − 8 + 7 × 4 − 8 + 9
5 -3 -10 -55 -63 -94 5 -3 4 25 65 200
8 15 36 -60 - 8 15 60 52 75
195
7 28 20 35
7 28 -28 -91
4 -4 5
4 -4 -13
8 17
8 17
9
9
m M
Exercise

Ans: _________
Exercise
• Fill in the following grid with the correct sub-problem
solutions for this sequence alignment problem with
these weights: 0 for mutation, 1 for insertion or
deletion, and 3 for a match (the goal is to maximize the
sum of the weights).
Here “ATC” is the starting sequence and “TCAG” is the
ending sequence.

• What is the optimal alignment?


248

You might also like