Lossless compression B 0 U B 1 U 0 1 B 2 0 1 1 0 - - PowerPoint PPT Presentation

lossless compression
SMART_READER_LITE
LIVE PREVIEW

Lossless compression B 0 U B 1 U 0 1 B 2 0 1 1 0 - - PowerPoint PPT Presentation

Lossless compression B 0 U B 1 U 0 1 B 2 0 1 1 0 A B 0 1 0 1 U ! C R D CSCI 470: Web Science Keith Vertanen


slide-1
SLIDE 1

CSCI ¡470: ¡Web ¡Science ¡ ¡• ¡ ¡Keith ¡Vertanen ¡

Lossless ¡compression ¡

B0 ¡ U ¡ B1 ¡ U ¡ B2 ¡ U ¡ ϵ ¡ … ¡ B ¡ C ¡ R ¡ ! ¡ D ¡ A ¡ 1 1 1 1 1

slide-2
SLIDE 2

Overview ¡

  • Lossless ¡compression ¡

– Mo7va7on ¡ – Rules ¡and ¡limits ¡of ¡the ¡game ¡ – Things ¡to ¡exploit ¡

  • Run-­‑length ¡encoding ¡(RLE) ¡

– Exploit ¡runs ¡of ¡same ¡character ¡

  • Huffman ¡coding ¡

– Variable-­‑length ¡codeword ¡for ¡each ¡paJern ¡ (character) ¡ – Transmit ¡codewords ¡plus ¡compressed ¡data ¡

2 ¡

Sec6on ¡5.5 ¡

slide-3
SLIDE 3

Mo7va7on ¡

  • Lossless ¡compression ¡

– Reduce ¡size ¡of ¡a ¡file ¡ – Save ¡space ¡while ¡storing ¡it ¡

  • Data ¡always ¡expands ¡to ¡fill ¡available ¡drive ¡space ¡

– Save ¡space ¡while ¡transmiPng ¡it ¡

  • Bandwidth ¡growing ¡rapidly, ¡but ¡so ¡are ¡files! ¡
  • HD ¡video: ¡ ¡

– (1920 ¡* ¡1080) ¡pixels/frame ¡* ¡30 ¡frames/sec ¡* ¡24 ¡bits/pixel ¡= ¡ 1.5Gbps! ¡

– Lossless ¡= ¡get ¡back ¡exactly ¡what ¡you ¡put ¡in ¡(e.g. ¡zip) ¡

  • Lossly ¡compression ¡(stay ¡tuned) ¡

– Informa7on ¡is ¡lost ¡(e.g. ¡JPEG, ¡MP3) ¡

3 ¡

slide-4
SLIDE 4

hJp://www-­‑01.ibm.com/sobware/data/bigdata/ ¡ Name ¡ Value ¡ Million ¡ 106 ¡ megabyte ¡ Billion ¡ 109 ¡ gigabyte ¡ Trillion ¡ 1012 ¡ terabyte ¡ Quadrillion ¡ 1015 ¡ petabyte ¡ Quin7llion ¡ 1018 ¡ exabyte ¡

4 ¡

slide-5
SLIDE 5

Lossless ¡compression: ¡applica7ons ¡

  • Generic ¡file ¡compression ¡

– compress, ¡gzip, ¡zip, ¡bzip2, ¡7z, ¡xz ¡ – NTFS, ¡HFS+, ¡ZFS ¡

  • Image ¡files ¡

– GIF, ¡PNG, ¡TIFF ¡

  • Audio ¡files ¡

– Free ¡Lossless ¡Audio ¡Codec ¡(FLAC) ¡ – Apple ¡Lossless ¡Audio ¡Codec ¡(ALAC) ¡

  • Data ¡transmission ¡

– HTTP, ¡PPP, ¡SSH, ¡fax ¡machines, ¡v.92 ¡modems ¡

5 ¡

slide-6
SLIDE 6

Compression ¡and ¡expansion ¡

6 ¡

Data ¡we ¡want ¡ to ¡be ¡smaller Smaller ¡(hopefully) ¡ version ¡of ¡data Exact ¡version ¡of ¡

  • riginal ¡data

Compression ¡ra8o: ¡ bits ¡in ¡C(B) ¡/ ¡bits ¡in ¡B ¡ ¡ Example: ¡ 17 ¡ASCII ¡characters, ¡7 ¡bits ¡each ¡= ¡119 ¡bits ¡ Output ¡12 ¡codewords, ¡8 ¡bits/codeword ¡= ¡96 ¡bits ¡ Compression ¡ra7o ¡= ¡81% ¡

0101110010010 ¡ 11010110 ¡ 0101110010010 ¡

bitstream ¡B ¡ compressed ¡bitstream ¡C(B) ¡

  • riginal ¡bitstream ¡B ¡

Compress ¡ Expand ¡

slide-7
SLIDE 7

