# Assignment 2: (Due at 10:30 a.m on Friday of Week 10) - PowerPoint PPT Presentation

1 / 23

Question 1 (Given in Tutorial 5) Question 2 (Given in Tutorial 7) If you do Question 1 only, you get 60 points. If you do Question 2 only, you get 90 points. If you correctly do both Question 1 and Question 2, you get 100 points.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Assignment 2: (Due at 10:30 a.m on Friday of Week 10)

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

#### Presentation Transcript

• Question 1 (Given in Tutorial 5)

• Question 2 (Given in Tutorial 7)

• If you do Question 1 only, you get 60 points.

• If you do Question 2 only, you get 90 points.

• If you correctly do both Question 1 and Question 2, you get 100 points.

• Bonus: 5 Points will be given to those who write a Java program for the Huffman code algorithm.

## Assignment 2: (Due at 10:30 a.m on Friday of Week 10)

Lecture 1: Some concept: Pseudo code, Abstract Data Type. (Page 60 of text book.)

Stack. Give the ADT of stack (slide 11 of lecture1)

The interface is on slide 19. (Q: Is the interface equivalent to ADT? Not really. We need the method for insertion and deletion, i.e., first in last out. )

Applications: parentheses matching

## Review of Lecture 1 to Lecture 6

Just know how to setup a list. (Assignment 1)

Lecture 3: Analysis of Algorithms (important)

Primitive operations

Count number of primitive operations for an algorithm

big-O notation 2nO(n), 5n2+10n+11++>O(n2).

## Lecture 2:

Definition of tree (slide 7)

Tree terminology: root, internal node, external node (leaf), depth of a node, height of a node, height of a node.

Inorder traversal of a binary tree

In terms of programming, understand TreeInExample1.java. (If tested in exam, java codes will be given. I do not want to give long code.)

## Lecture 4: Tree

Just understand the node:

Preorder traversal for any tree

Postorder traversal for any tree

Array-Based representation of binary tree (slide 9)

Algorithms for Depth(), Height() slide 12-15.

## Lecture 5: More on Trees

• Priority Queue ADT (slide 2)

• Heap:

• definition of heap

• What does “heap-order” mean?

• Complete Binary tree (what is a complete binary?)

• Height of a complete binary tree with n nodes is O(log n).

• Insert a node into a heap runtimg time O(log n).

• removeMin: remove a node with minimum key. Running time O(log n)

• Array-based complete binary tree representation.

• Show a sample exam paper.

## Lecture 6: Priority Queue (Heeps)

• Priority Queue ADT (slide 2)

• Heap:

• definition of heap

• What does “heap-order” mean?

• Complete Binary tree (what is a complete binary?)

• Height of a complete binary tree with n nodes is O(log n).

• Insert a node into a heap runtimg time O(log n).

• removeMin: remove a node with minimum key. Running time O(log n)

• Array-based complete binary tree representation.

• Show a sample exam paper.

## Lecture 6: Priority Queue (Heeps)

Exercise:

Give some trees and ask students to give InOrder, PostOrder and PreOrder.

Tutorial 6 of Question 2: Using PreOrder.

Given a complete binary, write the array representation.

Given an array, draw the complete binary tree.

Given a heap, show the steps to removMin.

Given a heap, show the steps to insert a node with key 3. (Do it for the tree version, do it for an array version.)

Linear time construction of a heap.

### Huffman codes (Page 565 Chapter 12.4)

• Binary character code: each character is represented by a unique binary string.

• A data file can be coded in two ways:

The first way needs 1003=300 bits. The second way needs

45 1+13 3+12 3+16 3+9 4+5 4=232 bits.

Hash Tables

### Variable-length code

• Need some care to read the code.

• 001011101 (codeword: a=0, b=00, c=01, d=11.)

• Where to cut? 00 can be explained as either aa or b.

• Prefix of 0011: 0, 00, 001, and 0011.

