Going from A to G Star=ng with a full tank, we can - - PDF document

going from a to g
SMART_READER_LITE
LIVE PREVIEW

Going from A to G Star=ng with a full tank, we can - - PDF document

7/20/10 Going from A to G Star=ng with a full tank, we can drive 350 miles before we need to gas up, minimize the number of CS420


slide-1
SLIDE 1

7/20/10 ¡ 1 ¡

CS420 ¡lecture ¡eight ¡ Greedy ¡Algorithms ¡ Going ¡from ¡A ¡to ¡G ¡

  • Star=ng ¡with ¡a ¡full ¡tank, ¡we ¡can ¡drive ¡350 ¡miles ¡

before ¡we ¡need ¡to ¡gas ¡up, ¡minimize ¡the ¡number ¡of ¡ =mes ¡we ¡need ¡ ¡to ¡stop ¡at ¡a ¡gas ¡sta=on ¡ ¡ ¡ ¡ ¡ ¡A ¡ ¡ ¡ ¡ ¡ ¡B ¡ ¡ ¡ ¡ ¡C ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡D ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡E ¡ ¡ ¡ ¡ ¡F ¡ ¡ ¡ ¡ ¡ ¡ ¡G ¡ ¡ ¡ ¡ ¡ ¡0 ¡ ¡ ¡ ¡ ¡250 ¡300 ¡ ¡ ¡ ¡ ¡600 ¡ ¡ ¡ ¡850 ¡ ¡900 ¡ ¡1100 ¡

Going ¡from ¡A ¡to ¡G ¡

  • We ¡can ¡drive ¡350 ¡miles ¡before ¡we ¡need ¡to ¡gas ¡up, ¡

minimize ¡the ¡number ¡of ¡=mes ¡we ¡need ¡ ¡to ¡stop ¡at ¡a ¡ gas ¡sta=on ¡

  • a ¡possible ¡(non ¡greedy) ¡op=mal ¡solu=on ¡

¡ ¡ ¡ ¡ ¡A ¡ ¡ ¡ ¡ ¡ ¡B ¡ ¡ ¡ ¡ ¡C ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡D ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡E ¡ ¡ ¡ ¡ ¡F ¡ ¡ ¡ ¡ ¡ ¡ ¡G ¡ ¡ ¡ ¡ ¡ ¡0 ¡ ¡ ¡ ¡ ¡250 ¡300 ¡ ¡ ¡ ¡ ¡600 ¡ ¡ ¡ ¡850 ¡ ¡900 ¡ ¡1100 ¡

Going ¡from ¡A ¡to ¡G ¡

  • We ¡can ¡drive ¡350 ¡miles ¡before ¡we ¡need ¡to ¡gas ¡up, ¡

minimize ¡the ¡number ¡of ¡=mes ¡we ¡need ¡ ¡to ¡stop ¡at ¡a ¡gas ¡ sta=on ¡

  • we ¡can ¡make ¡it ¡greedy: ¡

¡ ¡ ¡ ¡ ¡A ¡ ¡ ¡ ¡ ¡ ¡B ¡ ¡ ¡ ¡ ¡C ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡D ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡E ¡ ¡ ¡ ¡ ¡F ¡ ¡ ¡ ¡ ¡ ¡ ¡G ¡ ¡ ¡ ¡ ¡ ¡0 ¡ ¡ ¡ ¡ ¡250 ¡300 ¡ ¡ ¡ ¡ ¡600 ¡ ¡ ¡ ¡850 ¡ ¡900 ¡ ¡1100 ¡ A ¡greedy ¡solu=on ¡goes ¡as ¡far ¡as ¡possible ¡before ¡gassing ¡up, ¡ ¡ and ¡we ¡can ¡turn ¡an ¡op=mal ¡solu=on ¡into ¡a ¡greedy ¡one ¡by ¡ making ¡the ¡first ¡step ¡greedy, ¡and ¡then ¡applying ¡induc=on ¡

slide-2
SLIDE 2

7/20/10 ¡ 2 ¡

Greedy ¡algorithms ¡

Greedy ¡ ¡algorithms ¡determine ¡a ¡global ¡op3mum ¡ ¡ via ¡(a ¡number ¡of) ¡locally ¡op3mal ¡choices ¡

Ac=vity ¡Selec=on ¡

  • Given ¡a ¡set ¡of ¡ac=vi=es ¡S ¡= ¡{ ¡1,2,3,...,N ¡} ¡that ¡use ¡a ¡ ¡

resource ¡and ¡have ¡a ¡start ¡=me ¡Si ¡ ¡and ¡finish ¡=me ¡Fi ¡ ¡ Si<=Fi. ¡ ¡

  • Ac=vi=es ¡are ¡compa=ble ¡if ¡the ¡intervals ¡[Si,Fi) ¡and ¡ ¡

[Sj,Fj) ¡do ¡not ¡overlap: ¡Si>=Fj ¡or ¡Sj>=Fi ¡ ¡

  • ¡[ ¡ ¡[) ¡means: ¡includes ¡le\ ¡but ¡only ¡up ¡to ¡right ¡ ¡) ¡ ¡
  • The ¡ ¡Ac3vity-­‑selec3on ¡problem ¡is ¡to ¡select ¡a ¡ ¡

maximum-­‑size ¡set ¡of ¡mutually ¡compa3ble ¡ac=vi=es. ¡

