Вы находитесь на странице: 1из 404

Thinking about Algorithms Abstractly

Recursive Back Tracking


&
Dynamic Programming
Jeff Edmonds York University

Lecture 7

COSC 31011

Techniques

Optimization Problems
A Sequence of Decisions
The Little Bird & Friend
Optimal Substructure
Memoization
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

Problems
Bellman Ford
Best Path
Printing Neatly
Longest Common Subsequence
Knapsack Problem
The Event Scheduling Problem
Parsing
Satisfiability

Recursive Back Tracking


Bellman Ford

Consider your instance I.


Ask a little question (to the little bird) about its optimal solution.
Try all answers k.
Knowing k about the solution
restricts 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 Tracking


Bellman Ford
Specification: All Nodes Shortest-Weighted Paths with l edges
<preCond>: The input is a graph G (directed or undirected)
with edge weights (possibly negative) and integer l.
<postCond>: For each u,v, find a shortest path from u to v
with at most l edge.
Stored in a matrix Dist[u,v].

For a recursive algorithm,


we must give our friend
a smaller subinstance.
How can this instance be
made smaller?
Remove a node? and edge?

b 10

25

30

40
3

15

181

l=3
2

3
1
2

v
8

l=4
4

Recursive Back Tracking


Bellman Ford

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+
b 10
1
subsolk,v,l/2
c
u
is the best solution for I
30
2
th
25
40
3
consistent with the k birds
3
1
answer.
2
k
v
d
Try all k and return the best
2
8
of these best solutions.
1
15
2
g
i
l=4
181 h
6
5

Recursive Back Tracking


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.
b 10
1
Given graph G, find Dist[uv,l] for l = u
1,2,4,8, ,n
30
25

40

15

181

c
2

3
1
2

v
8

l=4
6

Dynamic Programming Algorithm


Loop 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] )
b 10
1
c
u
25

30

40
3

15

181

3
1
2

v
8

l=4
7

Dynamic Programming Algorithm


Loop 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] =
Dist[u,v,1] =
Dist[u,u,1] =
% Smallest Instances
for all u,v Verticies
if u,v Edges
Dist[u,v,1] = weight[u,v]
else
Dist[u,v,1] =

10

0 (sometimes useful)

b 10

25

30

40
3

15

181

c
2

3
1
2

v
8

l=4
8

Dynamic Programming Algorithm


Loop 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]

b 10

25

30

40
3

15

181

c
2

3
1
2

v
8

l=4
9

Dynamic Programming Algorithm


Dealing with negative cycles.
Dist[u,v,2] =
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,12] = 25+(3+1-5)2+3+2 = 28
Dist[u,v,303] = 25+(3+1-5)300+3+2 = 25-300 = -275
Dist[u,v,] = 25+(3+1-5)+3+2 = 25-300 = -
b 1
There is a negative cycle if
c
u
Dist[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] =

25

-5
3

10

Dynamic Programming Algorithm


Algorithm BellmanFord(G)
% Smallest Instances
for all u,v Verticies
if u,v Edges
Time = O(n3 logn)
Dist[u,v,1] = weight[u,v]
else
Dont actually need to keep
Dist[u,v,1] =
for l = 2,4,8,16,,2n
old and new values.
l
% Find Dist[uv,l] from Dist[u,v, /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] =
11

Dynamic Programming Algorithm


Algorithm BellmanFord(G)
% Smallest Instances
for 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

Dont actually need to keep


old and new values.

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] =

12

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

13

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

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.

15

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

16

Search Graph For Best Path


We use it because it nicely demonstrates
the concepts in a graphical way.

17

Search Graph For Best Path


An instance (input) consists of <G,s,t>.
G is a weighted directed layered graph
s source node
t sink node
4
2
2

3
5

2
3
3

2
1

18

Search Graph For Best Path


An instance (input) consists of <G,s,t>.
A solution for an instance is a path from s to t.
The cost of a solution is the sum of the weights.
The goal is to find a path with minimum total cost.

2+6+3+7=18
4+2+1+5=12
19

Brute Force Algorithm


Try all paths, return the best.
But there may be an exponential number of paths!

20

An Algorithm As
A Sequence of Decisions
I ask a question about the solution.
Which edge should we take first?
Some how I decide <s,v3>.
My friend asks the next question.
Which edge do we take second?
Some how he decides <v3,v5>.
His friend asks the next question.
Which edge do we take third?
Some how he decided <v5,v8>.
21

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?


Taking the best first edge.
Does not work!
22

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.

23

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.
But let's skip this part
by pretending that we have
a little bird to answer this
little question.
24

"Little Bird" Abstraction

(It is up to you whether or not to use it)


Recall: Non-deterministic Finite Automata
Non-deterministic Turing Machine
0

These have a higher power to tell them


which way to go.
The little bird is a little higher power,
answering a little question about an
optimal solution.

25

Little Bird & Friend Alg


I ask a question about the solution.
Which edge should we take first?
The bird answers <s,v1>.
My friend asks the next question.
Which edge do we take second?
The bird answers <v1,v4>.
But we dont want to
worry about how our friend
solves his problem.
26

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?

27

Little Bird & Friend Alg


The bird answers <s,v1>.
If I trust the little bird, I take step along edge <s,v1>
and ask my friend,
Which is the best path from v1 to t?
Friend answers <v1,v6,t>
with weight 10.
To get my solution
I tack on the birds edge
making the path <s,v1,v6,t>
with weight 10+3=13.
28

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 bird' s answer.
i.e. the best path from s to t
from amongst those
starting with <s,v1>.
Define optS<I,k> to be:
the optimum solution
for instance I
consistent with the kth bird' s answer.

29

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 bird' s answer.
i.e. the best path from s to t
from amongst those
starting with <s,v1>.
In reality we will try
all possible first edges,
giving ..

30

Faulty Bird

the best path from amongst


those starting with <s,v1>.

31

Faulty Bird

and the best path from amongst


those starting with <s,v2>.

32

Faulty Bird

and the best path from amongst


those starting with <s,v3>.

33

Faulty Bird

and the best path from amongst


those starting with <s,v4>.

34

Faulty Bird

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

Bird/Friend - Best of the Best


Consider our instance I.
Consider the set of solutions

A sequence of question to
a little bird about a solution
forms a tree of possible
answers.

36

Bird/Friend - Best of the Best


Consider our instance I.
Consider the set of solutions
k

But we only care about


the first bird answer.
The answers classifies
the possible solutions.

Solutions consistent
with the kth bird' s answer.
37

Bird/Friend - Best of the Best


Consider our instance I.
Consider the set of solutions
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>

Solutions consistent
with the kth bird' s answer.
38

Bird/Friend - Best of the Best


Consider our instance I.
Consider the set of solutions

optS<I,k>

optS[I]

Define optS<I,k> to be:


the optimum solution
k
for instance I
consistent with the kth bird' s answer.
Do this for each k.
Let kmax be the bird' s answer
giving the best optS<I,k>.
optS[I] = optS<I,k > = Bestk optS<I,k >

max

max

39

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 for
an answer k.
I ask my friend for his solution.
I combine them.
40

Recursive
backtracking
code always has this
same basic structure.

41

Be clear what are


the instances
its solution
the cost of a sol.

42

Loop through the


bird answers.
Be clear which is
the current one
being tried.

43

Give the bird


& friend algorithm
as a comment.
(Unless it is in
an earlier question.)

44

What is the bird


asked?
What does she
answer?

45

Get help from friend


Be clear what
sub-instance
you give him.
Store the solution
& cost
he gives you.
46

How do you form


your solution from
the friends and from
the birds?

47

How do you form


your cost from
the friends and from
the birds?
48

optSolk
is a best solution
for our instance
from amongst
those consistent
with the bird's
kth answer.
Take the best
of the best
49

Return the solution


and cost for the
original instance.
50

