1 / 394

# Recursive Back Tracking & Dynamic Programming - PowerPoint PPT Presentation

Thinking about Algorithms Abstractly. Recursive Back Tracking & Dynamic Programming. Jeff Edmonds York University. Lecture 7. COSC 3101. Problems. Techniques. Optimization Problems A Sequence of Decisions The Little Bird & Friend Optimal Substructure Memoization Set of Sub-Instances

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Recursive Back Tracking & Dynamic Programming' - felcia

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Recursive Back Tracking&Dynamic Programming

Jeff Edmonds York University

Lecture7

COSC 3101

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

Best Path

Printing Neatly

Longest Common Subsequence

Knapsack Problem

The Event Scheduling Problem

Parsing

Satisfiability

• A hard topic.

• I try to provide a unified way to think of itand a fixed set of steps to follow.

• Even if you don’t get the details of the algorithm correct, at least get the right structure.

• I provide analogies (little bird) to make it hopefully more fun & easier to follow.

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

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.

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!

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

An instance (input) consists of <G,s,t>.

G is a weighted directed layered graphs source nodet sink node

5

3

4

2

3

2

5

2

2

1

3

4

Search Graph For Best Path

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

Search Graph For Best Path

An instance (input) consists of <G,s,t>.

Try all paths, return the best.

But there may be an exponential number of paths!

An Algorithm As A Sequence of Decisions

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

An Algorithm As A Sequence of Decisions

“Which edge should we take first?”

How do I decide?

The greedy algorithm?

Does not work!

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

An Algorithm As A Sequence of Decisions

“Which edge should we take first?”

How do I decide?

• In reality we will try all possible first edges.

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.

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

But we don’t want toworry about how our friend

solves his problem.

Little Bird & Friend Alg

“Which edge should we take first?”

My friend asks the next question.

“Which edge do we take second?”

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?

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?”

To get my solution

I tack on the bird’s edge making the path <s,v1,v6,t>

with weight

10+3=13.

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

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 tfrom amongst those starting with <s,v1>.

In reality we will try all possible first edges,

giving …..

…the best path from amongst those starting with <s,v1>.

… and the best path from amongst those starting with <s,v2>.

… and the best path from amongst those starting with <s,v3>.

… and the best path from amongst those starting with <s,v4>.

I give the best of the best as the best path.

Faulty Bird

At least one of these four paths must be an over all best path.

Consider our instance I.

Consider the set of solutions

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

Consider our instance I.

Consider the set of solutions

k

k

Solutions consistent with the kth bird' s answer.

optS<I,k>

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.

k

Solutions consistent with the kth bird' s answer.

optS<I,k>

Consider our instance I.

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

kmax

k

Let kmax be the bird' s answergiving the best optS<I,k>.

optS[I] = optS<I,k >= Bestk optS<I,k >

max

Constructing optS<I,k> :

the optimum solution

for instance I

consistent with the kth bird' s answer.

Given my instance I.

I ask my friend for his solution.

I combine them.

Recursive backtracking

code always has thissame basic structure.

• the instances

• it’s solution

• the cost of a sol.

Be clear which is the current one being tried.

Give the bird & friend algorithmas a comment.

(Unless it is in an earlier question.)

Be clear what

sub-instance you give him.

Store the solution& cost he gives you.

How do you formyour solution from the friend’s and fromthe bird’s?

How do you formyour cost from the friend’s and fromthe bird’s?

optSolkis a best solutionfor our instance from amongstthose consistent with the bird'skth answer.

Take the bestof the best

Return the solutionand cost for the original instance.

Base Cases:Instances that are too small to have smaller instances to give to friends.

What are these?

What are their solutionsand costs?

i.e. for a path from s to t to be optimal,the sub-path from vi to t must optimal.

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.

i.e. for a path from s to t to be optimal,the sub-path from vi to t must optimal.

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

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

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

### Don’t all problems have this optimal substructure property?

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

B

1

2

3

1

4

C

D

A

Longest simple path

Optimal Substructure

NP-Complete

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.

But there may be an exponential number of paths!

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.

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.

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.

Redoing Work

“Which is the best path from v7 to t?”

How many friends solve this sub-instance?

Redoing Work

“Which is the best path from s to t?”

Redoing Work

“Which is the best path from v1to t?”

Redoing Work

“Which is the best path from v4to t?”

Redoing Work

“Which is the best path from v7to t?”

There’s one.

Redoing Work

“Which is the best path from s to t?”

Redoing Work

“Which is the best path from v3to t?”

Redoing Work

“Which is the best path from v5to t?”

Redoing Work

“Which is the best path from v7to t?”

There’s another.

Redoing Work

“Which is the best path from v7to t?”

How many friends solve this sub-instance?

Once for each path to v7

Waste time redoing work

Save time by only doing once.

Search

Drop

But we need shortest path

We allocate one friend to the job.

Having many friends solving this same sub-instanceis a waste of time.

It is my job to learn and remember

the optSol to my sub-Instance

i.e. the best path from v7to t

When I need to find

the best path from v4 to t

the best path from v7 to t

I will find my best pathand tell you.

When I need to find

the best path from v2 to t

the best path from v7 to t

I remember my best pathand will tell you.

When I need to find

the best path from v5 to t

the best path from v7 to t

I remember my best pathand will tell you.

Avoid waiting.

When I need to find

the best path from v2 to t

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 don’t you go first?

Before anyone asks me,I will find my best path

and remember it.

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

v21

Guess the complete set S of sub-Instances.

“Best path from v7to t?”

Yes

“Best path from v21to t?”

No

v21is not a part of ouroriginal instance.

Guess the complete set S of sub-Instances.

“Best path from v7to t?”

Yes

“Best path from v21to t?”

No

“Best path from v3to v7?”

No

All paths considered end in t.

Guess the complete set S of sub-Instances.

“Best path from v7to t?”

Yes

“Best path from v21to t?”

No

“Best path from v3to v7?”

No

All paths considered end in t.

i

“Best path from vi to t?”

Set of Sub-Instances

Guess the complete set S of sub-Instances.

“Best path from v7to t?”

Yes

“Best path from v21to t?”

No

“Best path from v3to v7?”

No

Yes

i

“Best path from vi to t?”

Set of Sub-Instances

Guess the complete set S of sub-Instances is