{"created_at":"Mon ¡Apr ¡14 ¡13:54:57 ¡+0000 ¡2014","id":455705773490724864,"id_str":"455705773490724864","text":"RT ¡@fanshu_choy: ¡\u81ea\u5df1\u96e3\u53d7 ¡\u53ea\u6 ¡ 709\u81ea\u5df1\u624d\u61c2\u3002","source":"\u003ca ¡href=\"http:\/\/twitter.com\/download\/iphone\" ¡rel=\"nofollow\"\u003eTwitter ¡for ¡iPhone\u003c\/a\u003e","t ¡ runcated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name ¡ ":null,"user":{"id":962422766,"id_str":"962422766","name":"Chyh ¡Leng","screen_name":"chyhleng","location":"","url":null,"description":"2702 ¡:)Music\u266a ¡Kpop ¡\ ¡ u00a7 ¡Kdrama\uff2c\uff2f\uff36\uff25\uff32\u2605 ¡EverLastingFriend ¡\u2665 ¡Minoz ¡\u2665","protected":false,"followers_count":96,"friends_count":139,"listed_count ¡ ":0,"created_at":"Wed ¡Nov ¡21 ¡13:41:44 ¡+0000 ¡2012","favourites_count":1182,"utc_offset":28800,"time_zone":"Kuala ¡Lumpur","geo_enabled":true,"verified":false,"sta ¡ tuses_count":4263,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"9AE4E8","profile_bac ¡ kground_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme16\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/them ¡ e16\/bg.gif","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/446224572749467648\/362O82YR_normal.jpeg","profile_ima ¡ ge_url_https":"https:\/\/pbs.twimg.com\/profile_images\/446224572749467648\/362O82YR_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners ¡ \/962422766\/1384276094","profile_link_color":"0084B4","profile_sidebar_border_color":"BDDCAD","profile_sidebar_fill_color":"DDFFCC","profile_text_color":"33333 ¡ 3","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null}," ¡ geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Mon ¡Apr ¡14 ¡13:47:11 ¡+0000 ¡2014","id":455703816617222144,"id_str" ¡ :"455703816617222144","text":"\u81ea\u5df1\u96e3\u53d7 ¡\u53ea\u6709\u81ea\u5df1\u624d\u61c2\u3002","source":"\u003ca ¡href=\"http:\/\/twitter.com\/download\/ipho ¡ ne\" ¡rel=\"nofollow\"\u003eTwitter ¡for ¡iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_ ¡ id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":701120072,"id_str":"701120072","name":"\u8521\u7f8e\u5100","screen_name":"fa ¡ nshu_choy","location":"","url":null,"description":"\u2022 ¡People ¡live ¡because ¡of ¡a ¡dream ¡\u2022","protected":false,"followers_count":627,"friends_count":165,"li ¡ sted_count":4,"created_at":"Tue ¡Jul ¡17 ¡14:10:14 ¡+0000 ¡2012","favourites_count":1461,"utc_offset":28800,"time_zone":"Beijing","geo_enabled":true,"verified":false ¡ ,"statuses_count":15213,"lang":"zh-­‑tw","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"131516","pr ¡

  • file_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/378800000066772524\/d72e619255e58c56db7d0e31ac4f11b3.jpeg","profile_background_i ¡

mage_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/378800000066772524\/d72e619255e58c56db7d0e31ac4f11b3.jpeg","profile_background_tile":true," ¡ profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/440752897995730944\/utP0AnlR_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profil ¡ e_images\/440752897995730944\/utP0AnlR_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/701120072\/1389849286","profile_link_color": ¡ "050505","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"E6F6F9","profile_text_color":"333333","profile_use_background_image":true,"defaul ¡ t_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"con ¡ tributors":null,"retweet_count":10,"favorite_count":5,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false," ¡ lang":"zh"},"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"fanshu_choy","name":"\u8521\u ¡ 7f8e\u5100","id":701120072,"id_str":"701120072","indices":[3,15]}]},"favorited":false,"retweeted":false,"filter_level":"medium","lang":"zh"} ¡ {"created_at":"Mon ¡Apr ¡14 ¡13:54:57 ¡+0000 ¡2014","id":455705773499494400,"id_str":"455705773499494400","text":"hallo","source":"web","truncated":false,"in_reply_t ¡

  • _status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1638668 ¡

713,"id_str":"1638668713","name":"geilespuitfles","screen_name":"geilespuitfles","location":"","url":null,"description":null,"protected":false,"followers_count" ¡ :697,"friends_count":1035,"listed_count":2,"created_at":"Thu ¡Aug ¡01 ¡19:08:08 ¡+0000 ¡2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":f ¡ alse,"verified":false,"statuses_count":692,"lang":"nl","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_col ¡

  • r":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.c ¡
  • m\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000457516431\/b5dba4268df ¡

e9417fe14b11f51d74f77_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000457516431\/b5dba4268dfe9417fe14b11f51d74f77_norm ¡ al.jpeg","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_ ¡ background_image":true,"default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordi ¡ nates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorite ¡ d":false,"retweeted":false,"filter_level":"medium","lang":"et"} ¡ {"delete":{"status":{"id":404847417381502976,"user_id":1331701896,"id_str":"404847417381502976","user_id_str":"1331701896"}}} ¡ {"created_at":"Mon ¡Apr ¡14 ¡13:54:57 ¡+0000 ¡2014","id":455705773507502080,"id_str":"455705773507502080","text":"@leeaoilove1 ¡\u3042\u304a\u3044\u3069\u30fc\u3057\u ¡ 305f\uff01","source":"\u003ca ¡href=\"http:\/\/twitter.com\/download\/iphone\" ¡rel=\"nofollow\"\u003eTwitter ¡for ¡iPhone\u003c\/a\u003e","truncated":false,"in_rep ¡ ly_to_status_id":455704001074302976,"in_reply_to_status_id_str":"455704001074302976","in_reply_to_user_id":2286226146,"in_reply_to_user_id_str":"2286226146","in ¡ _reply_to_screen_name":"leeaoilove1","user":{"id":2229970272,"id_str":"2229970272","name":"\u3051\u3044\u30fc\u305f","screen_name":"Keeita_0423","location":""," ¡ url":null,"description":"\u9cf4\u6d77208 ¡\u30c6\u30cb\u30b9\u90e8 ¡\u9cf4\u6d77\u306e\u4eba\u30d5\u30a9\u30ed\u30fc\u3057\u3066\u30fc\uff01","protected":false,"f ¡

  • llowers_count":210,"friends_count":237,"listed_count":0,"created_at":"Wed ¡Dec ¡04 ¡14:04:25 ¡+0000 ¡2013","favourites_count":178,"utc_offset":null,"time_zone":null ¡

,"geo_enabled":true,"verified":false,"statuses_count":3036,"lang":"ja","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profil ¡ e_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https ¡ :\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/450246912361242 ¡ 626\/XG8Azfho_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/450246912361242626\/XG8Azfho_normal.jpeg","profile_banner_url":"h ¡ ttps:\/\/pbs.twimg.com\/profile_banners\/2229970272\/1395118925","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_col ¡

  • r":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":null,"follow_req ¡

uest_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags" ¡ :[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"leeaoilove1","name":"\u3042\u304a\u3074\u301c\u307e\u3093","id":2286226146,"id_str":"2286226146","in ¡ dices":[0,12]}]},"favorited":false,"retweeted":false,"filter_level":"medium","lang":"ja"} ¡

219,743,396 ¡katie30390.txt ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡plaintext ¡ ¡52,583,716 ¡katie30390.txt.Z ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡compress ¡ ¡ ¡ ¡LZW ¡ ¡31,875,422 ¡katie30390.txt.zip ¡ ¡ ¡ ¡ ¡ ¡ ¡zip ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡DEFLATE ¡ ¡23,862,152 ¡katie30390.txt.bz2 ¡ ¡ ¡ ¡ ¡ ¡ ¡bzip2 ¡ ¡ ¡ ¡ ¡ ¡ ¡Burrows-­‑Wheeler ¡ ¡21,814,286 ¡katie30390.txt.lzma2.7z ¡ ¡7-­‑zip ¡ ¡ ¡ ¡ ¡ ¡ ¡LZMA2 ¡ ¡21,579,605 ¡katie30390.txt.7z ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡7-­‑zip ¡ ¡ ¡ ¡ ¡ ¡ ¡LZMA ¡ ¡21,320,608 ¡katie30390.txt.xz ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡xz ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡LZMA2 ¡ ¡21,271,440 ¡katie30390.txt.ppmd.7z ¡ ¡ ¡7-­‑zip ¡ ¡ ¡ ¡ ¡ ¡ ¡PPMd ¡

slide-8
SLIDE 8

"A ¡second ¡aspect ¡of ¡the ¡present ¡inven7on ¡which ¡further ¡enhances ¡its ¡ability ¡to ¡ achieve ¡high ¡compression ¡percentages, ¡is ¡its ¡ability ¡to ¡be ¡applied ¡to ¡data ¡

  • recursively. ¡Specifically, ¡the ¡methods ¡of ¡the ¡present ¡inven7on ¡are ¡able ¡to ¡make ¡

mul7ple ¡passes ¡over ¡a ¡file, ¡each ¡7me ¡further ¡compressing ¡the ¡file. ¡Thus, ¡a ¡series ¡of ¡ recursions ¡are ¡repeated ¡un7l ¡the ¡desired ¡compression ¡level ¡is ¡achieved." ¡ "the ¡direct ¡bit ¡encode ¡method ¡of ¡the ¡present ¡inven7on ¡is ¡effec7ve ¡for ¡reducing ¡an ¡ input ¡string ¡by ¡one ¡bit ¡regardless ¡of ¡the ¡bit ¡paJern ¡of ¡the ¡input ¡string." ¡

8 ¡

slide-9
SLIDE 9

Universal ¡data ¡compression? ¡

  • Reality: ¡No ¡algorithm ¡can ¡compress ¡every ¡bitstream ¡
  • Proof ¡1 ¡(by ¡contradic7on) ¡

– Suppose ¡you ¡have ¡a ¡universal ¡compressor ¡U ¡ – Given ¡bitstream ¡B0, ¡use ¡U ¡to ¡compress ¡to ¡smaller ¡B1 ¡ – Compress ¡B1 ¡to ¡get ¡smaller ¡B2 ¡ – Con7nue ¡un7l ¡bitstream ¡is ¡of ¡size ¡0 ¡ – Thus ¡all ¡bitstream ¡can ¡be ¡compressed ¡to ¡0 ¡bits! ¡

  • Proof ¡2 ¡(coun7ng) ¡

– Suppose ¡you ¡can ¡compress ¡all ¡1000-­‑bit ¡strings ¡ – 21000 ¡possible ¡bit ¡strings ¡with ¡1000 ¡bits ¡ – How ¡many ¡possible ¡shorter ¡encodings, ¡≤ ¡999 ¡bits? ¡

  • (# ¡of ¡1 ¡bit ¡numbers) ¡+ ¡(# ¡of ¡2 ¡bit ¡numbers) ¡+ ¡… ¡+ ¡(# ¡of ¡999 ¡bit ¡numbers) ¡
  • 1 ¡+ ¡2 ¡+ ¡4 ¡+ ¡… ¡+ ¡2999 ¡= ¡21000 ¡-­‑ ¡1 ¡

– Thus ¡fewer ¡than ¡the ¡21000 ¡we ¡need ¡for ¡unique ¡mapping ¡

9 ¡

B0 ¡ U ¡ B1 ¡ U ¡ B2 ¡ U ¡ ϵ ¡ … ¡

slide-10
SLIDE 10

10 ¡

1024 ¡x ¡768 ¡= ¡786,432 ¡bits ¡ ¡

  • Compressed ¡with ¡standard ¡compression ¡u7li7es ¡ ¡
  • My ¡top-­‑secret ¡method! ¡

¡98,304 ¡ ¡bits.bin ¡ ¡98,346 ¡ ¡bits.bin.gz ¡ ¡98,571 ¡ ¡bits.bin.zip ¡ ¡99,080 ¡ ¡bits.bin.bz2 ¡ ¡98,368 ¡ ¡bits.bin.xz ¡ ¡ ¡ ¡ ¡232 ¡ ¡bits.bin.kdv ¡ public ¡class ¡RandomBits ¡ ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡x ¡= ¡1111; ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡< ¡786432; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡x ¡= ¡x ¡* ¡314159 ¡+ ¡218291; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(x ¡> ¡0); ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.close(); ¡ ¡ ¡ ¡} ¡ } ¡

232 ¡* ¡8 ¡/ ¡786,432 ¡ 0.24% ¡of ¡original!

java ¡PictureDump ¡1024 ¡768 ¡< ¡bits.bin ¡

slide-11
SLIDE 11

11 ¡

Another ¡set ¡of ¡1024 ¡x ¡768 ¡= ¡786,432 ¡bits ¡

¡

What ¡is ¡the ¡op7mal ¡compressor ¡for ¡this ¡image? ¡

Undecideable! ¡ ¡ ¡ In ¡fact ¡this ¡image ¡is ¡completely ¡random. ¡

java ¡PictureDump ¡1024 ¡768 ¡< ¡2012-­‑04-­‑22.bin ¡ 1,048,576 ¡2012-­‑04-­‑22.bin ¡ 1,053,488 ¡2012-­‑04-­‑22.bz2 ¡ 1,048,769 ¡2012-­‑04-­‑22.gz ¡ 1,049,000 ¡2012-­‑04-­‑22.zip ¡

slide-12
SLIDE 12

Redundancy ¡in ¡English ¡

  • How ¡redundant ¡is ¡wriJen ¡English? ¡

¡

  • Answer: ¡very! ¡

– Shannon ¡es7mate: ¡0.6 ¡-­‑ ¡1.3 ¡bits ¡per ¡leJer ¡

12 ¡

Yet ¡aoccdrnig ¡to ¡a ¡sudty ¡at ¡Cmabrigde ¡Uinerv7sy, ¡it ¡deosn’t ¡ mJaer ¡in ¡waht ¡oredr ¡the ¡lJeers ¡in ¡a ¡wrod ¡are, ¡the ¡olny ¡ iprmoetnt ¡7hng ¡is ¡taht ¡the ¡frist ¡and ¡lsat ¡lJeer ¡be ¡at ¡the ¡rghit ¡

  • pclae. ¡The ¡rset ¡can ¡be ¡a ¡Joal ¡mses ¡and ¡you ¡can ¡sitll ¡raed ¡it ¡

wouthit ¡a ¡porbelm. ¡Tihs ¡is ¡bcuseae ¡the ¡huamn ¡mnid ¡deos ¡not ¡ raed ¡ervey ¡lteter ¡by ¡istlef, ¡but ¡the ¡wrod ¡as ¡a ¡wlohe. ¡

slide-13
SLIDE 13

Approaches ¡to ¡compression ¡

  • Exploit ¡1+ ¡of ¡the ¡following: ¡

1) Small ¡alphabets ¡ 2) Long ¡sequences ¡of ¡iden7cal ¡bits ¡ 3) Frequently ¡used ¡characters ¡ 4) Long ¡reused ¡bit ¡sequences ¡(next ¡7me) ¡

  • We'll ¡look ¡at ¡an ¡example ¡of ¡each ¡