Base Cases:
Instances that are too
small to have smaller
instances to give
to friends.
What are these?
What are their
solutions
and costs?

51

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,
i.e. for a path from s to t to be optimal,
the sub-path from vi to t must optimal.
If shorter from vi to t.
shorter to s to t.
52

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,
i.e. for a path from s to t to be optimal,
the sub-path from vi to t must optimal.
And finding such a sub-path is a
sub-instance of the same
computational problem.
53

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.

54

Optimal Substructure

Dont all problems have this


optimal substructure property?

55

Optimal Substructure
Longest simple path

B
3

1
4
Consider the
C
D
A
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

56

Same as Brute Force Algorithm


I try each edge out of s.
A friend tries each edge
out of these.
A friend tries each edge
out of these.
Time?

Same as the brute force algorithm


that tries each path.

57

Same as Brute Force Algorithm

But there may be an exponential number of paths!

58

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

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.

60

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.

61

Exponential Time
Redoing Work
Which is the best path from v7 to t?
How many friends solve this sub-instance?

62

Exponential Time
Redoing Work
Which is the best path from s to t?

63

Exponential Time
Redoing Work
Which is the best path from v1 to t?

64

Exponential Time
Redoing Work
Which is the best path from v4 to t?

65

Exponential Time
Redoing Work
Which is the best path from v7 to t?

Theres one.

66

Exponential Time
Redoing Work
Which is the best path from s to t?

67

Exponential Time
Redoing Work
Which is the best path from v3 to t?

68

Exponential Time
Redoing Work
Which is the best path from v5 to t?

69

Exponential Time
Redoing Work
Which is the best path from v7 to t?

Theres another.

70

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

Depth First
Search
Drop
bread crumbs
and dont revisit.
But we need
shortest path

72

Dynamic Programming

Having many friends


solving this same sub-instance
is a waste of time.
We allocate one friend to the job.

73

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

74

Dynamic Programming

When I need to find


the best path from v4 to t
I will ask you for
the best path from v7 to t
I will find my best path
and tell you.
75

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 path


and will tell you.
76

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 path
and will tell you.
77

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 path
and tell you.
But I hate to wait for you.
Recursion has a lot of overhead
Why dont you go first?
78

Dynamic Programming

Before anyone asks me,


I will find my best path
and remember it.

79

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,
80

Set of Sub-Instances
Guess the complete set S of sub-Instances.
Best path from v7 to t?
Best path from v21 to t?
v21

Yes
No

v21 is not a part of our


original instance.

81

Set of Sub-Instances
Guess the complete set S of sub-Instances.
Best path from v7 to t?
Best path from v21 to t?
Best path from v3 to v7?

Yes
No
No

All paths considered


end in t.

82

Set of Sub-Instances
Guess the complete set S of sub-Instances.
Best path from v7 to t?
Best path from v21 to t?
Best path from v3 to v7?

Yes
No
No

All paths considered


end in t.

83

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
i Best path from vi to t? Yes

84

Set of Sub-Instances
Guess the complete set S of sub-Instances is
i Best path from vi to t?
Assign one friend
to each sub-Instance.

85

Set of Sub-Instances
Guess the complete set S of sub-Instances is
i Best path from vi to t?

The set S of sub-Instances needs to:


include our given I

86

Set of Sub-Instances
Guess the complete set S of sub-Instances is
i Best path from vi to t?

The set S of sub-Instances needs to:


include our given I
closed under friend operation
Integers closed under addition
x,y I x+y I
sub-Instance S
subsub-Instance S

87

Set of Sub-Instances
Guess the complete set S of sub-Instances is
i Best path from vi to t?

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,

88

Set of Sub-Instances
Guess the complete set S of sub-Instances is
i Best path from vi to t?
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!
89

Order to complete
The complete set S of sub-Instances is
i Best path from vi to t?
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.
90

Order to complete
The complete set S of sub-Instances is
i Best path from vi to t?
In what order should they go?
in an order such that
no friend must wait.
from smallest to largest
First

Base Case easy

Last

Instance to be solved.
91

Dynamic Programming
"Which is the best path from t to t?"
Base Case
easy

92

Dynamic Programming
"Which is the best path from v8 to t?"
easy

93

Dynamic Programming
"Which is the best path from v7 to t?"
easy

94

Dynamic Programming
"Which is the best path from v6 to t?"
easy

95

Dynamic Programming
"Which is the best path from v5 to t?"
Harder

96

Dynamic Programming
"Which is the best path from v5 to t?"
Little bird
suggests first
edge <v5,v7>
Friend gives best
path <v7,t>.

97

Dynamic Programming
"Which is the best path from v5 to t?"
Little bird
suggests first
edge <v5,v8>
Friend gives best
path <v8,t>.

98

Dynamic Programming
"Which is the best path from v5 to t?"
Take best of best

99

Dynamic Programming
"Which is the best path from v4 to t?"

100

Dynamic Programming
"Which is the best path from v4 to t?"
Little bird
suggests first
edge <v4,v6>
Friend gives best
path <v7,t>.

101

Dynamic Programming
"Which is the best path from v4 to t?"
Little bird
suggests first
edge <v4,t>
Friend gives best
path <t,t>.

102

Dynamic Programming
"Which is the best path from v4 to t?"
Little bird
suggests first
edge <v4,v7>
Friend gives best
path <v7,t>.

103

Dynamic Programming
"Which is the best path from v4 to t?"
Take best of best

104

Dynamic Programming
"Which is the best path from v3 to t?"

105

Dynamic Programming
"Which is the best path from v3 to t?"
Little bird
suggests first
edge <v3,v5>
Friend gives best
path <v5,t>.

106

Dynamic Programming
"Which is the best path from v3 to t?"
Little bird
suggests first
edge <v3,v8>
Friend gives best
path <v8,t>.

107

Dynamic Programming
"Which is the best path from v3 to t?"
Take best of best

108

Dynamic Programming
"Which is the best path from v2 to t?"

109

Dynamic Programming
"Which is the best path from v2 to t?"
Little bird
suggests first
edge <v2,v4>
Friend gives best
path <v4,t>.

110

Dynamic Programming
"Which is the best path from v2 to t?"
Little bird
suggests first
edge <v2,v7>
Friend gives best
path <v7,t>.

111

Dynamic Programming
"Which is the best path from v2 to t?"
Take best of best

112

Dynamic Programming
"Which is the best path from v1 to t?"

113

Dynamic Programming
"Which is the best path from v1 to t?"
Little bird
suggests first
edge <v1,v3>
Friend gives best
path <v3,t>.

114

Dynamic Programming
"Which is the best path from v1 to t?"
Little bird
suggests first
edge <v1,v4>
Friend gives best
path <v4,t>.

115

Dynamic Programming
"Which is the best path from v1 to t?"
Little bird
suggests first
edge <v1,v5>
Friend gives best
path <v5,t>.

116

Dynamic Programming
"Which is the best path from v1 to t?"
Take best of best

117

Dynamic Programming
"Which is the best path from s to t?"
Original Problem

118

Dynamic Programming
"Which is the best path from s to t?"
Little bird
suggests first
edge <s,v1>
Friend gives best
path <v1,t>.

119

Dynamic Programming
"Which is the best path from s to t?"
Little bird
suggests first
edge <s,v2>
Friend gives best
path <v2,t>.

120

Dynamic Programming
"Which is the best path from s to t?"
Little bird
suggests first
edge <s,v3>
Friend gives best
path <v3,t>.

121

Dynamic Programming
"Which is the best path from s to t?"
Little bird
suggests first
edge <s,v4>
Friend gives best
path <v4,t>.

122

Dynamic Programming
"Which is the best path from s to t?"
Take best of best
DONE

123

Dynamic Programming
Construct a table
for storing an optimal solution & cost
for each sub-instance.
Map
i Best path from vi to t?
Sub-Instances
i [n], i.e. for each node vi