Assign one friend to each sub-Instance.

i

“Best path from vi to t?”

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

i

“Best path from vi to t?”

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

 x,yI x+y  I

 sub-Instance S

subsub-Instance S

i

“Best path from vi to t?”

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

i

“Best path from vi to t?”

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!

i

“Best path from vi to t?”

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

i

“Best path from vi to t?”

Instance to be solved.

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

easy

Dynamic Programming

"Which is the best path from t to t?"

"Which is the best path from v8 to t?"

easy

"Which is the best path from v7 to t?"

easy

"Which is the best path from v6to t?"

easy

"Which is the best path from v5to t?"

Harder

"Which is the best path from v5to t?"

Little bird suggests first edge <v5,v7>

Friend gives best

path <v7,t>.

"Which is the best path from v5 to t?"

Little bird suggests first edge <v5,v8>

Friend gives best

path <v8,t>.

"Which is the best path from v5 to t?"

Take best of best

"Which is the best path from v4to t?"

"Which is the best path from v4to t?"

Little bird suggests first edge <v4,v6>

Friend gives best

path <v7,t>.

"Which is the best path from v4to t?"

Little bird suggests first edge <v4,t>

Friend gives best

path <t,t>.

"Which is the best path from v4to t?"

Little bird suggests first edge <v4,v7>

Friend gives best

path <v7,t>.

"Which is the best path from v4to t?"

Take best of best

"Which is the best path from v3to t?"

"Which is the best path from v3to t?"

Little bird suggests first edge <v3,v5>

Friend gives best

path <v5,t>.

"Which is the best path from v3to t?"

Little bird suggests first edge <v3,v8>

Friend gives best

path <v8,t>.

"Which is the best path from v3to t?"

Take best of best

"Which is the best path from v2to t?"

"Which is the best path from v2to t?"

Little bird suggests first edge <v2,v4>

Friend gives best

path <v4,t>.

"Which is the best path from v2to t?"

Little bird suggests first edge <v2,v7>

Friend gives best

path <v7,t>.

"Which is the best path from v2to t?"

Take best of best

"Which is the best path from v1to t?"

"Which is the best path from v1to t?"

Little bird suggests first edge <v1,v3>

Friend gives best

path <v3,t>.

"Which is the best path from v1to t?"

Little bird suggests first edge <v1,v4>

Friend gives best

path <v4,t>.

"Which is the best path from v1to t?"

Little bird suggests first edge <v1,v5>

Friend gives best

path <v5,t>.

"Which is the best path from v1to t?"

Take best of best

"Which is the best path from s to t?"

Original Problem

"Which is the best path from s to t?"

Little bird suggests first edge <s,v1>

Friend gives best

path <v1,t>.

"Which is the best path from s to t?"

Little bird suggests first edge <s,v2>

Friend gives best

path <v2,t>.

"Which is the best path from s to t?"

Little bird suggests first edge <s,v3>

Friend gives best

path <v3,t>.

"Which is the best path from s to t?"

Little bird suggests first edge <s,v4>

Friend gives best

path <v4,t>.

"Which is the best path from s to t?"

Take best of best

DONE

i

“Best path from vi to t?”

Dynamic Programming

Construct a table

• for storing an optimal solution & cost

• for each sub-instance.

Map

Sub-Instances

i ϵ [n], i.e. for each node vi

Indexes

Cell of table

“Which is the best path from vi to t?”

t, v8, v7, v6, v5, …., s

i

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

k

k

i

k

i

Friend k gives friend i a best path from vk to t.

Recursive BackTracking

<optSubSol,optSubCost> = LeveledGraph(<G,vk,t>)

i

return(optSolmin,optCostmin),

Dynamic Programming

optSol[k] = optSolmin

optSubSol = optSol[k]

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

Dynamic Programming

code always has thissame basic structure.

• the instances

• it’s solution

• the cost of a solution.

Dynamic Programsdo not recurse making the instance smaller and smaller.

Instead, it up frontdetermines the set Sof all sub-instancesthat ever need to be solved.

Be clear what sub-instances are.

Be clear what sub-instances are.

How are they indexed?

Tables indexed by these sub-instancesstore an optimal solution and it’s cost.

The set Sof sub-instancesare solved fromsmallest to largestso that no body waits.

Base Cases:Instances that are too small to have smaller instances to give to friends.

They get solved firstand their solutions stored.

Then we iterate through the remaining sub-instances.

From smallest to largest.

Each gets solvedand their solutions stored.

Consider yourselfto be a friendworking on oneof these.

Be clear which sub-instance is yours.

Solve this as you did before.

Be clear which is the current one being tried.

Give the bird & friend algorithmas a comment.

(Unless it is in an earlier question.)

k

k

k

Get help from friend

Be clear what sub-instance you give your friend.

k

k

k

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.

How do you formyour solution from the friend’s and fromthe bird’s?

How do you formyour cost from the friend’s and fromthe bird’s?

optSol<i,k>is a best solution for our instance subI[i]from amongstthose consistent with the bird'skth answer.

Take the bestof the best

Store the solution to our instance subI[i]in the table.

Base Cases:Instances that are too small to have smaller instances to give to friends.

Is this code correct?

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

But there is a tableof subinstancesthat must be solved.

Some of these will bebase casesand their solutionsmust be stored in the table.

But there is a tableof subinstancesthat must be solved.

t=

n

n

n

n

Some of these will bebase casesand their solutionsmust be stored in the table.

But there is a tableof subinstancesthat must be solved.

Then we solvethe rest.

0

0

n

0

Return the solutionand cost for the original instance.

Path from s to t.

Path from t to t.

Path from s to s.

Path from s to t.

“Which is the best path from s to vi?”

i

Determine the complete set of sub-instances

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

?

Time =

# of Sub-Instances

× q(1)

= n

× d

Friend k gives best path from s to vk

to friend i, who adds the edge <vk,vi>.

Time =

q(1)

k

?

Time =

q(n)

i

optSolk = <optSol[k],vi>

Size of path =

q(n).

# of Sub-Instances

× q(1)

Store path costs, not paths

= q(n)

Time =

# of Sub-Instances

× size of solution

= q(n × d × n)

Space =

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

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

Little bird suggests last edge <v4,v7> with weight 2.

8

Friend gives cost 8