Greedy ¡algorithm ¡for ¡Ac=vity ¡Selec=on ¡

  • How ¡would ¡you ¡do ¡it? ¡

Greedy ¡algorithm ¡for ¡Ac=vity ¡Selec=on ¡

¡ ¡ ¡ ¡ ¡Sort ¡ac=vi=es ¡by ¡finish ¡=me ¡F1 ¡<= ¡F2 ¡...<= ¡Fn ¡ ¡ ¡ ¡ ¡ ¡A=1 ¡ ¡ ¡ ¡ ¡ ¡j=1 ¡ ¡ ¡ ¡ ¡ ¡for ¡i ¡= ¡2 ¡to ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡Si>=Fj ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡include ¡i ¡in ¡A ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡j=i ¡

slide-3
SLIDE 3

7/20/10 ¡ 3 ¡

Eg ¡from ¡Cormen ¡et. ¡al. ¡

¡i ¡ ¡ ¡ ¡1 ¡ ¡ ¡2 ¡ ¡ ¡3 ¡ ¡ ¡4 ¡ ¡ ¡5 ¡ ¡ ¡6 ¡ ¡ ¡ ¡7 ¡ ¡ ¡ ¡8 ¡ ¡ ¡ ¡9 ¡ ¡ ¡10 ¡ ¡ ¡11 ¡ ¡ ¡ ¡ Si ¡ ¡ ¡1 ¡ ¡ ¡3 ¡ ¡ ¡0 ¡ ¡ ¡5 ¡ ¡ ¡3 ¡ ¡ ¡5 ¡ ¡ ¡ ¡6 ¡ ¡ ¡ ¡8 ¡ ¡ ¡ ¡8 ¡ ¡ ¡ ¡ ¡2 ¡ ¡ ¡12 ¡ ¡ Fi ¡ ¡ ¡4 ¡ ¡ ¡5 ¡ ¡ ¡6 ¡ ¡ ¡7 ¡ ¡ ¡8 ¡ ¡ ¡9 ¡ ¡10 ¡ ¡11 ¡ ¡12 ¡ ¡13 ¡ ¡ ¡14 ¡

Eg ¡from ¡Cormen ¡et. ¡al. ¡

¡i ¡ ¡ ¡ ¡1 ¡ ¡ ¡2 ¡ ¡ ¡3 ¡ ¡ ¡4 ¡ ¡ ¡5 ¡ ¡ ¡6 ¡ ¡ ¡ ¡7 ¡ ¡ ¡ ¡8 ¡ ¡ ¡ ¡9 ¡ ¡ ¡10 ¡ ¡ ¡11 ¡ ¡ ¡ ¡ Si ¡ ¡ ¡1 ¡ ¡ ¡3 ¡ ¡ ¡0 ¡ ¡ ¡5 ¡ ¡ ¡3 ¡ ¡ ¡5 ¡ ¡ ¡ ¡6 ¡ ¡ ¡ ¡8 ¡ ¡ ¡ ¡8 ¡ ¡ ¡ ¡ ¡2 ¡ ¡ ¡12 ¡ ¡ Fi ¡ ¡ ¡4 ¡ ¡ ¡5 ¡ ¡ ¡6 ¡ ¡ ¡7 ¡ ¡ ¡8 ¡ ¡ ¡9 ¡ ¡10 ¡ ¡11 ¡ ¡12 ¡ ¡13 ¡ ¡ ¡14 ¡ A ¡= ¡1,4,8,11 ¡

Ac=vity ¡selec=on ¡ ¡

  • Are ¡there ¡other ¡ways ¡to ¡do ¡it? ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡sure... ¡

Greedy ¡works ¡for ¡Ac=vity ¡Selec=on ¡

  • BASE: ¡Op=mal ¡solu=on ¡contains ¡ac=vity ¡1 ¡as ¡first ¡

ac=vity ¡

  • Let ¡A ¡be ¡an ¡op=mal ¡solu=on ¡with ¡ac=vity ¡k ¡!= ¡1 ¡

as ¡first ¡ac=vity ¡

  • Then ¡we ¡can ¡replace ¡ac=vity ¡k ¡(which ¡has ¡Fk>=F1) ¡

by ¡ac=vity ¡1 ¡

  • So, ¡picking ¡the ¡first ¡element ¡in ¡a ¡greedy ¡fashion ¡

works ¡

slide-4
SLIDE 4

7/20/10 ¡ 4 ¡

Greedy ¡works ¡for ¡Ac=vity ¡Selec=on ¡

  • STEP: ¡ ¡A\er ¡the ¡first ¡choice ¡is ¡made, ¡remove ¡ ¡

all ¡ac=vi=es ¡that ¡are ¡incompa=ble ¡with ¡the ¡first ¡ ¡ chosen ¡ac=vity ¡and ¡recursively ¡define ¡a ¡new ¡ ¡ problem ¡consis=ng ¡of ¡the ¡remaining ¡ac=vi=es. ¡ ¡

  • The ¡first ¡ac=vity ¡for ¡this ¡reduced ¡problem ¡can ¡ ¡

be ¡made ¡in ¡a ¡greedy ¡fashion ¡by ¡principle ¡1. ¡ ¡

  • By ¡induc=on, ¡Greedy ¡is ¡op=mal. ¡

What ¡did ¡we ¡do? ¡

  • We ¡assumed ¡there ¡was ¡a ¡non ¡greedy ¡op=mal ¡

solu=on, ¡ ¡

  • then ¡we ¡stepwise ¡morphed ¡this ¡solu=on ¡in ¡a ¡

