Title: Recursive Back Tracking
1Recursive Back TrackingDynamic Programming
Thinking about Algorithms Abstractly
- Jeff Edmonds York University
COSC 3101
Lecture 7
2Problems
Techniques
Optimization Problems A Sequence of Decisions The
Little Bird Friend Optimal Substructure Memoizat
ion Set of Sub-Instances Tracing Dyn. Prog.
Alg Reversing Code Speeding Up Running
Time Multiple Opt Solutions Review Question for
Little Bird Review Don'ts
Best Path Printing Neatly Longest Common
Subsequence Knapsack Problem The Event Scheduling
Problem Parsing Satisfiability
3Dynamic Programming
- A hard topic.
- I try to provide a unified way to think of itand
a fixed set of steps to follow. - Even if you dont 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.
4Optimization 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.
5Optimization Problems
- 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.
6Optimization Problems
- Specification of an Optimization Problem
- ltpreCondgt The input is one instance.
- ltpostCondgt 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!
7Search Graph For Best Path
We use it because it nicely demonstrates the
concepts in a graphical way.
8Search Graph For Best Path
9Search Graph For Best Path
An instance (input) consists of ltG,s,tgt.
10Brute Force Algorithm
11An Algorithm As A Sequence of Decisions
I ask a question about the solution.
Which edge should we take first?
Some how I decide lts,v3gt.
My friend ask the next question.
Which edge do we take second?
Some how he decides ltv3,v5gt.
His friend ask the next question.
Which edge do we take third?
Some how he decided ltv5,v8gt.
12An Algorithm As A Sequence of Decisions
I ask a question about the solution.
Which edge should we take first?
How do I decide?
The greedy algorithm?
Does not work!
13Local 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.
14An Algorithm As A Sequence of Decisions
I ask a question about the solution.
Which edge should we take first?
How do I decide?
- In reality we will try all possible first edges.
15"Little Bird" Abstraction
(It is up to you whether or not to use it)
16Little Bird Friend Alg
I ask a question about the solution.
Which edge should we take first?
The bird answers lts,v1gt.
My friend asks the next question.
Which edge do we take second?
The bird answers ltv1,v4gt.
17Sub-Instance for Friend
- Our instance is ltG,s,tgt 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?
18Little Bird Friend Alg
The bird answers lts,v1gt.
If I trust the little bird, I take step along
edge lts,v1gt and ask my friend,
Which is the best path from v1 to t?
Friend answers ltv1,v6,tgt with weight 10.
To get my solution
I tack on the birds edge making the path
lts,v1,v6,tgt
with weight
10313.
19Faulty Bird
But what if we do not have a bird that we trust?
This work is not wasted, because we have found
- the best solution to our instance from amongst
those consistent with this birds answer.
- i.e. the best path from s to tfrom amongst those
starting with lts,v1gt.
In reality we will try all possible first
edges, giving ..
20Faulty Bird
the best path from amongst those starting with
lts,v1gt.
21Faulty Bird
and the best path from amongst those starting
with lts,v2gt.
22Faulty Bird
and the best path from amongst those starting
with lts,v3gt.
23Faulty Bird
and the best path from amongst those starting
with lts,v4gt.
24Faulty Bird
At least one of these four paths must be an over
all best path.
25Best of the Best
26Recursive backtracking code always has thissame
basic structure.
27- Be clear what are
- the sub-instance
- a solution
- the cost of a sol.
28Loop through the bird answers. Be clear which is
the current one being tried.
29Give the bird friend algorithmas a comment.
30What is the bird asked? What does she answer?
31Get help from friend Be clear what sub-instance
you give him. Store the solution cost he
gives you.
32How do you formyour solution from the friends
and fromthe birds?
33How do you formyour cost from the friends and
fromthe birds?
34Take the bestof the best
35What are the base case sub-instances? What are
their solutionsand costs?
36Return the solutionand cost for the original
instance.
37Optimal Substructure
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.
38Optimal Substructure
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.
39Optimal 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.
40Dont all problems have this optimal substructure
property?
Optimal Substructure
41Longest simple path
Optimal Substructure
- 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
NP-Complete
42Same as Brute Force Algorithm
Same as the brute force algorithm that tries
each path.
43Same as Brute Force Algorithm
44Speeding Up the Time
- 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.
45Speeding Up the Time
Sometimes entire an branch can be pruned off.
- Perhaps because these solutions are not valid or
not highly valued.
- 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.
46Speeding Up the Time
Memoization
- 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.
47Exponential Time Redoing Work
Which is the best path from v7 to t?
How many friends solve this sub-instance?
48Exponential Time Redoing Work
Which is the best path from s to t?
49Exponential Time Redoing Work
Which is the best path from v1 to t?
50Exponential Time Redoing Work
Which is the best path from v4 to t?
51Exponential Time Redoing Work
Which is the best path from v7 to t?
Theres one.
52Exponential Time Redoing Work
Which is the best path from s to t?
53Exponential Time Redoing Work
Which is the best path from v3 to t?
54Exponential Time Redoing Work
Which is the best path from v5 to t?
55Exponential Time Redoing Work
Which is the best path from v7 to t?
Theres another.
56Exponential Time Redoing Work
Which is the best path from v7 to t?
How many friends solve this sub-instance?
Once for each path to v7
Waste time redoing work
Save time by only doing once.
57Depth First Search
Drop bread crumbs and dont revisit.
But we need shortest path
58 Dynamic Programming
Having many friends solving this same
sub-instanceis a waste of time.
59 Dynamic Programming
It is my job to learn and remember the optSol to
my sub-Instance i.e. the best path from v7 to t
60 Dynamic Programming
I will find my best pathand tell you.
61 Dynamic Programming
When I need to find the best path from v2 to t I
will ask you for the best path from v7 to t
I remember my best pathand will tell you.
62 Dynamic Programming
When I need to find the best path from v5 to t I
will ask you for the best path from v7 to t
I remember my best pathand will tell you.
63 Dynamic Programming
Avoid waiting.
When I need to find the best path from v2 to t I
will ask you for the best path from v7 to t
I will find my best pathand tell you.
But I hate to wait for you. Recursion has a lot
of overhead Why dont you go first?
64 Dynamic Programming
Before anyone asks me,I will find my best
path and remember it.
65Set of Sub-Instances
But what sub-instance need to be solved and
in which order?
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,
66Set of Sub-Instances
Guess the complete set S of sub-Instances.
Best path from v7 to t?
Yes
Best path from v21 to t?
No
v21 is not a part of ouroriginal instance.
67Set of Sub-Instances
Guess the complete set S of sub-Instances.
Best path from v7 to t?
Yes
Best path from v21 to t?
No
Best path from v3 to v7?
No
All paths considered end in t.
68Set of Sub-Instances
Guess the complete set S of sub-Instances.
Best path from v7 to t?
Yes
Best path from v21 to t?
No
Best path from v3 to v7?
No
All paths considered end in t.
69Set of Sub-Instances
Guess the complete set S of sub-Instances.
Best path from v7 to t?
Yes
Best path from v21 to t?
No
Best path from v3 to v7?
No
Yes
70Set of Sub-Instances
Guess the complete set S of sub-Instances is
Assign one friend to each sub-Instance.
71Set of Sub-Instances
Guess the complete set S of sub-Instances is
- The set S of sub-Instances needs to
- include our given I
72Set of Sub-Instances
Guess the complete set S of sub-Instances is
- The set S of sub-Instances needs to
- include our given I
- closed under friend operation
Integers closed under addition
? x,y ? I ? xy ? I
? sub-Instance ? S ?
subsub-Instance ? S
73Set of Sub-Instances
Guess the complete set S of sub-Instances is
- The set S of sub-Instances needs to
- include our given I
- closed under friend operation
- each sub-Instance needs to be
asked of some friend, friend,
74Set of Sub-Instances
Guess the complete set S of sub-Instances is
- The set S of sub-Instances needs to
- include our given I
- closed under friend operation
- each sub-Instance needs to be
asked of some friend, friend,
A fine set of sub-instances!
75Order to complete
The complete set S of sub-Instances is
In what order should they go?
- in an order such that no friend must wait.
- from smallest to largest
For this problem, the order relies on the graph
being leveled.
76Order to complete
The complete set S of sub-Instances is
In what order should they go?
- in an order such that no friend must wait.
- from smallest to largest
First
Base Case easy
Last
77 Dynamic Programming
"Which is the best path from t to t?"
78 Dynamic Programming
"Which is the best path from v8 to t?"
easy
79 Dynamic Programming
"Which is the best path from v7 to t?"
easy
80 Dynamic Programming
"Which is the best path from v6 to t?"
easy
81 Dynamic Programming
"Which is the best path from v5 to t?"
Harder
82 Dynamic Programming
"Which is the best path from v5 to t?"
Little bird suggests first edge ltv5,v7gt
Friend gives best path ltv7,tgt.
83 Dynamic Programming
"Which is the best path from v5 to t?"
Little bird suggests first edge ltv5,v8gt
Friend gives best path ltv8,tgt.
84 Dynamic Programming
"Which is the best path from v5 to t?"
Take best of best
85 Dynamic Programming
"Which is the best path from v4 to t?"
86 Dynamic Programming
"Which is the best path from v4 to t?"
Little bird suggests first edge ltv4,v6gt
Friend gives best path ltv7,tgt.
87 Dynamic Programming
"Which is the best path from v4 to t?"
Little bird suggests first edge ltv4,tgt
Friend gives best path ltt,tgt.
88 Dynamic Programming
"Which is the best path from v4 to t?"
Little bird suggests first edge ltv4,v7gt
Friend gives best path ltv7,tgt.
89 Dynamic Programming
"Which is the best path from v4 to t?"
Take best of best
90 Dynamic Programming
"Which is the best path from v3 to t?"
91 Dynamic Programming
"Which is the best path from v3 to t?"
Little bird suggests first edge ltv3,v5gt
Friend gives best path ltv5,tgt.
92 Dynamic Programming
"Which is the best path from v3 to t?"
Little bird suggests first edge ltv3,v8gt
Friend gives best path ltv8,tgt.
93 Dynamic Programming
"Which is the best path from v3 to t?"
Take best of best
94 Dynamic Programming
"Which is the best path from v2 to t?"
95 Dynamic Programming
"Which is the best path from v2 to t?"
Little bird suggests first edge ltv2,v4gt
Friend gives best path ltv4,tgt.
96 Dynamic Programming
"Which is the best path from v2 to t?"
Little bird suggests first edge ltv2,v7gt
Friend gives best path ltv7,tgt.
97 Dynamic Programming
"Which is the best path from v2 to t?"
Take best of best
98 Dynamic Programming
"Which is the best path from v1 to t?"
99 Dynamic Programming
"Which is the best path from v1 to t?"
Little bird suggests first edge ltv1,v3gt
Friend gives best path ltv3,tgt.
100 Dynamic Programming
"Which is the best path from v1 to t?"
Little bird suggests first edge ltv1,v4gt
Friend gives best path ltv4,tgt.
101 Dynamic Programming
"Which is the best path from v1 to t?"
Little bird suggests first edge ltv1,v5gt
Friend gives best path ltv5,tgt.
102 Dynamic Programming
"Which is the best path from v1 to t?"
Take best of best
103 Dynamic Programming
"Which is the best path from s to t?"
Original Problem
104 Dynamic Programming
"Which is the best path from s to t?"
Little bird suggests first edge lts,v1gt
Friend gives best path ltv1,tgt.
105 Dynamic Programming
"Which is the best path from s to t?"
Little bird suggests first edge lts,v2gt
Friend gives best path ltv2,tgt.
106 Dynamic Programming
"Which is the best path from s to t?"
Little bird suggests first edge lts,v3gt
Friend gives best path ltv3,tgt.
107 Dynamic Programming
"Which is the best path from s to t?"
Little bird suggests first edge lts,v4gt
Friend gives best path ltv4,tgt.
108 Dynamic Programming
"Which is the best path from s to t?"
Take best of best
DONE
109 Dynamic Programming
Fill out a table containing an optimal solution
for each sub-instance.
Which is the best path from vi to t?
t, v8, v7, v6, v5, ., s
Base case
Original
110Communication
Friend k gives friend i a best path from vk to t.
Recursive BackTracking
ltoptSubSol,optSubCostgt
LeveledGraph(ltG,vk,tgt)
return(optSolmin,optCostmin) ,
i
Dynamic Programming
optSolk optSolmin
optSubSol optSolk
optSolk ltvi,vkgt optSolk
111Dynamic Programming code always has thissame
basic structure.
112- Be clear what are
- the sub-instances
- a solution
- the cost of a solution.
113Table indexed by sub-instances. Be clear what
they are.
114Loop through the sub-instances. In what
order? Be clear which the current one is.
115Loop through the bird answers. Be clear which is
the current one being tried.
116Give the bird friend algorithmas a comment.
117What is the bird asked? What does she answer?
118Get help from friend Be clear what sub-instance
you give him.
119How do you formyour solution from the friends
and fromthe birds?
120How do you formyour cost from the friends and
fromthe birds?
121Take the bestof the best
122What are the base case sub-instances? What are
their solutionsand costs?
123Return the solutionand cost for the original
instance.
124Reversing
125Reversing
Determine the complete set of sub-instances
126Reversing
Fill out a table containing an optimal solution
for each sub-instance.
Which is the best path from s to vi?
s, v1, v2, v3, v4, ., t
Base case
Original
127(No Transcript)
128Running Time
Time
of Sub-Instances
of Bird Answers
q(1)
n
d
129Communication Time
optSolk ltoptSolk,vigt
Size of path
q(n).
130Running Time
Time
of Sub-Instances
of Bird Answers
size of solution q(n d n)
Space
131Store Path Costs, not Paths
"What is cost of the best path from s to v7?"
132Store Path Costs, not Paths
"What is cost of the best path from s to v7?"
Little bird suggests last edge ltv4,v7gt with
weight 2.
8
Friend gives cost 8 of best path lts,v4gt.
Best cost via ltv4,v7gt is 8210.
133Store Path Costs, not Paths
"What is cost of the best path from s to v7?"
Little bird suggests last edge ltv2,v7gt with
weight 7.
Friend gives cost 2 of best path lts,v2gt.
Best cost via ltv2,v7gt is 279.
134Store Path Costs, not Paths
"What is cost of the best path from s to v7?"
Little bird suggests last edge ltv5,v7gt with
weight 5.
6
Friend gives cost 6 of best path lts,v5gt.
Best cost via ltv5,v7gt is 6511.
135Store Path Costs, not Paths
"What is cost of the best path from s to v7?"
Take best of best
10, 9, 11
136(No Transcript)
137Leave these lines as comments for extra clarity
for the reader
138 139Find Optimal Path
- Previous algorithm gives
- Cost of the best path from s to vi, ? i.
- Birds advice of last edge to vi.
We run the bird-friend algorithm again, but
with a reliable bird.
140Find Optimal Path
The bird gives that the last edge of the best
path from s to t is ltv8,tgt.
141Find Optimal Path
The bird gives that the last edge of the best
path from s to v8 is ltv5,v8gt.
142Find Optimal Path
The bird gives that the last edge of the best
path from s to v5 is ltv3,v5gt.
143Find Optimal Path
The bird gives that the last edge of the best
path from s to v3 is lts,v3gt.
144Find Optimal Path
Done!
145Find Optimal Path
This could be done iteratively.As an exercise,
design it.
146Multiple Optimal Solutions
She could give either answer.
By giving this edge she says There exists an
optimal solution consistent with this answer.
Similar to greedy proof.
6
147Multiple Optimal Solutions
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.
6
148Review
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 birds k and
- the friends optSubSol?
- How do you determine the cost of optSol from
- the birds k and
- the cost of the friends optSubSol?
- Try all birds answers and take best of best.
149Review
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 birds advice to find
the solution to your instance.
150The Question For the Little Bird
- Purpose of Little Bird
- An abstraction from which it iseasier to focus
on the difficult issues. - Her answers gives 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.
151The Question For the Little Bird
An instance Graph, s, and t
The Dynamic Programming reverses the recursive
backtracking algorithm. Hence, to end up with a
forward order,we first reverse the recursive
backtracking algorithm.
152The Question For the Little Bird
An instance Graph, s, and t
The Dynamic Programming reverses the recursive
backtracking algorithm. Hence, to end up with a
forward order,we first reverse the recursive
backtracking algorithm.
153The Question For the Little Bird
An instance Graph, s, and t
What is the last edge in the path?
A good question for the bird leaves you with a
good recursive sub-instance to ask your friend.
154The Question For the Little Bird
An instance Graph, s, and t
What is the last edge in the path?
Giving a good follow up question for your friend
to ask the bird.
What is the second last edge in the path?
155The Question For the Little Bird
- 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.
156The Question For the Little Bird
- 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.
Trying all is the Brute Force algorithm.
157The Question For the Little Bird
An instance Graph, s, and 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?
158The Question For the Little Bird
An instance ???
What is the last object in the sequence?
of answers K
of possible last objects.
What is the rest of the solution?
159The Question For the Little Bird
What is the last object in the sequence?
of answers K
of possible last objects.
Is there are smaller question that we could ask?
160The Question For the Little Bird
Is the last object of the instance included
in the optimal solution?
of answers K
2, Yes or No
161The Question For the Little Bird
An instance ???
What object is at the root?
What is the left sub-tree?
What is the right sub-tree?
Previous problems had one friend given a bird ans.
162The Question For the Little Bird
An instance ???
A solution a binary tree of objects
What object is at a leaf?
- Bad Question
- How does this help us solve the problem?
- What is a good follow up question for the friend
to ask?
163Printing Neatly
164Printing Neatly
An instance text to print neatly chars per
line
Love life man while there as we be, 11
The cost a measure of how neat,
small punishmentbig punishment
The goal is to to print it as neatly as
possible.
165Brute Force Algorithm
Try all ways to print, return the best.
But there may be an exponential number of ways to!
love.life.. man.. love.life.manlove.li
fe.man love.life.. man..
166Bird Friend Algorithm
An instance
Love life man while there as we be, 11
How many words on the last line?
She may answers 3.
Which is the best way to print the remaining
n-3 words?
167Bird Friend Algorithm
An instance
Love life man while there as we be, 11
Even if the bird was wrong, this work is not
wasted.
This is best way to print from amongst those
ending in 3 words.
and take best of best.
168Same as Brute Force Algorithm
Same as the brute force algorithm that tries
each path.
169Memoization
Assign one friend to each sub-instance.
170Set of Sub-Instances
Determine the complete 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,
171Set of Sub-Instances
Guess the complete set of sub-Instances.
Yes
No
No
This may appear on a line,but it will never be a
sub-Instance for a friend.
172Set of Sub-Instances
Guess the complete set of sub-Instances.
- The set S of sub-Instances needs to
- include our given I
- closed under friend operation
? sub-Instance ? S ?
173Set of Sub-Instances
Guess the complete set of sub-Instances.
Love life man while there as we be, 11
- The set S of sub-Instances needs to
- include our given I
- closed under friend operation
? sub-Instance ? S ?
subsub-Instance ? i
174Set of Sub-Instances
Guess the complete set of sub-Instances.
Love life man while there as we be, 11
- The set S of sub-Instances needs to
- include our given I
- closed under friend operation
175Set of Sub-Instances
Guess the complete set of sub-Instances.
- The set S of sub-Instances needs to
- include our given I
- closed under friend operation
A fine set of sub-instances!
176Set of Sub-Instances
The complete set of sub-Instances.
In what order should they go?
- in an order such that no friend must wait.
- from smallest to largest
Base Case easy
First
Last
Instance to be solved.
177 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
178Love life man while there as we be, 11
The 5th sub-instance is
Love life man while there, 11
5 words
with 4, 4, 3, 5, 5 letters.
179Love life man while there as we be, 11
The 5th sub-instance is
Love life man while there, 11
Its solution is
with 2,2,1 words on each line.
The birds advice is 1 word on last.
Solutions cost is
23 23 63 232
180Love 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
181Love life man while there as we be, 11
182Love life man while there as we be, 11
183Love life man while there as we be, 11
184Love life man while there as we be, 11
185Love life man while there as we be, 11
186Love life man while there as we be, 11
187- Be clear what are
- the sub-instances
- a solution
- the cost of a solution.
188Table indexed by sub-instances. Be clear what
they are.
189Loop through the sub-instances. Be clear which
is the current one.
190Loop through the bird answers. Be clear which is
the current one.
191Get help from friend Be clear what sub-instance
you give him.
192How do you formyour solution from the friends
and fromthe birds?
193How do you formyour cost from the friends and
fromthe birds?
194What are the base casesub-instances? What are
their solutionsand costs?
195Return the solutionand cost for the original
instance.
196Running Time
Time
Space
of Sub-Instances
q(n)
197Find Optimal Path
Previous algorithm gives cost and birds advice.
198Love life man while there as we be, 11
199Find Optimal Path
Previous algorithm gives cost and birds advice.
200Making Change
- To find the minimum number of Canadian coins to
make any amount, the greedy method always works - At each step, just choose the largest coin that
does not overshoot the desired amount - The greedy method would not work if we did not
have 5 coins - For 31 cents, the greedy method gives seven coins
(25111111), but we can do it with four
(1010101) - The greedy method also would not work if we had a
21 coin - For 63 cents, the greedy method gives six coins
(252510111), but we can do it with three
(212121) - How can we find the minimum number of coins for
any given set of denominations?
201Longest 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
202Longest Common Subsequence problem
X a s b e t c h d a Y r t w a b g j c k t f d
The cost The length of Z.
The goal is to find a longest common subsequence.
203Bird Friend Algorithm
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 include
204Bird Friend Algorithm
Is the last character of either X or Y included
in Z?
- She answers
- Last of X is not included
205Bird Friend Algorithm
Is the last character of either X or Y included
in Z?
- She answers
- Last of X is not included
My friend answers
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.
206Bird Friend Algorithm
Is the last character of either X or Y included
in Z?
- She answers
- Last of Y is not included
207Bird Friend Algorithm
Is the last character of either X or Y included
in Z?
- She answers
- Last of Y is not included
My friend answers
the same Z.
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
Not as good as lastbut we need to try.
208Bird Friend Algorithm
An instance
X a s b e t c h d Y r t w a b g j c k d f d
Last chars equal
Is the last character of either X or Y included
in Z?
- She answers
- Last of X and last of Y are both included
209Bird Friend Algorithm
An instance
X a s b e t c h d Y r t w a b g j c k d f d
Last chars equal
Is the last character of either X or Y included
in Z?
- She answers
- Last of X and last of Y are both included
My friend answers
Z a b c X a s b e t c h
Y r t w a b g j c k d f
Zd abcd.
210Bird Friend Algorithm
Last chars not equal
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 politely tell her that she is wrong.
211Bird Friend Algorithm
Is the last character of either X or Y included
in Z?
- She answers
- Last of X is included
212Bird Friend Algorithm
Is the last character of either X or Y included
in Z?
- She answers
- Last of X is included
My friend answers
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
Wrong
Za abcda.
213Bird Friend Algorithm
Is the last character of either X or Y included
in Z?
- She answers
- Last of X is included
214Bird Friend Algorithm
Is the last character of either X or Y included
in Z?
- She answers
- Last of X is included
My friend answers
Z t X a s b e t c h d
Y r t w
Za ta.
215Bird Friend Algorithm
Last chars not equal
Is the last character of either X or Y included
in Z?
Can we eliminatesome of her answers?
- 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 include
Given any optSolshe needs to havea valid answer.
?
?
?
216Bird Friend Algorithm
Last chars not equal
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 include
3
of answers K
217Same as Brute Force Algorithm
I try each of 3 bird ans.
My friends tries 3
His friends tries 3
Same as the brute force algorithm that tries
each solution.
218Memorization
Assign one friend to each sub-instances.
219Set of Sub-Instances
Determine the complete set of sub-Instances.
Given an instance I,
X a s b e t c h d a Y 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?
X a s b e t c Y r t w a b g j c k
Yes
220Set of Sub-Instances
Determine the complete set of sub-Instances.
Given an instance I,
X a s b e t c h d a Y 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?
No
X b e t Y a b g j c k
221Set of Sub-Instances
Determine the complete set of sub-Instances.
Given an instance I,
X a s b e t c h d a Y 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,xiY y1,,yj
X Y of these.
222Set 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 X j Y
223Set of Sub-Instances
Guess the complete set S of sub-Instances.
- The set S of sub-Instances needs to
- include our given I
- closed under friend operation
? sub-Instance ? S ?
subsub-Instance ? S
? S
224Set of Sub-Instances
Guess the complete set S of sub-Instances.
- The set S of sub-Instances needs to
- include our given I
- closed under friend operation
? sub-Instance ? S ?
subsub-Instance ? S
We showed this.
This is a fine set of sub-Instances!
225Set of Sub-Instances
The complete set S of sub-Instances.
For a table indexed by this sub-Instances.
sub-Instances are parameterized by i j,hence
parameterize the table by i j.
226(No Transcript)
227Table
Original instance I ltX,Ygt
228Table
Optimal Solution Longest Common
Subsequence
Cost length of LCS.
229Table
Yj
Xi
Optimal Solution Longest Common
Subsequence
230Table
Yj
Xi
Optimal Solution Longest Common
Subsequence
- Birds Advice
- delete xi
- take both xi and yj
231Table
Yj
Xi
Optimal Solution Longest Common
Subsequence
- Birds Advice
- delete xi
- delete yj
- take both xi and yj
232Fill in Box
Yj
Xi
- Fill in box
- Try all birds ans.
Friends sub-Instance Our cost friends cost
5
233Fill in Box
Yj
Xi
- Fill in box
- Try all birds ans.
Friends sub-Instance Our cost friends cost
5
234Fill in Box
Yj
Xi
- Fill in box
- Try all birds ans.
Friends sub-Instance Our cost friends cost
1
6
235Fill in Box
Yj
Xi
- Fill in box
- Try all birds ans.
- Take best of best
6
236Fill in Box
Yj
Xi
- Fill in box
- Try all birds ans.
Friends sub-Instance Our cost friends cost
4
237Fill in Box
Yj
Xi
- Fill in box
- Try all birds ans.
Friends sub-Instance Our cost friends cost
3
238Fill in Box
Yj
Xi
- Fill in box
- Try all birds ans.
Sorry bird is wrong. Our cost -?
-?
239Fill in Box
Yj
Xi
- Fill in box
- Try all birds ans.
- Take best of best
240Fill in Box
241Fill in Box
242Order to Fill in Table
- Order to fill table
- so that nobody waits
- This guy waits for
243Order to Fill in Table
(later)
244Base Cases
- Base Cases
- general algorithm
- does not work
- This guys friends are
245Base Cases
- Base Cases
- general algorithm
- does not work
246Base Cases
247With Advice
248With Advice
Done
249Knapsack Problem
Get as much value as you can into the knapsack
250Knapsack Problem
- Ingredients
- Instances The volume V of the knapsack.
The volume and price of n objects
ltltv1,p1gt,ltv2,p2gt, ,ltvn,pngtgt. - 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.
251Greedy Algorithm
Greedy Criteria
Most valuable pi
v4,p4
v4,p4
v7,p5
V8
gives 8
Optimal
Greedy give 5
252Greedy Algorithm
Greedy Criteria
v4,p4
v4,p4
v7,p5
V8
V7
gives 5
Optimal
Greedy give 4
253Greedy Algorithm
Greedy Criteria
If fractional solutions are allowed.
Works
Often an Integersolution is MUCH harder to find.
v4,p4
V7
Greedy give 4
¾ 4 7
Optimal
254Bird Friend Algorithm
My instance
ltVltv1,p1gt,ltv2,p2gt,..........................,ltvn,
pngtgt.
A solution
ltltv5,p5gt,ltv9,p9gt,...........,ltv82,p82gtgt.
What is the last object to take?
n
of answers K
255Bird Friend Algorithm
My instance
ltVltv1,p1gt,ltv2,p2gt,..........................,ltvn,
pngtgt.
A solution
ltltv5,p5gt,ltv9,p9gt,...........,ltv82,p82gtgt.
Do we keep the last object?
2 Yes No
of answers K
256Bird Friend Algorithm
My instance
ltVltv1,p1gt,ltv2,p2gt,..........................,ltvn,
pngtgt.
v4,p4
v4,p4
v7,p5
V9
Trust her and put it into your knapsack.
To fill the rest of the knapsack.
But what instance do I give him?
257Bird Friend Algorithm
His instance
ltV-vnltv1,p1gt,ltv2,p2gt,.........ltvn-1,pn-1gt,ltvn,pngt
gt.
v4,p4
v4,p4
v7,p5
V9-4
My solution
same pn
My cost
258Bird Friend Algorithm
My instance
ltVltv1,p1gt,ltv2,p2gt,..........................,ltvn,
pngtgt.
If we trust the bird and friend, this is valid
and optimal.
My solution
same pn
My cost
259Bird Friend Algorithm
My instance
ltVltv1,p1gt,ltv2,p2gt,..........................,ltvn,
pngtgt.
v4,p4
v4,p4
v7,p5
V9
Trust her and delete it.
To fill the knapsack with the rest.
What instance do I give him?
260Bird Friend Algorithm
His instance
ltVltv1,p1gt,ltv2,p2gt,..........................,ltvn,
pngtgt.
v4,p4
v4,p4
v7,p5
V9
My solution
same
If we trust the bird and friend, this is valid
and optimal.
same
My cost
261Same as Brute Force Algorithm
I try each of 2 bird ans.
My friends tries 2
His friends tries 2
Same as the brute force algorithm that tries
each solution.
262Memoization
Assign one friend to each sub-instances.
263Set 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,
Is this a sub-Instance?
ltVltv1,p1gt,ltv2,p2gt,ltv3,p3gtgt.
Yes, if the bird keeps saying No.
264Set of Sub-Instances
Given an instance I,
ltVltv1,p1gt,ltv2,p2gt,ltv3,p3gt,ltv4,p4gt,ltv5,p5gt,ltv6,p6gt
gt.
- Imagine running the recursive algorithm on it.
- Determine the complete set of sub-Instances ever
given to you, your friends, their friends,
Is this a sub-Instance?
ltVltv1,p1gt,ltv2,p2gt,ltv3,p3gt,ltv4,p4gt,ltv5,p5gt,ltv6,p6gt
gt.
No, the set of objects is always a prefix
of the original set.
265Set of Sub-Instances
Given an instance I,
ltVltv1,p1gt,ltv2,p2gt,ltv3,p3gt,ltv4,p4gt,ltv5,p5gt,ltv6,p6gt
gt.
- Imagine running the recursive algorithm on it.
- Determine the complete set of sub-Instances ever
given to you, your friends, their friends,
Quite possibly, if V ? V.
It is easier to solve than to determine if it is
a sub-instance.
266Set of Sub-Instances
My instance
ltVltv1,p1gt,ltv2,p2gt,..........................,ltvn,
pngtgt.
Guess the complete set S of sub-Instances.
- The set S of sub-Instances needs to
- include our given I
Yes VV i n
- closed under friend operation
? sub-Instance ? S ?
subsub-Instance ? S
ltVltv1,p1gt,ltv2,p2gt,......,ltvi,pigtgt ? S
ltV ltv1,p1gt,ltv2,p2gt,...,ltvi-1,pi-1gtgt
ltV-viltv1,p1gt,ltv2,p2gt,...,ltvi-1,pi-1gtgt
Yes
267The Table
0
1
2
V-vi
V
V
OptSol Cost Birds Advicefor this
sub-Instance
0
1
2
i-1
Yes
i
Take best of best.
Our cost?
n
268The Table
0
1
2
V-vi
V
V
OptSol Cost Birds Advicefor this
sub-Instance
0
1
2
i-1
Order to fill so nobody waits?
i
n
269(No Transcript)
270(No Transcript)
271Running Time
My instance
ltVltv1,p1gt,ltv2,p2gt,..........................,ltvn,
pngtgt.
Running time ?( of sub-instances
bird answers ) ?( Vn
2 ) ?( 2bits in
V n )
Polynomial?
Exponential in size in instance!
272The Knapsack Problem
- Dynamic Programming Running time ?( V
n ) ?( 2bits in
V n ) - Poly time if size of knapsack is small
- Exponential time if size is an arbitrary integer.
273The Knapsack Problem
- Dynamic Programming Running time ?( V
n ) ?( 2bits in
V n ) - NP-Complete
If there is a poly-time algorithmfor the
Knapsack Problem
For EVERY optimization problem there is a
poly-time algorithm.
274The Knapsack Problem
- Dynamic Programming Running time ?( V
n ) ?( 2bits in
V n ) - NP-Complete
Likely there is not a poly-time algorithmfor the
Knapsack Problem.
Likely there is not a poly-time algorithmfor
EVERY optimization problem.
275The Knapsack Problem
- Dynamic Programming Running time ?( V
n ) ?( 2bits in
V n ) - NP-Complete
- Approximate Algorithm
- In poly-time, solution can be found that is
(1?) as good as optimal.
done
276The Job/Event Scheduling Problem
Schedule as many events in your room as possible
277The Job/Event Scheduling Problem
- Ingredients
- Instances Events with starting and finishing
times ltlts1,f1gt,lts2,f2gt, ,ltsn,fngtgt. - 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.
278Greedy Algorithm
Greedy Criteria
Earliest Finishing Time
Schedule the event who will free up your room
for someone else as soon as possible.
Motivation
279Weighted Event Scheduling
- Ingredients
- Instances Events with starting and finishing
times and
weights ltlts1,f1,w1gt,lts2,f2,w2gt,
,ltsn,fn,wngtgt. - 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
280Greedy Algorithm
Greedy Criteria
Earliest Finishing Time
Schedule the event who will free up your room
for someone else as soon as possible.
Motivation
281Bird Friend Algorithm
An instance
ltlts1,f1,w1gt,lts2,f2,w2gt, ,ltsn,fn,wngtgt.
A solution
ltlts5,f5,w5gt,lts9,f9,w9gt, ,lts82,f82,w82gtgt.
What is the last event to take?
n
of answers K
282Bird Friend Algorithm
An instance
ltlts1,f1,w1gt,lts2,f2,w2gt, ,ltsn,fn,wngtgt.
A solution
ltlts5,f5,w5gt,lts9,f9,w9gt, ,lts82,f82,w82gtgt.
Do we keep the last event?
of answers K
2 Yes No
283Bird Friend Algorithm
An instance
ltlts1,f1,w1gt,lts2,f2,w2gt, ,ltsn,fn,wngtgt.
She answers No
His solution
ltlts5,f5,w5gt,lts9,f9,w9gt, ,lts82,f82,w82gtgt.
My solution
same
My cost
same
284Bird Friend Algorithm
An instance
ltlts1,f1,w1gt,lts2,f2,w2gt, ,ltsn,fn,wngtgt.
She answers Yes
His solution
ltlts5,f5,w5gt,lts9,f9,w9gt, ,lts82,f82,w82gtgt.
My solution
same ltsn,fn,wngt.
My cost
same wn
285Bird Friend Algorithm
My instance
Cant keep any events that overlap with it.
286Bird Friend Algorithm
My instance
287Bird Friend Algorithm
His instance
His solution
My solution
My solution
same ltsn,fn,wngt.
Valid?
288Bird Friend Algorithm
My instance
My solution
My solution
same ltsn,fn,wngt.
Yes
Valid?
289Bird Friend Algorithm
My instance
My solution
My solution
same ltsn,fn,wngt.
My cost
same wn
290Same as Brute Force Algorithm
I try each of 2 bird ans.
My friends tries 2
His friends tries 2
Same as the brute force algorithm that tries
each solution.
291Memorization
Assign one friend to each sub-instances.
292Set of Sub-Instances
Determine the complete set of sub-Instances.
Given an instance I,
- Imagine running the recursive algorithm on it.
- Determine the complete set of sub-Instances ever
g