Recursive Back Tracking - PowerPoint PPT Presentation

1 / 355
About This Presentation
Title:

Recursive Back Tracking

Description:

... needs to have an optimal substructure, And finding such a sub ... Optimal substructure does not mean that. If you have optimal solutions to all subproblems... – PowerPoint PPT presentation

Number of Views:95
Avg rating:3.0/5.0
Slides: 356
Provided by: JeffEd3
Category:

less

Transcript and Presenter's Notes

Title: Recursive Back Tracking


1
Recursive Back TrackingDynamic Programming
Thinking about Algorithms Abstractly
  • Jeff Edmonds York University

COSC 3101
Lecture 7
2
Problems
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
3
Dynamic 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.

4
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.

5
Optimization 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.

6
Optimization 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!

7
Search Graph For Best Path
We use it because it nicely demonstrates the
concepts in a graphical way.
8
Search Graph For Best Path
9
Search Graph For Best Path
An instance (input) consists of ltG,s,tgt.
10
Brute Force Algorithm
11
An 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.
12
An 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!
13
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.

14
An 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)
16
Little 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.
17
Sub-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?

18
Little 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.
19
Faulty 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 ..
20
Faulty Bird
the best path from amongst those starting with
lts,v1gt.
21
Faulty Bird
and the best path from amongst those starting
with lts,v2gt.
22
Faulty Bird
and the best path from amongst those starting
with lts,v3gt.
23
Faulty Bird
and the best path from amongst those starting
with lts,v4gt.
24
Faulty Bird
At least one of these four paths must be an over
all best path.
25
Best of the Best
26
Recursive backtracking code always has thissame
basic structure.
27
  • Be clear what are
  • the sub-instance
  • a solution
  • the cost of a sol.

28
Loop through the bird answers. Be clear which is
the current one being tried.
29
Give the bird friend algorithmas a comment.
30
What is the bird asked? What does she answer?
31
Get help from friend Be clear what sub-instance
you give him. Store the solution cost he
gives you.
32
How do you formyour solution from the friends
and fromthe birds?
33
How do you formyour cost from the friends and
fromthe birds?
34
Take the bestof the best
35
What are the base case sub-instances? What are
their solutionsand costs?
36
Return the solutionand cost for the original
instance.
37
Optimal 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.
38
Optimal 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.
39
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.

40
Dont all problems have this optimal substructure
property?
Optimal Substructure
41
Longest 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
42
Same as Brute Force Algorithm
Same as the brute force algorithm that tries
each path.
43
Same as Brute Force Algorithm
44
Speeding 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.

45
Speeding 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.

46
Speeding 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.

47
Exponential Time Redoing Work
Which is the best path from v7 to t?
How many friends solve this sub-instance?
48
Exponential Time Redoing Work
Which is the best path from s to t?
49
Exponential Time Redoing Work
Which is the best path from v1 to t?
50
Exponential Time Redoing Work
Which is the best path from v4 to t?
51
Exponential Time Redoing Work
Which is the best path from v7 to t?
Theres one.
52
Exponential Time Redoing Work
Which is the best path from s to t?
53
Exponential Time Redoing Work
Which is the best path from v3 to t?
54
Exponential Time Redoing Work
Which is the best path from v5 to t?
55
Exponential Time Redoing Work
Which is the best path from v7 to t?
Theres another.
56
Exponential 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.
57
Depth 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.
65
Set 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,
66
Set 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.
67
Set 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.
68
Set 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.
69
Set 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
70
Set of Sub-Instances
Guess the complete set S of sub-Instances is
Assign one friend to each sub-Instance.
71
Set of Sub-Instances
Guess the complete set S of sub-Instances is
  • The set S of sub-Instances needs to
  • include our given I

72
Set 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
73
Set 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,
74
Set 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!
75
Order 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.
76
Order 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
110
Communication
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
111
Dynamic Programming code always has thissame
basic structure.
112
  • Be clear what are
  • the sub-instances
  • a solution
  • the cost of a solution.

