Summary of basic algorithm series: Dynamic Planning (solving outsourcing cost issues)

Source: Internet
Author: User

Theoretical support:

Dynamic PlanningAlgorithmIt is usually used to solve a problem with a certain optimum nature. In such problems, there may be many feasible solutions. Each solution corresponds to a value. We hope to find a solution with the optimal value. The dynamic planning algorithm is similar to the divide and conquer method. Its basic idea is to break down the problem to be solved into several subproblems, first solve the subproblems, and then obtain the original solution from the subproblems. Different from the Division and control method, it is suitable for Dynamic Planning and solving problems. After decomposition, subproblems are often not independent of each other. If you use the divide and conquer method to solve this type of problem, the number of subproblems obtained by the decomposition is too large, and some subproblems are repeatedly calculated many times. If we can save the answers to the resolved sub-questions and find the answers we have obtained when necessary, we can avoid a large number of repeated computations and save time. We can use a table to record the answers to all resolved subquestions. Whether or not this subproblem is used in the future, as long as it has been computed, the results will be filled in the table. This is the basic idea of dynamic planning. The specific dynamic planning algorithms are diverse, but they have the same table filling format.

I copied the above definition text, but I still don't have the ability to summarize it. Well, if you are not very familiar with dynamic planning, you may see that the above definition text will go to the cloud, my general learning method is to first scan the basic definition, without looking into it (a bit unappealing), then look at some examples, combine your own experiences, and finally review the definition, so that I can really understand the definition. The following describes the idea of the above text based on a classic algorithm problem. The 0-1 knapsack problem is a required course in algorithm learning, this example is generally used when talking about dynamic planning.

Problem description:

A traveler has a backpack that can use up to M kilograms and now has n items,

Their weights are W1, W2,..., Wn,

Their values are P1, P2,..., Pn.

If each item has only one item without exceeding M kg, ask the traveler to obtain the maximum total value.

Input Format:

M, n

W1, p1

W2, p2

......

Problem Analysis:

After a rough definition of dynamic planning, we learned that the problem of dynamic planning is basically solved by creating a table and entering a table, which is no exception here.

First, we need to determine the logical relationship between cells in the table.

This is the most basic problem with a backpack. It features that each item has only one item, and you can choose to put it or not.

Define the state with a sub-problem: that is, F [I] [J] indicates the maximum value that a backpack with a size of J can obtain when I items are placed in the front. The state transition equation is:

F [I] [J] = max {f [I-1] [J], F [I-1] [J-W [I] + P [I]}

Why does Max appear here? Because it can only come from two States, that is, getting and not getting the current item. I obtained the maximum value of J in the previous item because I did not obtain the item I. For the sub-Problem of putting the first I item into a backpack with a capacity of V, if you only consider the policy of the first I item (put or not put ), then it can be converted into a problem that only involves the previous I-1 items. If I items are not put, then the problem is converted to "the former I-1 items into the capacity of the backpack", the value is f [I-1] [J]; if I items are placed, the problem is converted to "the previous I-1 items are placed in the backpack with the remaining capacity J-W [I ", the greatest value that can be obtained at this time is f [I-1] [J-W [I] plus the value p [I] obtained by placing the I item.

Next we will go through the algorithm flow through a set of real data:

Test data:
10, 3
3, 4
4, 5
5, 6

In the figure above, F [I] [J] = max {f [I-1] [J], f [I-1] [J-W [I] + P [I]} This formula is presented to the fullest .. dynamic Planning Issues are basically solved by creating tables and entering tables.

SolutionCode:

The Code is as follows (in C language to allow more people to read the code ):

 

View code

# Include < Stdio. h >
Int C [ 10 ] [ 100 ];
Int Knapsack ( Int M, Int N)
{
  Int I, j, W [ 10 ], P [ 10 ];
  For (I = 1 ; I < N + 1 ; I ++ )
Scanf ( " \ N % d, % d " , & W [I], & P [I]);
  For (I = 0 ; I < 10 ; I ++ )
For (J = 0 ; J < 100 ; J ++ )
C [I] [J] = 0 ;
  For (I = 1 ; I < N + 1 ; I ++ )
For (J = 1 ; J < M + 1 ; J ++ )
{
If (W [I] <= J)
{
If (P [I] + C [I - 1 ] [J - W [I] > C [I - 1 ] [J])
C [I] [J] = P [I] + C [I - 1 ] [J - W [I];
Else
C [I] [J] = C [I - 1 ] [J];
}
Else C [I] [J] = C [I - 1 ] [J];
}
  Return (C [N] [m]);

}
Int Main ()
{
Int M, N; Int I, J;
Scanf ( " % D, % d " , & M, & N );
Printf ( " Input each one: \ n " );
Printf ( " % D " , Knapsack (m, n ));
Printf ( " \ N " );
For (I = 0 ; I < N + 1 ; I ++ )
For (J = 0 ; J < M + 1 ; J ++ )
{
Printf ( " % 2D " , C [I] [J]);
If (J = M) printf ( " \ N " );
}
}

Algorithm practice:

 

Problem description:

Company X where zealot Yin is located needs at least W outsourcing staff from other companies. Now N companies provide options for Company X.

P_ I indicates the number of outsourced personnel units. For example, five persons are the number of one unit. If the company's plan is selected, the number of persons in the whole unit must be used, for example, five persons are the number of one unit, company X can only use N * 5 persons (n =, 2 ,....). C_ I represents the total salary provided by p_ I employees, and the Unit is yuan.

Input Format:

N W

P_ I C_ I

Excuse me, what kind of solution can I use to meet the requirements of Company X to recruit at least W outsourcing personnel for the minimum expenses.

For example, Company X needs at least 15 outsourced employees. Company A's price scheme is 20 thousand yuan/3 people, and Company B's price scheme is 30 thousand yuan/5 people.

Input scheme:

2 15
3 2
5 3

The minimum overhead is 90 thousand yuan.

Analyze the problem:

This is a problem of finding the optimal solution. It is similar to the problem of the backpack above. In this case, I will first think of a dynamic planning method to solve the problem. Similarly, we create a table here to solve the problem. Here we use an array to simulate the table, because every time we enter a company's solution, we will calculate the current optimal solution, therefore, we only need a simple one-dimensional array to construct the data structure for table creation. At the same time, the array DP [I] is also the best solution to recruit at least one person. Just like the input data example above, we not only found the solution for recruiting 15 people, but all the optimal solutions for less than 15 people have been found. That is to say, when we input 3 2, the final value of the DP array is as follows, in which the peach indicates that it cannot be resolved under the current situation, A [3] can be considered to be the optimal solution for recruiting 3 people. Of course, a [4] is unsolvable in the current situation, which is better understood.

After entering 5 3, the following figure shows the DP array:

Solution code:

 

View code

# Include < Stdio. h >
# Define Maxcompute 1000000000

IntDP [55005];

IntMin (IntX,IntY ){ReturnX<Y?X: Y ;}

Int Main ()
{
Int N, h;
Int I, J;
Int P, C;
While (Scanf ( " % D " , & N, & H) = 2 )
{
DP [ 0 ] =   0 ;
For (I =   1 ; I <= H; I ++ )
{
DP [I] = Max;
}

For (I =   0 ; I < N; I ++ )
{
Scanf ( " % D " , & P, & C );
For (J =   0 ; J <= H; j ++ )
{
If (J + P > H) DP [H] = Min (DP [H], DP [J] + C );
Else DP [J + P] = Min (DP [J + P], DP [J] + C );

Char Disstr = " X " ;
Printf ( " \ N " );
For ( Int X = 0 ; X <= H; x ++ )
{ If (DP [x] = 1000000000 )
Printf ( " % 2C " , Disstr );
Else
Printf ( " % 2D " , DP [x]);
}
}
Printf ( " \ N " );
}


Printf ( " The min value is % d \ n " , DP [H]);
}

Return   0 ;
}

 

Summary of algorithm ideas:

The basic idea of a dynamic planning algorithm is to break down the problem to be solved into several correlated subproblems, first solving subproblems, then, we can obtain the solution of the original problem from the solution of these subproblems. For repeated subproblems, we can solve them only when we first encounter them, and save the answers, you do not have to solve the problem again. The dynamic planning algorithm regards the solution to the problem as the result of a series of decisions. Unlike greedy algorithms, each greedy algorithm uses a greedy criterion to make an irrevocable decision; in the dynamic planning algorithm, we also need to check whether each optimal decision sequence contains an optimal decision subsequence, that is, whether the problem has the optimal sub-structure.

 

Algorithm series directory:

1. recursion and sub-Governance

2. Summary of the algorithm series: greedy algorithm

3. Summary of the algorithm series: Dynamic Planning (solving the outsourcing cost problem of the company)

4. Summary of the algorithm series: backtracking algorithm (solving the Fire Network problem)

5. Summary of algorithm series: branch Limit algorithm

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.