Indexes
Cell of table

Which is the best path from vi to t?


t,

v,

v , vi , v , .,

124

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,
Base case

v7, v6, v5, .,

s
Original

125

Communication
Friend k gives friend i a best path from vk to t.
Recursive BackTracking
i <optSubSol,optSubCost>
= LeveledGraph(<G,vk,t>)
k return(optSolmin,optCostmin) ,

i
k

Dynamic Programming
k optSol[k] = optSolmin
i

optSubSol = optSol[k]
optSolk = <vi,vk> + optSol[k]
126

Dynamic
Programming
code always has this
same basic structure.

127

Be clear what are


the instances
its solution
the cost of
a solution.

128

Dynamic Programs
do not recurse making
the instance smaller
and smaller.
Instead, it up front
determines the set S
of all sub-instances
that ever need
to be solved.
Be clear what
sub-instances are.
129

Be clear what
sub-instances are.

How are they indexed?


Tables indexed by
these sub-instances
store an optimal
solution and its cost.

130

The set S
of sub-instances
are solved from
smallest to largest
so that no body waits.
Base Cases:
Instances that are too
small to have smaller
instances to give
to friends.
They get solved first
and their solutions
stored.

131

Then we iterate
through the remaining
sub-instances.
From smallest
to largest.
Each gets solved
and their solutions
stored.
132

Consider yourself
to be a friend
working on one
of these.
Be clear which
sub-instance is yours.
Solve this as you
did before.

133

Loop through the


bird answers.
Be clear which is
the current one
being tried.
134

Give the bird


& friend algorithm
as a comment.
(Unless it is in
an earlier question.)

135

What is the bird


asked?
What does she
answer?

136

Get help from friend

Be clear what
sub-instance
you give your friend.

137

Get help from friend


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.

138

How do you form


your solution from
the friends and from
the birds?
139

How do you form


your cost from
the friends and from
the birds? 140

optSol<i,k>
is a best solution for
our instance subI[i]
from amongst
those consistent
with the bird's
kth answer.
Take the best
of the best
141

Store the solution to


our instance subI[i]
in the table.

142

Base Cases:
Instances that are too
small to have smaller
instances to give
to friends.
Is this code correct?

143

Dynamic Programs
do not recurse making
the instance smaller
and smaller.
Hence, lets not worry
about our instance I
being a base case.

144

But there is a table


of subinstances
that must be solved.
Some of these will be
base cases
and their solutions
must be stored
in the table.

145

t=
n

But there is a table


of subinstances
that must be solved.
Some of these will be
base cases
and their solutions
must be stored
in the table.

146

But there is a table


of subinstances
that must be solved.
Then we solve
the rest.

147

s=
0

Return the solution


and cost for the
original instance.148

Order Feels Backwards

Path from s to t.

Path from t to t.
149

An Esthetically Better

Path from s to s.

Path from s to t.
150

Reversing

151

Reversing
Determine the complete set of sub-instances
Which is the best path from s to vi? i

152

Reversing
Fill out a table containing
an optimal solution for each sub-instance.
Which is the best path from s to vi?
s, v1,
Base case

v2, v3, v4, .,

t
Original

153

154

Running Time

Time =
# of Sub-Instances
# of Bird Answers
?
= n d

155

Communication Time
optSolk = <optSol[k],vi>
Friend k gives best path from s to vk
to friend i, who adds the edge <vk,vi>.
k

Time =

Size of path = n
Time = n

156

Running Time

Time =
# of Sub-Instances
# of Bird Answers
size of solution
= (n d n)
Store path costs,
not paths
Space =
# of Sub-Instances

= n)
157

Store Path Costs, not Paths


"What is cost of the best path from s to v7?"

158

Store Path Costs, not Paths


"What is cost of the best path from s to v7?"
8

Little bird suggests last


edge <v4,v7> with weight 2.
Friend gives cost 8
of best path <s,v4>.
Best cost via <v4,v7> is 8+2=10.

159

Store Path Costs, not Paths


"What is cost of the best path from s to v7?"
2

Little bird suggests last


edge <v2,v7> with weight 7.
Friend gives cost 2
of best path <s,v2>.
Best cost via <v2,v7> is 2+7=9.

160

Store Path Costs, not Paths


"What is cost of the best path from s to v7?"
6

Little bird suggests last


edge <v5,v7> with weight 5.
Friend gives cost 6
of best path <s,v5>.
Best cost via <v5,v7> is 6+5=11.

161

Store Path Costs, not Paths


"What is cost of the best path from s to v7?"
2

Take best of best: 10, 9, 11

We also learn
the wise little birds advice.
We will store this in the table too.

162

Running Time

birdsAdvice[i] = kmin
163

Leave these lines


as comments
for extra clarity
for the reader

164

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.

165

Find Optimal Path


The bird gives that
the last edge
of the best path from
s to t is <v8,t>.

166

Find Optimal Path


The bird gives that
the last edge
of the best path from
s to v8 is <v5,v8>.

167

Find Optimal Path


The bird gives that
the last edge
of the best path from
s to v5 is <v3,v5>.

168

Find Optimal Path


The bird gives that
the last edge
of the best path from
s to v3 is <s,v3>.

169

Find Optimal Path


Done!

170

Find Optimal Path

This could be done iteratively.


As an exercise, design it.
171

Multiple Optimal Solutions


I ask the bird:
Which is the last edge?
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
172

Multiple Optimal Solutions


I ask the bird:
Which is the last edge?
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.

6
173

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

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

The Question For the Little Bird


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 question
helping 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.

176

The Question For the Little Bird


An instance: Graph, s, and t
A solution: a path
t
s
I ask the bird:
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.177

The Question For the Little Bird


An instance: Graph, s, and t
A solution: a path
t
s
I ask the bird:
What is the last 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

The Question For the Little Bird


An instance: Graph, s, and t
A solution: a path
t
s
I ask the bird:
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.
What is the rest of the path?

179

The Question For the Little Bird


An instance: Graph, s, and t
A solution: a path
t
s
I ask the bird:
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?

180

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.

Eg. What is best last edge?


K = number of edges into node t.

t
s

181

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.

Eg. What is an optimal solution?


K=

# of solutions.

Trying all is the Brute Force algorithm.


182

The Question For the Little Bird


An instance: Graph, s, and t
A solution: a path
t
s
I ask the bird:
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
183
to ask?

The Question For the Little Bird


An instance: ???
A solution: a sequence of objects
Z=abcd
I ask the bird:
What is the last object in the sequence?
# of answers K = # of possible last objects.
I ask my friend:
What is the rest of the solution?
184

The Question For the Little Bird


An instance: a sequence of objects
X == aa ss bb ee ff cc hh dd a
X
A solution: a subset of these objects
Z=abcd
I ask the bird:
What is the last object in the sequence?
# of answers K = # of possible last objects.
Is there a smaller question that we could ask?
185

The Question For the Little Bird


An instance: a sequence of objects
X=asbefchda
A solution: a subset of these objects
Z=abcd
I ask the bird:
Is the last object of the instance included
in the optimal solution?
# of answers K = 2, Yes or No

186

The Question For the Little Bird


An instance: ???
A solution: a binary tree of objects
38
25
17

51
31

4 21
28 35
I ask the bird:
What object is at the root?

I ask my friend:

42
40

63
49

55

71

I ask a second friend:

What is the left sub-tree?What is the right sub-tree?


Previous problems had one friend given a bird ans.187

The Question For the Little Bird


An instance: ???
A solution: a binary tree of objects
38
25
17

51
31

42

63

4 21
28 35 40 49
55 71
I ask the bird:
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?
188

Printing Neatly

189

Printing Neatly
An instance: text to print neatly & # chars per line
Love life man while there as we be, 11
A solution: # of words to put on each line.
The cost: a measure of how neat,
few blanks on the end of each line.
small punishment
8
8
216 big punishment
27
259
11
The goal is to to print it as neatly as possible. 190
Love.life..
man.while..
there......
as.we.be...