greedy ¡op=mal ¡solu=on, ¡

  • thereby ¡showing ¡that ¡the ¡greedy ¡solu=on ¡

works ¡in ¡the ¡first ¡place. ¡

MST: ¡Minimal ¡Spanning ¡Tree ¡

Given ¡a ¡connected ¡and ¡undirected ¡graph ¡with ¡ ¡ labeled ¡edges ¡(label ¡= ¡distance), ¡find ¡a ¡tree ¡that ¡ ¡

– is ¡a ¡sub-­‑graph ¡of ¡the ¡given ¡graph ¡(has ¡nodes ¡and ¡ edges ¡from ¡the ¡given ¡graph) ¡ ¡ – and ¡is ¡a ¡minimal ¡spanning ¡tree: ¡it ¡reaches ¡each ¡ node, ¡such ¡that ¡the ¡sum ¡of ¡the ¡edge ¡labels ¡is ¡ minimal ¡(MST). ¡ 12 ¡ 3 ¡ 10 ¡ 11 ¡ 7 ¡ 4 ¡ 8 ¡ 6 ¡ 15 ¡

slide-5
SLIDE 5

7/20/10 ¡ 5 ¡

12 ¡ 3 ¡ 10 ¡ 11 ¡ 7 ¡ 4 ¡ 8 ¡ 6 ¡ Greedy ¡solu=on ¡for ¡MST? ¡ 15 ¡ 12 ¡ 3 ¡ 10 ¡ 11 ¡ 7 ¡ 4 ¡ 8 ¡ 6 ¡ Pick ¡a ¡node ¡and ¡a ¡minimal ¡edge ¡emana=ng ¡from ¡it, ¡now ¡ ¡ we ¡have ¡a ¡MST ¡in ¡the ¡making. ¡ Keep ¡adding ¡minimal ¡edges ¡to ¡the ¡MST ¡un=l ¡connected. ¡ 15 ¡ 12 ¡ 3 ¡ 10 ¡ 11 ¡ 7 ¡ 4 ¡ 8 ¡ 6 ¡ 15 ¡ 12 ¡ 3 ¡ 10 ¡ 11 ¡ 7 ¡ 4 ¡ 8 ¡ 6 ¡ 15 ¡

slide-6
SLIDE 6

7/20/10 ¡ 6 ¡

12 ¡ 3 ¡ 10 ¡ 11 ¡ 7 ¡ 4 ¡ 8 ¡ 6 ¡ 15 ¡ 12 ¡ 3 ¡ 10 ¡ 11 ¡ 7 ¡ 4 ¡ 8 ¡ 6 ¡ 15 ¡ 12 ¡ 3 ¡ 10 ¡ 11 ¡ 7 ¡ 4 ¡ 8 ¡ 6 ¡ 15 ¡ 12 ¡ 3 ¡ 10 ¡ 11 ¡ 7 ¡ 4 ¡ 8 ¡ 6 ¡ 15 ¡

slide-7
SLIDE 7

7/20/10 ¡ 7 ¡

Greedy ¡works ¡for ¡MST ¡

  • Lemma ¡1 ¡

¡Let ¡G ¡be ¡connected ¡and ¡undirected ¡graph ¡(V,E) ¡ ¡and ¡S ¡be ¡a ¡spanning ¡tree ¡S ¡= ¡(V,T) ¡of ¡G, ¡then ¡ forall ¡V1,V2 ¡ ¡in ¡S ¡the ¡path ¡from ¡V1 ¡to ¡V2 ¡is ¡unique ¡ ¡ ¡ ¡ ¡ ¡why? ¡

Greedy ¡works ¡for ¡MST ¡

  • Lemma ¡1 ¡

¡Let ¡G ¡be ¡connected ¡and ¡undirected ¡graph ¡(V,E) ¡ ¡and ¡S ¡be ¡a ¡spanning ¡tree ¡S ¡= ¡(V,T) ¡of ¡G, ¡then ¡ for ¡all ¡V1,V2 ¡ ¡in ¡S ¡the ¡path ¡from ¡V1 ¡to ¡V2 ¡is ¡unique. ¡ ¡ ¡ ¡ ¡ ¡otherwise ¡it ¡wouldn't ¡be ¡a ¡tree ¡

Greedy ¡works ¡for ¡MST ¡

  • Lemma ¡2 ¡

¡Let ¡G ¡be ¡connected ¡and ¡undirected ¡graph ¡(V,E) ¡ ¡and ¡S ¡be ¡a ¡spanning ¡tree ¡S ¡= ¡(V,T) ¡of ¡G, ¡then ¡ if ¡any ¡edge ¡in ¡E-­‑T ¡is ¡added ¡to ¡S, ¡a ¡unique ¡cycle ¡ ¡

  • results. ¡

¡ ¡ ¡ ¡why? ¡

Greedy ¡works ¡for ¡MST ¡

  • Lemma ¡2 ¡

¡Let ¡G ¡be ¡connected ¡and ¡undirected ¡graph ¡(V,E) ¡ ¡and ¡S ¡be ¡a ¡spanning ¡tree ¡S ¡= ¡(V,T) ¡of ¡G, ¡then ¡ if ¡any ¡edge ¡in ¡E-­‑T ¡is ¡added ¡to ¡S, ¡a ¡unique ¡cycle ¡ ¡

  • results. ¡

