Download presentation
Presentation is loading. Please wait.
Published byElla Ferguson Modified over 9 years ago
1
Recursive Back Tracking & Dynamic Programming Lecture 7
2
Optimization Problems An important and practical class of computational problems. For most of these, the best known algorithm runs in exponential time. Industry would pay dearly to have faster algorithms. Heuristics Some have quick Greedy or Dynamic Programming algorithms For the rest, Recursive Back Tracking is the best option.
3
Ingredients: Instances: The possible inputs to the problem. Eg. Solutions for Instance: Each instance has an exponentially large set of solutions. Eg. Paths from s to t Cost of Solution: Each solution has an easy to compute cost or value. Eg. Weight of path Optimization Problems
4
Specification of an Optimization Problem Preconditions: The input is one instance. Postconditions: The output is one of the valid solutions for this instance with optimal cost. (minimum or maximum) Eg. A minimum weight path The solution might not be unique. Be Clear About These Ingredients Optimization Problems
5
Recursive Back Tracking Suppose, I am searching a maze and come to a fork. ? ?? ? ?
6
Recursive Back Tracking Suppose, I am searching a maze and come to a fork. ? ?? ? ? Which way should I go?
7
Recursive Back Tracking ? ?? ? ? Idea: Try all options and keep the best!
8
Recursive Back Tracking ? ?? ? ? Idea: Try all options and keep the best!
9
Recursive Back Tracking ? ?? ? Idea: Try all options and keep the best!
10
Recursive Back Tracking ? ?? ? Idea: Try all options and keep the best!
11
Recursive Back Tracking ? ?? ? Idea: Try all options and keep the best!
12
Recursive Back Tracking ? ?? Idea: Try all options and keep the best!
13
Recursive Back Tracking ? ?? Idea: Try all options and keep the best!
14
Help From Friends (Recursion) Too much for me to keep track of. ? ?? ? ?
15
Help From Friends (Recursion) For each option, a friend gets the best answer for this option. Too much for me to keep track of.
16
Help From Friends (Recursion) I collect the info and determine which option leads to best answer. This is my answer!
17
Help From Friends (Recursion) Don't worry about your what your friend does but he will have his own forks to deal with.
18
Best of Best Suppose I am searching for the best animal.
19
Best of Best Suppose I am searching for the best animal. A recursive algorithm breaks the search into smaller searches.
20
Best of Best First friend gives me the best bird.
21
Best of Best The second friend gives me the best mammal.
22
Best of Best The third friend gives me the best fish.
23
Best of Best I take the best of these best as my answer.
24
Best of Best Don't worry about your what your friend does but he will have his own forks to deal with.
25
Tree of recursive stackframes ~ taxonomy tree that classifies animals ~ tree of 20 questions Each solution is identified with a leaf. Recursive Back Tracking
26
Depth-first search traversal of classification tree. Some friend looks at each animal. Recursive Back Tracking
27
Time? Recursive Back Tracking
28
Same as the brute force algorithm that simply compares the animals. Time? Recursive Back Tracking
29
Speeding Up the Time But sometimes we can exploit the structure to speed up the algorithm.
30
Speeding Up the Time Perhaps because these solutions are not valid or not highly valued. Sometimes entire an branch can be pruned off.
31
Speeding Up the Time Sometimes entire an branch can be pruned off. Or because there is at least one optimal solution else where in the tree. (Similar to Greedy algorithms)
32
Speeding Up the Time A Greedy algorithm prunes off all branches except the one that looks best.
33
Remembers the solutions for the subinstances So that if ever a sufficiently similar subinstance needs to be solved, the same answer can be used. This effectively prunes off this later branch of the classification tree. Speeding Up the Time Memoization
34
Fibonacci Example Time?
35
Fibonacci Example Time: Exponential Waist time redoing work
36
Memoization
37
The algorithm determines the set of subinstance that need to be solved. The algorithm no longer traverses recursively through the classification tree, No longer keeps track of which friends are waiting for answers from which friends But simply solves each of the required subinstance in smallest to largest order. Speeding Up the Time Dynamic Programming takes this idea one step further.
38
Dynamic Programming First determine the complete set of subinstances Compute them in an order such that no friend must wait. {100, 99, 98,…, 0} 0 1 Smallest to largest
39
Dynamic Programming Fill out a table containing an optimal solution for each subinstance. 0, 1, 2, 3, 4, 5, …. 99, 100 0 1 11235 0 2.19 × 10 20 3.54 × 10 20
40
Dynamic Programming
41
How to Classify Whether the algorithm can be sped up, depends on the structure of the recursive tree, which depends on. how the maze forks how the animals are classified.
42
How to Classify ? ?? ? ? Algorithm designer chooses how to classify the animals. The forks in the maze fixed by the problem.
43
"Little Bird" Abstraction Suppose I am searching for the best animal. I ask the little bird "Is the best animal, a bird, mammal, reptile, or fish?" She tells me mammal. I ask my friend for the best mammal. Trusting the little bird and the friend, I give this as the best animal. Purpose: An abstraction from which it is easier to focus on the difficult issues. Classifying solutions Forming sub-instance for friend (It is up to you whether or not to use it)
44
"Little Bird" Abstraction A creative part of designing a recursive backtracking algorithm is to decide how to classify the solutions. I abstract this task by asking a little bird a question. "Is the best animal, a bird, mammal, or fish?" Not having a little bird, we must try all the possible answers.
45
But it can be fun to temporarily pretend that the little bird gives us the correct answer. "Little Bird" Abstraction
46
Recall: Non-deterministic Finite Automata Non-deterministic Turing Machine 0 The little bird is a little higher power, answering a little question about an optimal solution. These have a higher power to tell them which way to go. (It is up to you whether or not to use it)
47
Recursive Back Tracking
49
Search Graph For Best Path We use it because it nicely demonstrates the concepts in a graphical way.
50
Search Graph For Best Path An instance (input) consists of. G is a weighted directed layered graph s source node t sink node
51
Search Graph For Best Path An instance (input) consists of. The cost of a solution is the sum of the weights. 2+6+3+7=18 A solution for an instance is a path from s to t. The goal is to find a path with minimum total cost. 4+2+1+5=12
52
Search Graph For Best Path An instance (input) consists of. The cost of a solution is the sum of the weights. A solution for an instance is a path from s to t. The goal is to find a path with minimum total cost. The greedy algorithm? Does not work! Taking the best first edge.
53
Local vs Global Considerations We are able to make local observations and choices. –Eg. Which edge out of s is cheapest? But it is hard to see the global consequences –Which path is the overall cheapest? Sometimes a local initial sacrifice can globally lead to a better overall solution.
54
Brute Force Algorithm But there may be an exponential number of paths! Try all paths, return the best.
55
Bird & Friend Algorithm I classify paths by asking the bird a question about the best path. Coming up with what question to ask is one of the hardest parts of designing the algorithm
56
Bird & Friend Algorithm asking the bird a question about the best path. "Which edge should we take first?" I classify paths by Bird may answers.
57
Bird & Friend Algorithm When the bird answers, I find the best consistent answer by asking a friend, "Which is the best path starting with ?"
58
Bird & Friend Algorithm I find the best consistent answer by asking a friend, "Which is the best path starting with ?" When the bird answers, He answers path with weight 13. Class
59
Bird & Friend Algorithm When the bird answers. Friend answers path with weight 13. My answer is? Class
60
Bird & Friend Algorithm If I trust the little bird and the friend, I give this as the best path. When the bird answers. Friend answers path with weight 13. Class
61
Best of Best Algorithm If I do not trust the bird, Class
62
Best of Best Algorithm If I do not trust the bird, I try all the options.
63
Best of Best Algorithm If I do not trust the bird, I try all the options. One friend gives me the best path starting with.
64
Best of Best Algorithm If I do not trust the bird, I try all the options. One friend gives me the best path starting with.
65
Best of Best Algorithm If I do not trust the bird, I try all the options. One friend gives me the best path starting with.
66
Best of Best Algorithm If I do not trust the bird, I try all the options. One friend gives me the best path starting with.
67
Best of Best Algorithm At least one of these four paths must be an over all best path.
68
Best of Best Algorithm At least one of these four paths must be an over all best path. I give the best of the best as the best path.
69
"Little Bird" Abstraction A second creative part of designing a recursive backtracking algorithm is.... Trusting in little bird’s answer, helps us focus on the related sub-instance. how to express the problem the friend’s question as a sub-instance.
70
Recursive Back Tracking We will explain this later.
71
Recursive Back Tracking
72
SubInstance for Friend But? I ask my friend, "Which is the best path from s to t that starts with edge ?" My problem is, "Which is the best path from s to t?"
73
SubInstance for Friend A friend is really a recursive call to the same search problem. I must express my question to him as a sub-instance. I ask my friend, "Which is the best path from s to t that starts with edge ?" My problem is, "Which is the best path from s to t?" Which?
74
SubInstance for Friend "Which is the best path from v 1 to t?" I tack on the edge of weight 3, making the path with weight 10+3=13. Friend answers with weight 10. If I trust the little bird, I take step along edge and ask my friend,
75
SubInstance for Friend Even if the bird was wrong, this work is not wasted. This is best path from amongst those starting with.
76
Recursive Back Tracking
77
SubInstance for Friend … and the best path from amongst those starting with.
78
SubInstance for Friend … and the best path from amongst those starting with.
79
SubInstance for Friend … and the best path from amongst those starting with.
80
Best of Best Algorithm … and the best of the best.
81
Search Graph For Best Path
82
Best of Best Time?
83
Best of Best Same as the brute force algorithm that simply compares the animals/paths. Time?
84
Same as Brute Force Algorithm But there may be an exponential number of paths! Try all paths, return the best.
85
Memoization
86
Exponential Time Redoing Work "Which is the best path from v 7 to t?" How many friends solve this sub instance?
87
Exponential Time Redoing Work "Which is the best path from s to t?"
88
Exponential Time Redoing Work "Which is the best path from v 1 to t?"
89
Exponential Time Redoing Work "Which is the best path from v 4 to t?"
90
Exponential Time Redoing Work "Which is the best path from v 7 to t?" There’s one.
91
Exponential Time Redoing Work "Which is the best path from s to t?"
92
Exponential Time Redoing Work "Which is the best path from v 3 to t?"
93
Exponential Time Redoing Work "Which is the best path from v 5 to t?"
94
Exponential Time Redoing Work "Which is the best path from v 7 to t?" There’s another.
95
Exponential Time Redoing Work "Which is the best path from v 7 to t?" How many friends solve this sub instance? Once for each path to v 7 Save time by only doing once. Waist time redoing work
96
Dynamic Programming First determine the complete set of subinstances No “Which is the best path from v i to v j ?” i,j
97
Dynamic Programming First determine the complete set of subinstances “Which is the best path from v i to t?” i
98
Dynamic Programming Assign one friend to each subinstances. “Which is the best path from v i to t?” i
99
Dynamic Programming Compute them in an order such that no friend must wait.
100
Dynamic Programming "Which is the best path from t to t?" Base Case Easy
101
Dynamic Programming "Which is the best path from v 8 to t?" Easy
102
Dynamic Programming "Which is the best path from v 7 to t?" Easy
103
Dynamic Programming "Which is the best path from v 6 to t?" Easy
104
Dynamic Programming "Which is the best path from v 5 to t?" Harder
105
Dynamic Programming "Which is the best path from v 5 to t?" Friend gives best path. Little bird suggests first edge
106
Dynamic Programming "Which is the best path from v 5 to t?" Friend gives best path. Little bird suggests first edge
107
Dynamic Programming "Which is the best path from v 5 to t?" Take best of best
108
Dynamic Programming "Which is the best path from v 4 to t?"
109
Dynamic Programming "Which is the best path from v 4 to t?" Friend gives best path. Little bird suggests first edge
110
Dynamic Programming "Which is the best path from v 4 to t?" Friend gives best path. Little bird suggests first edge
111
Dynamic Programming "Which is the best path from v 4 to t?" Friend gives best path. Little bird suggests first edge
112
Dynamic Programming "Which is the best path from v 4 to t?" Take best of best
113
Dynamic Programming "Which is the best path from v 3 to t?"
114
Dynamic Programming "Which is the best path from v 3 to t?" Friend gives best path. Little bird suggests first edge
115
Dynamic Programming "Which is the best path from v 3 to t?" Friend gives best path. Little bird suggests first edge
116
Dynamic Programming "Which is the best path from v 3 to t?" Take best of best
117
Dynamic Programming "Which is the best path from v 2 to t?"
118
Dynamic Programming "Which is the best path from v 2 to t?" Friend gives best path. Little bird suggests first edge
119
Dynamic Programming "Which is the best path from v 2 to t?" Friend gives best path. Little bird suggests first edge
120
Dynamic Programming "Which is the best path from v 2 to t?" Take best of best
121
Dynamic Programming "Which is the best path from v 1 to t?"
122
Dynamic Programming "Which is the best path from v 1 to t?" Friend gives best path. Little bird suggests first edge
123
Dynamic Programming "Which is the best path from v 1 to t?" Friend gives best path. Little bird suggests first edge
124
Dynamic Programming "Which is the best path from v 1 to t?" Friend gives best path. Little bird suggests first edge
125
Dynamic Programming "Which is the best path from v 1 to t?" Take best of best
126
Dynamic Programming "Which is the best path from s to t?" Original Problem
127
Dynamic Programming "Which is the best path from s to t?" Friend gives best path. Little bird suggests first edge
128
Dynamic Programming "Which is the best path from s to t?" Friend gives best path. Little bird suggests first edge
129
Dynamic Programming "Which is the best path from s to t?" Friend gives best path. Little bird suggests first edge
130
Dynamic Programming "Which is the best path from s to t?" Take best of best DONE
131
Dynamic Programming Fill out a table containing an optimal solution for each subinstance. t, v 8, v 7, v 6, v 5, …., s Great algorithm, but backwards “Which is the best path from v i to t?” Base case Original
132
Reversing
133
Determine the complete set of subinstances “Which is the best path from s to v i ?” i
134
Reversing Fill out a table containing an optimal solution for each subinstance. s, v 1, v 2, v 3, v 4, …., t “Which is the best path from s to v i ?” Base case Original
135
Dynamic Programming
136
Running Time # of SubInstances × # of Bird Answers × Time = ?
137
Communication Time optSol k = Friend k gives best path from s to v k to friend i, who adds the edge. k i Time = ? Size of path = n Time = n
138
Running Time # of SubInstances × # of Bird Answers × size of solution = n × d × n) Time = Store path costs, not paths
139
"What is cost of the best path from s to v 7 ?" Store Path Costs, not Paths
140
Friend gives cost 8 of best path. Little bird suggests last edge with weight 2. 8 Best cost via is 8+2=10. "What is cost of the best path from s to v 7 ?" Store Path Costs, not Paths
141
Friend gives cost 2 of best path. Little bird suggests last edge with weight 7. 2 Best cost via is 2+7=9. "What is cost of the best path from s to v 7 ?" Store Path Costs, not Paths
142
Friend gives cost 6 of best path. Little bird suggests last edge with weight 5. Best cost via is 6+5=11. 6 "What is cost of the best path from s to v 7 ?" Store Path Costs, not Paths
143
Take best of best "What is cost of the best path from s to v 7 ?" Store Path Costs, not Paths 9 We also learn the wise little bird’s advice. We will store this in the table too.
144
Dynamic Programming birdsAdvice[i] = k min
145
Running Time # of SubInstances × # of Bird Answers × Time = = n × d) Space = # of SubInstances × = n)
146
Find Optimal Path Previous algorithm gives: Cost of the best path from s to v i, i. Bird’s advice of last edge to v i. We run the bird-friend algorithm again, but with a reliable bird.
147
Find Optimal Path The bird gives that the last edge of the best path from s to t is.
148
Find Optimal Path The bird gives that the last edge of the best path from s to v 8 is.
149
Find Optimal Path The bird gives that the last edge of the best path from s to v 5 is.
150
Find Optimal Path The bird gives that the last edge of the best path from s to v 3 is.
151
Find Optimal Path Done!
152
Find Optimal Path
153
The Recursive Structure of the Problem i.e. for a path from s to t to be optimal, the subpath from s to v i must optimal. In order to be able to design a recursive backtracking algorithm for a computational problem, the problem needs to have a recursive structure, And finding such a subpath is a subinstance of the same computational problem.
154
Solutions, Birds, & Friends Solution can be represented by the sequence of answers solution = =,,, > or =
155
Solutions, Birds, & Friends solution = =,,, > = or to give us the first answer. answer 1 vertebrate We ask the bird We ask the friend for Which subinstance that gives me this answer? the rest. answer 2,…,answer m mammal,cat,cheetah,,
156
Solutions, Birds, & Friends solution = =,,, > = or answer 1 vertebrate Our friend asks his bird answer 2,…,answer m mammal,cat,cheetah,, to give him the next answer. answer 2 mammal,
157
The Question For the Little Bird Coming up with which question is used to classify the solutions is one of the main creative steps. Hint: Ask About A Local Property
158
If the solution is a sequence of objects, The Question For the Little Bird –``What is the first object in the sequence?'' –``What is the first edge in the path?'' s t sequence of edges Eg. If the solution is a path though a graph,
159
If the instance is a sequence of objects and a solution is a subset of these object, The Question For the Little Bird –``Is the first object of the instance included in the optimal solution?'' X = a s b e f c h d a Y = r t w a b g j c k t f d Z = ? Eg. Longest Common Subsequence problem. X = a s b e f c h d a Y = r t w a b g j c k t f d Z = a b c d –``Is the first character of either X or Y included in Z?''
160
If a solution is a binary tree of objects, The Question For the Little Bird –``What object is at the root of the tree?'' –``Which key is at the root of the tree?'' 38 25 17 421 31 2835 51 42 4049 63 5571 Eg. The Best Binary Search Tree problem,
161
Bad Questions: Asking the bird for the number of edges in the best path The Question For the Little Bird
162
You can only ask the bird a little question. –Together with your question, you provide the little bird with a list A 1, A 2, …, A K of possible answers. –The little bird answers, k [1..K]. –For an efficient algorithm, K must be small. The Question For the Little Bird – K = Eg. "What is best edge?" s t number of edges out of node s. K
163
You can only ask the bird a little question. –Together with your question, you provide the little bird with a list A 1, A 2, …, A K of possible answers. –The little bird answers, k [1..K]. –For an efficient algorithm, K must be small. The Question For the Little Bird Trying all is the Brute Force algorithm. – K = Eg. "What is an optimal solution?" # of solutions.
164
Your friends will also ask his little bird a similar question. The Question For the Little Bird –``What is the first object in the sequence?'' s t
165
Your friends will also ask his little bird a similar question. The Question For the Little Bird s t –``What is the first object in the sequence?'' –``What is the second object in the sequence?''
166
Your friends will also ask his little bird a similar question. The Question For the Little Bird –``Is the first object of the instance included?'' X = a s b e f c h d a Y = r t w a b g j c k t f d Z = a b c d
167
Your friends will also ask his little bird a similar question. The Question For the Little Bird –``Is the first object of the instance included?'' X = a s b e f c h d a Y = r t w a b g j c k t f d Z = a b c d –``Is the second object of the instance included?''
168
Your friends will also ask his little bird a similar question. The Question For the Little Bird –``What is the root?'' 38 25 17 421 31 2835 51 42 4049 63 5571
169
Your friends will also ask his little bird a similar question. The Question For the Little Bird –``What is the root in the left/right subtree?'' –``What is the root?'' 38 25 17 421 31 2835 51 42 4049 63 5571
170
The Dynamic Programming reverses the recursive backtracking algorithm. Hence, to end up with a “forward order”, we first reverse the recursive backtracking algorithm. The Question For the Little Bird –Ask the bird about the last not first object. s t
171
The Dynamic Programming reverses the recursive backtracking algorithm. Hence, to end up with a “forward order”, we first reverse the recursive backtracking algorithm. The Question For the Little Bird –``Is the last object of the instance included?'' X = u a s b e f c h d a Y = r t w a b g j c k t f d Z = a b c d
172
The Dynamic Programming reverses the recursive backtracking algorithm. Hence, to end up with a “forward order”, we first reverse the recursive backtracking algorithm. The Question For the Little Bird –We still ask about the root. 38 25 17 421 31 2835 51 42 4049 63 5571 It is not useful to ask about the leaves.
174
Find a Satisfying Assignment c = (x 3 or x 5 or x 6 ) and (x 2 or x 5 or x 7 ) and (x 3 or x 4 ) Instance: Solution: x 1 = 0, x 2 = 1, x 3 = 0, x 4 = 0, x 5 = 1, x 6 = 0, x 7 = 1 true false true
175
Find a Satisfying Assignment c = (x 3 or x 5 or x 6 ) and (x 2 or x 5 or x 7 ) and (x 3 or x 4 ) Instance: Ask the little bird Value of x 1 in an optimal solution or even better Value of x 3 in an optimal solution For now, suppose she answered x 3 = 0. We will have to try both x 3 = 0 and x 3 = 1.
176
Find a Satisfying Assignment c = (x 3 or x 5 or x 6 ) and (x 2 or x 5 or x 7 ) and (x 3 or x 4 ) Instance: true false Commit to x 3 = 0 and simplify c = (x 2 or x 5 or x 7 ) and x 4 SubInstance: Friend gives SubSolution: x 1 = 0, x 2 = 1, x 4 = 0, x 5 = 1, x 6 = 0, x 7 = 1 Our Solution: x 1 = 0, x 2 = 1, x 3 = 0, x 4 = 0, x 5 = 1, x 6 = 0, x 7 = 1
177
In the end, some friend looks at each of the 2 n assignments, Speeding Up the Time x3x3 0 1 x2x2 0 1 x1x1 0 1 x1x1 0 1 x2x2 0 1 x2x2 0 1 x1x1 0 1
178
x3x3 0 1 x2x2 0 1 x1x1 0 1 x1x1 0 1 x2x2 0 1 x2x2 0 1 x1x1 0 1 But sometimes we can prune off branches.
179
Find a Satisfying Assignment Instance: c = (x 2 or x 5 or x 7 ) and x 3 x 3 is forced to x 3 = 0 x3x3 0 1 x2x2 0 1 x1x1 0 1 x1x1 0 1 x2x2 0 1 x2x2 0 1 x1x1 0 1
180
Find a Satisfying Assignment Instance: c = (x 2 or x 5 or x 7 ) and x 3 and x 3 This is trivially unsatisfiable because x 3 can’t be both 0 and 1. x3x3 0 1 x2x2 0 1 x1x1 0 1 x1x1 0 1 x2x2 0 1 x2x2 0 1 x1x1 0 1
184
Printing Neatly An instance: text to print neatly & # chars per line “Love life man while there as we be”, 11 The goal is to to print it as “neatly” as possible. The cost: a measure of how neat, Love. life.. man.while.. there…… as we be… 11 A solution: # of words to put on each line. few blanks on the end of each line. 22632263 3 = 8 3 = 8 3 = 216 3 = 81 259 small punishment big punishment
185
Printing Neatly An instance: text to print neatly & # chars per line “Love life man while there as we be”, 11 The goal is to to print it as “neatly” as possible. The cost: a measure of how neat. A solution: # of words to put on each line. with few blanks on the end of each line.
186
Brute Force Algorithm But there may be an exponential number of ways to! Try all ways to print, return the best. love.life.. man…….. love……. life.man… love……. life.man… love.life.. man……..
187
Bird & Friend Algorithm I classify paths by asking the bird a question about the best path. Coming up with what question to ask is one of the hardest parts of designing the algorithm
188
Bird & Friend Algorithm asking the bird a question about the best way. “How many words on the last line?" I classify ways to print by Bird may answers 3 words.
189
When the bird answers 3 words, I find the best consistent answer by asking a friend, "Which is the best way to print ending in 3 words?" This is my answer
190
When the bird answers 3 words, I find the best consistent answer by asking a friend, "Which is the best way to print the remaining n-3 words?" SubInstance for Friend I tack on the last three words
191
When the bird answers 3 words, SubInstance for Friend Even if the bird was wrong, this work is not wasted. This is best way to print from amongst those ending in 3 words.
192
When the bird answers words, SubInstance for Friend 12345 Try all bird answers and take best of best.
193
Best of Best Same as the brute force algorithm that simply compares the animals/paths. Time?
194
Memoization
195
Dynamic Programming Assign one friend to each subinstances. “Which is the best path from v i to t?” i
196
Dynamic Programming Assign one friend to each subinstances. An instance: text to print neatly & # chars per line “Love life man while there as we be”, 11 The bird answers 1 word on last line.
197
Dynamic Programming Assign one friend to each subinstances. An instance: text to print neatly & # chars per line “Love life man while there as we be”, 11 The bird answers 1 word on last line.
198
Dynamic Programming Assign one friend to each subinstances. An instance: text to print neatly & # chars per line “Love life man while there as we be”, 11 The bird answers 1 word on last line.
199
Dynamic Programming Assign one friend to each subinstances. An instance: text to print neatly & # chars per line “Love life man while there as we be”, 11 A subinstance for each prefix of the text.
200
Dynamic Programming Fill out a table containing an optimal solution for each subinstance. “Which is the best printing of first i words?” Base case Original
201
“Love life man while there as we be”, 11 Love. life.. man.while.. there.as.we be………
202
“Love life man while there as we be”, 11 Love. life.. man.while.. there.as… we.be……
203
“Love life man while there as we be”, 11 Love. life.. man.while.. there…… as.we.be…
204
“Love life man while there as we be”, 11 there.as.we.be
205
“Love life man while there as we be”, 11 Choose best of the best. Tried all bird answers.
206
Choose best of the best. “Love life man while there as we be”, 11
208
Running Time Time = # of SubInstances × # of Bird Answers = n × n) Space = # of SubInstances = n)
209
Find Optimal Path Previous algorithm gives cost and bird’s advice. We run the bird-friend algorithm again, but with a reliable bird.
210
“Love life man while there as we be”, 11 <2 Love. life..,1 there……,2 man.while..,3> as we be…
211
If the instance is a sequence of objects and a solution is a subset of these object, Longest Common Subsequence problem –``Is the first object of the instance included in the optimal solution?'' X = a s b e f c h d a Y = r t w a b g j c k t f d Z = ? X = a s b e f c h d a Y = r t w a b g j c k t f d Z = a b c d –``Is the first character of either X or Y included in Z?''
212
The Dynamic Programming reverses the recursive backtracking algorithm. Hence, to end up with a “forward order”, we first reverse the recursive backtracking algorithm. The Question For the Little Bird –``Is the last object of the instance included?'' X = u a s b e f c h d a Y = r t w a b g j c k t f d Z = a b c d
216
A Greedy Dynamic Program: The Weighted Job/Activity Scheduling Problem
219
Matrix Multiplication
220
If a solution is a binary tree of objects, The Question For the Little Bird –``What object is at the root of the tree?'' –``Which key is at the root of the tree?'' 38 25 17 421 31 2835 51 42 4049 63 5571 Eg. The Best Binary Search Tree problem,
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.