参考资料:
http://blog.jobbole.com/83952/
http://www.iplaypy.com/code/c346.html
http://python.jobbole.com/81463/
http://blog.csdn.net/blog_empire/article/details/50250561
一、基本概念:
所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的仅是在某种意义上的局部最优解。
贪心算法没有固定的算法框架,算法设计的关键是贪心策略的选择。必须注意的是,贪心算法不是对所有问题都能得到整体最优解,选择的贪心策略必须具备无后效性,即某个状态以后的过程不会影响以前的状态,只与当前状态有关。
所以对所采用的贪心策略一定要仔细分析其是否满足无后效性。
二、贪心算法的基本思路:
1.建立数学模型来描述问题。
2.把求解的问题分成若干个子问题。
3.对每一子问题求解,得到子问题的局部最优解。
4.把子问题的解局部最优解合成原来解问题的一个解。
三、贪心算法适用的问题
贪心策略适用的前提是:局部最优策略能导致产生全局最优解。
实际上,贪心算法适用的情况很少。一般,对一个问题分析是否适用于贪心算法,可以先选择该问题下的几个实际数据进行分析,就可做出判断。
四、贪心算法的实现框架
从问题的某一初始解出发;
while (能朝给定总目标前进一步)
{
利用可行的决策,求出可行解的一个解元素;
}
由所有解元素组合成问题的一个可行解;
五、贪心策略的选择
因为用贪心算法只能通过解局部最优解的策略来达到全局最优解,因此,一定要注意判断问题是否适合采用贪心算法策略,找到的解是否一定是问题的最优解。
六、实例
1.背包问题
2.Huffman编码
import numpy as np
import math
# This class is the process of the haffman
class createHuffmanTree():
def __init__(self, source):
self.queue = source
def haf(self):
queue = self.queue[:]#复制原来的队列
newqueue = [] #构造一个存放新队列的列表
#构建哈夫曼树
while len(queue)>1:
queue.sort(key=lambda item :item.probability)
node_left = queue.pop(0)
node_right = queue.pop(0)
if node_left.symbol is not None:
newqueue.append(node_left)
if node_right.symbol is not None:
newqueue.append(node_right)
if node_left.probability>node_right.probability:
node_left.codeWord = '0'
node_right.codeWord = '1'
else:
node_left.codeWord = '1'
node_right.codeWord = '0'
node_fagher = HuffmanObjext( probability = node_left.probability+node_right.probability )
node_fagher.lChild = node_left
node_fagher.rChild = node_right
node_left.father = node_fagher
node_right.father = node_fagher
queue.append(node_fagher)
queue[0].father = None
return newqueue, queue[0]
# 哈夫曼编码
def huffmanEncoding(self):
queue, root = result.haf()
probability = []
codes = ['']*len(queue)
symbol = []
for i in range(len(queue)):
tmp_node = queue[i]
while tmp_node.father != None:
codes[i] = tmp_node.codeWord + codes[i]
tmp_node = tmp_node.father
queue[i].codeWord = codes[i]
symbol.append(queue[i].symbol)
probability.append(queue[i].probability)
return symbol, codes, probability
# This is the Objext the Huffman
class HuffmanObjext():
# get the Object of huffman
def __init__(self, symbol = None, probability = None,codeWord = '', procode = None ):
self.symbol = symbol
self.probability = probability
self.codeWord = codeWord
self.procode = procode
self.father = None
self.lChild = None
self.rChild = None
if __name__ == '__main__':
x1 = HuffmanObjext(symbol = "X1", probability = 0.375, codeWord = '' )
x2 = HuffmanObjext(symbol = "X2", probability = 0.125,codeWord = '' )
x3 = HuffmanObjext(symbol = "X3", probability = 0.25, codeWord = '')
x4 = HuffmanObjext(symbol = "X4", probability = 0.25, codeWord = '')
x5 = HuffmanObjext(symbol = "X5", probability = 0.125, codeWord = '')
# x6 = HuffmanObjext(symbol = "X6", probability = 0.1,\
# codeWord = '')
result = createHuffmanTree([ x1,x2, x3, x4, x5])
# a, b = result.haf()
symbol, code, probability = result.huffmanEncoding()
H = []
K = []
for item in zip(code, probability):
print (item[1] )
H_X = item[1] * math.log(item[1],2)
K_X = item[1] *len(item[0])
H.append(H_X)
K.append(K_X)
H_X = -np.sum(H)
K_X = np.sum(K)
n = H_X /K_X
print ("Encoding" )
print (symbol, "\n" )
print ( code )
# print info
print (H_X )
print ( "average of procode", K_X )
print ("aberage of n", n )
3.最小生成树问题