0% found this document useful (0 votes)
106 views51 pages

Discrete Mathematics

The document provides information about discrete mathematics topics including universal addressing systems for ordered rooted trees, eccentricity in unrooted trees, prefix codes, and Huffman coding. It explains how to label vertices in an ordered rooted tree using a universal addressing scheme, defines eccentricity as the longest simple path from a vertex, and describes how prefix codes and Huffman coding can be used to assign variable-length binary codes to characters to reduce the number of bits used for encoding.

Uploaded by

Isif Sethi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views51 pages

Discrete Mathematics

The document provides information about discrete mathematics topics including universal addressing systems for ordered rooted trees, eccentricity in unrooted trees, prefix codes, and Huffman coding. It explains how to label vertices in an ordered rooted tree using a universal addressing scheme, defines eccentricity as the longest simple path from a vertex, and describes how prefix codes and Huffman coding can be used to assign variable-length binary codes to characters to reduce the number of bits used for encoding.

Uploaded by

Isif Sethi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 51

Discrete Mathematics

Trees

1
Outline of Today’s Lecture

 Universal Address System.


 Eccentricity.
 Prefix codes.
 Huffman Coding.

2
Universal Address System

 In ordered rooted trees, vertices may be


labeled according to the following
scheme:
 choose a root node and label it 0
 each of root’s k children are labeled, left to
right, as 1, 2, … , k
 for each vertex v at level n with label A, label
its kv children left to right as A.1, A.2, … A.k

3
Universal Address System of
an Ordered Rooted Tree

4
Address This Tree

5
Solution

6
Think Carefully

 Suppose that the address of the vertex v in the ordered


rooted tree T is 3.4.5.2.4.
 At what level is v?
 5
 b) What is the address of the parent of v?
 3.4.5.2
 c) What is the least number of siblings v can have?
 2 including v

7
Eccentricity

 The eccentricity of a vertex in an


unrooted tree is the length of the longest
simple path beginning at this vertex.
 A vertex is called a center if no vertex in
the tree has smaller eccentricity than
this vertex.

8
Center = c

Finding Eccentricity

Ecc(a) = 4
Ecc(b) = 5
Ecc(c) = 3
Ecc(d) = 4
Ecc(e) = 4
Ecc(f) = 5
Ecc(g) = 5
Ecc(h) = 5
Ecc(i) = 5
Ecc(j) = 6
Ecc(k) = 6
Ecc(l) = 6 9
Problem 1
 Consider the problem of using bit strings to encode the
letters of the English alphabet (where no distinction is
made between lowercase and uppercase letters).
 We can represent each letter with a bit string of length 5.
 The total number of bits used to encode data is five
times the number of characters in the text when each
character is encoded with five bits.
 Is it possible to find a coding scheme of these letters
such that, when data are coded, fewer bits are used? We
can save memory and reduce transmission time if this
can be done.

10
Problem 2

 Consider using bit strings of different lengths to


encode letters.
 Letters that occur more frequently should be
encoded using short bit strings, and longer bit
strings should be used to encode rarely occurring
letters.
 If e were encoded with 0, a with 1, and t with 01,
then the bit string 0101 could correspond to
 eat, (0 1 01)
 tea, (01 0 1)
 eaea, (0 1 0 1)
 or tt. (01 01) 11
Prefix Code – Solution to
Problems

 No code is the prefix of any other code.


 A prefix code can be represented using a binary
tree,
 Where leaves represent the characters in string.
 The edges of the tree are labeled so that an edge leading
to a left child is assigned a 0 and an edge leading to a
right child is assigned a 1.
 The bit string used to encode a character is the
sequence of labels of the edges in the unique path
from the root to the leaf that has this character as
its label.
12
A Binary Tree with Prefix Code

e=0
a = 10
t = 110
n = 1110
s = 1111

13
Decode 11111011100

1111 = s
Now string becomes = 1011100
10 = a
Now string becomes = 11100
1110 = n
0 =e

14
Construct the BT for Prefix
Codes

A:00, B:010, C:011, D:10, E:11

0 1

0 1 0 1
A D E
0 1
B C
15
Huffman Coding

 David A. Huffman (1951)


 Huffman coding uses frequencies of symbols in a string to build a variable rate prefix
code
 Each symbol is mapped to a binary string
 More frequent symbols have shorter codes
 No code is a prefix of another
 Example:
0 1

A 0 A
0 1
B 100
0 D
C 101 1
D 11 B C

16
Construction of Huffman Trees

P(A)= 0.4, P(B)= 0.1, P(C)= 0.3, P(D)= 0.1, P(E)= 0.1

