Greedy Algorithms ms Jeevani Goone*llake University of - - PowerPoint PPT Presentation

greedy algorithms ms jeevani goone llake university of
SMART_READER_LITE
LIVE PREVIEW

Greedy Algorithms ms Jeevani Goone*llake University of - - PowerPoint PPT Presentation

Greedy Algorithms ms Jeevani Goone*llake University of Colombo School of Compu*ng Sri Lanka Jeevani Goone*llake (University of Colombo - Sri


slide-1
SLIDE 1

¡ ¡ ¡ ¡ ¡ ¡Greedy ¡

¡Algorithms ms

Jeevani ¡Goone*llake ¡ University ¡of ¡Colombo ¡School ¡of ¡Compu*ng ¡ Sri ¡Lanka ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-2
SLIDE 2

Hufffma man ¡ ¡Coding

Jeevani ¡Goone*llake ¡ UCSC ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-3
SLIDE 3

Examp mple

a b c d e f

Frequency (in 1000s)

45 13 12 16 9 5

A ¡binary ¡code ¡encodes ¡each ¡character ¡as ¡a ¡binary ¡string ¡or ¡

  • codeword. ¡ ¡
  • Suppose ¡there ¡is ¡a ¡file ¡with ¡100K ¡characters. ¡ ¡
  • Assume ¡that ¡there ¡are ¡only ¡6 ¡dis*nct ¡characters ¡in ¡the ¡file ¡

from ¡a ¡through ¡f, ¡with ¡frequencies ¡as ¡indicated ¡below. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-4
SLIDE 4

Cod Codewor

  • rd
  • In ¡a ¡fixed-­‑length ¡code ¡each ¡codeword ¡has ¡the ¡same ¡length. ¡In ¡

a ¡variable-­‑length ¡code ¡codewords ¡may ¡have ¡different ¡lengths. ¡

a b c d e f Frequency (in 1000s) 45 13 12 16 9 5 Fixed-length codeword 000 001 010 011 100 101 Variable-length codeword 101 100 111 1101 1100

  • Fixed-­‑length ¡= ¡(45*3 ¡+ ¡13*3 ¡+ ¡12*3 ¡+ ¡16*3 ¡+ ¡9*3 ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡+ ¡5*3) ¡* ¡1000 ¡= ¡300K ¡bits ¡

  • Variable-­‑length ¡= ¡(45*1 ¡+ ¡13*3 ¡+ ¡12*3 ¡+ ¡16*3 ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡+ ¡9*4 ¡+ ¡5*4) ¡* ¡1000 ¡= ¡224K ¡bits ¡(Savings ¡= ¡25%) ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-5
SLIDE 5

Enc Encoding ding

  • Given ¡ a ¡ code ¡ (corresponding ¡ to ¡ some ¡ alphabet ¡ ∑ ¡ ) ¡ and ¡ a ¡

message ¡the ¡message ¡is ¡encoded ¡by ¡replacing ¡the ¡characters ¡ with ¡the ¡codewords. ¡

  • Example ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡∑ ¡= ¡{a,b,c,d} ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡C1 ¡ ¡{a ¡= ¡00 ¡ ¡b ¡= ¡01 ¡ ¡c= ¡10 ¡ ¡d= ¡11} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡bad ¡is ¡encoded ¡into ¡010011 ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-6
SLIDE 6

Dec Decoding ding

  • Given ¡ an ¡ encoded ¡ message, ¡ decoding ¡ is ¡ the ¡ process ¡ of ¡

turning ¡it ¡back ¡into ¡the ¡original ¡message. ¡ ¡

  • A ¡message ¡is ¡uniquely ¡decodable ¡if ¡it ¡can ¡only ¡be ¡decoded ¡in ¡
  • ne ¡way. ¡

¡

  • The ¡unique ¡decipherability ¡property ¡is ¡needed ¡in ¡order ¡for ¡a ¡

code ¡to ¡be ¡useful. ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-7
SLIDE 7

Decoding ¡ ¡: ¡ ¡Examp mple

  • Consider ¡the ¡codes ¡C1 ¡and ¡C2 ¡

¡ ¡ ¡ ¡ ¡ ¡C1 ¡ ¡{a ¡= ¡00 ¡ ¡b ¡= ¡01 ¡ ¡c= ¡10 ¡ ¡d= ¡11} ¡ ¡ ¡ ¡ ¡ ¡ ¡C2 ¡ ¡{a ¡= ¡1 ¡b ¡= ¡110 ¡c= ¡10 ¡d= ¡111} ¡ ¡

  • Rela*ve ¡to ¡C1, ¡010011 ¡is ¡uniquely ¡decodable ¡to ¡bad. ¡
  • Rela*ve ¡ to ¡ C2, ¡ 1101111 ¡ is ¡ not ¡ uniquely ¡ decodable ¡ since ¡ it ¡

could ¡be ¡decodable ¡to ¡bad, ¡acad, ¡bda ¡or ¡baaaa. ¡

Fixed-­‑length ¡codes ¡are ¡always ¡uniquely ¡decipherable ¡(e.g. ¡ASCII) ¡ Since ¡these ¡do ¡not ¡always ¡give ¡the ¡best ¡compression ¡variable ¡length ¡codes ¡are ¡preferred. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-8
SLIDE 8

Pr Prefix efix ¡Co ¡Code de

  • A ¡ code ¡ is ¡ called ¡ a ¡ prefix ¡ (free) ¡ code ¡ if ¡ no ¡ codeword ¡ is ¡ a ¡ prefix ¡ of ¡

another ¡one. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡C1 ¡{a ¡= ¡0 ¡b ¡= ¡110 ¡c ¡= ¡10 ¡d ¡= ¡111} ¡ ¡is ¡a ¡prefix ¡code. ¡

  • Every ¡message ¡encoded ¡by ¡a ¡prefix ¡free ¡code ¡is ¡uniquely ¡decodable. ¡ ¡
  • Unlike ¡ASCII, ¡prefix ¡codes ¡can ¡be ¡variable ¡length ¡codes. ¡
  • Please ¡also ¡refer ¡to ¡Frank ¡Drew’s ¡slide ¡no.10 ¡on ¡Huffman’s ¡algorithm.

¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-9
SLIDE 9

Cod Coding ¡t ¡trees ees ¡f ¡for

  • r ¡p

¡prefix ¡c efix ¡cod

  • des

es

  • A ¡ prefix ¡ code ¡ can ¡ be ¡ represented ¡ as ¡ a ¡ binary ¡ coding ¡ tree ¡

whose ¡leaves ¡are ¡the ¡symbols ¡in ¡this ¡tree. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Example: ¡ ¡ ¡ ¡

  • A ¡ Human ¡ code ¡ is ¡ a ¡ prefix ¡ code ¡ whose ¡ coding ¡ tree ¡ is ¡ a ¡ full ¡

binary ¡tree. ¡

000 ¡ 01 ¡ 10 ¡ 11 ¡ 0011 ¡ a ¡ b ¡ c ¡ d ¡ e ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-10
SLIDE 10

OpBma mal ¡ ¡Hufffma man ¡ ¡codes

Suppose ¡we ¡are ¡given ¡a ¡frequency ¡for ¡the ¡leders ¡in ¡∑ ¡, ¡f ¡: ¡∑ ¡ ¡N. ¡ ¡ ¡ ¡ ¡For ¡each ¡character ¡c ¡in ¡the ¡alphabet ¡C, ¡let ¡the ¡frequency ¡of ¡c ¡in ¡the ¡ file ¡is ¡denoted ¡by ¡f[c] ¡and ¡let ¡dT(c) ¡denote ¡the ¡depth ¡of ¡c’s ¡leaf ¡in ¡the ¡

  • tree. ¡ ¡

Note ¡that ¡dT(c) ¡is ¡also ¡the ¡length ¡of ¡the ¡codeword ¡for ¡character ¡c. ¡ The ¡number ¡of ¡bits ¡required ¡to ¡encode ¡a ¡file ¡is ¡thus ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡B(T)= ¡ ¡∑ ¡ ¡f(c) ¡dT(c) ¡ ¡ ¡ ¡ B(T) ¡represents ¡the ¡cost ¡of ¡the ¡tree ¡T ¡and ¡requires ¡to ¡minimize ¡this ¡

  • cost. ¡

¡ ¡

c ¡ϵ∑ ¡

Please ¡also ¡refer ¡to ¡Frank ¡Drew’s ¡slide ¡no.13 ¡on ¡Huffman’s ¡algorithm.

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-11
SLIDE 11

Hufffma man ¡ ¡Code

  • Huffman ¡developed ¡a ¡greedy ¡algorithm ¡that ¡produces ¡a ¡minimum ¡

cost ¡(op*mum) ¡prefix ¡code. ¡

  • ¡The ¡code ¡that ¡it ¡produces ¡is ¡called ¡a ¡Huffman ¡code ¡. ¡
  • The ¡two ¡smallest ¡frequency ¡nodes ¡are ¡chosen ¡at ¡each ¡step. ¡
  • This ¡local ¡decision ¡results ¡in ¡a ¡globally ¡op*mal ¡encoding ¡tree. ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-12
SLIDE 12

Hufffma man’S ¡ ¡Algorithm m

  • ¡C ¡is ¡a ¡set ¡of ¡n ¡characters ¡and ¡that ¡each ¡character ¡c ¡ϵ ¡ ¡C ¡is ¡

an ¡object ¡with ¡a ¡defined ¡frequency ¡f[c]. ¡ ¡

  • ¡ The ¡ algorithm ¡ builds ¡ the ¡ tree ¡ T ¡ corresponding ¡ to ¡ the ¡
  • p*mal ¡code ¡in ¡a ¡bodom-­‑up ¡manner. ¡ ¡
  • ¡ It ¡ begins ¡ with ¡ a ¡ set ¡ of ¡ |C| ¡ leaves ¡ and ¡ performs ¡ a ¡

sequence ¡of ¡|C| ¡-­‑ ¡1 ¡"merging" ¡opera*ons ¡to ¡create ¡the ¡ final ¡tree. ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-13
SLIDE 13

HUFFMAN(C) 1 n |C| 2 Q C 3 for i 1 to n - 1 4 do ALLOCATE-NODE(z) 5 left[z] x EXTRACT-MIN(Q) 6 right[z] y EXTRACT-MIN(Q) 7 f[z] f[x] + f[y] 8 INSERT(Q, z) 9 return EXTRACT-MIN(Q)

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-14
SLIDE 14

The ¡ ¡steps ¡ ¡of ¡ ¡Hufffma man's ¡ ¡algorithm ¡ m ¡

a:45 b:13 e:9 f:5 c:12 d:16 30 25 14 a:45 b:13 e:9 f:5 c:12 d:16

1 1 1

25 14 a:45 b:13 e:9 f:5 c:12 d:16

1 1

14 a:45 b:13 e:9 f:5 c:12 d:16

1

55 30 25 14 a:45 b:13 e:9 f:5 c:12 d:16

1 1 1 1

100 55 30 25 14 a:45 b:13 e:9 f:5 c:12 d:16

1 1 1 1 1

Step 1 Step 2 Step 3 Step 4 Step 5

a ¡ 0 ¡ b ¡ 101 ¡ c ¡ 100 ¡ d ¡ 111 ¡ e ¡ 1101 ¡ f ¡ 1100 ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-15
SLIDE 15

Running ¡ ¡Time me ¡ ¡Analysis ¡ ¡

  • Assumes ¡that ¡Q ¡is ¡implemented ¡as ¡a ¡binary ¡min-­‑heap. ¡
  • For ¡ a ¡ set ¡ C ¡ of ¡ n ¡ characters, ¡ the ¡ ini*aliza*on ¡ of ¡ Q ¡ can ¡ be ¡

performed ¡ in ¡ O(n) ¡ *me ¡ using ¡ the ¡ BUILD-­‑MIN-­‑HEAP ¡

  • procedure. ¡
  • The ¡ for ¡ loop ¡ is ¡ executed ¡ exactly ¡ |n| ¡ -­‑ ¡ 1 ¡ *mes. ¡ Each ¡ heap ¡
  • pera*on ¡requires ¡*me ¡O(log ¡n). ¡ ¡
  • The ¡loop ¡contributes ¡= ¡(|n|-­‑1)*O(logn) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡ ¡O(n ¡log ¡n) ¡

  • ¡ Thus, ¡ the ¡ total ¡ running ¡ *me ¡ of ¡ HUFFMAN ¡ on ¡ a ¡ set ¡ of ¡ n ¡

characters ¡ ¡ ¡= ¡ ¡ ¡O(n) ¡+ ¡O(nlog ¡n) ¡= ¡ ¡O(n ¡log ¡n) ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-16
SLIDE 16

100 14 28 58 14 86 a:45 b:13 e:9 f:5 c:12 d:16

a b c d e f

Frequency (in 1000s)

45 13 12 16 9 5

Fixed-length codeword

000 001 010 011 100 101 a b c d e f

Frequency (in 1000s)

45 13 12 16 9 5

Variable- length codeword

101 100 111 1101 1100

100 30 25 14 55 a:45 b:13 e:9 f:5 c:12 d:16 An optimal code for a file is always represented by a full binary tree, in which every non-leaf node has two children. The fixed-length code in our example is not optimal since its tree, is not a full binary tree.

1 1 1 1 1 1 1 1 1 1 1 Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-17
SLIDE 17

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Correctness ¡ ¡of ¡ ¡Hufffma man's ¡ ¡algorithm m

¡To ¡prove ¡that ¡ ¡the ¡ ¡greedy ¡ ¡algorithm ¡ ¡HUFFMAN ¡ ¡is ¡correct, ¡it ¡is ¡ ¡necessary ¡to ¡show ¡that ¡the ¡problem ¡of ¡determining ¡an ¡op*mal ¡ ¡prefix ¡code ¡exhibits ¡ ¡ ¡

  • ¡ ¡ ¡ ¡the ¡greedy-­‑choice ¡property ¡and ¡ ¡
  • ¡ ¡ ¡ ¡op*mal-­‑substructure ¡property. ¡

¡ This slide set only contains the proof of greedy choice property. Please also refer to Frank Drew’s slides on Huffman’s algorithm.

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-18
SLIDE 18

Lemma mma ¡ ¡:Greedy-­‑

  • ­‑Choice ¡

¡property ¡ ¡

Let C be an alphabet in which each character c C has frequency f[c]. Let x and y be two characters in C having the lowest frequencies. Then there exists an optimal prefix code for C in which the codewords for x and y have the same length and differ only in the last bit.

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-19
SLIDE 19

The idea of the proof is to take the tree T representing an arbitrary optimal prefix code. Modify it to make a tree representing another optimal prefix code such that the characters x and y appear as sibling leaves

  • f maximum depth in the new tree.

If we can do this, then their codewords will have the same length and differ only in the last bit.

Pr Proof ¡:G f ¡:Greedy eedy-­‑Cho

  • ­‑Choic

ice ¡pr e ¡property perty

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-20
SLIDE 20

Pr Proof ¡:G f ¡:Greedy eedy-­‑Cho

  • ­‑Choic

ice ¡pr e ¡property perty

b a y x

  • Let a and b be two characters that are sibling leaves of maximum

depth in T. Without loss in generality, assume that f[a] ≤ f[b] and f[x] ≤ f[y]

  • Since f[x] and f[y] are the two lowest frequencies in that order,

and f[a] and f[b] are two arbitrary frequencies in that order, we have f[x] ≤ f[a] and f[y] ≤ f[b].

b x y a T T’

Exchange ¡ ¡the ¡posi*ons ¡of ¡a ¡and ¡x ¡in ¡T, ¡to ¡produce ¡T’. ¡

( f([c] denotes the frequency of c )

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-21
SLIDE 21

Pr Proof ¡:G f ¡:Greedy eedy-­‑Cho

  • ­‑Choic

ice ¡pr e ¡property ¡ perty ¡

The difference in cost between T and T’ is B(T) – B(T’) = Σ f(c) dT(c) - Σ f(c) dT’(c) = f[x] dT(x) + f[a] dT(a) - f[x] dT’(x) - f[a] dT’(a) = f[x] dT(x) + f[a] dT(a) - f[x] dT(a) - f[a] dT(x) = (f[a] - f[x]) (dT(a) - dT(x)) ≥ 0 (cost is not increased)

For each character c in the alphabet C,

  • f(c) denote the frequency of c in the file
  • dT(c) denote the depth of c 's leaf in the tree.

(dT(c) is also the length of the codeword for character c)

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-22
SLIDE 22

b a y x

Similarly ¡exchanging ¡ ¡the ¡posi*ons ¡of ¡b ¡and ¡y ¡in ¡T’, ¡to ¡produce ¡ T’’ ¡does ¡not ¡increase ¡the ¡cost, ¡ ¡

b x y a T T’ y x b a T’’

B(T’) ¡– ¡B(T’’) ¡is ¡non-­‑nega*ve. ¡ Therefore ¡B(T’’) ¡ ¡≤ ¡ ¡B(T), ¡and ¡since ¡it ¡is ¡assumed ¡that ¡ ¡T ¡is ¡

  • p*mal, ¡we ¡have ¡B(T) ¡≤ ¡ ¡B(T’’), ¡ ¡ ¡=> ¡B(T’’) ¡= ¡B(T) ¡

Thus, ¡T’’ ¡is ¡an ¡op*mal ¡tree ¡in ¡which ¡x ¡& ¡y ¡appear ¡as ¡sibling ¡ leaves ¡of ¡maximum ¡depth ¡from ¡which ¡the ¡lemma ¡follows. ¡ Proof ¡:Greedy-­‑Choice ¡property ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-23
SLIDE 23

Hufffma man ¡ ¡Code

Ques(ons ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-24
SLIDE 24

Qu QuesBon ¡ ¡1

  • Construct ¡a ¡Huffman ¡code ¡for ¡the ¡following ¡characters ¡if ¡their ¡

frequencies ¡are ¡as ¡ ¡ ¡given ¡below: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡e ¡ ¡ ¡ ¡ ¡h ¡ ¡ ¡ ¡ ¡m ¡ ¡ ¡ ¡ ¡ ¡o ¡ ¡ ¡ ¡ ¡ ¡r ¡ ¡ ¡ ¡ ¡ ¡ ¡t ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡30% ¡ ¡ ¡ ¡ ¡15% ¡ ¡ ¡ ¡ ¡ ¡8% ¡ ¡ ¡ ¡ ¡ ¡25% ¡ ¡ ¡ ¡ ¡ ¡9% ¡ ¡ ¡ ¡ ¡13% ¡ ¡

  • Decode ¡the ¡following ¡message ¡using ¡the ¡Huffman ¡code ¡constructed ¡
  • before. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡100101110100111000 ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

slide-25
SLIDE 25

Qu QuesBon ¡ ¡2

  • Consider ¡that ¡it ¡is ¡necessary ¡to ¡use ¡Huffman’s ¡algorithm ¡to ¡obtain ¡the ¡ ¡

encoding ¡ ¡of ¡the ¡alphabet ¡{p,q,r} ¡with ¡frequencies ¡fp, ¡fq, ¡fr. ¡In ¡each ¡of ¡ the ¡following ¡cases, ¡either ¡give ¡ ¡an ¡ ¡example ¡ ¡of ¡ ¡frequencies ¡fp, ¡fq, ¡ ¡fr ¡ ¡ that ¡ ¡would ¡yield ¡ ¡the ¡specified ¡code, ¡or ¡explain ¡why ¡the ¡given ¡code ¡ cannot ¡be ¡obtained. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡(a) ¡p ¡= ¡0, ¡q ¡= ¡10, ¡ ¡r ¡= ¡11 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡(b) ¡p ¡= ¡0, ¡q ¡= ¡1, ¡ ¡r ¡= ¡00 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡(c) ¡p ¡= ¡10, ¡q ¡= ¡01, ¡ ¡r ¡= ¡00 ¡ ¡ ¡

Jeevani ¡Goone*llake ¡(University ¡of ¡Colombo ¡-­‑ ¡Sri ¡Lanka) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Lecture ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