Recursive back tracking dynamic programming
This presentation is the property of its rightful owner.
Sponsored Links
1 / 394

Recursive Back Tracking & Dynamic Programming PowerPoint PPT Presentation


  • 45 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

Recursive Back Tracking & Dynamic Programming

An Image/Link below is provided (as is) to download presentation

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

Thinking about Algorithms Abstractly

Recursive Back Tracking&Dynamic Programming

Jeff Edmonds York University

Lecture7

COSC 3101


Recursive back tracking dynamic programming

Problems

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


Dynamic programming

Dynamic Programming

  • A hard topic.

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

  • Even if you 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.


Optimization problems

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.


Recursive back tracking dynamic programming

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.


Recursive back tracking dynamic programming

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!


Search graph for best path

Search Graph For Best Path

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


Search graph for best path1

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


Search graph for best path2

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


Recursive back tracking dynamic programming

Brute Force Algorithm

Try all paths, return the best.

But there may be an exponential number of paths!


An algorithm as a sequence of decisions

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


An algorithm as a sequence of decisions1

Taking the best first edge.

An Algorithm As A Sequence of Decisions

I ask a question about the solution.

“Which edge should we take first?”

How do I decide?

The greedy algorithm?

Does not work!


Local vs global considerations

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.


An algorithm as a sequence of decisions2

  • But let's skip this partby pretending that we have

    a little bird to answer this little question.

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.


Recursive back tracking dynamic programming

"Little Bird" Abstraction

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)


Little bird friend alg

But we don’t want toworry about how our friend

solves his problem.

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


Sub instance for friend

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?


Little bird friend alg1

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 bird’s edge making the path <s,v1,v6,t>

with weight

10+3=13.


Faulty bird

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


Faulty bird1

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

In reality we will try all possible first edges,

giving …..


Faulty bird2

Faulty Bird

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


Faulty bird3

Faulty Bird

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


Faulty bird4

Faulty Bird

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


Faulty bird5

Faulty Bird

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


Faulty bird6

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.


Recursive back tracking dynamic programming

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 solutionforms a tree of possible answers.


Recursive back tracking dynamic programming

Bird/Friend - Best of the Best

Consider our instance I.

Consider the set of solutions

k

But we only care aboutthe first bird answer.

k

The answers classifiesthe possible solutions.

Solutions consistent with the kth bird' s answer.


Recursive back tracking dynamic programming

Bird/Friend - Best of the Best

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.


Recursive back tracking dynamic programming

Bird/Friend - Best of the Best

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


Recursive back tracking dynamic programming

Bird/Friend - Best of the Best

Constructing optS<I,k> :

the optimum solution

for instance I

consistent with the kth bird' s answer.

Given my instance I.

I ask my little bird foran answer k.

I ask my friend for his solution.

I combine them.


Recursive back tracking dynamic programming

Recursive backtracking

code always has thissame basic structure.


Recursive back tracking dynamic programming

Be clear what are

  • the instances

  • it’s solution

  • the cost of a sol.


Recursive back tracking dynamic programming

Loop through the bird answers.

Be clear which is the current one being tried.


Recursive back tracking dynamic programming

Give the bird & friend algorithmas a comment.

(Unless it is in an earlier question.)


Recursive back tracking dynamic programming

What is the bird asked?

What does she answer?


Recursive back tracking dynamic programming

Get help from friend

Be clear what

sub-instance you give him.

Store the solution& cost he gives you.


Recursive back tracking dynamic programming

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


Recursive back tracking dynamic programming

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


Recursive back tracking dynamic programming

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

Take the bestof the best


Recursive back tracking dynamic programming

Return the solutionand cost for the original instance.


Recursive back tracking dynamic programming

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

What are these?

What are their solutionsand costs?


Optimal substructure

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.


Optimal substructure1

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 substructure2

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.


Don t all problems have this optimal substructure property

Optimal Substructure

Don’t all problems have this optimal substructure property?


Longest simple path

Consider the following graph:

The longest simple path (path not containing a cycle) from A to D is A B C D

However, the subpath A B is not the longest simple path from A to B (A C B is longer)

The principle of optimality is not satisfied for this problem

Hence, the longest simple path problem cannot be solved by a dynamic programming approach

B

1

2

3

1

4

C

D

A

Longest simple path

Optimal Substructure

NP-Complete


Recursive back tracking dynamic programming

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.


Recursive back tracking dynamic programming

Same as Brute Force Algorithm

But there may be an exponential number of paths!


Recursive back tracking dynamic programming

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.


Recursive back tracking dynamic programming

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.


Recursive back tracking dynamic programming

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.