– Including ¡Java ¡implementa7on ¡ – Using ¡support ¡classes ¡for: ¡

  • Binary ¡file ¡input/output ¡
  • Data ¡structures ¡

13 ¡

slide-14
SLIDE 14

Reading ¡and ¡wri7ng ¡binary ¡data ¡

14 ¡

public ¡class ¡BinaryStdIn ¡

  • ­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑ ¡

boolean ¡readBoolean() ¡ ¡ ¡// ¡Read ¡1 ¡bit ¡of ¡data, ¡return ¡as ¡a ¡boolean ¡value ¡ ¡ ¡ ¡char ¡readChar() ¡ ¡ ¡ ¡ ¡ ¡// ¡Read ¡8 ¡bits ¡of ¡data, ¡return ¡as ¡a ¡char ¡value ¡ ¡ ¡ ¡char ¡readChar(int ¡r) ¡// ¡Read ¡r ¡bits ¡of ¡data, ¡return ¡as ¡a ¡char ¡value ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Read ¡r ¡bits ¡for ¡byte, ¡short, ¡int, ¡long, ¡double ¡ boolean ¡isEmpty() ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Is ¡the ¡bitstream ¡empty? ¡ ¡ ¡ ¡void ¡close() ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Close ¡the ¡bitstream ¡ public ¡class ¡BinaryStdOut ¡

  • ­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑-­‑ ¡