113
Table indexed by sub-instances. Be clear what
they are.
114
Loop through the sub-instances. In what
order? Be clear which the current one is.
115
Loop through the bird answers. Be clear which is
the current one being tried.
116
Give the bird friend algorithmas a comment.
117
What is the bird asked? What does she answer?
118
Get help from friend Be clear what sub-instance
you give him.
119
How do you formyour solution from the friends
and fromthe birds?
120
How do you formyour cost from the friends and
fromthe birds?
121
Take the bestof the best
122
What are the base case sub-instances? What are
their solutionsand costs?
123
Return the solutionand cost for the original
instance.
124
Reversing
125
Reversing
Determine the complete set of sub-instances
126
Reversing
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)
128
Running Time
Time
of Sub-Instances
of Bird Answers
q(1)
n
d
129
Communication Time
optSolk ltoptSolk,vigt
Size of path
q(n).
130
Running Time
Time
of Sub-Instances
of Bird Answers
size of solution q(n d n)
Space
131
Store Path Costs, not Paths
"What is cost of the best path from s to v7?"
132
Store 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.
133
Store 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.
134
Store 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.
135
Store 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)
137
Leave these lines as comments for extra clarity
for the reader

138

139
Find 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.
140
Find Optimal Path
The bird gives that the last edge of the best
path from s to t is ltv8,tgt.
141
Find Optimal Path
The bird gives that the last edge of the best
path from s to v8 is ltv5,v8gt.
142
Find Optimal Path
The bird gives that the last edge of the best
path from s to v5 is ltv3,v5gt.
143
Find Optimal Path
The bird gives that the last edge of the best
path from s to v3 is lts,v3gt.
144
Find Optimal Path
Done!
145
Find Optimal Path
This could be done iteratively.As an exercise,
design it.
146
Multiple 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
147
Multiple 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
148
Review
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.

149
Review
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.

150
The 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.

151
The 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.
152
The 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.
153
The 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.
154
The 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?
155
The 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.

156
The 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.
157
The 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?

158
The 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?
159
The 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?
160
The Question For the Little Bird
Is the last object of the instance included
in the optimal solution?
of answers K
2, Yes or No
161
The 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.
162
The 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?

163
Printing Neatly
164
Printing 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.
165
Brute 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..
166
Bird 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?
167
Bird 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.
168
Same as Brute Force Algorithm
Same as the brute force algorithm that tries
each path.
169
Memoization
Assign one friend to each sub-instance.
170
Set 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,

171
Set 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.
172
Set 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 ?
173
Set 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
174
Set 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

175
Set 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!
176
Set 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
178
Love 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.
179
Love 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
180
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
181
Love life man while there as we be, 11
182
Love life man while there as we be, 11
183
Love life man while there as we be, 11
184
Love life man while there as we be, 11
185
Love life man while there as we be, 11
186
Love life man while there as we be, 11
187
  • Be clear what are
  • the sub-instances
  • a solution
  • the cost of a solution.

188
Table indexed by sub-instances. Be clear what
they are.
189
Loop through the sub-instances. Be clear which
is the current one.
190
Loop through the bird answers. Be clear which is
the current one.
191
Get help from friend Be clear what sub-instance
you give him.
192
How do you formyour solution from the friends
and fromthe birds?
193
How do you formyour cost from the friends and
fromthe birds?
194
What are the base casesub-instances? What are
their solutionsand costs?
195
Return the solutionand cost for the original
instance.
196
Running Time
Time
Space
of Sub-Instances
q(n)
197
Find Optimal Path
Previous algorithm gives cost and birds advice.
198
Love life man while there as we be, 11
199
Find Optimal Path
Previous algorithm gives cost and birds advice.
200
Making 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?

201
Longest Common Subsequence problem
X a s b e f c h d a Y r t w a b g j c k t f d
202
Longest 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.
203
Bird 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

204
Bird Friend Algorithm
Is the last character of either X or Y included
in Z?
  • She answers
  • Last of X is not included

205
Bird 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.
206
Bird Friend Algorithm
Is the last character of either X or Y included
in Z?
  • She answers
  • Last of Y is not included

207
Bird 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.
208
Bird 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

209
Bird 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.
210
Bird 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.
211
Bird Friend Algorithm
Is the last character of either X or Y included
in Z?
  • She answers
  • Last of X is included

212
Bird 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.
213
Bird Friend Algorithm
Is the last character of either X or Y included
in Z?
  • She answers
  • Last of X is included

214
Bird 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.
215
Bird 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.
?
?
?
216
Bird 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
217
Same 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.
218
Memorization
Assign one friend to each sub-instances.
219
Set 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
220
Set 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
221
Set 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.
222
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 X j Y
223
Set 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
224
Set 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!
225
Set 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)
227
Table
Original instance I ltX,Ygt
228
Table
Optimal Solution Longest Common
Subsequence
Cost length of LCS.
229
Table
Yj
Xi
Optimal Solution Longest Common
Subsequence
  • Birds Advice
  • delete xi