of best path <s,v4>.

Best cost via <v4,v7> is 8+2=10.

2

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

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.

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

Little bird suggests last edge <v5,v7> with weight 5.

6

Friend gives cost 6

of best path <s,v5>.

Best cost via <v5,v7> is 6+5=11.

9

2

We also learn the wise little bird’s advice.

We will store this in the table too.

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

Take best of best:

10, 9, 11

for extra clarity for the reader

Previous algorithm gives:

• Cost of the best path from s to vi, i.

• Bird’s advice of last edge to vi.

We run the bird-friend algorithm again, but with a reliable bird.

The bird gives that the last edge

of the best path from s to tis<v8,t>.

The bird gives that the last edge

of the best path from s to v8is<v5,v8>.

The bird gives that the last edge

of the best path from s to v5is<v3,v5>.

The bird gives that the last edge

of the best path from s to v3is<s,v3>.

Done!

This could be done iteratively.As an exercise, design it.

“Which is the last edge?”

Multiple Optimal Solutions

By giving this edge she says

“There exists an optimal solution consistent with this answer.”

Similar to greedy proof.

6

“Which is the last edge?”

We try all the bird answers.

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

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

• Assume he gives you optSubSol for subI.

• How do you produce an optSol for I from

• the bird’s k and

• the friend’s optSubSol?

• How do you determine the cost of optSol from

• the bird’s kand

• the cost of the friend’s optSubSol?

• Try all bird’s answers and take best of best.

Recursive Back Tracking Algorithm

Dynamic Programming Algorithm

• Given an instanceI,

• Imagine running the recursive alg on it.

• Determine the complete set of sub-Instancesever given to you, your friends, their friends, …

• Build a table indexed by these sub-Instances

• Fill in the table in order so that nobody waits.

• the cost of its optimal solution

• advice given by the bird

• Run the recursive alg with bird’s advice to find the solution to your instance.

Purpose of Little Bird:

• An abstraction from which it iseasier to focus on the difficult issues.

• Her answers give us a list of things to try.

• Temporarily trusting the bird,helps us focus on the remaining questionhelping us formulate sub-instance for friend.

• Coming up with which question is one of the main creative steps.

• There are only so many question that you might ask so just try them all.

• A solution: a path

t

s

“What is the first edge in the path?”

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.

A solution: a path

t

s

“What is the last edge in the path?”

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.

A solution: a path

t

s

“What is the rest of the path?”

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.

A solution: a path

t

s

The Question For the Little Bird

An instance: Graph, s, and t

“What is the last edge in the path?”

“What is the second last edge in the path?”

number of edges into node t.

• K =

t

s

K

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.

# of solutions.

• K =

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.

A solution: a path

t

s

The Question For the Little Bird

An instance: Graph, s, and t

“How many edges are in the path?”

• 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?

A solution: a sequence of objects

Z = a b c d

The Question For the Little Bird

An instance: ???

“What is the last object in the sequence?”

# of possible last objects.

“What is the rest of the solution?”

An instance: a sequence of objects

X = a s b e f c h d a

X =asbe fchda

A solution: a subset of these objects

Z = a b c d

The Question For the Little Bird

“What is the last object in the sequence?”

# of possible last objects.

Is there a smaller question that we could ask?

An instance: a sequence of objects

X = a s b e f c h d a

A solution: a subset of these objects

Z = a b c d

The Question For the Little Bird

“Is the last object of the instance included in the optimal solution?”

2, Yes or No

A solution: a binary tree of objects

38

25

51

17

31

42

63

4

21

28

35

40

49

55

71

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.

38

25

51

17

31

42

63

4

21

28

35

40

49

55

71

The Question For the Little Bird

An instance: ???

A solution: a binary tree of objects

“What object is at a leaf?”

• How does this help us solve the problem?

• What is a good follow up question for the friend to ask?

A solution: # of words to put on each line.

few blanks on the end of each line.

3 = 83 = 83 = 2163 = 27 259

2263

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

11

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.

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

I combine

bird’s and

Bird & Friend Algorithm

An instance:

“Love life man while there as we be”, 11

“How many words on the last line?”

“Which is the best way to print the remaining n-3 words?”

We try the bird answers words,

1

2

3

4

5

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.

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.

i

“Which is the best path from vi to t?”

Assign one friend to each sub-instance.

“Love life man while there as we be”, 11

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

“Love life man while there as we be”, 11

“Love life man while there”, 11

“Hi there”, 81

“man while”, 11

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.

“Love life man while there as we be”, 11

“Love life man while there”, 11

“Love life man while”, 11

“Love life man”, 11

“Love life”, 11

“Love”, 11

“”, 11

“Love life man while there as”, 11

“Love life man while there as we”, 11

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

The set S of sub-Instances needs to:

• include our given I

• closed under “friend” operation

 sub-Instance S

5

3

2

1

4

“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

“”, 11

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

“Love life man while there as we be”, 11

The set S of sub-Instances needs to:

• include our givenI

• closed under “friend” operation

 sub-Instance S

subsub-Instance  i

“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

“”, 11

• each sub-Instance needs to be

asked of some friend, friend, …

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

“Love life man while there as we be”, 11

The set Sof sub-Instances needs to:

• include our given I

• closed under “friend” operation

“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

• each sub-Instance needs to be

asked of some friend, friend, …

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

The set S of sub-Instances needs to:

• include our given I

• closed under “friend” operation

A fine set of sub-instances!

“Love life man while there as we be”, 11

“Love life man while there as we”, 11

“Love life man while”, 11

“Love life man”, 11

“Love life man while there”, 11

“Love”, 11

“”, 11

“Love life man while there as”, 11

“Love life”, 11

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

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.

Construct a table

• for storing the cost of opt sol and bird’s advice.

• for each sub-instance.

Map

The set of prefixes of words.

Sub-Instances

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

Indexes

Cell of table

“Which is the best printing of first i words?”

i

Fill out a table containing an optimal solution for each sub-instance.

“Which is the best printing of first i words?”

Base case

Original

The 5th sub-instance is

“Love life man while there”, 11

5 words

with 4, 4, 3, 5, 5 letters.

Love.life.. man.while.. there......

The 5th sub-instance is

“Love life man while there”, 11

Its solution is

with 2,2,1 words on each line.

The bird’s advice is 1 word on last.