23
23
63
33

=
=
=
=

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.man...
love.......
life.man...
love.life..
man........
191

Bird & Friend Algorithm


An instance:Love life man while there as we be, 11
I ask the bird:
How many words on the last line?
She may answer 3.
I ask the friend:
Which is the best way to print
the remaining n-3 words?

I combine
birds and
friends answers.

192

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.
We try the bird
answers 12words,
34
5
and take best of best.

193

Same as Brute Force Algorithm

I try each # words on last line.


A friend tries # on next.
A friend tries # on next.
Time?

Same as the brute force algorithm


that tries each path.

194

Memoization
Assign one friend to each sub-instance.
Which is the best path from vi to t? i

195

Set of Sub-Instances
Determine the complete set of sub-Instances.
Given an instance I,
Love life man while there as we be, 11
Imagine running the recursive algorithm on
it.
Determine the complete set of subInstances ever given to you, your friends,
their friends,
196

Set of Sub-Instances
Guess the complete set of sub-Instances.
Love life man while there as we be, 11
Love life man while there, 11
Hi there, 81
man while, 11

Yes
No
No

This may appear on a line,


but it will never be a sub-Instance for a friend.
197

Set of Sub-Instances
The set of sub-Instances is the set of prefixes.
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
Love life man while, 11
Love life man, 11
Love life, 11
Love, 11 The set S of sub-Instances needs to:
, 11
include our given I
closed under friend operation
sub-Instance S
198

Set of Sub-Instances
The set of sub-Instances is the set of prefixes.
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
Love life man while, 11
Love life man, 11
Love life, 11
Love, 11 The set S of sub-Instances needs to:
, 11
include our given I
closed under friend operation
The bird
sub-Instance S
answers 12words,
subsub-Instance i
34
5
199

Set of Sub-Instances
The set of sub-Instances is the set of prefixes.
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
Love life man while, 11
Love life man, 11
Love life, 11
Love, 11 The set S of sub-Instances needs to:
, 11
include our given I
closed under friend operation
each sub-Instance needs to be
asked of some friend, friend,
200

Set of Sub-Instances
The set of sub-Instances is the set of prefixes.
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
Love life man while, 11

The bird
answers 1.

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! 201

Set of Sub-Instances
The set of sub-Instances is the set of prefixes.

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
Love life man while, 11
Love life man, 11
Love life, 11
Love, 11
In what order should they go?
, 11
in an order such that
no friend must wait.
from smallest to largest
First Base Case easy
Last
Instance to be solved.
202

The Table
Construct a table
for storing the cost of opt sol and birds advice.
for each sub-instance.
Map
The set of prefixes of words.
Sub-Instances
Indexes

i [n], i.e. for each word.

Cell of table

Which is the best printing of first i words?


i

203

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

204

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

Love life man while there as we be, 11

The 5th sub-instance is


Love.life.. Love life man while there, 11
man.while.. Its solution is
there...... with 2,2,1 words on each line.
The birds advice is 1 word on last.
Solutions cost is 23 + 23 +63 = 232 206

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

207

Love life man while there as we be, 11

Love.life..
man.while..
there.as.we
be.........
208

Love life man while there as we be, 11

Love.life..
man.while..
there.as...
we.be......
209

Love life man while there as we be, 11

Love.life..
man.while..
there......
as.we.be...
210

Love life man while there as we be, 11

there.as.we.be
211

Love life man while there as we be, 11

Tried all bird


answers.
Choose best of the best.
212

Love life man while there as we be, 11

Choose best of the best.


213

Dynamic
Programming
code always has this
same basic structure.

Amusingly,
when formatting
this code, I had to fight
with line breaks to get
the height/width ratio
Printing Neatly.
214

Be clear what are


the instances
its solution
the cost of
a solution.

215

Dynamic Programs
do not recurse making
the instance smaller
and smaller.
Instead, it up front
determines the set S
of all sub-instances
that ever need
to be solved.
Be clear what
sub-instances are.
216

Be clear what
sub-instances are.

How are they indexed?


Tables indexed by
these sub-instances
store an optimal
solution and its cost.

217

The set S
of sub-instances
are solved from
smallest to largest
so that no body waits.
Base Cases:
Instances that are too
small to have smaller
instances to give
to friends.
They get solved first
and their solutions
stored.

218

Then we iterate
through the remaining
sub-instances.
From smallest
to largest.
Each gets solved
and their solutions
stored.
Actually, we store the
birds advice instead
of the solution.
219

Consider yourself
to be a friend
working on one
of these.
Be clear which
sub-instance is yours.
Solve this as you
did before.

220

Loop through the


bird answers.
Be clear which is
the current one
being tried.
221

Give the bird


& friend algorithm
as a comment.
(Unless it is in
an earlier question.)

222

What is the bird


asked?
What does she
answer?

223

i-k

i-k

i-k

Get help from friend

Be clear what
sub-instance
you give your friend.

224

i-k

i-k

i-k

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.

225

How do you form


your solution from
the friends and from
the birds?
226

How do you form


your cost from
the friends and from
the birds? 227

optSol<i,k>
is a best solution for
our instance subI[i]
from amongst
those consistent
with the bird's
kth answer.
Take the best
of the best
228

Store the solution to


our instance subI[i]
in the table.
Actually, we store the
birds advice instead
of the solution.

229

Base Cases:
Instances that are too
small to have smaller
instances to give
to friends.
Is this code correct?

230

Dynamic Programs
do not recurse making
the instance smaller
and smaller.
Hence, lets not worry
about our instance I
being a base case.

231

But there is a table


of subinstances
that must be solved.
Some of these will be
base cases
and their solutions
must be stored
in the table.

232

But there is a table


of subinstances
that must be solved.
Some of these will be
base cases
and their solutions
must be stored
in the table.

233

But there is a table


of subinstances
that must be solved.
Then we solve
the rest.

234

Return the solution


and cost for the
original instance.
But actually,
we dont have
the solution.
We must rerun it,
this time with advice
from the bird. 235

Time =
# of Sub-Instances
# of Bird Answers
= (n n)

Space =
# of Sub-Instances
= n)

236

Find Optimal Path


Previous algorithm gives cost and birds advice.
We run the bird-friend algorithm again,
but with a reliable bird.

237

Love life man while there as we be, 11

Love.life..
man.while..
there
as we be

<2 ,2 ,1,3>

238

Longest Common Subsequence problem

X=asbefchda
Y=rtwabgjcktfd
239

Longest Common Subsequence problem

An instance: Two strings


X
X = a s b e ft c h d a
Y
Y=rtwabgjcktfd
A solution: A common subsequence.
Z=abcd
The cost: The length of Z.

The goal is to find a longest common subsequence.

240

Bird & Friend Algorithm


An instance:
X=asbetchda
Y=rtwabgjcktfd
I ask the bird:
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
241
Both are included

Bird & Friend Algorithm


An instance:
X=asbetchda
Y=rtwabgjcktfd
I ask the bird:
Is the last character of either X or Y included in Z?
She answers:
Last of X is not included
I ask my friend:
The instance:
X=asbetchd
Y=rtwabgjcktfd

242

Bird & Friend Algorithm


An instance:
X=asbetchda
Y=rtwabgjcktfd
I ask the bird:
Is the last character of either X or Y included in Z?
She answers:
Last of X is not included
I combine
My friend answers:
Z=abcd
The instance:
X=asbetchd
Y=rtwabgjcktfd

birds and
friends answers
and give the same Z.
243

Bird & Friend Algorithm


An instance:
X=asbetchda
Y=rtwabgjcktfd
I ask the bird:
Is the last character of either X or Y included in Z?
She answers:
Last of Y is not included
I ask my friend:
The instance:
X=asbetchda
Y=rtwabgjcktf

