How to Solve This Dynamic Programming Problem with Two Subsequences?

In summary, the problem at hand involves finding a sub-sequence of X with minimal sum, given two subsequences X and Y. The DP algorithm for this problem should run in O(mn) time, and it involves defining B(i,j) as the minimum sum of elements in a subset of X of size j, with i elements of Y also available. The suggested approach involves solving a related problem with a similar table, and then using that solution to find a solution for the original problem.
  • #1
ydan87
25
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
  • #2
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.
 
  • #3
That's great! Thanks :)
 

1. What is a dynamic programming problem?

A dynamic programming problem is a method of solving complex problems by breaking them down into smaller, simpler subproblems and storing the solutions to these subproblems. This allows for more efficient computation and avoids repeating calculations.

2. How is dynamic programming different from other problem-solving techniques?

Dynamic programming differs from other techniques, such as brute force or divide and conquer, because it uses a bottom-up approach where solutions to smaller subproblems are used to build up to the solution of the larger problem. This results in improved time complexity and avoids redundant calculations.

3. What are the key elements of a dynamic programming problem?

The key elements of a dynamic programming problem are:
1. Overlapping subproblems: The problem can be broken down into smaller subproblems that are repeated multiple times.
2. Optimal substructure: The optimal solution to the problem can be constructed from the optimal solutions of its subproblems.
3. Memoization: Storing the solutions to subproblems to avoid repeated calculations.
4. Bottom-up approach: Solving subproblems before solving the larger problem.

4. What types of problems can be solved using dynamic programming?

Dynamic programming can be applied to a wide range of problems, including optimization, sequence alignment, shortest path, and knapsack problems. It is particularly useful for problems that can be broken down into smaller subproblems and have optimal substructure.

5. What are some common algorithms used in dynamic programming?

Some common algorithms used in dynamic programming include:
1. Fibonacci sequence
2. Knapsack problem
3. Longest common subsequence
4. Shortest path algorithms (Dijkstra's, Bellman-Ford, Floyd-Warshall)
5. Edit distance
6. Coin change
7. Rod cutting
8. Matrix chain multiplication.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
2
Views
902
  • Programming and Computer Science
Replies
17
Views
969
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
  • Precalculus Mathematics Homework Help
Replies
7
Views
883
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
956
Replies
2
Views
306
Back
Top