Professional Documents
Culture Documents
1
10/23/2014
+ =1
2
10/23/2014
depth +1 + (depth + 1)
3
10/23/2014
4
10/23/2014
5
10/23/2014
6
10/23/2014
7
10/23/2014
8
10/23/2014
OPTIMAL-BST( , , )
1. let 1. . + 1,0. . , [1. . + 1,0. . ], root 1. . , 1. . be new tables
2. for = 1 to + 1
3. 1 =
4. 1 =
5. for = 1 to
6. for = 1 to +1
7. 1
8.
9. 1 +
10. for to
11. 1 + + 1, ]
12. if ]
13.
14. root
15.return and root
MAT-72006 AA+DS, Fall 2014 23-Oct-14 533
9
10/23/2014
10
10/23/2014
16 Greedy Algorithms
• Optimization algorithms typically go through a
sequence of steps, with a set of choices at each
• For many optimization problems, using dynamic
programming to determine the best choices is
overkill; simpler, more ef cient algorithms will do
• A greedy algorithm always makes the choice
that looks best at the moment
• That is, it makes a locally optimal choice in the
hope that this choice will lead to a globally
optimal solution
MAT-72006 AA+DS, Fall 2014 23-Oct-14 536
11
10/23/2014
12
10/23/2014
13
10/23/2014
14
10/23/2014
15
10/23/2014
RECURSIVE-ACTIVITY-SELECTOR )
1. +1
2. while and ]< ] // nd the rst
// activity in to nish
3. +1
4. if
5. return RECURSIVE-ACTIVITY-
SELECTOR )
6. else return
MAT-72006 AA+DS, Fall 2014 23-Oct-14 547
16
10/23/2014
17
10/23/2014
18
10/23/2014
19
10/23/2014
Pre x codes
• We consider only codes in which no codeword is
also a pre x of some other codeword
• A pre x code can always achieve the optimal data
compression among any character code, and so we
can restrict our attention to pre x codes
• Encoding is always simple for any binary character
code; we just concatenate the codewords
representing each character of the le
• E.g., with the variable-length pre x code, we code
the 3-character le as 101 100 = 0101100
MAT-72006 AA+DS, Fall 2014 23-Oct-14 554
20
10/23/2014
21
10/23/2014
22
10/23/2014
HUFFMAN )
1. |
2.
3. for 1 to 1
4. allocate a new node
5. EXTRACT-MIN )
6. EXTRACT-MIN )
7.
8. INSERT )
9. return EXTRACT-MIN ) // return the root of the tree
MAT-72006 AA+DS, Fall 2014 23-Oct-14 561
23
10/23/2014
24
10/23/2014
25