¡ ¡ ¡ ¡because ¡there ¡already ¡is ¡a ¡unique ¡path ¡ between ¡the ¡endpoints ¡of ¡the ¡added ¡edge ¡

slide-8
SLIDE 8

7/20/10 ¡ 8 ¡

Greedy ¡works ¡for ¡MST ¡

  • Lemma ¡2 ¡

¡Let ¡G ¡be ¡connected ¡and ¡undirected ¡graph ¡(V,E) ¡ ¡and ¡S ¡be ¡a ¡spanning ¡tree ¡S ¡= ¡(V,T) ¡of ¡G ¡then ¡ also, ¡any ¡edge ¡on ¡the ¡cycle ¡can ¡be ¡taken ¡away, ¡ ¡ making ¡the ¡graph ¡a ¡spanning ¡tree ¡again. ¡

Greedy ¡works ¡for ¡MST ¡

Proof ¡by ¡contradic=on: ¡ Suppose ¡we ¡can ¡create ¡an ¡MST ¡by ¡at ¡some ¡stage ¡not ¡ taking ¡the ¡minimal ¡cost ¡edge ¡min, ¡but ¡a ¡non-­‑minimal ¡ ¡ edge ¡other. ¡We ¡build ¡the ¡rest ¡of ¡the ¡spanning ¡tree, ¡so ¡ ¡ now ¡all ¡ver=ces ¡are ¡connected. ¡ We ¡can ¡now ¡make ¡a ¡lower ¡cost ¡spanning ¡tree ¡by ¡ ¡ removing ¡other ¡and ¡adding ¡min. ¡ Hence ¡the ¡spanning ¡tree ¡with ¡other ¡in ¡it ¡was ¡not ¡ ¡

  • minimal. ¡

Bounds ¡for ¡MST ¡

  • MST ¡ ¡= ¡ ¡Ω(|V|) ¡ ¡(we ¡need ¡to ¡touch ¡all ¡nodes) ¡
  • Greedy ¡with ¡priority ¡heap ¡ ¡for ¡nodes ¡is ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡O(|E| ¡lg|V|) ¡ ¡ ¡

– See ¡lecture ¡on ¡Shortest ¡Paths ¡

  • There ¡is ¡no ¡known ¡ ¡O(n) ¡algorithm ¡for ¡MST ¡

– MST ¡has ¡algorithmic ¡gap ¡

Huffman ¡codes ¡

  • Say ¡I ¡have ¡a ¡code ¡consis=ng ¡of ¡the ¡lemers ¡

¡ ¡ ¡ ¡ ¡a, ¡ ¡ ¡b, ¡ ¡ ¡ ¡c, ¡ ¡ ¡d, ¡e, ¡ ¡f ¡ ¡ ¡ ¡ ¡with ¡frequencies ¡(x1000) ¡ ¡ ¡ ¡ ¡45, ¡13, ¡12, ¡16, ¡9, ¡5 ¡ ¡

  • What ¡would ¡a ¡fixed ¡encoding ¡look ¡like? ¡
slide-9
SLIDE 9

7/20/10 ¡ 9 ¡

Huffman ¡codes ¡

  • Say ¡I ¡have ¡a ¡code ¡consis=ng ¡of ¡the ¡lemers ¡

¡ ¡ ¡ ¡ ¡a, ¡ ¡ ¡b, ¡ ¡ ¡ ¡c, ¡ ¡ ¡d, ¡e, ¡ ¡f ¡ ¡ ¡ ¡ ¡ ¡ ¡with ¡frequencies(x1000) ¡ ¡ ¡ ¡ ¡45, ¡13, ¡12, ¡16, ¡9, ¡5 ¡ ¡

  • What ¡would ¡a ¡fixed ¡bit ¡encoding ¡look ¡like? ¡

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

Variable ¡encoding ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a ¡ ¡ ¡ ¡b ¡ ¡ ¡ ¡ ¡c ¡ ¡ ¡ ¡ ¡ ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡e ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ frequency(x1000) ¡ ¡ ¡ ¡45 ¡ ¡13 ¡ ¡ ¡12 ¡ ¡ ¡ ¡16 ¡ ¡ ¡ ¡ ¡ ¡ ¡9 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡5 ¡ fixed ¡encoding ¡ ¡ ¡ ¡ ¡ ¡000 ¡ ¡001 ¡010 ¡ ¡011 ¡ ¡100 ¡ ¡ ¡ ¡ ¡101 ¡ variable ¡encoding ¡ ¡ ¡ ¡0 ¡ ¡101 ¡ ¡100 ¡ ¡111 ¡1101 ¡1100 ¡ ¡

Fixed ¡vs ¡variable ¡

  • 100,000 ¡characters ¡
  • Fixed: ¡

Fixed ¡vs ¡variable ¡

  • 100,000 ¡characters ¡
  • Fixed: ¡ ¡300,000 ¡bits ¡
  • Variable: ¡ ¡
slide-10
SLIDE 10

7/20/10 ¡ 10 ¡

Fixed ¡vs ¡variable ¡

  • 100,000 ¡characters ¡
  • Fixed: ¡ ¡300,000 ¡bits ¡
  • Variable: ¡ ¡

¡ ¡ ¡ ¡(1*45 ¡+ ¡3*13 ¡+ ¡3*12 ¡+ ¡3*16 ¡+ ¡4*9 ¡+ ¡4*5)*1000 ¡= ¡ 224,000 ¡bits ¡ ¡ ¡ ¡ ¡25% ¡saving ¡

