lecture 6 algorithmic techniques part 2 -...

61
Lecture 6 Algorithmic Techniques Part 2 COMP26120 Giles Reger March 2019 Giles Reger Lecture 7 March 2019 1 / 18

Upload: others

Post on 25-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Lecture 6Algorithmic Techniques Part 2

COMP26120

Giles Reger

March 2019

Giles Reger Lecture 7 March 2019 1 / 18

Page 2: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Coding Interview

Implement a function to compute the nthnumber of the Fibonacci sequence

Giles Reger Lecture 7 March 2019 2 / 18

Page 3: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Points from Exercise

Standard divide-and-conquer recursive approach is ‘top down’

If we meet the same sub-problem during top-down approach we canmemoize e.g. remember the result

If we go ‘bottom-up’ iteratively we can build the final solution fromsmaller ones

Giles Reger Lecture 7 March 2019 3 / 18

Page 4: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

The General Coin Problem

Question How do we solve the general case of the coin problem?

Greedy solutions fail in general.

Dynamic programming always produces an optimal solution to thisproblem.

Suppose we have coin types 1, . . . ,N and the value of coin type i is vi .

Suppose that we have enough coins of each type (if we have a limitednumber of coins of each type, we can modify the idea below).

How do we make a sum with a minimum number of coins?

Let us quickly think about how we might enumerate all solutions using aconfiguration (a1, a2, . . . aN) storing how many we have of each coin.

Giles Reger Lecture 7 March 2019 4 / 18

Page 5: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

The General Coin Problem

Question How do we solve the general case of the coin problem?

Greedy solutions fail in general.

Dynamic programming always produces an optimal solution to thisproblem.

Suppose we have coin types 1, . . . ,N and the value of coin type i is vi .

Suppose that we have enough coins of each type (if we have a limitednumber of coins of each type, we can modify the idea below).

How do we make a sum with a minimum number of coins?

Let us quickly think about how we might enumerate all solutions using aconfiguration (a1, a2, . . . aN) storing how many we have of each coin.

Giles Reger Lecture 7 March 2019 4 / 18

Page 6: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

The General Coin Problem

Question How do we solve the general case of the coin problem?

Greedy solutions fail in general.

Dynamic programming always produces an optimal solution to thisproblem.

Suppose we have coin types 1, . . . ,N and the value of coin type i is vi .

Suppose that we have enough coins of each type (if we have a limitednumber of coins of each type, we can modify the idea below).

How do we make a sum with a minimum number of coins?

Let us quickly think about how we might enumerate all solutions using aconfiguration (a1, a2, . . . aN) storing how many we have of each coin.

Giles Reger Lecture 7 March 2019 4 / 18

Page 7: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Coin Problem

Key property:

Let c(i , s) be the minimum number of coins from types 1 to i required tomake sum s. Consider what happens if we add another coin type i + 1:

c(i + 1, s) = min

c(i , s)c(i , s − vi+1) + 1...c(i , s − k × vi+1) + k

where (k + 1)vi+1 > s

c(i , s) = ∞ if value s cannot be made with coins 1 . . . i .

This says: if we have solved the problem for coins of types 1 . . . i and nowwe consider coins of type i + 1, then an optimal solution may be to use nocoins of type i + 1 or one such coin combined with an optimal solution ofa smaller problem using coin types 1 . . . i , or two coins of type i + 1...

Giles Reger Lecture 7 March 2019 5 / 18

Page 8: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Coin Problem

Key property:

Let c(i , s) be the minimum number of coins from types 1 to i required tomake sum s. Consider what happens if we add another coin type i + 1:

c(i + 1, s) = min

c(i , s)c(i , s − vi+1) + 1...c(i , s − k × vi+1) + k

where (k + 1)vi+1 > s

c(i , s) = ∞ if value s cannot be made with coins 1 . . . i .

This says: if we have solved the problem for coins of types 1 . . . i and nowwe consider coins of type i + 1, then an optimal solution may be to use nocoins of type i + 1 or one such coin combined with an optimal solution ofa smaller problem using coin types 1 . . . i , or two coins of type i + 1...

Giles Reger Lecture 7 March 2019 5 / 18

Page 9: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11

coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3

1 2 3 4 1 2 3 4 1 2 3

coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 10: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞

coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3

1 2 3 4 1 2 3 4 1 2 3

coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 11: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3

coin types 1,2,3

1 2 3 4 1 2 3 4 1 2 3

coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 12: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3

coin types 1,2,3

1 2 3 4 1 2 3 4 1 2 3

coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

c(2, 10) = min(c(1, 10), c(1, 9) + 1, c(1, 8) + 2, . . .)min(∞, 1 + 1,∞+ 2, . . .)2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 13: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3

1 2 3 4 1 2 3 4 1 2 3coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 14: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3 1

2 3 4 1 2 3 4 1 2 3coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 15: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3 1 2

3 4 1 2 3 4 1 2 3coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 16: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3 1 2 3

4 1 2 3 4 1 2 3coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 17: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3 1 2 3 4

1 2 3 4 1 2 3coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 18: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3 1 2 3 4 1

2 3 4 1 2 3coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 19: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3 1 2 3 4 1 2

3 4 1 2 3coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 20: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3 1 2 3 4 1 2 3

4 1 2 3coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 21: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3 1 2 3 4 1 2 3 4

1 2 3coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 22: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3 1 2 3 4 1 2 3 4 1

2 3coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 23: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3 1 2 3 4 1 2 3 4 1 2

3coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 24: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3 1 2 3 4 1 2 3 4 1 2 3

coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 25: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3 1 2 3 4 1 2 3 4 1 2 3coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 26: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Example: there are 4 types of coins, with values 9, 1, 5 and 6. We wish tomake sum 11.

Difficulty: Each subproblem that we encounter, using the above recursiverelation, may be needed several times to solve the problem - we do notwish to recalculate these results. So... store the subproblem results.

This is typical of dynamic programming - we construct an array ofsolutions to subproblems:

sum = 1 2 3 4 5 6 7 8 9 10 11coin type 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞ ∞coin types 1,2 1 2 3 4 5 6 7 8 1 2 3coin types 1,2,3 1 2 3 4 1 2 3 4 1 2 3coin types 1,2,3,4 1 2 3 4 1 1 2 3 1 2 2

Answer: Using all 4 coin types, 2 coins is the minimum number needed tomake a sum of 11.

Giles Reger Lecture 7 March 2019 6 / 18

Page 27: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

What is Happening?

Dynamic programming is a bottom-up method – we solve all smallerproblems first then combine them to solve the given problem.

Question: How efficient is this dynamic programming solution? What is itstime complexity?

The main factor is the size of the table of subproblem results. Thus for Ncoin types, and a value required of V , the table size is N × V .

For each item we need to scan through the previous row so we get N ×V 2.

Notice that some subproblem results are not required for the final solution- but this is not easy to use as a reduction strategy: it is difficult to predictwhat might be needed.

Giles Reger Lecture 7 March 2019 7 / 18

Page 28: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

What is Happening?

Dynamic programming is a bottom-up method – we solve all smallerproblems first then combine them to solve the given problem.

Question: How efficient is this dynamic programming solution? What is itstime complexity?

The main factor is the size of the table of subproblem results. Thus for Ncoin types, and a value required of V , the table size is N × V .

For each item we need to scan through the previous row so we get N ×V 2.

Notice that some subproblem results are not required for the final solution- but this is not easy to use as a reduction strategy: it is difficult to predictwhat might be needed.

Giles Reger Lecture 7 March 2019 7 / 18

Page 29: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

What is Happening?

Dynamic programming is a bottom-up method – we solve all smallerproblems first then combine them to solve the given problem.

Question: How efficient is this dynamic programming solution? What is itstime complexity?

The main factor is the size of the table of subproblem results. Thus for Ncoin types, and a value required of V , the table size is N × V .

For each item we need to scan through the previous row so we get N ×V 2.

Notice that some subproblem results are not required for the final solution- but this is not easy to use as a reduction strategy: it is difficult to predictwhat might be needed.

Giles Reger Lecture 7 March 2019 7 / 18

Page 30: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Properties Required for Dynamic Programming

First two: optimal substructure

Simple Subproblems

The global optimization problem can be broken into subproblems withsimilar structure to the original problem. Subproblems can be defined withjust a few indices, like i , j , k , and so on.

Subproblem Optimality

An optimal solution must be a composition of optimal subproblemsolutions, using a relatively simple combining operation. A globallyoptimal solution should not contain suboptimal subproblems.

Subproblem Overlap

Unrelated subproblems contain subproblems in common.

Giles Reger Lecture 7 March 2019 8 / 18

Page 31: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

Edit (Levenshtein) Distance Problem