Recursive back tracking dynamic programming

Exponential Time

Redoing Work

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

How many friends solve this sub-instance?


Recursive back tracking dynamic programming

Exponential Time

Redoing Work

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


Recursive back tracking dynamic programming

Exponential Time

Redoing Work

“Which is the best path from v1to t?”


Recursive back tracking dynamic programming

Exponential Time

Redoing Work

“Which is the best path from v4to t?”


Recursive back tracking dynamic programming

Exponential Time

Redoing Work

“Which is the best path from v7to t?”

There’s one.


Recursive back tracking dynamic programming

Exponential Time

Redoing Work

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


Recursive back tracking dynamic programming

Exponential Time

Redoing Work

“Which is the best path from v3to t?”


Recursive back tracking dynamic programming

Exponential Time

Redoing Work

“Which is the best path from v5to t?”


Recursive back tracking dynamic programming

Exponential Time

Redoing Work

“Which is the best path from v7to t?”

There’s another.


Recursive back tracking dynamic programming

Exponential Time

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.


Recursive back tracking dynamic programming

Depth First

Search

Drop

bread crumbs and don’t revisit.

But we need shortest path


Recursive back tracking dynamic programming

Dynamic Programming

We allocate one friend to the job.

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


Recursive back tracking dynamic programming

Dynamic Programming

It is my job to learn and remember

the optSol to my sub-Instance

i.e. the best path from v7to t


Recursive back tracking dynamic programming

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 pathand tell you.


Recursive back tracking dynamic programming

Dynamic Programming

When I need to find

the best path from v2 to t

I will ask you for

the best path from v7 to t

I remember my best pathand will tell you.


Recursive back tracking dynamic programming

Dynamic Programming

When I need to find

the best path from v5 to t

I will ask you for

the best path from v7 to t

I remember my best pathand will tell you.


Recursive back tracking dynamic programming

Dynamic Programming

Avoid waiting.

When I need to find

the best path from v2 to t

I will ask you for

the best path from v7 to t

I will find my best pathand tell you.

But I hate to wait for you.

Recursion has a lot of overhead

Why don’t you go first?


Recursive back tracking dynamic programming

Dynamic Programming

Before anyone asks me,I will find my best path

and remember it.


Recursive back tracking dynamic programming

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


Recursive back tracking dynamic programming

Set of Sub-Instances

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.


Recursive back tracking dynamic programming

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

All paths considered end in t.


Recursive back tracking dynamic programming

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

All paths considered end in t.


Recursive back tracking dynamic programming

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


Recursive back tracking dynamic programming

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


Recursive back tracking dynamic programming

 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


Recursive back tracking dynamic programming

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

Integers closed under addition

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

 sub-Instance S

subsub-Instance S


Recursive back tracking dynamic programming

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


Recursive back tracking dynamic programming

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


Recursive back tracking dynamic programming

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


Recursive back tracking dynamic programming

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


Recursive back tracking dynamic programming

Base Case

easy

Dynamic Programming

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


Recursive back tracking dynamic programming

Dynamic Programming

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

easy


Recursive back tracking dynamic programming

Dynamic Programming

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

easy


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v6to t?"

easy


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v5to t?"

Harder


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v5to t?"

Little bird suggests first edge <v5,v7>

Friend gives best

path <v7,t>.


Recursive back tracking dynamic programming

Dynamic Programming

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

Little bird suggests first edge <v5,v8>

Friend gives best

path <v8,t>.


Recursive back tracking dynamic programming

Dynamic Programming

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

Take best of best


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v4to t?"


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v4to t?"

Little bird suggests first edge <v4,v6>

Friend gives best

path <v7,t>.


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v4to t?"

Little bird suggests first edge <v4,t>

Friend gives best

path <t,t>.


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v4to t?"

Little bird suggests first edge <v4,v7>

Friend gives best

path <v7,t>.


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v4to t?"

Take best of best


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v3to t?"


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v3to t?"

Little bird suggests first edge <v3,v5>

Friend gives best

path <v5,t>.


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v3to t?"

Little bird suggests first edge <v3,v8>

Friend gives best

path <v8,t>.


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v3to t?"

Take best of best


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v2to t?"


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v2to t?"

Little bird suggests first edge <v2,v4>

Friend gives best

path <v4,t>.


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v2to t?"

Little bird suggests first edge <v2,v7>

Friend gives best

path <v7,t>.


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v2to t?"

Take best of best


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v1to t?"


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v1to t?"

Little bird suggests first edge <v1,v3>

Friend gives best

path <v3,t>.


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v1to t?"

Little bird suggests first edge <v1,v4>

Friend gives best