Variable ¡prefix ¡encoding ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a ¡ ¡ ¡ ¡b ¡ ¡ ¡ ¡ ¡c ¡ ¡ ¡ ¡ ¡ ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡e ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ frequency(x1000) ¡ ¡ ¡ ¡45 ¡ ¡13 ¡ ¡ ¡12 ¡ ¡ ¡16 ¡ ¡ ¡ ¡ ¡ ¡ ¡9 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡5 ¡ variable ¡encoding ¡ ¡ ¡ ¡0 ¡101 ¡ ¡100 ¡ ¡111 ¡1101 ¡1100 ¡ ¡ what ¡is ¡special ¡about ¡our ¡encoding? ¡

Variable ¡prefix ¡encoding ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a ¡ ¡ ¡ ¡b ¡ ¡ ¡ ¡ ¡c ¡ ¡ ¡ ¡ ¡ ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡e ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ frequency(x1000) ¡ ¡ ¡ ¡45 ¡ ¡13 ¡ ¡ ¡12 ¡ ¡ ¡16 ¡ ¡ ¡ ¡ ¡ ¡ ¡9 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡5 ¡ variable ¡encoding ¡ ¡ ¡ ¡0 ¡101 ¡ ¡100 ¡ ¡111 ¡1101 ¡1100 ¡ ¡ no ¡code ¡is ¡a ¡prefix ¡of ¡another. ¡ why ¡does ¡it ¡mamer? ¡

Variable ¡prefix ¡encoding ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a ¡ ¡ ¡ ¡b ¡ ¡ ¡ ¡ ¡c ¡ ¡ ¡ ¡ ¡ ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡e ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ frequency(x1000) ¡ ¡ ¡ ¡45 ¡ ¡13 ¡ ¡ ¡12 ¡ ¡ ¡16 ¡ ¡ ¡ ¡ ¡ ¡ ¡9 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡5 ¡ variable ¡encoding ¡ ¡ ¡ ¡0 ¡101 ¡ ¡100 ¡ ¡111 ¡1101 ¡1100 ¡ ¡ no ¡code ¡is ¡a ¡prefix ¡of ¡another. ¡ We ¡can ¡concatenate ¡the ¡codes ¡ ¡ ¡ ¡ ¡ ¡ ¡without ¡ambigui=es ¡

slide-11
SLIDE 11

7/20/10 ¡ 11 ¡

Variable ¡prefix ¡encoding ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a ¡ ¡ ¡ ¡b ¡ ¡ ¡ ¡ ¡c ¡ ¡ ¡ ¡ ¡ ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡e ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ frequency(x1000) ¡ ¡ ¡ ¡45 ¡ ¡13 ¡ ¡ ¡12 ¡ ¡ ¡16 ¡ ¡ ¡ ¡ ¡ ¡ ¡9 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡5 ¡ variable ¡encoding ¡ ¡ ¡ ¡0 ¡101 ¡ ¡100 ¡ ¡111 ¡1101 ¡1100 ¡ ¡ ¡ ¡ ¡ ¡0101100 ¡ ¡= ¡ 001011101 ¡= ¡ ¡ ¡ ¡

Represen=ng ¡an ¡encoding ¡

¡A ¡binary ¡tree, ¡where ¡ ¡ ¡ ¡ ¡the ¡intermediate ¡nodes ¡contain ¡frequencies, ¡ and ¡the ¡leaves ¡are ¡the ¡characters ¡(+their ¡ frequencies) ¡ ¡ ¡ ¡ ¡ ¡and ¡the ¡paths ¡to ¡the ¡leaves ¡are ¡the ¡codes, ¡ ¡ is ¡nice. ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡100 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a:45 ¡ ¡ ¡ ¡ ¡55 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡ ¡ ¡ ¡ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡25 ¡ ¡ ¡ ¡ ¡ ¡30 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡\1 ¡ ¡ ¡0/ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡c:12 ¡b:13 ¡14 ¡ ¡d:16 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡ ¡ ¡ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f:5 ¡ ¡ ¡ ¡e:9 ¡

THE ¡FREQUENCIES ¡OF ¡THE ¡

INTERNAL ¡NODES ¡ARE ¡THE ¡ SUMS ¡OF ¡THE ¡FREQUENCIES ¡ OF ¡THEIR ¡CHILDREN. ¡ ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡100 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a:45 ¡ ¡ ¡ ¡ ¡55 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡ ¡ ¡ ¡ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡25 ¡ ¡ ¡ ¡ ¡ ¡30 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡\1 ¡ ¡ ¡0/ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡c:12 ¡b:13 ¡14 ¡ ¡d:16 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡ ¡ ¡ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f:5 ¡ ¡ ¡ ¡e:9 ¡

THE ¡FREQUENCIES ¡OF ¡THE ¡

INTERNAL ¡NODES ¡ARE ¡THE ¡ SUMS ¡OF ¡THE ¡FREQUENCIES ¡ OF ¡THEIR ¡CHILDREN. ¡ ¡ ¡

IF ¡THE ¡TREE ¡IS ¡NOT ¡FULL, ¡THE ¡

ENCODING ¡IS ¡NON ¡OPTIMAL. ¡

WHY? ¡

slide-12
SLIDE 12