Solution’s cost is

23 + 23 +63 = 232

Assume the table is filled in so far.

We will work to fill in the last line

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

be.........

Love.life.. man.while.. there.as...

we.be......

Love.life.. man.while.. there......

as.we.be...

there.as.we.be

Choose best of the best.

Choose best of the best.

Dynamic Programming

code always has thissame basic structure.

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

• the instances

• it’s solution

• the cost of a solution.

Dynamic Programsdo not recurse making the instance smaller and smaller.

Instead, it up frontdetermines the set Sof all sub-instancesthat ever need to be solved.

Be clear what sub-instances are.

Be clear what sub-instances are.

How are they indexed?

Tables indexed by these sub-instancesstore an optimal solution and it’s cost.

The set Sof sub-instancesare solved fromsmallest to largestso that no body waits.

Base Cases:Instances that are too small to have smaller instances to give to friends.

They get solved firstand their solutions stored.

Then we iterate through the remaining sub-instances.

From smallest to largest.

Each gets solvedand their solutions stored.

Consider yourselfto be a friendworking on oneof these.

Be clear which sub-instance is yours.

Solve this as you did before.

Be clear which is the current one being tried.

Give the bird & friend algorithmas a comment.

(Unless it is in an earlier question.)

i-k

i-k

Get help from friend

Be clear what sub-instance you give your friend.

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.

How do you formyour solution from the friend’s and fromthe bird’s?

How do you formyour cost from the friend’s and fromthe bird’s?

optSol<i,k>is a best solution for our instance subI[i]from amongstthose consistent with the bird'skth answer.

Take the bestof the best

Store the solution toour instance subI[i]in the table.

Base Cases:Instances that are too small to have smaller instances to give to friends.

Is this code correct?

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

But there is a tableof subinstancesthat must be solved.

Some of these will bebase casesand their solutionsmust be stored in the table.

But there is a tableof subinstancesthat must be solved.

Some of these will bebase casesand their solutionsmust be stored in the table.

But there is a tableof subinstancesthat must be solved.

Then we solvethe rest.

Return the solutionand cost for the original instance.

But actually,we don’t have the solution.

We must rerun it, this time with advice from the bird.

= q(n × n)

Time =

Space =

# of Sub-Instances

= q(n)

We run the bird-friend algorithm again, but with a reliable bird.

Find Optimal Path

Previous algorithm gives cost and bird’s advice.

,1

,3>

,2

<2

Love.life..

man.while..

there……

as we be…

X =asbe fchda

Y = r t wabg jck t fd

An instance: Two strings

X = a s b e f c h d a

Y = r t w a b g j c k t f d

A solution: A common subsequence.

Z = a b c d

Longest Common Subsequence problem

X =asbe tchda

Y = r t wabg jck t fd

The cost: The length of Z.

The goal is to find a longest common subsequence.

An instance:

X = a s b e t c h d a

Y = r t w a b g j c k t f d

Bird & Friend Algorithm

“Is the last character of either X or Yincluded in Z?”

• Last of X is not included

• Last of Y is not included

• Last of X is included

• Last of Y is included

• Neither are included

• Both are included

An instance:

The instance:

X = a s b e t c h d a

Y = r t w a b g j c k t f d

X = a s b e t c h d

Y = r t w a b g j c k t f d

Bird & Friend Algorithm

“Is the last character of either X or Y included in Z?”

• Last of X is not included

An instance:

The instance:

X = a s b e t c h d a

Y = r t w a b g j c k t f d

X = a s b e t c h d

Y = r t w a b g j c k t f d

I combine

bird’s and

Bird & Friend Algorithm

“Is the last character of either Xor Y included in Z?”

• Last of X is not included

Z = a b c d

X =asbe tchd

Y = r t wabg jck t fd

the sameZ.

An instance:

The instance:

X = a s b e t c h d a

Y = r t w a b g j c k t f d

X = a s b e t c h d a

Y = r t w a b g j c k t f

Bird & Friend Algorithm

“Is the last character of either X or Yincluded in Z?”

• Last of Y is not included

An instance:

The instance:

X = a s b e t c h d a

Y = r t w a b g j c k t f d

X = a s b e t c h d a

Y = r t w a b g j c k t f

I combine

bird’s and

Bird & Friend Algorithm

“Is the last character of either X or Y included in Z?”

• Last of Y is not included

the same Z.

Z = a b c

X =asbe tchd a

Y = r t wabg jck t f

Not as good as lastbut we need to try.

The instance:

X = a s b e t c h

Y = r t w a b g j c k d f

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?”

• Last of X and last of Y are both included

The instance:

X = a s b e t c h

Y = r t w a b g j c k d f

I combine

bird’s and

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?”

• Last of X and last of Y are both included

Z = a b c

X =asbe tch

Y = r t wabg jck d f

Zd = abcd.

An instance:

X = a s b e t c h d a

Y = r t w a b g j c k t f d

Bird & Friend Algorithm

Last chars not equal

“Is the last character of either X or Y included in Z?”

• Last of X and last of Y are both included

I politely tell her that she is wrong.

An instance:

The instance:

X = a s b e t c h d a

Y = r t w a b g j c k t f d

X = a s b e t c h d

Y = r t w a b g j c k t f d

Bird & Friend Algorithm

“Is the last character of either X or Y included in Z?”

• Last of X is included

An instance:

The instance:

X = a s b e t c h d a

Y = r t w a b g j c k t f d

X = a s b e t c h d

Y = r t w a b g j c k t f d

I combine

bird’s and

Bird & Friend Algorithm

“Is the last character of either X or Y included in Z?”

• Last of X is included

Z = a b c d

X =asbe tch d

Y = r t wabg jck t f d

Wrong

Za = abcda.

An instance:

The instance:

X = a s b e t c h d a

Y = r t w a b g j c k t f d

X = a s b e t c h d

Y = r t w

Bird & Friend Algorithm

“Is the last character of either X or Y included in Z?”

• Last of X is included

An instance:

The instance:

X = a s b e t c h d a

Y = r t w a b g j c k t f d

X = a s b e t c h d

Y = r t w

I combine

bird’s and

Bird & Friend Algorithm

“Is the last character of either X or Y included in Z?”

• Last of X is included

Z = t

X =a s b e t c h d

