greedy algorithms notes of Date Structure and Algorithms Analysis in C

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值