7/20/10 ¡ 12 ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡100 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a:45 ¡ ¡ ¡ ¡ ¡55 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡ ¡ ¡ ¡ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡25 ¡ ¡ ¡ ¡ ¡ ¡30 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡\1 ¡ ¡ ¡0/ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡c:12 ¡b:13 ¡14 ¡ ¡d:16 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡ ¡ ¡ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f:5 ¡ ¡ ¡ ¡e:9 ¡

AN ¡OPTIMAL ¡CODE ¡IS ¡

REPRESENTED ¡BY ¡A ¡FULL ¡ BINARY ¡TREE, ¡WHERE ¡EACH ¡ INTERNAL ¡NODE ¡HAS ¡TWO ¡

  • CHILDREN. ¡

If ¡a ¡tree ¡is ¡not ¡full ¡it ¡has ¡ an ¡internal ¡node ¡with ¡

  • ne ¡child ¡labeled ¡with ¡a ¡

redundant ¡bit. ¡

(check ¡the ¡fixed ¡encoding) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡100 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡86 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡14 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡ ¡ ¡ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡| ¡ ¡ ¡ ¡ ¡ ¡ ¡58 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡28 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡14 ¡ ¡ ¡ ¡0/ ¡ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡0/ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡\1 ¡ ¡ ¡ ¡/ ¡ ¡ ¡ ¡ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡ ¡ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡ ¡ ¡ ¡\ ¡

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

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡100 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡86 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡14 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡ ¡ ¡ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡ ¡ ¡redundant ¡0 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡| ¡ ¡ ¡ ¡ ¡ ¡ ¡58 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡28 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡14 ¡ ¡ ¡ ¡0/ ¡ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡0/ ¡ ¡\1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0/ ¡ ¡\1 ¡ ¡ ¡ ¡/ ¡ ¡ ¡ ¡ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡ ¡ ¡ ¡\ ¡

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

Cost ¡of ¡encoding ¡a ¡file ¡

For ¡each ¡character ¡c ¡in ¡C, ¡f(c) ¡is ¡its ¡frequency ¡ ¡ and ¡d(c) ¡is ¡its ¡depth ¡in ¡the ¡tree, ¡which ¡equals ¡the ¡ ¡ number ¡of ¡bits ¡it ¡takes ¡to ¡encode ¡c. ¡ ¡ Then ¡the ¡cost ¡of ¡the ¡encoding ¡is ¡the ¡number ¡of ¡bits ¡ ¡ to ¡encode ¡the ¡file, ¡which ¡is ¡ ¡

f (c)d(c)

c in C

slide-13
SLIDE 13

7/20/10 ¡ 13 ¡

Huffman ¡code ¡

  • An ¡op=mal ¡encoding ¡of ¡a ¡file ¡has ¡a ¡minimal ¡
  • cost. ¡ ¡
  • Huffman ¡invented ¡a ¡greedy ¡algorithm ¡to ¡ ¡

¡ ¡ ¡ ¡construct ¡an ¡op=mal ¡prefix ¡code ¡called ¡the ¡ ¡ ¡ ¡ ¡Huffman ¡code. ¡ ¡

Huffman ¡algorithm ¡

  • Create ¡|C| ¡leaves, ¡one ¡for ¡each ¡character ¡
  • Perform ¡ ¡|C|-­‑1 ¡merge ¡opera=ons, ¡each ¡

crea=ng ¡a ¡new ¡node, ¡with ¡children ¡the ¡nodes ¡ with ¡least ¡two ¡frequencies ¡and ¡with ¡ frequency ¡the ¡sum ¡of ¡these ¡two ¡frequencies. ¡ ¡

  • By ¡using ¡a ¡heap ¡for ¡the ¡collec=on ¡of ¡ ¡

intermediate ¡trees ¡this ¡algorithm ¡takes ¡ ¡ ¡ ¡ ¡ ¡O(nlgn) ¡=me. ¡

1) ¡ ¡ ¡f:5 ¡ ¡e:9 ¡ ¡c:12 ¡ ¡b:13 ¡ ¡d:16 ¡a:45 ¡ 1) ¡ ¡ ¡f:5 ¡ ¡e:9 ¡ ¡c:12 ¡ ¡b:13 ¡ ¡d:16 ¡a:45 ¡ 2) ¡ ¡ ¡c:12 ¡b:13 ¡ ¡ ¡14 ¡ ¡ ¡ ¡d:16 ¡a:45 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ ¡ ¡ ¡e ¡

slide-14
SLIDE 14

7/20/10 ¡ 14 ¡

