The greedy algorithm (greedy algorithm) means that at each step, we make the best choice at the time, that is, the local optimal choice, which is expected to guide the global optimal solution. So not all problems can get the global optimal solution.
Typical examples such as fractional knapsack problem: Backpack capacity of 50kg, there are three products are 60 yuan/10kg, 100 yuan/20kg, 120 yuan/30kg, merchandise can be divided, how to choose goods to make the backpack value the most. Here use greedy algorithm, should first select the unit weight value biggest commodity, three kinds of goods unit weight value is 6, 5, 4 respectively, so choose 10kg, 20kg and 20kg, the total value is 240 yuan. However, for the 0-1 knapsack problem-the product can not be divided, the best solution at this time is the commodity 20kg, the product three 30kg total value of 220 yuan instead of the original unit weight of the highest value of the commodity one. Since the extra space of the backpack will change the value of the unit weight of the commodity, it cannot be solved by greedy algorithm, and should be changed to dynamic programming.
The key point of greedy algorithm is to prove that the optimal solution of the local optimal solution and the remaining sub-problem can be obtained by the global optimal solution of the original problem.
Activity selection Issues
The following table is the start time for multiple activities Si with the end time fi, which can only have one activity at a time, to find a non-conflicting set of maximum compatible activities.
|
I |
1 |
2
|
3 |
4 |
5 |
6 |
8
|
9 |
|
One |
|
Si |
1 |
3
|
0 |
5 |
3 |
5 |
8
|
8 |
2 |
page |
|
Fi |
4 |
5 |
6 |
7 |
9 |
9 |
10 |
11 |
12 |
14 |
16 |
For this example {A1,A4,A8,A11} and {A2,A4,A9,A11} are the maximum compatible subset of activities.
If we use the greedy algorithm, we can think of the strategy is to choose the end of the activity as long as possible, leaving the remaining activities a larger range of choices.
The idea is set up by a premise: any non-empty problem SK, so am is SK in the end of the earliest activity, then am in the SK One of the largest compatible active sub-sets .
prove AK is a subset of the largest compatible activity for SK, and AJ is the oldest activity in the AK end time. If aj=am, it has been shown that AM is in one of the largest compatible active sub-sets of SK. If aj≠am, make the set AK ' =ak-{aj}∪{am}, and the AJ in AK will be replaced with AM. AK's activities are disjoint, AK activities are not disjoint, AJ is the AK in the end of the earliest activity, and fm≤fi. Since 丨 ak ' 丨 = 丨 ak 丨, it is concluded that AK is also a subset of the largest compatible activity of SK and contains AM.
So the greedy algorithm is established. On the code implementation, recursion can be used: for the original problem, select the first end of the task and the remainder of the maximum compatible subset. The latter can continue to be recursively handed down.
1Resursiveactivityselector (s,f,k,n) {//the initial invocation parameter is (s,f,0,n)2m=k+1;3 for(m=k+1; m<=n && s[m]<f[k];m++);//finding the earliest end of a task with an end time greater than f[k]4 if(m<=N)5 return{a[m]}∪resursiveactivityselector (s,f,k,n);6 Else7 returnNULL;8}
This is a typical tail recursion, or you can use an iterative algorithm, just to keep looking for the first end of the current remaining activity to join the result set.
Greddyactivityselector (s,f) { n=s.length; A={a[1]}; K=1; for (m=2; m<=n;m++) { if(s[m]>=f[k]) { a.add (a[m]); K=m; } } return A;}
Huffman code
Huffman coding is to save space by encoding the characters of different occurrences of frequency into variable-length encodings, which are shorter in frequency. There is now only 6 different characters in a file, as shown in the following table
|
|
A |
B |
C |
D |
E |
F |
|
Frequency (thousands) |
45 |
13 |
12 |
16 |
9 |
5 |
|
Fixed length coding |
000 |
001 |
010 |
011 |
100 |
101 |
|
Variable length Coding |
0 |
101 |
100 |
111 |
1101 |
1100 |
You can see that the variable-length encoding actually requires a space of (45*1+13*3*12*3+16*3+9*4+5*4) *1000=224,000 bits, while the fixed-length encoding is 300,000 bits. The use of variable length encoding requires that no code word is a prefix for other code words, that is, all encodings can only correspond to one result.
Huffman coding is to construct the optimal prefix code by greedy algorithm:
All characters are first sorted by frequency in ascending order: F5 E9 C12 B13 d16 A45. Remove the smallest of the two add f:5+e:9=14, and re-insert back into the queue by size: 12 13 14 16 45. Then remove the minimum two additions and insert: 14 16 25 45. Followed by 25 30 45;45 55;100. For each of the two points taken out, they are used as the two nodes of the binary tree, their and the parent node, eventually forming a binary tree, and then on each fork labeled 0 and 1, the path is the variable length encoding of each node. Because Hoffman encodes each time to obtain two minimum values, it can be implemented by a small heap sort.
An introduction to algorithms--greedy algorithm