244

Bird & Friend Algorithm


An instance:
X=asbetchda
Y=rtwabgjcktfd
I ask the bird:
Is the last character of either X or Y included in Z?
She answers:
I combine
Last of Y is not included
birds and
friends answers
My friend answers:
the
same
Z.
and
give
Z=abc
The instance:
XX == aa ss bb ee tt cc hh dd aa
YY == rr tt w
w aa bb gg jj cc kk tt ff

Not as good as last


but we need to try.245

Bird & Friend Algorithm


An instance:
X=asbetchd
Y=rtwabgjckdfd

Last chars equal

I ask the bird:


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=asbetch
Y=rtwabgjckdf

246

Bird & Friend Algorithm


An instance:
X=asbetchd
Y=rtwabgjckdfd

Last chars equal

I ask the bird:


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 combine
My friend answers:
birds and
Z=abc
The instance:
friends answers
X=asbetch
and give
Y=rtwabgjckdf
Zd = abcd.
247

Bird & Friend Algorithm


An instance:
X=asbetchda
Y=rtwabgjcktfd

Last chars not equal

I ask the bird:


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.

248

Bird & Friend Algorithm


An instance:
X=asbetchda
Y=rtwabgjcktfd
I ask the bird:
Is the last character of either X or Y included in Z?
She answers:
Last of X is included
I ask my friend:
The instance:
X=asbetchd
Y=rtwabgjcktfd

249

Bird & Friend Algorithm


An instance:
X=asbetchda
Y=rtwabgjcktfd
I ask the bird:
Is the last character of either X or Y included in Z?
She answers:
Last of X is included
My friend answers:
Z=abcd
The instance:
Wrong
X=asbetchd
Y=rtwabgjcktfd

I combine
birds and
friends answers
and give
250

Bird & Friend Algorithm


An instance:
X=asbetchda
Y=rtwabgjcktfd
I ask the bird:
Is the last character of either X or Y included in Z?
She answers:
Last of X is included
I ask my friend:
The instance:
X=asbetchd
Y=rtw

251

Bird & Friend Algorithm


An instance:
X=asbetchda
Y=rtwabgjcktfd
I ask the bird:
Is the last character of either X or Y included in Z?
She answers:
Last of X is included
I combine
My friend answers:
Z=t
The instance:
X=asbetchd
Y=rtw

birds and
friends answers
and give
Za = ta.
252

Bird & Friend Algorithm


An instance:
X=asbetchda
Y=rtwabgjcktfd

Last chars not equal

I ask the bird:


Is the last character of either X or Y included in Z?
Can we eliminate
She answers one of :
Last of X is not included some of her answers?
Last of Y is not included
Given any optSol
Last of X is included ?
she needs to have
Last of Y is included ?
a valid answer.
Neither are included ?
253
Both are included

Bird & Friend Algorithm


An instance:
X=asbetchda
Y=rtwabgjcktfd

Last chars not equal

I ask the bird:


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
# of answers K = 3
Neither are included
254
Both are included

Same as Brute Force Algorithm


I try each of 3 bird ans.
My friends try 3
His friends try 3
Time?

Same as the brute force algorithm


that tries each solution.

255

Memorization
Assign one friend to each sub-instance.
Which is the best path from vi to t? i

256

Set of Sub-Instances
Determine the complete set of sub-Instances.
Given an instance I,
X=asbetchda
Y=rtwabgjcktfd
Imagine running the recursive alg on it.
Determine the complete set of subInstances ever given to you, your friends,
their friends
Is this a sub-Instance?
X = a s b e t c
Yes
Y = r t w a b g j c k
257

Set of Sub-Instances
Determine the complete set of sub-Instances.
Given an instance I,
X=asbetchda
Y=rtwabgjcktfd
Imagine running the recursive alg on it.
Determine the complete set of subInstances ever given to you, your friends,
their
Is this
a sub-Instance?
friends,

No
X = b e t
Y = a b g j c k

258

Set of Sub-Instances
Determine the complete set of sub-Instances.
Given an instance I,
X=asbetchda
Y=rtwabgjcktfd
Imagine running the recursive alg on it.
Determine the complete set of subInstances ever given to you, your friends,
their
Is this
a sub-Instance?
friends,

Yes
X = x1,xi
i [0..|X|] j [0..|Y|]
Y = y1,,yj |X| |Y| of these.

259

Set of Sub-Instances
Guess the complete set S of sub-Instances.
Xi = x1,xi
Yj = y1,,yj
i [0..|X|] j [0..|Y|]
The set S of sub-Instances needs to:
include our given I
Yes: i = |X| & j = |Y|

260

Set of Sub-Instances
Guess the complete set S of sub-Instances.
Xi = x1,xi
Yj = y1,,yj
i [0..|X|] j [0..|Y|]
The set S of sub-Instances needs to:
include our given I
closed under friend operation
sub-Instance S subsub-Instance S
Xi-1 = x1,xi-1 Yj = y1,,yj
Xi = x1,xi
S Xi = x1,xi Yj-1 = y1,,yj-1
Yj = y1,,yj
Xi-1 = x1,xi-1 Yj-1 = y1,,yj-1
S

261

Set of Sub-Instances
Guess the complete set S of sub-Instances.
Xi = x1,xi
Yj = y1,,yj
i [0..|X|] j [0..|Y|]
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!262

The Table
Construct a table
for storing the cost of opt sol and birds advice.
for each sub-instance.
Map
Xi = x1,xi
Sub-Instances

Yj = y1,,yj
i [0..|X|] j [0..|Y|]

Indexes
Cell of table

LCS of x1,xi and y1,,yj ?


j

263

The Table

264

Table
Y

Original instance
I = <X,Y>

265

Table
Yj
Xi

j=

sub-Instancei,j =
Xi = x1,xi
Yj = y1,,yj
Optimal Solution
= Longest Common

i=

Subsequence

Cost = length of LCS.

266

Table
Yj
Xi

sub-Instancei,j =
Xi = x1,xi
Yj = y1,,yj
Optimal Solution
= Longest Common
Subsequence

Birds Advice
delete xi

267

Table
Yj
Xi

sub-Instancei,j =
Xi = x1,xi
Yj = y1,,yj
Optimal Solution
= Longest Common
Subsequence

Birds Advice
delete xi
take both xi and yj
268

Table
Yj
Xi

sub-Instancei,j =
Xi = x1,xi
Yj = y1,,yj
Optimal Solution
= Longest Common
Subsequence

Birds Advice
delete xi
delete yj
take both xi and yj
269

Fill in Box
Yj

sub-Instancei,j =
Xi = x1,xi

Xi

Yj = y1,,yj
Fill in box
Try all birds ans.
delete xi

Friends sub-Instance
Our cost
= friends cost
270

Fill in Box
Yj

sub-Instancei,j =
Xi = x1,xi

Xi

Yj = y1,,yj
Fill in box
Try all birds ans.
delete yj

Friends sub-Instance
Our cost
= friends cost
271

Fill in Box
Yj

sub-Instancei,j =
Xi = x1,xi

Xi

Yj = y1,,yj
Fill in box
Try all birds ans.
take both xi and yj

Friends sub-Instance
Our cost
= friends cost
+1
272

Fill in Box
Yj

sub-Instancei,j =
Xi = x1,xi

Xi

Yj = y1,,yj
Fill in box
Try all birds ans.
Take best of best

273

Fill in Box
Yj

sub-Instancei,j =
Xi = x1,xi

Xi

Yj = y1,,yj
Fill in box
Try all birds ans.
delete xi

Friends sub-Instance
Our cost
= friends cost
274

Fill in Box
Yj

sub-Instancei,j =
Xi = x1,xi

Xi

Yj = y1,,yj
Fill in box
Try all birds ans.
delete yj

Friends sub-Instance
Our cost
= friends cost
275

