Introduction to algorithms notes--15th dynamic programming

Source: Internet
Author: User

Usually used to solve the optimization problem. When you make each selection, you usually generate sub-problems that are the same as the original problem form. Dynamic planning techniques are often very effective when more than one subset of choices produce the same sub-problem. The key technique is to save its solution for each of these sub-problems, and to avoid repetition when it recurs.

  Divide and conquer : Sub-problems divided into disjoint, recursion to solve sub-problems, and then combine their solutions.

  Dynamic Programming (dynamical programming, tabular rather than programmatic) is used in cases where sub-problems overlap.

Four steps to design a dynamic programming algorithm:

1 characterizing the structural characteristics of an optimal solution

2 recursively define the value of the optimal solution

3 Calculate the value of the optimal solution, usually using a bottom-up approach

4 using the computed information to construct an optimal solution

Optimal substructure : The optimal solution of the problem is composed of the optimal solution of the related sub-problem, and these sub-problems can be solved independently.

Implementation Method :

Top-down Method with Memo (Top-down with Memoization)

Bottom-up approach (Bottom-up method) with a smaller complexity factor.

15.3 Dynamic Programming Principles

Suitable for the optimization problem solved by the dynamic programming method, two elements should be possessed: optimal substructure and sub-problem overlap .

Common patterns for discovering optimal sub-structures :

Make a choice. It produces one or more sub-problems to be solved.

In the possible first step selection, assume that you already know which option will get the best solution

Determine which sub-problems will arise from this selection and how best to characterize sub-problem spaces

The solution of each sub-problem is its own optimal solution.

A good experience in characterizing sub-problem spaces: Keeping sub-problem spaces as simple as possible and expanding them only when necessary.

For different problem areas, the difference of the optimal substructure is embodied in two aspects: the total number of sub-problems to be used in the optimal solution and the number of choices to be explored when determining the optimal choice.

The product is used to roughly analyze the run time.

The child problem must be irrelevant (the longest simple path problem cannot use dynamic planning). If some resources are used to solve a sub-problem, and these resources are not available when solving other sub-problems, they cannot be dynamically planned.

If each sub-problem must be solved at least once: bottom-up

If some sub-problems of sub-problem space are completely not solved: top-down (recursive + memo)

15.4 Longest common sub-sequence (LCS)

A subsequence of a given sequence is the result of removing 0 or more elements from a given sequence. That is, it is not required to be continuous, so it is different from the substring, but similar to the * in fuzzy matching.

theorem 15.1 (optimal substructure of LCS) makes x=<x1,x2,..., xm>,y=<y1,y2,..., yn> to two sequences, z=<z1,z2,..., zk> to any LCS of X and Y.

1. If Xm=yn, then Zk=xm=yn and Zk-1 are an LCS of Xm-1 and Yn-1.

2. If xm! =yn, then zk! =XM means that Zk-1 is an LCS for Xm-1 and Y.

3. If xm! =yn, then zk! =yn means that Zk-1 is an LCS of X and Yn-1.

The length of the LCS can be found using an array of m*n or approximately Max (m,n) +o (1). For refactoring, it takes approximately m*n space to ensure that the m+n time is reconstructed.

15.5 Best binary search tree

Formal definition : given a sorted sequence of n different keywords K=<k1,k2,..., kn> (so k1<k2<...<kn), we want to construct a binary tree with these keywords. For each of the keywords Ki, there is a probability that pi represents its search probability. n keywords and n+1 non-existent pseudo-keywords all have a search probability. The search tree, the least expensive two-fork search, is called the optimal binary search tree.

is not necessarily the height of the shortest, the highest probability of the keyword does not necessarily appear in the binary search tree root node.

Step 1: Structure of the optimal binary search tree

Any subtree contains continuous keyword ki,...,kj, leaf node Di-1,...,dj (failed search).

Optimal substructure: If an optimal binary tree T has a subtree containing the keyword ki,...,kj t ', then T ' must be the optimal solution for sub-problems that contain the keyword ki,...,kj and pseudo-keyword DI-1,...,DJ.

Step 2: A recursive algorithm

Note the height of the subtree is increased by 1.

Step 3: Calculate the desired search cost for the optimal binary search tree

Introduction to algorithms notes--15th dynamic programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.