In problems for which dynamic programming solutions are considered, there is a concept of a state. A state is, in general, a point in a -dimensional space, where is called the number of dimensions in the solution. This may sound quite formal, but in fact, each person who solved at least one problem using dynamic programming approach used this concept.
For example, in Longest Common Subsequence problem, also known as LCS, the goal is for given two strings and , to find the length of their longest common subsequence. This is a classical problem which can be solved efficiently using dynamic programming approach. The idea is to compute , which is the length of the longest common subsequence of and . The value of can be computed in this problem knowing the values of , and . Then the final answer is the value of . In this approach, an entry in table is called a state, and since the table is -dimensional, this dynamic programming solution is -dimensional.
The above problem is very classical, so the above approach is well known and it can be shown that it is optimal. In other words, in a general case the above solution with states is optimal. However, when a new problem has to be solved from the scratch, the common situation is that at first some correct dynamic programming approach is defined, just to check if the problem can be solved correctly with this method. After that, the next step is to try to reduce the time complexity of the solution if it is too slow.
The time complexity of a dynamic programming approach can be improved in many ways. The most common are to either use some kind of data structure like a segment tree to speed up the computation of a single state or trying to reduce the number of states needed to solve the problem. Describing the latter is the main goal of this article.
Reducing the number of states
The number of states in a dynamic programming solution can be reduced in a few ways. One possible way is to try to reduce the size of one of existing dimensions. When the size is reduced by a factor of , then the overall time complexity of the solution remains the same, but the constant factor is improved. The most significant improvement can be achieved by getting rid of a dimension completely or reducing its size to a small constant.
The actual method of reducing the state space is very problem-specific. There is no general method, which can be applied to all the problems. However, one general idea worth to mention is that the number of dimensions can be reduced if there are some dimensions capturing the same information, or if there is a dimension capturing irrelevant information. Studying more and more war stories is the best approach to learn how to improve existing dynamic programming solutions.
In the following section, an example problem will be given, for which the initial solution will be improved.
Problem:
For a given array of distinct integers and two integers and , the goal is to find the number of subsequences of with exactly local minimums and local maximums. For a subsequence of , element for is a local minimum if and only if . Similarly, is a local maximum if and only if .
Since subsequences can be build incrementally, dynamic programming approach to this problem sounds promising.
Initial solution
Let be the number of subsequences of ending in with exactly local minimums, exactly local maximums and the last element smaller than the one before last element taken to a subsequence.
Similarly, let be the number of subsequences of ending in with exactly local minimums, exactly local maximums and the last element greater than the one before last element taken to a subsequence.
With these values defined, any entry of table with the as the value of its first dimension can be computed using entries with values of first dimension smaller than .
Details of computing a single state
Let be the fixed value of the first dimension of a state. In order to update any value for such a state, a possible method is to iterate over all indices , and for a fixed , consider two possibilities: either or . Since all elements of are distinct, equality is not possible here.
If , a subsequence of length can be created using just and , so this subsequence can be added to . Similarly, if , such a subsequence can be added to .
Next step is to extend any previously counted subsequences of length at least to subsequences ending with . In order to do this, let be the fixed number of local minimums and be a fixed number of local maximums. Again, there are two cases to consider:
If , a value of can be added to . Moreover, if , a value of can be added to creating a new local minimum at index .
Analogously, if , a value of can be added to . Moreover, if , a value of can be added to creating a new local maximum at index .
In order to compute the final result, all values and over all have to be summed. At the end, if and , a value of have to be added to the result in order to count all subsequences of length .
The total time complexity of this method is , because entries are filled, and a single one is filed in time.
Improving the initial solution by reducing the state space
In the above solution, the state space can be reduced. The basic idea here is based on the fact that input values in array are distinct.
Let be any subsequence of and let be the difference between local minimums in and local maximums in it. The crucial observation here is that since all elements of are distinct, can be only equal to , or .
In order to prove that, let be a subsequence of with local minimum and local maximums. Let be the index which is the local minimum. The important fact is that is an increasing subsequence from index , so any other subsequence which have as a prefix will be either increasing from index till the end, or it will have two adjacent indexes and such that , so the value for never grows over . The same argument can be used to show why the value of is never less than .
This fact can be used to reduce the state space in the original solution, which leads to speed up the overall solution. After the improvement, non-constant size dimensions has been reduced from to . In other words, for each state with local minimums there are at most possible values of - the number of local maximums: the only possible ones are , and . This observation leads to a solution with total time complexity .
This war story shows how the idea of removing one of two dimensions capturing the same information can be used by a deep analysis of the problem.
It is worth mentioning that even the above faster solution can be improved further using a data structure like a segment tree in order to speed up computing a single state of the solution.
Không có nhận xét nào:
Đăng nhận xét