Fill in Box
Yj

sub-Instancei,j =
Xi = x1,xi

Xi

Yj = y1,,yj
Fill in box
Try all birds ans.
take both xi and yj

Sorry bird is
wrong.
Our cost
= -
276

Fill in Box
Yj

sub-Instancei,j =
Xi = x1,xi

Xi

Yj = y1,,yj
Fill in box
Try all birds ans.
Take best of best

277

Fill in Box

278

Fill in Box

279

Order to Fill in Table


Order to fill table:

so that nobody waits


This guy waits for

280

Order to Fill in Table

(later)

281

Base Cases
Base Cases:
general algorithm
does not work
This guys
friends are

282

Base Cases
Base Cases:
general algorithm
does not work

283

Base Cases

284

With Advice

285

With Advice

Done286

Knapsack Problem

Get as much value


as you can
into the knapsack

287

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
288

Greedy Algorithm
Greedy Criteria: Most valuable pi

v=4,p=4
v=7,p=5 v=4,p=4
v=4,p=4
V=8

Greedy give 5

v=4,p=4

V=8

Optimal gives 8

289

Greedy Algorithm
Greedy Criteria: Most dense in value
pi
vi

v=7,p=5
V=8
V=7

Greedy give 4

v=4,p=4

v=4,p=4

v=7,p=5
V=7

Optimal gives 5

290

Greedy Algorithm

of

Greedy Criteria: Most dense in value


pi
vi
If fractional solutions are allowed.
Works
Often an Integer
solution is MUCH
v=4,p=4
harder to find.

v=4,p=4

v=7,p=5
V=7

Greedy give 4 + 4 = 7 = Optimal gives 5

V=7

291

Bird & Friend Algorithm


My instance:
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.

V=12

v=7,p=5

v=4,p=4

v=4,p=4

A solution:
<<v5,p5>,<v9,p9>,...........,<v82,p82>>.
I ask the bird:
What is the last object to take?
# of answers K = n

292

Bird & Friend Algorithm


My instance:
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.

V=12

v=7,p=5

v=4,p=4

v=4,p=4

A solution:
<<v5,p5>,<v9,p9>,...........,<v82,p82>>.
I ask the bird:
Do we keep the last object?
# of answers K = 2 Yes & No

293

Bird & Friend Algorithm


My instance:
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.

V=12

v=7,p=5

v=4,p=4

v=4,p=4

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?

294

Bird & Friend Algorithm


His instance:
<V-vn:<v1,p1>,<v2,p2>,.........<vn-1,pn-1>,<vn,pn>>.

V=12-4 v=7,p=5

v=4,p=4

v=4,p=4

His solution:
<<v5,p5>,<v9,p9>,...........,<v82,p82>>.
My solution:
<<v5,p5>,<v9,p9>,...........,<v82,p82>,<vn,pn>>
My cost: same + pn

295

Bird & Friend Algorithm


My instance:
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.

V=12

v=7,p=5

v=4,p=4

v=4,p=4

If we trust the bird and friend,


this is valid and optimal.
My solution:
<<v5,p5>,<v9,p9>,...........,<v82,p82>,<vn,pn>>
My cost: same +pn

296

Bird & Friend Algorithm


My instance:
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.

V=12

v=7,p=5

v=4,p=4

v=4,p=4

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?

297

Bird & Friend Algorithm


His instance:
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.

V=12

v=7,p=5

v=4,p=4

v=4,p=4

His solution:
<<v5,p5>,<v9,p9>,...........,<v82,p82>>.
My solution: same
My cost: same

If we trust the bird and friend,


this is valid and optimal. 298

Same as Brute Force Algorithm


I try each of 2 bird ans.
My friends tries 2
His friends tries 2
Time?

Same as the brute force algorithm


that tries each solution.

299

Memoization
Assign one friend to each sub-instance.
Which is the best path from vi to t? i

300

Set of Sub-Instances
Determine the complete set of sub-Instances.
Given an instance I,
<V:<v1,p1>,<v2,p2>,<v3,p3>,<v4,p4>,<v5,p5>,<v6,p6>>.
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?
<V:<v1,p1>,<v2,p2>,<v3,p3>>.
Yes, if the bird keeps saying No.
301

Set of Sub-Instances
Determine the complete set of sub-Instances.
Given an instance I,
<V:<v1,p1>,<v2,p2>,<v3,p3>,<v4,p4>,<v5,p5>,<v6,p6>>.
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?
<V:<v1,p1>,<v2,p2>,<v3,p3>,<v4,p4>,<v5,p5>,<v6,p6>>.
No, the set of objects is always a prefix
of the original set.
302

Set of Sub-Instances
Determine the complete set of sub-Instances.
Given an instance I,
<V:<v1,p1>,<v2,p2>,<v3,p3>,<v4,p4>,<v5,p5>,<v6,p6>>.
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
Is this a sub-Instance? determine if it is a sub-instance.
<V:<v1,p1>,<v2,p2>,<v3,p3>>.
303

Set of Sub-Instances

My instance:
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.
Guess the complete set S of sub-Instances.
V [0..V]
<V:<v1,p1>,<v2,p2>,......,<vi,pi>>.
i [0..n]
The set S of sub-Instances needs to:
include our given I
Yes: V=V & i = n
closed under friend operation
sub-Instance S subsub-Instance S
<V:<v1,p1>,<v2,p2>,......,<vi,pi>> S
No <V :<v1,p1>,<v2,p2>,...,<vi-1,pi-1>>
S
Yes <V-vi:<v1,p1>,<v2,p2>,...,<vi-1,pi-1>>
304

The Table
Construct a table
for storing the cost of opt sol and birds advice.
for each sub-instance.
Map
<V:<v1,p1>,<v2,p2>,......,<vi,pi>>.
Sub-Instances
Indexes
Cell of table

V [0..V]

i [0..n]

Which of first i objects


to put in a knapsack of size v?
v

305

The Table
The complete set S of sub-Instances.
<V:<v1,p1>,<v2,p2>,......,<vi,pi>>.
OptSol Cost
0
&
1
Birds Advice
2
for this
sub-Instance
No i-1
Yes
i
Our cost? n

0 1 2

V-vi

V [0..V]
i [0..n]
V
V

same + pi
same
Take best
of best.
306

The Table
The complete set S of sub-Instances.
<V:<v1,p1>,<v2,p2>,......,<vi,pi>>.
OptSol Cost
0
&
1
Birds Advice
2
for this
sub-Instance
i-1
Order to fill
i
so nobody
waits?
n

0 1 2

V-vi

V [0..V]
i [0..n]
V
V

307

The Code

308

309

310

Running Time