Given two strings s1 and s2 what are the minimum number or editoperations (insert a new symbol, delete an existing symbol, replace onesymbol by another) that can be applied to s1 to turn it into s2.

Giles Reger Lecture 7 March 2019 9 / 18

Page 32: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

Edit (Levenshtein) Distance Problem

Given two strings s1 and s2 what are the minimum number or editoperations (insert a new symbol, delete an existing symbol, replace onesymbol by another) that can be applied to s1 to turn it into s2.

Example: The Edit distance between ”kitten” and ”sitting” is 3.

kitten → sitten (substitution of ”s” for ”k”)

sitten → sittin (substitution of ”i” for ”e”)

sittin → sitting (insertion of ”g” at the end).

Giles Reger Lecture 7 March 2019 9 / 18

Page 33: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

Edit (Levenshtein) Distance Problem

Given two strings s1 and s2 what are the minimum number or editoperations (insert a new symbol, delete an existing symbol, replace onesymbol by another) that can be applied to s1 to turn it into s2.

Simple subproblems - define edit distance between as1 and bs2 interms of distance between of distance between s1 and s2

Subproblem optimality - can show by contradiction

Subproblem overlap - what if we do insertion+deletion ordeletion+insertion? Problem contains lots of symmetry

Giles Reger Lecture 7 March 2019 9 / 18

Page 34: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

The edit distance between strings s1 and s2 is distance(s1, s2), defined as

distance(s1, ε) = |s1|distance(ε, s2) = |s2|

distance(as1, bs2) = min

distance(s1, bs2) + 1distance(as1, s2) + 1distance(s1, s2) + 1 if a 6= bdistance(s1, s2) if a = b

Example:

distance(cat, hat) = min

distance(at, hat) + 1,distance(cat, at) + 1,distance(at, at) + 1

Giles Reger Lecture 7 March 2019 10 / 18

Page 35: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

The edit distance between strings s1 and s2 is distance(s1, s2), defined as

distance(s1, ε) = |s1|distance(ε, s2) = |s2|

distance(as1, bs2) = min

distance(s1, bs2) + 1distance(as1, s2) + 1distance(s1, s2) + 1 if a 6= bdistance(s1, s2) if a = b

Example:

distance(cat, hat) = min

distance(at, hat) + 1,distance(cat, at) + 1,distance(at, at) + 1

Giles Reger Lecture 7 March 2019 10 / 18

Page 36: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

The edit distance between strings s1 and s2 is distance(s1, s2), defined as

distance(s1, ε) = |s1|distance(ε, s2) = |s2|

distance(as1, bs2) = min

distance(s1, bs2) + 1distance(as1, s2) + 1distance(s1, s2) + 1 if a 6= bdistance(s1, s2) if a = b

Example:

distance(cat, hat) = min

min

distance(t, hat) + 1,distance(at, at) + 1,distance(t, at) + 1

+ 1

min

distance(at, at) + 1,distance(cat, t) + 1,distance(at, t) + 1

+ 1

min

distance(t, at) + 1,distance(at, t) + 1,distance(t, t)

+ 1

Giles Reger Lecture 7 March 2019 10 / 18

Page 37: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

s1s2

D(i , j) = min

D(i − 1, j) + 1D(i , j − 1) + 1D(i − 1, j − 1) + 1 if s1[i ] 6= s2[j ]D(i − 1, j − 1) if s1[i ] = s2[j ]

Giles Reger Lecture 7 March 2019 11 / 18

Page 38: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

s1s2

D(i , j) = min

D(i − 1, j) + 1D(i , j − 1) + 1D(i − 1, j − 1) + 1 if s1[i ] 6= s2[j ]D(i − 1, j − 1) if s1[i ] = s2[j ]

Giles Reger Lecture 7 March 2019 11 / 18

Page 39: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

s1s2

D(i , j) = min

D(i − 1, j) + 1D(i , j − 1) + 1D(i − 1, j − 1) + 1 if s1[i ] 6= s2[j ]D(i − 1, j − 1) if s1[i ] = s2[j ]

Giles Reger Lecture 7 March 2019 11 / 18

Page 40: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

s1s2

D(i , j) = min

D(i − 1, j) + 1D(i , j − 1) + 1D(i − 1, j − 1) + 1 if s1[i ] 6= s2[j ]D(i − 1, j − 1) if s1[i ] = s2[j ]

Giles Reger Lecture 7 March 2019 11 / 18

