"Design and Algorithm Analysis" course, teaching work of the gift, is "Introduction to the Algorithm" (Sanjoy Dasgupta Waiting, Tsinghua University Press. 2008 7 The first month of 1 versions).
Article 7 week, the main teaching material article 6 chapter dynamic planning discussion. Dynamic programming (referred to as DP). is a general problem solving method, which is mainly used in the optimization of operational research. Because of its high demand for thinking ability. Very affected by the major companies written tests, interviews welcomed. The students are learning at the beginning of their studies. Be able to step up, first understand the problems in the textbook and summarize, and then programming to achieve 2-3 questions. Finally go online to find 3-10 written tests, interview problem solving to open up ideas.
One: Job contentAt least one of the following assignments has been completed. Later engaged in IT industry classmate, fight for all finished.
(1) knapsack problem . For the knapsack problem mentioned above, table 1 (data file download Knapsack.txt, first behavior Backpack weight 15, number of items 5; 第2-6 line. The weight and value of 第1-5件 items, respectively), w=15. The program calculates the number, total weight and total value of the items in the backpack. Requires that the constructed two-dimensional table be output to the file KnapsackResult.txt. Post title: 7th Week homework problem .
(2) editing distance .
To the data file edit.txt (download please click: edit.txt), the first line is the need to calculate the word pair, 第2-4 line. For two words, consider the minimum number of operands (that is, edit distance) required to transform the first word into a second word. Please output the editing distance for each group of words. and try to output it in its way (like the textbook P177 the most downward view,-the number represents a void).
Post title : 7th Week job 2--editing distance .
(3) the longest increment of the subsequence . See the blog The maximum increment subsequence specific explanation (longest increasing subsequence) to explain this problem, first programmed to output 10 elements of a set of random integer sequence, and then use the dynamic programming algorithm to find its longest increment subsequence length and specific sub-sequences. Please test and verify the algorithm you have written many times. Post title : 7th Week job 3--the longest increment subsequence .
Second: Job Requirements
1. Please fetion the class to inform the students to finish the homework.
2. The work is counted in the usual results. Scoring is based on the level of completion for everyone-attitude (done/not done). The teacher will choose a number of students according to the quality of their work, and provide a basis for individualized teaching. Please provide the highest possible level of work according to your own abilities. To improve their ability to go all out.
3. This assignment. The teacher mainly check the class number is located in the 11-20 students and apply for exemption from the students, as well as spot checks some other students, please tell each other.
Three: Preview in advance
Preview the 7th Chapter Linear Planning and P205 (start).
Understand the problem of linear programming, network flow problem, two-part graph matching. The focus is on the simplex algorithm (p232-241).
Four: Recommended reading
Success is not accidental, and interest in programming or other aspects is also a process. Only when we understand some basics. can generate and excite interest. Here through a blog post, look at everyone's seniors Shinzanqui programming Road, and software major junior students Shanhong
Viewmode=contents "> The road of Interest.
V: Content of lectures (collation)
(1) the shortest path problem of a direction-free graph . Directed acyclic graph (abbreviation: DAG). This is the 3rd and 4th time we have been exposed to the application of a direction-free graph. First of all recall, what are the features of the DAG? What questions do we have about dags in the previous chapters? What have we done with the DAG? Warm and so know the new.
An important property of dags is that nodes can be arranged in a linear arrangement (topological ordering) and can use depth-first search (DFS). And mark the first visit (pre) and last access Time (post) for each node, and the last post in reverse order is the topology ordering of the DAG, in fact the node is in the DFS search. The reverse of the stack is the topological sort.
How do I calculate the shortest path for a dag? The 4th chapter (p135-136) calculates the distance of a node in the order in which it is sorted by the topology of the nodes in the graph. The calculation method can further describe the recursive formula as seen below.
So the distance from the source point S to Node D is computed. The formula can be solved by calculating the distance from S to B and S to C first. And so on, finally we can calculate S to S, S to C, s to a ... in turn. Calculate the distance of each node.
Algorithms such as the following:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzhl6mtk4mg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
Important: No matter what a dynamic programming problem, it can be equivalent to a directed acyclic graph Dag problem.
(2) two basic properties of dynamic programming .
(a) Optimal substructure properties: The solution of the sub-problems included in the optimal solution of the hypothetical problem is also optimal, and we call this problem to have the optimal substructure property (i.e. to satisfy the optimization principle). (b) Overlapping sub-problem properties: The overlapping nature of sub-problems refers to the problem is not always a new problem when the recursive algorithm is the top-down solution, and some sub-problems are repeatedly calculated. The dynamic programming algorithm takes advantage of the overlapping properties of such sub-problems. Calculate only once for each sub-problem. It then saves the results in a table, and when it is time to calculate the calculated sub-problems, simply look at the results in the table and get high efficiency.
(3) the longest increment of the subsequence . Enter a sequence of numbers in a similar order from which to pick a subset. So that the number in this subsequence satisfies the strict monotonically increasing relation. As below, the sequence of 2, 3, 6, 9 and 2, 3, 6, 7 and so on is the longest increment subsequence of the sequence.
How is it calculated? Using the nature of the dynamic programming method, we need to think about the structure of a overlapping sub-problem. Think: Suppose 2, 3, 6, 9 is a maximum increment subsequence. After removing the last number 9 and 7, 2, 3, 6 are also the longest increment subsequence of the sequence 5, 2, 8, 6, 3, 6 (Optimal substructure properties). So the longest increment subsequence that contains a number (such as 9) and ends at that number is calculated. You can consider numbers (such as 3, 6, 9, 5, 2, 8) that meet the strict increment relationship with 6 in the subsequence (5, 2, 8, 6, 3, 6) preceding the number. Assuming that the longest increment subsequence of 5, 2, 8, 6, 3, 6, and so on is calculated, the longest increment subsequence containing 9 is obtained. The operational relationship is shown below.
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzhl6mtk4mg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
To be Continued ...
(4) editing distance .
Find the similarity of two words and apply them in a number of situations such as spell checking. We have previously added the use of cosine theorem and tf-idf to calculate document similarity (TF-IDF and cosine similarity Application (a): self-extracting key words, TF-IDF and cosine similarity (ii): To find similar articles, today we learn a way to define distance.
Edit Distance: Transforms a string to the minimum edit operation required for a string--contains the number of insertions, deletions, and character substitutions (textbook P178).
For example, in two words snowy and sunny. The first method is 3, and the other is 5, but since the editing distance refers to the smallest edit operation, the two words have an editing distance of 3.
To change the word above into the following words, you only need to insert U after s. Replace o with N, delete W, altogether three operations (and of course other ways).
How to solve the problem of editing distance with dynamic programming method, we need to define the optimal substructure first. X[1 the word M] and y[1 N] is divided into two parts: the prefix part and the remainder part. such as the prefix (Prefix) section x[1 I] and y[1 J]. When you define the sub-problem E (i,j) of the editing distance, the original problem is calculation e (m,n). such as X[1 I] and y[1 J]. There are three ways to align them (for example, textbook P179).
such as two words exponential with polynomial, its E (7,5) For example to be seen. Now we are able to write a recursive form. E (i,j) =min{1+e (i 1,j), 1+e (I,j 1), diff (I,j) +e (i-1,j? 1)}, looking for the minimum editing distance under three alignment methods, and successfully converted E (I,J) to Solution e (i-1,j). E (i,j 1). E (i 1,j 1) A little bit of a problem.
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzhl6mtk4mg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
With this in mind, we are able to compile detailed program code according to the algorithm pseudo-code. Consider Figure 6-4 (textbook P180). After the calculation gets the edited distance e (m,n). How do we get the alignment of two strings? How do I add a minimal code implementation?
(5) knapsack problem .
There are classic backpack questions on the web, and further learning on this basis (knapsack problem nine-note _01 backpack). Research and summary of very many people, such as knapsack problem pen questions, we need to master the single copy of the Knapsack problem (textbook P185).
Single copy of the knapsack problem (knapsack without repetition): n items, respectively W1,..., WN, the value of V1,..., vn, the existing one backpack maximum capacity of W, how the combination can make the backpack loaded with the most valuable items?
Excerpt from: Knapsack-problem (Http://www.answers.com/topic/knapsack-problem)
If we numbered the items. The problem is converted to w=15, and the item number, weight, and value are seen in the table below. To do this, define the sub-problem so that it includes information about which item has been used. Two parameters W, J are introduced, such as the following
K (W,J) = based on backpack capacity W and item 1, .... The highest value that J can get. Among 0≤j≤n
Table 1: Knapsack Problem Item Value
Items |
Weight |
Value |
1 |
4 |
10 |
2 |
12 |
4 |
3 |
1 |
2 |
4 |
2 |
2 |
5 |
1 |
1 |
Our goal is to calculate K (w,n). How can K (w,j) be represented by smaller sub-problems? Very easy: Either you need to select item J for maximum value, or you do not need:
The first case is only when WJ≤W is established, in other words. K (W,J) can be expressed with sub-problem K (,j 1). So. The algorithm needs to fill in a two-dimensional table. Co-owned W+1 row n+1 column, algorithm complexity is O (NW), algorithm pseudo code such as the following. Achieve it.
(6) matrix multiplication . Everyone according to the textbook p186-188, their own understanding can be. Books are just read, think, and practise more. will have a deeper understanding.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Geake 1111-1114 Week Seven lecture classes, homework (dynamic planning, Duration: April 25, 2014 at 23 points-Friday evening, CST fetion inform students)