Y = r t w

Za = ta.

An instance:

X = a s b e t c h d a

Y = r t w a b g j c k t f d

Bird & Friend Algorithm

Last chars not equal

“Is the last character of either X or Yincluded in Z?”

Can we eliminatesome of her answers?

• Last of X is not included

• Last of Y is not included

• Last of X is included

• Last of Y is included

• Neither are included

• Both are included

Given any optSolshe needs to havea valid answer.

?

?

?

An instance:

X = a s b e t c h d a

Y = r t w a b g j c k t f d

Bird & Friend Algorithm

Last chars not equal

“Is the last character of either X or Y included in Z?”

• Last of X is not included

• Last of Y is not included

• Last of X is included

• Last of Y is included

• Neither are included

• Both are included

3

Time?

I try each of 3 bird ans.

My friends try 3

His friends try 3

Same as the brute force algorithm that tries each solution.

“Which is the best path from vito t?”

i

Assign one friend to each sub-instance.

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

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

i [0..|X|]

j [0..|Y|]

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.

Xi = x1,…xiYj = y1,…,yj

i [0..|X|]

 j [0..|Y|]

Guess the complete set S of sub-Instances.

The set S of sub-Instances needs to:

• include our givenI

Yes: i = |X| & j = |Y|

Xi = x1,…xiYj = y1,…,yj

Xi-1 = x1,…xi-1Yj = y1,…,yj

Xi = x1,…xiYj = y1,…,yj

S

Xi = x1,…xiYj-1 = y1,…,yj-1

Xi-1 = x1,…xi-1Yj-1 = y1,…,yj-1

 i [0..|X|]

j [0..|Y|]

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

Xi = x1,…xiYj = y1,…,yj

• each sub-Instance needs to be

asked of some friend, friend, …

 i [0..|X|]

j  [0..|Y|]

Guess the complete set S of sub-Instances.

The set S of sub-Instances needs to:

• include our givenI

• closed under “friend” operation

 sub-Instance S

subsub-Instance  S

We showed this.

This is a fine set of sub-Instances!

 i [0..|X|]

j [0..|Y|]

Construct a table

• for storing the cost of opt sol and bird’s advice.

• for each sub-instance.

Map

Xi = x1,…xiYj= y1,…,yj

Sub-Instances

Indexes

“LCS of x1,…xiand y1,…,yj ?”

Cell of table

j

i

X

Table

Original instance

I = <X,Y>

sub-Instancei,j=

Yj

j=

Xi = x1,…xi

Yj = y1,…,yj

Xi

i=

Table

Optimal Solution

= Longest Common

Subsequence

Cost = length of LCS.

sub-Instancei,j=

Xi = x1,…xi

Yj = y1,…,yj

Table

Yj

Xi

Optimal Solution

= Longest Common

Subsequence

• delete xi

sub-Instancei,j=

Xi = x1,…xi

Yj = y1,…,yj

Table

Yj

Xi

Optimal Solution

= Longest Common

Subsequence

• delete xi

• take both xi and yj

sub-Instancei,j=

Xi = x1,…xi

Yj = y1,…,yj

Table

Yj

Xi

Optimal Solution

= Longest Common

Subsequence

• delete xi

• delete yj

• take both xi and yj

sub-Instancei,j=

Xi = x1,…xi

Yj = y1,…,yj

Fill in Box

Yj

Xi

Fill in box

• Try all bird’s ans.

• delete xi

Friend’s sub-Instance

Our cost

= friend’s cost

5

sub-Instancei,j=

Xi = x1,…xi

Yj = y1,…,yj

Fill in Box

Yj

Xi

Fill in box

• Try all bird’s ans.

• delete yj

Friend’s sub-Instance

Our cost

= friend’s cost

5

sub-Instancei,j=

Xi = x1,…xi

Yj = y1,…,yj

Fill in Box

Yj

Xi

Fill in box

• Try all bird’s ans.

• take both xi and yj

Friend’s sub-Instance

Our cost

= friend’s cost +1

6

sub-Instancei,j=

Xi = x1,…xi

Yj = y1,…,yj

Fill in Box

Yj

Xi

Fill in box

• Try all bird’s ans.

• Take best of best

6

sub-Instancei,j=

Xi = x1,…xi

Yj = y1,…,yj

Fill in Box

Yj

Xi

Fill in box

• Try all bird’s ans.

• delete xi

Friend’s sub-Instance

Our cost

= friend’s cost

4

sub-Instancei,j=

Xi = x1,…xi

Yj = y1,…,yj

Fill in Box

Yj

Xi

Fill in box

• Try all bird’s ans.

• delete yj

Friend’s sub-Instance

Our cost

= friend’s cost

3

sub-Instancei,j=

Xi = x1,…xi

Yj = y1,…,yj

Fill in Box

Yj

Xi

Fill in box

• Try all bird’s ans.

• take both xi and yj

Sorry bird is wrong.

Our cost

= -

-

sub-Instancei,j=

Xi = x1,…xi

Yj = y1,…,yj

4

Fill in Box

Yj

Xi

Fill in box

• Try all bird’s ans.

• Take best of best

Order to fill table:

• so that nobody waits

This guy waits for

(later)

Base Cases:

• general algorithm

does not work

• This guy’s friends are

Base Cases:

• general algorithm

does not work

Done

Get as much value

as you can

into the knapsack

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SviV

• Cost of Solution: The total value of objects in set.

• i.e. iSpi

• Goal: Get as much value as you can into the knapsack.

v=4,p=4

v=4,p=4

V=8

Greedy Algorithm

Greedy Criteria:

Most valuable pi

v=4,p=4

v=4,p=4

v=7,p=5

V=8

gives 8

Optimal

Greedy give 5

pi

vi

v=7,p=5

V=7

Greedy Algorithm

Greedy Criteria:

v=4,p=4

v=4,p=4

v=7,p=5

V=8

V=7

gives 5

Optimal

Greedy give 4

pi

vi

¾ of

v=4,p=4

v=7,p=5

V=7

gives 5

Optimal

Greedy Algorithm

Greedy Criteria:

If fractional solutions are allowed.

Works

Often an Integersolution is MUCH

harder to find.

v=4,p=4

V=7

+ ¾ × 4 = 7

Greedy give 4

= Optimal

v=4,p=4