0.1 0.1 0.1 0.3 0.4


E D B C A

0.2 0.1 0.3 0.4


B C A

D E

17
Construction of Huffman Trees
0.1 0.2 0.3 0.4
B C A
0 1

D E

0.3 0.3 0.4


C A
0 1

B
0 1

D E

18
Construction of Huffman Trees

0.3 0.3 0.4 0.6 0.4


C A A
0 1 0 1

B C
0 1 0 1

D E B
0 1

D E

19
Construction of Huffman Trees
1.0
0.4 0.6
A
0 1
0 1

C A
0 1
0 1

B C
0 1 0 1

D E B
0 1

D E
20
Construction of Huffman Trees

0 1
A=0
A B = 100
0 1
C = 11
C D = 1010
0 1
E = 1011
B
0 1

D E

21
Another Example

22
Building a Tree
Scan the original text

 Consider the following short text:

 Eerie eyes seen near lake.

 Count up the occurrences of all characters in


the text

23
Building a Tree
Scan the original text

Eerie eyes seen near lake.


 What characters are present?

E e r i space ysnarlk.

24
Building a Tree
Scan the original text
Eerie eyes seen near lake.
 What is the frequency of each character in the text?

25
Building a Tree
Prioritize characters

 Create binary tree nodes with character


and frequency of each character
 Place nodes in a priority

26
Building a Tree
 After inserting all nodes

E i y l k . r s n a sp e
1 1 1 1 1 1 2 2 2 2 4 8

 Null Pointers are not shown

27
Building a Tree

y l k . r s n a sp e
1 1 1 1 2 2 2 2 4 8

E i

28
Building a Tree

y l k . r s n a sp e
1 1 1 1 2 2 2 2 4 8
E i

29
Building a Tree

k . r s n a sp e
1 1 2 2 2 2 4 8
E i

y l

30
Building a Tree

2
k . r s n a 2 sp e
1 1 2 2 2 2 4 8
y l
E i

31
Building a Tree

r s n a 2 2 sp e
2 2 2 2 4 8
y l
E i

k .

32
Building a Tree

r s n a 2 2 sp e
2
2 2 2 2 4 8
E i y l k .

33
Building a Tree

n a 2 sp e
2 2
2 2 4 8
E i y l k .

r s

34
Building a Tree

n a 2 sp e
2 4
2
2 2 4 8

E i y l k . r s

35
Building a Tree

2 4 e
2 2 sp
8
4
y l k . r s
E i

n a

36
Building a Tree

2 4 4 e
2 2 sp
8
4
y l k . r s n a
E i

37
Building a Tree

4 4 e
2 sp
8
4
k . r s n a

2 2

E i y l

38
Building a Tree

4 4 4
2 sp e
4 2 2 8
k . r s n a

E i y l

39
Building a Tree

4 4 4
e
2 2 8
r s n a

E i y l

2 sp

k .

40
Building a Tree

4 4 4 6 e
2 sp 8
r s n a 2 2
k .
E i y l

41
Building a Tree

4 6 e
2 2 2 8
sp

E i y l k .
8

4 4

r s n a

42
Building a Tree

4 6 e 8
2 2 2 8
sp
4 4
E i y l k .
r s n a

43
Building a Tree

8
e
8
4 4
10
r s n a
4
6
2 2
2 sp
E i y l k .

44
Building a Tree

8 10
e
8 4
4 4
6
2 2 2
r s n a sp
E i y l k .

45
Building a Tree

10
16
4
6
2 2 e 8
2 sp
E i y l k . 4 4

r s n a

46
Building a Tree

10 16

4
6
e 8
2 2
2 sp
4 4
E i y l k .

r s n a

47
Building a Tree

26

16
10

4 e 8
6
2 2 2 sp 4 4

E i y l k .
r s n a

Frequency of root node should equal number of


characters in text.
48
Prefix Codes Generated
Char Code
E 0000
i 0001
y 0010 26
l 0011
16
k 0100 10
. 0101
4
space 011 6
e 8
e 10 2 2 2 4
sp 4
r 1100
s 1101 E i y l k .
r s n a
n 1110
a 1111
Encoding the File
 Rescan text and encode file
using new code words Char Code
E 0000
Eerie eyes seen near lake. i 0001
y 0010
l 0011
k 0100
0000101100000110011100010 . 0101
1011011010011111010111111 space 011
e 10
00011001111110100100101 r 1100
s 1101
n 1110
a 1111
 Why is there no need
for a separator
character?
. 50
References

 Discrete Mathematics’ by Rosen, 7th


Edition
 Section 11.2

51

You might also like