¡ ¡ ¡void ¡write(boolean ¡b) ¡ ¡ ¡ ¡ ¡// ¡Write ¡the ¡specified ¡bit ¡ ¡ ¡ ¡void ¡writeChar(char ¡c) ¡ ¡ ¡ ¡// ¡Write ¡the ¡specified ¡8-­‑bit ¡char ¡ ¡ ¡ ¡void ¡write(char ¡c, ¡int ¡r) ¡// ¡Write ¡r ¡least ¡significant ¡bits ¡of ¡char ¡c ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Write ¡r ¡LSB ¡of ¡byte, ¡short, ¡int, ¡long, ¡double ¡ ¡ ¡ ¡void ¡close() ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Close ¡the ¡bitstream ¡

slide-15
SLIDE 15

% ¡java ¡PictureDump ¡16 ¡6 ¡< ¡abra.txt ¡ ¡ ¡ ¡ ¡ ¡

Visualizing ¡a ¡bitstream ¡

  • How ¡to ¡view ¡a ¡bitstream? ¡

15 ¡

% ¡more ¡abra.txt ¡ ABRACADABRA! ¡

Bitstream ¡as ¡characters ¡

% ¡java ¡BinaryDump ¡16 ¡< ¡abra.txt ¡ 0100000101000010 ¡ 0101001001000001 ¡ 0100001101000001 ¡ 0100010001000001 ¡ 0100001001010010 ¡ 0100000100100001 ¡ 00001010 ¡ 104 ¡bits ¡

Bitstream ¡represented ¡by ¡0 ¡and ¡1's ¡

% ¡java ¡HexDump ¡4 ¡< ¡abra.txt ¡ 41 ¡42 ¡52 ¡41 ¡ 43 ¡41 ¡44 ¡41 ¡ 42 ¡52 ¡41 ¡21 ¡ 0a ¡ 104 ¡bits ¡

Bitstream ¡represented ¡by ¡2-­‑digit ¡hex ¡ numbers ¡ Bitstream ¡as ¡pixels ¡in ¡a ¡picture ¡

slide-16
SLIDE 16

Method ¡1: ¡Small ¡alphabets ¡

16 ¡

