1 / 45

FDoDi201

Learn about Huffman coding, a greedy algorithm used for data compression. Explore the concepts of bits, trees, and codes, and understand how to construct a Huffman tree. Discover the benefits of data compression and its applications in storage and data transmission.

ruthc
Download Presentation

FDoDi201

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FDoDi201 • Huffman Coding/Compression • Overview of compress: bits, trees, codes, bits • Overview of uncompress: bits, trees, bits • From algorithms to data structures to programs • What are the essential parts of Huff assignment? • Toward understanding graphs • One more Priority Queue application, basis for so many important algorithms

  2. Huffman Coding • Understand Huffman Coding • Data compression • Priority Queue • Bits and Bytes • Greedy Algorithm • Many compression algorithms • Huffman is optimal, per-character compression • Still used, e.g., basis of Burrows-Wheeler • Other compression 'better', sometimes slower? • LZW, GZIP, BW, …

  3. Compression and Coding • What gets compressed? • Save on storage, why is this a good idea? • Save on data transmission, how and why? • What is information, how is it compressible? • Exploit redundancy, without that, hard to compress • Represent information: code (Morse cf. Huffman) • Dots and dashes or 0's and 1's • How to construct code?

  4. PQ Application: Data Compression • Compression is a high-profile application • .zip, .mp3, .jpg, .gif, .gz, … • What property of MP3 was a significant factor in what made Napster work (why did Napster ultimately fail?) • Why do we care? • Secondary storage capacity doubles every year • Disk space fills up there is more data to compress than ever before • Ever need to stop worrying about storage?

  5. More on Compression • Different compression techniques • .mp3 files and .zip files? • .gif and .jpg? • Impossible to compress/lossless everything: Why? • Lossy methods • pictures, video, and audio (JPEG, MPEG, etc.) • Lossless methods • Run-length encoding, Huffman 11 3 5 3 2 6 2 6 5 3 5 3 5 3 10

  6. Coding/Compression/Concepts • For ASCII we use 8 bits, for Unicode 16 bits • Minimum number of bits to represent N values? • Representation of genomic data (a, c ,g, t)? • What about noisy genomic data? • We can use a variable-length encoding, e.g., Huffman • How do we decide on lengths? How do we decode? • Values for Morse code encodings, why? • … - - - …

  7. Huffman Coding • D.A Huffman in early 1950’s: story of invention • Analyze and process data before compression • Not developed to compress data “on-the-fly” • Represent data using variable length codes • Each letter/chunk assigned a codeword/bitstring • Codeword for letter/chunk is produced by traversing the Huffman tree • Property: No codeword produced is the prefix of another • Frequent letters/chunk have short encoding, while those that appear rarely have longer ones • Huffman coding is optimal per-character coding method

  8. Mary Shaw • Software engineering and software architecture • Tools for constructing large software systems • Development is a small piece of total cost, maintenance is larger, depends on well-designed and developed techniques • Interested in computer science, programming, curricula, and canoeing, health-care costs • ACM Fellow, Alan Perlis Professor of Compsci at CMU

  9. g o h e p r s * 1 2 2 3 p e h r s * 1 1 1 1 1 2 6 g o 4 3 3 2 2 p e h r 1 1 1 1 Huffman coding: go go gophers ASCII 3 bits g 103 1100111 000 ?? o 111 1101111 001 ?? p 112 1110000 010 h 104 1101000 011 e 101 1100101 100 r 114 1110010 101 s 115 1110011 110 sp. 32 1000000 111 • choose two smallest weights • combine nodes + weights • Repeat • Priority queue? • Encoding uses tree: • 0 left/1 right • How many bits? 3 3 1 1 1 1 2

  10. 13 7 3 3 s s * * 1 1 2 2 6 6 g g o o 4 4 3 3 3 3 2 2 2 2 p p e e h h r r 1 1 1 1 1 1 1 1 Huffman coding: go go gophers ASCII 3 bits g 103 1100111 000 00 o 111 1101111 001 01 p 112 1110000 010 1100 h 104 1101000 011 1101 e 101 1100101 100 1110 r 114 1110010 101 1111 s 115 1110011 110 100 sp. 32 1000000 111 101 • Encoding uses tree/trie: • 0 left/1 right • How many bits? 37!! • Savings? Worth it?

  11. Building a Huffman tree • Begin with a forest of single-node trees/tries (leaves) • Each node/tree/leaf is weighted with character count • Node stores two values: character and count • Repeat until there is only one node left: root of tree • Remove two minimally weighted trees from forest • Create new tree/internal node with minimal trees as children, • Weight is sum of children’s weight (no char) • How does process terminate? Finding minimum? • Remove minimal trees, hummm……

  12. How do we create Huffman Tree/Trie? • Insert weighted values into priority queue • What are initial weights? Why? • Remove minimal nodes, weight by sums, re-insert • Total number of nodes? PriorityQueue<TreeNode> pq = new PriorityQueue<>(); for(int k=0; k < freq.length; k++){ pq.add(new TreeNode(k,freq[k],null,null)); } while (pq.size() > 1){ TreeNode left = pq.remove(); TreeNode right = pq.remove(); pq.add(new TreeNode(0,left.weight+right.weight, left,right)); } TreeNode root = pq.remove();

  13. Greedy Algorithms Reviewed • A greedy algorithm makes a locally optimal decision that leads to a globally optimal solution • Huffman: choose minimal weight nodes, combine • Leads to optimal coding, optimal Huffman tree • Making change with American coins: choose largest coin possible as many times as possible • Change for $0.63, change for $0.32 • What if we're out of nickels, change for $0.32? • Greedy doesn't always work, but it does sometimes! • Typically prove it works 

  14. Creating compressed file • Once we have new encodings, read every character • Write encoding, not the character, to compressed file • Why does this save bits? • What other information needed in compressed file? • How do we uncompress? • How do we know foo.hf represents compressed file? • Is suffix sufficient? Alternatives? • Why is Huffman coding a two-pass method? • Alternatives?

  15. Uncompression with Huffman • We need the trie to uncompress • 000100100010011001101111 • As we read a bit, what do we do? • Go left on 0, go right on 1 • When do we stop? What to do? • How do we get the trie? • How did we get it originally? Store 256 int/counts • How do we read counts? • How do we store a trie? Tree APT relevance? • Reading a trie? Leaf indicator? Node values?

  16. Other Huffman Issues • What do we need to decode? • How did we encode? How will we decode? • What information needed for decoding? • Reading and writing bits: chunks and stopping • Can you write 3 bits? Why not? Why? • PSEUDO_EOF • BitInputStream and BitOutputStream: API • What should happen when the file won’t compress? • Silently compress bigger? Warn user? Alternatives?

  17. WOTO http://bit.ly/201fall16-dec2 • Understanding parts of Huffman, code live afternoon 12/2

  18. Huffman Complexities • How do we measure? Size of input file, size of alphabet • Which is typically bigger? • Accumulating character counts: ______ • How can we do this in O(1) time, though not really • Building the heap/priority queue from counts ____ • Does priority queue really matter? Peformannce? • What will the "bottleneck" be in compressing data, think about drawbacks to Huffman coding • Why not used for streaming video?

  19. Anita Borg 1949-2003 • “Dr. Anita Borg tenaciously envisioned and set about to change the world for women and for technology. … she fought tirelessly for the development technology with positive social and human impact.” • “Anita Borg sought to revolutionize the world and the way we think about technology and its impact on our lives.” • http://www.youtube.com/watch?v=1yPxd5jqz_Q

  20. Graphs, the Internet, and Everything http://www.caida.org/

  21. Graphs: Structures and Algorithms • Packets of bits/information routed on the internet • Message divided into packets client-side • Packets routed toward destination over internet • Packets may take different routes to destination • What happens if packets lost or arrive out-of-order? • Routing tables store local information, not global • Why not global info? • Criteria used in routing? • Googlemap, mapquest, Garmin, Applemap, … • How do you get from here to there? • What's a route? How is this known?

  22. Graphs: Structures and Algorithms • What about The Oracle of Bacon, Erdos Numbers, and Word Ladders? • All can be modeled using graphs • What kind of connectivity does each concept model? • Graphs are everywhere in the world (of algorithms?) • What is a graph? Algorithms on graphs? • Graph representation?

  23. Old and New: Fan Chung Graham • Graphs are everywhere • http://math.ucsd.edu/~fan/graphs/gallery/

  24. Sir Tim Berners-Lee I want you to realize that, if you can imagine a computer doing something, you can program a computer to do that. Unbounded opportunity... limited only by your imagination. And a couple of laws of physics. • TCP/IP, HTTP • How, Why, What, When?

  25. NYC Phil LAX LGA ORD Boston Wash DC DCA Vocabulary • Graphs are collections of vertices and edges (vertex also called node) • Edge connects two vertices • Direction can be important, directed edge, directed graph • Edge may have associated weight/cost • A vertex sequence v0, v1, …, vn-1 is a path where vk and vk+1 are connected by an edge. • If some vertex is repeated, the path is a cycle • A graph is connected if there is a path between any pair of vertices 78 268 204 190 394 $441 $186 $412 $1701 $186

  26. Graph questions/algorithms • What vertices are reachable from a given vertex? • Two standard traversals: depth-first, breadth-first • connected components, groups of connected vertices • Shortest path between two vertices (weighted graphs?) • BFS works, possibly uses more storage than DFS • Dijkstra’s algorithm efficient, uses a priority queue! • Longest path in a graph • No known efficient algorithm • Visit all vertices without repeating? Visit all edges? • With minimal cost? Hard!

  27. Depth, Breadth, other traversals • We want to visit every vertex that can be reached from a specific starting vertex (we might try all starting vertices) • Make sure we don't visit a vertex more than once • Why isn't this an issue in trees? • Mark vertex as visited, use set/array/map for this • Order in which vertices visited can be important • Storage/runtime efficiency of traversals important • What other data structures do we have: stack, queue, … • What if we traverse using priority queue?

  28. Breadth first search • In an unweighted graph this finds the shortest path between a start vertex and every vertex • Visit every node one away from start • Visit every node two away from start • This is nodes one away from a node one away • Visit every node three away from start, … • Put vertex on queue to start (initially just one) • Repeat: dequeue vertex, enqueue adjacent vertices • Avoid enqueueing already visited/queued nodes • When are 1-away vertices enqueued? 2-away? N? • How many vertices on queue?

  29. 1 7 4 2 6 3 5 Code for breadth first public void breadth(String vertex){ Set<String> visited = new TreeSet<String>(); Queue<String> q = new LinkedList<String>(); q.add(vertex); visited.add(vertex); while (q.size() > 0) { String current = q.remove(); // process current for(each v adjacent to current){ if (!visited.contains(v)){// not visited visited.add(v); q.add(v); } } } }

  30. 1 7 4 2 6 3 5 Pseudo-code for depth-first search void depthfirst(String vertex){ if (! alreadySeen(vertex)){ markAsSeen(vertex); System.out.println(vertex); for(each v adjacent to vertex) { depthfirst(v); } } } • Clones are stacked up, problem? Can we make use of stack explicit?

  31. BFS compared to DFS public Set<String> bfs(String start){ Set<String> visited = new TreeSetString>(); Queue<String> qu = new LinkedList<String>(); visited.add(start); qu.add(start); while (qu.size() > 0){ String v = qu.remove(); for(String adj : myGraph.getAdjacent(v)){ if (! visited.contains(adj)) { visited.add(adj); qu.add(adj); } } } return visited; }

  32. BFS becomes DFS public Set<String> dfs(String start){ Set<String> visited = new TreeSet<String>(); Queue<String> qu = new LinkedList<String>(); visited.add(start); qu.add(start); while (qu.size() > 0){ String v = qu.remove(); for(String adj : myGraph.getAdjacent(v)){ if (! visited.contains(adj)) { visited.add(adj); qu.add(adj); } } } return visited; }

  33. DFS arrives public Set<String> dfs(String start){ Set<String> visited = new TreeSet<String>(); Stack<String> qu = new Stack<String>(); visited.add(start); qu.push(start); while (qu.size() > 0){ String v = qu.pop(); for(String adj : myGraph.getAdjacent(v)){ if (! visited.contains(adj)) { visited.add(adj); qu.push(adj); } } } return visited; }

  34. What is the Internet? • The Internet was originally designed as an "overlay" network running on top of existing phone and other networks. It is based on a small set of software protocols that direct routers inside the network to forward data from source to destination, while applications run on the Internet to rapidly scale into a critical global service. However, this success now makes it difficult to create and test new ways of protecting it from abuses, or from implementing innovative applications and services. http://www.intel.com/labs/features/idf09041.htm

  35. How does the Internet work? • Differences between the Internet and phone networks • Dedicated circuits/routes • Distributed, end-to-end • Where is the intelligence? • Not in the network, per se, in the design and the ends • End-to-end Arguments in System Design • Success of email, web, etc., relies on not building intelligence into the network • What about overlay networks? • What about PlanetLab?

  36. Jon Kleinberg • 2005 MacArthur Fellow, 2008 Infosys Award, 2008 Discover “20 Best Brains under 40” • Networks course and book • Theory + Practice = Knowledge • "....Try to keep an open mind about topics and areas going on....It's much easier to make progress on a problem when you are enjoying what you are doing. In addition to finding work that is important, find work that has some personal interest for you....I've benefited from a lot of mentoring throughout my career. I think it's important to pass it on to the next generation and work in a mentoring capacity or a teaching capacity with people entering the field....” ACM Infosys Interview

  37. Graph implementations • Typical operations on graph: • Add vertex • Add edge (parameters?) • getAdjacent(vertex) • getVertices(..) • String->Vertex (vice versa) • Different kinds of graphs • Lots of vertices, few edges, sparse graph • Use adjacency list • Lots of edges (max # ?) dense graph • Use adjacency matrix Adjacency list

  38. Graph implementations (continued) • Adjacency matrix • Every possible edge represented, how many? • Adjacency list uses O(V+E) space • What about matrix? • Which is better? • What do we do to get adjacent vertices for given vertex? • What is complexity? • Compared to adjacency list? • What about weighted edges? … T F

  39. C 3 E 4 A 6 8 2 D 3 4 F B 2 Shortest path in weighted graph • We need to modify approach slightly for weighted graph • Edges have weights, breadth first doesn’t work • What’s shortest path from A to F in graph below? • Use same idea as breadth first search • Don’t add 1 to current distance, add ??? • Might adjust distances more than once • What vertex do we visit next? • What vertex is next is key • Use greedy algorithm: closest • Huffman is greedy, …

  40. What about connected components? • What computers are reachable from this one? What people are reachable from me via acquaintanceship? • Start at some vertex, depth-first search (breadth?) • Mark nodes visited • Repeat from unvisited vertex until all visited • What is minimal size of a component? Maximal size? • What is complexity of algorithm in terms of V and E? • What algorithms does this lead to in graphs?

  41. Greedy Algorithms Reviewed • A greedy algorithm makes a locally optimal decision that leads to a globally optimal solution • Huffman: choose minimal weight nodes, combine • Leads to optimal coding, optimal Huffman tree • Making change with American coins: choose largest coin possible as many times as possible • Change for $0.63, change for $0.32 • What if we’re out of nickels, change for $0.32? • Greedy doesn't always work, but it does sometimes • Weighted shortest path algorithm is Dijkstra’s algorithm, greedy and uses priority queue

  42. Edsger Dijkstra • Turing Award, 1972 • Algol-60 programming language • Goto considered harmful • Shortest path algorithm • Structured programming “Program testing can show the presence of bugs, but never their absence” For me, the first challenge for computing science is to discover how to maintain order in a finite, but very large, discrete universe that is intricately intertwined. And a second, but not less important challenge is how to mould what you have achieved in solving the first problem, into a teachable discipline: it does not suffice to hone your own intellect (that will join you in your grave), you must teach others how to hone theirs. The more you concentrate on these two challenges, the clearer you will see that they are only two sides of the same coin: teaching yourself is discovering what is teachable EWD 709

  43. Dijkstra’s Shortest Path Algorithm • Similar to breadth first search, but uses a priority queue instead of a queue. Code below is for breadth first search (distance[] replaces set) Vertex cur = q.remove(); for(Vertex v : adjacent(cur)){ if (!visited.contains(v)){ // if distance[v] == INFINITY visited.add(v); // distance[v] = distance[cur]+1 q.add(v); } } • Dijkstra: Find minimal unvisited node, recalculate costs through node Vertex cur = pq.remove(); for(Vertex v : adjacent(cur)) if (distance[cur] + graph.weight(cur,v) < distance[v]) { distance[v] = distance[cur] + graph.weight(cur,v); pq.add(v); }

  44. 3 4 4 A A E E 7 7 2 2 S S A A B B C C E E 6 6 C C S S 0 7 2 6 8 3 3 7 7 7 0 6 2 5 2 2 B B Shortest paths, more details • Single-source shortest path • Start at some vertex S • Find shortest path to every reachable vertex from S • A set of vertices is processed • Initially just S is processed • Each pass processes a vertex After each pass, shortest path from S to any vertex using just vertices from processed set (except for last vertex) is always known • Next processed vertex is closest to S still needing processing 1 process B 9 0 7 2 5 3 1 process C

  45. Dijkstra’s algorithm works (greedily) • Choosing minimal unseen vertex to process leads to shortest paths Vertex cur = pq.remove(); for(Vertex v : adjacent(cur)) if (distance[cur]+graph.weight(cur,v) < distance[v]){ distance[v] = distance[cur] + graph.weight(cur,v); pq.add(v); } } • We always know shortest path through processed vertices • When we choose w, there can’t be a shorter path to w than distance[w] – it would go through processed u, we would have chosen u instead of w

More Related