Page 41: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

s1s2

D(i , j) = min

D(i − 1, j) + 1D(i , j − 1) + 1D(i − 1, j − 1) + 1 if s1[i ] 6= s2[j ]D(i − 1, j − 1) if s1[i ] = s2[j ]

Giles Reger Lecture 7 March 2019 11 / 18

Page 42: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

s1s2

D(i , j) = min

D(i − 1, j) + 1D(i , j − 1) + 1D(i − 1, j − 1) + 1 if s1[i ] 6= s2[j ]D(i − 1, j − 1) if s1[i ] = s2[j ]

Giles Reger Lecture 7 March 2019 11 / 18

Page 43: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

c a t

h

a

t

Complexity is clearly |s1| × |s2|. This is also the space complexity.

Giles Reger Lecture 7 March 2019 12 / 18

Page 44: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

c a t

h 1

a

t

Complexity is clearly |s1| × |s2|. This is also the space complexity.

Giles Reger Lecture 7 March 2019 12 / 18

Page 45: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

c a t

h 1 2

a 2

t

Complexity is clearly |s1| × |s2|. This is also the space complexity.

Giles Reger Lecture 7 March 2019 12 / 18

Page 46: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

c a t

h 1 2 3

a 2 1

t 3

Complexity is clearly |s1| × |s2|. This is also the space complexity.

Giles Reger Lecture 7 March 2019 12 / 18

Page 47: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

c a t

h 1 2 3

a 2 1 2

t 3 2

Complexity is clearly |s1| × |s2|. This is also the space complexity.

Giles Reger Lecture 7 March 2019 12 / 18

Page 48: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

c a t

h 1 2 3

a 2 1 2

t 3 2 1

Complexity is clearly |s1| × |s2|. This is also the space complexity.

Giles Reger Lecture 7 March 2019 12 / 18

Page 49: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Edit Distance

c a t

h 1 2 3

a 2 1 2

t 3 2 1

Complexity is clearly |s1| × |s2|. This is also the space complexity.

Giles Reger Lecture 7 March 2019 12 / 18

Page 50: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Travelling Salesman

Travelling Salesman Problem

Given a graph 〈V ,E ,W 〉 find a path of minimum weight that visits allvertices

This is an NP-complete problem.

There is a dynamic programming solution (but still exponential of course)

Giles Reger Lecture 7 March 2019 13 / 18

Page 51: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Travelling Salesman

Assume nodes numbered 1, . . . , n. Define D(S , i) to be a minimum pathstarting at 1 and ending at i visiting all nodes in S defined recursively as

D({i}, i) = W (1, i)D(S , i) = minx∈S−i (D(S − i , x) + W (x , i))

e.g. to find distance from 1 to i in S first find x ∈ S with x 6= i thatwhose path from 1 to x combined with the edge from x to i is minimum.

Simple subproblems - simpler

Subproblem optimality - have the recursive definition

Subproblem overlap - all larger sets depend on all smaller subsets

Giles Reger Lecture 7 March 2019 14 / 18

Page 52: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Travelling Salesman

Assume nodes numbered 1, . . . , n. Define D(S , i) to be a minimum pathstarting at 1 and ending at i visiting all nodes in S defined recursively as

D({i}, i) = W (1, i)D(S , i) = minx∈S−i (D(S − i , x) + W (x , i))

e.g. to find distance from 1 to i in S first find x ∈ S with x 6= i thatwhose path from 1 to x combined with the edge from x to i is minimum.Iterate through larger subsets of V as D(S , i) always defined in terms ofsmaller subsets. Solution is D(V − 1, 1). Held-Karp algorithm.

Complexity is still the size of the table but how big is it?

Index columns by states and rows by ?

Giles Reger Lecture 7 March 2019 14 / 18

Page 53: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Travelling Salesman

Assume nodes numbered 1, . . . , n. Define D(S , i) to be a minimum pathstarting at 1 and ending at i visiting all nodes in S defined recursively as

D({i}, i) = W (1, i)D(S , i) = minx∈S−i (D(S − i , x) + W (x , i))

e.g. to find distance from 1 to i in S first find x ∈ S with x 6= i thatwhose path from 1 to x combined with the edge from x to i is minimum.Iterate through larger subsets of V as D(S , i) always defined in terms ofsmaller subsets. Solution is D(V − 1, 1). Held-Karp algorithm.

Complexity is still the size of the table but how big is it?

