1

I am trying to go over some notes and examples of dynamic programming, and I have having some difficulty figuring out how it all works. I will post the question, and then what I am having difficulty with:

Given a sequence of points p1= (x1,y1),...,pn=(xn,yn) sorted from left to right (ie, x1 < x2 < ... < xn) and a number k between 1 and n, find a polygonal chain from p1 to pn with k edges that goes from left to right, minimizing the sum of the vertical distances of the points to the chain. Design dynamic programming algorithm that solves the problem in O(n^3) time. Set the subproblems, give all base cases necessary, calculate recursive formula, and write pseudocode for the algorithm. Also a function f(a,b) is defined for us to use in calculating the vertical difference, so I dont have to worry about implementing that. I can just use it as f(a,b)

I believe that the subproblems should be divided as such:

C[i,j] = polygonal chain from p1 to pi with j edges, minimizing the sum of vertical distances.

And then the answer would be: C[n,k]

Base case: C[i,0] = 0

And now I am having some difficulty coming up with the recursive formula. My first question, have I broken the subproblems up correctly? The question gives a hint that makes it seem like I did, but I am not 100% sure. If I am, any hints on how to proceed with deriving the recursive formula?

Thanks for any help guys.

1 Answer 1

1

Your subproblems are correct, but I think a little change to the formulation can help you come up with the formula:

Instead of having C(i, j) mean any chain from 1 to i with j edges, make it mean specifically "A chain that ends in i". Then, to determine the answer for C(i, j) you just have to try all the possibilities for where the last edge started.

Then the answer can be the optimal of all C(i, k).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.