How to Solve This Dynamic Programming Problem with Two Subsequences?

AI Thread Summary
The discussion revolves around solving a dynamic programming (DP) problem involving two subsequences, X and Y, where the objective is to find a subsequence of X that minimizes the sum of absolute differences between its elements and the elements of Y. The condition is that the length of Y is less than or equal to that of X, and Y is sorted in non-decreasing order. A proposed approach involves defining a DP function B(i,j), which represents the minimum sum of absolute differences for the first i elements of X and the first j elements of Y. The conversation suggests that by constructing a table based on this definition, one can systematically fill in values to derive the optimal solution. An example problem is provided to illustrate the concept of finding a minimal subset, reinforcing the idea that solving this related problem could aid in tackling the original challenge. The emphasis is on developing the DP table correctly to achieve the desired complexity of O(mn).
ydan87
Messages
25
Reaction score
0
I'm having problem to solve this problem in DP:
Given 2 subsequences:

X = {x1, x2,..., xm} and Y = {y1, y2, ... , yn}

so `n <= m` and also `y1 <= y2 <= ... <= yn`.

The goal is to find a sub-sequence of `X` (xi1, xi2, ... , xin) such that:

sum(k is 1 to n) | xik - yk | is minimal

The DP algorithm should be `o(mn)`.

I though about that: define `B(i,j)` as the minimun sum so |Y| is j and |X| is I, but it seems I get stuck here.

Any help?
Thanks in advance
 
Technology news on Phys.org
I think you are on the right track. Define B(i,j) as follows:

B(i,j): minimum of sum(k from 1 to j) | xik - yk |,
where the first i elements of x and the first j elements of y are available.

---

So, basically you are searching for a "subset" of x. I recommend working on a related problem:
Consider the sequence A = [7, 2, 1, 3, 10]. Find a subset with 3 elements such that the sum of these elements is minimized. Let's call this the minimal subset of size 3.

Define B(i,j) as follows:
B(i,j): minimal subset of size j if the first i elements of A are available.

You would then write down a table:
Code:
    | j:    1    2    3
------------------------
i:
1   |      7         
2   |      2          
3   |      1
4   |      1
5   |      1

The first column is easy to fill out. Can you fill the rest?

I'm sure that if you can solve this problem you can also find a solution to your problem.
 
That's great! Thanks :)
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top