Index columns by states and rows by subets of V

Giles Reger Lecture 7 March 2019 14 / 18

Page 54: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Travelling Salesman

Assume nodes numbered 1, . . . , n. Define D(S , i) to be a minimum pathstarting at 1 and ending at i visiting all nodes in S defined recursively as

D({i}, i) = W (1, i)D(S , i) = minx∈S−i (D(S − i , x) + W (x , i))

e.g. to find distance from 1 to i in S first find x ∈ S with x 6= i thatwhose path from 1 to x combined with the edge from x to i is minimum.Iterate through larger subsets of V as D(S , i) always defined in terms ofsmaller subsets. Solution is D(V − 1, 1). Held-Karp algorithm.

Complexity is still the size of the table but how big is it?

Index columns by states and rows by subets of V . . . so get (n × 2n)× n

Giles Reger Lecture 7 March 2019 14 / 18

Page 55: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Longest Common Subsequence

A subsequence of a string/list is obtained by dropping some elements. Asubsequence differs from a substring/sublist as the resulting elements donot need to be next to each other in the original.

Longest Common Subsequence Problem

Given two sequences find the longest common subsequence each themaximum sequence that is a subsequence of both original sequences.

Can define recursively over prefixes.

But does it look familiar?

Giles Reger Lecture 7 March 2019 15 / 18

Page 56: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Another Example

Does anybody recognise this equation?

COMP11212 Fundamentals of Computation – Part I

• or go from state j to state k (using only states with number at most k�1 in between), returnto state k an arbitrary number of times, and then go from state k to state i using only stateswith number at most k � 1 in between.

Hence we have

Lkj!i = Lk�1