1) ¡ ¡ ¡f:5 ¡ ¡e:9 ¡ ¡c:12 ¡ ¡b:13 ¡ ¡d:16 ¡a:45 ¡ 2) ¡ ¡ ¡c:12 ¡b:13 ¡ ¡ ¡14 ¡ ¡ ¡ ¡d:16 ¡a:45 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ ¡ ¡ ¡e ¡ 3) ¡ ¡ ¡14 ¡ ¡ ¡ ¡ ¡ ¡d:16 ¡ ¡ ¡ ¡ ¡ ¡ ¡25 ¡ ¡ ¡a:45 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ ¡ ¡ ¡e ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡c ¡ ¡ ¡ ¡b ¡ 1) ¡ ¡ ¡f:5 ¡ ¡e:9 ¡ ¡c:12 ¡ ¡b:13 ¡ ¡d:16 ¡a:45 ¡ 2) ¡ ¡ ¡c:12 ¡b:13 ¡ ¡ ¡14 ¡ ¡ ¡ ¡d:16 ¡a:45 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ ¡ ¡ ¡e ¡ 3) ¡ ¡ ¡ ¡ ¡14 ¡ ¡ ¡ ¡d:16 ¡ ¡ ¡25 ¡ ¡ ¡a:45 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ ¡ ¡ ¡e ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡c ¡ ¡ ¡ ¡b ¡ 4) ¡ ¡ ¡25 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡30 ¡ ¡ ¡ ¡ ¡ ¡a:45 ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡c ¡ ¡ ¡b ¡ ¡ ¡ ¡14 ¡ ¡ ¡ ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ ¡ ¡ ¡e ¡ 1) ¡ ¡ ¡f:5 ¡ ¡e:9 ¡ ¡c:12 ¡ ¡b:13 ¡ ¡d:16 ¡a:45 ¡ 2) ¡ ¡ ¡c:12 ¡b:13 ¡ ¡ ¡14 ¡ ¡ ¡ ¡d:16 ¡a:45 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ ¡ ¡ ¡e ¡ 3) ¡ ¡ ¡ ¡ ¡14 ¡ ¡ ¡ ¡d:16 ¡ ¡ ¡25 ¡ ¡ ¡a:45 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ ¡ ¡ ¡e ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡c ¡ ¡ ¡ ¡b ¡ 4) ¡ ¡ ¡25 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡30 ¡ ¡ ¡ ¡ ¡ ¡a:45 ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡c ¡ ¡ ¡b ¡ ¡ ¡ ¡14 ¡ ¡ ¡ ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ ¡ ¡ ¡e ¡ 5) ¡ ¡a:45 ¡ ¡ ¡ ¡55 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡25 ¡ ¡ ¡ ¡ ¡30 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡c ¡ ¡ ¡b ¡14 ¡ ¡ ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ ¡ ¡ ¡e ¡ 1) ¡ ¡ ¡f:5 ¡ ¡e:9 ¡ ¡c:12 ¡ ¡b:13 ¡ ¡d:16 ¡a:45 ¡ 2) ¡ ¡ ¡c:12 ¡b:13 ¡ ¡ ¡14 ¡ ¡ ¡ ¡d:16 ¡a:45 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ ¡ ¡ ¡e ¡ 3) ¡ ¡ ¡ ¡ ¡14 ¡ ¡ ¡ ¡ ¡ ¡ ¡d:16 ¡ ¡ ¡25 ¡ ¡ ¡a:45 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ ¡ ¡ ¡e ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡c ¡ ¡ ¡ ¡b ¡ 4) ¡ ¡ ¡25 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡30 ¡ ¡ ¡ ¡ ¡ ¡a:45 ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡c ¡ ¡ ¡b ¡ ¡ ¡ ¡14 ¡ ¡ ¡ ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ ¡ ¡ ¡e ¡ 5) ¡ ¡a:45 ¡ ¡ ¡ ¡55 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡25 ¡ ¡ ¡ ¡ ¡30 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡c ¡ ¡ ¡b ¡14 ¡ ¡ ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ ¡ ¡ ¡e ¡ 6) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡100 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a ¡ ¡ ¡55 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡25 ¡ ¡ ¡ ¡30 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡c ¡ ¡ ¡b ¡14 ¡ ¡ ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f ¡ ¡ ¡ ¡e ¡

slide-15
SLIDE 15

7/20/10 ¡ 15 ¡

Huffman ¡is ¡op=mal ¡

  • Base ¡step ¡induc=ve ¡approach. ¡
  • First ¡we ¡show: ¡

¡ ¡ ¡ ¡Let ¡x ¡and ¡y ¡be ¡the ¡two ¡characters ¡with ¡the ¡ minimal ¡frequencies, ¡then ¡there ¡is ¡a ¡minimal ¡ cost ¡encoding ¡tree ¡with ¡x ¡and ¡y ¡of ¡equal ¡and ¡ highest ¡depth ¡(see ¡e ¡and ¡f ¡in ¡our ¡example ¡ above). ¡ How? ¡

Huffman ¡is ¡op=mal ¡

  • Base ¡step ¡induc=ve ¡approach. ¡
  • First ¡we ¡show: ¡

¡ ¡ ¡ ¡Let ¡x ¡and ¡y ¡be ¡the ¡two ¡characters ¡with ¡the ¡ minimal ¡frequencies, ¡then ¡there ¡is ¡a ¡minimal ¡ cost ¡encoding ¡tree ¡with ¡x ¡and ¡y ¡of ¡equal ¡and ¡ highest ¡depth ¡(see ¡e ¡and ¡f ¡in ¡our ¡example ¡ above). ¡ How? ¡

Greedy ¡proof ¡technique ¡

The ¡proof ¡technique ¡is ¡the ¡same ¡as ¡we ¡have ¡ ¡ used ¡for ¡the ¡previous ¡two ¡problems ¡(ac=vi=es ¡ ¡ and ¡MST): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡If ¡the ¡greedy ¡choice ¡is ¡not ¡taken ¡then ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡we ¡show ¡that ¡by ¡taking ¡the ¡greedy ¡choice ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡we ¡get ¡a ¡solu3on ¡that ¡is ¡as ¡good ¡or ¡be]er. ¡

Lowest ¡leaves ¡x,y ¡lowest ¡frequencies ¡

Assume ¡that ¡two ¡other ¡characters ¡a ¡and ¡b ¡with ¡ ¡ higher ¡frequencies ¡are ¡siblings ¡at ¡the ¡lowest ¡ ¡ level ¡of ¡the ¡tree: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡T ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡O ¡ ¡ ¡ ¡ ¡x ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡y ¡ ¡ ¡O ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a ¡ ¡ ¡b ¡