• Prefix codes: no codeword is a prefix of some other codeword. (prefix free)

• Prefix codes are simple to encode and decode.

Hash Tables

### Using codeword in Table to encode and decode

• Encode: abc = 0.101.100 = 0101100

• (just concatenate the codewords.)

• Decode: 001011101 = 0.0.101.1101 = aabe

Hash Tables

100

0

100

0

1

1

86

a:45

14

0

1

0

0

1

0

1

1

58

28

14

0

0

1

0

1

0

1

c:12

b:13

d:16

14

30

0

1

55

25

a:45

b:13

c:12

d:16

e:9

f:5

f:5

e:9

• Encode: abc = 0.101.100 = 0101100

• (just concatenate the codewords.)

• Decode: 001011101 = 0.0.101.1101 = aabe

• (use the (right)binary tree below:)

Tree for the fixed length codeword

Tree for variable-length codeword

Hash Tables

### Binary tree

• Every nonleaf node has two children.

• The fixed-length code in our example is not optimal.

• The total number of bits required to encode a file is

• f ( c ): the frequency (number of occurrences) of c in the file

• dT(c): denote the depth of c’s leaf in the tree

Hash Tables

### Constructing an optimal code

• Formal definition of the problem:

• Input:a set of characters C={c1, c2, …, cn}, each cC has frequency f[c].

• Output: a binary tree representing codewords so that the total number of bits required for the file is minimized.

• Huffman proposed a greedy algorithm to solve the problem.

Hash Tables

c:12

b:13

a:45

d:16

0

1

f:5

e:9

14

(a)

f:5

e:9

c:12

b:13

d:16

a:45

(b)

Hash Tables

a:45

0

1

c:12

b:13

d:16

0

1

a:45

f:5

e:9

0

1

1

0

c:12

b:13

d:16

0

1

f:5

e:9

14

14

30

25

25

(c)

(d)

Hash Tables

a:45

0

1

0

100

1

0

1

1

0

a:45

c:12

b:13

d:16

0

1

0

1

f:5

e:9

0

1

1

0

c:12

b:13

d:16

14

14

30

30

0

1

55

55

25

25

f:5

e:9

(f)

(e)

Hash Tables

HUFFMAN(C)

1n:=|C|

2Q:=C

3for i:=1 to n-1 do

4z:=ALLOCATE_NODE()

5x:=left[z]:=EXTRACT_MIN(Q)

6y:=right[z]:=EXTRACT_MIN(Q)

7f[z]:=f[x]+f[y]

8INSERT(Q,z)

9return EXTRACT_MIN(Q)

Hash Tables

### The Huffman Algorithm

• This algorithm builds the tree T corresponding to the optimal code in a bottom-up manner.

• C is a set of n characters, and each character c in C is a character with a defined frequency f[c].

• Q is a priority queue, keyed on f, used to identify the two least-frequent characters to merge together.

• The result of the merger is a new object (internal node) whose frequency is the sum of the two objects.

Hash Tables

### Time complexity

• Lines 4-8 are executed n-1 times.

• Each heap operation in Lines 4-8 takes O(lg n) time.

• Total time required is O(n lg n).

Note: The details of heap operation will not be tested. Time complexity O(n lg n) should be remembered.

Hash Tables

10

0

1

c:6

b:9

d:11

e:4

a:6

### Another example:

e:4

a:6

c:6

b:9

d:11

Hash Tables

0

1

0

0

1

1

c:6

c:6

b:9

b:9

e:4

a:6

10

15

10

15

21

0

1

d:11

0

1

e:4

a:6

d:11

Hash Tables

0

1

36

10

15

21

0

1

0

1

d:11

c:6

b:9

0

1

e:4

a:6

Summary Huffman Code: Given a set of characters and frequency, you should be able to construct the binary tree for Huffman codes.Proofs for why this algorithm can give optimal solution are not required.

Hash Tables