greedy algorithms:
every step has the best possible solution.
greedy algorithm works only if local optium is equai to the global optimum.
Greedy algorithm does not guarantee optimal solutions.Sometimes it produces suboptimal options. If the absolute best answer is not required, then the simply greedy algorithms are OK.(It's more easy.)
#Huffman Codes - for file compression
##problem describing
Generally, we need [logN] bits to represent N characters.(for example, we need 00,01,10,11 to represent a,b,c,d)
ASCII character - 8 bits for 128 characters.
Strategy to save space:allow the code length to vary from charcater to character and to ensure the frequently occuring characters have short codes.
(If all the characters occur with the same frequency,then there re no any space to save.)
>>> trie (字典树)
此处上图
using a binary tree - each character is its leaf.
0 - left branch; 1 - right branch;
If chaacter ci is at depth di, and occurs fi times, then the cost of the code is equal to Σdi*fi
improve → placing the "newline" a level higher at its parent.
(此处上图)
all nodes must be leaves or have two child(full tree), otherwise is can be moved up to its parent.
So the main problem is to find the tree with minimal cost(as defined above),
##solving algorithm
1. Maintain a forest of single element trees to represent every character.
2.Choose the two trees with smallest occuring frequency, merge them.
3. Repeat step until one tree left.
##How to prove it is the optimal soluton?
1. It's full tree.
2. the least frequent elements must be the two deepest nodes
notes:
1. Any two elements at the same depth can be swapped.
2. The encoding information must be tramsmitted at the start of the compressed file in order to decode.
3. For small files, the cost of transmitting the table to Huffman codes will override any possible saving in compression. For large files, it's better.
4. It's two-pass algorithms(it reads its inout twice, one for getting the frequency date and the other for encoding). It's not good for large files.
# A simply scheduing problem
>> minimize the mean completion time
## One processor
shorest job first.
## Multiprocessor
start jobs in order, cycling through processors.(placing job(i+1) on a different processor from jobi)
>> minimize the final completion time
>>>>>>>> It's NP-cpmplete
## Flow shop scheduing
# bin packing problem
## on-line
### Next fit <2M ### First fit <1.7M ### Best fit <1.7M (choose the tightest)
## off-line
sort the items first and place the largest first>>> first fit decreasing // best fit decreasing