v=4,p=4

V=9

v=7,p=5

Bird & Friend Algorithm

My instance:

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

A solution:

<<v5,p5>,<v9,p9>,...........,<v82,p82>>.

“What is the last object to take?”

n

v=4,p=4

v=4,p=4

V=9

v=7,p=5

Bird & Friend Algorithm

My instance:

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

A solution:

<<v5,p5>,<v9,p9>,...........,<v82,p82>>.

“Do we keep the last object?”

2 Yes & No

Bird says, Yes keep the last object.

Bird & Friend Algorithm

My instance:

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

v=4,p=4

v=4,p=4

V=9

v=7,p=5

Trust her and put it into your knapsack.

To fill the rest of the knapsack.

But what instance do I give him?

<<v5,p5>,<v9,p9>,...........,<v82,p82>>.

<<v5,p5>,<v9,p9>,...........,<v82,p82>,<vn,pn>>

Bird & Friend Algorithm

His instance:

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

v=4,p=4

v=4,p=4

V=9-4

v=7,p=5

My solution:

same+ pn

My cost:

v=4,p=4

v=4,p=4

V=9

v=7,p=5

<<v5,p5>,<v9,p9>,...........,<v82,p82>,<vn,pn>>

Bird & Friend Algorithm

My instance:

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

If we trust the bird and friend,

this is valid and optimal.

My solution:

same +pn

My cost:

Bird says, No do not keep the last object.

Bird & Friend Algorithm

My instance:

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

v=4,p=4

v=4,p=4

V=9

v=7,p=5

Trust her and delete it.

To fill the knapsack with the rest.

What instance do I give him?

<<v5,p5>,<v9,p9>,...........,<v82,p82>>.

Bird & Friend Algorithm

His instance:

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

v=4,p=4

v=4,p=4

V=9

v=7,p=5

My solution:

same

If we trust the bird and friend,

this is valid and optimal.

same

My cost:

Time?

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.

“Which is the best path from vito t?”

i

Assign one friend to each sub-instance.

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

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.

Determine the complete set of sub-Instances.

Is this a sub-Instance?

<V’:<v1,p1>,<v2,p2>,<v3,p3>>.

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 determine if it is a sub-instance.

V’ [0..V]

<V’:<v1,p1>,<v2,p2>,......,<vi,pi>>.

i  [0..n]

No

S

My instance:

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

Guess the complete set S of sub-Instances.

The set S of sub-Instances needs to:

• include our given I

Yes: V’=V & i = n

• closed under “friend” operation

 sub-Instance S

subsub-Instance S

<V’:<v1,p1>,<v2,p2>,......,<vi,pi>> S

<V’ :<v1,p1>,<v2,p2>,...,<vi-1,pi-1>>

<V’-vi:<v1,p1>,<v2,p2>,...,<vi-1,pi-1>>

Yes

Construct a table

• for storing the cost of opt sol and bird’s advice.

• for each sub-instance.

Map

<V’:<v1,p1>,<v2,p2>,......,<vi,pi>>.

Sub-Instances

i [0..n]

V’ [0..V]

Indexes

“Which of first i objects to put in a knapsack of size v’?”

Cell of table

v’

i

The complete setS of sub-Instances.

V’ [0..V]

<V’:<v1,p1>,<v2,p2>,......,<vi,pi>>.

i [0..n]

same +pi

same

No

1

0

2

V’-vi

V’

V

OptSol Cost & Bird’s Advicefor this

sub-Instance

0

1

2

i-1

Yes

i

Take best of best.

Our cost?

n

The complete set S of sub-Instances.

V’ [0..V]

<V’:<v1,p1>,<v2,p2>,......,<vi,pi>>.

i [0..n]

1

0

2

V’-vi

V’

V

OptSol Cost & Bird’s Advicefor this

sub-Instance

0

1

2

i-1

Order to fill

so nobody

waits?

i

n

The complete set S of sub-Instances is

V’  [0..V]

<V’:<v1,p1>,<v2,p2>,......,<vi,pi>>.

i [0..n]

No

Yes

My instance:

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

Running time