public ¡class ¡OutputDate ¡ ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡month ¡= ¡12; ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡day ¡= ¡31; ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡year ¡= ¡1999; ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡mode ¡= ¡Integer.parseInt(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(mode ¡== ¡0) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.print(month+"/"+day+"/"+year); ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡if ¡(mode ¡== ¡1) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(month); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(day); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(year); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.close(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(month, ¡4); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(day, ¡5); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(year, ¡12); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.close(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡} ¡ } ¡

% ¡java ¡OutputDate ¡0 ¡ | ¡java ¡BinaryDump ¡8 ¡ ¡ 00110001 ¡ 00110010 ¡ 00101111 ¡ 00110011 ¡ 00110001 ¡ 00101111 ¡ 00110001 ¡ 00111001 ¡ 00111001 ¡ 00111001 ¡ 80 ¡bits ¡ 1 ¡ 2 ¡ / ¡ 3 ¡ 1 ¡ / ¡ 1 ¡ 9 ¡ 9 ¡ 9 ¡

Mode ¡0: ¡output ¡as ¡ASCII ¡text ¡ Mode ¡1: ¡output ¡four ¡32-­‑bit ¡ints ¡ Mode ¡2: ¡output ¡variable ¡length ¡bit ¡fields ¡

slide-17
SLIDE 17

public ¡class ¡OutputDate ¡ ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡month ¡= ¡12; ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡day ¡= ¡31; ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡year ¡= ¡1999; ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡mode ¡= ¡Integer.parseInt(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(mode ¡== ¡0) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.print(month+"/"+day+"/"+year); ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡if ¡(mode ¡== ¡1) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(month); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(day); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(year); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.close(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(month, ¡4); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(day, ¡5); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(year, ¡12); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.close(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡} ¡ } ¡

Method ¡1: ¡Small ¡alphabets ¡

17 ¡

% ¡java ¡OutputDate ¡1 ¡| ¡java ¡BinaryDump ¡32 ¡ ¡ 00000000000000000000000000001100 ¡ 00000000000000000000000000011111 ¡ 00000000000000000000011111001111 ¡ 96 ¡bits ¡ 12 ¡ 31 ¡ 1999 ¡

Mode ¡0: ¡output ¡as ¡ASCII ¡text ¡ Mode ¡1: ¡output ¡four ¡32-­‑bit ¡ints ¡ Mode ¡2: ¡output ¡variable ¡length ¡bit ¡fields ¡

slide-18
SLIDE 18

public ¡class ¡OutputDate ¡ ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡month ¡= ¡12; ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡day ¡= ¡31; ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡year ¡= ¡1999; ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡mode ¡= ¡Integer.parseInt(args[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(mode ¡== ¡0) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.print(month+"/"+day+"/"+year); ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡if ¡(mode ¡== ¡1) ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(month); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(day); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(year); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.close(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(month, ¡4); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(day, ¡5); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(year, ¡12); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.close(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡} ¡ } ¡

Method ¡1: ¡Small ¡alphabets ¡

18 ¡

% ¡java ¡OutputDate ¡2 ¡| ¡java ¡BinaryDump ¡32 ¡ ¡ 110011111011111001111000 ¡ 24 ¡bits ¡

Padding ¡since ¡we ¡ must ¡end ¡on ¡a ¡byte ¡ boundary. Compressing ¡by ¡using ¡a ¡ small ¡alphabet: ¡ e.g. ¡use ¡2-­‑bit ¡code ¡for ¡ nucleo8des ¡{A, ¡C, ¡T, ¡G} ¡ Mode ¡0: ¡output ¡as ¡ASCII ¡text ¡ Mode ¡1: ¡output ¡four ¡32-­‑bit ¡ints ¡ Mode ¡2: ¡output ¡variable ¡length ¡bit ¡fields ¡

slide-19
SLIDE 19

Method ¡2: ¡Long ¡sequences ¡ ¡

  • Run ¡Length ¡Encoding ¡(RLE) ¡

– Exploits ¡simple ¡form ¡of ¡redundancy ¡ – Long ¡runs ¡of ¡same ¡bit ¡value ¡

¡ ¡ ¡ ¡0 ¡0 ¡0 ¡0 ¡0 ¡0 ¡0 ¡0 ¡1 ¡1 ¡1 ¡1 ¡1 ¡0 ¡0 ¡0 ¡0 ¡0 ¡0 ¡0 ¡0 ¡0 ¡1 ¡1 ¡1 ¡1 ¡1 ¡1 ¡1 ¡1 ¡1 ¡1 ¡

– Store ¡the ¡count ¡using ¡8-­‑bits ¡(0-­‑255) ¡ – Alternate ¡between ¡0 ¡and ¡1 ¡ – If ¡>255, ¡put ¡run ¡of ¡length ¡0 ¡of ¡other ¡bit, ¡con7nue ¡ ¡

19 ¡

8 ¡ 5 ¡ 9 ¡ 10 ¡

slide-20
SLIDE 20

20 ¡

public ¡class ¡RunLength ¡ ¡ { ¡ ¡ ¡ ¡ ¡private ¡static ¡final ¡int ¡R ¡ ¡ ¡= ¡256; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Maximum ¡run-­‑length ¡count ¡ ¡ ¡ ¡ ¡private ¡static ¡final ¡int ¡lgR ¡= ¡8; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Number ¡of ¡bits ¡per ¡count ¡ ¡ ¡ ¡ ¡ ¡public ¡static ¡void ¡compress() ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡char ¡run ¡= ¡0; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡boolean ¡old ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Start ¡out ¡with ¡0-­‑bit ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(!BinaryStdIn.isEmpty()) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡boolean ¡b ¡= ¡BinaryStdIn.readBoolean(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(b ¡!= ¡old) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Did ¡the ¡bit ¡value ¡change? ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(run, ¡lgR); ¡ ¡ ¡ ¡ ¡ ¡// ¡Write ¡out ¡the ¡count ¡for ¡completed ¡run ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡run ¡= ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡old ¡= ¡!old; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(run ¡== ¡R-­‑1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡We ¡have ¡reached ¡255, ¡time ¡to ¡output ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(run, ¡lgR); ¡ ¡// ¡Write ¡run ¡of ¡255 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡run ¡= ¡0; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(run, ¡lgR); ¡ ¡// ¡Write ¡a ¡run ¡of ¡0 ¡of ¡the ¡other ¡bit ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡run++; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(run, ¡lgR); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.close(); ¡ ¡ ¡ ¡ ¡} ¡

slide-21
SLIDE 21

21 ¡

¡ ¡ ¡ ¡public ¡static ¡void ¡expand() ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡boolean ¡b ¡= ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(!BinaryStdIn.isEmpty()) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡run ¡= ¡BinaryStdIn.readInt(lgR); ¡// ¡Read ¡8-­‑bit ¡count ¡from ¡stdin ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡< ¡run; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(b); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Write ¡1-­‑bit ¡to ¡stdout ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡b ¡= ¡!b; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.close(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Pads ¡0s ¡to ¡get ¡to ¡byte ¡boundary ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String[] ¡args) ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡ ¡ ¡ ¡ ¡ ¡(args[0].equals("-­‑")) ¡compress(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡if ¡(args[0].equals("+")) ¡expand(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡throw ¡new ¡RuntimeException("Illegal ¡command ¡line ¡argument"); ¡ ¡ ¡ ¡ ¡} ¡ } ¡ ¡ ¡ ¡

slide-22
SLIDE 22

22 ¡

% ¡java ¡BinaryDump ¡32 ¡< ¡q32x48.bin ¡ 00000000000000000000000000000000 ¡ 00000000000000000000000000000000 ¡ 00000000000000011111110000000000 ¡ 00000000000011111111111111100000 ¡ 00000000001111000011111111100000 ¡ 00000000111100000000011111100000 ¡ 00000001110000000000001111100000 ¡ 00000011110000000000001111100000 ¡ 00000111100000000000001111100000 ¡ 00001111000000000000001111100000 ¡ 00001111000000000000001111100000 ¡ 00011110000000000000001111100000 ¡ 00011110000000000000001111100000 ¡ 00111110000000000000001111100000 ¡ 00111110000000000000001111100000 ¡ 00111110000000000000001111100000 ¡ 00111110000000000000001111100000 ¡ 00111110000000000000001111100000 ¡ 00111110000000000000001111100000 ¡ 00111110000000000000001111100000 ¡ 00111110000000000000001111100000 ¡ 00111111000000000000001111100000 ¡ 00111111000000000000001111100000 ¡ 00011111100000000000001111100000 ¡ 00011111100000000000001111100000 ¡ 00001111110000000000001111100000 ¡ 00001111111000000000001111100000 ¡ 00000111111100000000001111100000 ¡ 00000011111111000000011111100000 ¡ 00000001111111111111111111100000 ¡ 00000000011111111111001111100000 ¡ 00000000000011111000001111100000 ¡ 00000000000000000000001111100000 ¡ 00000000000000000000001111100000 ¡ 00000000000000000000001111100000 ¡ 00000000000000000000001111100000 ¡ 00000000000000000000001111100000 ¡ 00000000000000000000001111100000 ¡ 00000000000000000000001111100000 ¡ 00000000000000000000001111100000 ¡ 00000000000000000000001111100000 ¡ 00000000000000000000001111100000 ¡ 00000000000000000000001111100000 ¡ 00000000000000000000011111110000 ¡ 00000000000000000011111111111100 ¡ 00000000000000000111111111111110 ¡ 00000000000000000000000000000000 ¡ 00000000000000000000000000000000 ¡ 1536 ¡bits ¡

% ¡java ¡RunLength ¡-­‑ ¡< ¡q32x48.bin ¡| ¡java ¡ BinaryDump ¡8 ¡ 01001111 ¡ 00000111 ¡ 00010110 ¡ 00001111 ¡ 00001111 ¡ 00000100 ¡ 00000100 ¡ 00001001 ¡ 00001101 ¡ 00000100 ¡ ... ¡ 1144 ¡bits ¡ bit-­‑0 ¡run ¡= ¡79 ¡(01001111) ¡ ¡ ¡ bit-­‑1 ¡run ¡= ¡7 ¡ ¡(00000111) ¡ bit-­‑0 ¡run ¡= ¡22 ¡(00010110) ¡ bit-­‑1 ¡run ¡= ¡15 ¡(00001111) ¡ ... ¡ Compression ¡ratio: ¡ 1144 ¡/ ¡1536 ¡= ¡74% ¡

slide-23
SLIDE 23

23 ¡

ABCDEFGHIJKLMNOPQRSTUVWXYZABABABABABABBABABBBABABABBA BBABABABABBBBBBABABABABABAABABABBABBABABABBBABABABABA BABABABAABABABBABABABABABBABAABBABABABABAABABBBABABAB ABABAAABABABABABAABABABABABBBABABABABAAABABABABABBABA BABABABABABAAABABAABBABABAAABBBABABBABAAAAABABABABAAA BABABABABABABABBBABABABABAAABAABABABABABBABABABABABAB ABAAABABABABABABABABABABABABABABABABABABBABABBBABABAB ABABABABBBABABAAAAAAABAAAAAAABAABABABAAAAAABABBABABAB BABABAAABABBBBBBBBBAABABABBBBBBBBBBABABABABABABABBBAB ABABABABABABABABABABBBABABABBABBBABBBABABABABBABABABB ABABABABAAABAABABABABABBABBABBAABBBABABBBAABABABBAABA BABBABABAABABABABABABABAAABABABABABABABABABABABABABAB ABABABABABABBABABBBABABABABABABABBBABBABAAAAAAABBAAAB ABABABABABABABABABABABABABABABBBABABABBABABBAABAABABA ABABABABABAABABBABABABABABABAABABABABABABABABABABBABA ABABBBBABABABBABBBABABBABABAAAABBAABBABABBABABABABABB ABBABBBBABAABABBABABABABAABBBABABABABAAABABABABABBABA ABAABABABABABABABAAABABABABABABBBABABAAABABAAAABBABBA ¡ Exploi8ng ¡small ¡alphabet: ¡ 55 ¡columns ¡x ¡18 ¡rows ¡= ¡990 ¡characters ¡ 26 ¡possible ¡leJers, ¡A-­‑Z ¡ 26 ¡< ¡25 ¡= ¡32 ¡ 5 ¡bits/character ¡* ¡990 ¡characters ¡= ¡4950 ¡bits ¡ Huffman ¡coding: ¡ Key ¡idea: ¡ ¡Use ¡shorter ¡codewords ¡for ¡common ¡characters ¡

¡

Different ¡number ¡of ¡bits ¡used ¡to ¡encode ¡different ¡characters ¡

slide-24
SLIDE 24

Scheme ¡2: ¡29 ¡bits ¡ Scheme ¡1: ¡30 ¡bits ¡

Method ¡3: ¡Frequency ¡of ¡characters ¡

  • Variable-­‑length ¡prefix-­‑free ¡codes ¡

– Map ¡from ¡characters ¡to ¡bit ¡strings ¡(codewords) ¡ – Choose ¡codewords ¡so ¡none ¡is ¡a ¡prefix ¡of ¡another ¡

24 ¡

ABRACADABRA! ¡

Text ¡to ¡be ¡ compressed

key ¡ value ¡ ! ¡ 101 ¡ A ¡ 0 ¡ B ¡ 1111 ¡ C ¡ 110 ¡ D ¡ 100 ¡ R ¡ 1110 ¡ 011111110011001000111111100101 ¡ A ¡ ¡ ¡B ¡ ¡ ¡RA ¡ ¡CA ¡ ¡DA ¡ ¡ ¡B ¡ ¡ ¡RA ¡ ¡! ¡ key ¡ value ¡ ! ¡ 101 ¡ A ¡ 11 ¡ B ¡ 00 ¡ C ¡ 010 ¡ D ¡ 100 ¡ R ¡ 011 ¡ 11000111101011100110001111101 ¡ ¡A ¡B ¡ ¡R ¡A ¡ ¡C ¡A ¡ ¡D ¡A ¡B ¡ ¡R ¡A ¡ ¡! ¡

slide-25
SLIDE 25

Trie ¡representa7on ¡

  • Represent ¡prefix-­‑free ¡code ¡as ¡a ¡binary ¡trie ¡

– Characters ¡at ¡leafs ¡ – Codeword ¡is ¡path ¡from ¡root ¡to ¡leaf ¡ – 0 ¡= ¡leb, ¡1 ¡= ¡right ¡

25 ¡

key ¡ value ¡ ! ¡ 101 ¡ A ¡ 11 ¡ B ¡ 00 ¡ C ¡ 010 ¡ D ¡ 100 ¡ R ¡ 011 ¡

11000111101011100110001111101 ¡ ¡A ¡B ¡ ¡R ¡A ¡ ¡C ¡A ¡ ¡D ¡A ¡B ¡ ¡R ¡A ¡ ¡! ¡ 29 ¡bits ¡ B ¡ C ¡ R ¡ ! ¡ D ¡ A ¡ 1 1 1 1 1

slide-26
SLIDE 26

Using ¡the ¡trie ¡

  • Compression ¡

– Start ¡at ¡leaf ¡of ¡target ¡character ¡ – Follow ¡path ¡to ¡root, ¡print ¡bits ¡in ¡ reverse ¡order ¡ – …or ¡create ¡a ¡symbol ¡table ¡

26 ¡

key ¡ value ¡ ! ¡ 101 ¡ A ¡ 11 ¡ B ¡ 00 ¡ C ¡ 010 ¡ D ¡ 100 ¡ R ¡ 011 ¡

11000111101011100110001111101 ¡ ¡A ¡B ¡ ¡R ¡A ¡ ¡C ¡A ¡ ¡D ¡A ¡B ¡ ¡R ¡A ¡ ¡! ¡ 29 ¡bits ¡

  • Expansion ¡

– Start ¡at ¡root ¡ – Go ¡leb ¡if ¡bit ¡= ¡0, ¡go ¡right ¡if ¡bit ¡= ¡1 ¡ – If ¡leaf ¡node, ¡output ¡character ¡and ¡ return ¡to ¡root ¡

B ¡ C ¡ R ¡ ! ¡ D ¡ A ¡ 1 1 1 1 1

slide-27
SLIDE 27

private ¡static ¡class ¡Node ¡implements ¡Comparable<Node> ¡ ¡ { ¡ ¡ ¡ ¡private ¡final ¡char ¡ch; ¡ ¡ ¡ ¡private ¡final ¡int ¡freq; ¡ ¡ ¡ ¡private ¡final ¡Node ¡left, ¡right; ¡ ¡ ¡ ¡ ¡// ¡Initialize ¡a ¡new ¡Node ¡ ¡ ¡ ¡Node(char ¡ch, ¡int ¡freq, ¡Node ¡left, ¡Node ¡right) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡this.ch ¡ ¡ ¡ ¡= ¡ch; ¡ ¡ ¡ ¡ ¡ ¡ ¡this.freq ¡ ¡= ¡freq; ¡ ¡ ¡ ¡ ¡ ¡ ¡this.left ¡ ¡= ¡left; ¡ ¡ ¡ ¡ ¡ ¡ ¡this.right ¡= ¡right; ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡// ¡Is ¡this ¡node ¡a ¡leaf? ¡ ¡ ¡ ¡private ¡boolean ¡isLeaf() ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡(left ¡== ¡null ¡&& ¡right ¡== ¡null); ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡// ¡Compare ¡Nodes ¡by ¡frequency ¡ ¡ ¡ ¡public ¡int ¡compareTo(Node ¡that) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡this.freq ¡-­‑ ¡that.freq; ¡ ¡ ¡ ¡} ¡ } ¡

27 ¡

slide-28
SLIDE 28

public ¡static ¡void ¡expand() ¡ ¡ { ¡ ¡ ¡ ¡// ¡Read ¡in ¡the ¡encoding ¡trie ¡ ¡ ¡ ¡Node ¡root ¡= ¡readTrie(); ¡ ¡ ¡ ¡ ¡ ¡// ¡Number ¡of ¡bytes ¡to ¡write ¡ ¡ ¡ ¡int ¡length ¡= ¡BinaryStdIn.readInt(); ¡ ¡ ¡ ¡ ¡// ¡Decode ¡using ¡the ¡Huffman ¡trie ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡< ¡length; ¡i++) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡Node ¡x ¡= ¡root; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Expand ¡codeword ¡for ¡the ¡ith ¡character ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(!x.isLeaf()) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡boolean ¡bit ¡= ¡BinaryStdIn.readBoolean(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(bit) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡x ¡= ¡x.right; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡x ¡= ¡x.left; ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(x.ch); ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡BinaryStdOut.flush(); ¡ } ¡

Expansion ¡

28 ¡

  • Expansion ¡

– Start ¡at ¡root ¡ – Go ¡leb ¡if ¡bit=0, ¡go ¡right ¡if ¡bit=1 ¡ – If ¡leaf ¡node, ¡output ¡character ¡ and ¡return ¡to ¡root ¡ B ¡ C ¡ R ¡ ! ¡ D ¡ A ¡ 1 1 1 1 1

slide-29
SLIDE 29

TransmiPng ¡the ¡trie ¡

  • Trie ¡needed ¡in ¡order ¡to ¡expand ¡

– Must ¡be ¡sent ¡along ¡with ¡the ¡data ¡ ¡

  • Causes ¡overhead, ¡but ¡small ¡if ¡message ¡is ¡long ¡

– Write ¡preorder ¡traversal ¡of ¡trie ¡ – Mark ¡leaf ¡and ¡internal ¡nodes ¡with ¡a ¡bit ¡

29 ¡

private ¡static ¡void ¡writeTrie(Node ¡x) ¡ ¡ { ¡ ¡ ¡ ¡if ¡(x.isLeaf()) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(true); ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryStdOut.write(x.ch); ¡ ¡ ¡ ¡ ¡ ¡ ¡return; ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡BinaryStdOut.write(false); ¡ ¡ ¡ ¡writeTrie(x.left); ¡ ¡ ¡ ¡writeTrie(x.right); ¡ } ¡

00101000010010100001110101001000101000100100001010101000001 ¡ ¡ ¡ ¡B ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡C ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡R ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡D ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡! ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡A ¡ B ¡ C ¡ R ¡ ! ¡ D ¡ A ¡

slide-30
SLIDE 30

Reading ¡the ¡trie ¡

  • Reconstruc7ng ¡from ¡preorder ¡traversal ¡

– Use ¡0/1 ¡bits ¡to ¡decide ¡if ¡internal ¡or ¡leaf ¡node ¡

30 ¡

private ¡static ¡Node ¡readTrie() ¡ ¡ { ¡ ¡ ¡ ¡boolean ¡isLeaf ¡= ¡BinaryStdIn.readBoolean(); ¡ ¡ ¡ ¡if ¡(isLeaf) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡Node(BinaryStdIn.readChar(), ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡-­‑1, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡null, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡null); ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡Node('\0', ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡-­‑1, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡readTrie(), ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡readTrie()); ¡ ¡ ¡ ¡} ¡ } ¡

00101000010010100001110101001000101000100100001010101000001 ¡ ¡ ¡ ¡B ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡C ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡R ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡D ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡! ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡A ¡ B ¡ C ¡ R ¡ ! ¡ D ¡ A ¡

slide-31
SLIDE 31

Building ¡the ¡trie ¡

  • Can ¡we ¡always ¡find ¡op7mal ¡prefix-­‑free ¡code? ¡

– Yes! ¡ ¡ ¡ – Discovered ¡by ¡David ¡Huffman ¡while ¡a ¡PhD ¡student ¡

  • Huffman ¡algorithm: ¡

– Count ¡frequency ¡of ¡each ¡char ¡in ¡input ¡ – Create ¡forest ¡of ¡leaf ¡nodes ¡for ¡each ¡char ¡

  • Each ¡leaf ¡weighted ¡according ¡to ¡its ¡frequency ¡

– Repeat ¡un7l ¡single ¡trie: ¡

  • Select ¡2 ¡tries ¡with ¡min ¡sum ¡of ¡weights ¡
  • Merge ¡into ¡trie ¡with ¡sum ¡of ¡weights ¡

– Provably ¡op7mal ¡

31 ¡

This ¡trie ¡isn't ¡the ¡

  • p7mal ¡one ¡for ¡

ABRACADBRA!

slide-32
SLIDE 32

ABRACADABRA! ¡

R ¡ 2 ¡ B ¡ 2 ¡ C ¡ 1 ¡ A ¡ 5 ¡ D ¡ 1 ¡ ! ¡ 1 ¡

1) ¡Create ¡leafs ¡with ¡ weight ¡= ¡frequency ¡ in ¡en7re ¡text ¡

2 ¡

2) ¡Join ¡two ¡with ¡sum ¡= ¡2 ¡

(in ¡a ¡7e, ¡exact ¡choice ¡arbitrary) ¡

3 ¡

3) ¡Join ¡two ¡with ¡sum ¡= ¡3 ¡

4 ¡

4) ¡Join ¡two ¡with ¡sum ¡= ¡4 ¡

7 ¡

12 ¡ 5) ¡Join ¡two ¡with ¡sum ¡= ¡7 ¡ 6) ¡Join ¡last ¡two ¡tries ¡

0 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 100 ¡ 101 ¡ 110 ¡ 1110 ¡ 1111 ¡

32 ¡

Codewords: ¡

slide-33
SLIDE 33

ABRACADABRA! ¡

% ¡java ¡Huffman ¡-­‑ ¡< ¡abra.txt ¡| ¡java ¡ BinaryDump ¡32 ¡ 01010000010010100010001001000011 ¡ 01000011010101001010100001000000 ¡ 00000000000000000000000110001111 ¡ 100101101000111110010100 ¡ 120 ¡bits ¡ 01 ¡01000001 ¡ ¡ ¡65 ¡= ¡A ¡ 001 ¡01000100 ¡ ¡ ¡68 ¡= ¡D ¡ 01 ¡00100001 ¡ ¡ ¡33 ¡= ¡! ¡ 1 ¡01000011 ¡ ¡ ¡67 ¡= ¡C ¡ 01 ¡01010010 ¡ ¡ ¡82 ¡= ¡R ¡ 1 ¡01000010 ¡ ¡ ¡66 ¡= ¡B ¡ ¡ ¡00000000 ¡ ¡ ¡int ¡= ¡12 ¡ ¡00000000 ¡ ¡00000000 ¡ ¡00001100 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡A ¡ ¡ 111 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡B ¡ ¡ 110 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡R ¡ 0 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡A ¡ 1011 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡C ¡ 0 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡A ¡ 100 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡D ¡ 0 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡A ¡ 111 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡B ¡ 110 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡R ¡ 0 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡A ¡ 1010 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡! ¡ 0 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡padding ¡

trie ¡ size ¡of ¡ text ¡ compressed ¡ data ¡

33 ¡

R ¡ B ¡ C ¡ A D ! ¡ 0 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 100 ¡ 101 ¡ 110 ¡ 1110 ¡ 1111 ¡

33 ¡

slide-34
SLIDE 34

Summary ¡

  • Lossless ¡compression ¡

– Universal ¡compression: ¡impossible ¡ – Op7mal ¡data ¡compression: ¡undecidable ¡

  • Exploi7ng: ¡

– Small ¡alphabets ¡

  • Use ¡only ¡as ¡many ¡bits ¡as ¡needed ¡to ¡represent ¡data ¡

– Repeated ¡symbols ¡

  • Run ¡length ¡encoding ¡(RLE) ¡

– Frequency ¡of ¡symbols ¡

  • Prefix-­‑free ¡codes ¡
  • Huffman ¡coding ¡

34 ¡