6 Greedy
Continuous problem
You are given activities with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time
Solution: Select the least end time suitable remaining activities each time
Proof:
- Let be the greedy choice (starting with 1st activity in the sorted array).
- Let there be another choice starting with some activity (, so finishTime() >= finishTime(1)) which alone gives the optimal solution.
So, does not have the 1st activity and we can make more likes without changing the number of activities by:
Repeat until B turning into A.
We conclude that , therefore activity also gives the optimal solution.
Huffman Tree
Introduction
- Fixed-length coding: Information Entropy(en- tropical
turn
混乱) - Variable-length coding: Prefix-Free code (no codeword is a prefix of a codework of another symbol)
Proof
Theorem
- T: a tree for some prefix encoding A and some probability distribution p over the symbols.
- x and y: two leaves
- T': the tree obtained by swapping x and y in T.
Then
Lemma
something received or taken (di-:both side)
An optimal tree two symbols with least probabilities are sibling leaves in the lowest level. Easy
Correctness proof
- n=2 obvious
- n>2
Consider an alphabet A of n-1 letters.(except the two least probabilities leaves which parent is z)
Let T be an optimum tree for A
Let A'=A+{x,y}-z,T':adding x,y as children of z.
:optimum tree for A'
:removing x,y from H;
Then
so