贪心算法

if the problem cannot be solved by greedy algorithm then we can try to use depth-first search

1. beneath every greedy algorithm there is almost always a more cumbersome dynamic programming solution.
2. greedy algorithm usually will cast optimization problem into one where we make one choice and produces a subproblem
3. when the problem has properties of optimal substructure and overlapping subproblem then it can be solved by dynamic programming but it also could be solved by greedy algorithm possibly.
4. prove the problem can be solved by greedy algorithm.
  a. Prove the greedy choice is included in the globally optimal solution.
  b. proof method. contradiction method. prove by assuming that the greedy choice is not included in the globally optimal solution. then we can create a solution by replacing the choice with greedy choice and compare the solution with the globally optimal solution. if the solution is better than the globally optimal solution then we prove that greedy choice is included in the optimal solution and the problem can be solved by greedy algorithm.

huffman tree:
optimal substructure: T' is optimal tree for C' = C- {x,y} + {z} where x,y are chars with minimum frequency in C and z is new added character with f[z]=f[x]+f[y]. T obtained from T' by replacing leaf node z with an internal node having x and y as children, represents the optimal tree for C.
greedy choice property: always choose two chars with minimum frequency to merge as  a new char.
method to prove:
contradictory method(greedy algorithm)
induction method (recursion, tree, )

Fibonacci numbers:
F(n)=F(n-1)+F(n-2)
F(1)=1,F(2)=1
theorem: F(n)>F(10+F(2)+....F(n-2)

Proof: F(3)=F(1)+F(2)=2>a1=1
assume that F(n) > F(1)+F(2)+....F(n-2)
then F(n+1)=F(n)+F(n-1)> F(1) + F(2) + ... F(n-2) + F(n-1). proved.

你可能感兴趣的:(Algorithm,算法,properties,tree,optimization,recursion)