Date post: | 27-Jan-2016 |
Category: |
Documents |
Upload: | nicholas-logan |
View: | 213 times |
Download: | 0 times |
2
Optimization ProblemsA Sequence of DecisionsThe Little Bird & FriendOptimal SubstructureMemoizationSet of Sub-InstancesTracing Dyn. Prog. AlgReversingCodeSpeeding Up Running TimeMultiple Opt SolutionsReviewQuestion for Little BirdReview & Don'ts
Bellman FordBest PathPrinting NeatlyLongest Common SubsequenceKnapsack ProblemThe Event Scheduling ProblemParsingSatisfiability
Techniques Problems
3
• Consider your instance I.• Ask a little question (to the little bird) about its optimal solution.• Try all answers k.
• Knowing k about the solutionrestricts your instance to a subinstance subI.
• Ask your recursive friend for a optimal solution subsol for it. • Construct a solution optS<I,k> = subsol + k
for your instance that is the best of those consistent with the kth bird' s answer.
• Return the best of these best solutions.
Recursive Back TrackingBellman Ford
4
Specification: All Nodes Shortest-Weighted Paths • <preCond>: The input is a graph G (directed or undirected)
with edge weights (possibly negative)• <postCond>: For each u,v, find a shortest path from u to v
Stored in a matrix Dist[u,v].
b
d
cu
k
g i
v
h
40
1 10
2
15
181
2
6
8
1
230325
12
3
For a recursive algorithm, we must give our friend a smaller subinstance.How can this instance be made smaller?Remove a node? and edge?
Recursive Back TrackingBellman Ford
with ≤l edges
and integer l.
with at most l edge.
l=3
l=4
5
b
d
cu
k
g i
v
h
40
1 10
2
15
181
2
6
8
1
230325
12
3
Recursive Back TrackingBellman Ford
l=4
• Consider your instance I = u,v,l.• Ask a little question (to the little bird) about its optimal solution.
• “What node is in the middle of the path?”• She answers node k.• I ask one friend subI = u,k, l/2
and another subI = k,v, l/2• optS<I,k> = subsolu,k,l/2
+ k + subsolk,v,l/2 is the best solution for I consistent with the kth bird‘s answer.
• Try all k and return the best of these best solutions.
6
Dynamic Programming Algorithm• Given an instance I,
• Imagine running the recursive alg on it.• Determine the complete set of subI
ever given to you, your friends, their friends, …• Build a table indexed by these subI• Fill in the table in order so that nobody waits.
Recursive Back Tracking
Given graph G, find Dist[uv,l] for l =1,2,4,8,…
b
d
cu
k
g i
v
h
40
1 10
2
15
181
2
6
8
1
230325
12
3
l=4
,n
7
b
d
cu
k
g i
v
h
40
1 10
2
15
181
2
6
8
1
230325
12
3
l=4
Dynamic Programming AlgorithmLoop Invariant: For each u,v,
Dist[u,v,l] = a shortest path from u to v with ≤l edges
Exit
for l = 2,4,8,16,…,2n % Find Dist[uv,l] from Dist[u,v,l/2] for all u,v Verticies Dist[u,v,l] = Dist[u,v,l/2] for all k Verticies Dist[u,v,l] = min( Dist[u,v,l], Dist[u,k,l/2]+Dist[k,v,l/2] )
8
b
d
cu
k
g i
v
h
40
1 10
2
15
181
2
6
8
1
230325
12
3
l=4
Dynamic Programming AlgorithmLoop Invariant: For each u,v,
Dist[u,v,l] = a shortest path from u to v with ≤l edges
When l = 1, Dist[b,c,1] = 10Dist[u,v,1] = ∞
% Smallest Instancesfor all u,v Verticies if u,v Edges Dist[u,v,1] = weight[u,v] else Dist[u,v,1] = ∞
Dist[u,u,1] = 0 (sometimes useful)
9
b
d
cu
k
g i
v
h
40
1 10
2
15
181
2
6
8
1
230325
12
3
l=4
Dynamic Programming AlgorithmLoop Invariant: For each u,v,
Dist[u,v,l] = a shortest path from u to v with ≤l edges
Exit
When to exit? A simple path never uses a node more than once and so does not have more than l=n-1.
for all u,v Verticies Dist[u,v] = Dist[u,v,n]
10
Dynamic Programming Algorithm• Dealing with negative cycles.
b
d
cu
v
-5
1
2253
Dist[u,v,4] = ∞
Dist[u,v,8] = 25+3+2 = 30
Dist[u,v,9] = 25+(3+1-5)+3+2 = 29
Dist[u,v,2] = ∞
Dist[u,v,12] = 25+(3+1-5)2+3+2 = 28Dist[u,v,303] = 25+(3+1-5)300+3+2 = 25-300 = -275Dist[u,v,∞] = 25+(3+1-5)∞+3+2 = 25-300 = -∞
• There is a negative cycle ifDist[u,v,n] > Dist[u,v,2n]
% Check for negative cycles for all u,v Verticies if( Dist[u,v,2n]<Dist[u,v,n] ) Dist[u,v] = ∞
11
Dynamic Programming AlgorithmAlgorithm BellmanFord(G)% Smallest Instancesfor all u,v Verticies if u,v Edges Dist[u,v,1] = weight[u,v] else Dist[u,v,1] = ∞ for l = 2,4,8,16,…,2n % Find Dist[uv,l] from Dist[u,v,l/2] for all u,v Verticies Dist[u,v,l] = Dist[u,v,l/2] for all k Verticies Dist[u,v,l] = min( Dist[u,v,l], Dist[u,k,l/2]+Dist[k,v,l/2] )% Check for negative cycles for all u,v Verticies if( Dist[u,v,2n]==Dist[u,v,n] ) Dist[u,v] = Dist[u,v,n] else Dist[u,v] = ∞
Time = O(n3 logn)
• Don’t actually need to keep old and new values.
12
Dynamic Programming AlgorithmAlgorithm BellmanFord(G)% Smallest Instancesfor all u,v Verticies if u,v Edges Dist[u,v] = weight[u,v] else Dist[u,v] = ∞ for l = 2,4,8,16,…,2n % Find Dist[uv,l] from Dist[u,v,l/2] for all u,v Verticies for all k Verticies Dist[u,v] = min( Dist[u,v], Dist[u,k]+Dist[k,v] )% Check for negative cycles for all u,v Verticies if( changed last iteration ) Dist[u,v] = ∞
• Don’t actually need to keep old and new values.
13
Dynamic Programming
• A hard topic.• I try to provide a unified way to think of it
and a fixed set of steps to follow.• Even if you don’t get the details of the
algorithm correct, at least get the right structure. • I provide analogies (little bird) to make it
hopefully more fun & easier to follow.
14
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.
15
Ingredients:• Instances: The possible inputs to the problem. • Solutions for Instance: Each instance has an
exponentially large set of solutions. • Cost of Solution: Each solution has an easy to
compute cost or value.
Optimization Problems
16
Specification of an Optimization Problem• <preCond>: The input is one instance.• <postCond>:
The output is one of the valid solutions for this instance with optimal cost. (minimum or maximum)
• The solution might not be unique.• Be clear about these ingredients!
Optimization Problems
17
Search Graph For Best Path
We use it because it nicely demonstrates the concepts in a graphical way.
18
Search Graph For Best PathAn instance (input) consists of <G,s,t>.
G is a weighted directed layered graphs source nodet sink node
5
3
4
2 53
3 2
212
4
19
Search Graph For Best PathAn instance (input) consists of <G,s,t>.
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
20
Brute Force Algorithm
But there may be an exponential number of paths!
Try all paths, return the best.
21
An Algorithm As A Sequence of Decisions
“Which edge should we take first?”
Some how I decide <s,v3>.
I ask a question about the solution.
“Which edge do we take second?”
Some how he decides <v3,v5>.
My friend asks the next question.
“Which edge do we take third?”
Some how he decided <v5,v8>.
His friend asks the next question.
22
An Algorithm As A Sequence of Decisions
“Which edge should we take first?”I ask a question about the solution.
How do I decide?
The greedy algorithm?
Does not work!
Taking the best first edge.
23
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.
24
An Algorithm As A Sequence of Decisions
“Which edge should we take first?”I ask a question about the solution.
How do I decide?
• But let's skip this partby pretending that we have
a little bird to answer this little question.
• In reality we will try all possible first edges.
25
"Little Bird" Abstraction
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)
26
“Which edge should we take first?”
The bird answers <s,v1>.
I ask a question about the solution.
“Which edge do we take second?”
The bird answers <v1,v4>.
My friend asks the next question.
But we don’t want toworry about how our friend
solves his problem.
Little Bird & Friend Alg
27
Sub-Instance for Friend
Our instance is <G,s,t>: Find best path from s to t.Our friend is recursion• i.e. he is a smaller version of ourselves• we can trust him to give us a correct answer • as long as we give him
• a smaller instance• of the same problem.
• What sub-instance do we give him?
28
“Which is the best path from v1 to t?”
I tack on the bird’s edge making the path <s,v1,v6,t>
Friend answers <v1,v6,t> with weight 10.
If I trust the little bird, I take step along edge <s,v1> and ask my friend,
The bird answers <s,v1>.
To get my solution
with weight 10+3=13.
Little Bird & Friend Alg
29
Faulty BirdBut what if we do not have a bird that we trust?
• i.e. the best path from s to tfrom amongst those starting with <s,v1>.
This work is not wasted, because we have found
Define optS<I,k> to be: the optimum solution for instance I consistent with the kth bird' s answer.
• the best solution to our instance from amongst those consistent with this bird' s answer.
30
Faulty BirdBut what if we do not have a bird that we trust?
This work is not wasted, because we have found
In reality we will try all possible first edges, giving …..
• the best solution to our instance from amongst those consistent with this bird' s answer.
• i.e. the best path from s to tfrom amongst those starting with <s,v1>.
35
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.
Faulty Bird
36
Bird/Friend - Best of the Best
A sequence of question to a little bird about a solutionforms a tree of possible answers.
Consider our instance I.
Consider the set of solutions
37
Bird/Friend - Best of the Best
But we only care aboutthe first bird answer.
Consider our instance I.
Consider the set of solutions
The answers classifiesthe possible solutions.
Solutions consistent with the kth bird' s answer.
k
k
38
Bird/Friend - Best of the Best
Consider our instance I.
Consider the set of solutions
Solutions consistent with the kth bird' s answer.
k
Define optS<I,k> to be: the optimum solution for instance I consistent with the kth bird' s answer.Do this for each k.
optS<I,k>
39
Bird/Friend - Best of the Best
Consider our instance I.
Consider the set of solutions
Let kmax be the bird' s answergiving the best optS<I,k>.
Define optS<I,k> to be: the optimum solution for instance I consistent with the kth bird' s answer.Do this for each k.
k
optS<I,k>
kmax
optS[I] = optS<I,k > = Bestk optS<I,k > max
optS[I]
40
Bird/Friend - Best of the Best Constructing optS<I,k> : the optimum solution for instance I consistent with the kth bird' s answer.
Given my instance I.
I ask my little bird foran answer k. I ask my friend for his solution.
I combine them.
46
Get help from friendBe clear what sub-instance
you give him.
Store the solution& cost
he gives you.
49
Take the bestof the best
optSolk is a best solutionfor our instance from amongst
those consistent with the bird's
kth answer.
51
Base Cases:Instances that are too small to have smaller
instances to give to friends.
What are these?What are their
solutionsand costs?
52
i.e. for a path from s to t to be optimal,the sub-path from vi to t 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,
If shorter from vi to t. shorter to s to t.
Optimal Substructure
53
i.e. for a path from s to t to be optimal,the sub-path from vi to t must optimal.
In order to be able to design a recursive backtracking algorithm for a computational problem,
the problem needs to have an optimal substructure,
And finding such a sub-path is a sub-instance of the same computational problem.
Optimal Substructure
54
Optimal Substructure• Optimal substructure means that
– Every optimal solution to a problem contains...– ...optimal solutions to subproblems
• Optimal substructure does not mean that– If you have optimal solutions to all subproblems...– ...then you can combine any of them to get an optimal
solution to a larger problem.• Example: In Canadian coinage,
– The optimal solution to 7¢ is 5¢ + 1¢ + 1¢, and– The optimal solution to 6¢ is 5¢ + 1¢, but– The optimal solution to 13¢ is not 5¢ + 1¢ + 1¢ + 5¢ + 1¢
• But there is some way of dividing up 13¢ into subsets with optimal solutions (say, 11¢ + 2¢) that will give an optimal solution for 13¢– Hence, the making change problem exhibits optimal
substructure.
56
Longest simple path
• Consider the following graph:
• The longest simple path (path not containing a cycle) from A to D is A B C D
• However, the subpath A B is not the longest simple path from A to B (A C B is longer)
• The principle of optimality is not satisfied for this problem
• Hence, the longest simple path problem cannot be solved by a dynamic programming approach
A C D
B
42
31
1
Optimal Substructure
NP-Complete
57
Time?
I try each edge out of s.
Same as the brute force algorithm that tries each path.
A friend tries each edge out of these. A friend tries each edge out of these.
Same as Brute Force Algorithm
59
Why do all this work with birds & friends?• How else would you iterate through all paths? • But sometimes we can exploit the structure
to speed up the algorithm.
Speeding Up the Time
60
• Perhaps because these solutions are not valid or not highly valued.
Sometimes entire an branch can be pruned off.
• Or because there is at least one optimal solution elsewhere in the tree.• A Greedy algorithm prunes off all branches
except the one that looks best.
Speeding Up the Time
61
• Remembers the solutions for the sub-instances so that if ever it needs to be solved again, the answer can be used.
• This effectively prunes off this later branch of the classification tree.
Speeding Up the Time
Memoization
62
Exponential TimeRedoing Work
“Which is the best path from v7 to t?”
How many friends solve this sub-instance?
71
Exponential TimeRedoing Work
“Which is the best path from v7 to t?”
How many friends solve this sub-instance?
Once for each path to v7
Save time by only doing once.
Waste time redoing work
73
Having many friends solving this same sub-instanceis a waste of time.
We allocate one friend to the job.
Dynamic Programming
74
It is my job to learn and remember
the optSol to my sub-Instancei.e. the best path from v7 to t
Dynamic Programming
75
When I need to find the best path from v4 to t
I will ask you forthe best path from v7 to t
I will find my best pathand tell you.
Dynamic Programming
76
When I need to find the best path from v2 to t
I will ask you forthe best path from v7 to t
I remember my best pathand will tell you.
Dynamic Programming
77
When I need to find the best path from v5 to t
I will ask you forthe best path from v7 to t
I remember my best pathand will tell you.
Dynamic Programming
78
But I hate to wait for you.Recursion has a lot of overhead
Why don’t you go first?
I will find my best pathand tell you.
Dynamic ProgrammingWhen I need to find
the best path from v2 to tI will ask you for
the best path from v7 to t
Avoid waiting.
80
Set of Sub-InstancesBut what sub-instance need to be solvedand in which order?
Imagine running the recursive algorithm on it.
Determine the complete set of sub-Instances ever given to you, your friends, their friends, …
Given an instance I,
81
Guess the complete set S of sub-Instances.
“Best path from v7 to t?” Yes“Best path from v21 to t?” No
v21
v21 is not a part of ouroriginal instance.
Set of Sub-Instances
82
Guess the complete set S of sub-Instances.
“Best path from v7 to t?” Yes
“Best path from v3 to v7?” No
“Best path from v21 to t?” No
All paths considered end in t.
Set of Sub-Instances
83
Guess the complete set S of sub-Instances.
“Best path from v7 to t?” Yes
“Best path from v3 to v7?” No
“Best path from v21 to t?” No
All paths considered end in t.
Set of Sub-Instances
84
Guess the complete set S of sub-Instances.
“Best path from v7 to t?” Yes
“Best path from v3 to v7?” No
“Best path from v21 to t?” No
“Best path from vi to t?” i
Set of Sub-Instances
Yes
85
Guess the complete set S of sub-Instances is
“Best path from vi to t?” i
Set of Sub-Instances
Assign one friend to each sub-Instance.
86
Guess the complete set S of sub-Instances is
“Best path from vi to t?” i
Set of Sub-Instances
The set S of sub-Instances needs to:• include our given I
87
Guess the complete set S of sub-Instances is
“Best path from vi to t?” i
Set of Sub-Instances
• closed under “friend” operation
Integers closed under addition x,y I x+y I
sub-Instance S
subsub-Instance S
The set S of sub-Instances needs to:• include our given I
88
Guess the complete set S of sub-Instances is
“Best path from vi to t?” i
Set of Sub-Instances
• closed under “friend” operation
The set S of sub-Instances needs to:• include our given I
• each sub-Instance needs to beasked of some friend, friend, …
89
Guess the complete set S of sub-Instances is
“Best path from vi to t?” i
Set of Sub-Instances
• closed under “friend” operation
The set S of sub-Instances needs to:• include our given I
• each sub-Instance needs to beasked of some friend, friend, …
A fine set of sub-instances!
90
The complete set S of sub-Instances is
“Best path from vi to t?” i
Order to complete
• in an order such that no friend must wait.
• from “smallest” to “largest”
In what order should they go?
For this problem, the order relies on
the graph being “leveled.”
91
The complete set S of sub-Instances is
“Best path from vi to t?” i
• in an order such that no friend must wait.
• from “smallest” to “largest”
In what order should they go?
Order to complete
First
Last
Base Case easy
Instance to be solved.
97
Dynamic Programming"Which is the best path from v5 to t?"
Friend gives bestpath <v7,t>.
Little bird suggests first edge <v5,v7>
98
Dynamic Programming"Which is the best path from v5 to t?"
Friend gives bestpath <v8,t>.
Little bird suggests first edge <v5,v8>
101
Dynamic Programming"Which is the best path from v4 to t?"
Friend gives bestpath <v7,t>.
Little bird suggests first edge <v4,v6>
102
Dynamic Programming"Which is the best path from v4 to t?"
Friend gives bestpath <t,t>.
Little bird suggests first edge <v4,t>
103
Dynamic Programming"Which is the best path from v4 to t?"
Friend gives bestpath <v7,t>.
Little bird suggests first edge <v4,v7>
106
Dynamic Programming"Which is the best path from v3 to t?"
Friend gives bestpath <v5,t>.
Little bird suggests first edge <v3,v5>
107
Dynamic Programming"Which is the best path from v3 to t?"
Friend gives bestpath <v8,t>.
Little bird suggests first edge <v3,v8>
110
Dynamic Programming"Which is the best path from v2 to t?"
Friend gives bestpath <v4,t>.
Little bird suggests first edge <v2,v4>
111
Dynamic Programming"Which is the best path from v2 to t?"
Friend gives bestpath <v7,t>.
Little bird suggests first edge <v2,v7>
114
Dynamic Programming"Which is the best path from v1 to t?"
Friend gives bestpath <v3,t>.
Little bird suggests first edge <v1,v3>
115
Dynamic Programming"Which is the best path from v1 to t?"
Friend gives bestpath <v4,t>.
Little bird suggests first edge <v1,v4>
116
Dynamic Programming"Which is the best path from v1 to t?"
Friend gives bestpath <v5,t>.
Little bird suggests first edge <v1,v5>
119
Dynamic Programming"Which is the best path from s to t?"
Friend gives bestpath <v1,t>.
Little bird suggests first edge <s,v1>
120
Dynamic Programming"Which is the best path from s to t?"
Friend gives bestpath <v2,t>.
Little bird suggests first edge <s,v2>
121
Dynamic Programming"Which is the best path from s to t?"
Friend gives bestpath <v3,t>.
Little bird suggests first edge <s,v3>
122
Dynamic Programming"Which is the best path from s to t?"
Friend gives bestpath <v4,t>.
Little bird suggests first edge <s,v4>
124
Construct a table • for storing an optimal solution & cost • for each sub-instance.
Dynamic Programming
Sub-InstancesMap
Indexes
Cell of table
“Best path from vi to t?” i
i ϵ [n], i.e. for each node vi
t, v8, v7, v6, v5, …., s i
“Which is the best path from vi to t?”
125
Fill out a table containing an optimal solution for each sub-instance.
“Which is the best path from vi to t?”
Dynamic Programming
t, v8, v7, v6, v5, …., s Base case Original
126
Communication
optSubSol = optSol[k]
Friend k gives friend i a best path from vk to t.
Recursive BackTracking<optSubSol,optSubCost> = LeveledGraph(<G,vk,t>)
optSol[k] = optSolmin
return(optSolmin,optCostmin) ,
Dynamic Programmingk
i k
i
k
i
optSolk = <vi,vk> + optSol[k]
129
Dynamic Programsdo not recurse making the instance smaller
and smaller.
Instead, it up frontdetermines the set Sof all sub-instances
that ever need to be solved.
Be clear what sub-instances are.
130
Be clear what sub-instances are.
How are they indexed?
Tables indexed by these sub-instances
store an optimal solution and it’s cost.
131
The set Sof sub-instancesare solved from
smallest to largestso that no body waits.
Base Cases:Instances that are too small to have smaller
instances to give to friends.
They get solved firstand their solutions
stored.
132
Then we iterate through the remaining
sub-instances.
From smallest to largest.
Each gets solvedand their solutions
stored.
133
Consider yourselfto be a friend
working on oneof these.
Be clear which sub-instance is yours.
Solve this as you did before.
138
Instead of recursing,we simply look
in the table for the solution.
Because his instance is smaller, he has
already solved it and stored sol in the table.
Get help from friendk
kk k
141
Take the bestof the best
optSol<i,k> is a best solution for our instance subI[i]
from amongstthose consistent with the bird's
kth answer.
143
Base Cases:Instances that are too small to have smaller
instances to give to friends.
Is this code correct?
144
Dynamic Programsdo not recurse making the instance smaller
and smaller.Hence, lets not worry about our instance I
being a base case.
145
But there is a tableof subinstances
that must be solved.
Some of these will bebase cases
and their solutionsmust be stored
in the table.
146
But there is a tableof subinstances
that must be solved.
Some of these will bebase cases
and their solutionsmust be stored
in the table.
nnn n
t=
153
Reversing
Fill out a table containing an optimal solution for each sub-instance.
s, v1, v2, v3, v4, …., t
“Which is the best path from s to vi?”
Base case Original
156
Communication TimeoptSolk = <optSol[k],vi>
Friend k gives best path from s to vk
to friend i, who adds the edge <vk,vi>.
k
i
(1)qTime = ?Size of path =
(q n)Time =
(q n).
157
# of Sub-Instances× # of Bird Answers× size of solution= q(n × d × n)
Time =
Running Time
Store path costs, not paths
Space =# of Sub-Instances× (1)q= (q n)
159
Friend gives cost 8of best path <s,v4>.
Little bird suggests last edge <v4,v7> with weight 2.8
Best cost via <v4,v7> is 8+2=10.
"What is cost of the best path from s to v7?"
Store Path Costs, not Paths
160
Friend gives cost 2of best path <s,v2>.
Little bird suggests last edge <v2,v7> with weight 7.
2
Best cost via <v2,v7> is 2+7=9.
"What is cost of the best path from s to v7?"
Store Path Costs, not Paths
161
Friend gives cost 6of best path <s,v5>.
Little bird suggests last edge <v5,v7> with weight 5.
Best cost via <v5,v7> is 6+5=11.
6
"What is cost of the best path from s to v7?"
Store Path Costs, not Paths
162
Take best of best:
"What is cost of the best path from s to v7?"
Store Path Costs, not Paths
We also learn the wise little bird’s advice.We will store this in the table too.
9
2 10, 9, 11
165
Find Optimal Path
Previous algorithm gives:• Cost of the best path
from s to vi, i.• Bird’s advice of
last edge to vi.
We run the bird-friend algorithm again, but with a reliable bird.
172
Multiple Optimal Solutions
6
“Which is the last edge?”
I ask the bird:
She could give either answer.
By giving this edge she says “There exists an optimal solution consistent with this answer.”
Similar to greedy proof.
173
Multiple Optimal Solutions
6
“Which is the last edge?”
I ask the bird:
We try all the bird answers.
When we try this bird answer,
we find this best solution.When we try this bird answer,
we find this best solution.
When we take best of best, we choose between them.
174
Designing Recursive Back Tracking Algorithm• What are instances, solutions, and costs? • Given an instance I,• What question do you ask the little bird?• Given a bird answer k [K],
• What instance sub-Instance do your give your friend?• Assume he gives you optSubSol for subI.• How do you produce an optSol for I from
• the bird’s k and • the friend’s optSubSol?
• How do you determine the cost of optSol from • the bird’s k and • the cost of the friend’s optSubSol?
• Try all bird’s answers and take best of best.
Review
175
Recursive Back Tracking Algorithm
Dynamic Programming Algorithm• Given an instance I,• Imagine running the recursive alg on it.• Determine the complete set of sub-Instances
ever given to you, your friends, their friends, …• Build a table indexed by these sub-Instances• Fill in the table in order so that nobody waits.
• the cost of its optimal solution• advice given by the bird
• Run the recursive alg with bird’s advice to find the solution to your instance.
Review
176
Purpose of Little Bird: • An abstraction from which it is
easier to focus on the difficult issues. • Her answers give us a list of things to try.• Temporarily trusting the bird,
helps us focus on the remaining questionhelping us formulate sub-instance for friend.
• Coming up with which question is one of the main creative steps.• Hint: Ask about a local property • There are only so many question that you
might ask so just try them all.
The Question For the Little Bird
177
An instance: Graph, s, and t
The Question For the Little Bird
I ask the bird:
A solution: a path
s
t
“What is the first edge in the path?”
The Dynamic Programming reverses the recursive backtracking algorithm. Hence, to end up with a “forward order”,we first reverse the recursive backtracking algorithm.
178
An instance: Graph, s, and t
The Question For the Little Bird
I ask the bird:
A solution: a path
s
t
The Dynamic Programming reverses the recursive backtracking algorithm. Hence, to end up with a “forward order”,we first reverse the recursive backtracking algorithm.
“What is the last edge in the path?”
179
A good question for the bird leaves you with a good recursive sub-instance to ask your friend.
An instance: Graph, s, and t
The Question For the Little Bird
I ask the bird:
A solution: a path
s
t
“What is the last edge in the path?”
“What is the rest of the path?”
180
An instance: Graph, s, and t
The Question For the Little Bird
I ask the bird:
A solution: a path
s
t
Giving a good follow up question for your friend to ask the bird.
“What is the last edge in the path?”
“What is the second last edge in the path?”
181
• You can only ask the bird a little question.– Together with your question, you provide the little
bird with a list A1, A2, …, AK of possible answers. – The little bird answers, k [1..K]. – For an efficient algorithm, K must be small.
The Question For the Little Bird
number of edges into node t.
K
– K =
• Eg. “What is best last edge?”
s
t
182
• You can only ask the bird a little question.– Together with your question, you provide the little
bird with a list A1, A2, …, AK 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.
183
An instance: Graph, s, and t
The Question For the Little Bird
I ask the bird:
A solution: a path
s
t
“How many edges are in the path?”Bad Question: • it is not a local property• How does this help us solve the problem?• What is a good follow up question for the friend
to ask?
184
A solution: a sequence of objectsZ = a b c d
An instance: ???
The Question For the Little Bird
“What is the last object in the sequence?”
I ask the bird:
# of answers K = # of possible last objects.
I ask my friend:
“What is the rest of the solution?”
185
The Question For the Little Bird
“What is the last object in the sequence?”
I ask the bird:
An instance: a sequence of objects
X = a s b e f c h d a
A solution: a subset of these objectsZ = a b c d
X = a s b e f c h d a
# of answers K = # of possible last objects.
Is there a smaller question that we could ask?
186
The Question For the Little Bird
“Is the last object of the instance included in the optimal solution?”
I ask the bird:
An instance: a sequence of objects
X = a s b e f c h d a
A solution: a subset of these objectsZ = a b c d
# of answers K = 2, Yes or No
187
An instance: ???
The Question For the Little Bird
“What object is at the root?”
I ask the bird:
A solution: a binary tree of objects38
25
17
4 21
31
28 35
51
42
40 49
63
55 71
I ask my friend:
“What is the left sub-tree?”
I ask a second friend:
“What is the right sub-tree?”
Previous problems had one friend given a bird ans.
188
A solution: a binary tree of objects
An instance: ???
The Question For the Little Bird
“What object is at a leaf?”
I ask the bird:
38
25
17
4 21
31
28 35
51
42
40 49
63
55 71
Bad Question: • How does this help us solve the problem? • What is a good follow up question for the friend
to ask?
190
Printing NeatlyAn 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.
2263
3 = 83 = 83 = 2163 = 27 259
small punishment
big punishment
191
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........
192
Bird & Friend Algorithm
“How many words on the last line?”I ask the bird:
She may answer 3.
An instance:“Love life man while there as we be”, 11
“Which is the best way to print the remaining n-3 words?”
I ask the friend: I combine bird’s andfriend’s answers.
193
Even if the bird was wrong, this work is not wasted.
This is best way to print from amongst those ending in 3 words.
Bird & Friend AlgorithmAn instance:“Love life man while there as we be”, 11
We try the bird answers words,12345 and take best of best.
194
Time?
I try each # words on last line.
Same as the brute force algorithm that tries each path.
A friend tries # on next.
A friend tries # on next.
Same as Brute Force Algorithm
196
Set of Sub-Instances
Given an instance I,
• Imagine running the recursive algorithm on it.
• Determine the complete set of sub-Instances ever given to you, your friends, their friends, …
Determine the complete set of sub-Instances.
“Love life man while there as we be”, 11
197
Set of Sub-InstancesGuess the complete set of sub-Instances.
“Love life man while there”, 11 Yes
“Hi there”, 81 No
“man while”, 11 No
This may appear on a line,but it will never be a sub-Instance for a friend.
“Love life man while there as we be”, 11
198
Set of Sub-Instances
“Love life man while there as we be”, 11
The set of sub-Instances is the set of prefixes.
• closed under “friend” operation
sub-Instance S
The set S of sub-Instances needs to:• include our given I
“Love life man while there”, 11“Love life man while”, 11“Love life man”, 11“Love life”, 11“Love”, 11“”, 11
“Love life man while there as”, 11“Love life man while there as we”, 11
199
Set of Sub-Instances
“Love life man while there as we be”, 11
• closed under “friend” operation
sub-Instance S subsub-Instance i
The set S of sub-Instances needs to:• include our given I
The bird answers words,12345
“Love life man while there”, 11“Love life man while”, 11“Love life man”, 11“Love life”, 11“Love”, 11“”, 11
“Love life man while there as”, 11“Love life man while there as we”, 11
The set of sub-Instances is the set of prefixes.
200
Set of Sub-Instances
“Love life man while there as we be”, 11
• closed under “friend” operation
The set S of sub-Instances needs to:• include our given I
• each sub-Instance needs to beasked of some friend, friend, …
“Love life man while there”, 11“Love life man while”, 11“Love life man”, 11“Love life”, 11“Love”, 11“”, 11
“Love life man while there as”, 11“Love life man while there as we”, 11
The set of sub-Instances is the set of prefixes.
201
Set of Sub-Instances
“Love life man while there as we be”, 11“Love life man while there as we”, 11“Love life man while there as”, 11“Love life man while there”, 11
• closed under “friend” operation
The set S of sub-Instances needs to:• include our given I
• each sub-Instance needs to beasked of some friend, friend, …
The bird answers 1.
“Love life man while”, 11
A fine set of sub-instances!
The set of sub-Instances is the set of prefixes.
202
Set of Sub-Instances
“Love life man while there as we be”, 11
Base Case easyInstance to be solved.
• in an order such that no friend must wait.
• from “smallest” to “largest”
In what order should they go?
FirstLast
“Love life man while there”, 11“Love life man while”, 11“Love life man”, 11“Love life”, 11“Love”, 11“”, 11
“Love life man while there as”, 11“Love life man while there as we”, 11
The set of sub-Instances is the set of prefixes.
203
Construct a table • for storing the cost of opt sol and bird’s advice. • for each sub-instance.
Sub-InstancesMap
Indexes
Cell of table
i ϵ [n], i.e. for each word.
i
“Which is the best printing of first i words?”
The set of prefixes of words.
The Table
204
Dynamic Programming
Fill out a table containing an optimal solution for each sub-instance.
“Which is the best printing of first i words?”
Base case Original
205
“Love life man while there as we be”, 11
“Love life man while there”, 11The 5th sub-instance is
5 wordswith 4, 4, 3, 5, 5 letters.
206
“Love life man while there as we be”, 11
“Love life man while there”, 11The 5th sub-instance is
Love.life.. man.while.. there......
Its solution iswith 2,2,1 words on each line.
The bird’s advice is 1 word on last.Solution’s cost is 23 + 23 +63 = 232
207
“Love life man while there as we be”, 11
Assume the table is filled in so far.We will work to fill in the last line
214
Dynamic Programming
code always has thissame basic structure.
Amusingly,when formatting
this code, I had to fight with line breaks to get the height/width ratio
Printing Neatly.
216
Dynamic Programsdo not recurse making the instance smaller
and smaller.
Instead, it up frontdetermines the set Sof all sub-instances
that ever need to be solved.
Be clear what sub-instances are.
217
Be clear what sub-instances are.
How are they indexed?
Tables indexed by these sub-instances
store an optimal solution and it’s cost.
218
The set Sof sub-instancesare solved from
smallest to largestso that no body waits.
Base Cases:Instances that are too small to have smaller
instances to give to friends.
They get solved firstand their solutions
stored.
219
Then we iterate through the remaining
sub-instances.
From smallest to largest.
Each gets solvedand their solutions
stored.
Actually, we store thebird’s advice instead
of the solution.
220
Consider yourselfto be a friend
working on oneof these.
Be clear which sub-instance is yours.
Solve this as you did before.
225
Instead of recursing,we simply look
in the table for the solution.
Because his instance is smaller, he has
already solved it and stored sol in the table.
i-ki-k
i-k
228
Take the bestof the best
optSol<i,k> is a best solution for our instance subI[i]
from amongstthose consistent with the bird's
kth answer.
229
Store the solution toour instance subI[i]
in the table.
Actually, we store thebird’s advice instead
of the solution.
230
Base Cases:Instances that are too small to have smaller
instances to give to friends.
Is this code correct?
231
Dynamic Programsdo not recurse making the instance smaller
and smaller.Hence, lets not worry about our instance I
being a base case.
232
But there is a tableof subinstances
that must be solved.
Some of these will bebase cases
and their solutionsmust be stored
in the table.
233
But there is a tableof subinstances
that must be solved.
Some of these will bebase cases
and their solutionsmust be stored
in the table.
235
But actually,we don’t have the solution.
We must rerun it, this time with advice
from the bird.
Return the solutionand cost for the original instance.
237
Find Optimal Path
Previous algorithm gives cost and bird’s advice.
We run the bird-friend algorithm again, but with a reliable bird.
240
The goal is to find a longest common subsequence.
The cost: The length of Z.
A solution: A common subsequence.Z = a b c d
Longest Common Subsequence problemAn instance: Two strings
X = a s b e f c h d aY = r t w a b g j c k t f dX = a s b e t c h d aY = r t w a b g j c k t f d
241
Bird & Friend Algorithm
I ask the bird:
An instance:X = a s b e t c h d aY = r t w a b g j c k t f d
“Is the last character of either X or Y included in Z?”
She answers one of :• Last of X is not included• Last of Y is not included • Last of X is included• Last of Y is included• Neither are included• Both are included
242
Bird & Friend Algorithm
I ask the bird:
I ask my friend:
An instance:X = a s b e t c h d aY = r t w a b g j c k t f d
“Is the last character of either X or Y included in Z?”
She answers:• Last of X is not included
The instance:X = a s b e t c h d Y = r t w a b g j c k t f d
243
Bird & Friend Algorithm
I ask the bird:
My friend answers:
I combine bird’s andfriend’s answersand give
An instance:X = a s b e t c h d aY = r t w a b g j c k t f d
“Is the last character of either X or Y included in Z?”
She answers:• Last of X is not included
The instance:X = a s b e t c h d Y = r t w a b g j c k t f d
Z = a b c d X = a s b e t c h d Y = r t w a b g j c k t f d
the same Z.
244
Bird & Friend Algorithm
I ask the bird:
I ask my friend:
An instance:X = a s b e t c h d aY = r t w a b g j c k t f d
“Is the last character of either X or Y included in Z?”
She answers:• Last of Y is not included
The instance:X = a s b e t c h d a Y = r t w a b g j c k t f
245
Bird & Friend Algorithm
I ask the bird:
My friend answers:
I combine bird’s andfriend’s answersand give
An instance:X = a s b e t c h d aY = r t w a b g j c k t f d
“Is the last character of either X or Y included in Z?”
She answers:• Last of Y is not included
The instance:X = a s b e t c h d a Y = r t w a b g j c k t f
Z = a b c X = a s b e t c h d a Y = r t w a b g j c k t f
the same Z.
Not as good as lastbut we need to try.
246
Bird & Friend Algorithm
I ask the bird:
An instance:X = a s b e t c h dY = r t w a b g j c k d f d
“Is the last character of either X or Y included in Z?”
She answers:• Last of X and last of Y are both included
I ask my friend:The instance:
X = a s b e t c hY = r t w a b g j c k d f
Last chars equal
247
Bird & Friend Algorithm
I ask the bird:
An instance:X = a s b e t c h dY = r t w a b g j c k d f d
“Is the last character of either X or Y included in Z?”
She answers:• Last of X and last of Y are both included
The instance:X = a s b e t c hY = r t w a b g j c k d f
I combine bird’s andfriend’s answersand give
Zd = abcd.
Last chars equal
Z = a b c X = a s b e t c h Y = r t w a b g j c k d f
My friend answers:
248
Bird & Friend Algorithm
I ask the bird:
I politely tell her that she is wrong.
An instance:X = a s b e t c h d aY = r t w a b g j c k t f d
“Is the last character of either X or Y included in Z?”
She answers:• Last of X and last of Y are both included
Last chars not equal
249
Bird & Friend Algorithm
I ask the bird:
I ask my friend:
An instance:X = a s b e t c h d aY = r t w a b g j c k t f d
“Is the last character of either X or Y included in Z?”
She answers:• Last of X is included
The instance:X = a s b e t c h d Y = r t w a b g j c k t f d
250
Bird & Friend Algorithm
I ask the bird:
An instance:X = a s b e t c h d aY = r t w a b g j c k t f d
“Is the last character of either X or Y included in Z?”
She answers:• Last of X is included
I combine bird’s andfriend’s answersand give
Za = abcda.
The instance:X = a s b e t c h d Y = r t w a b g j c k t f d
Z = a b c d X = a s b e t c h d Y = r t w a b g j c k t f d
My friend answers:
Wrong
251
Bird & Friend Algorithm
I ask the bird:
I ask my friend:
An instance:X = a s b e t c h d aY = r t w a b g j c k t f d
“Is the last character of either X or Y included in Z?”
She answers:• Last of X is included
The instance:X = a s b e t c h d Y = r t w
252
Bird & Friend Algorithm
I ask the bird:
My friend answers:I combine bird’s andfriend’s answersand give
An instance:X = a s b e t c h d aY = r t w a b g j c k t f d
“Is the last character of either X or Y included in Z?”
She answers:• Last of X is included
The instance:X = a s b e t c h d Y = r t w
Z = t X = a s b e t c h d Y = r t w Za = ta.
253
Bird & Friend Algorithm
I ask the bird:
An instance:X = a s b e t c h d aY = r t w a b g j c k t f d
“Is the last character of either X or Y included in Z?”
She answers one of :• Last of X is not included• Last of Y is not included • Last of X is included• Last of Y is included• Neither are included• Both are included
Last chars not equal
Given any optSolshe needs to have
a valid answer.
Can we eliminatesome of her answers?
???
254
Bird & Friend Algorithm
I ask the bird:
An instance:X = a s b e t c h d aY = r t w a b g j c k t f d
“Is the last character of either X or Y included in Z?”
She answers one of :• Last of X is not included• Last of Y is not included • Last of X is included• Last of Y is included• Neither are included• Both are included
Last chars not equal
# of answers K = 3
255
Time?
Same as the brute force algorithm that tries each solution.
Same as Brute Force AlgorithmI try each of 3 bird ans.
My friends try 3
His friends try 3
257
Set of Sub-Instances
Given an instance I,
Determine the complete set of sub-Instances.
X = a s b e t c h d aY = r t w a b g j c k t f d• Imagine running the recursive alg on it.• Determine the complete set of sub-
Instances ever given to you, your friends, their friends…
X’ = a s b e t c Y’ = r t w a b g j c k
Is this a sub-Instance? Yes
258
Set of Sub-Instances
Given an instance I,
Determine the complete set of sub-Instances.
X = a s b e t c h d aY = r t w a b g j c k t f d• Imagine running the recursive alg on it.• Determine the complete set of sub-
Instances ever given to you, your friends, their
friends, …Is this a sub-Instance? NoX’ = b e t
Y’ = a b g j c k
259
Set of Sub-Instances
Given an instance I,
Determine the complete set of sub-Instances.
X = a s b e t c h d aY = r t w a b g j c k t f d• Imagine running the recursive alg on it.• Determine the complete set of sub-
Instances ever given to you, your friends, their
friends, …Is this a sub-Instance? Yes
X’ = x1,…xi
Y’ = y1,…,yj
i [0..|X|] j [0..|Y|]|X| × |Y| of these.
260
Set of Sub-InstancesGuess the complete set S of sub-Instances.
i [0..|X|] j [0..|Y|]
Xi = x1,…xi
Yj = y1,…,yj
The set S of sub-Instances needs to:• include our given I
Yes: i = |X| & j = |Y|
261
Set of Sub-InstancesGuess the complete set S of sub-Instances.
i [0..|X|] j [0..|Y|]
Xi = x1,…xi
Yj = y1,…,yj
The set S of sub-Instances needs to:• include our given I• closed under “friend” operation sub-Instance S subsub-Instance S
Xi = x1,…xi
Yj = y1,…,yj
S Xi-1 = x1,…xi-1 Yj = y1,…,yjXi = x1,…xi Yj-1 = y1,…,yj-1Xi-1 = x1,…xi-1 Yj-1 = y1,…,yj-1
S
262
Set of Sub-InstancesGuess the complete set S of sub-Instances.
i [0..|X|] j [0..|Y|]
Xi = x1,…xi
Yj = y1,…,yj
The set S of sub-Instances needs to:• include our given I• closed under “friend” operation
sub-Instance S subsub-Instance S• each sub-Instance needs to be
asked of some friend, friend, …We showed this.
This is a fine set of sub-Instances!
263
Construct a table • for storing the cost of opt sol and bird’s advice. • for each sub-instance.
Sub-InstancesMap
Indexes
Cell of table
i
“LCS of x1,…xi and y1,…,yj ?”
i [0..|X|] j [0..|Y|]
Xi = x1,…xi
Yj = y1,…,yj
j
The Table
266
Table
Xi = x1,…xi
Yj = y1,…,yj
sub-Instancei,j =
Xi
Yjj=
i=
Cost = length of LCS.
Optimal Solution = Longest Common Subsequence
267
Table
Xi = x1,…xi
Yj = y1,…,yj
sub-Instancei,j =
Xi
Yj
Optimal Solution = Longest Common Subsequence
Bird’s Advice• delete xi
268
Table
Xi
Yj
Optimal Solution = Longest Common Subsequence
Bird’s Advice• delete xi
• take both xi and yj
Xi = x1,…xi
Yj = y1,…,yj
sub-Instancei,j =
269
Table
Xi
Yj
Optimal Solution = Longest Common Subsequence
Bird’s Advice• delete xi
• delete yj
• take both xi and yj
Xi = x1,…xi
Yj = y1,…,yj
sub-Instancei,j =
270
Fill in Box
Xi
Yj
Fill in box• Try all bird’s ans.• delete xi
Friend’s sub-InstanceOur cost = friend’s cost5
Xi = x1,…xi
Yj = y1,…,yj
sub-Instancei,j =
271
Xi
Yj
Fill in box• Try all bird’s ans.• delete yj
Friend’s sub-InstanceOur cost = friend’s cost5
Fill in Box
Xi = x1,…xi
Yj = y1,…,yj
sub-Instancei,j =
272
Xi
Yj
Fill in box• Try all bird’s ans.• take both xi and yj
Friend’s sub-InstanceOur cost = friend’s cost +1
6
Fill in Box
Xi = x1,…xi
Yj = y1,…,yj
sub-Instancei,j =
273
Xi
Yj
Fill in box• Try all bird’s ans.• Take best of best
6
Fill in Box
Xi = x1,…xi
Yj = y1,…,yj
sub-Instancei,j =
274
Fill in Box
Xi
Yj
Fill in box• Try all bird’s ans.• delete xi
Friend’s sub-InstanceOur cost = friend’s cost4
Xi = x1,…xi
Yj = y1,…,yj
sub-Instancei,j =
275
Xi
Yj
Fill in box• Try all bird’s ans.• delete yj
Friend’s sub-InstanceOur cost = friend’s cost3
Fill in Box
Xi = x1,…xi
Yj = y1,…,yj
sub-Instancei,j =
276
Xi
Yj
Fill in box• Try all bird’s ans.• take both xi and yj
Sorry bird is wrong.Our cost = -
-
Fill in Box
Xi = x1,…xi
Yj = y1,…,yj
sub-Instancei,j =
277
Xi
Yj
Fill in box• Try all bird’s ans.• Take best of best
Fill in Box
4
Xi = x1,…xi
Yj = y1,…,yj
sub-Instancei,j =
288
Knapsack Problem
Ingredients:• Instances: The volume V of the knapsack.
The volume and price of n objects <<v1,p1>,<v2,p2>,… ,<vn,pn>>.
• Solutions: A set of objects that fit in the knapsack.• i.e. i S vi V
• Cost of Solution: The total value of objects in set.• i.e. i S pi
• Goal: Get as much value as you can into the knapsack.
289
v=4,p=4
v=4,p=4
V=8
Greedy Algorithm
Most valuable piGreedy Criteria:
v=7,p=5 v=4,p=4 v=4,p=4
V=8
Greedy give 5 Optimal gives 8
290
v=7,p=5V=7
Greedy AlgorithmGreedy Criteria:
v=7,p=5 v=4,p=4 v=4,p=4
V=7
Most dense in valuepi vi
Greedy give 4 Optimal gives 5
V=8
291
Greedy AlgorithmGreedy Criteria:
v=4,p=4
V=7
Greedy give 4
v=7,p=5V=7
v=4,p=4¾ of
+ ¾ × 4 = 7
If fractional solutions are allowed.
= Optimal
Works
Most dense in valuepi vi
Optimal gives 5
Often an Integersolution is MUCH
harder to find.
292
Bird & Friend Algorithm
I ask the bird:
My instance:
“What is the last object to take?”
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.
A solution: <<v5,p5>,<v9,p9>,...........,<v82,p82>>.
# of answers K = n
v=7,p=5 v=4,p=4 v=4,p=4V=12
293
Bird & Friend Algorithm
I ask the bird:
My instance:
A solution:
# of answers K =
v=7,p=5 v=4,p=4 v=4,p=4V=12
“Do we keep the last object?” 2 Yes & No
<<v5,p5>,<v9,p9>,...........,<v82,p82>>.
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.
294
Bird & Friend AlgorithmMy instance:
v=7,p=5 v=4,p=4 v=4,p=4V=12
Bird says, Yes keep the last object.
Trust her and put it into your knapsack.
I ask my friend:To fill the rest of the knapsack.But what instance do I give him?
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.
295
Bird & Friend AlgorithmHis instance:
v=7,p=5 v=4,p=4 v=4,p=4V=12-4
<V-vn:<v1,p1>,<v2,p2>,.........<vn-1,pn-1>,<vn,pn>>.
His solution: <<v5,p5>,<v9,p9>,...........,<v82,p82>>.
My solution:
My cost: same + pn
<<v5,p5>,<v9,p9>,...........,<v82,p82>,<vn,pn>>
296
Bird & Friend Algorithm
My solution: <<v5,p5>,<v9,p9>,...........,<v82,p82>,<vn,pn>>
My instance:
v=7,p=5 v=4,p=4 v=4,p=4V=12
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.
If we trust the bird and friend, this is valid and optimal.
My cost: same +pn
297
Bird & Friend AlgorithmMy instance:
v=7,p=5 v=4,p=4 v=4,p=4V=12
Bird says, No do not keep the last object.
Trust her and delete it.
I ask my friend:To fill the knapsack with the rest.What instance do I give him?
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.
298
Bird & Friend AlgorithmHis instance:
v=7,p=5 v=4,p=4 v=4,p=4V=12
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.
My solution:
My cost: same
His solution: <<v5,p5>,<v9,p9>,...........,<v82,p82>>.
sameIf we trust the bird and friend,
this is valid and optimal.
299
Time?
Same as the brute force algorithm that tries each solution.
Same as Brute Force AlgorithmI try each of 2 bird ans.
My friends tries 2
His friends tries 2
301
Set of Sub-InstancesDetermine the complete set of sub-Instances.
• Imagine running the recursive algorithm on it.• Determine the complete set of sub-Instances
ever given to you, your friends, their friends, …
Given an instance I,<V:<v1,p1>,<v2,p2>,<v3,p3>,<v4,p4>,<v5,p5>,<v6,p6>>.
Is this a sub-Instance?
Yes, if the bird keeps saying “No”.
<V:<v1,p1>,<v2,p2>,<v3,p3>>.
302
Set of Sub-InstancesDetermine the complete set of sub-Instances.
• Imagine running the recursive algorithm on it.• Determine the complete set of sub-Instances
ever given to you, your friends, their friends, …
Given an instance I,<V:<v1,p1>,<v2,p2>,<v3,p3>,<v4,p4>,<v5,p5>,<v6,p6>>.
Is this a sub-Instance?
No, the set of objects is always a prefix of the original set.
<V:<v1,p1>,<v2,p2>,<v3,p3>,<v4,p4>,<v5,p5>,<v6,p6>>.
303
Set of Sub-InstancesDetermine the complete set of sub-Instances.
• Imagine running the recursive algorithm on it.• Determine the complete set of sub-Instances
ever given to you, your friends, their friends, …
Given an instance I,<V:<v1,p1>,<v2,p2>,<v3,p3>,<v4,p4>,<v5,p5>,<v6,p6>>.
Quite possibly, if V’ V.
Is this a sub-Instance? <V’:<v1,p1>,<v2,p2>,<v3,p3>>.
It is easier to solve than to determine if it is a sub-instance.
304
Set of Sub-Instances
Guess the complete set S of sub-Instances.
The set S of sub-Instances needs to:• include our given I Yes: V’=V & i = n
My instance:
• closed under “friend” operation sub-Instance S subsub-Instance S
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.
i [0..n]<V’:<v1,p1>,<v2,p2>,......,<vi,pi>>. V’ [0..V]
<V’:<v1,p1>,<v2,p2>,......,<vi,pi>> S
YesNo <V’ :<v1,p1>,<v2,p2>,...,<vi-1,pi-1>>
<V’-vi:<v1,p1>,<v2,p2>,...,<vi-1,pi-1>> S
305
Construct a table • for storing the cost of opt sol and bird’s advice. • for each sub-instance.
Sub-InstancesMap
Indexes
Cell of table
i
“Which of first i objects to put in a knapsack of size v’?”
v’
<V’:<v1,p1>,<v2,p2>,......,<vi,pi>>.
i [0..n] V’ [0..V]
The Table
306
The TableThe complete set S of sub-Instances.
i [0..n]<V’:<v1,p1>,<v2,p2>,......,<vi,pi>>. V’ [0..V]
01
i-1
n
0 1
2
2 V’-vi VV’
iYesNo
OptSol Cost &
Bird’s Advicefor this
sub-Instance
Our cost?
samesame + pi
Take best of best.
307
The TableThe complete set S of sub-Instances.
i [0..n]<V’:<v1,p1>,<v2,p2>,......,<vi,pi>>. V’ [0..V]
01
i-1
n
0 1
2
2 V’-vi VV’
i
OptSol Cost &
Bird’s Advicefor this
sub-Instance
Order to fillso nobody
waits?
311
Running Time
Running time = ( # of sub-instances × # bird answers ) = ( Vn × 2 ) = ( 2#bits in V × n )
My instance:
Polynomial?
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.The complete set S of sub-Instances is
i [0..n]<V’:<v1,p1>,<v2,p2>,......,<vi,pi>>. V’ [0..V]
YesNo
Exponential in “size” in instance!
312
The Knapsack Problem• Dynamic Programming
Running time = ( V × n ) = ( 2#bits in V × n )• Poly time if size of knapsack is small• Exponential time if size is an arbitrary integer.
313
The Knapsack Problem
If there is a poly-time algorithmfor the Knapsack Problem
For EVERY optimization problem there is a poly-time algorithm.
• Dynamic Programming Running time = ( V × n ) = ( 2#bits in V × n )
• NP-Complete
314
The Knapsack Problem
Likely there is not a poly-time algorithmfor the Knapsack Problem.
Likely there is not a poly-time algorithmfor EVERY optimization problem.
• Dynamic Programming Running time = ( V × n ) = ( 2#bits in V × n )
• NP-Complete
315
The Knapsack Problem• Dynamic Programming
Running time = ( V × n ) = ( 2#bits in V × n )
• NP-Complete• Approximate Algorithm• In poly-time, solution can be found
that is (1+) as good as optimal.
done
317
Ingredients:• Instances: Events with starting and finishing times
<<s1,f1>,<s2,f2>,… ,<sn,fn>>.• Solutions: A set of events that do not overlap. • Cost of Solution: The number of events scheduled.
• Goal: Given a set of events, schedule as many as possible.
The Job/Event Scheduling Problem
318
Greedy Algorithm
Earliest Finishing TimeSchedule the event which will free up your room for someone else as soon as possible.
Motivation:
Greedy Criteria:
319
Ingredients:• Instances: Events with starting and finishing times
and weights <<s1,f1,w1>,<s2,f2,w2>,… ,<sn,fn,wn>>.
• Solutions: A set of events that do not overlap. • Cost of Solution: Total weight of events scheduled.
• Goal: Given a set of events, schedule max weight
Weighted Event Scheduling
320
Greedy Algorithm
Earliest Finishing TimeSchedule the event which will free up your room for someone else as soon as possible.
Motivation:
Greedy Criteria:
1001 1
321
Bird & Friend Algorithm
I ask the bird:
An instance:
“What is the last event to take?”
<<s1,f1,w1>,<s2,f2,w2>,… ,<sn,fn,wn>>.
A solution: <<s5,f5,w5>,<s9,f9,w9>,… ,<s82,f82,w82>>.
# of answers K = n
322
Bird & Friend Algorithm
I ask the bird:
An instance:
“Do we keep the last event?”
<<s1,f1,w1>,<s2,f2,w2>,… ,<sn,fn,wn>>.
A solution: <<s5,f5,w5>,<s9,f9,w9>,… ,<s82,f82,w82>>.
# of answers K = 2 Yes & No
323
Bird & Friend AlgorithmAn instance:
I ask the bird:
“Do we keep the last event?”
<<s1,f1,w1>,<s2,f2,w2>,… ,<sn,fn,wn>>.
His solution: <<s5,f5,w5>,<s9,f9,w9>,… ,<s82,f82,w82>>.
I ask my friend:
She answers: No
My solution: same My cost: same
<<s1,f1,w1>,<s2,f2,w2>,… ,<sn,fn,wn>>.
324
Bird & Friend AlgorithmAn instance:
I ask the bird:
“Do we keep the last event?”
<<s1,f1,w1>,<s2,f2,w2>,… ,<sn,fn,wn>>.
His solution: <<s5,f5,w5>,<s9,f9,w9>,… ,<s82,f82,w82>>.
I ask my friend:
She answers: Yes
My solution: same + <sn,fn,wn>. My cost: same +wn
<<s1,f1,w1>,<s2,f2,w2>,… ,<sn,fn,wn>>.
Carefull
325
Bird & Friend Algorithm
Bird answers:
“Yes keep the last event.” Give the rest to my friend.
No this solution is not valid!
Here is my best subsolution.
I add to his subsolution the bird’s answer.
last event
326
Bird & Friend Algorithm
Bird answers:
“Yes keep the last event.” Then I should politely tell the bird she is wrong
No we trust the bird!
327
Bird & Friend Algorithm
Bird answers:
“Yes keep the last event.” No we trust the
bird!
You only tell her she is wrong if you really know.
Eg k words don’t fit on the last lineThe bear does not fit into the knapsack
328
Bird & Friend Algorithm
Bird answers:
“Yes keep the last event.” No we trust the
bird!
Your friend could have just as easily given you this subsolution that does not
conflict with the bird’s answer.
329
Bird & Friend Algorithm
Bird answers:
“Yes keep the last event.” No we trust the
bird!
Or maybe he needs to make a sacrifice when finding his answer in order that the
overall solution is the best.
330
Bird & Friend Algorithm
Bird answers:
“Yes keep the last event.” No we trust the
bird!
Or goal now is to find the best solution to our instance that is consistent with the
bird’s answer.Then we will take the best of best.
331
Bird & Friend Algorithm
Bird answers:
“Yes keep the last event.” No we trust the
bird!
Dude! It is your job to give me the right subinstance so that I give you a
subsolution that does not conflict with the bird
332
Bird & Friend Algorithm
Bird answers:
“Yes keep the last event.”
My instance:last event
Cant keep any events that overlap with it.
333
Bird & Friend Algorithm
Bird answers:
“Yes keep the last event.”
My instance:last event
I ask my friend:
334
Bird & Friend Algorithm
Bird answers:
“Yes keep the last event.”
His instance:His solution
I ask my friend:
335
Bird & Friend Algorithm
Bird answers:
“Yes keep the last event.”
I ask my friend:
My solution: same + <sn,fn,wn>. Valid?
My instance:My solution:
Yes
336
Bird & Friend Algorithm
Bird answers:
“Yes keep the last event.”
My instance:My solution:
I ask my friend:
My solution: same + <sn,fn,wn>. My cost: same +wn
337
Time?
Same as the brute force algorithm that tries each solution.
Same as Brute Force AlgorithmI try each of 2 bird ans.
My friends tries 2
His friends tries 2
339
Set of Sub-Instances
Given an instance I,
Determine the complete set of sub-Instances.
• Imagine running the recursive algorithm on it.• Determine the complete set of sub-Instances
ever given to you, your friends, their friends, …
340
Every subset of {1,…,9}is a possible sub-Instance.
I.e. could be an exponentialnumber of them.
Hence, running time is exponential.
Greedy algorithm sortedjobs by finishing time.
Let us do that too.
342
Set of Sub-Instances
Guess the complete set S of sub-Instances.
The set S of sub-Instances needs to:• include our given I Yes: i = n
My instance:
<<s1,f1,w1>,<s2,f2,w2>,................… ,<sn,fn,wn>>.
i [0..n]<<s1,f1,w1>,<s2,f2,w2>,… ,<si,fi,wi>>
• closed under “friend” operation sub-Instance S subsub-Instance S
• each sub-Instance needs to beasked of some friend, friend…
?
Only n sub-InstancesGood enough.
343
Set of Sub-Instances
sub-Instance =
<<s1,f1,w1>,<s2,f2,w2>,................................,<si,fi,wi>>
last event
Show closed under “friend” operation sub-Instance S subsub-Instance S
Events sorted by finishing time.
344
Set of Sub-Instances
sub-Instance =
<<s1,f1,w1>,<s2,f2,w2>,................................,<si,fi,wi>>
Bird answers: “Yes keep the last event.”
last event
Show closed under “friend” operation sub-Instance S subsub-Instance S
Delete overlapping events for friend.
345
Set of Sub-Instances
subsub-Instance =
<<s1,f1,w1>,<s2,f2,w2>,.....,<sj,fj,wj>>
Bird answers: “Yes keep the last event.”
Show closed under “friend” operation sub-Instance S subsub-Instance S
Delete overlapping events for friend.
346
Set of Sub-Instances
subsub-Instance =
<<s1,f1,w1>,<s2,f2,w2>,.....,<sj,fj,wj>>
Show closed under “friend” operation sub-Instance S subsub-Instance S
subsub-Instance S set of kept jobs is a prefix of events.
typical deleted jobtypical kept job
Event j is kept fj si
347
Set of Sub-Instances
The complete set S of sub-Instances is
My instance:
<<s1,f1,w1>,<s2,f2,w2>,................… ,<sn,fn,wn>>.
i [0..n]<<s1,f1,w1>,<s2,f2,w2>,… ,<si,fi,wi>>
Table:
0, 1, 2, 3, 4, …. n Base case Original
348
Set of Sub-Instances
The complete set S of sub-Instances is
Running time = # of sub-instances × # bird answers = n × 2
My instance:
<<s1,f1,w1>,<s2,f2,w2>,................… ,<sn,fn,wn>>.
i [0..n]<<s1,f1,w1>,<s2,f2,w2>,… ,<si,fi,wi>>
Done
But to find your friend’s “yes” sub-instanceyou must know how many events overlapwith your last event. This takes time:O(logn) using binary searchfor a total of O(nlogn) time.
351
T AB CA TT
A AA BT a
B TA BC b e
C CB AC c d
Parsing
Context Free Grammar (Not look ahead one) For ease, we will assume every non-terminal either goes to two non-terminals or to one terminal
352
T AB CA TT
A AA BT a
B TA BC b e
C CB AC c d
Parsing
Input: start non-terminal = T string to parse = a1a2a3 ..... an = baeaadbda
Output: A parsing
T a1a2a3 ..... an T
C A
A
A
B
A
C
C B
A CTA B
B T
C A
b d a b a e a a d b
353
T AB CA TT
A AA BT a
B TA BC b e
C CB AC c d
Parsing
Recursive Algorithm: GetT does not know whether to call GetA, GetC, or GetT.
Input: T a1a2a3 ..... an T
C A
A
A
B
A
C
C B
A CTA B
B T
C A
b d a b a e a a d b
354
T AB CA TT
A AA BT a
B TA BC b e
C CB AC c d
Parsing
Ask Little Bird:• For first ruleAsk Friend• Parse leftAsk Another Friend• Parse right.
Input: T a1a2a3 ..... an T
C A
A
A
B
A
C
C B
A CTA B
B T
C A
b d a b a e a a d b
355
T AB CA TT
A AA BT a
B TA BC b e
C CB AC c d
Parsing
Ask Little Bird:• For first ruleInstance to give Friend•?
Input: T a1a2a3 ..... an T
C A
b d a b a e a a d b
356
T AB CA TT
A AA BT a
B TA BC b e
C CB AC c d
Parsing
Ask Little Bird:• For first ruleWant from Friend:• Left sub-parse tree.Instance to give him:• C baeaadb
Input: T a1a2a3 ..... an
C
A
A
B
A
C
C B
A CTA B
T
A
b d a b a e a a d b
357
T AB CA TT
A AA BT a
B TA BC b e
C CB AC c d
Parsing
Ask Little Bird:• For first ruleHow can we know split?• Ask the Bird!
Input: T a1a2a3 ..... an T
C A
A
A
B
A
C
C B
A CTA B
B T
C A
b d a b a e a a d b
358
T AB CA TT
A AA BT a
B TA BC b e
C CB AC c d
Parsing
Ask Little Bird:• For first rule• For the split.
# of ans K =# of ans K =
mT = # of rules for T.n = # chars in string.
Total # of ans K = mT × n.
Input: T a1a2a3 ..... an
b d a b a e a a d b
T
C A
359
T AB CA TT
A AA BT a
B TA BC b e
C CB AC c d
Parsing
Ask left friend:• Instance: C baeaadb• Solution: Left parsing
Input: T a1a2a3 ..... an
b a e a a d b
C
T
b d a
A
A
A
B
A
C
C B
A CTA B
360
T AB CA TT
A AA BT a
B TA BC b e
C CB AC c d
Parsing
Ask right friend:• Instance: A bda• Solution: Right parsing
Input: T a1a2a3 ..... an
A
B T
C A
b d a
T
C
b a e a a d b
361
T AB CA TT
A AA BT a
B TA BC b e
C CB AC c d
Parsing
Combine:• Instance: • Bird’s Answer• Left Friend’s Answer• Right Friend’s Answer
Input: T a1a2a3 ..... an
A
A
B
A
C
C B
A CTA B
B T
C A
T
b d a b a e a a d b
C A
363
Time?
Same as the brute force algorithm that tries each solution.
Same as Brute Force AlgorithmI try each of 2 bird ans.
My friends tries 2
His friends tries 2
365
Set of Sub-InstancesDetermine the complete set of sub-Instances.
• Imagine running the recursive algorithm on it.• Determine the complete set of sub-Instances
ever given to you, your friends, their friends, …
Given an instance I,
366
Set of Sub-InstancesDetermine the complete set of sub-Instances.
My instance I:
gives:
My left sub-Instance.
gives:
His right sub-Instance.
T a1a2a3 ..... an T
C A
CA
b d a b a e a a d b
367
Set of Sub-InstancesDetermine the complete set of sub-Instances.
T’ aiai+1 ..... aj • non-terminals T’ • i,j [1,n]
My instance I:T a1a2a3 ..... an
sub-Instances:
# of sub-Instances = # of non-terminals × n2
C
a d b
aiai+1...aj
T’=
a1...ai-1 aj+1...an
368
Construct a table • for storing the cost of opt sol and bird’s advice. • for each sub-instance.
Sub-InstancesMap
Indexes
Cell of table
The Table
T’ aiai+1 ..... aj
T’ i,j [1,n]
j
T’ i
370
T’ aiai+1 ..... aj non-terminals T’
& i,j [1,n]
sub-Instances:
Running Time
Running time = ( # of sub-instances × # bird answers ) = ( # of non-terminals × n2
gives: First rule and split
× # of rules · n )
Done
371
Find a Satisfying Assignment
c = (x3 or x5 or x6) and (x2 or x5 or x7) and (x3 or x4)An instance (input) consists of a circuit:
A solution is an assignment of the variables.x1 = 0, x2 = 1, x3 = 0, x4 = 0, x5 = 1, x6 = 0, x7 = 1
true falsetrue true
truetrue truetrue
The cost of a solution is • 1 if the assignment satisfies the circuit.• 0 if not.
The goal is to find satisfying assignment.
372
Find a Satisfying Assignment
c = (x3 or x5 or x6) and (x2 or x5 or x7) and (x3 or x4)Instance:
Ask the little bird
Value of x1 in an optimal solution or even better
Value of x3 in an optimal solution
For now, suppose she answered x3 = 0.
We will have to try both x3 = 0 and x3 = 1.
373
Find a Satisfying Assignment
c = (x3 or x5 or x6) and (x2 or x5 or x7) and (x3 or x4)Instance:
truetrue false
Commit to x3 = 0 and simplify
c = (x2 or x5 or x7) and x4
Sub-Instance:
Friend gives Sub-Solution: x1 = 0, x2 = 1, x4 = 0, x5 = 1, x6 = 0, x7 = 1
Our Solution: x1 = 0, x2 = 1, x3 = 0, x4 = 0, x5 = 1, x6 = 0, x7 = 1
374
In the end, some friend looks at each of the 2n assignments,
Speeding Up the Timex3
0 1x2
0 1
x1
0 1x1
0 1
x2
0 1
x2
0 1
x1
0 1
376
Set of Sub-Instances
Given an instance I,
Determine the complete set of sub-Instances.
• Imagine running the recursive algorithm on it.• Determine the complete set of sub-Instances
ever given to you, your friends, their friends, …
377
Set of Sub-Instances
Given an instance I,
Determine the complete set of sub-Instances.
Is this a sub-Instance?
Yes
c = (x1 or y1) and (x2 or y2) and (x3 or y3) and (x4 or y4)
c = (x1) and (x3)
Commit to y1=0, y2=1, y3=0, y4=1, and simplify
True for any subset of the xi. could be an exponential # of different sub-Instances. running time is exponential.
378
In the end, some friend looks at each of the 2n assignments,
Speeding Up the Timex3
0 1x2
0 1
x1
0 1x1
0 1
x2
0 1
x2
0 1
x1
0 1
379
Speeding Up the Timex3
0 1x2
0 1
x1
0 1x1
0 1
x2
0 1
x2
0 1
x1
0 1
But sometimes we can prune off branches.
380
Find a Satisfying Assignment
Instance:c = (x2 or x5 or x7) and x3
x3 is forced to x3 = 0
x3
0 1x2
0 1
x1
0 1x1
0 1
x2
0 1
x2
0 1
x1
0 1
381
Find a Satisfying Assignment
Instance:c = (x2 or x5 or x7) and x3 and x3
This is trivially unsatisfiable because x3 can’t be both 0 and 1.
x3
0 1x2
0 1
x1
0 1x1
0 1
x2
0 1
x2
0 1
x1
0 1
384
Designing Recursive Back Tracking Algorithm• What are instances, solutions, and costs? • Given an instance I,• What question do you ask the little bird?• Given a bird answer k [K],
• What sub-Instance do your give your friend?• Assume he gives you optSubSol for sub-Instance.• How do you produce an optSol for I from
• the bird’s k and • the friend’s optSubSol?
• How do you determine the cost of optSol from • the bird’s k and • the cost of the friend’s optSubSol?
• Try all bird’s answers and take best of best.
Review
385
Recursive Back Tracking Algorithm
Dynamic Programming Algorithm• Given an instance I,• Imagine running the recursive alg on it.• Determine the complete set of sub-Instances
ever given to you, your friends, their friends, …• Build a table indexed by these sub-Instances• Fill in the table in order so that nobody waits.
• the cost of its optimal solution• advice given by the bird
• Run the recursive algorithm with bird’s advice to find the solution to your instance.
Review
386
Optimization Problems• Don’t mix up the following
– What is an instance– What are the objects in an instance– What is a solution– What are the objects in a solution– What is the cost of a solution
• Greedy algorithm– What does the algorithm do & know– What does the Prover do & know– What does the Fairy God Mother do & know
• Recursive Backtracking / Dynamic Programming– What does the algorithm do & know– What does the little bird do & know– What does the friend do & know
387
Dynamic ProgrammingDon’ts
• Yes, the code has a basic structure that you should learn.• But don’t copy other code verbatim • Don’t say if(ai = cj)
(i.e. Longest Common Subsequence) when our problem does not have cj
388
Dynamic ProgrammingDon’ts
• When looping over the sub-instances • be clear what the set of sub-instances are • which is currently being solved,
i.e. which instance is cost(i,j)?• If you know that the set of sub-instances are the
prefixes of the input, i.e. <a1,a2, …, ai>, then don’t have a two dimensional table. Table[1..n,1..n].
• Don’t loop over i and loop over j if j never gets mentioned again.
389
Dynamic ProgrammingDon’ts
• When trying all bird answers • be clear what the set of bird answers are,• which is currently being tried,• & what it says about the solution being looked for.
• When getting help from your friend,• be clear what the sub-instance is that you are giving him• How do you use the current instance and the bird' s
answer to form his sub-instance?• Don’t simply say cost(i-1,j-1)
390
Dynamic ProgrammingDon’ts
• Think about what the base cases should be.• Don’t make an instance a base cases
if they can be solved using the general method.• % is used to start a comment.
Don’t put it in front of code.
391
• 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
4 21
31
28 35
51
42
40 49
63
55 71
• Eg. The Best Binary Search Tree problem,