path <v4,t>.


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v1to t?"

Little bird suggests first edge <v1,v5>

Friend gives best

path <v5,t>.


Recursive back tracking dynamic programming

Dynamic Programming

"Which is the best path from v1to t?"

Take best of best


Recursive back tracking dynamic programming

Dynamic Programming

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

Original Problem


Recursive back tracking dynamic programming

Dynamic Programming

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

Little bird suggests first edge <s,v1>

Friend gives best

path <v1,t>.


Recursive back tracking dynamic programming

Dynamic Programming

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

Little bird suggests first edge <s,v2>

Friend gives best

path <v2,t>.


Recursive back tracking dynamic programming

Dynamic Programming

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

Little bird suggests first edge <s,v3>

Friend gives best

path <v3,t>.


Recursive back tracking dynamic programming

Dynamic Programming

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

Little bird suggests first edge <s,v4>

Friend gives best

path <v4,t>.


Recursive back tracking dynamic programming

Dynamic Programming

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

Take best of best

DONE


Recursive back tracking dynamic programming

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


Recursive back tracking dynamic programming

Dynamic Programming

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

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

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

Base case

Original


Recursive back tracking dynamic programming

Communication

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]


Recursive back tracking dynamic programming

Dynamic Programming

code always has thissame basic structure.


Recursive back tracking dynamic programming

Be clear what are

  • the instances

  • it’s solution

  • the cost of a solution.


Recursive back tracking dynamic programming

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.


Recursive back tracking dynamic programming

Be clear what sub-instances are.

How are they indexed?

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


Recursive back tracking dynamic programming

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.


Recursive back tracking dynamic programming

Then we iterate through the remaining sub-instances.

From smallest to largest.

Each gets solvedand their solutions stored.


Recursive back tracking dynamic programming

Consider yourselfto be a friendworking on oneof these.

Be clear which sub-instance is yours.

Solve this as you did before.


Recursive back tracking dynamic programming

Loop through the bird answers.

Be clear which is the current one being tried.


Recursive back tracking dynamic programming

Give the bird & friend algorithmas a comment.

(Unless it is in an earlier question.)


Recursive back tracking dynamic programming

What is the bird asked?

What does she answer?


Recursive back tracking dynamic programming

k

k

k

k

Get help from friend

Be clear what sub-instance you give your friend.


Recursive back tracking dynamic programming

k

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.


Recursive back tracking dynamic programming

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


Recursive back tracking dynamic programming

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


Recursive back tracking dynamic programming

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


Recursive back tracking dynamic programming

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


Recursive back tracking dynamic programming

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

Is this code correct?


Recursive back tracking dynamic programming

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


Recursive back tracking dynamic programming

But there is a tableof subinstancesthat must be solved.

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


Recursive back tracking dynamic programming

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.


Recursive back tracking dynamic programming

But there is a tableof subinstancesthat must be solved.

Then we solvethe rest.


Recursive back tracking dynamic programming

s=

0

0

n

0

Return the solutionand cost for the original instance.


Recursive back tracking dynamic programming

Order Feels Backwards

Path from s to t.

Path from t to t.


Recursive back tracking dynamic programming

An Esthetically Better

Path from s to s.

Path from s to t.


Recursive back tracking dynamic programming

Reversing


Recursive back tracking dynamic programming

Reversing

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

i

Determine the complete set of sub-instances


Recursive back tracking dynamic programming

Reversing

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

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

s, v1, v2, v3, v4, …., t

Base case

Original


Recursive back tracking dynamic programming

Running Time

?

Time =

# of Sub-Instances

× # of Bird Answers

× q(1)

= n

× d


Recursive back tracking dynamic programming

Communication Time

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


Recursive back tracking dynamic programming

Running Time

# of Sub-Instances

× q(1)

Store path costs, not paths

= q(n)

Time =

# of Sub-Instances

× # of Bird Answers

× size of solution

= q(n × d × n)

Space =


Recursive back tracking dynamic programming

Store Path Costs, not Paths

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


Recursive back tracking dynamic programming

Store Path Costs, not Paths

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


Recursive back tracking dynamic programming

Store Path Costs, not Paths

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.


Recursive back tracking dynamic programming

Store Path Costs, not Paths

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


Recursive back tracking dynamic programming

Store Path Costs, not Paths

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


Recursive back tracking dynamic programming

Running Time

birdsAdvice[i] = kmin


Recursive back tracking dynamic programming

Leave these lines

as comments

for extra clarity for the reader


Find optimal path

Find Optimal Path

Previous algorithm gives:

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

  • Bird’s advice of last edge to vi.

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


Find optimal path1