My instance:
<V:<v1,p1>,<v2,p2>,..........................,<vn,pn>>.
The complete set S of sub-Instances is V [0..V]
<V:<v1,p1>,<v2,p2>,......,<vi,pi>>.
i [0..n]
No
Yes
Running time
= ( # of sub-instances # bird answers )
= (
Vn

2
)
= ( 2#bits in V n )
Polynomial?
Exponential in size in instance!
311

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.

312

The Knapsack Problem


Dynamic Programming
Running time = ( V n )
= ( 2#bits in V n )
NP-Complete
If there is a poly-time algorithm
for the Knapsack Problem
For EVERY optimization problem
there is a poly-time algorithm.
313

The Knapsack Problem


Dynamic Programming
Running time = ( V n )
= ( 2#bits in V n )
NP-Complete
Likely there is not a poly-time algorithm
for the Knapsack Problem.
Likely there is not a poly-time algorithm
for EVERY optimization problem.
314

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
315

The Job/Event Scheduling Problem

Schedule as
many events
in your room
as possible

316

The Job/Event Scheduling Problem


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.

317

Greedy Algorithm

Greedy Criteria: Earliest Finishing Time


Motivation: Schedule the event which will
free up your room for someone
else as soon as possible.

318

Weighted Event Scheduling


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

319

Greedy Algorithm
1

100

Greedy Criteria: Earliest Finishing Time


Motivation: Schedule the event which will
free up your room for someone
else as soon as possible.

320

Bird & Friend Algorithm


An instance:
<<s1,f1,w1>,<s2,f2,w2>, ,<sn,fn,wn>>.
A solution:
<<s5,f5,w5>,<s9,f9,w9>, ,<s82,f82,w82>>.
I ask the bird:
What is the last event to take?
# of answers K = n

321

Bird & Friend Algorithm


An instance:
<<s1,f1,w1>,<s2,f2,w2>, ,<sn,fn,wn>>.
A solution:
<<s5,f5,w5>,<s9,f9,w9>, ,<s82,f82,w82>>.
I ask the bird:
Do we keep the last event?
# of answers K = 2 Yes & No

322

Bird & Friend Algorithm


An instance:
<<s1,f1,w1>,<s2,f2,w2>, ,<sn,fn,wn>>.
I ask the bird:
Do we keep the last event?
She answers: No
I ask my friend:
<<s1,f1,w1>,<s2,f2,w2>, ,<sn,fn,wn>>.
His solution:
<<s5,f5,w5>,<s9,f9,w9>, ,<s82,f82,w82>>.
My solution: same
My cost: same

323

Bird & Friend Algorithm


An instance:
<<s1,f1,w1>,<s2,f2,w2>, ,<sn,fn,wn>>.
I ask the bird:
Do we keep the last event?
She answers: Yes

Carefull

I ask my friend:
<<s1,f1,w1>,<s2,f2,w2>, ,<sn,fn,wn>>.
His solution:
<<s5,f5,w5>,<s9,f9,w9>, ,<s82,f82,w82>>.
My solution: same + <sn,fn,wn>. My cost: same +w
324n

Bird & Friend Algorithm


last event

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 birds answer.
325

Bird & Friend Algorithm

Bird answers:

No we trust the
Yes keep the last event.
bird!
Then I should politely tell the bird
she is wrong

326

Bird & Friend Algorithm

Bird answers:

No we trust the
Yes keep the last event.
bird!
You only tell her she is wrong
if you really know.
Eg k words dont fit on the last line
The bear does not fit into the knapsack

327

Bird & Friend Algorithm

Bird answers:

No we trust the
Yes keep the last event.
bird!
Your friend could have just as easily
given you this subsolution that does not
conflict with the birds answer.
328

Bird & Friend Algorithm

Bird answers:

No we trust the
Yes keep the last event.
bird!
Or maybe he needs to make a sacrifice
when finding his answer in order that the
overall solution is the best.
329

Bird & Friend Algorithm

Bird answers:

No we trust the
Yes keep the last event.
bird!
Or goal now is to find the best solution to
our instance that is consistent with the
birds answer.
Then we will take the best of best.

330

Bird & Friend Algorithm

Bird answers:

No we trust the
Yes keep the last event.
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

331

Bird & Friend Algorithm


My instance:
last event

Bird answers:
Yes keep the last event.
Cant keep any events that overlap with it.

332

Bird & Friend Algorithm


My instance:
last event

Bird answers:
Yes keep the last event.
I ask my friend:
333

Bird & Friend Algorithm


His instance:
His solution

Bird answers:
Yes keep the last event.
I ask my friend:
334

Bird & Friend Algorithm


My instance:
My solution:

Bird answers:
Yes keep the last event.
I ask my friend:
My solution: same + <sn,fn,wn>.

Valid?

Yes
335

Bird & Friend Algorithm


My instance:
My solution:

Bird answers:
Yes keep the last event.
I ask my friend:
My solution: same + <sn,fn,wn>. My cost: same +wn
336

Same as Brute Force Algorithm


I try each of 2 bird ans.
My friends tries 2
His friends tries 2
Time?

Same as the brute force algorithm


that tries each solution.

337

Memorization
Assign one friend to each sub-instance.
Which is the best path from vi to t? i

338

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,

339

Every subset of {1,,9}


is a possible sub-Instance.
I.e. could be an exponential
number of them.
Hence, running time
is exponential.
Greedy algorithm sorted
jobs by finishing time.
Let us do that too.
340

Each sub-Instance is prefix.


I.e. only n of them.
Hence, running time
is polynomial!

341

Set of Sub-Instances

My instance:
<<s1,f1,w1>,<s2,f2,w2>,................ ,<sn,fn,wn>>.
Guess the complete set S of sub-Instances.
<<s1,f1,w1>,<s2,f2,w2>, ,<si,fi,wi>> i [0..n]
The set S of sub-Instances needs to:
include our given I
Yes: i = n
closed under friend operation
sub-Instance S subsub-Instance S ?
each sub-Instance needs to be
asked of some friend, friend Only n sub-Instances
Good enough. 342

Set of Sub-Instances

Show closed under friend operation


sub-Instance S subsub-Instance S
sub-Instance =
last event

<<s1,f1,w1>,<s2,f2,w2>,................................,<si,fi,wi>>
Events sorted by finishing time.

343

Set of Sub-Instances

Show closed under friend operation


sub-Instance S subsub-Instance S
sub-Instance =
last event

<<s1,f1,w1>,<s2,f2,w2>,................................,<si,fi,wi>>
Bird answers:

Yes keep the last event.

Delete overlapping events for friend.


344

Set of Sub-Instances

Show closed under friend operation


sub-Instance S subsub-Instance S
subsub-Instance =

<<s1,f1,w1>,<s2,f2,w2>,.....,<sj,fj,wj>>
Bird answers:

Yes keep the last event.

Delete overlapping events for friend.


345

Set of Sub-Instances

Show closed under friend operation


sub-Instance S subsub-Instance S
subsub-Instance =
typical kept job

typical deleted job

<<s1,f1,w1>,<s2,f2,w2>,.....,<sj,fj,wj>>
Event j is kept
fj si
set of kept jobs is a prefix of events.
subsub-Instance S
346

Set of Sub-Instances

My instance:
<<s1,f1,w1>,<s2,f2,w2>,................ ,<sn,fn,wn>>.
The complete set S of sub-Instances is
<<s1,f1,w1>,<s2,f2,w2>, ,<si,fi,wi>> i [0..n]
Table:
0, 1,
Base case

2,

3, 4, .

n
Original

347

Set of Sub-Instances

My instance:
<<s1,f1,w1>,<s2,f2,w2>,................ ,<sn,fn,wn>>.
The complete set S of sub-Instances is
<<s1,f1,w1>,<s2,f2,w2>, ,<si,fi,wi>> i [0..n]
Running time
= # of sub-instances # bird answers
=
n

2
But to find your friends yes sub-instance
you must know how many events overlap
with your last event. This takes time:
O(logn) using binary search
Done348
for a total of O(nlogn) time.

Parsing
Input: s=6*8+((2+42)*(5+12)+987*7*123+15*54)
Output:

349

Parsing

Recursive Alg:
GetExp calls GetTerm
GetTerm calls GetFact
GetFact calls GetExp

350

Parsing
T AB
CA
TT

A AA
BT
a

B TA
BC
b
e

C CB
AC
c
d

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

351

Parsing
T AB
CA
TT

A AA
BT
a

Input: T a1a2a3 ..... an


start non-terminal = T
string to parse
= a1a2a3 ..... an
= baeaadbda
Output: A parsing

B TA
BC
b
e

C CB
AC
c
d

T
A

C
C

A
A

C B
A C

T
C A

T
A B
b a e a a d b b d a352

Parsing
T AB
CA
TT

A AA
BT
a

Input: T a1a2a3 ..... an

B TA
BC
b
e

C CB
AC
c
d

T
C

Recursive Algorithm:
C
GetT does not know
B T
A
whether to call
A
B
C
A
A
C
GetA, GetC, or GetT.
A C
B T
A B
b a e a a d b b d a353

Parsing
T AB
CA
TT

A AA
BT
a

Input: T a1a2a3 ..... an


Ask Little Bird:
For first rule
Ask Friend
Parse left
Ask Another Friend
Parse right.

B TA
BC
b
e

C CB
AC
c
d

T
A

C
C

A
A

C B
A C

T
C A

T
A B
b a e a a d b b d a354

Parsing
T AB
CA
TT

A AA
BT
a

Input: T a1a2a3 ..... an


Ask Little Bird:
For first rule
Instance to give Friend

B TA
BC
b
e

C CB
AC
c
d

T
C

?
b a e a a d b b d a355

Parsing
T AB
CA
TT

A AA
BT
a

Input: T a1a2a3 ..... an

B TA
BC
b
e

C CB
AC
c
d

Ask Little Bird:


A
C
For first rule
C
A
Want from Friend:
Left sub-parse tree.
A
A C B
Instance to give him:
A C
B T
C baeaadb
A B
b a e a a d b b d a356

Parsing
T AB
CA
TT

A AA
BT
a

Input: T a1a2a3 ..... an

B TA
BC
b
e

C CB
AC
c
d

Ask Little Bird:


A
C
For first rule
C
B T
A
How can we know split?
Ask the Bird!
C A
A
A C B
A C
B T
A B
b a e a a d b b d a357

Parsing
T AB
CA
TT

A AA
BT
a

Input: T a1a2a3 ..... an

B TA
BC
b
e

C CB
AC
c
d

Ask Little Bird:


A
C
For first rule # of ans K = mT = # of rules for T.
For the split. # of ans K = n = # chars in string.
Total # of ans K = mT n.

b a e a a d b b d a358

Parsing
T AB
CA
TT

A AA
BT
a

Input: T a1a2a3 ..... an

B TA
BC
b
e

C CB
AC
c
d

A
Ask left friend:
C
Instance: C baeaadb
C
A
Solution: Left parsing
A
A C B
A C
B T
A B
b a e a a d b b d a359

Parsing
T AB
CA
TT

A AA
BT
a

Input: T a1a2a3 ..... an


Ask right friend:
Instance: A bda
Solution: Right parsing

B TA
BC
b
e

C CB
AC
c
d

T
C

A
B

T
C A

b a e a a d b b d a360

Parsing
T AB
CA
TT

A AA
BT
a

Input: T a1a2a3 ..... an

B TA
BC
b
e

C CB
AC
c
d

A
Combine:
C
Instance:
C
B T
A
Birds Answer
Left Friends Answer A
C A
A C B
Right Friends Answer
A C
B T
A B
b a e a a d b b d a361

362

Same as Brute Force Algorithm


I try each of 2 bird ans.
My friends tries 2
His friends tries 2
Time?

Same as the brute force algorithm


that tries each solution.

363

Memoization
Assign one friend to each sub-instance.
Which is the best path from vi to t? i

364

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,

365

Set of Sub-Instances
Determine the complete set of sub-Instances.
My instance I:
T a1a2a3 ..... an

gives:

My left sub-Instance.

gives:
His right sub-Instance.
b a e a a d b b d a366

Set of Sub-Instances
Determine the complete set of sub-Instances.
My instance I:
T a1a2a3 ..... an
sub-Instances:
T aiai+1 ..... aj
T= C
non-terminals T
i,j [1,n]
# of sub-Instances = # of non-terminals n2
a1...ai-1 aiai+1...aj aj+1...an
a d b

367

The Table
Construct a table
for storing the cost of opt sol and birds advice.
for each sub-instance.
Map
Sub-Instances
T aiai+1 ..... aj

T i,j [1,n]

Indexes
T

Cell of table

j
368

369

Running Time

Running time
= ( # of sub-instances
# bird answers )
= ( # of non-terminals n2 # of rules n )
sub-Instances:
T aiai+1 ..... aj non-terminals T
gives: First rule and split
& i,j [1,n]
Done
370

Find a Satisfying Assignment


An instance (input) consists of a circuit:
c = (x3 or x5 or x6) and (x2 or x5 or x7) and (x3 or x4)
true

true

true

true
true

false true
true

A solution is an assignment of the variables.


x1 = 0, x2 = 1, x3 = 0, x4 = 0, x5 = 1, x6 = 0, x7 = 1
The cost of a solution is
1 if the assignment satisfies the circuit.
0 if not.
The goal is to find satisfying assignment.

371

Find a Satisfying Assignment


Instance:
c = (x3 or x5 or x6) and (x2 or x5 or x7) and (x3 or x4)
Ask the little bird
Value of x1 in an optimal solution
or even better
Value of x3 in an optimal solution
We will have to try both x3 = 0 and x3 = 1.
For now, suppose she answered x3 = 0.
372

Find a Satisfying Assignment


Instance:
c = (x3 or x5 or x6) and (x2 or x5 or x7) and (x3 or x4)
true

false
true

Commit to x3 = 0 and simplify


Sub-Instance:
c=

(x2 or x5 or x7) and x4

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

373

Speeding Up the Time


x3
x2
x1
0

x1

x1 x2

x2

In the end, some friend looks


at each of the 2n assignments,

374

Memoization
Assign one friend to each sub-instance.
Which is the best path from vi to t? i

375

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,

376

Set of Sub-Instances
Determine the complete set of sub-Instances.
Given an instance I,
c = (x1 or y1) and (x2 or y2) and (x3 or y3) and (x4 or y4)
Is this a sub-Instance?
c = (x1)

and (x3)

Commit to
y1=0,
y2=1,
y3=0,
y4=1,
and simplify
Yes
True for any subset of the xi.
could be an exponential # of different sub-Instances.
running time is exponential.
377

Speeding Up the Time


x3
x2
x1
0

x1

x1 x2

x2

In the end, some friend looks


at each of the 2n assignments,

378

Speeding Up the Time


x3
x2
x1
0

x1

x1 x2

x2

But sometimes we can prune off branches.

379

Find a Satisfying Assignment


Instance:
c = (x2 or x5 or x7) and x3
x3 is forced to x3 = 0
x3
x2
x1
0

x1

x1 x2

x2

380

Find a Satisfying Assignment


Instance:
c = (x2 or x5 or x7) and x3 and x3
This is trivially unsatisfiable
because x3 cant be both 0 and 1.
x3
x2
x1
0

x1

x1 x2

x2

381

382

383

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

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 algorithm with birds advice to
find the solution to your instance.
385

Optimization Problems

Dont 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

386

Dynamic Programming
Donts
Yes, the code has a basic structure that you should learn.
But dont copy other code verbatim
Dont say if(ai = cj)
(i.e. Longest Common Subsequence)
when our problem does not have cj

387

Dynamic Programming
Donts
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 dont have a two dimensional table.
Table[1..n,1..n].
Dont loop over i and loop over j if j never gets
mentioned again.
388

Dynamic Programming
Donts
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?
Dont simply say cost(i-1,j-1)
389

Dynamic Programming
Donts
Think about what the base cases should be.
Dont make an instance a base cases
if they can be solved using the general method.
% is used to start a comment.
Dont put it in front of code.

390

The Question For the Little Bird


Eg. The Best Binary Search Tree problem,
Which key is at the root of the tree?
38

25

51

17

31

21

28

42

35

40

63

49

55

71

If a solution is a binary tree of objects,


What object is at the root of the tree?
391

Matrix Multiplication

392

393

394

395

396

All Pairs Shortest Paths

397

398

399

400

401

402

end

403

Dynamic Programming
Construct a table
for storing an optimal solution & cost
for each sub-instance.
Map
i Best path from vi to t?
Sub-Instances
i [n], i.e. for each node vi

Index
Cell of table

Which is the best path from vi to t?


t,

v,

v , vi , v , .,

404

Вам также может понравиться