slide-16
SLIDE 16

7/20/10 ¡ 16 ¡

Since ¡the ¡frequencies ¡of ¡x ¡and ¡y ¡are ¡lowest, ¡the ¡ ¡ cost ¡of ¡the ¡tree ¡can ¡only ¡improve ¡if ¡we ¡swap ¡y ¡ ¡ and ¡a, ¡and ¡x ¡and ¡b: ¡ ¡ ¡ ¡T ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡O ¡ ¡ ¡ ¡ ¡b ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a ¡ ¡ ¡O ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡y ¡ ¡ ¡x ¡ why? ¡ Since ¡the ¡frequencies ¡of ¡x ¡and ¡y ¡are ¡lowest, ¡the ¡ ¡ cost ¡of ¡the ¡tree ¡can ¡only ¡improve ¡if ¡we ¡swap ¡y ¡ ¡ and ¡a, ¡and ¡x ¡and ¡b: ¡ ¡ ¡ ¡T ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡ ¡ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡O ¡ ¡ ¡ ¡ ¡b ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a ¡ ¡ ¡O ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡/ ¡\ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡y ¡ ¡ ¡x ¡ what ¡is ¡the ¡cost ¡of ¡an ¡encoding ¡tree? ¡

Greedy ¡start ¡ ¡

We ¡have ¡shown ¡that ¡puxng ¡the ¡lowest ¡two ¡ ¡ frequency ¡characters ¡lowest ¡in ¡the ¡tree ¡is ¡a ¡good ¡ ¡ greedy ¡star=ng ¡point ¡for ¡our ¡algorithm: ¡ ¡ base ¡of ¡the ¡induc3on. ¡ ¡

Step ¡

If ¡we ¡have ¡an ¡alphabet ¡C' ¡= ¡C ¡with ¡x ¡and ¡y ¡ ¡ replaced ¡by ¡a ¡new ¡character ¡z ¡with ¡frequency ¡ ¡ f(z)=f(x)+f(y) ¡with ¡an ¡op3mal ¡encoding ¡tree ¡T' ¡

(eg, ¡the ¡tree ¡created ¡from ¡steps ¡2 ¡to ¡6 ¡in ¡the ¡example) ¡

then ¡we ¡need ¡to ¡show ¡that ¡the ¡tree ¡T ¡with ¡leaf ¡z ¡ ¡ replaced ¡by ¡an ¡internal ¡node ¡f(z) ¡with ¡children ¡ x:f(x) ¡and ¡y:f(y)) ¡is ¡an ¡op3mal ¡encoding ¡for ¡C ¡

(the ¡tree ¡created ¡form ¡steps ¡1 ¡to ¡6 ¡in ¡the ¡example). ¡

slide-17
SLIDE 17

7/20/10 ¡ 17 ¡

Proof ¡of ¡step: ¡by ¡contradic=on ¡

d(x)=d(y)=d(z)+1 ¡ ¡so ¡ f(x)d(x)+f(y)d(y) ¡= ¡(f(x)+f(y))(d(z)+1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡f(z)d(z)+f(x)+f(y) ¡ because ¡f(z)=f(x)+f(y) ¡ So ¡cost(T) ¡= ¡cost(T')+f(x)+f(y) ¡ ¡

  • Now ¡suppose ¡T ¡is ¡not ¡an ¡op=mal ¡encoding, ¡then ¡there ¡is ¡ ¡

another ¡op=mal ¡tree ¡T''. ¡ ¡

  • We ¡have ¡shown ¡that ¡we ¡can ¡put ¡x ¡and ¡y ¡as ¡siblings ¡at ¡

the ¡lowest ¡level ¡of ¡T''. ¡ ¡

  • Let ¡T''' ¡be ¡T'' ¡with ¡x ¡and ¡y ¡replaced ¡by ¡z, ¡then ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

cost(T''') ¡= ¡cost(T'')-­‑f(x)-­‑f(y) ¡< ¡cost(T)-­‑f(x)-­‑f(y) ¡= ¡cost(T'). ¡

  • But ¡that ¡yields ¡a ¡contradic=on ¡with ¡the ¡assump=on ¡that ¡

T' ¡was ¡op=mal ¡for ¡C'. ¡ Hence ¡Huffman ¡(ie ¡Greedy) ¡produces ¡an ¡op=mal ¡ ¡prefix ¡ ¡ encoding ¡tree. ¡ ¡

conclusion ¡

All ¡proofs ¡that ¡a ¡greedy ¡method ¡works ¡are ¡based ¡on ¡the ¡ ¡ same ¡principle. ¡We ¡show ¡that ¡taking ¡the ¡greedy ¡choice ¡ ¡ first ¡and ¡then ¡recursively ¡solving ¡the ¡reduced ¡problem ¡ ¡ is ¡op=mal, ¡because ¡ ¡ ¡ ¡ ¡either ¡we ¡can ¡change ¡an ¡op=mal ¡solu=on ¡without ¡ ¡ ¡ ¡the ¡greedy ¡first ¡choice ¡to ¡one ¡with ¡it, ¡ ¡ ¡ ¡or ¡we ¡can ¡show ¡that ¡an ¡op=mal ¡solu=on ¡must ¡have ¡ ¡ ¡ ¡ ¡the ¡greedy ¡first ¡choice. ¡