230
Table
Yj
Xi
Optimal Solution Longest Common
Subsequence
  • Birds Advice
  • delete xi
  • take both xi and yj

231
Table
Yj
Xi
Optimal Solution Longest Common
Subsequence
  • Birds Advice
  • delete xi
  • delete yj
  • take both xi and yj

232
Fill in Box
Yj
Xi
  • Fill in box
  • Try all birds ans.
  • delete xi

Friends sub-Instance Our cost friends cost
5
233
Fill in Box
Yj
Xi
  • Fill in box
  • Try all birds ans.
  • delete yj

Friends sub-Instance Our cost friends cost
5
234
Fill in Box
Yj
Xi
  • Fill in box
  • Try all birds ans.
  • take both xi and yj

Friends sub-Instance Our cost friends cost
1
6
235
Fill in Box
Yj
Xi
  • Fill in box
  • Try all birds ans.
  • Take best of best

6
236
Fill in Box
Yj
Xi
  • Fill in box
  • Try all birds ans.
  • delete xi

Friends sub-Instance Our cost friends cost
4
237
Fill in Box
Yj
Xi
  • Fill in box
  • Try all birds ans.
  • delete yj

Friends sub-Instance Our cost friends cost
3
238
Fill in Box
Yj
Xi
  • Fill in box
  • Try all birds ans.
  • take both xi and yj

Sorry bird is wrong. Our cost -?
-?
239
Fill in Box
Yj
Xi
  • Fill in box
  • Try all birds ans.
  • Take best of best

240
Fill in Box
241
Fill in Box
242
Order to Fill in Table
  • Order to fill table
  • so that nobody waits
  • This guy waits for

243
Order to Fill in Table
(later)
244
Base Cases
  • Base Cases
  • general algorithm
  • does not work
  • This guys friends are

245
Base Cases
  • Base Cases
  • general algorithm
  • does not work

246
Base Cases
247
With Advice
248
With Advice
Done
249
Knapsack Problem
Get as much value as you can into the knapsack
250
Knapsack 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.

251
Greedy Algorithm
Greedy Criteria
Most valuable pi
v4,p4
v4,p4
v7,p5
V8
gives 8
Optimal
Greedy give 5
252
Greedy Algorithm
Greedy Criteria
v4,p4
v4,p4
v7,p5
V8
V7
gives 5
Optimal
Greedy give 4
253
Greedy 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
254
Bird 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
255
Bird 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
256
Bird 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?
257
Bird 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
258
Bird 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
259
Bird 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?
260
Bird 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
261
Same 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.
262
Memoization
Assign one friend to each sub-instances.
263
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,

Is this a sub-Instance?
ltVltv1,p1gt,ltv2,p2gt,ltv3,p3gtgt.
Yes, if the bird keeps saying No.
264
Set 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.
265
Set 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.
266
Set 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
267
The 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
268
The 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)
271
Running 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!
272
The 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.

273
The 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.
274
The 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.
275
The 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
276
The Job/Event Scheduling Problem
Schedule as many events in your room as possible
277
The 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.

278
Greedy Algorithm
Greedy Criteria
Earliest Finishing Time
Schedule the event who will free up your room
for someone else as soon as possible.
Motivation
279
Weighted 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

280
Greedy Algorithm
Greedy Criteria
Earliest Finishing Time
Schedule the event who will free up your room
for someone else as soon as possible.
Motivation
281
Bird 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
282
Bird 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
283
Bird 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
284
Bird 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
285
Bird Friend Algorithm
My instance
Cant keep any events that overlap with it.
286
Bird Friend Algorithm
My instance
287
Bird Friend Algorithm
His instance
His solution
My solution
My solution
same ltsn,fn,wngt.
Valid?
288
Bird Friend Algorithm
My instance
My solution
My solution
same ltsn,fn,wngt.
Yes
Valid?
289
Bird Friend Algorithm
My instance
My solution
My solution
same ltsn,fn,wngt.
My cost
same wn
290
Same 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.
291
Memorization
Assign one friend to each sub-instances.
292
Set 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
Write a Comment
User Comments (0)
About PowerShow.com