Find Optimal Path

The bird gives that the last edge

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


Find optimal path2

Find Optimal Path

The bird gives that the last edge

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


Find optimal path3

Find Optimal Path

The bird gives that the last edge

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


Find optimal path4

Find Optimal Path

The bird gives that the last edge

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


Find optimal path5

Find Optimal Path

Done!


Find optimal path6

Find Optimal Path

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


Multiple optimal solutions

I ask the bird:

“Which is the last edge?”

Multiple Optimal Solutions

She could give either answer.

By giving this edge she says

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

Similar to greedy proof.

6


Multiple optimal solutions1

I ask the bird:

“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


Recursive back tracking dynamic programming

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

Review

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.


The question for the little bird

The Question For the Little Bird

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.

    • Hint: Ask about a local property

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


  • The question for the little bird1

    A solution: a path

    t

    s

    I ask the bird:

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


    The question for the little bird2

    A solution: a path

    t

    s

    I ask the bird:

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


    The question for the little bird3

    A solution: a path

    t

    s

    I ask the bird:

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


    The question for the little bird4

    A solution: a path

    t

    s

    I ask the bird:

    The Question For the Little Bird

    An instance: Graph, s, and t

    “What is the last edge in the path?”

    Giving a good follow up question for your friend to ask the bird.

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


    The question for the little bird5

    • Eg. “What is best last edge?”

    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.


    The question for the little bird6

    • Eg. “What is an optimal solution?”

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


    The question for the little bird7

    A solution: a path

    t

    s

    I ask the bird:

    The Question For the Little Bird

    An instance: Graph, s, and t

    “How many edges are in the path?”

    Bad Question:

    • it is not a local property

    • How does this help us solve the problem?

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


    The question for the little bird8

    I ask the bird:

    A solution: a sequence of objects

    Z = a b c d

    I ask my friend:

    The Question For the Little Bird

    An instance: ???

    “What is the last object in the sequence?”

    # of answersK =

    # of possible last objects.

    “What is the rest of the solution?”


    The question for the little bird9

    An instance: a sequence of objects

    I ask the bird:

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

    # of possible last objects.

    Is there a smaller question that we could ask?


    The question for the little bird10

    An instance: a sequence of objects

    I ask the bird:

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

    # of answersK =

    2, Yes or No


    The question for the little bird11

    I ask the bird:

    A solution: a binary tree of objects

    38

    25

    51

    I ask a second friend:

    I ask my friend:

    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.


    The question for the little bird12

    I ask the bird:

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

    Bad Question:

    • How does this help us solve the problem?

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


    Printing neatly

    Printing Neatly


    Printing neatly1

    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.


    Recursive back tracking dynamic programming

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


    Bird friend algorithm

    I ask the bird:

    I combine

    bird’s and

    friend’s answers.

    I ask the friend:

    Bird & Friend Algorithm

    An instance:

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

    “How many words on the last line?”

    She may answer 3.

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


    Bird friend algorithm1

    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.


    Recursive back tracking dynamic programming

    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.


    Recursive back tracking dynamic programming

    Memoization

    i

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

    Assign one friend to each sub-instance.


    Recursive back tracking dynamic programming

    Set of Sub-Instances

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


    Recursive back tracking dynamic programming

    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

    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.


    Recursive back tracking dynamic programming

    Set of Sub-Instances

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


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    The bird answers words,

    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


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    “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


    Recursive back tracking dynamic programming

    Set of Sub-Instances

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

    “Love life man while there as we”, 11

    “Love life man while there as”, 11

    “Love life man while there”, 11

    “Love life man while”, 11

    The bird

    answers 1.

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


    Recursive back tracking dynamic programming

    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.


    Recursive back tracking dynamic programming

    The Table

    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


    Recursive back tracking dynamic programming

    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


    Recursive back tracking dynamic programming

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


    Recursive back tracking dynamic programming

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

    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


    Recursive back tracking dynamic programming

    “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


    Recursive back tracking dynamic programming

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

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

    be.........


    Recursive back tracking dynamic programming

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

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

    we.be......


    Recursive back tracking dynamic programming

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

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

    as.we.be...


    Recursive back tracking dynamic programming

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

    there.as.we.be


    Recursive back tracking dynamic programming

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

    Tried all bird answers.

    Choose best of the best.


    Recursive back tracking dynamic programming

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

    Choose best of the best.


    Recursive back tracking dynamic programming

    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.


    Recursive back tracking dynamic programming

    Be clear what are

    • the instances

    • it’s solution

    • the cost of a solution.


    Recursive back tracking dynamic programming

    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.


    Recursive back tracking dynamic programming

    Be clear what sub-instances are.

    How are they indexed?

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


    Recursive back tracking dynamic programming

    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.


    Recursive back tracking dynamic programming

    Then we iterate through the remaining sub-instances.

    From smallest to largest.

    Each gets solvedand their solutions stored.

    Actually, we store thebird’s advice instead of the solution.


    Recursive back tracking dynamic programming

    Consider yourselfto be a friendworking on oneof these.

    Be clear which sub-instance is yours.

    Solve this as you did before.


    Recursive back tracking dynamic programming

    Loop through the bird answers.

    Be clear which is the current one being tried.


    Recursive back tracking dynamic programming

    Give the bird & friend algorithmas a comment.

    (Unless it is in an earlier question.)


    Recursive back tracking dynamic programming

    What is the bird asked?

    What does she answer?


    Recursive back tracking dynamic programming

    i-k

    i-k

    i-k

    Get help from friend

    Be clear what sub-instance you give your friend.


    Recursive back tracking dynamic programming

    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.


    Recursive back tracking dynamic programming

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


    Recursive back tracking dynamic programming

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


    Recursive back tracking dynamic programming

    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


    Recursive back tracking dynamic programming

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

    Actually, we store thebird’s advice instead of the solution.


    Recursive back tracking dynamic programming

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

    Is this code correct?


    Recursive back tracking dynamic programming

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


    Recursive back tracking dynamic programming

    But there is a tableof subinstancesthat must be solved.

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


    Recursive back tracking dynamic programming

    But there is a tableof subinstancesthat must be solved.

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


    Recursive back tracking dynamic programming

    But there is a tableof subinstancesthat must be solved.

    Then we solvethe rest.


    Recursive back tracking dynamic programming

    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.


    Recursive back tracking dynamic programming

    # of Sub-Instances

    × # of Bird Answers

    = q(n × n)

    Time =

    Space =

    # of Sub-Instances

    = q(n)


    Find optimal path7

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

    Find Optimal Path

    Previous algorithm gives cost and bird’s advice.


    Recursive back tracking dynamic programming

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

    ,1

    ,3>

    ,2

    <2

    Love.life..

    man.while..

    there……

    as we be…


    Recursive back tracking dynamic programming

    Longest Common Subsequence problem

    X =asbe fchda

    Y = r t wabg jck t fd


    Recursive back tracking dynamic programming

    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.


    Bird friend algorithm2

    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

    I ask the bird:

    Bird & Friend Algorithm

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

    She answers one of :

    • Last of X is not included

    • Last of Y is not included

    • Last of X is included

    • Last of Y is included

    • Neither are included

    • Both are included


    Bird friend algorithm3

    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 ask the bird:

    I ask my friend:

    Bird & Friend Algorithm

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

    She answers:

    • Last of X is not included


    Bird friend algorithm4

    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 ask the bird:

    I combine

    bird’s and

    friend’s answersand give

    Bird & Friend Algorithm

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

    She answers:

    • Last of X is not included

    My friend answers:

    Z = a b c d

    X =asbe tchd

    Y = r t wabg jck t fd

    the sameZ.


    Bird friend algorithm5

    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 ask the bird:

    I ask my friend:

    Bird & Friend Algorithm

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

    She answers:

    • Last of Y is not included


    Bird friend algorithm6

    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 ask the bird:

    I combine

    bird’s and

    friend’s answersand give

    Bird & Friend Algorithm

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

    She answers:

    • Last of Y is not included

    My friend answers:

    the same Z.

    Z = a b c

    X =asbe tchd a

    Y = r t wabg jck t f

    Not as good as lastbut we need to try.


    Bird friend algorithm7

    The instance:

    X = a s b e t c h

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

    I ask the bird:

    I ask my friend:

    Bird & Friend Algorithm

    An instance:

    X = a s b e t c h d

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

    Last chars equal

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

    She answers:

    • Last of X and last of Y are both included


    Bird friend algorithm8

    The instance:

    X = a s b e t c h

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

    I ask the bird:

    I combine

    bird’s and

    friend’s answersand give

    Bird & Friend Algorithm

    An instance:

    X = a s b e t c h d

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

    Last chars equal

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

    She answers:

    • Last of X and last of Y are both included

    My friend answers:

    Z = a b c

    X =asbe tch

    Y = r t wabg jck d f

    Zd = abcd.


    Bird friend algorithm9

    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

    I ask the bird:

    Bird & Friend Algorithm

    Last chars not equal

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

    She answers:

    • Last of X and last of Y are both included

    I politely tell her that she is wrong.


    Bird friend algorithm10

    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 ask the bird:

    I ask my friend:

    Bird & Friend Algorithm

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

    She answers:

    • Last of X is included


    Bird friend algorithm11

    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 ask the bird:

    I combine

    bird’s and

    friend’s answersand give

    Bird & Friend Algorithm

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

    She answers:

    • Last of X is included

    My friend answers:

    Z = a b c d

    X =asbe tch d

    Y = r t wabg jck t f d

    Wrong

    Za = abcda.


    Bird friend algorithm12

    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 ask the bird:

    I ask my friend:

    Bird & Friend Algorithm

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

    She answers:

    • Last of X is included


    Bird friend algorithm13

    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 ask the bird:

    I combine

    bird’s and

    friend’s answersand give

    Bird & Friend Algorithm

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

    She answers:

    • Last of X is included

    My friend answers:

    Z = t

    X =a s b e t c h d

    Y = r t w

    Za = ta.


    Bird friend algorithm14

    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

    I ask the bird:

    Bird & Friend Algorithm

    Last chars not equal

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

    Can we eliminatesome of her answers?

    She answers one of :

    • Last of X is not included

    • Last of Y is not included

    • Last of X is included

    • Last of Y is included

    • Neither are included

    • Both are included

    Given any optSolshe needs to havea valid answer.

    ?

    ?

    ?


    Bird friend algorithm15

    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

    I ask the bird:

    Bird & Friend Algorithm

    Last chars not equal

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

    She answers one of :

    • Last of X is not included

    • Last of Y is not included

    • Last of X is included

    • Last of Y is included

    • Neither are included

    • Both are included

    3

    # of answersK =


    Recursive back tracking dynamic programming

    Same as Brute Force Algorithm

    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.


    Recursive back tracking dynamic programming

    Memorization

    “Which is the best path from vito t?”

    i

    Assign one friend to each sub-instance.


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    Determine the complete set of sub-Instances.

    Given an instance I,

    X = a s b e t c h d a

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

    • Imagine running the recursive alg on it.

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

    Is this a sub-Instance?

    X’ = a s b e t c

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

    Yes


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    Determine the complete set of sub-Instances.

    Given an instance I,

    X = a s b e t c h d a

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

    • Imagine running the recursive alg on it.

    • Determine the complete set of sub-Instances ever given to you, your friends, their

      friends, …

    Is this a sub-Instance?

    No

    X’ = b e t

    Y’ = a b g j c k


    Recursive back tracking dynamic programming

    Set of Sub-Instances

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


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    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|


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    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


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    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!


    Recursive back tracking dynamic programming

    The Table

     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


    Recursive back tracking dynamic programming

    The Table


    Table

    Y

    X

    Table

    Original instance

    I = <X,Y>


    Table1

    sub-Instancei,j=

    Yj

    j=

    Xi = x1,…xi

    Yj = y1,…,yj

    Xi

    i=

    Table

    Optimal Solution

    = Longest Common

    Subsequence

    Cost = length of LCS.


    Table2

    sub-Instancei,j=

    Xi = x1,…xi

    Yj = y1,…,yj

    Table

    Yj

    Xi

    Optimal Solution

    = Longest Common

    Subsequence

    Bird’s Advice

    • delete xi


    Table3

    sub-Instancei,j=

    Xi = x1,…xi

    Yj = y1,…,yj

    Table

    Yj

    Xi

    Optimal Solution

    = Longest Common

    Subsequence

    Bird’s Advice

    • delete xi

    • take both xi and yj


    Table4

    sub-Instancei,j=

    Xi = x1,…xi

    Yj = y1,…,yj

    Table

    Yj

    Xi

    Optimal Solution

    = Longest Common

    Subsequence

    Bird’s Advice

    • delete xi

    • delete yj

    • take both xi and yj


    Fill in box

    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


    Fill in box1

    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


    Fill in box2

    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


    Fill in box3

    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


    Fill in box4

    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


    Fill in box5

    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


    Fill in box6

    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

    = -

    -


    Fill in box7

    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


    Recursive back tracking dynamic programming

    Fill in Box


    Recursive back tracking dynamic programming

    Fill in Box


    Recursive back tracking dynamic programming

    Order to Fill in Table

    Order to fill table:

    • so that nobody waits

      This guy waits for


    Recursive back tracking dynamic programming

    Order to Fill in Table

    (later)


    Recursive back tracking dynamic programming

    Base Cases

    Base Cases:

    • general algorithm

      does not work

    • This guy’s friends are


    Recursive back tracking dynamic programming

    Base Cases

    Base Cases:

    • general algorithm

      does not work


    Recursive back tracking dynamic programming

    Base Cases


    Recursive back tracking dynamic programming

    With Advice


    Recursive back tracking dynamic programming

    With Advice

    Done


    Knapsack problem

    Knapsack Problem

    Get as much value

    as you can

    into the knapsack


    Knapsack problem1

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


    Greedy algorithm

    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


    Greedy algorithm1

    Most dense in value

    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


    Greedy algorithm2

    Most dense in value

    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


    Bird friend algorithm16

    I ask the bird:

    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

    # of answers K =


    Bird friend algorithm17

    I ask the bird:

    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

    # of answers K =


    Bird friend algorithm18

    I ask my friend:

    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?


    Bird friend algorithm19

    His solution:

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


    Bird friend algorithm20

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

    I ask my friend:

    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?


    Bird friend algorithm22

    His solution:

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


    Recursive back tracking dynamic programming

    Same as Brute Force Algorithm

    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.


    Recursive back tracking dynamic programming

    Memoization

    “Which is the best path from vito t?”

    i

    Assign one friend to each sub-instance.


    Recursive back tracking dynamic programming

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


    Recursive back tracking dynamic programming

    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.


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    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.


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    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


    Recursive back tracking dynamic programming

    The Table

    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


    Recursive back tracking dynamic programming

    The Table

    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


    Recursive back tracking dynamic programming

    The Table

    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


    Recursive back tracking dynamic programming

    The Code


    Recursive back tracking dynamic programming

    Running Time

    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!


    Recursive back tracking dynamic programming

    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.


    Recursive back tracking dynamic programming

    The Knapsack Problem

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


    Recursive back tracking dynamic programming

    The Knapsack Problem

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


    Recursive back tracking dynamic programming

    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


    The job event scheduling problem

    The Job/Event Scheduling Problem

    Schedule as

    many events

    in your room

    as possible


    Recursive back tracking dynamic programming

    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.


  • Greedy algorithm3

    Greedy Algorithm

    Greedy Criteria:

    Earliest Finishing Time

    Motivation:

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


    Recursive back tracking dynamic programming

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

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


  • Greedy algorithm4

    100

    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 algorithm23

    I ask the bird:

    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

    # of answers K =


    Bird friend algorithm24

    I ask the bird:

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

    # of answers K =

    2 Yes & No


    Bird friend algorithm25

    I ask the bird:

    “Do we keep the last event?”

    I ask my friend:

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

    Bird & Friend Algorithm

    An instance:

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

    She answers: No

    His solution:

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

    My solution:

    same

    My cost:

    same


    Bird friend algorithm26

    I ask the bird:

    “Do we keep the last event?”

    I ask my friend:

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

    Bird & Friend Algorithm

    An instance:

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

    Carefull

    She answers: Yes

    His solution:

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

    My solution:

    same + <sn,fn,wn>.

    same +wn

    My cost:


    Bird friend algorithm27

    Bird answers:

    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.

    I add to his subsolution the bird’s answer.


    Bird friend algorithm28

    Bird answers:

    “Yes keep the last event.”

    Bird & Friend Algorithm

    No we trust the bird!

    Then I should politely tell the bird she is wrong


    Bird friend algorithm29

    Bird answers:

    “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


    Bird friend algorithm30

    Bird answers:

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


    Bird friend algorithm31

    Bird answers:

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


    Bird friend algorithm32

    Bird answers:

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


    Bird friend algorithm33

    Bird answers:

    “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


    Bird friend algorithm34

    Bird answers:

    last event

    “Yes keep the last event.”

    Bird & Friend Algorithm

    My instance:

    Cant keep any events that overlap with it.


    Bird friend algorithm35

    Bird answers:

    last event

    “Yes keep the last event.”

    I ask my friend:

    Bird & Friend Algorithm

    My instance:


    Bird friend algorithm36

    Bird answers:

    “Yes keep the last event.”

    I ask my friend:

    Bird & Friend Algorithm

    His instance:

    His solution


    Bird friend algorithm37

    Bird answers:

    “Yes keep the last event.”

    I ask my friend:

    Bird & Friend Algorithm

    My instance:

    My solution:

    My solution:

    same + <sn,fn,wn>.

    Yes

    Valid?


    Bird friend algorithm38

    Bird answers:

    “Yes keep the last event.”

    I ask my friend:

    Bird & Friend Algorithm

    My instance:

    My solution:

    My solution:

    same + <sn,fn,wn>.

    same+wn

    My cost:


    Recursive back tracking dynamic programming

    Same as Brute Force Algorithm

    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.


    Recursive back tracking dynamic programming

    Memorization

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

    i

    Assign one friend to each sub-instance.


    Recursive back tracking dynamic programming

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


    Recursive back tracking dynamic programming

    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.


    Recursive back tracking dynamic programming

    Each sub-Instance is prefix.

    I.e. only n of them.

    Hence, running time is polynomial!


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    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

    asked of some friend, friend…

    Only nsub-Instances

    Good enough.


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    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.


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    Bird answers:

    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.


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    Bird answers:

    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.


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    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


    Recursive back tracking dynamic programming

    Set of Sub-Instances

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


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    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


    Parsing

    Parsing

    Input:

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

    Output:


    Parsing1

    Parsing

    Recursive Alg:

    • GetExp calls GetTerm

    • GetTerm calls GetFact

    • GetFact calls GetExp


    Recursive back tracking dynamic programming

    Parsing

    C CB

    AC

    c

    d

    B TA

    BC

    b

    e

    AAA

    BT

    a

    TAB

    CA

    TT

    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


    Recursive back tracking dynamic programming

    Parsing

    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

    = baeaadbda

    T a1a2a3 ..... an

    Output: A parsing


    Recursive back tracking dynamic programming

    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.


    Recursive back tracking dynamic programming

    Parsing

    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

    Ask Little Bird:

    • For first rule

      Ask Friend

    • Parse left

      Ask Another Friend

    • Parse right.

    b d a


    Recursive back tracking dynamic programming

    Parsing

    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

    Ask Little Bird:

    • For first rule

      Instance to give Friend

    • ?

    b d a


    Recursive back tracking dynamic programming

    Parsing

    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

    Ask Little Bird:

    • For first rule

      Want from Friend:

    • Left sub-parse tree.

      Instance to give him:

    • C baeaadb

    C


    Recursive back tracking dynamic programming

    Parsing

    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

    Ask Little Bird:

    • For first rule

      How can we know split?

    • Ask the Bird!

    b d a


    Recursive back tracking dynamic programming

    Parsing

    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

    Ask Little Bird:

    • 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


    Recursive back tracking dynamic programming

    Parsing

    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

    Ask left friend:

    • Instance: Cbaeaadb

    • Solution: Left parsing

    C


    Recursive back tracking dynamic programming

    Parsing

    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

    Ask right friend:

    • Instance: Abda

    • Solution: Right parsing

    b d a


    Recursive back tracking dynamic programming

    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

    Combine:

    • Instance:

    • Bird’s Answer

    • Left Friend’s Answer

    • Right Friend’s Answer


    Recursive back tracking dynamic programming

    Same as Brute Force Algorithm

    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.


    Recursive back tracking dynamic programming

    Memoization

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

    i

    Assign one friend to each sub-instance.


    Recursive back tracking dynamic programming

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


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    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


    Recursive back tracking dynamic programming

    Set of Sub-Instances

    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 =


    Recursive back tracking dynamic programming

    The Table

    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


    Recursive back tracking dynamic programming

    Running Time

    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


    Recursive back tracking dynamic programming

    Find a Satisfying Assignment

    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.


    Recursive back tracking dynamic programming

    Find a Satisfying Assignment

    Ask the little bird

    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.


    Recursive back tracking dynamic programming

    Find a Satisfying Assignment

    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


    Speeding up the time

    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,


    Recursive back tracking dynamic programming

    Memoization

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

     i

    Assign one friend to each sub-instance.


    Recursive back tracking dynamic programming

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


    Recursive back tracking dynamic programming

    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.


    Speeding up the time1

    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,


    Speeding up the time2

    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.


    Recursive back tracking dynamic programming

    Find a Satisfying Assignment

    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:


    Recursive back tracking dynamic programming

    Find a Satisfying Assignment

    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:


    Recursive back tracking dynamic programming

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

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


    Optimization problems1

    Optimization Problems

    • Don’t mix up the following

      • What is an instance

      • What are the objects in an instance

      • What is a solution

      • What are the objects in a solution

      • What is the cost of a solution

    • Greedy algorithm

      • What does the algorithm do & know

      • What does the Prover do & know

      • What does the Fairy God Mother do & know

    • Recursive Backtracking / Dynamic Programming

      • What does the algorithm do & know

      • What does the little bird do & know

      • What does the friend do & know


    Dynamic programming don ts

    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 programming don ts1

    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 programming don ts2

    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 programming don ts3

    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.


    The question for the little bird13

    • “Which key is at the root of the tree?”

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


    Recursive back tracking dynamic programming

    Matrix Multiplication


    All pairs shortest paths

    All Pairs Shortest Paths


    Recursive back tracking dynamic programming

    end


    Recursive back tracking dynamic programming

    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


  • Login