I participated in the ACM competition a few days ago. Haha, right when I was studying with the prince, I had a poor score, but it was all over. The competition can be absent, and blog posts cannot go on. Haha, today I sum up my greedy Recent exercises.Algorithm. I writeArticleThe sequence is to write the dynamic planning, backtracking algorithm, and branch Limit algorithm first, and then prepare to write the greedy algorithm and the basic recursive and grouping algorithms. The most common computer algorithms are just like this, if you are really interested in algorithms, you can study more about algorithms. In fact, writing algorithms gives people a greater sense of accomplishment Than Doing projects. Our current projects do not actually need special algorithms, most of them are reading sdks and other documents, which can be basically solved by using design patterns and object-oriented methods. Of course, I have also met a project that has very high mathematical requirements. I designed Gaussian equations and differential points, but I was not involved in this project at the time. Haha. To put it bluntly, I am reluctant to do such a project, and I don't have much confidence in it.
Greedy algorithms are relatively simple in several basic algorithms, and the idea is very simple. Each step is always the best choice for the moment. That is to say, the greedy algorithm does not take the overall optimization into consideration. All it makes is a local optimal choice in a certain sense. The basic idea is to gradually approach the given goal from an initial solution of the problem and obtain a better solution as quickly as possible. When a certain step in an algorithm is reached, the algorithm stops.
For this simple algorithm, let's take a look at its advantages and disadvantages. Of course, it is simple, time-saving, and easy to use. Let's take a look at some problems in the greedy method.
Problems with the greedy method:
1. There is no guarantee that the final solution obtained is optimal;
2. cannot be used to find the maximum or limit the problem;
3. only feasible solutions that meet certain constraints can be obtained.
The above problems can be solved in the dynamic planning, backtracking algorithm, and branch Limit algorithm, but we will not give up on the use of greedy algorithms. This is exactly what agile development advocates. There will never be the best, but only the most suitable one. The reason we choose greedy algorithms is that they can meet current needs and are simpler than other algorithms.
See the example below:
There are n items, the weight of each item is wi, the price is: PI, there is a backpack, the maximum weight of M can be loaded. where (0 <= I <n, 0 <wi <m ).
Q: How can I install a product that has the highest value in the package? (for each product, only one part of the product can be loaded)
PseudoCode:
The parameters are N: number of items, respectively. M: maximum weight of a backpack. V []: Value array. W [] weight array.
Void knapsack (int n, float M, float V [], float W [], float X [])
{
Sort (n, V, W); // sort
Int I;
For (I = 1; I <= N; I ++)
X [I] = 0;
Float c = m; // C is the space left by the backpack
For (I = 1; I <= N; I ++ ){
If (W [I]> C) break;
X [I] = 1; // select the I-th item
C-= W [I]; // The remaining weight is reduced.
}
If (I <= N) // when the capacity of the backpack is insufficient to store the entire item, save part of it
X [I] = C/W [I];
}
The Code is as follows:
View code
# Include < Stdio. h >
// Parameter: N indicates the type of items that a backpack can store.
// Parameter: the pointer P points to an array that stores the value of an item.
// Parameter: the array pointed to by the pointer Q is the array storing the item weight.
Static Void Sort ( Int N, Float * P, Float * Q)
{
Int I;
Int J;
For (I = 0 ; I < N - 1 ; I ++ )
For (J = I + 1 ; J < N; j ++ )
If (( * (P + I )) / ( * (Q + I )) < ( * (P + J )) / ( * (Q + J )))
{
Float F;
F = * (P + I );
* (P + I) = * (P + J );
* (P + J) = F;
F = * (Q + I );
* (Q + I) = * (Q + J );
* (Q + J) = F;
}
}
// Parameter: the array pointed to by pointer X is used to store items.
// Parameter: M indicates the size of the backpack.
// Parameter: N indicates the type of items that a backpack can store.
Static Void Knapsack ( Int N, Float M, Float * V, Float * W, Float * X)
{
Sort (n, V, W );
Int I;
For (I = 0 ; I < N; I ++ )
{
If ( * (W + I) > M)
Break ;
// When this item can be stored, set 1
* (X + I) = 1 ;
// When placed, the capacity of the backpack is reduced
M -= * (W + I );
}
// When the capacity of the backpack is insufficient to store the entire item, store part of it.
If (I < N)
* (X + I) = M / ( * (W + I ));
}
Int Main ()
{
Int N = 6 ; // Item type
Int M = 100 ; // Backpack capacity
Float W1 [ 6 ] = { 15 , 5 , 60 , 25 , 55 , 80 }; // Weight of various items
Float V1 [ 6 ] = { 20 , 30 , 30 , 10 , 55 , 40 }; // Value of various items
Float X1 [ 6 ]; // Storage of various items
Float * X;
Float * W;
Float * V;
W = W1;
V = V1;
X = X1;
Int I;
For (I = 0 ; I < N; I ++ )
* (X + I) = 0 ;
Knapsack (n, m, V1, W1, X );
Printf ( " \ N =========== output item capacity array content ================================= \ n " );
For (I = 0 ; I < N; I ++ )
Printf ( " %. 1f \ t " , * (W + I ));
Printf ( " \ N ============= content of the output item value array ================================ \ n " );
For (I = 0 ; I < N; I ++ )
Printf ( " %. 1f \ t " , * (V + I ));
Printf ( " \ N ============= array of output item storage ============================= \ n " );
For (I = 0 ; I < N; I ++ )
Printf ( " %. 1f \ t " , * (X + I ));
Printf ( " \ N =============== end =========================================\ n " );
Return 0 ;
}
The main computing time of the algorithm knapsack is to sort various items by their unit of weight value from large to small. If you do not care about the sorting time, the complexity of the greedy algorithm is only O (n). The preceding example uses the O (N * n) sorting algorithm, this is just for simplicity. In fact, we can select an appropriate Sorting Algorithm to reduce the complexity of the sorting part to O (nlgn ).. such as heap sorting and quick sorting.
I have not recorded a complete example here because this algorithm is too easy to understand. Greedy algorithms are generally sorted before Policy Selection and optimized after sorting, this is the same as finding money in our life. If I need to pay 87 yuan to the customer, what will I do? First, we know the sorting of 10> 5> 1, then, in our experience, we will issue 8 pieces of 10 pieces (the principle of maximizing greed), 5 pieces of data, and two pieces of data. of course, you can also say that I only give five or one piece for one piece or not, but that is not the easiest way to do it. Be careful, algorithms are everywhere in life.
Algorithm series directory:
1. Summary of the algorithm series: grouping Algorithm
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