j!i [ Lk�1j!k · (Lk�1

k!k )⇤ · Lk�1k!i .

We note that if j = k or i = k then in the above expression only two di↵erent languages appear,namely

if j = k then Lk�1j!i = Lk�1

k!i and Lk�1j!k = Lk�1

k!k

andif i = k then Lk�1

j!i = Lk�1j!k and Lk�1

k!i = Lk�1k!k .

Thus we get slightly simpler expressions (compare L0!2 in the above example):

Ljj!i = (Lj�1

j!j )⇤Lj�1j!i

Lij!i = Li�1

j!i (Li�1i!i )⇤

Because the number of states involved in the paths in questions goes down the languages onthe right hand side of the equality are easier to calculate. If we have to we can keep applyingthe above equality until we have reached languages where the only state allowed on the way is astate with number below 0—that is, no state at all. Of course the overall expression we have willbecome longer and longer, so it pays to read o↵ languages from the automaton as soon as that ispossible.

Once we have reached paths which are not allowed to use any other states we have the following.For j = i:

L�1i!i =

8<:

{✏} if there is no transition from i to i{✏, x1, x2, . . . xl} if the xi are all the labels on the

transition from i to itself.

For j 6= i:

L�1j!i =

8<:

; if there is no transition from j to i{x1, x2, . . . xl} if these are all the labels on the

transition from j to i.

There are a number of equalities that allow us to simplify the expression we obtain in this way.

• ; [ L = L = L [ ;;

• ;⇤ = {✏} = {✏}⇤;

• ; · L = ; = L · ;;

• {✏} · L = L = L · {✏}

• ({✏} [ L)⇤ = L⇤ = (L [ {✏})⇤.

Once we have an expression consisting entirely of the simple languages of the form L�1j!i we

can insert regular expressions generating these, and use the rules from page 17 to get one regularexpression for the overall language.

The advantage of having a general algorithm that solves the problem is that one can now writea program implementing it that will work for all automata, even large ones.

For the marked exercises below, you should use the algorithm as described – even if it’s “obvi-ous” what the pattern might be.

45 2018-01-19

Translation from finite state automata to regular expressions can becomputed using dynamic programming

Giles Reger Lecture 7 March 2019 16 / 18

Page 57: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Another Example

Does anybody recognise this equation?

COMP11212 Fundamentals of Computation – Part I

• or go from state j to state k (using only states with number at most k�1 in between), returnto state k an arbitrary number of times, and then go from state k to state i using only stateswith number at most k � 1 in between.

Hence we have

Lkj!i = Lk�1

j!i [ Lk�1j!k · (Lk�1

k!k )⇤ · Lk�1k!i .

We note that if j = k or i = k then in the above expression only two di↵erent languages appear,namely

if j = k then Lk�1j!i = Lk�1

k!i and Lk�1j!k = Lk�1

k!k

andif i = k then Lk�1

j!i = Lk�1j!k and Lk�1

k!i = Lk�1k!k .

Thus we get slightly simpler expressions (compare L0!2 in the above example):

Ljj!i = (Lj�1

j!j )⇤Lj�1j!i

Lij!i = Li�1

j!i (Li�1i!i )⇤

Because the number of states involved in the paths in questions goes down the languages onthe right hand side of the equality are easier to calculate. If we have to we can keep applyingthe above equality until we have reached languages where the only state allowed on the way is astate with number below 0—that is, no state at all. Of course the overall expression we have willbecome longer and longer, so it pays to read o↵ languages from the automaton as soon as that ispossible.

Once we have reached paths which are not allowed to use any other states we have the following.For j = i:

L�1i!i =

8<:

{✏} if there is no transition from i to i{✏, x1, x2, . . . xl} if the xi are all the labels on the

transition from i to itself.

For j 6= i:

L�1j!i =

8<:

; if there is no transition from j to i{x1, x2, . . . xl} if these are all the labels on the

transition from j to i.

There are a number of equalities that allow us to simplify the expression we obtain in this way.

• ; [ L = L = L [ ;;

• ;⇤ = {✏} = {✏}⇤;

• ; · L = ; = L · ;;

• {✏} · L = L = L · {✏}

• ({✏} [ L)⇤ = L⇤ = (L [ {✏})⇤.

Once we have an expression consisting entirely of the simple languages of the form L�1j!i we

can insert regular expressions generating these, and use the rules from page 17 to get one regularexpression for the overall language.

The advantage of having a general algorithm that solves the problem is that one can now writea program implementing it that will work for all automata, even large ones.

For the marked exercises below, you should use the algorithm as described – even if it’s “obvi-ous” what the pattern might be.

45 2018-01-19

Translation from finite state automata to regular expressions can becomputed using dynamic programming

Giles Reger Lecture 7 March 2019 16 / 18

Page 58: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Not A Drinking Game

Your friends suggest a game that is not a drinking game. They place arandom drink on each square of a Chess Board. Each drink contains adifferent amount of liquid. You place a Queen on (1, 1) and move it to(8, 8) whilst drinking every drink you land on. You know the amount ofliquid on each square, given by a handy function d(i , j). The winner is theperson who drinks the least liquid.

1 2 3 4 5 6 7 812345678

Giles Reger Lecture 7 March 2019 17 / 18

Page 59: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Not A Drinking Game

Your friends suggest a game that is not a drinking game. They place arandom drink on each square of a Chess Board. Each drink contains adifferent amount of liquid. You place a Queen on (1, 1) and move it to(8, 8) whilst drinking every drink you land on. You know the amount ofliquid on each square, given by a handy function d(i , j). The winner is theperson who drinks the least liquid.

1 2 3 4 5 6 7 812345678

Giles Reger Lecture 7 March 2019 17 / 18

Page 60: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Other Applications

A few examples of optimisation problems with dynamic programmingsolutions

Some path-finding algorithms use dynamic programming, for exampleFloyd’s algorithm for the all-nodes shortest path problem.

Some text similarity tests: For example, longest common subsequence.

Knapsack problems: The 0/1 Knapsack problem can be solved usingdynamic programming.

Constructing optimal search trees.

Some travelling salesperson problems have dynamic programmingsolutions.

Genome matching and protein-chain matching use dynamicprogramming algorithms - invited lecture.

Giles Reger Lecture 7 March 2019 18 / 18

Page 61: Lecture 6 Algorithmic Techniques Part 2 - COMP26120syllabus.cs.manchester.ac.uk/ugt/COMP26120/2020slides/dynamic.p… · QuestionHow do we solve the general case of the coin problem?

Dynamic Programming: Other Applications

A few examples of optimisation problems with dynamic programmingsolutions

Some path-finding algorithms use dynamic programming, for exampleFloyd’s algorithm for the all-nodes shortest path problem.

Some text similarity tests: For example, longest common subsequence.

Knapsack problems: The 0/1 Knapsack problem can be solvedusing dynamic programming.

Constructing optimal search trees.

Some travelling salesperson problems have dynamic programmingsolutions.

Genome matching and protein-chain matching use dynamicprogramming algorithms - invited lecture.

Giles Reger Lecture 7 March 2019 18 / 18