= ( # of sub-instances × # bird answers )

= (Vn× 2 )

= ( 2#bits in V× n )

Polynomial?

Exponential in “size” in instance!

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

• Dynamic Programming Running time = ( V × n ) = ( 2#bits 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.

• Dynamic Programming Running time = ( V × n ) = ( 2#bits 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.

• 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

Schedule as

many events

as possible

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.

• Greedy Criteria:

Earliest Finishing Time

Motivation:

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

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: Totalweight of events scheduled.

• Goal: Given a set of events, schedule max weight

• 1

1

Greedy Algorithm

Greedy Criteria:

Earliest Finishing Time

Motivation:

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

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

“What is the last event to take?”

n

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

“Do we keep the last event?”

2 Yes & No

“Do we keep the last event?”

<<s1,f1,w1>,<s2,f2,w2>,… ,<sn,fn,wn>>.

Bird & Friend Algorithm

An instance:

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

“Do we keep the last event?”

<<s1,f1,w1>,<s2,f2,w2>,… ,<sn,fn,wn>>.

Bird & Friend Algorithm

An instance:

<<s1,f1,w1>,<s2,f2,w2>,… ,<sn,fn,wn>>.

Carefull

His solution:

<<s5,f5,w5>,<s9,f9,w9>,… ,<s82,f82,w82>>.

My solution:

same + <sn,fn,wn>.

same +wn

My cost:

last event

“Yes keep the last event.”

Bird & Friend Algorithm

No this solution is not valid!

Give the rest to my friend.

Here is my best subsolution.

“Yes keep the last event.”

Bird & Friend Algorithm

No we trust the bird!

Then I should politely tell the bird she is wrong

“Yes keep the last event.”

Bird & Friend Algorithm

No we trust the bird!

You only tell her she is wrong if you really know.Eg k words don’t fit on the last lineThe bear does not fit into the knapsack

“Yes keep the last event.”

Bird & Friend Algorithm

No we trust the bird!

Your friend could have just as easily given you this subsolution that does not conflict with the bird’s answer.

“Yes keep the last event.”

Bird & Friend Algorithm

No we trust the bird!

Or maybe he needs to make a sacrifice when finding his answer in order that the overall solution is the best.

“Yes keep the last event.”

Bird & Friend Algorithm

No we trust the bird!

Or goal now is to find the best solution to our instance that is consistent with the bird’s answer.Then we will take the best of best.

“Yes keep the last event.”

Bird & Friend Algorithm

No we trust the bird!

Dude! It is your job to give me the right subinstance so that I give you a subsolution that does not conflict with the bird

last event

“Yes keep the last event.”

Bird & Friend Algorithm

My instance:

Cant keep any events that overlap with it.

last event

“Yes keep the last event.”

Bird & Friend Algorithm

My instance:

“Yes keep the last event.”

Bird & Friend Algorithm

His instance:

His solution

“Yes keep the last event.”

Bird & Friend Algorithm

My instance:

My solution:

My solution:

same + <sn,fn,wn>.

Yes

Valid?

“Yes keep the last event.”

Bird & Friend Algorithm

My instance:

My solution:

My solution:

same + <sn,fn,wn>.

same+wn

My cost:

Time?

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.

“Which is the best path from vi to t?”

i

Assign one friend to each sub-instance.

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

Every subset of {1,…,9}is a possible sub-Instance.

I.e. could be an exponentialnumber of them.

Hence, running time is exponential.

Greedy algorithm sortedjobs by finishing time.Let us do that too.

Each sub-Instance is prefix.

I.e. only n of them.

Hence, running time is polynomial!

i [0..n]

<<s1,f1,w1>,<s2,f2,w2>,… ,<si,fi,wi>>

My instance:

<<s1,f1,w1>,<s2,f2,w2>,................… ,<sn,fn,wn>>.

Guess the complete set S of sub-Instances.

The set S of sub-Instances needs to:

• include our givenI

Yes: i = n

• closed under “friend” operation

?

 sub-Instance S

subsub-Instance S

• each sub-Instance needs to be

Only nsub-Instances

Good enough.

sub-Instance =

last event

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

Show closed under “friend” operation

 sub-Instance S

subsub-Instance S

Events sorted by finishing time.

sub-Instance =

last event

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

“Yes keep the last event.”

Show closed under “friend” operation

 sub-Instance S

subsub-Instance S

Delete overlapping events for friend.

subsub-Instance =

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

“Yes keep the last event.”

Show closed under “friend” operation

 sub-Instance S

subsub-Instance S

Delete overlapping events for friend.

subsub-Instance =

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

Event j is kept fjsi

Show closed under “friend” operation

 sub-Instance S

subsub-Instance S

typical kept job

typical deleted job

 set of kept jobs is a prefix of events.

subsub-Instance S

i [0..n]

<<s1,f1,w1>,<s2,f2,w2>,… ,<si,fi,wi>>

0, 1, 2, 3, 4, …. n

Base case

Original

My instance:

<<s1,f1,w1>,<s2,f2,w2>,................… ,<sn,fn,wn>>.

The complete set S of sub-Instances is

Table:

i [0..n]

<<s1,f1,w1>,<s2,f2,w2>,… ,<si,fi,wi>>

My instance:

<<s1,f1,w1>,<s2,f2,w2>,................… ,<sn,fn,wn>>.

The complete set S of sub-Instances is

Running time

= # of sub-instances × # bird answers

= n× 2

But to find your friend’s “yes” sub-instanceyou must know how many events overlapwith your last event. This takes time:

O(logn) using binary searchfor a total of O(nlogn) time.

Done

Input:

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

Output:

Recursive Alg:

• GetExp calls GetTerm

• GetTerm calls GetFact

• GetFact calls GetExp

C CB

AC

c

d

B TA

BC

b

e

AAA

BT

a

TAB

CA

TT

Context Free Grammar

For ease, we will assume every non-terminal

either goes to two non-terminals

or to one terminal

T

A

C

C

T

B

A

A

B

C

A

A

C

T

A

C

B

A

B

a d b

b d a

b a e a

CCB

AC

c

d

BTA

BC

b

e

AAA

BT

a

TAB

CA

TT

Input:

start non-terminal = T

string to parse

= a1a2a3 ..... an

T a1a2a3 ..... an

Output: A parsing

Input:

T a1a2a3 ..... an

T

A

C

C

T

B

A

A

B

C

A

A

C

T

A

C

B

A

B

a d b

b d a

b a e a

CCB

AC

c

d

BTA

BC

 b

e

AAA

BT

a

TAB

CA

TT

Recursive Algorithm:

GetT does not know whether to call

GetA, GetC, or GetT.

Input:

T a1a2a3 ..... an

A

C

C

T

B

A

A

B

C

A

A

C

T

A

C

B

A

B

a d b

b a e a

CCB

AC

c

d

B TA

BC

b

e

A AA

BT

a

T AB

CA

TT

T

• For first rule

• Parse left

• Parse right.

b d a

Input:

T a1a2a3 ..... an

A

C

a d b

b a e a

CCB

AC

c

d

BTA

BC

b

e

AAA

BT

a

TAB

CA

TT

T

• For first rule

Instance to give Friend

• ?

b d a

Input:

T a1a2a3 ..... an

T

A

C

A

B

A

A

C

T

A

C

B

A

B

a d b

b d a

b a e a

C CB

AC

c

d

BTA

BC

b

e

AAA

BT

a

TAB

CA

TT

• For first rule

Want from Friend:

• Left sub-parse tree.

Instance to give him:

C

Input:

T a1a2a3 ..... an

A

C

C

T

B

A

A

B

C

A

A

C

T

A

C

B

A

B

a d b

b a e a

C CB

AC

c

d

BTA

BC

b

e

AAA

BT

a

TAB

CA

TT

T

• For first rule

How can we know split?

b d a

Input:

T a1a2a3 ..... an

A

C

a d b

b a e a

C CB

AC

c

d

BTA

BC

b

e

AAA

BT

a

TAB

CA

TT

T

• For first rule

• For the split.

# of ans K =

mT = # of rules for T.

n = # chars in string.

# of ans K =

mT×n.

Total # of ans K =

b d a

Input:

T a1a2a3 ..... an

T

A

C

A

B

A

A

C

T

A

C

B

A

B

a d b

b d a

b a e a

CCB

AC

c

d

BTA

BC

b

e

AAA

BT

a

TAB

CA

TT

• Solution: Left parsing

C

Input:

T a1a2a3 ..... an

T

C

T

B

A

C

a d b

b a e a

CCB

AC

c

d

BTA

BC

b

e

AAA

BT

a

TAB

CA

TT

A

• Instance: Abda

• Solution: Right parsing

b d a

Input:

T a1a2a3 ..... an

T

A

C

C

T

B

A

A

B

C

A

A

C

T

A

C

B

A

B

a d b

b d a

b a e a

CCB

AC

c

d

BTA

BC

b

e

AAA

BT

a

TAB

CA

TT

Combine:

• Instance:

Time?

I try each of 2 bird ans.

My friends tries 2

His friends tries 2

Same as the bruteforce algorithm that tries each solution.

“Which is the best path from vi to t?”

i

Assign one friend to each sub-instance.

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

Determine the complete set of sub-Instances.

A

gives:

gives:

A

My instance I:

T a1a2a3 ..... an

T

C

C

My left sub-Instance.

His right sub-Instance.

a d b

b d a

b a e a

Determine the complete set of sub-Instances.

T’=

C

a1...ai-1

aiai+1...aj

aj+1...an

a d b

My instance I:

T a1a2a3 ..... an

sub-Instances:

T’ aiai+1 ..... aj

•  non-terminals T’

•  i,j  [1,n]

# of non-terminals ×n2

# of sub-Instances =

Construct a table

• for storing the cost of opt sol and bird’s advice.

• for each sub-instance.

Map

Sub-Instances

T’ aiai+1 ..... aj

T’  i,j  [1,n]

Indexes

i

T’

Cell of table

j

gives: First rule and split

sub-Instances:

T’ aiai+1 ..... aj  non-terminals T’

&  i,j  [1,n]

Running time

= ( # of sub-instances × # bird answers )

= (# of non-terminals × n2

× # of rules · n )

Done

An instance (input) consists of a circuit:

c =(x3orx5orx6) and (x2orx5orx7) and (x3orx4)

true

true

false

true

true

true

true

A solution is an assignment of the variables.

true

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.

Instance:

c = (x3orx5orx6) and (x2orx5orx7) and (x3orx4)

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.

true

false

true

Sub-Instance:

c =(x2orx5orx7) 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

Instance:

c =(x3orx5orx6) and (x2orx5orx7) and (x3orx4)

Commit to x3 = 0 and simplify

x3

1

0

x2

x1

1

0

1

0

x1

x2

x2

x1

1

1

1

0

0

0

1

0

Speeding Up the Time

In the end, some friend looks at each of the 2n assignments,

“Which is the best path from vi to t?”

 i

Assign one friend to each sub-instance.

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

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.

x3

1

0

x2

x1

1

0

1

0

x1

x2

x2

x1

1

1

1

0

0

0

1

0

Speeding Up the Time

In the end, some friend looks at each of the 2n assignments,

x3

1

0

x2

x1

1

0

1

0

x1

x2

x2

x1

1

1

1

0

0

0

1

0

Speeding Up the Time

But sometimes we can prune off branches.

x3 is forced to x3 = 0

x3

1

0

x2

x1

c =(x2orx5orx7) and x3

1

0

1

0

x1

x2

x2

x1

1

1

1

0

0

0

1

0

Instance:

This is trivially unsatisfiable because x3 can’t be both 0 and 1.

x3

1

0

x2

x1

c =(x2orx5orx7) and x3and x3

1

0

1

0

x1

x2

x2

x1

1

1

1

0

0

0

1

0

Instance:

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

• Assume he gives you optSubSol for sub-Instance.

• How do you produce an optSol for I from

• the bird’s k and

• the friend’s optSubSol?

• How do you determine the cost of optSol from

• the bird’sk and

• the cost of the friend’s optSubSol?

• Try all bird’s answers and take best of best.

Recursive Back Tracking Algorithm

Dynamic Programming Algorithm

• Given an instance I,

• Imagine running the recursive alg on it.

• Determine the complete set of sub-Instancesever given to you, your friends, their friends, …

• Build a table indexed by these sub-Instances

• Fill in the table in order so that nobody waits.

• the cost of its optimal solution

• advice given by the bird

• Run the recursive algorithm with bird’s advice to

find the solution to your instance.

• Don’t mix up the following

• What is an instance

• What are the objects in an instance

• What is a solution

• What are the objects in a solution

• What is the cost of a solution

• Greedy algorithm

• What does the algorithm do & know

• What does the Prover do & know

• What does the Fairy God Mother do & know

• Recursive Backtracking / Dynamic Programming

• What does the algorithm do & know

• What does the little bird do & know

• What does the friend do & know

Dynamic ProgrammingDon’ts

• Yes, the code has a basic structure that you should learn.

• But don’t copy other code verbatim

• Don’t say if(ai = cj) (i.e. Longest Common Subsequence) when our problem does not have cj

Dynamic ProgrammingDon’ts

• When looping over the sub-instances

• be clear what the set of sub-instances are

• which is currently being solved, i.e. which instance is cost(i,j)?

• If you know that the set of sub-instances are the prefixes of the input, i.e. <a1,a2, …, ai>, then don’t have a two dimensional table. Table[1..n,1..n].

• Don’t loop over i and loop over j if j never gets mentioned again.

Dynamic ProgrammingDon’ts

• When trying all bird answers

• be clear what the set of bird answers are,

• which is currently being tried,

• & what it says about the solution being looked for.

• When getting help from your friend,

• be clear what the sub-instance is that you are giving him

• How do you use the current instance and the bird' s answer to form his sub-instance?

• Don’t simply say cost(i-1,j-1)

Dynamic ProgrammingDon’ts

• Think about what the base cases should be.

• Don’t make an instance a base cases if they can be solved using the general method.

• % is used to start a comment. Don’t put it in front of code.

38

• Eg. The Best Binary Search Tree problem,

25

51

17

31

42

63

4

21

28

35

40

49

55

71

The Question For the Little Bird

• If a solution is a binary tree of objects,

• “What object is at the root of the tree?”

i

“Best path from vi to t?”

Dynamic Programming

Construct a table

• for storing an optimal solution & cost

• for each sub-instance.

Map

Sub-Instances

i ϵ [n], i.e. for each node vi

Index

Cell of table

“Which is the best path from vi to t?”

t, v8, v7, v6, v5, …., s

i